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