Files
api/traits/route_t.php
T
2024-12-17 12:01:01 +01:00

169 lines
4.7 KiB
PHP

<?php
namespace traits;
use classes\authentication;
use objects\logs_o;
trait route_t
{
private string $route;
public function __construct()
{
$this->route = $_SERVER['REQUEST_URI'];
}
private function registerRoute($route, $method, $callback): void
{
global $router;
$router->add($route, $method, $callback);
}
public function run(): void
{
// Add the routes here
}
/**
* GET route
* @param string $route Example: /home, /home/{id}
*/
public function get(string $route, callable $callback): void
{
$this->registerRoute($route, 'GET', $callback);
}
/**
* POST route
* @param string $route Example: /home, /home/{id}
*/
public function post(string $route, callable $callback): void
{
$this->registerRoute($route, 'POST', $callback);
}
/**
* PUT route
* @param string $route Example: /home, /home/{id}
*/
public function put(string $route, callable $callback): void
{
$this->registerRoute($route, 'PUT', $callback);
}
/**
* DELETE route
* @param string $route Example: /home, /home/{id}
*/
public function delete(string $route, callable $callback): void
{
$this->registerRoute($route, 'DELETE', $callback);
}
/**
* OPTIONS route
* @param string $route Example: /home, /home/{id}
*/
public function options(string $route, callable $callback): void
{
$this->registerRoute($route, 'OPTIONS', $callback);
}
/**
* PATCH route
* @param string $route Example: /home, /home/{id}
*/
public function patch(string $route, callable $callback): void
{
$this->registerRoute($route, 'PATCH', $callback);
}
/**
* Match route
* @param string $route Example: /home, /home/{id}
*/
private function match_route(string $route): bool
{
// Check if route is the same, or if it matches the regex pattern
return $route === $this->route || preg_match($route, $this->route);
}
/**
* Get the parameter from the route URL by index
* @param string $index
* @return string|null
*/
public function fromRoute(string $index): ?string
{
$params = explode('/', $this->route);
$index = array_search($index, $params);
return $params[$index] ?? null;
}
/**
* Require permission
* @param string $permission
* @return bool
*/
public function requirePermission(string $permission): bool
{
global $response;
// Check if the users authorization token has the required permission
try {
$user = (new authentication())->get_user();
// If there is no user, return an error
if (!$user) {
(new logs_o())->add('global', 'global', 1, 0, 'AUTHENTICATION_FAILED', 'Authentication failed. Invalid, or missing token');
$response->error('Authentication failed. Invalid or missing token.', 401);
}
if (!$user->hasPermission($permission)) {
(new logs_o())->add('global', 'global', 1, $user->id, 'PERMISSION_DENIED', 'Permission denied. Missing permission: ' . $permission);
$response->error('Permission denied. Missing permission: ' . $permission .' for user: ' . $user->id . ' In group: ' . $user->group_id->value(), 403);
}
} catch (\Exception $e) {
$response->error($e->getMessage(), 400);
}
return true;
}
/**
* Require plate scanner authentication
* @return bool
*/
public function requirePlateScannerAuth(): bool
{
global $response;
// Check if the plate scanner has a valid API key
try {
$plate_scanner = (new authentication())->get_plate_scanner();
if (!$plate_scanner) {
(new logs_o())->add('global', 'global', 1, 0, 'AUTHENTICATION_FAILED', 'Authentication failed. Invalid, or missing API key');
$response->error('Authentication failed. Invalid or missing API key.', 401);
}
} catch (\Exception $e) {
$response->error($e->getMessage(), 400);
}
return true;
}
/**
* Get the parameter from the query string by name
* @param string $name
* @return string|null
*/
public function fromQuery(string $name): ?string
{
return (isset($_GET[$name])) ? $_GET[$name] : null;
}
/**
* Get data from the request body or query string by name
* @param string $name
* @return string|null
*/
public function fromRequest(string $name): ?string
{
return (isset($_POST[$name])) ? $_POST[$name] : $this->fromQuery($name);
}
}