* 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
public static function time_hms($seconds)
$hours = floor($seconds / 3600);
$remainder = $seconds % 3600;
$minutes = floor($remainder / 60);
$seconds = $remainder % 60;
if ($minutes < 10 && $hours > 0)
$minutes = '0' . $minutes;
$seconds = '0' . $seconds;
public static function absolutize_url($relative, $base)
$iri = SimplePie_IRI::absolutize(new SimplePie_IRI($base), $relative);
* Get a HTML/XML element from a HTML string
* @deprecated Use DOMDocument instead (parsing HTML with regex is bad!)
* @param string $realname Element name (including namespace prefix if applicable)
* @param string $string HTML document
public static function get_element($realname, $string)
$name = preg_quote($realname, '/');
if (preg_match_all("/<($name)" . SIMPLEPIE_PCRE_HTML_ATTRIBUTE . "(>(.*)<\/$name>|(\/)?>)/siU", $string, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE))
for ($i = 0, $total_matches = count($matches); $i < $total_matches; $i++)
$return[$i]['tag'] = $realname;
$return[$i]['full'] = $matches[$i][0][0];
$return[$i]['offset'] = $matches[$i][0][1];
if (strlen($matches[$i][3][0]) <= 2)
$return[$i]['self_closing'] = true;
$return[$i]['self_closing'] = false;
$return[$i]['content'] = $matches[$i][4][0];
$return[$i]['attribs'] = array();
if (isset($matches[$i][2][0]) && preg_match_all('/[\x09\x0A\x0B\x0C\x0D\x20]+([^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3D\x3E]*)(?:[\x09\x0A\x0B\x0C\x0D\x20]*=[\x09\x0A\x0B\x0C\x0D\x20]*(?:"([^"]*)"|\'([^\']*)\'|([^\x09\x0A\x0B\x0C\x0D\x20\x22\x27\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x3E]*)?))?/', ' ' . $matches[$i][2][0] . ' ', $attribs, PREG_SET_ORDER))
for ($j = 0, $total_attribs = count($attribs); $j < $total_attribs; $j++)
if (count($attribs[$j]) === 2)
$attribs[$j][2] = $attribs[$j][1];
$return[$i]['attribs'][strtolower($attribs[$j][1])]['data'] = SimplePie_Misc::entities_decode(end($attribs[$j]));
public static function element_implode($element)
$full = "<$element[tag]";
foreach ($element['attribs'] as $key => $value)
$full .= " $key=\"" . htmlspecialchars($value['data'], ENT_COMPAT, 'UTF-8') . '"';
if ($element['self_closing'])
$full .= ">$element[content]</$element[tag]>";
public static function error($message, $level, $file, $line)
if ((ini_get('error_reporting') & $level) > 0)
if (!function_exists('error_log'))
$log_file = @ini_get('error_log');
if (!empty($log_file) && ('syslog' !== $log_file) && !@is_writable($log_file))
@error_log("$note: $message in $file on line $line", 0);
public static function fix_protocol($url, $http = 1)
$url = SimplePie_Misc::normalize_url($url);
$parsed = SimplePie_Misc::parse_url($url);
if ($parsed['scheme'] !== '' && $parsed['scheme'] !== 'http' && $parsed['scheme'] !== 'https')
return SimplePie_Misc::fix_protocol(SimplePie_Misc::compress_parse_url('http', $parsed['authority'], $parsed['path'], $parsed['query'], $parsed['fragment']), $http);
if ($parsed['scheme'] === '' && $parsed['authority'] === '' && !file_exists($url))
return SimplePie_Misc::fix_protocol(SimplePie_Misc::compress_parse_url('http', $parsed['path'], '', $parsed['query'], $parsed['fragment']), $http);
if ($http === 2 && $parsed['scheme'] !== '')
elseif ($http === 3 && strtolower($parsed['scheme']) === 'http')
return substr_replace($url, 'podcast', 0, 4);
elseif ($http === 4 && strtolower($parsed['scheme']) === 'http')
return substr_replace($url, 'itpc', 0, 4);
public static function array_merge_recursive($array1, $array2)
foreach ($array2 as $key => $value)
$array1[$key] = SimplePie_Misc::array_merge_recursive($array1[$key], $value);
public static function parse_url($url)
$iri = new SimplePie_IRI($url);
'scheme' => (string) $iri->scheme,
'authority' => (string) $iri->authority,
'path' => (string) $iri->path,
'query' => (string) $iri->query,
'fragment' => (string) $iri->fragment
public static function compress_parse_url($scheme = '', $authority = '', $path = '', $query = '', $fragment = '')
$iri = new SimplePie_IRI('');
$iri->authority = $authority;
$iri->fragment = $fragment;
public static function normalize_url($url)
$iri = new SimplePie_IRI($url);
public static function percent_encoding_normalization($match)
$integer = hexdec($match[1]);
if ($integer >= 0x41 && $integer <= 0x5A || $integer >= 0x61 && $integer <= 0x7A || $integer >= 0x30 && $integer <= 0x39 || $integer === 0x2D || $integer === 0x2E || $integer === 0x5F || $integer === 0x7E)
return strtoupper($match[0]);
* Converts a Windows-1252 encoded string to a UTF-8 encoded string
* @param string $string Windows-1252 encoded string
* @return string UTF-8 encoded string
public static function windows_1252_to_utf8($string)
static $convert_table = array("\x80" => "\xE2\x82\xAC", "\x81" => "\xEF\xBF\xBD", "\x82" => "\xE2\x80\x9A", "\x83" => "\xC6\x92", "\x84" => "\xE2\x80\x9E", "\x85" => "\xE2\x80\xA6", "\x86" => "\xE2\x80\xA0", "\x87" => "\xE2\x80\xA1", "\x88" => "\xCB\x86", "\x89" => "\xE2\x80\xB0", "\x8A" => "\xC5\xA0", "\x8B" => "\xE2\x80\xB9", "\x8C" => "\xC5\x92", "\x8D" => "\xEF\xBF\xBD", "\x8E" => "\xC5\xBD", "\x8F" => "\xEF\xBF\xBD", "\x90" => "\xEF\xBF\xBD", "\x91" => "\xE2\x80\x98", "\x92" => "\xE2\x80\x99", "\x93" => "\xE2\x80\x9C", "\x94" => "\xE2\x80\x9D", "\x95" => "\xE2\x80\xA2", "\x96" => "\xE2\x80\x93", "\x97" => "\xE2\x80\x94", "\x98" => "\xCB\x9C", "\x99" => "\xE2\x84\xA2", "\x9A" => "\xC5\xA1", "\x9B" => "\xE2\x80\xBA", "\x9C" => "\xC5\x93", "\x9D" => "\xEF\xBF\xBD", "\x9E" => "\xC5\xBE", "\x9F" => "\xC5\xB8", "\xA0" => "\xC2\xA0", "\xA1" => "\xC2\xA1", "\xA2" => "\xC2\xA2", "\xA3" => "\xC2\xA3", "\xA4" => "\xC2\xA4", "\xA5" => "\xC2\xA5", "\xA6" => "\xC2\xA6", "\xA7" => "\xC2\xA7", "\xA8" => "\xC2\xA8", "\xA9" => "\xC2\xA9", "\xAA" => "\xC2\xAA", "\xAB" => "\xC2\xAB", "\xAC" => "\xC2\xAC", "\xAD" => "\xC2\xAD", "\xAE" => "\xC2\xAE", "\xAF" => "\xC2\xAF", "\xB0" => "\xC2\xB0", "\xB1" => "\xC2\xB1", "\xB2" => "\xC2\xB2", "\xB3" => "\xC2\xB3", "\xB4" => "\xC2\xB4", "\xB5" => "\xC2\xB5", "\xB6" => "\xC2\xB6", "\xB7" => "\xC2\xB7", "\xB8" => "\xC2\xB8", "\xB9" => "\xC2\xB9", "\xBA" => "\xC2\xBA", "\xBB" => "\xC2\xBB", "\xBC" => "\xC2\xBC", "\xBD" => "\xC2\xBD", "\xBE" => "\xC2\xBE", "\xBF" => "\xC2\xBF", "\xC0" => "\xC3\x80", "\xC1" => "\xC3\x81", "\xC2" => "\xC3\x82", "\xC3" => "\xC3\x83", "\xC4" => "\xC3\x84", "\xC5" => "\xC3\x85", "\xC6" => "\xC3\x86", "\xC7" => "\xC3\x87", "\xC8" => "\xC3\x88", "\xC9" => "\xC3\x89", "\xCA" => "\xC3\x8A", "\xCB" => "\xC3\x8B", "\xCC" => "\xC3\x8C", "\xCD" => "\xC3\x8D", "\xCE" => "\xC3\x8E", "\xCF" => "\xC3\x8F", "\xD0" => "\xC3\x90", "\xD1" => "\xC3\x91", "\xD2" => "\xC3\x92", "\xD3" => "\xC3\x93", "\xD4" => "\xC3\x94", "\xD5" => "\xC3\x95", "\xD6" => "\xC3\x96", "\xD7" => "\xC3\x97", "\xD8" => "\xC3\x98", "\xD9" => "\xC3\x99", "\xDA" => "\xC3\x9A", "\xDB" => "\xC3\x9B", "\xDC" => "\xC3\x9C", "\xDD" => "\xC3\x9D", "\xDE" => "\xC3\x9E", "\xDF" => "\xC3\x9F", "\xE0" => "\xC3\xA0", "\xE1" => "\xC3\xA1", "\xE2" => "\xC3\xA2", "\xE3" => "\xC3\xA3", "\xE4" => "\xC3\xA4", "\xE5" => "\xC3\xA5", "\xE6" => "\xC3\xA6", "\xE7" => "\xC3\xA7", "\xE8" => "\xC3\xA8", "\xE9" => "\xC3\xA9", "\xEA" => "\xC3\xAA", "\xEB" => "\xC3\xAB", "\xEC" => "\xC3\xAC", "\xED" => "\xC3\xAD", "\xEE" => "\xC3\xAE", "\xEF" => "\xC3\xAF", "\xF0" => "\xC3\xB0", "\xF1" => "\xC3\xB1", "\xF2" => "\xC3\xB2", "\xF3" => "\xC3\xB3", "\xF4" => "\xC3\xB4", "\xF5" => "\xC3\xB5", "\xF6" => "\xC3\xB6", "\xF7" => "\xC3\xB7", "\xF8" => "\xC3\xB8", "\xF9" => "\xC3\xB9", "\xFA" => "\xC3\xBA", "\xFB" => "\xC3\xBB", "\xFC" => "\xC3\xBC", "\xFD" => "\xC3\xBD", "\xFE" => "\xC3\xBE", "\xFF" => "\xC3\xBF");
return strtr($string, $convert_table);
* Change a string from one encoding to another
* @param string $data Raw data in $input encoding
* @param string $input Encoding of $data
* @param string $output Encoding you want
* @return string|boolean False if we can't convert it
public static function change_encoding($data, $input, $output)
$input = SimplePie_Misc::encoding($input);
$output = SimplePie_Misc::encoding($output);
// We fail to fail on non US-ASCII bytes
if ($input === 'US-ASCII')
static $non_ascii_octects = '';
for ($i = 0x80; $i <= 0xFF; $i++)
$non_ascii_octects .= chr($i);
$data = substr($data, 0, strcspn($data, $non_ascii_octects));
// This is first, as behaviour of this is completely predictable
if ($input === 'windows-1252' && $output === 'UTF-8')
return SimplePie_Misc::windows_1252_to_utf8($data);
// This is second, as behaviour of this varies only with PHP version (the middle part of this expression checks the encoding is supported).
elseif (function_exists('mb_convert_encoding') && ($return = SimplePie_Misc::change_encoding_mbstring($data, $input, $output)))
// This is third, as behaviour of this varies with OS userland and PHP version
elseif (function_exists('iconv') && ($return = SimplePie_Misc::change_encoding_iconv($data, $input, $output)))
// This is last, as behaviour of this varies with OS userland and PHP version
elseif (class_exists('\UConverter') && ($return = SimplePie_Misc::change_encoding_uconverter($data, $input, $output)))
// If we can't do anything, just fail
protected static function change_encoding_mbstring($data, $input, $output)
if ($input === 'windows-949')
if ($output === 'windows-949')
if ($input === 'Windows-31J')
if ($output === 'Windows-31J')
// Check that the encoding is supported
if (!in_array($input, mb_list_encodings()))
if (@mb_convert_encoding("\x80", 'UTF-16BE', $input) === "\x00\x80")
// Let's do some conversion
if ($return = @mb_convert_encoding($data, $output, $input))
protected static function change_encoding_iconv($data, $input, $output)
return @iconv($input, $output, $data);
protected static function change_encoding_uconverter($data, $input, $output)
return @\UConverter::transcode($data, $output, $input);
* Normalize an encoding name
* This is automatically generated by create.php
* To generate it, run `php create.php` on the command line, and copy the
* output to replace this function.
* @param string $charset Character set to standardise
* @return string Standardised name
public static function encoding($charset)
// Normalization from UTS #22
switch (strtolower(preg_replace('/(?:[^a-zA-Z0-9]+|([^0-9])0+)/', '\1', $charset)))
case 'adobestandardencoding':
case 'csadobestandardencoding':
return 'Adobe-Standard-Encoding';
case 'adobesymbolencoding':
return 'Adobe-Symbol-Encoding';
return 'ANSI_X3.110-1983';
case 'csiso4unitedkingdom':
case 'csiso47bsviewdata':
case 'csiso121canadian1':
return 'CSA_Z243.4-1985-1';
case 'csiso122canadian2':
return 'CSA_Z243.4-1985-2';
case 'csiso123csaz24341985gr':
return 'CSA_Z243.4-1985-gr';
case 'csiso139csn369103':