using System; using System.Collections.Generic; using SharedLibrary; using System.Text; using System.Text.RegularExpressions; using System.Linq; using System.Collections.Specialized; namespace Webfront_Plugin { class Framework { private List activeServers; public Framework() { activeServers = new List(); } public void addServer(Server S) { activeServers.Add(S); } public void removeServer(Server S) { if (activeServers.Contains(S)) activeServers.Remove(S); } private String processTemplate(String Input, String Param) { try { Server requestedServer = null; int requestPageNum = 0; int ID = 0; String Query = ""; if (Param != null) { NameValueCollection querySet = System.Web.HttpUtility.ParseQueryString(Param); if (querySet["server"] != null) requestedServer = activeServers.Find(x => x.pID() == Int32.Parse(querySet["server"])); else requestedServer = activeServers.First(); if (querySet["page"] != null) requestPageNum = Int32.Parse(querySet["page"]); if (querySet["id"] != null) ID = Int32.Parse(querySet["id"]); if (querySet["query"] != null) Query = querySet["query"]; } String Pattern = @"\{\{.+\}\}"; Regex Search = new Regex(Pattern, RegexOptions.IgnoreCase); MatchCollection Matches = Search.Matches(Input); foreach (Match match in Matches) { Input = processReplacements(Input, match.Value, requestPageNum, ID, Query, requestedServer); } return Input; } catch (Exception E) { Page Error = new error(); return Error.Load().Replace("{{ERROR}}", E.Message); } } private String parsePagination(int totalItems, int itemsPerPage, int currentPage, String Page) { StringBuilder output = new StringBuilder(); output.Append("
"); if (currentPage > 0) output.AppendFormat("PREV", Page, currentPage - 1); double totalPages = Math.Ceiling(((float)totalItems / itemsPerPage)); output.Append("" + (currentPage + 1) + "/" + totalPages + ""); if ((currentPage + 1) < totalPages) output.AppendFormat("NEXT", Page, currentPage + 1); output.Append("
"); return output.ToString(); } private String processReplacements(String Input, String Macro, int curPage, int ID, String Query, params Server[] Servers) { bool Authenticated = false; bool UserPrivelege = false; if (Servers[0] != null && Manager.lastIP != null) { Player User = Servers[0].clientDB.getPlayer(Manager.lastIP.ToString()); if (User != null && User.Level > Player.Permission.Flagged) Authenticated = true; if (User != null && User.Level == Player.Permission.User) UserPrivelege = true; } if (Macro.Length < 5) return ""; String Looking = Macro.Substring(2, Macro.Length - 4); if (Looking == "SERVERS") { int cycleFix = 0; StringBuilder buffer = new StringBuilder(); foreach (Server S in activeServers) { StringBuilder players = new StringBuilder(); if (S.getClientNum() > 0) { int count = 0; double currentPlayers = S.statusPlayers.Count; players.Append(""); foreach (Player P in S.getPlayers()) { if (P == null) continue; if (count % 2 == 0) { switch (cycleFix) { case 0: players.Append(""); cycleFix = 1; break; case 1: players.Append(""); cycleFix = 0; break; } } players.AppendFormat("", P.databaseID, SharedLibrary.Utilities.nameHTMLFormatted(P)); if (count % 2 != 0) { players.Append(""); } count++; } players.Append("
{1}
"); } buffer.AppendFormat(@"
{0} {1} {2} {3} Stats Bans History
{5}", S.getName(), S.getMap(), S.getClientNum() + "/" + S.getMaxClients(), SharedLibrary.Utilities.gametypeLocalized(S.getGametype()), S.pID(), players.ToString()); if (S.getClientNum() > 0) { buffer.AppendFormat("
", S.pID(), '\"'); if (UserPrivelege || Authenticated) buffer.AppendFormat("
", S.pID(), '\"'); } buffer.Append("
"); } return Input.Replace(Macro, buffer.ToString()); } if(Looking == "CHAT") { StringBuilder chatMessages = new StringBuilder(); chatMessages.Append(""); if (Servers.Length > 0 && Servers[0] != null) { foreach (Chat Message in Servers[0].chatHistory) chatMessages.AppendFormat("", SharedLibrary.Utilities.nameHTMLFormatted(Message.Origin), Message.Message, Message.timeString()); } chatMessages.Append("
{0}{1}{2}
"); return chatMessages.ToString(); } if (Looking == "PLAYER") { StringBuilder buffer = new StringBuilder(); Server S = activeServers[0]; buffer.Append(""); List matchingPlayers = new List(); if (ID > 0) matchingPlayers.Add(S.clientDB.getPlayer(ID)); else if (Query.Length > 2) { matchingPlayers = S.clientDB.findPlayers(Query); if (matchingPlayers == null) matchingPlayers = new List(); List matchedDatabaseIDs = new List(); foreach (Aliases matchingAlias in S.aliasDB.findPlayers(Query)) matchedDatabaseIDs.Add(matchingAlias.Number); foreach (Player matchingP in S.clientDB.getPlayers(matchedDatabaseIDs)) { if (matchingPlayers.Find(x => x.databaseID == matchingP.databaseID) == null) matchingPlayers.Add(matchingP); } } if (matchingPlayers == null) buffer.Append("
NameAliasesIPRatingLevelConnectionsLast SeenProfile
"); else { foreach (Player Player in matchingPlayers) { if (Player == null) continue; buffer.Append(""); StringBuilder Names = new StringBuilder(); List nameAlias = new List(); List IPAlias = new List(); StringBuilder IPs = new StringBuilder(); if (Authenticated) { List allAlliases = S.getAliases(Player); foreach (Aliases A in allAlliases) { foreach (String Name in A.Names.Distinct()) nameAlias.Add(Name); foreach (String IP in A.IPS.Distinct()) IPAlias.Add(IP); } Names.Append("Show Aliases"); Names.Append("
"); foreach (String Name in nameAlias.Distinct()) Names.AppendFormat("{0}
", Utilities.stripColors(Name)); Names.Append("
"); IPs.Append("Show IPs"); IPs.Append("
"); foreach (String IP in IPAlias) IPs.AppendFormat("{0}
", IP); IPs.Append("
"); } if (!Authenticated) { Names.Append("Hidden"); IPs.Append("Hidden"); } Int64 forumID = 0; if (Player.npID.Length == 16) { forumID = Int64.Parse(Player.npID.Substring(0, 16), System.Globalization.NumberStyles.AllowHexSpecifier); forumID = forumID - 76561197960265728; } String Screenshot = String.Empty; //if (logged) Screenshot = String.Format("
", forumID, Player.Name); buffer.AppendFormat("{0}{10}{1}{2}{3}{4}{5}{6} ago{8}", Player.Name, Names, IPs, 0, SharedLibrary.Utilities.levelHTMLFormatted(Player.Level), Player.Connections, Player.getLastConnection(), forumID, Player.Name, "/player?id=" + Player.databaseID, Screenshot); buffer.Append(""); } buffer.Append(""); return Input.Replace(Macro, buffer.ToString()); } } if (Looking == "BANS") { StringBuilder buffer = new StringBuilder(); Server S = activeServers[0]; buffer.Append(""); int limitPerPage = 30; int Pagination = curPage; int totalBans = S.Bans.Count; int range; int start = Pagination * limitPerPage; int cycleFix = 0; if (totalBans <= limitPerPage) range = totalBans - 1; else if ((totalBans - start) < limitPerPage) range = (totalBans - start); else range = limitPerPage; List Bans = new List(); if (totalBans > 0) Bans = S.Bans.GetRange(start, range).OrderByDescending(x => x.When).ToList(); if (Bans.Count == 0) buffer.Append("No bans yet."); else { buffer.Append("

{{TIME}}


"); if (Bans[0] != null) buffer = buffer.Replace("{{TIME}}", "From " + SharedLibrary.Utilities.timePassed(Bans[0].When) + " ago" + " — " + totalBans + " total"); List npIDs = new List(); foreach (Ban B in Bans) npIDs.Add(B.npID); Player[] bannedPlayers = S.clientDB.getPlayers(npIDs).ToArray(); for (int i = 0; i < Bans.Count-1; i++) { if (Bans[i] == null) continue; Player P = bannedPlayers[i]; Player B; if (P.npID == Bans[i].bannedByID) B = new Player("IW4MAdmin", "", 0, SharedLibrary.Player.Permission.Banned, 0, "", 0, ""); else B = S.clientDB.getPlayer(Bans[i].bannedByID, -1); if (P == null) P = new Player("Unknown", "n/a", 0, 0, 0, "Unknown", 0, ""); if (B == null) B = new Player("Unknown", "n/a", 0, 0, 0, "Unknown", 0, ""); if (P.lastOffense == String.Empty) P.lastOffense = "Evade"; if (P != null && B != null) { String Prefix; if (cycleFix % 2 == 0) Prefix = "class=row-grey"; else Prefix = "class=row-white"; String Link = "/player?id=" + P.databaseID; buffer.AppendFormat("", P.Name, P.lastOffense, SharedLibrary.Utilities.nameHTMLFormatted(B), Bans[i].getWhen(), Prefix, Link); cycleFix++; } } } buffer.Append("
NameOffenseBanned ByTime
{0}{1}{2}{3}

"); if (totalBans > limitPerPage) buffer.Append(parsePagination(totalBans, limitPerPage, Pagination, "bans")); return Input.Replace(Macro, buffer.ToString()); } if (Looking == "GRAPH") { StringBuilder buffer = new StringBuilder(); buffer.Append("
"); buffer.Append(""); return Input.Replace(Macro, buffer.ToString()); } if (Looking == "TITLE") return Input.Replace(Macro, "IW4MAdmin by RaidMax"); if (Looking == "VERSION") return Input.Replace(Macro, "0.9.5"); return "PLACEHOLDER"; } public String processRequest(Kayak.Http.HttpRequestHead request) { Page requestedPage = new notfound(); Page Header = new header(); Page Footer = new footer(); if (request.Path == "/") requestedPage = new main(); else { string p = request.Path.ToLower().Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)[0]; switch (p) { case "bans": requestedPage = new bans(); break; case "player": requestedPage = new player(); break; case "graph": requestedPage = new graph(); return processTemplate(requestedPage.Load(), request.QueryString); case "chat": requestedPage = new chat(); return processTemplate(requestedPage.Load(), request.QueryString); case "error": requestedPage = new error(); break; default: requestedPage = new notfound(); break; } } return processTemplate(Header.Load(), null) + processTemplate(requestedPage.Load(), request.QueryString) + processTemplate(Footer.Load(), null); } } abstract class Page { public abstract String Load(); public abstract String Name { get; } protected String loadHTML() { IFile HTML = new IFile("webfront\\" + this.Name + ".html"); String Contents = HTML.getLines(); HTML.Close(); return Contents; } } class notfound : Page { public override String Name { get { return "notfound"; } } public override String Load() { return loadHTML(); } } class main : Page { public override String Name { get { return "main"; } } public override String Load() { return loadHTML(); } } class bans : Page { public override String Name { get { return "bans"; } } public override String Load() { return loadHTML(); } } class header : Page { public override String Name { get { return "header"; } } public override String Load() { return loadHTML(); } } class footer : Page { public override String Name { get { return "footer"; } } public override String Load() { return loadHTML(); } } class player : Page { public override String Name { get { return "player"; } } public override String Load() { return loadHTML(); } } class graph : Page { public override String Name { get { return "graph"; } } public override String Load() { return loadHTML(); } } class chat : Page { public override String Name { get { return "chat"; } } public override String Load() { return "{{CHAT}}"; } } class error : Page { public override String Name { get { return "error"; } } public override String Load() { return loadHTML(); } } }