| 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 |
namespace Liuch\DmarcSrg\Report; |
| 33 |
|
| 34 |
use Liuch\DmarcSrg\Core; |
| 35 |
use Liuch\DmarcSrg\ErrorHandler; |
| 36 |
use Liuch\DmarcSrg\Report\Report; |
| 37 |
use Liuch\DmarcSrg\Sources\Source; |
| 38 |
use Liuch\DmarcSrg\ReportLog\ReportLogItem; |
| 39 |
use Liuch\DmarcSrg\Exception\RuntimeException; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class ReportFetcher |
| 45 |
{ |
| 46 |
private $source = null; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function __construct($sou) |
| 56 |
{ |
| 57 |
$this->source = $sou; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function fetch(): array |
| 67 |
{ |
| 68 |
try { |
| 69 |
$this->source->rewind(); |
| 70 |
} catch (RuntimeException $e) { |
| 71 |
return [[ 'source_error' => $e->getMessage() ]]; |
| 72 |
} |
| 73 |
|
| 74 |
$core = Core::instance(); |
| 75 |
$stype = $this->source->type(); |
| 76 |
switch ($stype) { |
| 77 |
case Source::SOURCE_MAILBOX: |
| 78 |
$s_act = $core->config('fetcher/mailboxes/when_done', ''); |
| 79 |
$f_act = $core->config('fetcher/mailboxes/when_failed', ''); |
| 80 |
$limit = $core->config('fetcher/mailboxes/messages_maximum', 0); |
| 81 |
break; |
| 82 |
case Source::SOURCE_DIRECTORY: |
| 83 |
$s_act = $core->config('fetcher/directories/when_done', ''); |
| 84 |
$f_act = $core->config('fetcher/directories/when_failed', ''); |
| 85 |
$limit = $core->config('fetcher/directories/files_maximum', 0); |
| 86 |
break; |
| 87 |
case Source::SOURCE_REMOTE_FILESYSTEM: |
| 88 |
$s_act = $core->config('fetcher/remote_filesystems/when_done', ''); |
| 89 |
$f_act = $core->config('fetcher/remote_filesystems/when_failed', ''); |
| 90 |
$limit = $core->config('fetcher/remote_filesystems/files_maximum', 0); |
| 91 |
break; |
| 92 |
case Source::SOURCE_UPLOADED_FILE: |
| 93 |
$s_act = ''; |
| 94 |
$f_act = ''; |
| 95 |
$limit = 0; |
| 96 |
break; |
| 97 |
default: |
| 98 |
throw new RuntimeException('Unknown source type'); |
| 99 |
} |
| 100 |
$limit = intval($limit); |
| 101 |
if ($stype === Source::SOURCE_MAILBOX || |
| 102 |
$stype === Source::SOURCE_DIRECTORY || |
| 103 |
$stype === Source::SOURCE_REMOTE_FILESYSTEM |
| 104 |
) { |
| 105 |
$this->source->setParams([ |
| 106 |
'when_done' => $s_act, |
| 107 |
'when_failed' => $f_act |
| 108 |
]); |
| 109 |
} |
| 110 |
|
| 111 |
$results = []; |
| 112 |
while ($this->source->valid()) { |
| 113 |
$result = null; |
| 114 |
$fname = null; |
| 115 |
$report = null; |
| 116 |
$success = false; |
| 117 |
$err_msg = null; |
| 118 |
|
| 119 |
|
| 120 |
try { |
| 121 |
$rfile = $this->source->current(); |
| 122 |
$fname = $rfile->filename(); |
| 123 |
$report = Report::fromXmlFile($rfile->datastream()); |
| 124 |
$result = $report->save($fname); |
| 125 |
$success = true; |
| 126 |
} catch (RuntimeException $e) { |
| 127 |
$err_msg = $e->getMessage(); |
| 128 |
$result = ErrorHandler::exceptionResult($e); |
| 129 |
} |
| 130 |
unset($rfile); |
| 131 |
|
| 132 |
|
| 133 |
try { |
| 134 |
if ($success) { |
| 135 |
$this->source->accepted(); |
| 136 |
} else { |
| 137 |
$this->source->rejected(); |
| 138 |
} |
| 139 |
} catch (RuntimeException $e) { |
| 140 |
$err_msg = $e->getMessage(); |
| 141 |
$result['post_processing_message'] = $err_msg; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
if (!$err_msg) { |
| 146 |
$log = ReportLogItem::success($stype, $report, $fname, null)->save(); |
| 147 |
} else { |
| 148 |
$log = ReportLogItem::failed($stype, $report, $fname, $err_msg)->save(); |
| 149 |
if ($this->source->type() === Source::SOURCE_MAILBOX) { |
| 150 |
$msg = $this->source->mailMessage(); |
| 151 |
$ov = $msg->overview(); |
| 152 |
if (isset($ov['from'])) { |
| 153 |
$result['emailed_from'] = $ov['from']; |
| 154 |
} |
| 155 |
if (isset($ov['date'])) { |
| 156 |
$result['emailed_date'] = $ov['date']->format(\DateTime::RFC1123); |
| 157 |
} |
| 158 |
} |
| 159 |
if ($report) { |
| 160 |
if (isset($report->report_id)) { |
| 161 |
$result['report_id'] = $report->report_id; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
unset($report); |
| 166 |
|
| 167 |
|
| 168 |
$results[] = $result; |
| 169 |
|
| 170 |
|
| 171 |
if ($limit > 0) { |
| 172 |
if (--$limit === 0) { |
| 173 |
break; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
$this->source->next(); |
| 178 |
} |
| 179 |
return $results; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public static function makeSummaryResult(array $results): array |
| 190 |
{ |
| 191 |
$reps = []; |
| 192 |
$others = []; |
| 193 |
$r_count = 0; |
| 194 |
$loaded = 0; |
| 195 |
foreach ($results as &$r) { |
| 196 |
if (isset($r['source_error'])) { |
| 197 |
$others[] = $r['source_error']; |
| 198 |
} else { |
| 199 |
$reps[] = $r; |
| 200 |
++$r_count; |
| 201 |
if (!isset($r['error_code']) || $r['error_code'] === 0) { |
| 202 |
++$loaded; |
| 203 |
} |
| 204 |
if (isset($r['post_processing_message'])) { |
| 205 |
$others[] = $r['post_processing_message']; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
unset($r); |
| 210 |
|
| 211 |
$result = null; |
| 212 |
$o_count = count($others); |
| 213 |
if ($r_count + $o_count === 1) { |
| 214 |
if ($r_count === 1) { |
| 215 |
$result = $reps[0]; |
| 216 |
} else { |
| 217 |
$result = [ |
| 218 |
'error_code' => -1, |
| 219 |
'message' => $others[0] |
| 220 |
]; |
| 221 |
} |
| 222 |
} else { |
| 223 |
$err_code = null; |
| 224 |
$message = null; |
| 225 |
if ($loaded === $r_count) { |
| 226 |
$err_code = 0; |
| 227 |
if ($r_count > 0) { |
| 228 |
$message = strval($r_count) . ' report files have been loaded successfully'; |
| 229 |
} elseif ($o_count === 0) { |
| 230 |
$message = 'There are no report files to load'; |
| 231 |
} else { |
| 232 |
$err_code = -1; |
| 233 |
} |
| 234 |
} else { |
| 235 |
$err_code = -1; |
| 236 |
if ($loaded > 0) { |
| 237 |
$message = "Only {$loaded} of the {$r_count} report files have been loaded"; |
| 238 |
} else { |
| 239 |
$message = "None of the {$r_count} report files has been loaded"; |
| 240 |
} |
| 241 |
} |
| 242 |
$result['error_code'] = $err_code; |
| 243 |
$result['message'] = $message; |
| 244 |
if ($r_count > 0) { |
| 245 |
$result['results'] = $reps; |
| 246 |
} |
| 247 |
if ($o_count > 0) { |
| 248 |
$result['other_errors'] = $others; |
| 249 |
} |
| 250 |
} |
| 251 |
return $result; |
| 252 |
} |
| 253 |
} |