_parse_propfind.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_propfind.php,v 1.2 2004/01/05 12:33:22 hholzgra Exp $
  21. //
  22. /**
  23. * helper class for parsing PROPFIND request bodies
  24. *
  25. * @package HTTP_WebDAV_Server
  26. * @author Hartmut Holzgraefe <[email protected]>
  27. * @version 0.99.1dev
  28. */
  29. class _parse_propfind
  30. {
  31. /**
  32. * success state flag
  33. *
  34. * @var bool
  35. * @access public
  36. */
  37. var $success = false;
  38. /**
  39. * found properties are collected here
  40. *
  41. * @var array
  42. * @access public
  43. */
  44. var $props = false;
  45. /**
  46. * internal tag nesting depth counter
  47. *
  48. * @var int
  49. * @access private
  50. */
  51. var $depth = 0;
  52. /**
  53. * constructor
  54. *
  55. * @access public
  56. */
  57. function _parse_propfind($path)
  58. {
  59. // success state flag
  60. $this->success = true;
  61. // property storage array
  62. $this->props = array();
  63. // internal tag depth counter
  64. $this->depth = 0;
  65. // remember if any input was parsed
  66. $had_input = false;
  67. // open input stream
  68. $f_in = fopen($path, "r");
  69. if (!$f_in) {
  70. $this->success = false;
  71. return;
  72. }
  73. // create XML parser
  74. $xml_parser = xml_parser_create_ns("UTF-8", " ");
  75. // set tag and data handlers
  76. xml_set_element_handler($xml_parser,
  77. array(&$this, "_startElement"),
  78. array(&$this, "_endElement"));
  79. // we want a case sensitive parser
  80. xml_parser_set_option($xml_parser,
  81. XML_OPTION_CASE_FOLDING, false);
  82. // parse input
  83. while($this->success && !feof($f_in)) {
  84. $line = fgets($f_in);
  85. if (is_string($line)) {
  86. $had_input = true;
  87. $this->success &= xml_parse($xml_parser, $line, false);
  88. }
  89. }
  90. // finish parsing
  91. if($had_input) {
  92. $this->success &= xml_parse($xml_parser, "", true);
  93. }
  94. // free parser
  95. xml_parser_free($xml_parser);
  96. // close input stream
  97. fclose($f_in);
  98. // if no input was parsed it was a request
  99. if(!count($this->props)) $this->props = "all"; // default
  100. }
  101. /**
  102. * start tag handler
  103. *
  104. * @access private
  105. * @param resource parser
  106. * @param string tag name
  107. * @param array tag attributes
  108. */
  109. function _startElement($parser, $name, $attrs)
  110. {
  111. // name space handling
  112. if (strstr($name, " ")) {
  113. list($ns, $tag) = explode(" ", $name);
  114. if ($ns == "")
  115. $this->success = false;
  116. } else {
  117. $ns = "";
  118. $tag = $name;
  119. }
  120. // special tags at level 1: <allprop> and <propname>
  121. if ($this->depth == 1) {
  122. if ($tag == "allprop")
  123. $this->props = "all";
  124. if ($tag == "propname")
  125. $this->props = "names";
  126. }
  127. // requested properties are found at level 2
  128. if ($this->depth == 2) {
  129. $prop = array("name" => $tag);
  130. if ($ns)
  131. $prop["xmlns"] = $ns;
  132. $this->props[] = $prop;
  133. }
  134. // increment depth count
  135. $this->depth++;
  136. }
  137. /**
  138. * end tag handler
  139. *
  140. * @access private
  141. * @param resource parser
  142. * @param string tag name
  143. */
  144. function _endElement($parser, $name)
  145. {
  146. // here we only need to decrement the depth count
  147. $this->depth--;
  148. }
  149. }
  150. ?>