Joywork/engine/classes/DealProxy.php

110 lines
3.5 KiB
PHP
Raw Normal View History

2026-05-24 15:41:00 +02:00
<?php
/**
* Proxy class for forwarding Deal API requests.
2026-05-24 18:13:22 +02:00
* Compatible with PHP 5.6.
2026-05-24 15:41:00 +02:00
*/
class DealProxy
{
/** Path to log file */
private $logFile;
public function __construct()
{
$this->logFile = __DIR__ . '/proxy.log';
}
/** Simple logger */
2026-05-24 18:13:22 +02:00
private function logMessage($message)
2026-05-24 15:41:00 +02:00
{
$date = date('Y-m-d H:i:s');
file_put_contents($this->logFile, "[$date] $message\n", FILE_APPEND);
}
/** Build the target URL based on incoming request */
2026-05-24 18:13:22 +02:00
private function buildTargetUrl()
2026-05-24 15:41:00 +02:00
{
2026-05-24 18:13:22 +02:00
$path = isset($_GET['path']) ? $_GET['path'] : '';
2026-05-24 15:41:00 +02:00
unset($_GET['path']);
$query = http_build_query($_GET);
$apiUrl = "https://llar.joyworkdev.ru/api/" . ltrim($path, '/');
if ($query) {
$apiUrl .= '?' . $query;
}
return $apiUrl;
}
/** Build headers for the forwarded request */
2026-05-24 18:13:22 +02:00
private function buildHeaders()
2026-05-24 15:41:00 +02:00
{
2026-05-24 18:13:22 +02:00
$headers = array(
2026-05-24 15:41:00 +02:00
'Accept: application/json',
2026-05-24 18:13:22 +02:00
);
2026-05-24 15:41:00 +02:00
// Preserve original Content-Type if present
2026-05-24 18:13:22 +02:00
$originalContentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
2026-05-24 15:41:00 +02:00
if ($originalContentType) {
$headers[] = 'Content-Type: ' . $originalContentType;
$this->logMessage("Incoming Content-Type: '$originalContentType'");
} else {
$this->logMessage('WARNING: Incoming Content-Type not detected');
}
// Pass session cookie if exists
if (!empty($_COOKIE['PHPSESSID'])) {
$headers[] = 'Cookie: PHPSESSID=' . $_COOKIE['PHPSESSID'];
}
// Pass custom header from Vue if present
if (!empty($_SERVER['HTTP_X_SESSION_ID'])) {
$headers[] = 'X-Session-ID: ' . $_SERVER['HTTP_X_SESSION_ID'];
}
return $headers;
}
/** Forward the request using cURL and output the response */
2026-05-24 18:13:22 +02:00
private function forwardRequest($targetUrl, $headers, $method, $body)
2026-05-24 15:41:00 +02:00
{
$ch = curl_init();
2026-05-24 18:13:22 +02:00
curl_setopt_array($ch, array(
2026-05-24 15:41:00 +02:00
CURLOPT_URL => $targetUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HEADER => false,
2026-05-24 18:13:22 +02:00
));
2026-05-24 15:41:00 +02:00
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
if ($curlError) {
$this->logMessage('cURL Error: ' . $curlError);
}
curl_close($ch);
http_response_code($httpCode);
echo $response;
}
/** Main handler called from index.php */
2026-05-24 18:13:22 +02:00
public function handle()
2026-05-24 15:41:00 +02:00
{
2026-05-24 18:13:22 +02:00
// CORS headers must be sent before any output
2026-05-24 15:41:00 +02:00
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Accept, Content-Type, X-Requested-With, Authorization');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(204);
exit;
}
$targetUrl = $this->buildTargetUrl();
$headers = $this->buildHeaders();
$method = $_SERVER['REQUEST_METHOD'];
$body = file_get_contents('php://input');
$this->logMessage('Forwarding request to ' . $targetUrl);
$this->forwardRequest($targetUrl, $headers, $method, $body);
}
}
?>