Subversion Repositories personal-webbase

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 daniel-mar 1
<?php
2
 
3
class XmlElement
4
{
5
        var $name;
6
        var $attributes;
7
        var $content;
8
        var $children;
9
};
10
 
11
class xml
12
{
13
        function __construct()
14
        {
15
                if (!$this->required_functions())
16
                {
17
                        die();
18
                }
19
                $this->result=false;
20
        }
21
 
22
        private function required_functions()
23
        {
24
                $result = true;
25
                $essential = array('xml_parser_create','xml_parser_set_option', 'xml_parse_into_struct', 'xml_parser_free');
26
                foreach ($essential as $name)
27
                {
28
                        if (!function_exists($name))
29
                        {
30
                                $result = false;
31
                                trigger_error('xml error - this class need some functions like '.$name,E_USER_WARNING);
32
                        }
33
                }
34
                if (!$result)
35
                {
36
                        trigger_error('xml error - can\'t proceed', E_USER_ERROR);
37
                }
38
                return $result;
39
        }
40
 
41
        public function xml_code_to_object($xml)
42
        {
43
                // Template: http://www.php.net/manual/de/function.xml-parse-into-struct.php#66487
44
                // Changed for usage in ViaThinkSoft Personal WebBase
45
 
46
                $parser = xml_parser_create();
47
                xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
48
                xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
49
                xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
50
 
51
                xml_parse_into_struct($parser, $xml, $tags);
52
                xml_parser_free($parser);
53
 
54
                $elements = array();
55
                $stack = array();
56
                foreach ($tags as $tag)
57
                {
58
                        $index = count($elements);
59
                        if ($tag['type'] == "complete" || $tag['type'] == "open")
60
                        {
61
                                $elements[$index] = new XmlElement;
62
                                $elements[$index]->name = $tag['tag'];
63
                                if ((isset($tag['attributes'])) && ($tag['attributes'] != ''))
64
                                {
65
                                        foreach ($tag['attributes'] as $m1 => $m2)
66
                                        {
67
                                                $elements[$index]->attributes[$m1] = $this->xml_unescape($tag['attributes'][$m1]);
68
                                        }
69
                                }
70
                                else
71
                                {
72
                                        $elements[$index]->attributes = array();
73
                                }
74
                                if (isset($tag['value']))
75
                                {
76
                                        $elements[$index]->content = $this->xml_unescape($tag['value']);
77
                                }
78
                                if ($tag['type'] == "open")
79
                                {
80
                                        $elements[$index]->children = array();
81
                                        $stack[count($stack)] = &$elements;
82
                                        $elements = &$elements[$index]->children;
83
                                }
84
                        }
85
                        if ($tag['type'] == "close")
86
                        {
87
                                $elements = &$stack[count($stack) - 1];
88
                                unset($stack[count($stack) - 1]);
89
                        }
90
                }// echo $xml;
91
                //print_r($elements);
92
                return $elements[0];
93
        }
94
 
95
        public function xml_file_to_object($filename)
96
        {
97
                // Daniel Marschall
98
 
99
                $xml = file_get_contents($filename);
100
                return $this->xml_code_to_object($xml);
101
        }
102
 
103
        private function xml_escape($input)
104
        {
105
                // Daniel Marschall
106
 
107
                $input = str_replace('&', '&amp;',      $input);
108
                $input = str_replace("'", '&apos;', $input);
109
                $input = str_replace('"', '&quot;', $input);
110
                $input = str_replace('<', '&lt;',        $input);
111
                $input = str_replace('>', '&gt;',        $input);
112
 
113
                $input = utf8_encode($input);
114
 
115
                return $input;
116
        }
117
 
118
        private function xml_unescape($input)
119
        {
120
                // Daniel Marschall
121
 
122
                $input = utf8_decode($input);
123
 
124
                return $input;
125
        }
126
 
127
        private function recursive_xml_build($cu, $level = 0)
128
        {
129
                // Daniel Marschall
130
 
131
                $code = str_repeat("\t", $level).'<'.$cu->name;
132
                if (count($cu->attributes) > 0)
133
                {
134
                        foreach ($cu->attributes as $m1 => $m2)
135
                        {
136
                                $code .= ' '.$m1.'="'.$this->xml_escape($m2).'"';
137
                        }
138
                }
139
                if (($cu->content == '') && (count($cu->children) == 0))
140
                {
141
                        $code .= ' /';
142
                }
143
                else
144
                {
145
                        $code .= '>';
146
                        if ($cu->content != '')
147
                        {
148
                                $code .= $this->xml_escape($cu->content);
149
                        }
150
                        if (count($cu->children) > 0)
151
                        {
152
                                $code .= "\r\n";
153
                                foreach ($cu->children as $n1 => $n2)
154
                                {
155
                                        $code .= $this->{__FUNCTION__}($n2, $level+1);
156
                                }
157
                                $code .= str_repeat("\t", $level);
158
                        }
159
                        $code .= '</'.$cu->name;
160
                }
161
                $code .= ">\r\n";
162
 
163
                return $code;
164
        }
165
 
166
        public function object_to_xml_code($object)
167
        {
168
                // Daniel Marschall
169
 
170
                $code = '<?xml version="1.0" encoding="utf8"?>'."\r\n\r\n";
171
 
172
                $code .= $this->recursive_xml_build($object);
173
 
174
                return $code;
175
        }
176
 
177
        public function object_to_xml_file($object, $filename)
178
        {
179
                // Daniel Marschall
180
 
181
                if (is_writable($filename))
182
                {
183
                        $text = $this->recursive_xml_build($object);
184
 
185
                        if ($handler = @fopen($filename, 'w'))
186
                        {
187
                                if (@fwrite($handler, $text))
188
                                {
189
                                        @fclose($handler);
190
                                        return true;
191
                                }
192
                                else
193
                                {
194
                                        return false;
195
                                }
196
                        }
197
                        else
198
                        {
199
                                return false;
200
                        }
201
                }
202
                else
203
                {
204
                        return false;
205
                }
206
        }
207
}
208
 
209
?>