| 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\Database\Common; |
| 33 |
|
| 34 |
use Liuch\DmarcSrg\Core; |
| 35 |
use Liuch\DmarcSrg\Common; |
| 36 |
use Liuch\DmarcSrg\DateTime; |
| 37 |
use Liuch\DmarcSrg\Report\ReportData; |
| 38 |
use Liuch\DmarcSrg\Settings\SettingsList; |
| 39 |
use Liuch\DmarcSrg\Database\ReportMapperInterface; |
| 40 |
use Liuch\DmarcSrg\Exception\SoftException; |
| 41 |
use Liuch\DmarcSrg\Exception\LogicException; |
| 42 |
use Liuch\DmarcSrg\Exception\DatabaseFatalException; |
| 43 |
use Liuch\DmarcSrg\Exception\DatabaseNotFoundException; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
class CommonReportMapper implements ReportMapperInterface |
| 49 |
{ |
| 50 |
|
| 51 |
private $connector = null; |
| 52 |
|
| 53 |
private static $allowed_domains = null; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function __construct(object $connector) |
| 61 |
{ |
| 62 |
$this->connector = $connector; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function fetch($data): void |
| 78 |
{ |
| 79 |
$db = $this->connector->dbh(); |
| 80 |
try { |
| 81 |
$st = $db->prepare( |
| 82 |
'SELECT rp.id, end_time, loaded_time, email, extra_contact_info,' |
| 83 |
. ' error_string, policy_adkim, policy_aspf, policy_p, policy_sp, policy_np,' |
| 84 |
. ' policy_pct, policy_fo FROM ' . $this->connector->tablePrefix('reports') |
| 85 |
. ' AS rp INNER JOIN ' . $this->connector->tablePrefix('domains') |
| 86 |
. ' AS dom ON dom.id = rp.domain_id' |
| 87 |
. ' WHERE fqdn = ? AND begin_time = ? AND org = ? AND external_id = ?' |
| 88 |
); |
| 89 |
$st->bindValue(1, $data->domain, \PDO::PARAM_STR); |
| 90 |
$st->bindValue(2, $data->date['begin']->format('Y-m-d H:i:s'), \PDO::PARAM_STR); |
| 91 |
$st->bindValue(3, $data->org_name, \PDO::PARAM_STR); |
| 92 |
$st->bindValue(4, $data->report_id, \PDO::PARAM_STR); |
| 93 |
$st->execute(); |
| 94 |
if (!($res = $st->fetch(\PDO::FETCH_NUM))) { |
| 95 |
throw new DatabaseNotFoundException('The report is not found'); |
| 96 |
} |
| 97 |
$id = intval($res[0]); |
| 98 |
$data->date['end'] = new DateTime($res[1]); |
| 99 |
$data->loaded_time = new DateTime($res[2]); |
| 100 |
$data->email = $res[3]; |
| 101 |
$data->extra_contact_info = $res[4]; |
| 102 |
$data->error_string = json_decode($res[5] ?? '', true); |
| 103 |
$data->policy = [ |
| 104 |
'adkim' => $res[6], |
| 105 |
'aspf' => $res[7], |
| 106 |
'p' => $res[8], |
| 107 |
'sp' => $res[9], |
| 108 |
'np' => $res[10], |
| 109 |
'pct' => $res[11], |
| 110 |
'fo' => $res[12] |
| 111 |
]; |
| 112 |
|
| 113 |
$order_str = $this->sqlOrderRecords(); |
| 114 |
$st = $db->prepare( |
| 115 |
'SELECT report_id, ip, rcount, disposition, reason, dkim_auth , spf_auth, dkim_align,' |
| 116 |
. ' spf_align, envelope_to, envelope_from, header_from FROM ' |
| 117 |
. $this->connector->tablePrefix('rptrecords') . ' WHERE report_id = ?' . $order_str |
| 118 |
); |
| 119 |
$st->bindValue(1, $id, \PDO::PARAM_INT); |
| 120 |
$st->execute(); |
| 121 |
$data->records = []; |
| 122 |
while ($res = $st->fetch(\PDO::FETCH_NUM)) { |
| 123 |
$data->records[] = [ |
| 124 |
'ip' => inet_ntop($res[1]), |
| 125 |
'count' => intval($res[2]), |
| 126 |
'disposition' => Common::$disposition[$res[3]], |
| 127 |
'reason' => json_decode($res[4] ?? '', true), |
| 128 |
'dkim_auth' => json_decode($res[5] ?? '', true), |
| 129 |
'spf_auth' => json_decode($res[6] ?? '', true), |
| 130 |
'dkim_align' => Common::$align_res[$res[7]], |
| 131 |
'spf_align' => Common::$align_res[$res[8]], |
| 132 |
'envelope_to' => $res[9], |
| 133 |
'envelope_from' => $res[10], |
| 134 |
'header_from' => $res[11] |
| 135 |
]; |
| 136 |
} |
| 137 |
$st->closeCursor(); |
| 138 |
} catch (\PDOException $e) { |
| 139 |
throw new DatabaseFatalException('Failed to get the report from DB', -1, $e); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public function save($data): void |
| 151 |
{ |
| 152 |
$db = $this->connector->dbh(); |
| 153 |
$db->beginTransaction(); |
| 154 |
try { |
| 155 |
$domain_data = [ 'fqdn' => strtolower($data->domain) ]; |
| 156 |
$domain_mapper = $this->connector->getMapper('domain'); |
| 157 |
try { |
| 158 |
$domain_mapper->fetch($domain_data); |
| 159 |
if (!$domain_data['active']) { |
| 160 |
throw new SoftException('Failed to add an incoming report: the domain is inactive'); |
| 161 |
} |
| 162 |
$user_id = Core::instance()->getCurrentUser()->id(); |
| 163 |
if ($user_id !== 0 && !$domain_mapper->isAssigned($domain_data, $user_id)) { |
| 164 |
|
| 165 |
$this->unknownDomain($domain_data); |
| 166 |
} |
| 167 |
} catch (DatabaseNotFoundException $e) { |
| 168 |
|
| 169 |
if (Core::instance()->getCurrentUser()->id() !== 0) { |
| 170 |
$this->unknownDomain($domain_data); |
| 171 |
} |
| 172 |
$this->insertDomain($domain_data, $domain_mapper); |
| 173 |
} |
| 174 |
|
| 175 |
$ct = new DateTime(); |
| 176 |
$st = $db->prepare( |
| 177 |
'INSERT INTO ' . $this->connector->tablePrefix('reports') |
| 178 |
. ' (domain_id, begin_time, end_time, loaded_time, org, external_id, email,' |
| 179 |
. ' extra_contact_info, error_string, policy_adkim, policy_aspf, policy_p,' |
| 180 |
. ' policy_sp, policy_np, policy_pct, policy_fo, seen)' |
| 181 |
. ' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0)' |
| 182 |
); |
| 183 |
$st->bindValue(1, $domain_data['id'], \PDO::PARAM_INT); |
| 184 |
$st->bindValue(2, $data->date['begin']->format('Y-m-d H:i:s'), \PDO::PARAM_STR); |
| 185 |
$st->bindValue(3, $data->date['end']->format('Y-m-d H:i:s'), \PDO::PARAM_STR); |
| 186 |
$st->bindValue(4, $ct->format('Y-m-d H:i:s'), \PDO::PARAM_STR); |
| 187 |
$st->bindValue(5, $data->org_name, \PDO::PARAM_STR); |
| 188 |
$st->bindValue(6, $data->report_id, \PDO::PARAM_STR); |
| 189 |
$st->bindValue(7, $data->email ?? '', \PDO::PARAM_STR); |
| 190 |
$st->bindValue(8, $data->extra_contact_info, \PDO::PARAM_STR); |
| 191 |
self::sqlBindJson($st, 9, $data->error_string); |
| 192 |
$st->bindValue(10, $data->policy['adkim'], \PDO::PARAM_STR); |
| 193 |
$st->bindValue(11, $data->policy['aspf'], \PDO::PARAM_STR); |
| 194 |
$st->bindValue(12, $data->policy['p'], \PDO::PARAM_STR); |
| 195 |
$st->bindValue(13, $data->policy['sp'], \PDO::PARAM_STR); |
| 196 |
$st->bindValue(14, $data->policy['np'], \PDO::PARAM_STR); |
| 197 |
$st->bindValue(15, $data->policy['pct'], \PDO::PARAM_STR); |
| 198 |
$st->bindValue(16, $data->policy['fo'], \PDO::PARAM_STR); |
| 199 |
$st->execute(); |
| 200 |
$new_id = intval($db->lastInsertId()); |
| 201 |
$st->closeCursor(); |
| 202 |
|
| 203 |
$st = $db->prepare( |
| 204 |
'INSERT INTO ' . $this->connector->tablePrefix('rptrecords') |
| 205 |
. ' (report_id, ip, rcount, disposition, reason, dkim_auth, spf_auth, dkim_align,' |
| 206 |
. ' spf_align, envelope_to, envelope_from, header_from)' |
| 207 |
. ' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' |
| 208 |
); |
| 209 |
foreach ($data->records as &$rec_data) { |
| 210 |
$st->bindValue(1, $new_id, \PDO::PARAM_INT); |
| 211 |
$st->bindValue(2, inet_pton($rec_data['ip']), \PDO::PARAM_STR); |
| 212 |
$st->bindValue(3, $rec_data['count'], \PDO::PARAM_INT); |
| 213 |
$st->bindValue(4, array_search($rec_data['disposition'], Common::$disposition), \PDO::PARAM_INT); |
| 214 |
self::sqlBindJson($st, 5, $rec_data['reason']); |
| 215 |
self::sqlBindJson($st, 6, $rec_data['dkim_auth']); |
| 216 |
self::sqlBindJson($st, 7, $rec_data['spf_auth']); |
| 217 |
$st->bindValue(8, array_search($rec_data['dkim_align'], Common::$align_res), \PDO::PARAM_INT); |
| 218 |
$st->bindValue(9, array_search($rec_data['spf_align'], Common::$align_res), \PDO::PARAM_INT); |
| 219 |
$st->bindValue(10, $rec_data['envelope_to'], \PDO::PARAM_STR); |
| 220 |
$st->bindValue(11, $rec_data['envelope_from'], \PDO::PARAM_STR); |
| 221 |
$st->bindValue(12, $rec_data['header_from'], \PDO::PARAM_STR); |
| 222 |
$st->execute(); |
| 223 |
} |
| 224 |
unset($rec_data); |
| 225 |
$db->commit(); |
| 226 |
$data->loaded_time = $ct; |
| 227 |
} catch (\PDOException $e) { |
| 228 |
$db->rollBack(); |
| 229 |
if ($e->getCode() == '23000') { |
| 230 |
throw new SoftException('This report has already been loaded'); |
| 231 |
} |
| 232 |
throw new DatabaseFatalException('Failed to insert the report', -1, $e); |
| 233 |
} catch (\Exception $e) { |
| 234 |
$db->rollBack(); |
| 235 |
throw $e; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public function setProperty($data, string $name, $value): void |
| 251 |
{ |
| 252 |
if ($name !== 'seen' && gettype($value) !== 'boolean') { |
| 253 |
throw new LogicException('Incorrect parameters'); |
| 254 |
} |
| 255 |
|
| 256 |
try { |
| 257 |
$st = $this->connector->dbh()->prepare( |
| 258 |
'UPDATE ' . $this->connector->tablePrefix('reports') . ' AS rp' |
| 259 |
. ' INNER JOIN ' . $this->connector->tablePrefix('domains') . ' AS dom' |
| 260 |
. ' ON rp.domain_id = dom.id SET seen = ?' |
| 261 |
. ' WHERE fqdn = ? AND begin_time = ? AND org = ? AND external_id = ?' |
| 262 |
); |
| 263 |
$st->bindValue(1, $value, \PDO::PARAM_BOOL); |
| 264 |
$st->bindValue(2, $data->domain, \PDO::PARAM_STR); |
| 265 |
$st->bindValue(3, $data->date['begin']->format('Y-m-d H:i:s'), \PDO::PARAM_STR); |
| 266 |
$st->bindValue(4, $data->org_name, \PDO::PARAM_STR); |
| 267 |
$st->bindValue(5, $data->report_id, \PDO::PARAM_STR); |
| 268 |
$st->execute(); |
| 269 |
$st->closeCursor(); |
| 270 |
} catch (\PDOException $e) { |
| 271 |
throw new DatabaseFatalException('Failed to update the DB record', -1, $e); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public function list(array &$filter, array &$order, array &$limit, int $user_id): array |
| 290 |
{ |
| 291 |
$db = $this->connector->dbh(); |
| 292 |
$list = []; |
| 293 |
$f_data = $this->prepareFilterData($filter, 'rp'); |
| 294 |
$user_doms = $this->sqlUserRestriction($user_id, 'd.id'); |
| 295 |
$order_str = $this->sqlOrderList($order, 'rp.id'); |
| 296 |
$cond_str0 = $this->sqlConditionList($f_data, ' AND ', 0); |
| 297 |
$cond_str1 = $this->sqlConditionList($f_data, ' HAVING ', 1); |
| 298 |
$limit_str = $this->sqlLimit($limit); |
| 299 |
try { |
| 300 |
$st = $db->prepare( |
| 301 |
'SELECT org, begin_time, end_time, fqdn, external_id, seen, SUM(rcount) AS rcount,' |
| 302 |
. ' SUM(IF(dkim_align = 0, rcount, 0)) AS dkim_align_fail,' |
| 303 |
. ' SUM(IF(dkim_align = 1, rcount, 0)) AS dkim_align_unknown,' |
| 304 |
. ' SUM(IF(spf_align = 0, rcount, 0)) AS spf_align_fail,' |
| 305 |
. ' SUM(IF(spf_align = 1, rcount, 0)) AS spf_align_unknown,' |
| 306 |
. ' SUM(IF(disposition = 0, rcount, 0)) AS rejected,' |
| 307 |
. ' SUM(IF(disposition = 1, rcount, 0)) AS quarantined' |
| 308 |
. ' FROM ' . $this->connector->tablePrefix('rptrecords') |
| 309 |
. ' AS rr RIGHT JOIN (SELECT rp.id, org, begin_time, end_time, external_id,' |
| 310 |
. ' fqdn, seen FROM ' . $this->connector->tablePrefix('reports') |
| 311 |
. ' AS rp INNER JOIN ' . $this->connector->tablePrefix('domains') |
| 312 |
. ' AS d ON d.id = rp.domain_id' . $user_doms . $cond_str0 . $order_str |
| 313 |
. ') AS rp ON rp.id = rr.report_id GROUP BY rp.id' |
| 314 |
. $cond_str1 . $order_str . $limit_str |
| 315 |
); |
| 316 |
$this->sqlBindValues($st, $f_data, $limit); |
| 317 |
$st->execute(); |
| 318 |
while ($row = $st->fetch(\PDO::FETCH_NUM)) { |
| 319 |
$messages = intval($row[6]); |
| 320 |
$dkim_fail = intval($row[7]); |
| 321 |
$dkim_unkn = intval($row[8]); |
| 322 |
$spf_fail = intval($row[9]); |
| 323 |
$spf_unkn = intval($row[10]); |
| 324 |
$list[] = [ |
| 325 |
'org_name' => $row[0], |
| 326 |
'date' => [ |
| 327 |
'begin' => new DateTime($row[1]), |
| 328 |
'end' => new DateTime($row[2]) |
| 329 |
], |
| 330 |
'domain' => $row[3], |
| 331 |
'report_id' => $row[4], |
| 332 |
'seen' => (bool) $row[5], |
| 333 |
'messages' => $messages, |
| 334 |
'dkim_align' => [ |
| 335 |
'fail' => $dkim_fail, |
| 336 |
'unknown' => $dkim_unkn, |
| 337 |
'pass' => $messages - $dkim_fail - $dkim_unkn |
| 338 |
], |
| 339 |
'spf_align' => [ |
| 340 |
'fail' => $spf_fail, |
| 341 |
'unknown' => $spf_unkn, |
| 342 |
'pass' => $messages - $spf_fail - $spf_unkn |
| 343 |
], |
| 344 |
'rejected' => intval($row[11]), |
| 345 |
'quarantined' => intval($row[12]) |
| 346 |
]; |
| 347 |
} |
| 348 |
$st->closeCursor(); |
| 349 |
} catch (\PDOException $e) { |
| 350 |
throw new DatabaseFatalException('Failed to get the report list', -1, $e); |
| 351 |
} |
| 352 |
return $list; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
public function count(array &$filter, array &$limit, int $user_id): int |
| 365 |
{ |
| 366 |
$cnt = 0; |
| 367 |
$f_data = $this->prepareFilterData($filter, 'rp'); |
| 368 |
try { |
| 369 |
if (isset($filter['dkim']) || isset($filter['spf']) || isset($filter['disposition'])) { |
| 370 |
$st = $this->connector->dbh()->prepare( |
| 371 |
'SELECT COUNT(*) FROM (' |
| 372 |
. 'SELECT SUM(IF(dkim_align = 0, rcount, 0)) AS dkim_align_fail,' |
| 373 |
. ' SUM(IF(dkim_align = 1, rcount, 0)) AS dkim_align_unknown,' |
| 374 |
. ' SUM(IF(spf_align = 0, rcount, 0)) AS spf_align_fail,' |
| 375 |
. ' SUM(IF(spf_align = 1, rcount, 0)) AS spf_align_unknown,' |
| 376 |
. ' SUM(IF(disposition = 0, rcount, 0)) AS rejected,' |
| 377 |
. ' SUM(IF(disposition = 1, rcount, 0)) AS quarantined' |
| 378 |
. ' FROM ' . $this->connector->tablePrefix('rptrecords') |
| 379 |
. ' AS rr RIGHT JOIN (SELECT rp.id FROM ' . $this->connector->tablePrefix('reports') |
| 380 |
. ' AS rp INNER JOIN ' . $this->connector->tablePrefix('domains') |
| 381 |
. ' AS d ON d.id = rp.domain_id' . $this->sqlUserRestriction($user_id, 'd.id') |
| 382 |
. $this->sqlConditionList($f_data, ' AND ', 0) |
| 383 |
. ') AS rp ON rp.id = rr.report_id GROUP BY rp.id' |
| 384 |
. $this->sqlConditionList($f_data, ' HAVING ', 1) |
| 385 |
. ') AS ct' |
| 386 |
); |
| 387 |
} elseif ($user_id) { |
| 388 |
$st = $this->connector->dbh()->prepare( |
| 389 |
'SELECT COUNT(*) FROM ' . $this->connector->tablePrefix('reports') . ' AS rp' |
| 390 |
. ' INNER JOIN ' . $this->connector->tablePrefix('domains') |
| 391 |
. ' AS d ON d.id = rp.domain_id' . $this->sqlUserRestriction($user_id, 'd.id') |
| 392 |
. $this->sqlConditionList($f_data, ' AND ', 0) |
| 393 |
); |
| 394 |
} else { |
| 395 |
$st = $this->connector->dbh()->prepare( |
| 396 |
'SELECT COUNT(*) FROM ' . $this->connector->tablePrefix('reports') . ' AS rp' |
| 397 |
. $this->sqlConditionList($f_data, ' WHERE ', 0) |
| 398 |
); |
| 399 |
} |
| 400 |
$l_empty = [ 'offset' => 0, 'count' => 0 ]; |
| 401 |
$this->sqlBindValues($st, $f_data, $l_empty); |
| 402 |
$st->execute(); |
| 403 |
$cnt = intval($st->fetch(\PDO::FETCH_NUM)[0]); |
| 404 |
$st->closeCursor(); |
| 405 |
|
| 406 |
$offset = $limit['offset']; |
| 407 |
if ($offset > 0) { |
| 408 |
$cnt -= $offset; |
| 409 |
if ($cnt < 0) { |
| 410 |
$cnt = 0; |
| 411 |
} |
| 412 |
} |
| 413 |
$max = $limit['count']; |
| 414 |
if ($max > 0 && $max < $cnt) { |
| 415 |
$cnt = $max; |
| 416 |
} |
| 417 |
} catch (\PDOException $e) { |
| 418 |
throw new DatabaseFatalException('Failed to get the number of reports', -1, $e); |
| 419 |
} |
| 420 |
return $cnt; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
public function delete(array &$filter, array &$order, array &$limit): void |
| 437 |
{ |
| 438 |
if (Core::instance()->getCurrentUser()->id()) { |
| 439 |
throw new LogicException('Attempted deletion of reports by non-admin user'); |
| 440 |
} |
| 441 |
$f_data = $this->prepareFilterData($filter, ''); |
| 442 |
$cond_str = $this->sqlConditionList($f_data, ' WHERE ', 0); |
| 443 |
$order_str = $this->sqlOrderList($order, 'id'); |
| 444 |
$limit_str = $this->sqlLimit($limit); |
| 445 |
$db = $this->connector->dbh(); |
| 446 |
if (!$db->inTransaction()) { |
| 447 |
$db->beginTransaction(); |
| 448 |
$nested = false; |
| 449 |
} else { |
| 450 |
$nested = true; |
| 451 |
} |
| 452 |
try { |
| 453 |
$st = $db->prepare( |
| 454 |
'DELETE rr FROM ' . $this->connector->tablePrefix('rptrecords') |
| 455 |
. ' AS rr INNER JOIN (SELECT id FROM ' . $this->connector->tablePrefix('reports') |
| 456 |
. $cond_str . $order_str . $limit_str . ') AS rp ON rp.id = rr.report_id' |
| 457 |
); |
| 458 |
$this->sqlBindValues($st, $f_data, $limit); |
| 459 |
$st->execute(); |
| 460 |
$st->closeCursor(); |
| 461 |
|
| 462 |
$st = $db->prepare( |
| 463 |
'DELETE FROM ' . $this->connector->tablePrefix('reports') . "{$cond_str}{$order_str}{$limit_str}" |
| 464 |
); |
| 465 |
$this->sqlBindValues($st, $f_data, $limit); |
| 466 |
$st->execute(); |
| 467 |
$st->closeCursor(); |
| 468 |
|
| 469 |
if (!$nested) { |
| 470 |
$db->commit(); |
| 471 |
} |
| 472 |
} catch (\PDOException $e) { |
| 473 |
if (!$nested) { |
| 474 |
$db->rollBack(); |
| 475 |
} |
| 476 |
throw new DatabaseFatalException('Failed to delete reports', -1, $e); |
| 477 |
} catch (\Exception $e) { |
| 478 |
if (!$nested) { |
| 479 |
$db->rollBack(); |
| 480 |
} |
| 481 |
throw $e; |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public function months(int $user_id): array |
| 493 |
{ |
| 494 |
$res = []; |
| 495 |
$rep_tn = $this->connector->tablePrefix('reports'); |
| 496 |
try { |
| 497 |
$ud = $this->sqlUserRestriction($user_id, 'rp.domain_id'); |
| 498 |
$st = $this->connector->dbh()->query( |
| 499 |
'SELECT DISTINCT DATE_FORMAT(date, \'%Y-%m\') AS month FROM' |
| 500 |
. ' ((SELECT DISTINCT begin_time AS date FROM ' . $rep_tn |
| 501 |
. ' AS rp' . $ud . ') UNION (SELECT DISTINCT end_time AS date FROM ' . $rep_tn |
| 502 |
. ' AS rp' . $ud . ')) AS r ORDER BY month DESC' |
| 503 |
); |
| 504 |
while ($row = $st->fetch(\PDO::FETCH_NUM)) { |
| 505 |
$res[] = $row[0]; |
| 506 |
} |
| 507 |
$st->closeCursor(); |
| 508 |
} catch (\PDOException $e) { |
| 509 |
throw new DatabaseFatalException('Failed to get a list of months', -1, $e); |
| 510 |
} |
| 511 |
return $res; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
public function organizations(int $user_id): array |
| 522 |
{ |
| 523 |
$res = []; |
| 524 |
$rep_tn = $this->connector->tablePrefix('reports'); |
| 525 |
try { |
| 526 |
$ud = $this->sqlUserRestriction($user_id, 'rp.domain_id'); |
| 527 |
$st = $this->connector->dbh()->query( |
| 528 |
'SELECT DISTINCT org FROM ' . $rep_tn . " AS rp{$ud} ORDER BY org" |
| 529 |
); |
| 530 |
while ($row = $st->fetch(\PDO::FETCH_NUM)) { |
| 531 |
$res[] = $row[0]; |
| 532 |
} |
| 533 |
$st->closeCursor(); |
| 534 |
} catch (\PDOException $e) { |
| 535 |
throw new DatabaseFatalException('Failed to get a list of organizations', -1, $e); |
| 536 |
} |
| 537 |
return $res; |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
private function sqlUserRestriction(int $user_id, string $column): string |
| 549 |
{ |
| 550 |
if (!$user_id) { |
| 551 |
return ''; |
| 552 |
} |
| 553 |
return ' INNER JOIN ' . $this->connector->tablePrefix('userdomains') . ' AS ud ON ' |
| 554 |
. $column . ' = ud.domain_id WHERE user_id = ' . $user_id; |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
private function sqlOrderRecords(): string |
| 563 |
{ |
| 564 |
$o_set = explode(',', SettingsList::getSettingByName('report-view.sort-records-by')->value()); |
| 565 |
switch ($o_set[0]) { |
| 566 |
case 'ip': |
| 567 |
$fname = 'ip'; |
| 568 |
break; |
| 569 |
case 'message-count': |
| 570 |
default: |
| 571 |
$fname = 'rcount'; |
| 572 |
break; |
| 573 |
} |
| 574 |
$dir = $o_set[1] === 'descent' ? 'DESC' : 'ASC'; |
| 575 |
|
| 576 |
return " ORDER BY {$fname} {$dir}"; |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
private function insertDomain(array &$data, $mapper): void |
| 594 |
{ |
| 595 |
$core = Core::instance(); |
| 596 |
|
| 597 |
if (is_null(self::$allowed_domains)) { |
| 598 |
$allowed = $core->config('fetcher/allowed_domains', ''); |
| 599 |
if (!empty($allowed)) { |
| 600 |
self::$allowed_domains = "<{$allowed}>i"; |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
$add_f = false; |
| 605 |
if ($mapper->count(0, 1) === 0) { |
| 606 |
$add_f = true; |
| 607 |
} else { |
| 608 |
try { |
| 609 |
$add_f = !empty(self::$allowed_domains) && preg_match(self::$allowed_domains, $data['fqdn']) === 1; |
| 610 |
} catch (\ErrorException $e) { |
| 611 |
$core->logger()->warning( |
| 612 |
'The allow_domains parameter in the settings has an incorrect regular expression value.' |
| 613 |
); |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
if (!$add_f) { |
| 618 |
$this->unknownDomain($data); |
| 619 |
} |
| 620 |
$data['active'] = true; |
| 621 |
$data['description'] = 'The domain was added automatically.'; |
| 622 |
$mapper->save($data); |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
private function unknownDomain(array &$data): void |
| 635 |
{ |
| 636 |
$msg = 'Failed to add an incoming report: unknown domain'; |
| 637 |
if (!empty($data['fqdn'])) { |
| 638 |
$msg .= " {$data['fqdn']}"; |
| 639 |
} |
| 640 |
throw new SoftException($msg); |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
private static function sqlBindJson($st, int $idx, $data): void |
| 653 |
{ |
| 654 |
if (is_null($data)) { |
| 655 |
$val = null; |
| 656 |
$type = \PDO::PARAM_NULL; |
| 657 |
} else { |
| 658 |
$val = json_encode($data); |
| 659 |
$type = \PDO::PARAM_STR; |
| 660 |
} |
| 661 |
$st->bindValue($idx, $val, $type); |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
private function sqlOrderList(array &$order, string $tail_field): string |
| 673 |
{ |
| 674 |
if (count($order) == 0) { |
| 675 |
return ''; |
| 676 |
} |
| 677 |
|
| 678 |
$dir = $order['direction'] === 'ascent' ? 'ASC' : 'DESC'; |
| 679 |
$res = " ORDER BY {$order['field']} {$dir}"; |
| 680 |
if (!empty($tail_field)) { |
| 681 |
$res .= ", {$tail_field} {$dir}"; |
| 682 |
} |
| 683 |
return $res; |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
private static $filters_available = [ |
| 690 |
'domain', 'month', 'before_time', 'organization', 'dkim', 'spf', 'disposition', 'status' |
| 691 |
]; |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
private function prepareFilterData(array &$filter, string $tname = ''): array |
| 702 |
{ |
| 703 |
$filters = []; |
| 704 |
for ($i = 0; $i < 2; ++$i) { |
| 705 |
$filters[] = [ |
| 706 |
'a_str' => [], |
| 707 |
'bindings' => [] |
| 708 |
]; |
| 709 |
} |
| 710 |
if (!empty($tname)) { |
| 711 |
$tname = "{$tname}."; |
| 712 |
} |
| 713 |
foreach (self::$filters_available as $fn) { |
| 714 |
if (isset($filter[$fn])) { |
| 715 |
$fv = $filter[$fn]; |
| 716 |
switch (gettype($fv)) { |
| 717 |
case 'string': |
| 718 |
if (!empty($fv)) { |
| 719 |
if ($fn == 'domain') { |
| 720 |
$filters[0]['a_str'][] = $tname . 'domain_id = ?'; |
| 721 |
$d_data = [ 'fqdn' => $fv ]; |
| 722 |
$this->connector->getMapper('domain')->fetch($d_data); |
| 723 |
$filters[0]['bindings'][] = [ $d_data['id'], \PDO::PARAM_INT ]; |
| 724 |
} elseif ($fn == 'month') { |
| 725 |
$ma = explode('-', $fv); |
| 726 |
if (count($ma) != 2) { |
| 727 |
throw new SoftException('Report list filter: Incorrect date format'); |
| 728 |
} |
| 729 |
$year = (int)$ma[0]; |
| 730 |
$month = (int)$ma[1]; |
| 731 |
if ($year < 0 || $month < 1 || $month > 12) { |
| 732 |
throw new SoftException('Report list filter: Incorrect month or year value'); |
| 733 |
} |
| 734 |
$filters[0]['a_str'][] = 'begin_time < ? AND end_time >= ?'; |
| 735 |
$date1 = new DateTime("{$year}-{$month}-01"); |
| 736 |
$date2 = (clone $date1)->modify('first day of next month'); |
| 737 |
$date1->add(new \DateInterval('PT10S')); |
| 738 |
$date2->sub(new \DateInterval('PT10S')); |
| 739 |
$filters[0]['bindings'][] = [ $date2->format('Y-m-d H:i:s'), \PDO::PARAM_STR ]; |
| 740 |
$filters[0]['bindings'][] = [ $date1->format('Y-m-d H:i:s'), \PDO::PARAM_STR ]; |
| 741 |
} elseif ($fn == 'organization') { |
| 742 |
$filters[0]['a_str'][] = 'org = ?'; |
| 743 |
$filters[0]['bindings'][] = [ $fv, \PDO::PARAM_STR ]; |
| 744 |
} elseif ($fn == 'dkim') { |
| 745 |
if (!in_array($fv, Common::$align_res, true)) { |
| 746 |
throw new SoftException('Report list filter: Incorrect DKIM value'); |
| 747 |
} |
| 748 |
if ($fv === 'pass') { |
| 749 |
$filters[1]['a_str'][] = 'dkim_align_fail = 0 AND dkim_align_unknown = 0'; |
| 750 |
} else { |
| 751 |
$filters[1]['a_str'][] = "dkim_align_{$fv} > 0"; |
| 752 |
} |
| 753 |
} elseif ($fn == 'spf') { |
| 754 |
if (!in_array($fv, Common::$align_res, true)) { |
| 755 |
throw new SoftException('Report list filter: Incorrect SPF value'); |
| 756 |
} |
| 757 |
if ($fv === 'pass') { |
| 758 |
$filters[1]['a_str'][] = 'spf_align_fail = 0 AND spf_align_unknown = 0'; |
| 759 |
} else { |
| 760 |
$filters[1]['a_str'][] = "spf_align_{$fv} > 0"; |
| 761 |
} |
| 762 |
} elseif ($fn == 'disposition') { |
| 763 |
switch ($fv) { |
| 764 |
case 'none': |
| 765 |
$str = 'rejected = 0 AND quarantined = 0'; |
| 766 |
break; |
| 767 |
case 'quarantine': |
| 768 |
$str = 'quarantined > 0'; |
| 769 |
break; |
| 770 |
case 'reject': |
| 771 |
$str = 'rejected > 0'; |
| 772 |
break; |
| 773 |
default: |
| 774 |
throw new SoftException('Report list filter: Incorrect value of disposition'); |
| 775 |
} |
| 776 |
$filters[1]['a_str'][] = $str; |
| 777 |
} elseif ($fn == 'status') { |
| 778 |
if ($fv === 'read') { |
| 779 |
$val = true; |
| 780 |
} elseif ($fv === 'unread') { |
| 781 |
$val = false; |
| 782 |
} else { |
| 783 |
throw new SoftException('Report list filter: Incorrect status value'); |
| 784 |
} |
| 785 |
$filters[0]['a_str'][] = 'seen = ?'; |
| 786 |
$filters[0]['bindings'][] = [ $val, \PDO::PARAM_BOOL ]; |
| 787 |
} |
| 788 |
} |
| 789 |
break; |
| 790 |
case 'object': |
| 791 |
if ($fn == 'domain') { |
| 792 |
$filters[0]['a_str'][] = $tname . 'domain_id = ?'; |
| 793 |
$filters[0]['bindings'][] = [ $fv->id(), \PDO::PARAM_INT ]; |
| 794 |
} elseif ($fn == 'before_time') { |
| 795 |
$filters[0]['a_str'][] = 'begin_time < ?'; |
| 796 |
$filters[0]['bindings'][] = [ $fv->format('Y-m-d H:i:s'), \PDO::PARAM_STR ]; |
| 797 |
} |
| 798 |
break; |
| 799 |
case 'integer': |
| 800 |
if ($fn == 'domain') { |
| 801 |
$filters[0]['a_str'][] = $tname . 'domain_id = ?'; |
| 802 |
$filters[0]['bindings'][] = [ $fv, \PDO::PARAM_INT ]; |
| 803 |
} |
| 804 |
break; |
| 805 |
} |
| 806 |
} |
| 807 |
} |
| 808 |
$f_data = []; |
| 809 |
for ($i = 0; $i < count($filters); ++$i) { |
| 810 |
$filter = &$filters[$i]; |
| 811 |
if (count($filter['a_str']) > 0) { |
| 812 |
$f_data[$i] = [ |
| 813 |
'str' => implode(' AND ', $filter['a_str']), |
| 814 |
'bindings' => $filter['bindings'] |
| 815 |
]; |
| 816 |
} |
| 817 |
unset($filter); |
| 818 |
} |
| 819 |
return $f_data; |
| 820 |
} |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
private function sqlConditionList(array &$f_data, string $prefix, int $f_idx): string |
| 833 |
{ |
| 834 |
return isset($f_data[$f_idx]) ? ($prefix . $f_data[$f_idx]['str']) : ''; |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
private function sqlLimit(array &$limit): string |
| 845 |
{ |
| 846 |
$res = ''; |
| 847 |
if ($limit['count'] > 0) { |
| 848 |
$res = ' LIMIT ?'; |
| 849 |
if ($limit['offset'] > 0) { |
| 850 |
$res .= ', ?'; |
| 851 |
} |
| 852 |
} |
| 853 |
return $res; |
| 854 |
} |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
private function sqlBindValues($st, array &$f_data, array &$limit): void |
| 866 |
{ |
| 867 |
$pos = 0; |
| 868 |
if (isset($f_data[0])) { |
| 869 |
$this->sqlBindFilterValues($st, $f_data, 0, $pos); |
| 870 |
} |
| 871 |
if (isset($f_data[1])) { |
| 872 |
$this->sqlBindFilterValues($st, $f_data, 1, $pos); |
| 873 |
} |
| 874 |
if ($limit['count'] > 0) { |
| 875 |
if ($limit['offset'] > 0) { |
| 876 |
$st->bindValue(++$pos, $limit['offset'], \PDO::PARAM_INT); |
| 877 |
} |
| 878 |
$st->bindValue(++$pos, $limit['count'], \PDO::PARAM_INT); |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
private function sqlBindFilterValues($st, array &$f_data, int $filter_idx, int &$bind_pos): void |
| 893 |
{ |
| 894 |
foreach ($f_data[$filter_idx]['bindings'] as &$bv) { |
| 895 |
$st->bindValue(++$bind_pos, $bv[0], $bv[1]); |
| 896 |
} |
| 897 |
} |
| 898 |
} |