XRI.php

Go to the documentation of this file.
00001 <?php
00002 
00012 require_once 'Auth/Yadis/Misc.php';
00013 require_once 'Auth/Yadis/Yadis.php';
00014 require_once 'Auth/OpenID.php';
00015 
00016 function Auth_Yadis_getDefaultProxy()
00017 {
00018     return 'http://xri.net/';
00019 }
00020 
00021 function Auth_Yadis_getXRIAuthorities()
00022 {
00023     return array('!', '=', '@', '+', '$', '(');
00024 }
00025 
00026 function Auth_Yadis_getEscapeRE()
00027 {
00028     $parts = array();
00029     foreach (array_merge(Auth_Yadis_getUCSChars(),
00030                          Auth_Yadis_getIPrivateChars()) as $pair) {
00031         list($m, $n) = $pair;
00032         $parts[] = sprintf("%s-%s", chr($m), chr($n));
00033     }
00034 
00035     return sprintf('/[%s]/', implode('', $parts));
00036 }
00037 
00038 function Auth_Yadis_getXrefRE()
00039 {
00040     return '/\((.*?)\)/';
00041 }
00042 
00043 function Auth_Yadis_identifierScheme($identifier)
00044 {
00045     if (Auth_Yadis_startswith($identifier, 'xri://') ||
00046         ($identifier &&
00047           in_array($identifier[0], Auth_Yadis_getXRIAuthorities()))) {
00048         return "XRI";
00049     } else {
00050         return "URI";
00051     }
00052 }
00053 
00054 function Auth_Yadis_toIRINormal($xri)
00055 {
00056     if (!Auth_Yadis_startswith($xri, 'xri://')) {
00057         $xri = 'xri://' . $xri;
00058     }
00059 
00060     return Auth_Yadis_escapeForIRI($xri);
00061 }
00062 
00063 function _escape_xref($xref_match)
00064 {
00065     $xref = $xref_match[0];
00066     $xref = str_replace('/', '%2F', $xref);
00067     $xref = str_replace('?', '%3F', $xref);
00068     $xref = str_replace('#', '%23', $xref);
00069     return $xref;
00070 }
00071 
00072 function Auth_Yadis_escapeForIRI($xri)
00073 {
00074     $xri = str_replace('%', '%25', $xri);
00075     $xri = preg_replace_callback(Auth_Yadis_getXrefRE(),
00076                                  '_escape_xref', $xri);
00077     return $xri;
00078 }
00079 
00080 function Auth_Yadis_toURINormal($xri)
00081 {
00082     return Auth_Yadis_iriToURI(Auth_Yadis_toIRINormal($xri));
00083 }
00084 
00085 function Auth_Yadis_iriToURI($iri)
00086 {
00087     if (1) {
00088         return $iri;
00089     } else {
00090         // According to RFC 3987, section 3.1, "Mapping of IRIs to URIs"
00091         return preg_replace_callback(Auth_Yadis_getEscapeRE(),
00092                                      'Auth_Yadis_pct_escape_unicode', $iri);
00093     }
00094 }
00095 
00096 
00097 function Auth_Yadis_XRIAppendArgs($url, $args)
00098 {
00099     // Append some arguments to an HTTP query.  Yes, this is just like
00100     // OpenID's appendArgs, but with special seasoning for XRI
00101     // queries.
00102 
00103     if (count($args) == 0) {
00104         return $url;
00105     }
00106 
00107     // Non-empty array; if it is an array of arrays, use multisort;
00108     // otherwise use sort.
00109     if (array_key_exists(0, $args) &&
00110         is_array($args[0])) {
00111         // Do nothing here.
00112     } else {
00113         $keys = array_keys($args);
00114         sort($keys);
00115         $new_args = array();
00116         foreach ($keys as $key) {
00117             $new_args[] = array($key, $args[$key]);
00118         }
00119         $args = $new_args;
00120     }
00121 
00122     // According to XRI Resolution section "QXRI query parameters":
00123     //
00124     // "If the original QXRI had a null query component (only a
00125     //  leading question mark), or a query component consisting of
00126     //  only question marks, one additional leading question mark MUST
00127     //  be added when adding any XRI resolution parameters."
00128     if (strpos(rtrim($url, '?'), '?') !== false) {
00129         $sep = '&';
00130     } else {
00131         $sep = '?';
00132     }
00133 
00134     return $url . $sep . Auth_OpenID::httpBuildQuery($args);
00135 }
00136 
00137 function Auth_Yadis_providerIsAuthoritative($providerID, $canonicalID)
00138 {
00139     $lastbang = strrpos($canonicalID, '!');
00140     $p = substr($canonicalID, 0, $lastbang);
00141     return $p == $providerID;
00142 }
00143 
00144 function Auth_Yadis_rootAuthority($xri)
00145 {
00146     // Return the root authority for an XRI.
00147 
00148     $root = null;
00149 
00150     if (Auth_Yadis_startswith($xri, 'xri://')) {
00151         $xri = substr($xri, 6);
00152     }
00153 
00154     $authority = explode('/', $xri, 2);
00155     $authority = $authority[0];
00156     if ($authority[0] == '(') {
00157         // Cross-reference.
00158         // XXX: This is incorrect if someone nests cross-references so
00159         //   there is another close-paren in there.  Hopefully nobody
00160         //   does that before we have a real xriparse function.
00161         //   Hopefully nobody does that *ever*.
00162         $root = substr($authority, 0, strpos($authority, ')') + 1);
00163     } else if (in_array($authority[0], Auth_Yadis_getXRIAuthorities())) {
00164         // Other XRI reference.
00165         $root = $authority[0];
00166     } else {
00167         // IRI reference.
00168         $_segments = explode("!", $authority);
00169         $segments = array();
00170         foreach ($_segments as $s) {
00171             $segments = array_merge($segments, explode("*", $s));
00172         }
00173         $root = $segments[0];
00174     }
00175 
00176     return Auth_Yadis_XRI($root);
00177 }
00178 
00179 function Auth_Yadis_XRI($xri)
00180 {
00181     if (!Auth_Yadis_startswith($xri, 'xri://')) {
00182         $xri = 'xri://' . $xri;
00183     }
00184     return $xri;
00185 }
00186 
00187 function Auth_Yadis_getCanonicalID($iname, $xrds)
00188 {
00189     // Returns false or a canonical ID value.
00190 
00191     // Now nodes are in reverse order.
00192     $xrd_list = array_reverse($xrds->allXrdNodes);
00193     $parser =& $xrds->parser;
00194     $node = $xrd_list[0];
00195 
00196     $canonicalID_nodes = $parser->evalXPath('xrd:CanonicalID', $node);
00197 
00198     if (!$canonicalID_nodes) {
00199         return false;
00200     }
00201 
00202     $canonicalID = $canonicalID_nodes[0];
00203     $canonicalID = Auth_Yadis_XRI($parser->content($canonicalID));
00204 
00205     $childID = $canonicalID;
00206 
00207     for ($i = 1; $i < count($xrd_list); $i++) {
00208         $xrd = $xrd_list[$i];
00209 
00210         $parent_sought = substr($childID, 0, strrpos($childID, '!'));
00211         $parentCID = $parser->evalXPath('xrd:CanonicalID', $xrd);
00212         if (!$parentCID) {
00213             return false;
00214         }
00215         $parentCID = Auth_Yadis_XRI($parser->content($parentCID[0]));
00216 
00217         if (strcasecmp($parent_sought, $parentCID)) {
00218             // raise XRDSFraud.
00219             return false;
00220         }
00221 
00222         $childID = $parent_sought;
00223     }
00224 
00225     $root = Auth_Yadis_rootAuthority($iname);
00226     if (!Auth_Yadis_providerIsAuthoritative($root, $childID)) {
00227         // raise XRDSFraud.
00228         return false;
00229     }
00230 
00231     return $canonicalID;
00232 }
00233 
00234 ?>

Generated on Thu Feb 19 15:02:21 2009 for OXID eShop CE by  doxygen 1.5.5