ServerFactory.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <icewind@owncloud.com>
  8. * @author Roeland Jago Douma <rullzer@owncloud.com>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @copyright Copyright (c) 2016, ownCloud, Inc.
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\Connector\Sabre;
  29. use OCA\DAV\Files\BrowserErrorPagePlugin;
  30. use OCP\Files\Mount\IMountManager;
  31. use OCP\IConfig;
  32. use OCP\IDBConnection;
  33. use OCP\ILogger;
  34. use OCP\IRequest;
  35. use OCP\ITagManager;
  36. use OCP\IUserSession;
  37. use Sabre\DAV\Auth\Backend\BackendInterface;
  38. class ServerFactory {
  39. /** @var IConfig */
  40. private $config;
  41. /** @var ILogger */
  42. private $logger;
  43. /** @var IDBConnection */
  44. private $databaseConnection;
  45. /** @var IUserSession */
  46. private $userSession;
  47. /** @var IMountManager */
  48. private $mountManager;
  49. /** @var ITagManager */
  50. private $tagManager;
  51. /** @var IRequest */
  52. private $request;
  53. /**
  54. * @param IConfig $config
  55. * @param ILogger $logger
  56. * @param IDBConnection $databaseConnection
  57. * @param IUserSession $userSession
  58. * @param IMountManager $mountManager
  59. * @param ITagManager $tagManager
  60. * @param IRequest $request
  61. */
  62. public function __construct(
  63. IConfig $config,
  64. ILogger $logger,
  65. IDBConnection $databaseConnection,
  66. IUserSession $userSession,
  67. IMountManager $mountManager,
  68. ITagManager $tagManager,
  69. IRequest $request
  70. ) {
  71. $this->config = $config;
  72. $this->logger = $logger;
  73. $this->databaseConnection = $databaseConnection;
  74. $this->userSession = $userSession;
  75. $this->mountManager = $mountManager;
  76. $this->tagManager = $tagManager;
  77. $this->request = $request;
  78. }
  79. /**
  80. * @param string $baseUri
  81. * @param string $requestUri
  82. * @param BackendInterface $authBackend
  83. * @param callable $viewCallBack callback that should return the view for the dav endpoint
  84. * @return Server
  85. */
  86. public function createServer($baseUri,
  87. $requestUri,
  88. BackendInterface $authBackend,
  89. callable $viewCallBack) {
  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. $defaults = new \OC_Defaults();
  98. $server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin($this->config));
  99. $server->addPlugin(new \OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin($this->config));
  100. $server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend, $defaults->getName()));
  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. // Some WebDAV clients do require Class 2 WebDAV support (locking), since
  106. // we do not provide locking we emulate it using a fake locking plugin.
  107. if($this->request->isUserAgent([
  108. '/WebDAVFS/',
  109. '/Microsoft Office OneNote 2013/',
  110. '/Microsoft-WebDAV-MiniRedir/',
  111. ])) {
  112. $server->addPlugin(new \OCA\DAV\Connector\Sabre\FakeLockerPlugin());
  113. }
  114. if (BrowserErrorPagePlugin::isBrowserRequest($this->request)) {
  115. $server->addPlugin(new BrowserErrorPagePlugin());
  116. }
  117. // wait with registering these until auth is handled and the filesystem is setup
  118. $server->on('beforeMethod', function () use ($server, $objectTree, $viewCallBack) {
  119. // ensure the skeleton is copied
  120. $userFolder = \OC::$server->getUserFolder();
  121. /** @var \OC\Files\View $view */
  122. $view = $viewCallBack($server);
  123. $rootInfo = $view->getFileInfo('');
  124. // Create ownCloud Dir
  125. if ($rootInfo->getType() === 'dir') {
  126. $root = new \OCA\DAV\Connector\Sabre\Directory($view, $rootInfo, $objectTree);
  127. } else {
  128. $root = new \OCA\DAV\Connector\Sabre\File($view, $rootInfo);
  129. }
  130. $objectTree->init($root, $view, $this->mountManager);
  131. $server->addPlugin(
  132. new \OCA\DAV\Connector\Sabre\FilesPlugin(
  133. $objectTree,
  134. $view,
  135. $this->config,
  136. false,
  137. !$this->config->getSystemValue('debug', false)
  138. )
  139. );
  140. $server->addPlugin(new \OCA\DAV\Connector\Sabre\QuotaPlugin($view));
  141. if($this->userSession->isLoggedIn()) {
  142. $server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager));
  143. $server->addPlugin(new \OCA\DAV\Connector\Sabre\SharesPlugin(
  144. $objectTree,
  145. $this->userSession,
  146. $userFolder,
  147. \OC::$server->getShareManager()
  148. ));
  149. $server->addPlugin(new \OCA\DAV\Connector\Sabre\CommentPropertiesPlugin(\OC::$server->getCommentsManager(), $this->userSession));
  150. $server->addPlugin(new \OCA\DAV\Connector\Sabre\FilesReportPlugin(
  151. $objectTree,
  152. $view,
  153. \OC::$server->getSystemTagManager(),
  154. \OC::$server->getSystemTagObjectMapper(),
  155. $this->userSession,
  156. \OC::$server->getGroupManager(),
  157. $userFolder
  158. ));
  159. // custom properties plugin must be the last one
  160. $server->addPlugin(
  161. new \Sabre\DAV\PropertyStorage\Plugin(
  162. new \OCA\DAV\Connector\Sabre\CustomPropertiesBackend(
  163. $objectTree,
  164. $this->databaseConnection,
  165. $this->userSession->getUser()
  166. )
  167. )
  168. );
  169. }
  170. $server->addPlugin(new \OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin());
  171. }, 30); // priority 30: after auth (10) and acl(20), before lock(50) and handling the request
  172. return $server;
  173. }
  174. }