Debian Codegraph @

public/js/admin.js

raw symbols SCIP
1 /**
2 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
3 * Copyright (C) 2020-2024 Aleksey Andreev (liuch)
4 *
5 * Available at:
6 * https://github.com/liuch/dmarc-srg
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation, either version 3 of the License.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 class Admin {
22 constructor(id) {
23 this._state = null;
24 this._element = null;
25 this._sources = null;
26 this._database = null;
27 }
28
29 display() {
30 let cn = document.getElementById("main-block");
31 if (!this._element) {
32 this._element = document.createElement("div");
33 this._element.setAttribute("class", "panel-container round-border");
34 this._element.appendChild(this._get_database_elements());
35 this._element.appendChild(this._get_sources_elements());
36 }
37 cn.appendChild(this._element);
38 }
39
40 update() {
41 this._get_admin_state();
42 }
43
44 title() {
45 return "Admin Panel";
46 }
47
48 _get_database_elements() {
49 let fr = document.createDocumentFragment();
50 let h = document.createElement("h4");
51 h.appendChild(document.createTextNode("Database"));
52 fr.appendChild(h);
53 if (!this._database) {
54 this._database = new DatabaseListBox(this._create_db_item_menu_element());
55 }
56 fr.appendChild(this._database.element());
57 return fr;
58 }
59
60 _get_sources_elements() {
61 let fr = document.createDocumentFragment();
62 let h = document.createElement("h4");
63 h.appendChild(document.createTextNode("Report sources"));
64 fr.appendChild(h);
65 if (!this._sources) {
66 this._sources = new SourceListBox();
67 }
68 fr.appendChild(this._sources.element());
69 return fr;
70 }
71
72 _get_admin_state() {
73 [ this._database, this._sources ].forEach(function(c) {
74 c.set_status("wait");
75 });
76
77 let t = this;
78 window.fetch("admin.php", {
79 method: "GET",
80 cache: "no-store",
81 headers: HTTP_HEADERS,
82 credentials: "same-origin"
83 }).then(function(resp) {
84 if (!resp.ok)
85 throw new Error("Failed to fetch the admin data");
86 return resp.json();
87 }).then(function(data) {
88 Common.checkResult(data);
89 t._state = data;
90 t._fill_data();
91 }).catch(function(err) {
92 Common.displayError(err);
93 t._fill_data(err.message);
94 });
95 }
96
97 _send_command(cmd) {
98 let t = this;
99 return window.fetch("admin.php", {
100 method: "POST",
101 cache: "no-store",
102 headers: Object.assign(HTTP_HEADERS, HTTP_HEADERS_POST),
103 credentials: "same-origin",
104 body: JSON.stringify(cmd)
105 }).then(function(resp) {
106 if (!resp.ok)
107 throw new Error(`Failed to send command (${resp.status})`);
108 return resp.json();
109 }).finally(function() {
110 t._get_admin_state();
111 Status.instance().update().catch(function(){});
112 });
113 }
114
115 _fill_data(err_msg) {
116 if (!err_msg) {
117 let d = this._state.database || [];
118 this._database.set_data({
119 root: {
120 name: d.name || "-",
121 type: d.type || "-",
122 correct: d.correct,
123 message: d.message || "-",
124 location: d.location || "-"
125 },
126 groups: [
127 { name: "Tables", items: this._state.database.tables || [] }
128 ]
129 });
130 this._sources.set_data({
131 groups: [
132 { name: "Mailboxes", type: "mailbox", items: this._state.mailboxes || [] },
133 { name: "Directories", type: "directory", items: this._state.directories || [] },
134 { name: "Remote filesystems", type: "remotefs", items: this._state.remotefs || [] }
135 ]
136 });
137 }
138 else {
139 this._database.set_status("error", err_msg);
140 this._sources.set_status("error", err_msg);
141 }
142 if (this._state && this._state.database && this._state.database.needs_upgrade) {
143 document.querySelector(".db-menu-button li[data-action=upgradedb]").classList.remove("hidden");
144 }
145 }
146
147 _create_db_item_menu_element() {
148 const el = document.createElement("div");
149 el.classList.add("button");
150 const span = el.appendChild(document.createElement("span"));
151 span.setAttribute("role", "button");
152 span.textContent = "...";
153 //
154 const mn = el.appendChild(document.createElement("div"));
155 mn.classList.add("db-item-menu", "popup-menu", "round-border", "hidden");
156 const ul = mn.appendChild(document.createElement("ul"));
157 Admin.db_actions.forEach(it => {
158 const li = ul.appendChild(document.createElement("li"));
159 li.title = it.long_title;
160 li.dataset.title = it.title;
161 li.dataset.action = it.action;
162 li.appendChild(document.createElement("span")).textContent = it.name;
163 if (it.action === "upgradedb") li.classList.add("hidden");
164 });
165 el.addEventListener("click", event => {
166 event.stopPropagation();
167 el.querySelector(".popup-menu").classList.toggle("hidden");
168 const item = event.target.closest("li");
169 if (item) this._do_db_action_password(item.dataset.action, item.dataset.title);
170 });
171 return el;
172 }
173
174 _do_db_action_password(action, title) {
175 let ld = new LoginDialog({
176 nofetch: true,
177 nousername: true
178 });
179 document.getElementById("main-block").appendChild(ld.element());
180 let that = this;
181 ld.show().then(function(d) {
182 if (d) {
183 that._do_db_action(action, title, { password: d.password });
184 }
185 }).catch(function(err) {
186 Common.displayError(err);
187 }).finally(function() {
188 ld.remove();
189 });
190 }
191
192 _do_db_action(action, title, data) {
193 let d = { cmd: action };
194 if (data) {
195 d = Object.assign(d, data);
196 }
197 this._send_command(d).then(function(data) {
198 Common.checkResult(data);
199 Notification.add({ text: title + ": " + (data.message || "Completed successfully!"), type: "info", name: "db-act" });
200 }).catch(function(err) {
201 Common.displayError(err);
202 Notification.add({ text: title + ": " + err.message, type: "error", delay: 10000, name: "db-act" });
203 });
204 }
205 }
206
207 Admin.db_actions = [
208 {
209 name: "Initiate",
210 action: "initdb",
211 title: "Initiate DB",
212 long_title: "Create all needed tables and indexes in the database"
213 },
214 {
215 name: "Drop",
216 action: "cleandb",
217 title: "Drop tables",
218 long_title: "Drop all the tables from the database"
219 },
220 {
221 name: "Upgrade",
222 action: "upgradedb",
223 title: "Upgrade DB",
224 long_title: "Update the structure of the database"
225 }
226 ];
227
228 class DropdownListBox {
229 constructor() {
230 this._item_groups = [];
231 this._element = null;
232 this._root_item = null
233 this._list_element = null;
234 }
235
236 element() {
237 if (!this._element) {
238 let el = document.createElement("div");
239 el.setAttribute("class", "round-border");
240 let that = this;
241 el.addEventListener("click", function(event) {
242 if (event.target.closest(".root-list-block")) {
243 if (that._item_groups.length > 0) {
244 that._list_element.classList.toggle("hidden");
245 that._root_item.element().classList.toggle("bottom-border");
246 }
247 }
248 });
249 this._element = el;
250 this._update_element();
251 }
252 return this._element;
253 }
254
255 set_status(type, message) {
256 if (type === "wait") {
257 set_wait_status(this.element());
258 }
259 else if (type === "error") {
260 set_error_status(this.element(), message);
261 }
262 }
263
264 set_data(data) {
265 this._root_item = new ListBoxItem();
266 this._make_group_list(data);
267 this._make_root_columns(data);
268 if (this._element) {
269 this._update_element();
270 }
271 }
272
273 _update_element() {
274 if (this._element.children.length != 2)
275 this._element.replaceChildren(this._content_container(), this._list_container());
276 }
277
278 _content_container() {
279 if (!this._root_item) {
280 this._root_item = new ListBoxItem();
281 }
282 let c = this._root_item.element();
283 let cl = [];
284 for (let i = 0; i < c.classList.length; ++i) {
285 if (c.classList[i].startsWith("state-"))
286 cl.push(c.classList[i]);
287 }
288 c.setAttribute("class", "root-list-block" + (cl.length >0 && (" " + cl.join(" ")) || ""));
289 return c;
290 }
291
292 _list_container() {
293 let c = document.createElement("div");
294 c.setAttribute("class", "list-container hidden");
295 c.appendChild(this._make_list_item_elements());
296 this._list_element = c;
297 return c;
298 }
299
300 _make_root_columns(data) {
301 }
302
303 _make_group_list(data) {
304 this._item_groups = data.groups.map(function(gd) {
305 return this._make_group_item(gd);
306 }, this);
307 }
308
309 _make_group_item(gr_data) {
310 return new ListBoxItemGroup(gr_data);
311 }
312
313 _make_list_item_elements() {
314 let fr = document.createDocumentFragment();
315 this._item_groups.forEach(function(ig) {
316 fr.appendChild(ig.element());
317 });
318 return fr;
319 }
320 }
321
322 class DatabaseListBox extends DropdownListBox {
323 constructor(menu) {
324 super();
325 this._menu = menu;
326 this._name = null;
327 this._type = null;
328 this._correct = false;
329 this._message = null;
330 this._location = null;
331 }
332
333 set_data(data) {
334 this._name = data.root.name;
335 this._type = data.root.type;
336 this._correct = data.root.correct;
337 this._message = data.root.message;
338 this._location = data.root.location;
339 super.set_data(data);
340 }
341
342 _make_root_columns(data) {
343 this._root_item.state(this._correct && "green" || "red");
344 this._root_item.add_column(new StatusIndicator(this._name, this._message, "title-item-wrap"));
345 this._root_item.add_column(new ListBoxColumn(this._message, null, "message-item state-text"));
346 this._root_item.add_column(new ListBoxColumn(this._type, null, "db-type"));
347 this._root_item.add_column(new ListBoxColumn(this._location, null, "db-location"));
348 if (this._menu)
349 this._root_item.add_column(new ListBoxColumn(this._menu, null, "db-menu-button"));
350 }
351
352 _make_group_item(gr_data) {
353 return new DatabaseItemGroup(gr_data);
354 }
355 }
356
357 class SourceListBox extends DropdownListBox {
358 element() {
359 let _new = !this._element && true || false;
360 super.element();
361 if (_new) {
362 let that = this;
363 this._element.addEventListener("click", function(event) {
364 if (event.target.tagName == "BUTTON") {
365 let p = event.target.closest("div[data-id]")
366 if (p) {
367 let id = parseInt(p.getAttribute("data-id"));
368 let type = p.getAttribute("data-type");
369 that._check_button_clicked(id, type, event.target);
370 }
371 }
372 });
373 }
374 return this._element;
375 }
376
377 _make_root_columns(data) {
378 let count = this._item_groups.reduce(function(cnt, gr) {
379 return cnt + gr.count();
380 }, 0);
381 let enabled = (count > 0);
382 this._root_item.state(enabled && "green" || "gray");
383 this._root_item.add_column(new StatusIndicator("Total sources: " + count, enabled && "Enabled" || "Disabled"));
384 }
385
386 _make_group_item(gr_data) {
387 return new SourceItemGroup(gr_data);
388 }
389
390 _check_button_clicked(id, type, btn) {
391 let that = this;
392 let state = "yellow";
393 let btn_text = btn.textContent;
394 btn.textContent = "Checking...";
395 btn.disabled = true;
396 window.fetch("admin.php", {
397 method: "POST",
398 cache: "no-store",
399 headers: Object.assign(HTTP_HEADERS, HTTP_HEADERS_POST),
400 credentials: "same-origin",
401 body: JSON.stringify({ cmd: "checksource", id: id, type: type })
402 }).then(function(resp) {
403 if (!resp.ok)
404 throw new Error(`Failed to send checking request (${resp.status})`);
405 return resp.json();
406 }).then(function(data) {
407 Common.checkResult(data);
408 let msg = [ data.message ];
409 if (data.status) {
410 if (type === "mailbox") {
411 msg.push("Messages: " + data.status.messages);
412 msg.push("Unseen: " + data.status.unseen);
413 }
414 else if (type === "directory" || type === "remotefs") {
415 msg.push("Files: " + data.status.files);
416 }
417 }
418 Notification.add({ text: msg, type: "info" });
419 state = "green";
420 }).catch(function(err) {
421 Common.displayError(err);
422 Notification.add({ text: err.message, type: "error" });
423 }).finally(function() {
424 btn.textContent = btn_text;
425 btn.disabled = false;
426 that._set_state(state, id, type);
427 });
428 }
429
430 _set_state(state, id, type) {
431 let flag = 0;
432 let gstate = "green";
433 for (let i = 0; flag !== 3 && i < this._item_groups.length; ++i) {
434 let gr = this._item_groups[i];
435 if (!(flag & 1) && gr.type() === type) {
436 gr.state(state, id);
437 flag |= 1;
438 }
439 if (!(flag & 2)) {
440 let s = gr.state();
441 if (s !== "green") {
442 gstate = s;
443 flag |= 2;
444 }
445 }
446 }
447 this._root_item.state(gstate);
448 }
449 }
450
451 class ListBoxItem {
452 constructor() {
453 this._state = null;
454 this._element = null;
455 this._columns = [];
456 }
457
458 add_column(col) {
459 this._columns.push(col);
460 }
461
462 element() {
463 if (!this._element) {
464 this._element = document.createElement("div");
465 let extra_class = "";
466 if (this._state) {
467 extra_class = " state-" + this._state;
468 }
469 this._element.setAttribute("class", "block-list-item round-border" + extra_class);
470 this._insert_column_elements();
471 }
472 return this._element;
473 }
474
475 state(state) {
476 if (!state) {
477 return this._state;
478 }
479
480 if (this._element) {
481 if (this._state) {
482 this._element.classList.remove("state-" + this._state);
483 }
484 this._element.classList.add("state-" + state);
485 }
486 this._state = state;
487 }
488
489 _insert_column_elements() {
490 this._columns.forEach(function(c) {
491 this._element.appendChild(c.element());
492 }, this);
493 }
494 }
495
496 class SourceListItem extends ListBoxItem {
497 constructor(data) {
498 super();
499 this._id = data.id;
500 this._type = data.type;
501 }
502
503 id() {
504 return this._id;
505 }
506
507 type() {
508 return this._type;
509 }
510
511 element() {
512 let el = super.element();
513 el.setAttribute("data-id", this._id);
514 el.setAttribute("data-type", this._type);
515 return el;
516 }
517 }
518
519 class ListBoxItemGroup {
520 constructor(data) {
521 this._name = data.name;
522 this._type = data.type;
523 this._element = null;
524 this._items = data.items.map(function(it) {
525 return this._make_item(it);
526 }, this);
527 }
528
529 type() {
530 return this._type;
531 }
532
533 count() {
534 return this._items.length;
535 }
536
537 element() {
538 if (!this._element) {
539 let fr = document.createDocumentFragment();
540 let h = document.createElement("h5");
541 h.appendChild(document.createTextNode(this._name + " (" + this._items.length + ")"));
542 fr.appendChild(h);
543 this._items.forEach(function(it) {
544 fr.appendChild(it.element());
545 });
546 this._element = fr;
547 }
548 return this._element;
549 }
550
551 _make_item(d) {
552 return new ListBoxItem();
553 }
554 }
555
556 class DatabaseItemGroup extends ListBoxItemGroup {
557 _make_item(d) {
558 let it = super._make_item(d);
559 let state = d.error_code && "red" || (d.message === "Ok" && "green" || "yellow");
560 it.state(state);
561 it.add_column(new StatusIndicator(d.name, d.message, "title-item-wrap"));
562 it.add_column(new ListBoxColumn(d.engine || d.message, null, "message-item state-text"));
563 it.add_column(new ListBoxColumn(d.rows || 0, "Records", "dbtable-records"));
564 it.add_column(new ListBoxColumn((d.data_length || 0) + (d.index_length || 0), "Size", "dbtable-size"));
565 return it;
566 }
567 }
568
569 class SourceItemGroup extends ListBoxItemGroup {
570 state(new_state, item_id) {
571 if (item_id !== undefined) {
572 this._items.find(function(item) {
573 if (item.id() == item_id) {
574 item.state(new_state);
575 return true;
576 }
577 return false;
578 });
579 return;
580 }
581 let gstate = "green";
582 for (let i = 0; i < this._items.length; ++i) {
583 let state = this._items[i].state();
584 if (state !== gstate) {
585 return state;
586 }
587 }
588 return gstate;
589 }
590
591 _make_item(d) {
592 let it = new SourceListItem({ id: d.id, type: this._type });
593 it.state("green");
594 it.add_column(new StatusIndicator(d.name, null, "title-item-wrap"));
595 if (this._type === "mailbox") {
596 it.add_column(new ListBoxColumn(d.mailbox, null, "mailbox-location"));
597 it.add_column(new ListBoxColumn(d.host, "Host", "mailbox-host"));
598 }
599 else {
600 it.add_column(new ListBoxColumn(d.location, null, "directory-location"));
601 }
602 it.add_column(new ListBoxColumn(this._make_check_button(), null, "source-check-button"));
603 return it;
604 }
605
606 _make_check_button() {
607 let btn = document.createElement("button");
608 btn.appendChild(document.createTextNode("Check accessibility"));
609 return btn;
610 }
611
612 }
613
614 class ListBoxColumn {
615 constructor(value, title, class_string) {
616 this._value = value;
617 this._title = title;
618 this._class = class_string;
619 this._element = null;
620 }
621
622 element() {
623 if (!this._element) {
624 this._element = document.createElement("div");
625 this._element.setAttribute("class", "block-item-column" + (this._class && (" " + this._class) || ""));
626 this._add_children();
627 }
628 return this._element;
629 }
630
631 _add_children() {
632 let val_el = this._element;
633 if (this._title) {
634 let sp = document.createElement("span");
635 sp.appendChild(document.createTextNode(this._title + ":"));
636 this._element.appendChild(sp);
637 val_el = document.createElement("span");
638 val_el.setAttribute("class", "value");
639 this._element.appendChild(val_el);
640 }
641 if (typeof(this._value) != "object")
642 val_el.appendChild(document.createTextNode(this._value));
643 else
644 val_el.appendChild(this._value);
645 }
646 }
647
648 class StatusIndicator extends ListBoxColumn {
649 _add_children() {
650 let div = document.createElement("div");
651 div.setAttribute("class", "state-background status-indicator");
652 if (this._title) {
653 div.setAttribute("title", this._title);
654 }
655 this._element.appendChild(div);
656 if (this._value) {
657 div = document.createElement("div");
658 div.appendChild(document.createTextNode(this._value));
659 this._element.appendChild(div);
660 }
661 }
662 }