Debian Codegraph @

public/status.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) 2020-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 namespace Liuch\DmarcSrg;
24
25 use Liuch\DmarcSrg\Users\User;
26 use Liuch\DmarcSrg\Domains\Domain;
27 use Liuch\DmarcSrg\Settings\SettingsList;
28 use Liuch\DmarcSrg\Exception\RuntimeException;
29
30 require realpath(__DIR__ . '/..') . '/init.php';
31
32 if (Core::isJson()) {
33 if (Core::requestMethod() == 'GET') {
34 try {
35 $core = Core::instance();
36 $core->auth()->isAllowed(User::LEVEL_USER);
37
38 $fields = explode(',', $_GET['fields'] ?? '');
39 if (in_array('state', $fields)) {
40 $result = $core->status()->get();
41 } else {
42 $result = [];
43 }
44
45 if (!($result['error_code'] ?? 0)) {
46 if (in_array('user', $fields)) {
47 $user = $core->getCurrentUser();
48 $result['user'] = [
49 'name' => $user->name(),
50 'level' => User::levelToString($user->level())
51 ];
52 }
53
54 if (in_array('settings', $fields) && !empty($_GET['settings'])) {
55 $settings = [];
56 foreach (explode(',', $_GET['settings']) as $name) {
57 $setting = SettingsList::getSettingByName($name);
58 $settings[$name] = $setting->value();
59 }
60 $result['settings'] = $settings;
61 }
62
63 if (in_array('emails', $fields)) {
64 $filter = Common::getFilter();
65 if (isset($filter['domain'])) {
66 $domain = new Domain($filter['domain']);
67 } else {
68 $domain = null;
69 }
70 if (isset($filter['month'])) {
71 $range = Common::monthToRange($filter['month']);
72 $stat = Statistics::fromTo($domain, $range[0], $range[1]);
73 $days = $range[0]->format('F Y') . ' (filtered)';
74 } else {
75 $days = SettingsList::getSettingByName('status.emails-for-last-n-days')->value();
76 $stat = Statistics::lastNDays($domain, $days);
77 $days = "the last {$days} days";
78 }
79 $stat->setUser($core->getCurrentUser());
80 if ($filter) {
81 $stat->setFilter($filter);
82 }
83 $result['emails'] = $stat->summary()['emails'];
84 $result['emails']['days'] = $days;
85 }
86 }
87
88 Core::sendJson($result);
89 } catch (RuntimeException $e) {
90 $r = ErrorHandler::exceptionResult($e);
91 Core::sendJson($r);
92 }
93 return;
94 }
95 }
96
97 Core::sendBad();