2021-04-06 16:34:12 +02:00
|
|
|
<?php
|
|
|
|
|
2021-11-11 14:25:26 +01:00
|
|
|
define('WEBROOT', realpath("."));
|
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
include_once 'Core/core.php';
|
|
|
|
require_once 'Core/datetime.php';
|
|
|
|
include_once 'Core/constants.php';
|
|
|
|
|
|
|
|
use Core\Configuration\DatabaseScript;
|
|
|
|
use Core\Driver\SQL\Column\Column;
|
|
|
|
use Core\Driver\SQL\Condition\Compare;
|
|
|
|
use Core\Driver\SQL\Condition\CondIn;
|
|
|
|
use Core\Driver\SQL\Expression\DateSub;
|
|
|
|
use Core\Driver\SQL\SQL;
|
|
|
|
use Core\Objects\ConnectionData;
|
2021-04-06 16:34:12 +02:00
|
|
|
|
2021-04-07 00:03:14 +02:00
|
|
|
function printLine(string $line = "") {
|
|
|
|
echo $line . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _exit(string $line = "") {
|
|
|
|
printLine($line);
|
2021-04-06 16:34:12 +02:00
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDatabaseConfig(): ConnectionData {
|
2022-11-18 18:54:22 +01:00
|
|
|
$configClass = "\\Site\\Configuration\\Database";
|
2021-04-06 16:34:12 +02:00
|
|
|
$file = getClassPath($configClass);
|
|
|
|
if (!file_exists($file) || !is_readable($file)) {
|
2021-04-07 00:03:14 +02:00
|
|
|
_exit("Database configuration does not exist or is not readable");
|
2021-04-06 16:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
include_once $file;
|
|
|
|
return new $configClass();
|
|
|
|
}
|
|
|
|
|
2023-01-07 15:34:05 +01:00
|
|
|
$context = \Core\Objects\Context::instance();
|
2022-06-20 19:52:31 +02:00
|
|
|
if (!$context->isCLI()) {
|
|
|
|
_exit("Can only be executed via CLI");
|
|
|
|
}
|
|
|
|
|
|
|
|
$database = $context->getConfig()->getDatabase();
|
2022-02-21 13:01:03 +01:00
|
|
|
if ($database !== null && $database->getProperty("isDocker", false) && !is_file("/.dockerenv")) {
|
2022-06-01 09:47:31 +02:00
|
|
|
if (count($argv) < 3 || $argv[1] !== "db" || !in_array($argv[2], ["shell", "import", "export"])) {
|
2022-02-21 15:54:37 +01:00
|
|
|
$command = array_merge(["docker", "exec", "-it", "php", "php"], $argv);
|
|
|
|
$proc = proc_open($command, [1 => STDOUT, 2 => STDERR], $pipes, "/application");
|
|
|
|
exit(proc_close($proc));
|
|
|
|
}
|
2022-02-21 13:01:03 +01:00
|
|
|
}
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
function connectSQL(): ?SQL {
|
|
|
|
global $context;
|
|
|
|
$sql = $context->initSQL();
|
|
|
|
if (!$sql || !$sql->isConnected()) {
|
|
|
|
printLine("Could not establish database connection");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
2021-04-06 16:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function printHelp() {
|
2021-04-06 23:05:02 +02:00
|
|
|
// TODO: help
|
2021-04-06 16:34:12 +02:00
|
|
|
}
|
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
function applyPatch(\Core\Driver\SQL\SQL $sql, string $patchName): bool {
|
2021-04-09 12:37:24 +02:00
|
|
|
$class = str_replace('/', '\\', $patchName);
|
2022-11-19 01:15:34 +01:00
|
|
|
$className = "\\Core\\Configuration\\$class";
|
2021-04-09 12:37:24 +02:00
|
|
|
$classPath = getClassPath($className);
|
|
|
|
if (!file_exists($classPath) || !is_readable($classPath)) {
|
|
|
|
printLine("Database script file does not exist or is not readable");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
include_once $classPath;
|
|
|
|
$obj = new $className();
|
|
|
|
if (!($obj instanceof DatabaseScript)) {
|
|
|
|
printLine("Not a database script");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$queries = $obj->createQueries($sql);
|
|
|
|
foreach ($queries as $query) {
|
|
|
|
if (!$query->execute($sql)) {
|
|
|
|
printLine($sql->getLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-07 00:03:14 +02:00
|
|
|
function handleDatabase(array $argv) {
|
2021-04-06 16:34:12 +02:00
|
|
|
$action = $argv[2] ?? "";
|
|
|
|
|
2021-04-06 20:59:55 +02:00
|
|
|
if ($action === "migrate") {
|
2022-06-20 19:52:31 +02:00
|
|
|
$sql = connectSQL() or die();
|
2022-11-20 17:13:53 +01:00
|
|
|
|
2022-05-31 16:14:49 +02:00
|
|
|
} else if (in_array($action, ["export", "import", "shell"])) {
|
2021-04-06 20:59:55 +02:00
|
|
|
|
|
|
|
// database config
|
2022-02-21 15:57:15 +01:00
|
|
|
$config = getDatabaseConfig();
|
|
|
|
$dbType = $config->getProperty("type") ?? null;
|
|
|
|
$user = $config->getLogin();
|
|
|
|
$password = $config->getPassword();
|
|
|
|
$database = $config->getProperty("database");
|
|
|
|
$host = $config->getHost();
|
|
|
|
$port = $config->getPort();
|
2021-04-06 20:59:55 +02:00
|
|
|
|
|
|
|
// subprocess config
|
|
|
|
$env = [];
|
|
|
|
$options = array_slice($argv, 3);
|
|
|
|
$dataOnly = in_array("--data-only", $options) || in_array("-d", $options);
|
|
|
|
$descriptorSpec = [STDIN, STDOUT, STDOUT];
|
|
|
|
$inputData = null;
|
|
|
|
|
|
|
|
// argument config
|
|
|
|
if ($action === "import") {
|
|
|
|
$file = $argv[3] ?? null;
|
|
|
|
if (!$file) {
|
2021-04-07 00:03:14 +02:00
|
|
|
_exit("Usage: cli.php db import <path>");
|
2021-04-06 20:31:52 +02:00
|
|
|
}
|
|
|
|
|
2021-04-06 20:59:55 +02:00
|
|
|
if (!file_exists($file) || !is_readable($file)) {
|
2021-04-07 00:03:14 +02:00
|
|
|
_exit("File not found or not readable");
|
2021-04-06 20:31:52 +02:00
|
|
|
}
|
|
|
|
|
2021-04-06 20:59:55 +02:00
|
|
|
$inputData = file_get_contents($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($dbType === "mysql") {
|
|
|
|
$command_args = ["-u", $user, '-h', $host, '-P', $port, "--password=$password"];
|
|
|
|
if ($action === "export") {
|
|
|
|
$command_bin = "mysqldump";
|
|
|
|
if ($dataOnly) {
|
|
|
|
$command_args[] = "--skip-triggers";
|
|
|
|
$command_args[] = "--compact";
|
|
|
|
$command_args[] = "--no-create-info";
|
2021-04-06 20:31:52 +02:00
|
|
|
}
|
2021-04-06 20:59:55 +02:00
|
|
|
} else if ($action === "import") {
|
|
|
|
$command_bin = "mysql";
|
|
|
|
$descriptorSpec[0] = ["pipe", "r"];
|
2022-05-31 16:14:49 +02:00
|
|
|
} else if ($action === "shell") {
|
|
|
|
$command_bin = "mysql";
|
|
|
|
$descriptorSpec = [];
|
2021-04-06 20:31:52 +02:00
|
|
|
}
|
2021-04-06 20:59:55 +02:00
|
|
|
} else if ($dbType === "postgres") {
|
2021-04-06 20:31:52 +02:00
|
|
|
|
2021-04-06 20:59:55 +02:00
|
|
|
$env["PGPASSWORD"] = $password;
|
|
|
|
$command_args = ["-U", $user, '-h', $host, '-p', $port];
|
2021-04-06 19:01:20 +02:00
|
|
|
|
2021-04-06 20:59:55 +02:00
|
|
|
if ($action === "export") {
|
|
|
|
$command_bin = "/usr/bin/pg_dump";
|
|
|
|
if ($dataOnly) {
|
|
|
|
$command_args[] = "--data-only";
|
2021-04-06 19:01:20 +02:00
|
|
|
}
|
2021-04-06 20:59:55 +02:00
|
|
|
} else if ($action === "import") {
|
|
|
|
$command_bin = "/usr/bin/psql";
|
|
|
|
$descriptorSpec[0] = ["pipe", "r"];
|
2022-05-31 16:14:49 +02:00
|
|
|
} else if ($action === "shell") {
|
|
|
|
$command_bin = "/usr/bin/psql";
|
|
|
|
$descriptorSpec = [];
|
2021-04-06 16:34:12 +02:00
|
|
|
}
|
|
|
|
|
2021-04-06 20:59:55 +02:00
|
|
|
} else {
|
2021-04-07 00:03:14 +02:00
|
|
|
_exit("Unsupported database type");
|
2021-04-06 20:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($database) {
|
|
|
|
$command_args[] = $database;
|
|
|
|
}
|
|
|
|
|
|
|
|
$command = array_merge([$command_bin], $command_args);
|
2022-02-21 15:57:15 +01:00
|
|
|
if ($config->getProperty("isDocker", false)) {
|
2022-08-20 22:17:17 +02:00
|
|
|
$command = array_merge(["docker", "exec", "-it", $config->getHost()], $command);
|
2022-02-21 15:54:37 +01:00
|
|
|
}
|
|
|
|
|
2021-04-06 20:59:55 +02:00
|
|
|
$process = proc_open($command, $descriptorSpec, $pipes, null, $env);
|
|
|
|
|
|
|
|
if (is_resource($process)) {
|
|
|
|
if ($action === "import" && $inputData && count($pipes) > 0) {
|
|
|
|
fwrite($pipes[0], $inputData);
|
|
|
|
fclose($pipes[0]);
|
2021-04-06 16:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
proc_close($process);
|
2021-04-06 20:59:55 +02:00
|
|
|
}
|
2021-04-08 19:08:05 +02:00
|
|
|
} else if ($action === "clean") {
|
2022-06-20 19:52:31 +02:00
|
|
|
$sql = connectSQL() or die();
|
2021-04-08 19:08:05 +02:00
|
|
|
printLine("Deleting user related data older than 90 days...");
|
|
|
|
|
|
|
|
// 1st: Select all related tables and entities
|
|
|
|
$tables = [];
|
|
|
|
$res = $sql->select("entityId", "tableName")
|
|
|
|
->from("EntityLog")
|
2021-04-08 19:48:04 +02:00
|
|
|
->where(new Compare("modified", new DateSub($sql->now(), new Column("lifetime"), "DAY"), "<="))
|
|
|
|
->dump()
|
2021-04-08 19:08:05 +02:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$success = ($res !== false);
|
|
|
|
if (!$success) {
|
|
|
|
_exit("Error querying data: " . $sql->getLastError());
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($res as $row) {
|
|
|
|
$tableName = $row["tableName"];
|
|
|
|
$uid = $row["entityId"];
|
|
|
|
if (!isset($tables[$tableName])) {
|
|
|
|
$tables[$tableName] = [];
|
|
|
|
}
|
|
|
|
$tables[$tableName][] = $uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2nd: delete!
|
2022-06-20 19:52:31 +02:00
|
|
|
foreach ($tables as $table => $ids) {
|
2021-04-08 19:08:05 +02:00
|
|
|
$success = $sql->delete($table)
|
2022-06-20 19:52:31 +02:00
|
|
|
->where(new CondIn(new Column("id"), $ids))
|
2021-04-08 19:08:05 +02:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
if (!$success) {
|
|
|
|
printLine("Error deleting data: " . $sql->getLastError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printLine("Done!");
|
2021-04-06 23:05:02 +02:00
|
|
|
} else {
|
2022-05-31 16:14:49 +02:00
|
|
|
_exit("Usage: cli.php db <migrate|import|export|shell> [options...]");
|
2021-04-06 23:05:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 13:09:50 +02:00
|
|
|
function findPullBranch(array $output): ?string {
|
|
|
|
foreach ($output as $line) {
|
|
|
|
$parts = preg_split('/\s+/', $line);
|
|
|
|
if (count($parts) >= 3 && $parts[2] === '(fetch)') {
|
|
|
|
$remoteName = $parts[0];
|
|
|
|
$url = $parts[1];
|
|
|
|
if (endsWith($url, "@github.com:rhergenreder/web-base.git") ||
|
|
|
|
endsWith($url, "@romanh.de:Projekte/web-base.git") ||
|
|
|
|
$url === 'https://github.com/rhergenreder/web-base.git' ||
|
|
|
|
$url === 'https://git.romanh.de/Projekte/web-base.git') {
|
|
|
|
return "$remoteName/master";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-04-07 00:03:14 +02:00
|
|
|
function onMaintenance(array $argv) {
|
2021-04-06 23:05:02 +02:00
|
|
|
$action = $argv[2] ?? "status";
|
|
|
|
$maintenanceFile = "MAINTENANCE";
|
|
|
|
$isMaintenanceEnabled = file_exists($maintenanceFile);
|
|
|
|
|
|
|
|
if ($action === "status") {
|
2021-04-07 00:03:14 +02:00
|
|
|
_exit("Maintenance: " . ($isMaintenanceEnabled ? "on" : "off"));
|
2021-04-06 23:05:02 +02:00
|
|
|
} else if ($action === "on") {
|
2021-04-07 00:03:14 +02:00
|
|
|
$file = fopen($maintenanceFile, 'w') or _exit("Unable to create maintenance file");
|
2021-04-06 23:05:02 +02:00
|
|
|
fclose($file);
|
2021-04-07 00:03:14 +02:00
|
|
|
_exit("Maintenance enabled");
|
2021-04-06 23:05:02 +02:00
|
|
|
} else if ($action === "off") {
|
|
|
|
if (file_exists($maintenanceFile)) {
|
|
|
|
if (!unlink($maintenanceFile)) {
|
2021-04-07 00:03:14 +02:00
|
|
|
_exit("Unable to delete maintenance file");
|
2021-04-06 23:05:02 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-07 00:03:14 +02:00
|
|
|
_exit("Maintenance disabled");
|
2021-04-07 00:55:53 +02:00
|
|
|
} else if ($action === "update") {
|
|
|
|
|
2022-11-19 01:15:34 +01:00
|
|
|
$oldPatchFiles = glob('Core/Configuration/Patch/*.php');
|
2021-04-07 13:09:50 +02:00
|
|
|
printLine("$ git remote -v");
|
|
|
|
exec("git remote -v", $gitRemote, $ret);
|
|
|
|
if ($ret !== 0) {
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
|
|
|
$pullBranch = findPullBranch($gitRemote);
|
|
|
|
if ($pullBranch === null) {
|
|
|
|
$pullBranch = 'origin/master';
|
|
|
|
printLine("Unable to find remote update branch. Make sure, you are still in a git repository, and one of the remote branches " .
|
|
|
|
"have the original fetch url");
|
|
|
|
printLine("Trying to continue with '$pullBranch'");
|
|
|
|
} else {
|
|
|
|
printLine("Using remote update branch: $pullBranch");
|
|
|
|
}
|
2021-04-07 00:55:53 +02:00
|
|
|
|
|
|
|
printLine("$ git fetch " . str_replace("/", " ", $pullBranch));
|
|
|
|
exec("git fetch " . str_replace("/", " ", $pullBranch), $gitFetch, $ret);
|
|
|
|
if ($ret !== 0) {
|
2021-04-07 01:00:52 +02:00
|
|
|
die();
|
2021-04-07 00:55:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
printLine("$ git log HEAD..$pullBranch --oneline");
|
|
|
|
exec("git log HEAD..$pullBranch --oneline", $gitLog, $ret);
|
|
|
|
if ($ret !== 0) {
|
2021-04-07 01:00:52 +02:00
|
|
|
die();
|
2021-04-07 00:55:53 +02:00
|
|
|
} else if (count($gitLog) === 0) {
|
|
|
|
_exit("Already up to date.");
|
|
|
|
}
|
|
|
|
|
|
|
|
printLine("Found updates, checking repository state");
|
|
|
|
printLine("$ git diff-index --quiet HEAD --"); // check for any uncommitted changes
|
|
|
|
exec("git diff-index --quiet HEAD --", $gitDiff, $ret);
|
|
|
|
if ($ret !== 0) {
|
|
|
|
_exit("You have uncommitted changes. Please commit them before updating.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// enable maintenance mode if it wasn't turned on before
|
|
|
|
if (!$isMaintenanceEnabled) {
|
|
|
|
printLine("Turning on maintenance mode");
|
2021-04-07 01:00:52 +02:00
|
|
|
$file = fopen($maintenanceFile, 'w') or _exit("Unable to create maintenance file");
|
|
|
|
fclose($file);
|
2021-04-07 00:55:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
printLine("Ready to update, pulling and merging");
|
2021-04-07 01:00:52 +02:00
|
|
|
printLine("$ git pull " . str_replace("/", " ", $pullBranch) . " --no-ff");
|
|
|
|
exec("git pull " . str_replace("/", " ", $pullBranch) . " --no-ff", $gitPull, $ret);
|
2021-04-07 00:55:53 +02:00
|
|
|
if ($ret !== 0) {
|
2021-04-07 01:00:52 +02:00
|
|
|
printLine();
|
2021-04-07 12:57:00 +02:00
|
|
|
printLine("Update could not be applied, check the git output.");
|
2021-04-07 00:55:53 +02:00
|
|
|
printLine("Follow the instructions and afterwards turn off the maintenance mode again using:");
|
|
|
|
printLine("cli.php maintenance off");
|
2021-04-09 12:37:24 +02:00
|
|
|
printLine("Also don't forget to apply new database patches using: cli.php db migrate");
|
2021-04-07 01:00:52 +02:00
|
|
|
die();
|
2021-04-07 00:55:53 +02:00
|
|
|
}
|
|
|
|
|
2022-11-19 01:15:34 +01:00
|
|
|
// TODO: also collect patches from Site/Configuration/Patch ... and what about database entities?
|
|
|
|
$newPatchFiles = glob('Core/Configuration/Patch/*.php');
|
2021-04-09 12:37:24 +02:00
|
|
|
$newPatchFiles = array_diff($newPatchFiles, $oldPatchFiles);
|
|
|
|
if (count($newPatchFiles) > 0) {
|
|
|
|
printLine("Applying new database patches");
|
2022-06-20 19:52:31 +02:00
|
|
|
$sql = connectSQL();
|
|
|
|
if ($sql) {
|
2021-04-09 12:37:24 +02:00
|
|
|
foreach ($newPatchFiles as $patchFile) {
|
2022-11-19 01:15:34 +01:00
|
|
|
if (preg_match("/Core\/Configuration\/(Patch\/.*)\.class\.php/", $patchFile, $match)) {
|
2021-04-09 12:37:24 +02:00
|
|
|
$patchName = $match[1];
|
|
|
|
applyPatch($sql, $patchName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 00:55:53 +02:00
|
|
|
// disable maintenance mode again
|
|
|
|
if (!$isMaintenanceEnabled) {
|
|
|
|
printLine("Turning off maintenance mode");
|
2021-04-07 01:00:52 +02:00
|
|
|
if (file_exists($maintenanceFile)) {
|
|
|
|
if (!unlink($maintenanceFile)) {
|
|
|
|
_exit("Unable to delete maintenance file");
|
|
|
|
}
|
|
|
|
}
|
2021-04-07 00:55:53 +02:00
|
|
|
}
|
2021-04-07 00:03:14 +02:00
|
|
|
} else {
|
2021-04-07 00:55:53 +02:00
|
|
|
_exit("Usage: cli.php maintenance <status|on|off|update>");
|
2021-04-07 00:03:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
function getConsoleWidth(): int {
|
|
|
|
$width = getenv('COLUMNS');
|
|
|
|
if (!$width) {
|
|
|
|
$width = exec('tput cols');
|
|
|
|
if (!$width) {
|
|
|
|
$width = 80; // default gnome-terminal column count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return intval($width);
|
|
|
|
}
|
|
|
|
|
2021-04-07 00:03:14 +02:00
|
|
|
function printTable(array $head, array $body) {
|
|
|
|
|
|
|
|
$columns = [];
|
|
|
|
foreach ($head as $key) {
|
|
|
|
$columns[$key] = strlen($key);
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
$maxWidth = getConsoleWidth();
|
2021-04-07 00:03:14 +02:00
|
|
|
foreach ($body as $row) {
|
|
|
|
foreach ($head as $key) {
|
|
|
|
$value = $row[$key] ?? "";
|
|
|
|
$length = strlen($value);
|
|
|
|
$columns[$key] = max($columns[$key], $length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// print table
|
|
|
|
foreach ($head as $key) {
|
|
|
|
echo str_pad($key, $columns[$key]) . ' ';
|
|
|
|
}
|
|
|
|
printLine();
|
|
|
|
|
|
|
|
foreach ($body as $row) {
|
2021-12-08 16:53:43 +01:00
|
|
|
$line = 0;
|
2021-04-07 00:03:14 +02:00
|
|
|
foreach ($head as $key) {
|
2021-12-08 16:53:43 +01:00
|
|
|
$width = min(max($maxWidth - $line, 0), $columns[$key]);
|
|
|
|
$line += $width;
|
|
|
|
echo str_pad($row[$key] ?? "", $width) . ' ';
|
2021-04-07 00:03:14 +02:00
|
|
|
}
|
|
|
|
printLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
function onSettings(array $argv) {
|
2022-06-20 19:52:31 +02:00
|
|
|
global $context;
|
2022-11-19 01:15:34 +01:00
|
|
|
connectSQL() or die();
|
2021-12-08 16:53:43 +01:00
|
|
|
$action = $argv[2] ?? "list";
|
|
|
|
|
|
|
|
if ($action === "list" || $action === "get") {
|
|
|
|
$key = (($action === "list" || count($argv) < 4) ? null : $argv[3]);
|
2022-11-18 18:06:46 +01:00
|
|
|
$req = new \Core\API\Settings\Get($context);
|
2021-12-08 16:53:43 +01:00
|
|
|
$success = $req->execute(["key" => $key]);
|
|
|
|
if (!$success) {
|
|
|
|
_exit("Error listings settings: " . $req->getLastError());
|
|
|
|
} else {
|
|
|
|
$settings = [];
|
|
|
|
foreach ($req->getResult()["settings"] as $key => $value) {
|
|
|
|
$settings[] = ["key" => $key, "value" => $value];
|
|
|
|
}
|
|
|
|
printTable(["key", "value"], $settings);
|
|
|
|
}
|
|
|
|
} else if ($action === "set" || $action === "update") {
|
|
|
|
if (count($argv) < 5) {
|
|
|
|
_exit("Usage: $argv[0] settings $argv[2] <key> <value>");
|
|
|
|
} else {
|
|
|
|
$key = $argv[3];
|
|
|
|
$value = $argv[4];
|
2022-11-18 18:06:46 +01:00
|
|
|
$req = new \Core\API\Settings\Set($context);
|
2021-12-08 16:53:43 +01:00
|
|
|
$success = $req->execute(["settings" => [$key => $value]]);
|
|
|
|
if (!$success) {
|
|
|
|
_exit("Error updating settings: " . $req->getLastError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ($action === "unset" || $action === "delete") {
|
|
|
|
if (count($argv) < 4) {
|
|
|
|
_exit("Usage: $argv[0] settings $argv[2] <key>");
|
|
|
|
} else {
|
|
|
|
$key = $argv[3];
|
2022-11-18 18:06:46 +01:00
|
|
|
$req = new \Core\API\Settings\Set($context);
|
2021-12-08 16:53:43 +01:00
|
|
|
$success = $req->execute(["settings" => [$key => null]]);
|
|
|
|
if (!$success) {
|
|
|
|
_exit("Error updating settings: " . $req->getLastError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_exit("Usage: $argv[0] settings <get|set|unset>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 00:03:14 +02:00
|
|
|
function onRoutes(array $argv) {
|
2022-06-20 19:52:31 +02:00
|
|
|
global $context;
|
2022-11-19 01:15:34 +01:00
|
|
|
connectSQL() or die();
|
2021-04-07 00:03:14 +02:00
|
|
|
$action = $argv[2] ?? "list";
|
|
|
|
|
|
|
|
if ($action === "list") {
|
2022-11-18 18:06:46 +01:00
|
|
|
$req = new \Core\API\Routes\Fetch($context);
|
2021-04-07 00:03:14 +02:00
|
|
|
$success = $req->execute();
|
|
|
|
if (!$success) {
|
|
|
|
_exit("Error fetching routes: " . $req->getLastError());
|
|
|
|
} else {
|
|
|
|
$routes = $req->getResult()["routes"];
|
2022-12-01 11:01:49 +01:00
|
|
|
$head = ["id", "pattern", "type", "target", "extra", "active", "exact"];
|
2021-04-07 00:03:14 +02:00
|
|
|
|
|
|
|
// strict boolean
|
|
|
|
foreach ($routes as &$route) {
|
|
|
|
$route["active"] = $route["active"] ? "true" : "false";
|
2022-06-01 09:47:31 +02:00
|
|
|
$route["exact"] = $route["exact"] ? "true" : "false";
|
2021-04-07 00:03:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
printTable($head, $routes);
|
|
|
|
}
|
|
|
|
} else if ($action === "add") {
|
2022-12-01 11:01:49 +01:00
|
|
|
if (count($argv) < 7) {
|
|
|
|
_exit("Usage: cli.php routes add <pattern> <type> <target> <exact> [extra]");
|
2021-04-07 00:03:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$params = array(
|
2022-12-01 11:01:49 +01:00
|
|
|
"pattern" => $argv[3],
|
|
|
|
"type" => $argv[4],
|
2021-04-07 00:03:14 +02:00
|
|
|
"target" => $argv[5],
|
2022-12-01 11:01:49 +01:00
|
|
|
"exact" => $argv[6],
|
2022-06-01 09:47:31 +02:00
|
|
|
"extra" => $argv[7] ?? "",
|
2021-04-07 00:03:14 +02:00
|
|
|
);
|
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
$req = new \Core\API\Routes\Add($context);
|
2021-04-07 00:03:14 +02:00
|
|
|
$success = $req->execute($params);
|
|
|
|
if (!$success) {
|
|
|
|
_exit($req->getLastError());
|
|
|
|
} else {
|
|
|
|
_exit("Route added successfully");
|
2021-04-07 12:57:00 +02:00
|
|
|
}
|
2021-04-07 00:03:14 +02:00
|
|
|
} else if (in_array($action, ["remove","modify","enable","disable"])) {
|
2022-12-01 11:01:49 +01:00
|
|
|
$routeId = $argv[3] ?? null;
|
|
|
|
if ($routeId === null || ($action === "modify" && count($argv) < 8)) {
|
2021-04-07 00:03:14 +02:00
|
|
|
if ($action === "modify") {
|
2022-12-01 11:01:49 +01:00
|
|
|
_exit("Usage: cli.php routes $action <id> <pattern> <type> <target> <exact> [extra]");
|
2021-04-07 00:03:14 +02:00
|
|
|
} else {
|
2022-06-20 19:52:31 +02:00
|
|
|
_exit("Usage: cli.php routes $action <id>");
|
2021-04-07 00:03:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 11:01:49 +01:00
|
|
|
$params = ["id" => $routeId];
|
2021-04-07 00:03:14 +02:00
|
|
|
if ($action === "remove") {
|
|
|
|
$input = null;
|
|
|
|
do {
|
|
|
|
if ($input === "n") {
|
|
|
|
die();
|
|
|
|
}
|
2022-12-01 11:01:49 +01:00
|
|
|
echo "Remove route #$routeId? (y|n): ";
|
2021-04-07 00:03:14 +02:00
|
|
|
} while(($input = trim(fgets(STDIN))) !== "y");
|
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
$req = new \Core\API\Routes\Remove($context);
|
2021-04-07 00:03:14 +02:00
|
|
|
} else if ($action === "enable") {
|
2022-11-18 18:06:46 +01:00
|
|
|
$req = new \Core\API\Routes\Enable($context);
|
2021-04-07 00:03:14 +02:00
|
|
|
} else if ($action === "disable") {
|
2022-11-18 18:06:46 +01:00
|
|
|
$req = new \Core\API\Routes\Disable($context);
|
2021-04-07 00:03:14 +02:00
|
|
|
} else if ($action === "modify") {
|
2022-11-18 18:06:46 +01:00
|
|
|
$req = new \Core\API\Routes\Update($context);
|
2022-12-01 11:01:49 +01:00
|
|
|
$params["pattern"] = $argv[4];
|
|
|
|
$params["type"] = $argv[5];
|
2021-04-07 00:03:14 +02:00
|
|
|
$params["target"] = $argv[6];
|
2022-12-01 11:01:49 +01:00
|
|
|
$params["exact"] = $argv[7];
|
|
|
|
$params["extra"] = $argv[8] ?? "";
|
2021-04-07 00:03:14 +02:00
|
|
|
} else {
|
|
|
|
_exit("Unsupported action");
|
|
|
|
}
|
|
|
|
|
|
|
|
$success = $req->execute($params);
|
|
|
|
if (!$success) {
|
|
|
|
_exit($req->getLastError());
|
|
|
|
} else {
|
|
|
|
_exit("Route updated successfully");
|
|
|
|
}
|
2021-04-06 23:05:02 +02:00
|
|
|
} else {
|
2021-04-07 00:03:14 +02:00
|
|
|
_exit("Usage: cli.php routes <list|enable|disable|add|remove|modify> [options...]");
|
2021-04-06 16:34:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 12:37:24 +02:00
|
|
|
function onTest($argv) {
|
2021-12-08 16:53:43 +01:00
|
|
|
$files = glob(WEBROOT . '/test/*.test.php');
|
|
|
|
$requestedTests = array_filter(array_slice($argv, 2), function ($t) {
|
|
|
|
return !startsWith($t, "-");
|
|
|
|
});
|
2022-05-31 16:14:49 +02:00
|
|
|
$verbose = in_array("-v", $argv);
|
2021-12-08 16:53:43 +01:00
|
|
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
include_once $file;
|
|
|
|
$baseName = substr(basename($file), 0, - strlen(".test.php"));
|
|
|
|
if (!empty($requestedTests) && !in_array($baseName, $requestedTests)) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-04-09 12:37:24 +02:00
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
$className = $baseName . "Test";
|
|
|
|
if (class_exists($className)) {
|
|
|
|
echo "=== Running $className ===" . PHP_EOL;
|
|
|
|
$testClass = new \PHPUnit\Framework\TestSuite();
|
|
|
|
$testClass->addTestSuite($className);
|
|
|
|
$result = $testClass->run();
|
|
|
|
echo "Done after " . $result->time() . "s" . PHP_EOL;
|
|
|
|
$stats = [
|
|
|
|
"total" => $result->count(),
|
|
|
|
"skipped" => $result->skippedCount(),
|
|
|
|
"error" => $result->errorCount(),
|
|
|
|
"failure" => $result->failureCount(),
|
|
|
|
"warning" => $result->warningCount(),
|
|
|
|
];
|
|
|
|
|
|
|
|
// Summary
|
|
|
|
echo implode(", ", array_map(function ($key) use ($stats) {
|
|
|
|
return "$key: " . $stats[$key];
|
|
|
|
}, array_keys($stats))) . PHP_EOL;
|
|
|
|
|
|
|
|
$reports = array_merge($result->errors(), $result->failures());
|
|
|
|
foreach ($reports as $error) {
|
|
|
|
$exception = $error->thrownException();
|
|
|
|
echo $error->toString();
|
|
|
|
if ($verbose) {
|
|
|
|
echo ". Stacktrace:" . PHP_EOL . $exception->getTraceAsString() . PHP_EOL;
|
|
|
|
} else {
|
|
|
|
$location = array_filter($exception->getTrace(), function ($t) use ($file) {
|
|
|
|
return isset($t["file"]) && $t["file"] === $file;
|
|
|
|
});
|
|
|
|
$location = array_reverse($location);
|
|
|
|
$location = array_pop($location);
|
|
|
|
if ($location) {
|
|
|
|
echo " in " . substr($location["file"], strlen(WEBROOT)) . "#" . $location["line"] . PHP_EOL;
|
|
|
|
} else {
|
|
|
|
echo PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-09 12:37:24 +02:00
|
|
|
}
|
|
|
|
|
2021-04-09 16:05:36 +02:00
|
|
|
function onMail($argv) {
|
2022-06-20 19:52:31 +02:00
|
|
|
global $context;
|
2021-04-09 16:05:36 +02:00
|
|
|
$action = $argv[2] ?? null;
|
2022-11-18 18:06:46 +01:00
|
|
|
if ($action === "send_queue") {
|
2022-11-19 01:15:34 +01:00
|
|
|
connectSQL() or die();
|
2022-11-18 18:06:46 +01:00
|
|
|
$req = new \Core\API\Mail\SendQueue($context);
|
2022-02-20 16:53:26 +01:00
|
|
|
$debug = in_array("debug", $argv);
|
|
|
|
if (!$req->execute(["debug" => $debug])) {
|
|
|
|
_exit("Error processing mail queue: " . $req->getLastError());
|
|
|
|
}
|
2021-04-09 16:05:36 +02:00
|
|
|
} else {
|
2022-11-18 18:06:46 +01:00
|
|
|
_exit("Usage: cli.php mail <send_queue> [options...]");
|
2021-04-09 16:05:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
function onImpersonate($argv) {
|
2022-06-20 19:52:31 +02:00
|
|
|
global $context;
|
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
if (count($argv) < 3) {
|
2022-02-21 14:04:49 +01:00
|
|
|
_exit("Usage: cli.php impersonate <user_id|user_name>");
|
2022-02-20 16:53:26 +01:00
|
|
|
}
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
$sql = connectSQL() or die();
|
2022-02-21 14:04:49 +01:00
|
|
|
|
|
|
|
$userId = $argv[2];
|
|
|
|
if (!is_numeric($userId)) {
|
2022-06-20 19:52:31 +02:00
|
|
|
$res = $sql->select("id")
|
2022-02-21 14:04:49 +01:00
|
|
|
->from("User")
|
2022-11-26 23:57:28 +01:00
|
|
|
->whereEq("name", $userId)
|
2022-02-21 14:04:49 +01:00
|
|
|
->execute();
|
|
|
|
if ($res === false) {
|
|
|
|
_exit("SQL error: " . $sql->getLastError());
|
|
|
|
} else {
|
2022-06-20 19:52:31 +02:00
|
|
|
$userId = $res[0]["id"];
|
2022-02-21 14:04:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
$user = new \Core\Objects\DatabaseEntity\User($userId);
|
|
|
|
$session = new \Core\Objects\DatabaseEntity\Session($context, $user);
|
2022-02-20 16:53:26 +01:00
|
|
|
$session->setData(["2faAuthenticated" => true]);
|
2022-06-20 19:52:31 +02:00
|
|
|
$session->update();
|
2022-02-20 16:53:26 +01:00
|
|
|
echo "session=" . $session->getCookie() . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
2021-04-06 16:34:12 +02:00
|
|
|
$argv = $_SERVER['argv'];
|
|
|
|
if (count($argv) < 2) {
|
2022-02-20 16:53:26 +01:00
|
|
|
_exit("Usage: cli.php <db|routes|settings|maintenance|impersonate> [options...]");
|
2021-04-06 16:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$command = $argv[1];
|
|
|
|
switch ($command) {
|
|
|
|
case 'help':
|
|
|
|
printHelp();
|
|
|
|
exit;
|
|
|
|
case 'db':
|
|
|
|
handleDatabase($argv);
|
|
|
|
break;
|
|
|
|
case 'routes':
|
2021-04-07 00:03:14 +02:00
|
|
|
onRoutes($argv);
|
2021-04-06 23:05:02 +02:00
|
|
|
break;
|
|
|
|
case 'maintenance':
|
|
|
|
onMaintenance($argv);
|
2021-04-06 16:34:12 +02:00
|
|
|
break;
|
2021-04-09 12:37:24 +02:00
|
|
|
case 'test':
|
|
|
|
onTest($argv);
|
|
|
|
break;
|
2021-04-09 16:05:36 +02:00
|
|
|
case 'mail':
|
|
|
|
onMail($argv);
|
|
|
|
break;
|
2021-12-08 16:53:43 +01:00
|
|
|
case 'settings':
|
|
|
|
onSettings($argv);
|
|
|
|
break;
|
2022-02-20 16:53:26 +01:00
|
|
|
case 'impersonate':
|
|
|
|
onImpersonate($argv);
|
|
|
|
break;
|
2021-04-06 16:34:12 +02:00
|
|
|
default:
|
2021-04-07 00:03:14 +02:00
|
|
|
printLine("Unknown command '$command'");
|
|
|
|
printLine();
|
2021-04-06 16:34:12 +02:00
|
|
|
printHelp();
|
|
|
|
exit;
|
|
|
|
}
|