PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Implementing a WebDAV filesystem with PHP and SabreDAV

The Full WebDAV Server

Now that we've implemented the authentication method, we can add it to the code previously developed. The following listing shows the entire code as it should be pieced together.

The only thing you need to be wary of is that the user_exists() and get_user_hash() methods don't really exist - you must replace these with your own mechanism for looking up user details.

Additionally, I've omitted the Sabre_DAV_TemporaryFileFilterPlugin plug-in, but you can experiment with that if you want to.

Listing 10 The full WebDAV server (server.php)
<?php
    require_once('Sabre.autoload.php');
 
    // set the path and authentication realm
    $path  = '/var/www/dav/files';
    $realm = 'webdavserver';
 
    // set up authentication
    $auth = new Sabre_HTTP_DigestAuth();
    $auth->setRealm($realm);
    $auth->init();
 
    // @todo
    function user_exists($username)
    {
        return false;
    }
 
    // @todo
    function get_user_hash($username)
    {
        return null;
    }
 
    // find user details for authentication
    $username = $auth->getUsername();
 
    if (!user_exists($username)) {
        $auth->requireLogin();
        echo "Authentication required";
        exit;
    }
 
    $hash = get_user_hash($username);
 
    // try and authenticate the user
    if (!$auth->validateA1($hash)) {
        $auth->requireLogin();
        echo "Authentication required";
        exit;
    }
 
    // create the WebDAV server
 
    $tree = new Sabre_DAV_ObjectTree(
        new Sabre_DAV_FS_Directory($path)
    );
 
    $server = new Sabre_DAV_Server($tree);
 
    // add the browser plug-in
    $server->addPlugin(
        new Sabre_DAV_Browser_Plugin()
    );
 
    // add the locks plug-in
    $backend = new Sabre_DAV_Locks_Backend_FS();
 
    $server->addPlugin(
        new Sabre_DAV_Locks_Plugin($backend)
    );
 
    // handle the request
    $server->exec();
?>

In This Article