class.debug.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?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>');
}
}
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>');
}
}