HTTP_WebDAV_Server
[ class tree: HTTP_WebDAV_Server ] [ index: HTTP_WebDAV_Server ] [ all elements ]

Source for file Server.php

Documentation is available at Server.php

  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. require_once "HTTP/WebDAV/Tools/_parse_propfind.php";
  37. require_once "HTTP/WebDAV/Tools/_parse_proppatch.php";
  38. require_once "HTTP/WebDAV/Tools/_parse_lockinfo.php";
  39.  
  40. /**
  41.  * Virtual base class for implementing WebDAV servers
  42.  *
  43.  * WebDAV server base class, needs to be extended to do useful work
  44.  * 
  45.  * @package HTTP_WebDAV_Server
  46.  * @author  Hartmut Holzgraefe <hholzgra@php.net>
  47.  * @version @package_version@
  48.  */
  49. {
  50.     // {{{ Member Variables 
  51.     
  52.     /**
  53.      * complete URI for this request
  54.      *
  55.      * @var string 
  56.      */
  57.     var $uri;
  58.     
  59.     
  60.     /**
  61.      * base URI for this request
  62.      *
  63.      * @var string 
  64.      */
  65.     var $base_uri;
  66.  
  67.  
  68.     /**
  69.      * URI path for this request
  70.      *
  71.      * @var string 
  72.      */
  73.     var $path;
  74.  
  75.     /**
  76.      * Realm string to be used in authentification popups
  77.      *
  78.      * @var string 
  79.      */
  80.     var $http_auth_realm = "PHP WebDAV";
  81.  
  82.     /**
  83.      * String to be used in "X-Dav-Powered-By" header
  84.      *
  85.      * @var string 
  86.      */
  87.     var $dav_powered_by = "";
  88.  
  89.     /**
  90.      * Remember parsed If: (RFC2518/9.4) header conditions
  91.      *
  92.      * @var array 
  93.      */
  94.     var $_if_header_uris = array();
  95.  
  96.     /**
  97.      * HTTP response status/message
  98.      *
  99.      * @var string 
  100.      */
  101.     var $_http_status "200 OK";
  102.  
  103.     /**
  104.      * encoding of property values passed in
  105.      *
  106.      * @var string 
  107.      */
  108.     var $_prop_encoding "utf-8";
  109.  
  110.     /**
  111.      * Copy of $_SERVER superglobal array
  112.      *
  113.      * Derived classes may extend the constructor to
  114.      * modify its contents
  115.      *
  116.      * @var array 
  117.      */
  118.     var $_SERVER;
  119.  
  120.     // }}}
  121.  
  122.     // {{{ Constructor 
  123.  
  124.     /** 
  125.      * Constructor
  126.      *
  127.      * @param void 
  128.      */
  129.     function HTTP_WebDAV_Server(
  130.     {
  131.         // PHP messages destroy XML output -> switch them off
  132.         ini_set("display_errors"0);
  133.  
  134.         // copy $_SERVER variables to local _SERVER array
  135.         // so that derived classes can simply modify these
  136.         $this->_SERVER $_SERVER;
  137.     }
  138.  
  139.     // }}}
  140.  
  141.     // {{{ ServeRequest() 
  142.     /** 
  143.      * Serve WebDAV HTTP request
  144.      *
  145.      * dispatch WebDAV HTTP request to the apropriate method handler
  146.      * 
  147.      * @param  void 
  148.      * @return void 
  149.      */
  150.     function ServeRequest(
  151.     {
  152.         // prevent warning in litmus check 'delete_fragment'
  153.         if (strstr($this->_SERVER["REQUEST_URI"]'#')) {
  154.             $this->http_status("400 Bad Request");
  155.             return;
  156.         }
  157.  
  158.         // default uri is the complete request uri
  159.         $uri "http";
  160.         if (isset($this->_SERVER["HTTPS"]&& $this->_SERVER["HTTPS"=== "on"{
  161.           $uri "https";
  162.         }
  163.         $uri.= "://".$this->_SERVER["HTTP_HOST"].$this->_SERVER["SCRIPT_NAME"];
  164.         
  165.         // WebDAV has no concept of a query string and clients (including cadaver)
  166.         // seem to pass '?' unencoded, so we need to extract the path info out
  167.         // of the request URI ourselves
  168.         $path_info substr($this->_SERVER["REQUEST_URI"]strlen($this->_SERVER["SCRIPT_NAME"]));
  169.  
  170.         // just in case the path came in empty ...
  171.         if (empty($path_info)) {
  172.             $path_info "/";
  173.         }
  174.  
  175.         $this->base_uri = $uri;
  176.         $this->uri      = $uri $path_info;
  177.  
  178.         // set path
  179.         $this->path = $this->_urldecode($path_info);
  180.         if (!strlen($this->path)) {
  181.             if ($this->_SERVER["REQUEST_METHOD"== "GET"{
  182.                 // redirect clients that try to GET a collection
  183.                 // WebDAV clients should never try this while
  184.                 // regular HTTP clients might ...
  185.                 header("Location: ".$this->base_uri."/");
  186.                 return;
  187.             else {
  188.                 // if a WebDAV client didn't give a path we just assume '/'
  189.                 $this->path = "/";
  190.             }
  191.         
  192.         
  193.         if (ini_get("magic_quotes_gpc")) {
  194.             $this->path = stripslashes($this->path);
  195.         }
  196.         
  197.         
  198.         // identify ourselves
  199.         if (empty($this->dav_powered_by)) {
  200.             header("X-Dav-Powered-By: PHP class: ".get_class($this));
  201.         else {
  202.             header("X-Dav-Powered-By: ".$this->dav_powered_by);
  203.         }
  204.  
  205.         // check authentication
  206.         // for the motivation for not checking OPTIONS requests on / see 
  207.         // http://pear.php.net/bugs/bug.php?id=5363
  208.         if ( (   !(($this->_SERVER['REQUEST_METHOD'== 'OPTIONS'&& ($this->path == "/")))
  209.              && (!$this->_check_auth())) {
  210.             // RFC2518 says we must use Digest instead of Basic
  211.             // but Microsoft Clients do not support Digest
  212.             // and we don't support NTLM and Kerberos
  213.             // so we are stuck with Basic here
  214.             header('WWW-Authenticate: Basic realm="'.($this->http_auth_realm).'"');
  215.  
  216.             // Windows seems to require this being the last header sent
  217.             // (changed according to PECL bug #3138)
  218.             $this->http_status('401 Unauthorized');
  219.  
  220.             return;
  221.         }
  222.         
  223.         // check 
  224.         if ($this->_check_if_header_conditions()) {
  225.             return;
  226.         }
  227.         
  228.         // detect requested method names
  229.         $method  strtolower($this->_SERVER["REQUEST_METHOD"]);
  230.         $wrapper "http_".$method;
  231.         
  232.         // activate HEAD emulation by GET if no HEAD method found
  233.         if ($method == "head" && !method_exists($this"head")) {
  234.             $method "get";
  235.         }
  236.         
  237.         if (method_exists($this$wrapper&& ($method == "options" || method_exists($this$method))) {
  238.             $this->$wrapper();  // call method by name
  239.         else // method not found/implemented
  240.             if ($this->_SERVER["REQUEST_METHOD"== "LOCK"{
  241.                 $this->http_status("412 Precondition failed");
  242.             else {
  243.                 $this->http_status("405 Method not allowed");
  244.                 header("Allow: ".join(", "$this->_allow()));  // tell client what's allowed
  245.             }
  246.         }
  247.     }
  248.  
  249.     // }}}
  250.  
  251.     // {{{ abstract WebDAV methods 
  252.  
  253.     // {{{ GET() 
  254.     /**
  255.      * GET implementation
  256.      *
  257.      * overload this method to retrieve resources from your server
  258.      * <br>
  259.      * 
  260.      *
  261.      * @abstract
  262.      * @param array &$params Array of input and output parameters
  263.      *  <br><b>input</b><ul>
  264.      *  <li> path -
  265.      *  </ul>
  266.      *  <br><b>output</b><ul>
  267.      *  <li> size -
  268.      *  </ul>
  269.      * @returns int HTTP-Statuscode
  270.      */
  271.  
  272.     /* abstract
  273.      function GET(&$params) 
  274.      {
  275.      // dummy entry for PHPDoc
  276.      } 
  277.     */
  278.  
  279.     // }}}
  280.  
  281.     // {{{ PUT() 
  282.     /**
  283.      * PUT implementation
  284.      *
  285.      * PUT implementation
  286.      *
  287.      * @abstract
  288.      * @param array &$params 
  289.      * @returns int HTTP-Statuscode
  290.      */
  291.     
  292.     /* abstract
  293.      function PUT() 
  294.      {
  295.      // dummy entry for PHPDoc
  296.      } 
  297.     */
  298.     
  299.     // }}}
  300.  
  301.     // {{{ COPY() 
  302.  
  303.     /**
  304.      * COPY implementation
  305.      *
  306.      * COPY implementation
  307.      *
  308.      * @abstract
  309.      * @param array &$params 
  310.      * @returns int HTTP-Statuscode
  311.      */
  312.     
  313.     /* abstract
  314.      function COPY() 
  315.      {
  316.      // dummy entry for PHPDoc
  317.      } 
  318.     */
  319.  
  320.     // }}}
  321.  
  322.     // {{{ MOVE() 
  323.  
  324.     /**
  325.      * MOVE implementation
  326.      *
  327.      * MOVE implementation
  328.      *
  329.      * @abstract
  330.      * @param array &$params 
  331.      * @returns int HTTP-Statuscode
  332.      */
  333.     
  334.     /* abstract
  335.      function MOVE() 
  336.      {
  337.      // dummy entry for PHPDoc
  338.      } 
  339.     */
  340.  
  341.     // }}}
  342.  
  343.     // {{{ DELETE() 
  344.  
  345.     /**
  346.      * DELETE implementation
  347.      *
  348.      * DELETE implementation
  349.      *
  350.      * @abstract
  351.      * @param array &$params 
  352.      * @returns int HTTP-Statuscode
  353.      */
  354.     
  355.     /* abstract
  356.      function DELETE() 
  357.      {
  358.      // dummy entry for PHPDoc
  359.      } 
  360.     */
  361.     // }}}
  362.  
  363.     // {{{ PROPFIND() 
  364.  
  365.     /**
  366.      * PROPFIND implementation
  367.      *
  368.      * PROPFIND implementation
  369.      *
  370.      * @abstract
  371.      * @param array &$params 
  372.      * @returns int HTTP-Statuscode
  373.      */
  374.     
  375.     /* abstract
  376.      function PROPFIND() 
  377.      {
  378.      // dummy entry for PHPDoc
  379.      } 
  380.     */
  381.  
  382.     // }}}
  383.  
  384.     // {{{ PROPPATCH() 
  385.  
  386.     /**
  387.      * PROPPATCH implementation
  388.      *
  389.      * PROPPATCH implementation
  390.      *
  391.      * @abstract
  392.      * @param array &$params 
  393.      * @returns int HTTP-Statuscode
  394.      */
  395.     
  396.     /* abstract
  397.      function PROPPATCH() 
  398.      {
  399.      // dummy entry for PHPDoc
  400.      } 
  401.     */
  402.     // }}}
  403.  
  404.     // {{{ LOCK() 
  405.  
  406.     /**
  407.      * LOCK implementation
  408.      *
  409.      * LOCK implementation
  410.      *
  411.      * @abstract
  412.      * @param array &$params 
  413.      * @returns int HTTP-Statuscode
  414.      */
  415.     
  416.     /* abstract
  417.      function LOCK() 
  418.      {
  419.      // dummy entry for PHPDoc
  420.      } 
  421.     */
  422.     // }}}
  423.  
  424.     // {{{ UNLOCK() 
  425.  
  426.     /**
  427.      * UNLOCK implementation
  428.      *
  429.      * UNLOCK implementation
  430.      *
  431.      * @abstract
  432.      * @param array &$params 
  433.      * @returns int HTTP-Statuscode
  434.      */
  435.  
  436.     /* abstract
  437.      function UNLOCK() 
  438.      {
  439.      // dummy entry for PHPDoc
  440.      } 
  441.     */
  442.     // }}}
  443.  
  444.     // }}}
  445.  
  446.     // {{{ other abstract methods 
  447.  
  448.     // {{{ check_auth() 
  449.  
  450.     /**
  451.      * check authentication
  452.      *
  453.      * overload this method to retrieve and confirm authentication information
  454.      *
  455.      * @abstract
  456.      * @param string type Authentication type, e.g. "basic" or "digest"
  457.      * @param string username Transmitted username
  458.      * @param string passwort Transmitted password
  459.      * @returns bool Authentication status
  460.      */
  461.     
  462.     /* abstract
  463.      function checkAuth($type, $username, $password) 
  464.      {
  465.      // dummy entry for PHPDoc
  466.      } 
  467.     */
  468.     
  469.     // }}}
  470.  
  471.     // {{{ checklock() 
  472.  
  473.     /**
  474.      * check lock status for a resource
  475.      *
  476.      * overload this method to return shared and exclusive locks
  477.      * active for this resource
  478.      *
  479.      * @abstract
  480.      * @param string resource Resource path to check
  481.      * @returns array An array of lock entries each consisting
  482.      *                 of 'type' ('shared'/'exclusive'), 'token' and 'timeout'
  483.      */
  484.     
  485.     /* abstract
  486.      function checklock($resource) 
  487.      {
  488.      // dummy entry for PHPDoc
  489.      } 
  490.     */
  491.  
  492.     // }}}
  493.  
  494.     // }}}
  495.  
  496.     // {{{ WebDAV HTTP method wrappers 
  497.  
  498.     // {{{ http_OPTIONS() 
  499.  
  500.     /**
  501.      * OPTIONS method handler
  502.      *
  503.      * The OPTIONS method handler creates a valid OPTIONS reply
  504.      * including Dav: and Allowed: headers
  505.      * based on the implemented methods found in the actual instance
  506.      *
  507.      * @param  void 
  508.      * @return void 
  509.      */
  510.     function http_OPTIONS(
  511.     {
  512.         // Microsoft clients default to the Frontpage protocol 
  513.         // unless we tell them to use WebDAV
  514.         header("MS-Author-Via: DAV");
  515.  
  516.         // get allowed methods
  517.         $allow $this->_allow();
  518.  
  519.         // dav header
  520.         $dav = array(1);        // assume we are always dav class 1 compliant
  521.         if (isset($allow['LOCK'])) {
  522.             $dav[= 2;         // dav class 2 requires that locking is supported 
  523.         }
  524.  
  525.         // tell clients what we found
  526.         $this->http_status("200 OK");
  527.         header("DAV: "  .join(", "$dav));
  528.         header("Allow: ".join(", "$allow));
  529.  
  530.         header("Content-length: 0");
  531.     }
  532.  
  533.     // }}}
  534.  
  535.  
  536.     // {{{ http_PROPFIND() 
  537.  
  538.     /**
  539.      * PROPFIND method handler
  540.      *
  541.      * @param  void 
  542.      * @return void 
  543.      */
  544.     function http_PROPFIND(
  545.     {
  546.         $options = Array();
  547.         $files   = Array();
  548.  
  549.         $options["path"$this->path;
  550.         
  551.         // search depth from header (default is "infinity)
  552.         if (isset($this->_SERVER['HTTP_DEPTH'])) {
  553.             $options["depth"$this->_SERVER["HTTP_DEPTH"];
  554.         else {
  555.             $options["depth""infinity";
  556.         }       
  557.  
  558.         // analyze request payload
  559.         $propinfo = new _parse_propfind("php://input");
  560.         if (!$propinfo->success{
  561.             $this->http_status("400 Error");
  562.             return;
  563.         }
  564.         $options['props'$propinfo->props;
  565.  
  566.         // call user handler
  567.         if (!$this->PROPFIND($options$files)) {
  568.             $files = array("files" => array());
  569.             if (method_exists($this"checkLock")) {
  570.                 // is locked?
  571.                 $lock $this->checkLock($this->path);
  572.  
  573.                 if (is_array($lock&& count($lock)) {
  574.                     $created          = isset($lock['created'])  $lock['created']  time();
  575.                     $modified         = isset($lock['modified']$lock['modified'time();
  576.                     $files['files'][= array("path"  => $this->_slashify($this->path),
  577.                                               "props" => array($this->mkprop("displayname",      $this->path),
  578.                                                                $this->mkprop("creationdate",     $created),
  579.                                                                $this->mkprop("getlastmodified",  $modified),
  580.                                                                $this->mkprop("resourcetype",     ""),
  581.                                                                $this->mkprop("getcontenttype",   ""),
  582.                                                                $this->mkprop("getcontentlength"0))
  583.                                               );
  584.                 }
  585.             }
  586.  
  587.             if (empty($files['files'])) {
  588.                 $this->http_status("404 Not Found");
  589.                 return;
  590.             }
  591.         }
  592.         
  593.         // collect namespaces here
  594.         $ns_hash = array();
  595.         
  596.         // Microsoft Clients need this special namespace for date and time values
  597.         $ns_defs "xmlns:ns0=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\"";    
  598.     
  599.         // now we loop over all returned file entries
  600.         foreach ($files["files"as $filekey => $file{
  601.             
  602.             // nothing to do if no properties were returend for a file
  603.             if (!isset($file["props"]|| !is_array($file["props"])) {
  604.                 continue;
  605.             }
  606.             
  607.             // now loop over all returned properties
  608.             foreach ($file["props"as $key => $prop{
  609.                 // as a convenience feature we do not require that user handlers
  610.                 // restrict returned properties to the requested ones
  611.                 // here we strip all unrequested entries out of the response
  612.                 
  613.                 switch($options['props']{
  614.                 case "all":
  615.                     // nothing to remove
  616.                     break;
  617.                     
  618.                 case "names":
  619.                     // only the names of all existing properties were requested
  620.                     // so we remove all values
  621.                     unset($files["files"][$filekey]["props"][$key]["val"]);
  622.                     break;
  623.                     
  624.                 default:
  625.                     $found = false;
  626.                     
  627.                     // search property name in requested properties 
  628.                     foreach ((array)$options["props"as $reqprop{
  629.                         if (!isset($reqprop["xmlns"])) {
  630.                             $reqprop["xmlns""";
  631.                         }
  632.                         if (   $reqprop["name"]  == $prop["name"
  633.                                && $reqprop["xmlns"== $prop["ns"]{
  634.                             $found = true;
  635.                             break;
  636.                         }
  637.                     }
  638.                     
  639.                     // unset property and continue with next one if not found/requested
  640.                     if (!$found{
  641.                         $files["files"][$filekey]["props"][$key]="";
  642.                         continue(2);
  643.                     }
  644.                     break;
  645.                 }
  646.                 
  647.                 // namespace handling 
  648.                 if (empty($prop["ns"])) continue; // no namespace
  649.                 $ns $prop["ns"]
  650.                 if ($ns == "DAV:"continue; // default namespace
  651.                 if (isset($ns_hash[$ns])) continue; // already known
  652.  
  653.                 // register namespace 
  654.                 $ns_name "ns".(count($ns_hash+ 1);
  655.                 $ns_hash[$ns$ns_name;
  656.                 $ns_defs .= " xmlns:$ns_name=\"$ns\"";
  657.             }
  658.         
  659.             // we also need to add empty entries for properties that were requested
  660.             // but for which no values where returned by the user handler
  661.             if (is_array($options['props'])) {
  662.                 foreach ($options["props"as $reqprop{
  663.                     if ($reqprop['name']==""continue; // skip empty entries
  664.                     
  665.                     $found = false;
  666.                     
  667.                     if (!isset($reqprop["xmlns"])) {
  668.                         $reqprop["xmlns""";
  669.                     }
  670.  
  671.                     // check if property exists in result
  672.                     foreach ($file["props"as $prop{
  673.                         if (   $reqprop["name"]  == $prop["name"]
  674.                                && $reqprop["xmlns"== $prop["ns"]{
  675.                             $found = true;
  676.                             break;
  677.                         }
  678.                     }
  679.                     
  680.                     if (!$found{
  681.                         if ($reqprop["xmlns"]==="DAV:" && $reqprop["name"]==="lockdiscovery"{
  682.                             // lockdiscovery is handled by the base class
  683.                             $files["files"][$filekey]["props"][
  684.                                 = $this->mkprop("DAV:"
  685.                                                 "lockdiscovery"
  686.                                                 $this->lockdiscovery($files["files"][$filekey]['path']));
  687.                         else {
  688.                             // add empty value for this property
  689.                             $files["files"][$filekey]["noprops"][=
  690.                                 $this->mkprop($reqprop["xmlns"]$reqprop["name"]"");
  691.  
  692.                             // register property namespace if not known yet
  693.                             if ($reqprop["xmlns"!= "DAV:" && !isset($ns_hash[$reqprop["xmlns"]])) {
  694.                                 $ns_name "ns".(count($ns_hash+ 1);
  695.                                 $ns_hash[$reqprop["xmlns"]] $ns_name;
  696.                                 $ns_defs .= " xmlns:$ns_name=\"$reqprop[xmlns]\"";
  697.                             }
  698.                         }
  699.                     }
  700.                 }
  701.             }
  702.         }
  703.         
  704.         // now we generate the reply header ...
  705.         $this->http_status("207 Multi-Status");
  706.         header('Content-Type: text/xml; charset="utf-8"');
  707.         
  708.         // ... and payload
  709.         echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  710.         echo "<D:multistatus xmlns:D=\"DAV:\">\n";
  711.             
  712.         foreach ($files["files"as $file{
  713.             // ignore empty or incomplete entries
  714.             if (!is_array($file|| empty($file|| !isset($file["path"])) continue;
  715.             $path $file['path'];                  
  716.             if (!is_string($path|| $path===""continue;
  717.  
  718.             echo " <D:response $ns_defs>\n";
  719.         
  720.             /* TODO right now the user implementation has to make sure
  721.              collections end in a slash, this should be done in here
  722.              by checking the resource attribute */
  723.             $href $this->_mergePaths($this->_SERVER['SCRIPT_NAME']$path);
  724.  
  725.             /* minimal urlencoding is needed for the resource path */
  726.             $href $this->_urlencode($href);
  727.         
  728.             echo "  <D:href>$href</D:href>\n";
  729.         
  730.             // report all found properties and their values (if any)
  731.             if (isset($file["props"]&& is_array($file["props"])) {
  732.                 echo "  <D:propstat>\n";
  733.                 echo "   <D:prop>\n";
  734.  
  735.                 foreach ($file["props"as $key => $prop{
  736.                     
  737.                     if (!is_array($prop)) continue;
  738.                     if (!isset($prop["name"])) continue;
  739.                     
  740.                     if (!isset($prop["val"]|| $prop["val"=== "" || $prop["val"=== false{
  741.                         // empty properties (cannot use empty() for check as "0" is a legal value here)
  742.                         if ($prop["ns"]=="DAV:"{
  743.                             echo "     <D:$prop[name]/>\n";
  744.                         else if (!empty($prop["ns"])) {
  745.                             echo "     <".$ns_hash[$prop["ns"]].":$prop[name]/>\n";
  746.                         else {
  747.                             echo "     <$prop[name] xmlns=\"\"/>";
  748.                         }
  749.                     else if ($prop["ns"== "DAV:"{
  750.                         // some WebDAV properties need special treatment
  751.                         switch ($prop["name"]{
  752.                         case "creationdate":
  753.                             echo "     <D:creationdate ns0:dt=\"dateTime.tz\">"
  754.                                 . gmdate("Y-m-d\\TH:i:s\\Z"$prop['val'])
  755.                                 . "</D:creationdate>\n";
  756.                             break;
  757.                         case "getlastmodified":
  758.                             echo "     <D:getlastmodified ns0:dt=\"dateTime.rfc1123\">"
  759.                                 . gmdate("D, d M Y H:i:s "$prop['val'])
  760.                                 . "GMT</D:getlastmodified>\n";
  761.                             break;
  762.                         case "resourcetype":
  763.                             echo "     <D:resourcetype><D:$prop[val]/></D:resourcetype>\n";
  764.                             break;
  765.                         case "supportedlock":
  766.                             echo "     <D:supportedlock>$prop[val]</D:supportedlock>\n";
  767.                             break;
  768.                         case "lockdiscovery":  
  769.                             echo "     <D:lockdiscovery>\n";
  770.                             echo $prop["val"];
  771.                             echo "     </D:lockdiscovery>\n";
  772.                             break;
  773.                         // the following are non-standard Microsoft extensions to the DAV namespace
  774.                         case "lastaccessed":
  775.                             echo "     <D:lastaccessed ns0:dt=\"dateTime.rfc1123\">"
  776.                                 . gmdate("D, d M Y H:i:s "$prop['val'])
  777.                                 . "GMT</D:lastaccessed>\n";
  778.                             break;
  779.                         case "ishidden":
  780.                             echo "     <D:ishidden>"
  781.                                 . is_string($prop['val']$prop['val'($prop['val''true' 'false')
  782.                                 . "</D:ishidden>\n";
  783.                             break;
  784.                         default:                                    
  785.                             echo "     <D:$prop[name]>"
  786.                                 . $this->_prop_encode(htmlspecialchars($prop['val']))
  787.                                 .     "</D:$prop[name]>\n";                               
  788.                             break;
  789.                         }
  790.                     else {
  791.                         // properties from namespaces != "DAV:" or without any namespace 
  792.                         if ($prop["ns"]{
  793.                             echo "     <" $ns_hash[$prop["ns"]] . ":$prop[name]>"
  794.                                 . $this->_prop_encode(htmlspecialchars($prop['val']))
  795.                                 . "</" $ns_hash[$prop["ns"]] . ":$prop[name]>\n";
  796.                         else {
  797.                             echo "     <$prop[name] xmlns=\"\">"
  798.                                 . $this->_prop_encode(htmlspecialchars($prop['val']))
  799.                                 . "</$prop[name]>\n";
  800.                         }                               
  801.                     }
  802.                 }
  803.  
  804.                 echo "   </D:prop>\n";
  805.                 echo "   <D:status>HTTP/1.1 200 OK</D:status>\n";
  806.                 echo "  </D:propstat>\n";
  807.             }
  808.        
  809.             // now report all properties requested but not found
  810.             if (isset($file["noprops"])) {
  811.                 echo "  <D:propstat>\n";
  812.                 echo "   <D:prop>\n";
  813.  
  814.                 foreach ($file["noprops"as $key => $prop{
  815.                     if ($prop["ns"== "DAV:"{
  816.                         echo "     <D:$prop[name]/>\n";
  817.                     else if ($prop["ns"== ""{
  818.                         echo "     <$prop[name] xmlns=\"\"/>\n";
  819.                     else {
  820.                         echo "     <" $ns_hash[$prop["ns"]] . ":$prop[name]/>\n";
  821.                     }
  822.                 }
  823.  
  824.                 echo "   </D:prop>\n";
  825.                 echo "   <D:status>HTTP/1.1 404 Not Found</D:status>\n";
  826.                 echo "  </D:propstat>\n";
  827.             }
  828.             
  829.             echo " </D:response>\n";
  830.         }
  831.         
  832.         echo "</D:multistatus>\n";
  833.     }
  834.  
  835.     
  836.     // }}}
  837.     
  838.     // {{{ http_PROPPATCH() 
  839.  
  840.     /**
  841.      * PROPPATCH method handler
  842.      *
  843.      * @param  void 
  844.      * @return void 
  845.      */
  846.     function http_PROPPATCH(
  847.     {
  848.         if ($this->_check_lock_status($this->path)) {
  849.             $options = Array();
  850.  
  851.             $options["path"$this->path;
  852.  
  853.             $propinfo = new _parse_proppatch("php://input");
  854.             
  855.             if (!$propinfo->success{
  856.                 $this->http_status("400 Error");
  857.                 return;
  858.             }
  859.             
  860.             $options['props'$propinfo->props;
  861.             
  862.             $responsedescr $this->PROPPATCH($options);
  863.             
  864.             $this->http_status("207 Multi-Status");
  865.             header('Content-Type: text/xml; charset="utf-8"');
  866.             
  867.             echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  868.  
  869.             echo "<D:multistatus xmlns:D=\"DAV:\">\n";
  870.             echo " <D:response>\n";
  871.             echo "  <D:href>".$this->_urlencode($this->_mergePaths($this->_SERVER["SCRIPT_NAME"]$this->path))."</D:href>\n";
  872.  
  873.             foreach ($options["props"as $prop{
  874.                 echo "   <D:propstat>\n";
  875.                 echo "    <D:prop><$prop[name] xmlns=\"$prop[ns]\"/></D:prop>\n";
  876.                 echo "    <D:status>HTTP/1.1 $prop[status]</D:status>\n";
  877.                 echo "   </D:propstat>\n";
  878.             }
  879.  
  880.             if ($responsedescr{
  881.                 echo "  <D:responsedescription>".
  882.                     $this->_prop_encode(htmlspecialchars($responsedescr)).
  883.                     "</D:responsedescription>\n";
  884.             }
  885.  
  886.             echo " </D:response>\n";
  887.             echo "</D:multistatus>\n";
  888.         else {
  889.             $this->http_status("423 Locked");
  890.         }
  891.     }
  892.     
  893.     // }}}
  894.  
  895.  
  896.     // {{{ http_MKCOL() 
  897.  
  898.     /**
  899.      * MKCOL method handler
  900.      *
  901.      * @param  void 
  902.      * @return void 
  903.      */
  904.     function http_MKCOL(
  905.     {
  906.         $options = Array();
  907.  
  908.         $options["path"$this->path;
  909.  
  910.         $stat $this->MKCOL($options);
  911.  
  912.         $this->http_status($stat);
  913.     }
  914.  
  915.     // }}}
  916.  
  917.  
  918.     // {{{ http_GET() 
  919.  
  920.     /**
  921.      * GET method handler
  922.      *
  923.      * @param void 
  924.      * @returns void
  925.      */
  926.     function http_GET(
  927.     {
  928.         // TODO check for invalid stream
  929.         $options         = Array();
  930.         $options["path"$this->path;
  931.  
  932.         $this->_get_ranges($options);
  933.  
  934.         if (true === ($status $this->GET($options))) {
  935.             if (!headers_sent()) {
  936.                 $status "200 OK";
  937.  
  938.                 if (!isset($options['mimetype'])) {
  939.                     $options['mimetype'"application/octet-stream";
  940.                 }
  941.                 header("Content-type: $options[mimetype]");
  942.                 
  943.                 if (isset($options['mtime'])) {
  944.                     header("Last-modified:".gmdate("D, d M Y H:i:s "$options['mtime'])."GMT");
  945.                 }
  946.                 
  947.                 if (isset($options['stream'])) {
  948.                     // GET handler returned a stream
  949.                     if (!empty($options['ranges']&& (0===fseek($options['stream']0SEEK_SET))) {
  950.                         // partial request and stream is seekable 
  951.                         
  952.                         if (count($options['ranges']=== 1{
  953.                             $range $options['ranges'][0];
  954.                             
  955.                             if (isset($range['start'])) {
  956.                                 fseek($options['stream']$range['start']SEEK_SET);
  957.                                 if (feof($options['stream'])) {
  958.                                     $this->http_status("416 Requested range not satisfiable");
  959.                                     return;
  960.                                 }
  961.  
  962.                                 if (isset($range['end'])) {
  963.                                     $size $range['end']-$range['start']+1;
  964.                                     $this->http_status("206 partial");
  965.                                     header("Content-length: $size");
  966.                                     header("Content-range: $range[start]-$range[end]/"
  967.                                            . (isset($options['size']$options['size'"*"));
  968.                                     while ($size && !feof($options['stream'])) {
  969.                                         $buffer fread($options['stream']4096);
  970.                                         $size  -= $this->bytes($buffer);
  971.                                         echo $buffer;
  972.                                     }
  973.                                 else {
  974.                                     $this->http_status("206 partial");
  975.                                     if (isset($options['size'])) {
  976.                                         header("Content-length: ".($options['size'$range['start']));
  977.                                         header("Content-range: ".$range['start']."-".$range['end']."/"
  978.                                                . (isset($options['size']$options['size'"*"));
  979.                                     }
  980.                                     fpassthru($options['stream']);
  981.                                 }
  982.                             else {
  983.                                 header("Content-length: ".$range['last']);
  984.                                 fseek($options['stream']-$range['last']SEEK_END);
  985.                                 fpassthru($options['stream']);
  986.                             }
  987.                         else {
  988.                             $this->_multipart_byterange_header()// init multipart
  989.                             foreach ($options['ranges'as $range{
  990.                                 // TODO what if size unknown? 500?
  991.                                 if (isset($range['start'])) {
  992.                                     $from $range['start'];
  993.                                     $to   !empty($range['end']$range['end'$options['size']-1; 
  994.                                 else {
  995.                                     $from $options['size'$range['last']-1;
  996.                                     $to   $options['size'-1;
  997.                                 }
  998.                                 $total = isset($options['size']$options['size'"*"
  999.                                 $size  $to $from + 1;
  1000.                                 $this->_multipart_byterange_header($options['mimetype']$from$to$total);
  1001.  
  1002.  
  1003.                                 fseek($options['stream']$fromSEEK_SET);
  1004.                                 while ($size && !feof($options['stream'])) {
  1005.                                     $buffer fread($options['stream']4096);
  1006.                                     $size  -= $this->bytes($buffer);
  1007.                                     echo $buffer;
  1008.                                 }
  1009.                             }
  1010.                             $this->_multipart_byterange_header()// end multipart
  1011.                         }
  1012.                     else {
  1013.                         // normal request or stream isn't seekable, return full content
  1014.                         if (isset($options['size'])) {
  1015.                             header("Content-length: ".$options['size']);
  1016.                         }
  1017.                         fpassthru($options['stream']);
  1018.                         return// no more headers
  1019.                     }
  1020.                 elseif (isset($options['data'])) {
  1021.                     if (is_array($options['data'])) {
  1022.                         // reply to partial request
  1023.                     else {
  1024.                         header("Content-length: ".$this->bytes($options['data']));
  1025.                         echo $options['data'];
  1026.                     }
  1027.                 }
  1028.             
  1029.         
  1030.  
  1031.         if (!headers_sent()) {
  1032.             if (false === $status{
  1033.                 $this->http_status("404 not found");
  1034.             else {
  1035.                 // TODO: check setting of headers in various code paths above
  1036.                 $this->http_status("$status");
  1037.             }
  1038.         }
  1039.     }
  1040.  
  1041.  
  1042.     /**
  1043.      * parse HTTP Range: header
  1044.      *
  1045.      * @param  array options array to store result in
  1046.      * @return void 
  1047.      */
  1048.     function _get_ranges(&$options
  1049.     {
  1050.         // process Range: header if present
  1051.         if (isset($this->_SERVER['HTTP_RANGE'])) {
  1052.  
  1053.             // we only support standard "bytes" range specifications for now
  1054.             if (preg_match('/bytes\s*=\s*(.+)/'$this->_SERVER['HTTP_RANGE']$matches)) {
  1055.                 $options["ranges"= array();
  1056.  
  1057.                 // ranges are comma separated
  1058.                 foreach (explode(","$matches[1]as $range{
  1059.                     // ranges are either from-to pairs or just end positions
  1060.                     list($start$endexplode("-"$range);
  1061.                     $options["ranges"][($start===""
  1062.                         ? array("last"=>$end
  1063.                         : array("start"=>$start"end"=>$end);
  1064.                 }
  1065.             }
  1066.         }
  1067.     }
  1068.  
  1069.     /**
  1070.      * generate separator headers for multipart response
  1071.      *
  1072.      * first and last call happen without parameters to generate
  1073.      * the initial header and closing sequence, all calls inbetween
  1074.      * require content mimetype, start and end byte position and
  1075.      * optionaly the total byte length of the requested resource
  1076.      *
  1077.      * @param  string  mimetype
  1078.      * @param  int     start byte position
  1079.      * @param  int     end   byte position
  1080.      * @param  int     total resource byte size
  1081.      */
  1082.     function _multipart_byterange_header($mimetype = false$from = false$to=false$total=false
  1083.     {
  1084.         if ($mimetype === false{
  1085.             if (!isset($this->multipart_separator)) {
  1086.                 // initial
  1087.  
  1088.                 // a little naive, this sequence *might* be part of the content
  1089.                 // but it's really not likely and rather expensive to check 
  1090.                 $this->multipart_separator "SEPARATOR_".md5(microtime());
  1091.  
  1092.                 // generate HTTP header
  1093.                 header("Content-type: multipart/byteranges; boundary=".$this->multipart_separator);
  1094.             else {
  1095.                 // final 
  1096.  
  1097.                 // generate closing multipart sequence
  1098.                 echo "\n--{$this->multipart_separator}--";
  1099.             }
  1100.         } else {
  1101.             // generate separator and header for next part
  1102.             echo "\n--{$this->multipart_separator}\n";
  1103.             echo "Content-type: $mimetype\n";
  1104.             echo "Content-range: $from-$to/"($total === false ? "*" : $total);
  1105.             echo "\n\n";
  1106.         }
  1107.     }
  1108.  
  1109.             
  1110.  
  1111.     // }}}
  1112.     // {{{ http_HEAD() 
  1113.     /**
  1114.      * HEAD method handler
  1115.      *
  1116.      * @param  void
  1117.      * @return void
  1118.      */
  1119.     function http_HEAD() 
  1120.     {
  1121.         $status          = false;
  1122.         $options         = Array();
  1123.         $options["path"] = $this->path;
  1124.         
  1125.         if (method_exists($this, "HEAD")) {
  1126.             $status = $this->head($options);
  1127.         } else if (method_exists($this, "GET")) {
  1128.             ob_start();
  1129.             $status = $this->GET($options);
  1130.             if (!isset($options['size'])) {
  1131.                 $options['size'] = ob_get_length();
  1132.             }
  1133.             ob_end_clean();
  1134.         }
  1135.         
  1136.         if (!isset($options['mimetype'])) {
  1137.             $options['mimetype'] = "application/octet-stream";
  1138.         }
  1139.         header("Content-type: $options[mimetype]");
  1140.         
  1141.         if (isset($options['mtime'])) {
  1142.             header("Last-modified:".gmdate("D, d M Y H:i:s ", $options['mtime'])."GMT");
  1143.         }
  1144.                 
  1145.         if (isset($options['size'])) {
  1146.             header("Content-length: ".$options['size']);
  1147.         }
  1148.  
  1149.         if ($status === true)  $status = "200 OK";
  1150.         if ($status === false) $status = "404 Not found";
  1151.         
  1152.         $this->http_status($status);
  1153.     }
  1154.  
  1155.     // }}}
  1156.     // {{{ http_PUT() 
  1157.     /**
  1158.      * PUT method handler
  1159.      *
  1160.      * @param  void
  1161.      * @return void
  1162.      */
  1163.     function http_PUT() 
  1164.     {
  1165.         if ($this->_check_lock_status($this->path)) {
  1166.             $options                   = Array();
  1167.             $options["path"]           = $this->path;
  1168.             $options["content_length"] = $this->_SERVER["CONTENT_LENGTH"];
  1169.  
  1170.             // get the Content-type 
  1171.             if (isset($this->_SERVER["CONTENT_TYPE"])) {
  1172.                 // for now we do not support any sort of multipart requests
  1173.                 if (!strncmp($this->_SERVER["CONTENT_TYPE"], "multipart/", 10)) {
  1174.                     $this->http_status("501 not implemented");
  1175.                     echo "The service does not support mulipart PUT requests";
  1176.                     return;
  1177.                 }
  1178.                 $options["content_type"] = $this->_SERVER["CONTENT_TYPE"];
  1179.             } else {
  1180.                 // default content type if none given
  1181.                 $options["content_type"] = "application/octet-stream";
  1182.             }
  1183.  
  1184.             /* RFC 2616 2.6 says: "The recipient of the entity MUST NOT 
  1185.              ignore any Content-* (e.g. Content-Range) headers that it 
  1186.              does not understand or implement and MUST return a 501 
  1187.              (Not Implemented) response in such cases."
  1188.             */ 
  1189.             foreach ($this->_SERVER as $key => $val) {
  1190.                 if (strncmp($key, "HTTP_CONTENT", 11)) continue;
  1191.                 switch ($key) {
  1192.                 case 'HTTP_CONTENT_ENCODING'// RFC 2616 14.11
  1193.                     // TODO support this if ext/zlib filters are available
  1194.                     $this->http_status("501 not implemented")
  1195.                     echo "The service does not support '$val' content encoding";
  1196.                     return;
  1197.  
  1198.                 case 'HTTP_CONTENT_LANGUAGE'// RFC 2616 14.12
  1199.                     // we assume it is not critical if this one is ignored
  1200.                     // in the actual PUT implementation ...
  1201.                     $options["content_language"] = $val;
  1202.                     break;
  1203.  
  1204.                 case 'HTTP_CONTENT_LENGTH':
  1205.                     // defined on IIS and has the same value as CONTENT_LENGTH
  1206.                     break;
  1207.  
  1208.                 case 'HTTP_CONTENT_LOCATION'// RFC 2616 14.14
  1209.                     /* The meaning of the Content-Location header in PUT 
  1210.                      or POST requests is undefined; servers are free 
  1211.                      to ignore it in those cases. */
  1212.                     break;
  1213.  
  1214.                 case 'HTTP_CONTENT_RANGE':    // RFC 2616 14.16
  1215.                     // single byte range requests are supported
  1216.                     // the header format is also specified in RFC 2616 14.16
  1217.                     // TODO we have to ensure that implementations support this or send 501 instead
  1218.                     if (!preg_match('@bytes\s+(\d+)-(\d+)/((\d+)|\*)@', $val, $matches)) {
  1219.                         $this->http_status("400 bad request")
  1220.                         echo "The service does only support single byte ranges";
  1221.                         return;
  1222.                     }
  1223.                     
  1224.                     $range = array("start" => $matches[1], "end" => $matches[2]);
  1225.                     if (is_numeric($matches[3])) {
  1226.                         $range["total_length"] = $matches[3];
  1227.                     }
  1228.  
  1229.                     if (!isset($options['ranges'])) {
  1230.                         $options['ranges'] = array()
  1231.                     }
  1232.  
  1233.                     $options["ranges"][] = $range;
  1234.  
  1235.                     // TODO make sure the implementation supports partial PUT
  1236.                     // this has to be done in advance to avoid data being overwritten
  1237.                     // on implementations that do not support this ...
  1238.                     break;
  1239.  
  1240.                 case 'HTTP_CONTENT_TYPE':
  1241.                     // defined on IIS and has the same value as CONTENT_TYPE
  1242.                     break;
  1243.  
  1244.                 case 'HTTP_CONTENT_MD5':      // RFC 2616 14.15
  1245.                     // TODO: maybe we can just pretend here?
  1246.                     $this->http_status("501 not implemented")
  1247.                     echo "The service does not support content MD5 checksum verification"
  1248.                     return;
  1249.  
  1250.                 default: 
  1251.                     // any other unknown Content-* headers
  1252.                     $this->http_status("501 not implemented")
  1253.                     echo "The service does not support '$key'"
  1254.                     return;
  1255.                 }
  1256.             }
  1257.  
  1258.             $options["stream"] = fopen("php://input", "r");
  1259.  
  1260.             $stat = $this->PUT($options);
  1261.  
  1262.             if ($stat === false) {
  1263.                 $stat = "403 Forbidden";
  1264.             } else if (is_resource($stat) && get_resource_type($stat) == "stream") {
  1265.                 $stream = $stat;
  1266.  
  1267.                 $stat = $options["new"] ? "201 Created" : "204 No Content";
  1268.  
  1269.                 if (!empty($options["ranges"])) {
  1270.                     // TODO multipart support is missing (see also above)
  1271.                     if (0 == fseek($stream, $options['ranges'][0]["start"], SEEK_SET)) {
  1272.                         $length = $options['ranges'][0]["end"] - $options['ranges'][0]["start"]+1;
  1273.                         
  1274.                         while (!feof($options['stream'])) {
  1275.                             if ($length <= 0) {
  1276.                                break;
  1277.                             }
  1278.  
  1279.                             if ($length <= 8192) {
  1280.                                 $data = fread($options['stream'], $length);
  1281.                             } else {
  1282.                                 $data = fread($options['stream'], 8192);
  1283.                             }
  1284.  
  1285.                             if ($data === false) {
  1286.                                 $stat = "400 Bad request";
  1287.                             } elseif (strlen($data)) {
  1288.                                 if (false === fwrite($stream, $data)) {
  1289.                                     $stat = "403 Forbidden";
  1290.                                     break;
  1291.                                 }
  1292.                                 $length -= strlen($data);
  1293.                             }
  1294.                         }
  1295.                     } else {
  1296.                         $stat = "403 Forbidden"
  1297.                     }
  1298.                 } else {
  1299.                     while (!feof($options["stream"])) {
  1300.                         if (false === fwrite($stream, fread($options["stream"], 8192))) {
  1301.                             $stat = "403 Forbidden";
  1302.                             break;
  1303.                         }
  1304.                     }
  1305.                 }
  1306.  
  1307.                 fclose($stream);
  1308.             } 
  1309.  
  1310.             $this->http_status($stat);
  1311.         } else {
  1312.             $this->http_status("423 Locked");
  1313.         }
  1314.     }
  1315.  
  1316.     // }}}
  1317.  
  1318.     // {{{ http_DELETE() 
  1319.     /**
  1320.      * DELETE method handler
  1321.      *
  1322.      * @param  void
  1323.      * @return void
  1324.      */
  1325.     function http_DELETE() 
  1326.     {
  1327.         // check RFC 2518 Section 9.2, last paragraph
  1328.         if (isset($this->_SERVER["HTTP_DEPTH"])) {
  1329.             if ($this->_SERVER["HTTP_DEPTH"] != "infinity") {
  1330.                 $this->http_status("400 Bad Request");
  1331.                 return;
  1332.             }
  1333.         }
  1334.  
  1335.         // check lock status
  1336.         if ($this->_check_lock_status($this->path)) {
  1337.             // ok, proceed
  1338.             $options         = Array();
  1339.             $options["path"] = $this->path;
  1340.  
  1341.             $stat = $this->DELETE($options);
  1342.  
  1343.             $this->http_status($stat);
  1344.         } else {
  1345.             // sorry, its locked
  1346.             $this->http_status("423 Locked");
  1347.         }
  1348.     }
  1349.  
  1350.     // }}}
  1351.     // {{{ http_COPY() 
  1352.     /**
  1353.      * COPY method handler
  1354.      *
  1355.      * @param  void
  1356.      * @return void
  1357.      */
  1358.     function http_COPY() 
  1359.     {
  1360.         // no need to check source lock status here 
  1361.         // destination lock status is always checked by the helper method
  1362.         $this->_copymove("copy");
  1363.     }
  1364.  
  1365.     // }}}
  1366.     // {{{ http_MOVE() 
  1367.     /**
  1368.      * MOVE method handler
  1369.      *
  1370.      * @param  void
  1371.      * @return void
  1372.      */
  1373.     function http_MOVE() 
  1374.     {
  1375.         if ($this->_check_lock_status($this->path)) {
  1376.             // destination lock status is always checked by the helper method
  1377.             $this->_copymove("move");
  1378.         } else {
  1379.             $this->http_status("423 Locked");
  1380.         }
  1381.     }
  1382.  
  1383.     // }}}
  1384.  
  1385.     // {{{ http_LOCK() 
  1386.     /**
  1387.      * LOCK method handler
  1388.      *
  1389.      * @param  void
  1390.      * @return void
  1391.      */
  1392.     function http_LOCK() 
  1393.     {
  1394.         $options         = Array();
  1395.         $options["path"] = $this->path;
  1396.         
  1397.         if (isset($this->_SERVER['HTTP_DEPTH'])) {
  1398.             $options["depth"] = $this->_SERVER["HTTP_DEPTH"];
  1399.         } else {
  1400.             $options["depth"] = "infinity";
  1401.         }
  1402.         
  1403.         if (isset($this->_SERVER["HTTP_TIMEOUT"])) {
  1404.             $options["timeout"] = explode(",", $this->_SERVER["HTTP_TIMEOUT"]);
  1405.         }
  1406.         
  1407.         if (empty($this->_SERVER['CONTENT_LENGTH']) && !empty($this->_SERVER['HTTP_IF'])) {
  1408.             // check if locking is possible
  1409.             if (!$this->_check_lock_status($this->path)) {
  1410.                 $this->http_status("423 Locked");
  1411.                 return;
  1412.             }
  1413.  
  1414.             // refresh lock
  1415.             $options["locktoken"] = substr($this->_SERVER['HTTP_IF'], 2, -2);
  1416.             $options["update"]    = $options["locktoken"];
  1417.  
  1418.             // setting defaults for required fields, LOCK() SHOULD overwrite these
  1419.             $options['owner']     = "unknown";
  1420.             $options['scope']     = "exclusive";
  1421.             $options['type']      = "write";
  1422.  
  1423.  
  1424.             $stat = $this->LOCK($options);
  1425.         } else {
  1426.             // extract lock request information from request XML payload
  1427.             $lockinfo = new <a href="../HTTP_WebDAV_Server/_parse_lockinfo.html">_parse_lockinfo</a>("php://input");
  1428.             if (!$lockinfo->success) {
  1429.                 $this->http_status("400 bad request")
  1430.             }
  1431.  
  1432.             // check if locking is possible
  1433.             if (!$this->_check_lock_status($this->path, $lockinfo->lockscope === "shared")) {
  1434.                 $this->http_status("423 Locked");
  1435.                 return;
  1436.             }
  1437.  
  1438.             // new lock 
  1439.             $options["scope"]     = $lockinfo->lockscope;
  1440.             $options["type"]      = $lockinfo->locktype;
  1441.             $options["owner"]     = $lockinfo->owner;            
  1442.             $options["locktoken"] = $this->_new_locktoken();
  1443.             
  1444.             $stat = $this->LOCK($options);              
  1445.         }
  1446.         
  1447.         if (is_bool($stat)) {
  1448.             $http_stat = $stat ? "200 OK" : "423 Locked";
  1449.         } else {
  1450.             $http_stat = (string)$stat;
  1451.         }
  1452.         $this->http_status($http_stat);
  1453.         
  1454.         if ($http_stat{0} == 2) { // 2xx states are ok 
  1455.             if ($options["timeout"]) {
  1456.                 // if multiple timeout values were given we take the first only
  1457.                 if (is_array($options["timeout"])) {
  1458.                     reset($options["timeout"]);
  1459.                     $options["timeout"] = current($options["timeout"]);
  1460.                 }
  1461.                 // if the timeout is numeric only we need to reformat it
  1462.                 if (is_numeric($options["timeout"])) {
  1463.                     // more than a million is considered an absolute timestamp
  1464.                     // less is more likely a relative value
  1465.                     if ($options["timeout"]>1000000) {
  1466.                         $timeout = "Second-".($options['timeout']-time());
  1467.                     } else {
  1468.                         $timeout = "Second-$options[timeout]";
  1469.                     }
  1470.                 } else {
  1471.                     // non-numeric values are passed on verbatim,
  1472.                     // no error checking is performed here in this case
  1473.                     // TODO: send "Infinite" on invalid timeout strings?
  1474.                     $timeout = $options["timeout"];
  1475.                 }
  1476.             } else {
  1477.                 $timeout = "Infinite";
  1478.             }
  1479.             
  1480.             header('Content-Type: text/xml; charset="utf-8"');
  1481.             header("Lock-Token: <$options[locktoken]>");
  1482.             echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  1483.             echo "<D:prop xmlns:D=\"DAV:\">\n";
  1484.             echo " <D:lockdiscovery>\n";
  1485.             echo "  <D:activelock>\n";
  1486.             echo "   <D:lockscope><D:$options[scope]/></D:lockscope>\n";
  1487.             echo "   <D:locktype><D:$options[type]/></D:locktype>\n";
  1488.             echo "   <D:depth>$options[depth]</D:depth>\n";
  1489.             echo "   <D:owner>$options[owner]</D:owner>\n";
  1490.             echo "   <D:timeout>$timeout</D:timeout>\n";
  1491.             echo "   <D:locktoken><D:href>$options[locktoken]</D:href></D:locktoken>\n";
  1492.             echo "  </D:activelock>\n";
  1493.             echo " </D:lockdiscovery>\n";
  1494.             echo "</D:prop>\n\n";
  1495.         }
  1496.     }
  1497.     
  1498.  
  1499.     // }}}
  1500.     // {{{ http_UNLOCK() 
  1501.     /**
  1502.      * UNLOCK method handler
  1503.      *
  1504.      * @param  void
  1505.      * @return void
  1506.      */
  1507.     function http_UNLOCK() 
  1508.     {
  1509.         $options         = Array();
  1510.         $options["path"] = $this->path;
  1511.  
  1512.         if (isset($this->_SERVER['HTTP_DEPTH'])) {
  1513.             $options["depth"] = $this->_SERVER["HTTP_DEPTH"];
  1514.         } else {
  1515.             $options["depth"] = "infinity";
  1516.         }
  1517.  
  1518.         // strip surrounding <>
  1519.         $options["token"] = substr(trim($this->_SERVER["HTTP_LOCK_TOKEN"]), 1, -1);  
  1520.  
  1521.         // call user method
  1522.         $stat = $this->UNLOCK($options);
  1523.  
  1524.         $this->http_status($stat);
  1525.     }
  1526.  
  1527.     // }}}
  1528.     // }}}
  1529.     // {{{ _copymove() 
  1530.     function _copymove($what) 
  1531.     {
  1532.         $options         = Array();
  1533.         $options["path"] = $this->path;
  1534.  
  1535.         if (isset($this->_SERVER["HTTP_DEPTH"])) {
  1536.             $options["depth"] = $this->_SERVER["HTTP_DEPTH"];
  1537.         } else {
  1538.             $options["depth"] = "infinity";
  1539.         }
  1540.  
  1541.         $http_header_host = preg_replace("/:80$/", "", $this->_SERVER["HTTP_HOST"]);
  1542.  
  1543.         $url  = parse_url($this->_SERVER["HTTP_DESTINATION"]);
  1544.         $path = urldecode($url["path"]);
  1545.  
  1546.         if (isset($url["host"])) {
  1547.             // TODO check url scheme, too
  1548.             $http_host = $url["host"];
  1549.             if (isset($url["port"]) && $url["port"] != 80)
  1550.                 $http_host.= ":".$url["port"];
  1551.         } else {
  1552.             // only path given, set host to self
  1553.             $http_host == $http_header_host;
  1554.         }
  1555.  
  1556.         if ($http_host == $http_header_host &&
  1557.             !strncmp($this->_SERVER["SCRIPT_NAME"], $path,
  1558.                      strlen($this->_SERVER["SCRIPT_NAME"]))) {
  1559.             $options["dest"] = substr($path, strlen($this->_SERVER["SCRIPT_NAME"]));
  1560.             if (!$this->_check_lock_status($options["dest"])) {
  1561.                 $this->http_status("423 Locked");
  1562.                 return;
  1563.             }
  1564.  
  1565.         } else {
  1566.             $options["dest_url"] = $this->_SERVER["HTTP_DESTINATION"];
  1567.         }
  1568.  
  1569.         // see RFC 2518 Sections 9.6, 8.8.4 and 8.9.3
  1570.         if (isset($this->_SERVER["HTTP_OVERWRITE"])) {
  1571.             $options["overwrite"] = $this->_SERVER["HTTP_OVERWRITE"] == "T";
  1572.         } else {
  1573.             $options["overwrite"] = true;
  1574.         }
  1575.  
  1576.         $stat = $this->$what($options);
  1577.         $this->http_status($stat);
  1578.     }
  1579.  
  1580.     // }}}
  1581.     // {{{ _allow() 
  1582.     /**
  1583.      * check for implemented HTTP methods
  1584.      *
  1585.      * @param  void
  1586.      * @return array something
  1587.      */
  1588.     function _allow() 
  1589.     {
  1590.         // OPTIONS is always there
  1591.         $allow = array("OPTIONS" =>"OPTIONS");
  1592.  
  1593.         // all other METHODS need both a http_method() wrapper
  1594.         // and a method() implementation
  1595.         // the base class supplies wrappers only
  1596.         foreach (get_class_methods($this) as $method) {
  1597.             if (!strncmp("http_", $method, 5)) {
  1598.                 $method = strtoupper(substr($method, 5));
  1599.                 if (method_exists($this, $method)) {
  1600.                     $allow[$method] = $method;
  1601.                 }
  1602.             }
  1603.         }
  1604.  
  1605.         // we can emulate a missing HEAD implemetation using GET
  1606.         if (isset($allow["GET"]))
  1607.             $allow["HEAD"] = "HEAD";
  1608.  
  1609.         // no LOCK without checklok()
  1610.         if (!method_exists($this, "checklock")) {
  1611.             unset($allow["LOCK"]);
  1612.             unset($allow["UNLOCK"]);
  1613.         }
  1614.  
  1615.         return $allow;
  1616.     }
  1617.  
  1618.     // }}}
  1619.     /**
  1620.      * helper for property element creation
  1621.      *
  1622.      * @param  string  XML namespace (optional)
  1623.      * @param  string  property name
  1624.      * @param  string  property value
  1625.      * @return array   property array
  1626.      */
  1627.     function mkprop() 
  1628.     {
  1629.         $args = func_get_args();
  1630.         if (count($args) == 3) {
  1631.             return array("ns"   => $args[0], 
  1632.                          "name" => $args[1],
  1633.                          "val"  => $args[2]);
  1634.         } else {
  1635.             return array("ns"   => "DAV:", 
  1636.                          "name" => $args[0],
  1637.                          "val"  => $args[1]);
  1638.         }
  1639.     }
  1640.  
  1641.     // {{{ _check_auth 
  1642.     /**
  1643.      * check authentication if check is implemented
  1644.      * 
  1645.      * @param  void
  1646.      * @return bool  true if authentication succeded or not necessary
  1647.      */
  1648.     function _check_auth() 
  1649.     {
  1650.         $auth_type = isset($this->_SERVER["AUTH_TYPE"]) 
  1651.             ? $this->_SERVER["AUTH_TYPE"] 
  1652.             : null;
  1653.  
  1654.         $auth_user = isset($this->_SERVER["PHP_AUTH_USER"]) 
  1655.             ? $this->_SERVER["PHP_AUTH_USER"] 
  1656.             : null;
  1657.  
  1658.         $auth_pw   = isset($this->_SERVER["PHP_AUTH_PW"]) 
  1659.             ? $this->_SERVER["PHP_AUTH_PW"] 
  1660.             : null;
  1661.  
  1662.         if (method_exists($this, "checkAuth")) {
  1663.             // PEAR style method name
  1664.             return $this->checkAuth($auth_type, $auth_user, $auth_pw);
  1665.         } else if (method_exists($this, "check_auth")) {
  1666.             // old (pre 1.0) method name
  1667.             return $this->check_auth($auth_type, $auth_user, $auth_pw);
  1668.         } else {
  1669.             // no method found -> no authentication required
  1670.             return true;
  1671.         }
  1672.     }
  1673.  
  1674.     // }}}
  1675.     // {{{ UUID stuff 
  1676.     
  1677.     /**
  1678.      * generate Unique Universal IDentifier for lock token
  1679.      *
  1680.      * @param  void
  1681.      * @return string  a new UUID
  1682.      */
  1683.     function _new_uuid() 
  1684.     {
  1685.         // use uuid extension from PECL if available
  1686.         if (function_exists("uuid_create")) {
  1687.             return uuid_create();
  1688.         }
  1689.  
  1690.         // fallback
  1691.         $uuid = md5(microtime().getmypid());    // this should be random enough for now
  1692.         // set variant and version fields for 'true' random uuid
  1693.         $uuid{12} = "4";
  1694.         $n = 8 + (ord($uuid{16}) & 3);
  1695.         $hex = "0123456789abcdef";
  1696.         $uuid{16} = $hex{$n};
  1697.  
  1698.         // return formated uuid
  1699.         return substr($uuid,  0, 8)."-"
  1700.             .  substr($uuid,  8, 4)."-"
  1701.             .  substr($uuid, 12, 4)."-"
  1702.             .  substr($uuid, 16, 4)."-"
  1703.             .  substr($uuid, 20);
  1704.     }
  1705.  
  1706.     /**
  1707.      * create a new opaque lock token as defined in RFC2518
  1708.      *
  1709.      * @param  void
  1710.      * @return string  new RFC2518 opaque lock token
  1711.      */
  1712.     function _new_locktoken() 
  1713.     {
  1714.         return "opaquelocktoken:".$this->_new_uuid();
  1715.     }
  1716.  
  1717.     // }}}
  1718.     // {{{ WebDAV If: header parsing 
  1719.     /**
  1720.      * 
  1721.      *
  1722.      * @param  string  header string to parse
  1723.      * @param  int     current parsing position
  1724.      * @return array   next token (type and value)
  1725.      */
  1726.     function _if_header_lexer($string, &$pos) 
  1727.     {
  1728.         // skip whitespace
  1729.         while (ctype_space($string{$pos})) {
  1730.             ++$pos;
  1731.         }
  1732.  
  1733.         // already at end of string?
  1734.         if (strlen($string) <= $pos) {
  1735.             return false;
  1736.         }
  1737.  
  1738.         // get next character
  1739.         $c = $string{$pos++};
  1740.  
  1741.         // now it depends on what we found
  1742.         switch ($c) {
  1743.         case "<":
  1744.             // URIs are enclosed in <...>
  1745.             $pos2 = strpos($string, ">", $pos);
  1746.             $uri  = substr($string, $pos, $pos2 - $pos);
  1747.             $pos  = $pos2 + 1;
  1748.             return array("URI", $uri);
  1749.  
  1750.         case "[":
  1751.             //Etags are enclosed in [...]
  1752.             if ($string{$pos} == "W") {
  1753.                 $type = "ETAG_WEAK";
  1754.                 $pos += 2;
  1755.             } else {
  1756.                 $type = "ETAG_STRONG";
  1757.             }
  1758.             $pos2 = strpos($string, "]", $pos);
  1759.             $etag = substr($string, $pos + 1, $pos2 - $pos - 2);
  1760.             $pos  = $pos2 + 1;
  1761.             return array($type, $etag);
  1762.  
  1763.         case "N":
  1764.             // "N" indicates negation
  1765.             $pos += 2;
  1766.             return array("NOT", "Not");
  1767.  
  1768.         default:
  1769.             // anything else is passed verbatim char by char
  1770.             return array("CHAR", $c);
  1771.         }
  1772.     }
  1773.  
  1774.     /** 
  1775.      * parse If: header
  1776.      *
  1777.      * @param  string  header string
  1778.      * @return array   URIs and their conditions
  1779.      */
  1780.     function _if_header_parser($str) 
  1781.     {
  1782.         $pos  = 0;
  1783.         $len  = strlen($str);
  1784.         $uris = array();
  1785.  
  1786.         // parser loop
  1787.         while ($pos < $len) {
  1788.             // get next token
  1789.             $token = $this->_if_header_lexer($str, $pos);
  1790.  
  1791.             // check for URI
  1792.             if ($token[0] == "URI") {
  1793.                 $uri   = $token[1]// remember URI
  1794.                 $token = $this->_if_header_lexer($str, $pos)// get next token
  1795.             } else {
  1796.                 $uri = "";
  1797.             }
  1798.  
  1799.             // sanity check
  1800.             if ($token[0] != "CHAR" || $token[1] != "(") {
  1801.                 return false;
  1802.             }
  1803.  
  1804.             $list  = array();
  1805.             $level = 1;
  1806.             $not   = "";
  1807.             while ($level) {
  1808.                 $token = $this->_if_header_lexer($str, $pos);
  1809.                 if ($token[0] == "NOT") {
  1810.                     $not = "!";
  1811.                     continue;
  1812.                 }
  1813.                 switch ($token[0]) {
  1814.                 case "CHAR":
  1815.                     switch ($token[1]) {
  1816.                     case "(":
  1817.                         $level++;
  1818.                         break;
  1819.                     case ")":
  1820.                         $level--;
  1821.                         break;
  1822.                     default:
  1823.                         return false;
  1824.                     }
  1825.                     break;
  1826.  
  1827.                 case "URI":
  1828.                     $list[] = $not."<$token[1]>";
  1829.                     break;
  1830.  
  1831.                 case "ETAG_WEAK":
  1832.                     $list[] = $not."[W/'$token[1]']>";
  1833.                     break;
  1834.  
  1835.                 case "ETAG_STRONG":
  1836.                     $list[] = $not."['$token[1]']>";
  1837.                     break;
  1838.  
  1839.                 default:
  1840.                     return false;
  1841.                 }
  1842.                 $not = "";
  1843.             }
  1844.  
  1845.             if (isset($uris[$uri]) && is_array($uris[$uri])) {
  1846.                 $uris[$uri] = array_merge($uris[$uri], $list);
  1847.             } else {
  1848.                 $uris[$uri] = $list;
  1849.             }
  1850.         }
  1851.  
  1852.         return $uris;
  1853.     }
  1854.  
  1855.     /**
  1856.      * check if conditions from "If:" headers are meat 
  1857.      *
  1858.      * the "If:" header is an extension to HTTP/1.1
  1859.      * defined in RFC 2518 section 9.4
  1860.      *
  1861.      * @param  void
  1862.      * @return void
  1863.      */
  1864.     function _check_if_header_conditions() 
  1865.     {
  1866.         if (isset($this->_SERVER["HTTP_IF"])) {
  1867.             $this->_if_header_uris =
  1868.                 $this->_if_header_parser($this->_SERVER["HTTP_IF"]);
  1869.  
  1870.             foreach ($this->_if_header_uris as $uri => $conditions) {
  1871.                 if ($uri == "") {
  1872.                     $uri = $this->uri;
  1873.                 }
  1874.                 // all must match
  1875.                 $state = true;
  1876.                 foreach ($conditions as $condition) {
  1877.                     // lock tokens may be free form (RFC2518 6.3)
  1878.                     // but if opaquelocktokens are used (RFC2518 6.4)
  1879.                     // we have to check the format (litmus tests this)
  1880.                     if (!strncmp($condition, "<opaquelocktoken:", strlen("<opaquelocktoken"))) {
  1881.                         if (!preg_match('/^<opaquelocktoken:[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}>$/', $condition)) {
  1882.                             $this->http_status("423 Locked");
  1883.                             return false;
  1884.                         }
  1885.                     }
  1886.                     if (!$this->_check_uri_condition($uri, $condition)) {
  1887.                         $this->http_status("412 Precondition failed");
  1888.                         $state = false;
  1889.                         break;
  1890.                     }
  1891.                 }
  1892.  
  1893.                 // any match is ok
  1894.                 if ($state == true) {
  1895.                     return true;
  1896.                 }
  1897.             }
  1898.             return false;
  1899.         }
  1900.         return true;
  1901.     }
  1902.  
  1903.     /**
  1904.      * Check a single URI condition parsed from an if-header
  1905.      *
  1906.      * Check a single URI condition parsed from an if-header
  1907.      *
  1908.      * @abstract 
  1909.      * @param string $uri URI to check
  1910.      * @param string $condition Condition to check for this URI
  1911.      * @returns bool Condition check result
  1912.      */
  1913.     function _check_uri_condition($uri, $condition) 
  1914.     {
  1915.         // not really implemented here, 
  1916.         // implementations must override
  1917.         // a lock token can never be from the DAV: scheme
  1918.         // litmus uses DAV:no-lock in some tests
  1919.         if (!strncmp("<DAV:", $condition, 5)) {
  1920.             return false;
  1921.         }
  1922.  
  1923.         return true;
  1924.     }
  1925.  
  1926.  
  1927.     /**
  1928.      * 
  1929.      *
  1930.      * @param  string  path of resource to check
  1931.      * @param  bool    exclusive lock?
  1932.      */
  1933.     function _check_lock_status($path, $exclusive_only = false) 
  1934.     {
  1935.         // FIXME depth -> ignored for now
  1936.         if (method_exists($this, "checkLock")) {
  1937.             // is locked?
  1938.             $lock = $this->checkLock($path);
  1939.  
  1940.             // ... and lock is not owned?
  1941.             if (is_array($lock) && count($lock)) {
  1942.                 // FIXME doesn't check uri restrictions yet
  1943.                 if (!isset($this->_SERVER["HTTP_IF"]) || !strstr($this->_SERVER["HTTP_IF"], $lock["token"])) {
  1944.                     if (!$exclusive_only || ($lock["scope"] !== "shared"))
  1945.                         return false;
  1946.                 }
  1947.             }
  1948.         }
  1949.         return true;
  1950.     }
  1951.  
  1952.  
  1953.     // }}}
  1954.  
  1955.     /**
  1956.      * Generate lockdiscovery reply from checklock() result
  1957.      *
  1958.      * @param   string  resource path to check
  1959.      * @return  string  lockdiscovery response
  1960.      */
  1961.     function lockdiscovery($path) 
  1962.     {
  1963.         // no lock support without checklock() method
  1964.         if (!method_exists($this, "checklock")) {
  1965.             return "";
  1966.         }
  1967.  
  1968.         // collect response here
  1969.         $activelocks = "";
  1970.  
  1971.         // get checklock() reply
  1972.         $lock = $this->checklock($path);
  1973.  
  1974.         // generate <activelock> block for returned data
  1975.         if (is_array($lock) && count($lock)) {
  1976.             // check for 'timeout' or 'expires'
  1977.             if (!empty($lock["expires"])) {
  1978.                 $timeout = "Second-".($lock["expires"] - time());
  1979.             } else if (!empty($lock["timeout"])) {
  1980.                 $timeout = "Second-$lock[timeout]";
  1981.             } else {
  1982.                 $timeout = "Infinite";
  1983.             }
  1984.  
  1985.             // genreate response block
  1986.             $activelocks.= "
  1987.               <D:activelock>
  1988.                <D:lockscope><D:$lock[scope]/></D:lockscope>
  1989.                <D:locktype><D:$lock[type]/></D:locktype>
  1990.                <D:depth>$lock[depth]</D:depth>
  1991.                <D:owner>$lock[owner]</D:owner>
  1992.                <D:timeout>$timeout</D:timeout>
  1993.                <D:locktoken><D:href>$lock[token]</D:href></D:locktoken>
  1994.               </D:activelock>
  1995.              ";
  1996.         }
  1997.  
  1998.         // return generated response
  1999.         return $activelocks;
  2000.     }
  2001.  
  2002.     /**
  2003.      * set HTTP return status and mirror it in a private header
  2004.      *
  2005.      * @param  string  status code and message
  2006.      * @return void
  2007.      */
  2008.     function http_status($status) 
  2009.     {
  2010.         // simplified success case
  2011.         if ($status === true) {
  2012.             $status = "200 OK";
  2013.         }
  2014.  
  2015.         // remember status
  2016.         $this->_http_status = $status;
  2017.  
  2018.         // generate HTTP status response
  2019.         header("HTTP/1.1 $status");
  2020.         header("X-WebDAV-Status: $status", true);
  2021.     }
  2022.  
  2023.     /**
  2024.      * private minimalistic version of PHP urlencode()
  2025.      *
  2026.      * only blanks, percent and XML special chars must be encoded here
  2027.      * full urlencode() encoding confuses some clients ...
  2028.      *
  2029.      * @param  string  URL to encode
  2030.      * @return string  encoded URL
  2031.      */
  2032.     function _urlencode($url) 
  2033.     {
  2034.         return strtr($url, array(" "=>"%20",
  2035.                                  "%"=>"%25",
  2036.                                  "&"=>"%26",
  2037.                                  "<"=>"%3C",
  2038.                                  ">"=>"%3E",
  2039.                                  ));
  2040.     }
  2041.  
  2042.     /**
  2043.      * private version of PHP urldecode
  2044.      *
  2045.      * not really needed but added for completenes
  2046.      *
  2047.      * @param  string  URL to decode
  2048.      * @return string  decoded URL
  2049.      */
  2050.     function _urldecode($path) 
  2051.     {
  2052.         return rawurldecode($path);
  2053.     }
  2054.  
  2055.     /**
  2056.      * UTF-8 encode property values if not already done so
  2057.      *
  2058.      * @param  string  text to encode
  2059.      * @return string  utf-8 encoded text
  2060.      */
  2061.     function _prop_encode($text) 
  2062.     {
  2063.         switch (strtolower($this->_prop_encoding)) {
  2064.         case "utf-8":
  2065.             return $text;
  2066.         case "iso-8859-1":
  2067.         case "iso-8859-15":
  2068.         case "latin-1":
  2069.         default:
  2070.             return utf8_encode($text);
  2071.         }
  2072.     }
  2073.  
  2074.     /**
  2075.      * Slashify - make sure path ends in a slash
  2076.      *
  2077.      * @param   string directory path
  2078.      * @returns string directory path wiht trailing slash
  2079.      */
  2080.     function _slashify($path) 
  2081.     {
  2082.         if ($path[strlen($path)-1] != '/') {
  2083.             $path = $path."/";
  2084.         }
  2085.         return $path;
  2086.     }
  2087.  
  2088.     /**
  2089.      * Unslashify - make sure path doesn't in a slash
  2090.      *
  2091.      * @param   string directory path
  2092.      * @returns string directory path wihtout trailing slash
  2093.      */
  2094.     function _unslashify($path) 
  2095.     {
  2096.         if ($path[strlen($path)-1] == '/') {
  2097.             $path = substr($path, 0, strlen($path) -1);
  2098.         }
  2099.         return $path;
  2100.     }
  2101.  
  2102.     /**
  2103.      * Merge two paths, make sure there is exactly one slash between them
  2104.      *
  2105.      * @param  string  parent path
  2106.      * @param  string  child path
  2107.      * @return string  merged path
  2108.      */
  2109.     function _mergePaths($parent, $child) 
  2110.     {
  2111.         if ($child{0} == '/') {
  2112.             return $this->_unslashify($parent).$child;
  2113.         } else {
  2114.             return $this->_slashify($parent).$child;
  2115.         }
  2116.     }
  2117.     
  2118.     /**
  2119.      * mbstring.func_overload save strlen version: counting the bytes not the chars
  2120.      *
  2121.      * @param string $str
  2122.      * @return int
  2123.      */
  2124.     function bytes($str)
  2125.     {
  2126.         static $func_overload;
  2127.         
  2128.         if (is_null($func_overload))
  2129.         {
  2130.             $func_overload = @extension_loaded('mbstring') ? ini_get('mbstring.func_overload') : 0;
  2131.         }
  2132.         return $func_overload & 2 ? mb_strlen($str,'ascii') : strlen($str);
  2133.     }
  2134. } 
  2135.  
  2136. /*
  2137.  * Local variables:
  2138.  * tab-width: 4
  2139.  * c-basic-offset: 4
  2140.  * End:
  2141.  */

Documentation generated on Mon, 11 Mar 2019 15:50:56 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.