Files
html/api/twenty-proxy.php
2026-04-12 22:57:03 +02:00

69 lines
2.1 KiB
PHP

<?php
// Twenty CRM PHP Proxy — rewrites asset paths for /crm/ subpath
require_once '/var/www/html/api/auth-check.php';
$path = $_SERVER['REQUEST_URI'];
$path = preg_replace('#^/crm/?#', '/', $path);
if ($path === '') $path = '/';
$url = "http://127.0.0.1:3000" . $path;
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 15,
CURLOPT_HTTPHEADER => [
'Host: localhost',
'X-Forwarded-For: ' . ($_SERVER['REMOTE_ADDR'] ?? ''),
'X-Forwarded-Proto: https',
],
]);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('php://input'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge([
'Content-Type: ' . ($_SERVER['CONTENT_TYPE'] ?? 'application/json'),
]));
}
$response = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
$headers = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
// Set response code
http_response_code($httpCode);
// Forward content type
if ($contentType) {
header("Content-Type: " . $contentType);
}
// Rewrite paths in HTML responses
if (strpos($contentType, 'text/html') !== false) {
$body = str_replace('href="/', 'href="/crm/', $body);
$body = str_replace('src="/', 'src="/crm/', $body);
$body = str_replace("href='/", "href='/crm/", $body);
$body = str_replace("src='/", "src='/crm/", $body);
// Fix double-rewrite for external URLs
$body = str_replace('/crm/crm/', '/crm/', $body);
// Fix absolute https URLs that shouldn't be rewritten
$body = str_replace('href="/crm//', 'href="//', $body);
}
// Rewrite paths in JS responses
if (strpos($contentType, 'javascript') !== false) {
$body = str_replace('"/api/', '"/crm/api/', $body);
$body = str_replace("'/api/", "'/crm/api/", $body);
$body = str_replace('"/images/', '"/crm/images/', $body);
}
echo $body;