Licence and jqmobile beta 3 upgrade
[busui.git] / lib / openid-php / Auth / Yadis / XRDS.php
blob:a/lib/openid-php/Auth/Yadis/XRDS.php -> blob:b/lib/openid-php/Auth/Yadis/XRDS.php
<?php <?php
   
/** /**
* This module contains the XRDS parsing code. * This module contains the XRDS parsing code.
* *
* PHP versions 4 and 5 * PHP versions 4 and 5
* *
* LICENSE: See the COPYING file included in this distribution. * LICENSE: See the COPYING file included in this distribution.
* *
* @package OpenID * @package OpenID
* @author JanRain, Inc. <openid@janrain.com> * @author JanRain, Inc. <openid@janrain.com>
* @copyright 2005-2008 Janrain, Inc. * @copyright 2005-2008 Janrain, Inc.
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
*/ */
   
/** /**
* Require the XPath implementation. * Require the XPath implementation.
*/ */
require_once 'Auth/Yadis/XML.php'; require_once 'Auth/Yadis/XML.php';
   
/** /**
* This match mode means a given service must match ALL filters passed * This match mode means a given service must match ALL filters passed
* to the Auth_Yadis_XRDS::services() call. * to the Auth_Yadis_XRDS::services() call.
*/ */
define('SERVICES_YADIS_MATCH_ALL', 101); define('SERVICES_YADIS_MATCH_ALL', 101);
   
/** /**
* This match mode means a given service must match ANY filters (at * This match mode means a given service must match ANY filters (at
* least one) passed to the Auth_Yadis_XRDS::services() call. * least one) passed to the Auth_Yadis_XRDS::services() call.
*/ */
define('SERVICES_YADIS_MATCH_ANY', 102); define('SERVICES_YADIS_MATCH_ANY', 102);
   
/** /**
* The priority value used for service elements with no priority * The priority value used for service elements with no priority
* specified. * specified.
*/ */
define('SERVICES_YADIS_MAX_PRIORITY', pow(2, 30)); define('SERVICES_YADIS_MAX_PRIORITY', pow(2, 30));
   
/** /**
* XRD XML namespace * XRD XML namespace
*/ */
define('Auth_Yadis_XMLNS_XRD_2_0', 'xri://$xrd*($v*2.0)'); define('Auth_Yadis_XMLNS_XRD_2_0', 'xri://$xrd*($v*2.0)');
   
/** /**
* XRDS XML namespace * XRDS XML namespace
*/ */
define('Auth_Yadis_XMLNS_XRDS', 'xri://$xrds'); define('Auth_Yadis_XMLNS_XRDS', 'xri://$xrds');
   
function Auth_Yadis_getNSMap() function Auth_Yadis_getNSMap()
{ {
return array('xrds' => Auth_Yadis_XMLNS_XRDS, return array('xrds' => Auth_Yadis_XMLNS_XRDS,
'xrd' => Auth_Yadis_XMLNS_XRD_2_0); 'xrd' => Auth_Yadis_XMLNS_XRD_2_0);
} }
   
/** /**
* @access private * @access private
*/ */
function Auth_Yadis_array_scramble($arr) function Auth_Yadis_array_scramble($arr)
{ {
$result = array(); $result = array();
   
while (count($arr)) { while (count($arr)) {
$index = array_rand($arr, 1); $index = array_rand($arr, 1);
$result[] = $arr[$index]; $result[] = $arr[$index];
unset($arr[$index]); unset($arr[$index]);
} }
   
return $result; return $result;
} }
   
/** /**
* This class represents a <Service> element in an XRDS document. * This class represents a <Service> element in an XRDS document.
* Objects of this type are returned by * Objects of this type are returned by
* Auth_Yadis_XRDS::services() and * Auth_Yadis_XRDS::services() and
* Auth_Yadis_Yadis::services(). Each object corresponds directly * Auth_Yadis_Yadis::services(). Each object corresponds directly
* to a <Service> element in the XRDS and supplies a * to a <Service> element in the XRDS and supplies a
* getElements($name) method which you should use to inspect the * getElements($name) method which you should use to inspect the
* element's contents. See {@link Auth_Yadis_Yadis} for more * element's contents. See {@link Auth_Yadis_Yadis} for more
* information on the role this class plays in Yadis discovery. * information on the role this class plays in Yadis discovery.
* *
* @package OpenID * @package OpenID
*/ */
class Auth_Yadis_Service { class Auth_Yadis_Service {
   
/** /**
* Creates an empty service object. * Creates an empty service object.
*/ */
function Auth_Yadis_Service() function Auth_Yadis_Service()
{ {
$this->element = null; $this->element = null;
$this->parser = null; $this->parser = null;
} }
   
/** /**
* Return the URIs in the "Type" elements, if any, of this Service * Return the URIs in the "Type" elements, if any, of this Service
* element. * element.
* *
* @return array $type_uris An array of Type URI strings. * @return array $type_uris An array of Type URI strings.
*/ */
function getTypes() function getTypes()
{ {
$t = array(); $t = array();
foreach ($this->getElements('xrd:Type') as $elem) { foreach ($this->getElements('xrd:Type') as $elem) {
$c = $this->parser->content($elem); $c = $this->parser->content($elem);
if ($c) { if ($c) {
$t[] = $c; $t[] = $c;
} }
} }
return $t; return $t;
} }
   
function matchTypes($type_uris) function matchTypes($type_uris)
{ {
$result = array(); $result = array();
   
foreach ($this->getTypes() as $typ) { foreach ($this->getTypes() as $typ) {
if (in_array($typ, $type_uris)) { if (in_array($typ, $type_uris)) {
$result[] = $typ; $result[] = $typ;
} }
} }
   
return $result; return $result;
} }
   
/** /**
* Return the URIs in the "URI" elements, if any, of this Service * Return the URIs in the "URI" elements, if any, of this Service
* element. The URIs are returned sorted in priority order. * element. The URIs are returned sorted in priority order.
* *
* @return array $uris An array of URI strings. * @return array $uris An array of URI strings.
*/ */
function getURIs() function getURIs()
{ {
$uris = array(); $uris = array();
$last = array(); $last = array();
   
foreach ($this->getElements('xrd:URI') as $elem) { foreach ($this->getElements('xrd:URI') as $elem) {
$uri_string = $this->parser->content($elem); $uri_string = $this->parser->content($elem);
$attrs = $this->parser->attributes($elem); $attrs = $this->parser->attributes($elem);
if ($attrs && if ($attrs &&
array_key_exists('priority', $attrs)) { array_key_exists('priority', $attrs)) {
$priority = intval($attrs['priority']); $priority = intval($attrs['priority']);
if (!array_key_exists($priority, $uris)) { if (!array_key_exists($priority, $uris)) {
$uris[$priority] = array(); $uris[$priority] = array();
} }
   
$uris[$priority][] = $uri_string; $uris[$priority][] = $uri_string;
} else { } else {
$last[] = $uri_string; $last[] = $uri_string;
} }
} }
   
$keys = array_keys($uris); $keys = array_keys($uris);
sort($keys); sort($keys);
   
// Rebuild array of URIs. // Rebuild array of URIs.
$result = array(); $result = array();
foreach ($keys as $k) { foreach ($keys as $k) {
$new_uris = Auth_Yadis_array_scramble($uris[$k]); $new_uris = Auth_Yadis_array_scramble($uris[$k]);
$result = array_merge($result, $new_uris); $result = array_merge($result, $new_uris);
} }
   
$result = array_merge($result, $result = array_merge($result,
Auth_Yadis_array_scramble($last)); Auth_Yadis_array_scramble($last));
   
return $result; return $result;
} }
   
/** /**
* Returns the "priority" attribute value of this <Service> * Returns the "priority" attribute value of this <Service>
* element, if the attribute is present. Returns null if not. * element, if the attribute is present. Returns null if not.
* *
* @return mixed $result Null or integer, depending on whether * @return mixed $result Null or integer, depending on whether
* this Service element has a 'priority' attribute. * this Service element has a 'priority' attribute.
*/ */
function getPriority() function getPriority()
{ {
$attributes = $this->parser->attributes($this->element); $attributes = $this->parser->attributes($this->element);
   
if (array_key_exists('priority', $attributes)) { if (array_key_exists('priority', $attributes)) {
return intval($attributes['priority']); return intval($attributes['priority']);
} }
   
return null; return null;
} }
   
/** /**
* Used to get XML elements from this object's <Service> element. * Used to get XML elements from this object's <Service> element.
* *
* This is what you should use to get all custom information out * This is what you should use to get all custom information out
* of this element. This is used by service filter functions to * of this element. This is used by service filter functions to
* determine whether a service element contains specific tags, * determine whether a service element contains specific tags,
* etc. NOTE: this only considers elements which are direct * etc. NOTE: this only considers elements which are direct
* children of the <Service> element for this object. * children of the <Service> element for this object.
* *
* @param string $name The name of the element to look for * @param string $name The name of the element to look for
* @return array $list An array of elements with the specified * @return array $list An array of elements with the specified
* name which are direct children of the <Service> element. The * name which are direct children of the <Service> element. The
* nodes returned by this function can be passed to $this->parser * nodes returned by this function can be passed to $this->parser
* methods (see {@link Auth_Yadis_XMLParser}). * methods (see {@link Auth_Yadis_XMLParser}).
*/ */
function getElements($name) function getElements($name)
{ {
return $this->parser->evalXPath($name, $this->element); return $this->parser->evalXPath($name, $this->element);
} }
} }
   
/* /*
* Return the expiration date of this XRD element, or None if no * Return the expiration date of this XRD element, or None if no
* expiration was specified. * expiration was specified.
* *
* @param $default The value to use as the expiration if no expiration * @param $default The value to use as the expiration if no expiration
* was specified in the XRD. * was specified in the XRD.
*/ */
function Auth_Yadis_getXRDExpiration($xrd_element, $default=null) function Auth_Yadis_getXRDExpiration($xrd_element, $default=null)
{ {
$expires_element = $xrd_element->$parser->evalXPath('/xrd:Expires'); $expires_element = $xrd_element->$parser->evalXPath('/xrd:Expires');
if ($expires_element === null) { if ($expires_element === null) {
return $default; return $default;
} else { } else {
$expires_string = $expires_element->text; $expires_string = $expires_element->text;
   
// Will raise ValueError if the string is not the expected // Will raise ValueError if the string is not the expected
// format // format
$t = strptime($expires_string, "%Y-%m-%dT%H:%M:%SZ"); $t = strptime($expires_string, "%Y-%m-%dT%H:%M:%SZ");
   
if ($t === false) { if ($t === false) {
return false; return false;
} }
   
// [int $hour [, int $minute [, int $second [, // [int $hour [, int $minute [, int $second [,
// int $month [, int $day [, int $year ]]]]]] // int $month [, int $day [, int $year ]]]]]]
return mktime($t['tm_hour'], $t['tm_min'], $t['tm_sec'], return mktime($t['tm_hour'], $t['tm_min'], $t['tm_sec'],
$t['tm_mon'], $t['tm_day'], $t['tm_year']); $t['tm_mon'], $t['tm_day'], $t['tm_year']);
} }
} }
   
/** /**
* This class performs parsing of XRDS documents. * This class performs parsing of XRDS documents.
* *
* You should not instantiate this class directly; rather, call * You should not instantiate this class directly; rather, call
* parseXRDS statically: * parseXRDS statically:
* *
* <pre> $xrds = Auth_Yadis_XRDS::parseXRDS($xml_string);</pre> * <pre> $xrds = Auth_Yadis_XRDS::parseXRDS($xml_string);</pre>
* *
* If the XRDS can be parsed and is valid, an instance of * If the XRDS can be parsed and is valid, an instance of
* Auth_Yadis_XRDS will be returned. Otherwise, null will be * Auth_Yadis_XRDS will be returned. Otherwise, null will be
* returned. This class is used by the Auth_Yadis_Yadis::discover * returned. This class is used by the Auth_Yadis_Yadis::discover
* method. * method.
* *
* @package OpenID * @package OpenID
*/ */
class Auth_Yadis_XRDS { class Auth_Yadis_XRDS {
   
/** /**
* Instantiate a Auth_Yadis_XRDS object. Requires an XPath * Instantiate a Auth_Yadis_XRDS object. Requires an XPath
* instance which has been used to parse a valid XRDS document. * instance which has been used to parse a valid XRDS document.
*/ */
function Auth_Yadis_XRDS($xmlParser, $xrdNodes) function Auth_Yadis_XRDS($xmlParser, $xrdNodes)
{ {
$this->parser = $xmlParser; $this->parser = $xmlParser;
$this->xrdNode = $xrdNodes[count($xrdNodes) - 1]; $this->xrdNode = $xrdNodes[count($xrdNodes) - 1];
$this->allXrdNodes = $xrdNodes; $this->allXrdNodes = $xrdNodes;
$this->serviceList = array(); $this->serviceList = array();
$this->_parse(); $this->_parse();
} }
   
/** /**
* Parse an XML string (XRDS document) and return either a * Parse an XML string (XRDS document) and return either a
* Auth_Yadis_XRDS object or null, depending on whether the * Auth_Yadis_XRDS object or null, depending on whether the
* XRDS XML is valid. * XRDS XML is valid.
* *
* @param string $xml_string An XRDS XML string. * @param string $xml_string An XRDS XML string.
* @return mixed $xrds An instance of Auth_Yadis_XRDS or null, * @return mixed $xrds An instance of Auth_Yadis_XRDS or null,
* depending on the validity of $xml_string * depending on the validity of $xml_string
*/ */
static function parseXRDS($xml_string, $extra_ns_map = null) static function parseXRDS($xml_string, $extra_ns_map = null)
{ {
$_null = null; $_null = null;
   
if (!$xml_string) { if (!$xml_string) {
return $_null; return $_null;
} }
   
$parser = Auth_Yadis_getXMLParser(); $parser = Auth_Yadis_getXMLParser();
   
$ns_map = Auth_Yadis_getNSMap(); $ns_map = Auth_Yadis_getNSMap();
   
if ($extra_ns_map && is_array($extra_ns_map)) { if ($extra_ns_map && is_array($extra_ns_map)) {
$ns_map = array_merge($ns_map, $extra_ns_map); $ns_map = array_merge($ns_map, $extra_ns_map);
} }
   
if (!($parser && $parser->init($xml_string, $ns_map))) { if (!($parser && $parser->init($xml_string, $ns_map))) {
return $_null; return $_null;
} }
   
// Try to get root element. // Try to get root element.
$root = $parser->evalXPath('/xrds:XRDS[1]'); $root = $parser->evalXPath('/xrds:XRDS[1]');
if (!$root) { if (!$root) {
return $_null; return $_null;
} }
   
if (is_array($root)) { if (is_array($root)) {
$root = $root[0]; $root = $root[0];
} }
   
$attrs = $parser->attributes($root); $attrs = $parser->attributes($root);
   
if (array_key_exists('xmlns:xrd', $attrs) && if (array_key_exists('xmlns:xrd', $attrs) &&
$attrs['xmlns:xrd'] != Auth_Yadis_XMLNS_XRDS) { $attrs['xmlns:xrd'] != Auth_Yadis_XMLNS_XRDS) {
return $_null; return $_null;
} else if (array_key_exists('xmlns', $attrs) && } else if (array_key_exists('xmlns', $attrs) &&
preg_match('/xri/', $attrs['xmlns']) && preg_match('/xri/', $attrs['xmlns']) &&
$attrs['xmlns'] != Auth_Yadis_XMLNS_XRD_2_0) { $attrs['xmlns'] != Auth_Yadis_XMLNS_XRD_2_0) {
return $_null; return $_null;
} }
   
// Get the last XRD node. // Get the last XRD node.
$xrd_nodes = $parser->evalXPath('/xrds:XRDS[1]/xrd:XRD'); $xrd_nodes = $parser->evalXPath('/xrds:XRDS[1]/xrd:XRD');
   
if (!$xrd_nodes) { if (!$xrd_nodes) {
return $_null; return $_null;
} }
   
$xrds = new Auth_Yadis_XRDS($parser, $xrd_nodes); $xrds = new Auth_Yadis_XRDS($parser, $xrd_nodes);
return $xrds; return $xrds;
} }
   
/** /**
* @access private * @access private
*/ */
function _addService($priority, $service) function _addService($priority, $service)
{ {
$priority = intval($priority); $priority = intval($priority);
   
if (!array_key_exists($priority, $this->serviceList)) { if (!array_key_exists($priority, $this->serviceList)) {
$this->serviceList[$priority] = array(); $this->serviceList[$priority] = array();
} }
   
$this->serviceList[$priority][] = $service; $this->serviceList[$priority][] = $service;
} }
   
/** /**
* Creates the service list using nodes from the XRDS XML * Creates the service list using nodes from the XRDS XML
* document. * document.
* *
* @access private * @access private
*/ */
function _parse() function _parse()
{ {
$this->serviceList = array(); $this->serviceList = array();
   
$services = $this->parser->evalXPath('xrd:Service', $this->xrdNode); $services = $this->parser->evalXPath('xrd:Service', $this->xrdNode);
   
foreach ($services as $node) { foreach ($services as $node) {
$s = new Auth_Yadis_Service(); $s = new Auth_Yadis_Service();
$s->element = $node; $s->element = $node;
$s->parser = $this->parser; $s->parser = $this->parser;
   
$priority = $s->getPriority(); $priority = $s->getPriority();
   
if ($priority === null) { if ($priority === null) {
$priority = SERVICES_YADIS_MAX_PRIORITY; $priority = SERVICES_YADIS_MAX_PRIORITY;
} }
   
$this->_addService($priority, $s); $this->_addService($priority, $s);
} }
} }
   
/** /**
* Returns a list of service objects which correspond to <Service> * Returns a list of service objects which correspond to <Service>
* elements in the XRDS XML document for this object. * elements in the XRDS XML document for this object.
* *
* Optionally, an array of filter callbacks may be given to limit * Optionally, an array of filter callbacks may be given to limit
* the list of returned service objects. Furthermore, the default * the list of returned service objects. Furthermore, the default
* mode is to return all service objects which match ANY of the * mode is to return all service objects which match ANY of the
* specified filters, but $filter_mode may be * specified filters, but $filter_mode may be
* SERVICES_YADIS_MATCH_ALL if you want to be sure that the * SERVICES_YADIS_MATCH_ALL if you want to be sure that the
* returned services match all the given filters. See {@link * returned services match all the given filters. See {@link
* Auth_Yadis_Yadis} for detailed usage information on filter * Auth_Yadis_Yadis} for detailed usage information on filter
* functions. * functions.
* *
* @param mixed $filters An array of callbacks to filter the * @param mixed $filters An array of callbacks to filter the
* returned services, or null if all services are to be returned. * returned services, or null if all services are to be returned.