[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/pear/HTTP/WebDAV/Tools/ -> _parse_propfind.php (source)

   1  <?php // $Id$
   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  /**
  37   * helper class for parsing PROPFIND request bodies
  38   * 
  39   * @package HTTP_WebDAV_Server
  40   * @author Hartmut Holzgraefe <[email protected]>
  41   * @version @package-version@
  42   */
  43  class _parse_propfind 
  44  {
  45      /**
  46       * success state flag
  47       *
  48       * @var bool
  49       * @access public
  50       */
  51      var $success = false;
  52  
  53      /**
  54       * found properties are collected here
  55       *
  56       * @var array
  57       * @access public
  58       */
  59      var $props = false;
  60  
  61      /**
  62       * internal tag nesting depth counter
  63       *
  64       * @var int
  65       * @access private
  66       */
  67      var $depth = 0;
  68  
  69      
  70      /**
  71       * constructor
  72       *
  73       * @access public
  74       */
  75      function _parse_propfind($path) 
  76      {
  77          // success state flag
  78          $this->success = true;
  79          
  80          // property storage array
  81          $this->props = array();
  82  
  83          // internal tag depth counter
  84          $this->depth = 0;
  85  
  86          // remember if any input was parsed
  87          $had_input = false;
  88  
  89          // open input stream
  90          $f_in = fopen($path, "r");
  91          if (!$f_in) {
  92              $this->success = false;
  93              return;
  94          }
  95  
  96          // create XML parser
  97          $xml_parser = xml_parser_create_ns("UTF-8", " ");
  98  
  99          // set tag and data handlers
 100          xml_set_element_handler($xml_parser,
 101                                  array(&$this, "_startElement"),
 102                                  array(&$this, "_endElement"));
 103  
 104          // we want a case sensitive parser
 105          xml_parser_set_option($xml_parser, 
 106                                XML_OPTION_CASE_FOLDING, false);
 107  
 108  
 109          // parse input
 110          while ($this->success && !feof($f_in)) {
 111              $line = fgets($f_in);
 112              if (is_string($line)) {
 113                  $had_input = true;
 114                  $this->success &= xml_parse($xml_parser, $line, false);
 115              }
 116          } 
 117          
 118          // finish parsing
 119          if ($had_input) {
 120              $this->success &= xml_parse($xml_parser, "", true);
 121          }
 122  
 123          // free parser
 124          xml_parser_free($xml_parser);
 125          
 126          // close input stream
 127          fclose($f_in);
 128  
 129          // if no input was parsed it was a request
 130          if(!count($this->props)) $this->props = "all"; // default
 131      }
 132      
 133  
 134      /**
 135       * start tag handler
 136       * 
 137       * @access private
 138       * @param  resource  parser
 139       * @param  string    tag name
 140       * @param  array     tag attributes
 141       */
 142      function _startElement($parser, $name, $attrs) 
 143      {
 144          // name space handling
 145          if (strstr($name, " ")) {
 146              list($ns, $tag) = explode(" ", $name);
 147              if ($ns == "")
 148                  $this->success = false;
 149          } else {
 150              $ns  = "";
 151              $tag = $name;
 152          }
 153  
 154          // special tags at level 1: <allprop> and <propname>
 155          if ($this->depth == 1) {
 156              if ($tag == "allprop")
 157                  $this->props = "all";
 158  
 159              if ($tag == "propname")
 160                  $this->props = "names";
 161          }
 162  
 163          // requested properties are found at level 2
 164          if ($this->depth == 2) {
 165              $prop = array("name" => $tag);
 166              if ($ns)
 167                  $prop["xmlns"] = $ns;
 168              $this->props[] = $prop;
 169          }
 170  
 171          // increment depth count
 172          $this->depth++;
 173      }
 174      
 175  
 176      /**
 177       * end tag handler
 178       * 
 179       * @access private
 180       * @param  resource  parser
 181       * @param  string    tag name
 182       */
 183      function _endElement($parser, $name) 
 184      {
 185          // here we only need to decrement the depth count
 186          $this->depth--;
 187      }
 188  }
 189  
 190  
 191  ?>


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1