PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/system/application/filemanager/libraries/HTTP/WebDAV/Tools/_parse_lockinfo.php

https://github.com/ibnoe/Microweber
PHP | 251 lines | 106 code | 26 blank | 119 comment | 28 complexity | dd37396af7cd967841cc160788c90cb1 MD5 | raw file
  1. <?php // $Id: _parse_lockinfo.php 246152 2007-11-14 10:49:27Z hholzgra $
  2. /*
  3. +----------------------------------------------------------------------+
  4. | Copyright (c) 2002-2007 Christian Stocker, Hartmut Holzgraefe |
  5. | All rights reserved |
  6. | |
  7. | Redistribution and use in source and binary forms, with or without |
  8. | modification, are permitted provided that the following conditions |
  9. | are met: |
  10. | |
  11. | 1. Redistributions of source code must retain the above copyright |
  12. | notice, this list of conditions and the following disclaimer. |
  13. | 2. Redistributions in binary form must reproduce the above copyright |
  14. | notice, this list of conditions and the following disclaimer in |
  15. | the documentation and/or other materials provided with the |
  16. | distribution. |
  17. | 3. The names of the authors may not be used to endorse or promote |
  18. | products derived from this software without specific prior |
  19. | written permission. |
  20. | |
  21. | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  22. | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  23. | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
  24. | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
  25. | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
  26. | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  27. | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
  28. | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
  29. | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  30. | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
  31. | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
  32. | POSSIBILITY OF SUCH DAMAGE. |
  33. +----------------------------------------------------------------------+
  34. */
  35. /**
  36. * helper class for parsing LOCK request bodies
  37. *
  38. * @package HTTP_WebDAV_Server
  39. * @author Hartmut Holzgraefe <hholzgra@php.net>
  40. * @version @package-version@
  41. */
  42. class _parse_lockinfo
  43. {
  44. /**
  45. * success state flag
  46. *
  47. * @var bool
  48. * @access public
  49. */
  50. var $success = false;
  51. /**
  52. * lock type, currently only "write"
  53. *
  54. * @var string
  55. * @access public
  56. */
  57. var $locktype = "";
  58. /**
  59. * lock scope, "shared" or "exclusive"
  60. *
  61. * @var string
  62. * @access public
  63. */
  64. var $lockscope = "";
  65. /**
  66. * lock owner information
  67. *
  68. * @var string
  69. * @access public
  70. */
  71. var $owner = "";
  72. /**
  73. * flag that is set during lock owner read
  74. *
  75. * @var bool
  76. * @access private
  77. */
  78. var $collect_owner = false;
  79. /**
  80. * constructor
  81. *
  82. * @param string path of stream to read
  83. * @access public
  84. */
  85. function _parse_lockinfo($path)
  86. {
  87. // we assume success unless problems occur
  88. $this->success = true;
  89. // remember if any input was parsed
  90. $had_input = false;
  91. // open stream
  92. $f_in = fopen($path, "r");
  93. if (!$f_in) {
  94. $this->success = false;
  95. return;
  96. }
  97. // create namespace aware parser
  98. $xml_parser = xml_parser_create_ns("UTF-8", " ");
  99. // set tag and data handlers
  100. xml_set_element_handler($xml_parser,
  101. array(&$this, "_startElement"),
  102. array(&$this, "_endElement"));
  103. xml_set_character_data_handler($xml_parser,
  104. array(&$this, "_data"));
  105. // we want a case sensitive parser
  106. xml_parser_set_option($xml_parser,
  107. XML_OPTION_CASE_FOLDING, false);
  108. // parse input
  109. while ($this->success && !feof($f_in)) {
  110. $line = fgets($f_in);
  111. if (is_string($line)) {
  112. $had_input = true;
  113. $this->success &= xml_parse($xml_parser, $line, false);
  114. }
  115. }
  116. // finish parsing
  117. if ($had_input) {
  118. $this->success &= xml_parse($xml_parser, "", true);
  119. }
  120. // check if required tags where found
  121. $this->success &= !empty($this->locktype);
  122. $this->success &= !empty($this->lockscope);
  123. // free parser resource
  124. xml_parser_free($xml_parser);
  125. // close input stream
  126. fclose($f_in);
  127. }
  128. /**
  129. * tag start handler
  130. *
  131. * @param resource parser
  132. * @param string tag name
  133. * @param array tag attributes
  134. * @return void
  135. * @access private
  136. */
  137. function _startElement($parser, $name, $attrs)
  138. {
  139. // namespace handling
  140. if (strstr($name, " ")) {
  141. list($ns, $tag) = explode(" ", $name);
  142. } else {
  143. $ns = "";
  144. $tag = $name;
  145. }
  146. if ($this->collect_owner) {
  147. // everything within the <owner> tag needs to be collected
  148. $ns_short = "";
  149. $ns_attr = "";
  150. if ($ns) {
  151. if ($ns == "DAV:") {
  152. $ns_short = "D:";
  153. } else {
  154. $ns_attr = " xmlns='$ns'";
  155. }
  156. }
  157. $this->owner .= "<$ns_short$tag$ns_attr>";
  158. } else if ($ns == "DAV:") {
  159. // parse only the essential tags
  160. switch ($tag) {
  161. case "write":
  162. $this->locktype = $tag;
  163. break;
  164. case "exclusive":
  165. case "shared":
  166. $this->lockscope = $tag;
  167. break;
  168. case "owner":
  169. $this->collect_owner = true;
  170. break;
  171. }
  172. }
  173. }
  174. /**
  175. * data handler
  176. *
  177. * @param resource parser
  178. * @param string data
  179. * @return void
  180. * @access private
  181. */
  182. function _data($parser, $data)
  183. {
  184. // only the <owner> tag has data content
  185. if ($this->collect_owner) {
  186. $this->owner .= $data;
  187. }
  188. }
  189. /**
  190. * tag end handler
  191. *
  192. * @param resource parser
  193. * @param string tag name
  194. * @return void
  195. * @access private
  196. */
  197. function _endElement($parser, $name)
  198. {
  199. // namespace handling
  200. if (strstr($name, " ")) {
  201. list($ns, $tag) = explode(" ", $name);
  202. } else {
  203. $ns = "";
  204. $tag = $name;
  205. }
  206. // <owner> finished?
  207. if (($ns == "DAV:") && ($tag == "owner")) {
  208. $this->collect_owner = false;
  209. }
  210. // within <owner> we have to collect everything
  211. if ($this->collect_owner) {
  212. $ns_short = "";
  213. $ns_attr = "";
  214. if ($ns) {
  215. if ($ns == "DAV:") {
  216. $ns_short = "D:";
  217. } else {
  218. $ns_attr = " xmlns='$ns'";
  219. }
  220. }
  221. $this->owner .= "</$ns_short$tag$ns_attr>";
  222. }
  223. }
  224. }
  225. ?>