You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
7.4 KiB

10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@owncloud.com>
  7. * @author Georg Ehrke <georg@owncloud.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Vincent Petry <pvince81@owncloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\DAV;
  30. use OCA\DAV\CalDAV\Schedule\IMipPlugin;
  31. use OCA\DAV\CardDAV\ImageExportPlugin;
  32. use OCA\DAV\Comments\CommentsPlugin;
  33. use OCA\DAV\Connector\Sabre\Auth;
  34. use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
  35. use OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin;
  36. use OCA\DAV\Connector\Sabre\DavAclPlugin;
  37. use OCA\DAV\Connector\Sabre\DummyGetResponsePlugin;
  38. use OCA\DAV\Connector\Sabre\FakeLockerPlugin;
  39. use OCA\DAV\Connector\Sabre\FilesPlugin;
  40. use OCA\DAV\DAV\PublicAuth;
  41. use OCA\DAV\Connector\Sabre\QuotaPlugin;
  42. use OCA\DAV\Files\BrowserErrorPagePlugin;
  43. use OCA\DAV\Files\CustomPropertiesBackend;
  44. use OCA\DAV\SystemTag\SystemTagPlugin;
  45. use OCP\IRequest;
  46. use OCP\SabrePluginEvent;
  47. use Sabre\CardDAV\VCFExportPlugin;
  48. use Sabre\DAV\Auth\Plugin;
  49. use OCA\DAV\Connector\Sabre\TagsPlugin;
  50. class Server {
  51. /** @var IRequest */
  52. private $request;
  53. /** @var string */
  54. private $baseUri;
  55. /** @var Connector\Sabre\Server */
  56. private $server;
  57. public function __construct(IRequest $request, $baseUri) {
  58. $this->request = $request;
  59. $this->baseUri = $baseUri;
  60. $logger = \OC::$server->getLogger();
  61. $mailer = \OC::$server->getMailer();
  62. $dispatcher = \OC::$server->getEventDispatcher();
  63. $root = new RootCollection();
  64. $this->server = new \OCA\DAV\Connector\Sabre\Server($root);
  65. // Backends
  66. $authBackend = new Auth(
  67. \OC::$server->getSession(),
  68. \OC::$server->getUserSession(),
  69. \OC::$server->getRequest(),
  70. \OC::$server->getTwoFactorAuthManager(),
  71. \OC::$server->getBruteForceThrottler()
  72. );
  73. // Set URL explicitly due to reverse-proxy situations
  74. $this->server->httpRequest->setUrl($this->request->getRequestUri());
  75. $this->server->setBaseUri($this->baseUri);
  76. $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig()));
  77. $authPlugin = new Plugin();
  78. $authPlugin->addBackend($authBackend);
  79. $authPlugin->addBackend(new PublicAuth());
  80. $this->server->addPlugin($authPlugin);
  81. // allow setup of additional auth backends
  82. $event = new SabrePluginEvent($this->server);
  83. $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event);
  84. // because we are throwing exceptions this plugin has to be the last one
  85. $authPlugin->addBackend($authBackend);
  86. // debugging
  87. if(\OC::$server->getConfig()->getSystemValue('debug', false)) {
  88. $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin());
  89. } else {
  90. $this->server->addPlugin(new DummyGetResponsePlugin());
  91. }
  92. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger));
  93. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin());
  94. $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());
  95. // acl
  96. $acl = new DavAclPlugin();
  97. $acl->principalCollectionSet = [
  98. 'principals/users', 'principals/groups'
  99. ];
  100. $acl->defaultUsernamePath = 'principals/users';
  101. $this->server->addPlugin($acl);
  102. // calendar plugins
  103. $this->server->addPlugin(new \Sabre\CalDAV\Plugin());
  104. $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin());
  105. $this->server->addPlugin(new \Sabre\CalDAV\Schedule\Plugin());
  106. $this->server->addPlugin(new IMipPlugin($mailer, $logger));
  107. $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
  108. $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
  109. $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest()));
  110. $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin(
  111. \OC::$server->getConfig(),
  112. \OC::$server->getUrlGenerator()
  113. ));
  114. // addressbook plugins
  115. $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
  116. $this->server->addPlugin(new VCFExportPlugin());
  117. $this->server->addPlugin(new ImageExportPlugin(\OC::$server->getLogger()));
  118. // system tags plugins
  119. $this->server->addPlugin(new SystemTagPlugin(
  120. \OC::$server->getSystemTagManager(),
  121. \OC::$server->getGroupManager(),
  122. \OC::$server->getUserSession()
  123. ));
  124. // comments plugin
  125. $this->server->addPlugin(new CommentsPlugin(
  126. \OC::$server->getCommentsManager(),
  127. \OC::$server->getUserSession()
  128. ));
  129. $this->server->addPlugin(new CopyEtagHeaderPlugin());
  130. // Some WebDAV clients do require Class 2 WebDAV support (locking), since
  131. // we do not provide locking we emulate it using a fake locking plugin.
  132. if($request->isUserAgent([
  133. '/WebDAVFS/',
  134. '/Microsoft Office OneNote 2013/',
  135. ])) {
  136. $this->server->addPlugin(new FakeLockerPlugin());
  137. }
  138. if (BrowserErrorPagePlugin::isBrowserRequest($request)) {
  139. $this->server->addPlugin(new BrowserErrorPagePlugin());
  140. }
  141. // wait with registering these until auth is handled and the filesystem is setup
  142. $this->server->on('beforeMethod', function () {
  143. // custom properties plugin must be the last one
  144. $userSession = \OC::$server->getUserSession();
  145. $user = $userSession->getUser();
  146. if (!is_null($user)) {
  147. $view = \OC\Files\Filesystem::getView();
  148. $this->server->addPlugin(
  149. new FilesPlugin(
  150. $this->server->tree,
  151. $view,
  152. \OC::$server->getConfig(),
  153. $this->request,
  154. \OC::$server->getPreviewManager(),
  155. false,
  156. !\OC::$server->getConfig()->getSystemValue('debug', false)
  157. )
  158. );
  159. $this->server->addPlugin(
  160. new \Sabre\DAV\PropertyStorage\Plugin(
  161. new CustomPropertiesBackend(
  162. $this->server->tree,
  163. \OC::$server->getDatabaseConnection(),
  164. \OC::$server->getUserSession()->getUser()
  165. )
  166. )
  167. );
  168. $this->server->addPlugin(
  169. new QuotaPlugin($view)
  170. );
  171. $this->server->addPlugin(
  172. new TagsPlugin(
  173. $this->server->tree, \OC::$server->getTagManager()
  174. )
  175. );
  176. // TODO: switch to LazyUserFolder
  177. $userFolder = \OC::$server->getUserFolder();
  178. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\SharesPlugin(
  179. $this->server->tree,
  180. $userSession,
  181. $userFolder,
  182. \OC::$server->getShareManager()
  183. ));
  184. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\CommentPropertiesPlugin(
  185. \OC::$server->getCommentsManager(),
  186. $userSession
  187. ));
  188. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\FilesReportPlugin(
  189. $this->server->tree,
  190. $view,
  191. \OC::$server->getSystemTagManager(),
  192. \OC::$server->getSystemTagObjectMapper(),
  193. \OC::$server->getTagManager(),
  194. $userSession,
  195. \OC::$server->getGroupManager(),
  196. $userFolder
  197. ));
  198. }
  199. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin());
  200. });
  201. }
  202. public function exec() {
  203. $this->server->exec();
  204. }
  205. }