]> git.mxchange.org Git - friendica.git/blob - src/Module/Special/HTTPException.php
cleanup namespace usages for L10n
[friendica.git] / src / Module / Special / HTTPException.php
1 <?php
2
3 namespace Friendica\Module\Special;
4
5 use Friendica\Core\Renderer;
6 use Friendica\Core\System;
7 use Friendica\DI;
8
9 /**
10  * This special module displays HTTPException when they are thrown in modules.
11  *
12  * @package Friendica\Module\Special
13  */
14 class HTTPException
15 {
16         /**
17          * Generates the necessary template variables from the caught HTTPException.
18          *
19          * Fills in the blanks if title or descriptions aren't provided by the exception.
20          *
21          * @param \Friendica\Network\HTTPException $e
22          * @return array ['$title' => ..., '$description' => ...]
23          */
24         private static function getVars(\Friendica\Network\HTTPException $e)
25         {
26                 $message = $e->getMessage();
27
28                 $titles = [
29                         200 => 'OK',
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'),
36                 ];
37                 $title = ($titles[$e->getCode()] ?? '') ?: 'Error ' . $e->getCode();
38
39                 if (empty($message)) {
40                         // Explanations are taken from https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
41                         $explanation = [
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.'),
48                         ];
49
50                         $message = $explanation[$e->getCode()] ?? '';
51                 }
52
53                 return ['$title' => $title, '$message' => $message, '$back' => DI::l10n()->t('Go back')];
54         }
55
56         /**
57          * Displays a bare message page with no theming at all.
58          *
59          * @param \Friendica\Network\HTTPException $e
60          * @throws \Exception
61          */
62         public static function rawContent(\Friendica\Network\HTTPException $e)
63         {
64                 $content = '';
65
66                 if ($e->getCode() >= 400) {
67                         $tpl = Renderer::getMarkupTemplate('http_status.tpl');
68                         $content = Renderer::replaceMacros($tpl, self::getVars($e));
69                 }
70
71                 System::httpExit($e->getCode(), $e->httpdesc, $content);
72         }
73
74         /**
75          * Returns a content string that can be integrated in the current theme.
76          *
77          * @param \Friendica\Network\HTTPException $e
78          * @return string
79          * @throws \Exception
80          */
81         public static function content(\Friendica\Network\HTTPException $e)
82         {
83                 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $e->getCode() . ' ' . $e->httpdesc);
84
85                 $tpl = Renderer::getMarkupTemplate('exception.tpl');
86
87                 return Renderer::replaceMacros($tpl, self::getVars($e));
88         }
89 }