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.

Server.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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) <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\CalDAV\BirthdayService;
  38. use OCA\DAV\CardDAV\HasPhotoPlugin;
  39. use OCA\DAV\CardDAV\ImageExportPlugin;
  40. use OCA\DAV\CardDAV\MultiGetExportPlugin;
  41. use OCA\DAV\CardDAV\PhotoCache;
  42. use OCA\DAV\Comments\CommentsPlugin;
  43. use OCA\DAV\Connector\Sabre\AnonymousOptionsPlugin;
  44. use OCA\DAV\Connector\Sabre\Auth;
  45. use OCA\DAV\Connector\Sabre\BearerAuth;
  46. use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
  47. use OCA\DAV\Connector\Sabre\CachingTree;
  48. use OCA\DAV\Connector\Sabre\CommentPropertiesPlugin;
  49. use OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin;
  50. use OCA\DAV\Connector\Sabre\DavAclPlugin;
  51. use OCA\DAV\Connector\Sabre\DummyGetResponsePlugin;
  52. use OCA\DAV\Connector\Sabre\FakeLockerPlugin;
  53. use OCA\DAV\Connector\Sabre\FilesPlugin;
  54. use OCA\DAV\Connector\Sabre\FilesReportPlugin;
  55. use OCA\DAV\Connector\Sabre\PropfindCompressionPlugin;
  56. use OCA\DAV\Connector\Sabre\QuotaPlugin;
  57. use OCA\DAV\Connector\Sabre\SharesPlugin;
  58. use OCA\DAV\Connector\Sabre\TagsPlugin;
  59. use OCA\DAV\DAV\CustomPropertiesBackend;
  60. use OCA\DAV\DAV\PublicAuth;
  61. use OCA\DAV\Events\SabrePluginAuthInitEvent;
  62. use OCA\DAV\Files\BrowserErrorPagePlugin;
  63. use OCA\DAV\Files\LazySearchBackend;
  64. use OCA\DAV\Provisioning\Apple\AppleProvisioningPlugin;
  65. use OCA\DAV\SystemTag\SystemTagPlugin;
  66. use OCA\DAV\Upload\ChunkingPlugin;
  67. use OCP\EventDispatcher\IEventDispatcher;
  68. use OCP\IRequest;
  69. use OCP\SabrePluginEvent;
  70. use Sabre\CardDAV\VCFExportPlugin;
  71. use Sabre\DAV\Auth\Plugin;
  72. use Sabre\DAV\UUIDUtil;
  73. use SearchDAV\DAV\SearchPlugin;
  74. class Server {
  75. /** @var IRequest */
  76. private $request;
  77. /** @var string */
  78. private $baseUri;
  79. /** @var Connector\Sabre\Server */
  80. public $server;
  81. public function __construct(IRequest $request, $baseUri) {
  82. $this->request = $request;
  83. $this->baseUri = $baseUri;
  84. $logger = \OC::$server->getLogger();
  85. $dispatcher = \OC::$server->getEventDispatcher();
  86. /** @var IEventDispatcher $newDispatcher */
  87. $newDispatcher = \OC::$server->query(IEventDispatcher::class);
  88. $root = new RootCollection();
  89. $this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root));
  90. // Add maintenance plugin
  91. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig(), \OC::$server->getL10N('dav')));
  92. // Backends
  93. $authBackend = new Auth(
  94. \OC::$server->getSession(),
  95. \OC::$server->getUserSession(),
  96. \OC::$server->getRequest(),
  97. \OC::$server->getTwoFactorAuthManager(),
  98. \OC::$server->getBruteForceThrottler()
  99. );
  100. // Set URL explicitly due to reverse-proxy situations
  101. $this->server->httpRequest->setUrl($this->request->getRequestUri());
  102. $this->server->setBaseUri($this->baseUri);
  103. $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig()));
  104. $this->server->addPlugin(new AnonymousOptionsPlugin());
  105. $authPlugin = new Plugin();
  106. $authPlugin->addBackend(new PublicAuth());
  107. $this->server->addPlugin($authPlugin);
  108. // allow setup of additional auth backends
  109. $event = new SabrePluginEvent($this->server);
  110. $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event);
  111. $newAuthEvent = new SabrePluginAuthInitEvent($this->server);
  112. $newDispatcher->dispatchTyped($newAuthEvent);
  113. $bearerAuthBackend = new BearerAuth(
  114. \OC::$server->getUserSession(),
  115. \OC::$server->getSession(),
  116. \OC::$server->getRequest()
  117. );
  118. $authPlugin->addBackend($bearerAuthBackend);
  119. // because we are throwing exceptions this plugin has to be the last one
  120. $authPlugin->addBackend($authBackend);
  121. // debugging
  122. if (\OC::$server->getConfig()->getSystemValue('debug', false)) {
  123. $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin());
  124. } else {
  125. $this->server->addPlugin(new DummyGetResponsePlugin());
  126. }
  127. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger));
  128. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin());
  129. $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());
  130. // acl
  131. $acl = new DavAclPlugin();
  132. $acl->principalCollectionSet = [
  133. 'principals/users',
  134. 'principals/groups',
  135. 'principals/calendar-resources',
  136. 'principals/calendar-rooms',
  137. ];
  138. $acl->defaultUsernamePath = 'principals/users';
  139. $this->server->addPlugin($acl);
  140. // calendar plugins
  141. if ($this->requestIsForSubtree(['calendars', 'public-calendars', 'system-calendars', 'principals'])) {
  142. $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin());
  143. $this->server->addPlugin(new \OCA\DAV\CalDAV\ICSExportPlugin\ICSExportPlugin(\OC::$server->getConfig(), \OC::$server->getLogger()));
  144. $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin(\OC::$server->getConfig()));
  145. if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') {
  146. $this->server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class));
  147. }
  148. $this->server->addPlugin(new CalDAV\WebcalCaching\Plugin($request));
  149. $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
  150. $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
  151. $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
  152. $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin(
  153. \OC::$server->getConfig(),
  154. \OC::$server->getURLGenerator()
  155. ));
  156. }
  157. // addressbook plugins
  158. if ($this->requestIsForSubtree(['addressbooks', 'principals'])) {
  159. $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
  160. $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
  161. $this->server->addPlugin(new VCFExportPlugin());
  162. $this->server->addPlugin(new MultiGetExportPlugin());
  163. $this->server->addPlugin(new HasPhotoPlugin());
  164. $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(
  165. \OC::$server->getAppDataDir('dav-photocache'),
  166. \OC::$server->getLogger())
  167. ));
  168. }
  169. // system tags plugins
  170. $this->server->addPlugin(new SystemTagPlugin(
  171. \OC::$server->getSystemTagManager(),
  172. \OC::$server->getGroupManager(),
  173. \OC::$server->getUserSession()
  174. ));
  175. // comments plugin
  176. $this->server->addPlugin(new CommentsPlugin(
  177. \OC::$server->getCommentsManager(),
  178. \OC::$server->getUserSession()
  179. ));
  180. $this->server->addPlugin(new CopyEtagHeaderPlugin());
  181. $this->server->addPlugin(new ChunkingPlugin());
  182. // allow setup of additional plugins
  183. $dispatcher->dispatch('OCA\DAV\Connector\Sabre::addPlugin', $event);
  184. // Some WebDAV clients do require Class 2 WebDAV support (locking), since
  185. // we do not provide locking we emulate it using a fake locking plugin.
  186. if ($request->isUserAgent([
  187. '/WebDAVFS/',
  188. '/OneNote/',
  189. '/^Microsoft-WebDAV/',// Microsoft-WebDAV-MiniRedir/6.1.7601
  190. ])) {
  191. $this->server->addPlugin(new FakeLockerPlugin());
  192. }
  193. if (BrowserErrorPagePlugin::isBrowserRequest($request)) {
  194. $this->server->addPlugin(new BrowserErrorPagePlugin());
  195. }
  196. $lazySearchBackend = new LazySearchBackend();
  197. $this->server->addPlugin(new SearchPlugin($lazySearchBackend));
  198. // wait with registering these until auth is handled and the filesystem is setup
  199. $this->server->on('beforeMethod:*', function () use ($root, $lazySearchBackend) {
  200. // custom properties plugin must be the last one
  201. $userSession = \OC::$server->getUserSession();
  202. $user = $userSession->getUser();
  203. if ($user !== null) {
  204. $view = \OC\Files\Filesystem::getView();
  205. $this->server->addPlugin(
  206. new FilesPlugin(
  207. $this->server->tree,
  208. \OC::$server->getConfig(),
  209. $this->request,
  210. \OC::$server->getPreviewManager(),
  211. false,
  212. !\OC::$server->getConfig()->getSystemValue('debug', false)
  213. )
  214. );
  215. $this->server->addPlugin(
  216. new \Sabre\DAV\PropertyStorage\Plugin(
  217. new CustomPropertiesBackend(
  218. $this->server->tree,
  219. \OC::$server->getDatabaseConnection(),
  220. \OC::$server->getUserSession()->getUser()
  221. )
  222. )
  223. );
  224. if ($view !== null) {
  225. $this->server->addPlugin(
  226. new QuotaPlugin($view, false));
  227. }
  228. $this->server->addPlugin(
  229. new TagsPlugin(
  230. $this->server->tree, \OC::$server->getTagManager()
  231. )
  232. );
  233. // TODO: switch to LazyUserFolder
  234. $userFolder = \OC::$server->getUserFolder();
  235. $this->server->addPlugin(new SharesPlugin(
  236. $this->server->tree,
  237. $userSession,
  238. $userFolder,
  239. \OC::$server->getShareManager()
  240. ));
  241. $this->server->addPlugin(new CommentPropertiesPlugin(
  242. \OC::$server->getCommentsManager(),
  243. $userSession
  244. ));
  245. $this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin());
  246. if ($view !== null) {
  247. $this->server->addPlugin(new FilesReportPlugin(
  248. $this->server->tree,
  249. $view,
  250. \OC::$server->getSystemTagManager(),
  251. \OC::$server->getSystemTagObjectMapper(),
  252. \OC::$server->getTagManager(),
  253. $userSession,
  254. \OC::$server->getGroupManager(),
  255. $userFolder,
  256. \OC::$server->getAppManager()
  257. ));
  258. $lazySearchBackend->setBackend(new \OCA\DAV\Files\FileSearchBackend(
  259. $this->server->tree,
  260. $user,
  261. \OC::$server->getRootFolder(),
  262. \OC::$server->getShareManager(),
  263. $view
  264. ));
  265. }
  266. $this->server->addPlugin(new \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin(
  267. \OC::$server->getConfig(),
  268. \OC::$server->query(BirthdayService::class)
  269. ));
  270. $this->server->addPlugin(new AppleProvisioningPlugin(
  271. \OC::$server->getUserSession(),
  272. \OC::$server->getURLGenerator(),
  273. \OC::$server->getThemingDefaults(),
  274. \OC::$server->getRequest(),
  275. \OC::$server->getL10N('dav'),
  276. function () {
  277. return UUIDUtil::getUUID();
  278. }
  279. ));
  280. }
  281. // register plugins from apps
  282. $pluginManager = new PluginManager(
  283. \OC::$server,
  284. \OC::$server->getAppManager()
  285. );
  286. foreach ($pluginManager->getAppPlugins() as $appPlugin) {
  287. $this->server->addPlugin($appPlugin);
  288. }
  289. foreach ($pluginManager->getAppCollections() as $appCollection) {
  290. $root->addChild($appCollection);
  291. }
  292. });
  293. $this->server->addPlugin(
  294. new PropfindCompressionPlugin()
  295. );
  296. }
  297. public function exec() {
  298. $this->server->exec();
  299. }
  300. private function requestIsForSubtree(array $subTrees): bool {
  301. foreach ($subTrees as $subTree) {
  302. $subTree = trim($subTree, ' /');
  303. if (strpos($this->server->getRequestUri(), $subTree.'/') === 0) {
  304. return true;
  305. }
  306. }
  307. return false;
  308. }
  309. }