<?php
use Microservices\App\Constant;
use Microservices\App\Env;
use Microservices\App\HttpStatus;
use Microservices\App\SessionHandler\Session;
use Microservices\App\Reload;
use Microservices\App\Start;
use Microservices\TestCase\Test;
ini_set(option: 'display_errors', value: true);
error_reporting(error_level: E_ALL);
define('ROOT', realpath(path: __DIR__ . '/../../'));
define('ROUTE_URL_PARAM', 'route');
require_once ROOT . DIRECTORY_SEPARATOR . 'Autoload.php';
spl_autoload_register(
callback: 'Microservices\Autoload::register'
);
// Load .env(s)
foreach ([
'.env',
'.env.customer.container',
'.env.global.container',
'.env.rateLimiting',
'.env.route'
] as $envFilename) {
$envDataArr = parse_ini_file(
filename: ROOT . DIRECTORY_SEPARATOR . $envFilename
);
foreach ($envDataArr as $envVarName => $envVarValue) {
putenv(
assignment: "{$envVarName}={$envVarValue}"
);
}
}
Constant::init();
Env::$timestamp = time();
Env::init();
// Process the request
$httpReqData = [];
$httpReqData['streamData'] = true;
$httpReqData['server']['domainName'] = $_SERVER['HTTP_HOST'];
$httpReqData['server']['httpMethod'] = $_SERVER['REQUEST_METHOD'];
if (
((int)getenv('DISABLE_REQUESTS_VIA_PROXIES')) === 1
&& !isset($_SERVER['REMOTE_ADDR'])
) {
die('Invalid request');
}
$httpReqData['server']['httpRequestIP'] = getHttpRequestIp();
$httpReqData['header'] = getallheaders();
if (isset($_SERVER['Range'])) {
$httpReqData['header']['range'] = $_SERVER['Range'];
}
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$httpReqData['header']['userAgent'] = $_SERVER['HTTP_USER_AGENT'];
}
if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
$httpReqData['header']['tokenHeader'] = $_SERVER['HTTP_AUTHORIZATION'];
}
$httpReqData['get'] = &$_GET;
if (isset($httpReqData['get'][ROUTE_URL_PARAM])) {
$httpReqData['get'][ROUTE_URL_PARAM] = '/' . trim(
string: $httpReqData['get'][ROUTE_URL_PARAM],
characters: '/'
);
} else {
die('Missing route');
}
$httpReqData['post'] = file_get_contents(
filename: 'php://input'
);
$httpReqData['files'] = [];
if (isset($_FILES)) {
$httpReqData['files'] = &$_FILES;
}
$httpReqData['isWebRequest'] = true;
$httpReqData['httpRequestHash'] = httpRequestHash(
hashArray: [
$_SERVER['HTTP_ACCEPT_ENCODING'] ?? '',
$_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '',
$_SERVER['HTTP_ACCEPT'] ?? '',
$_SERVER['HTTP_USER_AGENT'] ?? ''
]
);
if (
isset($httpReqData['get'][ROUTE_URL_PARAM])
&& in_array(
needle: $httpReqData['get'][ROUTE_URL_PARAM],
haystack: [
'/all-test',
'/auth-test',
'/open-test',
'/open-test-xml',
'/supp-test'
],
strict: true
)
&& $httpReqData['server']['domainName'] === 'localhost'
) {
$testObj = new Test($httpReqData);
switch ($httpReqData['get'][ROUTE_URL_PARAM]) {
case '/all-test':
echo '<pre>'.print_r(value: $testObj->processAllTest(), return: true);
break;
case '/auth-test':
echo '<pre>'.print_r(value: $testObj->processPrivate(), return: true);
break;
case '/open-test':
echo '<pre>'.print_r(value: $testObj->processPublic(), return: true);
break;
case '/open-test-xml':
echo '<pre>'.print_r(value: $testObj->processPublicXml(), return: true);
break;
case '/supp-test':
echo '<pre>'.print_r(value: $testObj->processPrivateSupplement(), return: true);
break;
}
} else {
if ($httpReqData['get'][ROUTE_URL_PARAM] === '/' . Env::$reloadRequestRoutePrefix) {
Reload::process(
httpRequestIp: $httpReqData['server']['httpRequestIP']
);
return false;
} else {
ob_start();
[$responseHeaderArr, $responseContent, $responseCode] = Start::http(
httpReqData: $httpReqData
);
@ob_clean();
$responseCode = $responseCode ?? HttpStatus::$Ok;
http_response_code(response_code: $responseCode);
foreach ($responseHeaderArr as $headerName => $headerValue) {
header(
header: "{$headerName}: {$headerValue}"
);
}
die($responseContent);
}
}
/**
* Unique HTTP request hash
*
* @param array $hashArray Hash array
*
* @return string
*/
function httpRequestHash($hashArray): string
{
return md5(
json_encode(
value: $hashArray
)
);
}
/**
* Get request IP
*
* @return string
*/
function getHttpRequestIp() {
// Check for shared internet connections (e.g., Cloudflare, proxy)
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
// Check if the user is behind a proxy and the IP is forwarded
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
// HTTP_X_FORWARDED_FOR can contain a comma-separated list of IPs
// The first one is typically the original customer IP
$ipList = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$ip = trim($ipList[0]);
}
// Default method: get the remote address directly
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
|