wp/wp-includes/SimplePie/IRI.php
changeset 16 a86126ab1dd4
parent 0 d970ebf37754
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 
    45 /**
    44 /**
    46  * IRI parser/serialiser/normaliser
    45  * IRI parser/serialiser/normaliser
    47  *
    46  *
    48  * @package SimplePie
    47  * @package SimplePie
    49  * @subpackage HTTP
    48  * @subpackage HTTP
    50  * @author Geoffrey Sneddon
    49  * @author Sam Sneddon
    51  * @author Steve Minutillo
    50  * @author Steve Minutillo
    52  * @author Ryan McCue
    51  * @author Ryan McCue
    53  * @copyright 2007-2012 Geoffrey Sneddon, Steve Minutillo, Ryan McCue
    52  * @copyright 2007-2012 Sam Sneddon, Steve Minutillo, Ryan McCue
    54  * @license http://www.opensource.org/licenses/bsd-license.php
    53  * @license http://www.opensource.org/licenses/bsd-license.php
    55  */
    54  */
    56 class SimplePie_IRI
    55 class SimplePie_IRI
    57 {
    56 {
    58 	/**
    57 	/**
   210 
   209 
   211 		if ($return === null && isset($this->normalization[$this->scheme][$name]))
   210 		if ($return === null && isset($this->normalization[$this->scheme][$name]))
   212 		{
   211 		{
   213 			return $this->normalization[$this->scheme][$name];
   212 			return $this->normalization[$this->scheme][$name];
   214 		}
   213 		}
   215 		else
   214 
   216 		{
   215 		return $return;
   217 			return $return;
       
   218 		}
       
   219 	}
   216 	}
   220 
   217 
   221 	/**
   218 	/**
   222 	 * Overload __isset() to provide access via properties
   219 	 * Overload __isset() to provide access via properties
   223 	 *
   220 	 *
   224 	 * @param string $name Property name
   221 	 * @param string $name Property name
   225 	 * @return bool
   222 	 * @return bool
   226 	 */
   223 	 */
   227 	public function __isset($name)
   224 	public function __isset($name)
   228 	{
   225 	{
   229 		if (method_exists($this, 'get_' . $name) || isset($this->$name))
   226 		return method_exists($this, 'get_' . $name) || isset($this->$name);
   230 		{
       
   231 			return true;
       
   232 		}
       
   233 		else
       
   234 		{
       
   235 			return false;
       
   236 		}
       
   237 	}
   227 	}
   238 
   228 
   239 	/**
   229 	/**
   240 	 * Overload __unset() to provide access via properties
   230 	 * Overload __unset() to provide access via properties
   241 	 *
   231 	 *
   255 	 * @param string $iri
   245 	 * @param string $iri
   256 	 */
   246 	 */
   257 	public function __construct($iri = null)
   247 	public function __construct($iri = null)
   258 	{
   248 	{
   259 		$this->set_iri($iri);
   249 		$this->set_iri($iri);
       
   250 	}
       
   251 
       
   252 	/**
       
   253 	 * Clean up
       
   254 	 */
       
   255 	public function __destruct() {
       
   256 	    $this->set_iri(null, true);
       
   257 	    $this->set_path(null, true);
       
   258 	    $this->set_authority(null, true);
   260 	}
   259 	}
   261 
   260 
   262 	/**
   261 	/**
   263 	 * Create a new IRI object by resolving a relative IRI
   262 	 * Create a new IRI object by resolving a relative IRI
   264 	 *
   263 	 *
   346 					$target->ifragment = null;
   345 					$target->ifragment = null;
   347 				}
   346 				}
   348 				$target->scheme_normalization();
   347 				$target->scheme_normalization();
   349 				return $target;
   348 				return $target;
   350 			}
   349 			}
   351 			else
   350 
   352 			{
   351 			return false;
   353 				return false;
       
   354 			}
       
   355 		}
   352 		}
   356 	}
   353 	}
   357 
   354 
   358 	/**
   355 	/**
   359 	 * Parse an IRI into scheme/authority/path/query/fragment segments
   356 	 * Parse an IRI into scheme/authority/path/query/fragment segments
   386 			{
   383 			{
   387 				$match['fragment'] = null;
   384 				$match['fragment'] = null;
   388 			}
   385 			}
   389 			return $match;
   386 			return $match;
   390 		}
   387 		}
   391 		else
   388 
   392 		{
   389 		// This can occur when a paragraph is accidentally parsed as a URI
   393 			// This can occur when a paragraph is accidentally parsed as a URI
   390 		return false;
   394 			return false;
       
   395 		}
       
   396 	}
   391 	}
   397 
   392 
   398 	/**
   393 	/**
   399 	 * Remove dot segments from a path
   394 	 * Remove dot segments from a path
   400 	 *
   395 	 *
   766 	 *
   761 	 *
   767 	 * @return bool
   762 	 * @return bool
   768 	 */
   763 	 */
   769 	public function is_valid()
   764 	public function is_valid()
   770 	{
   765 	{
   771 		$isauthority = $this->iuserinfo !== null || $this->ihost !== null || $this->port !== null;
   766 		if ($this->ipath === '') return true;
   772 		if ($this->ipath !== '' &&
   767 
   773 			(
   768 		$isauthority = $this->iuserinfo !== null || $this->ihost !== null ||
   774 				$isauthority && (
   769 			$this->port !== null;
   775 					$this->ipath[0] !== '/' ||
   770 		if ($isauthority && $this->ipath[0] === '/') return true;
   776 					substr($this->ipath, 0, 2) === '//'
   771 
   777 				) ||
   772 		if (!$isauthority && (substr($this->ipath, 0, 2) === '//')) return false;
   778 				(
   773 
   779 					$this->scheme === null &&
   774 		// Relative urls cannot have a colon in the first path segment (and the
   780 					!$isauthority &&
   775 		// slashes themselves are not included so skip the first character).
   781 					strpos($this->ipath, ':') !== false &&
   776 		if (!$this->scheme && !$isauthority &&
   782 					(strpos($this->ipath, '/') === false ? true : strpos($this->ipath, ':') < strpos($this->ipath, '/'))
   777 		    strpos($this->ipath, ':') !== false &&
   783 				)
   778 		    strpos($this->ipath, '/', 1) !== false &&
   784 			)
   779 		    strpos($this->ipath, ':') < strpos($this->ipath, '/', 1)) return false;
   785 		)
       
   786 		{
       
   787 			return false;
       
   788 		}
       
   789 
   780 
   790 		return true;
   781 		return true;
   791 	}
   782 	}
   792 
   783 
   793 	/**
   784 	/**
   795 	 * are any invalid characters).
   786 	 * are any invalid characters).
   796 	 *
   787 	 *
   797 	 * @param string $iri
   788 	 * @param string $iri
   798 	 * @return bool
   789 	 * @return bool
   799 	 */
   790 	 */
   800 	public function set_iri($iri)
   791 	public function set_iri($iri, $clear_cache = false)
   801 	{
   792 	{
   802 		static $cache;
   793 		static $cache;
       
   794 		if ($clear_cache)
       
   795 		{
       
   796 			$cache = null;
       
   797 			return;
       
   798 		}
   803 		if (!$cache)
   799 		if (!$cache)
   804 		{
   800 		{
   805 			$cache = array();
   801 			$cache = array();
   806 		}
   802 		}
   807 
   803 
   819 				 $this->iquery,
   815 				 $this->iquery,
   820 				 $this->ifragment,
   816 				 $this->ifragment,
   821 				 $return) = $cache[$iri];
   817 				 $return) = $cache[$iri];
   822 			return $return;
   818 			return $return;
   823 		}
   819 		}
   824 		else
   820 
   825 		{
   821 		$parsed = $this->parse_iri((string) $iri);
   826 			$parsed = $this->parse_iri((string) $iri);
   822 		if (!$parsed)
   827 			if (!$parsed)
   823 		{
   828 			{
   824 			return false;
   829 				return false;
   825 		}
   830 			}
   826 
   831 
   827 		$return = $this->set_scheme($parsed['scheme'])
   832 			$return = $this->set_scheme($parsed['scheme'])
   828 			&& $this->set_authority($parsed['authority'])
   833 				&& $this->set_authority($parsed['authority'])
   829 			&& $this->set_path($parsed['path'])
   834 				&& $this->set_path($parsed['path'])
   830 			&& $this->set_query($parsed['query'])
   835 				&& $this->set_query($parsed['query'])
   831 			&& $this->set_fragment($parsed['fragment']);
   836 				&& $this->set_fragment($parsed['fragment']);
   832 
   837 
   833 		$cache[$iri] = array($this->scheme,
   838 			$cache[$iri] = array($this->scheme,
   834 							 $this->iuserinfo,
   839 								 $this->iuserinfo,
   835 							 $this->ihost,
   840 								 $this->ihost,
   836 							 $this->port,
   841 								 $this->port,
   837 							 $this->ipath,
   842 								 $this->ipath,
   838 							 $this->iquery,
   843 								 $this->iquery,
   839 							 $this->ifragment,
   844 								 $this->ifragment,
   840 							 $return);
   845 								 $return);
   841 		return $return;
   846 			return $return;
       
   847 		}
       
   848 	}
   842 	}
   849 
   843 
   850 	/**
   844 	/**
   851 	 * Set the scheme. Returns true on success, false on failure (if there are
   845 	 * Set the scheme. Returns true on success, false on failure (if there are
   852 	 * any invalid characters).
   846 	 * any invalid characters).
   877 	 * any invalid characters).
   871 	 * any invalid characters).
   878 	 *
   872 	 *
   879 	 * @param string $authority
   873 	 * @param string $authority
   880 	 * @return bool
   874 	 * @return bool
   881 	 */
   875 	 */
   882 	public function set_authority($authority)
   876 	public function set_authority($authority, $clear_cache = false)
   883 	{
   877 	{
   884 		static $cache;
   878 		static $cache;
       
   879 		if ($clear_cache)
       
   880 		{
       
   881 			$cache = null;
       
   882 			return;
       
   883 		}
   885 		if (!$cache)
   884 		if (!$cache)
   886 			$cache = array();
   885 			$cache = array();
   887 
   886 
   888 		if ($authority === null)
   887 		if ($authority === null)
   889 		{
   888 		{
   899 				 $this->port,
   898 				 $this->port,
   900 				 $return) = $cache[$authority];
   899 				 $return) = $cache[$authority];
   901 
   900 
   902 			return $return;
   901 			return $return;
   903 		}
   902 		}
       
   903 
       
   904 		$remaining = $authority;
       
   905 		if (($iuserinfo_end = strrpos($remaining, '@')) !== false)
       
   906 		{
       
   907 			$iuserinfo = substr($remaining, 0, $iuserinfo_end);
       
   908 			$remaining = substr($remaining, $iuserinfo_end + 1);
       
   909 		}
   904 		else
   910 		else
   905 		{
   911 		{
   906 			$remaining = $authority;
   912 			$iuserinfo = null;
   907 			if (($iuserinfo_end = strrpos($remaining, '@')) !== false)
   913 		}
   908 			{
   914 		if (($port_start = strpos($remaining, ':', strpos($remaining, ']'))) !== false)
   909 				$iuserinfo = substr($remaining, 0, $iuserinfo_end);
   915 		{
   910 				$remaining = substr($remaining, $iuserinfo_end + 1);
   916 			if (($port = substr($remaining, $port_start + 1)) === false)
   911 			}
       
   912 			else
       
   913 			{
       
   914 				$iuserinfo = null;
       
   915 			}
       
   916 			if (($port_start = strpos($remaining, ':', strpos($remaining, ']'))) !== false)
       
   917 			{
       
   918 				if (($port = substr($remaining, $port_start + 1)) === false)
       
   919 				{
       
   920 					$port = null;
       
   921 				}
       
   922 				$remaining = substr($remaining, 0, $port_start);
       
   923 			}
       
   924 			else
       
   925 			{
   917 			{
   926 				$port = null;
   918 				$port = null;
   927 			}
   919 			}
   928 
   920 			$remaining = substr($remaining, 0, $port_start);
   929 			$return = $this->set_userinfo($iuserinfo) &&
   921 		}
   930 					  $this->set_host($remaining) &&
   922 		else
   931 					  $this->set_port($port);
   923 		{
   932 
   924 			$port = null;
   933 			$cache[$authority] = array($this->iuserinfo,
   925 		}
   934 									   $this->ihost,
   926 
   935 									   $this->port,
   927 		$return = $this->set_userinfo($iuserinfo) &&
   936 									   $return);
   928 				  $this->set_host($remaining) &&
   937 
   929 				  $this->set_port($port);
   938 			return $return;
   930 
   939 		}
   931 		$cache[$authority] = array($this->iuserinfo,
       
   932 								   $this->ihost,
       
   933 								   $this->port,
       
   934 								   $return);
       
   935 
       
   936 		return $return;
   940 	}
   937 	}
   941 
   938 
   942 	/**
   939 	/**
   943 	 * Set the iuserinfo.
   940 	 * Set the iuserinfo.
   944 	 *
   941 	 *
  1034 		{
  1031 		{
  1035 			$this->port = (int) $port;
  1032 			$this->port = (int) $port;
  1036 			$this->scheme_normalization();
  1033 			$this->scheme_normalization();
  1037 			return true;
  1034 			return true;
  1038 		}
  1035 		}
  1039 		else
  1036 
  1040 		{
  1037 		$this->port = null;
  1041 			$this->port = null;
  1038 		return false;
  1042 			return false;
       
  1043 		}
       
  1044 	}
  1039 	}
  1045 
  1040 
  1046 	/**
  1041 	/**
  1047 	 * Set the ipath.
  1042 	 * Set the ipath.
  1048 	 *
  1043 	 *
  1049 	 * @param string $ipath
  1044 	 * @param string $ipath
  1050 	 * @return bool
  1045 	 * @return bool
  1051 	 */
  1046 	 */
  1052 	public function set_path($ipath)
  1047 	public function set_path($ipath, $clear_cache = false)
  1053 	{
  1048 	{
  1054 		static $cache;
  1049 		static $cache;
       
  1050 		if ($clear_cache)
       
  1051 		{
       
  1052 			$cache = null;
       
  1053 			return;
       
  1054 		}
  1055 		if (!$cache)
  1055 		if (!$cache)
  1056 		{
  1056 		{
  1057 			$cache = array();
  1057 			$cache = array();
  1058 		}
  1058 		}
  1059 
  1059 
  1164 		}
  1164 		}
  1165 		if ($this->ipath !== '')
  1165 		if ($this->ipath !== '')
  1166 		{
  1166 		{
  1167 			$iri .= $this->ipath;
  1167 			$iri .= $this->ipath;
  1168 		}
  1168 		}
  1169 		elseif (!empty($this->normalization[$this->scheme]['ipath']) && $iauthority !== null && $iauthority !== '')
  1169         elseif (!empty($this->normalization[$this->scheme]['ipath']) && $iauthority !== null && $iauthority !== '')
  1170 		{
  1170 		{
  1171 			$iri .= $this->normalization[$this->scheme]['ipath'];
  1171 			$iri .= $this->normalization[$this->scheme]['ipath'];
  1172 		}
  1172 		}
  1173 		if ($this->iquery !== null)
  1173 		if ($this->iquery !== null)
  1174 		{
  1174 		{
  1208 			}
  1208 			}
  1209 			if ($this->ihost !== null)
  1209 			if ($this->ihost !== null)
  1210 			{
  1210 			{
  1211 				$iauthority .= $this->ihost;
  1211 				$iauthority .= $this->ihost;
  1212 			}
  1212 			}
  1213 			if ($this->port !== null)
  1213             if ($this->port !== null && $this->port !== 0)
  1214 			{
  1214 			{
  1215 				$iauthority .= ':' . $this->port;
  1215 				$iauthority .= ':' . $this->port;
  1216 			}
  1216 			}
  1217 			return $iauthority;
  1217 			return $iauthority;
  1218 		}
  1218 		}
  1219 		else
  1219 
  1220 		{
  1220 		return null;
  1221 			return null;
       
  1222 		}
       
  1223 	}
  1221 	}
  1224 
  1222 
  1225 	/**
  1223 	/**
  1226 	 * Get the complete authority
  1224 	 * Get the complete authority
  1227 	 *
  1225 	 *
  1230 	protected function get_authority()
  1228 	protected function get_authority()
  1231 	{
  1229 	{
  1232 		$iauthority = $this->get_iauthority();
  1230 		$iauthority = $this->get_iauthority();
  1233 		if (is_string($iauthority))
  1231 		if (is_string($iauthority))
  1234 			return $this->to_uri($iauthority);
  1232 			return $this->to_uri($iauthority);
  1235 		else
  1233 
  1236 			return $iauthority;
  1234 		return $iauthority;
  1237 	}
  1235 	}
  1238 }
  1236 }