Debian Codegraph @

utils/mailbox_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 DMARC report email messages in mailboxes.
25 * The mailbox parameters and conditions for removal must be specified
26 * in the configuration file. The mailbox accessibility can be checked
27 * on the administration page in the web interface.
28 * The best place to use it is cron.
29 *
30 * @category Utilities
31 * @package DmarcSrg
32 * @author Aleksey Andreev (liuch)
33 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
34 */
35
36 namespace Liuch\DmarcSrg;
37
38 use Liuch\DmarcSrg\Mail\MailBox;
39 use Liuch\DmarcSrg\Mail\MailBoxes;
40 use Liuch\DmarcSrg\Sources\SourceAction;
41 use Liuch\DmarcSrg\Exception\RuntimeException;
42
43 require realpath(__DIR__ . '/..') . '/init.php';
44
45 if (Core::isWEB()) {
46 echo 'Forbidden';
47 exit(1);
48 }
49
50 $core = Core::instance();
51 $core->setCurrentUser('admin');
52
53 $days = $core->config('cleaner/mailboxes/days_old', -1);
54 if (gettype($days) !== 'integer' || $days < 0) {
55 exit(0);
56 }
57 $days_date = (new DateTime())->sub(new \DateInterval("P{$days}D"));
58
59 $maximum = $core->config('cleaner/mailboxes/delete_maximum', 0);
60 if (gettype($maximum) !== 'integer' || $maximum < 0) {
61 exit(0);
62 }
63
64 $leave = $core->config('cleaner/mailboxes/leave_minimum', 0);
65 if (gettype($leave) !== 'integer' || $leave < 0) {
66 exit(0);
67 }
68
69 // Get a list of the configured email boxes
70 $mb_list = new MailBoxes();
71 $mb_cnt = $mb_list->count();
72 if ($mb_cnt === 0) {
73 // There are no configured mailboxes
74 exit(0);
75 }
76
77 // Get the names of mailboxes where processed messages are moved to.
78 $dirs = [];
79 foreach ([ [ 'done', '', 1 ], [ 'failed', 'failed', 0 ] ] as $it) {
80 $opt_nm = $it[0];
81 $def_opt = $it[1];
82 $def_cri = $it[2];
83 $actions = SourceAction::fromSetting($core->config("fetcher/mailboxes/when_{$opt_nm}", ''), 0, '');
84 if (count($actions) === 0) {
85 $dir = $def_opt;
86 } else {
87 $dir = null;
88 foreach ($actions as $act) {
89 if ($act->type === SourceAction::ACTION_MOVE) {
90 $dir = $act->param;
91 break;
92 } elseif ($act->type === SourceAction::ACTION_SEEN) {
93 $dir = $def_opt;
94 }
95 }
96 if (is_null($dir)) {
97 continue;
98 }
99 }
100 switch (strtolower($core->config("cleaner/mailboxes/{$opt_nm}", ''))) {
101 case 'any':
102 $cri = 2;
103 break;
104 case 'seen':
105 $cri = 1;
106 break;
107 case '':
108 $cri = $def_cri;
109 break;
110 default:
111 $cri = 0;
112 break;
113 }
114 if (empty($dir) && $cri > 1) {
115 $cri = 1;
116 }
117 $dirs[$dir] = min(($dirs[$dir] ?? $cri), $cri);
118 }
119
120 try {
121 for ($mb_idx = 1; $mb_idx <= $mb_cnt; ++$mb_idx) {
122 foreach ($dirs as $dir_name => $i_criteria) {
123 if ($i_criteria > 0) {
124 $criteria = $i_criteria === 2 ? MailBox::SEARCH_ALL : MailBox::SEARCH_SEEN;
125 $mbox = $mb_list->mailbox($mb_idx);
126 if (!empty($dir_name)) {
127 if (!($mbox = $mbox->childMailbox($dir_name))) {
128 continue;
129 }
130 }
131 $s_res = $mbox->messages($criteria, MailBox::ORDER_ASCENT);
132 $max = $maximum > 0 ? $maximum : -1;
133 $lv = $leave === 0 ? count($s_res) : count($s_res) - $leave;
134 foreach ($s_res as $msg) {
135 if ($lv-- <= 0) {
136 break;
137 }
138 $mo = $msg->overview();
139 if (isset($mo['date'])) {
140 if ($mo['date'] > $days_date) {
141 break;
142 }
143 $msg->delete();
144 if ($max === 1) {
145 break;
146 }
147 if ($max > 0) {
148 --$max;
149 }
150 }
151 }
152 }
153 }
154 }
155 } catch (RuntimeException $e) {
156 echo ErrorHandler::exceptionText($e);
157 exit(1);
158 }
159
160 exit(0);