-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathDAVController.php
More file actions
379 lines (319 loc) · 12.5 KB
/
DAVController.php
File metadata and controls
379 lines (319 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
namespace App\Controller;
use App\Entity\Principal;
use App\Entity\User;
use App\Plugins\BirthdayCalendarPlugin;
use App\Plugins\DavisIMipPlugin;
use App\Plugins\PublicAwareDAVACLPlugin;
use App\Services\BasicAuth;
use App\Services\BirthdayService;
use App\Services\IMAPAuth;
use App\Services\LDAPAuth;
use Doctrine\ORM\EntityManagerInterface;
use PDO;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class DAVController extends AbstractController
{
public const AUTH_BASIC = 'Basic';
public const AUTH_IMAP = 'IMAP';
public const AUTH_LDAP = 'LDAP';
/**
* Is CalDAV enabled?
*
* @var bool
*/
protected $calDAVEnabled;
/**
* is CardDAV enabled?
*
* @var bool
*/
protected $cardDAVEnabled;
/**
* is WebDAV enabled?
*
* @var bool
*/
protected $webDAVEnabled;
/**
* Are public calendars enabled?
*
* @var bool
*/
protected $publicCalendarsEnabled;
/**
* Mail address to send mails from.
*
* @var string
*/
protected $inviteAddress;
/**
* Public directory of the Symfony installation.
* Needed to retrieve assets (images).
*
* @var string
*/
protected $publicDir;
/**
* WebDAV Public directory.
*
* @var string
*/
protected $webdavPublicDir;
/**
* WebDAV User Homes directory.
*
* @var string|null
*/
protected $webdavHomesDir;
/**
* WebDAV Temporary directory.
*
* @var string
*/
protected $webdavTmpDir;
/**
* @var EntityManagerInterface
*/
protected $em;
/**
* @var MailerInterface
*/
protected $mailer;
/**
* @var BirthdayService
*/
protected $birthdayService;
/**
* Base URI of the server.
*
* @var string
*/
protected $baseUri;
/**
* Basic Auth Backend class.
*
* @var BasicAuth
*/
protected $basicAuthBackend;
/**
* IMAP Auth Backend class.
*
* @var IMAPAuth
*/
protected $IMAPAuthBackend;
/**
* LDAP Auth Backend class.
*
* @var LDAPAuth
*/
protected $LDAPAuthBackend;
/**
* Logger for exceptions.
*
* @var Psr\Log\LoggerInterface;
*/
protected $logger;
/**
* Server.
*
* @var \Sabre\DAV\Server
*/
protected $server;
public function __construct(MailerInterface $mailer, BasicAuth $basicAuthBackend, IMAPAuth $IMAPAuthBackend, LDAPAuth $LDAPAuthBackend, UrlGeneratorInterface $router, EntityManagerInterface $entityManager, LoggerInterface $logger, BirthdayService $birthdayService, string $publicDir, bool $calDAVEnabled = true, bool $cardDAVEnabled = true, bool $webDAVEnabled = false, bool $publicCalendarsEnabled = true, ?string $inviteAddress = null, ?string $authMethod = null, ?string $authRealm = null, ?string $webdavPublicDir = null, ?string $webdavHomesDir = null, ?string $webdavTmpDir = null)
{
$this->publicDir = $publicDir;
$this->calDAVEnabled = $calDAVEnabled;
$this->cardDAVEnabled = $cardDAVEnabled;
$this->webDAVEnabled = $webDAVEnabled;
$this->publicCalendarsEnabled = $publicCalendarsEnabled;
$this->inviteAddress = $inviteAddress ?? null;
$this->webdavPublicDir = $webdavPublicDir;
$this->webdavHomesDir = $webdavHomesDir;
$this->webdavTmpDir = $webdavTmpDir;
$this->em = $entityManager;
$this->logger = $logger;
$this->mailer = $mailer;
$this->birthdayService = $birthdayService;
$this->baseUri = $router->generate('dav', ['path' => '']);
$this->basicAuthBackend = $basicAuthBackend;
$this->IMAPAuthBackend = $IMAPAuthBackend;
$this->LDAPAuthBackend = $LDAPAuthBackend;
$this->initServer($authMethod, $authRealm);
$this->initExceptionListener();
}
#[Route('/', name: 'home')]
public function home(): Response
{
return $this->render('index.html.twig', [
'version' => \App\Version::VERSION,
]);
}
private function initServer(string $authMethod, string $authRealm = User::DEFAULT_AUTH_REALM)
{
// Get the PDO Connection of type PDO
$pdo = $this->em->getConnection()->getNativeConnection();
/*
* The backends.
*/
switch ($authMethod) {
case self::AUTH_IMAP:
$authBackend = $this->IMAPAuthBackend;
break;
case self::AUTH_LDAP:
$authBackend = $this->LDAPAuthBackend;
break;
case self::AUTH_BASIC:
default:
$authBackend = $this->basicAuthBackend;
break;
}
$authBackend->setRealm($authRealm);
$principalBackend = new \Sabre\DAVACL\PrincipalBackend\PDO($pdo);
/**
* The directory tree.
*
* Basically this is an array which contains the 'top-level' directories in the
* WebDAV server.
*/
$nodes = [
// /principals
new \Sabre\CalDAV\Principal\Collection($principalBackend),
];
if ($this->webdavHomesDir) {
$nodes[] = new \Sabre\DAVACL\FS\HomeCollection($principalBackend, $this->webdavHomesDir);
}
if ($this->calDAVEnabled) {
$calendarBackend = new \Sabre\CalDAV\Backend\PDO($pdo);
$nodes[] = new \Sabre\CalDAV\CalendarRoot($principalBackend, $calendarBackend);
}
if ($this->cardDAVEnabled) {
$carddavBackend = new \Sabre\CardDAV\Backend\PDO($pdo);
$nodes[] = new \Sabre\CardDAV\AddressBookRoot($principalBackend, $carddavBackend);
}
if ($this->webDAVEnabled && $this->webdavTmpDir && $this->webdavPublicDir) {
$nodes[] = new \Sabre\DAV\FS\Directory($this->webdavPublicDir);
}
// The object tree needs in turn to be passed to the server class
$this->server = new \Sabre\DAV\Server($nodes);
$this->server->setBaseUri($this->baseUri);
// Plugins
$this->server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend, $authRealm));
$this->server->addPlugin(new \Sabre\DAV\Browser\Plugin(false)); // We disable the file creation / upload / sharing in the browser
$this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());
$aclPlugin = new PublicAwareDAVACLPlugin($this->em, $this->publicCalendarsEnabled);
$aclPlugin->hideNodesFromListings = true;
$aclPlugin->allowUnauthenticatedAccess = true; // Already the default, but setting it is future-proof
// Fetch admins, if any
$admins = $this->em->getRepository(Principal::class)->findBy(['isAdmin' => true]);
foreach ($admins as $principal) {
$aclPlugin->adminPrincipals[] = $principal->getUri();
}
$this->server->addPlugin($aclPlugin);
$this->server->addPlugin(new \Sabre\DAV\PropertyStorage\Plugin(
new \Sabre\DAV\PropertyStorage\Backend\PDO($pdo)
));
// CalDAV plugins
if ($this->calDAVEnabled) {
$this->server->addPlugin(new \Sabre\DAV\Sharing\Plugin());
$this->server->addPlugin(new \Sabre\CalDAV\Plugin());
$this->server->addPlugin(new \Sabre\CalDAV\Schedule\Plugin());
$this->server->addPlugin(new \Sabre\CalDAV\SharingPlugin());
$this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin());
$this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
if ($this->inviteAddress) {
$this->server->addPlugin(new DavisIMipPlugin($this->mailer, $this->inviteAddress, $this->publicDir));
}
}
// CardDAV plugins
if ($this->cardDAVEnabled) {
$this->server->addPlugin(new \Sabre\CardDAV\Plugin());
$this->server->addPlugin(new \Sabre\CardDAV\VCFExportPlugin());
}
if ($this->cardDAVEnabled && $this->calDAVEnabled) {
$this->server->addPlugin(new BirthdayCalendarPlugin($this->birthdayService, $calendarBackend));
}
// WebDAV plugins
if ($this->webDAVEnabled && $this->webdavTmpDir && $this->webdavPublicDir) {
if (!is_dir($this->webdavTmpDir) || !is_dir($this->webdavPublicDir)) {
throw new \Exception('The WebDAV temp dir and/or public dir are not available. Make sure they are created with the correct permissions.');
}
$lockBackend = new \Sabre\DAV\Locks\Backend\File($this->webdavTmpDir.'/locksdb');
$this->server->addPlugin(new \Sabre\DAV\Locks\Plugin($lockBackend));
$this->server->addPlugin(new \Sabre\DAV\Browser\GuessContentType());
$this->server->addPlugin(new \Sabre\DAV\TemporaryFileFilterPlugin($this->webdavTmpDir));
}
}
private function initExceptionListener()
{
$this->server->on('exception', function (\Throwable $e) {
// We don't need a trace for simple authentication exceptions
if ($e instanceof \Sabre\DAV\Exception\NotAuthenticated) {
$this->logger->warning('[401]: '.get_class($e)." - No 'Authorization: Basic' header found. Login was needed");
return;
}
$httpCode = ($e instanceof \Sabre\DAV\Exception) ? $e->getHTTPCode() : 500;
$this->logger->error('['.$httpCode.']: '.get_class($e).' - '.$e->getMessage(), $e->getTrace());
});
}
#[Route('/dav/{path}', name: 'dav', requirements: ['path' => '.*'])]
public function dav(Request $request, ?string $path, ?Profiler $profiler = null)
{
// We don't want the toolbar on the /dav/* routes
if ($profiler instanceof Profiler) {
$profiler->disable();
}
// We need to acknowledge the OPTIONS call before sabre/dav for public
// calendars since we're circumventing the lib
if ('OPTIONS' === $request->getMethod()) {
$response = new Response();
// Adapted from CorePlugin's httpOptions()
// https://github.com/sabre-io/dav/blob/master/lib/DAV/CorePlugin.php#L210
$methods = $this->server->getAllowedMethods('');
$response->headers->set('Allow', strtoupper(implode(', ', $methods)));
$features = ['1', '3', 'extended-mkcol'];
foreach ($this->server->getPlugins() as $plugin) {
$features = array_merge($features, $plugin->getFeatures());
}
$response->headers->set('DAV', implode(', ', $features));
$response->headers->set('MS-Author-Via', 'DAV');
return $response;
}
// \Sabre\DAV\Server does not let us use a custom SAPI, and its behaviour
// is to directly output headers and content to php://output. Hence, we
// let the headers pass (we have not choice) and capture the output in a
// buffer.
// This allows us to use a Response, and not to break the events triggered
// by Symfony after the response is sent, like for instance the TERMINATE
// event from the Kernel, that is used to send emails...
ob_start(); // Does not capture headers!
$this->server->start();
$output = ob_get_contents();
ob_end_clean();
// As previously said, headers are already _prepared_ by the server,
// so we can't modify them or remove them. But they are not _sent_ yet,
// so headers_sent() is false, and Symfony will add its own headers above it.
//
// The Content-type header is the problem, since Symfony will
// output `text/html` for everything since it doesn't know any better.
// Thus, we have to get the _real_ Content-type header already prepared,
// and force it in the Symfony Response.
//
// That's what we do here.
$response = new Response($output, http_response_code(), []);
foreach (headers_list() as $header) {
if ('content-type:' === strtolower(substr($header, 0, 13))) {
$headerArray = explode(':', $header);
$response->headers->set('Content-type', $headerArray[1]);
}
}
return $response;
}
}