Server.php 11 KB

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