2014-10-19 20:42:31 +02:00
|
|
|
<?php
|
|
|
|
|
2014-12-24 03:28:26 +01:00
|
|
|
namespace PicoFeed\Client;
|
2014-10-19 20:42:31 +02:00
|
|
|
|
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* URL class.
|
2014-10-19 20:42:31 +02:00
|
|
|
*
|
|
|
|
* @author Frederic Guillot
|
|
|
|
*/
|
|
|
|
class Url
|
|
|
|
{
|
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* URL.
|
2014-10-19 20:42:31 +02:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $url = '';
|
|
|
|
|
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* URL components.
|
2014-10-19 20:42:31 +02:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $components = array();
|
|
|
|
|
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Constructor.
|
2014-10-19 20:42:31 +02:00
|
|
|
*
|
2015-10-20 04:49:30 +02:00
|
|
|
* @param string $url URL
|
2014-10-19 20:42:31 +02:00
|
|
|
*/
|
|
|
|
public function __construct($url)
|
|
|
|
{
|
|
|
|
$this->url = $url;
|
|
|
|
$this->components = parse_url($url) ?: array();
|
|
|
|
|
|
|
|
// Issue with PHP < 5.4.7 and protocol relative url
|
|
|
|
if (version_compare(PHP_VERSION, '5.4.7', '<') && $this->isProtocolRelative()) {
|
|
|
|
$pos = strpos($this->components['path'], '/', 2);
|
|
|
|
|
|
|
|
if ($pos === false) {
|
|
|
|
$pos = strlen($this->components['path']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->components['host'] = substr($this->components['path'], 2, $pos - 2);
|
|
|
|
$this->components['path'] = substr($this->components['path'], $pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Shortcut method to get an absolute url from relative url.
|
2014-10-19 20:42:31 +02:00
|
|
|
*
|
|
|
|
* @static
|
2015-10-20 04:49:30 +02:00
|
|
|
* @param mixed $item_url Unknown url (can be relative or not)
|
|
|
|
* @param mixed $website_url Website url
|
2014-10-19 20:42:31 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function resolve($item_url, $website_url)
|
|
|
|
{
|
2015-10-20 04:49:30 +02:00
|
|
|
$link = is_string($item_url) ? new self($item_url) : $item_url;
|
|
|
|
$website = is_string($website_url) ? new self($website_url) : $website_url;
|
2014-10-19 20:42:31 +02:00
|
|
|
|
|
|
|
if ($link->isRelativeUrl()) {
|
|
|
|
if ($link->isRelativePath()) {
|
2015-01-02 21:25:11 +01:00
|
|
|
return $link->getAbsoluteUrl($website->getBaseUrl($website->getBasePath()));
|
2014-10-19 20:42:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $link->getAbsoluteUrl($website->getBaseUrl());
|
2015-10-20 04:49:30 +02:00
|
|
|
} elseif ($link->isProtocolRelative()) {
|
2014-10-19 20:42:31 +02:00
|
|
|
$link->setScheme($website->getScheme());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $link->getAbsoluteUrl();
|
|
|
|
}
|
|
|
|
|
2014-12-24 03:28:26 +01:00
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Shortcut method to get a base url.
|
2014-12-24 03:28:26 +01:00
|
|
|
*
|
|
|
|
* @static
|
2015-10-20 04:49:30 +02:00
|
|
|
* @param string $url
|
2014-12-24 03:28:26 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function base($url)
|
|
|
|
{
|
2015-10-20 04:49:30 +02:00
|
|
|
$link = new self($url);
|
|
|
|
|
2014-12-24 03:28:26 +01:00
|
|
|
return $link->getBaseUrl();
|
|
|
|
}
|
|
|
|
|
2014-10-19 20:42:31 +02:00
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Get the base URL.
|
|
|
|
*
|
|
|
|
* @param string $suffix Add a suffix to the url
|
2014-10-19 20:42:31 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getBaseUrl($suffix = '')
|
|
|
|
{
|
|
|
|
return $this->hasHost() ? $this->getScheme('://').$this->getHost().$this->getPort(':').$suffix : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Get the absolute URL.
|
|
|
|
*
|
|
|
|
* @param string $base_url Use this url as base url
|
2014-10-19 20:42:31 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAbsoluteUrl($base_url = '')
|
|
|
|
{
|
|
|
|
if ($base_url) {
|
2015-10-20 04:49:30 +02:00
|
|
|
$base = new self($base_url);
|
2014-10-19 20:42:31 +02:00
|
|
|
$url = $base->getAbsoluteUrl().substr($this->getFullPath(), 1);
|
2015-10-20 04:49:30 +02:00
|
|
|
} else {
|
2014-10-19 20:42:31 +02:00
|
|
|
$url = $this->hasHost() ? $this->getBaseUrl().$this->getFullPath() : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Return true if the url is relative.
|
2014-10-19 20:42:31 +02:00
|
|
|
*
|
2015-10-20 04:49:30 +02:00
|
|
|
* @return bool
|
2014-10-19 20:42:31 +02:00
|
|
|
*/
|
|
|
|
public function isRelativeUrl()
|
|
|
|
{
|
2015-10-20 04:49:30 +02:00
|
|
|
return !$this->hasScheme() && !$this->isProtocolRelative();
|
2014-10-19 20:42:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Return true if the path is relative.
|
2014-10-19 20:42:31 +02:00
|
|
|
*
|
2015-10-20 04:49:30 +02:00
|
|
|
* @return bool
|
2014-10-19 20:42:31 +02:00
|
|
|
*/
|
|
|
|
public function isRelativePath()
|
|
|
|
{
|
|
|
|
$path = $this->getPath();
|
2015-10-20 04:49:30 +02:00
|
|
|
|
|
|
|
return empty($path) || $path{0}
|
|
|
|
!== '/';
|
2014-10-19 20:42:31 +02:00
|
|
|
}
|
|
|
|
|
2015-08-02 20:08:21 +02:00
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Filters the path of a URI.
|
2015-08-02 20:08:21 +02:00
|
|
|
*
|
|
|
|
* Imported from Guzzle library: https://github.com/guzzle/psr7/blob/master/src/Uri.php#L568-L582
|
|
|
|
*
|
|
|
|
* @param $path
|
2017-06-21 01:41:55 +02:00
|
|
|
* @param string $charUnreserved
|
|
|
|
* @param string $charSubDelims
|
2015-08-02 20:08:21 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function filterPath($path, $charUnreserved = 'a-zA-Z0-9_\-\.~', $charSubDelims = '!\$&\'\(\)\*\+,;=')
|
|
|
|
{
|
|
|
|
return preg_replace_callback(
|
2015-10-20 04:49:30 +02:00
|
|
|
'/(?:[^'.$charUnreserved.$charSubDelims.':@\/%]+|%(?![A-Fa-f0-9]{2}))/',
|
2015-08-02 20:08:21 +02:00
|
|
|
function (array $matches) { return rawurlencode($matches[0]); },
|
|
|
|
$path
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-19 20:42:31 +02:00
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Get the path.
|
2014-10-19 20:42:31 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPath()
|
|
|
|
{
|
2015-08-02 20:08:21 +02:00
|
|
|
return $this->filterPath(empty($this->components['path']) ? '' : $this->components['path']);
|
2014-10-19 20:42:31 +02:00
|
|
|
}
|
|
|
|
|
2015-01-02 21:25:11 +01:00
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Get the base path.
|
2015-01-02 21:25:11 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getBasePath()
|
|
|
|
{
|
|
|
|
$current_path = $this->getPath();
|
|
|
|
|
|
|
|
$path = $this->isRelativePath() ? '/' : '';
|
|
|
|
$path .= substr($current_path, -1) === '/' ? $current_path : dirname($current_path);
|
|
|
|
|
2015-01-28 02:13:16 +01:00
|
|
|
return preg_replace('/\\\\\/|\/\//', '/', $path.'/');
|
2015-01-02 21:25:11 +01:00
|
|
|
}
|
|
|
|
|
2014-10-19 20:42:31 +02:00
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Get the full path (path + querystring + fragment).
|
2014-10-19 20:42:31 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFullPath()
|
|
|
|
{
|
|
|
|
$path = $this->isRelativePath() ? '/' : '';
|
|
|
|
$path .= $this->getPath();
|
|
|
|
$path .= empty($this->components['query']) ? '' : '?'.$this->components['query'];
|
|
|
|
$path .= empty($this->components['fragment']) ? '' : '#'.$this->components['fragment'];
|
|
|
|
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Get the hostname.
|
2014-10-19 20:42:31 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getHost()
|
|
|
|
{
|
|
|
|
return empty($this->components['host']) ? '' : $this->components['host'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Return true if the url has a hostname.
|
2014-10-19 20:42:31 +02:00
|
|
|
*
|
2015-10-20 04:49:30 +02:00
|
|
|
* @return bool
|
2014-10-19 20:42:31 +02:00
|
|
|
*/
|
|
|
|
public function hasHost()
|
|
|
|
{
|
2015-10-20 04:49:30 +02:00
|
|
|
return !empty($this->components['host']);
|
2014-10-19 20:42:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Get the scheme.
|
|
|
|
*
|
|
|
|
* @param string $suffix Suffix to add when there is a scheme
|
2014-10-19 20:42:31 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getScheme($suffix = '')
|
|
|
|
{
|
|
|
|
return ($this->hasScheme() ? $this->components['scheme'] : 'http').$suffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Set the scheme.
|
|
|
|
*
|
|
|
|
* @param string $scheme Set a scheme
|
2017-06-21 01:41:55 +02:00
|
|
|
* @return $this
|
2014-10-19 20:42:31 +02:00
|
|
|
*/
|
|
|
|
public function setScheme($scheme)
|
|
|
|
{
|
|
|
|
$this->components['scheme'] = $scheme;
|
2017-06-21 01:41:55 +02:00
|
|
|
return $this;
|
2014-10-19 20:42:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Return true if the url has a scheme.
|
2014-10-19 20:42:31 +02:00
|
|
|
*
|
2015-10-20 04:49:30 +02:00
|
|
|
* @return bool
|
2014-10-19 20:42:31 +02:00
|
|
|
*/
|
|
|
|
public function hasScheme()
|
|
|
|
{
|
2015-10-20 04:49:30 +02:00
|
|
|
return !empty($this->components['scheme']);
|
2014-10-19 20:42:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Get the port.
|
|
|
|
*
|
|
|
|
* @param string $prefix Prefix to add when there is a port
|
2014-10-19 20:42:31 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPort($prefix = '')
|
|
|
|
{
|
|
|
|
return $this->hasPort() ? $prefix.$this->components['port'] : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Return true if the url has a port.
|
2014-10-19 20:42:31 +02:00
|
|
|
*
|
2015-10-20 04:49:30 +02:00
|
|
|
* @return bool
|
2014-10-19 20:42:31 +02:00
|
|
|
*/
|
|
|
|
public function hasPort()
|
|
|
|
{
|
2015-10-20 04:49:30 +02:00
|
|
|
return !empty($this->components['port']);
|
2014-10-19 20:42:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-20 04:49:30 +02:00
|
|
|
* Return true if the url is protocol relative (start with //).
|
2014-10-19 20:42:31 +02:00
|
|
|
*
|
2015-10-20 04:49:30 +02:00
|
|
|
* @return bool
|
2014-10-19 20:42:31 +02:00
|
|
|
*/
|
|
|
|
public function isProtocolRelative()
|
|
|
|
{
|
|
|
|
return strpos($this->url, '//') === 0;
|
|
|
|
}
|
|
|
|
}
|