]> git.mxchange.org Git - friendica.git/blob - src/Module/Special/HTTPException.php
Merge pull request #8149 from annando/fix-warning
[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                 $vars = ['$title' => $title, '$message' => $message, '$back' => DI::l10n()->t('Go back')];
54
55                 if (is_site_admin()) {
56                         $vars['$trace'] = $e->getTraceAsString();
57                 }
58
59                 return $vars;
60         }
61
62         /**
63          * Displays a bare message page with no theming at all.
64          *
65          * @param \Friendica\Network\HTTPException $e
66          * @throws \Exception
67          */
68         public static function rawContent(\Friendica\Network\HTTPException $e)
69         {
70                 $content = '';
71
72                 if ($e->getCode() >= 400) {
73                         $tpl = Renderer::getMarkupTemplate('http_status.tpl');
74                         $content = Renderer::replaceMacros($tpl, self::getVars($e));
75                 }
76
77                 System::httpExit($e->getCode(), $e->httpdesc, $content);
78         }
79
80         /**
81          * Returns a content string that can be integrated in the current theme.
82          *
83          * @param \Friendica\Network\HTTPException $e
84          * @return string
85          * @throws \Exception
86          */
87         public static function content(\Friendica\Network\HTTPException $e)
88         {
89                 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $e->getCode() . ' ' . $e->httpdesc);
90
91                 $tpl = Renderer::getMarkupTemplate('exception.tpl');
92
93                 return Renderer::replaceMacros($tpl, self::getVars($e));
94         }
95 }