12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376 |
- <?php
- /**
- * @package Grav.Common
- *
- * @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
- * @license MIT License; see LICENSE file for details.
- */
- namespace Grav\Common;
- use Grav\Common\Config\Config;
- use Grav\Common\Language\Language;
- use Grav\Common\Page\Page;
- use Grav\Common\Page\Pages;
- use Grav\Framework\Route\RouteFactory;
- use Grav\Framework\Uri\UriFactory;
- use Grav\Framework\Uri\UriPartsFilter;
- class Uri
- {
- const HOSTNAME_REGEX = '/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/';
- /** @var \Grav\Framework\Uri\Uri */
- protected static $currentUri;
- /** @var \Grav\Framework\Route\Route */
- protected static $currentRoute;
- public $url;
- // Uri parts.
- protected $scheme;
- protected $user;
- protected $password;
- protected $host;
- protected $port;
- protected $path;
- protected $query;
- protected $fragment;
- // Internal stuff.
- protected $base;
- protected $basename;
- protected $content_path;
- protected $extension;
- protected $env;
- protected $paths;
- protected $queries;
- protected $params;
- protected $root;
- protected $root_path;
- protected $uri;
- protected $content_type;
- protected $post;
- /**
- * Uri constructor.
- * @param string|array $env
- */
- public function __construct($env = null)
- {
- if (is_string($env)) {
- $this->createFromString($env);
- } else {
- $this->createFromEnvironment(is_array($env) ? $env : $_SERVER);
- }
- }
- /**
- * Initialize the URI class with a url passed via parameter.
- * Used for testing purposes.
- *
- * @param string $url the URL to use in the class
- *
- * @return $this
- */
- public function initializeWithUrl($url = '')
- {
- if ($url) {
- $this->createFromString($url);
- }
- return $this;
- }
- /**
- * Initialize the URI class by providing url and root_path arguments
- *
- * @param string $url
- * @param string $root_path
- *
- * @return $this
- */
- public function initializeWithUrlAndRootPath($url, $root_path)
- {
- $this->initializeWithUrl($url);
- $this->root_path = $root_path;
- return $this;
- }
- /**
- * Validate a hostname
- *
- * @param string $hostname The hostname
- *
- * @return boolean
- */
- public function validateHostname($hostname)
- {
- return (bool)preg_match(static::HOSTNAME_REGEX, $hostname);
- }
- /**
- * Initializes the URI object based on the url set on the object
- */
- public function init()
- {
- $grav = Grav::instance();
- /** @var Config $config */
- $config = $grav['config'];
- /** @var Language $language */
- $language = $grav['language'];
- // add the port to the base for non-standard ports
- if ($this->port !== null && $config->get('system.reverse_proxy_setup') === false) {
- $this->base .= ':' . (string)$this->port;
- }
- // Handle custom base
- $custom_base = rtrim($grav['config']->get('system.custom_base_url'), '/');
- if ($custom_base) {
- $custom_parts = parse_url($custom_base);
- $orig_root_path = $this->root_path;
- $this->root_path = isset($custom_parts['path']) ? rtrim($custom_parts['path'], '/') : '';
- if (isset($custom_parts['scheme'])) {
- $this->base = $custom_parts['scheme'] . '://' . $custom_parts['host'];
- $this->root = $custom_base;
- } else {
- $this->root = $this->base . $this->root_path;
- }
- $this->uri = Utils::replaceFirstOccurrence($orig_root_path, $this->root_path, $this->uri);
- } else {
- $this->root = $this->base . $this->root_path;
- }
- $this->url = $this->base . $this->uri;
- $uri = str_replace(static::filterPath($this->root), '', $this->url);
- // remove the setup.php based base if set:
- $setup_base = $grav['pages']->base();
- if ($setup_base) {
- $uri = preg_replace('|^' . preg_quote($setup_base, '|') . '|', '', $uri);
- }
- // process params
- $uri = $this->processParams($uri, $config->get('system.param_sep'));
- // set active language
- $uri = $language->setActiveFromUri($uri);
- // split the URL and params
- $bits = parse_url($uri);
- //process fragment
- if (isset($bits['fragment'])) {
- $this->fragment = $bits['fragment'];
- }
- // Get the path. If there's no path, make sure pathinfo() still returns dirname variable
- $path = isset($bits['path']) ? $bits['path'] : '/';
- // remove the extension if there is one set
- $parts = pathinfo($path);
- // set the original basename
- $this->basename = $parts['basename'];
- // set the extension
- if (isset($parts['extension'])) {
- $this->extension = $parts['extension'];
- }
- $valid_page_types = implode('|', $config->get('system.pages.types'));
- // Strip the file extension for valid page types
- if (preg_match('/\.(' . $valid_page_types . ')$/', $parts['basename'])) {
- $path = rtrim(str_replace(DIRECTORY_SEPARATOR, DS, $parts['dirname']), DS) . '/' . $parts['filename'];
- }
- // set the new url
- $this->url = $this->root . $path;
- $this->path = static::cleanPath($path);
- $this->content_path = trim(str_replace($this->base, '', $this->path), '/');
- if ($this->content_path !== '') {
- $this->paths = explode('/', $this->content_path);
- }
- // Set some Grav stuff
- $grav['base_url_absolute'] = $config->get('system.custom_base_url') ?: $this->rootUrl(true);
- $grav['base_url_relative'] = $this->rootUrl(false);
- $grav['base_url'] = $config->get('system.absolute_urls') ? $grav['base_url_absolute'] : $grav['base_url_relative'];
- RouteFactory::setRoot($this->root_path);
- RouteFactory::setLanguage($language->getLanguageURLPrefix());
- }
- /**
- * Return URI path.
- *
- * @param string $id
- *
- * @return string|string[]
- */
- public function paths($id = null)
- {
- if ($id !== null) {
- return $this->paths[$id];
- }
- return $this->paths;
- }
- /**
- * Return route to the current URI. By default route doesn't include base path.
- *
- * @param bool $absolute True to include full path.
- * @param bool $domain True to include domain. Works only if first parameter is also true.
- *
- * @return string
- */
- public function route($absolute = false, $domain = false)
- {
- return ($absolute ? $this->rootUrl($domain) : '') . '/' . implode('/', $this->paths);
- }
- /**
- * Return full query string or a single query attribute.
- *
- * @param string $id Optional attribute. Get a single query attribute if set
- * @param bool $raw If true and $id is not set, return the full query array. Otherwise return the query string
- *
- * @return string|array Returns an array if $id = null and $raw = true
- */
- public function query($id = null, $raw = false)
- {
- if ($id !== null) {
- return isset($this->queries[$id]) ? $this->queries[$id] : null;
- }
- if ($raw) {
- return $this->queries;
- }
- if (!$this->queries) {
- return '';
- }
- return http_build_query($this->queries);
- }
- /**
- * Return all or a single query parameter as a URI compatible string.
- *
- * @param string $id Optional parameter name.
- * @param boolean $array return the array format or not
- *
- * @return null|string|array
- */
- public function params($id = null, $array = false)
- {
- $config = Grav::instance()['config'];
- $sep = $config->get('system.param_sep');
- $params = null;
- if ($id === null) {
- if ($array) {
- return $this->params;
- }
- $output = [];
- foreach ($this->params as $key => $value) {
- $output[] = "{$key}{$sep}{$value}";
- $params = '/' . implode('/', $output);
- }
- } elseif (isset($this->params[$id])) {
- if ($array) {
- return $this->params[$id];
- }
- $params = "/{$id}{$sep}{$this->params[$id]}";
- }
- return $params;
- }
- /**
- * Get URI parameter.
- *
- * @param string $id
- *
- * @return bool|string
- */
- public function param($id)
- {
- if (isset($this->params[$id])) {
- return html_entity_decode(rawurldecode($this->params[$id]));
- }
- return false;
- }
- /**
- * Gets the Fragment portion of a URI (eg #target)
- *
- * @param string $fragment
- *
- * @return string|null
- */
- public function fragment($fragment = null)
- {
- if ($fragment !== null) {
- $this->fragment = $fragment;
- }
- return $this->fragment;
- }
- /**
- * Return URL.
- *
- * @param bool $include_host Include hostname.
- *
- * @return string
- */
- public function url($include_host = false)
- {
- if ($include_host) {
- return $this->url;
- }
- $url = str_replace($this->base, '', rtrim($this->url, '/'));
- return $url ?: '/';
- }
- /**
- * Return the Path
- *
- * @return String The path of the URI
- */
- public function path()
- {
- return $this->path;
- }
- /**
- * Return the Extension of the URI
- *
- * @param string|null $default
- *
- * @return string The extension of the URI
- */
- public function extension($default = null)
- {
- if (!$this->extension) {
- $this->extension = $default;
- }
- return $this->extension;
- }
- public function method()
- {
- $method = isset($_SERVER['REQUEST_METHOD']) ? strtoupper($_SERVER['REQUEST_METHOD']) : 'GET';
- if ($method === 'POST' && isset($_SERVER['X-HTTP-METHOD-OVERRIDE'])) {
- $method = strtoupper($_SERVER['X-HTTP-METHOD-OVERRIDE']);
- }
- return $method;
- }
- /**
- * Return the scheme of the URI
- *
- * @param bool $raw
- * @return string The scheme of the URI
- */
- public function scheme($raw = false)
- {
- if (!$raw) {
- $scheme = '';
- if ($this->scheme) {
- $scheme = $this->scheme . '://';
- } elseif ($this->host) {
- $scheme = '//';
- }
- return $scheme;
- }
- return $this->scheme;
- }
- /**
- * Return the host of the URI
- *
- * @return string|null The host of the URI
- */
- public function host()
- {
- return $this->host;
- }
- /**
- * Return the port number if it can be figured out
- *
- * @param bool $raw
- * @return int|null
- */
- public function port($raw = false)
- {
- $port = $this->port;
- // If not in raw mode and port is not set, figure it out from scheme.
- if (!$raw && $port === null) {
- if ($this->scheme === 'http') {
- $this->port = 80;
- } elseif ($this->scheme === 'https') {
- $this->port = 443;
- }
- }
- return $this->port;
- }
- /**
- * Return user
- *
- * @return string|null
- */
- public function user()
- {
- return $this->user;
- }
- /**
- * Return password
- *
- * @return string|null
- */
- public function password()
- {
- return $this->password;
- }
- /**
- * Gets the environment name
- *
- * @return String
- */
- public function environment()
- {
- return $this->env;
- }
- /**
- * Return the basename of the URI
- *
- * @return String The basename of the URI
- */
- public function basename()
- {
- return $this->basename;
- }
- /**
- * Return the full uri
- *
- * @param bool $include_root
- * @return mixed
- */
- public function uri($include_root = true)
- {
- if ($include_root) {
- return $this->uri;
- }
- return str_replace($this->root_path, '', $this->uri);
- }
- /**
- * Return the base of the URI
- *
- * @return String The base of the URI
- */
- public function base()
- {
- return $this->base;
- }
- /**
- * Return the base relative URL including the language prefix
- * or the base relative url if multi-language is not enabled
- *
- * @return String The base of the URI
- */
- public function baseIncludingLanguage()
- {
- $grav = Grav::instance();
- /** @var Pages $pages */
- $pages = $grav['pages'];
- return $pages->baseUrl(null, false);
- }
- /**
- * Return root URL to the site.
- *
- * @param bool $include_host Include hostname.
- *
- * @return mixed
- */
- public function rootUrl($include_host = false)
- {
- if ($include_host) {
- return $this->root;
- }
- return str_replace($this->base, '', $this->root);
- }
- /**
- * Return current page number.
- *
- * @return int
- */
- public function currentPage()
- {
- return isset($this->params['page']) ? $this->params['page'] : 1;
- }
- /**
- * Return relative path to the referrer defaulting to current or given page.
- *
- * @param string $default
- * @param string $attributes
- *
- * @return string
- */
- public function referrer($default = null, $attributes = null)
- {
- $referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
- // Check that referrer came from our site.
- $root = $this->rootUrl(true);
- if ($referrer) {
- // Referrer should always have host set and it should come from the same base address.
- if (stripos($referrer, $root) !== 0) {
- $referrer = null;
- }
- }
- if (!$referrer) {
- $referrer = $default ?: $this->route(true, true);
- }
- if ($attributes) {
- $referrer .= $attributes;
- }
- // Return relative path.
- return substr($referrer, strlen($root));
- }
- public function __toString()
- {
- return static::buildUrl($this->toArray());
- }
- public function toArray()
- {
- return [
- 'scheme' => $this->scheme,
- 'host' => $this->host,
- 'port' => $this->port,
- 'user' => $this->user,
- 'pass' => $this->password,
- 'path' => $this->path,
- 'params' => $this->params,
- 'query' => $this->query,
- 'fragment' => $this->fragment
- ];
- }
- /**
- * Calculate the parameter regex based on the param_sep setting
- *
- * @return string
- */
- public static function paramsRegex()
- {
- return '/\/([^\:\#\/\?]*' . Grav::instance()['config']->get('system.param_sep') . '[^\:\#\/\?]*)/';
- }
- /**
- * Return the IP address of the current user
- *
- * @return string ip address
- */
- public static function ip()
- {
- if (getenv('HTTP_CLIENT_IP')) {
- $ip = getenv('HTTP_CLIENT_IP');
- } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
- $ip = getenv('HTTP_X_FORWARDED_FOR');
- } elseif (getenv('HTTP_X_FORWARDED')) {
- $ip = getenv('HTTP_X_FORWARDED');
- } elseif (getenv('HTTP_FORWARDED_FOR')) {
- $ip = getenv('HTTP_FORWARDED_FOR');
- } elseif (getenv('HTTP_FORWARDED')) {
- $ip = getenv('HTTP_FORWARDED');
- } elseif (getenv('REMOTE_ADDR')){
- $ip = getenv('REMOTE_ADDR');
- } else {
- $ip = 'UNKNOWN';
- }
- return $ip;
- }
- /**
- * Returns current Uri.
- *
- * @return \Grav\Framework\Uri\Uri
- */
- public static function getCurrentUri()
- {
- if (!static::$currentUri) {
- static::$currentUri = UriFactory::createFromEnvironment($_SERVER);
- }
- return static::$currentUri;
- }
- /**
- * Returns current route.
- *
- * @return \Grav\Framework\Route\Route
- */
- public static function getCurrentRoute()
- {
- if (!static::$currentRoute) {
- $uri = Grav::instance()['uri'];
- static::$currentRoute = RouteFactory::createFromParts($uri->toArray());
- }
- return static::$currentRoute;
- }
- /**
- * Is this an external URL? if it starts with `http` then yes, else false
- *
- * @param string $url the URL in question
- *
- * @return boolean is eternal state
- */
- public static function isExternal($url)
- {
- return Utils::startsWith($url, 'http');
- }
- /**
- * The opposite of built-in PHP method parse_url()
- *
- * @param array $parsed_url
- *
- * @return string
- */
- public static function buildUrl($parsed_url)
- {
- $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . ':' : '';
- $authority = isset($parsed_url['host']) ? '//' : '';
- $host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
- $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
- $user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
- $pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
- $pass = ($user || $pass) ? "{$pass}@" : '';
- $path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
- $path = !empty($parsed_url['params']) ? rtrim($path, '/') . static::buildParams($parsed_url['params']) : $path;
- $query = !empty($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
- $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
- return "{$scheme}{$authority}{$user}{$pass}{$host}{$port}{$path}{$query}{$fragment}";
- }
- /**
- * @param array $params
- * @return string
- */
- public static function buildParams(array $params)
- {
- if (!$params) {
- return '';
- }
- $grav = Grav::instance();
- $sep = $grav['config']->get('system.param_sep');
- $output = [];
- foreach ($params as $key => $value) {
- $output[] = "{$key}{$sep}{$value}";
- }
- return '/' . implode('/', $output);
- }
- /**
- * Converts links from absolute '/' or relative (../..) to a Grav friendly format
- *
- * @param Page $page the current page to use as reference
- * @param string|array $url the URL as it was written in the markdown
- * @param string $type the type of URL, image | link
- * @param bool $absolute if null, will use system default, if true will use absolute links internally
- * @param bool $route_only only return the route, not full URL path
- * @return string the more friendly formatted url
- */
- public static function convertUrl(Page $page, $url, $type = 'link', $absolute = false, $route_only = false)
- {
- $grav = Grav::instance();
- $uri = $grav['uri'];
- // Link processing should prepend language
- $language = $grav['language'];
- $language_append = '';
- if ($type === 'link' && $language->enabled()) {
- $language_append = $language->getLanguageURLPrefix();
- }
- // Handle Excerpt style $url array
- $url_path = is_array($url) ? $url['path'] : $url;
- $external = false;
- $base = $grav['base_url_relative'];
- $base_url = rtrim($base . $grav['pages']->base(), '/') . $language_append;
- $pages_dir = $grav['locator']->findResource('page://');
- // if absolute and starts with a base_url move on
- if (isset($url['scheme']) && Utils::startsWith($url['scheme'], 'http')) {
- $external = true;
- } elseif ($url_path === '' && isset($url['fragment'])) {
- $external = true;
- } elseif ($url_path === '/' || ($base_url !== '' && Utils::startsWith($url_path, $base_url))) {
- $url_path = $base_url . $url_path;
- } else {
- // see if page is relative to this or absolute
- if (Utils::startsWith($url_path, '/')) {
- $normalized_url = Utils::normalizePath($base_url . $url_path);
- $normalized_path = Utils::normalizePath($pages_dir . $url_path);
- } else {
- $page_route = ($page->home() && !empty($url_path)) ? $page->rawRoute() : $page->route();
- $normalized_url = $base_url . Utils::normalizePath($page_route . '/' . $url_path);
- $normalized_path = Utils::normalizePath($page->path() . '/' . $url_path);
- }
- // special check to see if path checking is required.
- $just_path = str_replace($normalized_url, '', $normalized_path);
- if ($normalized_url === '/' || $just_path === $page->path()) {
- $url_path = $normalized_url;
- } else {
- $url_bits = static::parseUrl($normalized_path);
- $full_path = $url_bits['path'];
- $raw_full_path = rawurldecode($full_path);
- if (file_exists($raw_full_path)) {
- $full_path = $raw_full_path;
- } elseif (!file_exists($full_path)) {
- $full_path = false;
- }
- if ($full_path) {
- $path_info = pathinfo($full_path);
- $page_path = $path_info['dirname'];
- $filename = '';
- if ($url_path === '..') {
- $page_path = $full_path;
- } else {
- // save the filename if a file is part of the path
- if (is_file($full_path)) {
- if ($path_info['extension'] !== 'md') {
- $filename = '/' . $path_info['basename'];
- }
- } else {
- $page_path = $full_path;
- }
- }
- // get page instances and try to find one that fits
- $instances = $grav['pages']->instances();
- if (isset($instances[$page_path])) {
- /** @var Page $target */
- $target = $instances[$page_path];
- $url_bits['path'] = $base_url . rtrim($target->route(), '/') . $filename;
- $url_path = Uri::buildUrl($url_bits);
- } else {
- $url_path = $normalized_url;
- }
- } else {
- $url_path = $normalized_url;
- }
- }
- }
- // handle absolute URLs
- if (is_array($url) && !$external && ($absolute === true || $grav['config']->get('system.absolute_urls', false))) {
- $url['scheme'] = $uri->scheme(true);
- $url['host'] = $uri->host();
- $url['port'] = $uri->port(true);
- // check if page exists for this route, and if so, check if it has SSL enabled
- $pages = $grav['pages'];
- $routes = $pages->routes();
- // if this is an image, get the proper path
- $url_bits = pathinfo($url_path);
- if (isset($url_bits['extension'])) {
- $target_path = $url_bits['dirname'];
- } else {
- $target_path = $url_path;
- }
- // strip base from this path
- $target_path = str_replace($uri->rootUrl(), '', $target_path);
- // set to / if root
- if (empty($target_path)) {
- $target_path = '/';
- }
- // look to see if this page exists and has ssl enabled
- if (isset($routes[$target_path])) {
- $target_page = $pages->get($routes[$target_path]);
- if ($target_page) {
- $ssl_enabled = $target_page->ssl();
- if ($ssl_enabled !== null) {
- if ($ssl_enabled) {
- $url['scheme'] = 'https';
- } else {
- $url['scheme'] = 'http';
- }
- }
- }
- }
- }
- // Handle route only
- if ($route_only) {
- $url_path = str_replace(static::filterPath($base_url), '', $url_path);
- }
- // transform back to string/array as needed
- if (is_array($url)) {
- $url['path'] = $url_path;
- } else {
- $url = $url_path;
- }
- return $url;
- }
- public static function parseUrl($url)
- {
- $grav = Grav::instance();
- $encodedUrl = preg_replace_callback(
- '%[^:/@?&=#]+%usD',
- function ($matches) { return rawurlencode($matches[0]); },
- $url
- );
- $parts = parse_url($encodedUrl);
- if (false === $parts) {
- return false;
- }
- foreach($parts as $name => $value) {
- $parts[$name] = rawurldecode($value);
- }
- if (!isset($parts['path'])) {
- $parts['path'] = '';
- }
- list($stripped_path, $params) = static::extractParams($parts['path'], $grav['config']->get('system.param_sep'));
- if (!empty($params)) {
- $parts['path'] = $stripped_path;
- $parts['params'] = $params;
- }
- return $parts;
- }
- public static function extractParams($uri, $delimiter)
- {
- $params = [];
- if (strpos($uri, $delimiter) !== false) {
- preg_match_all(static::paramsRegex(), $uri, $matches, PREG_SET_ORDER);
- foreach ($matches as $match) {
- $param = explode($delimiter, $match[1]);
- if (count($param) === 2) {
- $plain_var = filter_var(rawurldecode($param[1]), FILTER_SANITIZE_STRING);
- $params[$param[0]] = $plain_var;
- $uri = str_replace($match[0], '', $uri);
- }
- }
- }
- return [$uri, $params];
- }
- /**
- * Converts links from absolute '/' or relative (../..) to a Grav friendly format
- *
- * @param Page $page the current page to use as reference
- * @param string $markdown_url the URL as it was written in the markdown
- * @param string $type the type of URL, image | link
- * @param null $relative if null, will use system default, if true will use relative links internally
- *
- * @return string the more friendly formatted url
- */
- public static function convertUrlOld(Page $page, $markdown_url, $type = 'link', $relative = null)
- {
- $grav = Grav::instance();
- $language = $grav['language'];
- // Link processing should prepend language
- $language_append = '';
- if ($type === 'link' && $language->enabled()) {
- $language_append = $language->getLanguageURLPrefix();
- }
- $pages_dir = $grav['locator']->findResource('page://');
- if ($relative === null) {
- $base = $grav['base_url'];
- } else {
- $base = $relative ? $grav['base_url_relative'] : $grav['base_url_absolute'];
- }
- $base_url = rtrim($base . $grav['pages']->base(), '/') . $language_append;
- // if absolute and starts with a base_url move on
- if (pathinfo($markdown_url, PATHINFO_DIRNAME) === '.' && $page->url() === '/') {
- return '/' . $markdown_url;
- }
- // no path to convert
- if ($base_url !== '' && Utils::startsWith($markdown_url, $base_url)) {
- return $markdown_url;
- }
- // if contains only a fragment
- if (Utils::startsWith($markdown_url, '#')) {
- return $markdown_url;
- }
- $target = null;
- // see if page is relative to this or absolute
- if (Utils::startsWith($markdown_url, '/')) {
- $normalized_url = Utils::normalizePath($base_url . $markdown_url);
- $normalized_path = Utils::normalizePath($pages_dir . $markdown_url);
- } else {
- $normalized_url = $base_url . Utils::normalizePath($page->route() . '/' . $markdown_url);
- $normalized_path = Utils::normalizePath($page->path() . '/' . $markdown_url);
- }
- // special check to see if path checking is required.
- $just_path = str_replace($normalized_url, '', $normalized_path);
- if ($just_path === $page->path()) {
- return $normalized_url;
- }
- $url_bits = parse_url($normalized_path);
- $full_path = $url_bits['path'];
- if (file_exists($full_path)) {
- // do nothing
- } elseif (file_exists(rawurldecode($full_path))) {
- $full_path = rawurldecode($full_path);
- } else {
- return $normalized_url;
- }
- $path_info = pathinfo($full_path);
- $page_path = $path_info['dirname'];
- $filename = '';
- if ($markdown_url === '..') {
- $page_path = $full_path;
- } else {
- // save the filename if a file is part of the path
- if (is_file($full_path)) {
- if ($path_info['extension'] !== 'md') {
- $filename = '/' . $path_info['basename'];
- }
- } else {
- $page_path = $full_path;
- }
- }
- // get page instances and try to find one that fits
- $instances = $grav['pages']->instances();
- if (isset($instances[$page_path])) {
- /** @var Page $target */
- $target = $instances[$page_path];
- $url_bits['path'] = $base_url . rtrim($target->route(), '/') . $filename;
- return static::buildUrl($url_bits);
- }
- return $normalized_url;
- }
- /**
- * Adds the nonce to a URL for a specific action
- *
- * @param string $url the url
- * @param string $action the action
- * @param string $nonceParamName the param name to use
- *
- * @return string the url with the nonce
- */
- public static function addNonce($url, $action, $nonceParamName = 'nonce')
- {
- $fake = $url && $url[0] === '/';
- if ($fake) {
- $url = 'http://domain.com' . $url;
- }
- $uri = new static($url);
- $parts = $uri->toArray();
- $nonce = Utils::getNonce($action);
- $parts['params'] = (isset($parts['params']) ? $parts['params'] : []) + [$nonceParamName => $nonce];
- if ($fake) {
- unset($parts['scheme'], $parts['host']);
- }
- return static::buildUrl($parts);
- }
- /**
- * Is the passed in URL a valid URL?
- *
- * @param $url
- * @return bool
- */
- public static function isValidUrl($url)
- {
- $regex = '/^(?:(https?|ftp|telnet):)?\/\/((?:[a-z0-9@:.-]|%[0-9A-F]{2}){3,})(?::(\d+))?((?:\/(?:[a-z0-9-._~!$&\'\(\)\*\+\,\;\=\:\@]|%[0-9A-F]{2})*)*)(?:\?((?:[a-z0-9-._~!$&\'\(\)\*\+\,\;\=\:\/?@]|%[0-9A-F]{2})*))?(?:#((?:[a-z0-9-._~!$&\'\(\)\*\+\,\;\=\:\/?@]|%[0-9A-F]{2})*))?/';
- if (preg_match($regex, $url)) {
- return true;
- }
- return false;
- }
- /**
- * Removes extra double slashes and fixes back-slashes
- *
- * @param $path
- * @return mixed|string
- */
- public static function cleanPath($path)
- {
- $regex = '/(\/)\/+/';
- $path = str_replace(['\\', '/ /'], '/', $path);
- $path = preg_replace($regex,'$1',$path);
- return $path;
- }
- /**
- * Filters the user info string.
- *
- * @param string $info The raw user or password.
- * @return string The percent-encoded user or password string.
- */
- public static function filterUserInfo($info)
- {
- return $info !== null ? UriPartsFilter::filterUserInfo($info) : '';
- }
- /**
- * Filter Uri path.
- *
- * This method percent-encodes all reserved
- * characters in the provided path string. This method
- * will NOT double-encode characters that are already
- * percent-encoded.
- *
- * @param string $path The raw uri path.
- * @return string The RFC 3986 percent-encoded uri path.
- * @link http://www.faqs.org/rfcs/rfc3986.html
- */
- public static function filterPath($path)
- {
- return $path !== null ? UriPartsFilter::filterPath($path) : '';
- }
- /**
- * Filters the query string or fragment of a URI.
- *
- * @param string $query The raw uri query string.
- * @return string The percent-encoded query string.
- */
- public static function filterQuery($query)
- {
- return $query !== null ? UriPartsFilter::filterQueryOrFragment($query) : '';
- }
- /**
- * @param array $env
- */
- protected function createFromEnvironment(array $env)
- {
- // Build scheme.
- if (isset($env['HTTP_X_FORWARDED_PROTO'])) {
- $this->scheme = $env['HTTP_X_FORWARDED_PROTO'];
- } elseif (isset($env['X-FORWARDED-PROTO'])) {
- $this->scheme = $env['X-FORWARDED-PROTO'];
- } elseif (isset($env['REQUEST_SCHEME'])) {
- $this->scheme = $env['REQUEST_SCHEME'];
- } else {
- $https = isset($env['HTTPS']) ? $env['HTTPS'] : '';
- $this->scheme = (empty($https) || strtolower($https) === 'off') ? 'http' : 'https';
- }
- // Build user and password.
- $this->user = isset($env['PHP_AUTH_USER']) ? $env['PHP_AUTH_USER'] : null;
- $this->password = isset($env['PHP_AUTH_PW']) ? $env['PHP_AUTH_PW'] : null;
- // Build host.
- $hostname = 'localhost';
- if (isset($env['HTTP_HOST'])) {
- $hostname = $env['HTTP_HOST'];
- } elseif (isset($env['SERVER_NAME'])) {
- $hostname = $env['SERVER_NAME'];
- }
- // Remove port from HTTP_HOST generated $hostname
- $hostname = Utils::substrToString($hostname, ':');
- // Validate the hostname
- $this->host = $this->validateHostname($hostname) ? $hostname : 'unknown';
- // Build port.
- if (isset($env['HTTP_X_FORWARDED_PORT'])) {
- $this->port = (int)$env['HTTP_X_FORWARDED_PORT'];
- } elseif (isset($env['X-FORWARDED-PORT'])) {
- $this->port = (int)$env['X-FORWARDED-PORT'];
- } elseif (isset($env['SERVER_PORT'])) {
- $this->port = (int)$env['SERVER_PORT'];
- } else {
- $this->port = null;
- }
- if ($this->hasStandardPort()) {
- $this->port = null;
- }
- // Build path.
- $request_uri = isset($env['REQUEST_URI']) ? $env['REQUEST_URI'] : '';
- $this->path = rawurldecode(parse_url('http://example.com' . $request_uri, PHP_URL_PATH));
- // Build query string.
- $this->query = isset($env['QUERY_STRING']) ? $env['QUERY_STRING'] : '';
- if ($this->query === '') {
- $this->query = parse_url('http://example.com' . $request_uri, PHP_URL_QUERY);
- }
- // Support ngnix routes.
- if (strpos($this->query, '_url=') === 0) {
- parse_str($this->query, $query);
- unset($query['_url']);
- $this->query = http_build_query($query);
- }
- // Build fragment.
- $this->fragment = null;
- // Filter userinfo, path and query string.
- $this->user = $this->user !== null ? static::filterUserInfo($this->user) : null;
- $this->password = $this->password !== null ? static::filterUserInfo($this->password) : null;
- $this->path = empty($this->path) ? '/' : static::filterPath($this->path);
- $this->query = static::filterQuery($this->query);
- $this->reset();
- }
- /**
- * Does this Uri use a standard port?
- *
- * @return bool
- */
- protected function hasStandardPort()
- {
- return ($this->scheme === 'http' && $this->port === 80) || ($this->scheme === 'https' && $this->port === 443);
- }
- /**
- * @param string $url
- */
- protected function createFromString($url)
- {
- // Set Uri parts.
- $parts = parse_url($url);
- if ($parts === false) {
- throw new \RuntimeException('Malformed URL: ' . $url);
- }
- $this->scheme = isset($parts['scheme']) ? $parts['scheme'] : null;
- $this->user = isset($parts['user']) ? $parts['user'] : null;
- $this->password = isset($parts['pass']) ? $parts['pass'] : null;
- $this->host = isset($parts['host']) ? $parts['host'] : null;
- $this->port = isset($parts['port']) ? (int)$parts['port'] : null;
- $this->path = isset($parts['path']) ? $parts['path'] : '';
- $this->query = isset($parts['query']) ? $parts['query'] : '';
- $this->fragment = isset($parts['fragment']) ? $parts['fragment'] : null;
- // Validate the hostname
- if ($this->host) {
- $this->host = $this->validateHostname($this->host) ? $this->host : 'unknown';
- }
- // Filter userinfo, path, query string and fragment.
- $this->user = $this->user !== null ? static::filterUserInfo($this->user) : null;
- $this->password = $this->password !== null ? static::filterUserInfo($this->password) : null;
- $this->path = empty($this->path) ? '/' : static::filterPath($this->path);
- $this->query = static::filterQuery($this->query);
- $this->fragment = $this->fragment !== null ? static::filterQuery($this->fragment) : null;
- $this->reset();
- }
- protected function reset()
- {
- // resets
- parse_str($this->query, $this->queries);
- $this->extension = null;
- $this->basename = null;
- $this->paths = [];
- $this->params = [];
- $this->env = $this->buildEnvironment();
- $this->uri = $this->path . (!empty($this->query) ? '?' . $this->query : '');
- $this->base = $this->buildBaseUrl();
- $this->root_path = $this->buildRootPath();
- $this->root = $this->base . $this->root_path;
- $this->url = $this->base . $this->uri;
- }
- /**
- * Get's post from either $_POST or JSON response object
- * By default returns all data, or can return a single item
- *
- * @param string $element
- * @param string $filter_type
- * @return array|mixed|null
- */
- public function post($element = null, $filter_type = null)
- {
- if (!$this->post) {
- $content_type = $this->getContentType();
- if ($content_type === 'application/json') {
- $json = file_get_contents('php://input');
- $this->post = json_decode($json, true);
- } elseif (!empty($_POST)) {
- $this->post = (array)$_POST;
- }
- }
- if ($this->post && null !== $element) {
- $item = Utils::getDotNotation($this->post, $element);
- if ($filter_type) {
- $item = filter_var($item, $filter_type);
- }
- return $item;
- }
- return $this->post;
- }
- /**
- * Get content type from request
- *
- * @param bool $short
- * @return null|string
- */
- private function getContentType($short = true)
- {
- if (isset($_SERVER['CONTENT_TYPE'])) {
- $content_type = $_SERVER['CONTENT_TYPE'];
- if ($short) {
- return Utils::substrToString($content_type,';');
- }
- return $content_type;
- }
- return null;
- }
- /**
- * Get the base URI with port if needed
- *
- * @return string
- */
- private function buildBaseUrl()
- {
- return $this->scheme() . $this->host;
- }
- /**
- * Get the Grav Root Path
- *
- * @return string
- */
- private function buildRootPath()
- {
- // In Windows script path uses backslash, convert it:
- $scriptPath = str_replace('\\', '/', $_SERVER['PHP_SELF']);
- $rootPath = str_replace(' ', '%20', rtrim(substr($scriptPath, 0, strpos($scriptPath, 'index.php')), '/'));
- return $rootPath;
- }
- private function buildEnvironment()
- {
- // check for localhost variations
- if ($this->host === '127.0.0.1' || $this->host === '::1') {
- return 'localhost';
- }
- return $this->host ?: 'unknown';
- }
- /**
- * Process any params based in this URL, supports any valid delimiter
- *
- * @param $uri
- * @param string $delimiter
- *
- * @return string
- */
- private function processParams($uri, $delimiter = ':')
- {
- if (strpos($uri, $delimiter) !== false) {
- preg_match_all(static::paramsRegex(), $uri, $matches, PREG_SET_ORDER);
- foreach ($matches as $match) {
- $param = explode($delimiter, $match[1]);
- if (count($param) === 2) {
- $plain_var = filter_var($param[1], FILTER_SANITIZE_STRING);
- $this->params[$param[0]] = $plain_var;
- $uri = str_replace($match[0], '', $uri);
- }
- }
- }
- return $uri;
- }
- }
|