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