aboutsummaryrefslogtreecommitdiffstats
path: root/lib/HTTP/WebDAV/Tools/_parse_propfind.php
blob: a44c3a97a00afb74f100d26fe5ef38eeb7db3d02 (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
<?php
// +----------------------------------------------------------------------+
// | PHP Version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license,      |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Hartmut Holzgraefe <hholzgra@php.net>                       |
// |          Christian Stocker <chregu@bitflux.ch>                       |
// +----------------------------------------------------------------------+
//
// $Id: _parse_propfind.php,v 1.2 2006/04/14 02:50:10 jablko Exp $

/**
 * helper class for parsing PROPFIND request bodies
 *
 * @package HTTP_WebDAV_Server
 * @author Hartmut Holzgraefe <hholzgra@php.net>
 * @version 0.99.1dev
 */
class _parse_propfind
{
    /**
     * success state flag
     *
     * @var bool
     * @access public
     */
    var $success = false;

    /**
     * found properties are collected here
     *
     * @var array
     * @access public
     */
    var $props = false;

    /**
     * internal tag nesting depth counter
     *
     * @var int
     * @access private
     */
    var $depth = 0;

    /**
     * constructor
     *
     * @access public
     */
    function _parse_propfind($input)
    {
        // success state flag
        $this->success = true;

        // property storage array
        $this->props = array();

        // internal tag depth counter
        $this->depth = 0;

        // remember if any input was parsed
        $had_input = false;

        // open input stream
        $handle = fopen($input, 'r');
        if (!$handle) {
            $this->success = false;
            return;
        }

        // create XML parser
        $parser = xml_parser_create_ns('UTF-8', ' ');

        // set tag and data handlers
        xml_set_element_handler($parser, array(&$this, '_startElement'),
            array(&$this, '_endElement'));

        // we want a case sensitive parser
        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);

        // parse input
        while ($this->success && !feof($handle)) {
            $line = fgets($handle);
            if (is_string($line)) {
                $had_input = true;
                $this->success &= xml_parse($parser, $line, false);
            }
        }

        // finish parsing
        if ($had_input) {
            $this->success &= xml_parse($parser, '', true);
        }

        // free parser
        xml_parser_free($parser);

        // close input stream
        fclose($handle);

        // if no input was parsed it was a request
        if (empty($this->props)) {
            $this->props = 'allprop';
        }
    }

    /**
     * start tag handler
     *
     * @access private
     * @param resource parser
     * @param string tag name
     * @param array tag attributes
     */
    function _startElement($parser, $name, $attrs)
    {
        // name space handling
        if (strstr($name, ' ')) {
            list ($ns, $name) = explode(' ', $name);
            if (!$ns) {
                $this->success = false;
            }
        }

        // special tags at level 1: <allprop> and <propname>
        if ($this->depth == 1) {
            if ($name == 'allprop' || $name == 'propname') {
                $this->props = $name;
            }
        }

        // requested properties are found at level 2
        if ($this->depth == 2) {
            $prop = array('name' => $name);
            if ($ns) {
                $prop['ns'] = $ns;
            }
            $this->props[] = $prop;
        }

        // increment depth count
        $this->depth++;
    }

    /**
     * end tag handler
     *
     * @access private
     * @param resource parser
     * @param string tag name
     */
    function _endElement($parser, $name)
    {
        // here we only need to decrement the depth count
        $this->depth--;
    }
}
?>

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