2024-01-18 11:49:38 +01:00
|
|
|
<?php
|
2024-04-28 22:37:23 +02:00
|
|
|
/**
|
|
|
|
* Executes an endpoint script and returns the results.
|
|
|
|
*
|
|
|
|
* This function includes an endpoint PHP file that defines a function named `endpoint` which
|
|
|
|
* is expected to accept an array parameter and return an array result.
|
|
|
|
* It simply scopes an external file into a function to prevent variable conflicts.
|
|
|
|
*
|
|
|
|
* @param string $endpoint_file The path to the endpoint PHP file.
|
|
|
|
* @return array|null Returns the result of the endpoint function if successful, or null if the
|
|
|
|
* endpoint function or file does not behave as expected.
|
|
|
|
*/
|
|
|
|
function runEndpoint(string $endpoint_file): ?array
|
2024-01-18 11:49:38 +01:00
|
|
|
{
|
|
|
|
|
2024-01-18 11:53:39 +01:00
|
|
|
$endpoint_data = $_POST;
|
2024-01-18 11:49:38 +01:00
|
|
|
require_once $endpoint_file;
|
|
|
|
|
|
|
|
return endpoint($endpoint_data);
|
|
|
|
}
|
|
|
|
|
2024-04-28 22:37:23 +02:00
|
|
|
/**
|
|
|
|
* Retrieves and processes the output of a specified endpoint.
|
|
|
|
*
|
|
|
|
* This function determines the appropriate endpoint PHP file based on the provided endpoint name,
|
|
|
|
* executes the endpoint, and returns its results as a JSON-encoded string. It handles and
|
|
|
|
* translates different return types into a JSON format and manages HTTP response codes based on
|
|
|
|
* success or failure of the endpoint execution.
|
|
|
|
*
|
|
|
|
* @param string $endpoint_name The name of the endpoint, which is used to construct the file path to the endpoint script.
|
|
|
|
* @return string A JSON-encoded string representing the result of the endpoint, including a status indicator and any relevant data or error messages.
|
|
|
|
*@global array $routerRequest Current request data that might influence the endpoint processing.
|
|
|
|
* @global array $routerConfig Global configuration that contains paths and settings.
|
|
|
|
*/
|
|
|
|
function getEndpoint(string $endpoint_name): string
|
2024-01-18 11:49:38 +01:00
|
|
|
{
|
|
|
|
$output = array();
|
2024-02-03 16:08:26 +01:00
|
|
|
$output["Status"] = "Fail";
|
2024-01-18 11:49:38 +01:00
|
|
|
global $routerConfig;
|
|
|
|
global $routerRequest;
|
|
|
|
|
|
|
|
if(!$endpoint_name){
|
2024-02-06 16:28:36 +01:00
|
|
|
$endpoint_name = $routerRequest["site_name"];
|
2024-01-18 11:49:38 +01:00
|
|
|
}
|
|
|
|
|
2024-02-06 16:24:57 +01:00
|
|
|
$endpoint_file = $routerConfig["endpoint_dir"] . $endpoint_name . ".php";
|
2024-02-03 17:05:31 +01:00
|
|
|
|
2024-02-06 16:24:57 +01:00
|
|
|
if (file_exists($endpoint_file)){
|
2024-04-11 10:36:40 +02:00
|
|
|
$output_tmp = runEndpoint($endpoint_file);
|
|
|
|
$output["Endpoint"] = $endpoint_name;
|
2024-04-11 15:18:17 +02:00
|
|
|
$type = gettype($output_tmp);
|
|
|
|
switch ($type) {
|
2024-04-11 15:27:06 +02:00
|
|
|
case 'array':
|
2024-05-01 17:34:58 +02:00
|
|
|
$output = $output_tmp;
|
|
|
|
break;
|
2024-04-26 14:37:54 +02:00
|
|
|
case 'string':
|
2024-04-11 15:27:06 +02:00
|
|
|
$output = $output_tmp;
|
|
|
|
$output['Status'] = 'Success';
|
|
|
|
break;
|
2024-04-26 14:37:54 +02:00
|
|
|
case 'boolean':
|
2024-04-11 15:15:41 +02:00
|
|
|
$output['Status'] = $output_tmp ? 'Success' : 'Fail';
|
2024-04-11 10:36:40 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$output['Status'] = 'Fail';
|
|
|
|
$output["Error"] = "Endpoint error";
|
2024-04-11 15:18:17 +02:00
|
|
|
$output["Type"] = $type;
|
2024-04-11 10:36:40 +02:00
|
|
|
http_response_code(500);
|
|
|
|
}
|
2024-01-18 11:49:38 +01:00
|
|
|
}
|
|
|
|
else{
|
2024-02-06 16:28:36 +01:00
|
|
|
$output["Error"] = "Not found";
|
2024-01-18 11:49:38 +01:00
|
|
|
http_response_code(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_encode($output);
|
|
|
|
}
|