/Class/Dav/_parse_lockinfo.php

https://github.com/CircleCode/dynacase-core · PHP · 224 lines · 108 code · 3 blank · 113 comment · 28 complexity · f31c549c1397a578eb917d126f4cb0cb MD5 · raw file

  1. <?php
  2. /*
  3. * @author Anakeen
  4. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
  5. * @package FDL
  6. */
  7. /**
  8. * helper class for parsing LOCK request bodies
  9. *
  10. * @package FDL
  11. * @author Hartmut Holzgraefe <hholzgra@php.net>
  12. * @version @package-version@
  13. */
  14. /**
  15. */
  16. //
  17. // +----------------------------------------------------------------------+
  18. // | PHP Version 4 |
  19. // +----------------------------------------------------------------------+
  20. // | Copyright (c) 1997-2003 The PHP Group |
  21. // +----------------------------------------------------------------------+
  22. // | This source file is subject to version 2.02 of the PHP license, |
  23. // | that is bundled with this package in the file LICENSE, and is |
  24. // | available at through the world-wide-web at |
  25. // | http://www.php.net/license/2_02.txt. |
  26. // | If you did not receive a copy of the PHP license and are unable to |
  27. // | obtain it through the world-wide-web, please send a note to |
  28. // | license@php.net so we can mail you a copy immediately. |
  29. // +----------------------------------------------------------------------+
  30. // | Authors: Hartmut Holzgraefe <hholzgra@php.net> |
  31. // | Christian Stocker <chregu@bitflux.ch> |
  32. // +----------------------------------------------------------------------+
  33. //
  34. // $Id: _parse_lockinfo.php,v 1.1 2006/11/22 10:33:59 eric Exp $
  35. //
  36. class _parse_lockinfo
  37. {
  38. /**
  39. * success state flag
  40. *
  41. * @var bool
  42. * @access public
  43. */
  44. var $success = false;
  45. /**
  46. * lock type, currently only "write"
  47. *
  48. * @var string
  49. * @access public
  50. */
  51. var $locktype = "";
  52. /**
  53. * lock scope, "shared" or "exclusive"
  54. *
  55. * @var string
  56. * @access public
  57. */
  58. var $lockscope = "";
  59. /**
  60. * lock owner information
  61. *
  62. * @var string
  63. * @access public
  64. */
  65. var $owner = "";
  66. /**
  67. * flag that is set during lock owner read
  68. *
  69. * @var bool
  70. * @access private
  71. */
  72. var $collect_owner = false;
  73. /**
  74. * constructor
  75. *
  76. * @param string path of stream to read
  77. * @access public
  78. */
  79. function _parse_lockinfo($path)
  80. {
  81. // we assume success unless problems occur
  82. $this->success = true;
  83. // remember if any input was parsed
  84. $had_input = false;
  85. // open stream
  86. $f_in = fopen($path, "r");
  87. if (!$f_in) {
  88. $this->success = false;
  89. return;
  90. }
  91. // create namespace aware parser
  92. $xml_parser = xml_parser_create_ns("UTF-8", " ");
  93. // set tag and data handlers
  94. xml_set_element_handler($xml_parser, array(&$this,
  95. "_startElement"
  96. ) , array(&$this,
  97. "_endElement"
  98. ));
  99. xml_set_character_data_handler($xml_parser, array(&$this,
  100. "_data"
  101. ));
  102. // we want a case sensitive parser
  103. xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
  104. // parse input
  105. while ($this->success && !feof($f_in)) {
  106. $line = fgets($f_in);
  107. if (is_string($line)) {
  108. $had_input = true;
  109. $this->success&= xml_parse($xml_parser, $line, false);
  110. }
  111. }
  112. // finish parsing
  113. if ($had_input) {
  114. $this->success&= xml_parse($xml_parser, "", true);
  115. }
  116. // check if required tags where found
  117. $this->success&= !empty($this->locktype);
  118. $this->success&= !empty($this->lockscope);
  119. // free parser resource
  120. xml_parser_free($xml_parser);
  121. // close input stream
  122. fclose($f_in);
  123. }
  124. /**
  125. * tag start handler
  126. *
  127. * @param resource parser
  128. * @param string tag name
  129. * @param array tag attributes
  130. * @return void
  131. * @access private
  132. */
  133. function _startElement($parser, $name, $attrs)
  134. {
  135. // namespace handling
  136. if (strstr($name, " ")) {
  137. list($ns, $tag) = explode(" ", $name);
  138. } else {
  139. $ns = "";
  140. $tag = $name;
  141. }
  142. if ($this->collect_owner) {
  143. // everything within the <owner> tag needs to be collected
  144. $ns_short = "";
  145. $ns_attr = "";
  146. if ($ns) {
  147. if ($ns == "DAV:") {
  148. $ns_short = "D:";
  149. } else {
  150. $ns_attr = " xmlns='$ns'";
  151. }
  152. }
  153. $this->owner.= "<$ns_short$tag$ns_attr>";
  154. } else if ($ns == "DAV:") {
  155. // parse only the essential tags
  156. switch ($tag) {
  157. case "write":
  158. $this->locktype = $tag;
  159. break;
  160. case "exclusive":
  161. case "shared":
  162. $this->lockscope = $tag;
  163. break;
  164. case "owner":
  165. $this->collect_owner = true;
  166. break;
  167. }
  168. }
  169. }
  170. /**
  171. * data handler
  172. *
  173. * @param resource parser
  174. * @param string data
  175. * @return void
  176. * @access private
  177. */
  178. function _data($parser, $data)
  179. {
  180. // only the <owner> tag has data content
  181. if ($this->collect_owner) {
  182. $this->owner.= $data;
  183. }
  184. }
  185. /**
  186. * tag end handler
  187. *
  188. * @param resource parser
  189. * @param string tag name
  190. * @return void
  191. * @access private
  192. */
  193. function _endElement($parser, $name)
  194. {
  195. // namespace handling
  196. if (strstr($name, " ")) {
  197. list($ns, $tag) = explode(" ", $name);
  198. } else {
  199. $ns = "";
  200. $tag = $name;
  201. }
  202. // <owner> finished?
  203. if (($ns == "DAV:") && ($tag == "owner")) {
  204. $this->collect_owner = false;
  205. }
  206. // within <owner> we have to collect everything
  207. if ($this->collect_owner) {
  208. $ns_short = "";
  209. $ns_attr = "";
  210. if ($ns) {
  211. if ($ns == "DAV:") {
  212. $ns_short = "D:";
  213. } else {
  214. $ns_attr = " xmlns='$ns'";
  215. }
  216. }
  217. $this->owner.= "</$ns_short$tag$ns_attr>";
  218. }
  219. }
  220. }
  221. ?>