Completed
Push — stable8.2 ( ec0a29...f5c39b )
by
unknown
18:21
created

remote.php ➔ handleException()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 41
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72
Metric Value
cc 8
eloc 29
nc 6
nop 1
dl 0
loc 41
ccs 0
cts 35
cp 0
crap 72
rs 5.3846
1
<?php
2
/**
3
 * @author Brice Maron <[email protected]>
4
 * @author Christopher Schäpers <[email protected]>
5
 * @author Jörn Friedrich Dreyer <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 * @author Robin McCorkell <[email protected]>
9
 * @author Thomas Müller <[email protected]>
10
 * @author Vincent Petry <[email protected]>
11
 *
12
 * @copyright Copyright (c) 2015, ownCloud, Inc.
13
 * @license AGPL-3.0
14
 *
15
 * This code is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License, version 3,
17
 * as published by the Free Software Foundation.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License, version 3,
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
26
 *
27
 */
28
29
use OC\Connector\Sabre\ExceptionLoggerPlugin;
30
use Sabre\DAV\Exception\ServiceUnavailable;
31
use Sabre\DAV\Server;
32
33
/**
34
 * Class RemoteException
35
 * Dummy exception class to be use locally to identify certain conditions
36
 * Will not be logged to avoid DoS
37
 */
38
class RemoteException extends Exception {
39
}
40
41
/**
42
 * @param Exception $e
43
 */
44
function handleException(Exception $e) {
45
	$request = \OC::$server->getRequest();
46
	// in case the request content type is text/xml - we assume it's a WebDAV request
47
	$isXmlContentType = strpos($request->getHeader('Content-Type'), 'text/xml');
48
	if ($isXmlContentType === 0) {
49
		// fire up a simple server to properly process the exception
50
		$server = new Server();
51
		if (!($e instanceof RemoteException)) {
52
			// we shall not log on RemoteException
53
			$server->addPlugin(new ExceptionLoggerPlugin('webdav', \OC::$server->getLogger()));
54
		}
55
		$server->on('beforeMethod', function () use ($e) {
56
			if ($e instanceof RemoteException) {
57
				switch ($e->getCode()) {
58
					case OC_Response::STATUS_SERVICE_UNAVAILABLE:
59
						throw new ServiceUnavailable($e->getMessage());
60
					case OC_Response::STATUS_NOT_FOUND:
61
						throw new \Sabre\DAV\Exception\NotFound($e->getMessage());
62
				}
63
			}
64
			$class = get_class($e);
65
			$msg = $e->getMessage();
66
			throw new ServiceUnavailable("$class: $msg");
67
		});
68
		$server->exec();
69
	} else {
70
		$statusCode = OC_Response::STATUS_INTERNAL_SERVER_ERROR;
71
		if ($e instanceof \OC\ServiceUnavailableException ) {
72
			$statusCode = OC_Response::STATUS_SERVICE_UNAVAILABLE;
73
		}
74
		if ($e instanceof RemoteException) {
75
			// we shall not log on RemoteException
76
			OC_Response::setStatus($e->getCode());
77
			OC_Template::printErrorPage($e->getMessage());
78
		} else {
79
			\OCP\Util::writeLog('remote', $e->getMessage(), \OCP\Util::FATAL);
80
			OC_Response::setStatus($statusCode);
81
			OC_Template::printExceptionErrorPage($e);
82
		}
83
	}
84
}
85
86
try {
87
	require_once 'lib/base.php';
88
89
	if (\OCP\Util::needUpgrade()) {
90
		// since the behavior of apps or remotes are unpredictable during
91
		// an upgrade, return a 503 directly
92
		throw new RemoteException('Service unavailable', OC_Response::STATUS_SERVICE_UNAVAILABLE);
93
	}
94
95
	$request = \OC::$server->getRequest();
96
	$pathInfo = $request->getPathInfo();
97
	if ($pathInfo === false || $pathInfo === '') {
98
		throw new RemoteException('Path not found', OC_Response::STATUS_NOT_FOUND);
99
	}
100
	if (!$pos = strpos($pathInfo, '/', 1)) {
101
		$pos = strlen($pathInfo);
102
	}
103
	$service=substr($pathInfo, 1, $pos-1);
104
105
	$file = \OC::$server->getConfig()->getAppValue('core', 'remote_' . $service);
106
107
	if(is_null($file)) {
108
		throw new RemoteException('Path not found', OC_Response::STATUS_NOT_FOUND);
109
	}
110
111
	// force language as given in the http request
112
	\OC_L10N::setLanguageFromRequest();
113
114
	$file=ltrim($file, '/');
115
116
	$parts=explode('/', $file, 2);
117
	$app=$parts[0];
118
119
	// Load all required applications
120
	\OC::$REQUESTEDAPP = $app;
121
	OC_App::loadApps(array('authentication'));
122
	OC_App::loadApps(array('filesystem', 'logging'));
123
124
	switch ($app) {
125
		case 'core':
126
			$file =  OC::$SERVERROOT .'/'. $file;
127
			break;
128
		default:
129
			if (!\OC::$server->getAppManager()->isInstalled($app)) {
130
				throw new RemoteException('App not installed: ' . $app);
131
			}
132
			OC_App::loadApp($app);
133
			$file = OC_App::getAppPath($app) .'/'. $parts[1];
134
			break;
135
	}
136
	$baseuri = OC::$WEBROOT . '/remote.php/'.$service.'/';
137
	require_once $file;
138
139
} catch (Exception $ex) {
140
	handleException($ex);
141
}
142