From 75e31428a1e8eeb121e2e675ff0b52be69d51932 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Wed, 20 Jan 2021 10:07:27 +0100 Subject: [PATCH] Use safe baraja-core/url. --- composer.json | 3 ++- src/Api.php | 3 ++- src/Helpers.php | 38 ++------------------------------------ 3 files changed, 6 insertions(+), 38 deletions(-) diff --git a/composer.json b/composer.json index cf708ef..019cae3 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "php": ">=7.4.0", "nette/di": "^3.0", "nette/http": "^3.0", - "nette/application": "^3.0" + "nette/application": "^3.0", + "baraja-core/url": "^1.0" }, "require-dev": { "phpstan/phpstan": "^0.12.18", diff --git a/src/Api.php b/src/Api.php index c9a75e6..8aa803a 100644 --- a/src/Api.php +++ b/src/Api.php @@ -6,6 +6,7 @@ use Baraja\AssetsLoader\Minifier\Minifier; +use Baraja\Url\Url; use Nette\Utils\FileSystem; final class Api @@ -141,7 +142,7 @@ private function renderInjectTagsByData(string $route, array $data): array if (isset($this->formatHtmlInjects[$format]) === true) { $return[] = str_replace( '%path%', - Helpers::getBaseUrl() . '/assets/web-loader/' . $route . '.' . $format + Url::get()->getBaseUrl() . '/assets/web-loader/' . $route . '.' . $format . ($topModTime > 0 ? '?v=' . substr(md5((string) $topModTime), 0, 6) : ''), $this->formatHtmlInjects[$format] ); diff --git a/src/Helpers.php b/src/Helpers.php index ad57c00..84fa91d 100644 --- a/src/Helpers.php +++ b/src/Helpers.php @@ -5,6 +5,7 @@ namespace Baraja\AssetsLoader; +use Baraja\Url\Url; use Nette\Http\Request; final class Helpers @@ -23,22 +24,7 @@ public function __construct() */ public static function processPath(Request $httpRequest): string { - return trim(str_replace(rtrim($httpRequest->getUrl()->withoutUserInfo()->getBaseUrl(), '/'), '', (string) self::getCurrentUrl()), '/'); - } - - - /** - * Return current absolute URL. - * Return null, if current URL does not exist (for example in CLI mode). - */ - public static function getCurrentUrl(): ?string - { - if (!isset($_SERVER['REQUEST_URI'], $_SERVER['HTTP_HOST'])) { - return null; - } - - return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') - . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; + return trim(str_replace(rtrim($httpRequest->getUrl()->withoutUserInfo()->getBaseUrl(), '/'), '', Url::get()->getCurrentUrl()), '/'); } @@ -87,24 +73,4 @@ public static function length(string $s): int { return function_exists('mb_strlen') ? mb_strlen($s, 'UTF-8') : strlen(utf8_decode($s)); } - - - public static function getBaseUrl(): ?string - { - static $return; - if ($return === null) { - if (($currentUrl = self::getCurrentUrl()) !== null) { - if (preg_match('/^(https?:\/\/.+)\/www\//', $currentUrl, $localUrlParser)) { - $return = $localUrlParser[0]; - } elseif (preg_match('/^(https?:\/\/[^\/]+)/', $currentUrl, $publicUrlParser)) { - $return = $publicUrlParser[1]; - } - } - if ($return !== null) { - $return = rtrim($return, '/'); - } - } - - return $return; - } }