Add MotorAPI module with configuration management

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.
This commit is contained in:
Jepp9350
2025-02-18 09:03:06 +01:00
parent 9272342c3a
commit 5af6fc5aa5
9 changed files with 196 additions and 1 deletions
+22
View File
@@ -0,0 +1,22 @@
<?php
namespace classes;
require_once WD . '/modules/motorapi/motorapi_c.php';
use interfaces\motorapi_i;
use motorapi\motorapi_c;
class motorapi implements motorapi_i
{
/**
* Configuration of the motorapi module
* @var motorapi_c
*/
public motorapi_c $config;
public function __construct()
{
$this->config = new motorapi_c();
}
}
+1
View File
@@ -58,6 +58,7 @@ require_once 'classes/statistics.php';
require_once 'classes/recaptcha.php';
require_once 'classes/email.php';
require_once 'classes/backup_store.php';
require_once 'classes/motorapi.php';
/**
* Modules
@@ -0,0 +1,8 @@
<?php
namespace interfaces;
interface motorapi_i
{
}
@@ -0,0 +1,29 @@
<?php
namespace motorapi\config;
use Exception;
use traits\module_config_variable;
class motorapi_daily_limit_c
{
use module_config_variable;
/**
* @throws Exception
*/
public function __construct()
{
self::setupConfigVariable(
'motorapi',
'daily_limit',
'int',
true,
null,
'The API request limit for the motorapi module',
'1',
false,
0
);
}
}
@@ -0,0 +1,29 @@
<?php
namespace motorapi\config;
use Exception;
use traits\module_config_variable;
class motorapi_enabled_c
{
use module_config_variable;
/**
* @throws Exception
*/
public function __construct()
{
self::setupConfigVariable(
'motorapi',
'enabled',
'bool',
true,
null,
'Whether the motorapi module is enabled or not',
'1',
false,
'false'
);
}
}
@@ -0,0 +1,29 @@
<?php
namespace motorapi\config;
use Exception;
use traits\module_config_variable;
class motorapi_secret_key_c
{
use module_config_variable;
/**
* @throws Exception
*/
public function __construct()
{
self::setupConfigVariable(
'motorapi',
'secret_key',
'string',
false,
null,
'The API token for motorapi.dk',
'1',
true,
''
);
}
}
@@ -0,0 +1,45 @@
<?php
namespace motorapi;
require_once WD . '/modules/motorapi/config/motorapi_enabled_c.php';
require_once WD . '/modules/motorapi/config/motorapi_secret_key_c.php';
require_once WD . '/modules/motorapi/config/motorapi_daily_limit_c.php';
use motorapi\config\motorapi_daily_limit_c;
use motorapi\config\motorapi_enabled_c;
use motorapi\config\motorapi_secret_key_c;
use traits\module_config_t;
class motorapi_c
{
use module_config_t;
/**
* The status of the motorapi module
* @var motorapi_enabled_c
*/
public motorapi_enabled_c $enabled;
/**
* The secret key for the motorapi module (API token)
* @var motorapi_secret_key_c
*/
public motorapi_secret_key_c $secret_key;
/**
* The daily limit for the motorapi module
* @var motorapi_daily_limit_c
*/
public motorapi_daily_limit_c $daily_limit;
public function __construct()
{
$this->setupConfig('motorapi');
$this->allowUpdate([
motorapi_enabled_c::class,
motorapi_secret_key_c::class,
motorapi_daily_limit_c::class
]);
$this->enabled = new motorapi_enabled_c();
$this->secret_key = new motorapi_secret_key_c();
$this->daily_limit = new motorapi_daily_limit_c();
}
}
@@ -6,6 +6,7 @@ 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;
@@ -176,5 +177,36 @@ class moduleConfigRoute
}
});
/** 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);
}
});
}
}
@@ -146,7 +146,7 @@ trait module_config_t
function parseConfigVariableType($type, $value)
{
// If the variable is an int, convert it to an int
if ($type == 'integer') {
if ($type == 'integer' || $type == 'int') {
return (integer)$value;
}
// If the variable is a boolean, convert it to a boolean