Skip to content

Commit

Permalink
Add custom error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
samerton committed Mar 9, 2018
1 parent b19357b commit f1720d5
Show file tree
Hide file tree
Showing 23 changed files with 286 additions and 53 deletions.
7 changes: 2 additions & 5 deletions 404.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Made by Samerton
* https://github.com/NamelessMC/Nameless/
* NamelessMC version 2.0.0-pr2
* NamelessMC version 2.0.0-pr3
*
* License: MIT
*
Expand All @@ -17,7 +17,7 @@
<!DOCTYPE html>
<html lang="<?php echo (defined('HTML_LANG') ? HTML_LANG : 'en'); ?>">
<head>
<meta charset="utf-8">
<meta charset="<?php echo (defined('LANG_CHARSET') ? LANG_CHARSET : 'utf-8'); ?>">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="<?php echo SITE_NAME; ?> - 404">
Expand Down Expand Up @@ -56,9 +56,6 @@

// 404 template
$smarty->display(ROOT_PATH . '/custom/templates/' . TEMPLATE . '/404.tpl');

// Scripts
require(ROOT_PATH . '/core/templates/scripts.php');
?>
</body>
</html>
72 changes: 72 additions & 0 deletions core/classes/ErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/*
* Made by Samerton
* https://github.com/NamelessMC/Nameless/
* NamelessMC version 2.0.0-pr3
*
* License: MIT
*
* Error handler class
*/
class ErrorHandler {
public static function catchError($errno, $errstr, $errfile, $errline){
if(!(error_reporting() & $errno))
return false;

switch($errno){
case E_USER_ERROR:
define('ERRORHANDLER', true);
require_once(ROOT_PATH . DIRECTORY_SEPARATOR . 'error.php');
self::logError('user', '[' . date('Y-m-d, H:i:s') . '] ' . $errfile . '(' . $errline . ') ' . $errno . ': ' . $errstr);
die(1);
break;

case E_USER_WARNING:
self::logError('warning', '[' . date('Y-m-d, H:i:s') . '] ' . $errfile . '(' . $errline . ') ' . $errno . ': ' . $errstr);
break;

case E_USER_NOTICE:
self::logError('notice', '[' . date('Y-m-d, H:i:s') . '] ' . $errfile . '(' . $errline . ') ' . $errno . ': ' . $errstr);
break;

default:
self::logError('unknown', '[' . date('Y-m-d, H:i:s') . '] ' . $errfile . '(' . $errline . ') ' . $errno . ': ' . $errstr);
break;
}

return true;
}

public static function catchFatalError(){
$error = error_get_last();

if($error['type'] === E_ERROR){
$errstr = $error['message'];
$errfile = $error['file'];
$errline = $error['line'];

define('ERRORHANDLER', true);
require_once(ROOT_PATH . DIRECTORY_SEPARATOR . 'error.php');
self::logError('fatal', '[' . date('Y-m-d, H:i:s') . '] ' . $errfile . '(' . $errline . '): ' . $errstr);
die(1);
}
}

private static function logError($type, $contents){
try {
if(!is_dir(join(DIRECTORY_SEPARATOR, array(ROOT_PATH, 'cache', 'logs')))){
if(is_writable(ROOT_PATH . DIRECTORY_SEPARATOR . 'cache')) {
mkdir(ROOT_PATH . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'logs');
$dir_exists = true;
}
} else
$dir_exists = true;

if(isset($dir_exists))
file_put_contents(join(DIRECTORY_SEPARATOR, array(ROOT_PATH, 'cache', 'logs', $type . '-log.log')), $contents . PHP_EOL, FILE_APPEND);

} catch(Exception $e){
// Unable to write to file, ignore for now
}
}
}
9 changes: 7 additions & 2 deletions core/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
* Initialisation file
*/

// Nameless error handling
require_once(join(DIRECTORY_SEPARATOR, array(ROOT_PATH, 'core', 'classes', 'ErrorHandler.php')));
set_error_handler("ErrorHandler::catchError");
register_shutdown_function("ErrorHandler::catchFatalError");

session_start();

// Page variable must be set
Expand All @@ -33,7 +38,6 @@
}

// Require config

require(ROOT_PATH . '/core/config.php');

if(isset($conf) && is_array($conf))
Expand Down Expand Up @@ -97,7 +101,8 @@
ini_set('display_errors',1);
error_reporting(-1);

define('DEBUGGING', 1);
if(!defined('DEBUGGING'))
define('DEBUGGING', 1);
} else {
// Disabled
error_reporting(0);
Expand Down
8 changes: 1 addition & 7 deletions core/templates/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,4 @@
</style>
<?php
}
?>

<style>
html {
overflow-y: scroll;
}
</style>
?>
12 changes: 9 additions & 3 deletions custom/languages/Chinese/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* License: MIT
*
* Chinese Language - Error
* Translation progress : 100%
* Translation progress : <50%
* 翻譯有誤請使用GitHun回報issues
* https://github.com/haer0248/NamelessMC-v2-Traditional-Chinese/issues
*/
Expand All @@ -22,6 +22,12 @@
'404_back' => '返回',
'404_home' => '首頁',
'404_error' => '如果你一直看到這個網頁,{x}請聯絡管理者{y}', // Don't replace {x} or {y}
'maintenance_title' => '維修模式.',
'maintenance_retry' => '重整'
'maintenance_title' => '維修模式.',
'maintenance_retry' => '重整',
'fatal_error' => 'Fatal Error',
'fatal_error_title' => 'Sorry!',
'fatal_error_message_user' => 'Sorry, but something went wrong while loading the page. Please contact an administrator.',
'fatal_error_message_admin' => 'Sorry, but something went wrong while loading the page. Error details:',
'in_file' => 'in file <strong>{x}</strong>', // Don't replace {x}
'on_line' => 'on line <strong>{x}</strong>' // Don't replace {x}
);
10 changes: 8 additions & 2 deletions custom/languages/Czech/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Made by Samerton, translated by Zemos and Renzotom
* https://github.com/NamelessMC/Nameless/
* NamelessMC version 2.0.0-pr2
* NamelessMC version 2.0.0-pr3
*
* License: MIT
*
Expand All @@ -19,5 +19,11 @@
'404_home' => 'Domů',
'404_error' => 'Pokud se domníváte, že je to chyba, {x}kontaktujte prosím administrátora{y}', // Don't replace {x} or {y}
'maintenance_title' => 'Údržba.',
'maintenance_retry' => 'Opakovat'
'maintenance_retry' => 'Opakovat',
'fatal_error' => 'Fatal Error',
'fatal_error_title' => 'Sorry!',
'fatal_error_message_user' => 'Sorry, but something went wrong while loading the page. Please contact an administrator.',
'fatal_error_message_admin' => 'Sorry, but something went wrong while loading the page. Error details:',
'in_file' => 'in file <strong>{x}</strong>', // Don't replace {x}
'on_line' => 'on line <strong>{x}</strong>' // Don't replace {x}
);
10 changes: 8 additions & 2 deletions custom/languages/Dutch/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* en vertaald door Sander Lambrechts en Derkades
*
* https://github.com/NamelessMC/Nameless/
* NamelessMC version 2.0.0-pr2
* NamelessMC version 2.0.0-pr3
*
* License: MIT
*
Expand All @@ -21,5 +21,11 @@
'404_home' => 'Start',
'404_error' => 'Als je denkt dat dit een error is, {x} neem dan contact op met een beheerder van de website {y}', // Verander "{x}" of "{y}" niet
'maintenance_title' => 'Onderhoud modus.',
'maintenance_retry' => 'Probeer opnieuw'
'maintenance_retry' => 'Probeer opnieuw',
'fatal_error' => 'Fatal Error',
'fatal_error_title' => 'Sorry!',
'fatal_error_message_user' => 'Sorry, but something went wrong while loading the page. Please contact an administrator.',
'fatal_error_message_admin' => 'Sorry, but something went wrong while loading the page. Error details:',
'in_file' => 'in file <strong>{x}</strong>', // Don't replace {x}
'on_line' => 'on line <strong>{x}</strong>' // Don't replace {x}
);
12 changes: 9 additions & 3 deletions custom/languages/EnglishUK/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Made by Samerton
* https://github.com/NamelessMC/Nameless/
* NamelessMC version 2.0.0-dev
* NamelessMC version 2.0.0-pr3
*
* License: MIT
*
Expand All @@ -18,6 +18,12 @@
'404_back' => 'Go back',
'404_home' => 'Home',
'404_error' => 'If you believe this to be an error, {x}please contact an administrator{y}', // Don't replace {x} or {y}
'maintenance_title' => 'Maintenance mode.',
'maintenance_retry' => 'Retry'
'maintenance_title' => 'Maintenance mode.',
'maintenance_retry' => 'Retry',
'fatal_error' => 'Fatal Error',
'fatal_error_title' => 'Sorry!',
'fatal_error_message_user' => 'Sorry, but something went wrong while loading the page. Please contact an administrator.',
'fatal_error_message_admin' => 'Sorry, but something went wrong while loading the page. Error details:',
'in_file' => 'in file <strong>{x}</strong>', // Don't replace {x}
'on_line' => 'on line <strong>{x}</strong>' // Don't replace {x}
);
12 changes: 9 additions & 3 deletions custom/languages/EnglishUS/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Made by Samerton
* https://github.com/NamelessMC/Nameless/
* NamelessMC version 2.0.0-dev
* NamelessMC version 2.0.0-pr3
*
* License: MIT
*
Expand All @@ -18,6 +18,12 @@
'404_back' => 'Go back',
'404_home' => 'Home',
'404_error' => 'If you believe this to be an error, {x}please contact an administrator{y}', // Don't replace {x} or {y}
'maintenance_title' => 'Maintenance mode.',
'maintenance_retry' => 'Retry'
'maintenance_title' => 'Maintenance mode.',
'maintenance_retry' => 'Retry',
'fatal_error' => 'Fatal Error',
'fatal_error_title' => 'Sorry!',
'fatal_error_message_user' => 'Sorry, but something went wrong while loading the page. Please contact an administrator.',
'fatal_error_message_admin' => 'Sorry, but something went wrong while loading the page. Error details:',
'in_file' => 'in file <strong>{x}</strong>', // Don't replace {x}
'on_line' => 'on line <strong>{x}</strong>' // Don't replace {x}
);
12 changes: 9 additions & 3 deletions custom/languages/German/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Made by Samerton
* https://github.com/NamelessMC/Nameless/
* NamelessMC version 2.0.0-dev
* NamelessMC version 2.0.0-pr3
*
* License: MIT
*
Expand All @@ -26,6 +26,12 @@
'404_back' => 'Zurück',
'404_home' => 'Startseite',
'404_error' => 'Wenn du glaubst das dies ein Fehler ist, {x}Kontaktieren einen Administrator {y}',
'maintenance_title' => 'Wartungsmodus.',
'maintenance_retry' => 'Wiederholen'
'maintenance_title' => 'Wartungsmodus.',
'maintenance_retry' => 'Wiederholen',
'fatal_error' => 'Fatal Error',
'fatal_error_title' => 'Sorry!',
'fatal_error_message_user' => 'Sorry, but something went wrong while loading the page. Please contact an administrator.',
'fatal_error_message_admin' => 'Sorry, but something went wrong while loading the page. Error details:',
'in_file' => 'in file <strong>{x}</strong>', // Don't replace {x}
'on_line' => 'on line <strong>{x}</strong>' // Don't replace {x}
);
12 changes: 9 additions & 3 deletions custom/languages/Greek/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* https://github.com/Ar1sC
* https://twitter.com/Ar1cC
* https://github.com/NamelessMC/Nameless/
* NamelessMC version 2.0.0-dev
* NamelessMC version 2.0.0-pr3
*
* License: MIT
*
Expand All @@ -20,6 +20,12 @@
'404_back' => 'Πήγαινε πίσω',
'404_home' => 'Αρχική',
'404_error' => 'Αν πιστεύετε ότι πρόκειται για σφάλμα, {x}παρακαλούμε επικοινωνήστε με έναν διαχειριστή{y}', // Don't replace {x} or {y}
'maintenance_title' => 'Λειτουργία συντήρησης.',
'maintenance_retry' => 'Ξαναδοκιμάσετε'
'maintenance_title' => 'Λειτουργία συντήρησης.',
'maintenance_retry' => 'Ξαναδοκιμάσετε',
'fatal_error' => 'Fatal Error',
'fatal_error_title' => 'Sorry!',
'fatal_error_message_user' => 'Sorry, but something went wrong while loading the page. Please contact an administrator.',
'fatal_error_message_admin' => 'Sorry, but something went wrong while loading the page. Error details:',
'in_file' => 'in file <strong>{x}</strong>', // Don't replace {x}
'on_line' => 'on line <strong>{x}</strong>' // Don't replace {x}
);
12 changes: 9 additions & 3 deletions custom/languages/Japanese/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Made by Samerton
* https://github.com/NamelessMC/Nameless/
* NamelessMC version 2.0.0-dev
* NamelessMC version 2.0.0-pr3
*
* Translation by SimplyRin
*
Expand All @@ -20,6 +20,12 @@
'404_back' => '戻る',
'404_home' => 'ホーム',
'404_error' => 'これがエラーと思われる場合は、{x}管理者に連絡してください{y}', // Don't replace {x} or {y}
'maintenance_title' => 'メンテナンスモード',
'maintenance_retry' => 'リトライ'
'maintenance_title' => 'メンテナンスモード',
'maintenance_retry' => 'リトライ',
'fatal_error' => 'Fatal Error',
'fatal_error_title' => 'Sorry!',
'fatal_error_message_user' => 'Sorry, but something went wrong while loading the page. Please contact an administrator.',
'fatal_error_message_admin' => 'Sorry, but something went wrong while loading the page. Error details:',
'in_file' => 'in file <strong>{x}</strong>', // Don't replace {x}
'on_line' => 'on line <strong>{x}</strong>' // Don't replace {x}
);
12 changes: 9 additions & 3 deletions custom/languages/Norwegian/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Oversatt av Thesevs
* https://github.com/NamelessMC/Nameless/
* NamelessMC versjon 2.0.0-dev
* NamelessMC versjon 2.0.0-pr3
*
* License: MIT
*
Expand All @@ -18,6 +18,12 @@
'404_back' => 'Tilbake',
'404_home' => 'Hjem',
'404_error' => 'Hvis du mener dette er en feil, {x}vennligst kontakt administrasjonen{y}', // Don't replace {x} or {y}
'maintenance_title' => 'Vedlikeholdsmodus',
'maintenance_retry' => 'Prøv igjen'
'maintenance_title' => 'Vedlikeholdsmodus',
'maintenance_retry' => 'Prøv igjen',
'fatal_error' => 'Fatal Error',
'fatal_error_title' => 'Sorry!',
'fatal_error_message_user' => 'Sorry, but something went wrong while loading the page. Please contact an administrator.',
'fatal_error_message_admin' => 'Sorry, but something went wrong while loading the page. Error details:',
'in_file' => 'in file <strong>{x}</strong>', // Don't replace {x}
'on_line' => 'on line <strong>{x}</strong>' // Don't replace {x}
);
12 changes: 9 additions & 3 deletions custom/languages/Portuguese/errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Made by Samerton
* https://github.com/NamelessMC/Nameless/
* NamelessMC version 2.0.0-pr2
* NamelessMC version 2.0.0-pr3
*
* License: MIT
*
Expand All @@ -19,6 +19,12 @@
'404_back' => 'Voltar',
'404_home' => 'Home',
'404_error' => 'Se você acredita que isso seja um erro, {x}entre em contato com um administrador{y}', // Don't replace {x} or {y}
'maintenance_title' => 'Modo de manutenção.',
'maintenance_retry' => 'Tentar novamente'
'maintenance_title' => 'Modo de manutenção.',
'maintenance_retry' => 'Tentar novamente',
'fatal_error' => 'Fatal Error',
'fatal_error_title' => 'Sorry!',
'fatal_error_message_user' => 'Sorry, but something went wrong while loading the page. Please contact an administrator.',
'fatal_error_message_admin' => 'Sorry, but something went wrong while loading the page. Error details:',
'in_file' => 'in file <strong>{x}</strong>', // Don't replace {x}
'on_line' => 'on line <strong>{x}</strong>' // Don't replace {x}
);
Loading

0 comments on commit f1720d5

Please sign in to comment.