[ Index ]

PHP Cross Reference of Joomla 1.5.25

title

Body

[close]

/libraries/openid/Auth/OpenID/ -> URINorm.php (source)

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


Generated: Mon Nov 14 16:47:20 2011 Cross-referenced by PHPXref 0.7.1