| 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_propfind.php,v 1.2 2004/01/05 12:33:22 hholzgra Exp $ |
| 21 | // |
| 22 | |
| 23 | /** |
| 24 | * helper class for parsing PROPFIND request bodies |
| 25 | * |
| 26 | * @package HTTP_WebDAV_Server |
| 27 | * @author Hartmut Holzgraefe <hholzgra@php.net> |
| 28 | * @version 0.99.1dev |
| 29 | */ |
| 30 | class _parse_propfind |
| 31 | { |
| 32 | /** |
| 33 | * success state flag |
| 34 | * |
| 35 | * @var bool |
| 36 | * @access public |
| 37 | */ |
| 38 | var $success = false; |
| 39 | |
| 40 | /** |
| 41 | * found properties are collected here |
| 42 | * |
| 43 | * @var array |
| 44 | * @access public |
| 45 | */ |
| 46 | var $props = false; |
| 47 | |
| 48 | /** |
| 49 | * internal tag nesting depth counter |
| 50 | * |
| 51 | * @var int |
| 52 | * @access private |
| 53 | */ |
| 54 | var $depth = 0; |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * constructor |
| 59 | * |
| 60 | * @access public |
| 61 | */ |
| 62 | function _parse_propfind($path) |
| 63 | { |
| 64 | // success state flag |
| 65 | $this->success = true; |
| 66 | |
| 67 | // property storage array |
| 68 | $this->props = array(); |
| 69 | |
| 70 | // internal tag depth counter |
| 71 | $this->depth = 0; |
| 72 | |
| 73 | // remember if any input was parsed |
| 74 | $had_input = false; |
| 75 | |
| 76 | // open input stream |
| 77 | $f_in = fopen($path, "r"); |
| 78 | if (!$f_in) { |
| 79 | $this->success = false; |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | // create XML parser |
| 84 | $xml_parser = xml_parser_create_ns("UTF-8", " "); |
| 85 | |
| 86 | // set tag and data handlers |
| 87 | xml_set_element_handler($xml_parser, |
| 88 | array(&$this, "_startElement"), |
| 89 | array(&$this, "_endElement")); |
| 90 | |
| 91 | // we want a case sensitive parser |
| 92 | xml_parser_set_option($xml_parser, |
| 93 | XML_OPTION_CASE_FOLDING, false); |
| 94 | |
| 95 | |
| 96 | // parse input |
| 97 | while($this->success && !feof($f_in)) { |
| 98 | $line = fgets($f_in); |
| 99 | if (is_string($line)) { |
| 100 | $had_input = true; |
| 101 | $this->success &= xml_parse($xml_parser, $line, false); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // finish parsing |
| 106 | if($had_input) { |
| 107 | $this->success &= xml_parse($xml_parser, "", true); |
| 108 | } |
| 109 | |
| 110 | // free parser |
| 111 | xml_parser_free($xml_parser); |
| 112 | |
| 113 | // close input stream |
| 114 | fclose($f_in); |
| 115 | |
| 116 | // if no input was parsed it was a request |
| 117 | if(!count($this->props)) $this->props = "all"; // default |
| 118 | } |
| 119 | |
| 120 | |
| 121 | /** |
| 122 | * start tag handler |
| 123 | * |
| 124 | * @access private |
| 125 | * @param resource parser |
| 126 | * @param string tag name |
| 127 | * @param array tag attributes |
| 128 | */ |
| 129 | function _startElement($parser, $name, $attrs) |
| 130 | { |
| 131 | // name space handling |
| 132 | if (strstr($name, " ")) { |
| 133 | list($ns, $tag) = explode(" ", $name); |
| 134 | if ($ns == "") |
| 135 | $this->success = false; |
| 136 | } else { |
| 137 | $ns = ""; |
| 138 | $tag = $name; |
| 139 | } |
| 140 | |
| 141 | // special tags at level 1: <allprop> and <propname> |
| 142 | if ($this->depth == 1) { |
| 143 | if ($tag == "allprop") |
| 144 | $this->props = "all"; |
| 145 | |
| 146 | if ($tag == "propname") |
| 147 | $this->props = "names"; |
| 148 | } |
| 149 | |
| 150 | // requested properties are found at level 2 |
| 151 | if ($this->depth == 2) { |
| 152 | $prop = array("name" => $tag); |
| 153 | if ($ns) |
| 154 | $prop["xmlns"] = $ns; |
| 155 | $this->props[] = $prop; |
| 156 | } |
| 157 | |
| 158 | // increment depth count |
| 159 | $this->depth++; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | /** |
| 164 | * end tag handler |
| 165 | * |
| 166 | * @access private |
| 167 | * @param resource parser |
| 168 | * @param string tag name |
| 169 | */ |
| 170 | function _endElement($parser, $name) |
| 171 | { |
| 172 | // here we only need to decrement the depth count |
| 173 | $this->depth--; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | |
| 178 | ?> |