2018-04-18 19:03:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Grocy\Helpers;
|
|
|
|
|
|
|
|
class UrlManager
|
|
|
|
{
|
2018-04-21 19:18:00 +02:00
|
|
|
public function __construct(string $basePath)
|
|
|
|
{
|
|
|
|
if ($basePath === '/')
|
|
|
|
{
|
|
|
|
$this->BasePath = $this->GetBaseUrl();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->BasePath = $basePath;
|
|
|
|
}
|
2018-04-18 19:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected $BasePath;
|
|
|
|
|
2018-06-15 20:50:40 +02:00
|
|
|
public function ConstructUrl($relativePath, $isResource = false)
|
2018-04-18 19:03:39 +02:00
|
|
|
{
|
2018-07-24 19:41:35 +02:00
|
|
|
if (GROCY_DISABLE_URL_REWRITING === false || $isResource === true)
|
2018-06-15 20:50:40 +02:00
|
|
|
{
|
|
|
|
return rtrim($this->BasePath, '/') . $relativePath;
|
|
|
|
}
|
|
|
|
else // Is not a resource and URL rewriting is disabled
|
|
|
|
{
|
|
|
|
return rtrim($this->BasePath, '/') . '/index.php' . $relativePath;
|
|
|
|
}
|
2018-04-18 19:03:39 +02:00
|
|
|
}
|
2018-04-21 19:18:00 +02:00
|
|
|
|
|
|
|
private function GetBaseUrl()
|
|
|
|
{
|
2018-09-21 12:49:01 +02:00
|
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
|
|
|
|
{
|
|
|
|
$_SERVER['HTTPS'] = 'on';
|
|
|
|
}
|
|
|
|
|
2018-04-21 19:18:00 +02:00
|
|
|
return (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
|
|
|
|
}
|
2018-04-18 19:03:39 +02:00
|
|
|
}
|