Server.php 11 KB

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