Skip to content

Commit

Permalink
Use safe baraja-core/url.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Jan 20, 2021
1 parent 3255325 commit 75e3142
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 38 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


use Baraja\AssetsLoader\Minifier\Minifier;
use Baraja\Url\Url;
use Nette\Utils\FileSystem;

final class Api
Expand Down Expand Up @@ -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]
);
Expand Down
38 changes: 2 additions & 36 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Baraja\AssetsLoader;


use Baraja\Url\Url;
use Nette\Http\Request;

final class Helpers
Expand All @@ -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()), '/');
}


Expand Down Expand Up @@ -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;
}
}

0 comments on commit 75e3142

Please sign in to comment.