aboutsummaryrefslogtreecommitdiffstats
path: root/lib/HTTP/CalDAV/Tools/ReportParser.php
blob: 4c904dcb6ddbc05a2329958ace86ccb51b079410 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Helper for parsing REPORT request bodies
 *
 * Long description for file (if any)...
 *
 * PHP versions 4 & 5
 *
 * LICENSE: This source file is subject to version 3.0 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
 * the PHP License & are unable to obtain it through the web, please
 * send a note to license@php.net so we can mail you a copy immediately
 *
 * @category HTTP
 * @package HTTP_CalDAV_Server
 * @author Jack Bates <ms419@freezone.co.uk>
 * @copyright 2006 The PHP Group
 * @license PHP License 3.0 http://www.php.net/license/3_0.txt
 * @version CVS: $Id: ReportParser.php,v 1.2 2006/04/13 21:14:17 jablko Exp $
 * @link http://pear.php.net/package/HTTP_CalDAV_Server
 * @see HTTP_WebDAV_Server
 */

/**
 * Helper for parsing REPORT request bodies
 *
 * Long description
 *
 * @category HTTP
 * @package HTTP_CalDAV_Server
 * @author Jack Bates <ms419@freezone.co.uk>
 * @copyright 2006 The PHP Group
 * @license PHP License 3.0 http://www.php.net/license/3_0.txt
 * @version CVS: $Id: ReportParser.php,v 1.2 2006/04/13 21:14:17 jablko Exp $
 * @link http://pear.php.net/package/HTTP_CalDAV_Server
 * @see HTTP_WebDAV_Server
 */
class ReportParser
{
    /**
     * Success state flag
     *
     * @var bool
     * @access public
     */
    var $success = false;

    /**
     * Name of the requested report
     *
     * @var string
     * @access public
     */
    var $report;

    /**
     * Found properties are collected here
     *
     * @var array
     * @access public
     */
    var $props = array();

    /**
     * Found filters are collected here
     *
     * @var array
     * @access public
     */
    var $filters = array();

    /**
     * Stack of ancestor tag names
     *
     * @var array
     * @access private
     */
    var $_names = array();

    /**
     * Stack of component data
     *
     * @var array
     * @access private
     */
    var $_comps = array();

    /**
     * Constructor
     *
     * @param string path to report input data
     * @access public
     */
    function ReportParser($input)
    {
        $handle = fopen($input, 'r');
        if (!$handle) {
            return;
        }

        $parser = xml_parser_create_ns('UTF-8', ' ');
        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
        xml_set_element_handler($parser, array(&$this, '_startElement'),
            array(&$this, '_endElement'));

        $this->success = true;
        while (($line = fgets($handle, 4096)) !== false) {
            $this->success = xml_parse($parser, $line);
            if (!$this->success) {
                return;
            }
        }

        if (!feof($handle)) {
            $this->success = false;
            return;
        }

        xml_parser_free($parser);
        fclose($handle);

        if (empty($this->props)) {
            $this->props = 'allprop';
        }
    }

    /**
     * Start tag handler
     *
     * @param object parser
     * @param string tag name
     * @param array tag attributes
     * @access private
     */
    function _startElement($parser, $name, $attrs)
    {
        $nameComponents = explode(' ', $name);
        if (count($nameComponents) > 2) {
            $this->success = false;
            return;
        }

        if (count($nameComponents) == 2) {
            list ($ns, $name) = $nameComponents;
            if (empty($ns)) {
                $this->success = false;
                return;
            }
        }

        if (empty($this->_names)) {
            $this->report = $name;
            $this->_names[] = $name;
            return;
        }

        if (count($this->_names) == 1 &&
                ($name == 'allprop' || $name == 'propname')) {
            $this->props = $name;
            $this->_names[] = $name;
            return;
        }

        if (count($this->_names) == 2 && end($this->_names) == 'prop') {
            $prop = array('name' => $name);

            if ($ns) {
                $prop['ns'] = $ns;
            }

            if ($name == 'calendar-data') {
                $prop['value'] = array();
                $this->_comps[] =& $prop['value'];
            }

            $this->props[] = $prop;
            $this->_names[] = $name;
            return;
        }

        if ($name == 'comp') {
            end($this->_comps);

            // Gross - end returns a copy of the last value
            $comp =& $this->_comps[key($this->_comps)];

            if (!is_array($comp['comps'])) {
                $comp['comps'] = array();
            }

            $comp['comps'][$attrs['name']] = array();
            $this->_comps[] =& $comp['comps'][$attrs['name']];
            $this->_names[] = $name;
            return;
        }

        if (end($this->_names) == 'comp' && $name == 'prop') {
            end($this->_comps);

            // Gross - end returns a copy of the last value
            $comp =& $this->_comps[key($this->_comps)];

            if (!is_array($comp['props'])) {
                $comp['props'] = array();
            }

            $comp['props'][] = $attrs['name'];
            $this->_names[] = $name;
            return;
        }

        if (count($this->_names) == 1 && $name == 'filter') {
            $this->_comps[] =& $this->filters;
            $this->_names[] = $name;
            return;
        }

        if ($name == 'comp-filter') {
            end($this->_comps);

            // Gross - end returns a copy of the last value
            $comp =& $this->_comps[key($this->_comps)];

            if (!is_array($comp['comps'])) {
                $comp['comps'] = array();
            }

            $comp['comps'][$attrs['name']] = array();
            $this->_comps[] =& $comp['comps'][$attrs['name']];
            $this->_names[] = $name;
            return;
        }

        if (end($this->_names) == 'comp-filter') {
            end($this->_comps);

            // Gross - end returns a copy of the last value
            $comp =& $this->_comps[key($this->_comps)];

            if (!is_array($comp['filters'])) {
                $comp['filters'] = array();
            }

            $filter = array('name' => $name, 'value' => $attrs);

            $comp['filters'][] = $filter;
            $this->_names[] = $name;
            return;
        }

        $this->_names[] = $name;
    }

    /**
     * End tag handler
     *
     * @param object parser
     * @param string tag name
     * @param array tag attributes
     * @access private
     */
    function _endElement($parser, $name) {
        $nameComponents = explode(' ', $name);
        if (count($nameComponents) > 2) {
            $this->success = false;
            return;
        }

        if (count($nameComponents) == 2) {
            list ($ns, $name) = $nameComponents;
            if (empty($ns)) {
                $this->success = false;
                return;
            }
        }

        // Any need to pop at end of calendar-data?
        // Yes - $this->_comps is re-used for parsing filters
        if ($name == 'comp' || $name == 'calendar-data' ||
                $name == 'comp-filter' || $name == 'filter') {
            array_pop($this->_comps);
        }

        array_pop($this->_names);
    }
}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * c-handling-comment-ender-p: nil
 * End:
 */

?>

© 2014-2024 Faster IT GmbH | imprint | privacy policy