Debian Codegraph @

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