ServerFactory.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. * @author Vincent Petry <vincent@nextcloud.com>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OCA\DAV\Connector\Sabre;
  33. use OCP\Files\Folder;
  34. use OCA\DAV\AppInfo\PluginManager;
  35. use OCA\DAV\DAV\ViewOnlyPlugin;
  36. use OCA\DAV\Files\BrowserErrorPagePlugin;
  37. use OCP\Files\Mount\IMountManager;
  38. use OCP\IConfig;
  39. use OCP\IDBConnection;
  40. use OCP\IL10N;
  41. use OCP\IPreview;
  42. use OCP\IRequest;
  43. use OCP\ITagManager;
  44. use OCP\IUserSession;
  45. use OCP\SabrePluginEvent;
  46. use Psr\Log\LoggerInterface;
  47. use Sabre\DAV\Auth\Plugin;
  48. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  49. class ServerFactory {
  50. private IConfig $config;
  51. private LoggerInterface $logger;
  52. private IDBConnection $databaseConnection;
  53. private IUserSession $userSession;
  54. private IMountManager $mountManager;
  55. private ITagManager $tagManager;
  56. private IRequest $request;
  57. private IPreview $previewManager;
  58. private EventDispatcherInterface $eventDispatcher;
  59. private IL10N $l10n;
  60. public function __construct(
  61. IConfig $config,
  62. LoggerInterface $logger,
  63. IDBConnection $databaseConnection,
  64. IUserSession $userSession,
  65. IMountManager $mountManager,
  66. ITagManager $tagManager,
  67. IRequest $request,
  68. IPreview $previewManager,
  69. EventDispatcherInterface $eventDispatcher,
  70. IL10N $l10n
  71. ) {
  72. $this->config = $config;
  73. $this->logger = $logger;
  74. $this->databaseConnection = $databaseConnection;
  75. $this->userSession = $userSession;
  76. $this->mountManager = $mountManager;
  77. $this->tagManager = $tagManager;
  78. $this->request = $request;
  79. $this->previewManager = $previewManager;
  80. $this->eventDispatcher = $eventDispatcher;
  81. $this->l10n = $l10n;
  82. }
  83. /**
  84. * @param callable $viewCallBack callback that should return the view for the dav endpoint
  85. */
  86. public function createServer(string $baseUri,
  87. string $requestUri,
  88. Plugin $authPlugin,
  89. callable $viewCallBack): Server {
  90. // Fire up server
  91. $objectTree = new \OCA\DAV\Connector\Sabre\ObjectTree();
  92. $server = new \OCA\DAV\Connector\Sabre\Server($objectTree);
  93. // Set URL explicitly due to reverse-proxy situations
  94. $server->httpRequest->setUrl($requestUri);
  95. $server->setBaseUri($baseUri);
  96. // Load plugins
  97. $server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin($this->config, $this->l10n));
  98. $server->addPlugin(new \OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin($this->config));
  99. $server->addPlugin(new \OCA\DAV\Connector\Sabre\AnonymousOptionsPlugin());
  100. $server->addPlugin($authPlugin);
  101. // FIXME: The following line is a workaround for legacy components relying on being able to send a GET to /
  102. $server->addPlugin(new \OCA\DAV\Connector\Sabre\DummyGetResponsePlugin());
  103. $server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $this->logger));
  104. $server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin());
  105. $server->addPlugin(new RequestIdHeaderPlugin(\OC::$server->get(IRequest::class)));
  106. // Some WebDAV clients do require Class 2 WebDAV support (locking), since
  107. // we do not provide locking we emulate it using a fake locking plugin.
  108. if ($this->request->isUserAgent([
  109. '/WebDAVFS/',
  110. '/OneNote/',
  111. '/Microsoft-WebDAV-MiniRedir/',
  112. ])) {
  113. $server->addPlugin(new \OCA\DAV\Connector\Sabre\FakeLockerPlugin());
  114. }
  115. if (BrowserErrorPagePlugin::isBrowserRequest($this->request)) {
  116. $server->addPlugin(new BrowserErrorPagePlugin());
  117. }
  118. // wait with registering these until auth is handled and the filesystem is setup
  119. $server->on('beforeMethod:*', function () use ($server, $objectTree, $viewCallBack) {
  120. // ensure the skeleton is copied
  121. $userFolder = \OC::$server->getUserFolder();
  122. /** @var \OC\Files\View $view */
  123. $view = $viewCallBack($server);
  124. if ($userFolder instanceof Folder && $userFolder->getPath() === $view->getRoot()) {
  125. $rootInfo = $userFolder;
  126. } else {
  127. $rootInfo = $view->getFileInfo('');
  128. }
  129. // Create Nextcloud Dir
  130. if ($rootInfo->getType() === 'dir') {
  131. $root = new \OCA\DAV\Connector\Sabre\Directory($view, $rootInfo, $objectTree);
  132. } else {
  133. $root = new \OCA\DAV\Connector\Sabre\File($view, $rootInfo);
  134. }
  135. $objectTree->init($root, $view, $this->mountManager);
  136. $server->addPlugin(
  137. new \OCA\DAV\Connector\Sabre\FilesPlugin(
  138. $objectTree,
  139. $this->config,
  140. $this->request,
  141. $this->previewManager,
  142. $this->userSession,
  143. false,
  144. !$this->config->getSystemValue('debug', false)
  145. )
  146. );
  147. $server->addPlugin(new \OCA\DAV\Connector\Sabre\QuotaPlugin($view, true));
  148. $server->addPlugin(new \OCA\DAV\Connector\Sabre\ChecksumUpdatePlugin());
  149. // Allow view-only plugin for webdav requests
  150. $server->addPlugin(new ViewOnlyPlugin(
  151. $this->logger
  152. ));
  153. if ($this->userSession->isLoggedIn()) {
  154. $server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager));
  155. $server->addPlugin(new \OCA\DAV\Connector\Sabre\SharesPlugin(
  156. $objectTree,
  157. $this->userSession,
  158. $userFolder,
  159. \OC::$server->getShareManager()
  160. ));
  161. $server->addPlugin(new \OCA\DAV\Connector\Sabre\CommentPropertiesPlugin(\OC::$server->getCommentsManager(), $this->userSession));
  162. $server->addPlugin(new \OCA\DAV\Connector\Sabre\FilesReportPlugin(
  163. $objectTree,
  164. $view,
  165. \OC::$server->getSystemTagManager(),
  166. \OC::$server->getSystemTagObjectMapper(),
  167. \OC::$server->getTagManager(),
  168. $this->userSession,
  169. \OC::$server->getGroupManager(),
  170. $userFolder,
  171. \OC::$server->getAppManager()
  172. ));
  173. // custom properties plugin must be the last one
  174. $server->addPlugin(
  175. new \Sabre\DAV\PropertyStorage\Plugin(
  176. new \OCA\DAV\DAV\CustomPropertiesBackend(
  177. $objectTree,
  178. $this->databaseConnection,
  179. $this->userSession->getUser()
  180. )
  181. )
  182. );
  183. }
  184. $server->addPlugin(new \OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin());
  185. // Load dav plugins from apps
  186. $event = new SabrePluginEvent($server);
  187. $this->eventDispatcher->dispatch($event);
  188. $pluginManager = new PluginManager(
  189. \OC::$server,
  190. \OC::$server->getAppManager()
  191. );
  192. foreach ($pluginManager->getAppPlugins() as $appPlugin) {
  193. $server->addPlugin($appPlugin);
  194. }
  195. }, 30); // priority 30: after auth (10) and acl(20), before lock(50) and handling the request
  196. return $server;
  197. }
  198. }