]> git.mxchange.org Git - friendica.git/blob - src/Util/XML.php
c115e4d0de37c354f1fcfc95189025ebfb96d0b7
[friendica.git] / src / Util / XML.php
1 <?php
2 /**
3  * @file src/Util/XML.php
4  */
5 namespace Friendica\Util;
6
7 use Friendica\Core\Logger;
8 use DOMXPath;
9 use SimpleXMLElement;
10
11 /**
12  * @brief This class contain methods to work with XML data
13  */
14 class XML
15 {
16         /**
17          * @brief Creates an XML structure out of a given array
18          *
19          * @param array  $array         The array of the XML structure that will be generated
20          * @param object $xml           The createdXML will be returned by reference
21          * @param bool   $remove_header Should the XML header be removed or not?
22          * @param array  $namespaces    List of namespaces
23          * @param bool   $root          interally used parameter. Mustn't be used from outside.
24          *
25          * @return string The created XML
26          */
27         public static function fromArray($array, &$xml, $remove_header = false, $namespaces = [], $root = true)
28         {
29                 if ($root) {
30                         foreach ($array as $key => $value) {
31                                 foreach ($namespaces as $nskey => $nsvalue) {
32                                         $key .= " xmlns".($nskey == "" ? "":":").$nskey.'="'.$nsvalue.'"';
33                                 }
34
35                                 if (is_array($value)) {
36                                         $root = new SimpleXMLElement("<".$key."/>");
37                                         self::fromArray($value, $root, $remove_header, $namespaces, false);
38                                 } else {
39                                         $root = new SimpleXMLElement("<".$key.">".self::escape($value)."</".$key.">");
40                                 }
41
42                                 $dom = dom_import_simplexml($root)->ownerDocument;
43                                 $dom->formatOutput = true;
44                                 $xml = $dom;
45
46                                 $xml_text = $dom->saveXML();
47
48                                 if ($remove_header) {
49                                         $xml_text = trim(substr($xml_text, 21));
50                                 }
51
52                                 return $xml_text;
53                         }
54                 }
55
56                 $element = null;
57                 foreach ($array as $key => $value) {
58                         if (!isset($element) && isset($xml)) {
59                                 $element = $xml;
60                         }
61
62                         if (is_integer($key)) {
63                                 if (isset($element)) {
64                                         if (is_scalar($value)) {
65                                                 $element[0] = $value;
66                                         } else {
67                                                 /// @todo: handle nested array values
68                                         }
69                                 }
70                                 continue;
71                         }
72
73                         $element_parts = explode(":", $key);
74                         if ((count($element_parts) > 1) && isset($namespaces[$element_parts[0]])) {
75                                 $namespace = $namespaces[$element_parts[0]];
76                         } elseif (isset($namespaces[""])) {
77                                 $namespace = $namespaces[""];
78                         } else {
79                                 $namespace = null;
80                         }
81
82                         // Remove undefined namespaces from the key
83                         if ((count($element_parts) > 1) && is_null($namespace)) {
84                                 $key = $element_parts[1];
85                         }
86
87                         if (substr($key, 0, 11) == "@attributes") {
88                                 if (!isset($element) || !is_array($value)) {
89                                         continue;
90                                 }
91
92                                 foreach ($value as $attr_key => $attr_value) {
93                                         $element_parts = explode(":", $attr_key);
94                                         if ((count($element_parts) > 1) && isset($namespaces[$element_parts[0]])) {
95                                                 $namespace = $namespaces[$element_parts[0]];
96                                         } else {
97                                                 $namespace = null;
98                                         }
99
100                                         $element->addAttribute($attr_key, $attr_value, $namespace);
101                                 }
102
103                                 continue;
104                         }
105
106                         if (!is_array($value)) {
107                                 $element = $xml->addChild($key, self::escape($value), $namespace);
108                         } elseif (is_array($value)) {
109                                 $element = $xml->addChild($key, null, $namespace);
110                                 self::fromArray($value, $element, $remove_header, $namespaces, false);
111                         }
112                 }
113         }
114
115         /**
116          * @brief Copies an XML object
117          *
118          * @param object $source      The XML source
119          * @param object $target      The XML target
120          * @param string $elementname Name of the XML element of the target
121          * @return void
122          */
123         public static function copy(&$source, &$target, $elementname)
124         {
125                 if (count($source->children()) == 0) {
126                         $target->addChild($elementname, self::escape($source));
127                 } else {
128                         $child = $target->addChild($elementname);
129                         foreach ($source->children() as $childfield => $childentry) {
130                                 self::copy($childentry, $child, $childfield);
131                         }
132                 }
133         }
134
135         /**
136          * @brief Create an XML element
137          *
138          * @param object $doc        XML root
139          * @param string $element    XML element name
140          * @param string $value      XML value
141          * @param array  $attributes array containing the attributes
142          *
143          * @return object XML element object
144          */
145         public static function createElement($doc, $element, $value = "", $attributes = [])
146         {
147                 $element = $doc->createElement($element, self::escape($value));
148
149                 foreach ($attributes as $key => $value) {
150                         $attribute = $doc->createAttribute($key);
151                         $attribute->value = self::escape($value);
152                         $element->appendChild($attribute);
153                 }
154                 return $element;
155         }
156
157         /**
158          * @brief Create an XML and append it to the parent object
159          *
160          * @param object $doc        XML root
161          * @param object $parent     parent object
162          * @param string $element    XML element name
163          * @param string $value      XML value
164          * @param array  $attributes array containing the attributes
165          * @return void
166          */
167         public static function addElement($doc, $parent, $element, $value = "", $attributes = [])
168         {
169                 $element = self::createElement($doc, $element, $value, $attributes);
170                 $parent->appendChild($element);
171         }
172
173         /**
174          * @brief Convert an XML document to a normalised, case-corrected array
175          *   used by webfinger
176          *
177          * @param object  $xml_element     The XML document
178          * @param integer $recursion_depth recursion counter for internal use - default 0
179          *                                 internal use, recursion counter
180          *
181          * @return array | string The array from the xml element or the string
182          */
183         public static function elementToArray($xml_element, &$recursion_depth = 0)
184         {
185                 // If we're getting too deep, bail out
186                 if ($recursion_depth > 512) {
187                         return(null);
188                 }
189
190                 $xml_element_copy = '';
191                 if (!is_string($xml_element)
192                         && !is_array($xml_element)
193                         && (get_class($xml_element) == 'SimpleXMLElement')
194                 ) {
195                         $xml_element_copy = $xml_element;
196                         $xml_element = get_object_vars($xml_element);
197                 }
198
199                 if (is_array($xml_element)) {
200                         $result_array = [];
201                         if (count($xml_element) <= 0) {
202                                 return (trim(strval($xml_element_copy)));
203                         }
204
205                         foreach ($xml_element as $key => $value) {
206                                 $recursion_depth++;
207                                 $result_array[strtolower($key)] = self::elementToArray($value, $recursion_depth);
208                                 $recursion_depth--;
209                         }
210
211                         if ($recursion_depth == 0) {
212                                 $temp_array = $result_array;
213                                 $result_array = [
214                                         strtolower($xml_element_copy->getName()) => $temp_array,
215                                 ];
216                         }
217
218                         return ($result_array);
219                 } else {
220                         return (trim(strval($xml_element)));
221                 }
222         }
223
224         /**
225          * @brief Convert the given XML text to an array in the XML structure.
226          *
227          * Xml::toArray() will convert the given XML text to an array in the XML structure.
228          * Link: http://www.bin-co.com/php/scripts/xml2array/
229          * Portions significantly re-written by mike@macgirvin.com for Friendica
230          * (namespaces, lowercase tags, get_attribute default changed, more...)
231          *
232          * Examples: $array =  Xml::toArray(file_get_contents('feed.xml'));
233          *              $array =  Xml::toArray(file_get_contents('feed.xml', true, 1, 'attribute'));
234          *
235          * @param object  $contents       The XML text
236          * @param boolean $namespaces     True or false include namespace information
237          *                                    in the returned array as array elements.
238          * @param integer $get_attributes 1 or 0. If this is 1 the function will get the attributes as well as the tag values -
239          *                                    this results in a different array structure in the return value.
240          * @param string  $priority       Can be 'tag' or 'attribute'. This will change the way the resulting
241          *                                    array sturcture. For 'tag', the tags are given more importance.
242          *
243          * @return array The parsed XML in an array form. Use print_r() to see the resulting array structure.
244          */
245         public static function toArray($contents, $namespaces = true, $get_attributes = 1, $priority = 'attribute')
246         {
247                 if (!$contents) {
248                         return [];
249                 }
250
251                 if (!function_exists('xml_parser_create')) {
252                         Logger::log('Xml::toArray: parser function missing');
253                         return [];
254                 }
255
256
257                 libxml_use_internal_errors(true);
258                 libxml_clear_errors();
259
260                 if ($namespaces) {
261                         $parser = @xml_parser_create_ns("UTF-8", ':');
262                 } else {
263                         $parser = @xml_parser_create();
264                 }
265
266                 if (! $parser) {
267                         Logger::log('Xml::toArray: xml_parser_create: no resource');
268                         return [];
269                 }
270
271                 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
272                 // http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
273                 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
274                 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
275                 @xml_parse_into_struct($parser, trim($contents), $xml_values);
276                 @xml_parser_free($parser);
277
278                 if (! $xml_values) {
279                         Logger::log('Xml::toArray: libxml: parse error: ' . $contents, Logger::DATA);
280                         foreach (libxml_get_errors() as $err) {
281                                 Logger::log('libxml: parse: ' . $err->code . " at " . $err->line . ":" . $err->column . " : " . $err->message, Logger::DATA);
282                         }
283                         libxml_clear_errors();
284                         return;
285                 }
286
287                 //Initializations
288                 $xml_array = [];
289                 $parents = [];
290                 $opened_tags = [];
291                 $arr = [];
292
293                 $current = &$xml_array; // Reference
294
295                 // Go through the tags.
296                 $repeated_tag_index = []; // Multiple tags with same name will be turned into an array
297                 foreach ($xml_values as $data) {
298                         $tag        = $data['tag'];
299                         $type       = $data['type'];
300                         $level      = $data['level'];
301                         $attributes = isset($data['attributes']) ? $data['attributes'] : null;
302                         $value      = isset($data['value']) ? $data['value'] : null;
303
304                         $result = [];
305                         $attributes_data = [];
306
307                         if (isset($value)) {
308                                 if ($priority == 'tag') {
309                                         $result = $value;
310                                 } else {
311                                         $result['value'] = $value; // Put the value in a assoc array if we are in the 'Attribute' mode
312                                 }
313                         }
314
315                         //Set the attributes too.
316                         if (isset($attributes) and $get_attributes) {
317                                 foreach ($attributes as $attr => $val) {
318                                         if ($priority == 'tag') {
319                                                 $attributes_data[$attr] = $val;
320                                         } else {
321                                                 $result['@attributes'][$attr] = $val; // Set all the attributes in a array called 'attr'
322                                         }
323                                 }
324                         }
325
326                         // See tag status and do the needed.
327                         if ($namespaces && strpos($tag, ':')) {
328                                 $namespc = substr($tag, 0, strrpos($tag, ':'));
329                                 $tag = strtolower(substr($tag, strlen($namespc)+1));
330                                 $result['@namespace'] = $namespc;
331                         }
332                         $tag = strtolower($tag);
333
334                         if ($type == "open") {   // The starting of the tag '<tag>'
335                                 $parent[$level-1] = &$current;
336                                 if (!is_array($current) || (!in_array($tag, array_keys($current)))) { // Insert New tag
337                                         $current[$tag] = $result;
338                                         if ($attributes_data) {
339                                                 $current[$tag. '_attr'] = $attributes_data;
340                                         }
341                                         $repeated_tag_index[$tag.'_'.$level] = 1;
342
343                                         $current = &$current[$tag];
344                                 } else { // There was another element with the same tag name
345
346                                         if (isset($current[$tag][0])) { // If there is a 0th element it is already an array
347                                                 $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
348                                                 $repeated_tag_index[$tag.'_'.$level]++;
349                                         } else { // This section will make the value an array if multiple tags with the same name appear together
350                                                 $current[$tag] = [$current[$tag], $result]; // This will combine the existing item and the new item together to make an array
351                                                 $repeated_tag_index[$tag.'_'.$level] = 2;
352
353                                                 if (isset($current[$tag.'_attr'])) { // The attribute of the last(0th) tag must be moved as well
354                                                         $current[$tag]['0_attr'] = $current[$tag.'_attr'];
355                                                         unset($current[$tag.'_attr']);
356                                                 }
357                                         }
358                                         $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
359                                         $current = &$current[$tag][$last_item_index];
360                                 }
361                         } elseif ($type == "complete") { // Tags that ends in 1 line '<tag />'
362                                 //See if the key is already taken.
363                                 if (!isset($current[$tag])) { //New Key
364                                         $current[$tag] = $result;
365                                         $repeated_tag_index[$tag.'_'.$level] = 1;
366                                         if ($priority == 'tag' and $attributes_data) {
367                                                 $current[$tag. '_attr'] = $attributes_data;
368                                         }
369                                 } else { // If taken, put all things inside a list(array)
370                                         if (isset($current[$tag][0]) and is_array($current[$tag])) { // If it is already an array...
371
372                                                 // ...push the new element into that array.
373                                                 $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
374
375                                                 if ($priority == 'tag' and $get_attributes and $attributes_data) {
376                                                         $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
377                                                 }
378                                                 $repeated_tag_index[$tag.'_'.$level]++;
379                                         } else { // If it is not an array...
380                                                 $current[$tag] = [$current[$tag], $result]; //...Make it an array using using the existing value and the new value
381                                                 $repeated_tag_index[$tag.'_'.$level] = 1;
382                                                 if ($priority == 'tag' and $get_attributes) {
383                                                         if (isset($current[$tag.'_attr'])) { // The attribute of the last(0th) tag must be moved as well
384
385                                                                 $current[$tag]['0_attr'] = $current[$tag.'_attr'];
386                                                                 unset($current[$tag.'_attr']);
387                                                         }
388
389                                                         if ($attributes_data) {
390                                                                 $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
391                                                         }
392                                                 }
393                                                 $repeated_tag_index[$tag.'_'.$level]++; // 0 and 1 indexes are already taken
394                                         }
395                                 }
396                         } elseif ($type == 'close') { // End of tag '</tag>'
397                                 $current = &$parent[$level-1];
398                         }
399                 }
400
401                 return($xml_array);
402         }
403
404         /**
405          * @brief Delete a node in a XML object
406          *
407          * @param object $doc  XML document
408          * @param string $node Node name
409          * @return void
410          */
411         public static function deleteNode(&$doc, $node)
412         {
413                 $xpath = new DOMXPath($doc);
414                 $list = $xpath->query("//".$node);
415                 foreach ($list as $child) {
416                         $child->parentNode->removeChild($child);
417                 }
418         }
419
420         public static function parseString($s, $strict = true)
421         {
422                 // the "strict" parameter is deactivated
423                 libxml_use_internal_errors(true);
424
425                 $x = @simplexml_load_string($s);
426                 if (!$x) {
427                         Logger::log('libxml: parse: error: ' . $s, Logger::DATA);
428                         foreach (libxml_get_errors() as $err) {
429                                 Logger::log('libxml: parse: ' . $err->code." at ".$err->line.":".$err->column." : ".$err->message, Logger::DATA);
430                         }
431                         libxml_clear_errors();
432                 }
433                 return $x;
434         }
435
436         public static function getFirstNodeValue($xpath, $element, $context = null)
437         {
438                 $result = $xpath->evaluate($element, $context);
439                 if (!is_object($result)) {
440                         return '';
441                 }
442
443                 $first_item = $result->item(0);
444                 if (!is_object($first_item)) {
445                         return '';
446                 }
447
448                 return $first_item->nodeValue;
449         }
450
451         public static function getFirstAttributes($xpath, $element, $context = null)
452         {
453                 $result = $xpath->query($element, $context);
454                 if (!is_object($result)) {
455                         return false;
456                 }
457
458                 $first_item = $result->item(0);
459                 if (!is_object($first_item)) {
460                         return false;
461                 }
462
463                 return $first_item->attributes;
464         }
465
466         /**
467          * escape text ($str) for XML transport
468          * @param string $str
469          * @return string Escaped text.
470          */
471         public static function escape($str)
472         {
473                 $buffer = htmlspecialchars($str, ENT_QUOTES, "UTF-8");
474                 $buffer = trim($buffer);
475
476                 return $buffer;
477         }
478
479         /**
480          * undo an escape
481          * @param string $s xml escaped text
482          * @return string unescaped text
483          */
484         public static function unescape($s)
485         {
486                 $ret = htmlspecialchars_decode($s, ENT_QUOTES);
487                 return $ret;
488         }
489
490         /**
491          * apply escape() to all values of array $val, recursively
492          * @param array $val
493          * @return array
494          */
495         public static function arrayEscape($val)
496         {
497                 if (is_bool($val)) {
498                         return $val?"true":"false";
499                 } elseif (is_array($val)) {
500                         return array_map('XML::arrayEscape', $val);
501                 }
502                 return self::escape((string) $val);
503         }
504 }