diff --git a/cron/cleanup.php b/cron/cleanup.php index cfb264c..2eee5cc 100644 --- a/cron/cleanup.php +++ b/cron/cleanup.php @@ -78,7 +78,8 @@ orphanSessionMessagesCheck(); /* check and purge any expired sessions from the session_list table */ orphanSessionListCheck(); - +/* report error log entries to admins and flush the error log after doing so */ +reportErrorLogEntries(); /* Users that are unwarned and inactive since $iMonths */ @@ -228,3 +229,11 @@ function orphanSessionListCheck() $sQuery = "DELETE from session_list where TO_DAYS(NOW()) - TO_DAYS(stamp) > ?"; $hResult = query_parameters($sQuery, SESSION_DAYS_TO_EXPIRE + 2); } + +function reportErrorLogEntries() +{ + error_log::mail_admins_error_log(); + error_log::flush(); +} + +?> diff --git a/include/error_log.php b/include/error_log.php new file mode 100644 index 0000000..6ddd4c7 --- /dev/null +++ b/include/error_log.php @@ -0,0 +1,92 @@ +iUserId, + $sErrorType, + $sLogText, + $sRequestText, + '0'); + } + + function getEntryCount() + { + $sQuery = "SELECT count(*) as cnt FROM error_log WHERE deleted = '0'"; + $hResult = query_parameters($sQuery); + $oRow = mysql_fetch_object($hResult); + return $oRow->cnt; + } + + /* purge all of the current entries from the error log */ + function flush() + { + $sQuery = "UPDATE error_log SET deleted='1'"; + $hResult = query_parameters($sQuery); + + if($hResult) return true; + else return false; + } + + function mail_admins_error_log() + { + $sSubject = "Appdb error log\r\n"; + $sEmail = User::get_notify_email_address_list(null, null); /* get list admins */ + + $sQuery = "SELECT * from error_log WHERE deleted='0' ORDER BY submitTime"; + $hResult = query_parameters($sQuery); + + $bEmpty = false; + if(mysql_num_rows($hResult) == 0) + $bEmpty = true; + + $sMsg = "Log entries:\r\n"; + $sMsg.= "\r\n"; + + $sMsg.= "Submit time userid type\r\n"; + $sMsg.= "log_text\r\n"; + $sMsg.= "request_text\r\n"; + $sMsg.="----------------------------------\r\n\r\n"; + + /* append each log entry to $sMsg */ + while($oRow = mysql_fetch_object($hResult)) + { + $sMsg.=$oRow->submitTime." ".$oRow->userid." ".$oRow->type."\r\n"; + $sMsg.= "---------------------\r\n"; + $sMsg.=$oRow->log_text."\r\n"; + $sMsg.= "---------------------\r\n"; + $sMsg.=$oRow->request_text."\r\n\r\n"; + } + + /* if we had no entries we should indicate */ + /* that the error log is empty */ + if($bEmpty) + $sMsg = "The error log is empty.\r\n"; + + if($sEmail) + mail_appdb($sEmail, $sSubject, $sMsg); + } +} + +?> diff --git a/include/incl.php b/include/incl.php index 17046df..245a451 100644 --- a/include/incl.php +++ b/include/incl.php @@ -11,6 +11,7 @@ require(BASE."include/user.php"); require(BASE."include/session.php"); require(BASE."include/menu.php"); require(BASE."include/html.php"); +require(BASE."include/error_log.php"); require(BASE."include/query.php"); /* if magic quotes are enabled make sure the user disables them */ diff --git a/include/query.php b/include/query.php index b5100e1..840884b 100644 --- a/include/query.php +++ b/include/query.php @@ -113,11 +113,10 @@ function query_bugzilladb($sQuery,$sComment="") function query_error($sQuery, $sComment="") { - $sStatusMessage = "
Database Error!
";
- $sStatusMessage .= "Query: ".$sQuery."
";
- $sStatusMessage .= $sComment ? $sComment."
" : "";
- $sStatusMessage .= mysql_error()."
An internal error has occurred and has been logged and reported to appdb admins
"; + addmsg($sStatusMessage); + + error_log::log_error(ERROR_SQL, "Query: '".$sQuery."' mysql_error(): '".mysql_error()."' comment: '".$sComment."'"); } ?> diff --git a/tables/create_tables b/tables/create_tables index 6ffc3ff..fa659d1 100644 --- a/tables/create_tables +++ b/tables/create_tables @@ -30,3 +30,5 @@ mysql -p -u root < buglinks.sql echo Adding monitors mysql -p -u root < monitors.sql +echo Creating error logging tables +mysql -p -u root < error_log.sql diff --git a/tables/error_log.sql b/tables/error_log.sql new file mode 100644 index 0000000..22430d6 --- /dev/null +++ b/tables/error_log.sql @@ -0,0 +1,14 @@ +use apidb; + +drop table if exists error_log; + +create table error_log ( + id int not null auto_increment, + submitTime datetime, + userid int not null default '0', + type enum('sql_error', 'general_error'), + log_text text, + request_text text, + deleted bool, + key(id) +); \ No newline at end of file diff --git a/unit_test/run_tests.php b/unit_test/run_tests.php index f112589..e8eb374 100644 --- a/unit_test/run_tests.php +++ b/unit_test/run_tests.php @@ -12,5 +12,7 @@ echo "\n"; include_once("test_query.php"); echo "\n"; include_once("test_image.php"); +echo "\n"; +include_once("test_error_log.php"); -?> \ No newline at end of file +?> diff --git a/unit_test/test_error_log.php b/unit_test/test_error_log.php new file mode 100644 index 0000000..d84f637 --- /dev/null +++ b/unit_test/test_error_log.php @@ -0,0 +1,40 @@ + \ No newline at end of file