fix dealproxy

This commit is contained in:
7181128 2026-05-24 19:13:22 +03:00
parent fc526909df
commit 9e4e2711cc

View File

@ -1,10 +1,7 @@
<?php <?php
/** /**
* Proxy class for forwarding Deal API requests. * Proxy class for forwarding Deal API requests.
* * Compatible with PHP 5.6.
* Handles CORS, builds request headers (including session cookies),
* forwards the request to the legacy API and returns the response.
*/ */
class DealProxy class DealProxy
{ {
@ -17,16 +14,16 @@ class DealProxy
} }
/** Simple logger */ /** Simple logger */
private function logMessage(string $message): void private function logMessage($message)
{ {
$date = date('Y-m-d H:i:s'); $date = date('Y-m-d H:i:s');
file_put_contents($this->logFile, "[$date] $message\n", FILE_APPEND); file_put_contents($this->logFile, "[$date] $message\n", FILE_APPEND);
} }
/** Build the target URL based on incoming request */ /** Build the target URL based on incoming request */
private function buildTargetUrl(): string private function buildTargetUrl()
{ {
$path = $_GET['path'] ?? ''; $path = isset($_GET['path']) ? $_GET['path'] : '';
unset($_GET['path']); unset($_GET['path']);
$query = http_build_query($_GET); $query = http_build_query($_GET);
$apiUrl = "https://llar.joyworkdev.ru/api/" . ltrim($path, '/'); $apiUrl = "https://llar.joyworkdev.ru/api/" . ltrim($path, '/');
@ -37,14 +34,14 @@ class DealProxy
} }
/** Build headers for the forwarded request */ /** Build headers for the forwarded request */
private function buildHeaders(): array private function buildHeaders()
{ {
$headers = [ $headers = array(
'Accept: application/json', 'Accept: application/json',
]; );
// Preserve original Content-Type if present // Preserve original Content-Type if present
$originalContentType = $_SERVER['CONTENT_TYPE'] ?? ''; $originalContentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
if ($originalContentType) { if ($originalContentType) {
$headers[] = 'Content-Type: ' . $originalContentType; $headers[] = 'Content-Type: ' . $originalContentType;
$this->logMessage("Incoming Content-Type: '$originalContentType'"); $this->logMessage("Incoming Content-Type: '$originalContentType'");
@ -64,17 +61,17 @@ class DealProxy
} }
/** Forward the request using cURL and output the response */ /** Forward the request using cURL and output the response */
private function forwardRequest(string $targetUrl, array $headers, string $method, string $body): void private function forwardRequest($targetUrl, $headers, $method, $body)
{ {
$ch = curl_init(); $ch = curl_init();
curl_setopt_array($ch, [ curl_setopt_array($ch, array(
CURLOPT_URL => $targetUrl, CURLOPT_URL => $targetUrl,
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method, CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => $body, CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => $headers, CURLOPT_HTTPHEADER => $headers,
CURLOPT_HEADER => false, CURLOPT_HEADER => false,
]); ));
$response = curl_exec($ch); $response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
@ -89,9 +86,9 @@ class DealProxy
} }
/** Main handler called from index.php */ /** Main handler called from index.php */
public function handle(): void public function handle()
{ {
// CORS headers keep them here to be sent before any output // CORS headers must be sent before any output
header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Credentials: true'); header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS'); header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');