Debian Codegraph @

public/js/files.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 Files {
22 constructor() {
23 this._container = null;
24 this._fieldset1 = null;
25 this._element = document.getElementById("main-block");
26 this._fcount_info = null;
27 this._fsize_info = null;
28 this._limits = {
29 upload_max_file_count: 0,
30 upload_max_file_size: 0
31 };
32 this._directories = {};
33 [ "email", "local", "remote" ].forEach(type => {
34 this._directories[type] = { list: [], element: null, table: null };
35 });
36 }
37
38 display() {
39 this._create_container();
40 this._create_local_file_uploading_element();
41 this._container.appendChild(this._fieldset1);
42 if (User.level === "admin") {
43 this._create_directory_loading_element(
44 this._directories.email,
45 "email",
46 "Loading reports files from the mailboxes",
47 "No mailboxes are configured."
48 );
49 this._container.appendChild(this._directories.email.element);
50 this._create_directory_loading_element(
51 this._directories.local,
52 "local",
53 "Loading report files from the server directory",
54 "No directories are configured."
55 );
56 this._container.appendChild(this._directories.local.element);
57 this._create_directory_loading_element(
58 this._directories.remote,
59 "remote",
60 "Loading report files from the remote filesystem",
61 "No remote filesystems are configured."
62 );
63 this._container.appendChild(this._directories.remote.element);
64 }
65 this._element.appendChild(this._container);
66 this._fieldset1.focus();
67 }
68
69 update() {
70 if (!Status.instance().error()) {
71 let admin = User.level === "admin";
72 this._fetch_data(true, admin, admin, admin);
73 }
74 }
75
76 title() {
77 return "Report Files";
78 }
79
80 _create_container() {
81 this._container = document.createElement("div");
82 this._container.setAttribute("class", "panel-container round-border");
83 }
84
85 _create_local_file_uploading_element() {
86 this._fieldset1 = document.createElement("fieldset");
87 this._fieldset1.setAttribute("class", "round-border");
88 this._fieldset1.disabled = true;
89 let lg = document.createElement("legend");
90 lg.appendChild(document.createTextNode("Uploading local report files"));
91 this._fieldset1.appendChild(lg);
92 let fm = document.createElement("form");
93 fm.setAttribute("enctype", "multipart/form-data");
94 fm.setAttribute("method", "post");
95 fm.appendChild(this._create_input_element("hidden", "cmd", "upload-report"));
96 let fl = this._create_input_element("file", "report_file[]", null)
97 fl.required = true;
98 fl.multiple = true;
99 fm.appendChild(fl);
100 let dv = document.createElement("div");
101 dv.setAttribute("class", "buttons-block");
102 let sb = this._create_button_element("submit", "Upload reports");
103 sb.disabled = true;
104 dv.appendChild(sb);
105 dv.appendChild(this._create_button_element("reset", "Reset"));
106 fm.appendChild(dv);
107 let that = this;
108 fl.addEventListener("change", function(event) {
109 sb.disabled = !that._check_files(fl);
110 });
111 fm.addEventListener("reset", function(event) {
112 sb.disabled = true;
113 that._clear_warnings();
114 });
115 fm.addEventListener("submit", function(event) {
116 window.fetch("files.php", {
117 method: "POST",
118 credentials: "same-origin",
119 body: new FormData(fm)
120 }).then(function(resp) {
121 if (!resp.ok)
122 throw new Error("Failed to upload a report file");
123 return resp.json();
124 }).then(function(data) {
125 Common.checkResult(data);
126 Notification.add({ text: (data.message || "Uploaded successfully!"), type: "info" });
127 Status.instance().update().catch(() => {});
128 }).catch(function(err) {
129 Common.displayError(err);
130 Notification.add({ text: (err.message || "Error!"), type: "error" });
131 });
132 event.preventDefault();
133 fm.reset();
134 });
135 this._fieldset1.appendChild(fm);
136 }
137
138 _create_directory_loading_element(dir, type, title, no_message) {
139 dir.element = document.createElement("fieldset");
140 dir.element.setAttribute("class", "round-border");
141 dir.element.disabled = true;
142 dir.element.appendChild(document.createElement("legend")).textContent = title;
143
144 let fm = document.createElement("form");
145 fm.method = "post";
146 dir.table = new ITable({
147 class: "main-table subtable",
148 onclick: function(row) {
149 let userdata = row.userdata();
150 let checkbox = row.element().querySelector("input");
151 if (checkbox && !userdata.error) {
152 userdata.checked = !userdata.checked;
153 checkbox.checked = userdata.checked;
154 this._update_directory_button(dir);
155 }
156 }.bind(this),
157 nodata_text: no_message
158 });
159 [
160 { content: "", class: "cell-status" },
161 { content: "Name" }
162 ].forEach(function(col) {
163 dir.table.add_column(col);
164 }, this);
165 if (type === "email") {
166 dir.table.add_column({ content: "Host" });
167 dir.table.add_column({ content: "Mailbox" });
168 } else {
169 dir.table.add_column({ content: "Reports" });
170 dir.table.add_column({ content: "Location" });
171 }
172 fm.appendChild(dir.table.element());
173 let bb = document.createElement("div");
174 bb.setAttribute("class", "buttons-block");
175 fm.appendChild(bb);
176 let sb = this._create_button_element("submit", "Load reports");
177 sb.disabled = true;
178 bb.appendChild(sb);
179
180 fm.addEventListener("submit", function(event) {
181 sb.disabled = true;
182 let ids = dir.list.filter(function(it) {
183 return it.checked;
184 }).map(function(it) {
185 return it.id;
186 });
187 let cmd = null;
188 switch (type) {
189 case "email":
190 cmd = "load-mailbox";
191 break;
192 case "local":
193 cmd = "load-directory";
194 break;
195 case "remote":
196 cmd = "load-remotefs";
197 break;
198 }
199 let that = this;
200 window.fetch("files.php", {
201 method: "POST",
202 headers: Object.assign(HTTP_HEADERS, HTTP_HEADERS_POST),
203 credentials: "same-origin",
204 body: JSON.stringify({ cmd: cmd, ids: ids })
205 }).then(function(resp) {
206 if (!resp.ok)
207 throw new Error("Failed to load report files");
208 return resp.json();
209 }).then(function(data) {
210 if (!data.error_code) {
211 Notification.add({ text: (data.message || "Loaded successfully!"), type: "info" });
212 }
213 if (data.other_errors) {
214 that._notify_other_errors(data.other_errors);
215 }
216 Common.checkResult(data);
217 Status.instance().update().catch(() => {});
218 }).catch(function(err) {
219 Common.displayError(err);
220 Notification.add({ text: (err.message || "Error!"), type: "error" });
221 }).finally(function() {
222 that._fetch_data(false, type === "email", type === "local", type === "remote");
223 });
224 event.preventDefault();
225 }.bind(this));
226
227 dir.element.appendChild(fm);
228 }
229
230 _display_files_info() {
231 this._fcount_info = document.createElement("div");
232 this._fcount_info.classList.add("state-gray");
233 let dv = this._fcount_info.appendChild(document.createElement("div"));
234 dv.classList.add("state-text");
235 dv.append("You can upload not more than " + this._limits.upload_max_file_count + " files.");
236
237 this._fsize_info = document.createElement("div");
238 this._fsize_info.classList.add("state-gray");
239 dv = this._fsize_info.appendChild(document.createElement("div"));
240 dv.classList.add("state-text");
241 dv.append("You can upload a file with no more than " + bytes2size(this._limits.upload_max_file_size) + ".");
242
243 dv = this._fieldset1.appendChild(document.createElement("div"));
244 dv.classList.add("info-block");
245 dv.append(this._fcount_info, this._fsize_info);
246 }
247
248 _update_directory_loading_element(dir) {
249 dir.table.clear();
250 let d = {};
251 d.rows = dir.list.map(function(it) {
252 it.checked = false;
253 let rd = {
254 cells: [
255 new DirectoryCheckboxCell(it.error ? null : false),
256 { content: it.name }
257 ],
258 userdata: it
259 };
260 if (it.error) {
261 rd.class = "state-red";
262 rd.cells.push({ content: "Error!", class: "state-text" });
263 } else {
264 if (it.files !== undefined) {
265 rd.cells.push({ content: it.files });
266 } else {
267 rd.cells.push({ content: it.host });
268 }
269 }
270 rd.cells.push({ content: it.location || it.mailbox });
271 return rd;
272 });
273 dir.table.add_frame(new ITableFrame(d, dir.table.last_row_index() + 1));
274 }
275
276 _update_directory_button(item) {
277 item.element.querySelector("button[type=submit]").disabled = !item.list.some(function(it) {
278 return it.checked;
279 });
280 }
281
282 _clear_warnings() {
283 [ this._fcount_info, this._fsize_info ].forEach(function(el) {
284 if (el) {
285 el.classList.remove("state-red");
286 el.classList.add("state-gray");
287 }
288 });
289 }
290
291 _notify_other_errors(errors) {
292 let cut = null;
293 let length = errors.length;
294 if (length > 4) {
295 cut = errors.slice(0, 3);
296 cut.push("and " + (length - 3) + " more errors");
297 }
298 Notification.add({ text: cut || errors, type: "error" });
299 }
300
301 _set_warning(el) {
302 if (el) {
303 el.classList.remove("state-gray");
304 el.classList.add("state-red");
305 }
306 }
307
308 _check_files(fl_el) {
309 this._clear_warnings();
310
311 if (fl_el.files.length == 0) {
312 return false;
313 }
314
315 let res = true;
316 if (fl_el.files.length > this._limits.upload_max_file_count) {
317 res = false;
318 this._set_warning(this._fcount_info);
319 let message = "You can only upload " + this._limits.upload_max_file_count + " files.";
320 Notification.add({ type: "error", text: message, delay: 10000, name: "max-files" });
321 }
322
323 let bf_cnt = 0;
324 for (let i = 0; i < fl_el.files.length; ++i) {
325 if (fl_el.files[i].size > this._limits.upload_max_file_size) {
326 ++bf_cnt;
327 }
328 };
329 if (bf_cnt > 0) {
330 res = false;
331 this._set_warning(this._fsize_info);
332 Notification.add({
333 type: "error",
334 text: "" + bf_cnt + " file" + (bf_cnt > 1 && "s" || "") + " exceed the maximum allowed size.",
335 delay: 10000,
336 name: "max-size"
337 });
338 }
339
340 return res;
341 }
342
343 _create_button_element(type, text) {
344 let el = document.createElement("button");
345 el.setAttribute("type", type);
346 el.appendChild(document.createTextNode(text));
347 return el;
348 }
349
350 _create_input_element(type, name, value) {
351 let el = document.createElement("input");
352 el.setAttribute("type", type);
353 if (name)
354 el.setAttribute("name", name);
355 if (value)
356 el.setAttribute("value", value);
357 return el;
358 }
359
360 _fetch_data(files, ems, dirs, rfs) {
361 if (files) {
362 this._fieldset1.disabled = true;
363 this._fieldset1.insertBefore(set_wait_status(), this._fieldset1.children[0]);
364 }
365 let dmap = new Map();
366 if (ems) dmap.set("mailboxes", this._directories.email);
367 if (dirs) dmap.set("directories", this._directories.local);
368 if (rfs) dmap.set("remotefs", this._directories.remote);
369 for (let dir of dmap.values()) {
370 let el = dir.element;
371 el.disabled = true;
372 el.insertBefore(set_wait_status(), el.children[0]);
373 }
374 let that = this;
375 window.fetch("files.php", {
376 method: "GET",
377 headers: HTTP_HEADERS,
378 credentials: "same-origin"
379 }).then(function(resp) {
380 if (!resp.ok)
381 throw new Error("Failed to get loader data");
382 return resp.json();
383 }).then(function(data) {
384 Common.checkResult(data);
385 if (files) {
386 that._limits.upload_max_file_count = data.upload_max_file_count;
387 that._limits.upload_max_file_size = data.upload_max_file_size;
388 that._display_files_info();
389 that._fieldset1.disabled = false;
390 }
391 for (let key of dmap.keys()) {
392 let dir = dmap.get(key);
393 dir.list = data[key] || [];
394 that._update_directory_loading_element(dir);
395 dir.element.disabled = false;
396 }
397 }).catch(function(err) {
398 Common.displayError(err);
399 Notification.add({ type: "error", text: err.message });
400 if (files) {
401 that._fieldset1.insertBefore(set_error_status(null, err.message), that._fieldset1.children[0]);
402 }
403 for (let dir of dmap.values()) {
404 dir.element.insertBefore(set_error_status(null, err.message), dir.element.children[0]);
405 }
406 }).finally(function() {
407 if (files) {
408 that._fieldset1.querySelector(".wait-message").remove();
409 }
410 for (let dir of dmap.values()) {
411 dir.element.querySelector(".wait-message").remove();
412 }
413 });
414 }
415 }
416
417 class DirectoryCheckboxCell extends ITableCell {
418 value(target) {
419 if (target === "dom") {
420 let cb = document.createElement("input");
421 cb.setAttribute("type", "checkbox");
422 if (this._content !== null) {
423 cb.checked = this._content;
424 }
425 else {
426 cb.disabled = true;
427 cb.checked = false;
428 }
429 return cb;
430 }
431 return this._content;
432 }
433 }