Debian Codegraph @

public/summary.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2022-2025 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * =========================
23 *
24 * This script is to get summary report via the web interface
25 *
26 * HTTP GET query:
27 * when the header 'Accept' is 'application/json':
28 * if parameter `mode` is `options`, it returs data for the report options dialog;
29 * if parameter `mode` is `report`, it returns report data for the specified domain and period
30 * otherwise:
31 * it returns the content of the template.html file
32 * Other HTTP methods:
33 * it returns an error
34 *
35 * All the data is in json format.
36 *
37 * @category Web
38 * @package DmarcSrg
39 * @author Aleksey Andreev (liuch)
40 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
41 */
42
43 namespace Liuch\DmarcSrg;
44
45 use Liuch\DmarcSrg\Users\User;
46 use Liuch\DmarcSrg\Domains\Domain;
47 use Liuch\DmarcSrg\Domains\DomainList;
48 use Liuch\DmarcSrg\Report\SummaryReport;
49 use Liuch\DmarcSrg\Report\OverallReport;
50 use Liuch\DmarcSrg\Exception\SoftException;
51 use Liuch\DmarcSrg\Exception\RuntimeException;
52
53 require realpath(__DIR__ . '/..') . '/init.php';
54
55 if (Core::isJson() && isset($_GET['mode'])) {
56 try {
57 $core = Core::instance();
58 $core->auth()->isAllowed(User::LEVEL_USER);
59 $user = $core->getCurrentUser();
60
61 $mode = $_GET['mode'];
62 if ($mode === 'options') {
63 Core::sendJson(
64 [
65 'domains' => (new DomainList($user))->names()
66 ]
67 );
68 return;
69 } elseif ($mode === 'report') {
70 if (empty($_GET['domain'])) {
71 throw new SoftException('Parameter "domain" is not specified');
72 }
73 if (empty($_GET['period'])) {
74 throw new SoftException('Parameter "period" is not specified');
75 }
76
77 $format = $_GET['format'] ?? '';
78 if (!in_array($format, [ 'raw', 'text', 'csv' ])) {
79 throw new SoftException('The `format` parameter can only be `raw`, `text` or `csv`');
80 }
81
82 $ovr = null;
83 $rep = new SummaryReport($_GET['period']);
84 if (true) { // TODO this optionally using GET paramenter
85 $ovr = new OverallReport();
86 $rep->bindSection($ovr);
87 }
88 $reports = [];
89 foreach (explode(',', $_GET['domain']) as $d) {
90 $dom = new Domain($d);
91 $dom->isAssigned($user, true);
92 $rep->setDomain($dom);
93 $r = [ 'domain' => $d ];
94 switch ($format) {
95 case 'raw':
96 $r['data'] = $rep->toArray();
97 break;
98 case 'text':
99 $r['text'] = $rep->text();
100 break;
101 case 'csv':
102 $r['csv'] = $rep->csv();
103 break;
104 }
105 $reports[] = $r;
106 }
107 $res['reports'] = $reports;
108 if ($ovr) {
109 $r = [];
110 switch ($format) {
111 case 'raw':
112 $r['data'] = $ovr->toArray();
113 break;
114 case 'text':
115 $r['text'] = $ovr->text();
116 break;
117 case 'csv':
118 $r['csv'] = $ovr->csv();
119 break;
120 }
121 $res['overall'] = $r;
122 }
123
124 Core::sendJson($res);
125 return;
126 } else {
127 throw new SoftException('The `mode` parameter can only be `options` or `report`');
128 }
129 } catch (RuntimeException $e) {
130 Core::sendJson(ErrorHandler::exceptionResult($e));
131 return;
132 }
133 } elseif (Core::requestMethod() == 'GET') {
134 Core::instance()->sendHtml();
135 return;
136 }
137
138 Core::sendBad();