xrds.lib.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Parsing library for OpenID
  5. * @package chamilo.auth.openid
  6. */
  7. /**
  8. * Code
  9. */
  10. // Global variables to track parsing state
  11. $xrds_open_elements = array();
  12. $xrds_services = array();
  13. $xrds_current_service = array();
  14. /**
  15. * Main entry point for parsing XRDS documents
  16. */
  17. function xrds_parse($xml) {
  18. global $xrds_services;
  19. $parser = xml_parser_create_ns();
  20. xml_set_element_handler($parser, '_xrds_element_start', '_xrds_element_end');
  21. xml_set_character_data_handler($parser, '_xrds_cdata');
  22. xml_parse($parser, $xml);
  23. xml_parser_free($parser);
  24. return $xrds_services;
  25. }
  26. /**
  27. * Parser callback functions
  28. */
  29. function _xrds_element_start(&$parser, $name, $attribs) {
  30. global $xrds_open_elements;
  31. $xrds_open_elements[] = _xrds_strip_namespace($name);
  32. }
  33. function _xrds_element_end(&$parser, $name) {
  34. global $xrds_open_elements, $xrds_services, $xrds_current_service;
  35. $name = _xrds_strip_namespace($name);
  36. if ($name == 'SERVICE') {
  37. if (in_array(OPENID_NS_2_0 .'/signon', $xrds_current_service['types']) ||
  38. in_array(OPENID_NS_2_0 .'/server', $xrds_current_service['types'])) {
  39. $xrds_current_service['version'] = 2;
  40. }
  41. elseif (in_array(OPENID_NS_1_1, $xrds_current_service['types']) ||
  42. in_array(OPENID_NS_1_0, $xrds_current_service['types'])) {
  43. $xrds_current_service['version'] = 1;
  44. }
  45. if (!empty($xrds_current_service['version'])) {
  46. $xrds_services[] = $xrds_current_service;
  47. }
  48. $xrds_current_service = array();
  49. }
  50. array_pop($xrds_open_elements);
  51. }
  52. function _xrds_cdata(&$parser, $data) {
  53. global $xrds_open_elements, $xrds_services, $xrds_current_service;
  54. $path = strtoupper(implode('/', $xrds_open_elements));
  55. switch ($path) {
  56. case 'XRDS/XRD/SERVICE/TYPE':
  57. $xrds_current_service['types'][] = $data;
  58. break;
  59. case 'XRDS/XRD/SERVICE/URI':
  60. $xrds_current_service['uri'] = $data;
  61. break;
  62. case 'XRDS/XRD/SERVICE/DELEGATE':
  63. $xrds_current_service['delegate'] = $data;
  64. break;
  65. }
  66. }
  67. function _xrds_strip_namespace($name) {
  68. // Strip namespacing.
  69. $pos = strrpos($name, ':');
  70. if ($pos !== FALSE) {
  71. $name = substr($name, $pos + 1, strlen($name));
  72. }
  73. return $name;
  74. }