3 * IRI parser/serialiser/normaliser
6 * @subpackage Utilities
10 * IRI parser/serialiser/normaliser
12 * Copyright (c) 2007-2010, Geoffrey Sneddon and Steve Minutillo.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are met:
18 * * Redistributions of source code must retain the above copyright notice,
19 * this list of conditions and the following disclaimer.
21 * * Redistributions in binary form must reproduce the above copyright notice,
22 * this list of conditions and the following disclaimer in the documentation
23 * and/or other materials provided with the distribution.
25 * * Neither the name of the SimplePie Team nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
33 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
42 * @subpackage Utilities
43 * @author Geoffrey Sneddon
44 * @author Steve Minutillo
45 * @copyright 2007-2009 Geoffrey Sneddon and Steve Minutillo
46 * @license http://www.opensource.org/licenses/bsd-license.php
47 * @link http://hg.gsnedders.com/iri/
56 protected $scheme = null;
63 protected $iuserinfo = null;
70 protected $ihost = null;
77 protected $port = null;
84 protected $ipath = '';
91 protected $iquery = null;
98 protected $ifragment = null;
101 * Normalization database
103 * Each key is the scheme, each value is an array with each key as the IRI
104 * part and value as the default value for that part.
106 protected $normalization = array(
114 'ihost' => 'localhost'
125 * Return the entire IRI when you try and read the object as a string
129 public function __toString()
131 return $this->get_iri();
135 * Overload __set() to provide access via properties
137 * @param string $name Property name
138 * @param mixed $value Property value
140 public function __set($name, $value)
142 if (method_exists($this, 'set_' . $name))
144 call_user_func(array($this, 'set_' . $name), $value);
147 $name === 'iauthority'
148 || $name === 'iuserinfo'
151 || $name === 'iquery'
152 || $name === 'ifragment'
155 call_user_func(array($this, 'set_' . substr($name, 1)), $value);
160 * Overload __get() to provide access via properties
162 * @param string $name Property name
165 public function __get($name)
167 // isset() returns false for null, we don't want to do that
168 // Also why we use array_key_exists below instead of isset()
169 $props = get_object_vars($this);
174 $name === 'iauthority' ||
175 $name === 'authority'
178 $return = $this->{"get_$name"}();
180 elseif (array_key_exists($name, $props))
182 $return = $this->$name;
185 elseif (($prop = 'i' . $name) && array_key_exists($prop, $props))
188 $return = $this->$prop;
191 elseif (($prop = substr($name, 1)) && array_key_exists($prop, $props))
194 $return = $this->$prop;
198 trigger_error('Undefined property: ' . get_class($this) . '::' . $name, E_USER_NOTICE);
202 if ($return === null && isset($this->normalization[$this->scheme][$name]))
204 return $this->normalization[$this->scheme][$name];
213 * Overload __isset() to provide access via properties
215 * @param string $name Property name
218 public function __isset($name)
220 if (method_exists($this, 'get_' . $name) || isset($this->$name))
231 * Overload __unset() to provide access via properties
233 * @param string $name Property name
235 public function __unset($name)
237 if (method_exists($this, 'set_' . $name))
239 call_user_func(array($this, 'set_' . $name), '');
244 * Create a new IRI object, from a specified string
248 public function __construct($iri = null)
250 $this->set_iri($iri);
254 * Create a new IRI object by resolving a relative IRI
256 * Returns false if $base is not absolute, otherwise an IRI.
258 * @param IRI|string $base (Absolute) Base IRI
259 * @param IRI|string $relative Relative IRI
262 public static function absolutize($base, $relative)
264 if (!($relative instanceof Requests_IRI))
266 $relative = new Requests_IRI($relative);
268 if (!$relative->is_valid())
272 elseif ($relative->scheme !== null)
274 return clone $relative;
278 if (!($base instanceof Requests_IRI))
280 $base = new Requests_IRI($base);
282 if ($base->scheme !== null && $base->is_valid())
284 if ($relative->get_iri() !== '')
286 if ($relative->iuserinfo !== null || $relative->ihost !== null || $relative->port !== null)
288 $target = clone $relative;
289 $target->scheme = $base->scheme;
293 $target = new Requests_IRI;
294 $target->scheme = $base->scheme;
295 $target->iuserinfo = $base->iuserinfo;
296 $target->ihost = $base->ihost;
297 $target->port = $base->port;
298 if ($relative->ipath !== '')
300 if ($relative->ipath[0] === '/')
302 $target->ipath = $relative->ipath;
304 elseif (($base->iuserinfo !== null || $base->ihost !== null || $base->port !== null) && $base->ipath === '')
306 $target->ipath = '/' . $relative->ipath;
308 elseif (($last_segment = strrpos($base->ipath, '/')) !== false)
310 $target->ipath = substr($base->ipath, 0, $last_segment + 1) . $relative->ipath;
314 $target->ipath = $relative->ipath;
316 $target->ipath = $target->remove_dot_segments($target->ipath);
317 $target->iquery = $relative->iquery;
321 $target->ipath = $base->ipath;
322 if ($relative->iquery !== null)
324 $target->iquery = $relative->iquery;
326 elseif ($base->iquery !== null)
328 $target->iquery = $base->iquery;
331 $target->ifragment = $relative->ifragment;
336 $target = clone $base;
337 $target->ifragment = null;
339 $target->scheme_normalization();
350 * Parse an IRI into scheme/authority/path/query/fragment segments
355 protected function parse_iri($iri)
357 $iri = trim($iri, "\x20\x09\x0A\x0C\x0D");
358 if (preg_match('/^((?P<scheme>[^:\/?#]+):)?(\/\/(?P<authority>[^\/?#]*))?(?P<path>[^?#]*)(\?(?P<query>[^#]*))?(#(?P<fragment>.*))?$/', $iri, $match))
360 if ($match[1] === '')
362 $match['scheme'] = null;
364 if (!isset($match[3]) || $match[3] === '')
366 $match['authority'] = null;
368 if (!isset($match[5]))
372 if (!isset($match[6]) || $match[6] === '')
374 $match['query'] = null;
376 if (!isset($match[8]) || $match[8] === '')
378 $match['fragment'] = null;
384 trigger_error('This should never happen', E_USER_ERROR);
390 * Remove dot segments from a path
392 * @param string $input
395 protected function remove_dot_segments($input)
398 while (strpos($input, './') !== false || strpos($input, '/.') !== false || $input === '.' || $input === '..')
400 // A: If the input buffer begins with a prefix of "../" or "./", then remove that prefix from the input buffer; otherwise,
401 if (strpos($input, '../') === 0)
403 $input = substr($input, 3);
405 elseif (strpos($input, './') === 0)
407 $input = substr($input, 2);
409 // 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,
410 elseif (strpos($input, '/./') === 0)
412 $input = substr($input, 2);
414 elseif ($input === '/.')
418 // 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,
419 elseif (strpos($input, '/../') === 0)
421 $input = substr($input, 3);
422 $output = substr_replace($output, '', strrpos($output, '/'));
424 elseif ($input === '/..')
427 $output = substr_replace($output, '', strrpos($output, '/'));
429 // D: if the input buffer consists only of "." or "..", then remove that from the input buffer; otherwise,
430 elseif ($input === '.' || $input === '..')
434 // 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
435 elseif (($pos = strpos($input, '/', 1)) !== false)
437 $output .= substr($input, 0, $pos);
438 $input = substr_replace($input, '', 0, $pos);
446 return $output . $input;
450 * Replace invalid character with percent encoding
452 * @param string $string Input string
453 * @param string $extra_chars Valid characters not in iunreserved or
454 * iprivate (this is ASCII-only)
455 * @param bool $iprivate Allow iprivate
458 protected function replace_invalid_with_pct_encoding($string, $extra_chars, $iprivate = false)
460 // Normalize as many pct-encoded sections as possible
461 $string = preg_replace_callback('/(?:%[A-Fa-f0-9]{2})+/', array(&$this, 'remove_iunreserved_percent_encoded'), $string);
463 // Replace invalid percent characters
464 $string = preg_replace('/%(?![A-Fa-f0-9]{2})/', '%25', $string);
466 // Add unreserved and % to $extra_chars (the latter is safe because all
467 // pct-encoded sections are now valid).
468 $extra_chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~%';
470 // Now replace any bytes that aren't allowed with their pct-encoded versions
472 $strlen = strlen($string);
473 while (($position += strspn($string, $extra_chars, $position)) < $strlen)
475 $value = ord($string[$position]);
480 // By default we are valid
483 // No one byte sequences are valid due to the while.
484 // Two byte sequence:
485 if (($value & 0xE0) === 0xC0)
487 $character = ($value & 0x1F) << 6;
491 // Three byte sequence:
492 elseif (($value & 0xF0) === 0xE0)
494 $character = ($value & 0x0F) << 12;
498 // Four byte sequence:
499 elseif (($value & 0xF8) === 0xF0)
501 $character = ($value & 0x07) << 18;
515 if ($position + $length <= $strlen)
517 for ($position++; $remaining; $position++)
519 $value = ord($string[$position]);
521 // Check that the byte is valid, then add it to the character:
522 if (($value & 0xC0) === 0x80)
524 $character |= ($value & 0x3F) << (--$remaining * 6);
526 // If it is invalid, count the sequence as invalid and reprocess the current byte:
537 $position = $strlen - 1;
542 // Percent encode anything invalid or not in ucschar
546 // Non-shortest form sequences are invalid
547 || $length > 1 && $character <= 0x7F
548 || $length > 2 && $character <= 0x7FF
549 || $length > 3 && $character <= 0xFFFF
550 // Outside of range of ucschar codepoints
552 || ($character & 0xFFFE) === 0xFFFE
553 || $character >= 0xFDD0 && $character <= 0xFDEF
555 // Everything else not in ucschar
556 $character > 0xD7FF && $character < 0xF900
558 || $character > 0xEFFFD
561 // Everything not in iprivate, if it applies
563 || $character < 0xE000
564 || $character > 0x10FFFD
568 // If we were a character, pretend we weren't, but rather an error.
572 for ($j = $start; $j <= $position; $j++)
574 $string = substr_replace($string, sprintf('%%%02X', ord($string[$j])), $j, 1);
586 * Callback function for preg_replace_callback.
588 * Removes sequences of percent encoded bytes that represent UTF-8
589 * encoded characters in iunreserved
591 * @param array $match PCRE match
592 * @return string Replacement
594 protected function remove_iunreserved_percent_encoded($match)
596 // As we just have valid percent encoded sequences we can just explode
597 // and ignore the first member of the returned array (an empty string).
598 $bytes = explode('%', $match[0]);
600 // Initialize the new string (this is what will be returned) and that
601 // there are no bytes remaining in the current sequence (unsurprising
602 // at the first byte!).
606 // Loop over each and every byte, and set $value to its value
607 for ($i = 1, $len = count($bytes); $i < $len; $i++)
609 $value = hexdec($bytes[$i]);
611 // If we're the first byte of sequence:
617 // By default we are valid
620 // One byte sequence:
626 // Two byte sequence:
627 elseif (($value & 0xE0) === 0xC0)
629 $character = ($value & 0x1F) << 6;
633 // Three byte sequence:
634 elseif (($value & 0xF0) === 0xE0)
636 $character = ($value & 0x0F) << 12;
640 // Four byte sequence:
641 elseif (($value & 0xF8) === 0xF0)
643 $character = ($value & 0x07) << 18;
654 // Continuation byte:
657 // Check that the byte is valid, then add it to the character:
658 if (($value & 0xC0) === 0x80)
661 $character |= ($value & 0x3F) << ($remaining * 6);
663 // If it is invalid, count the sequence as invalid and reprocess the current byte as the start of a sequence:
672 // If we've reached the end of the current byte sequence, append it to Unicode::$data
675 // Percent encode anything invalid or not in iunreserved
679 // Non-shortest form sequences are invalid
680 || $length > 1 && $character <= 0x7F
681 || $length > 2 && $character <= 0x7FF
682 || $length > 3 && $character <= 0xFFFF
683 // Outside of range of iunreserved codepoints
685 || $character > 0xEFFFD
687 || ($character & 0xFFFE) === 0xFFFE
688 || $character >= 0xFDD0 && $character <= 0xFDEF
689 // Everything else not in iunreserved (this is all BMP)
690 || $character === 0x2F
691 || $character > 0x39 && $character < 0x41
692 || $character > 0x5A && $character < 0x61
693 || $character > 0x7A && $character < 0x7E
694 || $character > 0x7E && $character < 0xA0
695 || $character > 0xD7FF && $character < 0xF900
698 for ($j = $start; $j <= $i; $j++)
700 $string .= '%' . strtoupper($bytes[$j]);
705 for ($j = $start; $j <= $i; $j++)
707 $string .= chr(hexdec($bytes[$j]));
713 // If we have any bytes left over they are invalid (i.e., we are
714 // mid-way through a multi-byte sequence)
717 for ($j = $start; $j < $len; $j++)
719 $string .= '%' . strtoupper($bytes[$j]);
726 protected function scheme_normalization()
728 if (isset($this->normalization[$this->scheme]['iuserinfo']) && $this->iuserinfo === $this->normalization[$this->scheme]['iuserinfo'])
730 $this->iuserinfo = null;
732 if (isset($this->normalization[$this->scheme]['ihost']) && $this->ihost === $this->normalization[$this->scheme]['ihost'])
736 if (isset($this->normalization[$this->scheme]['port']) && $this->port === $this->normalization[$this->scheme]['port'])
740 if (isset($this->normalization[$this->scheme]['ipath']) && $this->ipath === $this->normalization[$this->scheme]['ipath'])
744 if (isset($this->ihost) && empty($this->ipath))
748 if (isset($this->normalization[$this->scheme]['iquery']) && $this->iquery === $this->normalization[$this->scheme]['iquery'])
750 $this->iquery = null;
752 if (isset($this->normalization[$this->scheme]['ifragment']) && $this->ifragment === $this->normalization[$this->scheme]['ifragment'])
754 $this->ifragment = null;
759 * Check if the object represents a valid IRI. This needs to be done on each
760 * call as some things change depending on another part of the IRI.
764 public function is_valid()
766 $isauthority = $this->iuserinfo !== null || $this->ihost !== null || $this->port !== null;
767 if ($this->ipath !== '' &&
770 $this->ipath[0] !== '/' ||
771 substr($this->ipath, 0, 2) === '//'
774 $this->scheme === null &&
776 strpos($this->ipath, ':') !== false &&
777 (strpos($this->ipath, '/') === false ? true : strpos($this->ipath, ':') < strpos($this->ipath, '/'))
789 * Set the entire IRI. Returns true on success, false on failure (if there
790 * are any invalid characters).
795 protected function set_iri($iri)
807 elseif (isset($cache[$iri]))
816 $return) = $cache[$iri];
821 $parsed = $this->parse_iri((string) $iri);
823 $return = $this->set_scheme($parsed['scheme'])
824 && $this->set_authority($parsed['authority'])
825 && $this->set_path($parsed['path'])
826 && $this->set_query($parsed['query'])
827 && $this->set_fragment($parsed['fragment']);
829 $cache[$iri] = array($this->scheme,
842 * Set the scheme. Returns true on success, false on failure (if there are
843 * any invalid characters).
845 * @param string $scheme
848 protected function set_scheme($scheme)
850 if ($scheme === null)
852 $this->scheme = null;
854 elseif (!preg_match('/^[A-Za-z][0-9A-Za-z+\-.]*$/', $scheme))
856 $this->scheme = null;
861 $this->scheme = strtolower($scheme);
867 * Set the authority. Returns true on success, false on failure (if there are
868 * any invalid characters).
870 * @param string $authority
873 protected function set_authority($authority)
879 if ($authority === null)
881 $this->iuserinfo = null;
886 elseif (isset($cache[$authority]))
888 list($this->iuserinfo,
891 $return) = $cache[$authority];
897 $remaining = $authority;
898 if (($iuserinfo_end = strrpos($remaining, '@')) !== false)
900 $iuserinfo = substr($remaining, 0, $iuserinfo_end);
901 $remaining = substr($remaining, $iuserinfo_end + 1);
907 if (($port_start = strpos($remaining, ':', strpos($remaining, ']'))) !== false)
909 if (($port = substr($remaining, $port_start + 1)) === false)
913 $remaining = substr($remaining, 0, $port_start);
920 $return = $this->set_userinfo($iuserinfo) &&
921 $this->set_host($remaining) &&
922 $this->set_port($port);
924 $cache[$authority] = array($this->iuserinfo,
936 * @param string $iuserinfo
939 protected function set_userinfo($iuserinfo)
941 if ($iuserinfo === null)
943 $this->iuserinfo = null;
947 $this->iuserinfo = $this->replace_invalid_with_pct_encoding($iuserinfo, '!$&\'()*+,;=:');
948 $this->scheme_normalization();
955 * Set the ihost. Returns true on success, false on failure (if there are
956 * any invalid characters).
958 * @param string $ihost
961 protected function set_host($ihost)
968 elseif (substr($ihost, 0, 1) === '[' && substr($ihost, -1) === ']')
970 if (Requests_IPv6::check_ipv6(substr($ihost, 1, -1)))
972 $this->ihost = '[' . Requests_IPv6::compress(substr($ihost, 1, -1)) . ']';
982 $ihost = $this->replace_invalid_with_pct_encoding($ihost, '!$&\'()*+,;=');
984 // Lowercase, but ignore pct-encoded sections (as they should
985 // remain uppercase). This must be done after the previous step
986 // as that can add unescaped characters.
988 $strlen = strlen($ihost);
989 while (($position += strcspn($ihost, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ%', $position)) < $strlen)
991 if ($ihost[$position] === '%')
997 $ihost[$position] = strtolower($ihost[$position]);
1002 $this->ihost = $ihost;
1005 $this->scheme_normalization();
1011 * Set the port. Returns true on success, false on failure (if there are
1012 * any invalid characters).
1014 * @param string $port
1017 protected function set_port($port)
1024 elseif (strspn($port, '0123456789') === strlen($port))
1026 $this->port = (int) $port;
1027 $this->scheme_normalization();
1040 * @param string $ipath
1043 protected function set_path($ipath)
1051 $ipath = (string) $ipath;
1053 if (isset($cache[$ipath]))
1055 $this->ipath = $cache[$ipath][(int) ($this->scheme !== null)];
1059 $valid = $this->replace_invalid_with_pct_encoding($ipath, '!$&\'()*+,;=@:/');
1060 $removed = $this->remove_dot_segments($valid);
1062 $cache[$ipath] = array($valid, $removed);
1063 $this->ipath = ($this->scheme !== null) ? $removed : $valid;
1065 $this->scheme_normalization();
1072 * @param string $iquery
1075 protected function set_query($iquery)
1077 if ($iquery === null)
1079 $this->iquery = null;
1083 $this->iquery = $this->replace_invalid_with_pct_encoding($iquery, '!$&\'()*+,;=:@/?', true);
1084 $this->scheme_normalization();
1090 * Set the ifragment.
1092 * @param string $ifragment
1095 protected function set_fragment($ifragment)
1097 if ($ifragment === null)
1099 $this->ifragment = null;
1103 $this->ifragment = $this->replace_invalid_with_pct_encoding($ifragment, '!$&\'()*+,;=:@/?');
1104 $this->scheme_normalization();
1110 * Convert an IRI to a URI (or parts thereof)
1114 protected function to_uri($string)
1119 $non_ascii = implode('', range("\x80", "\xFF"));
1123 $strlen = strlen($string);
1124 while (($position += strcspn($string, $non_ascii, $position)) < $strlen)
1126 $string = substr_replace($string, sprintf('%%%02X', ord($string[$position])), $position, 1);
1135 * Get the complete IRI
1139 protected function get_iri()
1141 if (!$this->is_valid())
1147 if ($this->scheme !== null)
1149 $iri .= $this->scheme . ':';
1151 if (($iauthority = $this->get_iauthority()) !== null)
1153 $iri .= '//' . $iauthority;
1155 $iri .= $this->ipath;
1156 if ($this->iquery !== null)
1158 $iri .= '?' . $this->iquery;
1160 if ($this->ifragment !== null)
1162 $iri .= '#' . $this->ifragment;
1169 * Get the complete URI
1173 protected function get_uri()
1175 return $this->to_uri($this->get_iri());
1179 * Get the complete iauthority
1183 protected function get_iauthority()
1185 if ($this->iuserinfo !== null || $this->ihost !== null || $this->port !== null)
1188 if ($this->iuserinfo !== null)
1190 $iauthority .= $this->iuserinfo . '@';
1192 if ($this->ihost !== null)
1194 $iauthority .= $this->ihost;
1196 if ($this->port !== null)
1198 $iauthority .= ':' . $this->port;
1209 * Get the complete authority
1213 protected function get_authority()
1215 $iauthority = $this->get_iauthority();
1216 if (is_string($iauthority))
1217 return $this->to_uri($iauthority);