Merge pull request #7068 from MrPetovan/task/7047-theme-error-page

Themed error page redux
This commit is contained in:
Philipp 2019-05-02 21:15:29 +02:00 committed by GitHub
commit 2628da422a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
71 changed files with 450 additions and 371 deletions

View file

@ -24,7 +24,7 @@ class Attach extends BaseModule
{
$a = self::getApp();
if ($a->argc != 2) {
System::httpExit(400); // Bad Request.
throw new \Friendica\Network\HTTPException\BadRequestException();
}
// @TODO: Replace with parameter from router
@ -33,19 +33,19 @@ class Attach extends BaseModule
// Check for existence
$item = MAttach::exists(['id' => $item_id]);
if ($item === false) {
System::httpExit(404, ['description' => L10n::t('Item was not found.')]);
throw new \Friendica\Network\HTTPException\NotFoundException(L10n::t('Item was not found.'));
}
// Now we'll fetch the item, if we have enough permisson
$item = MAttach::getByIdWithPermission($item_id);
if ($item === false) {
System::httpExit(403, ['description' => L10n::t('Permission denied.')]);
throw new \Friendica\Network\HTTPException\ForbiddenException(L10n::t('Permission denied.'));
}
$data = MAttach::getData($item);
if (is_null($data)) {
Logger::log('NULL data for attachment with id ' . $item['id']);
System::httpExit(404, ['description' => L10n::t('Item was not found.')]);
throw new \Friendica\Network\HTTPException\NotFoundException(L10n::t('Item was not found.'));
}
// Use quotes around the filename to prevent a "multiple Content-Disposition"

View file

@ -33,7 +33,7 @@ class Feed extends BaseModule
// @TODO: Replace with parameter from router
if ($a->argc < 2) {
System::httpExit(400);
throw new \Friendica\Network\HTTPException\BadRequestException();
}
$type = null;

View file

@ -20,13 +20,13 @@ class Followers extends BaseModule
// @TODO: Replace with parameter from router
if (empty($a->argv[1])) {
System::httpExit(404);
throw new \Friendica\Network\HTTPException\NotFoundException();
}
// @TODO: Replace with parameter from router
$owner = User::getOwnerDataByNick($a->argv[1]);
if (empty($owner)) {
System::httpExit(404);
throw new \Friendica\Network\HTTPException\NotFoundException();
}
$page = defaults($_REQUEST, 'page', null);

View file

@ -20,13 +20,13 @@ class Following extends BaseModule
// @TODO: Replace with parameter from router
if (empty($a->argv[1])) {
System::httpExit(404);
throw new \Friendica\Network\HTTPException\NotFoundException();
}
// @TODO: Replace with parameter from router
$owner = User::getOwnerDataByNick($a->argv[1]);
if (empty($owner)) {
System::httpExit(404);
throw new \Friendica\Network\HTTPException\NotFoundException();
}
$page = defaults($_REQUEST, 'page', null);

View file

@ -137,7 +137,7 @@ class Group extends BaseModule
$change = false;
if (!local_user()) {
System::httpExit(403);
throw new \Friendica\Network\HTTPException\ForbiddenException();
}
$a = self::getApp();
@ -276,7 +276,7 @@ class Group extends BaseModule
}
if (!isset($group)) {
System::httpExit(400);
throw new \Friendica\Network\HTTPException\BadRequestException();
}
$groupeditor = [

View file

@ -25,7 +25,7 @@ class Inbox extends BaseModule
$postdata = file_get_contents('php://input');
if (empty($postdata)) {
System::httpExit(400);
throw new \Friendica\Network\HTTPException\BadRequestException();
}
if (Config::get('debug', 'ap_inbox_log')) {
@ -43,7 +43,7 @@ class Inbox extends BaseModule
if (!empty($a->argv[1])) {
$user = DBA::selectFirst('user', ['uid'], ['nickname' => $a->argv[1]]);
if (!DBA::isResult($user)) {
System::httpExit(404);
throw new \Friendica\Network\HTTPException\NotFoundException();
}
$uid = $user['uid'];
} else {
@ -52,6 +52,6 @@ class Inbox extends BaseModule
ActivityPub\Receiver::processInbox($postdata, $_SERVER, $uid);
System::httpExit(202);
throw new \Friendica\Network\HTTPException\AcceptedException();
}
}

View file

@ -51,7 +51,7 @@ class Install extends BaseModule
$a = self::getApp();
if (!$a->getMode()->isInstall()) {
Core\System::httpExit(403);
throw new \Friendica\Network\HTTPException\ForbiddenException();
}
// route: install/testrwrite
@ -59,7 +59,7 @@ class Install extends BaseModule
// @TODO: Replace with parameter from router
if ($a->getArgumentValue(1, '') == 'testrewrite') {
// Status Code 204 means that it worked without content
Core\System::httpExit(204);
throw new \Friendica\Network\HTTPException\NoContentException();
}
self::$installer = new Core\Installer();

View file

@ -18,7 +18,7 @@ class NodeInfo extends BaseModule
$config = self::getApp()->getConfig();
if (!$config->get('system', 'nodeinfo')) {
System::httpExit(404);
throw new \Friendica\Network\HTTPException\NotFoundException();
}
}
@ -41,14 +41,14 @@ class NodeInfo extends BaseModule
*
* @param App $app
*
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \Friendica\Network\HTTPException\NotFoundException
*/
private static function printWellKnown(App $app)
{
$config = $app->getConfig();
if (!$config->get('system', 'nodeinfo')) {
System::httpExit(404);
throw new \Friendica\Network\HTTPException\NotFoundException();
}
$nodeinfo = [

View file

@ -20,7 +20,7 @@ class Objects extends BaseModule
$a = self::getApp();
if (empty($a->argv[1])) {
System::httpExit(404);
throw new \Friendica\Network\HTTPException\NotFoundException();
}
if (!ActivityPub::isRequest()) {
@ -38,7 +38,7 @@ class Objects extends BaseModule
// @TODO: Replace with parameter from router
$item = Item::selectFirst(['id', 'author-link'], ['guid' => $a->argv[1], 'private' => false]);
if (!DBA::isResult($item) || !strstr($item['author-link'], System::baseUrl())) {
System::httpExit(404);
throw new \Friendica\Network\HTTPException\NotFoundException();
}
}

View file

@ -20,12 +20,12 @@ class Outbox extends BaseModule
// @TODO: Replace with parameter from router
if (empty($a->argv[1])) {
System::httpExit(404);
throw new \Friendica\Network\HTTPException\NotFoundException();
}
$owner = User::getOwnerDataByNick($a->argv[1]);
if (empty($owner)) {
System::httpExit(404);
throw new \Friendica\Network\HTTPException\NotFoundException();
}
$page = defaults($_REQUEST, 'page', null);

View file

@ -0,0 +1,15 @@
<?php
namespace Friendica\Module;
use Friendica\BaseModule;
use Friendica\Core\L10n;
use Friendica\Network\HTTPException;
class PageNotFound extends BaseModule
{
public static function content()
{
throw new HTTPException\NotFoundException(L10n::t('Page not found.'));
}
}

View file

@ -6,6 +6,7 @@
namespace Friendica\Module;
use Friendica\BaseModule;
use Friendica\Core\L10n;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Model\Photo as MPhoto;
@ -27,7 +28,7 @@ class Photo extends BaseModule
$a = self::getApp();
// @TODO: Replace with parameter from router
if ($a->argc <= 1 || $a->argc > 4) {
System::httpExit(400);
throw new \Friendica\Network\HTTPException\BadRequestException();
}
if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
@ -74,9 +75,7 @@ class Photo extends BaseModule
}
if ($photo === false) {
// not using System::httpExit() because we don't want html here.
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found" , true, 404);
exit();
System::httpExit('404', 'Not Found');
}
$cacheable = ($photo["allow_cid"] . $photo["allow_gid"] . $photo["deny_cid"] . $photo["deny_gid"] === "") && (isset($photo["cacheable"]) ? $photo["cacheable"] : true);
@ -85,7 +84,7 @@ class Photo extends BaseModule
if (is_null($img) || !$img->isValid()) {
Logger::log("Invalid photo with id {$photo["id"]}.");
System::httpExit(500, ["description" => "Invalid photo with id {$photo["id"]}."]);
throw new \Friendica\Network\HTTPException\InternalServerErrorException(L10n::t('Invalid photo with id %s.', $photo["id"]));
}
// if customsize is set and image is not a gif, resize it

View file

@ -38,7 +38,7 @@ class Profile extends BaseModule
// @TODO: Replace with parameter from router
if ($a->argc < 2) {
System::httpExit(400);
throw new \Friendica\Network\HTTPException\BadRequestException();
}
self::$which = filter_var($a->argv[1], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK);

View file

@ -70,7 +70,7 @@ class Proxy extends BaseModule
$request = self::getRequestInfo();
if (empty($request['url'])) {
System::httpExit(400, ['title' => L10n::t('Bad Request.')]);
throw new \Friendica\Network\HTTPException\BadRequestException();
}
// Webserver already tried direct cache...

View file

@ -0,0 +1,91 @@
<?php
namespace Friendica\Module\Special;
use Friendica\Core\L10n;
use Friendica\Core\Logger;
use Friendica\Core\Renderer;
use Friendica\Core\System;
/**
* This special module displays HTTPException when they are thrown in modules.
*
* @package Friendica\Module\Special
*/
class HTTPException
{
/**
* Generates the necessary template variables from the caught HTTPException.
*
* Fills in the blanks if title or descriptions aren't provided by the exception.
*
* @param \Friendica\Network\HTTPException $e
* @return array ['$title' => ..., '$description' => ...]
*/
private static function getVars(\Friendica\Network\HTTPException $e)
{
$message = $e->getMessage();
$titles = [
200 => 'OK',
400 => L10n::t('Bad Request'),
401 => L10n::t('Unauthorized'),
403 => L10n::t('Forbidden'),
404 => L10n::t('Not Found'),
500 => L10n::t('Internal Server Error'),
503 => L10n::t('Service Unavailable'),
];
$title = defaults($titles, $e->getCode(), 'Error ' . $e->getCode());
if (empty($message)) {
// Explanations are taken from https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
$explanation = [
400 => L10n::t('The server cannot or will not process the request due to an apparent client error.'),
401 => L10n::t('Authentication is required and has failed or has not yet been provided.'),
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.'),
404 => L10n::t('The requested resource could not be found but may be available in the future.'),
500 => L10n::t('An unexpected condition was encountered and no more specific message is suitable.'),
503 => L10n::t('The server is currently unavailable (because it is overloaded or down for maintenance). Please try again later.'),
];
$message = defaults($explanation, $e->getCode(), '');
}
return ['$title' => $title, '$description' => $message];
}
/**
* Displays a bare message page with no theming at all.
*
* @param \Friendica\Network\HTTPException $e
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function rawContent(\Friendica\Network\HTTPException $e)
{
$content = '';
if ($e->getCode() >= 400) {
$tpl = Renderer::getMarkupTemplate('http_status.tpl');
$content = Renderer::replaceMacros($tpl, self::getVars($e));
}
System::httpExit($e->getCode(), $e->httpdesc, $content);
}
/**
* Returns a content string that can be integrated in the current theme.
*
* @param \Friendica\Network\HTTPException $e
* @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function content(\Friendica\Network\HTTPException $e)
{
header($_SERVER["SERVER_PROTOCOL"] . ' ' . $e->getCode() . ' ' . $e->httpdesc);
$tpl = Renderer::getMarkupTemplate('exception.tpl');
return Renderer::replaceMacros($tpl, self::getVars($e));
}
}

View file

@ -13,7 +13,7 @@ class Statistics extends BaseModule
$config = self::getApp()->getConfig();
if (!$config->get("system", "nodeinfo")) {
System::httpExit(404);
throw new \Friendica\Network\HTTPException\NotFoundException();
}
}

View file

@ -5,7 +5,6 @@ namespace Friendica\Module;
use Friendica\BaseModule;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Core\System;
use Friendica\Network\Probe;
/**
@ -13,22 +12,14 @@ use Friendica\Network\Probe;
*/
class WebFinger extends BaseModule
{
public static function init()
{
if (!local_user()) {
System::httpExit(
403,
[
'title' => L10n::t('Public access denied.'),
'description' => L10n::t('Only logged in users are permitted to perform a probing.'),
],
);
exit();
}
}
public static function content()
{
if (!local_user()) {
$e = new \Friendica\Network\HTTPException\ForbiddenException(L10n::t("Only logged in users are permitted to perform a probing."));
$e->httpdesc = L10n::t("Public access denied.");
throw $e;
}
$app = self::getApp();
$addr = defaults($_GET, 'addr', '');

View file

@ -58,7 +58,7 @@ class Xrd extends BaseModule
$user = User::getByNickname($name);
if (empty($user)) {
System::httpExit(404);
throw new \Friendica\Network\HTTPException\NotFoundException();
}
$profileURL = $app->getBaseURL() . '/profile/' . $user['nickname'];