8e8151b34d61a969a1d6e9e6f85aae3403542be0
[tine20] / tine20 / Tinebase / Server / WebDAV.php
1 <?php
2 /**
3  * Tine 2.0
4  * 
5  * @package     Tinebase
6  * @subpackage  Server
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @copyright   Copyright (c) 2011-2014 Metaways Infosystems GmbH (http://www.metaways.de)
9  * @author      Lars Kneschke <l.kneschke@metaways.de>
10  */
11
12 /**
13  * webdav Server class with handle() function
14  * 
15  * @package     Tinebase
16  * @subpackage  Server
17  */
18 class Tinebase_Server_WebDAV extends Tinebase_Server_Abstract implements Tinebase_Server_Interface
19 {
20     const REQUEST_TYPE = 'WebDAV';
21     
22    /**
23     * @var \Sabre\DAV\Server
24     */
25     protected static $_server;
26     
27     /**
28      * (non-PHPdoc)
29      * @see Tinebase_Server_Interface::handle()
30      */
31     public function handle(\Zend\Http\Request $request = null, $body = null)
32     {
33         $this->_request = $request instanceof \Zend\Http\Request ? $request : Tinebase_Core::get(Tinebase_Core::REQUEST);
34         if ($body !== null) {
35             $this->_body = $body;
36         } else if ($this->_request instanceof \Zend\Http\Request) {
37             $this->_body = fopen('php://temp', 'r+');
38             fwrite($this->_body, $request->getContent());
39             rewind($this->_body);
40         }
41         
42         try {
43             list($loginName, $password) = $this->_getAuthData($this->_request);
44             
45         } catch (Tinebase_Exception_NotFound $tenf) {
46             header('WWW-Authenticate: Basic realm="WebDAV for Tine 2.0"');
47             header('HTTP/1.1 401 Unauthorized');
48             
49             return;
50         }
51         
52         if (Tinebase_Core::isLogLevel(Zend_Log::INFO))
53             Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ .' is CalDav, CardDAV or WebDAV request.');
54         
55         Tinebase_Core::initFramework();
56         
57         if (Tinebase_Controller::getInstance()->login(
58             $loginName,
59             $password,
60             $this->_request,
61             self::REQUEST_TYPE
62         ) !== true) {
63             header('WWW-Authenticate: Basic realm="WebDAV for Tine 2.0"');
64             header('HTTP/1.1 401 Unauthorized');
65             
66             return;
67         }
68         
69         if (Tinebase_Core::isLogLevel(Zend_Log::INFO))
70             Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ .' requestUri:' . $this->_request->getRequestUri());
71         
72         self::$_server = new \Sabre\DAV\Server(new Tinebase_WebDav_Root());
73         
74         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
75             $contentType = self::$_server->httpRequest->getHeader('Content-Type');
76             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " requestContentType: " . $contentType);
77             
78             if (preg_match('/^text/', $contentType)) {
79                 // NOTE inputstream can not be rewinded
80                 $debugStream = fopen('php://temp','r+');
81                 stream_copy_to_stream($this->_body, $debugStream);
82                 rewind($debugStream);
83                 $this->_body = $debugStream;
84                 
85                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " <<< *DAV request\n" . stream_get_contents($this->_body));
86                 rewind($this->_body);
87             } else {
88                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " <<< *DAV request\n -- BINARY DATA --");
89             }
90         }
91         
92         self::$_server->httpRequest->setBody($this->_body);
93         
94         // compute base uri
95         self::$_server->setBaseUri($this->_request->getBaseUrl() . '/');
96         
97         $tempDir = Tinebase_Core::getTempDir();
98         if (!empty($tempDir)) {
99             self::$_server->addPlugin(
100                 new \Sabre\DAV\Locks\Plugin(new \Sabre\DAV\Locks\Backend\File($tempDir . '/webdav.lock'))
101             );
102         }
103         
104         self::$_server->addPlugin(
105             new \Sabre\DAV\Auth\Plugin(new Tinebase_WebDav_Auth(), null)
106         );
107         
108         $aclPlugin = new \Sabre\DAVACL\Plugin();
109         $aclPlugin->defaultUsernamePath    = Tinebase_WebDav_PrincipalBackend::PREFIX_USERS;
110         $aclPlugin->principalCollectionSet = array (Tinebase_WebDav_PrincipalBackend::PREFIX_USERS, Tinebase_WebDav_PrincipalBackend::PREFIX_GROUPS);
111         
112         $aclPlugin->principalSearchPropertySet = array(
113             '{DAV:}displayname'                                                   => 'Display name',
114             '{' . \Sabre\DAV\Server::NS_SABREDAV . '}email-address'               => 'Email address',
115             '{' . \Sabre\CalDAV\Plugin::NS_CALENDARSERVER . '}email-address-set'  => 'Email addresses',
116             '{' . \Sabre\CalDAV\Plugin::NS_CALENDARSERVER . '}first-name'         => 'First name',
117             '{' . \Sabre\CalDAV\Plugin::NS_CALENDARSERVER . '}last-name'          => 'Last name',
118             '{' . \Sabre\CalDAV\Plugin::NS_CALDAV         . '}calendar-user-address-set' => 'Calendar user address set',
119             '{' . \Sabre\CalDAV\Plugin::NS_CALDAV         . '}calendar-user-type' => 'Calendar user type'
120         );
121         
122         self::$_server->addPlugin($aclPlugin);
123         
124         self::$_server->addPlugin(new \Sabre\CardDAV\Plugin());
125         self::$_server->addPlugin(new Calendar_Frontend_CalDAV_SpeedUpPlugin); // this plugin must be loaded before CalDAV plugin
126         self::$_server->addPlugin(new \Sabre\CalDAV\Plugin());
127         self::$_server->addPlugin(new \Sabre\CalDAV\SharingPlugin());
128         self::$_server->addPlugin(new Calendar_Frontend_CalDAV_PluginAutoSchedule());
129         self::$_server->addPlugin(new Calendar_Frontend_CalDAV_PluginDefaultAlarms());
130         self::$_server->addPlugin(new Calendar_Frontend_CalDAV_PluginManagedAttachments());
131         self::$_server->addPlugin(new Calendar_Frontend_CalDAV_PluginPrivateEvents());
132         self::$_server->addPlugin(new Tinebase_WebDav_Plugin_Inverse());
133         self::$_server->addPlugin(new Tinebase_WebDav_Plugin_OwnCloud());
134         self::$_server->addPlugin(new Tinebase_WebDav_Plugin_PrincipalSearch());
135         #self::$_server->addPlugin(new DAV\Sync\Plugin());
136         self::$_server->addPlugin(new \Sabre\DAV\Browser\Plugin());
137         
138         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
139             ob_start();
140         }
141         
142         self::$_server->exec();
143         
144         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
145             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " >>> *DAV response:\n" . ob_get_contents());
146             ob_end_flush();
147         }
148         
149         Tinebase_Controller::getInstance()->logout($this->_request->getServer('REMOTE_ADDR'));
150     }
151     
152    /**
153     * helper to return request
154     *
155     * @return Sabre\HTTP\Request
156     */
157     public static function getRequest()
158     {
159         return self::$_server ? self::$_server->httpRequest : new Sabre\HTTP\Request();
160     }
161     
162     /**
163     * returns request method
164     *
165     * @return string
166     */
167     public function getRequestMethod()
168     {
169         return self::getRequest()->getMethod();
170     }
171 }