Server.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. * @author Lukas Reschke <lukas@statuscode.ch>
  5. * @author Roeland Jago Douma <rullzer@owncloud.com>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. * @author Vincent Petry <pvince81@owncloud.com>
  8. *
  9. * @copyright Copyright (c) 2016, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV;
  26. use OCA\DAV\CalDAV\Schedule\IMipPlugin;
  27. use OCA\DAV\CardDAV\ImageExportPlugin;
  28. use OCA\DAV\Connector\Sabre\Auth;
  29. use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
  30. use OCA\DAV\Connector\Sabre\DavAclPlugin;
  31. use OCA\DAV\Connector\Sabre\DummyGetResponsePlugin;
  32. use OCA\DAV\Connector\Sabre\FilesPlugin;
  33. use OCA\DAV\Files\BrowserErrorPagePlugin;
  34. use OCA\DAV\Files\CustomPropertiesBackend;
  35. use OCP\IRequest;
  36. use OCP\SabrePluginEvent;
  37. use Sabre\CardDAV\VCFExportPlugin;
  38. use Sabre\DAV\Auth\Plugin;
  39. class Server {
  40. /** @var IRequest */
  41. private $request;
  42. public function __construct(IRequest $request, $baseUri) {
  43. $this->request = $request;
  44. $this->baseUri = $baseUri;
  45. $logger = \OC::$server->getLogger();
  46. $mailer = \OC::$server->getMailer();
  47. $dispatcher = \OC::$server->getEventDispatcher();
  48. $root = new RootCollection();
  49. $this->server = new \OCA\DAV\Connector\Sabre\Server($root);
  50. // Backends
  51. $authBackend = new Auth(
  52. \OC::$server->getSession(),
  53. \OC::$server->getUserSession(),
  54. \OC::$server->getRequest(),
  55. \OC::$server->getTwoFactorAuthManager()
  56. );
  57. // Set URL explicitly due to reverse-proxy situations
  58. $this->server->httpRequest->setUrl($this->request->getRequestUri());
  59. $this->server->setBaseUri($this->baseUri);
  60. $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig()));
  61. $authPlugin = new Plugin($authBackend, 'ownCloud');
  62. $this->server->addPlugin($authPlugin);
  63. // allow setup of additional auth backends
  64. $event = new SabrePluginEvent($this->server);
  65. $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event);
  66. // debugging
  67. if(\OC::$server->getConfig()->getSystemValue('debug', false)) {
  68. $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin());
  69. } else {
  70. $this->server->addPlugin(new DummyGetResponsePlugin());
  71. }
  72. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger));
  73. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin());
  74. $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());
  75. // acl
  76. $acl = new DavAclPlugin();
  77. $acl->principalCollectionSet = [
  78. 'principals/users', 'principals/groups'
  79. ];
  80. $acl->defaultUsernamePath = 'principals/users';
  81. $this->server->addPlugin($acl);
  82. // calendar plugins
  83. $this->server->addPlugin(new \Sabre\CalDAV\Plugin());
  84. $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin());
  85. $this->server->addPlugin(new \Sabre\CalDAV\Schedule\Plugin());
  86. $this->server->addPlugin(new IMipPlugin($mailer, $logger));
  87. $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
  88. $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
  89. $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest()));
  90. // addressbook plugins
  91. $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
  92. $this->server->addPlugin(new VCFExportPlugin());
  93. $this->server->addPlugin(new ImageExportPlugin(\OC::$server->getLogger()));
  94. // system tags plugins
  95. $this->server->addPlugin(new \OCA\DAV\SystemTag\SystemTagPlugin(
  96. \OC::$server->getSystemTagManager(),
  97. \OC::$server->getGroupManager(),
  98. \OC::$server->getUserSession()
  99. ));
  100. // comments plugin
  101. $this->server->addPlugin(new \OCA\DAV\Comments\CommentsPlugin(
  102. \OC::$server->getCommentsManager(),
  103. \OC::$server->getUserSession()
  104. ));
  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($request->isUserAgent([
  108. '/WebDAVFS/',
  109. '/Microsoft Office OneNote 2013/',
  110. ])) {
  111. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\FakeLockerPlugin());
  112. }
  113. if (BrowserErrorPagePlugin::isBrowserRequest($request)) {
  114. $this->server->addPlugin(new BrowserErrorPagePlugin());
  115. }
  116. // wait with registering these until auth is handled and the filesystem is setup
  117. $this->server->on('beforeMethod', function () {
  118. // custom properties plugin must be the last one
  119. $user = \OC::$server->getUserSession()->getUser();
  120. if (!is_null($user)) {
  121. $view = \OC\Files\Filesystem::getView();
  122. $this->server->addPlugin(
  123. new FilesPlugin(
  124. $this->server->tree,
  125. $view,
  126. \OC::$server->getConfig(),
  127. $this->request,
  128. false,
  129. !\OC::$server->getConfig()->getSystemValue('debug', false)
  130. )
  131. );
  132. $this->server->addPlugin(
  133. new \Sabre\DAV\PropertyStorage\Plugin(
  134. new CustomPropertiesBackend(
  135. $this->server->tree,
  136. \OC::$server->getDatabaseConnection(),
  137. \OC::$server->getUserSession()->getUser()
  138. )
  139. )
  140. );
  141. }
  142. });
  143. }
  144. public function exec() {
  145. $this->server->exec();
  146. }
  147. }