Server.php 11 KB

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