| 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\Common; |
| 35 |
use Liuch\DmarcSrg\Exception\SoftException; |
| 36 |
use Liuch\DmarcSrg\Exception\DatabaseFatalException; |
| 37 |
use Liuch\DmarcSrg\Database\StatisticsMapperInterface; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class CommonStatisticsMapper implements StatisticsMapperInterface |
| 43 |
{ |
| 44 |
|
| 45 |
private $connector = null; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function __construct(object $connector) |
| 53 |
{ |
| 54 |
$this->connector = $connector; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function summary($domain, array &$range, array &$filter, int $user_id): array |
| 77 |
{ |
| 78 |
$is_domain = $domain ? true : false; |
| 79 |
$db = $this->connector->dbh(); |
| 80 |
try { |
| 81 |
$users = $this->sqlUserRestriction($user_id); |
| 82 |
$f_data = $this->prepareFilterData($domain, $range, $user_id, $filter); |
| 83 |
$st = $db->prepare( |
| 84 |
'SELECT SUM(rcount), SUM(IF(dkim_align = 2 AND spf_align = 2, rcount, 0)),' |
| 85 |
. ' SUM(IF(dkim_align = 2 AND spf_align <> 2, rcount, 0)),' |
| 86 |
. ' SUM(IF(dkim_align <> 2 AND spf_align = 2, rcount, 0)),' |
| 87 |
. ' SUM(IF(disposition = 0, rcount, 0)),' |
| 88 |
. ' SUM(IF(disposition = 1, rcount, 0))' |
| 89 |
. ' FROM ' . $this->connector->tablePrefix('rptrecords') . ' AS rr' |
| 90 |
. ' INNER JOIN ' . $this->connector->tablePrefix('reports') |
| 91 |
. ' AS rp ON rr.report_id = rp.id' . $users |
| 92 |
. $this->sqlCondition($f_data, ' WHERE ', 0) . $this->sqlCondition($f_data, ' AND ', 1) |
| 93 |
); |
| 94 |
$this->sqlBindValues($st, $f_data, [ 0, 1 ]); |
| 95 |
$st->execute(); |
| 96 |
$row = $st->fetch(\PDO::FETCH_NUM); |
| 97 |
$ems = [ |
| 98 |
'total' => intval($row[0]), |
| 99 |
'dkim_spf_aligned' => intval($row[1]), |
| 100 |
'dkim_aligned' => intval($row[2]), |
| 101 |
'spf_aligned' => intval($row[3]), |
| 102 |
'rejected' => intval($row[4]), |
| 103 |
'quarantined' => intval($row[5]) |
| 104 |
]; |
| 105 |
$st->closeCursor(); |
| 106 |
|
| 107 |
if (!isset($filter['dkim']) && !isset($filter['spf']) && !isset($filter['disposition'])) { |
| 108 |
$st = $db->prepare( |
| 109 |
'SELECT COUNT(*) FROM (SELECT org FROM ' . $this->connector->tablePrefix('reports') . ' AS rp' |
| 110 |
. $users . $this->sqlCondition($f_data, ' WHERE ', 0) . ' GROUP BY org) AS orgs' |
| 111 |
); |
| 112 |
} else { |
| 113 |
$st = $db->prepare( |
| 114 |
'SELECT COUNT(*) FROM (SELECT org FROM (' |
| 115 |
. 'SELECT org, MIN(dkim_align) as dkim_align, MIN(spf_align) AS spf_align,' |
| 116 |
. ' MIN(disposition) AS disposition FROM ' |
| 117 |
. $this->connector->tablePrefix('reports') . ' AS rp' |
| 118 |
. ' INNER JOIN ' . $this->connector->tablePrefix('rptrecords') . ' AS rr' |
| 119 |
. ' ON rp.id = rr.report_id' . $users . $this->sqlCondition($f_data, ' WHERE ', 0) |
| 120 |
. ' GROUP BY rr.report_id' |
| 121 |
. ') AS sr' . $this->sqlCondition($f_data, ' WHERE ', 1) . ' GROUP BY org) AS orgs' |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
$this->sqlBindValues($st, $f_data, [ 0, 1 ]); |
| 126 |
$st->execute(); |
| 127 |
$row = $st->fetch(\PDO::FETCH_NUM); |
| 128 |
$st->closeCursor(); |
| 129 |
} catch (\PDOException $e) { |
| 130 |
throw new DatabaseFatalException('Failed to get summary information', -1, $e); |
| 131 |
} |
| 132 |
|
| 133 |
return [ |
| 134 |
'emails' => $ems, |
| 135 |
'organizations' => intval($row[0]) |
| 136 |
]; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public function ips($domain, array &$range, array &$filter, int $user_id): array |
| 151 |
{ |
| 152 |
try { |
| 153 |
$f_data = $this->prepareFilterData($domain, $range, $user_id, $filter); |
| 154 |
$st = $this->connector->dbh()->prepare( |
| 155 |
'SELECT ip, SUM(rcount) AS rcount,' |
| 156 |
. ' SUM(IF(dkim_align = 2 AND spf_align = 2, rcount, 0)) AS dkim_spf_aligned,' |
| 157 |
. ' SUM(IF(dkim_align = 2 AND spf_align <> 2, rcount, 0)) AS dkim_aligned,' |
| 158 |
. ' SUM(IF(dkim_align <> 2 AND spf_align = 2, rcount, 0)) AS spf_aligned,' |
| 159 |
. ' SUM(IF(disposition = 0, rcount, 0)),' |
| 160 |
. ' SUM(IF(disposition = 1, rcount, 0))' |
| 161 |
. ' FROM ' . $this->connector->tablePrefix('rptrecords') . ' AS rr' |
| 162 |
. ' INNER JOIN ' . $this->connector->tablePrefix('reports') |
| 163 |
. ' AS rp ON rr.report_id = rp.id' . $this->sqlUserRestriction($user_id) |
| 164 |
. $this->sqlCondition($f_data, ' WHERE ', 0) . $this->sqlCondition($f_data, ' AND ', 1) |
| 165 |
. ' GROUP BY ip ORDER BY rcount DESC' |
| 166 |
); |
| 167 |
$this->sqlBindValues($st, $f_data, [ 0, 1 ]); |
| 168 |
$st->execute(); |
| 169 |
$res = []; |
| 170 |
while ($row = $st->fetch(\PDO::FETCH_NUM)) { |
| 171 |
$res[] = [ |
| 172 |
'ip' => inet_ntop($row[0]), |
| 173 |
'emails' => intval($row[1]), |
| 174 |
'dkim_spf_aligned' => intval($row[2]), |
| 175 |
'dkim_aligned' => intval($row[3]), |
| 176 |
'spf_aligned' => intval($row[4]), |
| 177 |
'rejected' => intval($row[5]), |
| 178 |
'quarantined' => intval($row[6]) |
| 179 |
]; |
| 180 |
} |
| 181 |
$st->closeCursor(); |
| 182 |
} catch (\PDOException $e) { |
| 183 |
throw new DatabaseFatalException('Failed to get IPs summary information', -1, $e); |
| 184 |
} |
| 185 |
return $res; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public function organizations($domain, array &$range, array &$filter, int $user_id): array |
| 200 |
{ |
| 201 |
try { |
| 202 |
$f_data = $this->prepareFilterData($domain, $range, $user_id, $filter); |
| 203 |
$st = $this->connector->dbh()->prepare( |
| 204 |
'SELECT org, COUNT(*), SUM(rr.rcount) AS rcount,' |
| 205 |
. ' SUM(rr.dkim_spf_aligned), SUM(rr.dkim_aligned), SUM(rr.spf_aligned),' |
| 206 |
. ' SUM(rr.rejected), SUM(rr.quarantined) FROM ' |
| 207 |
. $this->connector->tablePrefix('reports') . ' AS rp' |
| 208 |
. ' INNER JOIN (SELECT report_id, SUM(rcount) AS rcount,' |
| 209 |
. ' SUM(IF(dkim_align = 2 AND spf_align = 2, rcount, 0)) AS dkim_spf_aligned,' |
| 210 |
. ' SUM(IF(dkim_align = 2 AND spf_align <> 2, rcount, 0)) AS dkim_aligned,' |
| 211 |
. ' SUM(IF(dkim_align <> 2 AND spf_align = 2, rcount, 0)) AS spf_aligned,' |
| 212 |
. ' SUM(IF(disposition = 0, rcount, 0)) AS rejected,' |
| 213 |
. ' SUM(IF(disposition = 1, rcount, 0)) AS quarantined FROM ' |
| 214 |
. $this->connector->tablePrefix('rptrecords') |
| 215 |
. $this->sqlCondition($f_data, ' WHERE ', 1) |
| 216 |
. ' GROUP BY report_id) AS rr ON rp.id = rr.report_id' |
| 217 |
. $this->sqlUserRestriction($user_id) . $this->sqlCondition($f_data, ' WHERE ', 0) |
| 218 |
. ' GROUP BY org ORDER BY rcount DESC' |
| 219 |
); |
| 220 |
$this->sqlBindValues($st, $f_data, [ 1, 0 ]); |
| 221 |
$st->execute(); |
| 222 |
$res = []; |
| 223 |
while ($row = $st->fetch(\PDO::FETCH_NUM)) { |
| 224 |
$res[] = [ |
| 225 |
'name' => $row[0], |
| 226 |
'reports' => intval($row[1]), |
| 227 |
'emails' => intval($row[2]), |
| 228 |
'dkim_spf_aligned' => intval($row[3]), |
| 229 |
'dkim_aligned' => intval($row[4]), |
| 230 |
'spf_aligned' => intval($row[5]), |
| 231 |
'rejected' => intval($row[6]), |
| 232 |
'quarantined' => intval($row[7]) |
| 233 |
]; |
| 234 |
} |
| 235 |
$st->closeCursor(); |
| 236 |
} catch (\PDOException $e) { |
| 237 |
throw new DatabaseFatalException('Failed to get summary information of reporting organizations', -1, $e); |
| 238 |
} |
| 239 |
return $res; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
private static $filters_available = [ |
| 246 |
'organization', 'dkim', 'spf', 'disposition', 'status' |
| 247 |
]; |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
private function prepareFilterData($domain, array &$range, int $user_id, array &$filter): array |
| 260 |
{ |
| 261 |
$sql_cond1 = []; |
| 262 |
$sql_cond2 = []; |
| 263 |
$bindings1 = []; |
| 264 |
$bindings2 = []; |
| 265 |
if ($user_id) { |
| 266 |
$sql_cond1[] = 'user_id = ?'; |
| 267 |
$bindings1[] = [ $user_id, \PDO::PARAM_INT ]; |
| 268 |
} |
| 269 |
if ($domain) { |
| 270 |
$sql_cond1[] = 'rp.domain_id = ?'; |
| 271 |
$bindings1[] = [ $domain->id(), \PDO::PARAM_INT ]; |
| 272 |
} |
| 273 |
$sql_cond1[] = 'begin_time < ? AND end_time >= ?'; |
| 274 |
$bindings1[] = [ |
| 275 |
(clone $range['date2'])->sub(new \DateInterval('PT10S'))->format('Y-m-d H:i:s'), \PDO::PARAM_STR |
| 276 |
]; |
| 277 |
$bindings1[] = [ |
| 278 |
(clone $range['date1'])->add(new \DateInterval('PT10S'))->format('Y-m-d H:i:s'), \PDO::PARAM_STR |
| 279 |
]; |
| 280 |
foreach (self::$filters_available as $fname) { |
| 281 |
if (empty($filter[$fname])) { |
| 282 |
continue; |
| 283 |
} |
| 284 |
$fvalue = $filter[$fname]; |
| 285 |
switch ($fname) { |
| 286 |
case 'organization': |
| 287 |
$sql_cond1[] = 'org = ?'; |
| 288 |
$bindings1[] = [ $fvalue, \PDO::PARAM_STR ]; |
| 289 |
break; |
| 290 |
case 'dkim': |
| 291 |
if ($fvalue === Common::$align_res[0]) { |
| 292 |
$val = 0; |
| 293 |
} else { |
| 294 |
$val = count(Common::$align_res) - 1; |
| 295 |
if ($fvalue !== Common::$align_res[$val]) { |
| 296 |
throw new SoftException('Filter: Incorrect DKIM value'); |
| 297 |
} |
| 298 |
} |
| 299 |
$sql_cond2[] = 'dkim_align = ?'; |
| 300 |
$bindings2[] = [ $val, \PDO::PARAM_INT ]; |
| 301 |
break; |
| 302 |
case 'spf': |
| 303 |
if ($fvalue === Common::$align_res[0]) { |
| 304 |
$val = 0; |
| 305 |
} else { |
| 306 |
$val = count(Common::$align_res) - 1; |
| 307 |
if ($fvalue !== Common::$align_res[$val]) { |
| 308 |
throw new SoftException('Filter: Incorrect SPF value'); |
| 309 |
} |
| 310 |
} |
| 311 |
$sql_cond2[] = 'spf_align = ?'; |
| 312 |
$bindings2[] = [ $val, \PDO::PARAM_INT ]; |
| 313 |
break; |
| 314 |
case 'disposition': |
| 315 |
$val = array_search($fvalue, Common::$disposition); |
| 316 |
if ($val === false) { |
| 317 |
throw new SoftException('Filter: Incorrect value of disposition'); |
| 318 |
} |
| 319 |
$sql_cond2[] = 'disposition = ?'; |
| 320 |
$bindings2[] = [ $val, \PDO::PARAM_INT ]; |
| 321 |
break; |
| 322 |
case 'status': |
| 323 |
if ($fvalue === 'read') { |
| 324 |
$val = true; |
| 325 |
} elseif ($fvalue === 'unread') { |
| 326 |
$val = false; |
| 327 |
} else { |
| 328 |
throw new SoftException('Filter: Incorrect status value'); |
| 329 |
} |
| 330 |
$sql_cond1[] = 'seen = ?'; |
| 331 |
$bindings1[] = [ $val, \PDO::PARAM_BOOL ]; |
| 332 |
break; |
| 333 |
} |
| 334 |
} |
| 335 |
return [ |
| 336 |
'sql_cond' => [ $sql_cond1, $sql_cond2 ], |
| 337 |
'bindings' => [ $bindings1, $bindings2 ] |
| 338 |
]; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
private function sqlUserRestriction(int $user_id): string |
| 349 |
{ |
| 350 |
if (!$user_id) { |
| 351 |
return ''; |
| 352 |
} |
| 353 |
return ' INNER JOIN ' . $this->connector->tablePrefix('userdomains') |
| 354 |
. ' AS ud ON rp.domain_id = ud.domain_id'; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
private function sqlCondition(array &$f_data, string $prefix, int $num): string |
| 368 |
{ |
| 369 |
if (count($f_data['sql_cond'][$num]) > 0) { |
| 370 |
return $prefix . implode(' AND ', $f_data['sql_cond'][$num]); |
| 371 |
} |
| 372 |
return ''; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
private function sqlBindValues(object $st, array &$f_data, array $order): void |
| 385 |
{ |
| 386 |
$pnum = 0; |
| 387 |
foreach ($order as $idx) { |
| 388 |
foreach ($f_data['bindings'][$idx] as &$it) { |
| 389 |
$st->bindValue(++$pnum, $it[0], $it[1]); |
| 390 |
} |
| 391 |
unset($it); |
| 392 |
} |
| 393 |
} |
| 394 |
} |