/// <summary> /// Gets all the Users. /// </summary> /// <remarks>The array is unsorted.</remarks> public static UserInfo[] AllUsers(string groupName) { string tmp; tmp = Tools.LoadFile(SueetieSettings.UsersFile(groupName)).Replace("\r", ""); string[] lines = tmp.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); UserInfo[] result = new UserInfo[lines.Length]; string[] fields; for (int i = 0; i < lines.Length; i++) { fields = lines[i].Split('|'); // Structure: // Username|PasswordHash|Email|Active-Inactive|DateTime|Admin-User result[i] = new UserInfo(fields[0], fields[2]); } return(result); }
/// <summary> /// Reads all the Log Entries. /// </summary> /// <returns>The Entries.</returns> public static List <LogEntry> ReadEntries(string groupName) { string content = Tools.LoadFile(SueetieSettings.LogFile(groupName).Replace("\r", "")); List <LogEntry> result = new List <LogEntry>(50); string[] lines = content.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); string[] fields; for (int i = 0; i < lines.Length; i++) { fields = lines[i].Split('|'); try { // Try/catch to avoid problems with corrupted file (raw method) result.Add(new LogEntry(EntryTypeParse(fields[0]), DateTime.Parse(fields[1]), Resanitize(fields[2]), Resanitize(fields[3]))); } catch { } } result.Reverse(); return(result); }
/// <summary> /// Adds a new User to a Screwturn Wiki 2.0.35 Site. /// </summary> public static bool AddUser(string username, string email, string groupName) { if (Exists(new UserInfo(username, email), groupName)) { return(false); } StringBuilder sb = new StringBuilder(); sb.Append("\r\n"); // Important sb.Append(username); sb.Append("|"); sb.Append(WebConfigurationManager.AppSettings["SUEETIE.WikiLoginKey"].ToString()); sb.Append("|"); sb.Append(email); sb.Append("|"); sb.Append("ACTIVE"); sb.Append("|"); sb.Append(DateTime.Now.ToString("yyyy'/'MM'/'dd' 'HH':'mm':'ss")); sb.Append("|"); sb.Append("USER"); Tools.AppendFile(SueetieSettings.UsersFile(groupName), sb.ToString()); return(true); }
/// <summary> /// Writes an Entry in the Log. /// </summary> /// <param name="message">The Message.</param> /// <param name="type">The Type of the Entry.</param> /// <param name="user">The User that generated the Entry.</param> public static void LogEntry(string message, EntryType type, string user, string groupName) { message = Sanitize(message); user = Sanitize(user); FileStream fs = null; // Since the Log is the most accessed file, the # of access tries has been doubled for (int i = 0; i < FileAccessTries * 2; i++) { try { fs = new FileStream(SueetieSettings.LogFile(groupName), FileMode.Append, FileAccess.Write, FileShare.None); break; } catch { Thread.Sleep(FileAccessTryDelay); } } if (fs == null) { throw new IOException("Unable to open the file: " + SueetieSettings.LogFile(groupName)); } StreamWriter sw = new StreamWriter(fs, System.Text.UTF8Encoding.UTF8); // Type | DateTime | Message | User try { sw.Write(EntryTypeToString(type) + "|" + string.Format("{0:yyyy'/'MM'/'dd' 'HH':'mm':'ss}", DateTime.Now) + "|" + message + "|" + user + "\r\n"); } catch { } sw.Close(); FileInfo fi = new FileInfo(SueetieSettings.LogFile(groupName)); if (fi.Length > (long)(MaxLogSize * 1024)) { CutLog(groupName); } }
private static void CutLog(string groupName) { // Contains the log messages from the newest to the oldest List <LogEntry> entries = ReadEntries(groupName); // Estimate the size of each entry at 80 chars FileInfo fi = new FileInfo(SueetieSettings.LogFile(groupName)); long size = fi.Length; int difference = (int)(size - (long)(MaxLogSize * 1024)); int removeEntries = difference / 80 * 2; // Double the number of remove entries in order to reduce the # of times Cut is needed int preserve = entries.Count - removeEntries; // The number of entries to be preserved // Copy the entries to preserve in a temp list List <LogEntry> toStore = new List <LogEntry>(); for (int i = 0; i < preserve; i++) { toStore.Add(entries[i]); } // Reverse the temp list because entries contains the log messages // starting from the newest to the oldest toStore.Reverse(); StringBuilder sb = new StringBuilder(); // Type | DateTime | Message | User foreach (LogEntry e in toStore) { sb.Append(EntryTypeToString(e.EntryType)); sb.Append("|"); sb.Append(e.DateTime.ToString("yyyy'/'MM'/'dd' 'HH':'mm':'ss")); sb.Append("|"); sb.Append(e.Message); sb.Append("|"); sb.Append(e.User); sb.Append("\r\n"); } FileStream fs = null; // Since the Log is the most accessed file, the # of access tries has been doubled for (int i = 0; i < FileAccessTries * 2; i++) { try { fs = new FileStream(SueetieSettings.LogFile(groupName), FileMode.Create, FileAccess.Write, FileShare.None); break; } catch { Thread.Sleep(FileAccessTryDelay); } } if (fs == null) { throw new IOException("Unable to open the file: " + SueetieSettings.LogFile(groupName)); } StreamWriter sw = new StreamWriter(fs, System.Text.UTF8Encoding.UTF8); // Type | DateTime | Message | User try { sw.Write(sb.ToString()); } catch { } sw.Close(); }