]> git.mxchange.org Git - friendica.git/blob - src/Module/Special/HTTPException.php
Issue 11566: More detailled notification configuration
[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                         $tpl = Renderer::getMarkupTemplate('http_status.tpl');
74                         $content = Renderer::replaceMacros($tpl, self::getVars($e));
75                 }
76
77                 System::httpError($e->getCode(), $e->getDescription(), $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 function content(\Friendica\Network\HTTPException $e): string
88         {
89                 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $e->getCode() . ' ' . $e->getDescription());
90
91                 if ($e->getCode() >= 400) {
92                         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'] ?? '']);
93                 }
94
95                 $tpl = Renderer::getMarkupTemplate('exception.tpl');
96
97                 return Renderer::replaceMacros($tpl, self::getVars($e));
98         }
99 }