Completed
Push — stable8.1 ( 7a1b8f...6080a3 )
by
unknown
107:52
created

remote.php ➔ handleException()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 37
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56
Metric Value
cc 7
eloc 28
nc 5
nop 1
dl 0
loc 37
ccs 0
cts 33
cp 0
crap 56
rs 6.7272
1
<?php
2
/**
3
 * @author Brice Maron <[email protected]>
4
 * @author Christopher Schäpers <[email protected]>
5
 * @author Georg Ehrke <[email protected]>
6
 * @author Jörn Friedrich Dreyer <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Robin Appelman <[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
 */
37
class RemoteException extends Exception {
38
}
39
40
/**
41
 * @param Exception $e
42
 */
43
function handleException(Exception $e) {
44
	$request = \OC::$server->getRequest();
45
	// in case the request content type is text/xml - we assume it's a WebDAV request
46
	$isXmlContentType = strpos($request->getHeader('Content-Type'), 'text/xml');
47
	if ($isXmlContentType === 0) {
48
		// fire up a simple server to properly process the exception
49
		$server = new Server();
50
		$server->addPlugin(new ExceptionLoggerPlugin('webdav', \OC::$server->getLogger()));
51
		$server->on('beforeMethod', function () use ($e) {
52
			if ($e instanceof RemoteException) {
53
				switch ($e->getCode()) {
54
					case OC_Response::STATUS_SERVICE_UNAVAILABLE:
55
						throw new ServiceUnavailable($e->getMessage());
56
					case OC_Response::STATUS_NOT_FOUND:
57
						throw new \Sabre\DAV\Exception\NotFound($e->getMessage());
58
				}
59
			}
60
			$class = get_class($e);
61
			$msg = $e->getMessage();
62
			throw new ServiceUnavailable("$class: $msg");
63
		});
64
		$server->exec();
65
	} else {
66
		$statusCode = OC_Response::STATUS_INTERNAL_SERVER_ERROR;
67
		if ($e instanceof \OC\ServiceUnavailableException ) {
68
			$statusCode = OC_Response::STATUS_SERVICE_UNAVAILABLE;
69
		}
70
		\OCP\Util::writeLog('remote', $e->getMessage(), \OCP\Util::FATAL);
71
		if ($e instanceof RemoteException) {
72
			OC_Response::setStatus($e->getCode());
73
			OC_Template::printErrorPage($e->getMessage());
74
		} else {
75
			OC_Response::setStatus($statusCode);
76
			OC_Template::printExceptionErrorPage($e);
77
		}
78
	}
79
}
80
81
try {
82
	require_once 'lib/base.php';
83
84
	if (\OCP\Util::needUpgrade()) {
85
		// since the behavior of apps or remotes are unpredictable during
86
		// an upgrade, return a 503 directly
87
		throw new RemoteException('Service unavailable', OC_Response::STATUS_SERVICE_UNAVAILABLE);
88
	}
89
90
	$request = \OC::$server->getRequest();
91
	$pathInfo = $request->getPathInfo();
92
	if ($pathInfo === false || $pathInfo === '') {
93
		throw new RemoteException('Path not found', OC_Response::STATUS_NOT_FOUND);
94
	}
95
	if (!$pos = strpos($pathInfo, '/', 1)) {
96
		$pos = strlen($pathInfo);
97
	}
98
	$service=substr($pathInfo, 1, $pos-1);
99
100
	$file = \OC::$server->getConfig()->getAppValue('core', 'remote_' . $service);
101
102
	if(is_null($file)) {
103
		throw new RemoteException('Path not found', OC_Response::STATUS_NOT_FOUND);
104
	}
105
106
	// force language as given in the http request
107
	\OC_L10N::setLanguageFromRequest();
108
109
	$file=ltrim($file, '/');
110
111
	$parts=explode('/', $file, 2);
112
	$app=$parts[0];
113
114
	// Load all required applications
115
	\OC::$REQUESTEDAPP = $app;
116
	OC_App::loadApps(array('authentication'));
117
	OC_App::loadApps(array('filesystem', 'logging'));
118
119
	switch ($app) {
120
		case 'core':
121
			$file =  OC::$SERVERROOT .'/'. $file;
122
			break;
123
		default:
124
			if (!\OC::$server->getAppManager()->isInstalled($app)) {
125
				throw new Exception('App not installed: ' . $app);
126
			}
127
			OC_App::loadApp($app);
128
			$file = OC_App::getAppPath($app) .'/'. $parts[1];
129
			break;
130
	}
131
	$baseuri = OC::$WEBROOT . '/remote.php/'.$service.'/';
132
	require_once $file;
133
134
} catch (Exception $ex) {
135
	handleException($ex);
136
}
137