Server.php 13 KB

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