diff options
Diffstat (limited to 'lib/private/legacy/OC_Template.php')
-rw-r--r-- | lib/private/legacy/OC_Template.php | 116 |
1 files changed, 60 insertions, 56 deletions
diff --git a/lib/private/legacy/OC_Template.php b/lib/private/legacy/OC_Template.php index 974f26fa381..5caa733b115 100644 --- a/lib/private/legacy/OC_Template.php +++ b/lib/private/legacy/OC_Template.php @@ -1,44 +1,14 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Bart Visscher <bartv@thisnet.nl> - * @author Brice Maron <brice@bmaron.net> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Frank Karlitschek <frank@karlitschek.de> - * @author Individual IT Services <info@individual-it.net> - * @author Jakob Sack <mail@jakobsack.de> - * @author Jan-Christoph Borchardt <hey@jancborchardt.net> - * @author Joas Schilling <coding@schilljs.com> - * @author John Molakvoæ <skjnldsv@protonmail.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> - * @author Julius Härtl <jus@bitgrid.net> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Marin Treselj <marin@pixelipo.com> - * @author Michael Letzgus <www@chronos.michael-letzgus.de> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <vincent@nextcloud.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ use OC\TemplateLayout; +use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; use OCP\AppFramework\Http\TemplateResponse; +use OCP\EventDispatcher\IEventDispatcher; require_once __DIR__.'/template/functions.php'; @@ -249,20 +219,44 @@ class OC_Template extends \OC\Template\Base { // If the hint is the same as the message there is no need to display it twice. $hint = ''; } + $errors = [['error' => $error_msg, 'hint' => $hint]]; http_response_code($statusCode); try { - $content = new \OC_Template('', 'error', 'error', false); - $errors = [['error' => $error_msg, 'hint' => $hint]]; - $content->assign('errors', $errors); - $content->printPage(); - } catch (\Exception $e) { + // Try rendering themed html error page + $response = new TemplateResponse( + '', + 'error', + ['errors' => $errors], + TemplateResponse::RENDER_AS_ERROR, + $statusCode, + ); + $event = new BeforeTemplateRenderedEvent(false, $response); + \OC::$server->get(IEventDispatcher::class)->dispatchTyped($event); + print($response->render()); + } catch (\Throwable $e1) { $logger = \OC::$server->getLogger(); - $logger->error("$error_msg $hint", ['app' => 'core']); - $logger->logException($e, ['app' => 'core']); + $logger->logException($e1, [ + 'app' => 'core', + 'message' => 'Rendering themed error page failed. Falling back to unthemed error page.' + ]); - header('Content-Type: text/plain; charset=utf-8'); - print("$error_msg $hint"); + try { + // Try rendering unthemed html error page + $content = new \OC_Template('', 'error', 'error', false); + $content->assign('errors', $errors); + $content->printPage(); + } catch (\Exception $e2) { + // If nothing else works, fall back to plain text error page + $logger->error("$error_msg $hint", ['app' => 'core']); + $logger->logException($e2, [ + 'app' => 'core', + 'message' => 'Rendering unthemed error page failed. Falling back to plain text error page.' + ]); + + header('Content-Type: text/plain; charset=utf-8'); + print("$error_msg $hint"); + } } die(); } @@ -275,8 +269,11 @@ class OC_Template extends \OC\Template\Base { * @suppress PhanAccessMethodInternal */ public static function printExceptionErrorPage($exception, $statusCode = 503) { + $debug = false; http_response_code($statusCode); try { + $debug = \OC::$server->getSystemConfig()->getValue('debug', false); + $serverLogsDocumentation = \OC::$server->getSystemConfig()->getValue('documentation_url.server_logs', ''); $request = \OC::$server->getRequest(); $content = new \OC_Template('', 'exception', 'error', false); $content->assign('errorClass', get_class($exception)); @@ -285,7 +282,8 @@ class OC_Template extends \OC\Template\Base { $content->assign('file', $exception->getFile()); $content->assign('line', $exception->getLine()); $content->assign('exception', $exception); - $content->assign('debugMode', \OC::$server->getSystemConfig()->getValue('debug', false)); + $content->assign('debugMode', $debug); + $content->assign('serverLogsDocumentation', $serverLogsDocumentation); $content->assign('remoteAddr', $request->getRemoteAddress()); $content->assign('requestID', $request->getId()); $content->printPage(); @@ -296,22 +294,28 @@ class OC_Template extends \OC\Template\Base { $logger->logException($e, ['app' => 'core']); } catch (Throwable $e) { // no way to log it properly - but to avoid a white page of death we send some output - 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"); - print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n"); - print("More details can be found in the server log.\n"); + self::printPlainErrorPage($e, $debug); // and then throw it again to log it at least to the web server error log throw $e; } - 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"); - print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n"); - print("More details can be found in the server log.\n"); + self::printPlainErrorPage($e, $debug); } die(); } + + private static function printPlainErrorPage(\Throwable $exception, bool $debug = false) { + 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"); + print("Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.\n"); + print("More details can be found in the server log.\n"); + + if ($debug) { + print("\n"); + print($exception->getMessage() . ' ' . $exception->getFile() . ' at ' . $exception->getLine() . "\n"); + print($exception->getTraceAsString()); + } + } } |