function logd_error_handler($errno, $errstr, $errfile, $errline)
{
    global $session;
    static $in_error_handler = 0;
    // If we have used the @ operator, just don't report anything!
    if (!error_reporting()) {
        return;
    }
    $in_error_handler++;
    if ($in_error_handler > 1) {
        //prevents the error handler from being re-called when we're already within a call of it.
        if ($errno & (E_USER_WARNING | E_WARNING)) {
            echo "PHP Warning: \"{$errstr}\"<br>in <b>{$errfile}</b> at <b>{$errline}</b>.  Additionally this occurred while within logd_error_handler().<br>";
        } elseif ($errno & (E_USER_ERROR | E_ERROR)) {
            echo "PHP ERROR: \"{$errstr}\"<br>in <b>{$errfile}</b> at <b>{$errline}</b>.  Additionally this occurred while within logd_error_handler().<br>";
        }
        $in_error_handler--;
        return;
    }
    switch ($errno) {
        case E_NOTICE:
        case E_USER_NOTICE:
            if (getsetting('show_notices', 0) && $session['user']['superuser'] & SU_SHOW_PHPNOTICE) {
                debug("PHP Notice: \"{$errstr}\"<br>in <b>{$errfile}</b> at <b>{$errline}</b>.");
            }
            break;
        case E_WARNING:
        case E_USER_WARNING:
            require_once "show_backtrace.php";
            tlschema("errorhandler");
            output("PHP Warning: \"%s\"`nin `b%s`b at `b%s`b.`n", $errstr, $errfile, $errline, true);
            tlschema();
            $backtrace = show_backtrace();
            rawoutput($backtrace);
            if (getsetting("notify_on_warn", 0) > "") {
                //$args = func_get_args();
                //call_user_func_array("logd_error_notify",$args);
                logd_error_notify($errno, $errstr, $errfile, $errline, $backtrace);
            }
            break;
        case E_ERROR:
        case E_USER_ERROR:
            require_once "lib/show_backtrace.php";
            echo sprintf("PHP ERROR: \"%s\"<br>in <b>%s</b> at <b>%s</b>.<br>", $errstr, $errfile, $errline);
            $backtrace = show_backtrace();
            echo $backtrace;
            if (getsetting("notify_on_error", 0) > "") {
                //$args = func_get_args();
                //call_user_func_array("logd_error_notify",$args);
                logd_error_notify($errno, $errstr, $errfile, $errline, $backtrace);
            }
            die;
            break;
    }
    $in_error_handler--;
}
function db_query($sql, $die = true)
{
    //debug("SQL Query: ".$sql);
    if (defined("DB_NODB") && !defined("LINK")) {
        return array();
    }
    global $session, $dbinfo, $allqueries, $allqueriesbyfile;
    $dbinfo['queriesthishit']++;
    $fname = DBTYPE . "_query";
    $starttime = getmicrotime();
    $thisquery = array();
    $thisquery['query'] = $sql;
    $r = $fname($sql, LINK);
    if (!$r && $die === true) {
        if (defined("IS_INSTALLER")) {
            return array();
        } else {
            if ($session['user']['superuser'] & SU_DEVELOPER || 1) {
                require_once "lib/show_backtrace.php";
                die("<pre>" . HTMLEntities($sql, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "</pre>" . db_error(LINK) . show_backtrace());
            } else {
                die("A most bogus error has occurred.  I apologise, but the page you were trying to access is broken.  Please use your browser's back button and try again.");
            }
        }
    }
    $endtime = getmicrotime();
    if ($endtime - $starttime >= 1.0 && $session['user']['superuser'] & SU_DEBUG_OUTPUT) {
        $s = trim($sql);
        if (strlen($s) > 800) {
            $s = substr($s, 0, 400) . " ... " . substr($s, strlen($s) - 400);
        }
        debug("Slow Query (" . round($endtime - $starttime, 2) . "s): " . HTMLEntities($s, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "`n");
    }
    $thisquery['time'] = round($endtime - $starttime, 5);
    $trace = debug_backtrace();
    $thisquery['file1'] = $trace[0]['file'];
    $thisquery['line1'] = $trace[0]['line'];
    $thisquery['file2'] = $trace[1]['file'];
    $thisquery['line2'] = $trace[1]['line'];
    $allqueries[] = $thisquery;
    $allqueriesbyfile[$thisquery['file1']]['time'] += $thisquery['time'];
    $allqueriesbyfile[$thisquery['file1']]['hits'] += 1;
    unset($dbinfo['affected_rows']);
    $dbinfo['affected_rows'] = db_affected_rows();
    $dbinfo['querytime'] += $endtime - $starttime;
    return $r;
}
/**
 * Execute a SQLite query.
 * @return void
 */
function db_query(string $sql = '', bool $die = true)
{
    global $session, $dbinfo, $sqlite_resource;
    if (defined("DB_NODB") && !defined("LINK") && !is_object($sqlite_resource)) {
        return [];
    }
    $dbinfo['queriesthishit']++;
    $starttime = getmicrotime();
    //var_dump($sql);
    if (IS_INSTALLER) {
        $r = @$sqlite_resource->query($sql);
    } else {
        $r = $sqlite_resource->query($sql);
    }
    if (!$r && $die === true) {
        if (defined("IS_INSTALLER")) {
            return [];
        } else {
            if ($session['user']['superuser'] & SU_DEVELOPER || 1) {
                require_once "lib/show_backtrace.php";
                die("<pre>" . HTMLEntities($sql, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "</pre>" . db_error(LINK) . show_backtrace());
            } else {
                die("Please use your browser's back button and try again.");
            }
        }
    }
    $endtime = getmicrotime();
    if ($endtime - $starttime >= 1.0 && $session['user']['superuser'] & SU_DEBUG_OUTPUT) {
        $s = trim($sql);
        if (strlen($s) > 800) {
            $s = substr($s, 0, 400) . " ... " . substr($s, strlen($s) - 400);
        }
        debug("Slow Query (" . round($endtime - $starttime, 2) . "s): " . HTMLEntities($s, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "`n");
    }
    unset($dbinfo['affected_rows']);
    $dbinfo['affected_rows'] = db_affected_rows();
    $dbinfo['querytime'] += $endtime - $starttime;
    return $r;
}