|
1
|
|
|
<?php |
|
2
|
|
|
class pinp_xml { |
|
3
|
|
|
public static function _parser() { |
|
4
|
|
|
$context = pobject::getContext(); |
|
5
|
|
|
$me = $context['arCurrentObject']; |
|
6
|
|
|
$parser = new xml_parser($me); |
|
7
|
|
|
return $parser; |
|
8
|
|
|
} |
|
9
|
|
|
|
|
10
|
|
|
public static function _escape($text) { |
|
11
|
|
|
$search = array('&','"',"'",'<','>'); |
|
12
|
|
|
$replace = array('&','"',''','<','>'); |
|
13
|
|
|
return str_replace($search, $replace, $text); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public static function _unescape($text) { |
|
17
|
|
|
$search = array('"',''','<','>','&'); |
|
18
|
|
|
$replace = array('"',"'",'<','>','&'); |
|
19
|
|
|
return str_replace($search, $replace, $text); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
}; |
|
23
|
|
|
|
|
24
|
|
|
class xml_parser { |
|
25
|
|
|
protected $object; |
|
26
|
|
|
protected $tag_open_template; |
|
27
|
|
|
protected $tag_class_template; |
|
28
|
|
|
protected $tag_data_template; |
|
29
|
|
|
protected $ns = array(); |
|
30
|
|
|
public $elements; |
|
31
|
|
|
public $MULTI_TAGS; |
|
32
|
|
|
public $error; |
|
33
|
|
|
public $rss_items; |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
public function __construct($object) { |
|
37
|
|
|
$this->object = $object; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function _set_element_handler($tag_open, $tag_close) { |
|
41
|
|
|
$this->tag_open_template = $tag_open; |
|
42
|
|
|
$this->tag_close_template = $tag_close; |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function _set_character_data_handler($tag_data) { |
|
46
|
|
|
$this->tag_data_template = $tag_data; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function _parse($string) { |
|
50
|
|
|
$parser = xml_parser_create(); |
|
51
|
|
|
xml_set_object($parser, $this); |
|
52
|
|
|
xml_set_element_handler($parser, "call_tag_open", "call_tag_close"); |
|
53
|
|
|
xml_set_character_data_handler($parser, "call_tag_data"); |
|
54
|
|
View Code Duplication |
if (!xml_parse($parser, $string)) { |
|
55
|
|
|
$this->error = sprintf("XML error: %s at line %d", |
|
56
|
|
|
xml_error_string(xml_get_error_code($parser)), |
|
57
|
|
|
xml_get_current_line_number($parser)); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function _get_array($string, $MULTI_TAGS = array()) { |
|
62
|
|
|
$parser = xml_parser_create(); |
|
63
|
|
|
$this->elements = array(); |
|
64
|
|
|
$this->MULTI_TAGS = array(); |
|
65
|
|
|
foreach ($MULTI_TAGS as $tag) { |
|
66
|
|
|
$this->MULTI_TAGS[] = strtoupper($tag); |
|
67
|
|
|
} |
|
68
|
|
|
xml_set_object($parser, $this); |
|
69
|
|
|
xml_set_element_handler($parser, "startElement", "endElement"); |
|
70
|
|
|
xml_set_character_data_handler($parser, "characterData"); |
|
71
|
|
View Code Duplication |
if (!xml_parse($parser, $string)) { |
|
72
|
|
|
$this->error = sprintf("XML error: %s at line %d", |
|
73
|
|
|
xml_error_string(xml_get_error_code($parser)), |
|
74
|
|
|
xml_get_current_line_number($parser)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $this->elements; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function _parse_url($url) { |
|
81
|
|
|
if (!preg_match('|^https?://|i', $url)) { |
|
82
|
|
|
$this->error = "Not a valid URL ($url)"; |
|
83
|
|
|
} else { |
|
84
|
|
|
$parser = xml_parser_create(); |
|
85
|
|
|
xml_set_object($parser, $this); |
|
86
|
|
|
xml_set_element_handler($parser, "call_tag_open", "call_tag_close"); |
|
87
|
|
|
xml_set_character_data_handler($parser, "call_tag_data"); |
|
88
|
|
|
$fp = fopen($url, "r"); |
|
89
|
|
|
if (!$fp) { |
|
90
|
|
|
$this->error = "Could not open ($url)"; |
|
91
|
|
|
} else { |
|
92
|
|
|
while (!$this->error && !feof($fp)) { |
|
93
|
|
|
$string = fread($fp, 4096); |
|
94
|
|
View Code Duplication |
if (!xml_parse($parser, $string)) { |
|
95
|
|
|
$this->error = sprintf("XML error: %s at line %d", |
|
96
|
|
|
xml_error_string(xml_get_error_code($parser)), |
|
97
|
|
|
xml_get_current_line_number($parser)); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
fclose($fp); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function _parse_curl($url) { |
|
106
|
|
|
if (!preg_match('|^https?://|i', $url)) { |
|
107
|
|
|
$this->error = "Not a valid URL ($url)"; |
|
108
|
|
|
} else { |
|
109
|
|
|
$parser = xml_parser_create(); |
|
110
|
|
|
xml_set_object($parser, $this); |
|
111
|
|
|
xml_set_element_handler($parser, "call_tag_open", "call_tag_close"); |
|
112
|
|
|
xml_set_character_data_handler($parser, "call_tag_data"); |
|
113
|
|
|
|
|
114
|
|
|
$ch = curl_init($url); |
|
115
|
|
|
|
|
116
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
|
117
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); |
|
118
|
|
|
|
|
119
|
|
|
$string = curl_exec($ch); |
|
120
|
|
|
curl_close($ch); |
|
121
|
|
View Code Duplication |
if (!xml_parse($parser, $string)) { |
|
122
|
|
|
$this->error = sprintf("XML error: %s at line %d", |
|
123
|
|
|
xml_error_string(xml_get_error_code($parser)), |
|
124
|
|
|
xml_get_current_line_number($parser)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function call_tag_open($parser, $tag, $attributes) { |
|
|
|
|
|
|
131
|
|
|
global $ARBeenHere; |
|
132
|
|
|
$ARBeenHere = array(); |
|
133
|
|
|
if ($this->tag_open_template) { |
|
134
|
|
|
$this->object->call($this->tag_open_template, array("tag" => $tag, "attributes" => $attributes)); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function call_tag_close($parser, $tag) { |
|
|
|
|
|
|
139
|
|
|
global $ARBeenHere; |
|
140
|
|
|
$ARBeenHere = array(); |
|
141
|
|
|
if ($this->tag_close_template) { |
|
142
|
|
|
$this->object->call($this->tag_close_template, array("tag" => $tag)); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function call_tag_data($parser, $data) { |
|
|
|
|
|
|
147
|
|
|
global $ARBeenHere; |
|
148
|
|
|
$ARBeenHere = array(); |
|
149
|
|
|
if ($this->tag_data_template) { |
|
150
|
|
|
$this->object->call($this->tag_data_template, array("tag_data" => $data)); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
public function startElement($parser, $name, $attribs) { |
|
|
|
|
|
|
156
|
|
|
//global $MULTI_TAGS; |
|
157
|
|
|
$newElement = array(); |
|
158
|
|
|
$element = &$this->elements; |
|
159
|
|
|
|
|
160
|
|
|
if (is_array($this->ns)) { |
|
161
|
|
|
foreach ($this->ns as $n) { |
|
162
|
|
|
$element = &$element[$n]; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$this->ns[] = $name; |
|
167
|
|
|
$newElement[':attribs'] = $attribs; |
|
168
|
|
|
if (!in_array($name, $this->MULTI_TAGS)) { |
|
169
|
|
|
$element[$name] = $newElement; |
|
170
|
|
|
} else { |
|
171
|
|
|
$element[$name][] = $newElement; |
|
172
|
|
|
$this->ns[] = count($element[$name])-1; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function endElement($parser, $name) { |
|
|
|
|
|
|
177
|
|
|
//global $MULTI_TAGS; |
|
178
|
|
|
$element = &$this->elements; |
|
179
|
|
|
foreach ($this->ns as $n) { |
|
180
|
|
|
$parentElement = $element; |
|
181
|
|
|
$element = &$element[$n]; |
|
182
|
|
|
} |
|
183
|
|
|
switch ($name) { |
|
184
|
|
|
case 'item': |
|
185
|
|
|
$this->rss_items[] = $element; |
|
186
|
|
|
unset($parentElement[$name]); |
|
187
|
|
|
break; |
|
188
|
|
|
} |
|
189
|
|
|
$parent = $this->ns[count($this->ns)-2]; |
|
190
|
|
|
if (in_array($parent, $this->MULTI_TAGS) && $parent === $name) { |
|
191
|
|
|
array_pop($this->ns); |
|
192
|
|
|
} |
|
193
|
|
|
array_pop($this->ns); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
public function characterData($parser, $data) { |
|
|
|
|
|
|
197
|
|
|
$element = &$this->elements; |
|
198
|
|
|
$name = ""; |
|
199
|
|
|
foreach ($this->ns as $n) { |
|
200
|
|
|
$name .= ":$n"; |
|
201
|
|
|
$element = &$element[$n]; |
|
202
|
|
|
} |
|
203
|
|
|
switch ($n) { |
|
|
|
|
|
|
204
|
|
|
// do not put anything else above this line |
|
205
|
|
|
// or else '0' values will trigger it. |
|
206
|
|
|
case 0: |
|
207
|
|
|
default: |
|
208
|
|
|
if (!$element) { |
|
209
|
|
|
$element = array(); |
|
210
|
|
|
} |
|
211
|
|
|
$element[':data'] .= $data; |
|
212
|
|
|
break; |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: