import of Diogenes 0.9.18
[diogenes.git] / include / HTTP / WebDAV / Tools / _parse_lockinfo.php
1 <?php
2 //
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4 |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2003 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Authors: Hartmut Holzgraefe <hholzgra@php.net> |
17 // | Christian Stocker <chregu@bitflux.ch> |
18 // +----------------------------------------------------------------------+
19 //
20 // $Id: _parse_lockinfo.php,v 1.2 2004/01/05 12:32:40 hholzgra Exp $
21 //
22
23 /**
24 * helper class for parsing LOCK request bodies
25 *
26 * @package HTTP_WebDAV_Server
27 * @author Hartmut Holzgraefe <hholzgra@php.net>
28 * @version 0.99.1dev
29 */
30 class _parse_lockinfo
31 {
32 /**
33 * success state flag
34 *
35 * @var bool
36 * @access public
37 */
38 var $success = false;
39
40 /**
41 * lock type, currently only "write"
42 *
43 * @var string
44 * @access public
45 */
46 var $locktype = "";
47
48 /**
49 * lock scope, "shared" or "exclusive"
50 *
51 * @var string
52 * @access public
53 */
54 var $lockscope = "";
55
56 /**
57 * lock owner information
58 *
59 * @var string
60 * @access public
61 */
62 var $owner = "";
63
64 /**
65 * flag that is set during lock owner read
66 *
67 * @var bool
68 * @access private
69 */
70 var $collect_owner = false;
71
72 /**
73 * constructor
74 *
75 * @param string path of stream to read
76 * @access public
77 */
78 function _parse_lockinfo($path)
79 {
80 // we assume success unless problems occur
81 $this->success = true;
82
83 // remember if any input was parsed
84 $had_input = false;
85
86 // open stream
87 $f_in = fopen($path, "r");
88 if (!$f_in) {
89 $this->success = false;
90 return;
91 }
92
93 // create namespace aware parser
94 $xml_parser = xml_parser_create_ns("UTF-8", " ");
95
96 // set tag and data handlers
97 xml_set_element_handler($xml_parser,
98 array(&$this, "_startElement"),
99 array(&$this, "_endElement"));
100 xml_set_character_data_handler($xml_parser,
101 array(&$this, "_data"));
102
103 // we want a case sensitive parser
104 xml_parser_set_option($xml_parser,
105 XML_OPTION_CASE_FOLDING, false);
106
107 // parse input
108 while($this->success && !feof($f_in)) {
109 $line = fgets($f_in);
110 if (is_string($line)) {
111 $had_input = true;
112 $this->success &= xml_parse($xml_parser, $line, false);
113 }
114 }
115
116 // finish parsing
117 if($had_input) {
118 $this->success &= xml_parse($xml_parser, "", true);
119 }
120
121 // check if required tags where found
122 $this->success &= !empty($this->locktype);
123 $this->success &= !empty($this->lockscope);
124
125 // free parser resource
126 xml_parser_free($xml_parser);
127
128 // close input stream
129 fclose($f_in);
130 }
131
132
133 /**
134 * tag start handler
135 *
136 * @param resource parser
137 * @param string tag name
138 * @param array tag attributes
139 * @return void
140 * @access private
141 */
142 function _startElement($parser, $name, $attrs)
143 {
144 // namespace handling
145 if (strstr($name, " ")) {
146 list($ns, $tag) = explode(" ", $name);
147 } else {
148 $ns = "";
149 $tag = $name;
150 }
151
152
153 if ($this->collect_owner) {
154 // everything within the <owner> tag needs to be collected
155 $ns_short = "";
156 $ns_attr = "";
157 if ($ns) {
158 if ($ns == "DAV:") {
159 $ns_short = "D:";
160 } else {
161 $ns_attr = " xmlns='$ns'";
162 }
163 }
164 $this->owner .= "<$ns_short$tag$ns_attr>";
165 } else if ($ns == "DAV:") {
166 // parse only the essential tags
167 switch ($tag) {
168 case "write":
169 $this->locktype = $tag;
170 break;
171 case "exclusive":
172 case "shared":
173 $this->lockscope = $tag;
174 break;
175 case "owner":
176 $this->collect_owner = true;
177 break;
178 }
179 }
180 }
181
182 /**
183 * data handler
184 *
185 * @param resource parser
186 * @param string data
187 * @return void
188 * @access private
189 */
190 function _data($parser, $data)
191 {
192 // only the <owner> tag has data content
193 if ($this->collect_owner) {
194 $this->owner .= $data;
195 }
196 }
197
198 /**
199 * tag end handler
200 *
201 * @param resource parser
202 * @param string tag name
203 * @return void
204 * @access private
205 */
206 function _endElement($parser, $name)
207 {
208 // namespace handling
209 if (strstr($name, " ")) {
210 list($ns, $tag) = explode(" ", $name);
211 } else {
212 $ns = "";
213 $tag = $name;
214 }
215
216 // <owner> finished?
217 if (($ns == "DAV:") && ($tag == "owner")) {
218 $this->collect_owner = false;
219 }
220
221 // within <owner> we have to collect everything
222 if ($this->collect_owner) {
223 $ns_short = "";
224 $ns_attr = "";
225 if ($ns) {
226 if ($ns == "DAV:") {
227 $ns_short = "D:";
228 } else {
229 $ns_attr = " xmlns='$ns'";
230 }
231 }
232 $this->owner .= "</$ns_short$tag$ns_attr>";
233 }
234 }
235 }
236
237 ?>