| 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 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
namespace Liuch\DmarcSrg; |
| 52 |
|
| 53 |
use Liuch\DmarcSrg\Users\User; |
| 54 |
use Liuch\DmarcSrg\Domains\Domain; |
| 55 |
use Liuch\DmarcSrg\Domains\DomainList; |
| 56 |
use Liuch\DmarcSrg\Exception\SoftException; |
| 57 |
use Liuch\DmarcSrg\Exception\RuntimeException; |
| 58 |
|
| 59 |
require realpath(__DIR__ . '/..') . '/init.php'; |
| 60 |
|
| 61 |
if (Core::isJson()) { |
| 62 |
try { |
| 63 |
if (Core::requestMethod() == 'GET') { |
| 64 |
$core = Core::instance(); |
| 65 |
$core->auth()->isAllowed(User::LEVEL_USER); |
| 66 |
|
| 67 |
$user = $core->getCurrentUser(); |
| 68 |
if (isset($_GET['domain'])) { |
| 69 |
$fqdn = trim($_GET['domain']); |
| 70 |
if (empty($fqdn)) { |
| 71 |
$core->auth()->isAllowed(User::LEVEL_MANAGER); |
| 72 |
$res = []; |
| 73 |
if ($user->id() !== 0 && $core->config('users/domain_verification', 'none') === 'dns') { |
| 74 |
$res['verification'] = 'dns'; |
| 75 |
$res['verification_data'] = 'dmarcsrg-verification=' . $user->verificationString(); |
| 76 |
} |
| 77 |
} else { |
| 78 |
$domain = new Domain($_GET['domain']); |
| 79 |
$domain->isAssigned($user, true); |
| 80 |
$res = $domain->toArray(); |
| 81 |
} |
| 82 |
Core::sendJson($res); |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
$res = (new DomainList($user))->getList(); |
| 87 |
$list = array_map(function ($domain) { |
| 88 |
return $domain->toArray(); |
| 89 |
}, $res['domains']); |
| 90 |
|
| 91 |
Core::sendJson([ |
| 92 |
'domains' => $list, |
| 93 |
'more' => $res['more'] |
| 94 |
]); |
| 95 |
return; |
| 96 |
} elseif (Core::requestMethod() == 'POST') { |
| 97 |
$core = Core::instance(); |
| 98 |
Core::instance()->auth()->isAllowed(User::LEVEL_MANAGER); |
| 99 |
|
| 100 |
$data = Core::getJsonData(); |
| 101 |
if ($data) { |
| 102 |
$domain = new Domain([ |
| 103 |
'fqdn' => $data['fqdn'] ?? null, |
| 104 |
'active' => $data['active'] ?? null, |
| 105 |
'description' => $data['description'] ?? null |
| 106 |
]); |
| 107 |
$user = Core::instance()->getCurrentUser(); |
| 108 |
$action = $data['action'] ?? ''; |
| 109 |
switch ($action) { |
| 110 |
case 'add': |
| 111 |
if ($domain->isAssigned($user)) { |
| 112 |
throw new SoftException('The domain already exists'); |
| 113 |
} |
| 114 |
if ($user->id() !== 0) { |
| 115 |
$method = $core->config('users/domain_verification', 'none'); |
| 116 |
if ($method !== 'none') { |
| 117 |
$domain->verifyOwnership($user->verificationString(), $method); |
| 118 |
} |
| 119 |
} |
| 120 |
if (!$domain->exists()) { |
| 121 |
$domain->save(); |
| 122 |
} |
| 123 |
$domain->assignUser($user); |
| 124 |
break; |
| 125 |
case 'update': |
| 126 |
$domain->isAssigned($user, true); |
| 127 |
$domain->save(); |
| 128 |
break; |
| 129 |
case 'delete': |
| 130 |
if ($user->id() === 0) { |
| 131 |
$domain->delete($data['force'] ?? false); |
| 132 |
} else { |
| 133 |
$domain->unassignUser($user); |
| 134 |
} |
| 135 |
unset($domain); |
| 136 |
break; |
| 137 |
default: |
| 138 |
throw new SoftException('Unknown action. Valid values are "add", "update", "delete".'); |
| 139 |
} |
| 140 |
|
| 141 |
$res = [ |
| 142 |
'error_code' => 0, |
| 143 |
'message' => 'Successfully' |
| 144 |
]; |
| 145 |
if (isset($domain)) { |
| 146 |
$res['domain'] = $domain->toArray(); |
| 147 |
} |
| 148 |
Core::sendJson($res); |
| 149 |
return; |
| 150 |
} |
| 151 |
} |
| 152 |
} catch (RuntimeException $e) { |
| 153 |
Core::sendJson(ErrorHandler::exceptionResult($e)); |
| 154 |
return; |
| 155 |
} |
| 156 |
} elseif (Core::requestMethod() == 'GET') { |
| 157 |
Core::instance()->sendHtml(); |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
Core::sendBad(); |