Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: OpenID

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /openid/Auth/OpenID/URINorm.php

Documentation is available at URINorm.php

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

Documentation generated on Mon, 05 Mar 2007 21:30:16 +0000 by phpDocumentor 1.3.1