00001 <?php
00002
00012 require_once 'Auth/Yadis/Misc.php';
00013
00014
00015 function Auth_OpenID_getURIPattern()
00016 {
00017 return '&^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?&';
00018 }
00019
00020 function Auth_OpenID_getAuthorityPattern()
00021 {
00022 return '/^([^@]*@)?([^:]*)(:.*)?/';
00023 }
00024
00025 function Auth_OpenID_getEncodedPattern()
00026 {
00027 return '/%([0-9A-Fa-f]{2})/';
00028 }
00029
00030 # gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
#
# sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
00031 # / "*" / "+" / "," / ";" / "="
00032 #
00033 # unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
00034 function Auth_OpenID_getURLIllegalCharRE()
00035 {
00036 return "/([^-A-Za-z0-9:\/\?#\[\]@\!\$&'\(\)\*\+,;=\._~\%])/";
00037 }
00038
00039 function Auth_OpenID_getUnreserved()
00040 {
00041 $_unreserved = array();
00042 for ($i = 0; $i < 256; $i++) {
00043 $_unreserved[$i] = false;
00044 }
00045
00046 for ($i = ord('A'); $i <= ord('Z'); $i++) {
00047 $_unreserved[$i] = true;
00048 }
00049
00050 for ($i = ord('0'); $i <= ord('9'); $i++) {
00051 $_unreserved[$i] = true;
00052 }
00053
00054 for ($i = ord('a'); $i <= ord('z'); $i++) {
00055 $_unreserved[$i] = true;
00056 }
00057
00058 $_unreserved[ord('-')] = true;
00059 $_unreserved[ord('.')] = true;
00060 $_unreserved[ord('_')] = true;
00061 $_unreserved[ord('~')] = true;
00062
00063 return $_unreserved;
00064 }
00065
00066 function Auth_OpenID_getEscapeRE()
00067 {
00068 $parts = array();
00069 foreach (array_merge(Auth_Yadis_getUCSChars(),
00070 Auth_Yadis_getIPrivateChars()) as $pair) {
00071 list($m, $n) = $pair;
00072 $parts[] = sprintf("%s-%s", chr($m), chr($n));
00073 }
00074
00075 return sprintf('[%s]', implode('', $parts));
00076 }
00077
00078 function Auth_OpenID_pct_encoded_replace_unreserved($mo)
00079 {
00080 $_unreserved = Auth_OpenID_getUnreserved();
00081
00082 $i = intval($mo[1], 16);
00083 if ($_unreserved[$i]) {
00084 return chr($i);
00085 } else {
00086 return strtoupper($mo[0]);
00087 }
00088
00089 return $mo[0];
00090 }
00091
00092 function Auth_OpenID_pct_encoded_replace($mo)
00093 {
00094 return chr(intval($mo[1], 16));
00095 }
00096
00097 function Auth_OpenID_remove_dot_segments($path)
00098 {
00099 $result_segments = array();
00100
00101 while ($path) {
00102 if (Auth_Yadis_startswith($path, '../')) {
00103 $path = substr($path, 3);
00104 } else if (Auth_Yadis_startswith($path, './')) {
00105 $path = substr($path, 2);
00106 } else if (Auth_Yadis_startswith($path, '/./')) {
00107 $path = substr($path, 2);
00108 } else if ($path == '/.') {
00109 $path = '/';
00110 } else if (Auth_Yadis_startswith($path, '/../')) {
00111 $path = substr($path, 3);
00112 if ($result_segments) {
00113 array_pop($result_segments);
00114 }
00115 } else if ($path == '/..') {
00116 $path = '/';
00117 if ($result_segments) {
00118 array_pop($result_segments);
00119 }
00120 } else if (($path == '..') ||
00121 ($path == '.')) {
00122 $path = '';
00123 } else {
00124 $i = 0;
00125 if ($path[0] == '/') {
00126 $i = 1;
00127 }
00128 $i = strpos($path, '/', $i);
00129 if ($i === false) {
00130 $i = strlen($path);
00131 }
00132 $result_segments[] = substr($path, 0, $i);
00133 $path = substr($path, $i);
00134 }
00135 }
00136
00137 return implode('', $result_segments);
00138 }
00139
00140 function Auth_OpenID_urinorm($uri)
00141 {
00142 $uri_matches = array();
00143 preg_match(Auth_OpenID_getURIPattern(), $uri, $uri_matches);
00144
00145 if (count($uri_matches) < 9) {
00146 for ($i = count($uri_matches); $i <= 9; $i++) {
00147 $uri_matches[] = '';
00148 }
00149 }
00150
00151 $illegal_matches = array();
00152 preg_match(Auth_OpenID_getURLIllegalCharRE(),
00153 $uri, $illegal_matches);
00154 if ($illegal_matches) {
00155 return null;
00156 }
00157
00158 $scheme = $uri_matches[2];
00159 if ($scheme) {
00160 $scheme = strtolower($scheme);
00161 }
00162
00163 $scheme = $uri_matches[2];
00164 if ($scheme === '') {
00165
00166 return null;
00167 }
00168
00169 $scheme = strtolower($scheme);
00170 if (!in_array($scheme, array('http', 'https'))) {
00171
00172 return null;
00173 }
00174
00175 $authority = $uri_matches[4];
00176 if ($authority === '') {
00177
00178 return null;
00179 }
00180
00181 $authority_matches = array();
00182 preg_match(Auth_OpenID_getAuthorityPattern(),
00183 $authority, $authority_matches);
00184 if (count($authority_matches) === 0) {
00185
00186 return null;
00187 }
00188
00189 if (count($authority_matches) < 4) {
00190 for ($i = count($authority_matches); $i <= 4; $i++) {
00191 $authority_matches[] = '';
00192 }
00193 }
00194
00195 list($_whole, $userinfo, $host, $port) = $authority_matches;
00196
00197 if ($userinfo === null) {
00198 $userinfo = '';
00199 }
00200
00201 if (strpos($host, '%') !== -1) {
00202 $host = strtolower($host);
00203 $host = preg_replace_callback(
00204 Auth_OpenID_getEncodedPattern(),
00205 'Auth_OpenID_pct_encoded_replace', $host);
00206
00207
00208 } else {
00209 $host = strtolower($host);
00210 }
00211
00212 if ($port) {
00213 if (($port == ':') ||
00214 ($scheme == 'http' && $port == ':80') ||
00215 ($scheme == 'https' && $port == ':443')) {
00216 $port = '';
00217 }
00218 } else {
00219 $port = '';
00220 }
00221
00222 $authority = $userinfo . $host . $port;
00223
00224 $path = $uri_matches[5];
00225 $path = preg_replace_callback(
00226 Auth_OpenID_getEncodedPattern(),
00227 'Auth_OpenID_pct_encoded_replace_unreserved', $path);
00228
00229 $path = Auth_OpenID_remove_dot_segments($path);
00230 if (!$path) {
00231 $path = '/';
00232 }
00233
00234 $query = $uri_matches[6];
00235 if ($query === null) {
00236 $query = '';
00237 }
00238
00239 $fragment = $uri_matches[8];
00240 if ($fragment === null) {
00241 $fragment = '';
00242 }
00243
00244 return $scheme . '://' . $authority . $path . $query . $fragment;
00245 }
00246
00247 ?>
00248