| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace Liuch\DmarcSrg; |
| 24 |
|
| 25 |
use Liuch\DmarcSrg\Users\User; |
| 26 |
use Liuch\DmarcSrg\Users\AdminUser; |
| 27 |
use Liuch\DmarcSrg\Exception\SoftException; |
| 28 |
use Liuch\DmarcSrg\Exception\RuntimeException; |
| 29 |
|
| 30 |
require realpath(__DIR__ . '/..') . '/init.php'; |
| 31 |
|
| 32 |
$core = Core::instance(); |
| 33 |
if (Core::isJson()) { |
| 34 |
try { |
| 35 |
$core->auth()->isAllowed(User::LEVEL_ADMIN); |
| 36 |
|
| 37 |
if (Core::requestMethod() == 'GET') { |
| 38 |
Core::sendJson($core->admin()->state()); |
| 39 |
return; |
| 40 |
} elseif (Core::requestMethod() == 'POST') { |
| 41 |
$data = Core::getJsonData(); |
| 42 |
if ($data) { |
| 43 |
$cmd = $data['cmd']; |
| 44 |
if (in_array($cmd, [ 'initdb', 'cleandb', 'upgradedb' ])) { |
| 45 |
if ($core->auth()->isEnabled()) { |
| 46 |
$pwd = isset($data['password']) ? $data['password'] : ''; |
| 47 |
if (!(new AdminUser($core))->verifyPassword($pwd)) { |
| 48 |
throw new SoftException('Incorrect password'); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
if ($cmd === 'initdb') { |
| 53 |
Core::sendJson($core->database()->initDb()); |
| 54 |
return; |
| 55 |
} elseif ($cmd === 'cleandb') { |
| 56 |
Core::sendJson($core->database()->cleanDb()); |
| 57 |
return; |
| 58 |
} elseif ($cmd === 'checksource') { |
| 59 |
if (isset($data['id']) && isset($data['type'])) { |
| 60 |
$id = $data['id']; |
| 61 |
$type = $data['type']; |
| 62 |
if (gettype($id) === 'integer' && gettype($type) === 'string') { |
| 63 |
Core::sendJson( |
| 64 |
$core->admin()->checkSource($id, $type) |
| 65 |
); |
| 66 |
return; |
| 67 |
} |
| 68 |
} |
| 69 |
} elseif ($cmd === 'upgradedb') { |
| 70 |
$db = Core::instance()->database(); |
| 71 |
$db->getMapper('upgrader')->go($db::REQUIRED_VERSION); |
| 72 |
Core::sendJson( |
| 73 |
[ |
| 74 |
'error_code' => 0, |
| 75 |
'message' => 'Upgraded successfully' |
| 76 |
] |
| 77 |
); |
| 78 |
return; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
Core::sendJson([ 'error_code' => -1, 'message' => 'Bad request' ]); |
| 83 |
} catch (RuntimeException $e) { |
| 84 |
Core::sendJson(ErrorHandler::exceptionResult($e)); |
| 85 |
} |
| 86 |
return; |
| 87 |
} elseif (Core::requestMethod() == 'GET') { |
| 88 |
$core->sendHtml(); |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
Core::sendBad(); |