From 5ef903b19580d8c065d1b13e8b1e5d9135c73529 Mon Sep 17 00:00:00 2001 From: DoarMihai Date: Sun, 30 Mar 2025 23:33:26 +0300 Subject: [PATCH] [#7] finally made the online proxy to work --- app/Services/ProxyService.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/app/Services/ProxyService.php b/app/Services/ProxyService.php index e4a8465..c1e0cbc 100644 --- a/app/Services/ProxyService.php +++ b/app/Services/ProxyService.php @@ -176,6 +176,31 @@ class ProxyService return $this->processCssContent($content, $baseUrl); } + // Process stylesheet links that don't start with http or https + $content = preg_replace_callback( + '/]*rel=["\']stylesheet["\'][^>]*href=["\'](?!https?:\/\/)([^"\']+)["\']/i', + function($matches) use ($baseUrl) { + $url = $matches[1]; + // If the URL starts with /, use the host from baseUrl + if (strpos($url, '/') === 0) { + $parsedUrl = parse_url($baseUrl); + $url = $parsedUrl['scheme'] . '://' . $parsedUrl['host'] . $url; + } else { + // Otherwise, append to the base URL + $url = rtrim($baseUrl, '/') . '/' . ltrim($url, '/'); + } + + // Log the transformation + \Log::info('Stylesheet URL transformed', [ + 'original' => $matches[0], + 'transformed' => $url + ]); + + return str_replace($matches[1], $url, $matches[0]); + }, + $content + ); + // For HTML content, process only anchor tags with external URLs $patterns = [ // External links in href attributes (only for anchor tags) @@ -192,6 +217,16 @@ class ProxyService if (strpos($url, route('proxy.browse')) === 0) { return $matches[0]; } + // Skip URLs that are already proxied + if (strpos($url, route('proxy.browse')) === 0) { + return $matches[0]; + } + + // Proxy all http/https URLs + if (preg_match('/^https?:\/\//i', $url)) { + $url = route('proxy.browse') . '?url=' . base64_encode($url); + return $matches[1] . '="' . htmlspecialchars($url) . '"'; + } // Only proxy external URLs if (strpos($url, 'http') === 0 && strpos($url, parse_url($baseUrl, PHP_URL_HOST)) === false) {