wp/wp-includes/SimplePie/Misc.php
changeset 16 a86126ab1dd4
parent 0 d970ebf37754
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     3  * SimplePie
     3  * SimplePie
     4  *
     4  *
     5  * A PHP-Based RSS and Atom Feed Framework.
     5  * A PHP-Based RSS and Atom Feed Framework.
     6  * Takes the hard work out of managing a complete RSS/Atom solution.
     6  * Takes the hard work out of managing a complete RSS/Atom solution.
     7  *
     7  *
     8  * Copyright (c) 2004-2012, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
     8  * Copyright (c) 2004-2016, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
     9  * All rights reserved.
     9  * All rights reserved.
    10  *
    10  *
    11  * Redistribution and use in source and binary forms, with or without modification, are
    11  * Redistribution and use in source and binary forms, with or without modification, are
    12  * permitted provided that the following conditions are met:
    12  * permitted provided that the following conditions are met:
    13  *
    13  *
    31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    33  * POSSIBILITY OF SUCH DAMAGE.
    33  * POSSIBILITY OF SUCH DAMAGE.
    34  *
    34  *
    35  * @package SimplePie
    35  * @package SimplePie
    36  * @version 1.3.1
    36  * @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue
    37  * @copyright 2004-2012 Ryan Parman, Geoffrey Sneddon, Ryan McCue
       
    38  * @author Ryan Parman
    37  * @author Ryan Parman
    39  * @author Geoffrey Sneddon
    38  * @author Sam Sneddon
    40  * @author Ryan McCue
    39  * @author Ryan McCue
    41  * @link http://simplepie.org/ SimplePie
    40  * @link http://simplepie.org/ SimplePie
    42  * @license http://www.opensource.org/licenses/bsd-license.php BSD License
    41  * @license http://www.opensource.org/licenses/bsd-license.php BSD License
    43  */
    42  */
    44 
    43 
   136 	{
   135 	{
   137 		$full = "<$element[tag]";
   136 		$full = "<$element[tag]";
   138 		foreach ($element['attribs'] as $key => $value)
   137 		foreach ($element['attribs'] as $key => $value)
   139 		{
   138 		{
   140 			$key = strtolower($key);
   139 			$key = strtolower($key);
   141 			$full .= " $key=\"" . htmlspecialchars($value['data']) . '"';
   140 			$full .= " $key=\"" . htmlspecialchars($value['data'], ENT_COMPAT, 'UTF-8') . '"';
   142 		}
   141 		}
   143 		if ($element['self_closing'])
   142 		if ($element['self_closing'])
   144 		{
   143 		{
   145 			$full .= ' />';
   144 			$full .= ' />';
   146 		}
   145 		}
   216 		}
   215 		}
   217 		elseif ($http === 4 && strtolower($parsed['scheme']) === 'http')
   216 		elseif ($http === 4 && strtolower($parsed['scheme']) === 'http')
   218 		{
   217 		{
   219 			return substr_replace($url, 'itpc', 0, 4);
   218 			return substr_replace($url, 'itpc', 0, 4);
   220 		}
   219 		}
   221 		else
   220 
   222 		{
   221 		return $url;
   223 			return $url;
   222 	}
   224 		}
   223 
       
   224 	public static function array_merge_recursive($array1, $array2)
       
   225 	{
       
   226 		foreach ($array2 as $key => $value)
       
   227 		{
       
   228 			if (is_array($value))
       
   229 			{
       
   230 				$array1[$key] = SimplePie_Misc::array_merge_recursive($array1[$key], $value);
       
   231 			}
       
   232 			else
       
   233 			{
       
   234 				$array1[$key] = $value;
       
   235 			}
       
   236 		}
       
   237 
       
   238 		return $array1;
   225 	}
   239 	}
   226 
   240 
   227 	public static function parse_url($url)
   241 	public static function parse_url($url)
   228 	{
   242 	{
   229 		$iri = new SimplePie_IRI($url);
   243 		$iri = new SimplePie_IRI($url);
   258 		$integer = hexdec($match[1]);
   272 		$integer = hexdec($match[1]);
   259 		if ($integer >= 0x41 && $integer <= 0x5A || $integer >= 0x61 && $integer <= 0x7A || $integer >= 0x30 && $integer <= 0x39 || $integer === 0x2D || $integer === 0x2E || $integer === 0x5F || $integer === 0x7E)
   273 		if ($integer >= 0x41 && $integer <= 0x5A || $integer >= 0x61 && $integer <= 0x7A || $integer >= 0x30 && $integer <= 0x39 || $integer === 0x2D || $integer === 0x2E || $integer === 0x5F || $integer === 0x7E)
   260 		{
   274 		{
   261 			return chr($integer);
   275 			return chr($integer);
   262 		}
   276 		}
   263 		else
   277 
   264 		{
   278 		return strtoupper($match[0]);
   265 			return strtoupper($match[0]);
       
   266 		}
       
   267 	}
   279 	}
   268 
   280 
   269 	/**
   281 	/**
   270 	 * Converts a Windows-1252 encoded string to a UTF-8 encoded string
   282 	 * Converts a Windows-1252 encoded string to a UTF-8 encoded string
   271 	 *
   283 	 *
   315 		// This is second, as behaviour of this varies only with PHP version (the middle part of this expression checks the encoding is supported).
   327 		// This is second, as behaviour of this varies only with PHP version (the middle part of this expression checks the encoding is supported).
   316 		elseif (function_exists('mb_convert_encoding') && ($return = SimplePie_Misc::change_encoding_mbstring($data, $input, $output)))
   328 		elseif (function_exists('mb_convert_encoding') && ($return = SimplePie_Misc::change_encoding_mbstring($data, $input, $output)))
   317 		{
   329 		{
   318 			return $return;
   330 			return $return;
   319  		}
   331  		}
       
   332 		// This is third, as behaviour of this varies with OS userland and PHP version
       
   333 		elseif (function_exists('iconv') && ($return = SimplePie_Misc::change_encoding_iconv($data, $input, $output)))
       
   334 		{
       
   335 			return $return;
       
   336 		}
   320 		// This is last, as behaviour of this varies with OS userland and PHP version
   337 		// This is last, as behaviour of this varies with OS userland and PHP version
   321 		elseif (function_exists('iconv') && ($return = SimplePie_Misc::change_encoding_iconv($data, $input, $output)))
   338 		elseif (class_exists('\UConverter') && ($return = SimplePie_Misc::change_encoding_uconverter($data, $input, $output)))
   322 		{
   339 		{
   323 			return $return;
   340 			return $return;
   324 		}
   341 		}
       
   342 
   325 		// If we can't do anything, just fail
   343 		// If we can't do anything, just fail
   326 		else
   344 		return false;
   327 		{
       
   328 			return false;
       
   329 		}
       
   330 	}
   345 	}
   331 
   346 
   332 	protected static function change_encoding_mbstring($data, $input, $output)
   347 	protected static function change_encoding_mbstring($data, $input, $output)
   333 	{
   348 	{
   334 		if ($input === 'windows-949')
   349 		if ($input === 'windows-949')
   368 	}
   383 	}
   369 
   384 
   370 	protected static function change_encoding_iconv($data, $input, $output)
   385 	protected static function change_encoding_iconv($data, $input, $output)
   371 	{
   386 	{
   372 		return @iconv($input, $output, $data);
   387 		return @iconv($input, $output, $data);
       
   388 	}
       
   389 
       
   390 	/**
       
   391 	 * @param string $data
       
   392 	 * @param string $input
       
   393 	 * @param string $output
       
   394 	 * @return string|false
       
   395 	 */
       
   396 	protected static function change_encoding_uconverter($data, $input, $output)
       
   397 	{
       
   398 		return @\UConverter::transcode($data, $output, $input);
   373 	}
   399 	}
   374 
   400 
   375 	/**
   401 	/**
   376 	 * Normalize an encoding name
   402 	 * Normalize an encoding name
   377 	 *
   403 	 *
  1824 	{
  1850 	{
  1825 		if (($pos = strpos($mime, ';')) === false)
  1851 		if (($pos = strpos($mime, ';')) === false)
  1826 		{
  1852 		{
  1827 			return trim($mime);
  1853 			return trim($mime);
  1828 		}
  1854 		}
  1829 		else
  1855 
  1830 		{
  1856 		return trim(substr($mime, 0, $pos));
  1831 			return trim(substr($mime, 0, $pos));
       
  1832 		}
       
  1833 	}
  1857 	}
  1834 
  1858 
  1835 	public static function atom_03_construct_type($attribs)
  1859 	public static function atom_03_construct_type($attribs)
  1836 	{
  1860 	{
  1837 		if (isset($attribs['']['mode']) && strtolower(trim($attribs['']['mode']) === 'base64'))
  1861 		if (isset($attribs['']['mode']) && strtolower(trim($attribs['']['mode']) === 'base64'))
  1860 
  1884 
  1861 				default:
  1885 				default:
  1862 					return SIMPLEPIE_CONSTRUCT_NONE | $mode;
  1886 					return SIMPLEPIE_CONSTRUCT_NONE | $mode;
  1863 			}
  1887 			}
  1864 		}
  1888 		}
  1865 		else
  1889 
  1866 		{
  1890 		return SIMPLEPIE_CONSTRUCT_TEXT | $mode;
  1867 			return SIMPLEPIE_CONSTRUCT_TEXT | $mode;
       
  1868 		}
       
  1869 	}
  1891 	}
  1870 
  1892 
  1871 	public static function atom_10_construct_type($attribs)
  1893 	public static function atom_10_construct_type($attribs)
  1872 	{
  1894 	{
  1873 		if (isset($attribs['']['type']))
  1895 		if (isset($attribs['']['type']))
  1913 			else
  1935 			else
  1914 			{
  1936 			{
  1915 				return SIMPLEPIE_CONSTRUCT_BASE64;
  1937 				return SIMPLEPIE_CONSTRUCT_BASE64;
  1916 			}
  1938 			}
  1917 		}
  1939 		}
  1918 		else
  1940 
  1919 		{
  1941 		return SIMPLEPIE_CONSTRUCT_TEXT;
  1920 			return SIMPLEPIE_CONSTRUCT_TEXT;
       
  1921 		}
       
  1922 	}
  1942 	}
  1923 
  1943 
  1924 	public static function is_isegment_nz_nc($string)
  1944 	public static function is_isegment_nz_nc($string)
  1925 	{
  1945 	{
  1926 		return (bool) preg_match('/^([A-Za-z0-9\-._~\x{A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}\x{10000}-\x{1FFFD}\x{20000}-\x{2FFFD}\x{30000}-\x{3FFFD}\x{40000}-\x{4FFFD}\x{50000}-\x{5FFFD}\x{60000}-\x{6FFFD}\x{70000}-\x{7FFFD}\x{80000}-\x{8FFFD}\x{90000}-\x{9FFFD}\x{A0000}-\x{AFFFD}\x{B0000}-\x{BFFFD}\x{C0000}-\x{CFFFD}\x{D0000}-\x{DFFFD}\x{E1000}-\x{EFFFD}!$&\'()*+,;=@]|(%[0-9ABCDEF]{2}))+$/u', $string);
  1946 		return (bool) preg_match('/^([A-Za-z0-9\-._~\x{A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}\x{10000}-\x{1FFFD}\x{20000}-\x{2FFFD}\x{30000}-\x{3FFFD}\x{40000}-\x{4FFFD}\x{50000}-\x{5FFFD}\x{60000}-\x{6FFFD}\x{70000}-\x{7FFFD}\x{80000}-\x{8FFFD}\x{90000}-\x{9FFFD}\x{A0000}-\x{AFFFD}\x{B0000}-\x{BFFFD}\x{C0000}-\x{CFFFD}\x{D0000}-\x{DFFFD}\x{E1000}-\x{EFFFD}!$&\'()*+,;=@]|(%[0-9ABCDEF]{2}))+$/u', $string);
  1927 	}
  1947 	}
  1928 
  1948 
  1929 	public static function space_seperated_tokens($string)
  1949 	public static function space_separated_tokens($string)
  1930 	{
  1950 	{
  1931 		$space_characters = "\x20\x09\x0A\x0B\x0C\x0D";
  1951 		$space_characters = "\x20\x09\x0A\x0B\x0C\x0D";
  1932 		$string_length = strlen($string);
  1952 		$string_length = strlen($string);
  1933 
  1953 
  1934 		$position = strspn($string, $space_characters);
  1954 		$position = strspn($string, $space_characters);
  1973 		}
  1993 		}
  1974 		else if ($codepoint <= 0x10ffff)
  1994 		else if ($codepoint <= 0x10ffff)
  1975 		{
  1995 		{
  1976 			return chr(0xf0 | ($codepoint >> 18)) . chr(0x80 | (($codepoint >> 12) & 0x3f)) . chr(0x80 | (($codepoint >> 6) & 0x3f)) . chr(0x80 | ($codepoint & 0x3f));
  1996 			return chr(0xf0 | ($codepoint >> 18)) . chr(0x80 | (($codepoint >> 12) & 0x3f)) . chr(0x80 | (($codepoint >> 6) & 0x3f)) . chr(0x80 | ($codepoint & 0x3f));
  1977 		}
  1997 		}
  1978 		else
  1998 
  1979 		{
  1999 		// U+FFFD REPLACEMENT CHARACTER
  1980 			// U+FFFD REPLACEMENT CHARACTER
  2000 		return "\xEF\xBF\xBD";
  1981 			return "\xEF\xBF\xBD";
       
  1982 		}
       
  1983 	}
  2001 	}
  1984 
  2002 
  1985 	/**
  2003 	/**
  1986 	 * Similar to parse_str()
  2004 	 * Similar to parse_str()
  1987 	 *
  2005 	 *
  2181 		}
  2199 		}
  2182 		elseif (file_exists(dirname(__FILE__) . '/Core.php'))
  2200 		elseif (file_exists(dirname(__FILE__) . '/Core.php'))
  2183 		{
  2201 		{
  2184 			return filemtime(dirname(__FILE__) . '/Core.php');
  2202 			return filemtime(dirname(__FILE__) . '/Core.php');
  2185 		}
  2203 		}
  2186 		else
  2204 
  2187 		{
  2205 		return filemtime(__FILE__);
  2188 			return filemtime(__FILE__);
       
  2189 		}
       
  2190 	}
  2206 	}
  2191 
  2207 
  2192 	/**
  2208 	/**
  2193 	 * Format debugging information
  2209 	 * Format debugging information
  2194 	 */
  2210 	 */
  2242 	public static function silence_errors($num, $str)
  2258 	public static function silence_errors($num, $str)
  2243 	{
  2259 	{
  2244 		// No-op
  2260 		// No-op
  2245 	}
  2261 	}
  2246 }
  2262 }
  2247