Source code for file /openid/Services/Yadis/XRDS.php
Documentation is available at XRDS.php
* This module contains the XRDS parsing code.
* LICENSE: See the COPYING file included in this distribution.
* @copyright 2005 Janrain, Inc.
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* Require the XPath implementation.
require_once 'Services/Yadis/XML.php';
* This match mode means a given service must match ALL filters passed
* to the Services_Yadis_XRDS::services() call.
define('SERVICES_YADIS_MATCH_ALL', 101);
* This match mode means a given service must match ANY filters (at
* least one) passed to the Services_Yadis_XRDS::services() call.
define('SERVICES_YADIS_MATCH_ANY', 102);
global $_Services_Yadis_ns_map;
$_Services_Yadis_ns_map =
array('xrds' =>
'xri://$xrds',
'xrd' =>
'xri://$xrd*($v*2.0)');
define('SERVICES_YADIS_MAX_PRIORITY', pow(2, 30));
function Services_Yadis_array_scramble($arr)
$result[] =
$arr[$index];
* This class represents a <Service> element in an XRDS document.
* Objects of this type are returned by
* Services_Yadis_XRDS::services() and
* Services_Yadis_Yadis::services(). Each object corresponds directly
* to a <Service> element in the XRDS and supplies a
* getElements($name) method which you should use to inspect the
* element's contents. See {@link Services_Yadis_Yadis} for more
* information on the role this class plays in Yadis discovery.
* Creates an empty service object.
* Return the URIs in the "Type" elements, if any, of this Service
* @return array $type_uris An array of Type URI strings.
$c =
$this->parser->content($elem);
* Return the URIs in the "URI" elements, if any, of this Service
* element. The URIs are returned sorted in priority order.
* @return array $uris An array of URI strings.
$uri_string =
$this->parser->content($elem);
$attrs =
$this->parser->attributes($elem);
$priority =
intval($attrs['priority']);
$uris[$priority] =
array();
$uris[$priority][] =
$uri_string;
// Rebuild array of URIs.
$new_uris =
Services_Yadis_array_scramble($uris[$k]);
Services_Yadis_array_scramble($last));
* Returns the "priority" attribute value of this <Service>
* element, if the attribute is present. Returns null if not.
* @return mixed $result Null or integer, depending on whether
* this Service element has a 'priority' attribute.
$attributes =
$this->parser->attributes($this->element);
return intval($attributes['priority']);
* Used to get XML elements from this object's <Service> element.
* This is what you should use to get all custom information out
* of this element. This is used by service filter functions to
* determine whether a service element contains specific tags,
* etc. NOTE: this only considers elements which are direct
* children of the <Service> element for this object.
* @param string $name The name of the element to look for
* @return array $list An array of elements with the specified
* name which are direct children of the <Service> element. The
* nodes returned by this function can be passed to $this->parser
* methods (see {@link Services_Yadis_XMLParser}).
return $this->parser->evalXPath($name, $this->element);
* This class performs parsing of XRDS documents.
* You should not instantiate this class directly; rather, call
* <pre> $xrds = Services_Yadis_XRDS::parseXRDS($xml_string);</pre>
* If the XRDS can be parsed and is valid, an instance of
* Services_Yadis_XRDS will be returned. Otherwise, null will be
* returned. This class is used by the Services_Yadis_Yadis::discover
* Instantiate a Services_Yadis_XRDS object. Requires an XPath
* instance which has been used to parse a valid XRDS document.
$this->parser =
& $xmlParser;
$this->xrdNode =
$xrdNodes[count($xrdNodes) -
1];
$this->allXrdNodes =
& $xrdNodes;
$this->serviceList =
array();
* Parse an XML string (XRDS document) and return either a
* Services_Yadis_XRDS object or null, depending on whether the
* @param string $xml_string An XRDS XML string.
* @return mixed $xrds An instance of Services_Yadis_XRDS or null,
* depending on the validity of $xml_string
function parseXRDS($xml_string, $extra_ns_map =
null)
global $_Services_Yadis_ns_map;
$ns_map =
$_Services_Yadis_ns_map;
if ($extra_ns_map &&
is_array($extra_ns_map)) {
if (!($parser &&
$parser->init($xml_string, $ns_map))) {
// Try to get root element.
$root =
$parser->evalXPath('/xrds:XRDS[1]');
$attrs =
$parser->attributes($root);
$attrs['xmlns:xrd'] !=
'xri://$xrd*($v*2.0)') {
$attrs['xmlns'] !=
'xri://$xrd*($v*2.0)') {
// Get the last XRD node.
$xrd_nodes =
$parser->evalXPath('/xrds:XRDS[1]/xrd:XRD');
function _addService($priority, $service)
$priority =
intval($priority);
$this->serviceList[$priority] =
array();
$this->serviceList[$priority][] =
$service;
* Creates the service list using nodes from the XRDS XML
$this->serviceList =
array();
$services =
$this->parser->evalXPath('xrd:Service', $this->xrdNode);
foreach ($services as $node) {
$s->parser =
& $this->parser;
$priority =
$s->getPriority();
if ($priority ===
null) {
$this->_addService($priority, $s);
* Returns a list of service objects which correspond to <Service>
* elements in the XRDS XML document for this object.
* Optionally, an array of filter callbacks may be given to limit
* the list of returned service objects. Furthermore, the default
* mode is to return all service objects which match ANY of the
* specified filters, but $filter_mode may be
* SERVICES_YADIS_MATCH_ALL if you want to be sure that the
* returned services match all the given filters. See {@link }
* Services_Yadis_Yadis} for detailed usage information on filter
* @param mixed $filters An array of callbacks to filter the
* returned services, or null if all services are to be returned.
* @param integer $filter_mode SERVICES_YADIS_MATCH_ALL or
* SERVICES_YADIS_MATCH_ANY, depending on whether the returned
* services should match ALL or ANY of the specified filters,
* @return mixed $services An array of {@link }
* Services_Yadis_Service} objects if $filter_mode is a valid
* mode; null if $filter_mode is an invalid mode (i.e., not
* SERVICES_YADIS_MATCH_ANY or SERVICES_YADIS_MATCH_ALL).
$filter_mode =
SERVICES_YADIS_MATCH_ANY)
sort($pri_keys, SORT_NUMERIC);
// If no filters are specified, return the entire service
// list, ordered by priority.
foreach ($pri_keys as $pri) {
$result =
array_merge($result, $this->serviceList[$pri]);
// If a bad filter mode is specified, return null.
// Otherwise, use the callbacks in the filter list to
// determine which services are returned.
foreach ($pri_keys as $priority_value) {
$service_obj_list =
$this->serviceList[$priority_value];
foreach ($service_obj_list as $service) {
foreach ($filters as $filter) {
$pri =
$service->getPriority();
$filtered[$pri] =
array();
$filtered[$pri][] =
$service;
($matches ==
count($filters))) {
$pri =
$service->getPriority();
$filtered[$pri] =
array();
$filtered[$pri][] =
$service;
sort($pri_keys, SORT_NUMERIC);
foreach ($pri_keys as $pri) {