Introduced the MotorAPI module, including its configuration classes, interface, and integration with the router. The configuration supports enabling, managing API tokens, and daily request limits. Added support for fetching and updating MotorAPI settings via new API routes.
212 lines
9.1 KiB
PHP
212 lines
9.1 KiB
PHP
<?php
|
|
|
|
namespace routes;
|
|
|
|
use classes\authentication;
|
|
use classes\backup_store;
|
|
use classes\economic;
|
|
use classes\email;
|
|
use classes\motorapi;
|
|
use classes\recaptcha;
|
|
use classes\response;
|
|
use classes\router;
|
|
use objects\logs_o;
|
|
use traits\route_t;
|
|
|
|
class moduleConfigRoute
|
|
{
|
|
use route_t;
|
|
|
|
public function run(): void
|
|
{
|
|
global /** @var response $response */
|
|
/** @var router $router */
|
|
$router, $response;
|
|
|
|
|
|
/** Economic config > GET */
|
|
$this->get('/economic/config', function () {
|
|
global $response;
|
|
$this->requirePermission('economic_config');
|
|
$user = (new authentication())->get_user();
|
|
if ($user) {
|
|
(new logs_o())->add('economic_config', 'global', 1, $user->id, 'ECONOMIC_CONFIG', 'Successfully fetched economic config');
|
|
$response->success(
|
|
(new economic())->config->getConfigRequest()
|
|
);
|
|
} else {
|
|
(new logs_o())->add('economic_config', 'global', 1, 0, 'ECONOMIC_CONFIG', 'No user found, or invalid session');
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
/** Economic config > POST */
|
|
$this->post('/economic/config', function () {
|
|
global $response;
|
|
$this->requirePermission('economic_config');
|
|
$user = (new authentication())->get_user();
|
|
if ($user) {
|
|
(new logs_o())->add('economic_config', 'global', 1, $user->id, 'ECONOMIC_CONFIG', 'Successfully updated economic config');
|
|
$response->success(
|
|
(new economic())->config->postConfigRequest()
|
|
);
|
|
} else {
|
|
(new logs_o())->add('economic_config', 'global', 1, 0, 'ECONOMIC_CONFIG', 'No user found, or invalid session');
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
/** reCAPTCHA config > GET */
|
|
$this->get('/reCAPTCHA/config', function () {
|
|
global $response;
|
|
$this->requirePermission('recaptcha_config');
|
|
$user = (new authentication())->get_user();
|
|
if ($user) {
|
|
(new logs_o())->add('recaptcha_config', 'global', 1, $user->id, 'RECAPTCHA_CONFIG', 'Successfully fetched recaptcha config');
|
|
$response->success(
|
|
(new recaptcha())->config->getConfigRequest()
|
|
);
|
|
} else {
|
|
(new logs_o())->add('recaptcha_config', 'global', 1, 0, 'RECAPTCHA_CONFIG', 'No user found, or invalid session');
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
/** reCAPTCHA config > POST */
|
|
$this->post('/reCAPTCHA/config', function () {
|
|
global $response;
|
|
$this->requirePermission('recaptcha_config');
|
|
$user = (new authentication())->get_user();
|
|
if ($user) {
|
|
(new logs_o())->add('recaptcha_config', 'global', 1, $user->id, 'RECAPTCHA_CONFIG', 'Successfully updated recaptcha config');
|
|
$response->success(
|
|
(new recaptcha())->config->postConfigRequest()
|
|
);
|
|
} else {
|
|
(new logs_o())->add('recaptcha_config', 'global', 1, 0, 'RECAPTCHA_CONFIG', 'No user found, or invalid session');
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
/** Email config > GET */
|
|
$this->get('/email/config', function () {
|
|
global $response;
|
|
$this->requirePermission('email_config');
|
|
$user = (new authentication())->get_user();
|
|
if ($user) {
|
|
(new logs_o())->add('email_config', 'global', 1, $user->id, 'EMAIL_CONFIG', 'Successfully fetched email config');
|
|
$response->success(
|
|
(new email())->config->getConfigRequest()
|
|
);
|
|
} else {
|
|
(new logs_o())->add('email_config', 'global', 1, 0, 'EMAIL_CONFIG', 'No user found, or invalid session');
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
/** Email config > POST */
|
|
$this->post('/email/config', function () {
|
|
global $response;
|
|
$this->requirePermission('email_config');
|
|
$user = (new authentication())->get_user();
|
|
if ($user) {
|
|
(new logs_o())->add('email_config', 'global', 1, $user->id, 'EMAIL_CONFIG', 'Successfully updated email config');
|
|
$response->success(
|
|
(new email())->config->postConfigRequest()
|
|
);
|
|
} else {
|
|
(new logs_o())->add('email_config', 'global', 1, 0, 'EMAIL_CONFIG', 'No user found, or invalid session');
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
/** Email config > TEST */
|
|
$this->post('/email/config/test', function () {
|
|
global $response;
|
|
$this->requirePermission('email_config');
|
|
$user = (new authentication())->get_user();
|
|
if ($user) {
|
|
$test_recipient = $this->fromRequest('email');
|
|
if (!$test_recipient) {
|
|
(new logs_o())->add('email_config', 'global', 1, $user->id, 'EMAIL_CONFIG', 'Test email recipient not set');
|
|
$response->error('Test email recipient not set', 400);
|
|
}
|
|
// Check if the test recipient is a valid email
|
|
if (!filter_var($test_recipient, FILTER_VALIDATE_EMAIL)) {
|
|
(new logs_o())->add('email_config', 'global', 1, $user->id, 'EMAIL_CONFIG', 'Invalid test email recipient');
|
|
$response->error('Invalid test email recipient', 400);
|
|
}
|
|
// Log the incident
|
|
(new logs_o())->add('email_config', 'global', 1, $user->id, 'EMAIL_CONFIG', 'Successfully tested email config');
|
|
$response->success(
|
|
(new email())->testConfigRequest($test_recipient)
|
|
);
|
|
} else {
|
|
(new logs_o())->add('email_config', 'global', 1, 0, 'EMAIL_CONFIG', 'No user found, or invalid session');
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
$this->get('/backups/config', function () {
|
|
global $response;
|
|
$this->requirePermission('backups_config');
|
|
$user = (new authentication())->get_user();
|
|
if ($user) {
|
|
(new logs_o())->add('backups_config', 'global', 1, $user->id, 'BACKUPS_CONFIG', 'Successfully fetched backups config');
|
|
$response->success(
|
|
(new backup_store())->config->getConfigRequest()
|
|
);
|
|
} else {
|
|
(new logs_o())->add('backups_config', 'global', 1, 0, 'BACKUPS_CONFIG', 'No user found, or invalid session');
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
$this->post('/backups/config', function () {
|
|
global $response;
|
|
$this->requirePermission('backups_config');
|
|
$user = (new authentication())->get_user();
|
|
if ($user) {
|
|
(new logs_o())->add('backups_config', 'global', 1, $user->id, 'BACKUPS_CONFIG', 'Successfully updated backups config');
|
|
$response->success(
|
|
(new backup_store())->config->postConfigRequest()
|
|
);
|
|
} else {
|
|
(new logs_o())->add('backups_config', 'global', 1, 0, 'BACKUPS_CONFIG', 'No user found, or invalid session');
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
/** MotorAPI config > GET */
|
|
$this->get('/motorapi/config', function () {
|
|
global $response;
|
|
$this->requirePermission('motorapi_config');
|
|
$user = (new authentication())->get_user();
|
|
if ($user) {
|
|
(new logs_o())->add('motorapi_config', 'global', 1, $user->id, 'MOTORAPI_CONFIG', 'Successfully fetched motorapi config');
|
|
$response->success(
|
|
(new motorapi())->config->getConfigRequest()
|
|
);
|
|
} else {
|
|
(new logs_o())->add('motorapi_config', 'global', 1, 0, 'MOTORAPI_CONFIG', 'No user found, or invalid session');
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
/** MotorAPI config > POST */
|
|
$this->post('/motorapi/config', function () {
|
|
global $response;
|
|
$this->requirePermission('motorapi_config');
|
|
$user = (new authentication())->get_user();
|
|
if ($user) {
|
|
(new logs_o())->add('motorapi_config', 'global', 1, $user->id, 'MOTORAPI_CONFIG', 'Successfully updated motorapi config');
|
|
$response->success(
|
|
(new motorapi())->config->postConfigRequest()
|
|
);
|
|
} else {
|
|
(new logs_o())->add('motorapi_config', 'global', 1, 0, 'MOTORAPI_CONFIG', 'No user found, or invalid session');
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
}
|
|
} |