<?php

/**
 * A usefull debug class Please report bugs at kjarli@gmail.com
 * or at the website you downloaded this script from 
 * 
 * Ideas and suggestions always welcome!
 * 
 * @link       http://www.phphulp.nl/php/scripts/4/1168
 * @author     iltar van der berg
 * @version    1.0.0
 *
 */
class debug_console
{
	/**
	 * debug, if true, display errors and debug
	 *
	 * @var boolean
	 */
	public $debug = false;

	/**
	 * if true, overwrites the debug and shows ALL data
	 *
	 * @var boolean
	 */
	public $debug_extra = false;

	/**
	 * create an array of error messages to be returned
	 *
	 * @var array
	 */
	public $error_messages = array();

	/**
	 * the settings you want to load 
	 *
	 * @var array
	 */
	public $load_ini_settings = array();
	
	/**
	 * create a new and unique file for the debug console result
	 *
	 * @var bool
	 */
	public $debug_win_new_file = false;
	
	/**
	 * define the debug window width
	 *
	 * @var string
	 */
	public $debug_win_width = '750';
	
	/**
	 * define the debug window height
	 *
	 * @var string
	 */
	public $debug_win_height = '500';
	
	/**
	 * define the debug window width
	 *
	 * @var string
	 */
	public $debug_resizable = 'no';
	
	/**
	 * if true, one or more errors are returned
	 *
	 * @var boolean
	 */
	private $error = false;

	/**
	 * noting for now... 
	 * 
	 */
	public function __construct()
	{
		
	}

	/**
	 * manually print the array of the string
	 * than choose wether to echo the array or return it in a string
	 *
	 * @param array $array
	 * @param bool[optional] $echo_array
	 * @return string
	 */
	public function print_array($array, $echo_array = true)
	{
		if($echo_array)
		{
			echo '<pre>';
			print_r($array);
			echo '</pre>';
		}
		else
		{
			return '<pre>' . print_r($array, true) . '</pre>';
		}
	}

	/**
	 * return the array stats, use the print_array function to display it
	 *
	 * @param array $array
	 * @return array
	 */
	public function array_stats($array)
	{
		// for each array, give the number of keys and values
		$array_stats[] = 'The array has ' . count($array) . ' values and keys.';

		foreach($array as $key => $value)
		{
			if($value == NULL)
			{
				// there is no value
				$array_stats[] = 'The array key [' . $key . '] is <strong>NULL</strong>';
			}
			elseif(is_array($value))
			{
				// it is an array, walk thatone too
				$array_stats[] = $this->array_stats($value);
			}
			else
			{
				// it has a value
				$array_stats[] = 'The array key [' . $key . '] is ' . $value;
			}
		}

		return $array_stats;
	}

	/**
	 * upon destruction, return data to create the debug console
	 *
	 */
	public function __destruct()
	{
		// define the output string and create the html popup

		$output =
		'
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Debug Console</title>
			<style>
			body
			{
				padding : 10px;
				margin  : 0px;
			}
			
			h1,
			h2,
			h3,
			h4
			{
				font-family : verdana;
			}
			div
			{
				width : 100%;
			}
				
			pre
			{
				font-size   : 12px;
			}
			
			fieldset
			{
				margin-top    : 10px;
				border        : 1px solid black;
				
				font-family   : verdana;
				color         : black;
				font-size     : 11px;
				padding       : 12px;
				margin-bottom : 10px;
			}
			legend
			{
				border-top    : 1px solid black;
				border-left   : 1px solid black;
				border-right  : 1px solid black;
				padding       : 3px 10px;
			}
			
		</style>
	</head>
	
	<body>
		';

		// get the user input to check
		$php_ini_check_user = $this->load_ini_settings;

		// these fields need to be checked by default
		$php_ini_check_auto = array
		(
			'asp_tags',
			'short_open_tag',
			'safe_mode',
			'max_execution_time',
			'max_input_time',
			'memory_limit',
			'display_errors',
			'log_errors',
			'register_globals',
			'register_long_arrays',
			'magic_quotes_gpc',
			'file_uploads',
			'upload_tmp_dir',
			'upload_max_filesize',
			'sql.safe_mode',
			'session_name',
			'session.gc_maxlifetime'
		);

		$php_ini_check_this = array_merge($php_ini_check_auto, $php_ini_check_user);

		$output .= '<h3>php.ini settings</h3>';

		// for each ini setting, check their value and make them readable for humans
		foreach($php_ini_check_this as $key => $value)
		{
			$ini_value = ini_get($value);

			if(empty($ini_value) || strtolower($ini_value) == 'off' || $ini_value == 0 || !$ini_value)
			{
				$ini_value = 'false';
			}
			elseif(strtolower($ini_value) == 'on' || $ini_value == 1)
			{
				$ini_value = 'true';
			}

			// create a box with the result
			$php_ini_settings[$value] =
			'
			<fieldset style="width: 200px; float:left;">
				<legend>' . $value . '</legend>
				<strong>' .$ini_value . '</strong>
			</fieldset>
			';
			$output .= $php_ini_settings[$value];
		}

		if($this->debug)
		{
			// normal debug is true, let's create a box
			$output .= '<div style="float:right">';
			$output .= '<h3>Debug returned</h3>';

			// check if the user has started a session
			if(isset($_SESSION))
			{
				// print the session array
				$output .= '<fieldset><legend>Sessions</legend>';
				$output .= $this->print_array($_SESSION, false);
				$output .= '</fieldset>';
			}

			// print the globals
			$output .= '<fieldset><legend>Globals</legend>';
			$output .= $this->print_array($GLOBALS, false);
			$output .= '</fieldset>';

			// print the cookies
			$output .= '<fieldset><legend>Cookies</legend>';
			$output .= $this->print_array($_COOKIE, false);
			$output .= '</fieldset>';

			// print the files
			$output .= '<fieldset><legend>Files</legend>';
			$output .= $this->print_array($_FILES, false);
			$output .= '</fieldset>';

			// print the get
			$output .= '<fieldset><legend>Get</legend>';
			$output .= $this->print_array($_GET, false);
			$output .= '</fieldset>';

			// print the post
			$output .= '<fieldset><legend>Post</legend>';
			$output .= $this->print_array($_POST, false);
			$output .= '</fieldset>';

			// print the request
			$output .= '<fieldset><legend>Request</legend>';
			$output .= $this->print_array($_REQUEST, false);
			$output .= '</fieldset>';

			$output .= '</div>';
		}
		elseif($this->debug_extra)
		{
			// the user wants a full data array, let's give it to him
			$output .= '<div style="float:right">';
			$output .= '<h3>Debug extra returned</h3>';

			// print the Declared classes
			$output .= '<fieldset><legend>Declared classes</legend>';
			$output .= $this->print_array(get_declared_classes(), false);
			$output .= '</fieldset>';

			// print the Declared_interfaces
			$output .= '<fieldset><legend>Declared interfaces</legend>';
			$output .= $this->print_array(get_declared_interfaces(), false);
			$output .= '</fieldset>';

			// print the Defined constants
			$output .= '<fieldset><legend>Defined constants</legend>';
			$output .= $this->print_array(get_defined_constants(), false);
			$output .= '</fieldset>';

			// print the Defined functions
			$output .= '<fieldset><legend>Defined functions</legend>';
			$output .= $this->print_array(get_defined_functions(), false);
			$output .= '</fieldset>';

			// print the Included files
			$output .= '<fieldset><legend>Included files</legend>';
			$output .= $this->print_array(get_included_files(), false);
			$output .= '</fieldset>';

			// print the Loaded extension
			$output .= '<fieldset><legend>Loaded extension</legend>';
			$output .= $this->print_array(get_loaded_extensions(), false);
			$output .= '</fieldset>';
			$output .= '</div>';
		}
		
		$output .= 
		'
	</body>
</html>
		';
		
		$location = realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR;
		$location = preg_replace('#/|\\\#', DIRECTORY_SEPARATOR,  $location);
		
		// check wether user wants a new file to save results, or overwrite the old file
		if(!$this->debug_win_new_file)
		{
			$file = 'default_result.php';
			$debug_file = $location .  $file;
		}
		else
		{
			$file = date('H^i^s-d^m^y') . '_result.php';
			$debug_file = $location . $file;
		}

		if(!file_put_contents($debug_file, $output))
		{
			die('The file <strong>' . $debug_file . '</strong> could not be created');
		}
		
		echo '
<script type="text/javascript">
<!--
	var WinOpt = "width=' . $this->debug_win_width . '"; 
	WinOpt += ", height=' . $this->debug_win_height . '";
	WinOpt += ", resizable=' . $this->debug_resizable . '";
	WinOpt += ", scrollbars=yes";
	WinOpt += ", toolbar=no";
	WinOpt += ", location=no";
	WinOpt += ", directories=no";
	WinOpt += ", status=no";
	WinOpt += ", menubar=no";
	WinOpt += ", copyhistory=no";
	WinOpt += ", top=50";
	WinOpt += ", left=50";

	var debugWin = open(\'./' . $file . '\', \'DebugWindow\', WinOpt);
//-->
</script>
		';
	}
}

$debug = new debug_console();

//$debug->debug = true;
//$debug->debug_extra = true;
//$debug->debug_win_new_file = true;
//$debug->print_array($_SERVER, true);
//echo $debug->print_array($_SERVER, false);

//$debug->load_ini_settings = array('engine','zend.ze1_compatibility_mode','precision','y2k_compliance');

//$debug->print_array($debug->array_stats($array));

?>