* A PHP-Based RSS and Atom Feed Framework.
* Takes the hard work out of managing a complete RSS/Atom solution.
* Copyright (c) 2004-2016, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
* * Neither the name of the SimplePie Team nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
* AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue
* @link http://simplepie.org/ SimplePie
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* IRI parser/serialiser/normaliser
* @author Steve Minutillo
* @copyright 2007-2012 Sam Sneddon, Steve Minutillo, Ryan McCue
* @license http://www.opensource.org/licenses/bsd-license.php
protected $scheme = null;
protected $iuserinfo = null;
protected $iquery = null;
protected $ifragment = null;
* Each key is the scheme, each value is an array with each key as the IRI
* part and value as the default value for that part.
protected $normalization = array(
* Return the entire IRI when you try and read the object as a string
public function __toString()
* Overload __set() to provide access via properties
* @param string $name Property name
* @param mixed $value Property value
public function __set($name, $value)
if (method_exists($this, 'set_' . $name))
call_user_func(array($this, 'set_' . $name), $value);
call_user_func(array($this, 'set_' . substr($name, 1)), $value);
* Overload __get() to provide access via properties
* @param string $name Property name
public function __get($name)
// isset() returns false for null, we don't want to do that
// Also why we use array_key_exists below instead of isset()
$props = get_object_vars($this);
$name === 'iauthority' ||
$return = $this->{"get_$name"}();
elseif (array_key_exists($name, $props))
elseif (($prop = 'i' . $name) && array_key_exists($prop, $props))
elseif (($prop = substr($name, 1)) && array_key_exists($prop, $props))
trigger_error('Undefined property: ' . get_class($this) . '::' . $name, E_USER_NOTICE);
if ($return === null && isset($this->normalization[$this->scheme][$name]))
return $this->normalization[$this->scheme][$name];
* Overload __isset() to provide access via properties
* @param string $name Property name
public function __isset($name)
return method_exists($this, 'get_' . $name) || isset($this->$name);
* Overload __unset() to provide access via properties
* @param string $name Property name
public function __unset($name)
if (method_exists($this, 'set_' . $name))
call_user_func(array($this, 'set_' . $name), '');
* Create a new IRI object, from a specified string
public function __construct($iri = null)
public function __destruct() {
$this->set_iri(null, true);
$this->set_path(null, true);
$this->set_authority(null, true);
* Create a new IRI object by resolving a relative IRI
* Returns false if $base is not absolute, otherwise an IRI.
* @param IRI|string $base (Absolute) Base IRI
* @param IRI|string $relative Relative IRI
public static function absolutize($base, $relative)
if (!($relative instanceof SimplePie_IRI))
$relative = new SimplePie_IRI($relative);
if (!$relative->is_valid())
elseif ($relative->scheme !== null)
if (!($base instanceof SimplePie_IRI))
$base = new SimplePie_IRI($base);
if ($base->scheme !== null && $base->is_valid())
if ($relative->get_iri() !== '')
if ($relative->iuserinfo !== null || $relative->ihost !== null || $relative->port !== null)
$target = clone $relative;
$target->scheme = $base->scheme;
$target = new SimplePie_IRI;
$target->scheme = $base->scheme;
$target->iuserinfo = $base->iuserinfo;
$target->ihost = $base->ihost;
$target->port = $base->port;
if ($relative->ipath !== '')
if ($relative->ipath[0] === '/')
$target->ipath = $relative->ipath;
elseif (($base->iuserinfo !== null || $base->ihost !== null || $base->port !== null) && $base->ipath === '')
$target->ipath = '/' . $relative->ipath;
elseif (($last_segment = strrpos($base->ipath, '/')) !== false)
$target->ipath = substr($base->ipath, 0, $last_segment + 1) . $relative->ipath;
$target->ipath = $relative->ipath;
$target->ipath = $target->remove_dot_segments($target->ipath);
$target->iquery = $relative->iquery;
$target->ipath = $base->ipath;
if ($relative->iquery !== null)
$target->iquery = $relative->iquery;
elseif ($base->iquery !== null)
$target->iquery = $base->iquery;
$target->ifragment = $relative->ifragment;
$target->ifragment = null;
$target->scheme_normalization();
* Parse an IRI into scheme/authority/path/query/fragment segments
protected function parse_iri($iri)
$iri = trim($iri, "\x20\x09\x0A\x0C\x0D");
if (preg_match('/^((?P<scheme>[^:\/?#]+):)?(\/\/(?P<authority>[^\/?#]*))?(?P<path>[^?#]*)(\?(?P<query>[^#]*))?(#(?P<fragment>.*))?$/', $iri, $match))
if (!isset($match[3]) || $match[3] === '')
$match['authority'] = null;
if (!isset($match[6]) || $match[6] === '')
if (!isset($match[8]) || $match[8] === '')
$match['fragment'] = null;
// This can occur when a paragraph is accidentally parsed as a URI
* Remove dot segments from a path
protected function remove_dot_segments($input)
while (strpos($input, './') !== false || strpos($input, '/.') !== false || $input === '.' || $input === '..')
// A: If the input buffer begins with a prefix of "../" or "./", then remove that prefix from the input buffer; otherwise,
if (strpos($input, '../') === 0)
$input = substr($input, 3);
elseif (strpos($input, './') === 0)
$input = substr($input, 2);
// B: if the input buffer begins with a prefix of "/./" or "/.", where "." is a complete path segment, then replace that prefix with "/" in the input buffer; otherwise,
elseif (strpos($input, '/./') === 0)
$input = substr($input, 2);
// C: if the input buffer begins with a prefix of "/../" or "/..", where ".." is a complete path segment, then replace that prefix with "/" in the input buffer and remove the last segment and its preceding "/" (if any) from the output buffer; otherwise,
elseif (strpos($input, '/../') === 0)
$input = substr($input, 3);
$output = substr_replace($output, '', strrpos($output, '/'));
elseif ($input === '/..')
$output = substr_replace($output, '', strrpos($output, '/'));
// D: if the input buffer consists only of "." or "..", then remove that from the input buffer; otherwise,
elseif ($input === '.' || $input === '..')
// E: move the first path segment in the input buffer to the end of the output buffer, including the initial "/" character (if any) and any subsequent characters up to, but not including, the next "/" character or the end of the input buffer
elseif (($pos = strpos($input, '/', 1)) !== false)
$output .= substr($input, 0, $pos);
$input = substr_replace($input, '', 0, $pos);
* Replace invalid character with percent encoding
* @param string $string Input string
* @param string $extra_chars Valid characters not in iunreserved or
* iprivate (this is ASCII-only)
* @param bool $iprivate Allow iprivate
protected function replace_invalid_with_pct_encoding($string, $extra_chars, $iprivate = false)
// Normalize as many pct-encoded sections as possible
$string = preg_replace_callback('/(?:%[A-Fa-f0-9]{2})+/', array($this, 'remove_iunreserved_percent_encoded'), $string);
// Replace invalid percent characters
$string = preg_replace('/%(?![A-Fa-f0-9]{2})/', '%25', $string);
// Add unreserved and % to $extra_chars (the latter is safe because all
// pct-encoded sections are now valid).
$extra_chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~%';
// Now replace any bytes that aren't allowed with their pct-encoded versions
$strlen = strlen($string);
while (($position += strspn($string, $extra_chars, $position)) < $strlen)
$value = ord($string[$position]);
// By default we are valid
// No one byte sequences are valid due to the while.
if (($value & 0xE0) === 0xC0)
$character = ($value & 0x1F) << 6;
elseif (($value & 0xF0) === 0xE0)
$character = ($value & 0x0F) << 12;