Routes CLI
This commit is contained in:
parent
1a3c4e44f6
commit
ae99ef8a09
15
cli.php
15
cli.php
@ -223,7 +223,7 @@ function onMaintenance(array $argv) {
|
|||||||
exec("git pull " . str_replace("/", " ", $pullBranch) . " --no-ff", $gitPull, $ret);
|
exec("git pull " . str_replace("/", " ", $pullBranch) . " --no-ff", $gitPull, $ret);
|
||||||
if ($ret !== 0) {
|
if ($ret !== 0) {
|
||||||
printLine();
|
printLine();
|
||||||
printLine("Update could not be applied, check the following git message.");
|
printLine("Update could not be applied, check the git output.");
|
||||||
printLine("Follow the instructions and afterwards turn off the maintenance mode again using:");
|
printLine("Follow the instructions and afterwards turn off the maintenance mode again using:");
|
||||||
printLine("cli.php maintenance off");
|
printLine("cli.php maintenance off");
|
||||||
die();
|
die();
|
||||||
@ -306,14 +306,13 @@ function onRoutes(array $argv) {
|
|||||||
"extra" => $argv[6] ?? ""
|
"extra" => $argv[6] ?? ""
|
||||||
);
|
);
|
||||||
|
|
||||||
/*
|
|
||||||
$req = new Api\Routes\Add($user);
|
$req = new Api\Routes\Add($user);
|
||||||
$success = $req->execute($params);
|
$success = $req->execute($params);
|
||||||
if (!$success) {
|
if (!$success) {
|
||||||
_exit($req->getLastError());
|
_exit($req->getLastError());
|
||||||
} else {
|
} else {
|
||||||
_exit("Route added successfully");
|
_exit("Route added successfully");
|
||||||
}*/
|
}
|
||||||
} else if (in_array($action, ["remove","modify","enable","disable"])) {
|
} else if (in_array($action, ["remove","modify","enable","disable"])) {
|
||||||
$uid = $argv[3] ?? null;
|
$uid = $argv[3] ?? null;
|
||||||
if ($uid === null || ($action === "modify" && count($argv) < 7)) {
|
if ($uid === null || ($action === "modify" && count($argv) < 7)) {
|
||||||
@ -334,13 +333,13 @@ function onRoutes(array $argv) {
|
|||||||
echo "Remove route #$uid? (y|n): ";
|
echo "Remove route #$uid? (y|n): ";
|
||||||
} while(($input = trim(fgets(STDIN))) !== "y");
|
} while(($input = trim(fgets(STDIN))) !== "y");
|
||||||
|
|
||||||
// $req = new Api\Routes\Remove($user);
|
$req = new Api\Routes\Remove($user);
|
||||||
} else if ($action === "enable") {
|
} else if ($action === "enable") {
|
||||||
// $req = new Api\Routes\Enable($user);
|
$req = new Api\Routes\Enable($user);
|
||||||
} else if ($action === "disable") {
|
} else if ($action === "disable") {
|
||||||
// $req = new Api\Routes\Disable($user);
|
$req = new Api\Routes\Disable($user);
|
||||||
} else if ($action === "modify") {
|
} else if ($action === "modify") {
|
||||||
// $req = new Api\Routes\Update($user);
|
$req = new Api\Routes\Update($user);
|
||||||
$params["request"] = $argv[4];
|
$params["request"] = $argv[4];
|
||||||
$params["action"] = $argv[5];
|
$params["action"] = $argv[5];
|
||||||
$params["target"] = $argv[6];
|
$params["target"] = $argv[6];
|
||||||
@ -349,14 +348,12 @@ function onRoutes(array $argv) {
|
|||||||
_exit("Unsupported action");
|
_exit("Unsupported action");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
$success = $req->execute($params);
|
$success = $req->execute($params);
|
||||||
if (!$success) {
|
if (!$success) {
|
||||||
_exit($req->getLastError());
|
_exit($req->getLastError());
|
||||||
} else {
|
} else {
|
||||||
_exit("Route updated successfully");
|
_exit("Route updated successfully");
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
} else {
|
} else {
|
||||||
_exit("Usage: cli.php routes <list|enable|disable|add|remove|modify> [options...]");
|
_exit("Usage: cli.php routes <list|enable|disable|add|remove|modify> [options...]");
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Api {
|
namespace Api {
|
||||||
|
|
||||||
|
use Driver\SQL\Condition\Compare;
|
||||||
|
|
||||||
abstract class RoutesAPI extends Request {
|
abstract class RoutesAPI extends Request {
|
||||||
|
|
||||||
|
const ACTIONS = array("redirect_temporary", "redirect_permanently", "static", "dynamic");
|
||||||
|
|
||||||
protected function formatRegex(string $input, bool $append) : string {
|
protected function formatRegex(string $input, bool $append) : string {
|
||||||
$start = startsWith($input, "^");
|
$start = startsWith($input, "^");
|
||||||
$end = endsWith($input, "$");
|
$end = endsWith($input, "$");
|
||||||
@ -16,6 +21,39 @@ namespace Api {
|
|||||||
|
|
||||||
return $input;
|
return $input;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function routeExists($uid): bool {
|
||||||
|
$sql = $this->user->getSQL();
|
||||||
|
$res = $sql->select($sql->count())
|
||||||
|
->from("Route")
|
||||||
|
->where(new Compare("uid", $uid))
|
||||||
|
->execute();
|
||||||
|
|
||||||
|
$this->success = ($res !== false);
|
||||||
|
$this->lastError = $sql->getLastError();
|
||||||
|
if ($this->success) {
|
||||||
|
if ($res[0]["count"] === 0) {
|
||||||
|
return $this->createError("Route not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->success;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function toggleRoute($uid, $active): bool {
|
||||||
|
if (!$this->routeExists($uid)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$sql = $this->user->getSQL();
|
||||||
|
$this->success = $sql->update("Route")
|
||||||
|
->set("active", $active)
|
||||||
|
->where(new Compare("uid", $uid))
|
||||||
|
->execute();
|
||||||
|
|
||||||
|
$this->lastError = $sql->getLastError();
|
||||||
|
return $this->success;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,8 +63,10 @@ namespace Api\Routes {
|
|||||||
use Api\Parameter\StringType;
|
use Api\Parameter\StringType;
|
||||||
use Api\RoutesAPI;
|
use Api\RoutesAPI;
|
||||||
use Driver\SQL\Column\Column;
|
use Driver\SQL\Column\Column;
|
||||||
|
use Driver\SQL\Condition\Compare;
|
||||||
use Driver\SQL\Condition\CondBool;
|
use Driver\SQL\Condition\CondBool;
|
||||||
use Driver\SQL\Condition\CondRegex;
|
use Driver\SQL\Condition\CondRegex;
|
||||||
|
use Objects\User;
|
||||||
|
|
||||||
class Fetch extends RoutesAPI {
|
class Fetch extends RoutesAPI {
|
||||||
|
|
||||||
@ -162,7 +202,7 @@ namespace Api\Routes {
|
|||||||
return $this->success;
|
return $this->success;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function validateRoutes() {
|
private function validateRoutes(): bool {
|
||||||
|
|
||||||
$this->routes = array();
|
$this->routes = array();
|
||||||
$keys = array(
|
$keys = array(
|
||||||
@ -173,10 +213,6 @@ namespace Api\Routes {
|
|||||||
"active" => Parameter::TYPE_BOOLEAN
|
"active" => Parameter::TYPE_BOOLEAN
|
||||||
);
|
);
|
||||||
|
|
||||||
$actions = array(
|
|
||||||
"redirect_temporary", "redirect_permanently", "static", "dynamic"
|
|
||||||
);
|
|
||||||
|
|
||||||
foreach($this->getParam("routes") as $index => $route) {
|
foreach($this->getParam("routes") as $index => $route) {
|
||||||
foreach($keys as $key => $expectedType) {
|
foreach($keys as $key => $expectedType) {
|
||||||
if (!array_key_exists($key, $route)) {
|
if (!array_key_exists($key, $route)) {
|
||||||
@ -193,7 +229,7 @@ namespace Api\Routes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$action = $route["action"];
|
$action = $route["action"];
|
||||||
if (!in_array($action, $actions)) {
|
if (!in_array($action, self::ACTIONS)) {
|
||||||
return $this->createError("Invalid action: $action");
|
return $this->createError("Invalid action: $action");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -213,5 +249,149 @@ namespace Api\Routes {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Add extends RoutesAPI {
|
||||||
|
|
||||||
|
public function __construct(User $user, bool $externalCall = false) {
|
||||||
|
parent::__construct($user, $externalCall, array(
|
||||||
|
"request" => new StringType("request", 128),
|
||||||
|
"action" => new StringType("action"),
|
||||||
|
"target" => new StringType("target", 128),
|
||||||
|
"extra" => new StringType("extra", 64, true, ""),
|
||||||
|
));
|
||||||
|
$this->isPublic = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function execute($values = array()): bool {
|
||||||
|
if (!parent::execute($values)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$request = $this->formatRegex($this->getParam("request"), true);
|
||||||
|
$action = $this->getParam("action");
|
||||||
|
$target = $this->getParam("target");
|
||||||
|
$extra = $this->getParam("extra");
|
||||||
|
|
||||||
|
if (!in_array($action, self::ACTIONS)) {
|
||||||
|
return $this->createError("Invalid action: $action");
|
||||||
|
}
|
||||||
|
|
||||||
|
$sql = $this->user->getSQL();
|
||||||
|
$this->success = $sql->insert("Route", ["request", "action", "target", "extra"])
|
||||||
|
->addRow($request, $action, $target, $extra)
|
||||||
|
->execute();
|
||||||
|
|
||||||
|
$this->lastError = $sql->getLastError();
|
||||||
|
return $this->success;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Update extends RoutesAPI {
|
||||||
|
public function __construct(User $user, bool $externalCall = false) {
|
||||||
|
parent::__construct($user, $externalCall, array(
|
||||||
|
"uid" => new Parameter("uid", Parameter::TYPE_INT),
|
||||||
|
"request" => new StringType("request", 128),
|
||||||
|
"action" => new StringType("action"),
|
||||||
|
"target" => new StringType("target", 128),
|
||||||
|
"extra" => new StringType("extra", 64, true, ""),
|
||||||
|
));
|
||||||
|
$this->isPublic = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function execute($values = array()): bool {
|
||||||
|
if (!parent::execute($values)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$uid = $this->getParam("uid");
|
||||||
|
if (!$this->routeExists($uid)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$request = $this->formatRegex($this->getParam("request"), true);
|
||||||
|
$action = $this->getParam("action");
|
||||||
|
$target = $this->getParam("target");
|
||||||
|
$extra = $this->getParam("extra");
|
||||||
|
if (!in_array($action, self::ACTIONS)) {
|
||||||
|
return $this->createError("Invalid action: $action");
|
||||||
|
}
|
||||||
|
|
||||||
|
$sql = $this->user->getSQL();
|
||||||
|
$this->success = $sql->update("Route")
|
||||||
|
->set("request", $request)
|
||||||
|
->set("action", $action)
|
||||||
|
->set("target", $target)
|
||||||
|
->set("extra", $extra)
|
||||||
|
->where(new Compare("uid", $uid))
|
||||||
|
->execute();
|
||||||
|
|
||||||
|
$this->lastError = $sql->getLastError();
|
||||||
|
return $this->success;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Remove extends RoutesAPI {
|
||||||
|
public function __construct(User $user, bool $externalCall = false) {
|
||||||
|
parent::__construct($user, $externalCall, array(
|
||||||
|
"uid" => new Parameter("uid", Parameter::TYPE_INT)
|
||||||
|
));
|
||||||
|
$this->isPublic = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function execute($values = array()): bool {
|
||||||
|
if (!parent::execute($values)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$uid = $this->getParam("uid");
|
||||||
|
if (!$this->routeExists($uid)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$sql = $this->user->getSQL();
|
||||||
|
$this->success = $sql->delete("Route")
|
||||||
|
->where(new Compare("uid", $uid))
|
||||||
|
->execute();
|
||||||
|
|
||||||
|
$this->lastError = $sql->getLastError();
|
||||||
|
return $this->success;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Enable extends RoutesAPI {
|
||||||
|
public function __construct(User $user, bool $externalCall = false) {
|
||||||
|
parent::__construct($user, $externalCall, array(
|
||||||
|
"uid" => new Parameter("uid", Parameter::TYPE_INT)
|
||||||
|
));
|
||||||
|
$this->isPublic = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function execute($values = array()): bool {
|
||||||
|
if (!parent::execute($values)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$uid = $this->getParam("uid");
|
||||||
|
return $this->toggleRoute($uid, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Disable extends RoutesAPI {
|
||||||
|
public function __construct(User $user, bool $externalCall = false) {
|
||||||
|
parent::__construct($user, $externalCall, array(
|
||||||
|
"uid" => new Parameter("uid", Parameter::TYPE_INT)
|
||||||
|
));
|
||||||
|
$this->isPublic = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function execute($values = array()): bool {
|
||||||
|
if (!parent::execute($values)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$uid = $this->getParam("uid");
|
||||||
|
return $this->toggleRoute($uid, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user