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 36383fd commit 97f14ee
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 38 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -14,6 +14,7 @@
"ext-mbstring": "*",
"ext-iconv": "*",
"baraja-core/service-method-invoker": "^1.0",
"baraja-core/url": "^1.0",
"nette/di": "^3.0",
"nette/utils": "^3.2",
"nette/http": "^3.1",
Expand Down
40 changes: 2 additions & 38 deletions src/Helpers.php
Expand Up @@ -5,6 +5,7 @@
namespace Baraja\StructuredApi;


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

Expand All @@ -24,44 +25,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'];
}


public static function getBaseUrl(): ?string
{
static $return;

if ($return !== null) {
return $return;
}
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;
return trim(str_replace(rtrim($httpRequest->getUrl()->withoutUserInfo()->getBaseUrl(), '/'), '', Url::get()->getCurrentUrl()), '/');
}


Expand Down

0 comments on commit 97f14ee

Please sign in to comment.