Debian Codegraph @

public/logs.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) 2021-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 returns the log items
25 *
26 * HTTP GET query:
27 * When the header 'Accept' is 'application/json':
28 * It returns the item specified in the GET parameter `id` if specified. The parameter must contain integer value.
29 * Otherwise it returns a list of the log items. The request may have the following GET parameters:
30 * `position` integer The position from which the list will be returned. The default value is 0.
31 * `direction` string The sort direction. Can be one of the following values: `ascent`, `descent'.
32 * The default value is `ascent`. The list will be sorted by Event time.
33 * The data will be returned in json format.
34 * Otherwise:
35 * It returns the content of the template.html file.
36 * Other HTTP methods:
37 * It returns an error.
38 *
39 * @category Web
40 * @package DmarcSrg
41 * @author Aleksey Andreev (liuch)
42 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
43 */
44
45 namespace Liuch\DmarcSrg;
46
47 use Liuch\DmarcSrg\Users\User;
48 use Liuch\DmarcSrg\ReportLog\ReportLog;
49 use Liuch\DmarcSrg\ReportLog\ReportLogItem;
50 use Liuch\DmarcSrg\Settings\SettingsList;
51 use Liuch\DmarcSrg\Exception\RuntimeException;
52
53 require realpath(__DIR__ . '/..') . '/init.php';
54
55 if (Core::requestMethod() == "GET") {
56 if (Core::isJson()) {
57 try {
58 Core::instance()->auth()->isAllowed(User::LEVEL_ADMIN);
59
60 if (isset($_GET['id'])) {
61 Core::sendJson(ReportLogItem::byId(intval($_GET['id']))->toArray());
62 return;
63 }
64
65 $pos = $_GET['position'] ?? 0;
66 $dir = $_GET['direction'] ?? '';
67 if (empty($dir)) {
68 $o_set = explode(',', SettingsList::getSettingByName('log-view.sort-list-by')->value());
69 if ($o_set[0] === 'event_time') {
70 $dir = $o_set[1];
71 }
72 }
73 if ($dir !== 'ascent') {
74 $dir = 'descent';
75 }
76
77 $log = new ReportLog();
78 if (($filter = Common::getFilter())) {
79 if (isset($filter['success'])) {
80 $filter['success'] = $filter['success'] === 'true' ? true : false;
81 }
82 foreach ([ 'from_time', 'till_time' ] as $k) {
83 if (isset($filter[$k])) {
84 if (($d = DateTime::createFromFormat('!Y-m-d', $filter[$k]))) {
85 $filter[$k] = $d;
86 } else {
87 unset($filter[$k]);
88 }
89 }
90 }
91 $log->setFilter($filter);
92 }
93 $log->setOrder($dir === 'ascent' ? ReportLog::ORDER_ASCENT : ReportLog::ORDER_DESCENT);
94 $res = $log->getList($pos);
95 $res['sorted_by'] = [ 'column' => 'event_time', 'direction' => $dir ];
96 Core::sendJson($res);
97 return;
98 } catch (RuntimeException $e) {
99 Core::sendJson(ErrorHandler::exceptionResult($e));
100 return;
101 }
102 } else {
103 Core::instance()->sendHtml();
104 return;
105 }
106 }
107
108 Core::sendBad();