From fb3946ae0a723f1e067f33f29a5aff95d79043f0 Mon Sep 17 00:00:00 2001 From: lonalore Date: Wed, 14 Dec 2016 15:01:51 +0100 Subject: [PATCH] Error page improvement: template support. --- e107.htaccess | 3 +- e107_core/controllers/system/error.php | 152 +++++------ .../shortcodes/batch/error_shortcodes.php | 81 ++++++ e107_core/templates/error_template.php | 204 +++++++++++--- e107_core/url/system/rewrite_url.php | 2 +- e107_handlers/e107_class.php | 21 +- e107_languages/English/lan_error.php | 3 +- error.php | 248 +++++++++++------- 8 files changed, 498 insertions(+), 216 deletions(-) create mode 100644 e107_core/shortcodes/batch/error_shortcodes.php diff --git a/e107.htaccess b/e107.htaccess index 06b25d474..b543dd69c 100644 --- a/e107.htaccess +++ b/e107.htaccess @@ -1,9 +1,8 @@ # Custom error pages for php scripts only - + ErrorDocument 400 /error.php?400 ErrorDocument 401 /error.php?401 ErrorDocument 403 /error.php?403 - ErrorDocument 404 /error.php?404 ErrorDocument 500 /error.php?500 diff --git a/e107_core/controllers/system/error.php b/e107_core/controllers/system/error.php index 9dd0afb70..02e0ebc2b 100644 --- a/e107_core/controllers/system/error.php +++ b/e107_core/controllers/system/error.php @@ -1,102 +1,104 @@ _forward('notfound'); - } - - public function actionNotfound() - { - $this->getResponse() - ->setRenderMod('error404') - ->addHeader('HTTP/1.0 404 Not Found'); - - $this->addTitle(LAN_ERROR_7); - $template = e107::getCoreTemplate('error', 404); - - $vars = new e_vars(array( - 'SITEURL' => SITEURL, - 'SEARCHURL' => e107::getUrl()->create('search'), - )); - - - $body = e107::getParser()->parseTemplate( - $this->updateTemplate($template['start']). - $this->updateTemplate($template['body']). - $this->updateTemplate($template['end']) - , true, null, $vars); - - $this->addBody($body); - } - /** - * Update template to v2.x spec. ALL CAPS shortcodes only. - * @param $template - * @return mixed - */ - private function updateTemplate($template) - { - $srch = array('{siteUrl}','{searchUrl}'); - $repl = array('{SITEURL}','{SEARCHURL}'); - - return str_replace($srch,$repl,$template); - } - - /** - * Alias + * Alias for "Error 403". */ public function action403() { $this->_forward('forbidden'); } + /** + * Alias for "Error 404". + */ + public function action404() + { + $this->_forward('notfound'); + } + + /** + * Error 403. + */ public function actionForbidden() { - $this->getResponse() - ->setRenderMod('error403') - ->addHeader('HTTP/1.0 403 Forbidden'); - - $this->addTitle(LAN_ERROR_7); - $template = e107::getCoreTemplate('error', 403); - - $vars = new e_vars(array( - 'SITEURL' => SITEURL, + $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 . '
' . LAN_ERROR_6 . '

' . LAN_ERROR_2; + + $sc->setVars(array( + 'title' => $title, + 'subtitle' => $subtitle, + 'caption' => $caption, + 'content' => $content, )); - - $body = e107::getParser()->parseTemplate( - $this->updateTemplate($template['start']). - $this->updateTemplate($template['body']). - $this->updateTemplate($template['end']) - , true, null, $vars); - + + $body = $tp->parseTemplate($tpl, true, $sc); $this->addBody($body); } - - function actionHelloWorld() + + /** + * Error 404. + */ + public function actionNotfound() { - //$this->addTitle('Hello!'); - //echo 'Hello World'; + $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 . '
' . LAN_ERROR_9; + + $sc->setVars(array( + 'title' => $title, + 'subtitle' => $subtitle, + 'caption' => $caption, + 'content' => $content, + )); + + $body = $tp->parseTemplate($tpl, true, $sc); + $this->addBody($body); } + } diff --git a/e107_core/shortcodes/batch/error_shortcodes.php b/e107_core/shortcodes/batch/error_shortcodes.php new file mode 100644 index 000000000..7b3a3dcda --- /dev/null +++ b/e107_core/shortcodes/batch/error_shortcodes.php @@ -0,0 +1,81 @@ +var['title'], ''); + } + + /** + * @return mixed + */ + public function sc_error_subtitle() + { + return varset($this->var['subtitle'], ''); + } + + /** + * @return mixed + */ + public function sc_error_caption() + { + return varset($this->var['caption'], ''); + } + + /** + * @return mixed + */ + public function sc_error_content() + { + return varset($this->var['content'], ''); + } + + /** + * @return string + */ + public function sc_error_link_home() + { + $icon = e107::getParser()->toGlyph('fa-home'); + $url = SITEURL; + + return '' . $icon . ' ' . LAN_ERROR_20 . ''; + } + + /** + * @return string + */ + public function sc_error_link_search() + { + $icon = e107::getParser()->toGlyph('fa-search'); + $url = e107::getUrl()->create('search'); + + return '' . $icon . ' ' . LAN_ERROR_22 . ''; + } + +} + diff --git a/e107_core/templates/error_template.php b/e107_core/templates/error_template.php index 2c035aa66..7263c8515 100644 --- a/e107_core/templates/error_template.php +++ b/e107_core/templates/error_template.php @@ -1,47 +1,187 @@ '; -$ERROR_TEMPLATE['404']['body'] = ' -

'.LAN_ERROR_45.'

-

- '.LAN_ERROR_21.'
'.LAN_ERROR_9.' -

- '.LAN_ERROR_20.'
- '.LAN_ERROR_22.' +/** + * 400 Bad Request. + */ +$ERROR_TEMPLATE['400'] = ' +

+ {ERROR_TITLE} +

+

+ {ERROR_SUBTITLE} +

+
+
+
+
+ {ERROR_CAPTION} +
+
+ {ERROR_CONTENT} +
+
+
+
+
+ {ERROR_LINK_HOME} +
+
'; -$ERROR_TEMPLATE['404']['end'] = ''; - -$ERROR_TEMPLATE['403']['start'] = '
'; -$ERROR_TEMPLATE['403']['body'] = ' -

'.LAN_ERROR_4.'

-

- '.LAN_ERROR_5.'
'.LAN_ERROR_6.'

'.LAN_ERROR_2.' -

- '.LAN_ERROR_20.'
+/** + * 401 Unauthorized. + */ +$ERROR_TEMPLATE['401'] = ' +

+ {ERROR_TITLE} +

+

+ {ERROR_SUBTITLE} +

+
+
+
+
+ {ERROR_CAPTION} +
+
+ {ERROR_CONTENT} +
+
+
+
+
+ {ERROR_LINK_HOME} +
+
'; -$ERROR_TEMPLATE['403']['end'] = '
'; +/** + * 403 Forbidden. + */ +$ERROR_TEMPLATE['403'] = ' +

+ {ERROR_TITLE} +

+

+ {ERROR_SUBTITLE} +

+
+
+
+
+ {ERROR_CAPTION} +
+
+ {ERROR_CONTENT} +
+
+
+
+
+ {ERROR_LINK_HOME} +
+
+'; + +/** + * 404 Not Found. + */ +$ERROR_TEMPLATE['404'] = ' +

+ {ERROR_TITLE} +

+

+ {ERROR_SUBTITLE} +

+
+
+
+
+ {ERROR_CAPTION} +
+
+ {ERROR_CONTENT} +
+
+
+
+
+ {ERROR_LINK_HOME} {ERROR_LINK_SEARCH} +
+
+'; + +/** + * 500 Internal server error. + */ +$ERROR_TEMPLATE['500'] = ' +

+ {ERROR_TITLE} +

+

+ {ERROR_SUBTITLE} +

+
+
+
+
+ {ERROR_CAPTION} +
+
+ {ERROR_CONTENT} +
+
+
+
+
+ {ERROR_LINK_HOME} +
+
+'; + +/** + * Default error page. + */ +$ERROR_TEMPLATE['DEFAULT'] = ' +

+ {ERROR_TITLE} +

+

+ {ERROR_SUBTITLE} +

+
+
+
+
+ {ERROR_CAPTION} +
+
+ {ERROR_CONTENT} +
+
+
+
+
+ {ERROR_LINK_HOME} +
+
+'; diff --git a/e107_core/url/system/rewrite_url.php b/e107_core/url/system/rewrite_url.php index bf4745035..3a2aa7f30 100644 --- a/e107_core/url/system/rewrite_url.php +++ b/e107_core/url/system/rewrite_url.php @@ -42,7 +42,7 @@ class core_system_rewrite_url extends eUrlConfig 'name' => LAN_EURL_CORE_SYSTEM, // Module name 'label' => LAN_EURL_SYSTEM_REWRITE_LABEL, // Current profile name 'description' => LAN_EURL_SYSTEM_REWRITE_DESCR, // - 'examples' => array("{SITEURL}system/error404") + 'examples' => array("{SITEURL}system/error/404") ), 'form' => array(), // Under construction - additional configuration options 'callbacks' => array(), // Under construction - could be used for e.g. URL generator functionallity diff --git a/e107_handlers/e107_class.php b/e107_handlers/e107_class.php index 87a09302b..7685f16ab 100644 --- a/e107_handlers/e107_class.php +++ b/e107_handlers/e107_class.php @@ -3036,11 +3036,26 @@ class e107 /** * Simple redirect method for developers. - * @param $url string : 'admin' to redirect to admin entry page or leave blank to go to home page (SITEURL) + * + * @param string $url + * 'admin' to redirect to admin entry page or leave blank to go to home page + * (SITEURL). + * @param int $http_response_code + * The HTTP status code to use for the redirection, defaults to 302. + * The valid values for 3xx redirection status codes are defined in RFC 2616 + * and the draft for the new HTTP status codes: + * - 301: Moved Permanently (the recommended value for most redirects). + * - 302: Found (default in PHP, sometimes used for spamming search engines). + * - 303: See Other. + * - 304: Not Modified. + * - 305: Use Proxy. + * - 307: Temporary Redirect. + * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3 + * @see https://tools.ietf.org/html/draft-reschke-http-status-308-07 */ - public static function redirect($url='') + public static function redirect($url = '', $http_response_code = 302) { - self::getRedirect()->go($url); + self::getRedirect()->go($url, true, $http_response_code); } diff --git a/e107_languages/English/lan_error.php b/e107_languages/English/lan_error.php index be471298c..b7e1e5d53 100644 --- a/e107_languages/English/lan_error.php +++ b/e107_languages/English/lan_error.php @@ -10,6 +10,7 @@ +----------------------------------------------------------------------------+ */ define("PAGE_NAME", "Error"); +define("LAN_ERROR_TITLE", "Oops!"); define("LAN_ERROR_1", "Error 401 - Authentication Failed"); define("LAN_ERROR_2", "The URL you've requested requires a correct username and password. Either you entered an incorrect username/password, or your browser doesn't support this feature."); @@ -21,7 +22,7 @@ define("LAN_ERROR_6", "Please inform the administrator of the referring page if define("LAN_ERROR_7", "Error 404 - Document Not Found"); define("LAN_ERROR_9", "Please inform the administrator of the referring page if you think this error message has been shown by mistake."); -define("LAN_ERROR_10", "Error 500 - Malformed Header"); +define("LAN_ERROR_10", "Error 500 - Internal server error"); define("LAN_ERROR_11", "The server encountered an internal error or misconfiguration and was unable to complete your request"); define("LAN_ERROR_12", "Please inform the administrator of the referring page if you think this error page has been shown by mistake."); define("LAN_ERROR_13", "Error - Unknown"); diff --git a/error.php b/error.php index c9f71eeb6..816b4d9e3 100644 --- a/error.php +++ b/error.php @@ -1,115 +1,159 @@ ".LAN_ERROR_37." ".LAN_ERROR_35."
".LAN_ERROR_36."

".LAN_ERROR_3."
-
".LAN_ERROR_2."
".LAN_ERROR_20."
"; - break; - case 401: - $errorHeader = "HTTP/1.1 401 Unauthorized"; - $errorText = "

".LAN_ERROR_37." ".LAN_ERROR_1."

".LAN_ERROR_2."

".LAN_ERROR_3."
-
".LAN_ERROR_2."
".LAN_ERROR_20."
"; - break; - case 403: - $errorHeader = "HTTP/1.1 403 Forbidden"; - $errorText = "

".LAN_ERROR_37." ".LAN_ERROR_4."

".LAN_ERROR_5."

".LAN_ERROR_6."
-
".LAN_ERROR_2."
".LAN_ERROR_20."
"; - break; - case 404: - $errorHeader = "HTTP/1.1 404 Not Found"; - $errorText = "

".LAN_ERROR_37." ".LAN_ERROR_7."

".LAN_ERROR_21.'
'.LAN_ERROR_9."

"; - if (strlen($errFrom)) $errorText .= LAN_ERROR_23." {$errFrom} ".LAN_ERROR_24." -- ".LAN_ERROR_19."

"; - //.LAN_ERROR_23."{$errTo}".LAN_ERROR_24."

" ??? - - $errorText .= "

".LAN_ERROR_45."

"; - if($errReturnTo) - { - foreach ($errReturnTo as $url => $label) - { - $errorText .= "".$label."
"; - } - $errorText .= '
'; - } - $errorText .= "".LAN_ERROR_20."
"; - $errorText .= "".LAN_ERROR_22.""; - break; - case 500: - $errorHeader = "HTTP/1.1 500 Internal Server Error"; - $errorText = "

".LAN_ERROR_37." ".LAN_ERROR_10."

".LAN_ERROR_11."

".LAN_ERROR_12."
-
".LAN_ERROR_2."
".LAN_ERROR_20."
"; - break; - case 999: - if (E107_DEBUG_LEVEL) - { - echo LAN_ERROR_33."
\n";
-	  print_r($_SERVER);
-	  print_r($_REQUEST);
-	  echo "\n
\n"; - } - else - { - header("location: ".e_HTTP."index.php"); - exit; - } - break; - - default : - $errorText = "

".LAN_ERROR_13." (".$errorQuery.")

".LAN_ERROR_14."

".LAN_ERROR_15."
-
"; - -// default: -// $errorText = LAN_ERROR_34." e_QUERY = '".e_QUERY."'
".LAN_ERROR_20.""; -// break; + session_start(); +} + +// Include language file. +e107::coreLan('error'); + + +/** + * Class error_front. + */ +class error_front +{ + + /** + * @var + */ + private $errorNumber; + + /** + * Constructor. + */ + public function __construct() + { + if(is_numeric(e_QUERY)) + { + $this->errorNumber = intval(e_QUERY); + } + + $this->renderErrorPage(); + } + + /** + * Renders the error page. + */ + public function renderErrorPage() + { + 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 . '
' . LAN_ERROR_3; + 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 . '
' . LAN_ERROR_3; + 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 . '
' . LAN_ERROR_6 . '

' . LAN_ERROR_2; + 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 . '
' . LAN_ERROR_9; + + $errFrom = isset($_SESSION['e107_http_referer']) ? $_SESSION['e107_http_referer'] : $_SERVER['HTTP_REFERER']; + + if(strlen($errFrom)) + { + $content .= '
'; + $content .= '
'; + $content .= LAN_ERROR_23 . ' ' . $errFrom . ' '; + $content .= LAN_ERROR_24; + } + + 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 . '
' . 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 = '
' . print_r($_SERVER) . print_r($_REQUEST) . '
'; + 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; + 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); + } + } -if ($errorHeader) header($errorHeader); require_once(HEADERF); - -e107::getRender()->tablerender(PAGE_NAME, $errorText); +new error_front(); require_once(FOOTERF); -?> \ No newline at end of file