serverfactory.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. * @author Robin Appelman <icewind@owncloud.com>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. *
  8. * @copyright Copyright (c) 2016, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\DAV\Connector\Sabre;
  25. use OCP\Files\Mount\IMountManager;
  26. use OCP\IConfig;
  27. use OCP\IDBConnection;
  28. use OCP\ILogger;
  29. use OCP\IRequest;
  30. use OCP\ITagManager;
  31. use OCP\IUserSession;
  32. use Sabre\DAV\Auth\Backend\BackendInterface;
  33. class ServerFactory {
  34. /** @var IConfig */
  35. private $config;
  36. /** @var ILogger */
  37. private $logger;
  38. /** @var IDBConnection */
  39. private $databaseConnection;
  40. /** @var IUserSession */
  41. private $userSession;
  42. /** @var IMountManager */
  43. private $mountManager;
  44. /** @var ITagManager */
  45. private $tagManager;
  46. /** @var IRequest */
  47. private $request;
  48. /**
  49. * @param IConfig $config
  50. * @param ILogger $logger
  51. * @param IDBConnection $databaseConnection
  52. * @param IUserSession $userSession
  53. * @param IMountManager $mountManager
  54. * @param ITagManager $tagManager
  55. * @param IRequest $request
  56. */
  57. public function __construct(
  58. IConfig $config,
  59. ILogger $logger,
  60. IDBConnection $databaseConnection,
  61. IUserSession $userSession,
  62. IMountManager $mountManager,
  63. ITagManager $tagManager,
  64. IRequest $request
  65. ) {
  66. $this->config = $config;
  67. $this->logger = $logger;
  68. $this->databaseConnection = $databaseConnection;
  69. $this->userSession = $userSession;
  70. $this->mountManager = $mountManager;
  71. $this->tagManager = $tagManager;
  72. $this->request = $request;
  73. }
  74. /**
  75. * @param string $baseUri
  76. * @param string $requestUri
  77. * @param BackendInterface $authBackend
  78. * @param callable $viewCallBack callback that should return the view for the dav endpoint
  79. * @return Server
  80. */
  81. public function createServer($baseUri,
  82. $requestUri,
  83. BackendInterface $authBackend,
  84. callable $viewCallBack) {
  85. // Fire up server
  86. $objectTree = new \OCA\DAV\Connector\Sabre\ObjectTree();
  87. $server = new \OCA\DAV\Connector\Sabre\Server($objectTree);
  88. // Set URL explicitly due to reverse-proxy situations
  89. $server->httpRequest->setUrl($requestUri);
  90. $server->setBaseUri($baseUri);
  91. // Load plugins
  92. $defaults = new \OC_Defaults();
  93. $server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin($this->config));
  94. $server->addPlugin(new \OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin($this->config));
  95. $server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend, $defaults->getName()));
  96. // FIXME: The following line is a workaround for legacy components relying on being able to send a GET to /
  97. $server->addPlugin(new \OCA\DAV\Connector\Sabre\DummyGetResponsePlugin());
  98. $server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $this->logger));
  99. $server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin());
  100. // Some WebDAV clients do require Class 2 WebDAV support (locking), since
  101. // we do not provide locking we emulate it using a fake locking plugin.
  102. if($this->request->isUserAgent([
  103. '/WebDAVFS/',
  104. '/Microsoft Office OneNote 2013/',
  105. ])) {
  106. $server->addPlugin(new \OCA\DAV\Connector\Sabre\FakeLockerPlugin());
  107. }
  108. // wait with registering these until auth is handled and the filesystem is setup
  109. $server->on('beforeMethod', function () use ($server, $objectTree, $viewCallBack) {
  110. // ensure the skeleton is copied
  111. $userFolder = \OC::$server->getUserFolder();
  112. /** @var \OC\Files\View $view */
  113. $view = $viewCallBack($server);
  114. $rootInfo = $view->getFileInfo('');
  115. // Create ownCloud Dir
  116. if ($rootInfo->getType() === 'dir') {
  117. $root = new \OCA\DAV\Connector\Sabre\Directory($view, $rootInfo, $objectTree);
  118. } else {
  119. $root = new \OCA\DAV\Connector\Sabre\File($view, $rootInfo);
  120. }
  121. $objectTree->init($root, $view, $this->mountManager);
  122. $server->addPlugin(new \OCA\DAV\Connector\Sabre\FilesPlugin($objectTree, $view));
  123. $server->addPlugin(new \OCA\DAV\Connector\Sabre\QuotaPlugin($view));
  124. if($this->userSession->isLoggedIn()) {
  125. $server->addPlugin(new \OCA\DAV\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager));
  126. $server->addPlugin(new \OCA\DAV\Connector\Sabre\CommentPropertiesPlugin(\OC::$server->getCommentsManager(), $this->userSession));
  127. $server->addPlugin(new \OCA\DAV\Connector\Sabre\FilesReportPlugin(
  128. $objectTree,
  129. $view,
  130. \OC::$server->getSystemTagManager(),
  131. \OC::$server->getSystemTagObjectMapper(),
  132. $this->userSession,
  133. \OC::$server->getGroupManager(),
  134. $userFolder
  135. ));
  136. // custom properties plugin must be the last one
  137. $server->addPlugin(
  138. new \Sabre\DAV\PropertyStorage\Plugin(
  139. new \OCA\DAV\Connector\Sabre\CustomPropertiesBackend(
  140. $objectTree,
  141. $this->databaseConnection,
  142. $this->userSession->getUser()
  143. )
  144. )
  145. );
  146. }
  147. $server->addPlugin(new \OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin());
  148. }, 30); // priority 30: after auth (10) and acl(20), before lock(50) and handling the request
  149. return $server;
  150. }
  151. }