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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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(\OC::$server->get(\OCA\DAV\CalDAV\Trashbin\Plugin::class));
  149. $this->server->addPlugin(new \OCA\DAV\CalDAV\WebcalCaching\Plugin($request));
  150. $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
  151. $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
  152. $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
  153. $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin(
  154. \OC::$server->getConfig(),
  155. \OC::$server->getURLGenerator()
  156. ));
  157. }
  158. // addressbook plugins
  159. if ($this->requestIsForSubtree(['addressbooks', 'principals'])) {
  160. $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
  161. $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
  162. $this->server->addPlugin(new VCFExportPlugin());
  163. $this->server->addPlugin(new MultiGetExportPlugin());
  164. $this->server->addPlugin(new HasPhotoPlugin());
  165. $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(
  166. \OC::$server->getAppDataDir('dav-photocache'),
  167. \OC::$server->getLogger())
  168. ));
  169. }
  170. // system tags plugins
  171. $this->server->addPlugin(new SystemTagPlugin(
  172. \OC::$server->getSystemTagManager(),
  173. \OC::$server->getGroupManager(),
  174. \OC::$server->getUserSession()
  175. ));
  176. // comments plugin
  177. $this->server->addPlugin(new CommentsPlugin(
  178. \OC::$server->getCommentsManager(),
  179. \OC::$server->getUserSession()
  180. ));
  181. $this->server->addPlugin(new CopyEtagHeaderPlugin());
  182. $this->server->addPlugin(new ChunkingPlugin());
  183. // allow setup of additional plugins
  184. $dispatcher->dispatch('OCA\DAV\Connector\Sabre::addPlugin', $event);
  185. // Some WebDAV clients do require Class 2 WebDAV support (locking), since
  186. // we do not provide locking we emulate it using a fake locking plugin.
  187. if ($request->isUserAgent([
  188. '/WebDAVFS/',
  189. '/OneNote/',
  190. '/^Microsoft-WebDAV/',// Microsoft-WebDAV-MiniRedir/6.1.7601
  191. ])) {
  192. $this->server->addPlugin(new FakeLockerPlugin());
  193. }
  194. if (BrowserErrorPagePlugin::isBrowserRequest($request)) {
  195. $this->server->addPlugin(new BrowserErrorPagePlugin());
  196. }
  197. $lazySearchBackend = new LazySearchBackend();
  198. $this->server->addPlugin(new SearchPlugin($lazySearchBackend));
  199. // wait with registering these until auth is handled and the filesystem is setup
  200. $this->server->on('beforeMethod:*', function () use ($root, $lazySearchBackend) {
  201. // custom properties plugin must be the last one
  202. $userSession = \OC::$server->getUserSession();
  203. $user = $userSession->getUser();
  204. if ($user !== null) {
  205. $view = \OC\Files\Filesystem::getView();
  206. $this->server->addPlugin(
  207. new FilesPlugin(
  208. $this->server->tree,
  209. \OC::$server->getConfig(),
  210. $this->request,
  211. \OC::$server->getPreviewManager(),
  212. false,
  213. !\OC::$server->getConfig()->getSystemValue('debug', false)
  214. )
  215. );
  216. $this->server->addPlugin(
  217. new \Sabre\DAV\PropertyStorage\Plugin(
  218. new CustomPropertiesBackend(
  219. $this->server->tree,
  220. \OC::$server->getDatabaseConnection(),
  221. \OC::$server->getUserSession()->getUser()
  222. )
  223. )
  224. );
  225. if ($view !== null) {
  226. $this->server->addPlugin(
  227. new QuotaPlugin($view, false));
  228. }
  229. $this->server->addPlugin(
  230. new TagsPlugin(
  231. $this->server->tree, \OC::$server->getTagManager()
  232. )
  233. );
  234. // TODO: switch to LazyUserFolder
  235. $userFolder = \OC::$server->getUserFolder();
  236. $this->server->addPlugin(new SharesPlugin(
  237. $this->server->tree,
  238. $userSession,
  239. $userFolder,
  240. \OC::$server->getShareManager()
  241. ));
  242. $this->server->addPlugin(new CommentPropertiesPlugin(
  243. \OC::$server->getCommentsManager(),
  244. $userSession
  245. ));
  246. $this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin());
  247. if ($view !== null) {
  248. $this->server->addPlugin(new FilesReportPlugin(
  249. $this->server->tree,
  250. $view,
  251. \OC::$server->getSystemTagManager(),
  252. \OC::$server->getSystemTagObjectMapper(),
  253. \OC::$server->getTagManager(),
  254. $userSession,
  255. \OC::$server->getGroupManager(),
  256. $userFolder,
  257. \OC::$server->getAppManager()
  258. ));
  259. $lazySearchBackend->setBackend(new \OCA\DAV\Files\FileSearchBackend(
  260. $this->server->tree,
  261. $user,
  262. \OC::$server->getRootFolder(),
  263. \OC::$server->getShareManager(),
  264. $view
  265. ));
  266. }
  267. $this->server->addPlugin(new \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin(
  268. \OC::$server->getConfig(),
  269. \OC::$server->query(BirthdayService::class)
  270. ));
  271. $this->server->addPlugin(new AppleProvisioningPlugin(
  272. \OC::$server->getUserSession(),
  273. \OC::$server->getURLGenerator(),
  274. \OC::$server->getThemingDefaults(),
  275. \OC::$server->getRequest(),
  276. \OC::$server->getL10N('dav'),
  277. function () {
  278. return UUIDUtil::getUUID();
  279. }
  280. ));
  281. }
  282. // register plugins from apps
  283. $pluginManager = new PluginManager(
  284. \OC::$server,
  285. \OC::$server->getAppManager()
  286. );
  287. foreach ($pluginManager->getAppPlugins() as $appPlugin) {
  288. $this->server->addPlugin($appPlugin);
  289. }
  290. foreach ($pluginManager->getAppCollections() as $appCollection) {
  291. $root->addChild($appCollection);
  292. }
  293. });
  294. $this->server->addPlugin(
  295. new PropfindCompressionPlugin()
  296. );
  297. }
  298. public function exec() {
  299. $this->server->exec();
  300. }
  301. private function requestIsForSubtree(array $subTrees): bool {
  302. foreach ($subTrees as $subTree) {
  303. $subTree = trim($subTree, ' /');
  304. if (strpos($this->server->getRequestUri(), $subTree.'/') === 0) {
  305. return true;
  306. }
  307. }
  308. return false;
  309. }
  310. }