/**
* Error and message handler, call with trigger_error if required
*/
function msg_handler($errno, $errtext, $errfile, $errline)
{
    global $db, $config;
    $errdate = date("Y-m-d H:i:s (T)");
    $errortype = array(E_ERROR => "PHP Error", E_WARNING => "PHP Warning", E_NOTICE => "PHP Notice", E_USER_ERROR => "User Error", E_USER_WARNING => "User Warning", E_USER_NOTICE => "User Notice");
    if ($config['error_log'] == 1) {
        switch ($errno) {
            case E_WARNING:
            case E_NOTICE:
            case E_USER_WARNING:
            case E_USER_NOTICE:
            case E_USER_ERROR:
            case E_ERROR:
                $errlogfile = 'data/errlog_php.inc.php';
                $new = array();
                if (file_exists($errlogfile)) {
                    $lines = file($errlogfile);
                    foreach ($lines as $row) {
                        $row = trim($row);
                        if (!empty($row)) {
                            $new[] = $row;
                        }
                    }
                } else {
                    $new = array();
                }
                $errtext2 = str_replace(array("\r\n", "\n", "\r", "\t"), " ", $errtext);
                $sru = str_replace(array("\r\n", "\n", "\r", "\t"), " ", $_SERVER['REQUEST_URI']);
                $new[] = $errno . "\t" . $errtext2 . "\t" . $errfile . "\t" . $errline . "\t" . $sru . "\t" . time() . "\t" . PHP_VERSION . " (" . PHP_OS . ")";
                file_put_contents($errlogfile, implode("\n", $new));
                break;
        }
    }
    switch ($errno) {
        case E_WARNING:
        case E_NOTICE:
        case E_USER_WARNING:
        case E_USER_NOTICE:
            echo "<br /><strong>" . $errortype[$errno] . "</strong>: " . $errtext . " (File: <tt>" . $errfile . "</tt> on line <tt>" . $errline . "</tt>)";
            break;
        case E_USER_ERROR:
        case E_ERROR:
            if (isset($db)) {
                $db->close();
            }
            if (function_exists('ob_clean')) {
                @ob_clean();
            }
            ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
		<title>Viscacha <?php 
            echo $config['version'];
            ?>
 &raquo; Error</title>
		<style type="text/css">
		<!--
		body{
			color: #000000;
			background-color: #FAFAFA;
			font-size: 12px;
			line-height: 130%;
			font-family: Sans-Serif;
			margin-left: 10%;
			margin-right: 10%;
			border: 1px solid #aaaaaa;
		}
		p {
			margin: 0px;
			padding: 2px;
			padding-left: 15px;
			padding-right: 5px;
		}
		a {
			color: #A80000;
		}
		a:hover {
			color: #000000;
		}
		h1 {
			text-align: center;
			padding: 10px;
			margin: 0px;
			margin-bottom: 20px;
			background-color: #eeeeee;
			border-bottom: 1px solid #aaaaaa;
		}
		h3 {
			padding: 0px;
			margin: 0px;
			padding-left: 5px;
			padding-right: 5px;
			margin-bottom: 7px;
			margin-top: 20px;
			color: #A80000;
			border-bottom: 1px solid #EEE;
		}
		.code {
			background: #FFFFFF;
			border: 1px solid #dddddd;
			margin-right: 5px;
			margin-bottom: 2px;
			margin-top: 2px;
			margin-left: 15px;
			padding: 2px;
			font-family: Monospace;
			list-style: none;
		}
		.lineone {
			padding:0 5px;
			margin:2px 0;
			background:#F9F9F9;
		}
		.center {
			text-align: center;
		}
		.linetwo {
			padding:0 5px;
			margin:2px 0;
			background:#FCFCFC;
		}
		.mark {
			padding:0 5px;
			margin:2px 0;
			background: #eedddd;
			color: #880000;
			font-weight: bold;
		}
		-->
		</style>
	</head>
	<body>
		<h1>General Error</h1>
		<p class="center">
			[<a href="<?php 
            echo $config['furl'];
            ?>
/index.php">Return to Index</a>]
			<?php 
            if (check_hp($_SERVER['HTTP_REFERER'])) {
                ?>
			&nbsp;&nbsp;[<a href="<?php 
                echo htmlspecialchars($_SERVER['HTTP_REFERER']);
                ?>
">Return to last Page</a>]
			<?php 
            }
            ?>
		</p>
		<h3>Error Message</h3>
		<p><strong><?php 
            echo $errortype[$errno];
            ?>
</strong>: <?php 
            echo $errtext;
            ?>
</p>
		<h3>Error Details</h3>
		<p>
			File: <?php 
            echo $errfile;
            ?>
<br />
			Line: <?php 
            echo $errline;
            ?>
<br />
			Date: <?php 
            echo $errdate;
            ?>
<br />
		</p>
		<h3>Code Snippet</h3>
		<?php 
            echo getErrorCodeSnippet($errfile, $errline);
            ?>
		<h3>Backtrace</h3>
		<?php 
            echo get_backtrace();
            ?>
		<h3>Contact</h3>
		<p>Please notify the board administrator: <a href="mailto:<?php 
            echo $config['forenmail'];
            ?>
"><?php 
            echo $config['forenmail'];
            ?>
</a></p>
		<h3>Copyright</h3>
		<p>
			<strong><a href="http://www.viscacha.org" target="_blank">Viscacha <?php 
            echo $config['version'];
            ?>
</a></strong><br />
			Copyright &copy; by MaMo Net
		</p>
	</body>
</html>
			<?php 
            exit;
            break;
    }
}
/**
* Custom Error handler, call with trigger_error if required.
*/
function msg_handler($errno, $errtext, $errfile, $errline)
{
    $replevel = error_reporting();
    // If the @ error suppression operator was used, error_reporting is temporarily set to 0
    if ($replevel == 0) {
        return;
    }
    // This checks whether the error should be shown - according to what we set before with error_reporting()
    if (($errno & $replevel) != $errno) {
        return;
    }
    global $db, $config, $error_handler_history;
    $errdate = date("Y-m-d H:i:s (T)");
    $errortype = array(E_RECOVERABLE_ERROR => "PHP Error", E_WARNING => "PHP Warning", E_NOTICE => "PHP Notice", E_DEPRECATED => "PHP Deprecated", E_STRICT => "PHP Strict", E_USER_ERROR => "Viscacha Error", E_USER_WARNING => "Viscacha Warning", E_USER_NOTICE => "Viscacha Notice", E_USER_DEPRECATED => "Viscacha Deprecated");
    log_handler($errno, $errtext, $errfile, $errline);
    switch ($errno) {
        case E_WARNING:
        case E_NOTICE:
        case E_USER_WARNING:
        case E_USER_NOTICE:
            echo "<br /><strong>{$errortype[$errno]}</strong>: {$errtext} (File: <tt>{$errfile}</tt> on line <tt>{$errline}</tt>)";
            $error_handler_history[] = compact('errno', 'errtext', 'errfile', 'errline');
            return true;
            // Avoid PHP error handler
            break;
        case E_USER_ERROR:
        case E_RECOVERABLE_ERROR:
            if (isset($db) && is_a($db, 'DB_Driver')) {
                $db->close();
            }
            while (ob_get_length() !== false) {
                ob_end_clean();
            }
            ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
		<title>Viscacha <?php 
            echo $config['version'];
            ?>
 &raquo; Error</title>
		<style type="text/css">
		<!--
		body { color: #000; background-color: #fafafa; font-size: 12px; line-height: 130%; font-family: sans-serif; margin: auto 10%; border: 1px solid #aaa; }
		p { margin: 0; padding: 2px 5px 2px 15px; }
		a { color: #a80000; }
		a:hover { color: #000; }
		h1 { text-align: center; padding: 10px; margin: 0 0 20px 0; background-color: #eee; border-bottom: 1px solid #aaaaaa; }
		h3 { padding: 0 5px; margin: 20px 0 7px 0; color: #a80000; border-bottom: 1px solid #eee; }
		.code { background: #fff; border: 1px solid #ddd; margin: 2px 5px 2px 15px; padding: 2px; font-family: monospace; list-style: none; }
		.lineone { padding: 0 5px; margin: 2px 0; background:#f9f9f9; }
		.linetwo { padding: 0 5px; margin: 2px 0; background: #fcfcfc; }
		.mark { padding: 0 5px; margin: 2px 0; background: #eedddd; color: #880000; font-weight: bold; }
		.center { text-align: center; }
		-->
		</style>
	</head>
	<body>
		<h1><?php 
            echo $errortype[$errno];
            ?>
</h1>
		<p class="center">
			[<a href="<?php 
            echo $config['furl'];
            ?>
/index.php">Return to Index</a>]
			<?php 
            if (check_hp($_SERVER['HTTP_REFERER'])) {
                ?>
			&nbsp;&nbsp;[<a href="<?php 
                echo htmlspecialchars($_SERVER['HTTP_REFERER']);
                ?>
">Return to last Page</a>]
			<?php 
            }
            ?>
		</p>
		<h3>Error Message</h3>
		<p><?php 
            echo $errtext;
            ?>
</p>
		<h3>Error Details</h3>
		<p>
			<strong>File:</strong> <?php 
            echo $errfile;
            ?>
<br />
			<strong>Line:</strong> <?php 
            echo $errline;
            ?>
<br />
			<strong>Date:</strong> <?php 
            echo $errdate;
            ?>
		</p>
		<h3>Code Snippet</h3>
		<?php 
            echo getErrorCodeSnippet($errfile, $errline);
            ?>
		<h3>Backtrace</h3>
		<?php 
            echo get_backtrace(2);
            ?>
		<?php 
            if (count($error_handler_history) > 0) {
                ?>
		<h3>Previous Notices and Warnings</h3>
		<p>Additionally <?php 
                echo count($error_handler_history);
                ?>
 notices and/or warnings occured:</p>
		<ul>
		<?php 
                foreach ($error_handler_history as $e) {
                    ?>
			<li>
				<strong><?php 
                    echo $errortype[$e['errno']];
                    ?>
</strong>: <?php 
                    echo $e['errtext'];
                    ?>
<br />
				File: <tt><?php 
                    echo $e['errfile'];
                    ?>
</tt> - Line: <tt><?php 
                    echo $e['errline'];
                    ?>
</tt>
			</li>
		<?php 
                }
                ?>
		</ul>
		<?php 
            }
            ?>
		<h3>Contact</h3>
		<p>Please notify the board administrator: <a href="mailto:<?php 
            echo $config['forenmail'];
            ?>
"><?php 
            echo $config['forenmail'];
            ?>
</a></p>
		<h3>Copyright</h3>
		<p>
			Powered by <strong><a href="http://www.viscacha.org" target="_blank">Viscacha <?php 
            echo $config['version'];
            ?>
</a></strong><br />
			Copyright &copy; 2004-2009, The Viscacha Project
		</p>
	</body>
</html>
			<?php 
            exit;
            break;
        default:
            // E_STRICT
            return;
            // Do nothing
            break;
    }
}