web-base/core/Api/RoutesAPI.class.php

401 lines
12 KiB
PHP
Raw Normal View History

2020-06-20 20:13:51 +02:00
<?php
namespace Api {
2021-04-07 12:57:00 +02:00
2022-05-31 16:14:49 +02:00
use Api\Routes\GenerateCache;
2021-04-07 12:57:00 +02:00
use Driver\SQL\Condition\Compare;
2022-05-31 16:14:49 +02:00
use Objects\User;
2021-04-07 12:57:00 +02:00
2020-06-20 20:13:51 +02:00
abstract class RoutesAPI extends Request {
2020-06-21 22:36:50 +02:00
2021-04-07 12:57:00 +02:00
const ACTIONS = array("redirect_temporary", "redirect_permanently", "static", "dynamic");
2022-05-31 16:14:49 +02:00
const ROUTER_CACHE_CLASS = "\\Cache\\RouterCache";
2021-04-07 12:57:00 +02:00
2022-05-31 16:14:49 +02:00
protected string $routerCachePath;
2020-06-21 22:36:50 +02:00
2022-05-31 16:14:49 +02:00
public function __construct(User $user, bool $externalCall, array $params) {
parent::__construct($user, $externalCall, $params);
$this->routerCachePath = getClassPath(self::ROUTER_CACHE_CLASS);
2020-06-21 22:36:50 +02:00
}
2021-04-07 12:57:00 +02:00
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();
2022-05-31 16:14:49 +02:00
$this->success = $this->success && $this->regenerateCache();
return $this->success;
}
protected function regenerateCache(): bool {
$req = new GenerateCache($this->user);
$this->success = $req->execute();
$this->lastError = $req->getLastError();
2021-04-07 12:57:00 +02:00
return $this->success;
}
2020-06-20 20:13:51 +02:00
}
}
namespace Api\Routes {
use Api\Parameter\Parameter;
use Api\Parameter\StringType;
use Api\RoutesAPI;
2021-04-07 12:57:00 +02:00
use Driver\SQL\Condition\Compare;
2020-06-20 20:13:51 +02:00
use Driver\SQL\Condition\CondBool;
2022-05-31 16:14:49 +02:00
use Objects\Router;
2021-04-07 12:57:00 +02:00
use Objects\User;
2020-06-20 20:13:51 +02:00
class Fetch extends RoutesAPI {
public function __construct($user, $externalCall = false) {
2022-05-31 16:14:49 +02:00
parent::__construct($user, $externalCall, array());
2020-06-20 20:13:51 +02:00
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2020-06-20 20:13:51 +02:00
$sql = $this->user->getSQL();
$res = $sql
2022-05-31 16:14:49 +02:00
->select("uid", "request", "action", "target", "extra", "active", "exact")
2020-06-20 20:13:51 +02:00
->from("Route")
2022-05-31 16:14:49 +02:00
->orderBy("uid")
->ascending()
2020-06-20 20:13:51 +02:00
->execute();
$this->lastError = $sql->getLastError();
$this->success = ($res !== FALSE);
if ($this->success) {
2022-05-31 16:14:49 +02:00
$routes = array();
foreach ($res as $row) {
$routes[] = array(
"uid" => intval($row["uid"]),
2020-06-20 20:13:51 +02:00
"request" => $row["request"],
2022-05-31 16:14:49 +02:00
"action" => $row["action"],
"target" => $row["target"],
"extra" => $row["extra"] ?? "",
"active" => intval($sql->parseBool($row["active"])),
"exact" => intval($sql->parseBool($row["exact"])),
2020-06-20 20:13:51 +02:00
);
}
2022-05-31 16:14:49 +02:00
$this->result["routes"] = $routes;
2020-06-20 20:13:51 +02:00
}
return $this->success;
}
}
class Save extends RoutesAPI {
private array $routes;
public function __construct($user, $externalCall = false) {
parent::__construct($user, $externalCall, array(
2022-05-31 16:14:49 +02:00
'routes' => new Parameter('routes', Parameter::TYPE_ARRAY, false)
2020-06-20 20:13:51 +02:00
));
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2020-06-20 20:13:51 +02:00
if (!$this->validateRoutes()) {
return false;
}
$sql = $this->user->getSQL();
// DELETE old rules
$this->success = ($sql->truncate("Route")->execute() !== FALSE);
$this->lastError = $sql->getLastError();
// INSERT new routes
if ($this->success) {
2022-05-31 16:14:49 +02:00
$stmt = $sql->insert("Route", array("request", "action", "target", "extra", "active", "exact"));
2020-06-25 21:53:33 +02:00
2022-05-31 16:14:49 +02:00
foreach ($this->routes as $route) {
$stmt->addRow($route["request"], $route["action"], $route["target"], $route["extra"], $route["active"], $route["exact"]);
2020-06-20 20:13:51 +02:00
}
$this->success = ($stmt->execute() !== FALSE);
$this->lastError = $sql->getLastError();
}
2022-05-31 16:14:49 +02:00
$this->success = $this->success && $this->regenerateCache();
2020-06-20 20:13:51 +02:00
return $this->success;
}
2021-04-07 12:57:00 +02:00
private function validateRoutes(): bool {
2020-06-20 20:13:51 +02:00
$this->routes = array();
$keys = array(
2022-05-31 16:14:49 +02:00
"request" => [Parameter::TYPE_STRING, Parameter::TYPE_INT],
2020-06-20 20:13:51 +02:00
"action" => Parameter::TYPE_STRING,
"target" => Parameter::TYPE_STRING,
2022-05-31 16:14:49 +02:00
"extra" => Parameter::TYPE_STRING,
"active" => Parameter::TYPE_BOOLEAN,
"exact" => Parameter::TYPE_BOOLEAN,
2020-06-20 20:13:51 +02:00
);
2022-05-31 16:14:49 +02:00
foreach ($this->getParam("routes") as $index => $route) {
foreach ($keys as $key => $expectedType) {
2020-06-20 20:13:51 +02:00
if (!array_key_exists($key, $route)) {
return $this->createError("Route $index missing key: $key");
}
$value = $route[$key];
$type = Parameter::parseType($value);
2022-05-31 16:14:49 +02:00
if (!is_array($expectedType)) {
$expectedType = [$expectedType];
}
if (!in_array($type, $expectedType)) {
if (count($expectedType) > 0) {
$expectedTypeName = "expected: " . Parameter::names[$expectedType];
} else {
$expectedTypeName = "expected one of: " . implode(",", array_map(
function ($type) {
return Parameter::names[$type];
}, $expectedType));
}
2020-06-20 20:13:51 +02:00
$gotTypeName = Parameter::names[$type];
2022-05-31 16:14:49 +02:00
return $this->createError("Route $index has invalid value for key: $key, $expectedTypeName, got: $gotTypeName");
2020-06-20 20:13:51 +02:00
}
}
$action = $route["action"];
2021-04-07 12:57:00 +02:00
if (!in_array($action, self::ACTIONS)) {
2020-06-20 20:13:51 +02:00
return $this->createError("Invalid action: $action");
}
2022-05-31 16:14:49 +02:00
if (empty($route["request"])) {
2020-06-20 20:13:51 +02:00
return $this->createError("Request cannot be empty.");
}
2022-05-31 16:14:49 +02:00
if (empty($route["target"])) {
2020-06-20 20:13:51 +02:00
return $this->createError("Target cannot be empty.");
}
$this->routes[] = $route;
}
return true;
}
}
2021-04-07 12:57:00 +02:00
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),
2022-05-31 16:14:49 +02:00
"extra" => new StringType("extra", 64, true, ""),
2021-04-07 12:57:00 +02:00
));
$this->isPublic = false;
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2021-04-07 12:57:00 +02:00
2022-05-31 16:14:49 +02:00
$request = $this->getParam("request");
2021-04-07 12:57:00 +02:00
$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();
2022-05-31 16:14:49 +02:00
$this->success = $this->success && $this->regenerateCache();
2021-04-07 12:57:00 +02:00
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),
2022-05-31 16:14:49 +02:00
"extra" => new StringType("extra", 64, true, ""),
2021-04-07 12:57:00 +02:00
));
$this->isPublic = false;
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2021-04-07 12:57:00 +02:00
$uid = $this->getParam("uid");
if (!$this->routeExists($uid)) {
return false;
}
2022-05-31 16:14:49 +02:00
$request = $this->getParam("request");
2021-04-07 12:57:00 +02:00
$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();
2022-05-31 16:14:49 +02:00
$this->success = $this->success && $this->regenerateCache();
2021-04-07 12:57:00 +02:00
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;
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2021-04-07 12:57:00 +02:00
$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();
2022-05-31 16:14:49 +02:00
$this->success = $this->success && $this->regenerateCache();
2021-04-07 12:57:00 +02:00
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;
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2021-04-07 12:57:00 +02:00
$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;
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2021-04-07 12:57:00 +02:00
$uid = $this->getParam("uid");
return $this->toggleRoute($uid, false);
}
}
2022-05-31 16:14:49 +02:00
class GenerateCache extends RoutesAPI {
private ?Router $router;
public function __construct(User $user, bool $externalCall = false) {
parent::__construct($user, $externalCall, []);
$this->isPublic = false;
$this->router = null;
}
protected function _execute(): bool {
$sql = $this->user->getSQL();
$res = $sql
->select("uid", "request", "action", "target", "extra", "exact")
->from("Route")
->where(new CondBool("active"))
->orderBy("uid")->ascending()
->execute();
$this->success = $res !== false;
$this->lastError = $sql->getLastError();
if (!$this->success) {
return false;
}
$this->router = new Router($this->user);
foreach ($res as $row) {
$request = $row["request"];
$target = $row["target"];
$exact = $sql->parseBool($row["exact"]);
switch ($row["action"]) {
case "redirect_temporary":
$this->router->addRoute(new Router\RedirectRoute($request, $exact, $target, 307));
break;
case "redirect_permanently":
$this->router->addRoute(new Router\RedirectRoute($request, $exact, $target, 308));
break;
case "static":
$this->router->addRoute(new Router\StaticFileRoute($request, $exact, $target));
break;
case "dynamic":
$extra = json_decode($row["extra"]) ?? [];
$this->router->addRoute(new Router\DocumentRoute($request, $exact, $target, ...$extra));
break;
default:
break;
}
}
$this->success = $this->router->writeCache($this->routerCachePath);
if (!$this->success) {
return $this->createError("Error saving router cache file: " . $this->routerCachePath);
}
return $this->success;
}
public function getRouter(): ?Router {
return $this->router;
}
}
2020-06-20 20:13:51 +02:00
}