4 namespace Friendica\Module\Special;
6 use Friendica\Core\L10n;
7 use Friendica\Core\Logger;
8 use Friendica\Core\Renderer;
9 use Friendica\Core\System;
12 * This special module displays HTTPException when they are thrown in modules.
14 * @package Friendica\Module\Special
19 * Generates the necessary template variables from the caught HTTPException.
21 * Fills in the blanks if title or descriptions aren't provided by the exception.
23 * @param \Friendica\Network\HTTPException $e
24 * @return array ['$title' => ..., '$description' => ...]
26 private static function getVars(\Friendica\Network\HTTPException $e)
28 $message = $e->getMessage();
32 400 => L10n::t('Bad Request'),
33 401 => L10n::t('Unauthorized'),
34 403 => L10n::t('Forbidden'),
35 404 => L10n::t('Not Found'),
36 500 => L10n::t('Internal Server Error'),
37 503 => L10n::t('Service Unavailable'),
39 $title = ($titles[$e->getCode()] ?? '') ?: 'Error ' . $e->getCode();
41 if (empty($message)) {
42 // Explanations are taken from https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
44 400 => L10n::t('The server cannot or will not process the request due to an apparent client error.'),
45 401 => L10n::t('Authentication is required and has failed or has not yet been provided.'),
46 403 => L10n::t('The request was valid, but the server is refusing action. The user might not have the necessary permissions for a resource, or may need an account.'),
47 404 => L10n::t('The requested resource could not be found but may be available in the future.'),
48 500 => L10n::t('An unexpected condition was encountered and no more specific message is suitable.'),
49 503 => L10n::t('The server is currently unavailable (because it is overloaded or down for maintenance). Please try again later.'),
52 $message = $explanation[$e->getCode()] ?? '';
55 return ['$title' => $title, '$message' => $message, '$back' => L10n::t('Go back')];
59 * Displays a bare message page with no theming at all.
61 * @param \Friendica\Network\HTTPException $e
64 public static function rawContent(\Friendica\Network\HTTPException $e)
68 if ($e->getCode() >= 400) {
69 $tpl = Renderer::getMarkupTemplate('http_status.tpl');
70 $content = Renderer::replaceMacros($tpl, self::getVars($e));
73 System::httpExit($e->getCode(), $e->httpdesc, $content);
77 * Returns a content string that can be integrated in the current theme.
79 * @param \Friendica\Network\HTTPException $e
83 public static function content(\Friendica\Network\HTTPException $e)
85 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $e->getCode() . ' ' . $e->httpdesc);
87 $tpl = Renderer::getMarkupTemplate('exception.tpl');
89 return Renderer::replaceMacros($tpl, self::getVars($e));