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