friendica-github/src/Module/Special/HTTPException.php

134 lines
3.8 KiB
PHP
Raw Normal View History

<?php
2024-08-24 15:27:00 +02:00
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace Friendica\Module\Special;
2024-12-06 21:12:29 +00:00
use Friendica\App\Arguments;
use Friendica\App\Request;
2022-12-26 21:17:32 +01:00
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
2022-12-26 21:17:32 +01:00
use Friendica\Core\Session\Model\UserSession;
use Friendica\Core\System;
use Friendica\Module\Response;
2024-12-06 21:12:29 +00:00
use Friendica\Network\HTTPException as NetworkHTTPException;
2022-12-26 21:17:32 +01:00
use Psr\Log\LoggerInterface;
/**
* This special module displays HTTPException when they are thrown in modules.
*
* @package Friendica\Module\Special
*/
class HTTPException
{
2022-12-26 21:17:32 +01:00
/** @var L10n */
protected $l10n;
/** @var LoggerInterface */
protected $logger;
2024-12-06 21:12:29 +00:00
/** @var Arguments */
2022-12-26 21:17:32 +01:00
protected $args;
/** @var bool */
protected $isSiteAdmin;
/** @var array */
protected $server;
/** @var string */
protected $requestId;
2024-12-06 21:12:29 +00:00
public function __construct(L10n $l10n, LoggerInterface $logger, Arguments $args, UserSession $session, Request $request, array $server = [])
2022-12-26 21:17:32 +01:00
{
$this->logger = $logger;
$this->l10n = $l10n;
$this->args = $args;
$this->isSiteAdmin = $session->isSiteAdmin();
$this->server = $server;
$this->requestId = $request->getRequestId();
}
/**
* Generates the necessary template variables from the caught HTTPException.
*
* Fills in the blanks if title or descriptions aren't provided by the exception.
*
* @return array ['$title' => ..., '$description' => ...]
*/
2024-12-06 21:12:29 +00:00
private function getVars(NetworkHTTPException $e)
{
2021-10-29 23:21:07 +00:00
// Explanations are mostly taken from https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
$vars = [
2022-12-26 21:17:32 +01:00
'$title' => $e->getDescription() ?: 'Error ' . $e->getCode(),
'$message' => $e->getMessage() ?: $e->getExplanation(),
'$back' => $this->l10n->t('Go back'),
'$stack_trace' => $this->l10n->t('Stack trace:'),
'$request_id' => $this->requestId,
];
2022-12-26 21:17:32 +01:00
if ($this->isSiteAdmin) {
$vars['$thrown'] = $this->l10n->t('Exception thrown in %s:%d', $e->getFile(), $e->getLine());
$vars['$trace'] = $e->getTraceAsString();
}
return $vars;
}
/**
* Displays a bare message page with no theming at all.
*
2019-05-04 21:54:05 -04:00
* @throws \Exception
*/
2024-12-06 21:12:29 +00:00
public function rawContent(NetworkHTTPException $e)
{
$content = '';
if ($e->getCode() >= 400) {
2022-12-26 21:17:32 +01:00
$vars = $this->getVars($e);
try {
2022-12-26 21:17:32 +01:00
$tpl = Renderer::getMarkupTemplate('http_status.tpl');
$content = Renderer::replaceMacros($tpl, $vars);
} catch (\Exception $e) {
$vars = array_map('htmlentities', $vars);
$content = "<h1>{$vars['$title']}</h1><p>{$vars['$message']}</p>";
2022-12-26 21:17:32 +01:00
if ($this->isSiteAdmin) {
$content .= "<p>{$vars['$thrown']}</p>";
$content .= "<pre>{$vars['$trace']}</pre>";
}
}
}
// We can't use a constructor parameter for this response object because we
// are in an Exception context where we don't want an existing Response.
2024-12-06 21:12:29 +00:00
$reason = ($e instanceof NetworkHTTPException) ? $e->getDescription() : $e->getMessage();
$response = new Response();
2024-12-06 21:12:29 +00:00
$response->setStatus($e->getCode(), $reason);
$response->addContent($content);
System::echoResponse($response->generate());
System::exit();
}
/**
* Returns a content string that can be integrated in the current theme.
*
* @return string
2019-05-04 21:54:05 -04:00
* @throws \Exception
*/
2024-12-06 21:12:29 +00:00
public function content(NetworkHTTPException $e): string
{
2021-10-29 23:21:07 +00:00
if ($e->getCode() >= 400) {
2022-12-26 21:17:32 +01:00
$this->logger->debug('Exit with error',
[
'code' => $e->getCode(),
'description' => $e->getDescription(),
'query' => $this->args->getQueryString(),
'method' => $this->args->getMethod(),
'agent' => $this->server['HTTP_USER_AGENT'] ?? ''
]);
2021-10-29 23:21:07 +00:00
}
$tpl = Renderer::getMarkupTemplate('exception.tpl');
2022-12-26 21:17:32 +01:00
return Renderer::replaceMacros($tpl, $this->getVars($e));
}
}