/// <summary> /// Handles the GET message type. /// </summary> /// <param name="theBuffer">Contents of the message.</param> /// <param name="theSocket">Socket to utilize</param> internal static void HandleGetMsg(HeaderInfo theInfo) { FormData theData = new FormData(); string reqString = theInfo.FullPath; string reqFile = theInfo.FileName; if (reqString.IndexOf('?') > 0) { theData = HandleFormGetMsg(theInfo.HttpVer, reqString); reqString = reqString.Split('?')[0]; } if (reqString.LastIndexOf(".") > 0) { reqFile = reqString.Substring(reqString.LastIndexOf('/') + 1); } else if (reqString.Substring(reqString.LastIndexOf('/')) != "") { reqString += "/"; } if (reqFile == "") { reqFile = WebConnection.GetDefaultFileName(WebServer.WorkingDir + reqString); reqString += reqFile; } theInfo.FullPath = reqString; theInfo.FileName = reqFile; theInfo.parsedData = theData; }
/// <summary> /// Prepares a received message for /// sending to the browser. /// </summary> internal void PrepareToSend() { msgSent = false; if (this["Cookie"] != "") { BGUsers.HandleCookie(this); } if (parsedData != null && parsedData.Length > 0) { isReady = WebConnection.HandleFormData(this); } if (BGUsers.CheckLogged(this)) { cookieData = BGUsers.GetUserData(cookieData["nick"]); } WebConnection.SendToClient(this); }
/// <summary> /// Writes the user documentation using the provided formdata /// </summary> /// <param name="theData">Data to write.</param> private static void WriteUserDoc(FormData theData) { string fullname = Uri.UnescapeDataString(theData["fullname"]); string nick = Uri.UnescapeDataString(theData["nick"]); string info = theData["info"]; string password = Uri.UnescapeDataString(theData["password"]); string passCheck = Uri.UnescapeDataString(theData["passCheck"]); string fileName = GetUserFile(nick); bool checkPass = false; if (theData["avatar"] != "" && theData.GetType() == typeof(MultiFormData) && ((MultiFormData)theData).tableOfInfo.Count > 0) { if (HasProperExtension(theData["avatar"])) { byte[] file = ((MultiInfo)(((MultiFormData)theData).tableOfInfo["avatar"])).contents; if (file.Length > 0) { theData["avatar"] = theData["nick"] + theData["avatar"].Substring(theData["avatar"].LastIndexOf('.')).ToLower(); WriteImageFile(theData["nick"], theData["avatar"], file); } else { theData["avatar"] = GetUserData(nick)["avatar"]; } } else { throw new BGException("471", new string[] { string.Join(", ", WebServer.AvatarTypes) }); } } else { theData["avatar"] = GetUserData(nick)["avatar"]; } if (theData["action"] == "updateUser") { FormData oldData = GetUserData(nick); if (theData["oldPass"] != "") { if (oldData["password"] == EncodePassword(theData["oldPass"])) { if (theData["password"] == "" && theData["passCheck"] == "") { password = theData["oldPass"]; passCheck = theData["oldPass"]; } checkPass = true; } else { throw new BGException("472", new string[] { }); } } else { theData["password"] = oldData["password"]; } foreach (string key in oldData.TableOfValues.Keys) { if (!theData.TableOfValues.Contains(key)) { theData[key] = oldData[key]; } } } if (theData["action"] == "adduser" || checkPass) { if (!password.Equals(passCheck)) throw new BGException("474", new string[] { }); if (GetNotAllowed(password, passPattern) != "") throw new BGException("476", new string[] { "Password", GetNotAllowed(password, passPattern) }); if ((password.Length < minPassLength && !theData["rights"].Contains("superadmin")) || password.Length == 0) throw new BGException("477", new string[] { "Password", "" + minPassLength }); if (password.Length > maxPassLength) throw new BGException("478", new string[] { "Password", "" + maxPassLength }); theData["password"] = EncodePassword(password); } if (GetNotAllowed(fullname, fullNamePattern) != "") throw new BGException("476", new string[] { "Full name", GetNotAllowed(fullname, fullNamePattern) }); if (fullname.Length < minFullNameLength) throw new BGException("477", new string[] { "Full name", "" + minFullNameLength }); if (fullname.Length > maxFullNameLength) throw new BGException("478", new string[] { "Full name", "" + maxFullNameLength }); lock (FileLocker.GetLock(GetUserFile(nick))) { XmlTextWriter theWriter = new XmlTextWriter(GetUserFile(theData["nick"]), null); theWriter.Formatting = Formatting.Indented; theWriter.WriteStartDocument(); theWriter.WriteStartElement("user"); foreach (DictionaryEntry entry in theData.TableOfValues) { if (isAField(entry.Key.ToString())) { theWriter.WriteStartElement("" + entry.Key); theWriter.WriteValue(entry.Value); theWriter.WriteEndElement(); } } theWriter.WriteEndElement(); theWriter.WriteEndDocument(); theWriter.Close(); } }
/// <summary> /// Places user information into a FormData variable. /// </summary> /// <param name="userName">User name to look up</param> /// <returns>Data for current user or null if user not found</returns> internal static FormData GetUserData(string userName) { string userFile = GetUserFile(userName); FormData theData = new FormData(); if (!File.Exists(userFile)) { return theData; } lock (FileLocker.GetLock(userFile)) { XmlTextReader theReader = new XmlTextReader(userFile); theReader.MoveToContent(); while (theReader.Read()) { if (theReader.NodeType == XmlNodeType.Element) { theData.AddValue(theReader.Name, theReader.ReadString()); } } theReader.Close(); theReader = null; } return theData; }
/// <summary> /// Parses cookie information. /// </summary> /// <param name="cookieString">Cookie string to parse.</param> /// <param name="theData">FormData to add cookie information to.</param> internal static void GetCookieInfo(string cookieString, FormData theData) { MatchCollection cookies = Regex.Matches(cookieString, @"([a-z]+)=([" + Regex.Replace(BGUsers.NickPattern, @"\^", "") + "]+)"); if (theData == null) { theData = new FormData(); } foreach (Match cookie in cookies) { if (cookie.Groups[1].Value == "username") { theData.AddValue("nick", cookie.Groups[2].Value); } else if (cookie.Groups[1].Value == "hash") { theData.AddValue("key", cookie.Groups[2].Value); } } }
/// <summary> /// Parses cookie info /// </summary> /// <param name="cookieString">Cookie string to parse</param> /// <returns>FormData containing cookie info</returns> internal static FormData GetCookieInfo(string cookieString) { FormData data = new FormData(); GetCookieInfo(cookieString, data); return data; }
/// <summary> /// A namespace-accessible method for /// adding a user. /// </summary> /// <param name="theData">Form information (should contain user data)</param> internal static FormData AddUser(FormData theData) { string nick = theData["nick"]; if (File.Exists(GetUserFile(nick))) throw new BGException("475", new string[] { nick }); if (nick.Length < minNickLength) throw new BGException("477", new string[] { "Nickname", "" + minNickLength }); if (nick.Length > maxNickLength) throw new BGException("478", new string[] { "Nickname", "" + maxNickLength }); if (GetNotAllowed(nick, nickPattern) != "") throw new BGException("476", new string[] { "Nickname", GetNotAllowed(nick, nickPattern) }); if (!Directory.Exists(WebServer.UserDir)) { Directory.CreateDirectory(WebServer.UserDir); } if (theData["password"] == "" || theData["passCheck"] == "") throw new BGException("473", new string[] { }); theData["addtime"] = DateTime.Now.ToString(); WriteUserDoc(theData); theData.AddValue("MSG", "User <b>'" + Uri.UnescapeDataString(nick + "'</b> added successfully.<br>\r\n")); return theData; }
/// <summary> /// A function used to handle GET messages /// </summary> /// <param name="httpVer">HTTP version</param> /// <param name="reqString">Requested File/Directory & input string</param> /// <param name="theSocket">Socket to utilize</param> private static FormData HandleFormGetMsg(string httpVer, string reqString) { string[] input = reqString.Substring(reqString.IndexOf('?') + 1).Split('&'); FormData theData = new FormData(); for (int i = 0; i < input.Length; i++) { theData.AddValue(input[i].Split('=')[0], input[i].Split('=')[1]); } return theData; }