PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/hybrid/Auth/Yadis/XRDS.php

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