_parse_proppatch.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. // | [email protected] so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Hartmut Holzgraefe <[email protected]> |
  17. // | Christian Stocker <[email protected]> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: _parse_proppatch.php,v 1.3 2004/01/05 12:41:34 hholzgra Exp $
  21. //
  22. /**
  23. * helper class for parsing PROPPATCH request bodies
  24. *
  25. * @package HTTP_WebDAV_Server
  26. * @author Hartmut Holzgraefe <[email protected]>
  27. * @version 0.99.1dev
  28. */
  29. class _parse_proppatch
  30. {
  31. /**
  32. *
  33. *
  34. * @var
  35. * @access
  36. */
  37. var $success;
  38. /**
  39. *
  40. *
  41. * @var
  42. * @access
  43. */
  44. var $props;
  45. /**
  46. *
  47. *
  48. * @var
  49. * @access
  50. */
  51. var $depth;
  52. /**
  53. *
  54. *
  55. * @var
  56. * @access
  57. */
  58. var $mode;
  59. /**
  60. *
  61. *
  62. * @var
  63. * @access
  64. */
  65. var $current;
  66. /**
  67. * constructor
  68. *
  69. * @param string path of input stream
  70. * @access public
  71. */
  72. function _parse_proppatch($path)
  73. {
  74. $this->success = true;
  75. $this->depth = 0;
  76. $this->props = array();
  77. $had_input = false;
  78. $f_in = fopen($path, "r");
  79. if (!$f_in) {
  80. $this->success = false;
  81. return;
  82. }
  83. $xml_parser = xml_parser_create_ns("UTF-8", " ");
  84. xml_set_element_handler($xml_parser,
  85. array(&$this, "_startElement"),
  86. array(&$this, "_endElement"));
  87. xml_set_character_data_handler($xml_parser,
  88. array(&$this, "_data"));
  89. xml_parser_set_option($xml_parser,
  90. XML_OPTION_CASE_FOLDING, false);
  91. while($this->success && !feof($f_in)) {
  92. $line = fgets($f_in);
  93. if (is_string($line)) {
  94. $had_input = true;
  95. $this->success &= xml_parse($xml_parser, $line, false);
  96. }
  97. }
  98. if($had_input) {
  99. $this->success &= xml_parse($xml_parser, "", true);
  100. }
  101. xml_parser_free($xml_parser);
  102. fclose($f_in);
  103. }
  104. /**
  105. * tag start handler
  106. *
  107. * @param resource parser
  108. * @param string tag name
  109. * @param array tag attributes
  110. * @return void
  111. * @access private
  112. */
  113. function _startElement($parser, $name, $attrs)
  114. {
  115. if (strstr($name, " ")) {
  116. list($ns, $tag) = explode(" ", $name);
  117. if ($ns == "")
  118. $this->success = false;
  119. } else {
  120. $ns = "";
  121. $tag = $name;
  122. }
  123. if ($this->depth == 1) {
  124. $this->mode = $tag;
  125. }
  126. if ($this->depth == 3) {
  127. $prop = array("name" => $tag);
  128. $this->current = array("name" => $tag, "ns" => $ns, "status"=> 200);
  129. if ($this->mode == "set") {
  130. $this->current["val"] = ""; // default set val
  131. }
  132. }
  133. if ($this->depth >= 4) {
  134. $this->current["val"] .= "<$tag";
  135. foreach ($attr as $key => $val) {
  136. $this->current["val"] .= ' '.$key.'="'.str_replace('"','&quot;', $val).'"';
  137. }
  138. $this->current["val"] .= ">";
  139. }
  140. $this->depth++;
  141. }
  142. /**
  143. * tag end handler
  144. *
  145. * @param resource parser
  146. * @param string tag name
  147. * @return void
  148. * @access private
  149. */
  150. function _endElement($parser, $name)
  151. {
  152. if (strstr($name, " ")) {
  153. list($ns, $tag) = explode(" ", $name);
  154. if ($ns == "")
  155. $this->success = false;
  156. } else {
  157. $ns = "";
  158. $tag = $name;
  159. }
  160. $this->depth--;
  161. if ($this->depth >= 4) {
  162. $this->current["val"] .= "</$tag>";
  163. }
  164. if ($this->depth == 3) {
  165. if (isset($this->current)) {
  166. $this->props[] = $this->current;
  167. unset($this->current);
  168. }
  169. }
  170. }
  171. /**
  172. * input data handler
  173. *
  174. * @param resource parser
  175. * @param string data
  176. * @return void
  177. * @access private
  178. */
  179. function _data($parser, $data) {
  180. if (isset($this->current)) {
  181. $this->current["val"] .= $data;
  182. }
  183. }
  184. }
  185. ?>