Subversion Repositories Applications.annuaire

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
42 aurelien 1
<?php
2
 
3
/**
4
 * This module contains the XRDS parsing code.
5
 *
6
 * PHP versions 4 and 5
7
 *
8
 * LICENSE: See the COPYING file included in this distribution.
9
 *
10
 * @package Yadis
11
 * @author JanRain, Inc. <openid@janrain.com>
12
 * @copyright 2005 Janrain, Inc.
13
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
14
 */
15
 
16
/**
17
 * Require the XPath implementation.
18
 */
19
require_once 'Services/Yadis/XML.php';
20
 
21
/**
22
 * This match mode means a given service must match ALL filters passed
23
 * to the Services_Yadis_XRDS::services() call.
24
 */
25
define('SERVICES_YADIS_MATCH_ALL', 101);
26
 
27
/**
28
 * This match mode means a given service must match ANY filters (at
29
 * least one) passed to the Services_Yadis_XRDS::services() call.
30
 */
31
define('SERVICES_YADIS_MATCH_ANY', 102);
32
 
33
/**
34
 * The priority value used for service elements with no priority
35
 * specified.
36
 */
37
define('SERVICES_YADIS_MAX_PRIORITY', pow(2, 30));
38
 
39
function Services_Yadis_getNSMap()
40
{
41
    return array('xrds' => 'xri://$xrds',
42
                 'xrd' => 'xri://$xrd*($v*2.0)');
43
}
44
 
45
/**
46
 * @access private
47
 */
48
function Services_Yadis_array_scramble($arr)
49
{
50
    $result = array();
51
 
52
    while (count($arr)) {
53
        $index = array_rand($arr, 1);
54
        $result[] = $arr[$index];
55
        unset($arr[$index]);
56
    }
57
 
58
    return $result;
59
}
60
 
61
/**
62
 * This class represents a <Service> element in an XRDS document.
63
 * Objects of this type are returned by
64
 * Services_Yadis_XRDS::services() and
65
 * Services_Yadis_Yadis::services().  Each object corresponds directly
66
 * to a <Service> element in the XRDS and supplies a
67
 * getElements($name) method which you should use to inspect the
68
 * element's contents.  See {@link Services_Yadis_Yadis} for more
69
 * information on the role this class plays in Yadis discovery.
70
 *
71
 * @package Yadis
72
 */
73
class Services_Yadis_Service {
74
 
75
    /**
76
     * Creates an empty service object.
77
     */
78
    function Services_Yadis_Service()
79
    {
80
        $this->element = null;
81
        $this->parser = null;
82
    }
83
 
84
    /**
85
     * Return the URIs in the "Type" elements, if any, of this Service
86
     * element.
87
     *
88
     * @return array $type_uris An array of Type URI strings.
89
     */
90
    function getTypes()
91
    {
92
        $t = array();
93
        foreach ($this->getElements('xrd:Type') as $elem) {
94
            $c = $this->parser->content($elem);
95
            if ($c) {
96
                $t[] = $c;
97
            }
98
        }
99
        return $t;
100
    }
101
 
102
    /**
103
     * Return the URIs in the "URI" elements, if any, of this Service
104
     * element.  The URIs are returned sorted in priority order.
105
     *
106
     * @return array $uris An array of URI strings.
107
     */
108
    function getURIs()
109
    {
110
        $uris = array();
111
        $last = array();
112
 
113
        foreach ($this->getElements('xrd:URI') as $elem) {
114
            $uri_string = $this->parser->content($elem);
115
            $attrs = $this->parser->attributes($elem);
116
            if ($attrs &&
117
                array_key_exists('priority', $attrs)) {
118
                $priority = intval($attrs['priority']);
119
                if (!array_key_exists($priority, $uris)) {
120
                    $uris[$priority] = array();
121
                }
122
 
123
                $uris[$priority][] = $uri_string;
124
            } else {
125
                $last[] = $uri_string;
126
            }
127
        }
128
 
129
        $keys = array_keys($uris);
130
        sort($keys);
131
 
132
        // Rebuild array of URIs.
133
        $result = array();
134
        foreach ($keys as $k) {
135
            $new_uris = Services_Yadis_array_scramble($uris[$k]);
136
            $result = array_merge($result, $new_uris);
137
        }
138
 
139
        $result = array_merge($result,
140
                              Services_Yadis_array_scramble($last));
141
 
142
        return $result;
143
    }
144
 
145
    /**
146
     * Returns the "priority" attribute value of this <Service>
147
     * element, if the attribute is present.  Returns null if not.
148
     *
149
     * @return mixed $result Null or integer, depending on whether
150
     * this Service element has a 'priority' attribute.
151
     */
152
    function getPriority()
153
    {
154
        $attributes = $this->parser->attributes($this->element);
155
 
156
        if (array_key_exists('priority', $attributes)) {
157
            return intval($attributes['priority']);
158
        }
159
 
160
        return null;
161
    }
162
 
163
    /**
164
     * Used to get XML elements from this object's <Service> element.
165
     *
166
     * This is what you should use to get all custom information out
167
     * of this element. This is used by service filter functions to
168
     * determine whether a service element contains specific tags,
169
     * etc.  NOTE: this only considers elements which are direct
170
     * children of the <Service> element for this object.
171
     *
172
     * @param string $name The name of the element to look for
173
     * @return array $list An array of elements with the specified
174
     * name which are direct children of the <Service> element.  The
175
     * nodes returned by this function can be passed to $this->parser
176
     * methods (see {@link Services_Yadis_XMLParser}).
177
     */
178
    function getElements($name)
179
    {
180
        return $this->parser->evalXPath($name, $this->element);
181
    }
182
}
183
 
184
/**
185
 * This class performs parsing of XRDS documents.
186
 *
187
 * You should not instantiate this class directly; rather, call
188
 * parseXRDS statically:
189
 *
190
 * <pre>  $xrds = Services_Yadis_XRDS::parseXRDS($xml_string);</pre>
191
 *
192
 * If the XRDS can be parsed and is valid, an instance of
193
 * Services_Yadis_XRDS will be returned.  Otherwise, null will be
194
 * returned.  This class is used by the Services_Yadis_Yadis::discover
195
 * method.
196
 *
197
 * @package Yadis
198
 */
199
class Services_Yadis_XRDS {
200
 
201
    /**
202
     * Instantiate a Services_Yadis_XRDS object.  Requires an XPath
203
     * instance which has been used to parse a valid XRDS document.
204
     */
205
    function Services_Yadis_XRDS(&$xmlParser, &$xrdNodes)
206
    {
207
        $this->parser =& $xmlParser;
208
        $this->xrdNode = $xrdNodes[count($xrdNodes) - 1];
209
        $this->allXrdNodes =& $xrdNodes;
210
        $this->serviceList = array();
211
        $this->_parse();
212
    }
213
 
214
    /**
215
     * Parse an XML string (XRDS document) and return either a
216
     * Services_Yadis_XRDS object or null, depending on whether the
217
     * XRDS XML is valid.
218
     *
219
     * @param string $xml_string An XRDS XML string.
220
     * @return mixed $xrds An instance of Services_Yadis_XRDS or null,
221
     * depending on the validity of $xml_string
222
     */
223
    function &parseXRDS($xml_string, $extra_ns_map = null)
224
    {
225
        $_null = null;
226
 
227
        if (!$xml_string) {
228
            return $_null;
229
        }
230
 
231
        $parser = Services_Yadis_getXMLParser();
232
 
233
        $ns_map = Services_Yadis_getNSMap();
234
 
235
        if ($extra_ns_map && is_array($extra_ns_map)) {
236
            $ns_map = array_merge($ns_map, $extra_ns_map);
237
        }
238
 
239
        if (!($parser && $parser->init($xml_string, $ns_map))) {
240
            return $_null;
241
        }
242
 
243
        // Try to get root element.
244
        $root = $parser->evalXPath('/xrds:XRDS[1]');
245
        if (!$root) {
246
            return $_null;
247
        }
248
 
249
        if (is_array($root)) {
250
            $root = $root[0];
251
        }
252
 
253
        $attrs = $parser->attributes($root);
254
 
255
        if (array_key_exists('xmlns:xrd', $attrs) &&
256
            $attrs['xmlns:xrd'] != 'xri://$xrd*($v*2.0)') {
257
            return $_null;
258
        } else if (array_key_exists('xmlns', $attrs) &&
259
                   preg_match('/xri/', $attrs['xmlns']) &&
260
                   $attrs['xmlns'] != 'xri://$xrd*($v*2.0)') {
261
            return $_null;
262
        }
263
 
264
        // Get the last XRD node.
265
        $xrd_nodes = $parser->evalXPath('/xrds:XRDS[1]/xrd:XRD');
266
 
267
        if (!$xrd_nodes) {
268
            return $_null;
269
        }
270
 
271
        $xrds = new Services_Yadis_XRDS($parser, $xrd_nodes);
272
        return $xrds;
273
    }
274
 
275
    /**
276
     * @access private
277
     */
278
    function _addService($priority, $service)
279
    {
280
        $priority = intval($priority);
281
 
282
        if (!array_key_exists($priority, $this->serviceList)) {
283
            $this->serviceList[$priority] = array();
284
        }
285
 
286
        $this->serviceList[$priority][] = $service;
287
    }
288
 
289
    /**
290
     * Creates the service list using nodes from the XRDS XML
291
     * document.
292
     *
293
     * @access private
294
     */
295
    function _parse()
296
    {
297
        $this->serviceList = array();
298
 
299
        $services = $this->parser->evalXPath('xrd:Service', $this->xrdNode);
300
 
301
        foreach ($services as $node) {
302
            $s =& new Services_Yadis_Service();
303
            $s->element = $node;
304
            $s->parser =& $this->parser;
305
 
306
            $priority = $s->getPriority();
307
 
308
            if ($priority === null) {
309
                $priority = SERVICES_YADIS_MAX_PRIORITY;
310
            }
311
 
312
            $this->_addService($priority, $s);
313
        }
314
    }
315
 
316
    /**
317
     * Returns a list of service objects which correspond to <Service>
318
     * elements in the XRDS XML document for this object.
319
     *
320
     * Optionally, an array of filter callbacks may be given to limit
321
     * the list of returned service objects.  Furthermore, the default
322
     * mode is to return all service objects which match ANY of the
323
     * specified filters, but $filter_mode may be
324
     * SERVICES_YADIS_MATCH_ALL if you want to be sure that the
325
     * returned services match all the given filters.  See {@link
326
     * Services_Yadis_Yadis} for detailed usage information on filter
327
     * functions.
328
     *
329
     * @param mixed $filters An array of callbacks to filter the
330
     * returned services, or null if all services are to be returned.
331
     * @param integer $filter_mode SERVICES_YADIS_MATCH_ALL or
332
     * SERVICES_YADIS_MATCH_ANY, depending on whether the returned
333
     * services should match ALL or ANY of the specified filters,
334
     * respectively.
335
     * @return mixed $services An array of {@link
336
     * Services_Yadis_Service} objects if $filter_mode is a valid
337
     * mode; null if $filter_mode is an invalid mode (i.e., not
338
     * SERVICES_YADIS_MATCH_ANY or SERVICES_YADIS_MATCH_ALL).
339
     */
340
    function services($filters = null,
341
                      $filter_mode = SERVICES_YADIS_MATCH_ANY)
342
    {
343
 
344
        $pri_keys = array_keys($this->serviceList);
345
        sort($pri_keys, SORT_NUMERIC);
346
 
347
        // If no filters are specified, return the entire service
348
        // list, ordered by priority.
349
        if (!$filters ||
350
            (!is_array($filters))) {
351
 
352
            $result = array();
353
            foreach ($pri_keys as $pri) {
354
                $result = array_merge($result, $this->serviceList[$pri]);
355
            }
356
 
357
            return $result;
358
        }
359
 
360
        // If a bad filter mode is specified, return null.
361
        if (!in_array($filter_mode, array(SERVICES_YADIS_MATCH_ANY,
362
                                          SERVICES_YADIS_MATCH_ALL))) {
363
            return null;
364
        }
365
 
366
        // Otherwise, use the callbacks in the filter list to
367
        // determine which services are returned.
368
        $filtered = array();
369
 
370
        foreach ($pri_keys as $priority_value) {
371
            $service_obj_list = $this->serviceList[$priority_value];
372
 
373
            foreach ($service_obj_list as $service) {
374
 
375
                $matches = 0;
376
 
377
                foreach ($filters as $filter) {
378
                    if (call_user_func_array($filter, array($service))) {
379
                        $matches++;
380
 
381
                        if ($filter_mode == SERVICES_YADIS_MATCH_ANY) {
382
                            $pri = $service->getPriority();
383
                            if ($pri === null) {
384
                                $pri = SERVICES_YADIS_MAX_PRIORITY;
385
                            }
386
 
387
                            if (!array_key_exists($pri, $filtered)) {
388
                                $filtered[$pri] = array();
389
                            }
390
 
391
                            $filtered[$pri][] = $service;
392
                            break;
393
                        }
394
                    }
395
                }
396
 
397
                if (($filter_mode == SERVICES_YADIS_MATCH_ALL) &&
398
                    ($matches == count($filters))) {
399
 
400
                    $pri = $service->getPriority();
401
                    if ($pri === null) {
402
                        $pri = SERVICES_YADIS_MAX_PRIORITY;
403
                    }
404
 
405
                    if (!array_key_exists($pri, $filtered)) {
406
                        $filtered[$pri] = array();
407
                    }
408
                    $filtered[$pri][] = $service;
409
                }
410
            }
411
        }
412
 
413
        $pri_keys = array_keys($filtered);
414
        sort($pri_keys, SORT_NUMERIC);
415
 
416
        $result = array();
417
        foreach ($pri_keys as $pri) {
418
            $result = array_merge($result, $filtered[$pri]);
419
        }
420
 
421
        return $result;
422
    }
423
}
424
 
425
?>