2018-05-22 14:40:54 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Bastien Durel <bastien@durel.org>
|
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
|
|
|
*
|
2018-05-22 14:40:54 +03:00
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-05-22 14:40:54 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\Connector\Sabre;
|
|
|
|
|
|
|
|
use Sabre\DAV\CorePlugin;
|
|
|
|
use Sabre\DAV\FS\Directory;
|
|
|
|
use Sabre\DAV\ServerPlugin;
|
|
|
|
use Sabre\DAV\Tree;
|
|
|
|
use Sabre\HTTP\RequestInterface;
|
|
|
|
use Sabre\HTTP\ResponseInterface;
|
|
|
|
|
|
|
|
class AnonymousOptionsPlugin extends ServerPlugin {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Sabre\DAV\Server
|
|
|
|
*/
|
|
|
|
private $server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Sabre\DAV\Server $server
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function initialize(\Sabre\DAV\Server $server) {
|
|
|
|
$this->server = $server;
|
|
|
|
// before auth
|
|
|
|
$this->server->on('beforeMethod', [$this, 'handleAnonymousOptions'], 9);
|
|
|
|
}
|
|
|
|
|
2019-01-04 15:05:35 +03:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isRequestInRoot($path) {
|
|
|
|
return $path === '' || (is_string($path) && strpos($path, '/') === FALSE);
|
|
|
|
}
|
|
|
|
|
2018-05-22 14:40:54 +03:00
|
|
|
/**
|
|
|
|
* @throws \Sabre\DAV\Exception\Forbidden
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function handleAnonymousOptions(RequestInterface $request, ResponseInterface $response) {
|
2019-08-14 10:27:20 +03:00
|
|
|
$isOffice = preg_match('/Microsoft Office/i', $request->getHeader('User-Agent'));
|
2019-12-23 15:08:10 +03:00
|
|
|
$emptyAuth = $request->getHeader('Authorization') === null
|
|
|
|
|| $request->getHeader('Authorization') === ''
|
|
|
|
|| trim($request->getHeader('Authorization')) === 'Bearer';
|
|
|
|
$isAnonymousOption = $request->getMethod() === 'OPTIONS' && $emptyAuth;
|
|
|
|
$isOfficeHead = $request->getMethod() === 'HEAD' && $isOffice && $emptyAuth;
|
2019-08-14 10:27:20 +03:00
|
|
|
if ($isAnonymousOption || $isOfficeHead) {
|
2018-05-22 14:40:54 +03:00
|
|
|
/** @var CorePlugin $corePlugin */
|
|
|
|
$corePlugin = $this->server->getPlugin('core');
|
|
|
|
// setup a fake tree for anonymous access
|
|
|
|
$this->server->tree = new Tree(new Directory(''));
|
|
|
|
$corePlugin->httpOptions($request, $response);
|
2018-06-28 15:53:44 +03:00
|
|
|
$this->server->emit('afterMethod', [$request, $response]);
|
|
|
|
$this->server->emit('afterMethod:OPTIONS', [$request, $response]);
|
|
|
|
|
2018-05-22 14:40:54 +03:00
|
|
|
$this->server->sapi->sendResponse($response);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|