| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 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 |
|
| 70 |
$mb_list = new MailBoxes(); |
| 71 |
$mb_cnt = $mb_list->count(); |
| 72 |
if ($mb_cnt === 0) { |
| 73 |
|
| 74 |
exit(0); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 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); |