Drupal 7 mit SabreDAV verknüpfen
Connect Drupal 7 to SabreDAV
One of the best Apache2 compatible WebDAV server implementations is the object oriented
PHP implementation SabreDAV. If you use a Drupal for your content management and like to
add a WebDAV file storage, you can connect SabreDAV to the Drupal user database. You can
to this by using the Drupal module "User Data Connector" (
http://drupal.org/project/udc
, the project is used in the implementation of the Sabre_DAV_Auth_IBackend
interface,
as shown in this source code:
Eine der besten Apache2-kompatiblen WebDAV-Servervarianten ist die objektorientierte PHP-Implementation SabreDAV. Wenn als CMS Drupal verwendet wird und die dort registrierten Nutzer auch Zugriff auf WebDAV haben sollen, so kann mit ein paar Zeilen ein kleiner SabreDAV-Backend implementiert werden, dass über das Drupal User Connector Modul die Nutzerabfrage an Drupal weiterleitet:
SabreDAV BasicAuth backend class
<?php
require_once 'Sabre/autoload.php';
/**
* DP connected user authentification
*/
class Sabre_DAV_Auth_Backend_Basic_DrupalConnected implements Sabre_DAV_Auth_IBackend {
/**
* @var DrupalUserAuth
*/
public $dpu = null;
public $udcClassesLocation = null;
/**
* Returns information about the currently logged in username.
* If nobody is currently logged in, this method should return null.
* @return string|null
*/
public function getCurrentUser() {
return !$this->dpu ? null : $this->dpu->name;
}
/**
* Authenticates the user based on the current request.
* If authentication is succesful, true must be returned.
* If authentication fails, an exception must be thrown.
* @throws Sabre_DAV_Exception_NotAuthenticated
* @return bool
*/
public function authenticate(Sabre_DAV_Server $server,$realm) {
// Assuming we only accept https for basic auth
if(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') {
throw new Sabre_DAV_Exception_NotAuthenticated('HTTPS only');
}
// Prepare SabreDav auth instances, get user & password pair
$auth = new Sabre_HTTP_BasicAuth();
$auth->setHTTPRequest($server->httpRequest);
$auth->setHTTPResponse($server->httpResponse);
$auth->setRealm($realm);
$userpass = $auth->getUserPass();
// Pre check
if(!$userpass) {
$auth->requireLogin();
throw new Sabre_DAV_Exception_NotAuthenticated('No basic authentication headers were found');
}
// Include UDC
if(empty($this->udcClassesLocation)) {
$this->udcClassesLocation = $_SERVER['DOCUMENT_ROOT'] . '/sites/all/modules/udc/client';
} else {
$this->udcClassesLocation = rtrim($this->udcClassesLocation, '/');
}
require_once($this->udcClassesLocation . '/DrupalUserAuth.class.inc');
// The token used to connect to DP
DrupalUserBase::setToken("---my---token---here---");
// Authentification request
$dpu = new DrupalUserAuth();
$dpu->request(
$userpass[0], // user name
$userpass[1], // pass
null, // email
true, // active users only
null // no profile fields to fetch
);
// Validation
if (!$dpu->valid) {
$auth->requireLogin();
throw new Sabre_DAV_Exception_NotAuthenticated('Username or password does not match');
} else {
$this->dpu = $dpu;
return true;
}
}
}
SabreDAV init
<?php
require_once 'Sabre_DAV_Auth_Backend_Basic_DrupalConnected.class.php';
// Directory structure of this sample code:
//
// drwx - document root (www-data)
// dr-x - webdav
// -r-- - index.php
// drwx - davfiles
// (DAV FILES ARE IN HERE)
// drw- - data
// -rw- - locks
// dr-x - Sabre
//
$rootDirectory = new Sabre_DAV_FS_Directory('davfiles');
$server = new Sabre_DAV_Server($rootDirectory);
$server->setBaseUri('/webdav/');
// The lock manager is reponsible for making sure users don't overwrite each others changes.
// Change 'data' to a different directory, if you're storing your data somewhere else.
$lockBackend = new Sabre_DAV_Locks_Backend_File('data/locks');
$lockPlugin = new Sabre_DAV_Locks_Plugin($lockBackend);
$server->addPlugin($lockPlugin);
$authBackend = new Sabre_DAV_Auth_Backend_Basic_DrupalConnected();
$authPlugin = new Sabre_DAV_Auth_Plugin($authBackend,'WebDAV area');
$server->addPlugin($authPlugin);
$server->exec(); // All we need to do now, is to fire up the server