Debian Codegraph @

public/list.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\Report\ReportList;
27 use Liuch\DmarcSrg\Exception\RuntimeException;
28
29 require realpath(__DIR__ . '/..') . '/init.php';
30
31 if (Core::requestMethod() == 'GET') {
32 if (Core::isJson() && isset($_GET['list'])) {
33 try {
34 $core = Core::instance();
35 $core->auth()->isAllowed(User::LEVEL_USER);
36
37 $lst = explode(',', $_GET['list']);
38 $res = [];
39 $first_page = false;
40 if (array_search('reports', $lst) !== false) {
41 $pos = isset($_GET['position']) ? intval($_GET['position']) : 0;
42 $dir = isset($_GET['direction']) ? $_GET['direction'] : 'ascent';
43 $order = isset($_GET['order']) ? $_GET['order'] : 'begin_time';
44 $filter = Common::getFilter();
45 $list = new ReportList($core->getCurrentUser());
46 if ($filter) {
47 $list->setFilter($filter);
48 }
49 $n_dir = $dir === 'ascent' ?
50 ReportList::ORDER_ASCENT : ReportList::ORDER_DESCENT;
51 $n_order = $order === 'begin_time' ?
52 ReportList::ORDER_BEGIN_TIME : ReportList::ORDER_NONE;
53 $list->setOrder($n_order, $n_dir);
54 $res = $list->getList($pos);
55 $first_page = ($pos === 0);
56 }
57 if (array_search('count', $lst) !== false) {
58 if ($first_page && !$res['more'] && intval($_GET['position'] ?? 0) === 0) {
59 $tc = 0;
60 $uc = 0;
61 foreach ($res['reports'] as $r) {
62 if (!$r['seen']) {
63 ++$uc;
64 }
65 ++$tc;
66 }
67 } else {
68 $list = new ReportList($core->getCurrentUser());
69 $filter = Common::getFilter();
70 if ($filter) {
71 $list->setFilter($filter);
72 }
73 $tc = $list->count();
74 if (!$filter) {
75 $filter = [];
76 }
77 $status = $filter['status'] ?? '';
78 if ($status === 'unread') {
79 $uc = $tc;
80 } elseif ($status === 'read') {
81 $uc = 0;
82 } else {
83 $filter['status'] = 'unread';
84 $uc = $list->setFilter($filter)->count();
85 }
86 }
87 $res['count'] = [ 'total' => $tc, 'unread' => $uc ];
88 }
89 if (array_search('filters', $lst) !== false) {
90 $res['filters'] = (new ReportList($core->getCurrentUser()))->getFilterList();
91 }
92 Core::sendJson($res);
93 } catch (RuntimeException $e) {
94 Core::sendJson(ErrorHandler::exceptionResult($e));
95 }
96 return;
97 }
98 Core::instance()->sendHtml();
99 return;
100 }
101
102 Core::sendBad();