Пример #1
0
function phorum_mod_event_logging_database_error($error)
{
    global $PHORUM;
    if (!$PHORUM["mod_event_logging"]["do_log_database_error"]) {
        return $error;
    }
    // Check for suspended logging.
    if (!empty($PHORUM["MOD_EVENT_LOGGING"]["SUSPEND"])) {
        return $error;
    }
    // Prevention against recursive logging calls.
    if (!empty($PHORUM["MOD_EVENT_LOGGING"]["LOOPLOCK"])) {
        return $error;
    }
    $PHORUM["MOD_EVENT_LOGGING"]["LOOPLOCK"]++;
    // Construct a backtrace.
    require_once PHORUM_PATH . '/include/api/error/backtrace.php';
    $backtrace = phorum_api_error_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));
    $PHORUM["MOD_EVENT_LOGGING"]["LOOPLOCK"]--;
    return $error;
}
Пример #2
0
/**
 * @deprecated Replaced by {@link phorum_api_error_backtrace()}.
 */
function phorum_generate_backtrace($skip = 0, $hidepath = "{path to Phorum}")
{
    require_once PHORUM_PATH . '/include/api/error/backtrace.php';
    return phorum_api_error_backtrace($skip, $hidepath);
}
Пример #3
0
/**
 * Get the name of the PHP file to include for rendering a given template.
 *
 * If the format for $template is <module>::<template>, then
 * the template is loaded from the module's template directory. The
 * directory structure for storing module templates is the same as for the
 * main templates directory, only it is stored within a module's
 * directory:
 *
 *   <phorum_dir>/mods/templates/<template name>/<page>.tpl
 *
 * @param string $page
 *     The name of the temlate to compile (e.g. "header", "css",
 *     "somemod::template", etc.).
 *
 * @return string
 *     The name of the PHP file to include for rendering the template.
 */
function phorum_api_template($page)
{
    // This might for example happen if a template contains code like
    // {INCLUDE template} instead of {INCLUDE "template"}.
    if ($page === NULL || $page == "") {
        require_once PHORUM_PATH . '/include/api/error/backtrace.php';
        print "<html><head><title>Phorum Template Error</title><body>";
        print "<h1>Phorum Template Error</h1>";
        print "phorum_api_template() was called with an empty page name.<br/>";
        print "This might indicate a template problem.<br/>";
        if (function_exists('debug_print_backtrace')) {
            print "Here's a backtrace that might help finding the error:";
            print "<pre>";
            print phorum_api_error_backtrace();
            print "</pre>";
        }
        print "</body></html>";
        exit(1);
    }
    list($page, $phpfile, $tplfile) = phorum_api_template_resolve($page);
    // No template to process. This will happen in case a .php file
    // is used for defining the template instead of a .tpl file.
    if (empty($tplfile)) {
        return $phpfile;
    }
    // Compile the template if the output PHP file is not available.
    if (!file_exists($phpfile)) {
        require_once PHORUM_PATH . '/include/api/template/compile.php';
        phorum_api_template_compile($page, $tplfile, $phpfile);
    }
    return $phpfile;
}
Пример #4
0
/**
 * Database error handling function.
 *
 * @param string $error
 *     The database error message.
 */
function phorum_api_error_database($error)
{
    global $PHORUM;
    $hcharset = $PHORUM['DATA']['HCHARSET'];
    // Clear any output that we buffered so far (e.g. in the admin interface,
    // we might already have sent the page header).
    phorum_api_buffer_clear();
    /*
     * [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_api_error_database</literal> (which you can find in
     *     <filename>include/api/error.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_api_hook("database_error", $error);
    }
    // Find out what type of error handling is configured.
    // If no type if set, then we use "screen" by default.
    $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_api_error_backtrace(2);
    // Error page header.
    if (PHP_SAPI != "cli") {
        // 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, $hcharset) . " -->";
        }
    } else {
        // In CLI mode, we always show the error message on screen.
        // No need to be hiding this info from a user that can run CLI code.
        print "Sorry, a Phorum database error occurred:\n";
        print "------------------------------------------------------\n";
        print "Error: {$error}\n";
        if ($backtrace !== NULL) {
            print "------------------------------------------------------\n";
            print "Backtrace:\n" . $backtrace . "\n";
        }
        print "------------------------------------------------------\n";
    }
    switch ($logopt) {
        // Log the database error to a logfile.
        case "file":
            $cache_dir = $PHORUM['CACHECONFIG']['directory'];
            $logfile = $cache_dir . '/phorum-sql-errors.log';
            if ($fp = @fopen($logfile, "a")) {
                fputs($fp, "Time: " . time() . "\n" . "Error: {$error}\n" . ($backtrace !== NULL ? "Back trace:\n{$backtrace}\n\n" : ""));
                fclose($fp);
                if (PHP_SAPI != 'cli') {
                    print "The error message has been logged<br/>" . "to the phorum-sql-errors.log error log.<br/>" . "Please, try again later!";
                } else {
                    print "The error message has been logged to the db error log:\n";
                    print "{$logfile}\n";
                }
            } else {
                trigger_error("phorum_api_error_database(): cannot write to {$logfile}", E_USER_ERROR);
            }
            break;
            // Display the database error on screen.
        // Display the database error on screen.
        case "screen":
            // For CLI scripts, the error was already shown on screen.
            if (PHP_SAPI != 'cli') {
                $htmlbacktrace = $backtrace === NULL ? NULL : nl2br(htmlspecialchars($backtrace, ENT_COMPAT, $hcharset));
                print "Please try again later!" . "<h3>Error:</h3>" . htmlspecialchars($error, ENT_COMPAT, $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 PHORUM_PATH . '/include/api/mail.php';
            $data = array('mailmessage' => "A database error occured in your Phorum installation\n" . htmlspecialchars($PHORUM['http_path']) . ":\n" . "\n" . "Error message:\n" . (require_once "--------------\n" . "\n" . "{$error}\n" . "\n" . ($backtrace !== NULL ? "Backtrace:\n" . "----------\n" . "\n" . "{$backtrace}\n" : "")), 'mailsubject' => 'Phorum: A database error occured');
            $adminmail = $PHORUM['system_email_from_address'];
            phorum_api_mail($adminmail, $data);
            if (PHP_SAPI != 'cli') {
                print "The administrator of this forum has been<br/>" . "notified by email about the error.<br/>" . "Please, try again later!";
            } else {
                print "The error message was sent by mail to {$adminmail}\n";
            }
            break;
    }
    // Error page footer.
    if (PHP_SAPI != "cli") {
        print '</body></html>';
    }
    exit;
}