Scripts

Debugger

Met dit script kun je debuggen zonder iets op de server te moeten installeren. gebruik: zoals je elke static class gebruikt.. dus Debug::function(); mogelijke functies: Debug::log(); registreerd de functie die debug::log() aanroept (je krijgt dus een lijst met aangeroepen functies, uitgebreider dus dan debug_backtrace();) Debug::output(boolean); print alle informatie uit zoals $_GET $_POST en $_SESSION; een boolean kan meegegeven worden om de output van $_SESSION te beperken. Debug::addDebuggerIp(array); hierin geef je een array met ip's op van de debugger. in dat geval word de output alleen geprint als de website aangeroepen worden vanaf de in die array staande ip addressen.

class.debug.php
<?php
class Debug
{
	private static $debuggerIpList;
	private static $calls;
	private static $vardumps;
	
	# this function will add an array of IP's from which a debugger accesses the website
	public static function addDebuggerIp(array $ipList)
	{
		self::$debuggerIpList = $ipList;
	}
	
	# this function will record the function and class it was called from.
	public static function log($message = null)
	{
		if(!is_array(self::$calls))
			self::$calls = array();
			
		$calls = debug_backtrace(false);
		$call = (isset($calls[1]))?$calls[1]:$calls[0];
		if(!isset($call['file']))
			$call['file'] = "PHP Core";
			
		$call['message'] = $message;
		array_push(self::$calls, $call);
	}
	
	# records a variable or array for output in the debug::output(); name is optional
	public static function vardump($vars, $name="")
	{
		if(!is_array(self::$vardumps))
			self::$vardumps = array();
			
		$calls = debug_backtrace(false);
		$call = (isset($calls[1]))?$calls[1]:$calls[0];
		
		$call['vardump'] = $vars;
		$call['file'] = $calls[0]['file'];
		$call['line'] = $calls[0]['line'];
		
		if($name != "")
			self::$vardumps[$name] = $call;
		else
			array_push(self::$vardump, $call); 
	}
	
	# will return the full path of the $file without the document_root.
	private static function basename($file)
	{
		return str_replace($_SERVER["DOCUMENT_ROOT"].DIRECTORY_SEPARATOR, '', $file);
	}
	
	# this will output all recorded vardumps, function calls, session, post and get. 
	#if $session set to false, the output of session will show only the name and the type of variable stored in it.
	public static function output($session = true)
	{
		if(is_array(self::$debuggerIpList))
			if(!in_array($_SERVER["REMOTE_ADDR"], self::$debuggerIpList))
				return false;
		$args = "";
		$debugs = array_reverse(debug_backtrace(false));
		
		print("<div style='margin:20px;'>");
		
		# print all $_SESSION variables, if they are objects, they could cause problems so 
		# use the try/catch for proper error handling
		print('<pre style="padding:0; margin:0;"><strong>$_SESSION:</strong></pre>');
		try 
		{
			print('<pre style="padding:0; margin:0;">');
			if(isset($_SESSION) && count($_SESSION)>0 && $session)
				@print_r($_SESSION);
			else if(isset($_SESSION) && count($_SESSION) > 0 && !$session)
				foreach($_SESSION as $key=>$value)
					print('$_SESSION["'.$key.'"] contains: '.gettype($value).' <br/>');
			else
				print('No $_SESSION variables');
			print('</pre><br/>');				
		}
		catch(Exception $e)
		{
			print('<pre style="padding:0; margin:0;">'.$e.'</pre><br />');
		}
		
		# print all $_POST variables
		print('<pre style="padding:0; margin:0;"><strong>$_POST:</strong></pre>
		<pre style="padding:0; margin:0;">');
		if(count($_POST)>0)
			print_r($_POST);
		else
			print('No $_POST variables');
		print('</pre><br/>');
		
		# print all $_GET variables
		print('<pre style="padding:0; margin:0;"><strong>$_GET:</strong></pre>
		<pre style="padding:0; margin:0;">');
		if(count($_GET)!=0)
			print_r($_GET);
		else
			print('No $_GET variables');
		print('</pre><br/>');
		
		# print all vardumps
		if(!empty(self::$vardumps))
			foreach(self::$vardumps as $key => $vardump)
				print('<pre style="padding:0; margin:0;"><strong>'.$key.' in '.((!empty($vardump['class'])?$vardump['class'].$vardump['type']:'')).$vardump['function'].'() ('.self::basename($vardump['file']).') on line '.$vardump['line'].':</strong></pre>
				<pre style="padding:0; margin:0;">'.print_r($vardump['vardump'], true).'</pre></br>');
		
		# print list of used files
		print('<pre style="padding:0; margin:0;"><strong>Used files:</strong></pre>');
		$included_files = get_included_files();
		if(count($included_files)>0)
			foreach ($included_files as $filename) 
	    		print('<pre style="padding:0; margin:0;">'.self::basename($filename).'</pre>');
		else
			print('<pre style="padding:0; margin:0;">No used files</pre>');

		# print logged function calls
		print('<br/><table border="0" style="border-collapse:collapse;"><tr><td colspan="2" style="padding:0; margin:0;"><pre style="padding:0; margin:0;"><strong>Logged function calls:</strong></pre></td></tr>');
		if(!empty(self::$calls))
		{
			foreach(self::$calls as $call)
			{
				if(!empty($call['args']))
				foreach($call['args'] as $arg)
				{
					if($args=="")
						$args .=self::basename($arg);
					else
						$args .=", ".self::basename($arg);
				}
				print("<tr><td style='padding-right:20px;'><pre style='padding:0; margin:0;'>".self::basename($call['file'])."</pre></td>
				 <td> <pre style='padding:0; margin:0;'>: ".((!empty($call['class']))?$call['class'].$call['type']:'').$call['function']."(".$args.")".((!empty($call['message']))?": ".$call['message']:'')." ".((isset($call['line']))?"on line ".$call['line']:'')."</pre>
				 </td></tr>\n");
				$args = "";	
			}	
		}
		else
			print('<tr><td><pre style="padding:0; margin:0;">No calls logged!</pre></td></tr>');
		
		# print debug_backtrace() in a readable way
		print('</table><br/><table border="0" style="border-collapse:collapse;"><tr><td colspan="2" style="padding:0; margin:0;"><pre style="padding:0; margin:0;"><strong>Debug backtrace:</strong></pre></td></tr>');
	
		foreach($debugs as $call)
		{
			if(isset($call['class']) && $call['class']!="Debug")
			{
				foreach($call['args'] as $arg)
				{
					if($args=="")
						$args .=self::basename($arg);
					else
						$args .=", ".self::basename($arg);
				}
				print("<tr><td style='padding-right:20px;'><pre style='padding:0; margin:0;'>".self::basename($call['file'])."</pre></td>
				 <td> <pre style='padding:0; margin:0;'>: ".((!empty($call['class']))?$call['class'].$call['type']:'').$call['function']."(".$args.")".((!empty($call['message']))?": ".$call['message']:'')." ".((isset($call['line']))?"on line ".$call['line']:'')."</pre>
				 </td></tr>\n");
				$args = "";
			}	
		}
		print('</table></div>');
	}
}

Reacties

0
Nog geen reacties.