]> git.mxchange.org Git - friendica.git/blob - include/xml.php
Merge pull request #2758 from annando/1609-sql-charset
[friendica.git] / include / xml.php
1 <?php
2 /**
3  * @file include/xml.php
4  */
5
6
7 /**
8  * @brief This class contain functions to work with XML data
9  *
10  */
11 class xml {
12         /**
13          * @brief Creates an XML structure out of a given array
14          *
15          * @param array $array The array of the XML structure that will be generated
16          * @param object $xml The createdXML will be returned by reference
17          * @param bool $remove_header Should the XML header be removed or not?
18          * @param array $namespaces List of namespaces
19          * @param bool $root - interally used parameter. Mustn't be used from outside.
20          *
21          * @return string The created XML
22          */
23         public static function from_array($array, &$xml, $remove_header = false, $namespaces = array(), $root = true) {
24
25                 if ($root) {
26                         foreach($array as $key => $value) {
27                                 foreach ($namespaces AS $nskey => $nsvalue)
28                                         $key .= " xmlns".($nskey == "" ? "":":").$nskey.'="'.$nsvalue.'"';
29
30                                 if (is_array($value)) {
31                                         $root = new SimpleXMLElement("<".$key."/>");
32                                         self::from_array($value, $root, $remove_header, $namespaces, false);
33                                 } else
34                                         $root = new SimpleXMLElement("<".$key.">".xmlify($value)."</".$key.">");
35
36                                 $dom = dom_import_simplexml($root)->ownerDocument;
37                                 $dom->formatOutput = true;
38                                 $xml = $dom;
39
40                                 $xml_text = $dom->saveXML();
41
42                                 if ($remove_header)
43                                         $xml_text = trim(substr($xml_text, 21));
44
45                                 return $xml_text;
46                         }
47                 }
48
49                 foreach($array as $key => $value) {
50                         if (!isset($element) AND isset($xml))
51                                 $element = $xml;
52
53                         if (is_integer($key)) {
54                                 if (isset($element)) {
55                                         if (is_scalar($value)) {
56                                                 $element[0] = $value;
57                                         } else {
58                                                 /// @todo: handle nested array values
59                                         }
60                                 }
61                                 continue;
62                         }
63
64                         $element_parts = explode(":", $key);
65                         if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
66                                 $namespace = $namespaces[$element_parts[0]];
67                         elseif (isset($namespaces[""])) {
68                                 $namespace = $namespaces[""];
69                         } else
70                                 $namespace = NULL;
71
72                         // Remove undefined namespaces from the key
73                         if ((count($element_parts) > 1) AND is_null($namespace))
74                                 $key = $element_parts[1];
75
76                         if (substr($key, 0, 11) == "@attributes") {
77                                 if (!isset($element) OR !is_array($value))
78                                         continue;
79
80                                 foreach ($value as $attr_key => $attr_value) {
81                                         $element_parts = explode(":", $attr_key);
82                                         if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
83                                                 $namespace = $namespaces[$element_parts[0]];
84                                         else
85                                                 $namespace = NULL;
86
87                                         $element->addAttribute($attr_key, $attr_value, $namespace);
88                                 }
89
90                                 continue;
91                         }
92
93                         if (!is_array($value))
94                                 $element = $xml->addChild($key, xmlify($value), $namespace);
95                         elseif (is_array($value)) {
96                                 $element = $xml->addChild($key, NULL, $namespace);
97                                 self::from_array($value, $element, $remove_header, $namespaces, false);
98                         }
99                 }
100         }
101
102         /**
103          * @brief Copies an XML object
104          *
105          * @param object $source The XML source
106          * @param object $target The XML target
107          * @param string $elementname Name of the XML element of the target
108          */
109         public static function copy(&$source, &$target, $elementname) {
110                 if (count($source->children()) == 0)
111                         $target->addChild($elementname, xmlify($source));
112                 else {
113                         $child = $target->addChild($elementname);
114                         foreach ($source->children() AS $childfield => $childentry)
115                                 self::copy($childentry, $child, $childfield);
116                 }
117         }
118
119         /**
120          * @brief Create an XML element
121          *
122          * @param object $doc XML root
123          * @param string $element XML element name
124          * @param string $value XML value
125          * @param array $attributes array containing the attributes
126          *
127          * @return object XML element object
128          */
129         public static function create_element($doc, $element, $value = "", $attributes = array()) {
130                 $element = $doc->createElement($element, xmlify($value));
131
132                 foreach ($attributes AS $key => $value) {
133                         $attribute = $doc->createAttribute($key);
134                         $attribute->value = xmlify($value);
135                         $element->appendChild($attribute);
136                 }
137                 return $element;
138         }
139
140         /**
141          * @brief Create an XML and append it to the parent object
142          *
143          * @param object $doc XML root
144          * @param object $parent parent object
145          * @param string $element XML element name
146          * @param string $value XML value
147          * @param array $attributes array containing the attributes
148          */
149         public static function add_element($doc, $parent, $element, $value = "", $attributes = array()) {
150                 $element = self::create_element($doc, $element, $value, $attributes);
151                 $parent->appendChild($element);
152         }
153
154         /**
155          * @brief Convert an XML document to a normalised, case-corrected array
156          *   used by webfinger
157          *
158          * @param object $xml_element The XML document
159          * @param integer $recursion_depth recursion counter for internal use - default 0
160          *    internal use, recursion counter
161          *
162          * @return array | sring The array from the xml element or the string
163          */
164         public static function element_to_array($xml_element, &$recursion_depth=0) {
165
166                 // If we're getting too deep, bail out
167                 if ($recursion_depth > 512) {
168                         return(null);
169                 }
170
171                 if (!is_string($xml_element) &&
172                 !is_array($xml_element) &&
173                 (get_class($xml_element) == 'SimpleXMLElement')) {
174                         $xml_element_copy = $xml_element;
175                         $xml_element = get_object_vars($xml_element);
176                 }
177
178                 if (is_array($xml_element)) {
179                         $result_array = array();
180                         if (count($xml_element) <= 0) {
181                                 return (trim(strval($xml_element_copy)));
182                         }
183
184                         foreach($xml_element as $key=>$value) {
185
186                                 $recursion_depth++;
187                                 $result_array[strtolower($key)] =
188                                         self::element_to_array($value, $recursion_depth);
189                                 $recursion_depth--;
190                         }
191                         if ($recursion_depth == 0) {
192                                 $temp_array = $result_array;
193                                 $result_array = array(
194                                         strtolower($xml_element_copy->getName()) => $temp_array,
195                                 );
196                         }
197
198                         return ($result_array);
199
200                 } else {
201                         return (trim(strval($xml_element)));
202                 }
203         }
204
205         /**
206          * @brief Convert the given XML text to an array in the XML structure.
207          *
208          * xml::to_array() will convert the given XML text to an array in the XML structure.
209          * Link: http://www.bin-co.com/php/scripts/xml2array/
210          * Portions significantly re-written by mike@macgirvin.com for Friendica
211          * (namespaces, lowercase tags, get_attribute default changed, more...)
212          *
213          * Examples: $array =  xml::to_array(file_get_contents('feed.xml'));
214          *              $array =  xml::to_array(file_get_contents('feed.xml', true, 1, 'attribute'));
215          *
216          * @param object $contents The XML text
217          * @param boolean $namespaces True or false include namespace information
218          *      in the returned array as array elements.
219          * @param integer $get_attributes 1 or 0. If this is 1 the function will get the attributes as well as the tag values -
220          *      this results in a different array structure in the return value.
221          * @param string $priority Can be 'tag' or 'attribute'. This will change the way the resulting
222          *       array sturcture. For 'tag', the tags are given more importance.
223          *
224          * @return array The parsed XML in an array form. Use print_r() to see the resulting array structure.
225          */
226         public static function to_array($contents, $namespaces = true, $get_attributes=1, $priority = 'attribute') {
227                 if(!$contents) return array();
228
229                 if(!function_exists('xml_parser_create')) {
230                         logger('xml::to_array: parser function missing');
231                         return array();
232                 }
233
234
235                 libxml_use_internal_errors(true);
236                 libxml_clear_errors();
237
238                 if($namespaces)
239                         $parser = @xml_parser_create_ns("UTF-8",':');
240                 else
241                         $parser = @xml_parser_create();
242
243                 if(! $parser) {
244                         logger('xml::to_array: xml_parser_create: no resource');
245                         return array();
246                 }
247
248                 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
249                 // http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
250                 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
251                 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
252                 @xml_parse_into_struct($parser, trim($contents), $xml_values);
253                 @xml_parser_free($parser);
254
255                 if(! $xml_values) {
256                         logger('xml::to_array: libxml: parse error: ' . $contents, LOGGER_DATA);
257                         foreach(libxml_get_errors() as $err)
258                                 logger('libxml: parse: ' . $err->code . " at " . $err->line . ":" . $err->column . " : " . $err->message, LOGGER_DATA);
259                         libxml_clear_errors();
260                         return;
261                 }
262
263                 //Initializations
264                 $xml_array = array();
265                 $parents = array();
266                 $opened_tags = array();
267                 $arr = array();
268
269                 $current = &$xml_array; // Reference
270
271                 // Go through the tags.
272                 $repeated_tag_index = array(); // Multiple tags with same name will be turned into an array
273                 foreach($xml_values as $data) {
274                         unset($attributes,$value); // Remove existing values, or there will be trouble
275
276                         // This command will extract these variables into the foreach scope
277                         // tag(string), type(string), level(int), attributes(array).
278                         extract($data); // We could use the array by itself, but this cooler.
279
280                         $result = array();
281                         $attributes_data = array();
282
283                         if(isset($value)) {
284                                 if($priority == 'tag') $result = $value;
285                                 else $result['value'] = $value; // Put the value in a assoc array if we are in the 'Attribute' mode
286                         }
287
288                         //Set the attributes too.
289                         if(isset($attributes) and $get_attributes) {
290                                 foreach($attributes as $attr => $val) {
291                                         if($priority == 'tag') $attributes_data[$attr] = $val;
292                                         else $result['@attributes'][$attr] = $val; // Set all the attributes in a array called 'attr'
293                                 }
294                         }
295
296                         // See tag status and do the needed.
297                         if($namespaces && strpos($tag,':')) {
298                                 $namespc = substr($tag,0,strrpos($tag,':'));
299                                 $tag = strtolower(substr($tag,strlen($namespc)+1));
300                                 $result['@namespace'] = $namespc;
301                         }
302                         $tag = strtolower($tag);
303
304                         if($type == "open") {   // The starting of the tag '<tag>'
305                                 $parent[$level-1] = &$current;
306                                 if(!is_array($current) or (!in_array($tag, array_keys($current)))) { // Insert New tag
307                                         $current[$tag] = $result;
308                                         if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
309                                         $repeated_tag_index[$tag.'_'.$level] = 1;
310
311                                         $current = &$current[$tag];
312
313                                 } else { // There was another element with the same tag name
314
315                                         if(isset($current[$tag][0])) { // If there is a 0th element it is already an array
316                                                 $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
317                                                 $repeated_tag_index[$tag.'_'.$level]++;
318                                         } else { // This section will make the value an array if multiple tags with the same name appear together
319                                                 $current[$tag] = array($current[$tag],$result); // This will combine the existing item and the new item together to make an array
320                                                 $repeated_tag_index[$tag.'_'.$level] = 2;
321
322                                                 if(isset($current[$tag.'_attr'])) { // The attribute of the last(0th) tag must be moved as well
323                                                         $current[$tag]['0_attr'] = $current[$tag.'_attr'];
324                                                         unset($current[$tag.'_attr']);
325                                                 }
326
327                                         }
328                                         $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
329                                         $current = &$current[$tag][$last_item_index];
330                                 }
331
332                         } elseif($type == "complete") { // Tags that ends in 1 line '<tag />'
333                                 //See if the key is already taken.
334                                 if(!isset($current[$tag])) { //New Key
335                                         $current[$tag] = $result;
336                                         $repeated_tag_index[$tag.'_'.$level] = 1;
337                                         if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
338
339                                 } else { // If taken, put all things inside a list(array)
340                                         if(isset($current[$tag][0]) and is_array($current[$tag])) { // If it is already an array...
341
342                                                 // ...push the new element into that array.
343                                                 $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
344
345                                                 if($priority == 'tag' and $get_attributes and $attributes_data) {
346                                                         $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
347                                                 }
348                                                 $repeated_tag_index[$tag.'_'.$level]++;
349
350                                         } else { // If it is not an array...
351                                                 $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
352                                                 $repeated_tag_index[$tag.'_'.$level] = 1;
353                                                 if($priority == 'tag' and $get_attributes) {
354                                                         if(isset($current[$tag.'_attr'])) { // The attribute of the last(0th) tag must be moved as well
355
356                                                                 $current[$tag]['0_attr'] = $current[$tag.'_attr'];
357                                                                 unset($current[$tag.'_attr']);
358                                                         }
359
360                                                         if($attributes_data) {
361                                                                 $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
362                                                         }
363                                                 }
364                                                 $repeated_tag_index[$tag.'_'.$level]++; // 0 and 1 indexes are already taken
365                                         }
366                                 }
367
368                         } elseif($type == 'close') { // End of tag '</tag>'
369                                 $current = &$parent[$level-1];
370                         }
371                 }
372
373                 return($xml_array);
374         }
375 }
376 ?>