]> git.mxchange.org Git - friendica.git/blob - include/xml.php
Bugfix: The function to remove parts of attachments forgot about the rest
[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                                 $root = new SimpleXMLElement("<".$key."/>");
31                                 self::from_array($value, $root, $remove_header, $namespaces, false);
32
33                                 $dom = dom_import_simplexml($root)->ownerDocument;
34                                 $dom->formatOutput = true;
35                                 $xml = $dom;
36
37                                 $xml_text = $dom->saveXML();
38
39                                 if ($remove_header)
40                                         $xml_text = trim(substr($xml_text, 21));
41
42                                 return $xml_text;
43                         }
44                 }
45
46                 foreach($array as $key => $value) {
47                         if ($key == "@attributes") {
48                                 if (!isset($element) OR !is_array($value))
49                                         continue;
50
51                                 foreach ($value as $attr_key => $attr_value) {
52                                         $element_parts = explode(":", $attr_key);
53                                         if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
54                                                 $namespace = $namespaces[$element_parts[0]];
55                                         else
56                                                 $namespace = NULL;
57
58                                         $element->addAttribute ($attr_key, $attr_value, $namespace);
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                         else
68                                 $namespace = NULL;
69
70                         if (!is_array($value))
71                                 $element = $xml->addChild($key, xmlify($value), $namespace);
72                         elseif (is_array($value)) {
73                                 $element = $xml->addChild($key, NULL, $namespace);
74                                 self::from_array($value, $element, $remove_header, $namespaces, false);
75                         }
76                 }
77         }
78
79         /**
80          * @brief Copies an XML object
81          *
82          * @param object $source The XML source
83          * @param object $target The XML target
84          * @param string $elementname Name of the XML element of the target
85          */
86         public static function copy(&$source, &$target, $elementname) {
87                 if (count($source->children()) == 0)
88                         $target->addChild($elementname, xmlify($source));
89                 else {
90                         $child = $target->addChild($elementname);
91                         foreach ($source->children() AS $childfield => $childentry)
92                                 self::copy($childentry, $child, $childfield);
93                 }
94         }
95
96         /**
97          * @brief Create an XML element
98          *
99          * @param object $doc XML root
100          * @param string $element XML element name
101          * @param string $value XML value
102          * @param array $attributes array containing the attributes
103          *
104          * @return object XML element object
105          */
106         public static function create_element($doc, $element, $value = "", $attributes = array()) {
107                 $element = $doc->createElement($element, xmlify($value));
108
109                 foreach ($attributes AS $key => $value) {
110                         $attribute = $doc->createAttribute($key);
111                         $attribute->value = xmlify($value);
112                         $element->appendChild($attribute);
113                 }
114                 return $element;
115         }
116
117         /**
118          * @brief Create an XML and append it to the parent object
119          *
120          * @param object $doc XML root
121          * @param object $parent parent object
122          * @param string $element XML element name
123          * @param string $value XML value
124          * @param array $attributes array containing the attributes
125          */
126         public static function add_element($doc, $parent, $element, $value = "", $attributes = array()) {
127                 $element = self::create_element($doc, $element, $value, $attributes);
128                 $parent->appendChild($element);
129         }
130 }
131 ?>