2022-01-26 18:48:42 +01:00
|
|
|
<?php declare(strict_types=1);
|
2022-01-31 20:01:06 +01:00
|
|
|
|
2022-01-24 18:59:00 +01:00
|
|
|
namespace App\Controller;
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-01-31 20:01:06 +01:00
|
|
|
use Exception;
|
|
|
|
|
2022-01-26 18:48:42 +01:00
|
|
|
error_reporting(error_level: E_ALL);
|
|
|
|
|
2022-01-24 18:59:00 +01:00
|
|
|
require dirname(path: __DIR__) . '/vendor/autoload.php';
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-01-22 17:31:29 +01:00
|
|
|
// read config
|
2022-01-24 18:59:00 +01:00
|
|
|
$configFile = dirname(path: __DIR__) . "/config.json";
|
2022-01-26 18:48:42 +01:00
|
|
|
$configJSON = file_get_contents(filename: $configFile);
|
|
|
|
$config = json_decode(json: $configJSON, associative: true);
|
2022-01-22 17:31:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
// TODO only valid clients?
|
2022-01-24 18:59:00 +01:00
|
|
|
header(header: "Access-Control-Allow-Origin: *");
|
|
|
|
header(header: "Content-Type: application/json; charset=UTF-8");
|
|
|
|
header(header: "Access-Control-Allow-Methods: OPTIONS,GET,POST,PUT,DELETE");
|
|
|
|
header(header: "Access-Control-Max-Age: 3600");
|
|
|
|
header(header: "Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
2022-01-18 19:14:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
// TODO make a log class
|
2022-01-31 20:01:06 +01:00
|
|
|
$oFile = fopen(filename: 'log.txt', mode: 'a');
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-01-24 18:59:00 +01:00
|
|
|
$uri = parse_url(url: $_SERVER['REQUEST_URI'], component: PHP_URL_PATH);
|
|
|
|
fputs(stream: $oFile, data: $uri . PHP_EOL);
|
|
|
|
fclose(stream: $oFile);
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-01-31 20:01:06 +01:00
|
|
|
$uri = explode(separator: '/', string: $uri);
|
2022-01-18 19:14:24 +01:00
|
|
|
if ($uri[1] !== 'api') {
|
2022-01-24 18:59:00 +01:00
|
|
|
header(header: "HTTP/1.1 404 Not Found");
|
2022-01-18 19:14:24 +01:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
$requestMethod = $_SERVER["REQUEST_METHOD"];
|
|
|
|
|
2022-01-31 20:01:06 +01:00
|
|
|
try {
|
|
|
|
$controller = new RequestController(config: $config, requestMethod: $requestMethod, uri: $uri);
|
|
|
|
$controller->processRequest();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
echo json_encode(value: [
|
|
|
|
'error' => $e->getMessage()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
|