1
0
mirror of https://github.com/e107inc/e107.git synced 2025-07-31 03:40:37 +02:00

Introduce e107::setErrorPage() for delivering an error page to the browser.

This commit is contained in:
lonalore
2016-12-15 10:06:54 +01:00
parent d9f3d487f7
commit 2667537fd1
4 changed files with 305 additions and 199 deletions

View File

@@ -48,28 +48,7 @@ class core_system_error_controller extends eController
*/
public function actionForbidden()
{
$response = $this->getResponse();
$response->setRenderMod('error403');
$response->addHeader('HTTP/1.0 403 Forbidden');
$tp = e107::getParser();
$tpl = e107::getCoreTemplate('error', '403');
$sc = e107::getScBatch('error');
$title = LAN_ERROR_TITLE;
$subtitle = LAN_ERROR_4;
$caption = LAN_ERROR_45;
$content = LAN_ERROR_5 . '<br/>' . LAN_ERROR_6 . '<br/><br/>' . LAN_ERROR_2;
$sc->setVars(array(
'title' => $title,
'subtitle' => $subtitle,
'caption' => $caption,
'content' => $content,
));
$body = $tp->parseTemplate($tpl, true, $sc);
$this->addBody($body);
e107::setErrorPage(403);
}
/**
@@ -77,28 +56,7 @@ class core_system_error_controller extends eController
*/
public function actionNotfound()
{
$response = $this->getResponse();
$response->setRenderMod('error404');
$response->addHeader('HTTP/1.0 404 Not Found');
$tp = e107::getParser();
$tpl = e107::getCoreTemplate('error', '404');
$sc = e107::getScBatch('error');
$title = LAN_ERROR_TITLE;
$subtitle = LAN_ERROR_7;
$caption = LAN_ERROR_45;
$content = LAN_ERROR_21 . '<br/>' . LAN_ERROR_9;
$sc->setVars(array(
'title' => $title,
'subtitle' => $subtitle,
'caption' => $caption,
'content' => $content,
));
$body = $tp->parseTemplate($tpl, true, $sc);
$this->addBody($body);
e107::setErrorPage(404);
}
}

View File

@@ -216,7 +216,8 @@ class e107
'validatorClass' => '{e_HANDLER}validator_class.php',
'xmlClass' => '{e_HANDLER}xml_class.php',
'e107MailManager' => '{e_HANDLER}mail_manager_class.php',
'e_library_manager' => '{e_HANDLER}library_manager.php'
'e_library_manager' => '{e_HANDLER}library_manager.php',
'error_page' => '{e_HANDLER}error_page_class.php',
);
/**
@@ -3059,6 +3060,53 @@ class e107
}
/**
* Sends error page contents to the browser as HTML.
*
* @param int $status_code
* The HTTP status code to use for the error page, defaults to 404.
* Status codes are defined in RFC 2616.
* @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
*
* @return void
*/
public static function setErrorPage($status_code = 404)
{
if(!defined('ERR_PAGE_ACTIVE'))
{
define("ERR_PAGE_ACTIVE", true);
}
$errorPage = self::getSingleton('error_page', true);
switch ($status_code)
{
case 400:
$errorPage->deliverPageBadRequest();
break;
case 401:
$errorPage->deliverPageUnauthorized();
break;
case 403:
$errorPage->deliverPageForbidden();
break;
case 404:
$errorPage->deliverPageNotFound();
break;
case 500:
$errorPage->deliverPageInternalServerError();
break;
default:
$errorPage->deliverPageUnknown();
break;
}
}
/**
* Parses an array into a valid, rawurlencoded query string. This differs from http_build_query() as we need to

View File

@@ -0,0 +1,172 @@
<?php
/**
* e107 website system
*
* Copyright (C) 2008-2016 e107 Inc (e107.org)
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
* @file
* Class for system error pages.
*/
// [e_LANGUAGEDIR]/[e_LANGUAGE]/lan_error.php
e107::lan('core', 'error');
/**
* Class error_page.
*/
class error_page
{
/**
* @var
*/
private $statusCode;
/**
* Constructor.
*
* Use {@link getInstance()}, direct instantiating is not possible for
* signleton objects.
*/
public function __construct()
{
}
/**
* Cloning is not allowed.
*/
private function __clone()
{
}
/**
* @return void
*/
protected function _init()
{
}
/**
* Delivers a "Bad Request" error page to the browser.
*/
public function deliverPageBadRequest()
{
header('HTTP/1.1 400 Bad Request', true, 400);
$title = LAN_ERROR_35; // Error 400 - Bad Request
$caption = LAN_ERROR_45;
$content = LAN_ERROR_36 . '<br/>' . LAN_ERROR_3;
$this->statusCode = 400;
$this->renderPage($title, $caption, $content);
}
/**
* Delivers a "Authentication Failed" error page to the browser.
*/
public function deliverPageUnauthorized()
{
header('HTTP/1.1 401 Unauthorized', true, 401);
$title = LAN_ERROR_1; // Error 401 - Authentication Failed
$caption = LAN_ERROR_45;
$content = LAN_ERROR_2 . '<br/>' . LAN_ERROR_3;
$this->statusCode = 401;
$this->renderPage($title, $caption, $content);
}
/**
* Delivers a "Access forbidden" error page to the browser.
*/
public function deliverPageForbidden()
{
header('HTTP/1.1 403 Forbidden', true, 403);
$title = LAN_ERROR_4; // Error 403 - Access forbidden
$caption = LAN_ERROR_45;
$content = LAN_ERROR_5 . '<br/>' . LAN_ERROR_6 . '<br/><br/>' . LAN_ERROR_2;
$this->statusCode = 403;
$this->renderPage($title, $caption, $content);
}
/**
* Delivers a "Not Found" error page to the browser.
*/
public function deliverPageNotFound()
{
header('HTTP/1.1 404 Not Found', true, 404);
$title = LAN_ERROR_7; // Error 404 - Document Not Found
$caption = LAN_ERROR_45;
$content = LAN_ERROR_21 . '<br/>' . LAN_ERROR_9;
$this->statusCode = 404;
$this->renderPage($title, $caption, $content);
}
/**
* Delivers a "Internal server error" error page to the browser.
*/
public function deliverPageInternalServerError()
{
header('HTTP/1.1 500 Internal Server Error', true, 500);
$title = LAN_ERROR_10; // Error 500 - Internal server error
$caption = LAN_ERROR_14;
$content = LAN_ERROR_11 . '<br/>' . LAN_ERROR_12;
$this->statusCode = 500;
$this->renderPage($title, $caption, $content);
}
/**
* Delivers a "Unknown" error page to the browser.
*/
public function deliverPageUnknown()
{
header('HTTP/1.1 501 Not Implemented', true, 501);
$errorQuery = htmlentities($_SERVER['QUERY_STRING']);
$title = LAN_ERROR_13 . ' (' . $errorQuery . ')'; // Error - Unknown
$caption = LAN_ERROR_14;
$content = LAN_ERROR_15;
$this->statusCode = 'DEFAULT'; // Use default template.
$this->renderPage($title, $caption, $content);
}
/**
* Renders and delivers an error page to the browser.
*
* @param $title
* Page title.
* @param $caption
* Title for info panel.
* @param $content
* Content for info panel.
*/
private function renderPage($title, $caption, $content)
{
$tp = e107::getParser();
$tpl = e107::getCoreTemplate('error', $this->statusCode);
$sc = e107::getScBatch('error');
$sc->setVars(array(
'title' => LAN_ERROR_TITLE, // Oops!
'subtitle' => $title,
'caption' => $caption,
'content' => $content,
));
$body = $tp->parseTemplate($tpl, true, $sc);
e107::getRender()->tablerender('', $body);
}
}

View File

@@ -21,15 +21,6 @@ $_E107 = array(
require_once("class2.php");
// Start session if required.
if(!session_id())
{
session_start();
}
// Include language file.
e107::coreLan('error');
/**
* Class error_front.
@@ -63,92 +54,29 @@ class error_front
switch($this->errorNumber)
{
case 400:
header('HTTP/1.1 400 Bad Request');
$subtitle = LAN_ERROR_35; // Error 400 - Bad Request
$caption = LAN_ERROR_45;
$content = LAN_ERROR_36 . '<br/>' . LAN_ERROR_3;
e107::setErrorPage(400);
break;
case 401:
header('HTTP/1.1 401 Unauthorized');
$subtitle = LAN_ERROR_1; // Error 401 - Authentication Failed
$caption = LAN_ERROR_45;
$content = LAN_ERROR_2 . '<br/>' . LAN_ERROR_3;
e107::setErrorPage(401);
break;
case 403:
header('HTTP/1.1 403 Forbidden');
$subtitle = LAN_ERROR_4; // Error 403 - Access forbidden
$caption = LAN_ERROR_45;
$content = LAN_ERROR_5 . '<br/>' . LAN_ERROR_6 . '<br/><br/>' . LAN_ERROR_2;
e107::setErrorPage(403);
break;
case 404:
header('HTTP/1.1 404 Not Found');
$subtitle = LAN_ERROR_7; // Error 404 - Document Not Found
$caption = LAN_ERROR_45;
$content = LAN_ERROR_21 . '<br/>' . LAN_ERROR_9;
$errFrom = isset($_SESSION['e107_http_referer']) ? $_SESSION['e107_http_referer'] : $_SERVER['HTTP_REFERER'];
if(strlen($errFrom))
{
$content .= '<br/>';
$content .= '<br/>';
$content .= LAN_ERROR_23 . ' <a href="' . $errFrom . '" rel="external">' . $errFrom . '</a> ';
$content .= LAN_ERROR_24;
}
e107::setErrorPage(404);
break;
case 500:
header('HTTP/1.1 500 Internal Server Error');
$subtitle = LAN_ERROR_10; // Error 500 - Internal server error
$caption = LAN_ERROR_14;
$content = LAN_ERROR_11 . '<br/>' . LAN_ERROR_12;
break;
case 999:
if(!defset('E107_DEBUG_LEVEL', false))
{
e107::redirect();
}
$this->errorNumber = 'DEFAULT'; // Use default template.
$subtitle = LAN_ERROR_33;
$caption = LAN_ERROR_14;
$content = '<pre>' . print_r($_SERVER) . print_r($_REQUEST) . '</pre>';
e107::setErrorPage(500);
break;
default:
$this->errorNumber = 'DEFAULT'; // Use default template.
$errorQuery = htmlentities($_SERVER['QUERY_STRING']);
$subtitle = LAN_ERROR_13 . ' (' . $errorQuery . ')'; // Error - Unknown
$caption = LAN_ERROR_14;
$content = LAN_ERROR_15;
e107::setErrorPage('unknown');
break;
}
$tp = e107::getParser();
$tpl = e107::getCoreTemplate('error', $this->errorNumber);
$sc = e107::getScBatch('error');
$sc->setVars(array(
'title' => LAN_ERROR_TITLE,
'subtitle' => $subtitle,
'caption' => $caption,
'content' => $content,
));
$body = $tp->parseTemplate($tpl, true, $sc);
e107::getRender()->tablerender('', $body);
}
}