Debian Codegraph @

public/admin.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2020-2025 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
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();