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