diff --git a/cli.php b/cli.php index 8c32a7e..73de6f8 100644 --- a/cli.php +++ b/cli.php @@ -223,7 +223,7 @@ function onMaintenance(array $argv) { exec("git pull " . str_replace("/", " ", $pullBranch) . " --no-ff", $gitPull, $ret); if ($ret !== 0) { 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("cli.php maintenance off"); die(); @@ -306,14 +306,13 @@ function onRoutes(array $argv) { "extra" => $argv[6] ?? "" ); - /* $req = new Api\Routes\Add($user); $success = $req->execute($params); if (!$success) { _exit($req->getLastError()); } else { _exit("Route added successfully"); - }*/ + } } else if (in_array($action, ["remove","modify","enable","disable"])) { $uid = $argv[3] ?? null; if ($uid === null || ($action === "modify" && count($argv) < 7)) { @@ -334,13 +333,13 @@ function onRoutes(array $argv) { echo "Remove route #$uid? (y|n): "; } while(($input = trim(fgets(STDIN))) !== "y"); - // $req = new Api\Routes\Remove($user); + $req = new Api\Routes\Remove($user); } else if ($action === "enable") { - // $req = new Api\Routes\Enable($user); + $req = new Api\Routes\Enable($user); } else if ($action === "disable") { - // $req = new Api\Routes\Disable($user); + $req = new Api\Routes\Disable($user); } else if ($action === "modify") { - // $req = new Api\Routes\Update($user); + $req = new Api\Routes\Update($user); $params["request"] = $argv[4]; $params["action"] = $argv[5]; $params["target"] = $argv[6]; @@ -349,14 +348,12 @@ function onRoutes(array $argv) { _exit("Unsupported action"); } - /* $success = $req->execute($params); if (!$success) { _exit($req->getLastError()); } else { _exit("Route updated successfully"); } - */ } else { _exit("Usage: cli.php routes [options...]"); } diff --git a/core/Api/RoutesAPI.class.php b/core/Api/RoutesAPI.class.php index 3247d42..da96438 100644 --- a/core/Api/RoutesAPI.class.php +++ b/core/Api/RoutesAPI.class.php @@ -1,8 +1,13 @@ 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\RoutesAPI; use Driver\SQL\Column\Column; + use Driver\SQL\Condition\Compare; use Driver\SQL\Condition\CondBool; use Driver\SQL\Condition\CondRegex; + use Objects\User; class Fetch extends RoutesAPI { @@ -162,7 +202,7 @@ namespace Api\Routes { return $this->success; } - private function validateRoutes() { + private function validateRoutes(): bool { $this->routes = array(); $keys = array( @@ -173,10 +213,6 @@ namespace Api\Routes { "active" => Parameter::TYPE_BOOLEAN ); - $actions = array( - "redirect_temporary", "redirect_permanently", "static", "dynamic" - ); - foreach($this->getParam("routes") as $index => $route) { foreach($keys as $key => $expectedType) { if (!array_key_exists($key, $route)) { @@ -193,7 +229,7 @@ namespace Api\Routes { } $action = $route["action"]; - if (!in_array($action, $actions)) { + if (!in_array($action, self::ACTIONS)) { return $this->createError("Invalid action: $action"); } @@ -213,5 +249,149 @@ namespace Api\Routes { 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); + } + } }