| 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\DateTime; |
| 35 |
use Liuch\DmarcSrg\ErrorCodes; |
| 36 |
use Liuch\DmarcSrg\Database\DomainMapperInterface; |
| 37 |
use Liuch\DmarcSrg\Exception\SoftException; |
| 38 |
use Liuch\DmarcSrg\Exception\LogicException; |
| 39 |
use Liuch\DmarcSrg\Exception\DatabaseFatalException; |
| 40 |
use Liuch\DmarcSrg\Exception\DatabaseNotFoundException; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class CommonDomainMapper implements DomainMapperInterface |
| 46 |
{ |
| 47 |
|
| 48 |
private $connector = null; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function __construct(object $connector) |
| 56 |
{ |
| 57 |
$this->connector = $connector; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function exists(array &$data): bool |
| 68 |
{ |
| 69 |
try { |
| 70 |
$st = $this->connector->dbh()->prepare( |
| 71 |
'SELECT id FROM ' . $this->connector->tablePrefix('domains') . |
| 72 |
' WHERE ' . $this->sqlCondition($data) |
| 73 |
); |
| 74 |
$this->sqlBindValues($st, $data, 1); |
| 75 |
$st->execute(); |
| 76 |
$res = $st->fetch(\PDO::FETCH_NUM); |
| 77 |
$st->closeCursor(); |
| 78 |
if (!$res) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
$data['id'] = intval($res[0]); |
| 82 |
} catch (\PDOException $e) { |
| 83 |
throw new DatabaseFatalException('Failed to get domain ID', -1, $e); |
| 84 |
} |
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function isAssigned(array &$data, int $user_id): bool |
| 97 |
{ |
| 98 |
$res = null; |
| 99 |
try { |
| 100 |
$st = $this->connector->dbh()->prepare( |
| 101 |
'SELECT 1 FROM ' . $this->connector->tablePrefix('userdomains') . ' INNER JOIN ' |
| 102 |
. $this->connector->tablePrefix('domains') . ' ON domain_id = id WHERE user_id = ? AND ' |
| 103 |
. $this->sqlCondition($data) |
| 104 |
); |
| 105 |
$st->bindValue(1, $user_id, \PDO::PARAM_INT); |
| 106 |
$this->sqlBindValues($st, $data, 2); |
| 107 |
$st->execute(); |
| 108 |
$res = $st->fetch(\PDO::FETCH_NUM); |
| 109 |
$st->closeCursor(); |
| 110 |
} catch (\PDOException $e) { |
| 111 |
throw new DatabaseFatalException('Failed to get user domain data', -1, $e); |
| 112 |
} |
| 113 |
return boolval($res); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public function fetch(array &$data): void |
| 124 |
{ |
| 125 |
try { |
| 126 |
$st = $this->connector->dbh()->prepare( |
| 127 |
'SELECT id, fqdn, active, description, created_time, updated_time FROM ' |
| 128 |
. $this->connector->tablePrefix('domains') . ' WHERE ' . $this->sqlCondition($data) |
| 129 |
); |
| 130 |
$this->sqlBindValues($st, $data, 1); |
| 131 |
$st->execute(); |
| 132 |
$res = $st->fetch(\PDO::FETCH_NUM); |
| 133 |
$st->closeCursor(); |
| 134 |
if (!$res) { |
| 135 |
throw new DatabaseNotFoundException('Domain not found'); |
| 136 |
} |
| 137 |
$data['id'] = intval($res[0]); |
| 138 |
$data['fqdn'] = $res[1]; |
| 139 |
$data['active'] = boolval($res[2]); |
| 140 |
$data['description'] = $res[3]; |
| 141 |
$data['created_time'] = new DateTime($res[4]); |
| 142 |
$data['updated_time'] = new DateTime($res[5]); |
| 143 |
} catch (\PDOException $e) { |
| 144 |
throw new DatabaseFatalException('Failed to fetch the domain data', -1, $e); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function save(array &$data): void |
| 156 |
{ |
| 157 |
$db = $this->connector->dbh(); |
| 158 |
$data['updated_time'] = new DateTime(); |
| 159 |
if ($this->exists($data)) { |
| 160 |
try { |
| 161 |
$st = $db->prepare( |
| 162 |
'UPDATE ' . $this->connector->tablePrefix('domains') |
| 163 |
. ' SET active = ?, description = ?, updated_time = ? WHERE id = ?' |
| 164 |
); |
| 165 |
$st->bindValue(1, $data['active'], \PDO::PARAM_BOOL); |
| 166 |
$st->bindValue(2, $data['description'], \PDO::PARAM_STR); |
| 167 |
$st->bindValue(3, $data['updated_time']->format('Y-m-d H:i:s'), \PDO::PARAM_STR); |
| 168 |
$st->bindValue(4, $data['id'], \PDO::PARAM_INT); |
| 169 |
$st->execute(); |
| 170 |
$st->closeCursor(); |
| 171 |
} catch (\PDOException $e) { |
| 172 |
throw new DatabaseFatalException('Failed to update the domain data', -1, $e); |
| 173 |
} |
| 174 |
} else { |
| 175 |
try { |
| 176 |
$active = $data['active'] ?? false; |
| 177 |
$data['created_time'] = $data['updated_time']; |
| 178 |
if (is_null($data['description'])) { |
| 179 |
$sql1 = ''; |
| 180 |
$sql2 = ''; |
| 181 |
} else { |
| 182 |
$sql1 = ', description'; |
| 183 |
$sql2 = ', ?'; |
| 184 |
} |
| 185 |
$st = $db->prepare( |
| 186 |
'INSERT INTO ' . $this->connector->tablePrefix('domains') |
| 187 |
. ' (fqdn, active' . $sql1 . ', created_time, updated_time)' |
| 188 |
. ' VALUES (?, ?' . $sql2 . ', ?, ?)' |
| 189 |
); |
| 190 |
$idx = 0; |
| 191 |
$st->bindValue(++$idx, $data['fqdn'], \PDO::PARAM_STR); |
| 192 |
$st->bindValue(++$idx, $active, \PDO::PARAM_BOOL); |
| 193 |
if (!is_null($data['description'])) { |
| 194 |
$st->bindValue(++$idx, $data['description'], \PDO::PARAM_STR); |
| 195 |
} |
| 196 |
$st->bindValue(++$idx, $data['created_time']->format('Y-m-d H:i:s'), \PDO::PARAM_STR); |
| 197 |
$st->bindValue(++$idx, $data['updated_time']->format('Y-m-d H:i:s'), \PDO::PARAM_STR); |
| 198 |
$st->execute(); |
| 199 |
$st->closeCursor(); |
| 200 |
$data['id'] = intval($db->lastInsertId()); |
| 201 |
$data['active'] = $active; |
| 202 |
} catch (\PDOException $e) { |
| 203 |
throw new DatabaseFatalException('Failed to insert the domain data', -1, $e); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public function delete(int $id, bool $force): void |
| 221 |
{ |
| 222 |
$db = $this->connector->dbh(); |
| 223 |
$db->beginTransaction(); |
| 224 |
try { |
| 225 |
$filter = [ 'domain' => $id ]; |
| 226 |
$limit = [ 'offset' => 0, 'count' => 0 ]; |
| 227 |
if ($force) { |
| 228 |
$order = []; |
| 229 |
$this->connector->getMapper('report')->delete($filter, $order, $limit); |
| 230 |
} else { |
| 231 |
$r_count = $this->connector->getMapper('report')->count($filter, $limit, 0); |
| 232 |
if ($r_count > 0) { |
| 233 |
switch ($r_count) { |
| 234 |
case 1: |
| 235 |
$s1 = 'is'; |
| 236 |
$s2 = ''; |
| 237 |
break; |
| 238 |
default: |
| 239 |
$s1 = 'are'; |
| 240 |
$s2 = 's'; |
| 241 |
break; |
| 242 |
} |
| 243 |
throw new SoftException( |
| 244 |
"Failed to delete: there {$s1} {$r_count} incoming report{$s2} for this domain", |
| 245 |
ErrorCodes::DOMAIN_HAS_REPORTS |
| 246 |
); |
| 247 |
} |
| 248 |
} |
| 249 |
$st = $db->prepare( |
| 250 |
'DELETE FROM ' . $this->connector->tablePrefix('userdomains') . ' WHERE domain_id = ?' |
| 251 |
); |
| 252 |
$st->bindValue(1, $id, \PDO::PARAM_INT); |
| 253 |
$st->execute(); |
| 254 |
$st->closeCursor(); |
| 255 |
$st = $db->prepare('DELETE FROM ' . $this->connector->tablePrefix('domains') . ' WHERE id = ?'); |
| 256 |
$st->bindValue(1, $id, \PDO::PARAM_INT); |
| 257 |
$st->execute(); |
| 258 |
$st->closeCursor(); |
| 259 |
$db->commit(); |
| 260 |
} catch (\PDOException $e) { |
| 261 |
$db->rollBack(); |
| 262 |
throw new DatabaseFatalException('Failed to delete the domain', -1, $e); |
| 263 |
} catch (\Exception $e) { |
| 264 |
$db->rollBack(); |
| 265 |
throw $e; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public function list(int $user_id): array |
| 277 |
{ |
| 278 |
$list = []; |
| 279 |
try { |
| 280 |
$query_str = 'SELECT id, fqdn, active, description, created_time, updated_time FROM '; |
| 281 |
if ($user_id) { |
| 282 |
$query_str .= $this->connector->tablePrefix('userdomains') . ' INNER JOIN ' |
| 283 |
. $this->connector->tablePrefix('domains') . ' ON domain_id = id WHERE user_id = ' |
| 284 |
. $user_id; |
| 285 |
} else { |
| 286 |
$query_str .= $this->connector->tablePrefix('domains'); |
| 287 |
} |
| 288 |
$st = $this->connector->dbh()->query($query_str); |
| 289 |
while ($row = $st->fetch(\PDO::FETCH_NUM)) { |
| 290 |
$list[] = [ |
| 291 |
'id' => intval($row[0]), |
| 292 |
'fqdn' => $row[1], |
| 293 |
'active' => boolval($row[2]), |
| 294 |
'description' => $row[3], |
| 295 |
'created_time' => new DateTime($row[4]), |
| 296 |
'updated_time' => new DateTime($row[5]) |
| 297 |
]; |
| 298 |
} |
| 299 |
$st->closeCursor(); |
| 300 |
} catch (\PDOException $e) { |
| 301 |
throw new DatabaseFatalException('Failed to get the domain list', -1, $e); |
| 302 |
} |
| 303 |
return $list; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
public function names(int $user_id): array |
| 314 |
{ |
| 315 |
$res = []; |
| 316 |
try { |
| 317 |
if ($user_id) { |
| 318 |
$query_str = 'SELECT fqdn FROM ' . $this->connector->tablePrefix('userdomains') |
| 319 |
. ' INNER JOIN ' . $this->connector->tablePrefix('domains') |
| 320 |
. ' ON domain_id = id WHERE user_id = ' . $user_id . ' ORDER BY fqdn'; |
| 321 |
} else { |
| 322 |
$query_str = 'SELECT fqdn FROM ' . $this->connector->tablePrefix('domains') . ' ORDER BY fqdn'; |
| 323 |
} |
| 324 |
$st = $this->connector->dbh()->query($query_str, \PDO::FETCH_NUM); |
| 325 |
while ($name = $st->fetchColumn(0)) { |
| 326 |
$res[] = $name; |
| 327 |
} |
| 328 |
$st->closeCursor(); |
| 329 |
} catch (\PDOException $e) { |
| 330 |
throw new DatabaseFatalException('Failed to get a list of domain names', -1, $e); |
| 331 |
} |
| 332 |
return $res; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
public function count(int $user_id, int $max = 0): int |
| 344 |
{ |
| 345 |
$number = 0; |
| 346 |
try { |
| 347 |
if ($user_id === 0) { |
| 348 |
$tn = 'domains'; |
| 349 |
$wr = ''; |
| 350 |
} else { |
| 351 |
$tn = 'userdomains'; |
| 352 |
$wr = " WHERE user_id = {$user_id}"; |
| 353 |
} |
| 354 |
$tn = $this->connector->tablePrefix($tn); |
| 355 |
$query_str = "SELECT COUNT(*) FROM {$tn}{$wr}"; |
| 356 |
if ($max > 0) { |
| 357 |
$query_str .= " LIMIT {$max}"; |
| 358 |
} |
| 359 |
$st = $this->connector->dbh()->query($query_str, \PDO::FETCH_NUM); |
| 360 |
$number = intval($st->fetchColumn(0)); |
| 361 |
$st->closeCursor(); |
| 362 |
} catch (\PDOException $e) { |
| 363 |
throw new DatabaseFatalException('Failed to get the number of domains', -1, $e); |
| 364 |
} |
| 365 |
return $number; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
public function assignUser(array &$data, int $user_id): void |
| 377 |
{ |
| 378 |
if (!$user_id) { |
| 379 |
throw new LogicException('Attempting to assign a domain to admin'); |
| 380 |
} |
| 381 |
|
| 382 |
$db = $this->connector->dbh(); |
| 383 |
$db->beginTransaction(); |
| 384 |
try { |
| 385 |
$st = $db->prepare( |
| 386 |
'SELECT id FROM ' . $this->connector->tablePrefix('domains') |
| 387 |
. ' WHERE ' . $this->sqlCondition($data) |
| 388 |
); |
| 389 |
$this->sqlBindValues($st, $data, 1); |
| 390 |
$st->execute(); |
| 391 |
$id = $st->fetchColumn(0); |
| 392 |
$st->closeCursor(); |
| 393 |
if ($id !== false) { |
| 394 |
$data['id'] = intval($id); |
| 395 |
$st = $db->prepare( |
| 396 |
'SELECT 1 FROM ' . $this->connector->tablePrefix('users') . ' WHERE id = ?' |
| 397 |
); |
| 398 |
$st->bindValue(1, $user_id, \PDO::PARAM_INT); |
| 399 |
$st->execute(); |
| 400 |
$res = $st->fetchColumn(0); |
| 401 |
$st->closeCursor(); |
| 402 |
if ($res) { |
| 403 |
$ud_tn = $this->connector->tablePrefix('userdomains'); |
| 404 |
$st = $db->prepare('SELECT 1 FROM ' . $ud_tn . ' WHERE domain_id = ? AND user_id = ?'); |
| 405 |
$st->bindValue(1, $data['id'], \PDO::PARAM_INT); |
| 406 |
$st->bindValue(2, $user_id, \PDO::PARAM_INT); |
| 407 |
$st->execute(); |
| 408 |
$res = $st->fetchColumn(0); |
| 409 |
$st->closeCursor(); |
| 410 |
if (!$res) { |
| 411 |
$st = $db->prepare('INSERT INTO ' . $ud_tn . ' (domain_id, user_id) VALUES (?, ?)'); |
| 412 |
$st->bindValue(1, $data['id'], \PDO::PARAM_INT); |
| 413 |
$st->bindValue(2, $user_id, \PDO::PARAM_INT); |
| 414 |
$st->execute(); |
| 415 |
$st->closeCursor(); |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
$db->commit(); |
| 420 |
} catch (\Exception $e) { |
| 421 |
$db->rollBack(); |
| 422 |
throw new DatabaseFatalException('Failed to assign a domain', -1, $e); |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
public function unassignUser(array &$data, int $user_id): void |
| 435 |
{ |
| 436 |
if (!$user_id) { |
| 437 |
throw new LogicException('Attempting to unassign a domain from admin'); |
| 438 |
} |
| 439 |
|
| 440 |
try { |
| 441 |
$dm_tn = $this->connector->tablePrefix('domains'); |
| 442 |
$ud_tn = $this->connector->tablePrefix('userdomains'); |
| 443 |
$st = $this->connector->dbh()->prepare( |
| 444 |
"DELETE {$ud_tn} FROM {$ud_tn} INNER JOIN {$dm_tn} ON domain_id = id WHERE " |
| 445 |
. $this->sqlCondition($data) |
| 446 |
); |
| 447 |
$this->sqlBindValues($st, $data, 1); |
| 448 |
$st->execute(); |
| 449 |
$st->closeCursor(); |
| 450 |
} catch (\PDOException $e) { |
| 451 |
throw new DatabaseFatalException('Failed to unassign a domain', -1, $e); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
public function updateUserDomains(array &$domains, int $user_id): void |
| 464 |
{ |
| 465 |
if (!$user_id) { |
| 466 |
throw new LogicException('Attempting to udpate domains for admin'); |
| 467 |
} |
| 468 |
|
| 469 |
$db = $this->connector->dbh(); |
| 470 |
$db->beginTransaction(); |
| 471 |
try { |
| 472 |
$st = $db->prepare( |
| 473 |
'DELETE FROM ' . $this->connector->tablePrefix('userdomains') . ' WHERE user_id = ?' |
| 474 |
); |
| 475 |
$st->bindValue(1, $user_id, \PDO::PARAM_INT); |
| 476 |
$st->execute(); |
| 477 |
$st->closeCursor(); |
| 478 |
$cnt = count($domains); |
| 479 |
if ($cnt) { |
| 480 |
$query_str = 'INSERT INTO ' . $this->connector->tablePrefix('userdomains') |
| 481 |
. ' (domain_id, user_id) SELECT id, ' . $user_id . ' FROM ' |
| 482 |
. $this->connector->tablePrefix('domains') . ' WHERE fqdn IN (' |
| 483 |
. substr(str_repeat('?,', $cnt), 0, -1) . ')'; |
| 484 |
$st = $db->prepare($query_str); |
| 485 |
$pos = 0; |
| 486 |
foreach ($domains as $dom_str) { |
| 487 |
$st->bindValue(++$pos, $dom_str, \PDO::PARAM_STR); |
| 488 |
} |
| 489 |
$st->execute(); |
| 490 |
$st->closeCursor(); |
| 491 |
} |
| 492 |
$db->commit(); |
| 493 |
} catch (\Exception $e) { |
| 494 |
$db->rollBack(); |
| 495 |
throw new DatabaseFatalException('Failed to update the user\'s domains', -1, $e); |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
private function sqlCondition(array &$data): string |
| 507 |
{ |
| 508 |
if (isset($data['id'])) { |
| 509 |
return 'id = ?'; |
| 510 |
} |
| 511 |
return 'fqdn = ?'; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
private function sqlBindValues($st, array &$data, int $pos): void |
| 524 |
{ |
| 525 |
if (isset($data['id'])) { |
| 526 |
$st->bindValue($pos, $data['id'], \PDO::PARAM_INT); |
| 527 |
} else { |
| 528 |
$st->bindValue($pos, $data['fqdn'], \PDO::PARAM_STR); |
| 529 |
} |
| 530 |
} |
| 531 |
} |