3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Special;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\System;
30 * This special module displays HTTPException when they are thrown in modules.
32 * @package Friendica\Module\Special
37 * Generates the necessary template variables from the caught HTTPException.
39 * Fills in the blanks if title or descriptions aren't provided by the exception.
41 * @param \Friendica\Network\HTTPException $e
42 * @return array ['$title' => ..., '$description' => ...]
44 private static function getVars(\Friendica\Network\HTTPException $e)
46 // Explanations are mostly taken from https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
48 '$title' => $e->getDescription() ?: 'Error ' . $e->getCode(),
49 '$message' => $e->getMessage() ?: $e->getExplanation(),
50 '$back' => DI::l10n()->t('Go back'),
51 '$stack_trace' => DI::l10n()->t('Stack trace:'),
54 if (DI::app()->isSiteAdmin()) {
55 $vars['$thrown'] = DI::l10n()->t('Exception thrown in %s:%d', $e->getFile(), $e->getLine());
56 $vars['$trace'] = $e->getTraceAsString();
63 * Displays a bare message page with no theming at all.
65 * @param \Friendica\Network\HTTPException $e
68 public function rawContent(\Friendica\Network\HTTPException $e)
72 if ($e->getCode() >= 400) {
73 $tpl = Renderer::getMarkupTemplate('http_status.tpl');
74 $content = Renderer::replaceMacros($tpl, self::getVars($e));
77 System::httpError($e->getCode(), $e->getDescription(), $content);
81 * Returns a content string that can be integrated in the current theme.
83 * @param \Friendica\Network\HTTPException $e
87 public function content(\Friendica\Network\HTTPException $e): string
89 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $e->getCode() . ' ' . $e->getDescription());
91 if ($e->getCode() >= 400) {
92 Logger::debug('Exit with error', ['code' => $e->getCode(), 'description' => $e->getDescription(), 'query' => DI::args()->getQueryString(), 'callstack' => System::callstack(20), 'method' => DI::args()->getMethod(), 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
95 $tpl = Renderer::getMarkupTemplate('exception.tpl');
97 return Renderer::replaceMacros($tpl, self::getVars($e));