= 0) { $urlTarget = WEBSITE_SCHEME.'://'.WEBSITE_HOST."/$username/$urlFile"; } else { $urlTarget = WEBSITE_SCHEME.'://'.WEBSITE_HOST."/$urlFile"; } } $referer = $_SERVER["HTTP_REFERER"] ?? $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["SERVER_NAME"].$path; $sessionId = isset($_COOKIE['PHPSESSID']) ? $_COOKIE['PHPSESSID'] : sha1(sprintf("%.15s%ld%ld%0.8F", $referer, time(), microtime(), lcg_value() * 10)); $isPost = $_SERVER["REQUEST_METHOD"] === 'POST'; $headers = "Cookie: PHPSESSID=$sessionId\r\n"; foreach ($_SERVER as $key => $value) { if (strpos($key, 'HTTP_X') === 0 || strpos($key, 'HTTP_HX') === 0) { $headerName = toKebabCase(substr($key, 5)); $headers .= "$headerName: $value\r\n"; } elseif (strpos($key, 'CONTENT_') === 0) { $headerName = toKebabCase($key); $headers .= "$headerName: $value\r\n"; } } $http = [ 'http' => [ 'method' => $_SERVER["REQUEST_METHOD"], 'header' => $headers, ] ]; if ($isPost) { function mapper($k, $v) { return "$k=$v"; } $http['http']['content'] = implode('&', array_map('mapper', array_keys($_POST), array_values($_POST))); } $context = stream_context_create($http); $response = file_get_contents($urlTarget, false, $context); if (isset($http_response_header)) { // Pass headers to output foreach ($http_response_header as $header) { $headerParts = preg_split('/:\s+/', $header, 2); $headerName = $headerParts[0]; $loweredHeaderName = strtolower($headerName); if (isset($headerParts[1])) { $headerValue = $headerParts[1]; $loweredHeaderValue = strtolower($headerValue); } if (strpos($loweredHeaderName, ' 301 ') !== false) { header($headerName, true); } elseif ($loweredHeaderName === 'location' || strpos($loweredHeaderName, 'x-') === 0 || strpos($loweredHeaderName, 'hx-') === 0) { $newValue = preg_replace('/^.*\/\/'.WEBSITE_HOST.'\/'.$username.'/', $referer, $headerValue); header("$headerName: $newValue", true); } elseif (in_array($loweredHeaderName, ['content-type', 'content-language', 'content-security', 'content-length', 'server'])) { // Pass following headers to response header("$headerName: $headerValue", true); } elseif ($loweredHeaderName === 'set-cookie') { // Replace cookie domain and path $newValue = preg_replace('/((?>domain)\s*=\s*)[^;\s]+/', '\1.' . $_SERVER['HTTP_HOST'], $headerValue); $newValue = preg_replace('/\s*;?\s*path\s*=\s*[^;\s]+/', '', $newValue); header("$headerName: $newValue", true); } /*elseif ($loweredHeaderName === 'content-encoding' && $loweredHeaderValue === 'gzip') { // Decode response body if gzip encoding is used $response = gzdecode($response); }*/ } } $response = preg_replace('/(href|src|action)=([\'"]?)\/(?:'.$username.'\/?)?/', '$1=$2'.$path.'/', $response); $response = preg_replace('/https?:\\\\?\/\\\\?\/'.WEBSITE_HOST.'\\\\?\/'.$username.'/', $referer, $response); echo $response;