<?php
//funciton by Arkadiusz Binder @2010-11-11 ; purpose to debug applications inc code easly
//example DEBUG_S(0,"Brak oczekiwanego pliku do zalaczenia",$DEBUG_ARR/STRING);
//version 2010-12-04
//changelog:
// * 2010-12-05 by plabudda: js(file in head not needed); style
// * 2010-12-06 by plabudda: $FILE,$FUNCTION,$LINE from debug_backtrace(); style
// * 2010-05-25 by plabudda: debug_backtrace() -> Exception->getTrace()

function DEBUG_S($DEBUG_LEVEL,$DESCRIPTION,$DEBUG_STRING = null,$FILE=null,$FUNCTION=null,$LINE=null) {
	static $debug_rand_color;
if(isset($_SESSION['DEBUG']))
  $SESSION['DEBUG']=$_SESSION['DEBUG']; // narzucamy zmienna session dla celowych wyzwolen DEBUG_S , bez ich koniecznego deklarowania jej jako SESSION , aby tylko raz uzyc
  else $SESSION['DEBUG']=null;
if($DEBUG_LEVEL<0) $SESSION['DEBUG']=abs($DEBUG_LEVEL); //jezeli podamy w parametr $DEBUG_LEVEL=-3 , to funkcja dziala tak, jakby zmienna sesji byla na $_SESSION['DEBUG']=3

	if (empty($SESSION['DEBUG'])) return;

	if ($FILE) $FILE = end(explode('/', $FILE));

	if (!is_array($debug_rand_color)) {
		$debug_rand_color = array();
  for($i=1;$i<2;$i++) {
			$debug_rand_color []= "#".dechex(rand(16, 64)).'00'.'00';
			$debug_rand_color []= "#".dechex(rand(64, 128)).'00'.'00';
			$debug_rand_color []= "#00".dechex(rand(16, 64)).dechex(rand(16, 128));
			$debug_rand_color []= "#".dechex(rand(64, 128)).dechex(rand(16, 64)).dechex(rand(16, 128));
			$debug_rand_color []= "#".dechex(rand(16, 64)).dechex(rand(64, 128)).dechex(rand(16, 128));
			$debug_rand_color []= "#".dechex(rand(64, 128)).dechex(rand(16, 128)).dechex(rand(16, 128));
			$debug_rand_color []= "#".dechex(rand(16, 64)).'00'.dechex(rand(64, 128));
			$debug_rand_color []= "#".dechex(rand(64, 128)).'00'.dechex(rand(16, 128));
			$debug_rand_color []= "#".dechex(rand(16, 64)).dechex(rand(64, 128)).dechex(rand(16, 128));
  }
 }
 
	if (next($debug_rand_color)); else reset($debug_rand_color);
	$color = current($debug_rand_color);

	$dbg_trace = '';
/*	if ($_SESSION['DEBUG'] > 0) {
		try {
			throw new Exception('DEBUG_S ...');
		} catch (Exception $e) {
			$first = true;
			$dbg_trace_arr = $e->getTrace();
			foreach ($dbg_trace_arr as $v) {
				$tmp_dbg_file = end(explode('/',$v['file']));
				$dbg_trace .= $tmp_dbg_file.'('.$v['line'].')'.':';
				if ($first) {
					$dbg_trace .= $DESCRIPTION;
					if (!$FILE) $FILE = $tmp_dbg_file;
					if (!$LINE) $LINE = $v['line'];
					$first = false;
				} else {
					$dbg_trace .= $v['function'];
				}
				$dbg_trace .= "\n";
			}//end foreach
		}
	}
*/
	if ($SESSION['DEBUG'] <= 1) {
		echo"\n".'<!--  '."DBG:\n".$dbg_trace.' -->'."\n";
	} else {
		if ($SESSION['DEBUG'] >= $DEBUG_LEVEL) {
			$_SESSION['DEBUG_BOX_ID'] = (isset($_SESSION['DEBUG_BOX_ID']))? $_SESSION['DEBUG_BOX_ID'] + 1 : 1;
	 
			$js = "var a=this.nextSibling;if(a.style.display=='none'){a.style.display='';}else{a.style.display='none';}return false;";
			echo'<div style="background:#fff;border:1px solid '.$color.';"><a href="#" onclick="'.$js.'" style="display:block;color:'.$color.';font-size:xx-small;">+DEBUG';
			echo':';
				echo "$FILE($LINE):$FUNCTION: <i>$DESCRIPTION</i>";
			echo'</a><div style="display:none;color:'.$color.';font-size:x-small;font-family:monospace;">';
			echo str_replace("\n", '<br />', $dbg_trace);
			if ($DEBUG_STRING) {
				echo'<hr />';
				if (is_string($DEBUG_STRING)) {
					if($SESSION['DEBUG']==3) echo''.$DEBUG_STRING; //jezeli typ 3 to robimy normalny html
					else 
					echo''.htmlspecialchars($DEBUG_STRING);
				} else {
					echo'<pre>';
					htmlspecialchars(print_r($DEBUG_STRING));
					echo'</pre>';
				}
			}

	if($SESSION['DEBUG']>6) {
				echo'<hr />';
				echo"enviroment:\n".'<br />';
		if(isset($_SERVER['REQUEST_URI'])) echo "<b>REQUEST_URI</b>:".$_SERVER['REQUEST_URI']."<br>\n";
		if(isset($_SERVER['SCRIPT_NAME'])) echo "<b>SCRIPT_NAME</b>:".$_SERVER['SCRIPT_NAME']."<br>\n";
		if(isset($_SERVER['SCRIPT_FILENAME'])) echo "<b>SCRIPT_FILENAME</b>:".$_SERVER['SCRIPT_FILENAME']."<br>\n";
		if(isset($_SERVER['REQUEST_METHOD'])) echo "<b>REQUEST_METHOD</b>:".$_SERVER['REQUEST_METHOD']."<br>\n";
		if(isset($_SERVER['QUERY_STRING'])) echo "<b>QUERY_STRING</b>:".$_SERVER['QUERY_STRING']."<br>\n";
		if(isset($_SERVER['HTTP_HOST'])) echo "<b>HTTP_HOST</b>:".$_SERVER['HTTP_HOST']." ";
	    if(isset($_SERVER['SERVER_ADDR'])) echo "<b>SERVER_ADDR</b>:".$_SERVER['SERVER_ADDR']." ";
		if(isset($_SERVER['SERVER_PORT'])) echo "<b>SERVER_PORT</b>:".$_SERVER['SERVER_PORT']."<br>\n";
    }
	
			echo'</div></div>'."\n";
		}// if($_SESSION['DEBUG']>=$DEBUG_LEVEL)
	}
}
