function phorum_mod_event_logging_database_error($error) { if (!$GLOBALS["PHORUM"]["mod_event_logging"]["do_log_database_error"]) { return $error; } // Check for suspended logging. if (!empty($GLOBALS["PHORUM"]["MOD_EVENT_LOGGING"]["SUSPEND"])) { return $error; } // Prevention against recursive logging calls. if (!empty($GLOBALS["PHORUM"]["MOD_EVENT_LOGGING"]["LOOPLOCK"])) { return $error; } $GLOBALS["PHORUM"]["MOD_EVENT_LOGGING"]["LOOPLOCK"]++; // Construct a back trace. $backtrace = phorum_generate_backtrace(3); list($source, $from_module) = event_logging_find_source(4); // Log the event. event_logging_writelog(array("message" => "Database error: {$error}", "details" => $backtrace === NULL ? NULL : "\nBack trace:\n\n{$backtrace}", "loglevel" => EVENTLOG_LVL_ALERT, "source" => $source, "category" => $from_module ? EVENTLOG_CAT_MODULE : EVENTLOG_CAT_APPLICATION)); $GLOBALS["PHORUM"]["MOD_EVENT_LOGGING"]["LOOPLOCK"]--; return $error; }
/** * Database error handling function. * * @param $error - The error message. */ function phorum_database_error($error) { $PHORUM = $GLOBALS["PHORUM"]; // Flush output that we buffered so far (for displaying a // clean page in the admin interface). phorum_ob_clean(); /* * [hook] * database_error * * [description] * Give modules a chance to handle or process database errors. * This can be useful to implement addional logging backends and/or * alerting mechanisms. Another option is to fully override Phorum's * default database error handling by handling the error and then * calling exit() from the hook to prevent the default Phorum code * from running.<sbr/> * <sbr/> * Note: If you decide to use the full override scenario, then * it is best to make your module run the database_error hook * last, so other modules can still run their hook handling * before the script exits. To accomplish this, add this to your * module info: * <programlisting> * priority: run hook database_error after * * </programlisting> * * [category] * Miscellaneous * * [when] * At the start of the function * <literal>phorum_database_error</literal> (which you can find in * <filename>common.php</filename>). This function is called from * the database layer when some database error occurs. * * [input] * The error message that was returned from the database layer. * This error is not HTML escaped, so if you send it to the browser, * be sure to preprocess it using <phpfunc>htmlspecialchars</phpfunc>. * * [output] * Same as input. * * [example] * <hookcode> * function phorum_mod_foo_database_error($error) * { * // Log database errors to syslog facility "LOCAL0". * openlog("Phorum", LOG_PID | LOG_PERROR, LOG_LOCAL0); * syslog(LOG_ERR, $error); * * return $error; * } * </hookcode> */ if (isset($PHORUM["hooks"]["database_error"])) { phorum_hook("database_error", $error); } // Find out what type of error handling is required. $logopt = isset($PHORUM["error_logging"]) ? $PHORUM["error_logging"] : 'screen'; // Create a backtrace report, so it's easier to find out where a problem // is coming from. $backtrace = phorum_generate_backtrace(0); // Start the error page. ?> <html> <head><title>Phorum database error</title></head> <body> <h1>Phorum Database Error</h1> Sorry, a Phorum database error occurred.<br/> <?php // In admin scripts, we will always include the // error message inside a comment in the page. if (defined("PHORUM_ADMIN")) { print "<!-- " . htmlspecialchars($error, ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"]) . " -->"; } switch ($logopt) { // Log the database error to a logfile. case "file": $cache_dir = $PHORUM["cache"]; $fp = fopen($cache_dir . "/phorum-sql-errors.log", "a"); fputs($fp, "Time: " . time() . "\n" . "Error: {$error}\n" . ($backtrace !== NULL ? "Back trace:\n{$backtrace}\n\n" : "")); fclose($fp); print "The error message has been written<br/>" . "to the phorum-sql-errors.log error log.<br/>" . "Please try again later!"; break; // Display the database error on screen. // Display the database error on screen. case "screen": $htmlbacktrace = $backtrace === NULL ? NULL : nl2br(htmlspecialchars($backtrace, ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"])); print "Please try again later!" . "<h3>Error:</h3>" . htmlspecialchars($error, ENT_COMPAT, $PHORUM["DATA"]["HCHARSET"]) . ($backtrace !== NULL ? "<h3>Backtrace:</h3>\n{$htmlbacktrace}" : ""); break; // Send a mail to the administrator about the database error. // Send a mail to the administrator about the database error. case "mail": default: require_once "./include/email_functions.php"; $data = array("mailmessage" => "A database error occured in your Phorum installation.\n" . "\n" . "Error message:\n" . "--------------\n" . "\n" . "{$error}\n" . "\n" . ($backtrace !== NULL ? "Backtrace:\n----------\n\n{$backtrace}" : ""), "mailsubject" => "Phorum: A database error occured"); $adminmail = $PHORUM["system_email_from_address"]; phorum_email_user(array($adminmail), $data); print "The administrator of this forum has been<br/>" . "notified by email about the error.<br/>" . "Please try again later!"; break; } // Finish the error page. ?> </body> </html> <?php exit; }