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'
127 * Return the entire IRI when you try and read the object as a string
131 public function __toString()
133 return $this->get_iri();
137 * Overload __set() to provide access via properties
139 * @param string $name Property name
140 * @param mixed $value Property value
142 public function __set($name, $value)
144 if (method_exists($this, 'set_' . $name))
146 call_user_func(array($this, 'set_' . $name), $value);
149 $name === 'iauthority'
150 || $name === 'iuserinfo'
153 || $name === 'iquery'
154 || $name === 'ifragment'
157 call_user_func(array($this, 'set_' . substr($name, 1)), $value);
162 * Overload __get() to provide access via properties
164 * @param string $name Property name
167 public function __get($name)
169 // isset() returns false for null, we don't want to do that
170 // Also why we use array_key_exists below instead of isset()
171 $props = get_object_vars($this);
176 $name === 'iauthority' ||
177 $name === 'authority'
180 $return = $this->{"get_$name"}();
182 elseif (array_key_exists($name, $props))
184 $return = $this->$name;
187 elseif (($prop = 'i' . $name) && array_key_exists($prop, $props))
190 $return = $this->$prop;
193 elseif (($prop = substr($name, 1)) && array_key_exists($prop, $props))
196 $return = $this->$prop;
200 trigger_error('Undefined property: ' . get_class($this) . '::' . $name, E_USER_NOTICE);
204 if ($return === null && isset($this->normalization[$this->scheme][$name]))
206 return $this->normalization[$this->scheme][$name];
215 * Overload __isset() to provide access via properties
217 * @param string $name Property name
220 public function __isset($name)
222 if (method_exists($this, 'get_' . $name) || isset($this->$name))
233 * Overload __unset() to provide access via properties
235 * @param string $name Property name
237 public function __unset($name)
239 if (method_exists($this, 'set_' . $name))
241 call_user_func(array($this, 'set_' . $name), '');
246 * Create a new IRI object, from a specified string
250 public function __construct($iri = null)
252 $this->set_iri($iri);
256 * Create a new IRI object by resolving a relative IRI
258 * Returns false if $base is not absolute, otherwise an IRI.
260 * @param IRI|string $base (Absolute) Base IRI
261 * @param IRI|string $relative Relative IRI
264 public static function absolutize($base, $relative)
266 if (!($relative instanceof Requests_IRI))
268 $relative = new Requests_IRI($relative);
270 if (!$relative->is_valid())
274 elseif ($relative->scheme !== null)
276 return clone $relative;
280 if (!($base instanceof Requests_IRI))
282 $base = new Requests_IRI($base);
284 if ($base->scheme !== null && $base->is_valid())
286 if ($relative->get_iri() !== '')
288 if ($relative->iuserinfo !== null || $relative->ihost !== null || $relative->port !== null)
290 $target = clone $relative;
291 $target->scheme = $base->scheme;
295 $target = new Requests_IRI;
296 $target->scheme = $base->scheme;
297 $target->iuserinfo = $base->iuserinfo;
298 $target->ihost = $base->ihost;
299 $target->port = $base->port;
300 if ($relative->ipath !== '')
302 if ($relative->ipath[0] === '/')
304 $target->ipath = $relative->ipath;
306 elseif (($base->iuserinfo !== null || $base->ihost !== null || $base->port !== null) && $base->ipath === '')
308 $target->ipath = '/' . $relative->ipath;
310 elseif (($last_segment = strrpos($base->ipath, '/')) !== false)
312 $target->ipath = substr($base->ipath, 0, $last_segment + 1) . $relative->ipath;
316 $target->ipath = $relative->ipath;
318 $target->ipath = $target->remove_dot_segments($target->ipath);
319 $target->iquery = $relative->iquery;
323 $target->ipath = $base->ipath;
324 if ($relative->iquery !== null)
326 $target->iquery = $relative->iquery;
328 elseif ($base->iquery !== null)
330 $target->iquery = $base->iquery;
333 $target->ifragment = $relative->ifragment;
338 $target = clone $base;
339 $target->ifragment = null;
341 $target->scheme_normalization();
352 * Parse an IRI into scheme/authority/path/query/fragment segments
357 protected function parse_iri($iri)
359 $iri = trim($iri, "\x20\x09\x0A\x0C\x0D");
360 if (preg_match('/^((?P<scheme>[^:\/?#]+):)?(\/\/(?P<authority>[^\/?#]*))?(?P<path>[^?#]*)(\?(?P<query>[^#]*))?(#(?P<fragment>.*))?$/', $iri, $match))
362 if ($match[1] === '')
364 $match['scheme'] = null;
366 if (!isset($match[3]) || $match[3] === '')
368 $match['authority'] = null;
370 if (!isset($match[5]))
374 if (!isset($match[6]) || $match[6] === '')
376 $match['query'] = null;
378 if (!isset($match[8]) || $match[8] === '')
380 $match['fragment'] = null;
386 trigger_error('This should never happen', E_USER_ERROR);
392 * Remove dot segments from a path
394 * @param string $input
397 protected function remove_dot_segments($input)
400 while (strpos($input, './') !== false || strpos($input, '/.') !== false || $input === '.' || $input === '..')
402 // A: If the input buffer begins with a prefix of "../" or "./", then remove that prefix from the input buffer; otherwise,
403 if (strpos($input, '../') === 0)
405 $input = substr($input, 3);
407 elseif (strpos($input, './') === 0)
409 $input = substr($input, 2);
411 // 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,
412 elseif (strpos($input, '/./') === 0)
414 $input = substr($input, 2);
416 elseif ($input === '/.')
420 // 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,
421 elseif (strpos($input, '/../') === 0)
423 $input = substr($input, 3);
424 $output = substr_replace($output, '', strrpos($output, '/'));
426 elseif ($input === '/..')
429 $output = substr_replace($output, '', strrpos($output, '/'));
431 // D: if the input buffer consists only of "." or "..", then remove that from the input buffer; otherwise,
432 elseif ($input === '.' || $input === '..')
436 // 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
437 elseif (($pos = strpos($input, '/', 1)) !== false)
439 $output .= substr($input, 0, $pos);
440 $input = substr_replace($input, '', 0, $pos);
448 return $output . $input;
452 * Replace invalid character with percent encoding
454 * @param string $string Input string
455 * @param string $extra_chars Valid characters not in iunreserved or
456 * iprivate (this is ASCII-only)
457 * @param bool $iprivate Allow iprivate
460 protected function replace_invalid_with_pct_encoding($string, $extra_chars, $iprivate = false)
462 // Normalize as many pct-encoded sections as possible
463 $string = preg_replace_callback('/(?:%[A-Fa-f0-9]{2})+/', array(&$this, 'remove_iunreserved_percent_encoded'), $string);
465 // Replace invalid percent characters
466 $string = preg_replace('/%(?![A-Fa-f0-9]{2})/', '%25', $string);
468 // Add unreserved and % to $extra_chars (the latter is safe because all
469 // pct-encoded sections are now valid).
470 $extra_chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~%';
472 // Now replace any bytes that aren't allowed with their pct-encoded versions
474 $strlen = strlen($string);
475 while (($position += strspn($string, $extra_chars, $position)) < $strlen)
477 $value = ord($string[$position]);
482 // By default we are valid
485 // No one byte sequences are valid due to the while.
486 // Two byte sequence:
487 if (($value & 0xE0) === 0xC0)
489 $character = ($value & 0x1F) << 6;
493 // Three byte sequence:
494 elseif (($value & 0xF0) === 0xE0)
496 $character = ($value & 0x0F) << 12;
500 // Four byte sequence:
501 elseif (($value & 0xF8) === 0xF0)
503 $character = ($value & 0x07) << 18;
517 if ($position + $length <= $strlen)
519 for ($position++; $remaining; $position++)
521 $value = ord($string[$position]);
523 // Check that the byte is valid, then add it to the character:
524 if (($value & 0xC0) === 0x80)
526 $character |= ($value & 0x3F) << (--$remaining * 6);
528 // If it is invalid, count the sequence as invalid and reprocess the current byte:
539 $position = $strlen - 1;
544 // Percent encode anything invalid or not in ucschar
548 // Non-shortest form sequences are invalid
549 || $length > 1 && $character <= 0x7F
550 || $length > 2 && $character <= 0x7FF
551 || $length > 3 && $character <= 0xFFFF
552 // Outside of range of ucschar codepoints
554 || ($character & 0xFFFE) === 0xFFFE
555 || $character >= 0xFDD0 && $character <= 0xFDEF
557 // Everything else not in ucschar
558 $character > 0xD7FF && $character < 0xF900
560 || $character > 0xEFFFD
563 // Everything not in iprivate, if it applies
565 || $character < 0xE000
566 || $character > 0x10FFFD
570 // If we were a character, pretend we weren't, but rather an error.
574 for ($j = $start; $j <= $position; $j++)
576 $string = substr_replace($string, sprintf('%%%02X', ord($string[$j])), $j, 1);
588 * Callback function for preg_replace_callback.
590 * Removes sequences of percent encoded bytes that represent UTF-8
591 * encoded characters in iunreserved
593 * @param array $match PCRE match
594 * @return string Replacement
596 protected function remove_iunreserved_percent_encoded($match)
598 // As we just have valid percent encoded sequences we can just explode
599 // and ignore the first member of the returned array (an empty string).
600 $bytes = explode('%', $match[0]);
602 // Initialize the new string (this is what will be returned) and that
603 // there are no bytes remaining in the current sequence (unsurprising
604 // at the first byte!).
608 // Loop over each and every byte, and set $value to its value
609 for ($i = 1, $len = count($bytes); $i < $len; $i++)
611 $value = hexdec($bytes[$i]);
613 // If we're the first byte of sequence:
619 // By default we are valid
622 // One byte sequence:
628 // Two byte sequence:
629 elseif (($value & 0xE0) === 0xC0)
631 $character = ($value & 0x1F) << 6;
635 // Three byte sequence:
636 elseif (($value & 0xF0) === 0xE0)
638 $character = ($value & 0x0F) << 12;
642 // Four byte sequence:
643 elseif (($value & 0xF8) === 0xF0)
645 $character = ($value & 0x07) << 18;
656 // Continuation byte:
659 // Check that the byte is valid, then add it to the character:
660 if (($value & 0xC0) === 0x80)
663 $character |= ($value & 0x3F) << ($remaining * 6);
665 // If it is invalid, count the sequence as invalid and reprocess the current byte as the start of a sequence:
674 // If we've reached the end of the current byte sequence, append it to Unicode::$data
677 // Percent encode anything invalid or not in iunreserved
681 // Non-shortest form sequences are invalid
682 || $length > 1 && $character <= 0x7F
683 || $length > 2 && $character <= 0x7FF
684 || $length > 3 && $character <= 0xFFFF
685 // Outside of range of iunreserved codepoints
687 || $character > 0xEFFFD
689 || ($character & 0xFFFE) === 0xFFFE
690 || $character >= 0xFDD0 && $character <= 0xFDEF
691 // Everything else not in iunreserved (this is all BMP)
692 || $character === 0x2F
693 || $character > 0x39 && $character < 0x41
694 || $character > 0x5A && $character < 0x61
695 || $character > 0x7A && $character < 0x7E
696 || $character > 0x7E && $character < 0xA0
697 || $character > 0xD7FF && $character < 0xF900
700 for ($j = $start; $j <= $i; $j++)
702 $string .= '%' . strtoupper($bytes[$j]);
707 for ($j = $start; $j <= $i; $j++)
709 $string .= chr(hexdec($bytes[$j]));
715 // If we have any bytes left over they are invalid (i.e., we are
716 // mid-way through a multi-byte sequence)
719 for ($j = $start; $j < $len; $j++)
721 $string .= '%' . strtoupper($bytes[$j]);
728 protected function scheme_normalization()
730 if (isset($this->normalization[$this->scheme]['iuserinfo']) && $this->iuserinfo === $this->normalization[$this->scheme]['iuserinfo'])
732 $this->iuserinfo = null;
734 if (isset($this->normalization[$this->scheme]['ihost']) && $this->ihost === $this->normalization[$this->scheme]['ihost'])
738 if (isset($this->normalization[$this->scheme]['port']) && $this->port === $this->normalization[$this->scheme]['port'])
742 if (isset($this->normalization[$this->scheme]['ipath']) && $this->ipath === $this->normalization[$this->scheme]['ipath'])
746 if (isset($this->normalization[$this->scheme]['iquery']) && $this->iquery === $this->normalization[$this->scheme]['iquery'])
748 $this->iquery = null;
750 if (isset($this->normalization[$this->scheme]['ifragment']) && $this->ifragment === $this->normalization[$this->scheme]['ifragment'])
752 $this->ifragment = null;
757 * Check if the object represents a valid IRI. This needs to be done on each
758 * call as some things change depending on another part of the IRI.
762 public function is_valid()
764 $isauthority = $this->iuserinfo !== null || $this->ihost !== null || $this->port !== null;
765 if ($this->ipath !== '' &&
768 $this->ipath[0] !== '/' ||
769 substr($this->ipath, 0, 2) === '//'
772 $this->scheme === null &&
774 strpos($this->ipath, ':') !== false &&
775 (strpos($this->ipath, '/') === false ? true : strpos($this->ipath, ':') < strpos($this->ipath, '/'))
787 * Set the entire IRI. Returns true on success, false on failure (if there
788 * are any invalid characters).
793 protected function set_iri($iri)
805 elseif (isset($cache[$iri]))
814 $return) = $cache[$iri];
819 $parsed = $this->parse_iri((string) $iri);
821 $return = $this->set_scheme($parsed['scheme'])
822 && $this->set_authority($parsed['authority'])
823 && $this->set_path($parsed['path'])
824 && $this->set_query($parsed['query'])
825 && $this->set_fragment($parsed['fragment']);
827 $cache[$iri] = array($this->scheme,
840 * Set the scheme. Returns true on success, false on failure (if there are
841 * any invalid characters).
843 * @param string $scheme
846 protected function set_scheme($scheme)
848 if ($scheme === null)
850 $this->scheme = null;
852 elseif (!preg_match('/^[A-Za-z][0-9A-Za-z+\-.]*$/', $scheme))
854 $this->scheme = null;
859 $this->scheme = strtolower($scheme);
865 * Set the authority. Returns true on success, false on failure (if there are
866 * any invalid characters).
868 * @param string $authority
871 protected function set_authority($authority)
877 if ($authority === null)
879 $this->iuserinfo = null;
884 elseif (isset($cache[$authority]))
886 list($this->iuserinfo,
889 $return) = $cache[$authority];
895 $remaining = $authority;
896 if (($iuserinfo_end = strrpos($remaining, '@')) !== false)
898 $iuserinfo = substr($remaining, 0, $iuserinfo_end);
899 $remaining = substr($remaining, $iuserinfo_end + 1);
905 if (($port_start = strpos($remaining, ':', strpos($remaining, ']'))) !== false)
907 if (($port = substr($remaining, $port_start + 1)) === false)
911 $remaining = substr($remaining, 0, $port_start);
918 $return = $this->set_userinfo($iuserinfo) &&
919 $this->set_host($remaining) &&
920 $this->set_port($port);
922 $cache[$authority] = array($this->iuserinfo,
934 * @param string $iuserinfo
937 protected function set_userinfo($iuserinfo)
939 if ($iuserinfo === null)
941 $this->iuserinfo = null;
945 $this->iuserinfo = $this->replace_invalid_with_pct_encoding($iuserinfo, '!$&\'()*+,;=:');
946 $this->scheme_normalization();
953 * Set the ihost. Returns true on success, false on failure (if there are
954 * any invalid characters).
956 * @param string $ihost
959 protected function set_host($ihost)
966 elseif (substr($ihost, 0, 1) === '[' && substr($ihost, -1) === ']')
968 if (Requests_IPv6::check_ipv6(substr($ihost, 1, -1)))
970 $this->ihost = '[' . Requests_IPv6::compress(substr($ihost, 1, -1)) . ']';
980 $ihost = $this->replace_invalid_with_pct_encoding($ihost, '!$&\'()*+,;=');
982 // Lowercase, but ignore pct-encoded sections (as they should
983 // remain uppercase). This must be done after the previous step
984 // as that can add unescaped characters.
986 $strlen = strlen($ihost);
987 while (($position += strcspn($ihost, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ%', $position)) < $strlen)
989 if ($ihost[$position] === '%')
995 $ihost[$position] = strtolower($ihost[$position]);
1000 $this->ihost = $ihost;
1003 $this->scheme_normalization();
1009 * Set the port. Returns true on success, false on failure (if there are
1010 * any invalid characters).
1012 * @param string $port
1015 protected function set_port($port)
1022 elseif (strspn($port, '0123456789') === strlen($port))
1024 $this->port = (int) $port;
1025 $this->scheme_normalization();
1038 * @param string $ipath
1041 protected function set_path($ipath)
1049 $ipath = (string) $ipath;
1051 if (isset($cache[$ipath]))
1053 $this->ipath = $cache[$ipath][(int) ($this->scheme !== null)];
1057 $valid = $this->replace_invalid_with_pct_encoding($ipath, '!$&\'()*+,;=@:/');
1058 $removed = $this->remove_dot_segments($valid);
1060 $cache[$ipath] = array($valid, $removed);
1061 $this->ipath = ($this->scheme !== null) ? $removed : $valid;
1064 $this->scheme_normalization();
1071 * @param string $iquery
1074 protected function set_query($iquery)
1076 if ($iquery === null)
1078 $this->iquery = null;
1082 $this->iquery = $this->replace_invalid_with_pct_encoding($iquery, '!$&\'()*+,;=:@/?', true);
1083 $this->scheme_normalization();
1089 * Set the ifragment.
1091 * @param string $ifragment
1094 protected function set_fragment($ifragment)
1096 if ($ifragment === null)
1098 $this->ifragment = null;
1102 $this->ifragment = $this->replace_invalid_with_pct_encoding($ifragment, '!$&\'()*+,;=:@/?');
1103 $this->scheme_normalization();
1109 * Convert an IRI to a URI (or parts thereof)
1113 protected function to_uri($string)
1118 $non_ascii = implode('', range("\x80", "\xFF"));
1122 $strlen = strlen($string);
1123 while (($position += strcspn($string, $non_ascii, $position)) < $strlen)
1125 $string = substr_replace($string, sprintf('%%%02X', ord($string[$position])), $position, 1);
1134 * Get the complete IRI
1138 protected function get_iri()
1140 if (!$this->is_valid())
1146 if ($this->scheme !== null)
1148 $iri .= $this->scheme . ':';
1150 if (($iauthority = $this->get_iauthority()) !== null)
1152 $iri .= '//' . $iauthority;
1154 $iri .= $this->ipath;
1155 if ($this->iquery !== null)
1157 $iri .= '?' . $this->iquery;
1159 if ($this->ifragment !== null)
1161 $iri .= '#' . $this->ifragment;
1168 * Get the complete URI
1172 protected function get_uri()
1174 return $this->to_uri($this->get_iri());
1178 * Get the complete iauthority
1182 protected function get_iauthority()
1184 if ($this->iuserinfo !== null || $this->ihost !== null || $this->port !== null)
1187 if ($this->iuserinfo !== null)
1189 $iauthority .= $this->iuserinfo . '@';
1191 if ($this->ihost !== null)
1193 $iauthority .= $this->ihost;
1195 if ($this->port !== null)
1197 $iauthority .= ':' . $this->port;
1208 * Get the complete authority
1212 protected function get_authority()
1214 $iauthority = $this->get_iauthority();
1215 if (is_string($iauthority))
1216 return $this->to_uri($iauthority);