]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/Auth/OpenID/URINorm.php
Merge branch 'nightly' of git.gnu.io:gnu/gnu-social into nightly
[quix0rs-gnu-social.git] / extlib / Auth / OpenID / URINorm.php
1 <?php
2
3 /**
4  * URI normalization routines.
5  *
6  * @package OpenID
7  * @author JanRain, Inc. <openid@janrain.com>
8  * @copyright 2005-2008 Janrain, Inc.
9  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
10  */
11
12 require_once 'Auth/Yadis/Misc.php';
13
14 // from appendix B of rfc 3986 (http://www.ietf.org/rfc/rfc3986.txt)
15 function Auth_OpenID_getURIPattern()
16 {
17     return '&^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?&';
18 }
19
20 function Auth_OpenID_getAuthorityPattern()
21 {
22     return '/^([^@]*@)?([^:]*)(:.*)?/';
23 }
24
25 function Auth_OpenID_getEncodedPattern()
26 {
27     return '/%([0-9A-Fa-f]{2})/';
28 }
29
30 # gen-delims  = ":" / "/" / "?" / "#" / "[" / "]" / "@"
31 #
32 # sub-delims  = "!" / "$" / "&" / "'" / "(" / ")"
33 #                  / "*" / "+" / "," / ";" / "="
34 #
35 # unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"
36 function Auth_OpenID_getURLIllegalCharRE()
37 {
38     return "/([^-A-Za-z0-9:\/\?#\[\]@\!\$&'\(\)\*\+,;=\._~\%])/";
39 }
40
41 function Auth_OpenID_getUnreserved()
42 {
43     $_unreserved = array();
44     for ($i = 0; $i < 256; $i++) {
45         $_unreserved[$i] = false;
46     }
47
48     for ($i = ord('A'); $i <= ord('Z'); $i++) {
49         $_unreserved[$i] = true;
50     }
51
52     for ($i = ord('0'); $i <= ord('9'); $i++) {
53         $_unreserved[$i] = true;
54     }
55
56     for ($i = ord('a'); $i <= ord('z'); $i++) {
57         $_unreserved[$i] = true;
58     }
59
60     $_unreserved[ord('-')] = true;
61     $_unreserved[ord('.')] = true;
62     $_unreserved[ord('_')] = true;
63     $_unreserved[ord('~')] = true;
64
65     return $_unreserved;
66 }
67
68 function Auth_OpenID_getEscapeRE()
69 {
70     $parts = array();
71     foreach (array_merge(Auth_Yadis_getUCSChars(),
72                          Auth_Yadis_getIPrivateChars()) as $pair) {
73         list($m, $n) = $pair;
74         $parts[] = sprintf("%s-%s", chr($m), chr($n));
75     }
76
77     return sprintf('[%s]', implode('', $parts));
78 }
79
80 function Auth_OpenID_pct_encoded_replace_unreserved($mo)
81 {
82     $_unreserved = Auth_OpenID_getUnreserved();
83
84     $i = intval($mo[1], 16);
85     if ($_unreserved[$i]) {
86         return chr($i);
87     } else {
88         return strtoupper($mo[0]);
89     }
90
91     return $mo[0];
92 }
93
94 function Auth_OpenID_pct_encoded_replace($mo)
95 {
96     $code = intval($mo[1], 16);
97
98     // Prevent request splitting by ignoring newline and space characters
99     if($code === 0xA || $code === 0xD || $code === ord(' '))
100     {
101         return $mo[0];
102     }
103     else
104     {
105         return chr($code);
106     }
107 }
108
109 function Auth_OpenID_remove_dot_segments($path)
110 {
111     $result_segments = array();
112
113     while ($path) {
114         if (Auth_Yadis_startswith($path, '../')) {
115             $path = substr($path, 3);
116         } else if (Auth_Yadis_startswith($path, './')) {
117             $path = substr($path, 2);
118         } else if (Auth_Yadis_startswith($path, '/./')) {
119             $path = substr($path, 2);
120         } else if ($path == '/.') {
121             $path = '/';
122         } else if (Auth_Yadis_startswith($path, '/../')) {
123             $path = substr($path, 3);
124             if ($result_segments) {
125                 array_pop($result_segments);
126             }
127         } else if ($path == '/..') {
128             $path = '/';
129             if ($result_segments) {
130                 array_pop($result_segments);
131             }
132         } else if (($path == '..') ||
133                    ($path == '.')) {
134             $path = '';
135         } else {
136             $i = 0;
137             if ($path[0] == '/') {
138                 $i = 1;
139             }
140             $i = strpos($path, '/', $i);
141             if ($i === false) {
142                 $i = strlen($path);
143             }
144             $result_segments[] = substr($path, 0, $i);
145             $path = substr($path, $i);
146         }
147     }
148
149     return implode('', $result_segments);
150 }
151
152 function Auth_OpenID_urinorm($uri)
153 {
154     $uri_matches = array();
155     preg_match(Auth_OpenID_getURIPattern(), $uri, $uri_matches);
156
157     if (count($uri_matches) < 9) {
158         for ($i = count($uri_matches); $i <= 9; $i++) {
159             $uri_matches[] = '';
160         }
161     }
162
163     $illegal_matches = array();
164     preg_match(Auth_OpenID_getURLIllegalCharRE(),
165                $uri, $illegal_matches);
166     if ($illegal_matches) {
167         return null;
168     }
169
170     $scheme = $uri_matches[2];
171     if ($scheme) {
172         $scheme = strtolower($scheme);
173     }
174
175     $scheme = $uri_matches[2];
176     if ($scheme === '') {
177         // No scheme specified
178         return null;
179     }
180
181     $scheme = strtolower($scheme);
182     if (!in_array($scheme, array('http', 'https'))) {
183         // Not an absolute HTTP or HTTPS URI
184         return null;
185     }
186
187     $authority = $uri_matches[4];
188     if ($authority === '') {
189         // Not an absolute URI
190         return null;
191     }
192
193     $authority_matches = array();
194     preg_match(Auth_OpenID_getAuthorityPattern(),
195                $authority, $authority_matches);
196     if (count($authority_matches) === 0) {
197         // URI does not have a valid authority
198         return null;
199     }
200
201     if (count($authority_matches) < 4) {
202         for ($i = count($authority_matches); $i <= 4; $i++) {
203             $authority_matches[] = '';
204         }
205     }
206
207     list($_whole, $userinfo, $host, $port) = $authority_matches;
208
209     if ($userinfo === null) {
210         $userinfo = '';
211     }
212
213     if (strpos($host, '%') !== -1) {
214         $host = strtolower($host);
215         $host = preg_replace_callback(
216                   Auth_OpenID_getEncodedPattern(),
217                   'Auth_OpenID_pct_encoded_replace', $host);
218         // NO IDNA.
219         // $host = unicode($host, 'utf-8').encode('idna');
220     } else {
221         $host = strtolower($host);
222     }
223
224     if ($port) {
225         if (($port == ':') ||
226             ($scheme == 'http' && $port == ':80') ||
227             ($scheme == 'https' && $port == ':443')) {
228             $port = '';
229         }
230     } else {
231         $port = '';
232     }
233
234     $authority = $userinfo . $host . $port;
235
236     $path = $uri_matches[5];
237     $path = preg_replace_callback(
238                Auth_OpenID_getEncodedPattern(),
239                'Auth_OpenID_pct_encoded_replace_unreserved', $path);
240
241     $path = Auth_OpenID_remove_dot_segments($path);
242     if (!$path) {
243         $path = '/';
244     }
245
246     $query = $uri_matches[6];
247     if ($query === null) {
248         $query = '';
249     }
250
251     $fragment = $uri_matches[8];
252     if ($fragment === null) {
253         $fragment = '';
254     }
255
256     return $scheme . '://' . $authority . $path . $query . $fragment;
257 }
258
259