Update Kirby and Composer dependencies
This commit is contained in:
parent
f5d3ea5e84
commit
ec74d78ba9
382 changed files with 25077 additions and 4955 deletions
|
@ -11,7 +11,7 @@ use Kirby\Toolkit\Str;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Cookie
|
||||
|
|
|
@ -8,7 +8,7 @@ namespace Kirby\Http\Exceptions;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class NextRouteException extends \Exception
|
||||
|
|
|
@ -11,7 +11,7 @@ use Kirby\Filesystem\F;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Header
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
namespace Kirby\Http;
|
||||
|
||||
use Kirby\Toolkit\Str;
|
||||
use TrueBV\Punycode;
|
||||
|
||||
/**
|
||||
* Handles Internationalized Domain Names
|
||||
|
@ -11,19 +10,31 @@ use TrueBV\Punycode;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Idn
|
||||
{
|
||||
/**
|
||||
* Convert domain name from IDNA ASCII to Unicode
|
||||
*
|
||||
* @param string $domain
|
||||
* @return string|false
|
||||
*/
|
||||
public static function decode(string $domain)
|
||||
{
|
||||
return (new Punycode())->decode($domain);
|
||||
return idn_to_utf8($domain);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert domain name to IDNA ASCII form
|
||||
*
|
||||
* @param string $domain
|
||||
* @return string|false
|
||||
*/
|
||||
public static function encode(string $domain)
|
||||
{
|
||||
return (new Punycode())->encode($domain);
|
||||
return idn_to_ascii($domain);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -12,7 +12,7 @@ use Kirby\Toolkit\Str;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Params extends Query
|
||||
|
@ -69,10 +69,13 @@ class Params extends Query
|
|||
}
|
||||
|
||||
$paramParts = Str::split($p, $separator);
|
||||
$paramKey = $paramParts[0];
|
||||
$paramKey = $paramParts[0] ?? null;
|
||||
$paramValue = $paramParts[1] ?? null;
|
||||
|
||||
$params[$paramKey] = $paramValue;
|
||||
if ($paramKey !== null) {
|
||||
$params[$paramKey] = $paramValue;
|
||||
}
|
||||
|
||||
unset($path[$index]);
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ use Kirby\Toolkit\Str;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Path extends Collection
|
||||
|
|
|
@ -12,7 +12,7 @@ use Kirby\Toolkit\Obj;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Query extends Obj
|
||||
|
@ -43,7 +43,7 @@ class Query extends Obj
|
|||
|
||||
public function toString($questionMark = false): string
|
||||
{
|
||||
$query = http_build_query($this, null, '&', PHP_QUERY_RFC3986);
|
||||
$query = http_build_query($this, '', '&', PHP_QUERY_RFC3986);
|
||||
|
||||
if (empty($query) === true) {
|
||||
return '';
|
||||
|
|
|
@ -15,13 +15,13 @@ use Kirby\Toolkit\Str;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Remote
|
||||
{
|
||||
const CA_INTERNAL = 1;
|
||||
const CA_SYSTEM = 2;
|
||||
public const CA_INTERNAL = 1;
|
||||
public const CA_SYSTEM = 2;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
|
|
|
@ -18,7 +18,7 @@ use Kirby\Toolkit\Str;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Request
|
||||
|
@ -174,7 +174,7 @@ class Request
|
|||
*/
|
||||
public function body()
|
||||
{
|
||||
return $this->body = $this->body ?? new Body();
|
||||
return $this->body ??= new Body();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -220,7 +220,7 @@ class Request
|
|||
$methods = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH'];
|
||||
|
||||
// the request method can be overwritten with a header
|
||||
$methodOverride = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] ?? null);
|
||||
$methodOverride = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] ?? '');
|
||||
|
||||
if ($method === null && in_array($methodOverride, $methods) === true) {
|
||||
$method = $methodOverride;
|
||||
|
@ -269,7 +269,7 @@ class Request
|
|||
*/
|
||||
public function files()
|
||||
{
|
||||
return $this->files = $this->files ?? new Files();
|
||||
return $this->files ??= new Files();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -379,7 +379,7 @@ class Request
|
|||
*/
|
||||
public function query()
|
||||
{
|
||||
return $this->query = $this->query ?? new Query();
|
||||
return $this->query ??= new Query();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -407,6 +407,6 @@ class Request
|
|||
return $this->url()->clone($props);
|
||||
}
|
||||
|
||||
return $this->url = $this->url ?? Uri::current();
|
||||
return $this->url ??= Uri::current();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ namespace Kirby\Http\Request;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Body
|
||||
|
|
|
@ -13,7 +13,7 @@ namespace Kirby\Http\Request;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
trait Data
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace Kirby\Http\Request;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Files
|
||||
|
|
|
@ -10,7 +10,7 @@ namespace Kirby\Http\Request;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Query
|
||||
|
|
|
@ -14,7 +14,7 @@ use Throwable;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Response
|
||||
|
@ -160,14 +160,14 @@ class Response
|
|||
throw new Exception('The file could not be found');
|
||||
}
|
||||
|
||||
$filename = $filename ?? basename($file);
|
||||
$modified = filemtime($file);
|
||||
$body = file_get_contents($file);
|
||||
$size = strlen($body);
|
||||
$filename ??= basename($file);
|
||||
$modified = filemtime($file);
|
||||
$body = file_get_contents($file);
|
||||
$size = strlen($body);
|
||||
|
||||
$props = array_replace_recursive([
|
||||
'body' => $body,
|
||||
'type' => 'application/force-download',
|
||||
'type' => F::mime($file),
|
||||
'headers' => [
|
||||
'Pragma' => 'public',
|
||||
'Cache-Control' => 'no-cache, no-store, must-revalidate',
|
||||
|
@ -234,7 +234,7 @@ class Response
|
|||
public static function json($body = '', ?int $code = null, ?bool $pretty = null, array $headers = [])
|
||||
{
|
||||
if (is_array($body) === true) {
|
||||
$body = json_encode($body, $pretty === true ? JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES : null);
|
||||
$body = json_encode($body, $pretty === true ? JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES : 0);
|
||||
}
|
||||
|
||||
return new static([
|
||||
|
|
|
@ -8,7 +8,7 @@ use Closure;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Route
|
||||
|
|
|
@ -5,12 +5,13 @@ namespace Kirby\Http;
|
|||
use Closure;
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use Kirby\Toolkit\A;
|
||||
|
||||
/**
|
||||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Router
|
||||
|
@ -59,8 +60,11 @@ class Router
|
|||
throw new InvalidArgumentException('Invalid route parameters');
|
||||
}
|
||||
|
||||
$methods = array_map('trim', explode('|', strtoupper($props['method'] ?? 'GET')));
|
||||
$patterns = is_array($props['pattern']) === false ? [$props['pattern']] : $props['pattern'];
|
||||
$patterns = A::wrap($props['pattern']);
|
||||
$methods = A::map(
|
||||
explode('|', strtoupper($props['method'] ?? 'GET')),
|
||||
'trim'
|
||||
);
|
||||
|
||||
if ($methods === ['ALL']) {
|
||||
$methods = array_keys($this->routes);
|
||||
|
@ -88,7 +92,7 @@ class Router
|
|||
*/
|
||||
public function call(string $path = null, string $method = 'GET', Closure $callback = null)
|
||||
{
|
||||
$path = $path ?? '';
|
||||
$path ??= '';
|
||||
$ignore = [];
|
||||
$result = null;
|
||||
$loop = true;
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Kirby\Http;
|
||||
|
||||
use Kirby\Toolkit\A;
|
||||
|
||||
/**
|
||||
* A set of methods that make it more convenient to get variables
|
||||
* from the global server array
|
||||
|
@ -9,11 +11,15 @@ namespace Kirby\Http;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Server
|
||||
{
|
||||
public const HOST_FROM_SERVER = 1;
|
||||
public const HOST_FROM_HEADER = 2;
|
||||
public const HOST_ALLOW_EMPTY = 4;
|
||||
|
||||
/**
|
||||
* Cache for the cli status
|
||||
*
|
||||
|
@ -21,6 +27,13 @@ class Server
|
|||
*/
|
||||
public static $cli;
|
||||
|
||||
/**
|
||||
* List of trusted hosts
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $hosts = [];
|
||||
|
||||
/**
|
||||
* Returns the server's IP address
|
||||
*
|
||||
|
@ -28,7 +41,7 @@ class Server
|
|||
*/
|
||||
public static function address(): string
|
||||
{
|
||||
return static::get('SERVER_ADDR');
|
||||
return static::get('SERVER_ADDR', '');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -83,6 +96,186 @@ class Server
|
|||
return static::sanitize($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the correct host
|
||||
*
|
||||
* @param bool $forwarded Deprecated. Todo: remove in 3.7.0
|
||||
* @return string
|
||||
*/
|
||||
public static function host(bool $forwarded = false): string
|
||||
{
|
||||
$hosts[] = static::get('SERVER_NAME');
|
||||
$hosts[] = static::get('SERVER_ADDR');
|
||||
|
||||
// insecure host parameters are only allowed when hosts
|
||||
// are validated against set of host patterns
|
||||
if (empty(static::$hosts) === false) {
|
||||
$hosts[] = static::get('HTTP_HOST');
|
||||
$hosts[] = static::get('HTTP_X_FORWARDED_HOST');
|
||||
}
|
||||
|
||||
// remove empty hosts
|
||||
$hosts = array_filter($hosts);
|
||||
|
||||
foreach ($hosts as $host) {
|
||||
if (static::isAllowedHost($host) === true) {
|
||||
return explode(':', $host)[0];
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter and getter for the the static $hosts property
|
||||
*
|
||||
* $hosts = null -> return all defined hosts
|
||||
* $hosts = Server::HOST_FROM_SERVER -> []
|
||||
* $hosts = Server::HOST_FROM_HEADER -> ['*']
|
||||
* $hosts = array -> [array of trusted hosts]
|
||||
* $hosts = string -> [single trusted host]
|
||||
*
|
||||
* @param string|array|int|null $hosts
|
||||
* @return array
|
||||
*/
|
||||
public static function hosts($hosts = null): array
|
||||
{
|
||||
if ($hosts === null) {
|
||||
return static::$hosts;
|
||||
}
|
||||
|
||||
if (is_int($hosts) && $hosts & static::HOST_FROM_SERVER) {
|
||||
return static::$hosts = [];
|
||||
}
|
||||
|
||||
if (is_int($hosts) && $hosts & static::HOST_FROM_HEADER) {
|
||||
return static::$hosts = ['*'];
|
||||
}
|
||||
|
||||
// make sure hosts are always an array
|
||||
$hosts = A::wrap($hosts);
|
||||
|
||||
// return unique hosts
|
||||
return static::$hosts = array_unique($hosts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a https request
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function https(): bool
|
||||
{
|
||||
$https = $_SERVER['HTTPS'] ?? null;
|
||||
$off = ['off', null, '', 0, '0', false, 'false', -1, '-1'];
|
||||
|
||||
// check for various options to send a negative HTTPS header
|
||||
if (in_array($https, $off, true) === false) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// check for the port
|
||||
if (static::port() === 443) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for allowed host names
|
||||
*
|
||||
* @param string $host
|
||||
* @return bool
|
||||
*/
|
||||
public static function isAllowedHost(string $host): bool
|
||||
{
|
||||
if (empty(static::$hosts) === true) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach (static::$hosts as $pattern) {
|
||||
if (empty($pattern) === true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (fnmatch($pattern, $host) === true) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the server is behind a
|
||||
* proxy server.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isBehindProxy(): bool
|
||||
{
|
||||
return empty($_SERVER['HTTP_X_FORWARDED_HOST']) === false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the correct port number
|
||||
*
|
||||
* @param bool $forwarded Deprecated. Todo: remove in 3.7.0
|
||||
* @return int
|
||||
*/
|
||||
public static function port(bool $forwarded = false): int
|
||||
{
|
||||
$port = null;
|
||||
|
||||
// handle reverse proxy setups
|
||||
if (static::isBehindProxy() === true) {
|
||||
// based on forwarded port
|
||||
$port = static::get('HTTP_X_FORWARDED_PORT');
|
||||
|
||||
// based on the forwarded host
|
||||
if (empty($port) === true) {
|
||||
$port = (int)parse_url(static::get('HTTP_X_FORWARDED_HOST'), PHP_URL_PORT);
|
||||
}
|
||||
|
||||
// based on the forwarded proto
|
||||
if (empty($port) === true) {
|
||||
if (in_array($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? null, ['https', 'https, http']) === true) {
|
||||
$port = 443;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// based on the host
|
||||
if (empty($port) === true) {
|
||||
$port = (int)parse_url(static::get('HTTP_HOST'), PHP_URL_PORT);
|
||||
}
|
||||
|
||||
// based on server port
|
||||
if (empty($port) === true) {
|
||||
$port = static::get('SERVER_PORT');
|
||||
}
|
||||
|
||||
return $port ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with path and query
|
||||
* from the REQUEST_URI
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function requestUri(): array
|
||||
{
|
||||
$uri = static::get('REQUEST_URI', '');
|
||||
$uri = parse_url($uri);
|
||||
|
||||
return [
|
||||
'path' => $uri['path'] ?? null,
|
||||
'query' => $uri['query'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Help to sanitize some _SERVER keys
|
||||
*
|
||||
|
@ -97,81 +290,59 @@ class Server
|
|||
case 'SERVER_NAME':
|
||||
case 'HTTP_HOST':
|
||||
case 'HTTP_X_FORWARDED_HOST':
|
||||
$value ??= '';
|
||||
$value = strtolower($value);
|
||||
$value = strip_tags($value);
|
||||
$value = basename($value);
|
||||
$value = preg_replace('![^\w.:-]+!iu', '', $value);
|
||||
$value = htmlspecialchars($value, ENT_COMPAT);
|
||||
$value = trim($value, '-');
|
||||
$value = htmlspecialchars($value);
|
||||
$value = trim($value, '.');
|
||||
break;
|
||||
case 'SERVER_PORT':
|
||||
case 'HTTP_X_FORWARDED_PORT':
|
||||
$value ??= '';
|
||||
$value = (int)(preg_replace('![^0-9]+!', '', $value));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the correct port number
|
||||
* Returns the path to the php script
|
||||
* within the document root without the
|
||||
* filename of the script.
|
||||
*
|
||||
* @param bool $forwarded
|
||||
* @return int
|
||||
*/
|
||||
public static function port(bool $forwarded = false): int
|
||||
{
|
||||
// based on forwarded port
|
||||
if ($forwarded === true) {
|
||||
if ($port = static::get('HTTP_X_FORWARDED_PORT')) {
|
||||
return $port;
|
||||
}
|
||||
}
|
||||
|
||||
// based on HTTP host
|
||||
$host = static::get('HTTP_HOST');
|
||||
if ($pos = strpos($host, ':')) {
|
||||
return (int)substr($host, $pos + 1);
|
||||
}
|
||||
|
||||
// based on server port
|
||||
return static::get('SERVER_PORT');
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a https request
|
||||
* i.e. /subfolder/index.php -> subfolder
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function https(): bool
|
||||
{
|
||||
if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
|
||||
return true;
|
||||
} elseif (static::port() === 443) {
|
||||
return true;
|
||||
} elseif (in_array(static::get('HTTP_X_FORWARDED_PROTO'), ['https', 'https, http'])) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the correct host
|
||||
* This can be used to build the base url
|
||||
* for subfolder installations
|
||||
*
|
||||
* @param bool $forwarded
|
||||
* @return string
|
||||
*/
|
||||
public static function host(bool $forwarded = false): string
|
||||
public static function scriptPath(): string
|
||||
{
|
||||
$host = $forwarded === true ? static::get('HTTP_X_FORWARDED_HOST') : null;
|
||||
|
||||
if (empty($host) === true) {
|
||||
$host = static::get('SERVER_NAME');
|
||||
if (static::cli() === true) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (empty($host) === true) {
|
||||
$host = static::get('SERVER_ADDR');
|
||||
$path = $_SERVER['SCRIPT_NAME'] ?? '';
|
||||
// replace Windows backslashes
|
||||
$path = str_replace('\\', '/', $path);
|
||||
// remove the script
|
||||
$path = dirname($path);
|
||||
// replace those fucking backslashes again
|
||||
$path = str_replace('\\', '/', $path);
|
||||
// remove the leading and trailing slashes
|
||||
$path = trim($path, '/');
|
||||
|
||||
// top-level scripts don't have a path
|
||||
// and dirname() will return '.'
|
||||
if ($path === '.') {
|
||||
$path = '';
|
||||
}
|
||||
|
||||
return explode(':', $host)[0];
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ use Throwable;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Uri
|
||||
|
@ -144,10 +144,10 @@ class Uri
|
|||
|
||||
// parse the path and extract params
|
||||
if (empty($props['path']) === false) {
|
||||
$extract = Params::extract($props['path']);
|
||||
$props['params'] = $props['params'] ?? $extract['params'];
|
||||
$props['path'] = $extract['path'];
|
||||
$props['slash'] = $props['slash'] ?? $extract['slash'];
|
||||
$extract = Params::extract($props['path']);
|
||||
$props['params'] ??= $extract['params'];
|
||||
$props['path'] = $extract['path'];
|
||||
$props['slash'] ??= $extract['slash'];
|
||||
}
|
||||
|
||||
$this->setProperties($this->props = $props);
|
||||
|
@ -237,7 +237,7 @@ class Uri
|
|||
|
||||
/**
|
||||
* @param array $props
|
||||
* @param bool $forwarded
|
||||
* @param bool $forwarded Deprecated! Todo: remove in 3.7.0
|
||||
* @return static
|
||||
*/
|
||||
public static function current(array $props = [], bool $forwarded = false)
|
||||
|
@ -246,16 +246,13 @@ class Uri
|
|||
return static::$current;
|
||||
}
|
||||
|
||||
$uri = Server::get('REQUEST_URI');
|
||||
$uri = preg_replace('!^(http|https)\:\/\/' . Server::get('HTTP_HOST') . '!', '', $uri);
|
||||
$uri = parse_url('http://getkirby.com' . $uri);
|
||||
|
||||
$uri = Server::requestUri();
|
||||
$url = new static(array_merge([
|
||||
'scheme' => Server::https() === true ? 'https' : 'http',
|
||||
'host' => Server::host($forwarded),
|
||||
'port' => Server::port($forwarded),
|
||||
'path' => $uri['path'] ?? null,
|
||||
'query' => $uri['query'] ?? null,
|
||||
'host' => Server::host(),
|
||||
'port' => Server::port(),
|
||||
'path' => $uri['path'],
|
||||
'query' => $uri['query'],
|
||||
], $props));
|
||||
|
||||
return $url;
|
||||
|
@ -331,34 +328,16 @@ class Uri
|
|||
* or any other executed script.
|
||||
*
|
||||
* @param array $props
|
||||
* @param bool $forwarded
|
||||
* @param bool $forwarded Deprecated! Todo: remove in 3.7.0
|
||||
* @return string
|
||||
*/
|
||||
public static function index(array $props = [], bool $forwarded = false)
|
||||
{
|
||||
if (Server::cli() === true) {
|
||||
$path = null;
|
||||
} else {
|
||||
$path = Server::get('SCRIPT_NAME');
|
||||
// replace Windows backslashes
|
||||
$path = str_replace('\\', '/', $path);
|
||||
// remove the script
|
||||
$path = dirname($path);
|
||||
// replace those fucking backslashes again
|
||||
$path = str_replace('\\', '/', $path);
|
||||
// remove the leading and trailing slashes
|
||||
$path = trim($path, '/');
|
||||
}
|
||||
|
||||
if ($path === '.') {
|
||||
$path = null;
|
||||
}
|
||||
|
||||
return static::current(array_merge($props, [
|
||||
'path' => $path,
|
||||
'path' => Server::scriptPath(),
|
||||
'query' => null,
|
||||
'fragment' => null,
|
||||
]), $forwarded);
|
||||
]));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ use Kirby\Toolkit\Str;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Url
|
||||
|
@ -77,13 +77,13 @@ class Url
|
|||
/**
|
||||
* Tries to fix a broken url without protocol
|
||||
*
|
||||
* @param string $url
|
||||
* @param string|null $url
|
||||
* @return string
|
||||
*/
|
||||
public static function fix(string $url = null): string
|
||||
{
|
||||
// make sure to not touch absolute urls
|
||||
return (!preg_match('!^(https|http|ftp)\:\/\/!i', $url)) ? 'http://' . $url : $url;
|
||||
return (!preg_match('!^(https|http|ftp)\:\/\/!i', $url ?? '')) ? 'http://' . $url : $url;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -100,18 +100,18 @@ class Url
|
|||
* Returns the url to the executed script
|
||||
*
|
||||
* @param array $props
|
||||
* @param bool $forwarded
|
||||
* @param bool $forwarded Deprecated! Todo: remove in 3.7.0
|
||||
* @return string
|
||||
*/
|
||||
public static function index(array $props = [], bool $forwarded = false): string
|
||||
{
|
||||
return Uri::index($props, $forwarded)->toString();
|
||||
return Uri::index($props)->toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an URL is absolute
|
||||
*
|
||||
* @param string $url
|
||||
* @param string|null $url
|
||||
* @return bool
|
||||
*/
|
||||
public static function isAbsolute(string $url = null): bool
|
||||
|
@ -120,14 +120,14 @@ class Url
|
|||
// //example.com/uri
|
||||
// http://example.com/uri, https://example.com/uri, ftp://example.com/uri
|
||||
// mailto:example@example.com, geo:49.0158,8.3239?z=11
|
||||
return preg_match('!^(//|[a-z0-9+-.]+://|mailto:|tel:|geo:)!i', $url) === 1;
|
||||
return $url !== null && preg_match('!^(//|[a-z0-9+-.]+://|mailto:|tel:|geo:)!i', $url) === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a relative path into an absolute URL
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $home
|
||||
* @param string|null $path
|
||||
* @param string|null $home
|
||||
* @return string
|
||||
*/
|
||||
public static function makeAbsolute(string $path = null, string $home = null): string
|
||||
|
@ -145,8 +145,8 @@ class Url
|
|||
}
|
||||
|
||||
// build the full url
|
||||
$path = ltrim($path, '/');
|
||||
$home = $home ?? static::home();
|
||||
$path = ltrim($path, '/');
|
||||
$home ??= static::home();
|
||||
|
||||
if (empty($path) === true) {
|
||||
return $home;
|
||||
|
@ -260,6 +260,9 @@ class Url
|
|||
*/
|
||||
public static function to(string $path = null, $options = null): string
|
||||
{
|
||||
// make sure $path is string
|
||||
$path ??= '';
|
||||
|
||||
// keep relative urls
|
||||
if (substr($path, 0, 2) === './' || substr($path, 0, 3) === '../') {
|
||||
return $path;
|
||||
|
|
|
@ -15,7 +15,7 @@ use Kirby\Toolkit\Str;
|
|||
* @package Kirby Http
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link https://getkirby.com
|
||||
* @copyright Bastian Allgeier GmbH
|
||||
* @copyright Bastian Allgeier
|
||||
* @license https://opensource.org/licenses/MIT
|
||||
*/
|
||||
class Visitor
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue