web-base/Core/API/RoutesAPI.class.php

404 lines
12 KiB
PHP
Raw Normal View History

2020-06-20 20:13:51 +02:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\API {
2021-04-07 12:57:00 +02:00
2022-11-18 18:06:46 +01:00
use Core\API\Routes\GenerateCache;
use Core\Driver\SQL\Condition\Compare;
use Core\Objects\Context;
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-11-18 18:06:46 +01:00
const ROUTER_CACHE_CLASS = "\\Core\\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-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall, array $params) {
parent::__construct($context, $externalCall, $params);
2022-05-31 16:14:49 +02:00
$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 {
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2021-04-07 12:57:00 +02:00
$res = $sql->select($sql->count())
->from("Route")
->whereEq("id", $uid)
2021-04-07 12:57:00 +02:00
->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;
}
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2021-04-07 12:57:00 +02:00
$this->success = $sql->update("Route")
->set("active", $active)
->whereEq("id", $uid)
2021-04-07 12:57:00 +02:00
->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 {
2022-06-20 19:52:31 +02:00
$req = new GenerateCache($this->context);
2022-05-31 16:14:49 +02:00
$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
}
}
2022-11-18 18:06:46 +01:00
namespace Core\API\Routes {
use Core\API\Parameter\Parameter;
use Core\API\Parameter\StringType;
use Core\API\RoutesAPI;
use Core\Driver\SQL\Condition\Compare;
use Core\Driver\SQL\Condition\CondBool;
use Core\Objects\Context;
use Core\Objects\Router\DocumentRoute;
use Core\Objects\Router\RedirectRoute;
use Core\Objects\Router\Router;
use Core\Objects\Router\StaticFileRoute;
2020-06-20 20:13:51 +02:00
class Fetch extends RoutesAPI {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, $externalCall = false) {
parent::__construct($context, $externalCall, array());
2020-06-20 20:13:51 +02:00
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2020-06-20 20:13:51 +02:00
$res = $sql
2022-06-20 19:52:31 +02:00
->select("id", "request", "action", "target", "extra", "active", "exact")
2020-06-20 20:13:51 +02:00
->from("Route")
2022-06-20 19:52:31 +02:00
->orderBy("id")
2022-05-31 16:14:49 +02:00
->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(
2022-06-20 19:52:31 +02:00
"id" => intval($row["id"]),
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;
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, $externalCall = false) {
parent::__construct($context, $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;
}
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2020-06-20 20:13:51 +02:00
// 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 {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false) {
parent::__construct($context, $externalCall, array(
2021-04-07 12:57:00 +02:00
"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");
}
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2021-04-07 12:57:00 +02:00
$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 {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false) {
parent::__construct($context, $externalCall, array(
"id" => new Parameter("id", Parameter::TYPE_INT),
2021-04-07 12:57:00 +02:00
"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-06-20 19:52:31 +02:00
$id = $this->getParam("id");
if (!$this->routeExists($id)) {
2021-04-07 12:57:00 +02:00
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");
}
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2021-04-07 12:57:00 +02:00
$this->success = $sql->update("Route")
->set("request", $request)
->set("action", $action)
->set("target", $target)
->set("extra", $extra)
->whereEq("id", $id)
2021-04-07 12:57:00 +02:00
->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 {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false) {
parent::__construct($context, $externalCall, array(
"id" => new Parameter("id", Parameter::TYPE_INT)
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
$id = $this->getParam("id");
if (!$this->routeExists($id)) {
2021-04-07 12:57:00 +02:00
return false;
}
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2021-04-07 12:57:00 +02:00
$this->success = $sql->delete("Route")
->where("id", $id)
2021-04-07 12:57:00 +02:00
->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 {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false) {
parent::__construct($context, $externalCall, array(
"id" => new Parameter("id", Parameter::TYPE_INT)
2021-04-07 12:57:00 +02:00
));
$this->isPublic = false;
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2022-06-20 19:52:31 +02:00
$uid = $this->getParam("id");
2021-04-07 12:57:00 +02:00
return $this->toggleRoute($uid, true);
}
}
class Disable extends RoutesAPI {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false) {
parent::__construct($context, $externalCall, array(
"id" => new Parameter("id", Parameter::TYPE_INT)
2021-04-07 12:57:00 +02:00
));
$this->isPublic = false;
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2022-06-20 19:52:31 +02:00
$uid = $this->getParam("id");
2021-04-07 12:57:00 +02:00
return $this->toggleRoute($uid, false);
}
}
2022-05-31 16:14:49 +02:00
class GenerateCache extends RoutesAPI {
private ?Router $router;
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false) {
parent::__construct($context, $externalCall, []);
2022-05-31 16:14:49 +02:00
$this->isPublic = false;
$this->router = null;
}
protected function _execute(): bool {
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2022-05-31 16:14:49 +02:00
$res = $sql
2022-06-20 19:52:31 +02:00
->select("id", "request", "action", "target", "extra", "exact")
2022-05-31 16:14:49 +02:00
->from("Route")
->where(new CondBool("active"))
2022-06-20 19:52:31 +02:00
->orderBy("id")->ascending()
2022-05-31 16:14:49 +02:00
->execute();
$this->success = $res !== false;
$this->lastError = $sql->getLastError();
if (!$this->success) {
return false;
}
2022-06-20 19:52:31 +02:00
$this->router = new Router($this->context);
2022-05-31 16:14:49 +02:00
foreach ($res as $row) {
$request = $row["request"];
$target = $row["target"];
$exact = $sql->parseBool($row["exact"]);
switch ($row["action"]) {
case "redirect_temporary":
2022-06-01 09:47:31 +02:00
$this->router->addRoute(new RedirectRoute($request, $exact, $target, 307));
2022-05-31 16:14:49 +02:00
break;
case "redirect_permanently":
2022-06-01 09:47:31 +02:00
$this->router->addRoute(new RedirectRoute($request, $exact, $target, 308));
2022-05-31 16:14:49 +02:00
break;
case "static":
2022-06-01 09:47:31 +02:00
$this->router->addRoute(new StaticFileRoute($request, $exact, $target));
2022-05-31 16:14:49 +02:00
break;
case "dynamic":
$extra = json_decode($row["extra"]) ?? [];
2022-06-01 09:47:31 +02:00
$this->router->addRoute(new DocumentRoute($request, $exact, $target, ...$extra));
2022-05-31 16:14:49 +02:00
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
}