123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- <?php
- namespace OCA\DAV;
- use OCA\DAV\AppInfo\PluginManager;
- use OCA\DAV\CalDAV\BirthdayService;
- use OCA\DAV\CardDAV\HasPhotoPlugin;
- use OCA\DAV\CardDAV\ImageExportPlugin;
- use OCA\DAV\CardDAV\MultiGetExportPlugin;
- use OCA\DAV\CardDAV\PhotoCache;
- use OCA\DAV\Comments\CommentsPlugin;
- use OCA\DAV\Connector\Sabre\AnonymousOptionsPlugin;
- use OCA\DAV\Connector\Sabre\Auth;
- use OCA\DAV\Connector\Sabre\BearerAuth;
- use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
- use OCA\DAV\Connector\Sabre\CachingTree;
- use OCA\DAV\Connector\Sabre\CommentPropertiesPlugin;
- use OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin;
- use OCA\DAV\Connector\Sabre\DavAclPlugin;
- use OCA\DAV\Connector\Sabre\DummyGetResponsePlugin;
- use OCA\DAV\Connector\Sabre\FakeLockerPlugin;
- use OCA\DAV\Connector\Sabre\FilesPlugin;
- use OCA\DAV\Connector\Sabre\FilesReportPlugin;
- use OCA\DAV\Connector\Sabre\PropfindCompressionPlugin;
- use OCA\DAV\Connector\Sabre\QuotaPlugin;
- use OCA\DAV\Connector\Sabre\SharesPlugin;
- use OCA\DAV\Connector\Sabre\TagsPlugin;
- use OCA\DAV\DAV\CustomPropertiesBackend;
- use OCA\DAV\DAV\PublicAuth;
- use OCA\DAV\Events\SabrePluginAuthInitEvent;
- use OCA\DAV\Files\BrowserErrorPagePlugin;
- use OCA\DAV\Files\LazySearchBackend;
- use OCA\DAV\Provisioning\Apple\AppleProvisioningPlugin;
- use OCA\DAV\SystemTag\SystemTagPlugin;
- use OCA\DAV\Upload\ChunkingPlugin;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\IRequest;
- use OCP\SabrePluginEvent;
- use Sabre\CardDAV\VCFExportPlugin;
- use Sabre\DAV\Auth\Plugin;
- use Sabre\DAV\UUIDUtil;
- use SearchDAV\DAV\SearchPlugin;
- class Server {
-
- private $request;
-
- private $baseUri;
-
- public $server;
- public function __construct(IRequest $request, $baseUri) {
- $this->request = $request;
- $this->baseUri = $baseUri;
- $logger = \OC::$server->getLogger();
- $dispatcher = \OC::$server->getEventDispatcher();
-
- $newDispatcher = \OC::$server->query(IEventDispatcher::class);
- $root = new RootCollection();
- $this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root));
-
- $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig(), \OC::$server->getL10N('dav')));
-
- $authBackend = new Auth(
- \OC::$server->getSession(),
- \OC::$server->getUserSession(),
- \OC::$server->getRequest(),
- \OC::$server->getTwoFactorAuthManager(),
- \OC::$server->getBruteForceThrottler()
- );
-
- $this->server->httpRequest->setUrl($this->request->getRequestUri());
- $this->server->setBaseUri($this->baseUri);
- $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig()));
- $this->server->addPlugin(new AnonymousOptionsPlugin());
- $authPlugin = new Plugin();
- $authPlugin->addBackend(new PublicAuth());
- $this->server->addPlugin($authPlugin);
-
- $event = new SabrePluginEvent($this->server);
- $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event);
- $newAuthEvent = new SabrePluginAuthInitEvent($this->server);
- $newDispatcher->dispatchTyped($newAuthEvent);
- $bearerAuthBackend = new BearerAuth(
- \OC::$server->getUserSession(),
- \OC::$server->getSession(),
- \OC::$server->getRequest()
- );
- $authPlugin->addBackend($bearerAuthBackend);
-
- $authPlugin->addBackend($authBackend);
-
- if (\OC::$server->getConfig()->getSystemValue('debug', false)) {
- $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin());
- } else {
- $this->server->addPlugin(new DummyGetResponsePlugin());
- }
- $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger));
- $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin());
- $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());
-
- $acl = new DavAclPlugin();
- $acl->principalCollectionSet = [
- 'principals/users',
- 'principals/groups',
- 'principals/calendar-resources',
- 'principals/calendar-rooms',
- ];
- $acl->defaultUsernamePath = 'principals/users';
- $this->server->addPlugin($acl);
-
- if ($this->requestIsForSubtree(['calendars', 'public-calendars', 'system-calendars', 'principals'])) {
- $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin());
- $this->server->addPlugin(new \OCA\DAV\CalDAV\ICSExportPlugin\ICSExportPlugin(\OC::$server->getConfig(), \OC::$server->getLogger()));
- $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin(\OC::$server->getConfig()));
- if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') {
- $this->server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class));
- }
- $this->server->addPlugin(new CalDAV\WebcalCaching\Plugin($request));
- $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
- $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
- $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
- $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin(
- \OC::$server->getConfig(),
- \OC::$server->getURLGenerator()
- ));
- }
-
- if ($this->requestIsForSubtree(['addressbooks', 'principals'])) {
- $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
- $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
- $this->server->addPlugin(new VCFExportPlugin());
- $this->server->addPlugin(new MultiGetExportPlugin());
- $this->server->addPlugin(new HasPhotoPlugin());
- $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(
- \OC::$server->getAppDataDir('dav-photocache'),
- \OC::$server->getLogger())
- ));
- }
-
- $this->server->addPlugin(new SystemTagPlugin(
- \OC::$server->getSystemTagManager(),
- \OC::$server->getGroupManager(),
- \OC::$server->getUserSession()
- ));
-
- $this->server->addPlugin(new CommentsPlugin(
- \OC::$server->getCommentsManager(),
- \OC::$server->getUserSession()
- ));
- $this->server->addPlugin(new CopyEtagHeaderPlugin());
- $this->server->addPlugin(new ChunkingPlugin());
-
- $dispatcher->dispatch('OCA\DAV\Connector\Sabre::addPlugin', $event);
-
-
- if ($request->isUserAgent([
- '/WebDAVFS/',
- '/OneNote/',
- '/^Microsoft-WebDAV/',
- ])) {
- $this->server->addPlugin(new FakeLockerPlugin());
- }
- if (BrowserErrorPagePlugin::isBrowserRequest($request)) {
- $this->server->addPlugin(new BrowserErrorPagePlugin());
- }
- $lazySearchBackend = new LazySearchBackend();
- $this->server->addPlugin(new SearchPlugin($lazySearchBackend));
-
- $this->server->on('beforeMethod:*', function () use ($root, $lazySearchBackend) {
-
- $userSession = \OC::$server->getUserSession();
- $user = $userSession->getUser();
- if ($user !== null) {
- $view = \OC\Files\Filesystem::getView();
- $this->server->addPlugin(
- new FilesPlugin(
- $this->server->tree,
- \OC::$server->getConfig(),
- $this->request,
- \OC::$server->getPreviewManager(),
- false,
- !\OC::$server->getConfig()->getSystemValue('debug', false)
- )
- );
- $this->server->addPlugin(
- new \Sabre\DAV\PropertyStorage\Plugin(
- new CustomPropertiesBackend(
- $this->server->tree,
- \OC::$server->getDatabaseConnection(),
- \OC::$server->getUserSession()->getUser()
- )
- )
- );
- if ($view !== null) {
- $this->server->addPlugin(
- new QuotaPlugin($view, false));
- }
- $this->server->addPlugin(
- new TagsPlugin(
- $this->server->tree, \OC::$server->getTagManager()
- )
- );
-
- $userFolder = \OC::$server->getUserFolder();
- $this->server->addPlugin(new SharesPlugin(
- $this->server->tree,
- $userSession,
- $userFolder,
- \OC::$server->getShareManager()
- ));
- $this->server->addPlugin(new CommentPropertiesPlugin(
- \OC::$server->getCommentsManager(),
- $userSession
- ));
- $this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin());
- if ($view !== null) {
- $this->server->addPlugin(new FilesReportPlugin(
- $this->server->tree,
- $view,
- \OC::$server->getSystemTagManager(),
- \OC::$server->getSystemTagObjectMapper(),
- \OC::$server->getTagManager(),
- $userSession,
- \OC::$server->getGroupManager(),
- $userFolder,
- \OC::$server->getAppManager()
- ));
- $lazySearchBackend->setBackend(new \OCA\DAV\Files\FileSearchBackend(
- $this->server->tree,
- $user,
- \OC::$server->getRootFolder(),
- \OC::$server->getShareManager(),
- $view
- ));
- }
- $this->server->addPlugin(new \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin(
- \OC::$server->getConfig(),
- \OC::$server->query(BirthdayService::class)
- ));
- $this->server->addPlugin(new AppleProvisioningPlugin(
- \OC::$server->getUserSession(),
- \OC::$server->getURLGenerator(),
- \OC::$server->getThemingDefaults(),
- \OC::$server->getRequest(),
- \OC::$server->getL10N('dav'),
- function () {
- return UUIDUtil::getUUID();
- }
- ));
- }
-
- $pluginManager = new PluginManager(
- \OC::$server,
- \OC::$server->getAppManager()
- );
- foreach ($pluginManager->getAppPlugins() as $appPlugin) {
- $this->server->addPlugin($appPlugin);
- }
- foreach ($pluginManager->getAppCollections() as $appCollection) {
- $root->addChild($appCollection);
- }
- });
- $this->server->addPlugin(
- new PropfindCompressionPlugin()
- );
- }
- public function exec() {
- $this->server->exec();
- }
- private function requestIsForSubtree(array $subTrees): bool {
- foreach ($subTrees as $subTree) {
- $subTree = trim($subTree, ' /');
- if (strpos($this->server->getRequestUri(), $subTree.'/') === 0) {
- return true;
- }
- }
- return false;
- }
- }
|