PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/pear/HTTP/WebDAV/Tools/_parse_propfind.php

https://github.com/nadavkav/Moodle-RTL--Shenkar-Translation-Team-
PHP | 191 lines | 67 code | 28 blank | 96 comment | 19 complexity | 69af20813ba63dd4efba63f11ab045a9 MD5 | raw file
  1. <?php // $Id: _parse_propfind.php,v 1.1.2.2 2008/02/27 02:50:21 martinlanghoff Exp $
  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 PROPFIND request bodies
  37. *
  38. * @package HTTP_WebDAV_Server
  39. * @author Hartmut Holzgraefe <hholzgra@php.net>
  40. * @version @package-version@
  41. */
  42. class _parse_propfind
  43. {
  44. /**
  45. * success state flag
  46. *
  47. * @var bool
  48. * @access public
  49. */
  50. var $success = false;
  51. /**
  52. * found properties are collected here
  53. *
  54. * @var array
  55. * @access public
  56. */
  57. var $props = false;
  58. /**
  59. * internal tag nesting depth counter
  60. *
  61. * @var int
  62. * @access private
  63. */
  64. var $depth = 0;
  65. /**
  66. * constructor
  67. *
  68. * @access public
  69. */
  70. function _parse_propfind($path)
  71. {
  72. // success state flag
  73. $this->success = true;
  74. // property storage array
  75. $this->props = array();
  76. // internal tag depth counter
  77. $this->depth = 0;
  78. // remember if any input was parsed
  79. $had_input = false;
  80. // open input stream
  81. $f_in = fopen($path, "r");
  82. if (!$f_in) {
  83. $this->success = false;
  84. return;
  85. }
  86. // create XML parser
  87. $xml_parser = xml_parser_create_ns("UTF-8", " ");
  88. // set tag and data handlers
  89. xml_set_element_handler($xml_parser,
  90. array(&$this, "_startElement"),
  91. array(&$this, "_endElement"));
  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. // free parser
  108. xml_parser_free($xml_parser);
  109. // close input stream
  110. fclose($f_in);
  111. // if no input was parsed it was a request
  112. if(!count($this->props)) $this->props = "all"; // default
  113. }
  114. /**
  115. * start tag handler
  116. *
  117. * @access private
  118. * @param resource parser
  119. * @param string tag name
  120. * @param array tag attributes
  121. */
  122. function _startElement($parser, $name, $attrs)
  123. {
  124. // name space handling
  125. if (strstr($name, " ")) {
  126. list($ns, $tag) = explode(" ", $name);
  127. if ($ns == "")
  128. $this->success = false;
  129. } else {
  130. $ns = "";
  131. $tag = $name;
  132. }
  133. // special tags at level 1: <allprop> and <propname>
  134. if ($this->depth == 1) {
  135. if ($tag == "allprop")
  136. $this->props = "all";
  137. if ($tag == "propname")
  138. $this->props = "names";
  139. }
  140. // requested properties are found at level 2
  141. if ($this->depth == 2) {
  142. $prop = array("name" => $tag);
  143. if ($ns)
  144. $prop["xmlns"] = $ns;
  145. $this->props[] = $prop;
  146. }
  147. // increment depth count
  148. $this->depth++;
  149. }
  150. /**
  151. * end tag handler
  152. *
  153. * @access private
  154. * @param resource parser
  155. * @param string tag name
  156. */
  157. function _endElement($parser, $name)
  158. {
  159. // here we only need to decrement the depth count
  160. $this->depth--;
  161. }
  162. }
  163. ?>