What's new? | Help | Directory | Sign in
Google
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php

/**
* Parse a XRDS discovery description to a simple array format.
*
* For now a simple parse of the document. Better error checking
* in a later version.
*
* @version $Id$
* @author Marc Worrell <marc@mediamatic.nl>
* @copyright (c) 2008 Mediamatic Lab
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

/* example of use:

header('content-type: text/plain');
$file = file_get_contents('../../test/discovery/xrds-magnolia.xrds');
$xrds = xrds_parse($file);
print_r($xrds);

*/

/**
* Parse the xrds file in the argument. The xrds description must have been
* fetched via curl or something else.
*
* TODO: more robust checking, support for more service documents
* TODO: support for URIs to definition instead of local xml:id
*
* @param string data contents of xrds file
* @exception Exception when the file is in an unknown format
* @return array
*/
function xrds_parse ( $data )
{
$oauth = array();
$doc = @DOMDocument::loadXML($data);
if ($doc === false)
{
throw new Exception('Error in XML, can\'t load XRDS document');
}

$xpath = new DOMXPath($doc);
$xpath->registerNamespace('xrds', 'xri://$xrds');
$xpath->registerNamespace('xrd', 'xri://$XRD*($v*2.0)');
$xpath->registerNamespace('simple', 'http://xrds-simple.net/core/1.0');

$uris = xrds_oauth_service_uris($xpath);

foreach ($uris as $uri)
{
// TODO: support uris referring to service documents outside this one
if ($uri{0} == '#')
{
$id = substr($uri, 1);
$oauth = xrds_xrd_oauth($xpath, $id);
if (is_array($oauth) && !empty($oauth))
{
return $oauth;
}
}
}

return false;
}


/**
* Parse a XRD definition for OAuth and return the uris etc.
*
* @param XPath xpath
* @param string id
* @return array
*/
function xrds_xrd_oauth ( $xpath, $id )
{
$oauth = array();
$xrd = $xpath->query('//xrds:XRDS/xrd:XRD[@xml:id="'.$id.'"]');
if ($xrd->length >= 1)
{
$x = $xrd->item(0);
$services = array();
foreach ($x->childNodes as $n)
{
switch ($n->nodeName)
{
case 'Type':
if ($n->nodeValue != 'xri://$xrds*simple')
{
// Not a simple XRDS document
return false;
}
break;
case 'Expires':
$oauth['expires'] = $n->nodeValue;
break;
case 'Service':
list($type,$service) = xrds_xrd_oauth_service($n);
if ($type)
{
$services[$type][xrds_priority($n)][] = $service;
}
break;
}
}

// Flatten the services on priority
foreach ($services as $type => $service)
{
$oauth[$type] = xrds_priority_flatten($service);
}
}
else
{
$oauth = false;
}
return $oauth;
}


/**
* Parse a service definition for OAuth in a simple xrd element
*
* @param DOMElement n
* @return array (type, service desc)
*/
function xrds_xrd_oauth_service ( $n )
{
$service = array(
'uri' => '',
'signature_method' => array(),
'parameters' => array()
);

$type = false;
foreach ($n->childNodes as $c)
{
$name = $c->nodeName;
$value = $c->nodeValue;

if ($name == 'URI')
{
$service['uri'] = $value;
}
else if ($name == 'Type')
{
if (strncmp($value, 'http://oauth.net/core/1.0/endpoint/', 35) == 0)
{
$type = basename($value);
}
else if (strncmp($value, 'http://oauth.net/core/1.0/signature/', 36) == 0)
{
$service['signature_method'][] = basename($value);
}
else if (strncmp($value, 'http://oauth.net/core/1.0/parameters/', 37) == 0)
{
$service['parameters'][] = basename($value);
}
else if (strncmp($value, 'http://oauth.net/discovery/1.0/consumer-identity/', 49) == 0)
{
$type = 'consumer_identity';
$service['method'] = basename($value);
unset($service['signature_method']);
unset($service['parameters']);
}
else
{
$service['unknown'][] = $value;
}
}
else if ($name == 'LocalID')
{
$service['consumer_key'] = $value;
}
else if ($name{0} != '#')
{
$service[strtolower($name)] = $value;
}
}
return array($type, $service);
}


/**
* Return the OAuth service uris in order of the priority.
*
* @param XPath xpath
* @return array
*/
function xrds_oauth_service_uris ( $xpath )
{
$uris = array();
$xrd_oauth = $xpath->query('//xrds:XRDS/xrd:XRD/xrd:Service/xrd:Type[.=\'http://oauth.net/discovery/1.0\']');
if ($xrd_oauth->length > 0)
{
$service = array();
foreach ($xrd_oauth as $xo)
{
// Find the URI of the service definition
$cs = $xo->parentNode->childNodes;
foreach ($cs as $c)
{
if ($c->nodeName == 'URI')
{
$prio = xrds_priority($xo);
$service[$prio][] = $c->nodeValue;
}
}
}
$uris = xrds_priority_flatten($service);
}
return $uris;
}



/**
* Flatten an array according to the priority
*
* @param array ps buckets per prio
* @return array one dimensional array
*/
function xrds_priority_flatten ( $ps )
{
$prio = array();
$null = array();
ksort($ps);
foreach ($ps as $idx => $bucket)
{
if (!empty($bucket))
{
if ($idx == 'null')
{
$null = $bucket;
}
else
{
$prio = array_merge($prio, $bucket);
}
}
}
$prio = array_merge($prio, $bucket);
return $prio;
}


/**
* Fetch the priority of a element
*
* @param DOMElement elt
* @return mixed 'null' or int
*/
function xrds_priority ( $elt )
{
if ($elt->hasAttribute('priority'))
{
$prio = $elt->getAttribute('priority');
if (is_numeric($prio))
{
$prio = intval($prio);
}
}
else
{
$prio = 'null';
}
return $prio;
}


/* vi:set ts=4 sts=4 sw=4 binary noeol: */

?>
Show details Hide details

Change log

r11 by ma...@pobox.com on Apr 08, 2008   Diff
removed debugging code, moved as an
example to start of file.
Go to: 
Project members, sign in to write a code review

Older revisions

r10 by ma...@pobox.com on Apr 08, 2008   Diff
Added simple xrds parsing.
Work in progress, needs support for
more cases.
All revisions of this file

File info

Size: 6191 bytes, 287 lines