ServerFactory.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 <pvince81@owncloud.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 OC\Files\Node\Folder;
  34. use OCA\DAV\AppInfo\PluginManager;
  35. use OCA\DAV\Files\BrowserErrorPagePlugin;
  36. use OCP\Files\Mount\IMountManager;
  37. use OCP\IConfig;
  38. use OCP\IDBConnection;
  39. use OCP\IL10N;
  40. use OCP\ILogger;
  41. use OCP\IPreview;
  42. use OCP\IRequest;
  43. use OCP\ITagManager;
  44. use OCP\IUserSession;
  45. use OCP\SabrePluginEvent;
  46. use Sabre\DAV\Auth\Plugin;
  47. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  48. class ServerFactory {
  49. /** @var IConfig */
  50. private $config;
  51. /** @var ILogger */
  52. private $logger;
  53. /** @var IDBConnection */
  54. private $databaseConnection;
  55. /** @var IUserSession */
  56. private $userSession;
  57. /** @var IMountManager */
  58. private $mountManager;
  59. /** @var ITagManager */
  60. private $tagManager;
  61. /** @var IRequest */
  62. private $request;
  63. /** @var IPreview */
  64. private $previewManager;
  65. /** @var EventDispatcherInterface */
  66. private $eventDispatcher;
  67. /** @var IL10N */
  68. private $l10n;
  69. /**
  70. * @param IConfig $config
  71. * @param ILogger $logger
  72. * @param IDBConnection $databaseConnection
  73. * @param IUserSession $userSession
  74. * @param IMountManager $mountManager
  75. * @param ITagManager $tagManager
  76. * @param IRequest $request
  77. * @param IPreview $previewManager
  78. */
  79. public function __construct(
  80. IConfig $config,
  81. ILogger $logger,
  82. IDBConnection $databaseConnection,
  83. IUserSession $userSession,
  84. IMountManager $mountManager,
  85. ITagManager $tagManager,
  86. IRequest $request,
  87. IPreview $previewManager,
  88. EventDispatcherInterface $eventDispatcher,
  89. IL10N $l10n
  90. ) {
  91. $this->config = $config;
  92. $this->logger = $logger;
  93. $this->databaseConnection = $databaseConnection;
  94. $this->userSession = $userSession;
  95. $this->mountManager = $mountManager;
  96. $this->tagManager = $tagManager;
  97. $this->request = $request;
  98. $this->previewManager = $previewManager;
  99. $this->eventDispatcher = $eventDispatcher;
  100. $this->l10n = $l10n;
  101. }
  102. /**
  103. * @param string $baseUri
  104. * @param string $requestUri
  105. * @param Plugin $authPlugin
  106. * @param callable $viewCallBack callback that should return the view for the dav endpoint
  107. * @return Server
  108. */
  109. public function createServer($baseUri,
  110. $requestUri,
  111. Plugin $authPlugin,
  112. callable $viewCallBack) {
  113. // Fire up server
  114. $objectTree = new \OCA\DAV\Connector\Sabre\ObjectTree();
  115. $server = new \OCA\DAV\Connector\Sabre\Server($objectTree);
  116. // Set URL explicitly due to reverse-proxy situations
  117. $server->httpRequest->setUrl($requestUri);
  118. $server->setBaseUri($baseUri);
  119. // Load plugins
  120. $server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin($this->config, $this->l10n));
  121. $server->addPlugin(new \OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin($this->config));
  122. $server->addPlugin(new \OCA\DAV\Connector\Sabre\AnonymousOptionsPlugin());
  123. $server->addPlugin($authPlugin);
  124. // FIXME: The following line is a workaround for legacy components relying on being able to send a GET to /
  125. $server->addPlugin(new \OCA\DAV\Connector\Sabre\DummyGetResponsePlugin());
  126. $server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $this->logger));
  127. $server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin());
  128. // Some WebDAV clients do require Class 2 WebDAV support (locking), since
  129. // we do not provide locking we emulate it using a fake locking plugin.
  130. if ($this->request->isUserAgent([
  131. '/WebDAVFS/',
  132. '/OneNote/',
  133. '/Microsoft-WebDAV-MiniRedir/',
  134. ])) {
  135. $server->addPlugin(new \OCA\DAV\Connector\Sabre\FakeLockerPlugin());
  136. }
  137. if (BrowserErrorPagePlugin::isBrowserRequest($this->request)) {
  138. $server->addPlugin(new BrowserErrorPagePlugin());
  139. }
  140. // wait with registering these until auth is handled and the filesystem is setup
  141. $server->on('beforeMethod:*', function () use ($server, $objectTree, $viewCallBack) {
  142. // ensure the skeleton is copied
  143. $userFolder = \OC::$server->getUserFolder();
  144. /** @var \OC\Files\View $view */
  145. $view = $viewCallBack($server);
  146. if ($userFolder instanceof Folder && $userFolder->getPath() === $view->getRoot()) {
  147. $rootInfo = $userFolder;
  148. } else {
  149. $rootInfo = $view->getFileInfo('');
  150. }
  151. // Create Nextcloud Dir
  152. if ($rootInfo->getType() === 'dir') {
  153. $root = new \OCA\DAV\Connector\Sabre\Directory($view, $rootInfo, $objectTree);
  154. } else {
  155. $root = new \OCA\DAV\Connector\Sabre\File($view, $rootInfo);
  156. }
  157. $objectTree->init($root, $view, $this->mountManager);
  158. $server->addPlugin(
  159. new \OCA\DAV\Connector\Sabre\FilesPlugin(
  160. $objectTree,
  161. $this->config,
  162. $this->request,
  163. $this->previewManager,
  164. false,
  165. !$this->config->getSystemValue('debug', false)
  166. )
  167. );
  168. $server->addPlugin(new \OCA\DAV\Connector\Sabre\QuotaPlugin($view, true));
  169. if ($this->userSession->isLoggedIn()) {
  170. $server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager));
  171. $server->addPlugin(new \OCA\DAV\Connector\Sabre\SharesPlugin(
  172. $objectTree,
  173. $this->userSession,
  174. $userFolder,
  175. \OC::$server->getShareManager()
  176. ));
  177. $server->addPlugin(new \OCA\DAV\Connector\Sabre\CommentPropertiesPlugin(\OC::$server->getCommentsManager(), $this->userSession));
  178. $server->addPlugin(new \OCA\DAV\Connector\Sabre\FilesReportPlugin(
  179. $objectTree,
  180. $view,
  181. \OC::$server->getSystemTagManager(),
  182. \OC::$server->getSystemTagObjectMapper(),
  183. \OC::$server->getTagManager(),
  184. $this->userSession,
  185. \OC::$server->getGroupManager(),
  186. $userFolder,
  187. \OC::$server->getAppManager()
  188. ));
  189. // custom properties plugin must be the last one
  190. $server->addPlugin(
  191. new \Sabre\DAV\PropertyStorage\Plugin(
  192. new \OCA\DAV\DAV\CustomPropertiesBackend(
  193. $objectTree,
  194. $this->databaseConnection,
  195. $this->userSession->getUser()
  196. )
  197. )
  198. );
  199. }
  200. $server->addPlugin(new \OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin());
  201. // Load dav plugins from apps
  202. $event = new SabrePluginEvent($server);
  203. $this->eventDispatcher->dispatch($event);
  204. $pluginManager = new PluginManager(
  205. \OC::$server,
  206. \OC::$server->getAppManager()
  207. );
  208. foreach ($pluginManager->getAppPlugins() as $appPlugin) {
  209. $server->addPlugin($appPlugin);
  210. }
  211. }, 30); // priority 30: after auth (10) and acl(20), before lock(50) and handling the request
  212. return $server;
  213. }
  214. }