Debian Codegraph @

utils/reports_cleaner.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-2024 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 deletes old reports from DB.
25 * The conditions for removal must be specified in the configuration file.
26 * The best place to use it is cron.
27 * Note: You must leave enough reports if you want to get correct summary report.
28 *
29 * @category Utilities
30 * @package DmarcSrg
31 * @author Aleksey Andreev (liuch)
32 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
33 */
34
35 namespace Liuch\DmarcSrg;
36
37 use Liuch\DmarcSrg\Report\ReportList;
38 use Liuch\DmarcSrg\Exception\RuntimeException;
39
40 require realpath(__DIR__ . '/..') . '/init.php';
41
42 if (Core::isWEB()) {
43 echo 'Forbidden';
44 exit(1);
45 }
46
47 $core = Core::instance();
48 $core->setCurrentUser('admin');
49
50 $days = $core->config('cleaner/reports/days_old', -1);
51 if (gettype($days) !== 'integer' || $days < 0) {
52 exit(0);
53 }
54 $days_date = (new DateTime())->sub(new \DateInterval("P{$days}D"));
55
56 $maximum = $core->config('cleaner/reports/delete_maximum', 0);
57 if (gettype($maximum) !== 'integer' || $maximum < 0) {
58 exit(0);
59 }
60
61 $leave = $core->config('cleaner/reports/leave_minimum', 0);
62 if (gettype($leave) !== 'integer' || $leave < 0) {
63 exit(0);
64 }
65
66 try {
67 $rl = new ReportList($core->getCurrentUser());
68 $cnt = $rl->count() - $leave;
69 if ($cnt > 0) {
70 $rl->setFilter([ 'before_time' => $days_date ]);
71 if ($leave * $maximum !== 0) {
72 if ($maximum > 0 && $cnt > $maximum) {
73 $cnt = $maximum;
74 }
75 $rl->setMaxCount($cnt);
76 $rl->setOrder(ReportList::ORDER_BEGIN_TIME, ReportList::ORDER_ASCENT);
77 }
78 $rl->delete();
79 }
80 } catch (RuntimeException $e) {
81 echo ErrorHandler::exceptionText($e);
82 exit(1);
83 }
84
85 exit(0);