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