serverfactory.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * @author Robin Appelman <icewind@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OC\Connector\Sabre;
  22. use OCP\Files\Mount\IMountManager;
  23. use OCP\IConfig;
  24. use OCP\IDBConnection;
  25. use OCP\ILogger;
  26. use OCP\ITagManager;
  27. use OCP\IUserSession;
  28. use Sabre\DAV\Auth\Backend\BackendInterface;
  29. class ServerFactory {
  30. public function __construct(
  31. IConfig $config,
  32. ILogger $logger,
  33. IDBConnection $databaseConnection,
  34. IUserSession $userSession,
  35. IMountManager $mountManager,
  36. ITagManager $tagManager
  37. ) {
  38. $this->config = $config;
  39. $this->logger = $logger;
  40. $this->databaseConnection = $databaseConnection;
  41. $this->userSession = $userSession;
  42. $this->mountManager = $mountManager;
  43. $this->tagManager = $tagManager;
  44. }
  45. /**
  46. * @param string $baseUri
  47. * @param string $requestUri
  48. * @param BackendInterface $authBackend
  49. * @param callable $viewCallBack callback that should return the view for the dav endpoint
  50. * @return Server
  51. */
  52. public function createServer($baseUri, $requestUri, BackendInterface $authBackend, callable $viewCallBack) {
  53. // Fire up server
  54. $objectTree = new \OC\Connector\Sabre\ObjectTree();
  55. $server = new \OC\Connector\Sabre\Server($objectTree);
  56. // Set URL explicitly due to reverse-proxy situations
  57. $server->httpRequest->setUrl($requestUri);
  58. $server->setBaseUri($baseUri);
  59. // Load plugins
  60. $defaults = new \OC_Defaults();
  61. $server->addPlugin(new \OC\Connector\Sabre\BlockLegacyClientPlugin($this->config));
  62. $server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend, $defaults->getName()));
  63. // FIXME: The following line is a workaround for legacy components relying on being able to send a GET to /
  64. $server->addPlugin(new \OC\Connector\Sabre\DummyGetResponsePlugin());
  65. $server->addPlugin(new \OC\Connector\Sabre\FilesPlugin($objectTree));
  66. $server->addPlugin(new \OC\Connector\Sabre\MaintenancePlugin($this->config));
  67. $server->addPlugin(new \OC\Connector\Sabre\ExceptionLoggerPlugin('webdav', $this->logger));
  68. // wait with registering these until auth is handled and the filesystem is setup
  69. $server->on('beforeMethod', function () use ($server, $objectTree, $viewCallBack) {
  70. /** @var \OC\Files\View $view */
  71. $view = $viewCallBack();
  72. $rootInfo = $view->getFileInfo('');
  73. // Create ownCloud Dir
  74. if ($rootInfo->getType() === 'dir') {
  75. $root = new \OC\Connector\Sabre\Directory($view, $rootInfo);
  76. } else {
  77. $root = new \OC\Connector\Sabre\File($view, $rootInfo);
  78. }
  79. $objectTree->init($root, $view, $this->mountManager);
  80. $server->addPlugin(new \OC\Connector\Sabre\QuotaPlugin($view));
  81. if($this->userSession->isLoggedIn()) {
  82. $server->addPlugin(new \OC\Connector\Sabre\TagsPlugin($objectTree, $this->tagManager));
  83. // custom properties plugin must be the last one
  84. $server->addPlugin(
  85. new \Sabre\DAV\PropertyStorage\Plugin(
  86. new \OC\Connector\Sabre\CustomPropertiesBackend(
  87. $objectTree,
  88. $this->databaseConnection,
  89. $this->userSession->getUser()
  90. )
  91. )
  92. );
  93. }
  94. $server->addPlugin(new \OC\Connector\Sabre\CopyEtagHeaderPlugin());
  95. }, 30); // priority 30: after auth (10) and acl(20), before lock(50) and handling the request
  96. return $server;
  97. }
  98. }