/include/HTTP_WebDAV_Server/Tools/_parse_lockinfo.php

https://github.com/jacknicole/sugarcrm_dev · PHP · 237 lines · 106 code · 26 blank · 105 comment · 27 complexity · bf062d20864aa65ad832b96961b90b01 MD5 · raw file

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