334b97469f5cbc8e23cc7721674a2e7eebc4ef72
[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-2015 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         \Sabre\DAV\Server::$exposeVersion = false;
74         
75         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
76             self::$_server->debugExceptions = true;
77             $contentType = self::$_server->httpRequest->getHeader('Content-Type');
78             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " requestContentType: " . $contentType);
79             
80             if (stripos($contentType, 'text') === 0 || stripos($contentType, '/xml') !== false) {
81                 // NOTE inputstream can not be rewinded
82                 $debugStream = fopen('php://temp','r+');
83                 stream_copy_to_stream($this->_body, $debugStream);
84                 rewind($debugStream);
85                 $this->_body = $debugStream;
86                 
87                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " <<< *DAV request\n" . stream_get_contents($this->_body));
88                 rewind($this->_body);
89             } else {
90                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " <<< *DAV request\n -- BINARY DATA --");
91             }
92         }
93         
94         self::$_server->httpRequest->setBody($this->_body);
95         
96         // compute base uri
97         self::$_server->setBaseUri($this->_request->getBaseUrl() . '/');
98         
99         $tempDir = Tinebase_Core::getTempDir();
100         if (!empty($tempDir)) {
101             self::$_server->addPlugin(
102                 new \Sabre\DAV\Locks\Plugin(new \Sabre\DAV\Locks\Backend\File($tempDir . '/webdav.lock'))
103             );
104         }
105         
106         self::$_server->addPlugin(
107             new \Sabre\DAV\Auth\Plugin(new Tinebase_WebDav_Auth(), null)
108         );
109         
110         $aclPlugin = new \Sabre\DAVACL\Plugin();
111         $aclPlugin->defaultUsernamePath    = Tinebase_WebDav_PrincipalBackend::PREFIX_USERS;
112         $aclPlugin->principalCollectionSet = array (Tinebase_WebDav_PrincipalBackend::PREFIX_USERS, Tinebase_WebDav_PrincipalBackend::PREFIX_GROUPS, Tinebase_WebDav_PrincipalBackend::PREFIX_INTELLIGROUPS);
113         
114         $aclPlugin->principalSearchPropertySet = array(
115             '{DAV:}displayname'                                                   => 'Display name',
116             '{' . \Sabre\DAV\Server::NS_SABREDAV . '}email-address'               => 'Email address',
117             '{' . \Sabre\CalDAV\Plugin::NS_CALENDARSERVER . '}email-address-set'  => 'Email addresses',
118             '{' . \Sabre\CalDAV\Plugin::NS_CALENDARSERVER . '}first-name'         => 'First name',
119             '{' . \Sabre\CalDAV\Plugin::NS_CALENDARSERVER . '}last-name'          => 'Last name',
120             '{' . \Sabre\CalDAV\Plugin::NS_CALDAV         . '}calendar-user-address-set' => 'Calendar user address set',
121             '{' . \Sabre\CalDAV\Plugin::NS_CALDAV         . '}calendar-user-type' => 'Calendar user type'
122         );
123         
124         self::$_server->addPlugin($aclPlugin);
125         
126         self::$_server->addPlugin(new \Sabre\CardDAV\Plugin());
127         self::$_server->addPlugin(new Calendar_Frontend_CalDAV_SpeedUpPlugin); // this plugin must be loaded before CalDAV plugin
128         self::$_server->addPlugin(new Calendar_Frontend_CalDAV_FixMultiGet404Plugin()); // replacement for new \Sabre\CalDAV\Plugin());
129         self::$_server->addPlugin(new \Sabre\CalDAV\SharingPlugin());
130         self::$_server->addPlugin(new Calendar_Frontend_CalDAV_PluginAutoSchedule());
131         self::$_server->addPlugin(new Calendar_Frontend_CalDAV_PluginDefaultAlarms());
132         self::$_server->addPlugin(new Calendar_Frontend_CalDAV_PluginManagedAttachments());
133         self::$_server->addPlugin(new Calendar_Frontend_CalDAV_PluginPrivateEvents());
134         self::$_server->addPlugin(new Tinebase_WebDav_Plugin_Inverse());
135         self::$_server->addPlugin(new Tinebase_WebDav_Plugin_OwnCloud());
136         self::$_server->addPlugin(new Tinebase_WebDav_Plugin_PrincipalSearch());
137         self::$_server->addPlugin(new Tinebase_WebDav_Plugin_ExpandedPropertiesReport());
138         self::$_server->addPlugin(new \Sabre\DAV\Browser\Plugin());
139         self::$_server->addPlugin(new Tinebase_WebDav_Plugin_SyncToken());
140         self::$_server->addPlugin(new Calendar_Frontend_CalDAV_SpeedUpPropfindPlugin());
141
142         $contentType = self::$_server->httpRequest->getHeader('Content-Type');
143         $logOutput = Tinebase_Core::isLogLevel(Zend_Log::DEBUG) && (stripos($contentType, 'text') === 0 || stripos($contentType, '/xml') !== false);
144
145         if ($logOutput) {
146             ob_start();
147         }
148         
149         self::$_server->exec();
150         
151         if ($logOutput) {
152             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " >>> *DAV response:\n" . ob_get_contents());
153             ob_end_flush();
154         } else {
155
156             Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . " <<< *DAV response\n -- BINARY DATA --");
157         }
158
159         Tinebase_Controller::getInstance()->logout($this->_request->getServer('REMOTE_ADDR'));
160     }
161     
162    /**
163     * helper to return request
164     *
165     * @return Sabre\HTTP\Request
166     */
167     public static function getRequest()
168     {
169         return self::$_server ? self::$_server->httpRequest : new Sabre\HTTP\Request();
170     }
171
172     /**
173      * helper to return response
174      *
175      * @return Sabre\HTTP\Response
176      */
177     public static function getResponse()
178     {
179         return self::$_server ? self::$_server->httpResponse : new Sabre\HTTP\Response();
180     }
181
182     /**
183     * returns request method
184     *
185     * @return string
186     */
187     public function getRequestMethod()
188     {
189         return self::getRequest()->getMethod();
190     }
191 }