| 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 |
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); |