<?php
/***********************************************
* File      :   debug.php
* Project   :   Netwin php debug logs
************************************************/

global $debugstr;

$logfile;

function debug($str) {
    global $debugstr;
    $debugstr .= "$str\n";
}

function getDebugInfo() {
    global $debugstr;

    return $debugstr;
}
function dbg_init($file)
{
	global $logfile;
	$logfile=$file;
    if(file_exists($file) && filesize($file)>2000000) {
		dbg("File2=".$file);
		$parts=explode(".",$file);
		$file2=$parts[0]."1.".$parts[1];
		unlink($file2);
		rename($file,$file2);
	}
    if(ini_get('date.timezone')==""){
            date_default_timezone_set("UTC");
    }
}
function dbg($message) {
    global $auth_user;
    global $davroot;
    global $logfile;
    if(empty($logfile)) return;
    $user = (isset($auth_user))?"[". $auth_user ."] ":"";
    @$fp = fopen($logfile,"a");
    @$date = date("Y-m-d H:i:s");
    @fwrite($fp, "$date [". getmypid() ."] ". $user . "$message \n");
    @fclose($fp);
}

function zarafa_error_handler($errno, $errstr, $errfile, $errline, $errcontext=null) {
    $bt = debug_backtrace();
    switch ($errno) {
        case 8192:      // E_DEPRECATED since PHP 5.3.0
            // do not handle this message
            break;

        case E_NOTICE:
        case E_WARNING:
            dbg("ERROR:$errfile:$errline $errstr ($errno)");
            break;

        default:
            dbg("------------------------- ERROR BACKTRACE -------------------------");
            dbg("TRACE: $errfile:$errline $errstr ($errno) - backtrace: ". (count($bt)-1) . " steps");
            for($i = 1, $bt_length = count($bt); $i < $bt_length; $i++)
                debugLog("TRACE: $i:". $bt[$i]['file']. ":" . $bt[$i]['line']. " - " . ((isset($bt[$i]['class']))? $bt[$i]['class'] . $bt[$i]['type']:""). $bt[$i]['function']. "()");
            break;
    }
}

function vl($var)
{
	ob_start();
	var_dump($var);
	$dump = ob_get_contents();
	ob_end_clean();
	return $dump;
}

error_reporting(E_ALL);
set_error_handler("zarafa_error_handler");

function bar_encode($txt)
{
    $txt2 = preg_replace("/%/","%25",$txt);
    $txt = preg_replace("/\|/","%7C",$txt2);
    return $txt;
}

?>