Skip to content

Commit 889250d

Browse files
committed
Use safe baraja-core/url.
1 parent ef9a0ee commit 889250d

File tree

3 files changed

+4
-50
lines changed

3 files changed

+4
-50
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"nette/utils": "^3.2",
1515
"nette/application": "^3.1",
1616
"nette/di": "^3.0",
17-
"league/commonmark": "^1.5"
17+
"league/commonmark": "^1.5",
18+
"baraja-core/url": "^1.0"
1819
},
1920
"require-dev": {
2021
"phpstan/phpstan": "^0.12.18",

src/BaseRenderer.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Baraja\Markdown;
66

77

8+
use Baraja\Url\Url;
89
use Nette\Application\LinkGenerator;
910
use Nette\Application\UI\InvalidLinkException;
1011
use Nette\Localization\Translator;
@@ -120,18 +121,7 @@ function (array $match): string {
120121

121122
protected function resolveBaseUrl(): string
122123
{
123-
if ($this->baseUrl === null) {
124-
if (($baseUrl = Helpers::getBaseUrl()) === null) {
125-
throw new \LogicException(
126-
'BaseUrl can not be null.' . "\n"
127-
. 'To solve this issue: BaseUrl is automatically detected according to the current HTTP request. '
128-
. 'In CLI mode (when there is no HTTP request), you need to manually define the BaseUrl by the setBaseUrl() method.'
129-
);
130-
}
131-
$this->baseUrl = $baseUrl;
132-
}
133-
134-
return $this->baseUrl;
124+
return $this->baseUrl ?? Url::get()->getBaseUrl();
135125
}
136126

137127

src/Helpers.php

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,6 @@ public function __construct()
1515
}
1616

1717

18-
/**
19-
* Return current absolute URL.
20-
* Return null, if current URL does not exist (for example in CLI mode).
21-
*/
22-
public static function getCurrentUrl(): ?string
23-
{
24-
if (!isset($_SERVER['REQUEST_URI'], $_SERVER['HTTP_HOST'])) {
25-
return null;
26-
}
27-
28-
return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http')
29-
. '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
30-
}
31-
32-
33-
public static function getBaseUrl(): ?string
34-
{
35-
static $return;
36-
37-
if ($return !== null) {
38-
return $return;
39-
}
40-
if (($currentUrl = self::getCurrentUrl()) !== null) {
41-
if (preg_match('/^(https?:\/\/.+)\/www\//', $currentUrl, $localUrlParser)) {
42-
$return = $localUrlParser[0];
43-
} elseif (preg_match('/^(https?:\/\/[^\/]+)/', $currentUrl, $publicUrlParser)) {
44-
$return = $publicUrlParser[1];
45-
}
46-
}
47-
if ($return !== null) {
48-
$return = rtrim($return, '/');
49-
}
50-
51-
return $return;
52-
}
53-
54-
5518
/**
5619
* Sanitizes string for use inside href attribute.
5720
*/

0 commit comments

Comments
 (0)