Files
ezServer-MacOS/server/router.php
T

46 lines
1.5 KiB
PHP

<?php
// Launch the original API front controller for the verified routes.
function failRequest($status, $message)
{
http_response_code($status);
header('Content-Type: application/json; charset=utf-8');
echo json_encode(['error' => $message]);
}
header('Cache-Control: no-store');
$host = $_SERVER['HTTP_HOST'] ?? '';
if (!preg_match('/\A(?:127\.0\.0\.1|localhost)(?::[0-9]{1,5})?\z/i', $host)) {
failRequest(400, 'Use a loopback host');
return;
}
if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'GET') {
header('Allow: GET');
failRequest(405, 'Only GET is supported');
return;
}
$runtime = getenv('EZSERVER_RUNTIME');
$path = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
if ($path === '/') {
header('Content-Type: text/html; charset=utf-8');
readfile($runtime . '/index.html');
return;
}
$patientLookup = getenv('EZSERVER_SQLITE')
&& preg_match('~\A/api(?:/index\.php)?/v1/e2ds/patientsbychartno/?\z~i', $path ?? '');
if (!$patientLookup && !preg_match('~\A/api(?:/index\.php)?(?:/v1/(?:auth/ping|info/(?:versions|versionsbyname)))?/?\z~i', $path ?? '')) {
failRequest(404, 'Route is unavailable in the macOS preview');
return;
}
$public = $runtime . '/www/ezwebserver/src/public';
$_SERVER['DOCUMENT_ROOT'] = $public;
$_SERVER['SCRIPT_FILENAME'] = $public . '/api/index.php';
$_SERVER['SCRIPT_NAME'] = '/api/index.php';
$_SERVER['PHP_SELF'] = '/api/index.php';
$_SERVER['CI_ENV'] = 'production';
chdir($public . '/api');
require $_SERVER['SCRIPT_FILENAME'];