<?php

/**
 * CodeIgniter Controller
 *
 * @category CodeIgniter
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Christian Koller
 * @copyright 2009 untermStrich
 * @package controllers
 * @version $Id: dav.php christian $
 */
require_once APPPATH . 'us_libraries/vendor/autoload.php';

/**
 * The CalDAV functions controller of untermStrich
 * 
 * calendarobjects
 * calendars
 * calendarinstances
 * calendarsubscriptions
 * calendarchanges
 * schedulingobjects
 */
class Dav extends My_Controller
{
    protected $full_base_url;

    public function __construct()
    {
        parent::__construct();

        
        function shutDownFunction()
        {
            $error = error_get_last();

            if ($error !== NULL) {
            } else {
            }
        }

        register_shutdown_function('shutDownFunction');
        
        $this->full_base_url = base_url() . 'dav/';
    }

    /**
     * Remap all requests
     *
     * @param   string  $object_called  Name of the called object
     * @return  void
     */
    public function _remap($object_called)
    {
        $uri = $this->uri->uri_string();
        $dav_log_msg = 'Hello ' . $_SERVER['REQUEST_METHOD'] . ' - ' . $object_called . ' - ' . $uri . "\nUri: " . print_r($_SERVER['REQUEST_URI'], true) . "\nPost: " . print_r($_POST, true) . "\nBody: " . print_r(file_get_contents('php://input'), true)."\n";
        log_message('debug', $dav_log_msg, false, 'dav');
        
        $this->load->model('Specific_config_model', 'specific_config');
        $dav_calendar_active = $this->specific_config->get('dav_calendar_active', false);
        $dav_card_active = $this->specific_config->get('dav_card_active', false);
        
        $this->load->us_library('dav/DavUstrichAuthBackend');
        $this->load->us_library('dav/DavUstrichPrincipalBackend');
        $this->load->us_library('dav/DavUstrichSchedulePlugin');
        
        $principalBackend = new DavUstrichPrincipalBackend();
        $tree = [
            new Sabre\CalDAV\Principal\Collection($principalBackend)
        ];

        if ($dav_card_active) {
            $this->load->us_library('dav/DavUstrichCardBackend');
            $cardBackend = new DavUstrichCardBackend();
            $tree[] = new Sabre\CardDAV\AddressBookRoot($principalBackend, $cardBackend);
        }

        if ($dav_calendar_active) {
            $this->load->us_library('dav/DavUstrichCalendarBackend');
            $calendarBackend = new DavUstrichCalendarBackend();
            $tree[] = new Sabre\CalDAV\CalendarRoot($principalBackend, $calendarBackend);
        }

        $server = new Sabre\DAV\Server($tree);

        $server->setBaseUri(parse_url($this->full_base_url, PHP_URL_PATH));

        $authBackend = new DavUstrichAuthBackend($object_called);
        
        $dav_log_msg = 'AuthBackend ' . get_class($authBackend);
        log_message('debug', $dav_log_msg, false, 'dav');
        
        $authPlugin = new Sabre\DAV\Auth\Plugin($authBackend);
        $server->addPlugin($authPlugin);

        $aclPlugin = new Sabre\DAVACL\Plugin();
        $aclPlugin->allowUnauthenticatedAccess = false;
        $aclPlugin->hideNodesFromListings = true;
        $server->addPlugin($aclPlugin);

        $server->addPlugin(new Sabre\DAV\Sync\Plugin());
        
        if ($dav_calendar_active) {
            $caldavPlugin = new Sabre\CalDAV\Plugin();
            $server->addPlugin($caldavPlugin);

            $server->addPlugin(
                    new Sabre\CalDAV\Subscriptions\Plugin()
            );

            $server->addPlugin(
                    new DavUstrichSchedulePlugin()
            );

            $server->addPlugin(new Sabre\DAV\Sharing\Plugin());
            $server->addPlugin(new Sabre\CalDAV\SharingPlugin());
        }
        
        if ($dav_card_active) {
            $carddavPlugin = new Sabre\CardDAV\Plugin();
            $server->addPlugin($carddavPlugin);
        }

        $server->addPlugin(new Sabre\DAV\Browser\Plugin());

        $server->start();
    }

}
