]> git.mxchange.org Git - friendica.git/blob - src/Module/Special/HTTPException.php
Merge pull request #12459 from MrPetovan/bug/12454-link-preview-translation
[friendica.git] / src / Module / Special / HTTPException.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Logger;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\System;
27 use Friendica\DI;
28
29 /**
30  * This special module displays HTTPException when they are thrown in modules.
31  *
32  * @package Friendica\Module\Special
33  */
34 class HTTPException
35 {
36         /**
37          * Generates the necessary template variables from the caught HTTPException.
38          *
39          * Fills in the blanks if title or descriptions aren't provided by the exception.
40          *
41          * @param \Friendica\Network\HTTPException $e
42          * @return array ['$title' => ..., '$description' => ...]
43          */
44         private static function getVars(\Friendica\Network\HTTPException $e)
45         {
46                 // Explanations are mostly taken from https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
47                 $vars = [
48                         '$title' => $e->getDescription() ?: 'Error ' . $e->getCode(),
49                         '$message' => $e->getMessage() ?: $e->getExplanation(),
50                         '$back' => DI::l10n()->t('Go back'),
51                         '$stack_trace' => DI::l10n()->t('Stack trace:'),
52                 ];
53
54                 if (DI::app()->isSiteAdmin()) {
55                         $vars['$thrown'] = DI::l10n()->t('Exception thrown in %s:%d', $e->getFile(), $e->getLine());
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 function rawContent(\Friendica\Network\HTTPException $e)
69         {
70                 $content = '';
71
72                 if ($e->getCode() >= 400) {
73                         $vars = self::getVars($e);
74                         try {
75                                 $tpl = Renderer::getMarkupTemplate('http_status.tpl');
76                                 $content = Renderer::replaceMacros($tpl, $vars);
77                         } catch (\Exception $e) {
78                                 $content = "<h1>{$vars['$title']}</h1><p>{$vars['$message']}</p>";
79                                 if (DI::app()->isSiteAdmin()) {
80                                         $content .= "<p>{$vars['$thrown']}</p>";
81                                         $content .= "<pre>{$vars['$trace']}</pre>";
82                                 }
83                         }
84                 }
85
86                 System::httpError($e->getCode(), $e->getDescription(), $content);
87         }
88
89         /**
90          * Returns a content string that can be integrated in the current theme.
91          *
92          * @param \Friendica\Network\HTTPException $e
93          * @return string
94          * @throws \Exception
95          */
96         public function content(\Friendica\Network\HTTPException $e): string
97         {
98                 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $e->getCode() . ' ' . $e->getDescription());
99
100                 if ($e->getCode() >= 400) {
101                         Logger::debug('Exit with error', ['code' => $e->getCode(), 'description' => $e->getDescription(), 'query' => DI::args()->getQueryString(), 'callstack' => System::callstack(20), 'method' => DI::args()->getMethod(), 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
102                 }
103
104                 $tpl = Renderer::getMarkupTemplate('exception.tpl');
105
106                 return Renderer::replaceMacros($tpl, self::getVars($e));
107         }
108 }