From aeebdaa71b8604a19729738fd6d0f73cc315266a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sun, 28 Jan 2018 14:30:19 +0100 Subject: [PATCH] send mail on exception --- www/index.php | 3 +-- www/internals/base.php | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/www/index.php b/www/index.php index 2a8c55b..bc6c1a7 100644 --- a/www/index.php +++ b/www/index.php @@ -181,6 +181,7 @@ try { if (isProd()) { + sendExceptionMail($e); httpError('500 ', 'Internal server error'); } else @@ -191,11 +192,9 @@ try { } -//TODO printed books 2 hp //TODO fast //TODO gzip (?) //TODO better gh widget //TODO remove db table prefixes //TODO euler insert+show 32bit | 64bit mode //TODO send cache header (?) -//TODO global uncached error logger -> send by mail diff --git a/www/internals/base.php b/www/internals/base.php index 2a12414..3eada85 100644 --- a/www/internals/base.php +++ b/www/internals/base.php @@ -289,4 +289,43 @@ function smart_resize_image($file, $string = null, $width = 0, $height = 0, $pro } return true; +} + +function sendMail($subject, $content, $to, $from) { + mail($to, $subject, $content, 'From: ' . $from); +} + +function ParamServerOrUndef($idx) { + return isset($_SERVER[$idx]) ? $_SERVER[$idx] : 'NOT_SET'; +} + +/** + * @param Exception $e + */ +function sendExceptionMail($e) +{ + try { + $subject = "Server has encountered an Error at " . date("Y-m-d H:i:s") . "] "; + + $content = ""; + + $content .= 'HTTP_HOST: ' . ParamServerOrUndef('HTTP_HOST') . "\n"; + $content .= 'REQUEST_URI: ' . ParamServerOrUndef('REQUEST_URI') . "\n"; + $content .= 'TIME: ' . date('Y-m-d H:i:s') . "\n"; + $content .= 'REMOTE_ADDR: ' . ParamServerOrUndef('REMOTE_ADDR') . "\n"; + $content .= 'HTTP_X_FORWARDED_FOR: ' . ParamServerOrUndef('HTTP_X_FORWARDED_FOR') . "\n"; + $content .= 'HTTP_USER_AGENT: ' . ParamServerOrUndef('HTTP_USER_AGENT') . "\n"; + $content .= 'MESSAGE:' . "\n" . $e->getMessage() . "\n"; + $content .= 'CODE:' . "\n" . $e->getCode() . "\n"; + $content .= 'TRACE:' . "\n" . $e->getTraceAsString() . "\n"; + $content .= '$_GET:' . "\n" . print_r($_GET, true) . "\n"; + $content .= '$_POST:' . "\n" . print_r($_POST, true) . "\n"; + $content .= '$_FILES:' . "\n" . print_r($_FILES, true) . "\n"; + + sendMail($subject, $content, 'virtualadmin@mikescher.de', 'webserver-error@mikescher.com'); + } + catch (Exception $e) + { + // + } } \ No newline at end of file