Code

Added xml class
[gosa.git] / gosa-core / include / utils / class_xml.inc
1 <?php
2 /*
3  * This code is part of GOsa (https://gosa.gonicus.de)
4  * Copyright (C) 2008 Cajus Pollmeier and
5  *                    http://www.bin-co.com/php/scripts/xml2array/
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
22 class xml {
23   static function xml2array($contents, $get_attributes=1, $priority = 'tag') {
24     if(!$contents) return array();
26     if(!function_exists('xml_parser_create')) {
27         //print "'xml_parser_create()' function not found!";
28         return array();
29     }
31     //Get the XML parser of PHP - PHP must have this module for the parser to work
32     $parser = xml_parser_create('');
33     xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
34     xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
35     xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
36     xml_parse_into_struct($parser, trim($contents), $xml_values);
37     xml_parser_free($parser);
39     if(!$xml_values) return;//Hmm...
41     //Initializations
42     $xml_array = array();
43     $parents = array();
44     $opened_tags = array();
45     $arr = array();
47     $current = &$xml_array; //Refference
49     //Go through the tags.
50     $repeated_tag_index = array();//Multiple tags with same name will be turned into an array
51     foreach($xml_values as $data) {
52         unset($attributes,$value);//Remove existing values, or there will be trouble
54         //This command will extract these variables into the foreach scope
55         // tag(string), type(string), level(int), attributes(array).
56         extract($data);//We could use the array by itself, but this cooler.
58         $result = array();
59         $attributes_data = array();
60         
61         if(isset($value)) {
62             if($priority == 'tag') $result = $value;
63             else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
64         }
66         //Set the attributes too.
67         if(isset($attributes) and $get_attributes) {
68             foreach($attributes as $attr => $val) {
69                 if($priority == 'tag') $attributes_data[$attr] = $val;
70                 else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
71             }
72         }
74         //See tag status and do the needed.
75         if($type == "open") {//The starting of the tag '<tag>'
76             $parent[$level-1] = &$current;
77             if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
78                 $current[$tag] = $result;
79                 if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
80                 $repeated_tag_index[$tag.'_'.$level] = 1;
82                 $current = &$current[$tag];
84             } else { //There was another element with the same tag name
86                 if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
87                     $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
88                     $repeated_tag_index[$tag.'_'.$level]++;
89                 } else {//This section will make the value an array if multiple tags with the same name appear together
90                     $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
91                     $repeated_tag_index[$tag.'_'.$level] = 2;
92                     
93                     if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
94                         $current[$tag]['0_attr'] = $current[$tag.'_attr'];
95                         unset($current[$tag.'_attr']);
96                     }
98                 }
99                 $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
100                 $current = &$current[$tag][$last_item_index];
101             }
103         } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
104             //See if the key is already taken.
105             if(!isset($current[$tag])) { //New Key
106                 $current[$tag] = $result;
107                 $repeated_tag_index[$tag.'_'.$level] = 1;
108                 if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
110             } else { //If taken, put all things inside a list(array)
111                 if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
113                     // ...push the new element into that array.
114                     $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
115                     
116                     if($priority == 'tag' and $get_attributes and $attributes_data) {
117                         $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
118                     }
119                     $repeated_tag_index[$tag.'_'.$level]++;
121                 } else { //If it is not an array...
122                     $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
123                     $repeated_tag_index[$tag.'_'.$level] = 1;
124                     if($priority == 'tag' and $get_attributes) {
125                         if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
126                             
127                             $current[$tag]['0_attr'] = $current[$tag.'_attr'];
128                             unset($current[$tag.'_attr']);
129                         }
130                         
131                         if($attributes_data) {
132                             $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
133                         }
134                     }
135                     $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
136                 }
137             }
139         } elseif($type == 'close') { //End of tag '</tag>'
140             $current = &$parent[$level-1];
141         }
142     }
143     
144     return($xml_array);
145   }
146 }