URINorm.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. $code = intval($mo[1], 16);
  79. // Prevent request splitting by ignoring newline and space characters
  80. if($code === 0xA || $code === 0xD || $code === ord(' '))
  81. {
  82. return $mo[0];
  83. }
  84. else
  85. {
  86. return chr($code);
  87. }
  88. }
  89. function Auth_OpenID_remove_dot_segments($path)
  90. {
  91. $result_segments = array();
  92. while ($path) {
  93. if (Auth_Yadis_startswith($path, '../')) {
  94. $path = substr($path, 3);
  95. } else if (Auth_Yadis_startswith($path, './')) {
  96. $path = substr($path, 2);
  97. } else if (Auth_Yadis_startswith($path, '/./')) {
  98. $path = substr($path, 2);
  99. } else if ($path == '/.') {
  100. $path = '/';
  101. } else if (Auth_Yadis_startswith($path, '/../')) {
  102. $path = substr($path, 3);
  103. if ($result_segments) {
  104. array_pop($result_segments);
  105. }
  106. } else if ($path == '/..') {
  107. $path = '/';
  108. if ($result_segments) {
  109. array_pop($result_segments);
  110. }
  111. } else if (($path == '..') ||
  112. ($path == '.')) {
  113. $path = '';
  114. } else {
  115. $i = 0;
  116. if ($path[0] == '/') {
  117. $i = 1;
  118. }
  119. $i = strpos($path, '/', $i);
  120. if ($i === false) {
  121. $i = strlen($path);
  122. }
  123. $result_segments[] = substr($path, 0, $i);
  124. $path = substr($path, $i);
  125. }
  126. }
  127. return implode('', $result_segments);
  128. }
  129. function Auth_OpenID_urinorm($uri)
  130. {
  131. $uri_matches = array();
  132. preg_match(Auth_OpenID_getURIPattern(), $uri, $uri_matches);
  133. if (count($uri_matches) < 9) {
  134. for ($i = count($uri_matches); $i <= 9; $i++) {
  135. $uri_matches[] = '';
  136. }
  137. }
  138. $illegal_matches = array();
  139. preg_match(Auth_OpenID_getURLIllegalCharRE(),
  140. $uri, $illegal_matches);
  141. if ($illegal_matches) {
  142. return null;
  143. }
  144. $scheme = $uri_matches[2];
  145. if ($scheme) {
  146. $scheme = strtolower($scheme);
  147. }
  148. $scheme = $uri_matches[2];
  149. if ($scheme === '') {
  150. // No scheme specified
  151. return null;
  152. }
  153. $scheme = strtolower($scheme);
  154. if (!in_array($scheme, array('http', 'https'))) {
  155. // Not an absolute HTTP or HTTPS URI
  156. return null;
  157. }
  158. $authority = $uri_matches[4];
  159. if ($authority === '') {
  160. // Not an absolute URI
  161. return null;
  162. }
  163. $authority_matches = array();
  164. preg_match(Auth_OpenID_getAuthorityPattern(),
  165. $authority, $authority_matches);
  166. if (count($authority_matches) === 0) {
  167. // URI does not have a valid authority
  168. return null;
  169. }
  170. if (count($authority_matches) < 4) {
  171. for ($i = count($authority_matches); $i <= 4; $i++) {
  172. $authority_matches[] = '';
  173. }
  174. }
  175. list($_whole, $userinfo, $host, $port) = $authority_matches;
  176. if ($userinfo === null) {
  177. $userinfo = '';
  178. }
  179. if (strpos($host, '%') !== -1) {
  180. $host = strtolower($host);
  181. $host = preg_replace_callback(
  182. Auth_OpenID_getEncodedPattern(),
  183. 'Auth_OpenID_pct_encoded_replace', $host);
  184. // NO IDNA.
  185. // $host = unicode($host, 'utf-8').encode('idna');
  186. } else {
  187. $host = strtolower($host);
  188. }
  189. if ($port) {
  190. if (($port == ':') ||
  191. ($scheme == 'http' && $port == ':80') ||
  192. ($scheme == 'https' && $port == ':443')) {
  193. $port = '';
  194. }
  195. } else {
  196. $port = '';
  197. }
  198. $authority = $userinfo . $host . $port;
  199. $path = $uri_matches[5];
  200. $path = preg_replace_callback(
  201. Auth_OpenID_getEncodedPattern(),
  202. 'Auth_OpenID_pct_encoded_replace_unreserved', $path);
  203. $path = Auth_OpenID_remove_dot_segments($path);
  204. if (!$path) {
  205. $path = '/';
  206. }
  207. $query = $uri_matches[6];
  208. if ($query === null) {
  209. $query = '';
  210. }
  211. $fragment = $uri_matches[8];
  212. if ($fragment === null) {
  213. $fragment = '';
  214. }
  215. return $scheme . '://' . $authority . $path . $query . $fragment;
  216. }