URINorm.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * URI normalization routines.
  4. *
  5. * @package OpenID
  6. * @author JanRain, Inc. <openid@janrain.com>
  7. * @copyright 2005-2008 Janrain, Inc.
  8. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  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. # gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
  25. #
  26. # sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
  27. # / "*" / "+" / "," / ";" / "="
  28. #
  29. # unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
  30. function Auth_OpenID_getURLIllegalCharRE()
  31. {
  32. return "/([^-A-Za-z0-9:\/\?#\[\]@\!\$&'\(\)\*\+,;=\._~\%])/";
  33. }
  34. function Auth_OpenID_getUnreserved()
  35. {
  36. $_unreserved = array();
  37. for ($i = 0; $i < 256; $i++) {
  38. $_unreserved[$i] = false;
  39. }
  40. for ($i = ord('A'); $i <= ord('Z'); $i++) {
  41. $_unreserved[$i] = true;
  42. }
  43. for ($i = ord('0'); $i <= ord('9'); $i++) {
  44. $_unreserved[$i] = true;
  45. }
  46. for ($i = ord('a'); $i <= ord('z'); $i++) {
  47. $_unreserved[$i] = true;
  48. }
  49. $_unreserved[ord('-')] = true;
  50. $_unreserved[ord('.')] = true;
  51. $_unreserved[ord('_')] = true;
  52. $_unreserved[ord('~')] = true;
  53. return $_unreserved;
  54. }
  55. function Auth_OpenID_getEscapeRE()
  56. {
  57. $parts = array();
  58. foreach (array_merge(Auth_Yadis_getUCSChars(),
  59. Auth_Yadis_getIPrivateChars()) as $pair) {
  60. list($m, $n) = $pair;
  61. $parts[] = sprintf("%s-%s", chr($m), chr($n));
  62. }
  63. return sprintf('[%s]', implode('', $parts));
  64. }
  65. function Auth_OpenID_pct_encoded_replace_unreserved($mo)
  66. {
  67. $_unreserved = Auth_OpenID_getUnreserved();
  68. $i = intval($mo[1], 16);
  69. if ($_unreserved[$i]) {
  70. return chr($i);
  71. } else {
  72. return strtoupper($mo[0]);
  73. }
  74. return $mo[0];
  75. }
  76. function Auth_OpenID_pct_encoded_replace($mo)
  77. {
  78. return chr(intval($mo[1], 16));
  79. }
  80. function Auth_OpenID_remove_dot_segments($path)
  81. {
  82. $result_segments = array();
  83. while ($path) {
  84. if (Auth_Yadis_startswith($path, '../')) {
  85. $path = substr($path, 3);
  86. } else if (Auth_Yadis_startswith($path, './')) {
  87. $path = substr($path, 2);
  88. } else if (Auth_Yadis_startswith($path, '/./')) {
  89. $path = substr($path, 2);
  90. } else if ($path == '/.') {
  91. $path = '/';
  92. } else if (Auth_Yadis_startswith($path, '/../')) {
  93. $path = substr($path, 3);
  94. if ($result_segments) {
  95. array_pop($result_segments);
  96. }
  97. } else if ($path == '/..') {
  98. $path = '/';
  99. if ($result_segments) {
  100. array_pop($result_segments);
  101. }
  102. } else if (($path == '..') ||
  103. ($path == '.')) {
  104. $path = '';
  105. } else {
  106. $i = 0;
  107. if ($path[0] == '/') {
  108. $i = 1;
  109. }
  110. $i = strpos($path, '/', $i);
  111. if ($i === false) {
  112. $i = strlen($path);
  113. }
  114. $result_segments[] = substr($path, 0, $i);
  115. $path = substr($path, $i);
  116. }
  117. }
  118. return implode('', $result_segments);
  119. }
  120. function Auth_OpenID_urinorm($uri)
  121. {
  122. $uri_matches = array();
  123. preg_match(Auth_OpenID_getURIPattern(), $uri, $uri_matches);
  124. if (count($uri_matches) < 9) {
  125. for ($i = count($uri_matches); $i <= 9; $i++) {
  126. $uri_matches[] = '';
  127. }
  128. }
  129. $illegal_matches = array();
  130. preg_match(Auth_OpenID_getURLIllegalCharRE(),
  131. $uri, $illegal_matches);
  132. if ($illegal_matches) {
  133. return null;
  134. }
  135. $scheme = $uri_matches[2];
  136. if ($scheme) {
  137. $scheme = strtolower($scheme);
  138. }
  139. $scheme = $uri_matches[2];
  140. if ($scheme === '') {
  141. // No scheme specified
  142. return null;
  143. }
  144. $scheme = strtolower($scheme);
  145. if (!in_array($scheme, array('http', 'https'))) {
  146. // Not an absolute HTTP or HTTPS URI
  147. return null;
  148. }
  149. $authority = $uri_matches[4];
  150. if ($authority === '') {
  151. // Not an absolute URI
  152. return null;
  153. }
  154. $authority_matches = array();
  155. preg_match(Auth_OpenID_getAuthorityPattern(),
  156. $authority, $authority_matches);
  157. if (count($authority_matches) === 0) {
  158. // URI does not have a valid authority
  159. return null;
  160. }
  161. if (count($authority_matches) < 4) {
  162. for ($i = count($authority_matches); $i <= 4; $i++) {
  163. $authority_matches[] = '';
  164. }
  165. }
  166. list($_whole, $userinfo, $host, $port) = $authority_matches;
  167. if ($userinfo === null) {
  168. $userinfo = '';
  169. }
  170. if (strpos($host, '%') !== -1) {
  171. $host = strtolower($host);
  172. $host = preg_replace_callback(
  173. Auth_OpenID_getEncodedPattern(),
  174. 'Auth_OpenID_pct_encoded_replace', $host);
  175. // NO IDNA.
  176. // $host = unicode($host, 'utf-8').encode('idna');
  177. } else {
  178. $host = strtolower($host);
  179. }
  180. if ($port) {
  181. if (($port == ':') ||
  182. ($scheme == 'http' && $port == ':80') ||
  183. ($scheme == 'https' && $port == ':443')) {
  184. $port = '';
  185. }
  186. } else {
  187. $port = '';
  188. }
  189. $authority = $userinfo . $host . $port;
  190. $path = $uri_matches[5];
  191. $path = preg_replace_callback(
  192. Auth_OpenID_getEncodedPattern(),
  193. 'Auth_OpenID_pct_encoded_replace_unreserved', $path);
  194. $path = Auth_OpenID_remove_dot_segments($path);
  195. if (!$path) {
  196. $path = '/';
  197. }
  198. $query = $uri_matches[6];
  199. if ($query === null) {
  200. $query = '';
  201. }
  202. $fragment = $uri_matches[8];
  203. if ($fragment === null) {
  204. $fragment = '';
  205. }
  206. return $scheme . '://' . $authority . $path . $query . $fragment;
  207. }