Refactored database methods to improve code readability, reusability, and error handling. Introduced input validation helper functions and parsing capabilities for objects with optional callbacks. Added new routes for handling categories and improved product-related functionality to align with the new architecture.
226 lines
6.7 KiB
PHP
226 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace traits;
|
|
|
|
use classes\authentication;
|
|
use classes\recaptcha;
|
|
use objects\logs_o;
|
|
|
|
trait route_t
|
|
{
|
|
private string $route;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->route = $_SERVER['REQUEST_URI'];
|
|
}
|
|
|
|
public function run(): void
|
|
{
|
|
// Add the routes here
|
|
}
|
|
|
|
public function requireParameters(array $parameters): void
|
|
{
|
|
global $response;
|
|
$missing_parameters = [];
|
|
// Check if all required parameters are set
|
|
foreach ( $parameters as $parameter ) {
|
|
if (!$response->getRequestParameter($parameter)) {
|
|
$missing_parameters[] = $parameter;
|
|
}
|
|
}
|
|
if (count($missing_parameters) > 0) {
|
|
$response->error('Missing required parameters: ' . implode(', ', $missing_parameters), 400);
|
|
}
|
|
}
|
|
|
|
public function isParametersSet(array $parameters): bool
|
|
{
|
|
global $response;
|
|
// Check if all required parameters are set
|
|
foreach ( $parameters as $parameter ) {
|
|
if (!$response->isRequestParameterSet($parameter)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* GET route
|
|
* @param string $route Example: /home, /home/{id}
|
|
*/
|
|
public function get(string $route, callable $callback): void
|
|
{
|
|
$this->registerRoute($route, 'GET', $callback);
|
|
}
|
|
|
|
private function registerRoute($route, $method, $callback): void
|
|
{
|
|
global $router;
|
|
$router->add($route, $method, $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);
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* Require reCAPTCHA for the request
|
|
* @return bool
|
|
*/
|
|
public function requireRecaptcha(): bool
|
|
{
|
|
global $response;
|
|
// Check if the reCAPTCHA is valid
|
|
try {
|
|
$recaptcha_response = $response->getRequestParameter('g_recaptcha_response');
|
|
$recaptcha = (new recaptcha())->validate($recaptcha_response);
|
|
if (!$recaptcha) {
|
|
(new logs_o())->add('global', 'global', 1, 0, 'AUTHENTICATION_FAILED', 'Authentication failed. Invalid, or missing reCAPTCHA');
|
|
$response->error('Authentication failed. Invalid or missing reCAPTCHA.', 401);
|
|
}
|
|
} catch (\Exception $e) {
|
|
$response->error($e->getMessage(), 400);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get data from the request body or query string by name
|
|
* @param string $name
|
|
* @return string|null
|
|
*/
|
|
public function fromRequest(string $name): ?string
|
|
{
|
|
// If the $_POST variable is set, return the value from the POST variable, otherwise return the value from the query string
|
|
if (isset($_POST)) {
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
if (isset($data[$name])) {
|
|
return $data[$name];
|
|
}
|
|
}
|
|
return (isset($_POST[$name])) ? $_POST[$name] : $this->fromQuery($name);
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
} |