| 1 |
<?php
|
| 2 |
|
| 3 |
/* This file is part of "WebDav for Drupal Module".
|
| 4 |
* Copyright 2009, arNuméral
|
| 5 |
* Author : Yoran Brault
|
| 6 |
* eMail : yoran.brault@bad_arnumeral.fr (remove bad_ before sending an email)
|
| 7 |
* Site : http://www.arnumeral.fr/node/5
|
| 8 |
*
|
| 9 |
* "WebDav for Drupal Module" is free software; you can redistribute it and/or
|
| 10 |
* modify it under the terms of the GNU General Public License as
|
| 11 |
* published by the Free Software Foundation; either version 2.1 of
|
| 12 |
* the License, or (at your option) any later version.
|
| 13 |
*
|
| 14 |
* "WebDav for Drupal Module" is distributed in the hope that it will be useful,
|
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 17 |
* General Public License for more details.
|
| 18 |
*
|
| 19 |
* You should have received a copy of the GNU General Public
|
| 20 |
* License along with "Broken Anchor for Node comments Module"; if not, write to the Free
|
| 21 |
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
| 22 |
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
| 23 |
*/
|
| 24 |
|
| 25 |
|
| 26 |
require_once 'HTTP/WebDAV/Server.php';
|
| 27 |
|
| 28 |
class DrupalDavServer extends HTTP_WebDAV_Server {
|
| 29 |
var $dav_powered_by = 'Drupal WebDAV (+http://drupal.org/project/webdav)';
|
| 30 |
|
| 31 |
/** Serve a webdav request. */
|
| 32 |
function ServeRequest() {
|
| 33 |
parent :: ServeRequest();
|
| 34 |
}
|
| 35 |
|
| 36 |
function checkLock ($path) {
|
| 37 |
// $cursor = db_query("SELECT * FROM {webdav_locks} WHERE path = '%s'",$path);
|
| 38 |
// $lock = db_fetch_array($cursor);
|
| 39 |
// if (webdav_is_debug()) webdav_debug("Checking $path lock... : ".$lock['token']);
|
| 40 |
// return $lock;
|
| 41 |
return NULL;
|
| 42 |
}
|
| 43 |
|
| 44 |
/** PROPFIND method handler. */
|
| 45 |
function PROPFIND(& $options, & $files) {
|
| 46 |
webdav_session_start($options['path']);
|
| 47 |
if (webdav_session_failed()) return webdav_session_close();
|
| 48 |
|
| 49 |
|
| 50 |
// search for the associated collection
|
| 51 |
$handler = webdav_route_resolve(webdav_session('route'));
|
| 52 |
if (!webdav_check_collection_perms($handler)) {
|
| 53 |
return webdav_session_close();
|
| 54 |
}
|
| 55 |
$resource = webdav_resource($handler);
|
| 56 |
if ($resource===false) {
|
| 57 |
webdav_session_close(WEBDAV_STATUS_NOT_FOUND);
|
| 58 |
return false;
|
| 59 |
}
|
| 60 |
|
| 61 |
// if client forget the leading / for a collection URL, we should redirect it
|
| 62 |
webdav_redirect_wrong_collection( $resource);
|
| 63 |
|
| 64 |
/* first, this is a folder...
|
| 65 |
* FIXME sure this will not work for depth > 1
|
| 66 |
*/
|
| 67 |
$members=array();
|
| 68 |
if ($resource['type'] == 'collection' && $options['depth'] != 0) {
|
| 69 |
$members = webdav_members($handler);
|
| 70 |
}
|
| 71 |
|
| 72 |
// make the root item first of the list
|
| 73 |
array_unshift($members, $resource);
|
| 74 |
|
| 75 |
// Convert items to DAV files
|
| 76 |
$files['files'] = array ();
|
| 77 |
$members[0]['id']=''; //
|
| 78 |
foreach ($members as $_item) {
|
| 79 |
$files['files'][] = $this->convert_item($session, $_item);
|
| 80 |
}
|
| 81 |
|
| 82 |
// DEBUG: display result
|
| 83 |
if (webdav_is_debug()) {
|
| 84 |
$count=0;
|
| 85 |
foreach ($files['files'] as $file) {
|
| 86 |
webdav_debug(' + ' . $file['path']);
|
| 87 |
$count++;
|
| 88 |
if ($count > 5) {
|
| 89 |
break;
|
| 90 |
}
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
| 94 |
webdav_session_close();
|
| 95 |
return true; // No status for this, everything is handled by undelying class
|
| 96 |
}
|
| 97 |
|
| 98 |
/** convert DFS item to Dav item */
|
| 99 |
function convert_item(&$session, &$resource) {
|
| 100 |
$file = array ();
|
| 101 |
|
| 102 |
// Common properties
|
| 103 |
$route=webdav_session('route');
|
| 104 |
array_push($route, $resource['id']);
|
| 105 |
$route[0]=WEBDAV_ROOT_MENU;
|
| 106 |
$file['path'] = webdav_route_to_path($route);
|
| 107 |
$file['props'] = array ();
|
| 108 |
|
| 109 |
if (!empty($resource['name'])) {
|
| 110 |
$file['props'][] = $this->mkprop(
|
| 111 |
'displayname',$resource['name']); // No need to convert HTML entities (< > &) as is done by pear
|
| 112 |
}
|
| 113 |
|
| 114 |
if (!empty($resource['created'])) {
|
| 115 |
$file['props'][] = $this->mkprop('creationdate', $resource['created']);
|
| 116 |
}
|
| 117 |
if (!empty($resource['changed'])) {
|
| 118 |
$file['props'][] = $this->mkprop('getlastmodified', $resource['changed']);
|
| 119 |
}
|
| 120 |
|
| 121 |
if ($resource['type'] == 'collection') {
|
| 122 |
$file['path'] = webdav_slashify($file['path']);
|
| 123 |
$file['props'][] = $this->mkprop('resourcetype', 'collection');
|
| 124 |
$file['props'][] = $this->mkprop('getcontentlength', 1);
|
| 125 |
} else {
|
| 126 |
$file['path'] = webdav_unslashify($file['path']);
|
| 127 |
$file['props'][] = $this->mkprop('creationdate', $resource['created']);
|
| 128 |
$file['props'][] = $this->mkprop('getlastmodified', $resource['changed']);
|
| 129 |
$file['props'][] = $this->mkprop('resourcetype', '');
|
| 130 |
$file['props'][] = $this->mkprop('getcontenttype', $resource['type']);
|
| 131 |
$file['props'][] = $this->mkprop('getcontentlength', $resource['size']);
|
| 132 |
}
|
| 133 |
return $file;
|
| 134 |
}
|
| 135 |
|
| 136 |
/**
|
| 137 |
* GET method handler
|
| 138 |
*
|
| 139 |
* @param array parameter passing array
|
| 140 |
* @return bool true on success
|
| 141 |
*/
|
| 142 |
function GET(& $options) {
|
| 143 |
webdav_session_start($options['path']);
|
| 144 |
|
| 145 |
// search for the associated collection
|
| 146 |
$handler = webdav_route_resolve(webdav_session('route'));
|
| 147 |
webdav_check_collection_perms($handler); // FIXME : we don't have to give the not found information
|
| 148 |
$resource = webdav_resource($handler);
|
| 149 |
if ($resource===false) {
|
| 150 |
return webdav_session_close(WEBDAV_STATUS_NOT_FOUND);
|
| 151 |
}
|
| 152 |
if (webdav_session_failed()) {
|
| 153 |
if ($resource['type']=='collection') {
|
| 154 |
webdav_set_output_type(WEBDAV_CONTENT_DRUPAL);
|
| 155 |
}
|
| 156 |
return webdav_session_close();
|
| 157 |
}
|
| 158 |
|
| 159 |
if ($resource['type']!='collection') {
|
| 160 |
|
| 161 |
$options['mimetype'] = $resource['type'];
|
| 162 |
if (!empty($resource['encoding'])) {
|
| 163 |
$options['mimetype'].='; charset=' . $resource['encoding'];
|
| 164 |
} else {
|
| 165 |
// FIXME No encoding <=> binary, not that clever, we need a proper contet-type handling here
|
| 166 |
webdav_set_output_type(WEBDAV_CONTENT_BINARY);
|
| 167 |
}
|
| 168 |
if (webdav_is_debug()) webdav_debug("Document encoding : ".$options['mimetype']);
|
| 169 |
$options['mtime'] = $resource['changed'];
|
| 170 |
$options['ctime'] = $resource['created'];
|
| 171 |
$options['size'] = $resource['size'];
|
| 172 |
|
| 173 |
// Load string content
|
| 174 |
webdav_session_set_status(WEBDAV_STATUS_OK);
|
| 175 |
$result=webdav_operation($handler, 'get');
|
| 176 |
if (webdav_session_failed()) return webdav_session_close();
|
| 177 |
|
| 178 |
if (is_string($result)) {
|
| 179 |
// Save temporary file
|
| 180 |
$file_name = tempnam(file_directory_temp(), 'FOO');
|
| 181 |
$handle = fopen($file_name, 'w');
|
| 182 |
fwrite($handle, $result);
|
| 183 |
fclose($handle);
|
| 184 |
$stream=fopen($file_name, 'r');
|
| 185 |
} else {
|
| 186 |
$stream=$result;
|
| 187 |
}
|
| 188 |
$options['stream'] = $stream;
|
| 189 |
webdav_session_close();
|
| 190 |
return true;
|
| 191 |
} else {
|
| 192 |
webdav_redirect_wrong_collection( $resource);
|
| 193 |
|
| 194 |
if (empty($resource['name'])) {
|
| 195 |
$resource['name']=$resource['id'];
|
| 196 |
}
|
| 197 |
$members = webdav_members($handler);
|
| 198 |
foreach ($members as &$_item) {
|
| 199 |
if (empty($_item['name'])) {
|
| 200 |
$_item['name']=$_item['id'];
|
| 201 |
}
|
| 202 |
if ($_item['type']=='collection') {
|
| 203 |
$_item['id']=webdav_slashify($_item['id']);
|
| 204 |
}
|
| 205 |
}
|
| 206 |
$contents = theme('webdav_listing', $resource, $members);
|
| 207 |
|
| 208 |
$path = drupal_get_path('module', 'webdav');
|
| 209 |
drupal_add_css($path . '/webdav_listing.css');
|
| 210 |
webdav_set_output_type(WEBDAV_CONTENT_DRUPAL);
|
| 211 |
print $contents;
|
| 212 |
webdav_session_close();
|
| 213 |
return false;
|
| 214 |
}
|
| 215 |
}
|
| 216 |
|
| 217 |
/**
|
| 218 |
* PUT method handler
|
| 219 |
*
|
| 220 |
* @param array parameter passing array
|
| 221 |
* @return bool true on success
|
| 222 |
*/
|
| 223 |
function PUT(& $options) {
|
| 224 |
webdav_session_start($options['path']);
|
| 225 |
if (webdav_session_failed()) return webdav_session_close();
|
| 226 |
|
| 227 |
// lookup item (if it is an existing one)
|
| 228 |
$route=webdav_session('route');
|
| 229 |
$name=array_pop($route);
|
| 230 |
$handler = webdav_route_resolve($route);
|
| 231 |
if (!webdav_check_collection_perms($handler)) {
|
| 232 |
return webdav_session_close();
|
| 233 |
}
|
| 234 |
$parent_item = webdav_resource($handler);
|
| 235 |
if ($parent_item===false) {
|
| 236 |
return webdav_session_close(WEBDAV_STATUS_NOT_FOUND);
|
| 237 |
}
|
| 238 |
|
| 239 |
// prepare item stuff
|
| 240 |
$file_name = file_directory_temp()."/".$name;
|
| 241 |
$handle = fopen($file_name, 'w');
|
| 242 |
while (!feof($options["stream"])) {
|
| 243 |
$data= fread($options["stream"], 8192);
|
| 244 |
fwrite($handle, $data);
|
| 245 |
}
|
| 246 |
fclose($handle);
|
| 247 |
|
| 248 |
webdav_session_set_status(WEBDAV_STATUS_CREATED);
|
| 249 |
webdav_operation($handler, 'put', $file_name);
|
| 250 |
return webdav_session_close();
|
| 251 |
}
|
| 252 |
|
| 253 |
/**
|
| 254 |
* MKCOL method handler
|
| 255 |
*
|
| 256 |
* @param array general parameter passing array
|
| 257 |
* @return bool true on success
|
| 258 |
*/
|
| 259 |
function MKCOL($options) {
|
| 260 |
webdav_session_start($options['path']);
|
| 261 |
if (webdav_session_failed()) return webdav_session_close();
|
| 262 |
|
| 263 |
// lookup item
|
| 264 |
$route=webdav_session('route');
|
| 265 |
$new_folder=array_pop($route);
|
| 266 |
$handler = webdav_route_resolve($route);
|
| 267 |
if (!webdav_check_collection_perms($handler)) return webdav_session_close();
|
| 268 |
|
| 269 |
$resource = webdav_resource($handler);
|
| 270 |
if ($resource===false) return webdav_session_close(WEBDAV_STATUS_NOT_FOUND);
|
| 271 |
|
| 272 |
webdav_session_set_status(WEBDAV_STATUS_CREATED);
|
| 273 |
webdav_operation($handler, 'create', $new_folder);
|
| 274 |
return webdav_session_close();
|
| 275 |
}
|
| 276 |
|
| 277 |
/**
|
| 278 |
* DELETE method handler
|
| 279 |
*
|
| 280 |
* @param array general parameter passing array
|
| 281 |
* @return bool true on success
|
| 282 |
*/
|
| 283 |
function DELETE($options) {
|
| 284 |
webdav_session_start($options['path']);
|
| 285 |
if (webdav_session_failed()) return webdav_session_close();
|
| 286 |
|
| 287 |
// lookup item
|
| 288 |
$handler = webdav_route_resolve(webdav_session('route'));
|
| 289 |
if (!webdav_check_collection_perms($handler)) return webdav_session_close();
|
| 290 |
$resource = webdav_resource($handler);
|
| 291 |
if ($resource===false) {
|
| 292 |
return webdav_session_close(WEBDAV_STATUS_NOT_FOUND);
|
| 293 |
}
|
| 294 |
|
| 295 |
|
| 296 |
// prepare item stuff
|
| 297 |
webdav_session_set_status(WEBDAV_STATUS_NO_CONTENT);
|
| 298 |
webdav_operation($handler, 'delete');
|
| 299 |
// cleaning locks
|
| 300 |
if (!webdav_session_failed()) {
|
| 301 |
db_query("DELETE FROM {webdav_locks} WHERE path='%s'", $options['path']);
|
| 302 |
}
|
| 303 |
cache_clear_all('w');
|
| 304 |
return webdav_session_close();
|
| 305 |
}
|
| 306 |
|
| 307 |
/**
|
| 308 |
* OPTIONS method handler
|
| 309 |
*
|
| 310 |
* @param array general parameter passing array
|
| 311 |
* @return bool true on success
|
| 312 |
*/
|
| 313 |
function OPTIONS($options) {
|
| 314 |
webdav_session_start($options['path']);
|
| 315 |
if (webdav_session_failed()) return webdav_session_close();
|
| 316 |
|
| 317 |
// lookup item
|
| 318 |
$handler = webdav_route_resolve(webdav_session('route'));
|
| 319 |
// if ( ( !(($this->_SERVER['REQUEST_METHOD'] == 'OPTIONS') && ($this->path == "/")))
|
| 320 |
if (!webdav_check_collection_perms($handler)) return webdav_session_close();
|
| 321 |
$resource = webdav_resource($handler);
|
| 322 |
if ($resource===false) {
|
| 323 |
return webdav_session_close(WEBDAV_STATUS_NOT_FOUND);
|
| 324 |
}
|
| 325 |
|
| 326 |
// if client forget the leading / for a collection URL, we should redirect it
|
| 327 |
webdav_redirect_wrong_collection( $resource);
|
| 328 |
|
| 329 |
$resource=webdav_resource($handler);
|
| 330 |
header("Content-Type:".$resource['type']=='collection'?'httpd/unix-directory':$resource['type']);
|
| 331 |
webdav_session_close(WEBDAV_STATUS_OK); // We
|
| 332 |
}
|
| 333 |
|
| 334 |
|
| 335 |
/**
|
| 336 |
* MOVE method handler
|
| 337 |
*
|
| 338 |
* @param array general parameter passing array
|
| 339 |
* @return bool true on success
|
| 340 |
*/
|
| 341 |
function MOVE($options) {
|
| 342 |
webdav_session_start($options['path']);
|
| 343 |
if (webdav_session_failed()) return webdav_session_close();
|
| 344 |
|
| 345 |
// lookup item
|
| 346 |
$arguments=webdav_session('route');
|
| 347 |
$source_collection = webdav_route_resolve($arguments);
|
| 348 |
if (!webdav_check_collection_perms($source_collection)) return webdav_session_close();
|
| 349 |
$source_item = webdav_resource($source_collection);
|
| 350 |
if ($source_item===false) {
|
| 351 |
return webdav_session_close(WEBDAV_STATUS_NOT_FOUND);
|
| 352 |
}
|
| 353 |
|
| 354 |
$target_session=webdav_session_create($options['dest']);
|
| 355 |
$target_name=array_pop($target_session->route);
|
| 356 |
$target_collection = webdav_route_resolve($target_session->route);
|
| 357 |
if (!webdav_check_collection_perms($target_collection)) return webdav_session_close();
|
| 358 |
$target_item = webdav_resource($target_collection);
|
| 359 |
if ($target_item===false) {
|
| 360 |
return webdav_session_close(WEBDAV_STATUS_NOT_FOUND);
|
| 361 |
}
|
| 362 |
|
| 363 |
webdav_session_set_status(WEBDAV_STATUS_CREATED);
|
| 364 |
webdav_operation($target_collection, 'move', $target_name, $source_collection);
|
| 365 |
if (!webdav_session_failed()) {
|
| 366 |
// moving locks
|
| 367 |
db_query("update {webdav_locks} set path='%s' where path='%s'",$options['dest'],$options['path']);
|
| 368 |
}
|
| 369 |
return webdav_session_close();
|
| 370 |
}
|
| 371 |
|
| 372 |
|
| 373 |
/**
|
| 374 |
* COPY method handler
|
| 375 |
*
|
| 376 |
* @param array general parameter passing array
|
| 377 |
* @return bool true on success
|
| 378 |
*/
|
| 379 |
function COPY($options) {
|
| 380 |
webdav_session_start($options['path']);
|
| 381 |
if (webdav_session_failed()) return webdav_session_close();
|
| 382 |
|
| 383 |
// lookup item
|
| 384 |
$arguments=webdav_session('route');
|
| 385 |
$source_collection = webdav_route_resolve($arguments);
|
| 386 |
if (!webdav_check_collection_perms($source_collection)) return webdav_session_close();
|
| 387 |
$source_item = webdav_resource($source_collection);
|
| 388 |
if ($source_item===false) {
|
| 389 |
return webdav_session_close(WEBDAV_STATUS_NOT_FOUND);
|
| 390 |
}
|
| 391 |
|
| 392 |
$target_session=webdav_session_create($options['dest']);
|
| 393 |
$target_name=array_pop($target_session->route);
|
| 394 |
$target_collection = webdav_route_resolve($target_session->route);
|
| 395 |
if (!webdav_check_collection_perms($target_collection)) return webdav_session_close();
|
| 396 |
$target_item = webdav_resource($target_collection);
|
| 397 |
if ($target_item===false) {
|
| 398 |
return webdav_session_close(WEBDAV_STATUS_NOT_FOUND);
|
| 399 |
}
|
| 400 |
|
| 401 |
webdav_session_set_status(WEBDAV_STATUS_CREATED);
|
| 402 |
webdav_operation($target_collection, 'copy', $target_name, $source_collection);
|
| 403 |
return webdav_session_close();
|
| 404 |
}
|
| 405 |
|
| 406 |
/**
|
| 407 |
* PROPPATCH method handler
|
| 408 |
*
|
| 409 |
* @param array general parameter passing array
|
| 410 |
* @return bool true on success
|
| 411 |
*/
|
| 412 |
function PROPPATCH(& $options) {
|
| 413 |
webdav_session_start($options['path']);
|
| 414 |
webdav_status_not_implemented();
|
| 415 |
if (webdav_session_failed()) return webdav_session_close();
|
| 416 |
}
|
| 417 |
|
| 418 |
/**
|
| 419 |
* LOCK method handler
|
| 420 |
*
|
| 421 |
* @param array general parameter passing array
|
| 422 |
* @return bool true on success
|
| 423 |
*/
|
| 424 |
function LOCK(& $options) {
|
| 425 |
webdav_session_start($options['path']);
|
| 426 |
if (webdav_session_failed()) return webdav_session_status();
|
| 427 |
|
| 428 |
// lookup the collection to lock
|
| 429 |
$handler = webdav_route_resolve(webdav_session('route'));
|
| 430 |
if (!webdav_check_collection_perms($handler)) return webdav_session_close();
|
| 431 |
if ($handler===false) return webdav_session_close(WEBDAV_STATUS_NOT_FOUND);
|
| 432 |
|
| 433 |
webdav_operation($handler, 'lock');
|
| 434 |
if (webdav_session_failed()) return webdav_session_close();
|
| 435 |
|
| 436 |
$options["timeout"] = time()+webdav_lock_timeout();
|
| 437 |
|
| 438 |
if (isset($options["update"])) { // Lock Update
|
| 439 |
$cursor = db_query("SELECT * FROM {webdav_locks} WHERE path = '%s' AND token = '%s'",
|
| 440 |
$options[path],
|
| 441 |
$options[update]);
|
| 442 |
|
| 443 |
$lock = db_fetch_array($cursor);
|
| 444 |
if ($lock) {
|
| 445 |
$lock['expires']=$options[timeout];
|
| 446 |
$lock['modified']=time();
|
| 447 |
drupal_write_record('dav_locks', $lock, 'token');
|
| 448 |
$options['owner'] = $lock['owner'];
|
| 449 |
$options['scope'] = $lock["exclusivelock"] ? "exclusive" : "shared";
|
| 450 |
$options['type'] = $lock["exclusivelock"] ? "write" : "read";
|
| 451 |
return true;
|
| 452 |
} else {
|
| 453 |
return false;
|
| 454 |
}
|
| 455 |
}
|
| 456 |
|
| 457 |
$lock=array(
|
| 458 |
'token' => $options[locktoken],
|
| 459 |
'path' => $options[path],
|
| 460 |
'created' => time(),
|
| 461 |
'modified' => time(),
|
| 462 |
'owner' => $options[owner],
|
| 463 |
'expires' => $options[timeout],
|
| 464 |
'exclusivelock' => $options['scope'] === "exclusive" ? "1" : "0",
|
| 465 |
);
|
| 466 |
drupal_write_record('webdav_locks', $lock);
|
| 467 |
|
| 468 |
return webdav_session_close();
|
| 469 |
}
|
| 470 |
|
| 471 |
/**
|
| 472 |
* UNLOCK method handler
|
| 473 |
*
|
| 474 |
* @param array general parameter passing array
|
| 475 |
* @return bool true on success
|
| 476 |
*/
|
| 477 |
function UNLOCK(& $options) {
|
| 478 |
webdav_session_start($options['path']);
|
| 479 |
if (webdav_session_failed()) return webdav_session_close();
|
| 480 |
|
| 481 |
// lookup the collection to lock
|
| 482 |
$handler = webdav_route_resolve(webdav_session('route'));
|
| 483 |
if (!webdav_check_collection_perms($handler)) return webdav_session_close();
|
| 484 |
if ($handler===false) return webdav_session_close(WEBDAV_STATUS_NOT_FOUND);
|
| 485 |
|
| 486 |
webdav_operation($session, $handler, 'unlock');
|
| 487 |
if (webdav_session_failed()) return webdav_session_close();
|
| 488 |
|
| 489 |
db_query("DELETE FROM {webdav_locks} where path='%s' and token='%s'",
|
| 490 |
$options[path],
|
| 491 |
$options[token]);
|
| 492 |
return webdav_session_close(WEBDAV_STATUS_NO_CONTENT);
|
| 493 |
}
|
| 494 |
}
|