PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/0.0.00.00/lib/include/Auth/OpenID/URINorm.php

https://github.com/applideveloper/OpenPNE2CE
PHP | 231 lines | 177 code | 39 blank | 15 comment | 55 complexity | 14045603c3ae4a2ff43efc5cd3a01762 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. <?php
  2. /**
  3. * URI normalization routines.
  4. *
  5. * @package OpenID
  6. * @author JanRain, Inc. <openid@janrain.com>
  7. * @copyright 2005 Janrain, Inc.
  8. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  9. */
  10. require_once 'Auth/Yadis/Misc.php';
  11. // from appendix B of rfc 3986 (http://www.ietf.org/rfc/rfc3986.txt)
  12. function Auth_OpenID_getURIPattern()
  13. {
  14. return '&^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?&';
  15. }
  16. function Auth_OpenID_getAuthorityPattern()
  17. {
  18. return '/^([^@]*@)?([^:]*)(:.*)?/';
  19. }
  20. function Auth_OpenID_getEncodedPattern()
  21. {
  22. return '/%([0-9A-Fa-f]{2})/';
  23. }
  24. function Auth_OpenID_getUnreserved()
  25. {
  26. $_unreserved = array();
  27. for ($i = 0; $i < 256; $i++) {
  28. $_unreserved[$i] = false;
  29. }
  30. for ($i = ord('A'); $i <= ord('Z'); $i++) {
  31. $_unreserved[$i] = true;
  32. }
  33. for ($i = ord('0'); $i <= ord('9'); $i++) {
  34. $_unreserved[$i] = true;
  35. }
  36. for ($i = ord('a'); $i <= ord('z'); $i++) {
  37. $_unreserved[$i] = true;
  38. }
  39. $_unreserved[ord('-')] = true;
  40. $_unreserved[ord('.')] = true;
  41. $_unreserved[ord('_')] = true;
  42. $_unreserved[ord('~')] = true;
  43. return $_unreserved;
  44. }
  45. function Auth_OpenID_getEscapeRE()
  46. {
  47. $parts = array();
  48. foreach (array_merge(Auth_Yadis_getUCSChars(),
  49. Auth_Yadis_getIPrivateChars()) as $pair) {
  50. list($m, $n) = $pair;
  51. $parts[] = sprintf("%s-%s", chr($m), chr($n));
  52. }
  53. return sprintf('[%s]', implode('', $parts));
  54. }
  55. function Auth_OpenID_pct_encoded_replace_unreserved($mo)
  56. {
  57. $_unreserved = Auth_OpenID_getUnreserved();
  58. $i = intval($mo[1], 16);
  59. if ($_unreserved[$i]) {
  60. return chr($i);
  61. } else {
  62. return strtoupper($mo[0]);
  63. }
  64. return $mo[0];
  65. }
  66. function Auth_OpenID_pct_encoded_replace($mo)
  67. {
  68. return chr(intval($mo[1], 16));
  69. }
  70. function Auth_OpenID_remove_dot_segments($path)
  71. {
  72. $result_segments = array();
  73. while ($path) {
  74. if (Auth_Yadis_startswith($path, '../')) {
  75. $path = substr($path, 3);
  76. } else if (Auth_Yadis_startswith($path, './')) {
  77. $path = substr($path, 2);
  78. } else if (Auth_Yadis_startswith($path, '/./')) {
  79. $path = substr($path, 2);
  80. } else if ($path == '/.') {
  81. $path = '/';
  82. } else if (Auth_Yadis_startswith($path, '/../')) {
  83. $path = substr($path, 3);
  84. if ($result_segments) {
  85. array_pop($result_segments);
  86. }
  87. } else if ($path == '/..') {
  88. $path = '/';
  89. if ($result_segments) {
  90. array_pop($result_segments);
  91. }
  92. } else if (($path == '..') ||
  93. ($path == '.')) {
  94. $path = '';
  95. } else {
  96. $i = 0;
  97. if ($path[0] == '/') {
  98. $i = 1;
  99. }
  100. $i = strpos($path, '/', $i);
  101. if ($i === false) {
  102. $i = strlen($path);
  103. }
  104. $result_segments[] = substr($path, 0, $i);
  105. $path = substr($path, $i);
  106. }
  107. }
  108. return implode('', $result_segments);
  109. }
  110. function Auth_OpenID_urinorm($uri)
  111. {
  112. $uri_matches = array();
  113. preg_match(Auth_OpenID_getURIPattern(), $uri, $uri_matches);
  114. if (count($uri_matches) < 9) {
  115. for ($i = count($uri_matches); $i <= 9; $i++) {
  116. $uri_matches[] = '';
  117. }
  118. }
  119. $scheme = $uri_matches[2];
  120. if ($scheme) {
  121. $scheme = strtolower($scheme);
  122. }
  123. $scheme = $uri_matches[2];
  124. if ($scheme === '') {
  125. // No scheme specified
  126. return null;
  127. }
  128. $scheme = strtolower($scheme);
  129. if (!in_array($scheme, array('http', 'https'))) {
  130. // Not an absolute HTTP or HTTPS URI
  131. return null;
  132. }
  133. $authority = $uri_matches[4];
  134. if ($authority === '') {
  135. // Not an absolute URI
  136. return null;
  137. }
  138. $authority_matches = array();
  139. preg_match(Auth_OpenID_getAuthorityPattern(),
  140. $authority, $authority_matches);
  141. if (count($authority_matches) === 0) {
  142. // URI does not have a valid authority
  143. return null;
  144. }
  145. if (count($authority_matches) < 4) {
  146. for ($i = count($authority_matches); $i <= 4; $i++) {
  147. $authority_matches[] = '';
  148. }
  149. }
  150. list($_whole, $userinfo, $host, $port) = $authority_matches;
  151. if ($userinfo === null) {
  152. $userinfo = '';
  153. }
  154. if (strpos($host, '%') !== -1) {
  155. $host = strtolower($host);
  156. $host = preg_replace_callback(
  157. Auth_OpenID_getEncodedPattern(),
  158. 'Auth_OpenID_pct_encoded_replace', $host);
  159. // NO IDNA.
  160. // $host = unicode($host, 'utf-8').encode('idna');
  161. } else {
  162. $host = strtolower($host);
  163. }
  164. if ($port) {
  165. if (($port == ':') ||
  166. ($scheme == 'http' && $port == ':80') ||
  167. ($scheme == 'https' && $port == ':443')) {
  168. $port = '';
  169. }
  170. } else {
  171. $port = '';
  172. }
  173. $authority = $userinfo . $host . $port;
  174. $path = $uri_matches[5];
  175. $path = preg_replace_callback(
  176. Auth_OpenID_getEncodedPattern(),
  177. 'Auth_OpenID_pct_encoded_replace_unreserved', $path);
  178. $path = Auth_OpenID_remove_dot_segments($path);
  179. if (!$path) {
  180. $path = '/';
  181. }
  182. $query = $uri_matches[6];
  183. if ($query === null) {
  184. $query = '';
  185. }
  186. $fragment = $uri_matches[8];
  187. if ($fragment === null) {
  188. $fragment = '';
  189. }
  190. return $scheme . '://' . $authority . $path . $query . $fragment;
  191. }
  192. ?>