Server.php 10 KB

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