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

452 lines
14 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\Objects\Context;
2022-11-27 15:58:44 +01:00
use Core\Objects\DatabaseEntity\Route;
2024-03-28 11:56:17 +01:00
use Core\Objects\Router\ApiRoute;
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
2022-12-04 13:21:40 +01:00
const ROUTER_CACHE_CLASS = "\\Site\\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);
2023-01-09 15:59:53 +01:00
$this->routerCachePath = WEBROOT . DIRECTORY_SEPARATOR . getClassPath(self::ROUTER_CACHE_CLASS);
2020-06-21 22:36:50 +02:00
}
2021-04-07 12:57:00 +02:00
2022-11-27 15:58:44 +01:00
protected function toggleRoute(int $id, bool $active): bool {
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2022-11-27 15:58:44 +01:00
$route = Route::find($sql, $id);
if ($route === false) {
2021-04-07 12:57:00 +02:00
return false;
2022-11-27 15:58:44 +01:00
} else if ($route === null) {
return $this->createError("Route not found");
2024-03-28 11:56:17 +01:00
} else if ($route instanceof ApiRoute) {
return $this->createError("This route cannot be modified");
2021-04-07 12:57:00 +02:00
}
2022-11-27 15:58:44 +01:00
$route->setActive($active);
2022-11-29 14:17:11 +01:00
$this->success = $route->save($sql, ["active"]);
2021-04-07 12:57:00 +02:00
$this->lastError = $sql->getLastError();
2022-11-27 15:58:44 +01:00
return $this->success && $this->regenerateCache();
2022-05-31 16:14:49 +02:00
}
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;
}
2022-11-27 15:58:44 +01:00
protected function createRoute(string $type, string $pattern, string $target,
?string $extra, bool $exact, bool $active = true): ?Route {
$routeClass = Route::ROUTE_TYPES[$type] ?? null;
if (!$routeClass) {
$this->createError("Invalid type: $type");
return null;
}
try {
$routeClass = new \ReflectionClass($routeClass);
$routeObj = $routeClass->newInstance($pattern, $exact, $target);
$routeObj->setExtra($extra);
$routeObj->setActive($active);
return $routeObj;
} catch (\ReflectionException $exception) {
$this->createError("Error instantiating route class: " . $exception->getMessage());
return null;
}
}
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;
2023-01-16 21:47:23 +01:00
use Core\Driver\SQL\Query\Insert;
2022-11-18 18:06:46 +01:00
use Core\Objects\Context;
2023-01-16 21:47:23 +01:00
use Core\Objects\DatabaseEntity\Group;
2022-11-27 12:33:27 +01:00
use Core\Objects\DatabaseEntity\Route;
2024-03-28 11:56:17 +01:00
use Core\Objects\Router\ApiRoute;
2022-11-18 18:06:46 +01:00
use Core\Objects\Router\Router;
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
2022-11-27 15:58:44 +01:00
$routes = Route::findAll($sql);
2020-06-20 20:13:51 +02:00
$this->lastError = $sql->getLastError();
2022-11-27 15:58:44 +01:00
$this->success = ($routes !== FALSE);
2020-06-20 20:13:51 +02:00
if ($this->success) {
2024-03-28 11:56:17 +01:00
$this->result["routes"] = $routes;
2020-06-20 20:13:51 +02:00
}
return $this->success;
}
2023-01-16 21:47:23 +01:00
public static function getDefaultACL(Insert $insert): void {
2024-03-27 14:12:01 +01:00
$insert->addRow(self::getEndpoint(), [Group::ADMIN], "Allows users to fetch site routing", true);
2023-01-16 21:47:23 +01:00
}
2020-06-20 20:13:51 +02:00
}
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();
2022-11-27 15:58:44 +01:00
$sql->startTransaction();
2020-06-20 20:13:51 +02:00
2022-11-27 15:58:44 +01:00
// DELETE old rules;
2020-06-20 20:13:51 +02:00
$this->success = ($sql->truncate("Route")->execute() !== FALSE);
$this->lastError = $sql->getLastError();
// INSERT new routes
if ($this->success) {
2022-11-27 15:58:44 +01:00
$insertStatement = Route::getHandler($sql)->getInsertQuery($this->routes);
$this->success = ($insertStatement->execute() !== FALSE);
2020-06-20 20:13:51 +02:00
$this->lastError = $sql->getLastError();
}
2022-11-27 15:58:44 +01:00
if ($this->success) {
$sql->commit();
return $this->regenerateCache();
} else {
$sql->rollback();
return false;
}
2020-06-20 20:13:51 +02:00
}
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-11-27 15:58:44 +01:00
"id" => Parameter::TYPE_INT,
"pattern" => [Parameter::TYPE_STRING, Parameter::TYPE_INT],
"type" => Parameter::TYPE_STRING,
2020-06-20 20:13:51 +02:00
"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)) {
2022-11-27 15:58:44 +01:00
if ($key !== "id") { // id is optional
return $this->createError("Route $index missing key: $key");
} else {
continue;
}
2020-06-20 20:13:51 +02:00
}
$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
}
}
2022-11-27 15:58:44 +01:00
$type = $route["type"];
if (!isset(Route::ROUTE_TYPES[$type])) {
return $this->createError("Invalid type: $type");
2020-06-20 20:13:51 +02:00
}
2022-11-27 15:58:44 +01:00
if (empty($route["pattern"])) {
return $this->createError("Pattern cannot be empty.");
2020-06-20 20:13:51 +02:00
}
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;
}
2023-01-16 21:47:23 +01:00
public static function getDefaultACL(Insert $insert): void {
2024-03-27 14:12:01 +01:00
$insert->addRow(self::getEndpoint(), [Group::ADMIN], "Allows users to save the site routing", true);
2023-01-16 21:47:23 +01:00
}
2020-06-20 20:13:51 +02:00
}
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(
2022-11-27 15:58:44 +01:00
"pattern" => new StringType("pattern", 128),
"type" => new StringType("type"),
2021-04-07 12:57:00 +02:00
"target" => new StringType("target", 128),
2022-05-31 16:14:49 +02:00
"extra" => new StringType("extra", 64, true, ""),
2022-11-27 15:58:44 +01:00
"exact" => new Parameter("exact", Parameter::TYPE_BOOLEAN),
"active" => new Parameter("active", Parameter::TYPE_BOOLEAN, true, 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-11-27 15:58:44 +01:00
$pattern = $this->getParam("pattern");
$type = $this->getParam("type");
2021-04-07 12:57:00 +02:00
$target = $this->getParam("target");
$extra = $this->getParam("extra");
2022-11-27 15:58:44 +01:00
$exact = $this->getParam("exact");
$active = $this->getParam("active");
$route = $this->createRoute($type, $pattern, $target, $extra, $exact, $active);
if ($route === null) {
return false;
2021-04-07 12:57:00 +02:00
}
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2022-11-27 15:58:44 +01:00
$this->success = $route->save($sql) !== false;
2021-04-07 12:57:00 +02:00
$this->lastError = $sql->getLastError();
2022-11-27 15:58:44 +01:00
return $this->success && $this->regenerateCache();
2021-04-07 12:57:00 +02:00
}
2023-01-16 21:47:23 +01:00
public static function getDefaultACL(Insert $insert): void {
2024-03-27 14:12:01 +01:00
$insert->addRow(self::getEndpoint(), [Group::ADMIN], "Allows users to add new routes", true);
2023-01-16 21:47:23 +01:00
}
2021-04-07 12:57:00 +02:00
}
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),
2022-11-27 15:58:44 +01:00
"pattern" => new StringType("pattern", 128),
"type" => new StringType("type"),
2021-04-07 12:57:00 +02:00
"target" => new StringType("target", 128),
2022-05-31 16:14:49 +02:00
"extra" => new StringType("extra", 64, true, ""),
2022-12-01 11:01:49 +01:00
"exact" => new Parameter("exact", Parameter::TYPE_BOOLEAN),
"active" => new Parameter("active", Parameter::TYPE_BOOLEAN, true, 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");
2022-11-27 15:58:44 +01:00
$sql = $this->context->getSQL();
$route = Route::find($sql, $id);
if ($route === false) {
return $this->createError("Error fetching route: " . $sql->getLastError());
} else if ($route === null) {
return $this->createError("Route not found");
2021-04-07 12:57:00 +02:00
}
$target = $this->getParam("target");
$extra = $this->getParam("extra");
2022-11-27 15:58:44 +01:00
$type = $this->getParam("type");
$pattern = $this->getParam("pattern");
$exact = $this->getParam("exact");
$active = $this->getParam("active");
if ($route->getType() !== $type) {
2023-01-16 21:47:23 +01:00
if (!$route->delete($sql)) {
2022-11-27 15:58:44 +01:00
return false;
2023-01-16 21:47:23 +01:00
} else {
$route = $this->createRoute($type, $pattern, $target, $extra, $exact, $active);
if ($route === null) {
return false;
}
2022-11-27 15:58:44 +01:00
}
} else {
$route->setPattern($pattern);
$route->setActive($active);
$route->setExtra($extra);
$route->setTarget($target);
$route->setExact($exact);
2021-04-07 12:57:00 +02:00
}
2022-11-27 15:58:44 +01:00
$this->success = $route->save($sql) !== false;
2021-04-07 12:57:00 +02:00
$this->lastError = $sql->getLastError();
2022-11-27 15:58:44 +01:00
return $this->success && $this->regenerateCache();
2021-04-07 12:57:00 +02:00
}
2023-01-16 21:47:23 +01:00
public static function getDefaultACL(Insert $insert): void {
2024-03-27 14:12:01 +01:00
$insert->addRow(self::getEndpoint(), [Group::ADMIN], "Allows users to update existing routes", true);
2023-01-16 21:47:23 +01:00
}
2021-04-07 12:57:00 +02:00
}
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
));
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2021-04-07 12:57:00 +02:00
2022-11-27 15:58:44 +01:00
$sql = $this->context->getSQL();
$id = $this->getParam("id");
2022-11-27 15:58:44 +01:00
$route = Route::find($sql, $id);
if ($route === false) {
return $this->createError("Error fetching route: " . $sql->getLastError());
} else if ($route === null) {
return $this->createError("Route not found");
2024-03-28 11:56:17 +01:00
} else if ($route instanceof ApiRoute) {
return $this->createError("This route cannot be deleted");
2021-04-07 12:57:00 +02:00
}
2022-11-27 15:58:44 +01:00
$this->success = $route->delete($sql) !== false;
2021-04-07 12:57:00 +02:00
$this->lastError = $sql->getLastError();
2022-11-27 15:58:44 +01:00
return $this->success && $this->regenerateCache();
2021-04-07 12:57:00 +02:00
}
2023-01-16 21:47:23 +01:00
public static function getDefaultACL(Insert $insert): void {
2024-03-27 14:12:01 +01:00
$insert->addRow(self::getEndpoint(), [Group::ADMIN], "Allows users to remove routes", true);
2023-01-16 21:47:23 +01:00
}
2021-04-07 12:57:00 +02:00
}
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
));
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2022-11-27 15:58:44 +01:00
$id = $this->getParam("id");
return $this->toggleRoute($id, true);
2021-04-07 12:57:00 +02:00
}
2023-01-16 21:47:23 +01:00
public static function getDefaultACL(Insert $insert): void {
2024-03-27 14:12:01 +01:00
$insert->addRow(self::getEndpoint(), [Group::ADMIN], "Allows users to enable a route", true);
2023-01-16 21:47:23 +01:00
}
2021-04-07 12:57:00 +02:00
}
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
));
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2022-11-27 15:58:44 +01:00
$id = $this->getParam("id");
return $this->toggleRoute($id, false);
2021-04-07 12:57:00 +02:00
}
2023-01-16 21:47:23 +01:00
public static function getDefaultACL(Insert $insert): void {
2024-03-27 14:12:01 +01:00
$insert->addRow(self::getEndpoint(), [Group::ADMIN], "Allows users to disable a route", true);
2023-01-16 21:47:23 +01:00
}
2021-04-07 12:57:00 +02:00
}
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->router = null;
}
protected function _execute(): bool {
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2022-11-27 12:33:27 +01:00
$routes = Route::findBy(Route::createBuilder($sql, false)
->whereTrue("active")
->orderBy("id")
->ascending());
2022-05-31 16:14:49 +02:00
2022-11-27 12:33:27 +01:00
$this->success = $routes !== false;
2022-05-31 16:14:49 +02:00
$this->lastError = $sql->getLastError();
if (!$this->success) {
return false;
}
2022-06-20 19:52:31 +02:00
$this->router = new Router($this->context);
2022-11-27 12:33:27 +01:00
foreach ($routes as $route) {
$this->router->addRoute($route);
2022-05-31 16:14:49 +02:00
}
$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;
}
2023-01-16 21:47:23 +01:00
public static function getDefaultACL(Insert $insert): void {
2024-03-28 11:56:17 +01:00
$insert->addRow(self::getEndpoint(),
[Group::ADMIN, Group::SUPPORT],
"Allows users to regenerate the routing cache", true
);
2023-01-16 21:47:23 +01:00
}
2022-05-31 16:14:49 +02:00
}
2024-03-27 20:50:57 +01:00
class Check extends RoutesAPI {
public function __construct(Context $context, bool $externalCall) {
parent::__construct($context, $externalCall, [
"id" => new Parameter("id", Parameter::TYPE_INT),
"path" => new StringType("path")
]);
}
protected function _execute(): bool {
$sql = $this->context->getSQL();
$routeId = $this->getParam("id");
$route = Route::find($sql, $routeId);
if ($route === false) {
$this->lastError = $sql->getLastError();
return false;
} else if ($route === null) {
return $this->createError("Route not found");
}
$this->success = true;
$path = $this->getParam("path");
$this->result["match"] = $route->match($path);
return $this->success;
}
public static function getDefaultACL(Insert $insert): void {
$insert->addRow("routes/check",
[Group::ADMIN, Group::MODERATOR],
"Users with this permission can see, if a route is matched with the given path for debugging purposes",
true
);
}
}
2020-06-20 20:13:51 +02:00
}