Code

01d5a4ade3cd0997dc86c2128edda4ad8d24b95d
[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 {
24   static function validate($file, $schema) {
25     // Enable user error handling
26     libxml_use_internal_errors(true);
27     
28     $xml= new DOMDocument();
29     $xml->load($file);
30     
31     if (!$xml->schemaValidate($schema)) {
32       $errors = libxml_get_errors();
33       foreach ($errors as $error) {
34         $estr= "";
35         switch ($error->level) {
36             case LIBXML_ERR_WARNING:
37                 $estr= _("Warning")." ".$error->code.":";
38                 break;
39             case LIBXML_ERR_ERROR:
40                 $estr= _("Error")." ".$error->code.":";
41                 break;
42             case LIBXML_ERR_FATAL:
43                 $estr= _("Fatal error")." ".$error->code.":";
44                 break;
45         }
46         if ($error->file) {
47           $str= sprintf("%s %s in %s", $estr, trim($error->message), $error->file);
48         } else {
49           $str= sprintf("%s %s in %s on line %s", $estr, trim($error->message), $error->file, $error->line);
50         }
51         msg_dialog::display(_("XML error"), $str, ERROR_DIALOG);
52       }
53       libxml_clear_errors();
54     }
55   }
58   static function xml2array($contents, $get_attributes=1, $priority = 'tag') {
59     if(!$contents) return array();
61     if(!function_exists('xml_parser_create')) {
62         //print "'xml_parser_create()' function not found!";
63         return array();
64     }
66     //Get the XML parser of PHP - PHP must have this module for the parser to work
67     $parser = xml_parser_create('');
68     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
69     xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
70     xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
71     xml_parse_into_struct($parser, trim($contents), $xml_values);
72     xml_parser_free($parser);
74     if(!$xml_values) return;//Hmm...
76     //Initializations
77     $xml_array = array();
78     $parents = array();
79     $opened_tags = array();
80     $arr = array();
82     $current = &$xml_array; //Refference
84     //Go through the tags.
85     $repeated_tag_index = array();//Multiple tags with same name will be turned into an array
86     foreach($xml_values as $data) {
87         unset($attributes,$value);//Remove existing values, or there will be trouble
89         //This command will extract these variables into the foreach scope
90         // tag(string), type(string), level(int), attributes(array).
91         extract($data);//We could use the array by itself, but this cooler.
93         $result = array();
94         $attributes_data = array();
95         
96         if(isset($value)) {
97             if($priority == 'tag') $result = $value;
98             else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
99         }
101         //Set the attributes too.
102         if(isset($attributes) and $get_attributes) {
103             foreach($attributes as $attr => $val) {
104                 if($priority == 'tag') $attributes_data[$attr] = $val;
105                 else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
106             }
107         }
109         //See tag status and do the needed.
110         if($type == "open") {//The starting of the tag '<tag>'
111             $parent[$level-1] = &$current;
112             if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
113                 $current[$tag] = $result;
114                 if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
115                 $repeated_tag_index[$tag.'_'.$level] = 1;
117                 $current = &$current[$tag];
119             } else { //There was another element with the same tag name
121                 if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
122                     $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
123                     $repeated_tag_index[$tag.'_'.$level]++;
124                 } else {//This section will make the value an array if multiple tags with the same name appear together
125                     $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
126                     $repeated_tag_index[$tag.'_'.$level] = 2;
127                     
128                     if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
129                         $current[$tag]['0_attr'] = $current[$tag.'_attr'];
130                         unset($current[$tag.'_attr']);
131                     }
133                 }
134                 $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
135                 $current = &$current[$tag][$last_item_index];
136             }
138         } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
139             //See if the key is already taken.
140             if(!isset($current[$tag])) { //New Key
141                 $current[$tag] = $result;
142                 $repeated_tag_index[$tag.'_'.$level] = 1;
143                 if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
145             } else { //If taken, put all things inside a list(array)
146                 if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
148                     // ...push the new element into that array.
149                     $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
150                     
151                     if($priority == 'tag' and $get_attributes and $attributes_data) {
152                         $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
153                     }
154                     $repeated_tag_index[$tag.'_'.$level]++;
156                 } else { //If it is not an array...
157                     $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
158                     $repeated_tag_index[$tag.'_'.$level] = 1;
159                     if($priority == 'tag' and $get_attributes) {
160                         if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
161                             
162                             $current[$tag]['0_attr'] = $current[$tag.'_attr'];
163                             unset($current[$tag.'_attr']);
164                         }
165                         
166                         if($attributes_data) {
167                             $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
168                         }
169                     }
170                     $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
171                 }
172             }
174         } elseif($type == 'close') { //End of tag '</tag>'
175             $current = &$parent[$level-1];
176         }
177     }
178     
179     return($xml_array);
180   }
182 }