carddav.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. // Backends
  8. use OC\KnownUser\KnownUserService;
  9. use OCA\DAV\AppInfo\PluginManager;
  10. use OCA\DAV\CalDAV\Proxy\ProxyMapper;
  11. use OCA\DAV\CardDAV\AddressBookRoot;
  12. use OCA\DAV\CardDAV\CardDavBackend;
  13. use OCA\DAV\CardDAV\ImageExportPlugin;
  14. use OCA\DAV\CardDAV\PhotoCache;
  15. use OCA\DAV\CardDAV\Security\CardDavRateLimitingPlugin;
  16. use OCA\DAV\CardDAV\Validation\CardDavValidatePlugin;
  17. use OCA\DAV\Connector\LegacyDAVACL;
  18. use OCA\DAV\Connector\Sabre\Auth;
  19. use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
  20. use OCA\DAV\Connector\Sabre\MaintenancePlugin;
  21. use OCA\DAV\Connector\Sabre\Principal;
  22. use OCP\Accounts\IAccountManager;
  23. use OCP\App\IAppManager;
  24. use OCP\EventDispatcher\IEventDispatcher;
  25. use OCP\IGroupManager;
  26. use OCP\Server;
  27. use Psr\Log\LoggerInterface;
  28. use Sabre\CardDAV\Plugin;
  29. $authBackend = new Auth(
  30. \OC::$server->getSession(),
  31. \OC::$server->getUserSession(),
  32. \OC::$server->getRequest(),
  33. \OC::$server->getTwoFactorAuthManager(),
  34. \OC::$server->getBruteForceThrottler(),
  35. 'principals/'
  36. );
  37. $principalBackend = new Principal(
  38. \OC::$server->getUserManager(),
  39. \OC::$server->getGroupManager(),
  40. \OC::$server->get(IAccountManager::class),
  41. \OC::$server->getShareManager(),
  42. \OC::$server->getUserSession(),
  43. \OC::$server->getAppManager(),
  44. \OC::$server->query(ProxyMapper::class),
  45. \OC::$server->get(KnownUserService::class),
  46. \OC::$server->getConfig(),
  47. \OC::$server->getL10NFactory(),
  48. 'principals/'
  49. );
  50. $db = \OC::$server->getDatabaseConnection();
  51. $cardDavBackend = new CardDavBackend(
  52. $db,
  53. $principalBackend,
  54. \OC::$server->getUserManager(),
  55. \OC::$server->get(IEventDispatcher::class),
  56. \OC::$server->get(\OCA\DAV\CardDAV\Sharing\Backend::class),
  57. );
  58. $debugging = \OC::$server->getConfig()->getSystemValue('debug', false);
  59. // Root nodes
  60. $principalCollection = new \Sabre\CalDAV\Principal\Collection($principalBackend);
  61. $principalCollection->disableListing = !$debugging; // Disable listing
  62. $pluginManager = new PluginManager(\OC::$server, \OC::$server->query(IAppManager::class));
  63. $addressBookRoot = new AddressBookRoot($principalBackend, $cardDavBackend, $pluginManager, \OC::$server->getUserSession()->getUser(), \OC::$server->get(IGroupManager::class));
  64. $addressBookRoot->disableListing = !$debugging; // Disable listing
  65. $nodes = [
  66. $principalCollection,
  67. $addressBookRoot,
  68. ];
  69. // Fire up server
  70. $server = new \Sabre\DAV\Server($nodes);
  71. $server::$exposeVersion = false;
  72. $server->httpRequest->setUrl(\OC::$server->getRequest()->getRequestUri());
  73. $server->setBaseUri($baseuri);
  74. // Add plugins
  75. $server->addPlugin(new MaintenancePlugin(\OC::$server->getConfig(), \OC::$server->getL10N('dav')));
  76. $server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend));
  77. $server->addPlugin(new Plugin());
  78. $server->addPlugin(new LegacyDAVACL());
  79. if ($debugging) {
  80. $server->addPlugin(new Sabre\DAV\Browser\Plugin());
  81. }
  82. $server->addPlugin(new \Sabre\DAV\Sync\Plugin());
  83. $server->addPlugin(new \Sabre\CardDAV\VCFExportPlugin());
  84. $server->addPlugin(new ImageExportPlugin(new PhotoCache(
  85. \OC::$server->getAppDataDir('dav-photocache'),
  86. \OC::$server->get(LoggerInterface::class)
  87. )));
  88. $server->addPlugin(new ExceptionLoggerPlugin('carddav', \OC::$server->get(LoggerInterface::class)));
  89. $server->addPlugin(Server::get(CardDavRateLimitingPlugin::class));
  90. $server->addPlugin(Server::get(CardDavValidatePlugin::class));
  91. // And off we go!
  92. $server->exec();