Code:class oDebug extends sql_db{
protected $aResultQuery=array();
protected $aResultTime=array(); //temp
protected $aFilesRequired=array(); //list files
protected $aErrors=array(); // list of erors, warnings and notices
protected $timeSql=0; // total execution time of sql queries
// Array of the Classes default options
// - output type 'HTML' default empty puts debug output as text in footer
// - 'true' restricts visibility of debug output to admins or user IP
// - array of IP addresses that can view debug output if not an admin
protected $aDefaultOptions = array(
'render_type' => 'HTML', // Renderer type: HTML = display in html pop up page. Empty key will display verbose output at bottom of page
'restrict_access' => 'true', // change to 'false' to allow anyone access to the debugger
'allowed_ip' => array('83.84.1.180') // add your IP here will allow you to see the debugger even if not logged in as an admin (restrict_access must be 'true')
);
protected $aOptionsODebug = array(); //array of final options - can be set in file being debugged to verride the $aDefaultOptions array (they are combined)
protected $fStartTime; // start execution timer
protected $fEndTime; // end of execution time
// Constructor
// array $aOptions
// @return void
function __construct($dbhost, $dbuname, $dbpass, $dbname){
//$this->objet = $oObjet;
parent::sql_db($dbhost, $dbuname, $dbpass, $dbname, false);
$this->fStartTime = $this->getMicroTime();
$this->aOptionsODebug = $this->aDefaultOptions;
error_reporting (E_ALL);
set_error_handler(array($this,'MonitoringError'),E_ALL);
}
function sql_query($query) {
$sqlerr = mysql_error();
$sTypeQuery = strtoupper(trim(substr(trim($query), 0, 6)));
$query_start=microtime(true);
$output = parent::sql_query($query);
if (!isset($this->aResultQuery[$sTypeQuery])){
$this->aResultQuery[$sTypeQuery]=array();
}
$this->aResultQuery[$sTypeQuery][sizeof($this->aResultQuery[$sTypeQuery])]=array(
"query"=>$query,
"sqlerr"=>mysql_error(),
"time_query"=>round((microtime(true) - $query_start),4),
"memory_usage"=>function_exists ('memory_get_usage')?@memory_get_usage():"-1"
);
return $output;
}
// Destructor
// @return void
function __destruct(){
$this->fEndTime = $this->getMicroTime();
$this->aFilesRequired = get_required_files();
if ($this->allowDebug()){
switch ($this->aOptionsODebug['render_type']) {
case 'HTML':
$this->renderHTML();
break;
default:
echo "<pre>".
print_r($this->aResultQuery,true).
print_r($this->aResultTime,true).
print_r($this->aFilesRequired,true).
print_r($this->aErrors,true).
print_r($_COOKIE,true).
print_r($_ENV,true).
print_r($_FILES,true).
print_r($_GET,true).
print_r($_POST,true).
print_r($_REQUEST,true).
print_r($_SERVER,true).
print_r($_SESSION,true).
get_defined_constants(true).
"</pre>";
echo "Time taken to execute: ".round( ($this->fEndTime - $this->fStartTime),4);
break;
}
}
}
/**
******** PRIVATE FUNCTIONS ***************
*/
// add time inspector
public function setTime($sName=""){
$this->aResultTime[]=array("time"=>$this->getMicroTime(),"name"=>$sName);
}
// permission check to dislay output
protected function allowDebug(){
global $admin;
if ($this->aDefaultOptions['restrict_access'] === 'true') {
if (in_array($_SERVER['REMOTE_ADDR'],$this->aDefaultOptions['allowed_ip'])) { return true; }
elseif (is_admin($admin)) { return true; }
else { return false; }
}
if ($this->aDefaultOptions['restrict_access'] === 'false') return true;
}
// Return microtime from a timestamp
protected static function getMicroTime(){
list($usec, $sec) = explode(' ', microtime());
return (float)$usec + (float)$sec;
}
// MonitoringError
// return boolean
public function MonitoringError($iNumError, $sMsgError, $sScript, $iLine){
$aBackTrace = debug_backtrace();
$this->WriteTrace($aBackTrace, $iNumError, $sMsgError, $sScript, $iLine);
return true;
}
// Log errors
// return void
private function WriteTrace($aBackTrace, $iNumError, $sMsgError, $sScript, $iLine){
$iError=sizeof($this->aErrors);
$this->aErrors[$iError]["date"]=Date("d/m/Y H:i:s");
$this->aErrors[$iError]["line"]=$iLine;
if (isset($aBackTrace[$iError]["function"])){
$Function = $aBackTrace[$iError]["function"];
$this->aErrors[$iError]["function"]=$Function;
}
$this->aErrors[$iError]["error"] =$sMsgError;
$this->aErrors[$iError]["num_error"] = $iNumError;
$this->aErrors[$iError]["sScript"] = $sScript;
// $this->aErrors[$iError]["aBackTrace"] = $aBackTrace;
}
// HTML output generator
// return HTML output
private function renderHTML(){
$sOutPut=<<<eos
<div id="mainDebug" style="padding: 0;margin: 0;font-family: Arial, sans-serif;font-size: 12px;color: #333333;text-align:left;line-height: 12px;display:block;">
<div style="position:absolute; margin: 0;padding: 1px 5px;right: 0px;top: 10px;opacity: 0.80;filter: alpha(opacity:80);z-index: 10000;background-color:#dddddd;display:block;height:20px;">
<div style="float:left;text-align:center;display:block;">
<a onclick="if (document.getElementById('menuDebug').style.display=='inline'){document.getElementById('menuDebug').style.display='none';document.getElementById('paletteLog').style.display='none';document.getElementById('paletteConfig').style.display='none';document.getElementById('paletteTime').style.display='none';document.getElementById('paletteSQL').style.display='none';}else{document.getElementById('menuDebug').style.display='inline';}; return false;" href="#" style="color:#000000;text-decoration:none;"> <b> CA Debugger </b></a></div>
<ul style="display:none;padding:5px;margin-right:7px;-moz-padding-start:40px;list-style-type:disc;margin:1em 0;" id="menuDebug">
<li style="border-right:1px solid #aaaaaa;display:inline;list-style-image:none;list-style-position:outside;list-style-type:none;margin:0;padding:0 5px;"> <a onclick="if (document.getElementById('paletteConfig').style.display=='inline'){document.getElementById('paletteConfig').style.display='none';}else{document.getElementById('paletteConfig').style.display='inline';document.getElementById('paletteTime').style.display='none';document.getElementById('paletteLog').style.display='none';document.getElementById('paletteSQL').style.display='none';}; return false;" href="#" style="color:#000000;text-decoration:none;"> Variables & Configuration</a></li>
<li style="border-right:1px solid #aaaaaa;display:inline;list-style-image:none;list-style-position:outside;list-style-type:none;margin:0;padding:0 5px;"> <a onclick="if (document.getElementById('paletteLog').style.display=='inline'){document.getElementById('paletteLog').style.display='none';}else{document.getElementById('paletteLog').style.display='inline';document.getElementById('paletteConfig').style.display='none';document.getElementById('paletteTime').style.display='none';document.getElementById('paletteSQL').style.display='none';}; return false;" href="#" style="color:#000000;text-decoration:none;"> Logs & Messages</a></li>
<li style="border-right:1px solid #aaaaaa;display:inline;list-style-image:none;list-style-position:outside;list-style-type:none;margin:0;padding:0 5px;"> <a onclick="if (document.getElementById('paletteSQL').style.display=='inline'){document.getElementById('paletteSQL').style.display='none';}else{document.getElementById('paletteSQL').style.display='inline';document.getElementById('paletteConfig').style.display='none';document.getElementById('paletteLog').style.display='none';document.getElementById('paletteTime').style.display='none';}; return false;" href="#" style="color:#000000;text-decoration:none;"> SQL</a></li>
<li style="display:inline;list-style-image:none;list-style-position:outside;list-style-type:none;margin:0;padding:0 5px;"> <a onclick="if (document.getElementById('paletteTime').style.display=='inline'){document.getElementById('paletteTime').style.display='none';}else{document.getElementById('paletteTime').style.display='inline';document.getElementById('paletteConfig').style.display='none';document.getElementById('paletteLog').style.display='none';document.getElementById('paletteSQL').style.display='none';}; return false;" href="#" style="color:#000000;text-decoration:none;"> Time</a> </li>
</ul>
<a onclick="document.getElementById('mainDebug').style.display='none'; return false;" href="#" style="color:#ff0000;text-decoration:none;font-weight:bold;font-size:20px"> X </a> </div>
<div id="paletteConfig" style="background-color:#efefef; border-bottom:1px solid #aaaaaa;left:0;padding:10px;position:absolute;top:0;width:98%;z-index:9999;display: none;line-height:normal">{$this->renderPaletteConfig()}</div>
<div id="paletteLog" style="background-color:#efefef; border-bottom:1px solid #aaaaaa;left:0;padding:10px;position:absolute;top:0;width:98%;z-index:9999;display: none;line-height:normal"> {$this->renderPaletteLog()}</div>
<div id="paletteSQL" style="background-color:#efefef; border-bottom:1px solid #aaaaaa;left:0;padding:10px;position:absolute;top:0;width:98%;z-index:9999;display: none;line-height:normal"> {$this->renderPaletteSQL()}</div>
<div id="paletteTime" style="background-color:#efefef; border-bottom:1px solid #aaaaaa;left:0;padding:10px;position:absolute;top:0;width:98%;z-index:9999;display: none;line-height:normal"> {$this->renderPaletteTime()}</div>
</div>
eos;
echo $sOutPut;
}
// Vars & config generator
//return string
private function renderPaletteConfig(){
$sOutput='<span style="font-size:20px;">Variables & Configuration</span><br/><br/>';
$aVars=array( 'COOKIES'=>(isset($_COOKIE)) ? $_COOKIE : array("no cookie"),
'ENV'=>array_merge($_ENV,array("php Version"=>phpversion()),ini_get_all()),
'FILES'=>$_FILES,
'GET'=>$_GET,
'POST'=>$_POST,
'SERVER'=>$_SERVER,
'SESSION'=>(isset($_SESSION)) ? $_SESSION : array('debugger session'),
'CONSTANTS'=>get_defined_constants(true));
foreach ($aVars as $var=>$aVar){
$sOutput.='<span style="font-size:12px;font-weight:bold"><a onclick="if (document.getElementById(\'paletteConfig'.$var.'\').style.display==\'block\'){document.getElementById(\'paletteConfig'.$var.'\').style.display=\'none\';}else{document.getElementById(\'paletteConfig'.$var.'\').style.display=\'block\';}; return false;" href="#" style="color:#000000;text-decoration:none;">'.$var.' : </a></span><table border="0" cellspacing="4" style="font-size:10px;display:none;" id="paletteConfig'.$var.'">';
foreach ( $aVar as $k=>$v){
$sOutput.= "<tr style=\"height:10px\"><td><b style=\"font-size:12px\">".$k."</b></td><td>=></td><td><pre>".print_r($v,true)."</pre></td></tr>";
}
$sOutput.="</table><br/>";
}
return $sOutput;
}
// Logs HTML generator
// return string
private function renderPaletteLog(){
$sOutput='<span style="font-size:20px;">PHP Errors</span><br/><br/>';
$sOutput.=<<<eos
<table>
<tr style="font-size:10px;">
<td style="width:15px;background-color:#ffb32f;"></td><td style="width:100px">Warning</td>
<td style="width:15px;background-color:#8fb8ff;"></td><td style="width:100px">Strict</td>
<td style="width:15px;background-color:#fdff1f;"></td><td style="width:100px">Notice</td>
<td style="width:15px;background-color:#66ff66;"></td><td style="width:100px">OK</td>
</tr>
</table>
<table bgcolor="#66ff66" width="100%" border="1" cellspacing="0" cellpadding="2" summary="logs">
<tr>
<td></td>
<td width ="80px"><div align="center"><strong>After</strong></div></td>
<td><div align="center"><strong>File</strong></div></td>
<td><div align="center"><strong>Line</strong></div></td>
<td><div align="center"><strong>Function</strong></div></td>
<td><div align="center"><strong>Message</strong></div></td>
</tr>
eos;
foreach ($this->aFilesRequired as $k=>$v){
$sOutput.=<<<eos
<tr style="font-size:10px;">
<td></td>
<td>Included file</td>
<td>$v</td>
<td></td>
<td></td>
<td></td>
</tr>
eos;
}
foreach ($this->aErrors as $k=>$v){
switch ($v['num_error']){
case 1:
$sColor="ff5f65";
break;
case 2:
$sColor="ffb32f";
break;
case 2048:
$sColor="8fb8ff";
break;
case 8:
$sColor="fdff1f";
break;
default:
$sColor="66ff66";
break;
}
$temps=round( ($this->getMicroTime() - $this->fStartTime),4);
if($k===0){$v['function']="";}
$sOutput.=<<<eos
<tr style="font-size:10px;background-color:#$sColor;">
<td>$k</td>
<td>$temps ms</td>
<td>{$v['sScript']}</td>
<td>{$v['line']}</td>
<td>{$v['function']}</td>
<td>{$v['error']}</td>
</tr>
eos;
}
$sOutput.="</table>";
return $sOutput;
}
// SQL logs HTML generator
// return string
private function renderPaletteSQL(){
$sOutput='<span style="font-size:20px;">SQL queries</span><br/><br/>';
$sOutput.=<<<eos
<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="logs">
eos;
foreach ($this->aResultQuery as $k=>$v){
$sOutput.=<<<eos
<tr style="font-size:12px;">
<td colspan="4"><b>{$k}</b></td>
</tr>
<tr style="font-size:10px;">
<td><div align="center"><strong>Queries</strong></div></td>
<td><div align="center"><strong>Errors</strng></div></td>
<td><div align="center"><strong>Exec time</strong></div></td>
<td><div align="center"><strong>Memory usage</strong></div></td>
</tr>
eos;
foreach ($v as $vv){
$sOutput.=<<<eos
<tr style="font-size:10px;">
<td>{$vv['query']}</td>
<td>{$vv['sqlerr']}</td>
<td>{$vv['time_query']} ms</td>
<td>{$vv['memory_usage']} ms</td>
</tr>
eos;
$this->timeSql+=$vv['time_query'];
}
}
$sOutput.="</table>";
return $sOutput;
}
// Time logs HTML generator
// return string
private function renderPaletteTime(){
$totalTime=round( ($this->fEndTime - $this->fStartTime),4);
$phpTime=$totalTime-$this->timeSql;
$percentphp=round($phpTime/$totalTime*100,2);
$percentsql=100-$percentphp;
$sOutput='<span style="font-size:20px;">Timer</span><br/><br/>';
$sOutput.=<<<eos
<table width="50%" border="1" cellspacing="0" cellpadding="2" summary="logs" style="font-size:10px;">
<tr style="font-size:12px;">
<td><div align="center"><strong>Type</strong></div></td>
<td><div align="center"><strong>Elapsed time (ms)</strong></div></td>
<td><div align="center"><strong>Percent</strong></div></td>
</tr>
<tr>
<td><div align="center">Global</div></td>
<td><div align="center">$totalTime</div></td>
<td><div align="center">100 %</div></td>
</tr>
<tr>
<td><div align="center">PHP/HTML</div></td>
<td><div align="center">$phpTime</div></td>
<td><div align="center">$percentphp %</div></td>
</tr>
<tr>
<td><div align="center">SQL</div></td>
<td><div align="center">{$this->timeSql}</div></td>
<td><div align="center">$percentsql %</div></td>
</tr>
</table>
<br />
<table width="50%" border="1" cellspacing="0" cellpadding="2" summary="logs" style="font-size:10px;">
<tr style="font-size:12px;">
<td><div align="center"><strong>Code Part</strong></div></td>
<td><div align="center"><strong>Exec time</strong></div></td>
</tr>
eos;
foreach ($this->aResultTime as $k=>$v){
if($k===sizeof($this->aResultTime)-1){
$iTime=round( ($this->fEndTime - $v['time']),4);
}else{
$iTime=round( ($this->aResultTime[$k+1]['time'] - $v['time']),4);
}
$sName=(empty($v['name']))?$k+1:$v['name'];
$sOutput.=<<<eos
<tr style="font-size:12px;">
<td><div align="center"><strong>$sName</strong></div></td>
<td><div align="center"><strong>$iTime ms</strong></div></td>
</tr>
eos;
}$sOutput.="</table>";
return $sOutput;
}
}
?>
|