3 namespace Friendica\Module\Special;
5 use Friendica\Core\Renderer;
6 use Friendica\Core\System;
10 * This special module displays HTTPException when they are thrown in modules.
12 * @package Friendica\Module\Special
17 * Generates the necessary template variables from the caught HTTPException.
19 * Fills in the blanks if title or descriptions aren't provided by the exception.
21 * @param \Friendica\Network\HTTPException $e
22 * @return array ['$title' => ..., '$description' => ...]
24 private static function getVars(\Friendica\Network\HTTPException $e)
26 $message = $e->getMessage();
30 400 => DI::l10n()->t('Bad Request'),
31 401 => DI::l10n()->t('Unauthorized'),
32 403 => DI::l10n()->t('Forbidden'),
33 404 => DI::l10n()->t('Not Found'),
34 500 => DI::l10n()->t('Internal Server Error'),
35 503 => DI::l10n()->t('Service Unavailable'),
37 $title = ($titles[$e->getCode()] ?? '') ?: 'Error ' . $e->getCode();
39 if (empty($message)) {
40 // Explanations are taken from https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
42 400 => DI::l10n()->t('The server cannot or will not process the request due to an apparent client error.'),
43 401 => DI::l10n()->t('Authentication is required and has failed or has not yet been provided.'),
44 403 => DI::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.'),
45 404 => DI::l10n()->t('The requested resource could not be found but may be available in the future.'),
46 500 => DI::l10n()->t('An unexpected condition was encountered and no more specific message is suitable.'),
47 503 => DI::l10n()->t('The server is currently unavailable (because it is overloaded or down for maintenance). Please try again later.'),
50 $message = $explanation[$e->getCode()] ?? '';
53 return ['$title' => $title, '$message' => $message, '$back' => DI::l10n()->t('Go back')];
57 * Displays a bare message page with no theming at all.
59 * @param \Friendica\Network\HTTPException $e
62 public static function rawContent(\Friendica\Network\HTTPException $e)
66 if ($e->getCode() >= 400) {
67 $tpl = Renderer::getMarkupTemplate('http_status.tpl');
68 $content = Renderer::replaceMacros($tpl, self::getVars($e));
71 System::httpExit($e->getCode(), $e->httpdesc, $content);
75 * Returns a content string that can be integrated in the current theme.
77 * @param \Friendica\Network\HTTPException $e
81 public static function content(\Friendica\Network\HTTPException $e)
83 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $e->getCode() . ' ' . $e->httpdesc);
85 $tpl = Renderer::getMarkupTemplate('exception.tpl');
87 return Renderer::replaceMacros($tpl, self::getVars($e));