| 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; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  | 	 * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | 	public function isRequestInRoot($path) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 		return $path === '' || (is_string($path) && strpos($path, '/') === FALSE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	 * @throws \Sabre\DAV\Exception\Forbidden | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	public function handleAnonymousOptions(RequestInterface $request, ResponseInterface $response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		$isOffice = preg_match('/Microsoft Office/i', $request->getHeader('User-Agent')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$isAnonymousOption = ($request->getMethod() === 'OPTIONS' && ($request->getHeader('Authorization') === null || trim($request->getHeader('Authorization')) === 'Bearer') && $this->isRequestInRoot($request->getPath())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		$isOfficeHead = $request->getMethod() === 'HEAD' && $isOffice && $request->getHeader('Authorization') === 'Bearer'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		if ($isAnonymousOption || $isOfficeHead) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			/** @var CorePlugin $corePlugin */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			$corePlugin = $this->server->getPlugin('core'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			// setup a fake tree for anonymous access | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			$this->server->tree = new Tree(new Directory('')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			$corePlugin->httpOptions($request, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			$this->server->emit('afterMethod', [$request, $response]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			$this->server->emit('afterMethod:OPTIONS', [$request, $response]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			$this->server->sapi->sendResponse($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |  | 
            
                        
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: