AnonymousOptionsPlugin.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Bastien Durel <bastien@durel.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\Connector\Sabre;
  28. use Sabre\DAV\CorePlugin;
  29. use Sabre\DAV\FS\Directory;
  30. use Sabre\DAV\ServerPlugin;
  31. use Sabre\DAV\Tree;
  32. use Sabre\HTTP\RequestInterface;
  33. use Sabre\HTTP\ResponseInterface;
  34. class AnonymousOptionsPlugin extends ServerPlugin {
  35. /**
  36. * @var \Sabre\DAV\Server
  37. */
  38. private $server;
  39. /**
  40. * @param \Sabre\DAV\Server $server
  41. * @return void
  42. */
  43. public function initialize(\Sabre\DAV\Server $server) {
  44. $this->server = $server;
  45. // before auth
  46. $this->server->on('beforeMethod:*', [$this, 'handleAnonymousOptions'], 9);
  47. }
  48. /**
  49. * @return bool
  50. */
  51. public function isRequestInRoot($path) {
  52. return $path === '' || (is_string($path) && strpos($path, '/') === false);
  53. }
  54. /**
  55. * @throws \Sabre\DAV\Exception\Forbidden
  56. * @return bool
  57. */
  58. public function handleAnonymousOptions(RequestInterface $request, ResponseInterface $response) {
  59. $isOffice = preg_match('/Microsoft Office/i', $request->getHeader('User-Agent') ?? '');
  60. $emptyAuth = $request->getHeader('Authorization') === null
  61. || $request->getHeader('Authorization') === ''
  62. || trim($request->getHeader('Authorization')) === 'Bearer';
  63. $isAnonymousOfficeOption = $request->getMethod() === 'OPTIONS' && $isOffice && $emptyAuth;
  64. $isOfficeHead = $request->getMethod() === 'HEAD' && $isOffice && $emptyAuth;
  65. if ($isAnonymousOfficeOption || $isOfficeHead) {
  66. /** @var CorePlugin $corePlugin */
  67. $corePlugin = $this->server->getPlugin('core');
  68. // setup a fake tree for anonymous access
  69. $this->server->tree = new Tree(new Directory(''));
  70. $corePlugin->httpOptions($request, $response);
  71. $this->server->emit('afterMethod:*', [$request, $response]);
  72. $this->server->emit('afterMethod:OPTIONS', [$request, $response]);
  73. $this->server->sapi->sendResponse($response);
  74. return false;
  75. }
  76. }
  77. }