4 * Routines for XRI resolution.
7 * @author JanRain, Inc. <openid@janrain.com>
8 * @copyright 2005-2008 Janrain, Inc.
9 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
12 require_once 'Auth/Yadis/Misc.php';
13 require_once 'Auth/Yadis/Yadis.php';
14 require_once 'Auth/OpenID.php';
16 function Auth_Yadis_getDefaultProxy()
18 return 'http://xri.net/';
21 function Auth_Yadis_getXRIAuthorities()
23 return array('!', '=', '@', '+', '$', '(');
26 function Auth_Yadis_getEscapeRE()
29 foreach (array_merge(Auth_Yadis_getUCSChars(),
30 Auth_Yadis_getIPrivateChars()) as $pair) {
32 $parts[] = sprintf("%s-%s", chr($m), chr($n));
35 return sprintf('/[%s]/', implode('', $parts));
38 function Auth_Yadis_getXrefRE()
43 function Auth_Yadis_identifierScheme($identifier)
45 if (Auth_Yadis_startswith($identifier, 'xri://') ||
47 in_array($identifier[0], Auth_Yadis_getXRIAuthorities()))) {
54 function Auth_Yadis_toIRINormal($xri)
56 if (!Auth_Yadis_startswith($xri, 'xri://')) {
57 $xri = 'xri://' . $xri;
60 return Auth_Yadis_escapeForIRI($xri);
63 function _escape_xref($xref_match)
65 $xref = $xref_match[0];
66 $xref = str_replace('/', '%2F', $xref);
67 $xref = str_replace('?', '%3F', $xref);
68 $xref = str_replace('#', '%23', $xref);
72 function Auth_Yadis_escapeForIRI($xri)
74 $xri = str_replace('%', '%25', $xri);
75 $xri = preg_replace_callback(Auth_Yadis_getXrefRE(),
76 '_escape_xref', $xri);
80 function Auth_Yadis_toURINormal($xri)
82 return Auth_Yadis_iriToURI(Auth_Yadis_toIRINormal($xri));
85 function Auth_Yadis_iriToURI($iri)
90 // According to RFC 3987, section 3.1, "Mapping of IRIs to URIs"
91 return preg_replace_callback(Auth_Yadis_getEscapeRE(),
92 'Auth_Yadis_pct_escape_unicode', $iri);
97 function Auth_Yadis_XRIAppendArgs($url, $args)
99 // Append some arguments to an HTTP query. Yes, this is just like
100 // OpenID's appendArgs, but with special seasoning for XRI
103 if (count($args) == 0) {
107 // Non-empty array; if it is an array of arrays, use multisort;
108 // otherwise use sort.
109 if (array_key_exists(0, $args) &&
110 is_array($args[0])) {
113 $keys = array_keys($args);
116 foreach ($keys as $key) {
117 $new_args[] = array($key, $args[$key]);
122 // According to XRI Resolution section "QXRI query parameters":
124 // "If the original QXRI had a null query component (only a
125 // leading question mark), or a query component consisting of
126 // only question marks, one additional leading question mark MUST
127 // be added when adding any XRI resolution parameters."
128 if (strpos(rtrim($url, '?'), '?') !== false) {
134 return $url . $sep . Auth_OpenID::httpBuildQuery($args);
137 function Auth_Yadis_providerIsAuthoritative($providerID, $canonicalID)
139 $lastbang = strrpos($canonicalID, '!');
140 $p = substr($canonicalID, 0, $lastbang);
141 return $p == $providerID;
144 function Auth_Yadis_rootAuthority($xri)
146 // Return the root authority for an XRI.
150 if (Auth_Yadis_startswith($xri, 'xri://')) {
151 $xri = substr($xri, 6);
154 $authority = explode('/', $xri, 2);
155 $authority = $authority[0];
156 if ($authority[0] == '(') {
158 // XXX: This is incorrect if someone nests cross-references so
159 // there is another close-paren in there. Hopefully nobody
160 // does that before we have a real xriparse function.
161 // Hopefully nobody does that *ever*.
162 $root = substr($authority, 0, strpos($authority, ')') + 1);
163 } else if (in_array($authority[0], Auth_Yadis_getXRIAuthorities())) {
164 // Other XRI reference.
165 $root = $authority[0];
168 $_segments = explode("!", $authority);
170 foreach ($_segments as $s) {
171 $segments = array_merge($segments, explode("*", $s));
173 $root = $segments[0];
176 return Auth_Yadis_XRI($root);
179 function Auth_Yadis_XRI($xri)
181 if (!Auth_Yadis_startswith($xri, 'xri://')) {
182 $xri = 'xri://' . $xri;
187 function Auth_Yadis_getCanonicalID($iname, $xrds)
189 // Returns false or a canonical ID value.
191 // Now nodes are in reverse order.
192 $xrd_list = array_reverse($xrds->allXrdNodes);
193 $parser = $xrds->parser;
194 $node = $xrd_list[0];
196 $canonicalID_nodes = $parser->evalXPath('xrd:CanonicalID', $node);
198 if (!$canonicalID_nodes) {
202 $canonicalID = $canonicalID_nodes[0];
203 $canonicalID = Auth_Yadis_XRI($parser->content($canonicalID));
205 $childID = $canonicalID;
207 for ($i = 1; $i < count($xrd_list); $i++) {
208 $xrd = $xrd_list[$i];
210 $parent_sought = substr($childID, 0, strrpos($childID, '!'));
211 $parentCID = $parser->evalXPath('xrd:CanonicalID', $xrd);
215 $parentCID = Auth_Yadis_XRI($parser->content($parentCID[0]));
217 if (strcasecmp($parent_sought, $parentCID)) {
222 $childID = $parent_sought;
225 $root = Auth_Yadis_rootAuthority($iname);
226 if (!Auth_Yadis_providerIsAuthoritative($root, $childID)) {