Completed
Push — stable13 ( 389b98...394079 )
by Morris
20:43
created

AnonymousOptionsPlugin::handleAnonymousOptions()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\DAV\Connector\Sabre;
23
24
use Sabre\DAV\CorePlugin;
25
use Sabre\DAV\FS\Directory;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, OCA\DAV\Connector\Sabre\Directory.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
26
use Sabre\DAV\ServerPlugin;
27
use Sabre\DAV\Tree;
28
use Sabre\HTTP\RequestInterface;
29
use Sabre\HTTP\ResponseInterface;
30
31
class AnonymousOptionsPlugin extends ServerPlugin {
32
33
	/**
34
	 * @var \Sabre\DAV\Server
35
	 */
36
	private $server;
37
38
	/**
39
	 * @param \Sabre\DAV\Server $server
40
	 * @return void
41
	 */
42
	public function initialize(\Sabre\DAV\Server $server) {
43
		$this->server = $server;
44
		// before auth
45
		$this->server->on('beforeMethod', [$this, 'handleAnonymousOptions'], 9);
46
	}
47
48
	/**
49
	 * @throws \Sabre\DAV\Exception\Forbidden
50
	 * @return bool
51
	 */
52
	public function handleAnonymousOptions(RequestInterface $request, ResponseInterface $response) {
53
		if ($request->getMethod() === 'OPTIONS' && $request->getPath() === '') {
54
			/** @var CorePlugin $corePlugin */
55
			$corePlugin = $this->server->getPlugin('core');
56
			// setup a fake tree for anonymous access
57
			$this->server->tree = new Tree(new Directory(''));
58
			$corePlugin->httpOptions($request, $response);
59
			$this->server->emit('afterMethod', [$request, $response]);
60
			$this->server->emit('afterMethod:OPTIONS', [$request, $response]);
61
62
			$this->server->sapi->sendResponse($response);
63
			return false;
64
		}
65
	}
66
}
67