Debian Codegraph @

utils/database_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-2024 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 *
24 * This script performs some administration functions with the database.
25 * It has one required parameter, it can has follow values: `status`, `init`, `upgrade`, `drop`.
26 * `status` - Will display the database status.
27 * `init` - Will create all the required tables in the database.
28 * The database must contain no tables.
29 * `upgrade` - Will upgrade the database structure if it is necessary.
30 * `drop` - Will remove all tables from the database.
31 *
32 * @category Utilities
33 * @package DmarcSrg
34 * @author Aleksey Andreev (liuch)
35 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
36 */
37
38 namespace Liuch\DmarcSrg;
39
40 use Liuch\DmarcSrg\Database\Database;
41 use Liuch\DmarcSrg\Database\DatabaseUpgrader;
42 use Liuch\DmarcSrg\Settings\SettingString;
43 use Liuch\DmarcSrg\Exception\RuntimeException;
44
45 require realpath(__DIR__ . '/..') . '/init.php';
46
47 if (Core::isWEB()) {
48 echo 'Forbidden';
49 exit(1);
50 }
51
52 if (!isset($argv)) {
53 echo 'Cannot get the script arguments. Probably register_argc_argv is disabled.', PHP_EOL;
54 exit(1);
55 }
56
57 $res = null;
58 $core = Core::instance();
59 try {
60 $core->setCurrentUser('admin');
61 $action = $argv[1] ?? '';
62 switch ($action) {
63 case 'status':
64 $res = $core->database()->state();
65 $tcn = 0;
66 if (isset($res['tables'])) {
67 foreach ($res['tables'] as &$t) {
68 if (isset($t['exists']) && $t['exists'] === true) {
69 ++$tcn;
70 }
71 }
72 unset($t);
73 }
74 echo 'Version : ', ($res['version'] ?? 'n/a'), PHP_EOL;
75 echo 'Tables : ', $tcn, PHP_EOL;
76 $prefix = $core->config('database/table_prefix');
77 echo 'Table prefix: ', (empty($prefix) ? 'None' : "\"{$prefix}\""), PHP_EOL;
78 echo PHP_EOL;
79 break;
80 case 'init':
81 $res = $core->database()->initDb();
82 break;
83 case 'upgrade':
84 $db = $core->database();
85 $cur_ver = $db->state()['version'] ?? 'n/a';
86 echo "Current version: {$cur_ver}", PHP_EOL;
87 echo 'Required version: ', $db::REQUIRED_VERSION, PHP_EOL;
88 if ($cur_ver !== $db::REQUIRED_VERSION) {
89 $db->getMapper('upgrader')->go($db::REQUIRED_VERSION);
90 $res = [
91 'error_code' => 0,
92 'message' => 'Upgraded successfully'
93 ];
94 } else {
95 $res = [
96 'error_code' => 0,
97 'message' => 'No upgrade required'
98 ];
99 }
100 break;
101 case 'drop':
102 $res = $core->database()->cleanDb();
103 break;
104 default:
105 echo "Usage: {$argv[0]} <command>", PHP_EOL, PHP_EOL;
106 echo 'Commands:', PHP_EOL;
107 echo ' status Displays the database status.', PHP_EOL;
108 echo ' init Creates all the required the database tables.', PHP_EOL;
109 echo ' upgrade Upgrages the database structure if it is necessary.', PHP_EOL;
110 echo ' drop Deletes all tables from the database taking into account the table prefix.', PHP_EOL;
111 exit(2);
112 }
113 } catch (RuntimeException $e) {
114 $res = ErrorHandler::exceptionResult($e);
115 }
116
117 $error = ($res['error_code'] ?? 0) ? 'Error! ' : '';
118 echo "Message: {$error}{$res['message']}", PHP_EOL;
119
120 if (isset($res['debug_info'])) {
121 $debug_info = $res['debug_info'];
122 echo "Debug info:", PHP_EOL;
123 echo '-----', PHP_EOL;
124 echo "[{$debug_info['code']}]", PHP_EOL;
125 echo '-----', PHP_EOL;
126 echo "{$debug_info['content']}", PHP_EOL;
127 }
128
129 exit(empty($error) ? 0 : 3);