| 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 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
namespace Liuch\DmarcSrg; |
| 46 |
|
| 47 |
use Liuch\DmarcSrg\Mail\MailBoxes; |
| 48 |
use Liuch\DmarcSrg\Report\ReportFetcher; |
| 49 |
use Liuch\DmarcSrg\Sources\Source; |
| 50 |
use Liuch\DmarcSrg\Sources\MailboxSource; |
| 51 |
use Liuch\DmarcSrg\Sources\DirectorySource; |
| 52 |
use Liuch\DmarcSrg\Sources\RemoteFilesystemSource; |
| 53 |
use Liuch\DmarcSrg\Directories\DirectoryList; |
| 54 |
use Liuch\DmarcSrg\Exception\SoftException; |
| 55 |
use Liuch\DmarcSrg\Exception\RuntimeException; |
| 56 |
use Liuch\DmarcSrg\RemoteFilesystems\RemoteFilesystemList; |
| 57 |
|
| 58 |
require realpath(__DIR__ . '/..') . '/init.php'; |
| 59 |
|
| 60 |
if (Core::isWEB()) { |
| 61 |
echo 'Forbidden'; |
| 62 |
exit(1); |
| 63 |
} |
| 64 |
|
| 65 |
if (!isset($argv)) { |
| 66 |
echo 'Cannot get the script arguments. Probably register_argc_argv is disabled.', PHP_EOL; |
| 67 |
exit(1); |
| 68 |
} |
| 69 |
|
| 70 |
$usage = false; |
| 71 |
$source = null; |
| 72 |
for ($i = 1; $i < count($argv); ++$i) { |
| 73 |
$av = explode('=', $argv[$i]); |
| 74 |
if (count($av) !== 2) { |
| 75 |
echo 'Invalid parameter format' . PHP_EOL; |
| 76 |
$usage = true; |
| 77 |
break; |
| 78 |
} |
| 79 |
switch ($av[0]) { |
| 80 |
case 'source': |
| 81 |
$source = $av[1]; |
| 82 |
break; |
| 83 |
default: |
| 84 |
echo 'Unknown parameter "' . $av[0] . '"' . PHP_EOL; |
| 85 |
$usage = true; |
| 86 |
break; |
| 87 |
} |
| 88 |
} |
| 89 |
if ($source && !in_array($source, [ 'email', 'directory', 'remotefs' ])) { |
| 90 |
echo 'Invalid source type "' . $source . '". "email", "directory" or "remotefs" expected.' . PHP_EOL; |
| 91 |
exit(1); |
| 92 |
} |
| 93 |
|
| 94 |
if ($usage) { |
| 95 |
echo PHP_EOL; |
| 96 |
echo "Usage: {$argv[0]} [source=email|directory|remotefs]", PHP_EOL; |
| 97 |
exit(1); |
| 98 |
} |
| 99 |
|
| 100 |
$sou_list = []; |
| 101 |
$problems = []; |
| 102 |
$addError = function (\Throwable $e, array &$errors): void { |
| 103 |
$err_r = ErrorHandler::exceptionResult($e); |
| 104 |
$errors['messages'][] = $err_r['message']; |
| 105 |
if (!$errors['debug_info'] && isset($err_r['debug_info'])) { |
| 106 |
$errors['debug_info'] = $err_r['debug_info']['content']; |
| 107 |
} |
| 108 |
}; |
| 109 |
$updateProblems = function (int $state, array &$errors, array &$problems): void { |
| 110 |
if (count($errors['messages']) > 0) { |
| 111 |
$problems[] = [ |
| 112 |
'state' => $state, |
| 113 |
'messages' => $errors['messages'], |
| 114 |
'debug_info' => $errors['debug_info'] |
| 115 |
]; |
| 116 |
} |
| 117 |
}; |
| 118 |
|
| 119 |
const MAILBOX_LIST = 1; |
| 120 |
const DIRECTORY_LIST = 2; |
| 121 |
const REMOTEFS_LIST = 3; |
| 122 |
const FETCHER = 4; |
| 123 |
|
| 124 |
$core = Core::instance(); |
| 125 |
$core->setCurrentUser('admin'); |
| 126 |
|
| 127 |
$state = MAILBOX_LIST; |
| 128 |
if (!$source || $source === 'email') { |
| 129 |
$mb_list = new MailBoxes(); |
| 130 |
$mb_cnt = $mb_list->count(); |
| 131 |
if ($mb_cnt > 0) { |
| 132 |
$errors = [ 'messages' => [], 'debug_info' => null ]; |
| 133 |
try { |
| 134 |
$core->checkDependencies('imap,xml,zip'); |
| 135 |
for ($mb_num = 1; $mb_num <= $mb_cnt; ++$mb_num) { |
| 136 |
try { |
| 137 |
$sou_list[] = new MailboxSource($mb_list->mailbox($mb_num)); |
| 138 |
} catch (RuntimeException $e) { |
| 139 |
$addError($e, $errors); |
| 140 |
} |
| 141 |
} |
| 142 |
} catch (SoftException $e) { |
| 143 |
$errors['messages'][] = $e->getMessage(); |
| 144 |
} |
| 145 |
$updateProblems($state, $errors, $problems); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
$state = DIRECTORY_LIST; |
| 150 |
if (!$source || $source === 'directory') { |
| 151 |
$errors = [ 'messages' => [], 'debug_info' => null ]; |
| 152 |
try { |
| 153 |
$core->checkDependencies('xml,zip'); |
| 154 |
foreach ((new DirectoryList())->list() as $dir) { |
| 155 |
try { |
| 156 |
$sou_list[] = new DirectorySource($dir); |
| 157 |
} catch (RuntimeException $e) { |
| 158 |
$addError($e, $errors); |
| 159 |
} |
| 160 |
} |
| 161 |
} catch (SoftException $e) { |
| 162 |
$errors['messages'][] = $e->getMessage(); |
| 163 |
} |
| 164 |
$updateProblems($state, $errors, $problems); |
| 165 |
} |
| 166 |
|
| 167 |
$state = REMOTEFS_LIST; |
| 168 |
if ((!$source && $core->config('remote_filesystems', null)) || $source === 'remotefs') { |
| 169 |
$errors = [ 'messages' => [], 'debug_info' => null ]; |
| 170 |
try { |
| 171 |
$core->checkDependencies('flyfs,xml,zip'); |
| 172 |
foreach ((new RemoteFilesystemList(true))->list() as $fs) { |
| 173 |
try { |
| 174 |
$sou_list[] = new RemoteFilesystemSource($fs); |
| 175 |
} catch (RuntimeException $e) { |
| 176 |
$addError($e, $errors); |
| 177 |
} |
| 178 |
} |
| 179 |
} catch (SoftException $e) { |
| 180 |
$errors['messages'][] = $e->getMessage(); |
| 181 |
} |
| 182 |
$updateProblems($state, $errors, $problems); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
$state = FETCHER; |
| 187 |
try { |
| 188 |
foreach ($sou_list as $source) { |
| 189 |
$results = (new ReportFetcher($source))->fetch(); |
| 190 |
foreach ($results as &$res) { |
| 191 |
$messages = []; |
| 192 |
if (isset($res['source_error'])) { |
| 193 |
$messages[] = $res['source_error']; |
| 194 |
} |
| 195 |
if (isset($res['post_processing_message'])) { |
| 196 |
$messages[] = $res['post_processing_message']; |
| 197 |
} |
| 198 |
if (isset($res['error_code']) && $res['error_code'] !== 0 && isset($res['message'])) { |
| 199 |
$messages[] = $res['message']; |
| 200 |
} |
| 201 |
if (count($messages) > 0) { |
| 202 |
$pr = [ 'state' => $state, 'messages' => $messages ]; |
| 203 |
foreach ([ 'report_id', 'emailed_from', 'emailed_date' ] as $it) { |
| 204 |
if (isset($res[$it])) { |
| 205 |
$pr[$it] = $res[$it]; |
| 206 |
} |
| 207 |
} |
| 208 |
if ($source->type() === Source::SOURCE_MAILBOX) { |
| 209 |
$cont = $source->container(); |
| 210 |
$pr['mailbox'] = $cont->folder() . ' (' . $cont->name() . ')'; |
| 211 |
} |
| 212 |
if (isset($res['debug_info']['content'])) { |
| 213 |
$pr['debug_info'] = $res['debug_info']['content']; |
| 214 |
} |
| 215 |
$problems[] = $pr; |
| 216 |
} |
| 217 |
} |
| 218 |
unset($res); |
| 219 |
} |
| 220 |
} catch (RuntimeException $e) { |
| 221 |
$problems[] = [ 'state' => $state, 'messages' => [ $e->getMessage() ] ]; |
| 222 |
} |
| 223 |
|
| 224 |
if (count($problems) > 0) { |
| 225 |
$debug_info = null; |
| 226 |
foreach ($problems as $i => $pr) { |
| 227 |
if ($i > 0) { |
| 228 |
echo PHP_EOL; |
| 229 |
} |
| 230 |
switch ($pr['state']) { |
| 231 |
case MAILBOX_LIST: |
| 232 |
echo 'Failed to get mailbox list:'; |
| 233 |
break; |
| 234 |
case DIRECTORY_LIST: |
| 235 |
echo 'Failed to get directory list:'; |
| 236 |
break; |
| 237 |
case REMOTEFS_LIST: |
| 238 |
echo 'Failed to get remote filesystem list'; |
| 239 |
break; |
| 240 |
case FETCHER: |
| 241 |
echo 'Failed to get incoming report:'; |
| 242 |
break; |
| 243 |
} |
| 244 |
echo PHP_EOL; |
| 245 |
echo ' Error message:', PHP_EOL; |
| 246 |
$messages = array_map(function ($msg) { |
| 247 |
return " - {$msg}"; |
| 248 |
}, $pr['messages']); |
| 249 |
echo implode(PHP_EOL, $messages), PHP_EOL; |
| 250 |
if (isset($pr['report_id'])) { |
| 251 |
echo " Report ID: {$pr['report_id']}", PHP_EOL; |
| 252 |
} |
| 253 |
if (isset($pr['emailed_from']) || isset($pr['emailed_date']) || isset($pr['mailbox'])) { |
| 254 |
echo ' Email message metadata:', PHP_EOL; |
| 255 |
echo ' - From: ' . ($pr['emailed_from'] ?? '-'), PHP_EOL; |
| 256 |
echo ' - Date: ' . ($pr['emailed_date'] ?? '-'), PHP_EOL; |
| 257 |
echo ' - Mailbox: ' . ($pr['mailbox'] ?? '-'), PHP_EOL; |
| 258 |
} |
| 259 |
if (!$debug_info && !empty($pr['debug_info'])) { |
| 260 |
$debug_info = $pr['debug_info']; |
| 261 |
} |
| 262 |
} |
| 263 |
if ($debug_info) { |
| 264 |
echo PHP_EOL; |
| 265 |
echo 'Debug information:', PHP_EOL, $debug_info, PHP_EOL; |
| 266 |
} |
| 267 |
exit(1); |
| 268 |
} |
| 269 |
|
| 270 |
exit(0); |