Cofem/lib/endpoint.php

49 lines
1.2 KiB
PHP
Raw Normal View History

2024-01-18 11:49:38 +01:00
<?php
function runEndpoint($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);
}
function getEndpoint($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;
switch (gettype($output)) {
case 'string':
$output = $output_tmp;
$output['Status'] = 'Success';
break;
case 'bool':
$output['Status'] = $output ? 'Success' : 'Fail';
break;
default:
$output['Status'] = 'Fail';
$output["Error"] = "Endpoint error";
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);
}