PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/Auth/OpenID/URINorm.php

https://github.com/chregu/fluxcms
PHP | 211 lines | 159 code | 37 blank | 15 comment | 55 complexity | 5c8fd135f6e323e3cfd983c2669b8352 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, 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 'Services/Yadis/Misc.php';
  11. // from appendix B of rfc 3986 (http://www.ietf.org/rfc/rfc3986.txt)
  12. $__uri_pattern = '&^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?&';
  13. $__authority_pattern = '/^([^@]*@)?([^:]*)(:.*)?/';
  14. $__pct_encoded_pattern = '/%([0-9A-Fa-f]{2})/';
  15. $_unreserved = array();
  16. for ($i = 0; $i < 256; $i++) {
  17. $_unreserved[$i] = false;
  18. }
  19. for ($i = ord('A'); $i <= ord('Z'); $i++) {
  20. $_unreserved[$i] = true;
  21. }
  22. for ($i = ord('0'); $i <= ord('9'); $i++) {
  23. $_unreserved[$i] = true;
  24. }
  25. for ($i = ord('a'); $i <= ord('z'); $i++) {
  26. $_unreserved[$i] = true;
  27. }
  28. $_unreserved[ord('-')] = true;
  29. $_unreserved[ord('.')] = true;
  30. $_unreserved[ord('_')] = true;
  31. $_unreserved[ord('~')] = true;
  32. $parts = array();
  33. foreach (array_merge($__UCSCHAR, $__IPRIVATE) as $pair) {
  34. list($m, $n) = $pair;
  35. $parts[] = sprintf("%s-%s", chr($m), chr($n));
  36. }
  37. $_escapeme_re = sprintf('[%s]', implode('', $parts));
  38. function _pct_encoded_replace_unreserved($mo)
  39. {
  40. global $_unreserved;
  41. $i = intval($mo[1], 16);
  42. if ($_unreserved[$i]) {
  43. return chr($i);
  44. } else {
  45. return strtoupper($mo[0]);
  46. }
  47. return $mo[0];
  48. }
  49. function _pct_encoded_replace($mo)
  50. {
  51. return chr(intval($mo[1], 16));
  52. }
  53. function remove_dot_segments($path)
  54. {
  55. $result_segments = array();
  56. while ($path) {
  57. if (_startswith($path, '../')) {
  58. $path = substr($path, 3);
  59. } else if (_startswith($path, './')) {
  60. $path = substr($path, 2);
  61. } else if (_startswith($path, '/./')) {
  62. $path = substr($path, 2);
  63. } else if ($path == '/.') {
  64. $path = '/';
  65. } else if (_startswith($path, '/../')) {
  66. $path = substr($path, 3);
  67. if ($result_segments) {
  68. array_pop($result_segments);
  69. }
  70. } else if ($path == '/..') {
  71. $path = '/';
  72. if ($result_segments) {
  73. array_pop($result_segments);
  74. }
  75. } else if (($path == '..') ||
  76. ($path == '.')) {
  77. $path = '';
  78. } else {
  79. $i = 0;
  80. if ($path[0] == '/') {
  81. $i = 1;
  82. }
  83. $i = strpos($path, '/', $i);
  84. if ($i === false) {
  85. $i = strlen($path);
  86. }
  87. $result_segments[] = substr($path, 0, $i);
  88. $path = substr($path, $i);
  89. }
  90. }
  91. return implode('', $result_segments);
  92. }
  93. function Auth_OpenID_urinorm($uri)
  94. {
  95. global $__uri_pattern, $__authority_pattern, $__pct_encoded_pattern;
  96. $uri_matches = array();
  97. preg_match($__uri_pattern, $uri, $uri_matches);
  98. if (count($uri_matches) < 9) {
  99. for ($i = count($uri_matches); $i <= 9; $i++) {
  100. $uri_matches[] = '';
  101. }
  102. }
  103. $scheme = $uri_matches[2];
  104. if ($scheme) {
  105. $scheme = strtolower($scheme);
  106. }
  107. $scheme = $uri_matches[2];
  108. if ($scheme === '') {
  109. // No scheme specified
  110. return null;
  111. }
  112. $scheme = strtolower($scheme);
  113. if (!in_array($scheme, array('http', 'https'))) {
  114. // Not an absolute HTTP or HTTPS URI
  115. return null;
  116. }
  117. $authority = $uri_matches[4];
  118. if ($authority === '') {
  119. // Not an absolute URI
  120. return null;
  121. }
  122. $authority_matches = array();
  123. preg_match($__authority_pattern, $authority, $authority_matches);
  124. if (count($authority_matches) === 0) {
  125. // URI does not have a valid authority
  126. return null;
  127. }
  128. if (count($authority_matches) < 4) {
  129. for ($i = count($authority_matches); $i <= 4; $i++) {
  130. $authority_matches[] = '';
  131. }
  132. }
  133. list($_whole, $userinfo, $host, $port) = $authority_matches;
  134. if ($userinfo === null) {
  135. $userinfo = '';
  136. }
  137. if (strpos($host, '%') !== -1) {
  138. $host = strtolower($host);
  139. $host = preg_replace_callback(
  140. $__pct_encoded_pattern, '_pct_encoded_replace', $host);
  141. // NO IDNA.
  142. // $host = unicode($host, 'utf-8').encode('idna');
  143. } else {
  144. $host = strtolower($host);
  145. }
  146. if ($port) {
  147. if (($port == ':') ||
  148. ($scheme == 'http' && $port == ':80') ||
  149. ($scheme == 'https' && $port == ':443')) {
  150. $port = '';
  151. }
  152. } else {
  153. $port = '';
  154. }
  155. $authority = $userinfo . $host . $port;
  156. $path = $uri_matches[5];
  157. $path = preg_replace_callback(
  158. $__pct_encoded_pattern,
  159. '_pct_encoded_replace_unreserved', $path);
  160. $path = remove_dot_segments($path);
  161. if (!$path) {
  162. $path = '/';
  163. }
  164. $query = $uri_matches[6];
  165. if ($query === null) {
  166. $query = '';
  167. }
  168. $fragment = $uri_matches[8];
  169. if ($fragment === null) {
  170. $fragment = '';
  171. }
  172. return $scheme . '://' . $authority . $path . $query . $fragment;
  173. }
  174. ?>