169 lines
6.3 KiB
PHP
169 lines
6.3 KiB
PHP
<?php
|
|
|
|
// CORS заголовки
|
|
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;
|
|
}
|
|
|
|
function logMessage($message)
|
|
{
|
|
$logFile = __DIR__ . '/proxy.log';
|
|
$date = date('Y-m-d H:i:s');
|
|
file_put_contents($logFile, "[$date] $message\n", FILE_APPEND);
|
|
}
|
|
|
|
$path = isset($_GET['path']) ? $_GET['path'] : '';
|
|
unset($_GET['path']);
|
|
$query_params = http_build_query($_GET);
|
|
|
|
$api_url = "https://llar.joyworkdev.ru/api/" . ltrim($path, '/');
|
|
if (!empty($query_params)) {
|
|
$api_url .= '?' . $query_params;
|
|
}
|
|
|
|
$contentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
|
|
$isMultipart = strpos($contentType, 'multipart/form-data') !== false;
|
|
|
|
$headers = [
|
|
'Accept: application/json',
|
|
];
|
|
|
|
$sessionIdFromCookie = isset($_COOKIE['PHPSESSID']) ? $_COOKIE['PHPSESSID'] : null;
|
|
|
|
if ($sessionIdFromCookie) {
|
|
$headers[] = "Cookie: PHPSESSID=$sessionIdFromCookie";
|
|
}
|
|
|
|
if (!empty($_SERVER['HTTP_X_SESSION_ID'])) {
|
|
$headers[] = 'X-Session-ID: ' . $_SERVER['HTTP_X_SESSION_ID'];
|
|
}
|
|
|
|
// Basic Auth для dev.joywork.ru
|
|
//$headers[] = 'Authorization: Basic YWRtaW5sb2dpbjphZG1pbnBhc3N3b3Jk'; // adminlogin:adminpassword
|
|
//$headers[] = 'Content-Type: application/json';
|
|
|
|
$originalContentType = isset($_SERVER['HTTP_CONTENT_TYPE']) ? $_SERVER['HTTP_CONTENT_TYPE'] : isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
|
|
logMessage("Incoming Content-Type: '$originalContentType'");
|
|
|
|
// Передаём Content-Type, который пришёл от клиента (Vue)
|
|
if ($originalContentType) {
|
|
$headers[] = 'Content-Type: ' . $originalContentType;
|
|
} else {
|
|
logMessage("WARNING: Incoming Content-Type was not detected.");
|
|
}
|
|
|
|
logMessage("Forwarding Content-Type: " . ($originalContentType ?: 'NOT SET'));
|
|
|
|
// Для multipart это бинарные данные, но мы передаём их как есть.
|
|
$contentLength = isset($_SERVER['CONTENT_LENGTH']) ? $_SERVER['CONTENT_LENGTH'] : 0;
|
|
$input_data = file_get_contents('php://input');
|
|
logMessage("Input data length: " . strlen($input_data));
|
|
|
|
//if ($isMultipart) {
|
|
// $name = isset($_POST['name']) ? $_POST['name'] : null;
|
|
// $target_id = (int)(isset($_POST['target_id']) ? $_POST['target_id'] : 0);
|
|
// $target_type = (int)(isset($_POST['target_type']) ? $_POST['target_type'] : 0);
|
|
// $parent = (int)(isset($_POST['parent']) ? $_POST['parent'] : 0);
|
|
//
|
|
// logMessage("Multipart upload started. name=$name, target_id=$target_id, target_type=$target_type, parent=$parent");
|
|
//
|
|
// if (isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK) {
|
|
// $file = $_FILES['file'];
|
|
// logMessage("File received: " . $file['name'] . " (size: " . $file['size'] . " bytes)");
|
|
//
|
|
// $fileName = $file['name'];
|
|
// $filePath = 'documents/' . $fileName;
|
|
// $fullPath = $_SERVER['DOCUMENT_ROOT'] . '/' . $filePath;
|
|
//
|
|
// if (move_uploaded_file($file['tmp_name'], $fullPath)) {
|
|
// logMessage("File moved successfully to $fullPath");
|
|
//
|
|
// $mysqli = new mysqli("localhost", "user", "password", "database");
|
|
// if (!$mysqli->connect_error) {
|
|
// logMessage("DB connected successfully");
|
|
//
|
|
// $stmt = $mysqli->prepare("INSERT INTO documents (target_id, target_type, parent, name, file) VALUES (?, ?, ?, ?, ?)");
|
|
// $user_id = (int)(isset($_SESSION['id']) ? $_SESSION['id'] : 0);
|
|
// $stmt->bind_param("iiiis", $target_id, $target_type, $parent, $name, $filePath);
|
|
// $stmt->execute();
|
|
// $documentId = $stmt->insert_id;
|
|
// $stmt->close();
|
|
//
|
|
// logMessage("Document inserted with ID=$documentId");
|
|
//
|
|
// $api_url = "http://llar.joyworkdev.ru/api/documents/storeFromLegacy";
|
|
// $postData = json_encode([
|
|
// 'document_id' => $documentId,
|
|
// 'name' => $name,
|
|
// 'file' => $filePath,
|
|
// 'target_id' => $target_id,
|
|
// 'target_type' => $target_type,
|
|
// ]);
|
|
//
|
|
// $ch = curl_init();
|
|
// curl_setopt_array($ch, [
|
|
// CURLOPT_URL => $api_url,
|
|
// CURLOPT_RETURNTRANSFER => true,
|
|
// CURLOPT_CUSTOMREQUEST => 'POST',
|
|
// CURLOPT_POSTFIELDS => $postData,
|
|
// CURLOPT_HTTPHEADER => $headers,
|
|
// CURLOPT_HEADER => false,
|
|
// ]);
|
|
//
|
|
// $response = curl_exec($ch);
|
|
// $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
// curl_close($ch);
|
|
//
|
|
// logMessage("API response code=$httpCode, response=" . substr($response, 0, 200));
|
|
//
|
|
// http_response_code($httpCode);
|
|
// echo $response;
|
|
// exit;
|
|
// } else {
|
|
// logMessage("DB connection failed: " . $mysqli->connect_error);
|
|
// http_response_code(500);
|
|
// echo json_encode(['error' => 'DB connection failed']);
|
|
// exit;
|
|
// }
|
|
// } else {
|
|
// logMessage("Failed to move uploaded file");
|
|
// http_response_code(500);
|
|
// echo json_encode(['error' => 'Failed to save file']);
|
|
// exit;
|
|
// }
|
|
// } else {
|
|
// logMessage("File upload error: " . (isset($_FILES['file']['error']) ? $_FILES['file']['error'] : 'no file'));
|
|
// http_response_code(422);
|
|
// echo json_encode(['error' => 'File upload error']);
|
|
// exit;
|
|
// }
|
|
//}
|
|
|
|
$ch = curl_init();
|
|
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => $api_url,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_CUSTOMREQUEST => $_SERVER['REQUEST_METHOD'],
|
|
CURLOPT_POSTFIELDS => $input_data,
|
|
CURLOPT_HTTPHEADER => $headers,
|
|
CURLOPT_HEADER => false,
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$curlError = curl_error($ch);
|
|
if ($curlError) {
|
|
logMessage("cURL Error: " . $curlError);
|
|
}
|
|
|
|
curl_close($ch);
|
|
|
|
http_response_code($httpCode);
|
|
echo $response; |