Skip to content

Commit b0474a1

Browse files
committed
some refactoring, modified to use a JSON file for error codes and msgs
1 parent 7c01204 commit b0474a1

File tree

1 file changed

+26
-61
lines changed

1 file changed

+26
-61
lines changed

errpages/httperror.php

Lines changed: 26 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ function isHTTPS() {
2323
}
2424

2525
$http_status = 0;
26-
$error_code = '';
27-
$explanation = '';
28-
// post-error redirection, place a path to a resource below
29-
// in the `case` statments and it will be redirected to
30-
// automatically after `$redirect_delay` seconds.
31-
$redirect_to = '';
32-
$redirect_delay = 10;
3326

3427
// get ready...
3528
if(defined('_DEBUG') && _DEBUG === true) {
@@ -44,60 +37,6 @@ function isHTTPS() {
4437
$server_url = (isHTTPS() ? 'https://' : 'http://') . $_SERVER['SERVER_NAME'] . '/';
4538
}
4639

47-
// check the server's error code...
48-
switch($http_status) {
49-
# '400 - Bad Request'
50-
case 400:
51-
$error_code = '400 - Bad Request';
52-
$explanation = 'The syntax of the URL submitted by your browser could not be understood. Please verify the address and try again.';
53-
// edit as needed for each `$http_status`
54-
$redirect_to = '';
55-
break;
56-
57-
# '401 - Unauthorized'
58-
case 401:
59-
$error_code = '401 - Unauthorized';
60-
$explanation = 'This section requires a password or is otherwise protected. If you feel you have reached this page in error, please return to the login page and try again, or contact the webmaster if you continue to have problems.';
61-
$redirect_to = '';
62-
break;
63-
64-
# '403 - Forbidden'
65-
# might be handled by the server instead of here
66-
case 403:
67-
$error_code = '403 - Forbidden';
68-
$explanation = 'This section requires a password or is otherwise protected. If you feel you have reached this page in error, please return to the login page and try again, or contact the webmaster if you continue to have problems.';
69-
$redirect_to = '';
70-
break;
71-
72-
# '404 - Not Found'
73-
case 404:
74-
$error_code = '404 - Not Found';
75-
$explanation = 'The requested resource: <span>' . $page_redirected_from . '</span>, could not be found on this server. Please verify the address and try again.';
76-
if(defined('_DEBUG') && _DEBUG === true) {
77-
$redirect_to = 'https://google.com';
78-
} else {
79-
$redirect_to = '';
80-
}
81-
break;
82-
83-
# '405 - Method Not Allowed'
84-
case 405:
85-
$error_code = '405 - Method Not Allowed';
86-
$explanation = 'The request method is known by the server but has been disabled and cannot be used.';
87-
$redirect_to = '';
88-
break;
89-
90-
# everything else...
91-
default:
92-
$error_code = $http_status . ' - Unknown';
93-
$explanation = 'Something bad happened, sorry!.';
94-
$redirect_to = '';
95-
break;
96-
}
97-
98-
define('SRVNAME', ((isset($_SERVER['SERVER_NAME']) === true) ? $_SERVER['SERVER_NAME'] : 'none'));
99-
define('REMADDR', ((isset($_SERVER['REMOTE_ADDR']) === true) ? $_SERVER['REMOTE_ADDR'] : 'none'));
100-
10140
/*
10241
cidrmatch() - returns `true` only if the IP
10342
falls within the CIDR
@@ -119,6 +58,9 @@ function cidrmatch($ip, $cidr)
11958
isLive() - returns `true` if this script is
12059
running on a "live" server
12160
*/
61+
define('SRVNAME', ((isset($_SERVER['SERVER_NAME']) === true) ? $_SERVER['SERVER_NAME'] : 'none'));
62+
define('REMADDR', ((isset($_SERVER['REMOTE_ADDR']) === true) ? $_SERVER['REMOTE_ADDR'] : 'none'));
63+
12264
function isLive() {
12365
$ret = ((SRVNAME !== 'localhost') &&
12466
(SRVNAME !== '127.0.0.1') &&
@@ -147,6 +89,29 @@ function isLive() {
14789
require_once $imagepool;
14890
}
14991

92+
$error_code = '';
93+
$explanation = '';
94+
// post-error redirection, it will be redirected
95+
// automatically after `$redirect_delay` seconds.
96+
$redirect_to = '';
97+
$redirect_delay = 10;
98+
99+
define('REDIRECT', 0);
100+
define('ERROR_CODE', 1);
101+
define('EXPLANATION', 2);
102+
103+
$errorcodes = json_decode(file_get_contents('./errpages/httperror.json'), true);
104+
105+
if(@$errorcodes["{$http_status}"]) {
106+
$error_code = $errorcodes["{$http_status}"][ERROR_CODE];
107+
$explanation = $errorcodes["{$http_status}"][EXPLANATION];
108+
$redirect_to = $errorcodes["{$http_status}"][REDIRECT];
109+
} else {
110+
$error_code = $http_status . ' - Unknown';
111+
$explanation = 'Something bad happened, sorry!.';
112+
$redirect_to = '';
113+
}
114+
150115
// this will help defeat forced caching, like some android
151116
// browsers. they even ignore the anti-caching meta tags.
152117
$randquery = '?' . (microtime(true) * 10000);

0 commit comments

Comments
 (0)