123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- <?php
- require_once 'Auth/Yadis/XML.php';
- define('SERVICES_YADIS_MATCH_ALL', 101);
- define('SERVICES_YADIS_MATCH_ANY', 102);
- define('SERVICES_YADIS_MAX_PRIORITY', pow(2, 30));
- define('Auth_Yadis_XMLNS_XRD_2_0', 'xri://$xrd*($v*2.0)');
- define('Auth_Yadis_XMLNS_XRDS', 'xri://$xrds');
- function Auth_Yadis_getNSMap()
- {
- return array('xrds' => Auth_Yadis_XMLNS_XRDS,
- 'xrd' => Auth_Yadis_XMLNS_XRD_2_0);
- }
- function Auth_Yadis_array_scramble($arr)
- {
- $result = array();
- while (count($arr)) {
- $index = array_rand($arr, 1);
- $result[] = $arr[$index];
- unset($arr[$index]);
- }
- return $result;
- }
- class Auth_Yadis_Service {
-
- function Auth_Yadis_Service()
- {
- $this->element = null;
- $this->parser = null;
- }
-
- function getTypes()
- {
- $t = array();
- foreach ($this->getElements('xrd:Type') as $elem) {
- $c = $this->parser->content($elem);
- if ($c) {
- $t[] = $c;
- }
- }
- return $t;
- }
- function matchTypes($type_uris)
- {
- $result = array();
- foreach ($this->getTypes() as $typ) {
- if (in_array($typ, $type_uris)) {
- $result[] = $typ;
- }
- }
- return $result;
- }
-
- function getURIs()
- {
- $uris = array();
- $last = array();
- foreach ($this->getElements('xrd:URI') as $elem) {
- $uri_string = $this->parser->content($elem);
- $attrs = $this->parser->attributes($elem);
- if ($attrs &&
- array_key_exists('priority', $attrs)) {
- $priority = intval($attrs['priority']);
- if (!array_key_exists($priority, $uris)) {
- $uris[$priority] = array();
- }
- $uris[$priority][] = $uri_string;
- } else {
- $last[] = $uri_string;
- }
- }
- $keys = array_keys($uris);
- sort($keys);
-
- $result = array();
- foreach ($keys as $k) {
- $new_uris = Auth_Yadis_array_scramble($uris[$k]);
- $result = array_merge($result, $new_uris);
- }
- $result = array_merge($result,
- Auth_Yadis_array_scramble($last));
- return $result;
- }
-
- function getPriority()
- {
- $attributes = $this->parser->attributes($this->element);
- if (array_key_exists('priority', $attributes)) {
- return intval($attributes['priority']);
- }
- return null;
- }
-
- function getElements($name)
- {
- return $this->parser->evalXPath($name, $this->element);
- }
- }
- function Auth_Yadis_getXRDExpiration($xrd_element, $default=null)
- {
- $expires_element = $xrd_element->$parser->evalXPath('/xrd:Expires');
- if ($expires_element === null) {
- return $default;
- } else {
- $expires_string = $expires_element->text;
-
-
- $t = strptime($expires_string, "%Y-%m-%dT%H:%M:%SZ");
- if ($t === false) {
- return false;
- }
-
-
- return mktime($t['tm_hour'], $t['tm_min'], $t['tm_sec'],
- $t['tm_mon'], $t['tm_day'], $t['tm_year']);
- }
- }
- class Auth_Yadis_XRDS {
-
- function Auth_Yadis_XRDS($xmlParser, $xrdNodes)
- {
- $this->parser = $xmlParser;
- $this->xrdNode = $xrdNodes[count($xrdNodes) - 1];
- $this->allXrdNodes = $xrdNodes;
- $this->serviceList = array();
- $this->_parse();
- }
-
- static function parseXRDS($xml_string, $extra_ns_map = null)
- {
- $_null = null;
- if (!$xml_string) {
- return $_null;
- }
- $parser = Auth_Yadis_getXMLParser();
- $ns_map = Auth_Yadis_getNSMap();
- if ($extra_ns_map && is_array($extra_ns_map)) {
- $ns_map = array_merge($ns_map, $extra_ns_map);
- }
- if (!($parser && $parser->init($xml_string, $ns_map))) {
- return $_null;
- }
-
- $root = $parser->evalXPath('/xrds:XRDS[1]');
- if (!$root) {
- return $_null;
- }
- if (is_array($root)) {
- $root = $root[0];
- }
- $attrs = $parser->attributes($root);
- if (array_key_exists('xmlns:xrd', $attrs) &&
- $attrs['xmlns:xrd'] != Auth_Yadis_XMLNS_XRDS) {
- return $_null;
- } else if (array_key_exists('xmlns', $attrs) &&
- preg_match('/xri/', $attrs['xmlns']) &&
- $attrs['xmlns'] != Auth_Yadis_XMLNS_XRD_2_0) {
- return $_null;
- }
-
- $xrd_nodes = $parser->evalXPath('/xrds:XRDS[1]/xrd:XRD');
- if (!$xrd_nodes) {
- return $_null;
- }
- $xrds = new Auth_Yadis_XRDS($parser, $xrd_nodes);
- return $xrds;
- }
-
- function _addService($priority, $service)
- {
- $priority = intval($priority);
- if (!array_key_exists($priority, $this->serviceList)) {
- $this->serviceList[$priority] = array();
- }
- $this->serviceList[$priority][] = $service;
- }
-
- function _parse()
- {
- $this->serviceList = array();
- $services = $this->parser->evalXPath('xrd:Service', $this->xrdNode);
- foreach ($services as $node) {
- $s = new Auth_Yadis_Service();
- $s->element = $node;
- $s->parser = $this->parser;
- $priority = $s->getPriority();
- if ($priority === null) {
- $priority = SERVICES_YADIS_MAX_PRIORITY;
- }
- $this->_addService($priority, $s);
- }
- }
-
- function services($filters = null,
- $filter_mode = SERVICES_YADIS_MATCH_ANY)
- {
- $pri_keys = array_keys($this->serviceList);
- sort($pri_keys, SORT_NUMERIC);
-
-
- if (!$filters ||
- (!is_array($filters))) {
- $result = array();
- foreach ($pri_keys as $pri) {
- $result = array_merge($result, $this->serviceList[$pri]);
- }
- return $result;
- }
-
- if (!in_array($filter_mode, array(SERVICES_YADIS_MATCH_ANY,
- SERVICES_YADIS_MATCH_ALL))) {
- return null;
- }
-
-
- $filtered = array();
- foreach ($pri_keys as $priority_value) {
- $service_obj_list = $this->serviceList[$priority_value];
- foreach ($service_obj_list as $service) {
- $matches = 0;
- foreach ($filters as $filter) {
- if (call_user_func_array($filter, array($service))) {
- $matches++;
- if ($filter_mode == SERVICES_YADIS_MATCH_ANY) {
- $pri = $service->getPriority();
- if ($pri === null) {
- $pri = SERVICES_YADIS_MAX_PRIORITY;
- }
- if (!array_key_exists($pri, $filtered)) {
- $filtered[$pri] = array();
- }
- $filtered[$pri][] = $service;
- break;
- }
- }
- }
- if (($filter_mode == SERVICES_YADIS_MATCH_ALL) &&
- ($matches == count($filters))) {
- $pri = $service->getPriority();
- if ($pri === null) {
- $pri = SERVICES_YADIS_MAX_PRIORITY;
- }
- if (!array_key_exists($pri, $filtered)) {
- $filtered[$pri] = array();
- }
- $filtered[$pri][] = $service;
- }
- }
- }
- $pri_keys = array_keys($filtered);
- sort($pri_keys, SORT_NUMERIC);
- $result = array();
- foreach ($pri_keys as $pri) {
- $result = array_merge($result, $filtered[$pri]);
- }
- return $result;
- }
- }
|