/// <summary> /// Gets TabIDs based in restrictions in input arguments. /// </summary> /// <param name="Arguments">Tab restrictions.</param> /// <param name="Node">Script node getting the tabs.</param> /// <returns>Tab IDs</returns> public static string[] GetTabs(IElement[] Arguments, ScriptNode Node) { switch (Arguments.Length) { case 0: return(ClientEvents.GetTabIDs()); case 1: object Obj = Arguments[0].AssociatedObjectValue; if (Obj is Array A) { if (!(A is string[] Pages)) { Pages = (string[])Expression.ConvertTo(A, typeof(string[]), Node); } return(ClientEvents.GetTabIDsForLocations(Pages)); } else if (Obj is IUser User) { return(ClientEvents.GetTabIDsForUser(User)); } else { return(ClientEvents.GetTabIDsForLocation(Obj?.ToString())); } case 2: return(ClientEvents.GetTabIDsForLocation(Arguments[0].AssociatedObjectValue?.ToString(), GetQueryFilter(Arguments[1], Node))); default: return(new string[0]); } }
private string[] GetTabIDs() { if (Gateway.Configuring) { return(ClientEvents.GetTabIDs()); } else { return(ClientEvents.GetTabIDsForLocation("/Settings/Roster.md")); } }
/// <summary> /// Removes a file from all pages viewing backup files /// </summary> /// <param name="FileName">Name of file</param> public static void UpdateClientsFileDeleted(string FileName) { StringBuilder sb = new StringBuilder(); sb.Append("{\"fileName\":\""); sb.Append(CommonTypes.JsonStringEncode(FileName)); sb.Append("\"}"); string[] TabIDs = ClientEvents.GetTabIDsForLocation("/Settings/Backup.md"); Task _ = ClientEvents.PushEvent(TabIDs, "FileDeleted", sb.ToString(), true, "User"); }
private async Task Push(DateTime Timestamp, string Message, string Function, bool CloseIfNoTabs) { try { DateTime Now = DateTime.Now; if ((Now - this.tabIdTimestamp).TotalSeconds > 2 || this.tabIds is null || this.tabIds.Length == 0) { this.tabIds = ClientEvents.GetTabIDsForLocation(this.resource, true, "SnifferId", this.snifferId); this.tabIdTimestamp = Now; } if (this.feedbackCheck && Message.StartsWith("{") && Message.EndsWith("}")) { try { object Parsed = JSON.Parse(Message); if (Parsed is IDictionary <string, object> Obj && Obj.TryGetValue("data", out object Temp) && Temp is IDictionary <string, object> Obj2 && Obj2.TryGetValue("timestamp", out object Timestamp2) && Obj2.TryGetValue("message", out object Message2) && (this.outgoing?.ContainsKey(this.ToJson(Timestamp2, Message2)) ?? true)) { return; } } catch (Exception) { // Ignore } } string Data = this.ToJson(XML.Encode(Timestamp), Message); this.outgoing?.Add(Data, true); int Tabs = await ClientEvents.PushEvent(this.tabIds, Function, Data, true, this.userVariable, this.privileges); if (CloseIfNoTabs && Tabs <= 0 && (Now - this.created).TotalSeconds >= 5) { await this.Close(); } } catch (Exception ex) { Log.Critical(ex); } }
/// <summary> /// Updates the status of a file on all pages viewing backup files /// </summary> /// <param name="FileName">Name of file</param> /// <param name="Length">Size of file</param> /// <param name="Created">When file was created</param> public static void UpdateClientsFileUpdated(string FileName, long Length, DateTime Created) { StringBuilder sb = new StringBuilder(); sb.Append("{\"fileName\":\""); sb.Append(CommonTypes.JsonStringEncode(FileName)); sb.Append("\", \"size\": \""); if (Length >= 0) { sb.Append(CommonTypes.JsonStringEncode(Export.FormatBytes(Length))); } sb.Append("\", \"created\": \""); sb.Append(CommonTypes.JsonStringEncode(Created.ToString())); sb.Append("\", \"button\": \""); sb.Append(CommonTypes.JsonStringEncode("<button class=\"posButtonSm\" onclick=\"DeleteExport('" + FileName + "');\">Delete</button>")); sb.Append("\", \"isKey\": "); sb.Append(FileName.EndsWith(".key", StringComparison.CurrentCultureIgnoreCase) ? "true" : "false"); sb.Append("}"); string[] TabIDs = ClientEvents.GetTabIDsForLocation("/Settings/Backup.md"); Task _ = ClientEvents.PushEvent(TabIDs, "UpdateExport", sb.ToString(), true, "User"); }
/// <summary> /// Exports gateway data /// </summary> /// <param name="Output">Export Output</param> /// <param name="Database">If the contents of the database is to be exported.</param> /// <param name="WebContent">If web content is to be exported.</param> /// <param name="Folders">Root subfolders to export.</param> public static async Task DoExport(IExportFormat Output, bool Database, bool WebContent, string[] Folders) { try { List <KeyValuePair <string, object> > Tags = new List <KeyValuePair <string, object> >() { new KeyValuePair <string, object>("Database", Database) }; foreach (string Folder in Folders) { Tags.Add(new KeyValuePair <string, object>(Folder, true)); } Log.Informational("Starting export.", Output.FileName, Tags.ToArray()); await Output.Start(); if (Database) { StringBuilder Temp = new StringBuilder(); using (XmlWriter w = XmlWriter.Create(Temp, XML.WriterSettings(false, true))) { await Persistence.Database.Analyze(w, string.Empty, Path.Combine(Gateway.RootFolder, "Data"), false, true); } await Persistence.Database.Export(Output); } if (WebContent || Folders.Length > 0) { await Output.StartFiles(); try { string[] FileNames; string Folder2; if (WebContent) { FileNames = Directory.GetFiles(Gateway.RootFolder, "*.*", SearchOption.TopDirectoryOnly); foreach (string FileName in FileNames) { await ExportFile(FileName, Output); } Export.FolderCategory[] ExportFolders = Export.GetRegisteredFolders(); foreach (string Folder in Directory.GetDirectories(Gateway.RootFolder, "*.*", SearchOption.TopDirectoryOnly)) { bool IsWebContent = true; foreach (Export.FolderCategory FolderCategory in ExportFolders) { foreach (string Folder3 in FolderCategory.Folders) { if (string.Compare(Folder3, Folder, true) == 0) { IsWebContent = false; break; } } if (!IsWebContent) { break; } } if (IsWebContent) { FileNames = Directory.GetFiles(Folder, "*.*", SearchOption.AllDirectories); foreach (string FileName in FileNames) { await ExportFile(FileName, Output); } } } } foreach (string Folder in Folders) { if (Directory.Exists(Folder2 = Path.Combine(Gateway.RootFolder, Folder))) { FileNames = Directory.GetFiles(Folder2, "*.*", SearchOption.AllDirectories); foreach (string FileName in FileNames) { await ExportFile(FileName, Output); } } } } finally { await Output.EndFiles(); } } Log.Informational("Export successfully completed.", Output.FileName); } catch (Exception ex) { Log.Critical(ex); string[] Tabs = ClientEvents.GetTabIDsForLocation("/Backup.md"); ClientEvents.PushEvent(Tabs, "BackupFailed", "{\"fileName\":\"" + CommonTypes.JsonStringEncode(Output.FileName) + "\", \"message\": \"" + CommonTypes.JsonStringEncode(ex.Message) + "\"}", true, "User"); } finally { try { await Output.End(); Output.Dispose(); } catch (Exception ex) { Log.Critical(ex); } lock (synchObject) { exporting = false; } } }
/// <summary> /// Analyzes the object database /// </summary> /// <param name="FullPath">Full path of report file</param> /// <param name="FileName">Filename of report</param> /// <param name="Created">Time when report file was created</param> /// <param name="XmlOutput">XML Output</param> /// <param name="fs">File stream</param> /// <param name="Repair">If database should be repaired, if errors are found</param> public static async Task DoAnalyze(string FullPath, string FileName, DateTime Created, XmlWriter XmlOutput, FileStream fs, bool Repair) { try { Log.Informational("Starting analyzing database.", FileName); ExportFormats.ExportFormat.UpdateClientsFileUpdated(FileName, 0, Created); await Database.Analyze(XmlOutput, Path.Combine(Gateway.AppDataFolder, "Transforms", "DbStatXmlToHtml.xslt"), Gateway.AppDataFolder, false, Repair); XmlOutput.Flush(); fs.Flush(); ExportFormats.ExportFormat.UpdateClientsFileUpdated(FileName, fs.Length, Created); XmlOutput.Dispose(); XmlOutput = null; fs.Dispose(); fs = null; if (xslt is null) { xslt = XSL.LoadTransform(typeof(Gateway).Namespace + ".Transforms.DbStatXmlToHtml.xslt"); } string s = File.ReadAllText(FullPath); s = XSL.Transform(s, xslt); byte[] Bin = utf8Bom.GetBytes(s); string FullPath2 = FullPath.Substring(0, FullPath.Length - 4) + ".html"; File.WriteAllBytes(FullPath2, Bin); ExportFormats.ExportFormat.UpdateClientsFileUpdated(FileName.Substring(0, FileName.Length - 4) + ".html", Bin.Length, Created); Log.Informational("Database analysis successfully completed.", FileName); } catch (Exception ex) { Log.Critical(ex); string[] Tabs = ClientEvents.GetTabIDsForLocation("/Settings/Backup.md"); ClientEvents.PushEvent(Tabs, "BackupFailed", "{\"fileName\":\"" + CommonTypes.JsonStringEncode(FileName) + "\", \"message\": \"" + CommonTypes.JsonStringEncode(ex.Message) + "\"}", true, "User"); } finally { try { XmlOutput?.Dispose(); fs?.Dispose(); } catch (Exception ex) { Log.Critical(ex); } lock (synchObject) { analyzing = false; } } }
/// <summary> /// Queues an event to be output. /// </summary> /// <param name="Event">Event to queue.</param> public override async Task Queue(Event Event) { try { DateTime Now = DateTime.Now; if ((Now - this.tabIdTimestamp).TotalSeconds > 2 || this.tabIds is null || this.tabIds.Length == 0) { this.tabIds = ClientEvents.GetTabIDsForLocation(this.resource, true); this.tabIdTimestamp = Now; } Dictionary <string, object>[] Tags; if (Event.Tags is null) { Tags = null; } else { int i, c = Event.Tags.Length; Tags = new Dictionary <string, object> [c]; for (i = 0; i < c; i++) { Tags[i] = new Dictionary <string, object>() { { "name", Event.Tags[i].Key }, { "value", Event.Tags[i].Value } }; } } string Data = JSON.Encode(new KeyValuePair <string, object>[] { new KeyValuePair <string, object>("date", XML.Encode(Event.Timestamp.Date, true)), new KeyValuePair <string, object>("time", Event.Timestamp.TimeOfDay.ToString()), new KeyValuePair <string, object>("type", Event.Type.ToString()), new KeyValuePair <string, object>("level", Event.Level.ToString()), new KeyValuePair <string, object>("id", Event.EventId), new KeyValuePair <string, object>("object", Event.Object), new KeyValuePair <string, object>("actor", Event.Actor), new KeyValuePair <string, object>("module", Event.Module), new KeyValuePair <string, object>("facility", Event.Facility), new KeyValuePair <string, object>("message", Event.Message), new KeyValuePair <string, object>("stackTrace", Event.StackTrace), new KeyValuePair <string, object>("tags", Tags) }, false); int Tabs = await ClientEvents.PushEvent(this.tabIds, "NewEvent", Data, true, this.userVariable, this.privileges); if (Now >= this.expires || (Tabs <= 0 && (Now - this.created).TotalSeconds >= 5)) { await ClientEvents.PushEvent(this.tabIds, "SinkClosed", string.Empty, false, this.userVariable, this.privileges); Log.Unregister(this); this.Dispose(); } } catch (Exception ex) { Log.Critical(ex); } }