]> git.mxchange.org Git - friendica.git/blob - src/Module/Special/HTTPException.php
Display the publish time in the local timezone
[friendica.git] / src / Module / Special / HTTPException.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Special;
23
24 use Friendica\Core\Renderer;
25 use Friendica\Core\System;
26 use Friendica\DI;
27
28 /**
29  * This special module displays HTTPException when they are thrown in modules.
30  *
31  * @package Friendica\Module\Special
32  */
33 class HTTPException
34 {
35         /**
36          * Generates the necessary template variables from the caught HTTPException.
37          *
38          * Fills in the blanks if title or descriptions aren't provided by the exception.
39          *
40          * @param \Friendica\Network\HTTPException $e
41          * @return array ['$title' => ..., '$description' => ...]
42          */
43         private static function getVars(\Friendica\Network\HTTPException $e)
44         {
45                 $message = $e->getMessage();
46
47                 $titles = [
48                         200 => 'OK',
49                         400 => DI::l10n()->t('Bad Request'),
50                         401 => DI::l10n()->t('Unauthorized'),
51                         403 => DI::l10n()->t('Forbidden'),
52                         404 => DI::l10n()->t('Not Found'),
53                         500 => DI::l10n()->t('Internal Server Error'),
54                         503 => DI::l10n()->t('Service Unavailable'),
55                 ];
56                 $title = ($titles[$e->getCode()] ?? '') ?: 'Error ' . $e->getCode();
57
58                 if (empty($message)) {
59                         // Explanations are taken from https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
60                         $explanation = [
61                                 400 => DI::l10n()->t('The server cannot or will not process the request due to an apparent client error.'),
62                                 401 => DI::l10n()->t('Authentication is required and has failed or has not yet been provided.'),
63                                 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.'),
64                                 404 => DI::l10n()->t('The requested resource could not be found but may be available in the future.'),
65                                 500 => DI::l10n()->t('An unexpected condition was encountered and no more specific message is suitable.'),
66                                 503 => DI::l10n()->t('The server is currently unavailable (because it is overloaded or down for maintenance). Please try again later.'),
67                         ];
68
69                         $message = $explanation[$e->getCode()] ?? '';
70                 }
71
72                 $vars = [
73                         '$title' => $title,
74                         '$message' => $message,
75                         '$back' => DI::l10n()->t('Go back'),
76                         '$stack_trace' => DI::l10n()->t('Stack trace:'),
77                 ];
78
79                 if (is_site_admin()) {
80                         $vars['$thrown'] = DI::l10n()->t('Exception thrown in %s:%d', $e->getFile(), $e->getLine());
81                         $vars['$trace'] = $e->getTraceAsString();
82                 }
83
84                 return $vars;
85         }
86
87         /**
88          * Displays a bare message page with no theming at all.
89          *
90          * @param \Friendica\Network\HTTPException $e
91          * @throws \Exception
92          */
93         public static function rawContent(\Friendica\Network\HTTPException $e)
94         {
95                 $content = '';
96
97                 if ($e->getCode() >= 400) {
98                         $tpl = Renderer::getMarkupTemplate('http_status.tpl');
99                         $content = Renderer::replaceMacros($tpl, self::getVars($e));
100                 }
101
102                 System::httpExit($e->getCode(), $e->httpdesc, $content);
103         }
104
105         /**
106          * Returns a content string that can be integrated in the current theme.
107          *
108          * @param \Friendica\Network\HTTPException $e
109          * @return string
110          * @throws \Exception
111          */
112         public static function content(\Friendica\Network\HTTPException $e)
113         {
114                 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $e->getCode() . ' ' . $e->httpdesc);
115
116                 $tpl = Renderer::getMarkupTemplate('exception.tpl');
117
118                 return Renderer::replaceMacros($tpl, self::getVars($e));
119         }
120 }