Server.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Brandon Kirsch <brandonkirsch@github.com>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Georg Ehrke <oc.list@georgehrke.com>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author John Molakvoæ <skjnldsv@protonmail.com>
  12. * @author Lukas Reschke <lukas@statuscode.ch>
  13. * @author Morris Jobke <hey@morrisjobke.de>
  14. * @author Robin Appelman <robin@icewind.nl>
  15. * @author Roeland Jago Douma <roeland@famdouma.nl>
  16. * @author Thomas Citharel <nextcloud@tcit.fr>
  17. * @author Thomas Müller <thomas.mueller@tmit.eu>
  18. * @author Vincent Petry <vincent@nextcloud.com>
  19. *
  20. * @license AGPL-3.0
  21. *
  22. * This code is free software: you can redistribute it and/or modify
  23. * it under the terms of the GNU Affero General Public License, version 3,
  24. * as published by the Free Software Foundation.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU Affero General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Affero General Public License, version 3,
  32. * along with this program. If not, see <http://www.gnu.org/licenses/>
  33. *
  34. */
  35. namespace OCA\DAV;
  36. use OCA\DAV\AppInfo\PluginManager;
  37. use OCA\DAV\BulkUpload\BulkUploadPlugin;
  38. use OCA\DAV\CalDAV\BirthdayService;
  39. use OCA\DAV\CardDAV\HasPhotoPlugin;
  40. use OCA\DAV\CardDAV\ImageExportPlugin;
  41. use OCA\DAV\CardDAV\MultiGetExportPlugin;
  42. use OCA\DAV\CardDAV\PhotoCache;
  43. use OCA\DAV\Comments\CommentsPlugin;
  44. use OCA\DAV\Connector\Sabre\AnonymousOptionsPlugin;
  45. use OCA\DAV\Connector\Sabre\Auth;
  46. use OCA\DAV\Connector\Sabre\BearerAuth;
  47. use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
  48. use OCA\DAV\Connector\Sabre\CachingTree;
  49. use OCA\DAV\Connector\Sabre\ChecksumUpdatePlugin;
  50. use OCA\DAV\Connector\Sabre\CommentPropertiesPlugin;
  51. use OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin;
  52. use OCA\DAV\Connector\Sabre\DavAclPlugin;
  53. use OCA\DAV\Connector\Sabre\DummyGetResponsePlugin;
  54. use OCA\DAV\Connector\Sabre\FakeLockerPlugin;
  55. use OCA\DAV\Connector\Sabre\FilesPlugin;
  56. use OCA\DAV\Connector\Sabre\FilesReportPlugin;
  57. use OCA\DAV\Connector\Sabre\PropfindCompressionPlugin;
  58. use OCA\DAV\Connector\Sabre\QuotaPlugin;
  59. use OCA\DAV\Connector\Sabre\RequestIdHeaderPlugin;
  60. use OCA\DAV\Connector\Sabre\SharesPlugin;
  61. use OCA\DAV\Connector\Sabre\TagsPlugin;
  62. use OCA\DAV\DAV\CustomPropertiesBackend;
  63. use OCA\DAV\DAV\PublicAuth;
  64. use OCA\DAV\DAV\ViewOnlyPlugin;
  65. use OCA\DAV\Events\SabrePluginAuthInitEvent;
  66. use OCA\DAV\Files\BrowserErrorPagePlugin;
  67. use OCA\DAV\Files\LazySearchBackend;
  68. use OCA\DAV\Profiler\ProfilerPlugin;
  69. use OCA\DAV\Provisioning\Apple\AppleProvisioningPlugin;
  70. use OCA\DAV\SystemTag\SystemTagPlugin;
  71. use OCA\DAV\Upload\ChunkingPlugin;
  72. use OCP\AppFramework\Http\Response;
  73. use OCP\Diagnostics\IEventLogger;
  74. use OCP\EventDispatcher\IEventDispatcher;
  75. use OCP\IRequest;
  76. use OCP\Profiler\IProfiler;
  77. use OCP\SabrePluginEvent;
  78. use Psr\Log\LoggerInterface;
  79. use Sabre\CardDAV\VCFExportPlugin;
  80. use Sabre\DAV\Auth\Plugin;
  81. use Sabre\DAV\UUIDUtil;
  82. use SearchDAV\DAV\SearchPlugin;
  83. class Server {
  84. private IRequest $request;
  85. private string $baseUri;
  86. public Connector\Sabre\Server $server;
  87. private IProfiler $profiler;
  88. public function __construct(IRequest $request, string $baseUri) {
  89. $this->profiler = \OC::$server->get(IProfiler::class);
  90. if ($this->profiler->isEnabled()) {
  91. /** @var IEventLogger $eventLogger */
  92. $eventLogger = \OC::$server->get(IEventLogger::class);
  93. $eventLogger->start('runtime', 'DAV Runtime');
  94. }
  95. $this->request = $request;
  96. $this->baseUri = $baseUri;
  97. $logger = \OC::$server->get(LoggerInterface::class);
  98. $dispatcher = \OC::$server->getEventDispatcher();
  99. /** @var IEventDispatcher $newDispatcher */
  100. $newDispatcher = \OC::$server->query(IEventDispatcher::class);
  101. $root = new RootCollection();
  102. $this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root));
  103. // Add maintenance plugin
  104. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig(), \OC::$server->getL10N('dav')));
  105. // Backends
  106. $authBackend = new Auth(
  107. \OC::$server->getSession(),
  108. \OC::$server->getUserSession(),
  109. \OC::$server->getRequest(),
  110. \OC::$server->getTwoFactorAuthManager(),
  111. \OC::$server->getBruteForceThrottler()
  112. );
  113. // Set URL explicitly due to reverse-proxy situations
  114. $this->server->httpRequest->setUrl($this->request->getRequestUri());
  115. $this->server->setBaseUri($this->baseUri);
  116. $this->server->addPlugin(new ProfilerPlugin($this->request));
  117. $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig()));
  118. $this->server->addPlugin(new AnonymousOptionsPlugin());
  119. $authPlugin = new Plugin();
  120. $authPlugin->addBackend(new PublicAuth());
  121. $this->server->addPlugin($authPlugin);
  122. // allow setup of additional auth backends
  123. $event = new SabrePluginEvent($this->server);
  124. $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event);
  125. $newAuthEvent = new SabrePluginAuthInitEvent($this->server);
  126. $newDispatcher->dispatchTyped($newAuthEvent);
  127. $bearerAuthBackend = new BearerAuth(
  128. \OC::$server->getUserSession(),
  129. \OC::$server->getSession(),
  130. \OC::$server->getRequest()
  131. );
  132. $authPlugin->addBackend($bearerAuthBackend);
  133. // because we are throwing exceptions this plugin has to be the last one
  134. $authPlugin->addBackend($authBackend);
  135. // debugging
  136. if (\OC::$server->getConfig()->getSystemValue('debug', false)) {
  137. $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin());
  138. } else {
  139. $this->server->addPlugin(new DummyGetResponsePlugin());
  140. }
  141. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger));
  142. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin());
  143. $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());
  144. // acl
  145. $acl = new DavAclPlugin();
  146. $acl->principalCollectionSet = [
  147. 'principals/users',
  148. 'principals/groups',
  149. 'principals/calendar-resources',
  150. 'principals/calendar-rooms',
  151. ];
  152. $this->server->addPlugin($acl);
  153. // calendar plugins
  154. if ($this->requestIsForSubtree(['calendars', 'public-calendars', 'system-calendars', 'principals'])) {
  155. $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin());
  156. $this->server->addPlugin(new \OCA\DAV\CalDAV\ICSExportPlugin\ICSExportPlugin(\OC::$server->getConfig(), $logger));
  157. $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin(\OC::$server->getConfig()));
  158. if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') {
  159. $this->server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class));
  160. }
  161. $this->server->addPlugin(\OC::$server->get(\OCA\DAV\CalDAV\Trashbin\Plugin::class));
  162. $this->server->addPlugin(new \OCA\DAV\CalDAV\WebcalCaching\Plugin($request));
  163. $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
  164. $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
  165. $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
  166. $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin(
  167. \OC::$server->getConfig(),
  168. \OC::$server->getURLGenerator()
  169. ));
  170. }
  171. // addressbook plugins
  172. if ($this->requestIsForSubtree(['addressbooks', 'principals'])) {
  173. $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
  174. $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
  175. $this->server->addPlugin(new VCFExportPlugin());
  176. $this->server->addPlugin(new MultiGetExportPlugin());
  177. $this->server->addPlugin(new HasPhotoPlugin());
  178. $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(
  179. \OC::$server->getAppDataDir('dav-photocache'),
  180. $logger)
  181. ));
  182. }
  183. // system tags plugins
  184. $this->server->addPlugin(new SystemTagPlugin(
  185. \OC::$server->getSystemTagManager(),
  186. \OC::$server->getGroupManager(),
  187. \OC::$server->getUserSession()
  188. ));
  189. // comments plugin
  190. $this->server->addPlugin(new CommentsPlugin(
  191. \OC::$server->getCommentsManager(),
  192. \OC::$server->getUserSession()
  193. ));
  194. $this->server->addPlugin(new CopyEtagHeaderPlugin());
  195. $this->server->addPlugin(new RequestIdHeaderPlugin(\OC::$server->get(IRequest::class)));
  196. $this->server->addPlugin(new ChunkingPlugin());
  197. // allow setup of additional plugins
  198. $dispatcher->dispatch('OCA\DAV\Connector\Sabre::addPlugin', $event);
  199. // Some WebDAV clients do require Class 2 WebDAV support (locking), since
  200. // we do not provide locking we emulate it using a fake locking plugin.
  201. if ($request->isUserAgent([
  202. '/WebDAVFS/',
  203. '/OneNote/',
  204. '/^Microsoft-WebDAV/',// Microsoft-WebDAV-MiniRedir/6.1.7601
  205. ])) {
  206. $this->server->addPlugin(new FakeLockerPlugin());
  207. }
  208. // Allow view-only plugin for webdav requests
  209. $this->server->addPlugin(new ViewOnlyPlugin(
  210. $logger
  211. ));
  212. if (BrowserErrorPagePlugin::isBrowserRequest($request)) {
  213. $this->server->addPlugin(new BrowserErrorPagePlugin());
  214. }
  215. $lazySearchBackend = new LazySearchBackend();
  216. $this->server->addPlugin(new SearchPlugin($lazySearchBackend));
  217. // wait with registering these until auth is handled and the filesystem is setup
  218. $this->server->on('beforeMethod:*', function () use ($root, $lazySearchBackend, $logger) {
  219. // custom properties plugin must be the last one
  220. $userSession = \OC::$server->getUserSession();
  221. $user = $userSession->getUser();
  222. if ($user !== null) {
  223. $view = \OC\Files\Filesystem::getView();
  224. $this->server->addPlugin(
  225. new FilesPlugin(
  226. $this->server->tree,
  227. \OC::$server->getConfig(),
  228. $this->request,
  229. \OC::$server->getPreviewManager(),
  230. \OC::$server->getUserSession(),
  231. false,
  232. !\OC::$server->getConfig()->getSystemValue('debug', false)
  233. )
  234. );
  235. $this->server->addPlugin(new ChecksumUpdatePlugin());
  236. $this->server->addPlugin(
  237. new \Sabre\DAV\PropertyStorage\Plugin(
  238. new CustomPropertiesBackend(
  239. $this->server->tree,
  240. \OC::$server->getDatabaseConnection(),
  241. \OC::$server->getUserSession()->getUser()
  242. )
  243. )
  244. );
  245. if ($view !== null) {
  246. $this->server->addPlugin(
  247. new QuotaPlugin($view));
  248. }
  249. $this->server->addPlugin(
  250. new TagsPlugin(
  251. $this->server->tree, \OC::$server->getTagManager()
  252. )
  253. );
  254. // TODO: switch to LazyUserFolder
  255. $userFolder = \OC::$server->getUserFolder();
  256. $this->server->addPlugin(new SharesPlugin(
  257. $this->server->tree,
  258. $userSession,
  259. $userFolder,
  260. \OC::$server->getShareManager()
  261. ));
  262. $this->server->addPlugin(new CommentPropertiesPlugin(
  263. \OC::$server->getCommentsManager(),
  264. $userSession
  265. ));
  266. $this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin());
  267. if ($view !== null) {
  268. $this->server->addPlugin(new FilesReportPlugin(
  269. $this->server->tree,
  270. $view,
  271. \OC::$server->getSystemTagManager(),
  272. \OC::$server->getSystemTagObjectMapper(),
  273. \OC::$server->getTagManager(),
  274. $userSession,
  275. \OC::$server->getGroupManager(),
  276. $userFolder,
  277. \OC::$server->getAppManager()
  278. ));
  279. $lazySearchBackend->setBackend(new \OCA\DAV\Files\FileSearchBackend(
  280. $this->server->tree,
  281. $user,
  282. \OC::$server->getRootFolder(),
  283. \OC::$server->getShareManager(),
  284. $view
  285. ));
  286. $this->server->addPlugin(
  287. new BulkUploadPlugin(
  288. $userFolder,
  289. $logger
  290. )
  291. );
  292. }
  293. $this->server->addPlugin(new \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin(
  294. \OC::$server->getConfig(),
  295. \OC::$server->query(BirthdayService::class)
  296. ));
  297. $this->server->addPlugin(new AppleProvisioningPlugin(
  298. \OC::$server->getUserSession(),
  299. \OC::$server->getURLGenerator(),
  300. \OC::$server->getThemingDefaults(),
  301. \OC::$server->getRequest(),
  302. \OC::$server->getL10N('dav'),
  303. function () {
  304. return UUIDUtil::getUUID();
  305. }
  306. ));
  307. }
  308. // register plugins from apps
  309. $pluginManager = new PluginManager(
  310. \OC::$server,
  311. \OC::$server->getAppManager()
  312. );
  313. foreach ($pluginManager->getAppPlugins() as $appPlugin) {
  314. $this->server->addPlugin($appPlugin);
  315. }
  316. foreach ($pluginManager->getAppCollections() as $appCollection) {
  317. $root->addChild($appCollection);
  318. }
  319. });
  320. $this->server->addPlugin(
  321. new PropfindCompressionPlugin()
  322. );
  323. }
  324. public function exec() {
  325. /** @var IEventLogger $eventLogger */
  326. $eventLogger = \OC::$server->get(IEventLogger::class);
  327. $eventLogger->start('dav_server_exec', '');
  328. $this->server->exec();
  329. $eventLogger->end('dav_server_exec');
  330. if ($this->profiler->isEnabled()) {
  331. $eventLogger->end('runtime');
  332. $profile = $this->profiler->collect(\OC::$server->get(IRequest::class), new Response());
  333. $this->profiler->saveProfile($profile);
  334. }
  335. }
  336. private function requestIsForSubtree(array $subTrees): bool {
  337. foreach ($subTrees as $subTree) {
  338. $subTree = trim($subTree, ' /');
  339. if (strpos($this->server->getRequestUri(), $subTree.'/') === 0) {
  340. return true;
  341. }
  342. }
  343. return false;
  344. }
  345. }