You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AnonymousOptionsPlugin.php 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. $isAnonymousOption = $request->getMethod() === 'OPTIONS' && $emptyAuth;
  64. $isOfficeHead = $request->getMethod() === 'HEAD' && $isOffice && $emptyAuth;
  65. if ($isAnonymousOption || $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. }