3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2003 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Authors: Hartmut Holzgraefe <hholzgra@php.net> |
17 // | Christian Stocker <chregu@bitflux.ch> |
18 // +----------------------------------------------------------------------+
20 // $Id: Server.php,v 1.21 2004/04/14 21:44:26 hholzgra Exp $
22 require_once "HTTP/WebDAV/Tools/_parse_propfind.php";
23 require_once "HTTP/WebDAV/Tools/_parse_proppatch.php";
24 require_once "HTTP/WebDAV/Tools/_parse_lockinfo.php";
29 * Virtual base class for implementing WebDAV servers
31 * WebDAV server base class, needs to be extended to do useful work
33 * @package HTTP_WebDAV_Server
34 * @author Hartmut Holzgraefe <hholzgra@php.net>
37 class HTTP_WebDAV_Server
39 // {{{ Member Variables
42 * URI path for this request
49 * Realm string to be used in authentification popups
53 var $http_auth_realm = "PHP WebDAV";
56 * String to be used in "X-Dav-Powered-By" header
60 var $dav_powered_by = "";
63 * Remember parsed If: (RFC2518/9.4) header conditions
67 var $_if_header_uris = array();
70 * HTTP response status/message
74 var $_http_status = "200 OK";
77 * encoding of property values passed in
81 var $_prop_encoding = "utf-8";
92 function HTTP_WebDAV_Server()
94 // PHP messages destroy XML output -> switch them off
95 ini_set("display_errors", 0);
100 // {{{ ServeRequest()
102 * Serve WebDAV HTTP request
104 * dispatch WebDAV HTTP request to the apropriate method handler
109 function ServeRequest()
111 // identify ourselves
112 if (empty($this->dav_powered_by
)) {
113 header("X-Dav-Powered-By: PHP class: ".get_class($this));
115 header("X-Dav-Powered-By: ".$this->dav_powered_by
);
118 // check authentication
119 if (!$this->_check_auth()) {
120 $this->http_status('401 Unauthorized');
122 // RFC2518 says we must use Digest instead of Basic
123 // but Microsoft Clients do not support Digest
124 // and we don't support NTLM and Kerberos
125 // so we are stuck with Basic here
126 header('WWW-Authenticate: Basic realm="'.($this->http_auth_realm
).'"');
132 if(! $this->_check_if_header_conditions()) {
133 $this->http_status("412 Precondition failed");
138 $this->path
= $this->_urldecode(!empty($_SERVER["PATH_INFO"]) ?
$_SERVER["PATH_INFO"] : "/");
139 if(ini_get("magic_quotes_gpc")) {
140 $this->path
= stripslashes($this->path
);
144 // detect requested method names
145 $method = strtolower($_SERVER["REQUEST_METHOD"]);
146 $wrapper = "http_".$method;
148 // activate HEAD emulation by GET if no HEAD method found
149 if ($method == "head" && !method_exists($this, "head")) {
153 if (method_exists($this, $wrapper) && ($method == "options" ||
method_exists($this, $method))) {
154 $this->$wrapper(); // call method by name
155 } else { // method not found/implemented
156 if ($_SERVER["REQUEST_METHOD"] == "LOCK") {
157 $this->http_status("412 Precondition failed");
159 $this->http_status("405 Method not allowed");
160 header("Allow: ".join(", ", $this->_allow())); // tell client what's allowed
167 // {{{ abstract WebDAV methods
173 * overload this method to retrieve resources from your server
178 * @param array &$params Array of input and output parameters
179 * <br><b>input</b><ul>
182 * <br><b>output</b><ul>
185 * @returns int HTTP-Statuscode
189 function GET(&$params)
191 // dummy entry for PHPDoc
204 * @param array &$params
205 * @returns int HTTP-Statuscode
211 // dummy entry for PHPDoc
220 * COPY implementation
222 * COPY implementation
225 * @param array &$params
226 * @returns int HTTP-Statuscode
232 // dummy entry for PHPDoc
241 * MOVE implementation
243 * MOVE implementation
246 * @param array &$params
247 * @returns int HTTP-Statuscode
253 // dummy entry for PHPDoc
262 * DELETE implementation
264 * DELETE implementation
267 * @param array &$params
268 * @returns int HTTP-Statuscode
274 // dummy entry for PHPDoc
282 * PROPFIND implementation
284 * PROPFIND implementation
287 * @param array &$params
288 * @returns int HTTP-Statuscode
294 // dummy entry for PHPDoc
303 * PROPPATCH implementation
305 * PROPPATCH implementation
308 * @param array &$params
309 * @returns int HTTP-Statuscode
315 // dummy entry for PHPDoc
323 * LOCK implementation
325 * LOCK implementation
328 * @param array &$params
329 * @returns int HTTP-Statuscode
335 // dummy entry for PHPDoc
343 * UNLOCK implementation
345 * UNLOCK implementation
348 * @param array &$params
349 * @returns int HTTP-Statuscode
355 // dummy entry for PHPDoc
362 // {{{ other abstract methods
367 * check authentication
369 * overload this method to retrieve and confirm authentication information
372 * @param string type Authentication type, e.g. "basic" or "digest"
373 * @param string username Transmitted username
374 * @param string passwort Transmitted password
375 * @returns bool Authentication status
379 function checkAuth($type, $username, $password)
381 // dummy entry for PHPDoc
390 * check lock status for a resource
392 * overload this method to return shared and exclusive locks
393 * active for this resource
396 * @param string resource Resource path to check
397 * @returns array An array of lock entries each consisting
398 * of 'type' ('shared'/'exclusive'), 'token' and 'timeout'
402 function checklock($resource)
404 // dummy entry for PHPDoc
412 // {{{ WebDAV HTTP method wrappers
414 // {{{ http_OPTIONS()
417 * OPTIONS method handler
419 * The OPTIONS method handler creates a valid OPTIONS reply
420 * including Dav: and Allowed: heaers
421 * based on the implemented methods found in the actual instance
426 function http_OPTIONS()
428 // Microsoft clients default to the Frontpage protocol
429 // unless we tell them to use WebDAV
430 header("MS-Author-Via: DAV");
432 // get allowed methods
433 $allow = $this->_allow();
436 $dav = array(1); // assume we are always dav class 1 compliant
437 if (isset($allow['LOCK'])) {
438 $dav[] = 2; // dav class 2 requires that locking is supported
441 // tell clients what we found
442 $this->http_status("200 OK");
443 header("DAV: " .join("," , $dav));
444 header("Allow: ".join(", ", $allow));
450 // {{{ http_PROPFIND()
453 * PROPFIND method handler
458 function http_PROPFIND()
461 $options["path"] = $this->path
;
463 // search depth from header (default is "infinity)
464 if (isset($_SERVER['HTTP_DEPTH'])) {
465 $options["depth"] = $_SERVER["HTTP_DEPTH"];
467 $options["depth"] = "infinity";
470 // analyze request payload
471 $propinfo = new _parse_propfind("php://input");
472 if (!$propinfo->success
) {
473 $this->http_status("400 Error");
476 $options['props'] = $propinfo->props
;
479 if (!$this->propfind($options, $files)) {
480 $this->http_status("404 Not Found");
484 // collect namespaces here
487 // Microsoft Clients need this special namespace for date and time values
488 $ns_defs = "xmlns:ns0=\"urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\"";
490 // now we loop over all returned file entries
491 foreach($files["files"] as $filekey => $file) {
493 // nothing to do if no properties were returend for a file
494 if (!isset($file["props"]) ||
!is_array($file["props"])) {
498 // now loop over all returned properties
499 foreach($file["props"] as $key => $prop) {
500 // as a convenience feature we do not require that user handlers
501 // restrict returned properties to the requested ones
502 // here we strip all unrequested entries out of the response
504 switch($options['props']) {
510 // only the names of all existing properties were requested
511 // so we remove all values
512 unset($files["files"][$filekey]["props"][$key]["val"]);
518 // search property name in requested properties
519 foreach((array)$options["props"] as $reqprop) {
520 if ( $reqprop["name"] == $prop["name"]
521 && $reqprop["xmlns"] == $prop["ns"]) {
527 // unset property and continue with next one if not found/requested
529 $files["files"][$filekey]["props"][$key]="";
535 // namespace handling
536 if (empty($prop["ns"])) continue; // no namespace
538 if ($ns == "DAV:") continue; // default namespace
539 if (isset($ns_hash[$ns])) continue; // already known
541 // register namespace
542 $ns_name = "ns".(count($ns_hash) +
1);
543 $ns_hash[$ns] = $ns_name;
544 $ns_defs .= " xmlns:$ns_name=\"$ns\"";
547 // we also need to add empty entries for properties that were requested
548 // but for which no values where returned by the user handler
549 if (is_array($options['props'])) {
550 foreach($options["props"] as $reqprop) {
551 if($reqprop['name']=="") continue; // skip empty entries
555 // check if property exists in result
556 foreach($file["props"] as $prop) {
557 if ( $reqprop["name"] == $prop["name"]
558 && $reqprop["xmlns"] == $prop["ns"]) {
565 if($reqprop["xmlns"]==="DAV:" && $reqprop["name"]==="lockdiscovery") {
566 // lockdiscovery is handled by the base class
567 $files["files"][$filekey]["props"][]
568 = $this->mkprop("DAV:",
570 $this->lockdiscovery($files["files"][$filekey]['path']));
572 // add empty value for this property
573 $files["files"][$filekey]["noprops"][] =
574 $this->mkprop($reqprop["xmlns"], $reqprop["name"], "");
576 // register property namespace if not known yet
577 if ($reqprop["xmlns"] != "DAV:" && !isset($ns_hash[$reqprop["xmlns"]])) {
578 $ns_name = "ns".(count($ns_hash) +
1);
579 $ns_hash[$reqprop["xmlns"]] = $ns_name;
580 $ns_defs .= " xmlns:$ns_name=\"$reqprop[xmlns]\"";
588 // now we generate the reply header ...
589 $this->http_status("207 Multi-Status");
590 header('Content-Type: text/xml; charset="utf-8"');
593 echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
594 echo "<D:multistatus xmlns:D=\"DAV:\">\n";
596 foreach($files["files"] as $file) {
597 // ignore empty or incomplete entries
598 if(!is_array($file) ||
empty($file) ||
!isset($file["path"])) continue;
599 $path = $file['path'];
600 if(!is_string($path) ||
$path==="") continue;
602 echo " <D:response $ns_defs>\n";
604 $href = (@$_SERVER["HTTPS"] === "on" ?
"https:" : "http:");
605 $href.= "//".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
607 //TODO make sure collection resource pathes end in a trailing slash
609 echo " <D:href>$href</D:href>\n";
611 // report all found properties and their values (if any)
612 if (isset($file["props"]) && is_array($file["props"])) {
613 echo " <D:propstat>\n";
616 foreach($file["props"] as $key => $prop) {
618 if (!is_array($prop)) continue;
619 if (!isset($prop["name"])) continue;
621 if (!isset($prop["val"]) ||
$prop["val"] === "" ||
$prop["val"] === false
) {
622 // empty properties (cannot use empty() for check as "0" is a legal value here)
623 if($prop["ns"]=="DAV:") {
624 echo " <D:$prop[name]/>\n";
625 } else if(!empty($prop["ns"])) {
626 echo " <".$ns_hash[$prop["ns"]].":$prop[name]/>\n";
628 echo " <$prop[name] xmlns=\"\"/>";
630 } else if ($prop["ns"] == "DAV:") {
631 // some WebDAV properties need special treatment
632 switch ($prop["name"]) {
634 echo " <D:creationdate ns0:dt=\"dateTime.tz\">"
635 . gmdate("Y-m-d\\TH:i:s\\Z",$prop['val'])
636 . "</D:creationdate>\n";
638 case "getlastmodified":
639 echo " <D:getlastmodified ns0:dt=\"dateTime.rfc1123\">"
640 . gmdate("D, d M Y H:i:s ", $prop['val'])
641 . "GMT</D:getlastmodified>\n";
644 echo " <D:resourcetype><D:$prop[val]/></D:resourcetype>\n";
646 case "supportedlock":
647 echo " <D:supportedlock>$prop[val]</D:supportedlock>\n";
649 case "lockdiscovery":
650 echo " <D:lockdiscovery>\n";
652 echo " </D:lockdiscovery>\n";
655 echo " <D:$prop[name]>"
656 . $this->_prop_encode(htmlspecialchars($prop['val']))
657 . "</D:$prop[name]>\n";
661 // properties from namespaces != "DAV:" or without any namespace
663 echo " <" . $ns_hash[$prop["ns"]] . ":$prop[name]>"
664 . $this->_prop_encode(htmlspecialchars($prop['val']))
665 . "</" . $ns_hash[$prop["ns"]] . ":$prop[name]>\n";
667 echo " <$prop[name] xmlns=\"\">"
668 . $this->_prop_encode(htmlspecialchars($prop['val']))
669 . "</$prop[name]>\n";
675 echo " <D:status>HTTP/1.1 200 OK</D:status>\n";
676 echo " </D:propstat>\n";
679 // now report all properties requested bot not found
680 if (isset($file["noprops"])) {
681 echo " <D:propstat>\n";
684 foreach($file["noprops"] as $key => $prop) {
685 if ($prop["ns"] == "DAV:") {
686 echo " <D:$prop[name]/>\n";
687 } else if ($prop["ns"] == "") {
688 echo " <$prop[name] xmlns=\"\"/>\n";
690 echo " <" . $ns_hash[$prop["ns"]] . ":$prop[name]/>\n";
695 echo " <D:status>HTTP/1.1 404 Not Found</D:status>\n";
696 echo " </D:propstat>\n";
699 echo " </D:response>\n";
702 echo "</D:multistatus>\n";
708 // {{{ http_PROPPATCH()
711 * PROPPATCH method handler
716 function http_PROPPATCH()
718 if($this->_check_lock_status($this->path
)) {
720 $options["path"] = $this->path
;
722 $propinfo = new _parse_proppatch("php://input");
724 if (!$propinfo->success
) {
725 $this->http_status("400 Error");
729 $options['props'] = $propinfo->props
;
731 $responsedescr = $this->proppatch($options);
733 $this->http_status("207 Multi-Status");
734 header('Content-Type: text/xml; charset="utf-8"');
736 echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
738 echo "<D:multistatus xmlns:D=\"DAV:\">\n";
739 echo " <D:response>\n";
740 echo " <D:href>".$this->_urlencode($_SERVER["SCRIPT_NAME"].$this->path
)."</D:href>\n";
742 foreach($options["props"] as $prop) {
743 echo " <D:propstat>\n";
744 echo " <D:prop><$prop[name] xmlns=\"$prop[ns]\"/></D:prop>\n";
745 echo " <D:status>HTTP/1.1 $prop[status]</D:status>\n";
746 echo " </D:propstat>\n";
749 if ($responsedescr) {
750 echo " <D:responsedescription>".
751 $this->_prop_encode(htmlspecialchars($responsedescr)).
752 "</D:responsedescription>\n";
755 echo " </D:response>\n";
756 echo "</D:multistatus>\n";
758 $this->http_status("423 Locked");
768 * MKCOL method handler
773 function http_MKCOL()
776 $options["path"] = $this->path
;
778 $stat = $this->mkcol($options);
780 $this->http_status($stat);
796 // TODO check for invalid stream
798 $options["path"] = $this->path
;
800 $this->_get_ranges($options);
802 if (true
=== ($status = $this->get($options))) {
803 if (!headers_sent()) {
806 if (!isset($options['mimetype'])) {
807 $options['mimetype'] = "application/octet-stream";
809 header("Content-type: $options[mimetype]");
811 if (isset($options['mtime'])) {
812 header("Last-modified:".gmdate("D, d M Y H:i:s ", $options['mtime'])."GMT");
815 if (isset($options['stream'])) {
816 // GET handler returned a stream
817 if (!empty($options['ranges']) && (0===fseek($options['stream'], 0, SEEK_SET
))) {
818 // partial request and stream is seekable
820 if (count($options['ranges']) === 1) {
821 $range = $options['ranges'][0];
823 if (isset($range['start'])) {
824 fseek($options['stream'], $range['start'], SEEK_SET
);
825 if (feof($options['stream'])) {
826 http_status("416 Requested range not satisfiable");
830 if (isset($range['end'])) {
831 $size = $range['end']-$range['start']+
1;
832 http_status("206 partial");
833 header("Content-length: $size");
834 header("Content-range: $range[start]-$range[end]/"
835 . (isset($options['size']) ?
$options['size'] : "*"));
836 while ($size && !feof($options['stream'])) {
837 $buffer = fread($options['stream'], 4096);
838 $size -= strlen($buffer);
842 http_status("206 partial");
843 if (isset($options['size'])) {
844 header("Content-length: ".($options['size'] - $range['start']));
845 header("Content-range: $start-$end/"
846 . (isset($options['size']) ?
$options['size'] : "*"));
848 fpassthru($options['stream']);
851 header("Content-length: ".$range['last']);
852 fseek($options['stream'], -$range['last'], SEEK_END
);
853 fpassthru($options['stream']);
856 $this->_multipart_byterange_header(); // init multipart
857 foreach ($options['ranges'] as $range) {
858 // TODO what if size unknown? 500?
859 if (isset($range['start'])) {
860 $from = $range['start'];
861 $to = !empty($range['end']) ?
$range['end'] : $options['size']-1;
863 $from = $options['size'] - $range['last']-1;
864 $to = $options['size'] -1;
866 $total = isset($options['size']) ?
$options['size'] : "*";
867 $size = $to - $from +
1;
868 $this->_multipart_byterange_header($options['mimetype'], $from, $to, $total);
871 fseek($options['stream'], $start, SEEK_SET
);
872 while ($size && !feof($options['stream'])) {
873 $buffer = fread($options['stream'], 4096);
874 $size -= strlen($buffer);
878 $this->_multipart_byterange_header(); // end multipart
881 // normal request or stream isn't seekable, return full content
882 if (isset($options['size'])) {
883 header("Content-length: ".$options['size']);
885 fpassthru($options['stream']);
886 return; // no more headers
888 } elseif (isset($options['data'])) {
889 if (is_array($options['data'])) {
890 // reply to partial request
892 header("Content-length: ".strlen($options['data']));
893 echo $options['data'];
899 if (false
=== $status) {
900 $this->http_status("404 not found");
903 $this->http_status("$status");
908 * parse HTTP Range: header
910 * @param array options array to store result in
913 function _get_ranges(&$options)
915 // process Range: header if present
916 if (isset($_SERVER['HTTP_RANGE'])) {
918 // we only support standard "bytes" range specifications for now
919 if (ereg("bytes[[:space:]]*=[[:space:]]*(.+)", $_SERVER['HTTP_RANGE'], $matches)) {
920 $options["ranges"] = array();
922 // ranges are comma separated
923 foreach (explode(",", $matches[1]) as $range) {
924 // ranges are either from-to pairs or just end positions
925 list($start, $end) = explode("-", $range);
926 $options["ranges"][] = ($start==="")
927 ?
array("last"=>$end)
928 : array("start"=>$start, "end"=>$end);
935 * generate separator headers for multipart response
937 * first and last call happen without parameters to generate
938 * the initial header and closing sequence, all calls inbetween
939 * require content mimetype, start and end byte position and
940 * optionaly the total byte length of the requested resource
942 * @param string mimetype
943 * @param int start byte position
944 * @param int end byte position
945 * @param int total resource byte size
947 function _multipart_byterange_header($mimetype = false
, $from = false
, $to=false
, $total=false
)
949 if ($mimetype === false
) {
950 if (!isset($this->multipart_separator
)) {
953 // a little naive, this sequence *might* be part of the content
954 // but it's really not likely and rather expensive to check
955 $this->multipart_separator
= "SEPARATOR_".md5(microtime());
957 // generate HTTP header
958 header("Content-type: multipart/byteranges; boundary=".$this->multipart_separator
);
962 // generate closing multipart sequence
963 echo "\n--{$this->multipart_separator}--";
966 // generate separator and header for next part
967 echo "\n--{$this->multipart_separator}\n";
968 echo "Content-type: $mimetype\n";
969 echo "Content-range: $from-$to/". ($total === false ?
"*" : $total);
981 * HEAD method handler
990 $options["path"] = $this->path
;
992 if (method_exists($this, "HEAD")) {
993 $status = $this->head($options);
994 } else if (method_exists($this, "GET")) {
996 $status = $this->GET($options);
1000 if($status===true
) $status = "200 OK";
1001 if($status===false
) $status = "404 Not found";
1003 $this->http_status($status);
1011 * PUT method handler
1018 if ($this->_check_lock_status($this->path
)) {
1020 $options["path"] = $this->path
;
1021 $options["content_length"] = $_SERVER["CONTENT_LENGTH"];
1023 // get the Content-type
1024 if (isset($_SERVER["CONTENT_TYPE"])) {
1025 // for now we do not support any sort of multipart requests
1026 if (!strncmp($_SERVER["CONTENT_TYPE"], "multipart/", 10)) {
1027 $this->http_status("501 not implemented");
1028 echo "The service does not support mulipart PUT requests";
1031 $options["content_type"] = $_SERVER["CONTENT_TYPE"];
1033 // default content type if none given
1034 $options["content_type"] = "application/octet-stream";
1037 /* RFC 2616 2.6 says: "The recipient of the entity MUST NOT
1038 ignore any Content-* (e.g. Content-Range) headers that it
1039 does not understand or implement and MUST return a 501
1040 (Not Implemented) response in such cases."
1042 foreach ($_SERVER as $key => $val) {
1043 if (strncmp($key, "HTTP_CONTENT", 11)) continue;
1045 case 'HTTP_CONTENT_ENCODING': // RFC 2616 14.11
1046 // TODO support this if ext/zlib filters are available
1047 $this->http_status("501 not implemented");
1048 echo "The service does not support '$val' content encoding";
1051 case 'HTTP_CONTENT_LANGUAGE': // RFC 2616 14.12
1052 // we assume it is not critical if this one is ignored
1053 // in the actual PUT implementation ...
1054 $options["content_language"] = $value;
1057 case 'HTTP_CONTENT_LOCATION': // RFC 2616 14.14
1058 /* The meaning of the Content-Location header in PUT
1059 or POST requests is undefined; servers are free
1060 to ignore it in those cases. */
1063 case 'HTTP_CONTENT_RANGE': // RFC 2616 14.16
1064 // single byte range requests are supported
1065 // the header format is also specified in RFC 2616 14.16
1066 // TODO we have to ensure that implementations support this or send 501 instead
1067 if (!preg_match('@bytes\s+(\d+)-(\d+)/((\d+)|\*)@', $value, $matches)) {
1068 $this->http_status("400 bad request");
1069 echo "The service does only support single byte ranges";
1073 $range = array("start"=>$matches[1], "end"=>$matches[2]);
1074 if (is_numeric($matches[3])) {
1075 $range["total_length"] = $matches[3];
1077 $option["ranges"][] = $range;
1079 // TODO make sure the implementation supports partial PUT
1080 // this has to be done in advance to avoid data being overwritten
1081 // on implementations that do not support this ...
1084 case 'HTTP_CONTENT_MD5': // RFC 2616 14.15
1085 // TODO: maybe we can just pretend here?
1086 $this->http_status("501 not implemented");
1087 echo "The service does not support content MD5 checksum verification";
1091 // any other unknown Content-* headers
1092 $this->http_status("501 not implemented");
1093 echo "The service does not support '$key'";
1098 $options["stream"] = fopen("php://input", "r");
1100 $stat = $this->PUT($options);
1102 if (is_resource($stat) && get_resource_type($stat) == "stream") {
1104 if (!empty($options["ranges"])) {
1105 // TODO multipart support is missing (see also above)
1106 // TODO error checking
1107 $stat = fseek($stream, $range[0]["start"], SEEK_SET
);
1108 fwrite($stream, fread($options["stream"], $range[0]["end"]-$range[0]["start"]+
1));
1110 while (!feof($options["stream"])) {
1111 fwrite($stream, fread($options["stream"], 4096));
1116 $stat = $options["new"] ?
"201 Created" : "204 No Content";
1119 $this->http_status($stat);
1121 $this->http_status("423 Locked");
1128 // {{{ http_DELETE()
1131 * DELETE method handler
1136 function http_DELETE()
1138 // check RFC 2518 Section 9.2, last paragraph
1139 if (isset($_SERVER["HTTP_DEPTH"])) {
1140 if ($_SERVER["HTTP_DEPTH"] != "infinity") {
1141 $this->http_status("400 Bad Request");
1146 // check lock status
1147 if ($this->_check_lock_status($this->path
)) {
1150 $options["path"] = $this->path
;
1152 $stat = $this->delete($options);
1154 $this->http_status($stat);
1156 // sorry, its locked
1157 $this->http_status("423 Locked");
1166 * COPY method handler
1171 function http_COPY()
1173 // no need to check source lock status here
1174 // destination lock status is always checked by the helper method
1175 $this->_copymove("copy");
1183 * MOVE method handler
1188 function http_MOVE()
1190 if ($this->_check_lock_status($this->path
)) {
1191 // destination lock status is always checked by the helper method
1192 $this->_copymove("move");
1194 $this->http_status("423 Locked");
1204 * LOCK method handler
1209 function http_LOCK()
1212 $options["path"] = $this->path
;
1214 if (isset($_SERVER['HTTP_DEPTH'])) {
1215 $options["depth"] = $_SERVER["HTTP_DEPTH"];
1217 $options["depth"] = "infinity";
1220 if (isset($_SERVER["HTTP_TIMEOUT"])) {
1221 $options["timeout"] = explode(",", $_SERVER["HTTP_TIMEOUT"]);
1224 if(empty($_SERVER['CONTENT_LENGTH']) && !empty($_SERVER['HTTP_IF'])) {
1225 // check if locking is possible
1226 if(!$this->_check_lock_status($this->path
)) {
1227 $this->http_status("423 Locked");
1232 $options["update"] = substr($_SERVER['HTTP_IF'], 2, -2);
1233 $stat = $this->lock($options);
1235 // extract lock request information from request XML payload
1236 $lockinfo = new _parse_lockinfo("php://input");
1237 if (!$lockinfo->success
) {
1238 $this->http_status("400 bad request");
1241 // check if locking is possible
1242 if(!$this->_check_lock_status($this->path
, $lockinfo->lockscope
=== "shared")) {
1243 $this->http_status("423 Locked");
1248 $options["scope"] = $lockinfo->lockscope
;
1249 $options["type"] = $lockinfo->locktype
;
1250 $options["owner"] = $lockinfo->owner
;
1252 $options["locktoken"] = $this->_new_locktoken();
1254 $stat = $this->lock($options);
1257 if(is_bool($stat)) {
1258 $http_stat = $stat ?
"200 OK" : "423 Locked";
1263 $this->http_status($http_stat);
1265 if ($http_stat{0} == 2) { // 2xx states are ok
1266 if($options["timeout"]) {
1267 // more than a million is considered an absolute timestamp
1268 // less is more likely a relative value
1269 if($options["timeout"]>1000000) {
1270 $timeout = "Second-".($options['timeout']-time());
1272 $timeout = "Second-$options[timeout]";
1275 $timeout = "Infinite";
1278 header('Content-Type: text/xml; charset="utf-8"');
1279 header("Lock-Token: <$options[locktoken]>");
1280 echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
1281 echo "<D:prop xmlns:D=\"DAV:\">\n";
1282 echo " <D:lockdiscovery>\n";
1283 echo " <D:activelock>\n";
1284 echo " <D:lockscope><D:$options[scope]/></D:lockscope>\n";
1285 echo " <D:locktype><D:$options[type]/></D:locktype>\n";
1286 echo " <D:depth>$options[depth]</D:depth>\n";
1287 echo " <D:owner>$options[owner]</D:owner>\n";
1288 echo " <D:timeout>$timeout</D:timeout>\n";
1289 echo " <D:locktoken><D:href>$options[locktoken]</D:href></D:locktoken>\n";
1290 echo " </D:activelock>\n";
1291 echo " </D:lockdiscovery>\n";
1292 echo "</D:prop>\n\n";
1299 // {{{ http_UNLOCK()
1302 * UNLOCK method handler
1307 function http_UNLOCK()
1310 $options["path"] = $this->path
;
1312 if (isset($_SERVER['HTTP_DEPTH'])) {
1313 $options["depth"] = $_SERVER["HTTP_DEPTH"];
1315 $options["depth"] = "infinity";
1318 // strip surrounding <>
1319 $options["token"] = substr(trim($_SERVER["HTTP_LOCK_TOKEN"]), 1, -1);
1322 $stat = $this->unlock($options);
1324 $this->http_status($stat);
1333 function _copymove($what)
1336 $options["path"] = $this->path
;
1338 if (isset($_SERVER["HTTP_DEPTH"])) {
1339 $options["depth"] = $_SERVER["HTTP_DEPTH"];
1341 $options["depth"] = "infinity";
1344 extract(parse_url($_SERVER["HTTP_DESTINATION"]));
1346 if (isset($port) && $port != 80)
1347 $http_host.= ":$port";
1349 list($http_header_host,$http_header_port) = explode(":",$_SERVER["HTTP_HOST"]);
1350 if (isset($http_header_port) && $http_header_port != 80) {
1351 $http_header_host .= ":".$http_header_port;
1354 if ($http_host == $http_header_host &&
1355 !strncmp($_SERVER["SCRIPT_NAME"], $path,
1356 strlen($_SERVER["SCRIPT_NAME"]))) {
1357 $options["dest"] = substr($path, strlen($_SERVER["SCRIPT_NAME"]));
1358 if (!$this->_check_lock_status($options["dest"])) {
1359 $this->http_status("423 Locked");
1364 $options["dest_url"] = $_SERVER["HTTP_DESTINATION"];
1367 // see RFC 2518 Sections 9.6, 8.8.4 and 8.9.3
1368 if (isset($_SERVER["HTTP_OVERWRITE"])) {
1369 $options["overwrite"] = $_SERVER["HTTP_OVERWRITE"] == "T";
1371 $options["overwrite"] = true
;
1374 $stat = $this->$what($options);
1375 $this->http_status($stat);
1383 * check for implemented HTTP methods
1386 * @return array something
1390 // OPTIONS is always there
1391 $allow = array("OPTIONS" =>"OPTIONS");
1393 // all other METHODS need both a http_method() wrapper
1394 // and a method() implementation
1395 // the base class supplies wrappers only
1396 foreach(get_class_methods($this) as $method) {
1397 if (!strncmp("http_", $method, 5)) {
1398 $method = strtoupper(substr($method, 5));
1399 if (method_exists($this, $method)) {
1400 $allow[$method] = $method;
1405 // we can emulate a missing HEAD implemetation using GET
1406 if (isset($allow["GET"]))
1407 $allow["HEAD"] = "HEAD";
1409 // no LOCK without checklok()
1410 if (!method_exists($this, "checklock")) {
1411 unset($allow["LOCK"]);
1412 unset($allow["UNLOCK"]);
1421 * helper for property element creation
1423 * @param string XML namespace (optional)
1424 * @param string property name
1425 * @param string property value
1426 * @return array property array
1430 $args = func_get_args();
1431 if (count($args) == 3) {
1432 return array("ns" => $args[0],
1436 return array("ns" => "DAV:",
1445 * check authentication if check is implemented
1448 * @return bool true if authentication succeded or not necessary
1450 function _check_auth()
1452 if (method_exists($this, "checkAuth")) {
1453 // PEAR style method name
1454 return $this->checkAuth(@$_SERVER["AUTH_TYPE"],
1455 @$_SERVER["PHP_AUTH_USER"],
1456 @$_SERVER["PHP_AUTH_PW"]);
1457 } else if (method_exists($this, "check_auth")) {
1458 // old (pre 1.0) method name
1459 return $this->check_auth(@$_SERVER["AUTH_TYPE"],
1460 @$_SERVER["PHP_AUTH_USER"],
1461 @$_SERVER["PHP_AUTH_PW"]);
1463 // no method found -> no authentication required
1473 * generate Unique Universal IDentifier for lock token
1476 * @return string a new UUID
1478 function _new_uuid()
1480 // use uuid extension from PECL if available
1481 if (function_exists("uuid_create")) {
1482 return uuid_create();
1486 $uuid = md5(microtime().getmypid()); // this should be random enough for now
1488 // set variant and version fields for 'true' random uuid
1490 $n = 8 +
(ord($uuid{16}) & 3);
1491 $hex = "0123456789abcdef";
1492 $uuid{16} = $hex{$n};
1494 // return formated uuid
1495 return substr($uuid, 0, 8)."-"
1496 . substr($uuid, 8, 4)."-"
1497 . substr($uuid, 12, 4)."-"
1498 . substr($uuid, 16, 4)."-"
1499 . substr($uuid, 20);
1503 * create a new opaque lock token as defined in RFC2518
1506 * @return string new RFC2518 opaque lock token
1508 function _new_locktoken()
1510 return "opaquelocktoken:".$this->_new_uuid();
1515 // {{{ WebDAV If: header parsing
1520 * @param string header string to parse
1521 * @param int current parsing position
1522 * @return array next token (type and value)
1524 function _if_header_lexer($string, &$pos)
1527 while (ctype_space($string{$pos})) {
1531 // already at end of string?
1532 if (strlen($string) <= $pos) {
1536 // get next character
1537 $c = $string{$pos++
};
1539 // now it depends on what we found
1542 // URIs are enclosed in <...>
1543 $pos2 = strpos($string, ">", $pos);
1544 $uri = substr($string, $pos, $pos2 - $pos);
1546 return array("URI", $uri);
1549 //Etags are enclosed in [...]
1550 if ($string{$pos} == "W") {
1551 $type = "ETAG_WEAK";
1554 $type = "ETAG_STRONG";
1556 $pos2 = strpos($string, "]", $pos);
1557 $etag = substr($string, $pos +
1, $pos2 - $pos - 2);
1559 return array($type, $etag);
1562 // "N" indicates negation
1564 return array("NOT", "Not");
1567 // anything else is passed verbatim char by char
1568 return array("CHAR", $c);
1575 * @param string header string
1576 * @return array URIs and their conditions
1578 function _if_header_parser($str)
1581 $len = strlen($str);
1586 while ($pos < $len) {
1588 $token = $this->_if_header_lexer($str, $pos);
1591 if ($token[0] == "URI") {
1592 $uri = $token[1]; // remember URI
1593 $token = $this->_if_header_lexer($str, $pos); // get next token
1599 if ($token[0] != "CHAR" ||
$token[1] != "(") {
1607 $token = $this->_if_header_lexer($str, $pos);
1608 if ($token[0] == "NOT") {
1612 switch ($token[0]) {
1614 switch ($token[1]) {
1627 $list[] = $not."<$token[1]>";
1631 $list[] = $not."[W/'$token[1]']>";
1635 $list[] = $not."['$token[1]']>";
1644 if (@is_array
($uris[$uri])) {
1645 $uris[$uri] = array_merge($uris[$uri],$list);
1647 $uris[$uri] = $list;
1655 * check if conditions from "If:" headers are meat
1657 * the "If:" header is an extension to HTTP/1.1
1658 * defined in RFC 2518 section 9.4
1663 function _check_if_header_conditions()
1665 if (isset($_SERVER["HTTP_IF"])) {
1666 $this->_if_header_uris
=
1667 $this->_if_header_parser($_SERVER["HTTP_IF"]);
1669 foreach($this->_if_header_uris
as $uri => $conditions) {
1671 // default uri is the complete request uri
1672 $uri = (@$_SERVER["HTTPS"] === "on" ?
"https:" : "http:");
1673 $uri.= "//$_SERVER[HTTP_HOST]$_SERVER[SCRIPT_NAME]$_SERVER[PATH_INFO]";
1677 foreach($conditions as $condition) {
1678 // lock tokens may be free form (RFC2518 6.3)
1679 // but if opaquelocktokens are used (RFC2518 6.4)
1680 // we have to check the format (litmus tests this)
1681 if (!strncmp($condition, "<opaquelocktoken:", strlen("<opaquelocktoken"))) {
1682 if (!ereg("^<opaquelocktoken:[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}>$", $condition)) {
1686 if (!$this->_check_uri_condition($uri, $condition)) {
1693 if ($state == true
) {
1703 * Check a single URI condition parsed from an if-header
1705 * Check a single URI condition parsed from an if-header
1708 * @param string $uri URI to check
1709 * @param string $condition Condition to check for this URI
1710 * @returns bool Condition check result
1712 function _check_uri_condition($uri, $condition)
1714 // not really implemented here,
1715 // implementations must override
1723 * @param string path of resource to check
1724 * @param bool exclusive lock?
1726 function _check_lock_status($path, $exclusive_only = false
)
1728 // FIXME depth -> ignored for now
1729 if (method_exists($this, "checkLock")) {
1731 $lock = $this->checkLock($path);
1733 // ... and lock is not owned?
1734 if (is_array($lock) && count($lock)) {
1735 // FIXME doesn't check uri restrictions yet
1736 if (!strstr($_SERVER["HTTP_IF"], $lock["token"])) {
1737 if (!$exclusive_only ||
($lock["scope"] !== "shared"))
1750 * Generate lockdiscovery reply from checklock() result
1752 * @param string resource path to check
1753 * @return string lockdiscovery response
1755 function lockdiscovery($path)
1757 // no lock support without checklock() method
1758 if (!method_exists($this, "checklock")) {
1762 // collect response here
1765 // get checklock() reply
1766 $lock = $this->checklock($path);
1768 // generate <activelock> block for returned data
1769 if (is_array($lock) && count($lock)) {
1770 // check for 'timeout' or 'expires'
1771 if (!empty($lock["expires"])) {
1772 $timeout = "Second-".($lock["expires"] - time());
1773 } else if (!empty($lock["timeout"])) {
1774 $timeout = "Second-$lock[timeout]";
1776 $timeout = "Infinite";
1779 // genreate response block
1782 <D:lockscope><D:$lock[scope]/></D:lockscope>
1783 <D:locktype><D:$lock[type]/></D:locktype>
1784 <D:depth>$lock[depth]</D:depth>
1785 <D:owner>$lock[owner]</D:owner>
1786 <D:timeout>$timeout</D:timeout>
1787 <D:locktoken><D:href>$lock[token]</D:href></D:locktoken>
1792 // return generated response
1793 return $activelocks;
1797 * set HTTP return status and mirror it in a private header
1799 * @param string status code and message
1802 function http_status($status)
1804 // simplified success case
1805 if($status === true
) {
1810 $this->_http_status
= $status;
1812 // generate HTTP status response
1813 header("HTTP/1.1 $status");
1814 header("X-WebDAV-Status: $status", true
);
1818 * private minimalistic version of PHP urlencode()
1820 * only blanks and XML special chars must be encoded here
1821 * full urlencode() encoding confuses some clients ...
1823 * @param string URL to encode
1824 * @return string encoded URL
1826 function _urlencode($url)
1828 return strtr($url, array(" "=>"%20",
1836 * private version of PHP urldecode
1838 * not really needed but added for completenes
1840 * @param string URL to decode
1841 * @return string decoded URL
1843 function _urldecode($path)
1845 return urldecode($path);
1849 * UTF-8 encode property values if not already done so
1851 * @param string text to encode
1852 * @return string utf-8 encoded text
1854 function _prop_encode($text)
1856 switch (strtolower($this->_prop_encoding
)) {
1863 return utf8_encode($text);