1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-02 12:48:26 +02:00

Use e107::getError()->render() instead.

This commit is contained in:
Lóna Lore
2016-12-15 19:11:50 +01:00
parent 2667537fd1
commit a59888a1e8
4 changed files with 125 additions and 117 deletions

View File

@@ -48,7 +48,7 @@ class core_system_error_controller extends eController
*/ */
public function actionForbidden() public function actionForbidden()
{ {
e107::setErrorPage(403); e107::getError()->render(403);
} }
/** /**
@@ -56,7 +56,7 @@ class core_system_error_controller extends eController
*/ */
public function actionNotfound() public function actionNotfound()
{ {
e107::setErrorPage(404); e107::getError()->render(404);
} }
} }

View File

@@ -3061,50 +3061,13 @@ class e107
/** /**
* Sends error page contents to the browser as HTML. * Retrieve error page handler.
* *
* @param int $status_code * @return error_page
* 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) public static function getError()
{ {
if(!defined('ERR_PAGE_ACTIVE')) return self::getSingleton('error_page', true);
{
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;
}
} }

View File

@@ -24,7 +24,22 @@ class error_page
/** /**
* @var * @var
*/ */
private $statusCode; private $template = 'DEFAULT';
/**
* @var
*/
private $title;
/**
* @var
*/
private $caption;
/**
* @var
*/
private $content;
/** /**
* Constructor. * Constructor.
@@ -37,132 +52,162 @@ class error_page
} }
/** /**
* Cloning is not allowed. * Singleton is not required, we go for factory instead.
*
* @return error_page
*/ */
private function __clone() public static function getInstance()
{ {
return e107::getError();
} }
/** /**
* @return void * Set a "Bad Request" error page.
*/ */
protected function _init() private function setPageBadRequest()
{
}
/**
* Delivers a "Bad Request" error page to the browser.
*/
public function deliverPageBadRequest()
{ {
header('HTTP/1.1 400 Bad Request', true, 400); header('HTTP/1.1 400 Bad Request', true, 400);
$title = LAN_ERROR_35; // Error 400 - Bad Request $this->template = 400;
$caption = LAN_ERROR_45; $this->title = LAN_ERROR_35;
$content = LAN_ERROR_36 . '<br/>' . LAN_ERROR_3; $this->caption = LAN_ERROR_45;
$this->content = LAN_ERROR_36;
$this->statusCode = 400;
$this->renderPage($title, $caption, $content);
} }
/** /**
* Delivers a "Authentication Failed" error page to the browser. * Set a "Authentication Failed" error page.
*/ */
public function deliverPageUnauthorized() private function setPageUnauthorized()
{ {
header('HTTP/1.1 401 Unauthorized', true, 401); header('HTTP/1.1 401 Unauthorized', true, 401);
$title = LAN_ERROR_1; // Error 401 - Authentication Failed $this->template = 401;
$caption = LAN_ERROR_45; $this->title = LAN_ERROR_1;
$content = LAN_ERROR_2 . '<br/>' . LAN_ERROR_3; $this->caption = LAN_ERROR_45;
$this->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. * Set a "Access forbidden" error page.
*/ */
public function deliverPageForbidden() private function setPageForbidden()
{ {
header('HTTP/1.1 403 Forbidden', true, 403); header('HTTP/1.1 403 Forbidden', true, 403);
$title = LAN_ERROR_4; // Error 403 - Access forbidden $this->template = 403;
$caption = LAN_ERROR_45; $this->title = LAN_ERROR_4;
$content = LAN_ERROR_5 . '<br/>' . LAN_ERROR_6 . '<br/><br/>' . LAN_ERROR_2; $this->caption = LAN_ERROR_45;
$this->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. * Set a "Not Found" error page.
*/ */
public function deliverPageNotFound() private function setPageNotFound()
{ {
header('HTTP/1.1 404 Not Found', true, 404); header('HTTP/1.1 404 Not Found', true, 404);
$title = LAN_ERROR_7; // Error 404 - Document Not Found $this->template = 404;
$caption = LAN_ERROR_45; $this->title = LAN_ERROR_7;
$content = LAN_ERROR_21 . '<br/>' . LAN_ERROR_9; $this->caption = LAN_ERROR_45;
$this->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. * Set a "Internal server error" error page.
*/ */
public function deliverPageInternalServerError() private function setPageInternalServerError()
{ {
header('HTTP/1.1 500 Internal Server Error', true, 500); header('HTTP/1.1 500 Internal Server Error', true, 500);
$title = LAN_ERROR_10; // Error 500 - Internal server error $this->template = 500;
$caption = LAN_ERROR_14; $this->title = LAN_ERROR_10;
$content = LAN_ERROR_11 . '<br/>' . LAN_ERROR_12; $this->caption = LAN_ERROR_14;
$this->content = LAN_ERROR_11 . '<br/>' . LAN_ERROR_12;
$this->statusCode = 500;
$this->renderPage($title, $caption, $content);
} }
/** /**
* Delivers a "Unknown" error page to the browser. * Set a "Unknown" error page.
*/ */
public function deliverPageUnknown() private function setPageUnknown()
{ {
header('HTTP/1.1 501 Not Implemented', true, 501); header('HTTP/1.1 501 Not Implemented', true, 501);
$errorQuery = htmlentities($_SERVER['QUERY_STRING']); $errorQuery = htmlentities($_SERVER['QUERY_STRING']);
$title = LAN_ERROR_13 . ' (' . $errorQuery . ')'; // Error - Unknown $this->template = 'DEFAULT';
$caption = LAN_ERROR_14; $this->title = LAN_ERROR_13 . ' (' . $errorQuery . ')';
$content = LAN_ERROR_15; $this->caption = LAN_ERROR_14;
$this->content = LAN_ERROR_15;
}
$this->statusCode = 'DEFAULT'; // Use default template. /**
$this->renderPage($title, $caption, $content); * Set error page.
*
* @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
*/
public function set($status_code = 404)
{
switch($status_code)
{
case 400:
$this->setPageBadRequest();
break;
case 401:
$this->setPageUnauthorized();
break;
case 403:
$this->setPageForbidden();
break;
case 404:
$this->setPageNotFound();
break;
case 500:
$this->setPageInternalServerError();
break;
default:
$this->setPageUnknown();
break;
}
} }
/** /**
* Renders and delivers an error page to the browser. * Renders and delivers an error page to the browser.
* *
* @param $title * @param int $status_code
* Page title. * The HTTP status code to use for the error page, defaults to 404.
* @param $caption * Status codes are defined in RFC 2616.
* Title for info panel. * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
* @param $content
* Content for info panel.
*/ */
private function renderPage($title, $caption, $content) public function render($status_code = null)
{ {
if(!defined('ERR_PAGE_ACTIVE'))
{
define("ERR_PAGE_ACTIVE", true);
}
if($status_code)
{
$this->set($status_code);
}
$tp = e107::getParser(); $tp = e107::getParser();
$tpl = e107::getCoreTemplate('error', $this->statusCode); $tpl = e107::getCoreTemplate('error', $this->template);
$sc = e107::getScBatch('error'); $sc = e107::getScBatch('error');
$sc->setVars(array( $sc->setVars(array(
'title' => LAN_ERROR_TITLE, // Oops! 'title' => LAN_ERROR_TITLE, // Oops!
'subtitle' => $title, 'subtitle' => $this->title,
'caption' => $caption, 'caption' => $this->caption,
'content' => $content, 'content' => $this->content,
)); ));
$body = $tp->parseTemplate($tpl, true, $sc); $body = $tp->parseTemplate($tpl, true, $sc);

View File

@@ -54,27 +54,27 @@ class error_front
switch($this->errorNumber) switch($this->errorNumber)
{ {
case 400: case 400:
e107::setErrorPage(400); e107::getError()->render(400);
break; break;
case 401: case 401:
e107::setErrorPage(401); e107::getError()->render(401);
break; break;
case 403: case 403:
e107::setErrorPage(403); e107::getError()->render(403);
break; break;
case 404: case 404:
e107::setErrorPage(404); e107::getError()->render(404);
break; break;
case 500: case 500:
e107::setErrorPage(500); e107::getError()->render(500);
break; break;
default: default:
e107::setErrorPage('unknown'); e107::getError()->render('unknown');
break; break;
} }
} }