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 $this->print_a( $value, TRUE, $key_bg_color );
187 } elseif($type == 'object') {
188 if( $this->show_object_vars ) {
189 $this->print_a( get_object_vars( $value ), TRUE, $key_bg_color );
190 } else {
191 $this->output .= '<div style="'.$value_style.'">OBJECT</div>';
192 }
193 } else {
194 $this->output .= '<div style="'.$value_style.'" title="'.$type.'">'.$value.'</div>';
195 }
197 $this->output .= '</td>';
198 $this->output .= '</tr>';
199 }
200 $this->output .= '</table>';
201 }
202 }
204 # helper function.. calls print_a() inside the print_a_class
205 function print_a( $array, $return_mode = FALSE, $show_object_vars = FALSE, $export_flag = FALSE ) {
206 $e= error_reporting (0);
207 if( is_array( $array ) or is_object( $array ) ) {
208 $pa = new Print_a_class;
209 $show_object_vars and $pa->show_object_vars = TRUE;
210 $export_flag and $pa->export_flag = $export_flag;
212 $pa->print_a( $array );
214 # $output = $pa->output; unset($pa);
215 $output = &$pa->output;
216 } else {
217 $output = '<span style="color:red;font-size:small;">print_a( '.gettype( $array ).' )</span>';
218 }
220 error_reporting ($e);
221 if($return_mode) {
222 return $output;
223 } else {
224 print $output;
225 return TRUE;
226 }
227 }
230 // shows mysql-result as a table.. # not ready yet :(
231 function print_result($RESULT) {
233 if(!$RESULT) return;
235 $fieldcount = mysql_num_fields($RESULT);
237 for($i=0; $i<$fieldcount; $i++) {
238 $tables[mysql_field_table($RESULT, $i)]++;
239 }
241 print '
242 <style type="text/css">
243 .rs_tb_th {
244 font-family: Verdana;
245 font-size:9pt;
246 font-weight:bold;
247 color:white;
248 }
249 .rs_f_th {
250 font-family:Verdana;
251 font-size:7pt;
252 font-weight:bold;
253 color:white;
254 }
255 .rs_td {
256 font-family:Verdana;
257 font-size:7pt;
258 }
259 </style>
260 <script type="text/javascript" language="JavaScript">
261 var lastID;
262 function highlight(id) {
263 if(lastID) {
264 lastID.style.color = "#000000";
265 lastID.style.textDecoration = "none";
266 }
267 tdToHighlight = document.getElementById(id);
268 tdToHighlight.style.color ="#FF0000";
269 tdToHighlight.style.textDecoration = "underline";
270 lastID = tdToHighlight;
271 }
272 </script>
273 ';
275 print '<table summary=\"\" border="0" bgcolor="#000000" cellspacing="1" cellpadding="1">';
277 print '<tr>';
278 foreach($tables as $tableName => $tableCount) {
279 $col == '0054A6' ? $col = '003471' : $col = '0054A6';
280 print '<th colspan="'.$tableCount.'" class="rs_tb_th" style="background-color:#'.$col.';">'.$tableName.'</th>';
281 }
282 print '</tr>';
284 print '<tr>';
285 for($i=0;$i < mysql_num_fields($RESULT);$i++) {
286 $FIELD = mysql_field_name($RESULT, $i);
287 $col == '0054A6' ? $col = '003471' : $col = '0054A6';
288 print '<td align="center" bgcolor="#'.$col.'" class="rs_f_th">'.$FIELD.'</td>';
289 }
290 print '</tr>';
292 mysql_data_seek($RESULT, 0);
294 while($DB_ROW = mysql_fetch_array($RESULT, MYSQL_NUM)) {
295 $pointer++;
296 if($toggle) {
297 $col1 = "E6E6E6";
298 $col2 = "DADADA";
299 } else {
300 $col1 = "E1F0FF";
301 $col2 = "DAE8F7";
302 }
303 $toggle = !$toggle;
304 print '<tr id="ROW'.$pointer.'" onMouseDown="highlight(\'ROW'.$pointer.'\');">';
305 foreach($DB_ROW as $value) {
306 $col == $col1 ? $col = $col2 : $col = $col1;
307 print '<td valign="top" bgcolor="#'.$col.'" class="rs_td" nowrap>'.nl2br($value).'</td>';
308 }
309 print '</tr>';
310 }
311 print '</table>';
312 mysql_data_seek($RESULT, 0);
313 }
315 function _script_globals() {
316 global $GLOBALS_initial_count;
318 $varcount = 0;
320 foreach($GLOBALS as $GLOBALS_current_key => $GLOBALS_current_value) {
321 if(++$varcount > $GLOBALS_initial_count) {
322 /* die wollen wir nicht! */
323 if ($GLOBALS_current_key != 'HTTP_SESSION_VARS' && $GLOBALS_current_key != '_SESSION') {
324 $script_GLOBALS[$GLOBALS_current_key] = $GLOBALS_current_value;
325 }
326 }
327 }
329 unset($script_GLOBALS['GLOBALS_initial_count']);
330 return $script_GLOBALS;
331 }
333 function show_runtime() {
334 $MICROTIME_END = microtime();
335 $MICROTIME_START = explode(' ', $GLOBALS['MICROTIME_START']);
336 $MICROTIME_END = explode(' ', $MICROTIME_END);
337 $GENERATIONSEC = $MICROTIME_END[1] - $MICROTIME_START[1];
338 $GENERATIONMSEC = $MICROTIME_END[0] - $MICROTIME_START[0];
339 $GENERATIONTIME = substr($GENERATIONSEC + $GENERATIONMSEC, 0, 8);
341 return '<span style="color:red;font-weight:normal;font-size:9px;">(runtime: '.$GENERATIONTIME.' sec)</span>';
342 }
345 ######################
346 # function shows all superglobals and script defined global variables
347 # show_vars() without the first parameter shows all superglobals except $_ENV and $_SERVER
348 # show_vars(1) shows all
349 # show_vars(,1) shows object properties in addition
350 #
351 function show_vars($show_all_vars = FALSE, $show_object_vars = FALSE) {
352 if(isset($GLOBALS['no_vars'])) return;
354 $script_globals = _script_globals();
355 print '
356 <style type="text/css">
357 .vars-container {
358 font-family: Verdana, Arial, Helvetica, Geneva, Swiss, SunSans-Regular, sans-serif;
359 font-size: 8pt;
360 padding:5px;
361 }
362 .varsname {
363 font-weight:bold;
364 }
365 </style>
366 ';
368 print '<br />
369 <div style="border-style:dotted;border-width:1px;padding:2px;font-family:Verdana;font-size:10pt;font-weight:bold;">
370 DEBUG '.show_runtime().'
371 ';
373 $vars_arr['script_globals'] = array('global script variables', '#7ACCC8');
374 $vars_arr['_GET'] = array('$_GET', '#7DA7D9');
375 $vars_arr['_POST'] = array('$_POST', '#F49AC1');
376 $vars_arr['_FILES'] = array('$_POST FILES', '#82CA9C');
377 $vars_arr['_SESSION'] = array('$_SESSION', '#FCDB26');
378 $vars_arr['_COOKIE'] = array('$_COOKIE', '#A67C52');
380 if($show_all_vars) {
381 $vars_arr['_SERVER'] = array('SERVER', '#A186BE');
382 $vars_arr['_ENV'] = array('ENV', '#7ACCC8');
383 }
385 foreach($vars_arr as $vars_name => $vars_data) {
386 if($vars_name != 'script_globals') global $$vars_name;
387 if($$vars_name) {
388 print '<div class="vars-container" style="background-color:'.$vars_data[1].';"><span class="varsname">'.$vars_data[0].'</span><br />';
389 print_a($$vars_name, FALSE, $show_object_vars, FALSE );
390 print '</div>';
391 }
392 }
393 print '</div>';
394 }
397 ######################
398 # function prints sql strings
399 #
400 function pre($sql_string, $simple_mode = FALSE) {
402 if(!$simple_mode) {
403 # erste leere Zeile im SQL löschen
404 $sql_string = preg_replace('/\^s+/m','', $sql_string);
405 # letze leere Zeile im SQL löschen
406 $sql_string = preg_replace('/\s+$/m','', $sql_string);
408 # kleinste Anzahl von führenden TABS zählen
409 preg_match_all('/^\t+/m', $sql_string, $matches);
410 $minTabCount = strlen(min($matches[0]));
412 # und entfernen
413 $sql_string = preg_replace('/^\t{'.$minTabCount.'}/m', '', $sql_string);
414 }
416 print '<pre>'.$sql_string.'</pre>';
417 }
418 ?>