cvs: pear /HTTP_WebDAV_Server HTTP_WebDAV_Server.php HTTP_WebDAV_Server_file.php Server.php file.php htaccess package.xml /HTTP_WebDAV_Server/Server
Filesystem.php _parse_lockinfo.php _parse_propfind.php _parse_proppatch.php /HTTP_WebDAV_Server/tests split_log.php test.sh testdb.sql
| From: | Hartmut Holzgraefe | Date: | Mon, 10 Feb 2003 00:34:17 +0000 |
| Subject: | cvs: pear /HTTP_WebDAV_Server HTTP_WebDAV_Server.php HTTP_WebDAV_Server_file.php Server.php file.php htaccess package.xml /HTTP_WebDAV_Server/Server Filesystem.php _parse_lockinfo.php _parse_propfind.php _parse_proppatch.php /HTTP_WebDAV_Server/tests split_log.php test.sh testdb.sql |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-8790@lists.php.net to get a copy of this message | ||
hholzgra Sun Feb 9 19:34:17 2003 EDT
Added files:
/pear/HTTP_WebDAV_Server Server.php package.xml
/pear/HTTP_WebDAV_Server/Server Filesystem.php _parse_lockinfo.php
_parse_propfind.php
_parse_proppatch.php
/pear/HTTP_WebDAV_Server/tests split_log.php test.sh testdb.sql
Removed files:
/pear/HTTP_WebDAV_Server HTTP_WebDAV_Server.php
HTTP_WebDAV_Server_file.php htaccess
Modified files:
/pear/HTTP_WebDAV_Server file.php
Log:
- more PEARification
- package description file added
- sample file server now passes all but one litmus testcase
Index: pear/HTTP_WebDAV_Server/file.php diff -u pear/HTTP_WebDAV_Server/file.php:1.1 pear/HTTP_WebDAV_Server/file.php:1.2 --- pear/HTTP_WebDAV_Server/file.php:1.1 Wed Sep 18 05:57:10 2002 +++ pear/HTTP_WebDAV_Server/file.php Sun Feb 9 19:34:17 2003 @@ -1,8 +1,6 @@ <?php - - require_once "HTTP_WebDAV_Server.php"; - require_once "HTTP_WebDAV_Server_file.php"; - - new dav_fileserver("/usr/local/httpd/htdocs/tmp"); - -?> + ini_set("include_path", ini_get("include_path").":/usr/local/apache/htdocs"); + require_once "HTTP/WebDAV/Server/Filesystem.php"; + $server = new HTTP_WebDAV_Server_Filesystem(); + $server->ServeRequest($_SERVER["DOCUMENT_ROOT"]); +?> \ No newline at end of file Index: pear/HTTP_WebDAV_Server/Server.php +++ pear/HTTP_WebDAV_Server/Server.php <?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2003 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Hartmut Holzgraefe <hholzgra@php.net> | // | Christian Stocker <chregu@bitflux.ch> | // +----------------------------------------------------------------------+ // // $Id: Server.php,v 1.1 2003/02/10 00:34:17 hholzgra Exp $ // // WebDAV server base class, needs to be extended to do useful work // // require_once "HTTP/HTTP.php"; require_once "HTTP/WebDAV/Server/_parse_propfind.php"; require_once "HTTP/WebDAV/Server/_parse_proppatch.php"; require_once "HTTP/WebDAV/Server/_parse_lockinfo.php"; /** * Virtual base class for implementing WebDAV servers * * this is it * bla bla * * @package HTTP_WebDAV_Server * @author Hartmut Holzgraefe <hholzgra@php.net> * @version 0.7 */ class HTTP_WebDAV_Server { // {{{ Member Variables /** * URI path for this request * * @var string */ var $path; /** * Realm string to be used in authentification popups * * @var string */ var $http_auth_realm = "PHP WebDAV"; /** * Remember parsed If: (RFC2518/9.4) header conditions * * @var array */ var $_if_header_uris = array(); var $_http_status = "200 OK"; // }}} // {{{ Constructor /** * Constructor * * @param void */ function HTTP_WebDAV_Server() { // PHP messages destroy XML output -> switch them off ini_set("display_errors", 0); } // }}} // {{{ ServeRequest() /** * Serve WebDAV HTTP request * * dispatch WebDAV HTTP request to the apropriate method handler * * @param void * @return void */ function ServeRequest() { // identify ourselves header("X-Dav-Powered-By: PHP class: ".get_class($this)); if (!$this->_check_auth()) { header('WWW-Authenticate: Basic realm="'.($this->http_auth_realm).'"'); header('HTTP/1.0 401 Unauthorized'); exit; } if(! $this->_check_if_header_conditions()) { header("HTTP/1.0 412 Precondition failed"); exit; } // set path $this->path = !empty($_SERVER["PATH_INFO"]) ? $_SERVER["PATH_INFO"] : "/"; // detect requested method names $method = strtolower($_SERVER["REQUEST_METHOD"]); $wrapper = "http_".$method; // activate HEAD emulation by GET if no HEAD method found if ($method == "head" && !method_exists($this, "head")) { $method = "get"; } if (method_exists($this, $wrapper) && ($method == "options" || method_exists($this, $method))) { $this->$wrapper(); // call method by name } else { if ($_SERVER["REQUEST_METHOD"] == "LOCK") { $this->http_status("412 Precondition failed"); } else { $this->http_status("405 Method not allowed"); header("Allow: ".join(", ", $this->_allow())); // tell client what's allowed } } } // }}} // {{{ abstract WebDAV methods // {{{ GET() /** * GET implementation * * overload this method to retrieve resources from your server * <br> * * * @abstract * @param array &$params Array of input and output parameters * <br><b>input</b><ul> * <li> path - * </ul> * <br><b>output</b><ul> * <li> size - * </ul> * @returns int HTTP-Statuscode */ /* abstract function GET(&$params) { // dummy entry for PHPDoc } */ // }}} // {{{ PUT() /** * PUT implementation * * PUT implementation * * @abstract * @param array &$params * @returns int HTTP-Statuscode */ /* abstract function PUT() { // dummy entry for PHPDoc } */ // }}} // {{{ COPY() /** * COPY implementation * * COPY implementation * * @abstract * @param array &$params * @returns int HTTP-Statuscode */ /* abstract function COPY() { // dummy entry for PHPDoc } */ // }}} // {{{ MOVE() /** * MOVE implementation * * MOVE implementation * * @abstract * @param array &$params * @returns int HTTP-Statuscode */ /* abstract function MOVE() { // dummy entry for PHPDoc } */ // }}} // {{{ DELETE() /** * DELETE implementation * * DELETE implementation * * @abstract * @param array &$params * @returns int HTTP-Statuscode */ /* abstract function DELETE() { // dummy entry for PHPDoc } */ // }}} // {{{ PROPFIND() /** * PROPFIND implementation * * PROPFIND implementation * * @abstract * @param array &$params * @returns int HTTP-Statuscode */ /* abstract function PROPFIND() { // dummy entry for PHPDoc } */ // }}} // {{{ PROPPATCH() /** * PROPPATCH implementation * * PROPPATCH implementation * * @abstract * @param array &$params * @returns int HTTP-Statuscode */ /* abstract function PROPPATCH() { // dummy entry for PHPDoc } */ // }}} // {{{ LOCK() /** * LOCK implementation * * LOCK implementation * * @abstract * @param array &$params * @returns int HTTP-Statuscode */ /* abstract function LOCK() { // dummy entry for PHPDoc } */ // }}} // {{{ UNLOCK() /** * UNLOCK implementation * * UNLOCK implementation * * @abstract * @param array &$params * @returns int HTTP-Statuscode */ /* abstract function UNLOCK() { // dummy entry for PHPDoc } */ // }}} // }}} // {{{ other abstract methods // {{{ check_auth() /** * check authentication * * overload this method to retrieve and confirm authentication information * * @abstract * @param string type Authentication type, e.g. "basic" or "digest" * @param string username Transmitted username * @param string passwort Transmitted password * @returns bool Authentication status */ /* abstract function check_auth($type, $username, $password) { // dummy entry for PHPDoc } */ // }}} // {{{ checklock() /** * check lock status for a resource * * overload this method to return shared and exclusive locks * active for this resource * * @abstract * @param string resource Resource path to check * @returns array An array of lock entries each consisting * of 'type' ('shared'/'exclusive'), 'token' and 'timeout' */ /* abstract function checklock($resource) { // dummy entry for PHPDoc } */ // }}} // }}} // {{{ WebDAV HTTP method wrappers // {{{ http_OPTIONS() /** * OPTIONS method handler * * The OPTIONS method handler creates a valid OPTIONS reply * including Dav: and Allowed: heaers * based on the implemented methods found in the actual instance * * @param void * @returns void */ function http_OPTIONS() { $this->http_status("200 OK"); // be nice to M$ clients header("MS-Author-Via: DAV"); // get allowed methods $allow = $this->_allow(); // dav header $dav = array(1); // assume we are always dav class 1 compliant if (isset($allow['lock'])) $dav[] = 2; // dav class 2 requires locking header("DAV: ".join(",", $dav)); header("Allow: ".join(", ", $allow)); } // }}} // {{{ http_PROPFIND() function http_PROPFIND() { $options = Array(); $options["path"] = $this->path; if (isset($_SERVER['HTTP_DEPTH'])) { $options["depth"] = $_SERVER["HTTP_DEPTH"]; } else { $options["depth"] = "infinity"; } $propinfo = new _parse_propfind("php://input"); if (!$propinfo->success) { $this->http_status("400 Error"); return; } $options['props'] = $propinfo->props; if ($this->propfind($options, $files)) { // collect namespaces $ns_hash = array(); $ns_defs = "xmlns:ns0='urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/'"; // M$ needs this for time values foreach($files["files"] as $filekey => $file) { if (@is_array($file["props"])) { foreach($file["props"] as $key => $prop) { // clean up returned properties, leave only requested entries switch($options['props']) { case "all": break; case "names": unset($files["files"][$filekey]["props"][$key]["val"]); break; default: $found = false; if (is_array($options["props"])) { foreach($options["props"] as $reqprop) { if ($reqprop["name"] == $prop["name"]) { // todo NameSpaces $found = true; break; } } } if (!$found) { $files["files"][$filekey]["props"][$key]=""; continue(2); } break; } if (empty($prop["ns"])) continue; $ns = $prop["ns"]; if ($ns == "DAV:") continue; if (isset($ns_hash[$ns])) continue; $ns_name = "ns".(count($ns_hash) + 1); $ns_hash[$ns] = $ns_name; $ns_defs .= " xmlns:$ns_name='$ns'"; } } // add entries requested but not found if (is_array($options['props'])) { foreach($options["props"] as $reqprop) { if($reqprop['name']=="") continue; $found = false; foreach($file["props"] as $prop) { if ($reqprop["name"] == $prop["name"]) { // todo NameSpaces $found = true; break; } } if (!$found) { if($reqprop["xmlns"]==="DAV:" && $reqprop["name"]==="lockdiscovery") { $files["files"][$filekey]["props"][] = $this->mkprop("DAV:", "lockdiscovery" , $this->lockdiscovery($files["files"][$filekey]['path'])); } else { $files["files"][$filekey]["noprops"][] = $this->mkprop($reqprop["xmlns"], $reqprop["name"], ""); if ($reqprop["xmlns"] != "DAV:" && !isset($ns_hash[$reqprop["xmlns"]])) { $ns_name = "ns".(count($ns_hash) + 1); $ns_hash[$reqprop["xmlns"]] = $ns_name; $ns_defs .= " xmlns:$ns_name='$reqprop[xmlns]'"; } } } } } } $this->http_status("207 Multi-Status"); header('Content-Type: text/xml; charset="utf-8"'); echo "<?xml version='1.0' encoding='utf-8'?>\n"; echo "<D:multistatus xmlns:D='DAV:'>\n"; foreach($files["files"] as $file) { echo " <D:response $ns_defs>\n"; $path = $file['path']; // todo: make sure collection hrefs end in '/' // http://$_SERVER[HTTP_HOST] echo " <D:href>".str_replace(' ', '%20', $_SERVER["SCRIPT_NAME"].$path). "</D:href>\n"; echo " <D:propstat>\n"; echo " <D:prop>\n"; if (@is_array($file["props"])) { foreach($file["props"] as $key => $prop) { if (!is_array($prop)) continue; if (!isset($prop["name"])) continue; if (!isset($prop["val"]) || $prop["val"] === "" || $prop["val"] === false) { if($prop["ns"]=="DAV:") { echo " <D:$prop[name]/>\n"; } else if($prop["ns"]) { echo " <".$ns_hash[$prop["ns"]].":$prop[name]/>\n"; } else { echo " <$prop[name] xmlns=''/>"; } } else if ($prop["ns"] == "DAV:") { switch ($prop["name"]) { case "creationdate": echo " <D:creationdate ns0:dt='dateTime.tz'>". date("Y-m-d\\TH:i:s\\Z",$prop['val']). "</D:creationdate>\n"; break; case "getlastmodified": echo " <D:getlastmodified ns0:dt='dateTime.rfc1123'>". date("D, j M Y H:m:s ", $prop['val']). "GMT</D:getlastmodified>\n"; break; case "resourcetype": echo " <D:resourcetype><D:$prop[val]/></D:resourcetype>\n"; break; case "supportedlock": echo " <D:supportedlock>$prop[val]</D:supportedlock>\n"; break; case "lockdiscovery": echo " <D:lockdiscovery>\n"; echo $prop["val"]; echo " </D:lockdiscovery>\n"; break; default: echo " <D:$prop[name]>". utf8_encode(htmlspecialchars ($prop['val'])). "</D:$prop[name]>\n"; break; } } else { if ($prop["ns"]) { echo " <".$ns_hash[$prop["ns"]]. ":$prop[name]>". utf8_encode(htmlspecialchars ($prop['val']))."</". $ns_hash[$prop["ns"]]. ":$prop[name]>\n"; } else { echo " <$prop[name] xmlns=''>". utf8_encode(htmlspecialchars ($prop['val'])). "</$prop[name]>\n"; } } } } echo " </D:prop>\n"; echo " <D:status>HTTP/1.1 200 OK</D:status>\n"; echo " </D:propstat>\n"; if (@is_array($file["noprops"])) { echo " <D:propstat>\n"; echo " <D:prop>\n"; foreach($file["noprops"] as $key => $prop) { if (!is_array($prop)) $prop = array("val" => $prop); if ($prop["ns"] == "DAV:") { echo " <D:$prop[name]/>\n"; } else if ($prop["ns"] == "") { echo " <$prop[name] xmlns=''/>\n"; } else { echo " <".$ns_hash[$prop["ns"]]. ":$prop[name]/>\n"; } } echo " </D:prop>\n"; echo " <D:status>HTTP/1.1 404 Not Found</D:status>\n"; echo " </D:propstat>\n"; } echo " </D:response>\n"; } echo "</D:multistatus>\n"; } else { $this->http_status("404 Not Found"); } } // }}} // {{{ http_PROPPATCH() function http_PROPPATCH() { if($this->_check_lock_status($this->path)) { $options = Array(); $options["path"] = $this->path; $propinfo = new _parse_proppatch("php://input"); if (!$propinfo->success) { $this->http_status("400 Error"); return; } $options['props'] = $propinfo->props; $responsedescr = $this->proppatch($options); $this->http_status("207 Multi-Status"); header('Content-Type: text/xml'); echo "<?xml version='1.0' encoding='utf-8'?>\n"; echo "<D:multistatus xmlns:D='DAV:'>\n"; echo " <D:response>\n"; echo " <D:href>".str_replace(' ', '%20', $_SERVER["SCRIPT_NAME"].$this->path). "</D:href>\n"; foreach($options["props"] as $prop) { echo " <D:propstat>\n"; echo " <D:prop><$prop[name] xmlns='$prop[ns]'/></D:prop>\n"; echo " <D:status>HTTP/1.1 $prop[status]</D:status>\n"; echo " </D:propstat>\n"; } if ($responsedescr) { echo " <D:responsedescription>". utf8_encode(htmlspecialchars($responsedescr)). "</D:responsedescription>\n"; } echo " </D:response>\n"; echo "</D:multistatus>\n"; } else { $this->http_status("423 Locked"); } } // }}} // {{{ http_MKCOL() function http_MKCOL() { $options = Array(); $options["path"] = $this->path; $stat = $this->mkcol($options); $this->http_status($stat); } // }}} // {{{ http_GET() /** * GET wrapper * * GET wrapper * * @param void * @returns void */ function http_GET() { $options = Array(); $options["path"] = $this->path; if ($status = $this->get($options)) { if(!headers_sent()) { if($status === true) $status = "200 OK"; $this->http_status("$status"); } } else { $this->http_status("404 not found"); } } // }}} // {{{ http_HEAD() function http_HEAD() { $options = Array(); $options["path"] = $this->path; if (method_exists($this, "head")) { if (!$this->head($options)) { $this->http_status("404 Not Found"); } } else { ob_start(); if (!$this->get($options)) { $this->http_status("404 Not Found"); } ob_end_clean(); } } // }}} // {{{ http_PUT() function http_PUT() { if($this->_check_lock_status($this->path)) { $options = Array(); $options["path"] = $this->path; $options["content_length"] = $_SERVER["CONTENT_LENGTH"]; $options["stream"] = fopen("php://input", "r"); $stat = $this->put($options); $this->http_status($stat); } else { $this->http_status("423 Locked"); } } // }}} // {{{ http_DELETE() function http_DELETE() { if($this->_check_lock_status($this->path)) { $options = Array(); $options["path"] = $this->path; $stat = $this->delete($options); $this->http_status($stat); } else { $this->http_status("423 Locked"); } } // }}} // {{{ http_COPY() function http_COPY() { $this->_copymove("copy"); } // }}} // {{{ http_MOVE() function http_MOVE() { if($this->_check_lock_status($this->path)) { $this->_copymove("move"); } else { $this->http_status("423 Locked"); } } // }}} // {{{ http_LOCK() function http_LOCK() { $options = Array(); $options["path"] = $this->path; if (isset($_SERVER['HTTP_DEPTH'])) { $options["depth"] = $_SERVER["HTTP_DEPTH"]; } else { $options["depth"] = "infinity"; } if (isset($_SERVER["HTTP_TIMEOUT"])) { $options["timeout"] = explode(",", $_SERVER["HTTP_TIMEOUT"]); } if(empty($_SERVER['CONTENT_LENGTH']) && !empty($_SERVER['HTTP_IF'])) { $options["update"] = substr($_SERVER['HTTP_IF'],2,-2); $stat = $this->lock($options); } else { // new lock $lockinfo = new _parse_lockinfo("php://input"); if ($lockinfo->success) { $options["scope"] = $lockinfo->lockscope; $options["type"] = $lockinfo->locktype; $options["owner"] = $lockinfo->owner; } $options["locktoken"] = $this->_new_locktoken(); $stat = $this->lock($options); } if(is_bool($stat)) { $http_stat = $stat ? "200 OK" : "423 Locked"; } else { $http_stat = $stat; } $this->http_status($http_stat); if($options["timeout"]) { $timeout = "Second-$options[timeout]"; } else { $timeout = "Infinite"; } if ($stat == true) { // ok header("Lock-Token: <$options[locktoken]>"); echo "<?xml version='1.0' encoding='utf8'?>\n"; echo "<D:prop xmlns:D='DAV:'>\n"; echo " <D:lockdiscovery>\n"; echo " <D:activelock>\n"; echo " <D:lockscope><D:$options[scope]/></D:lockscope>\n"; echo " <D:locktype><D:$options[type]/></D:locktype>\n"; echo " <D:depth>$options[depth]</D:depth>\n"; echo " <D:owner>$options[owner]</D:owner>\n"; echo " <D:timeout>$timeout</D:timeout>\n"; echo " <D:locktoken><D:href>$options[locktoken]</D:href></D:locktoken>\n"; echo " </D:activelock>\n"; echo " </D:lockdiscovery>\n"; echo "</D:prop>\n"; } else { // fail // TODO!!! } } // }}} // {{{ http_UNLOCK() function http_UNLOCK() { $options = Array(); $options["path"] = $this->path; if (isset($_SERVER['HTTP_DEPTH'])) { $options["depth"] = $_SERVER["HTTP_DEPTH"]; } else { $options["depth"] = "infinity"; } $options["token"] = substr($_SERVER["HTTP_LOCK_TOKEN"],1,-1); // strip <> $stat = $this->unlock($options); $this->http_status($stat); } // }}} // }}} // {{{ _copymove() function _copymove($what) { $options = Array(); $options["path"] = $this->path; if (isset($_SERVER['HTTP_DEPTH'])) { $options["depth"] = $_SERVER["HTTP_DEPTH"]; } else { $options["depth"] = "infinity"; } extract(parse_url($_SERVER["HTTP_DESTINATION"])); $http_host = $host; if (isset($port)) $http_host .= ":$port"; if ($http_host == $_SERVER["HTTP_HOST"] && !strncmp($_SERVER["SCRIPT_NAME"], $path, strlen($_SERVER["SCRIPT_NAME"]))) { $options["dest"] = substr($path, strlen($_SERVER["SCRIPT_NAME"])); if(!$this->_check_lock_status($options["dest"])) { $this->http_status("423 Locked"); return; } } else { $options["dest_url"] = $_SERVER["HTTP_DESTINATION"]; } $options["overwrite"] = @$_SERVER["HTTP_OVERWRITE"] == "T"; $stat = $this->$what($options); $this->http_status($stat); } // }}} // {{{ _allow() /** * check for implemented HTTP methods * * check for implemented HTTP methods * * @param void * @returns array something */ function _allow() { // OPTIONS is always there $allow = array("options" => "OPTIONS"); // all other METHODS need both a http_method() wrapper // and a method() implementation // the base class supplies wrappers only foreach(get_class_methods($this) as $method) { if (!strncmp("http_", $method, 5)) { $method = substr($method, 5); if (method_exists($this, $method)) { $allow[$method] = strtoupper($method); } } } // we can emulate a missing HEAD implemetation using GET if (isset($allow["get"])) $allow["head"] = "HEAD"; // no LOCK without checklok() if (!method_exists($this, "checklock")) { unset($allow["lock"]); unset($allow["unlock"]); } return $allow; } // }}} function mkprop() { $args = func_get_args(); if (count($args) == 3) { return array("name" => $args[1], "ns" => $args[0], "val" => $args[2]); } else { return array("name" => $args[0], "ns" => "DAV:", "val" => $args[1]); } } // {{{ _check_auth function _check_auth() { if (method_exists($this, "check_auth")) { return $this->check_auth(@$_SERVER["AUTH_TYPE"], @$_SERVER["PHP_AUTH_USER"], @$_SERVER["PHP_AUTH_PW"]); } else { return true; } } // }}} // {{{ UUID stuff function _new_uuid() { if (function_exists("uuid_create")) { return uuid_create(); } // fallback $uuid = md5(microtime().getmypid()); // this should be random enough for now // set variant and version fields for 'true' random uuid $uuid{12} = "4"; $n = 8 + (ord($uuid{16}) & 3); $hex = "0123456789abcdef"; $uuid{16} = $hex{$n}; // return formated uuid return substr($uuid,0,8)."-".substr($uuid,8,4)."-".substr($uuid,12,4)."-".substr($uuid,16,4)."-".substr($uuid,20); } function _new_locktoken() { return "opaquelocktoken:" . $this->_new_uuid(); } // }}} // {{{ WebDAV If: header parsing function _if_header_lexer($string , &$pos) { while(ctype_space($string{$pos})) ++$pos; // skip whitespace if(strlen($string) <= $pos) return false; $c = $string{$pos++}; switch($c) { case "<": $pos2 = strpos($string, ">", $pos); $uri = substr($string, $pos, $pos2 - $pos); $pos = $pos2 + 1; return array("URI", $uri); case "[": if($string{$pos}=="W") { $type = "ETAG_WEAK"; $pos += 2; } else { $type = "ETAG_STRONG"; } $pos2 = strpos($string, "]", $pos); $etag= substr($string, $pos+1, $pos2 - $pos -2); $pos = $pos2 + 1; return array($type, $etag); case "N": $pos += 2; return array("NOT", "Not"); default: return array("CHAR", $c); } } /** * parse If: header * * dispatch WebDAV HTTP request to the apropriate method handler * * @param $str * @return void */ function _if_header_parser($str) { $pos = 0; $len = strlen($str); $uris = array(); while($pos < $len) { $token = $this->_if_header_lexer($str, $pos); if($token[0] == "URI") { $uri = $token[1]; $token = $this->_if_header_lexer($str, $pos); } else { $uri = ""; } if($token[0] != "CHAR" || $token[1] != "(") return false; $list = array(); $level = 1; $not = ""; while($level) { $token = $this->_if_header_lexer($str, $pos); if($token[0] == "NOT") { $not = "!"; continue; } switch($token[0]) { case "CHAR": switch($token[1]) { case "(": $level++; break; case ")": $level--; break; default: return false; } break; case "URI": $list[] = $not."<$token[1]>"; break; case "ETAG_WEAK": $list[] = $not."[W/'$token[1]']>"; break; case "ETAG_STRONG": $list[] = $not."['$token[1]']>"; break; default: return false; } $not = ""; } $uris[$uri] = $list; } return $uris; } function _check_if_header_conditions() { // see rfc 2518 sec. 9.4 if(isset($_SERVER["HTTP_IF"])) { $this->_if_header_uris = $this->_if_header_parser($_SERVER["HTTP_IF"]); foreach($this->_if_header_uris as $uri => $conditions) { if($uri == "") { // default uri is the complete request uri $uri = (@$_SERVER["HTTPS"] === "on" ? "https:" : "http:"); } // all must match $state = true; foreach($conditions as $condition) { if(! $this->_check_uri_condition($uri, $condition)) { $state = false; break; } } // any match is ok if($state == true) return true; } return false; } return true; } /** * Check a single URI condition parsed from an if-header * * Check a single URI condition parsed from an if-header * * @abstract * @param string $uri URI to check * @param string $condition Condition to check for this URI * @returns bool Condition check result */ function _check_uri_condition($uri, $condition) { // not really implemented here, // implementations must override return true; } function _check_lock_status($path, $depth=0) { if(method_exists($this, "checklock")) { // FIXME depth -> ignored for now // is locked? $lock = $this->checklock($path); // ... and lock is not owned? if(is_array($lock) && count($lock)) { // FIXME doesn't check uri restrictions yet if(!strstr($_SERVER["HTTP_IF"], $lock["token"])) { return false; } } } return true; } // }}} function lockdiscovery($path) { if(!method_exists($this, "checklock")) { return ""; } $lock = $this->checklock($path); $activelocks = ""; if(is_array($lock) && count($lock)) { if(!empty($lock["expires"])) { $timeout = "Second-".($lock["expires"]-time()); } else if(!empty($lock["timeout"])) { $timeout = "Second-$lock[timeout]"; } else { $timeout = "Infinite"; } $activelocks .= " <D:activelock> <D:lockscope><D:$lock[scope]/></D:lockscope> <D:locktype><D:$lock[type]/></D:locktype> <D:depth>$lock[depth]</D:depth> <D:owner>$lock[owner]</D:owner> <D:timeout>$timeout</D:timeout> <D:locktoken><D:href>$lock[token]</D:href></D:locktoken> </D:activelock> "; } return $activelocks; } function http_status($status) { if($status === true) $status = "200 OK"; $this->_http_status = $status; header("HTTP/1.1 $status"); header("X-WebDAV-Status: $status"); } } /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: */ ?> Index: pear/HTTP_WebDAV_Server/package.xml +++ pear/HTTP_WebDAV_Server/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE package SYSTEM "../package.dtd"> <package version="1.0"> <name>HTTP_WebDAV_Server</name> <summary>WebDAV Server Baseclass.</summary> <description> Mostly RFC2518 compliant helper class for WebDAV server implementation. </description> <maintainers> <maintainer> <user>hholzgra</user> <name>Hartmut Holzgraefe</name> <email>hholzgra@php.net</email> <role>lead</role> </maintainer> <maintainer> <user>chregu</user> <name>Christian Stocker</name> <email>chregu@bitflux.ch</email> <role>lead</role> </maintainer> </maintainers> <release> <version>0.2</version> <date>2002-07-27</date> <state>stable</state> <license>alpha</license> <notes> o still work in progress </notes> <deps> <dep type="php" rel="ge" version="4.3"/> <dep type="pkg" rel="ge" version="1.0">HTTP</dep> </deps> <filelist> <dir name="/" baseinstalldir="HTTP/WebDAV"> <file role="php">Server.php</file> <file role="php">FileServer.php</file> <dir name="Server"> <file role="php">_parse_propfind.php</file> <file role="php">_parse_proppatch.php</file> <file role="php">_parse_lockinfo.php</file> </dir> </dir> </filelist> </release> <changelog> </changelog> </package> Index: pear/HTTP_WebDAV_Server/Server/Filesystem.php +++ pear/HTTP_WebDAV_Server/Server/Filesystem.php <?php require_once "HTTP/WebDAV/Server.php"; class HTTP_WebDAV_Server_Filesystem extends HTTP_WebDAV_Server { var $base; function ServeRequest($base) { // special treatment for litmus compliance test // reply on its identifier header // not needed for the test itself but eases debugging foreach(apache_request_headers() as $key => $value) { if(stristr($key,"litmus")) { # error_log("--- $value"); header("X-Litmus-reply: ".$value); } } $this->base = $base; mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("webdav") or die(mysql_error()); parent::ServeRequest(); } function check_auth($type, $user, $pass) { return true; } function propfind ($options,&$files) { $fspath = realpath($this->base . $options["path"]); if (!file_exists($fspath)) { return false; } $files["files"] = array(); $files["files"][] = $this->fileinfo($options["path"], $options); if (!empty($options["depth"])) { if (substr($options["path"],-1) != "/") { $options["path"] .= "/"; } $handle = opendir($fspath); while ($filename = readdir($handle)) { if ($filename != "." && $filename != "..") { $files["files"][] = $this->fileinfo ($options["path"].$filename, $options); } } } return true; } function fileinfo($uri, $options) { $fspath = $this->base . $uri; $file = array(); $file["path"]= $uri; $file["props"][] = $this->mkprop("displayname", strtoupper($uri)); $file["props"][] = $this->mkprop("creationdate", filectime($fspath)); $file["props"][] = $this->mkprop("getlastmodified", filemtime($fspath)); if (is_dir($fspath)) { $file["props"][] = $this->mkprop("getcontentlength", 0); $file["props"][] = $this->mkprop("resourcetype", "collection"); $file["props"][] = $this->mkprop("getcontenttype", "httpd/unix-directory"); } else { $file["props"][] = $this->mkprop("resourcetype", ""); $file["props"][] = $this->mkprop("getcontentlength", filesize($fspath)); if (is_readable($fspath)) { $file["props"][] = $this->mkprop("getcontenttype", rtrim(preg_replace("/^([^;]*);.*/","$1",`file -izb '$fspath' 2> /dev/null`))); } else { $file["props"][] = $this->mkprop("getcontenttype", "application/x-non-readable"); } } $query = "SELECT ns, name, value FROM properties WHERE path = '$uri'"; $res = mysql_query($query); while($row = mysql_fetch_assoc($res)) { $file["props"][] = $this->mkprop($row["ns"], $row["name"], $row["value"]); } mysql_free_result($res); return $file; } function get($options) { $fspath = $this->base . $options["path"]; if (file_exists($fspath)) { if (!is_dir($fspath)) { header("Content-Type: " . `file -izb '$fspath' 2> /dev/null`); } else { header ("Content-Type: httpd/unix-directory"); } readfile($fspath); return true; } else { return false; } } function put($options) { $fspath = $this->base . $options["path"]; if(!@is_dir(dirname($fspath))) { return "409 Conflict"; } $new = ! file_exists($fspath); $fp = fopen($fspath, "w"); if(is_resource($fp) && is_resource($options["stream"])) { while(!feof($options["stream"])) fwrite($fp, fread($options["stream"], 4096)); fclose($fp); } return $new ? "201 Created" : "204 No Content"; } function mkcol($options) { $path = $this->base .$options["path"]; $parent = dirname($path); $name = basename($path); if(!file_exists($parent)) { return "409 Conflict"; } if(!is_dir($parent)) { return "403 Forbidden"; } if( file_exists($parent."/".$name) ) { return "405 Method not allowed"; } if(!empty($_SERVER["CONTENT_LENGTH"])) { // no body parsing yet return "415 Unsupported media type"; } $stat = mkdir ($parent."/".$name,0777); if(!$stat) { return "403 Forbidden"; } return ("201 Created"); } function delete($options) { $path = $this->base . "/" .$options["path"]; if(!file_exists($path)) return "404 Not found"; if (is_dir($path)) { $query = "DELETE FROM properties WHERE path LIKE '$options[path]%'"; mysql_query($query); system("rm -rf $path"); } else { unlink ($path); } $query = "DELETE FROM properties WHERE path = '$options[path]'"; mysql_query($query); return "204 No Content"; } function move($options) { return $this->copy($options, true); } function copy($options, $del=false) { if(!empty($_SERVER["CONTENT_LENGTH"])) { // no body parsing yet return "415 Unsupported media type"; } if(isset($options["dest_url"])) { return "502 bad gateway"; } $source = $this->base .$options["path"]; if(!file_exists($source)) return "404 Not found"; $dest = $this->base . $options["dest"]; $new = !file_exists($dest); $existing_col = false; if(!$new) { if($del && is_dir($dest)) { if(!$options["overwrite"]) { return "412 precondition failed"; } $dest .= basename($source); if(file_exists($dest.basename($source))) { $options["dest"] .= basename($source); } else { $new = true; $existing_col = true; } } } if(!$new) { if($options["overwrite"]) { $stat = $this->delete(array("path" => $options["dest"])); if($stat{0} != "2") return $stat; } else { return "412 precondition failed"; } } if (is_dir($source)) { if($options["depth"] == "infinity") { system("cp -R $source $dest"); } else { mkdir($dest, 0777); } if($del) { system("rm -rf $source"); } } else { if($del) { @unlink($dest); $query = "DELETE FROM properties WHERE path = '$options[dest]'"; mysql_query($query); rename($source, $dest); $query = "UPDATE properties SET path = '$options[dest]' WHERE path = '$options[path]'"; mysql_query($query); } else { if(substr($dest,-1)=="/") $dest = substr($dest,0,-1); copy($source, $dest); } } return ($new && !$existing_col) ? "201 Created" : "204 No Content"; } function proppatch(&$options) { global $prefs, $tab; $msg = ""; $path = $options["path"]; $dir = dirname($path)."/"; $base = basename($path); foreach($options["props"] as $key => $prop) { if($ns == "DAV:") { $options["props"][$key][$status] = "403 Forbidden"; } else { if(isset($prop["val"])) { $query = "REPLACE INTO properties SET path = '$options[path]', name = '$prop[name]', ns= '$prop[ns]', value = '$prop[val]'"; } else { $query = "DELETE FROM properties WHERE path = '$options[path]' AND name = '$prop[name]' AND ns = '$prop[ns]'"; } mysql_query($query); } } return ""; } function lock(&$options) { if(isset($options["update"])) { // Lock Update $query = "UPDATE locks SET expires = ".(time()+300); mysql_query($query); if(mysql_affected_rows()) { $options["timeout"] = 300; // 5min hardcoded return true; } else { return false; } } $options["timeout"] = time()+300; // 5min. hardcoded $query = "INSERT INTO locks SET token = '$options[locktoken]' , path = '$options[path]' , owner = '$options[owner]' , expires = '$options[timeout]' "; mysql_query($query); return mysql_affected_rows() > 0; return "200 OK"; } function unlock(&$options) { $query = "DELETE FROM locks WHERE path = '$options[path]' AND token = '$options[token]'"; mysql_query($query); return mysql_affected_rows() ? "200 OK" : "409 Conflict"; } function checklock($path) { $result = false; $query = "SELECT owner, token, expires FROM locks WHERE path = '$path' "; $res = mysql_query($query); if($res) { $row = mysql_fetch_array($res); mysql_free_result($res); if($row) { $result = array( "type" => "write", "scope" => "exclusive", "depth" => 0, "owner" => $row['owner'], "token" => $row['token'], "expires" => $row['expires'] ); } } return $result; } } ?> Index: pear/HTTP_WebDAV_Server/Server/_parse_lockinfo.php +++ pear/HTTP_WebDAV_Server/Server/_parse_lockinfo.php <?php // helper class for parsing LOCK request bodies class _parse_lockinfo { var $owner = ""; var $collect_owner = false; function _parse_lockinfo($path) { $this->success = true; $had_input = false; $xml_parser = xml_parser_create_ns("UTF-8", " "); xml_set_element_handler($xml_parser, array(&$this, "_startElement"), array(&$this, "_endElement")); xml_set_character_data_handler($xml_parser, array(&$this, "_data")); xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); $f_in = fopen($path, "r"); while($this->success && !feof($f_in)) { $line = fgets($f_in); if (is_string($line)) { $line = trim($line); if($line == "") continue; $had_input = true; $this->success &= xml_parse($xml_parser, $line, false); } } if($had_input) { $this->success &= xml_parse($xml_parser, "", true); } fclose($f_in); xml_parser_free($xml_parser); } function _startElement($parser, $name, $attrs) { if (strstr($name, " ")) { list($ns, $tag) = explode(" ", $name); } else { $ns = ""; $tag = $name; } if ($this->collect_owner) { $ns_short = ""; $ns_attr = ""; if ($ns) { if ($ns == "DAV:") { $ns_short = "D:"; } else { $ns_attr = " xmlns='$ns'"; } } $this->owner .= "<$ns_short$tag$ns_attr>"; } else if ($ns == "DAV:") { switch ($tag) { case "write": $this->locktype = $tag; break; case "exclusive": case "shared": $this->lockscope = $tag; break; case "owner": $this->collect_owner = true; break; } } } function _data($parser, $data) { if ($this->collect_owner) { $this->owner .= $data; } } function _endElement($parser, $name) { if (strstr($name, " ")) { list($ns, $tag) = explode(" ", $name); } else { $ns = ""; $tag = $name; } if (($ns == "DAV:") && ($tag == "owner")) { $this->collect_owner = false; } if ($this->collect_owner) { $ns_short = ""; $ns_attr = ""; if ($ns) { if ($ns == "DAV:") { $ns_short = "D:"; } else { $ns_attr = " xmlns='$ns'"; } } $this->owner .= "</$ns_short$tag$ns_attr>"; } } } ?> Index: pear/HTTP_WebDAV_Server/Server/_parse_propfind.php +++ pear/HTTP_WebDAV_Server/Server/_parse_propfind.php <?php // helper class for parsing PROPFIND request bodies class _parse_propfind { // get requested properties as array containing name/namespace pairs function _parse_propfind($path) { $this->success = true; $this->depth = 0; $this->props = array(); $had_input = false; $xml_parser = xml_parser_create_ns("UTF-8", " "); xml_set_element_handler($xml_parser, array(&$this, "_startElement"), array(&$this, "_endElement")); xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); $f_in = fopen($path, "r"); while($this->success && !feof($f_in)) { $line = fgets($f_in); if (is_string($line)) { $line = trim($line); if($line == "") continue; $had_input = true; $this->success &= xml_parse($xml_parser, $line, false); } } if($had_input) { $this->success &= xml_parse($xml_parser, "", true); } fclose($f_in); xml_parser_free($xml_parser); if(!count($this->props)) $this->props = "all"; // default } function _startElement($parser, $name, $attrs) { if (strstr($name, " ")) { list($ns, $tag) = explode(" ", $name); if ($ns == "") $this->success = false; } else { $ns = ""; $tag = $name; } if ($this->depth == 1) { if ($tag == "allprop") $this->props = "all"; if ($tag == "propname") $this->props = "names"; } if ($this->depth == 2) { $prop = array("name" => $tag); if ($ns) $prop["xmlns"] = $ns; $this->props[] = $prop; } $this->depth++; } function _endElement($parser, $name) { $this->depth--; } } ?> Index: pear/HTTP_WebDAV_Server/Server/_parse_proppatch.php +++ pear/HTTP_WebDAV_Server/Server/_parse_proppatch.php <?php // helper class for parsing PROPPATCH request bodies class _parse_proppatch { function _parse_proppatch($path) { $this->success = true; $this->depth = 0; $this->props = array(); $had_input = false; $xml_parser = xml_parser_create_ns("UTF-8", " "); xml_set_element_handler($xml_parser, array(&$this, "_startElement"), array(&$this, "_endElement")); xml_set_character_data_handler($xml_parser, array(&$this, "_data")); xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); $f_in = fopen($path, "r"); while($this->success && !feof($f_in)) { $line = fgets($f_in); if (is_string($line)) { $line = trim($line); if($line == "") continue; $had_input = true; $this->success &= xml_parse($xml_parser, $line, false); } } if($had_input) { $this->success &= xml_parse($xml_parser, "", true); } fclose($f_in); xml_parser_free($xml_parser); } function _startElement($parser, $name, $attrs) { if (strstr($name, " ")) { list($ns, $tag) = explode(" ", $name); if ($ns == "") $this->success = false; } else { $ns = ""; $tag = $name; } if ($this->depth == 1) { $this->mode = $tag; } if ($this->depth == 3) { $prop = array("name" => $tag); $this->current = array("name" => $tag, "ns" => $ns, "status"=> 200); if ($this->mode == "set") $this->current["val"] = ""; // default set val } $this->depth++; } function _endElement($parser, $name) { if (isset($this->current)) { $this->props[] = $this->current; unset($this->current); } $this->depth--; } function _data($parser, $data) { if (isset($this->current)) { $this->current["val"] = $data; } } } ?> Index: pear/HTTP_WebDAV_Server/tests/split_log.php +++ pear/HTTP_WebDAV_Server/tests/split_log.php <?php $in = fopen("debug.log","r"); $out = false; $last=0; $group=1; while(!feof($in)) { $line = fgets($in); if(ereg(" Running test ([[:digit:]]+): ([^[:space:]]+)", $line, $matches)) { if($out) fclose($out); $nr = $matches[1]; if($nr<$last) $group++; $last=$nr; $out = fopen("{$group}_{$nr}_$matches[2].log","w"); } if($out) fputs($out, $line); } fclose($in); fclose($out); ?> Index: pear/HTTP_WebDAV_Server/tests/test.sh +++ pear/HTTP_WebDAV_Server/tests/test.sh #!/bin/bash cp -R ../* /usr/local/apache/htdocs/HTTP/WebDAV clear rm -f *.log mysql -u root webdav -e "TRUNCATE TABLE locks" mysql -u root webdav -e "TRUNCATE TABLE properties" litmus http://localhost:8080/HTTP/WebDAV/file.php php -q split_log.php Index: pear/HTTP_WebDAV_Server/tests/testdb.sql +++ pear/HTTP_WebDAV_Server/tests/testdb.sql -- MySQL dump 9.06 -- -- Host: localhost Database: webdav --------------------------------------------------------- -- Server version 4.0.3-beta -- -- Table structure for table 'locks' -- CREATE TABLE locks ( token varchar(255) NOT NULL default '', path varchar(200) NOT NULL default '', expires int(11) NOT NULL default '0', owner varchar(200) default NULL, recursive int(11) default '0', writelock int(11) default '0', exclusivelock int(11) default '0', PRIMARY KEY (token), UNIQUE KEY token (token), UNIQUE KEY path (path), KEY path_2 (path), KEY path_3 (path,token), KEY expires (expires) ) TYPE=MyISAM; -- -- Dumping data for table 'locks' -- -- -- Table structure for table 'properties' -- CREATE TABLE properties ( path varchar(255) NOT NULL default '', name varchar(120) NOT NULL default '', ns varchar(120) NOT NULL default 'DAV:', value text, PRIMARY KEY (path,name,ns), KEY path (path) ) TYPE=MyISAM; -- -- Dumping data for table 'properties' --
Index: pear/HTTP_WebDAV_Server/file.php diff -u pear/HTTP_WebDAV_Server/file.php:1.1 pear/HTTP_WebDAV_Server/file.php:1.2 --- pear/HTTP_WebDAV_Server/file.php:1.1 Wed Sep 18 05:57:10 2002 +++ pear/HTTP_WebDAV_Server/file.php Sun Feb 9 19:34:17 2003 @@ -1,8 +1,6 @@ <?php - - require_once "HTTP_WebDAV_Server.php"; - require_once "HTTP_WebDAV_Server_file.php"; - - new dav_fileserver("/usr/local/httpd/htdocs/tmp"); - -?> + ini_set("include_path", ini_get("include_path").":/usr/local/apache/htdocs"); + require_once "HTTP/WebDAV/Server/Filesystem.php"; + $server = new HTTP_WebDAV_Server_Filesystem(); + $server->ServeRequest($_SERVER["DOCUMENT_ROOT"]); +?> \ No newline at end of file Index: pear/HTTP_WebDAV_Server/Server.php +++ pear/HTTP_WebDAV_Server/Server.php <?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2003 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Hartmut Holzgraefe <hholzgra@php.net> | // | Christian Stocker <chregu@bitflux.ch> | // +----------------------------------------------------------------------+ // // $Id: Server.php,v 1.1 2003/02/10 00:34:17 hholzgra Exp $ // // WebDAV server base class, needs to be extended to do useful work // // require_once "HTTP/HTTP.php"; require_once "HTTP/WebDAV/Server/_parse_propfind.php"; require_once "HTTP/WebDAV/Server/_parse_proppatch.php"; require_once "HTTP/WebDAV/Server/_parse_lockinfo.php"; /** * Virtual base class for implementing WebDAV servers * * this is it * bla bla * * @package HTTP_WebDAV_Server * @author Hartmut Holzgraefe <hholzgra@php.net> * @version 0.7 */ class HTTP_WebDAV_Server { // {{{ Member Variables /** * URI path for this request * * @var string */ var $path; /** * Realm string to be used in authentification popups * * @var string */ var $http_auth_realm = "PHP WebDAV"; /** * Remember parsed If: (RFC2518/9.4) header conditions * * @var array */ var $_if_header_uris = array(); var $_http_status = "200 OK"; // }}} // {{{ Constructor /** * Constructor * * @param void */ function HTTP_WebDAV_Server() { // PHP messages destroy XML output -> switch them off ini_set("display_errors", 0); } // }}} // {{{ ServeRequest() /** * Serve WebDAV HTTP request * * dispatch WebDAV HTTP request to the apropriate method handler * * @param void * @return void */ function ServeRequest() { // identify ourselves header("X-Dav-Powered-By: PHP class: ".get_class($this)); if (!$this->_check_auth()) { header('WWW-Authenticate: Basic realm="'.($this->http_auth_realm).'"'); header('HTTP/1.0 401 Unauthorized'); exit; } if(! $this->_check_if_header_conditions()) { header("HTTP/1.0 412 Precondition failed"); exit; } // set path $this->path = !empty($_SERVER["PATH_INFO"]) ? $_SERVER["PATH_INFO"] : "/"; // detect requested method names $method = strtolower($_SERVER["REQUEST_METHOD"]); $wrapper = "http_".$method; // activate HEAD emulation by GET if no HEAD method found if ($method == "head" && !method_exists($this, "head")) { $method = "get"; } if (method_exists($this, $wrapper) && ($method == "options" || method_exists($this, $method))) { $this->$wrapper(); // call method by name } else { if ($_SERVER["REQUEST_METHOD"] == "LOCK") { $this->http_status("412 Precondition failed"); } else { $this->http_status("405 Method not allowed"); header("Allow: ".join(", ", $this->_allow())); // tell client what's allowed } } } // }}} // {{{ abstract WebDAV methods // {{{ GET() /** * GET implementation * * overload this method to retrieve resources from your server * <br> * * * @abstract * @param array &$params Array of input and output parameters * <br><b>input</b><ul> * <li> path - * </ul> * <br><b>output</b><ul> * <li> size - * </ul> * @returns int HTTP-Statuscode */ /* abstract function GET(&$params) { // dummy entry for PHPDoc } */ // }}} // {{{ PUT() /** * PUT implementation * * PUT implementation * * @abstract * @param array &$params * @returns int HTTP-Statuscode */ /* abstract function PUT() { // dummy entry for PHPDoc } */ // }}} // {{{ COPY() /** * COPY implementation * * COPY implementation * * @abstract * @param array &$params * @returns int HTTP-Statuscode */ /* abstract function COPY() { // dummy entry for PHPDoc } */ // }}} // {{{ MOVE() /** * MOVE implementation * * MOVE implementation * * @abstract * @param array &$params * @returns int HTTP-Statuscode */ /* abstract function MOVE() { // dummy entry for PHPDoc } */ // }}} // {{{ DELETE() /** * DELETE implementation * * DELETE implementation * * @abstract * @param array &$params * @returns int HTTP-Statuscode */ /* abstract function DELETE() { // dummy entry for PHPDoc } */ // }}} // {{{ PROPFIND() /** * PROPFIND implementation * * PROPFIND implementation * * @abstract * @param array &$params * @returns int HTTP-Statuscode */ /* abstract function PROPFIND() { // dummy entry for PHPDoc } */ // }}} // {{{ PROPPATCH() /** * PROPPATCH implementation * * PROPPATCH implementation * * @abstract * @param array &$params * @returns int HTTP-Statuscode */ /* abstract function PROPPATCH() { // dummy entry for PHPDoc } */ // }}} // {{{ LOCK() /** * LOCK implementation * * LOCK implementation * * @abstract * @param array &$params * @returns int HTTP-Statuscode */ /* abstract function LOCK() { // dummy entry for PHPDoc } */ // }}} // {{{ UNLOCK() /** * UNLOCK implementation * * UNLOCK implementation * * @abstract * @param array &$params * @returns int HTTP-Statuscode */ /* abstract function UNLOCK() { // dummy entry for PHPDoc } */ // }}} // }}} // {{{ other abstract methods // {{{ check_auth() /** * check authentication * * overload this method to retrieve and confirm authentication information * * @abstract * @param string type Authentication type, e.g. "basic" or "digest" * @param string username Transmitted username * @param string passwort Transmitted password * @returns bool Authentication status */ /* abstract function check_auth($type, $username, $password) { // dummy entry for PHPDoc } */ // }}} // {{{ checklock() /** * check lock status for a resource * * overload this method to return shared and exclusive locks * active for this resource * * @abstract * @param string resource Resource path to check * @returns array An array of lock entries each consisting * of 'type' ('shared'/'exclusive'), 'token' and 'timeout' */ /* abstract function checklock($resource) { // dummy entry for PHPDoc } */ // }}} // }}} // {{{ WebDAV HTTP method wrappers // {{{ http_OPTIONS() /** * OPTIONS method handler * * The OPTIONS method handler creates a valid OPTIONS reply * including Dav: and Allowed: heaers * based on the implemented methods found in the actual instance * * @param void * @returns void */ function http_OPTIONS() { $this->http_status("200 OK"); // be nice to M$ clients header("MS-Author-Via: DAV"); // get allowed methods $allow = $this->_allow(); // dav header $dav = array(1); // assume we are always dav class 1 compliant if (isset($allow['lock'])) $dav[] = 2; // dav class 2 requires locking header("DAV: ".join(",", $dav)); header("Allow: ".join(", ", $allow)); } // }}} // {{{ http_PROPFIND() function http_PROPFIND() { $options = Array(); $options["path"] = $this->path; if (isset($_SERVER['HTTP_DEPTH'])) { $options["depth"] = $_SERVER["HTTP_DEPTH"]; } else { $options["depth"] = "infinity"; } $propinfo = new _parse_propfind("php://input"); if (!$propinfo->success) { $this->http_status("400 Error"); return; } $options['props'] = $propinfo->props; if ($this->propfind($options, $files)) { // collect namespaces $ns_hash = array(); $ns_defs = "xmlns:ns0='urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/'"; // M$ needs this for time values foreach($files["files"] as $filekey => $file) { if (@is_array($file["props"])) { foreach($file["props"] as $key => $prop) { // clean up returned properties, leave only requested entries switch($options['props']) { case "all": break; case "names": unset($files["files"][$filekey]["props"][$key]["val"]); break; default: $found = false; if (is_array($options["props"])) { foreach($options["props"] as $reqprop) { if ($reqprop["name"] == $prop["name"]) { // todo NameSpaces $found = true; break; } } } if (!$found) { $files["files"][$filekey]["props"][$key]=""; continue(2); } break; } if (empty($prop["ns"])) continue; $ns = $prop["ns"]; if ($ns == "DAV:") continue; if (isset($ns_hash[$ns])) continue; $ns_name = "ns".(count($ns_hash) + 1); $ns_hash[$ns] = $ns_name; $ns_defs .= " xmlns:$ns_name='$ns'"; } } // add entries requested but not found if (is_array($options['props'])) { foreach($options["props"] as $reqprop) { if($reqprop['name']=="") continue; $found = false; foreach($file["props"] as $prop) { if ($reqprop["name"] == $prop["name"]) { // todo NameSpaces $found = true; break; } } if (!$found) { if($reqprop["xmlns"]==="DAV:" && $reqprop["name"]==="lockdiscovery") { $files["files"][$filekey]["props"][] = $this->mkprop("DAV:", "lockdiscovery" , $this->lockdiscovery($files["files"][$filekey]['path'])); } else { $files["files"][$filekey]["noprops"][] = $this->mkprop($reqprop["xmlns"], $reqprop["name"], ""); if ($reqprop["xmlns"] != "DAV:" && !isset($ns_hash[$reqprop["xmlns"]])) { $ns_name = "ns".(count($ns_hash) + 1); $ns_hash[$reqprop["xmlns"]] = $ns_name; $ns_defs .= " xmlns:$ns_name='$reqprop[xmlns]'"; } } } } } } $this->http_status("207 Multi-Status"); header('Content-Type: text/xml; charset="utf-8"'); echo "<?xml version='1.0' encoding='utf-8'?>\n"; echo "<D:multistatus xmlns:D='DAV:'>\n"; foreach($files["files"] as $file) { echo " <D:response $ns_defs>\n"; $path = $file['path']; // todo: make sure collection hrefs end in '/' // http://$_SERVER[HTTP_HOST] echo " <D:href>".str_replace(' ', '%20', $_SERVER["SCRIPT_NAME"].$path). "</D:href>\n"; echo " <D:propstat>\n"; echo " <D:prop>\n"; if (@is_array($file["props"])) { foreach($file["props"] as $key => $prop) { if (!is_array($prop)) continue; if (!isset($prop["name"])) continue; if (!isset($prop["val"]) || $prop["val"] === "" || $prop["val"] === false) { if($prop["ns"]=="DAV:") { echo " <D:$prop[name]/>\n"; } else if($prop["ns"]) { echo " <".$ns_hash[$prop["ns"]].":$prop[name]/>\n"; } else { echo " <$prop[name] xmlns=''/>"; } } else if ($prop["ns"] == "DAV:") { switch ($prop["name"]) { case "creationdate": echo " <D:creationdate ns0:dt='dateTime.tz'>". date("Y-m-d\\TH:i:s\\Z",$prop['val']). "</D:creationdate>\n"; break; case "getlastmodified": echo " <D:getlastmodified ns0:dt='dateTime.rfc1123'>". date("D, j M Y H:m:s ", $prop['val']). "GMT</D:getlastmodified>\n"; break; case "resourcetype": echo " <D:resourcetype><D:$prop[val]/></D:resourcetype>\n"; break; case "supportedlock": echo " <D:supportedlock>$prop[val]</D:supportedlock>\n"; break; case "lockdiscovery": echo " <D:lockdiscovery>\n"; echo $prop["val"]; echo " </D:lockdiscovery>\n"; break; default: echo " <D:$prop[name]>". utf8_encode(htmlspecialchars ($prop['val'])). "</D:$prop[name]>\n"; break; } } else { if ($prop["ns"]) { echo " <".$ns_hash[$prop["ns"]]. ":$prop[name]>". utf8_encode(htmlspecialchars ($prop['val']))."</". $ns_hash[$prop["ns"]]. ":$prop[name]>\n"; } else { echo " <$prop[name] xmlns=''>". utf8_encode(htmlspecialchars ($prop['val'])). "</$prop[name]>\n"; } } } } echo " </D:prop>\n"; echo " <D:status>HTTP/1.1 200 OK</D:status>\n"; echo " </D:propstat>\n"; if (@is_array($file["noprops"])) { echo " <D:propstat>\n"; echo " <D:prop>\n"; foreach($file["noprops"] as $key => $prop) { if (!is_array($prop)) $prop = array("val" => $prop); if ($prop["ns"] == "DAV:") { echo " <D:$prop[name]/>\n"; } else if ($prop["ns"] == "") { echo " <$prop[name] xmlns=''/>\n"; } else { echo " <".$ns_hash[$prop["ns"]]. ":$prop[name]/>\n"; } } echo " </D:prop>\n"; echo " <D:status>HTTP/1.1 404 Not Found</D:status>\n"; echo " </D:propstat>\n"; } echo " </D:response>\n"; } echo "</D:multistatus>\n"; } else { $this->http_status("404 Not Found"); } } // }}} // {{{ http_PROPPATCH() function http_PROPPATCH() { if($this->_check_lock_status($this->path)) { $options = Array(); $options["path"] = $this->path; $propinfo = new _parse_proppatch("php://input"); if (!$propinfo->success) { $this->http_status("400 Error"); return; } $options['props'] = $propinfo->props; $responsedescr = $this->proppatch($options); $this->http_status("207 Multi-Status"); header('Content-Type: text/xml'); echo "<?xml version='1.0' encoding='utf-8'?>\n"; echo "<D:multistatus xmlns:D='DAV:'>\n"; echo " <D:response>\n"; echo " <D:href>".str_replace(' ', '%20', $_SERVER["SCRIPT_NAME"].$this->path). "</D:href>\n"; foreach($options["props"] as $prop) { echo " <D:propstat>\n"; echo " <D:prop><$prop[name] xmlns='$prop[ns]'/></D:prop>\n"; echo " <D:status>HTTP/1.1 $prop[status]</D:status>\n"; echo " </D:propstat>\n"; } if ($responsedescr) { echo " <D:responsedescription>". utf8_encode(htmlspecialchars($responsedescr)). "</D:responsedescription>\n"; } echo " </D:response>\n"; echo "</D:multistatus>\n"; } else { $this->http_status("423 Locked"); } } // }}} // {{{ http_MKCOL() function http_MKCOL() { $options = Array(); $options["path"] = $this->path; $stat = $this->mkcol($options); $this->http_status($stat); } // }}} // {{{ http_GET() /** * GET wrapper * * GET wrapper * * @param void * @returns void */ function http_GET() { $options = Array(); $options["path"] = $this->path; if ($status = $this->get($options)) { if(!headers_sent()) { if($status === true) $status = "200 OK"; $this->http_status("$status"); } } else { $this->http_status("404 not found"); } } // }}} // {{{ http_HEAD() function http_HEAD() { $options = Array(); $options["path"] = $this->path; if (method_exists($this, "head")) { if (!$this->head($options)) { $this->http_status("404 Not Found"); } } else { ob_start(); if (!$this->get($options)) { $this->http_status("404 Not Found"); } ob_end_clean(); } } // }}} // {{{ http_PUT() function http_PUT() { if($this->_check_lock_status($this->path)) { $options = Array(); $options["path"] = $this->path; $options["content_length"] = $_SERVER["CONTENT_LENGTH"]; $options["stream"] = fopen("php://input", "r"); $stat = $this->put($options); $this->http_status($stat); } else { $this->http_status("423 Locked"); } } // }}} // {{{ http_DELETE() function http_DELETE() { if($this->_check_lock_status($this->path)) { $options = Array(); $options["path"] = $this->path; $stat = $this->delete($options); $this->http_status($stat); } else { $this->http_status("423 Locked"); } } // }}} // {{{ http_COPY() function http_COPY() { $this->_copymove("copy"); } // }}} // {{{ http_MOVE() function http_MOVE() { if($this->_check_lock_status($this->path)) { $this->_copymove("move"); } else { $this->http_status("423 Locked"); } } // }}} // {{{ http_LOCK() function http_LOCK() { $options = Array(); $options["path"] = $this->path; if (isset($_SERVER['HTTP_DEPTH'])) { $options["depth"] = $_SERVER["HTTP_DEPTH"]; } else { $options["depth"] = "infinity"; } if (isset($_SERVER["HTTP_TIMEOUT"])) { $options["timeout"] = explode(",", $_SERVER["HTTP_TIMEOUT"]); } if(empty($_SERVER['CONTENT_LENGTH']) && !empty($_SERVER['HTTP_IF'])) { $options["update"] = substr($_SERVER['HTTP_IF'],2,-2); $stat = $this->lock($options); } else { // new lock $lockinfo = new _parse_lockinfo("php://input"); if ($lockinfo->success) { $options["scope"] = $lockinfo->lockscope; $options["type"] = $lockinfo->locktype; $options["owner"] = $lockinfo->owner; } $options["locktoken"] = $this->_new_locktoken(); $stat = $this->lock($options); } if(is_bool($stat)) { $http_stat = $stat ? "200 OK" : "423 Locked"; } else { $http_stat = $stat; } $this->http_status($http_stat); if($options["timeout"]) { $timeout = "Second-$options[timeout]"; } else { $timeout = "Infinite"; } if ($stat == true) { // ok header("Lock-Token: <$options[locktoken]>"); echo "<?xml version='1.0' encoding='utf8'?>\n"; echo "<D:prop xmlns:D='DAV:'>\n"; echo " <D:lockdiscovery>\n"; echo " <D:activelock>\n"; echo " <D:lockscope><D:$options[scope]/></D:lockscope>\n"; echo " <D:locktype><D:$options[type]/></D:locktype>\n"; echo " <D:depth>$options[depth]</D:depth>\n"; echo " <D:owner>$options[owner]</D:owner>\n"; echo " <D:timeout>$timeout</D:timeout>\n"; echo " <D:locktoken><D:href>$options[locktoken]</D:href></D:locktoken>\n"; echo " </D:activelock>\n"; echo " </D:lockdiscovery>\n"; echo "</D:prop>\n"; } else { // fail // TODO!!! } } // }}} // {{{ http_UNLOCK() function http_UNLOCK() { $options = Array(); $options["path"] = $this->path; if (isset($_SERVER['HTTP_DEPTH'])) { $options["depth"] = $_SERVER["HTTP_DEPTH"]; } else { $options["depth"] = "infinity"; } $options["token"] = substr($_SERVER["HTTP_LOCK_TOKEN"],1,-1); // strip <> $stat = $this->unlock($options); $this->http_status($stat); } // }}} // }}} // {{{ _copymove() function _copymove($what) { $options = Array(); $options["path"] = $this->path; if (isset($_SERVER['HTTP_DEPTH'])) { $options["depth"] = $_SERVER["HTTP_DEPTH"]; } else { $options["depth"] = "infinity"; } extract(parse_url($_SERVER["HTTP_DESTINATION"])); $http_host = $host; if (isset($port)) $http_host .= ":$port"; if ($http_host == $_SERVER["HTTP_HOST"] && !strncmp($_SERVER["SCRIPT_NAME"], $path, strlen($_SERVER["SCRIPT_NAME"]))) { $options["dest"] = substr($path, strlen($_SERVER["SCRIPT_NAME"])); if(!$this->_check_lock_status($options["dest"])) { $this->http_status("423 Locked"); return; } } else { $options["dest_url"] = $_SERVER["HTTP_DESTINATION"]; } $options["overwrite"] = @$_SERVER["HTTP_OVERWRITE"] == "T"; $stat = $this->$what($options); $this->http_status($stat); } // }}} // {{{ _allow() /** * check for implemented HTTP methods * * check for implemented HTTP methods * * @param void * @returns array something */ function _allow() { // OPTIONS is always there $allow = array("options" => "OPTIONS"); // all other METHODS need both a http_method() wrapper // and a method() implementation // the base class supplies wrappers only foreach(get_class_methods($this) as $method) { if (!strncmp("http_", $method, 5)) { $method = substr($method, 5); if (method_exists($this, $method)) { $allow[$method] = strtoupper($method); } } } // we can emulate a missing HEAD implemetation using GET if (isset($allow["get"])) $allow["head"] = "HEAD"; // no LOCK without checklok() if (!method_exists($this, "checklock")) { unset($allow["lock"]); unset($allow["unlock"]); } return $allow; } // }}} function mkprop() { $args = func_get_args(); if (count($args) == 3) { return array("name" => $args[1], "ns" => $args[0], "val" => $args[2]); } else { return array("name" => $args[0], "ns" => "DAV:", "val" => $args[1]); } } // {{{ _check_auth function _check_auth() { if (method_exists($this, "check_auth")) { return $this->check_auth(@$_SERVER["AUTH_TYPE"], @$_SERVER["PHP_AUTH_USER"], @$_SERVER["PHP_AUTH_PW"]); } else { return true; } } // }}} // {{{ UUID stuff function _new_uuid() { if (function_exists("uuid_create")) { return uuid_create(); } // fallback $uuid = md5(microtime().getmypid()); // this should be random enough for now // set variant and version fields for 'true' random uuid $uuid{12} = "4"; $n = 8 + (ord($uuid{16}) & 3); $hex = "0123456789abcdef"; $uuid{16} = $hex{$n}; // return formated uuid return substr($uuid,0,8)."-".substr($uuid,8,4)."-".substr($uuid,12,4)."-".substr($uuid,16,4)."-".substr($uuid,20); } function _new_locktoken() { return "opaquelocktoken:" . $this->_new_uuid(); } // }}} // {{{ WebDAV If: header parsing function _if_header_lexer($string , &$pos) { while(ctype_space($string{$pos})) ++$pos; // skip whitespace if(strlen($string) <= $pos) return false; $c = $string{$pos++}; switch($c) { case "<": $pos2 = strpos($string, ">", $pos); $uri = substr($string, $pos, $pos2 - $pos); $pos = $pos2 + 1; return array("URI", $uri); case "[": if($string{$pos}=="W") { $type = "ETAG_WEAK"; $pos += 2; } else { $type = "ETAG_STRONG"; } $pos2 = strpos($string, "]", $pos); $etag= substr($string, $pos+1, $pos2 - $pos -2); $pos = $pos2 + 1; return array($type, $etag); case "N": $pos += 2; return array("NOT", "Not"); default: return array("CHAR", $c); } } /** * parse If: header * * dispatch WebDAV HTTP request to the apropriate method handler * * @param $str * @return void */ function _if_header_parser($str) { $pos = 0; $len = strlen($str); $uris = array(); while($pos < $len) { $token = $this->_if_header_lexer($str, $pos); if($token[0] == "URI") { $uri = $token[1]; $token = $this->_if_header_lexer($str, $pos); } else { $uri = ""; } if($token[0] != "CHAR" || $token[1] != "(") return false; $list = array(); $level = 1; $not = ""; while($level) { $token = $this->_if_header_lexer($str, $pos); if($token[0] == "NOT") { $not = "!"; continue; } switch($token[0]) { case "CHAR": switch($token[1]) { case "(": $level++; break; case ")": $level--; break; default: return false; } break; case "URI": $list[] = $not."<$token[1]>"; break; case "ETAG_WEAK": $list[] = $not."[W/'$token[1]']>"; break; case "ETAG_STRONG": $list[] = $not."['$token[1]']>"; break; default: return false; } $not = ""; } $uris[$uri] = $list; } return $uris; } function _check_if_header_conditions() { // see rfc 2518 sec. 9.4 if(isset($_SERVER["HTTP_IF"])) { $this->_if_header_uris = $this->_if_header_parser($_SERVER["HTTP_IF"]); foreach($this->_if_header_uris as $uri => $conditions) { if($uri == "") { // default uri is the complete request uri $uri = (@$_SERVER["HTTPS"] === "on" ? "https:" : "http:"); } // all must match $state = true; foreach($conditions as $condition) { if(! $this->_check_uri_condition($uri, $condition)) { $state = false; break; } } // any match is ok if($state == true) return true; } return false; } return true; } /** * Check a single URI condition parsed from an if-header * * Check a single URI condition parsed from an if-header * * @abstract * @param string $uri URI to check * @param string $condition Condition to check for this URI * @returns bool Condition check result */ function _check_uri_condition($uri, $condition) { // not really implemented here, // implementations must override return true; } function _check_lock_status($path, $depth=0) { if(method_exists($this, "checklock")) { // FIXME depth -> ignored for now // is locked? $lock = $this->checklock($path); // ... and lock is not owned? if(is_array($lock) && count($lock)) { // FIXME doesn't check uri restrictions yet if(!strstr($_SERVER["HTTP_IF"], $lock["token"])) { return false; } } } return true; } // }}} function lockdiscovery($path) { if(!method_exists($this, "checklock")) { return ""; } $lock = $this->checklock($path); $activelocks = ""; if(is_array($lock) && count($lock)) { if(!empty($lock["expires"])) { $timeout = "Second-".($lock["expires"]-time()); } else if(!empty($lock["timeout"])) { $timeout = "Second-$lock[timeout]"; } else { $timeout = "Infinite"; } $activelocks .= " <D:activelock> <D:lockscope><D:$lock[scope]/></D:lockscope> <D:locktype><D:$lock[type]/></D:locktype> <D:depth>$lock[depth]</D:depth> <D:owner>$lock[owner]</D:owner> <D:timeout>$timeout</D:timeout> <D:locktoken><D:href>$lock[token]</D:href></D:locktoken> </D:activelock> "; } return $activelocks; } function http_status($status) { if($status === true) $status = "200 OK"; $this->_http_status = $status; header("HTTP/1.1 $status"); header("X-WebDAV-Status: $status"); } } /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: */ ?> Index: pear/HTTP_WebDAV_Server/package.xml +++ pear/HTTP_WebDAV_Server/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE package SYSTEM "../package.dtd"> <package version="1.0"> <name>HTTP_WebDAV_Server</name> <summary>WebDAV Server Baseclass.</summary> <description> Mostly RFC2518 compliant helper class for WebDAV server implementation. </description> <maintainers> <maintainer> <user>hholzgra</user> <name>Hartmut Holzgraefe</name> <email>hholzgra@php.net</email> <role>lead</role> </maintainer> <maintainer> <user>chregu</user> <name>Christian Stocker</name> <email>chregu@bitflux.ch</email> <role>lead</role> </maintainer> </maintainers> <release> <version>0.2</version> <date>2002-07-27</date> <state>stable</state> <license>alpha</license> <notes> o still work in progress </notes> <deps> <dep type="php" rel="ge" version="4.3"/> <dep type="pkg" rel="ge" version="1.0">HTTP</dep> </deps> <filelist> <dir name="/" baseinstalldir="HTTP/WebDAV"> <file role="php">Server.php</file> <file role="php">FileServer.php</file> <dir name="Server"> <file role="php">_parse_propfind.php</file> <file role="php">_parse_proppatch.php</file> <file role="php">_parse_lockinfo.php</file> </dir> </dir> </filelist> </release> <changelog> </changelog> </package> Index: pear/HTTP_WebDAV_Server/Server/Filesystem.php +++ pear/HTTP_WebDAV_Server/Server/Filesystem.php <?php require_once "HTTP/WebDAV/Server.php"; class HTTP_WebDAV_Server_Filesystem extends HTTP_WebDAV_Server { var $base; function ServeRequest($base) { // special treatment for litmus compliance test // reply on its identifier header // not needed for the test itself but eases debugging foreach(apache_request_headers() as $key => $value) { if(stristr($key,"litmus")) { # error_log("--- $value"); header("X-Litmus-reply: ".$value); } } $this->base = $base; mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("webdav") or die(mysql_error()); parent::ServeRequest(); } function check_auth($type, $user, $pass) { return true; } function propfind ($options,&$files) { $fspath = realpath($this->base . $options["path"]); if (!file_exists($fspath)) { return false; } $files["files"] = array(); $files["files"][] = $this->fileinfo($options["path"], $options); if (!empty($options["depth"])) { if (substr($options["path"],-1) != "/") { $options["path"] .= "/"; } $handle = opendir($fspath); while ($filename = readdir($handle)) { if ($filename != "." && $filename != "..") { $files["files"][] = $this->fileinfo ($options["path"].$filename, $options); } } } return true; } function fileinfo($uri, $options) { $fspath = $this->base . $uri; $file = array(); $file["path"]= $uri; $file["props"][] = $this->mkprop("displayname", strtoupper($uri)); $file["props"][] = $this->mkprop("creationdate", filectime($fspath)); $file["props"][] = $this->mkprop("getlastmodified", filemtime($fspath)); if (is_dir($fspath)) { $file["props"][] = $this->mkprop("getcontentlength", 0); $file["props"][] = $this->mkprop("resourcetype", "collection"); $file["props"][] = $this->mkprop("getcontenttype", "httpd/unix-directory"); } else { $file["props"][] = $this->mkprop("resourcetype", ""); $file["props"][] = $this->mkprop("getcontentlength", filesize($fspath)); if (is_readable($fspath)) { $file["props"][] = $this->mkprop("getcontenttype", rtrim(preg_replace("/^([^;]*);.*/","$1",`file -izb '$fspath' 2> /dev/null`))); } else { $file["props"][] = $this->mkprop("getcontenttype", "application/x-non-readable"); } } $query = "SELECT ns, name, value FROM properties WHERE path = '$uri'"; $res = mysql_query($query); while($row = mysql_fetch_assoc($res)) { $file["props"][] = $this->mkprop($row["ns"], $row["name"], $row["value"]); } mysql_free_result($res); return $file; } function get($options) { $fspath = $this->base . $options["path"]; if (file_exists($fspath)) { if (!is_dir($fspath)) { header("Content-Type: " . `file -izb '$fspath' 2> /dev/null`); } else { header ("Content-Type: httpd/unix-directory"); } readfile($fspath); return true; } else { return false; } } function put($options) { $fspath = $this->base . $options["path"]; if(!@is_dir(dirname($fspath))) { return "409 Conflict"; } $new = ! file_exists($fspath); $fp = fopen($fspath, "w"); if(is_resource($fp) && is_resource($options["stream"])) { while(!feof($options["stream"])) fwrite($fp, fread($options["stream"], 4096)); fclose($fp); } return $new ? "201 Created" : "204 No Content"; } function mkcol($options) { $path = $this->base .$options["path"]; $parent = dirname($path); $name = basename($path); if(!file_exists($parent)) { return "409 Conflict"; } if(!is_dir($parent)) { return "403 Forbidden"; } if( file_exists($parent."/".$name) ) { return "405 Method not allowed"; } if(!empty($_SERVER["CONTENT_LENGTH"])) { // no body parsing yet return "415 Unsupported media type"; } $stat = mkdir ($parent."/".$name,0777); if(!$stat) { return "403 Forbidden"; } return ("201 Created"); } function delete($options) { $path = $this->base . "/" .$options["path"]; if(!file_exists($path)) return "404 Not found"; if (is_dir($path)) { $query = "DELETE FROM properties WHERE path LIKE '$options[path]%'"; mysql_query($query); system("rm -rf $path"); } else { unlink ($path); } $query = "DELETE FROM properties WHERE path = '$options[path]'"; mysql_query($query); return "204 No Content"; } function move($options) { return $this->copy($options, true); } function copy($options, $del=false) { if(!empty($_SERVER["CONTENT_LENGTH"])) { // no body parsing yet return "415 Unsupported media type"; } if(isset($options["dest_url"])) { return "502 bad gateway"; } $source = $this->base .$options["path"]; if(!file_exists($source)) return "404 Not found"; $dest = $this->base . $options["dest"]; $new = !file_exists($dest); $existing_col = false; if(!$new) { if($del && is_dir($dest)) { if(!$options["overwrite"]) { return "412 precondition failed"; } $dest .= basename($source); if(file_exists($dest.basename($source))) { $options["dest"] .= basename($source); } else { $new = true; $existing_col = true; } } } if(!$new) { if($options["overwrite"]) { $stat = $this->delete(array("path" => $options["dest"])); if($stat{0} != "2") return $stat; } else { return "412 precondition failed"; } } if (is_dir($source)) { if($options["depth"] == "infinity") { system("cp -R $source $dest"); } else { mkdir($dest, 0777); } if($del) { system("rm -rf $source"); } } else { if($del) { @unlink($dest); $query = "DELETE FROM properties WHERE path = '$options[dest]'"; mysql_query($query); rename($source, $dest); $query = "UPDATE properties SET path = '$options[dest]' WHERE path = '$options[path]'"; mysql_query($query); } else { if(substr($dest,-1)=="/") $dest = substr($dest,0,-1); copy($source, $dest); } } return ($new && !$existing_col) ? "201 Created" : "204 No Content"; } function proppatch(&$options) { global $prefs, $tab; $msg = ""; $path = $options["path"]; $dir = dirname($path)."/"; $base = basename($path); foreach($options["props"] as $key => $prop) { if($ns == "DAV:") { $options["props"][$key][$status] = "403 Forbidden"; } else { if(isset($prop["val"])) { $query = "REPLACE INTO properties SET path = '$options[path]', name = '$prop[name]', ns= '$prop[ns]', value = '$prop[val]'"; } else { $query = "DELETE FROM properties WHERE path = '$options[path]' AND name = '$prop[name]' AND ns = '$prop[ns]'"; } mysql_query($query); } } return ""; } function lock(&$options) { if(isset($options["update"])) { // Lock Update $query = "UPDATE locks SET expires = ".(time()+300); mysql_query($query); if(mysql_affected_rows()) { $options["timeout"] = 300; // 5min hardcoded return true; } else { return false; } } $options["timeout"] = time()+300; // 5min. hardcoded $query = "INSERT INTO locks SET token = '$options[locktoken]' , path = '$options[path]' , owner = '$options[owner]' , expires = '$options[timeout]' "; mysql_query($query); return mysql_affected_rows() > 0; return "200 OK"; } function unlock(&$options) { $query = "DELETE FROM locks WHERE path = '$options[path]' AND token = '$options[token]'"; mysql_query($query); return mysql_affected_rows() ? "200 OK" : "409 Conflict"; } function checklock($path) { $result = false; $query = "SELECT owner, token, expires FROM locks WHERE path = '$path' "; $res = mysql_query($query); if($res) { $row = mysql_fetch_array($res); mysql_free_result($res); if($row) { $result = array( "type" => "write", "scope" => "exclusive", "depth" => 0, "owner" => $row['owner'], "token" => $row['token'], "expires" => $row['expires'] ); } } return $result; } } ?> Index: pear/HTTP_WebDAV_Server/Server/_parse_lockinfo.php +++ pear/HTTP_WebDAV_Server/Server/_parse_lockinfo.php <?php // helper class for parsing LOCK request bodies class _parse_lockinfo { var $owner = ""; var $collect_owner = false; function _parse_lockinfo($path) { $this->success = true; $had_input = false; $xml_parser = xml_parser_create_ns("UTF-8", " "); xml_set_element_handler($xml_parser, array(&$this, "_startElement"), array(&$this, "_endElement")); xml_set_character_data_handler($xml_parser, array(&$this, "_data")); xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); $f_in = fopen($path, "r"); while($this->success && !feof($f_in)) { $line = fgets($f_in); if (is_string($line)) { $line = trim($line); if($line == "") continue; $had_input = true; $this->success &= xml_parse($xml_parser, $line, false); } } if($had_input) { $this->success &= xml_parse($xml_parser, "", true); } fclose($f_in); xml_parser_free($xml_parser); } function _startElement($parser, $name, $attrs) { if (strstr($name, " ")) { list($ns, $tag) = explode(" ", $name); } else { $ns = ""; $tag = $name; } if ($this->collect_owner) { $ns_short = ""; $ns_attr = ""; if ($ns) { if ($ns == "DAV:") { $ns_short = "D:"; } else { $ns_attr = " xmlns='$ns'"; } } $this->owner .= "<$ns_short$tag$ns_attr>"; } else if ($ns == "DAV:") { switch ($tag) { case "write": $this->locktype = $tag; break; case "exclusive": case "shared": $this->lockscope = $tag; break; case "owner": $this->collect_owner = true; break; } } } function _data($parser, $data) { if ($this->collect_owner) { $this->owner .= $data; } } function _endElement($parser, $name) { if (strstr($name, " ")) { list($ns, $tag) = explode(" ", $name); } else { $ns = ""; $tag = $name; } if (($ns == "DAV:") && ($tag == "owner")) { $this->collect_owner = false; } if ($this->collect_owner) { $ns_short = ""; $ns_attr = ""; if ($ns) { if ($ns == "DAV:") { $ns_short = "D:"; } else { $ns_attr = " xmlns='$ns'"; } } $this->owner .= "</$ns_short$tag$ns_attr>"; } } } ?> Index: pear/HTTP_WebDAV_Server/Server/_parse_propfind.php +++ pear/HTTP_WebDAV_Server/Server/_parse_propfind.php <?php // helper class for parsing PROPFIND request bodies class _parse_propfind { // get requested properties as array containing name/namespace pairs function _parse_propfind($path) { $this->success = true; $this->depth = 0; $this->props = array(); $had_input = false; $xml_parser = xml_parser_create_ns("UTF-8", " "); xml_set_element_handler($xml_parser, array(&$this, "_startElement"), array(&$this, "_endElement")); xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); $f_in = fopen($path, "r"); while($this->success && !feof($f_in)) { $line = fgets($f_in); if (is_string($line)) { $line = trim($line); if($line == "") continue; $had_input = true; $this->success &= xml_parse($xml_parser, $line, false); } } if($had_input) { $this->success &= xml_parse($xml_parser, "", true); } fclose($f_in); xml_parser_free($xml_parser); if(!count($this->props)) $this->props = "all"; // default } function _startElement($parser, $name, $attrs) { if (strstr($name, " ")) { list($ns, $tag) = explode(" ", $name); if ($ns == "") $this->success = false; } else { $ns = ""; $tag = $name; } if ($this->depth == 1) { if ($tag == "allprop") $this->props = "all"; if ($tag == "propname") $this->props = "names"; } if ($this->depth == 2) { $prop = array("name" => $tag); if ($ns) $prop["xmlns"] = $ns; $this->props[] = $prop; } $this->depth++; } function _endElement($parser, $name) { $this->depth--; } } ?> Index: pear/HTTP_WebDAV_Server/Server/_parse_proppatch.php +++ pear/HTTP_WebDAV_Server/Server/_parse_proppatch.php <?php // helper class for parsing PROPPATCH request bodies class _parse_proppatch { function _parse_proppatch($path) { $this->success = true; $this->depth = 0; $this->props = array(); $had_input = false; $xml_parser = xml_parser_create_ns("UTF-8", " "); xml_set_element_handler($xml_parser, array(&$this, "_startElement"), array(&$this, "_endElement")); xml_set_character_data_handler($xml_parser, array(&$this, "_data")); xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); $f_in = fopen($path, "r"); while($this->success && !feof($f_in)) { $line = fgets($f_in); if (is_string($line)) { $line = trim($line); if($line == "") continue; $had_input = true; $this->success &= xml_parse($xml_parser, $line, false); } } if($had_input) { $this->success &= xml_parse($xml_parser, "", true); } fclose($f_in); xml_parser_free($xml_parser); } function _startElement($parser, $name, $attrs) { if (strstr($name, " ")) { list($ns, $tag) = explode(" ", $name); if ($ns == "") $this->success = false; } else { $ns = ""; $tag = $name; } if ($this->depth == 1) { $this->mode = $tag; } if ($this->depth == 3) { $prop = array("name" => $tag); $this->current = array("name" => $tag, "ns" => $ns, "status"=> 200); if ($this->mode == "set") $this->current["val"] = ""; // default set val } $this->depth++; } function _endElement($parser, $name) { if (isset($this->current)) { $this->props[] = $this->current; unset($this->current); } $this->depth--; } function _data($parser, $data) { if (isset($this->current)) { $this->current["val"] = $data; } } } ?> Index: pear/HTTP_WebDAV_Server/tests/split_log.php +++ pear/HTTP_WebDAV_Server/tests/split_log.php <?php $in = fopen("debug.log","r"); $out = false; $last=0; $group=1; while(!feof($in)) { $line = fgets($in); if(ereg(" Running test ([[:digit:]]+): ([^[:space:]]+)", $line, $matches)) { if($out) fclose($out); $nr = $matches[1]; if($nr<$last) $group++; $last=$nr; $out = fopen("{$group}_{$nr}_$matches[2].log","w"); } if($out) fputs($out, $line); } fclose($in); fclose($out); ?> Index: pear/HTTP_WebDAV_Server/tests/test.sh +++ pear/HTTP_WebDAV_Server/tests/test.sh #!/bin/bash cp -R ../* /usr/local/apache/htdocs/HTTP/WebDAV clear rm -f *.log mysql -u root webdav -e "TRUNCATE TABLE locks" mysql -u root webdav -e "TRUNCATE TABLE properties" litmus http://localhost:8080/HTTP/WebDAV/file.php php -q split_log.php Index: pear/HTTP_WebDAV_Server/tests/testdb.sql +++ pear/HTTP_WebDAV_Server/tests/testdb.sql -- MySQL dump 9.06 -- -- Host: localhost Database: webdav --------------------------------------------------------- -- Server version 4.0.3-beta -- -- Table structure for table 'locks' -- CREATE TABLE locks ( token varchar(255) NOT NULL default '', path varchar(200) NOT NULL default '', expires int(11) NOT NULL default '0', owner varchar(200) default NULL, recursive int(11) default '0', writelock int(11) default '0', exclusivelock int(11) default '0', PRIMARY KEY (token), UNIQUE KEY token (token), UNIQUE KEY path (path), KEY path_2 (path), KEY path_3 (path,token), KEY expires (expires) ) TYPE=MyISAM; -- -- Dumping data for table 'locks' -- -- -- Table structure for table 'properties' -- CREATE TABLE properties ( path varchar(255) NOT NULL default '', name varchar(120) NOT NULL default '', ns varchar(120) NOT NULL default 'DAV:', value text, PRIMARY KEY (path,name,ns), KEY path (path) ) TYPE=MyISAM; -- -- Dumping data for table 'properties' --