diff --git a/index.php b/index.php index 75511de12e1..b66281e6449 100644 --- a/index.php +++ b/index.php @@ -45,8 +45,7 @@ try { \OC::$server->getLogger()->logException($ex, array('app' => 'index')); //show the user a detailed error page - OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE); - OC_Template::printExceptionErrorPage($ex); + OC_Template::printExceptionErrorPage($ex, \OC_Response::STATUS_SERVICE_UNAVAILABLE); } catch (\OC\HintException $ex) { try { OC_Template::printErrorPage($ex->getMessage(), $ex->getHint(), OC_Response::STATUS_SERVICE_UNAVAILABLE); @@ -55,8 +54,7 @@ try { \OC::$server->getLogger()->logException($ex2, array('app' => 'index')); //show the user a detailed error page - OC_Response::setStatus(OC_Response::STATUS_INTERNAL_SERVER_ERROR); - OC_Template::printExceptionErrorPage($ex); + OC_Template::printExceptionErrorPage($ex, \OC_Response::STATUS_INTERNAL_SERVER_ERROR); } } catch (\OC\User\LoginException $ex) { OC_Template::printErrorPage($ex->getMessage(), $ex->getMessage(), OC_Response::STATUS_FORBIDDEN); @@ -90,6 +88,5 @@ try { throw $e; } - OC_Response::setStatus(OC_Response::STATUS_INTERNAL_SERVER_ERROR); - OC_Template::printExceptionErrorPage($ex); + OC_Template::printExceptionErrorPage($ex, \OC_Response::STATUS_INTERNAL_SERVER_ERROR); } diff --git a/lib/base.php b/lib/base.php index 54cbc700ec3..60a4c03bc41 100644 --- a/lib/base.php +++ b/lib/base.php @@ -434,8 +434,7 @@ class OC { } catch (Exception $e) { \OC::$server->getLogger()->logException($e, ['app' => 'base']); //show the user a detailed error page - OC_Response::setStatus(OC_Response::STATUS_INTERNAL_SERVER_ERROR); - OC_Template::printExceptionErrorPage($e); + OC_Template::printExceptionErrorPage($e, \OC_Response::STATUS_INTERNAL_SERVER_ERROR); die(); } diff --git a/lib/private/legacy/template.php b/lib/private/legacy/template.php index 4e89b18c144..f84d6386deb 100644 --- a/lib/private/legacy/template.php +++ b/lib/private/legacy/template.php @@ -304,9 +304,10 @@ class OC_Template extends \OC\Template\Base { * Print a fatal error page and terminates the script * @param string $error_msg The error message to show * @param string $hint An optional hint message - needs to be properly escape + * @param int $statusCode * @suppress PhanAccessMethodInternal */ - public static function printErrorPage( $error_msg, $hint = '', $statusCode = \OC_Response::STATUS_INTERNAL_SERVER_ERROR ) { + public static function printErrorPage( $error_msg, $hint = '', $statusCode = 500) { if (\OC::$server->getAppManager()->isEnabledForUser('theming') && !\OC_App::isAppLoaded('theming')) { \OC_App::loadApp('theming'); } @@ -337,11 +338,12 @@ class OC_Template extends \OC\Template\Base { /** * print error page using Exception details * @param Exception|Throwable $exception - * @param bool $fetchPage + * @param int $statusCode * @return bool|string * @suppress PhanAccessMethodInternal */ - public static function printExceptionErrorPage($exception, $fetchPage = false) { + public static function printExceptionErrorPage($exception, $statusCode = 503) { + http_response_code($statusCode); try { $request = \OC::$server->getRequest(); $content = new \OC_Template('', 'exception', 'error', false); @@ -354,16 +356,12 @@ class OC_Template extends \OC\Template\Base { $content->assign('debugMode', \OC::$server->getSystemConfig()->getValue('debug', false)); $content->assign('remoteAddr', $request->getRemoteAddress()); $content->assign('requestID', $request->getId()); - if ($fetchPage) { - return $content->fetchPage(); - } $content->printPage(); } catch (\Exception $e) { $logger = \OC::$server->getLogger(); $logger->logException($exception, ['app' => 'core']); $logger->logException($e, ['app' => 'core']); - header(self::getHttpProtocol() . ' 500 Internal Server Error'); header('Content-Type: text/plain; charset=utf-8'); print("Internal Server Error\n\n"); print("The server encountered an internal error and was unable to complete your request.\n"); @@ -372,26 +370,4 @@ class OC_Template extends \OC\Template\Base { } die(); } - - /** - * This is only here to reduce the dependencies in case of an exception to - * still be able to print a plain error message. - * - * Returns the used HTTP protocol. - * - * @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0. - * @internal Don't use this - use AppFramework\Http\Request->getHttpProtocol instead - */ - protected static function getHttpProtocol() { - $claimedProtocol = strtoupper($_SERVER['SERVER_PROTOCOL']); - $validProtocols = [ - 'HTTP/1.0', - 'HTTP/1.1', - 'HTTP/2', - ]; - if(in_array($claimedProtocol, $validProtocols, true)) { - return $claimedProtocol; - } - return 'HTTP/1.1'; - } } diff --git a/public.php b/public.php index 42fd2e9f602..c81a6b5a960 100644 --- a/public.php +++ b/public.php @@ -79,16 +79,15 @@ try { } catch (Exception $ex) { if ($ex instanceof \OC\ServiceUnavailableException) { - OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE); + $status = OC_Response::STATUS_SERVICE_UNAVAILABLE; } else { - OC_Response::setStatus(OC_Response::STATUS_INTERNAL_SERVER_ERROR); + $status = OC_Response::STATUS_INTERNAL_SERVER_ERROR; } //show the user a detailed error page \OC::$server->getLogger()->logException($ex, ['app' => 'public']); - OC_Template::printExceptionErrorPage($ex); + OC_Template::printExceptionErrorPage($ex, $status); } catch (Error $ex) { //show the user a detailed error page - OC_Response::setStatus(OC_Response::STATUS_INTERNAL_SERVER_ERROR); \OC::$server->getLogger()->logException($ex, ['app' => 'public']); - OC_Template::printExceptionErrorPage($ex); + OC_Template::printExceptionErrorPage($ex, OC_Response::STATUS_INTERNAL_SERVER_ERROR); } diff --git a/remote.php b/remote.php index 38c349bd7bd..d90bb2d8ee5 100644 --- a/remote.php +++ b/remote.php @@ -80,8 +80,7 @@ function handleException($e) { OC_Template::printErrorPage($e->getMessage(), '', $e->getCode()); } else { \OC::$server->getLogger()->logException($e, ['app' => 'remote']); - OC_Response::setStatus($statusCode); - OC_Template::printExceptionErrorPage($e); + OC_Template::printExceptionErrorPage($e, $statusCode); } } }