[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/openid/Auth/Yadis/ -> XRI.php (source)

   1  <?php
   2  
   3  /**
   4   * Routines for XRI resolution.
   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  require_once 'Auth/Yadis/Yadis.php';
  17  require_once 'Auth/OpenID.php';
  18  
  19  function Auth_Yadis_getDefaultProxy()
  20  {
  21      return 'http://xri.net/';
  22  }
  23  
  24  function Auth_Yadis_getXRIAuthorities()
  25  {
  26      return array('!', '=', '@', '+', '$', '(');
  27  }
  28  
  29  function Auth_Yadis_getEscapeRE()
  30  {
  31      $parts = array();
  32      foreach (array_merge(Auth_Yadis_getUCSChars(),
  33                           Auth_Yadis_getIPrivateChars()) as $pair) {
  34          list($m, $n) = $pair;
  35          $parts[] = sprintf("%s-%s", chr($m), chr($n));
  36      }
  37  
  38      return sprintf('/[%s]/', implode('', $parts));
  39  }
  40  
  41  function Auth_Yadis_getXrefRE()
  42  {
  43      return '/\((.*?)\)/';
  44  }
  45  
  46  function Auth_Yadis_identifierScheme($identifier)
  47  {
  48      if (Auth_Yadis_startswith($identifier, 'xri://') ||
  49          ($identifier &&
  50            in_array($identifier[0], Auth_Yadis_getXRIAuthorities()))) {
  51          return "XRI";
  52      } else {
  53          return "URI";
  54      }
  55  }
  56  
  57  function Auth_Yadis_toIRINormal($xri)
  58  {
  59      if (!Auth_Yadis_startswith($xri, 'xri://')) {
  60          $xri = 'xri://' . $xri;
  61      }
  62  
  63      return Auth_Yadis_escapeForIRI($xri);
  64  }
  65  
  66  function _escape_xref($xref_match)
  67  {
  68      $xref = $xref_match[0];
  69      $xref = str_replace('/', '%2F', $xref);
  70      $xref = str_replace('?', '%3F', $xref);
  71      $xref = str_replace('#', '%23', $xref);
  72      return $xref;
  73  }
  74  
  75  function Auth_Yadis_escapeForIRI($xri)
  76  {
  77      $xri = str_replace('%', '%25', $xri);
  78      $xri = preg_replace_callback(Auth_Yadis_getXrefRE(),
  79                                   '_escape_xref', $xri);
  80      return $xri;
  81  }
  82  
  83  function Auth_Yadis_toURINormal($xri)
  84  {
  85      return Auth_Yadis_iriToURI(Auth_Yadis_toIRINormal($xri));
  86  }
  87  
  88  function Auth_Yadis_iriToURI($iri)
  89  {
  90      if (1) {
  91          return $iri;
  92      } else {
  93          // According to RFC 3987, section 3.1, "Mapping of IRIs to URIs"
  94          return preg_replace_callback(Auth_Yadis_getEscapeRE(),
  95                                       'Auth_Yadis_pct_escape_unicode', $iri);
  96      }
  97  }
  98  
  99  
 100  function Auth_Yadis_XRIAppendArgs($url, $args)
 101  {
 102      // Append some arguments to an HTTP query.  Yes, this is just like
 103      // OpenID's appendArgs, but with special seasoning for XRI
 104      // queries.
 105  
 106      if (count($args) == 0) {
 107          return $url;
 108      }
 109  
 110      // Non-empty array; if it is an array of arrays, use multisort;
 111      // otherwise use sort.
 112      if (array_key_exists(0, $args) &&
 113          is_array($args[0])) {
 114          // Do nothing here.
 115      } else {
 116          $keys = array_keys($args);
 117          sort($keys);
 118          $new_args = array();
 119          foreach ($keys as $key) {
 120              $new_args[] = array($key, $args[$key]);
 121          }
 122          $args = $new_args;
 123      }
 124  
 125      // According to XRI Resolution section "QXRI query parameters":
 126      //
 127      // "If the original QXRI had a null query component (only a
 128      //  leading question mark), or a query component consisting of
 129      //  only question marks, one additional leading question mark MUST
 130      //  be added when adding any XRI resolution parameters."
 131      if (strpos(rtrim($url, '?'), '?') !== false) {
 132          $sep = '&';
 133      } else {
 134          $sep = '?';
 135      }
 136  
 137      return $url . $sep . Auth_OpenID::httpBuildQuery($args);
 138  }
 139  
 140  function Auth_Yadis_providerIsAuthoritative($providerID, $canonicalID)
 141  {
 142      $lastbang = strrpos($canonicalID, '!');
 143      $p = substr($canonicalID, 0, $lastbang);
 144      return $p == $providerID;
 145  }
 146  
 147  function Auth_Yadis_rootAuthority($xri)
 148  {
 149      // Return the root authority for an XRI.
 150  
 151      $root = null;
 152  
 153      if (Auth_Yadis_startswith($xri, 'xri://')) {
 154          $xri = substr($xri, 6);
 155      }
 156  
 157      $authority = explode('/', $xri, 2);
 158      $authority = $authority[0];
 159      if ($authority[0] == '(') {
 160          // Cross-reference.
 161          // XXX: This is incorrect if someone nests cross-references so
 162          //   there is another close-paren in there.  Hopefully nobody
 163          //   does that before we have a real xriparse function.
 164          //   Hopefully nobody does that *ever*.
 165          $root = substr($authority, 0, strpos($authority, ')') + 1);
 166      } else if (in_array($authority[0], Auth_Yadis_getXRIAuthorities())) {
 167          // Other XRI reference.
 168          $root = $authority[0];
 169      } else {
 170          // IRI reference.
 171          $_segments = explode("!", $authority);
 172          $segments = array();
 173          foreach ($_segments as $s) {
 174              $segments = array_merge($segments, explode("*", $s));
 175          }
 176          $root = $segments[0];
 177      }
 178  
 179      return Auth_Yadis_XRI($root);
 180  }
 181  
 182  function Auth_Yadis_XRI($xri)
 183  {
 184      if (!Auth_Yadis_startswith($xri, 'xri://')) {
 185          $xri = 'xri://' . $xri;
 186      }
 187      return $xri;
 188  }
 189  
 190  function Auth_Yadis_getCanonicalID($iname, $xrds)
 191  {
 192      // Returns false or a canonical ID value.
 193  
 194      // Now nodes are in reverse order.
 195      $xrd_list = array_reverse($xrds->allXrdNodes);
 196      $parser =& $xrds->parser;
 197      $node = $xrd_list[0];
 198  
 199      $canonicalID_nodes = $parser->evalXPath('xrd:CanonicalID', $node);
 200  
 201      if (!$canonicalID_nodes) {
 202          return false;
 203      }
 204  
 205      $canonicalID = $canonicalID_nodes[0];
 206      $canonicalID = Auth_Yadis_XRI($parser->content($canonicalID));
 207  
 208      $childID = $canonicalID;
 209  
 210      for ($i = 1; $i < count($xrd_list); $i++) {
 211          $xrd = $xrd_list[$i];
 212  
 213          $parent_sought = substr($childID, 0, strrpos($childID, '!'));
 214          $parentCID = $parser->evalXPath('xrd:CanonicalID', $xrd);
 215          if (!$parentCID) {
 216              return false;
 217          }
 218          $parentCID = Auth_Yadis_XRI($parser->content($parentCID[0]));
 219  
 220          if (strcasecmp($parent_sought, $parentCID)) {
 221              // raise XRDSFraud.
 222              return false;
 223          }
 224  
 225          $childID = $parent_sought;
 226      }
 227  
 228      $root = Auth_Yadis_rootAuthority($iname);
 229      if (!Auth_Yadis_providerIsAuthoritative($root, $childID)) {
 230          // raise XRDSFraud.
 231          return false;
 232      }
 233  
 234      return $canonicalID;
 235  }
 236  
 237  ?>


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1