| 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\Domains; |
| 33 |
|
| 34 |
use Liuch\DmarcSrg\Core; |
| 35 |
use Liuch\DmarcSrg\Exception\SoftException; |
| 36 |
use Liuch\DmarcSrg\Exception\LogicException; |
| 37 |
use Liuch\DmarcSrg\Exception\DatabaseNotFoundException; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class Domain |
| 46 |
{ |
| 47 |
private $db = null; |
| 48 |
private $ex_f = null; |
| 49 |
private $data = [ |
| 50 |
'id' => null, |
| 51 |
'fqdn' => null, |
| 52 |
'active' => null, |
| 53 |
'description' => null, |
| 54 |
'created_time' => null, |
| 55 |
'updated_time' => null |
| 56 |
]; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function __construct($data, $db = null) |
| 79 |
{ |
| 80 |
$this->db = $db ?? Core::instance()->database(); |
| 81 |
switch (gettype($data)) { |
| 82 |
case 'integer': |
| 83 |
$this->data['id'] = $data; |
| 84 |
return; |
| 85 |
case 'string': |
| 86 |
$this->data['fqdn'] = strtolower($data); |
| 87 |
$this->checkFqdn(); |
| 88 |
return; |
| 89 |
case 'array': |
| 90 |
if (isset($data['id'])) { |
| 91 |
if (gettype($data['id']) !== 'integer') { |
| 92 |
break; |
| 93 |
} |
| 94 |
$this->data['id'] = $data['id']; |
| 95 |
} |
| 96 |
if (isset($data['fqdn'])) { |
| 97 |
if (gettype($data['fqdn']) !== 'string') { |
| 98 |
break; |
| 99 |
} |
| 100 |
$this->data['fqdn'] = strtolower($data['fqdn']); |
| 101 |
$this->checkFqdn(); |
| 102 |
} |
| 103 |
if (isset($data['active'])) { |
| 104 |
if (gettype($data['active']) !== 'boolean') { |
| 105 |
break; |
| 106 |
} |
| 107 |
$this->data['active'] = $data['active']; |
| 108 |
} else { |
| 109 |
$this->data['active'] = false; |
| 110 |
} |
| 111 |
if (isset($data['description'])) { |
| 112 |
if (gettype($data['description']) !== 'string') { |
| 113 |
break; |
| 114 |
} |
| 115 |
$this->data['description'] = $data['description']; |
| 116 |
} |
| 117 |
if (isset($data['created_time'])) { |
| 118 |
if (gettype($data['created_time']) !== 'object') { |
| 119 |
break; |
| 120 |
} |
| 121 |
$this->data['created_time'] = $data['created_time']; |
| 122 |
} |
| 123 |
if (isset($data['updated_time'])) { |
| 124 |
if (gettype($data['updated_time']) !== 'object') { |
| 125 |
break; |
| 126 |
} |
| 127 |
$this->data['updated_time'] = $data['updated_time']; |
| 128 |
} |
| 129 |
if (!is_null($this->data['id']) || !is_null($this->data['fqdn'])) { |
| 130 |
return; |
| 131 |
} |
| 132 |
} |
| 133 |
throw new LogicException('Wrong domain data'); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function exists(): bool |
| 142 |
{ |
| 143 |
if (is_null($this->ex_f)) { |
| 144 |
$this->ex_f = $this->db->getMapper('domain')->exists($this->data); |
| 145 |
} |
| 146 |
return $this->ex_f; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public function ensure(string $state): void |
| 159 |
{ |
| 160 |
switch ($state) { |
| 161 |
case 'exist': |
| 162 |
if (!$this->exists()) { |
| 163 |
$this->unknownDomain(); |
| 164 |
} |
| 165 |
break; |
| 166 |
case 'nonexist': |
| 167 |
if ($this->exists()) { |
| 168 |
throw new SoftException('The domain already exists'); |
| 169 |
} |
| 170 |
break; |
| 171 |
default: |
| 172 |
throw new LogicException('Unknown domain state'); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public function id(): int |
| 182 |
{ |
| 183 |
if (is_null($this->data['id'])) { |
| 184 |
$this->fetchData(); |
| 185 |
} |
| 186 |
return $this->data['id']; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public function fqdn(): string |
| 195 |
{ |
| 196 |
if (is_null($this->data['fqdn'])) { |
| 197 |
$this->fetchData(); |
| 198 |
} |
| 199 |
return $this->data['fqdn']; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public function active(): bool |
| 211 |
{ |
| 212 |
if (is_null($this->data['active'])) { |
| 213 |
$this->fetchData(); |
| 214 |
} |
| 215 |
return $this->data['active']; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public function description() |
| 224 |
{ |
| 225 |
if (is_null($this->data['id']) || is_null($this->data['fqdn'])) { |
| 226 |
$this->fetchData(); |
| 227 |
} |
| 228 |
return $this->data['description']; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public function toArray(): array |
| 237 |
{ |
| 238 |
foreach ([ 'id', 'fqdn', 'active' ] as $it) { |
| 239 |
if (is_null($this->data[$it])) { |
| 240 |
$this->fetchData(); |
| 241 |
break; |
| 242 |
} |
| 243 |
} |
| 244 |
$res = $this->data; |
| 245 |
unset($res['id']); |
| 246 |
return $res; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public function isAssigned($user, bool $exception = false): bool |
| 258 |
{ |
| 259 |
if ($this->ex_f !== false) { |
| 260 |
$user_id = $user->id(); |
| 261 |
if ($user_id === 0) { |
| 262 |
|
| 263 |
if ($this->exists()) { |
| 264 |
return true; |
| 265 |
} |
| 266 |
} elseif ($this->db->getMapper('domain')->isAssigned($this->data, $user_id)) { |
| 267 |
$this->ex_f = true; |
| 268 |
return true; |
| 269 |
} |
| 270 |
} |
| 271 |
if ($exception) { |
| 272 |
$this->unknownDomain(); |
| 273 |
} |
| 274 |
return false; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public function assignUser($user): void |
| 285 |
{ |
| 286 |
$user_id = $user->id(); |
| 287 |
if ($user_id !== 0) { |
| 288 |
$this->db->getMapper('domain')->assignUser($this->data, $user_id); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
public function unassignUser($user): void |
| 300 |
{ |
| 301 |
$user_id = $user->id(); |
| 302 |
if ($user_id !== 0) { |
| 303 |
$this->db->getMapper('domain')->unassignUser($this->data, $user_id); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public function save(): void |
| 316 |
{ |
| 317 |
$this->db->getMapper('domain')->save($this->data); |
| 318 |
$this->ex_f = true; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public function delete(bool $force): void |
| 334 |
{ |
| 335 |
if (is_null($this->data['id'])) { |
| 336 |
$this->fetchData(); |
| 337 |
} |
| 338 |
$this->db->getMapper('domain')->delete($this->data['id'], $force); |
| 339 |
$this->ex_f = false; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
public function verifyOwnership(string $key, string $method): void |
| 353 |
{ |
| 354 |
$passed = false; |
| 355 |
switch ($method) { |
| 356 |
case 'dns': |
| 357 |
$dr = dns_get_record($this->data['fqdn'], DNS_TXT); |
| 358 |
if ($dr) { |
| 359 |
$str = 'dmarcsrg-verification=' . $key; |
| 360 |
foreach ($dr as &$rec) { |
| 361 |
if (trim($rec['txt']) === $str) { |
| 362 |
$passed = true; |
| 363 |
break; |
| 364 |
} |
| 365 |
} |
| 366 |
unset($rec); |
| 367 |
} |
| 368 |
break; |
| 369 |
} |
| 370 |
if (!$passed) { |
| 371 |
throw new SoftException('Domain verification failed'); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
private function checkFqdn(): void |
| 381 |
{ |
| 382 |
$fqdn = trim($this->data['fqdn']); |
| 383 |
if (substr($fqdn, -1) === '.') { |
| 384 |
$fqdn = trim(substr($fqdn, 0, -1)); |
| 385 |
} |
| 386 |
if ($fqdn === '') { |
| 387 |
throw new SoftException('The domain name must not be an empty string'); |
| 388 |
} |
| 389 |
$this->data['fqdn'] = $fqdn; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
private function fetchData(): void |
| 398 |
{ |
| 399 |
if ($this->ex_f === false) { |
| 400 |
return; |
| 401 |
} |
| 402 |
|
| 403 |
try { |
| 404 |
$this->db->getMapper('domain')->fetch($this->data); |
| 405 |
$this->ex_f = true; |
| 406 |
} catch (DatabaseNotFoundException $e) { |
| 407 |
$this->ex_f = false; |
| 408 |
$this->unknownDomain(); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
private function unknownDomain(): void |
| 420 |
{ |
| 421 |
$name = ''; |
| 422 |
if (!empty($this->data['fqdn'])) { |
| 423 |
$name = ': ' . $this->data['fqdn']; |
| 424 |
} |
| 425 |
throw new SoftException('Unknown domain' . $name); |
| 426 |
} |
| 427 |
} |