_parse_lockinfo.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_lockinfo.php,v 1.2 2004/01/05 12:32:40 hholzgra Exp $
  21. //
  22. /**
  23. * helper class for parsing LOCK request bodies
  24. *
  25. * @package HTTP_WebDAV_Server
  26. * @author Hartmut Holzgraefe <[email protected]>
  27. * @version 0.99.1dev
  28. */
  29. class _parse_lockinfo
  30. {
  31. /**
  32. * success state flag
  33. *
  34. * @var bool
  35. * @access public
  36. */
  37. var $success = false;
  38. /**
  39. * lock type, currently only "write"
  40. *
  41. * @var string
  42. * @access public
  43. */
  44. var $locktype = "";
  45. /**
  46. * lock scope, "shared" or "exclusive"
  47. *
  48. * @var string
  49. * @access public
  50. */
  51. var $lockscope = "";
  52. /**
  53. * lock owner information
  54. *
  55. * @var string
  56. * @access public
  57. */
  58. var $owner = "";
  59. /**
  60. * flag that is set during lock owner read
  61. *
  62. * @var bool
  63. * @access private
  64. */
  65. var $collect_owner = false;
  66. /**
  67. * constructor
  68. *
  69. * @param string path of stream to read
  70. * @access public
  71. */
  72. function _parse_lockinfo($path)
  73. {
  74. // we assume success unless problems occur
  75. $this->success = true;
  76. // remember if any input was parsed
  77. $had_input = false;
  78. // open stream
  79. $f_in = fopen($path, "r");
  80. if (!$f_in) {
  81. $this->success = false;
  82. return;
  83. }
  84. // create namespace aware parser
  85. $xml_parser = xml_parser_create_ns("UTF-8", " ");
  86. // set tag and data handlers
  87. xml_set_element_handler($xml_parser,
  88. array(&$this, "_startElement"),
  89. array(&$this, "_endElement"));
  90. xml_set_character_data_handler($xml_parser,
  91. array(&$this, "_data"));
  92. // we want a case sensitive parser
  93. xml_parser_set_option($xml_parser,
  94. XML_OPTION_CASE_FOLDING, false);
  95. // parse input
  96. while($this->success && !feof($f_in)) {
  97. $line = fgets($f_in);
  98. if (is_string($line)) {
  99. $had_input = true;
  100. $this->success &= xml_parse($xml_parser, $line, false);
  101. }
  102. }
  103. // finish parsing
  104. if($had_input) {
  105. $this->success &= xml_parse($xml_parser, "", true);
  106. }
  107. // check if required tags where found
  108. $this->success &= !empty($this->locktype);
  109. $this->success &= !empty($this->lockscope);
  110. // free parser resource
  111. xml_parser_free($xml_parser);
  112. // close input stream
  113. fclose($f_in);
  114. }
  115. /**
  116. * tag start handler
  117. *
  118. * @param resource parser
  119. * @param string tag name
  120. * @param array tag attributes
  121. * @return void
  122. * @access private
  123. */
  124. function _startElement($parser, $name, $attrs)
  125. {
  126. // namespace handling
  127. if (strstr($name, " ")) {
  128. list($ns, $tag) = explode(" ", $name);
  129. } else {
  130. $ns = "";
  131. $tag = $name;
  132. }
  133. if ($this->collect_owner) {
  134. // everything within the <owner> tag needs to be collected
  135. $ns_short = "";
  136. $ns_attr = "";
  137. if ($ns) {
  138. if ($ns == "DAV:") {
  139. $ns_short = "D:";
  140. } else {
  141. $ns_attr = " xmlns='$ns'";
  142. }
  143. }
  144. $this->owner .= "<$ns_short$tag$ns_attr>";
  145. } else if ($ns == "DAV:") {
  146. // parse only the essential tags
  147. switch ($tag) {
  148. case "write":
  149. $this->locktype = $tag;
  150. break;
  151. case "exclusive":
  152. case "shared":
  153. $this->lockscope = $tag;
  154. break;
  155. case "owner":
  156. $this->collect_owner = true;
  157. break;
  158. }
  159. }
  160. }
  161. /**
  162. * data handler
  163. *
  164. * @param resource parser
  165. * @param string data
  166. * @return void
  167. * @access private
  168. */
  169. function _data($parser, $data)
  170. {
  171. // only the <owner> tag has data content
  172. if ($this->collect_owner) {
  173. $this->owner .= $data;
  174. }
  175. }
  176. /**
  177. * tag end handler
  178. *
  179. * @param resource parser
  180. * @param string tag name
  181. * @return void
  182. * @access private
  183. */
  184. function _endElement($parser, $name)
  185. {
  186. // namespace handling
  187. if (strstr($name, " ")) {
  188. list($ns, $tag) = explode(" ", $name);
  189. } else {
  190. $ns = "";
  191. $tag = $name;
  192. }
  193. // <owner> finished?
  194. if (($ns == "DAV:") && ($tag == "owner")) {
  195. $this->collect_owner = false;
  196. }
  197. // within <owner> we have to collect everything
  198. if ($this->collect_owner) {
  199. $ns_short = "";
  200. $ns_attr = "";
  201. if ($ns) {
  202. if ($ns == "DAV:") {
  203. $ns_short = "D:";
  204. } else {
  205. $ns_attr = " xmlns='$ns'";
  206. }
  207. }
  208. $this->owner .= "</$ns_short$tag$ns_attr>";
  209. }
  210. }
  211. }
  212. ?>