1 <?php
2 /************************************************
3 ** Title.........: Debug Lib
4 ** Version.......: 0.5.4
5 ** Author........: Thomas Schüßler <tulpe@atomar.de>
6 ** Filename......: debuglib.php(s)
7 ** Last changed..: 16. July 2003
8 ** License.......: Free to use. Postcardware ;)
9 **
10 *************************************************
11 **
12 ** Functions in this library:
13 **
14 ** print_a( array array [,int mode] )
15 ** prints arrays in a readable, understandable form.
16 ** if mode is defined the function returns the output instead of
17 ** printing it to the browser
18 **
19 **
20 ** show_vars([int mode])
21 ** use this function on the bottom of your script to see all
22 ** superglobals and global variables in your script in a nice
23 ** formated way
24 **
25 ** show_vars() without parameter shows $_GET, $_POST, $_SESSION,
26 ** $_FILES and all global variables you've defined in your script
27 **
28 ** show_vars(1) shows $_SERVER and $_ENV in addition
29 **
30 **
31 **
32 ** print_result( result_handle )
33 ** prints a mysql_result set returned by mysql_query() as a table
34 ** this function is work in progress! use at your own risk
35 **
36 **
37 **
38 **
39 ** Happy debugging and feel free to email me your comments.
40 **
41 **
42 **
43 ** History: (starting with version 0.5.3 at 2003-02-24)
44 **
45 ** - added tooltips to the td's showing the type of keys and values (thanks Itomic)
46 ** 2003-07-16
47 ** - pre() function now trims trailing tabs
48 ************************************************/
51 # This file must be the first include on your page.
53 /* used for tracking of generation-time */
54 {
55 $MICROTIME_START = microtime();
56 @$GLOBALS_initial_count = count($GLOBALS);
57 }
59 /************************************************
60 ** print_a class and helper function
61 ** prints out an array in a more readable way
62 ** than print_r()
63 **
64 ** based on the print_a() function from
65 ** Stephan Pirson (Saibot)
66 ************************************************/
68 class Print_a_class {
70 # this can be changed to FALSE if you don't like the fancy string formatting
71 var $look_for_leading_tabs = TRUE;
73 var $output;
74 var $iterations;
75 var $key_bg_color = '1E32C8';
76 var $value_bg_color = 'DDDDEE';
77 var $fontsize = '8pt';
78 var $keyalign = 'center';
79 var $fontfamily = 'Verdana';
80 var $export_flag;
81 var $show_object_vars;
82 var $export_dumper_path = 'http://tools.www.mdc.xmc.de/print_a_dumper/print_a_dumper.php';
83 # i'm still working on the dumper! don't use it now
84 # put the next line into the print_a_dumper.php file (optional)
85 # print htmlspecialchars( stripslashes ( $_POST['array'] ) );
86 var $export_hash;
88 function Print_a_class() {
89 $this->export_hash = uniqid('');
90 }
92 # recursive function!
93 function print_a($array, $iteration = FALSE, $key_bg_color = FALSE) {
94 $key_bg_color or $key_bg_color = $this->key_bg_color;
96 # if print_a() was called with a fourth parameter (1 or 2)
97 # and you click on the table a window opens with only the output of print_a() in it
98 # 1 = serialized array
99 # 2 = normal print_a() display
101 /* put the following code on the page defined with $export_dumper_path;
102 --->%---- snip --->%----
104 if($_GET['mode'] == 1) {
105 print htmlspecialchars( stripslashes ( $_POST['array'] ) );
106 } elseif($_GET['mode'] == 2) {
107 print_a(unserialize( stripslashes($_POST['array'])) );
108 }
110 ---%<---- snip ---%<----
111 */
113 if( !$iteration && isset($this->export_flag) ) {
114 $this->output .= '<form id="pa_form_'.$this->export_hash.'" action="'.$this->export_dumper_path.'?mode='.$this->export_flag.'" method="post" target="_blank"><input name="array" type="hidden" value="'.htmlspecialchars( serialize( $array ) ).'"></form>';
115 }
117 # lighten up the background color for the key td's =)
118 if( $iteration ) {
119 for($i=0; $i<6; $i+=2) {
120 $c = substr( $key_bg_color, $i, 2 );
121 $c = hexdec( $c );
122 ( $c += 15 ) > 255 and $c = 255;
123 isset($tmp_key_bg_color) or $tmp_key_bg_color = '';
124 $tmp_key_bg_color .= sprintf( "%02X", $c );
125 }
126 $key_bg_color = $tmp_key_bg_color;
127 }
129 # build a single table ... may be nested
130 $this->output .= '<table summary="" style="border:none;" cellspacing="1" '.( !$iteration && $this->export_flag ? 'onClick="document.getElementById(\'pa_form_'.$this->export_hash.'\').submit();" )' : '' ).'>';
131 foreach( $array as $key => $value ) {
133 $value_style = 'color:black;';
134 $key_style = 'color:white;';
136 $type = gettype( $value );
137 # print $type.'<br />';
139 # change the color and format of the value
140 switch( $type ) {
141 case 'array':
142 break;
144 case 'object':
145 $key_style = 'color:#FF9B2F;';
146 break;
148 case 'integer':
149 $value_style = 'color:green;';
150 break;
152 case 'double':
153 $value_style = 'color:red;';
154 break;
156 case 'bool':
157 $value_style = 'color:blue;';
158 break;
160 case 'resource':
161 $value_style = 'color:darkblue;';
162 break;
164 case 'string':
165 if( $this->look_for_leading_tabs && preg_match('/^\t/m', $value) ) {
166 $search = array('/\t/', "/\n/");
167 $replace = array(' ','<br />');
168 $value = preg_replace( $search, $replace, htmlspecialchars( $value ) );
169 $value_style = 'color:black;border:1px gray dotted;';
170 } else {
171 $value_style = 'color:black;';
172 $value = nl2br( htmlspecialchars( $value ) );
173 }
174 break;
175 }
177 $this->output .= '<tr>';
178 $this->output .= '<td nowrap align="'.$this->keyalign.'" style="background-color:#'.$key_bg_color.';'.$key_style.';font:bold '.$this->fontsize.' '.$this->fontfamily.';" title="'.gettype( $key ).'['.$type.']">';
179 $this->output .= $key;
180 $this->output .= '</td>';
181 $this->output .= '<td nowrap="nowrap" style="background-color:#'.$this->value_bg_color.';font: '.$this->fontsize.' '.$this->fontfamily.'; color:black;">';
184 # value output
185 if($type == 'array') {
186 if(count($value)){
187 $this->print_a( $value, TRUE, $key_bg_color );
188 }else{
189 $this->output .= '<div style="color:blue;">Array (empty)</div>';
190 }
191 } elseif($type == 'object') {
192 if( $this->show_object_vars ) {
193 $this->print_a( get_object_vars( $value ), TRUE, $key_bg_color );
194 } else {
195 $this->output .= '<div style="'.$value_style.'">OBJECT - '.get_class($value).'</div>';
196 }
197 } else {
198 $this->output .= '<div style="'.$value_style.'" title="'.$type.'">'.$value.'</div>';
199 }
201 $this->output .= '</td>';
202 $this->output .= '</tr>';
203 }
204 $this->output .= '</table>';
205 }
206 }
208 # helper function.. calls print_a() inside the print_a_class
209 function print_a( $array, $return_mode = FALSE, $show_object_vars = FALSE, $export_flag = FALSE ) {
210 $e= error_reporting (0);
211 if( is_array( $array ) or is_object( $array ) ) {
212 $pa = new Print_a_class;
213 $show_object_vars and $pa->show_object_vars = TRUE;
214 $export_flag and $pa->export_flag = $export_flag;
216 $pa->print_a( $array );
218 # $output = $pa->output; unset($pa);
219 $output = &$pa->output;
220 } else {
221 $output = '<span style="color:red;font-size:small;">print_a( '.gettype( $array ).' )</span>';
222 }
224 error_reporting ($e);
225 if($return_mode) {
226 return $output;
227 } else {
228 print $output;
229 return TRUE;
230 }
231 }
234 // shows mysql-result as a table.. # not ready yet :(
235 function print_result($RESULT) {
237 if(!$RESULT) return;
239 $fieldcount = mysql_num_fields($RESULT);
241 for($i=0; $i<$fieldcount; $i++) {
242 $tables[mysql_field_table($RESULT, $i)]++;
243 }
245 print '
246 <style type="text/css">
247 .rs_tb_th {
248 font-family: Verdana;
249 font-size:9pt;
250 font-weight:bold;
251 color:white;
252 }
253 .rs_f_th {
254 font-family:Verdana;
255 font-size:7pt;
256 font-weight:bold;
257 color:white;
258 }
259 .rs_td {
260 font-family:Verdana;
261 font-size:7pt;
262 }
263 </style>
264 <script type="text/javascript" language="JavaScript">
265 var lastID;
266 function highlight(id) {
267 if(lastID) {
268 lastID.style.color = "#000000";
269 lastID.style.textDecoration = "none";
270 }
271 tdToHighlight = document.getElementById(id);
272 tdToHighlight.style.color ="#FF0000";
273 tdToHighlight.style.textDecoration = "underline";
274 lastID = tdToHighlight;
275 }
276 </script>
277 ';
279 print '<table summary="" border="0" bgcolor="#000000" cellspacing="1" cellpadding="1">';
281 print '<tr>';
282 foreach($tables as $tableName => $tableCount) {
283 $col == '0054A6' ? $col = '003471' : $col = '0054A6';
284 print '<th colspan="'.$tableCount.'" class="rs_tb_th" style="background-color:#'.$col.';">'.$tableName.'</th>';
285 }
286 print '</tr>';
288 print '<tr>';
289 for($i=0;$i < mysql_num_fields($RESULT);$i++) {
290 $FIELD = mysql_field_name($RESULT, $i);
291 $col == '0054A6' ? $col = '003471' : $col = '0054A6';
292 print '<td align="center" bgcolor="#'.$col.'" class="rs_f_th">'.$FIELD.'</td>';
293 }
294 print '</tr>';
296 mysql_data_seek($RESULT, 0);
298 while($DB_ROW = mysql_fetch_array($RESULT, MYSQL_NUM)) {
299 $pointer++;
300 if($toggle) {
301 $col1 = "E6E6E6";
302 $col2 = "DADADA";
303 } else {
304 $col1 = "E1F0FF";
305 $col2 = "DAE8F7";
306 }
307 $toggle = !$toggle;
308 print '<tr id="ROW'.$pointer.'" onMouseDown="highlight(\'ROW'.$pointer.'\');">';
309 foreach($DB_ROW as $value) {
310 $col == $col1 ? $col = $col2 : $col = $col1;
311 print '<td valign="top" bgcolor="#'.$col.'" class="rs_td" nowrap>'.nl2br($value).'</td>';
312 }
313 print '</tr>';
314 }
315 print '</table>';
316 mysql_data_seek($RESULT, 0);
317 }
319 function _script_globals() {
320 global $GLOBALS_initial_count;
322 $varcount = 0;
324 foreach($GLOBALS as $GLOBALS_current_key => $GLOBALS_current_value) {
325 if(++$varcount > $GLOBALS_initial_count) {
326 /* die wollen wir nicht! */
327 if ($GLOBALS_current_key != 'HTTP_SESSION_VARS' && $GLOBALS_current_key != '_SESSION') {
328 $script_GLOBALS[$GLOBALS_current_key] = $GLOBALS_current_value;
329 }
330 }
331 }
333 unset($script_GLOBALS['GLOBALS_initial_count']);
334 return $script_GLOBALS;
335 }
337 function show_runtime() {
338 $MICROTIME_END = microtime();
339 $MICROTIME_START = explode(' ', $GLOBALS['MICROTIME_START']);
340 $MICROTIME_END = explode(' ', $MICROTIME_END);
341 $GENERATIONSEC = $MICROTIME_END[1] - $MICROTIME_START[1];
342 $GENERATIONMSEC = $MICROTIME_END[0] - $MICROTIME_START[0];
343 $GENERATIONTIME = substr($GENERATIONSEC + $GENERATIONMSEC, 0, 8);
345 return '<span style="color:red;font-weight:normal;font-size:9px;">(runtime: '.$GENERATIONTIME.' sec)</span>';
346 }
349 ######################
350 # function shows all superglobals and script defined global variables
351 # show_vars() without the first parameter shows all superglobals except $_ENV and $_SERVER
352 # show_vars(1) shows all
353 # show_vars(,1) shows object properties in addition
354 #
355 function show_vars($show_all_vars = FALSE, $show_object_vars = FALSE) {
356 if(isset($GLOBALS['no_vars'])) return;
358 $script_globals = _script_globals();
359 print '
360 <style type="text/css">
361 .vars-container {
362 font-family: Verdana, Arial, Helvetica, Geneva, Swiss, SunSans-Regular, sans-serif;
363 font-size: 8pt;
364 padding:5px;
365 }
366 .varsname {
367 font-weight:bold;
368 }
369 </style>
370 ';
372 print '<br />
373 <div style="border-style:dotted;border-width:1px;padding:2px;font-family:Verdana;font-size:10pt;font-weight:bold;">
374 DEBUG '.show_runtime().'
375 ';
377 $vars_arr['script_globals'] = array('global script variables', '#7ACCC8');
378 $vars_arr['_GET'] = array('$_GET', '#7DA7D9');
379 $vars_arr['_POST'] = array('$_POST', '#F49AC1');
380 $vars_arr['_FILES'] = array('$_POST FILES', '#82CA9C');
381 $vars_arr['_SESSION'] = array('$_SESSION', '#FCDB26');
382 $vars_arr['_COOKIE'] = array('$_COOKIE', '#A67C52');
384 if($show_all_vars) {
385 $vars_arr['_SERVER'] = array('SERVER', '#A186BE');
386 $vars_arr['_ENV'] = array('ENV', '#7ACCC8');
387 }
389 foreach($vars_arr as $vars_name => $vars_data) {
390 if($vars_name != 'script_globals') global $$vars_name;
391 if($$vars_name) {
392 print '<div class="vars-container" style="background-color:'.$vars_data[1].';"><span class="varsname">'.$vars_data[0].'</span><br />';
393 print_a($$vars_name, FALSE, $show_object_vars, FALSE );
394 print '</div>';
395 }
396 }
397 print '</div>';
398 }
401 ######################
402 # function prints sql strings
403 #
404 function pre($sql_string, $simple_mode = FALSE) {
406 if(!$simple_mode) {
407 # erste leere Zeile im SQL löschen
408 $sql_string = preg_replace('/\^s+/m','', $sql_string);
409 # letze leere Zeile im SQL löschen
410 $sql_string = preg_replace('/\s+$/m','', $sql_string);
412 # kleinste Anzahl von führenden TABS zählen
413 preg_match_all('/^\t+/m', $sql_string, $matches);
414 $minTabCount = strlen(min($matches[0]));
416 # und entfernen
417 $sql_string = preg_replace('/^\t{'.$minTabCount.'}/m', '', $sql_string);
418 }
420 print '<pre>'.$sql_string.'</pre>';
421 }
422 ?>