4 * URI normalization routines.
7 * @author JanRain, Inc. <openid@janrain.com>
8 * @copyright 2005-2008 Janrain, Inc.
9 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
12 require_once 'Auth/Yadis/Misc.php';
14 // from appendix B of rfc 3986 (http://www.ietf.org/rfc/rfc3986.txt)
15 function Auth_OpenID_getURIPattern()
17 return '&^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?&';
20 function Auth_OpenID_getAuthorityPattern()
22 return '/^([^@]*@)?([^:]*)(:.*)?/';
25 function Auth_OpenID_getEncodedPattern()
27 return '/%([0-9A-Fa-f]{2})/';
30 # gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
32 # sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
33 # / "*" / "+" / "," / ";" / "="
35 # unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
36 function Auth_OpenID_getURLIllegalCharRE()
38 return "/([^-A-Za-z0-9:\/\?#\[\]@\!\$&'\(\)\*\+,;=\._~\%])/";
41 function Auth_OpenID_getUnreserved()
43 $_unreserved = array();
44 for ($i = 0; $i < 256; $i++) {
45 $_unreserved[$i] = false;
48 for ($i = ord('A'); $i <= ord('Z'); $i++) {
49 $_unreserved[$i] = true;
52 for ($i = ord('0'); $i <= ord('9'); $i++) {
53 $_unreserved[$i] = true;
56 for ($i = ord('a'); $i <= ord('z'); $i++) {
57 $_unreserved[$i] = true;
60 $_unreserved[ord('-')] = true;
61 $_unreserved[ord('.')] = true;
62 $_unreserved[ord('_')] = true;
63 $_unreserved[ord('~')] = true;
68 function Auth_OpenID_getEscapeRE()
71 foreach (array_merge(Auth_Yadis_getUCSChars(),
72 Auth_Yadis_getIPrivateChars()) as $pair) {
74 $parts[] = sprintf("%s-%s", chr($m), chr($n));
77 return sprintf('[%s]', implode('', $parts));
80 function Auth_OpenID_pct_encoded_replace_unreserved($mo)
82 $_unreserved = Auth_OpenID_getUnreserved();
84 $i = intval($mo[1], 16);
85 if ($_unreserved[$i]) {
88 return strtoupper($mo[0]);
94 function Auth_OpenID_pct_encoded_replace($mo)
96 return chr(intval($mo[1], 16));
99 function Auth_OpenID_remove_dot_segments($path)
101 $result_segments = array();
104 if (Auth_Yadis_startswith($path, '../')) {
105 $path = substr($path, 3);
106 } else if (Auth_Yadis_startswith($path, './')) {
107 $path = substr($path, 2);
108 } else if (Auth_Yadis_startswith($path, '/./')) {
109 $path = substr($path, 2);
110 } else if ($path == '/.') {
112 } else if (Auth_Yadis_startswith($path, '/../')) {
113 $path = substr($path, 3);
114 if ($result_segments) {
115 array_pop($result_segments);
117 } else if ($path == '/..') {
119 if ($result_segments) {
120 array_pop($result_segments);
122 } else if (($path == '..') ||
127 if ($path[0] == '/') {
130 $i = strpos($path, '/', $i);
134 $result_segments[] = substr($path, 0, $i);
135 $path = substr($path, $i);
139 return implode('', $result_segments);
142 function Auth_OpenID_urinorm($uri)
144 $uri_matches = array();
145 preg_match(Auth_OpenID_getURIPattern(), $uri, $uri_matches);
147 if (count($uri_matches) < 9) {
148 for ($i = count($uri_matches); $i <= 9; $i++) {
153 $illegal_matches = array();
154 preg_match(Auth_OpenID_getURLIllegalCharRE(),
155 $uri, $illegal_matches);
156 if ($illegal_matches) {
160 $scheme = $uri_matches[2];
162 $scheme = strtolower($scheme);
165 $scheme = $uri_matches[2];
166 if ($scheme === '') {
167 // No scheme specified
171 $scheme = strtolower($scheme);
172 if (!in_array($scheme, array('http', 'https'))) {
173 // Not an absolute HTTP or HTTPS URI
177 $authority = $uri_matches[4];
178 if ($authority === '') {
179 // Not an absolute URI
183 $authority_matches = array();
184 preg_match(Auth_OpenID_getAuthorityPattern(),
185 $authority, $authority_matches);
186 if (count($authority_matches) === 0) {
187 // URI does not have a valid authority
191 if (count($authority_matches) < 4) {
192 for ($i = count($authority_matches); $i <= 4; $i++) {
193 $authority_matches[] = '';
197 list($_whole, $userinfo, $host, $port) = $authority_matches;
199 if ($userinfo === null) {
203 if (strpos($host, '%') !== -1) {
204 $host = strtolower($host);
205 $host = preg_replace_callback(
206 Auth_OpenID_getEncodedPattern(),
207 'Auth_OpenID_pct_encoded_replace', $host);
209 // $host = unicode($host, 'utf-8').encode('idna');
211 $host = strtolower($host);
215 if (($port == ':') ||
216 ($scheme == 'http' && $port == ':80') ||
217 ($scheme == 'https' && $port == ':443')) {
224 $authority = $userinfo . $host . $port;
226 $path = $uri_matches[5];
227 $path = preg_replace_callback(
228 Auth_OpenID_getEncodedPattern(),
229 'Auth_OpenID_pct_encoded_replace_unreserved', $path);
231 $path = Auth_OpenID_remove_dot_segments($path);
236 $query = $uri_matches[6];
237 if ($query === null) {
241 $fragment = $uri_matches[8];
242 if ($fragment === null) {
246 return $scheme . '://' . $authority . $path . $query . $fragment;