1 <?php
2 /*
3 * This code is part of GOsa (https://gosa.gonicus.de)
4 * Copyright (C) 2003 Cajus Pollmeier
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
21 class xmlParse {
23 /* XML parser */
24 var $parser;
26 /* Selected connection */
27 var $current = array();
28 var $currentPos = 0;
29 var $depth = array();
30 var $data = array();
32 function xmlParse()
33 {
34 $this->parser = xml_parser_create();
35 xml_set_object($this->parser, $this);
36 xml_set_element_handler($this->parser, "tag_open", "tag_close");
37 }
39 function parseMenu($file)
40 {
41 if (!($fp = @fopen($file, "r"))) {
42 print_red(sprintf(_("could not open XML input '%s'."),$file));
43 $this->data = array();
44 return(false);
45 }
47 $data = fread($fp, filesize($file));
49 $t = split("\n",$data);
50 $s = preg_replace("/<!DOCTYPE.*>/","<Input>",$data."</Input>");
51 $data = $t[0]."\n".$s;
53 fclose($fp);
54 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 1);
55 xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 0);
56 xml_parse_into_struct($this->parser, $data, $vals, $index);
58 $params = array();
59 $level = array();
61 $this->data = array();
62 $i = 0 ;
63 $current ="";
64 $dir = "";
65 foreach($vals as $ele){
66 if($ele['tag'] =="INPUT") continue;
67 if($ele['tag'] =="INCLUDE") continue;
69 if(!in_array($ele['tag'],array("MENU","CATEGORY","DIRECTORY"))) continue;
71 if($ele['type'] == "open"){
72 $tag = $ele['tag'].$i;
74 if($ele['tag'] != "MENU"){
75 $i++;
76 $this->currentPos ++ ;
77 $this->depth[$this->currentPos] = $tag;
78 }
79 $current = &$this->data;
80 foreach($this->depth as $name){
81 $current = &$current[$name];
82 }
83 }elseif($ele['type']=="close"){
84 unset($this->depth[($this->currentPos)]);
85 $this->currentPos --;
86 if($ele['tag'] == "MENU")
87 $dir ="";
88 }
89 if(($ele['tag'] == "CATEGORY")&&(!empty($ele['value']))&&(isset($ele['value']))){
90 $current[$dir]['name'] = $ele['value'];
91 }
92 if(isset($current['type'])){
93 unset($current['type']);
94 }
95 if($ele['tag'] == "DIRECTORY"){
96 $dir .= preg_replace("/\.directory/","",$ele['value'])."/";
97 }
98 if(($ele['tag'] == "CATEGORY")&&(!empty($ele['value']))&&(isset($ele['value']))){
99 $current[$dir] = $ele['value'];
100 }
101 $curback = $current;
102 }
103 }
106 function parse($filename)
107 {
108 if(!file_exists($filename)){
109 return(false);
110 }else{
111 $fh= fopen($filename, "r");
112 $xmldata= fread($fh, 100000);
113 fclose($fh);
114 if(!xml_parse($this->parser, chop($xmldata))){
115 print(sprintf(_("XML error in %s : %s at line %d"),$filename,
116 xml_error_string(xml_get_error_code($this->parser)),
117 xml_get_current_line_number($this->parser)));
118 }
119 }
120 return(true);
121 }
123 function tag_open($parser, $tag, $attrs)
124 {
125 $this->currentPos ++ ;
126 $this->depth[$this->currentPos] = $tag;
128 $current = &$this->data;
129 foreach($this->depth as $name){
130 $current = &$current[$name];
131 }
132 $current = $attrs;
133 }
135 function tag_close($parser, $tag)
136 {
137 unset($this->depth[($this->currentPos)]);
138 $this->currentPos --;
139 }
141 function GetData()
142 {
143 return ($this->data);
144 }
146 }
147 ?>