<?php

// !!!! Make sure the Sabre directory is in the include_path !!!
// example:
//set_include_path('lib/' . PATH_SEPARATOR . get_include_path());
//  VERSION:1.0   WARNING: NOT YET UPDATED for Version 2


// settings
date_default_timezone_set('Canada/Eastern');

$phplib=getenv("SURGEMAIL_ROOT")."/phplib";
require_once "$phplib/netwin_v2/debug.php";
require_once "$phplib/".getenv("SABRE_VERSION")."/lib/Sabre/autoload.php";
require_once "$phplib/netwin_v2/Nwauth_sabre.php";
$db_path="data";
$publicDir = 'public';
$tmpDir = 'tmpdata';
dbg_init("dav.log");

dbg("----");
dbg("Sabre DAV");


// If you want to run the SabreDAV server in a custom location (using mod_rewrite for instance)
// You can override the baseUri here.
$baseUri = '/dav';
$pdo = new PDO("sqlite:$db_path/db.sqlite");
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

// Create the root node
$root = new Sabre_DAV_FS_Directory($publicDir);

// The rootnode needs in turn to be passed to the server class
$server = new Sabre_DAV_Server($root);

if (isset($baseUri))
    $server->setBaseUri($baseUri);

// Support for LOCK and UNLOCK
$lockBackend = new Sabre_DAV_Locks_Backend_File($tmpDir . '/locksdb');
$lockPlugin = new Sabre_DAV_Locks_Plugin($lockBackend);
$server->addPlugin($lockPlugin);

// Support for html frontend
$browser = new Sabre_DAV_Browser_Plugin();
$server->addPlugin($browser);

// Automatically guess (some) contenttypes, based on extesion
$server->addPlugin(new Sabre_DAV_Browser_GuessContentType());

// Authentication backend
$authBackend = new Sabre_DAV_Auth_Backend_Nwauth($pdo);
$auth = new Sabre_DAV_Auth_Plugin($authBackend,'SabreDAV');
$server->addPlugin($auth);

// Temporary file filter
$tempFF = new Sabre_DAV_TemporaryFileFilterPlugin($tmpDir);
$server->addPlugin($tempFF);
$browser = new Sabre_DAV_Browser_Plugin();
$server->addPlugin($browser);
print("aa"); return;

// And off we go!
$server->exec();
