Server.php 11 KB

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