Server.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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\CommentPropertiesPlugin;
  36. use OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin;
  37. use OCA\DAV\Connector\Sabre\DavAclPlugin;
  38. use OCA\DAV\Connector\Sabre\DummyGetResponsePlugin;
  39. use OCA\DAV\Connector\Sabre\FakeLockerPlugin;
  40. use OCA\DAV\Connector\Sabre\FilesPlugin;
  41. use OCA\DAV\Connector\Sabre\FilesReportPlugin;
  42. use OCA\DAV\Connector\Sabre\SharesPlugin;
  43. use OCA\DAV\DAV\PublicAuth;
  44. use OCA\DAV\Connector\Sabre\QuotaPlugin;
  45. use OCA\DAV\Files\BrowserErrorPagePlugin;
  46. use OCA\DAV\Files\CustomPropertiesBackend;
  47. use OCA\DAV\SystemTag\SystemTagPlugin;
  48. use OCP\IRequest;
  49. use OCP\SabrePluginEvent;
  50. use Sabre\CardDAV\VCFExportPlugin;
  51. use Sabre\DAV\Auth\Plugin;
  52. use OCA\DAV\Connector\Sabre\TagsPlugin;
  53. use SearchDAV\DAV\SearchPlugin;
  54. class Server {
  55. /** @var IRequest */
  56. private $request;
  57. /** @var string */
  58. private $baseUri;
  59. /** @var Connector\Sabre\Server */
  60. private $server;
  61. public function __construct(IRequest $request, $baseUri) {
  62. $this->request = $request;
  63. $this->baseUri = $baseUri;
  64. $logger = \OC::$server->getLogger();
  65. $mailer = \OC::$server->getMailer();
  66. $dispatcher = \OC::$server->getEventDispatcher();
  67. $root = new RootCollection();
  68. $this->server = new \OCA\DAV\Connector\Sabre\Server($root);
  69. // Backends
  70. $authBackend = new Auth(
  71. \OC::$server->getSession(),
  72. \OC::$server->getUserSession(),
  73. \OC::$server->getRequest(),
  74. \OC::$server->getTwoFactorAuthManager(),
  75. \OC::$server->getBruteForceThrottler()
  76. );
  77. // Set URL explicitly due to reverse-proxy situations
  78. $this->server->httpRequest->setUrl($this->request->getRequestUri());
  79. $this->server->setBaseUri($this->baseUri);
  80. $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig()));
  81. $authPlugin = new Plugin();
  82. $authPlugin->addBackend(new PublicAuth());
  83. $this->server->addPlugin($authPlugin);
  84. // allow setup of additional auth backends
  85. $event = new SabrePluginEvent($this->server);
  86. $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event);
  87. // because we are throwing exceptions this plugin has to be the last one
  88. $authPlugin->addBackend($authBackend);
  89. // debugging
  90. if(\OC::$server->getConfig()->getSystemValue('debug', false)) {
  91. $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin());
  92. } else {
  93. $this->server->addPlugin(new DummyGetResponsePlugin());
  94. }
  95. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger));
  96. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin());
  97. $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());
  98. // acl
  99. $acl = new DavAclPlugin();
  100. $acl->principalCollectionSet = [
  101. 'principals/users', 'principals/groups'
  102. ];
  103. $acl->defaultUsernamePath = 'principals/users';
  104. $this->server->addPlugin($acl);
  105. // calendar plugins
  106. $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin());
  107. $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin());
  108. $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin());
  109. $this->server->addPlugin(new IMipPlugin($mailer, $logger));
  110. $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
  111. $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
  112. $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest()));
  113. $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin(
  114. \OC::$server->getConfig(),
  115. \OC::$server->getURLGenerator()
  116. ));
  117. // addressbook plugins
  118. $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
  119. $this->server->addPlugin(new VCFExportPlugin());
  120. $this->server->addPlugin(new ImageExportPlugin(\OC::$server->getLogger()));
  121. // system tags plugins
  122. $this->server->addPlugin(new SystemTagPlugin(
  123. \OC::$server->getSystemTagManager(),
  124. \OC::$server->getGroupManager(),
  125. \OC::$server->getUserSession()
  126. ));
  127. // comments plugin
  128. $this->server->addPlugin(new CommentsPlugin(
  129. \OC::$server->getCommentsManager(),
  130. \OC::$server->getUserSession()
  131. ));
  132. $this->server->addPlugin(new CopyEtagHeaderPlugin());
  133. // Some WebDAV clients do require Class 2 WebDAV support (locking), since
  134. // we do not provide locking we emulate it using a fake locking plugin.
  135. if($request->isUserAgent([
  136. '/WebDAVFS/',
  137. '/Microsoft Office OneNote 2013/',
  138. '/^Microsoft-WebDAV/',// Microsoft-WebDAV-MiniRedir/6.1.7601
  139. ])) {
  140. $this->server->addPlugin(new FakeLockerPlugin());
  141. }
  142. if (BrowserErrorPagePlugin::isBrowserRequest($request)) {
  143. $this->server->addPlugin(new BrowserErrorPagePlugin());
  144. }
  145. // wait with registering these until auth is handled and the filesystem is setup
  146. $this->server->on('beforeMethod', function () {
  147. // custom properties plugin must be the last one
  148. $userSession = \OC::$server->getUserSession();
  149. $user = $userSession->getUser();
  150. if ($user !== null) {
  151. $view = \OC\Files\Filesystem::getView();
  152. $this->server->addPlugin(
  153. new FilesPlugin(
  154. $this->server->tree,
  155. \OC::$server->getConfig(),
  156. $this->request,
  157. \OC::$server->getPreviewManager(),
  158. false,
  159. !\OC::$server->getConfig()->getSystemValue('debug', false)
  160. )
  161. );
  162. $this->server->addPlugin(
  163. new \Sabre\DAV\PropertyStorage\Plugin(
  164. new CustomPropertiesBackend(
  165. $this->server->tree,
  166. \OC::$server->getDatabaseConnection(),
  167. \OC::$server->getUserSession()->getUser()
  168. )
  169. )
  170. );
  171. if ($view !== null) {
  172. $this->server->addPlugin(
  173. new QuotaPlugin($view));
  174. }
  175. $this->server->addPlugin(
  176. new TagsPlugin(
  177. $this->server->tree, \OC::$server->getTagManager()
  178. )
  179. );
  180. // TODO: switch to LazyUserFolder
  181. $userFolder = \OC::$server->getUserFolder();
  182. $this->server->addPlugin(new SharesPlugin(
  183. $this->server->tree,
  184. $userSession,
  185. $userFolder,
  186. \OC::$server->getShareManager()
  187. ));
  188. $this->server->addPlugin(new CommentPropertiesPlugin(
  189. \OC::$server->getCommentsManager(),
  190. $userSession
  191. ));
  192. if ($view !== null) {
  193. $this->server->addPlugin(new FilesReportPlugin(
  194. $this->server->tree,
  195. $view,
  196. \OC::$server->getSystemTagManager(),
  197. \OC::$server->getSystemTagObjectMapper(),
  198. \OC::$server->getTagManager(),
  199. $userSession,
  200. \OC::$server->getGroupManager(),
  201. $userFolder
  202. ));
  203. $this->server->addPlugin(new SearchPlugin(new \OCA\DAV\Files\FileSearchBackend(
  204. $this->server->tree,
  205. $user,
  206. \OC::$server->getRootFolder(),
  207. \OC::$server->getShareManager(),
  208. $view
  209. )));
  210. }
  211. }
  212. });
  213. }
  214. public function exec() {
  215. $this->server->exec();
  216. }
  217. }