server.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. * @author Thomas Müller <thomas.mueller@tmit.eu>
  5. * @author Vincent Petry <pvince81@owncloud.com>
  6. *
  7. * @copyright Copyright (c) 2016, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\DAV;
  24. use OCA\DAV\CalDAV\Schedule\IMipPlugin;
  25. use OCA\DAV\Connector\FedAuth;
  26. use OCA\DAV\Connector\Sabre\Auth;
  27. use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
  28. use OCA\DAV\Files\CustomPropertiesBackend;
  29. use OCP\IRequest;
  30. use OCP\SabrePluginEvent;
  31. use Sabre\DAV\Auth\Plugin;
  32. use Sabre\DAV\IFile;
  33. use Sabre\HTTP\RequestInterface;
  34. use Sabre\HTTP\ResponseInterface;
  35. use Sabre\HTTP\Util;
  36. class Server {
  37. /** @var IRequest */
  38. private $request;
  39. public function __construct(IRequest $request, $baseUri) {
  40. $this->request = $request;
  41. $this->baseUri = $baseUri;
  42. $logger = \OC::$server->getLogger();
  43. $mailer = \OC::$server->getMailer();
  44. $dispatcher = \OC::$server->getEventDispatcher();
  45. $root = new RootCollection();
  46. $this->server = new \OCA\DAV\Connector\Sabre\Server($root);
  47. // Backends
  48. $authBackend = new Auth(
  49. \OC::$server->getSession(),
  50. \OC::$server->getUserSession()
  51. );
  52. // Set URL explicitly due to reverse-proxy situations
  53. $this->server->httpRequest->setUrl($this->request->getRequestUri());
  54. $this->server->setBaseUri($this->baseUri);
  55. $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig()));
  56. $authPlugin = new Plugin($authBackend, 'ownCloud');
  57. $this->server->addPlugin($authPlugin);
  58. // allow setup of additional auth backends
  59. $event = new SabrePluginEvent($this->server);
  60. $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event);
  61. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\DummyGetResponsePlugin());
  62. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger));
  63. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin());
  64. $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());
  65. // acl
  66. $acl = new \Sabre\DAVACL\Plugin();
  67. $acl->defaultUsernamePath = 'principals/users';
  68. $this->server->addPlugin($acl);
  69. // calendar plugins
  70. $this->server->addPlugin(new \Sabre\CalDAV\Plugin());
  71. $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin());
  72. $this->server->addPlugin(new \Sabre\CalDAV\Schedule\Plugin());
  73. $this->server->addPlugin(new IMipPlugin($mailer, $logger));
  74. $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
  75. $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
  76. $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest()));
  77. // addressbook plugins
  78. $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
  79. // system tags plugins
  80. $this->server->addPlugin(new \OCA\DAV\SystemTag\SystemTagPlugin(\OC::$server->getSystemTagManager()));
  81. // comments plugin
  82. $this->server->addPlugin(new \OCA\DAV\Comments\CommentsPlugin(
  83. \OC::$server->getCommentsManager(),
  84. \OC::$server->getUserSession()
  85. ));
  86. // Some WebDAV clients do require Class 2 WebDAV support (locking), since
  87. // we do not provide locking we emulate it using a fake locking plugin.
  88. if($request->isUserAgent([
  89. '/WebDAVFS/',
  90. '/Microsoft Office OneNote 2013/',
  91. ])) {
  92. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\FakeLockerPlugin());
  93. }
  94. // Serve all files with an Content-Disposition of type "attachment"
  95. $this->server->on('beforeMethod', function (RequestInterface $requestInterface, ResponseInterface $responseInterface) {
  96. if ($requestInterface->getMethod() === 'GET') {
  97. $path = $requestInterface->getPath();
  98. if ($this->server->tree->nodeExists($path)) {
  99. $node = $this->server->tree->getNodeForPath($path);
  100. if (($node instanceof IFile)) {
  101. $responseInterface->addHeader('Content-Disposition', 'attachment');
  102. }
  103. }
  104. }
  105. });
  106. // wait with registering these until auth is handled and the filesystem is setup
  107. $this->server->on('beforeMethod', function () {
  108. // custom properties plugin must be the last one
  109. $user = \OC::$server->getUserSession()->getUser();
  110. if (!is_null($user)) {
  111. $this->server->addPlugin(
  112. new \Sabre\DAV\PropertyStorage\Plugin(
  113. new CustomPropertiesBackend(
  114. $this->server->tree,
  115. \OC::$server->getDatabaseConnection(),
  116. \OC::$server->getUserSession()->getUser()
  117. )
  118. )
  119. );
  120. }
  121. });
  122. }
  123. public function exec() {
  124. $this->server->exec();
  125. }
  126. }