FusionDirectory
 All Data Structures Files Functions Variables
functions_debug.inc
Go to the documentation of this file.
1 <?php
2 
3 /*----------------------------------------------------------------------
4 * Title.........: Debug Lib
5 * Version.......: 0.5.4
6 * Author........: Thomas Schüßler <tulpe@atomar.de>
7 * Filename......: debuglib.php(s)
8 * Last changed..: 16. July 2003
9 * License.......: Free to use. Postcardware ;)
10 *
11 *-----------------------------------------------------------------------
12 *
13 * Functions in this library:
14 *
15 * print_a( array array [,int mode] )
16 * prints arrays in a readable, understandable form.
17 * if mode is defined the function returns the output instead of
18 * printing it to the browser
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 * Happy debugging and feel free to email me your comments.
31 *
32 * History: (starting with version 0.5.3 at 2003-02-24)
33 *
34 * - added tooltips to the td's showing the type of keys and values (thanks Itomic)
35 * 2003-07-16
36 * - pre() function now trims trailing tabs
37 ----------------------------------------------------------------------*/
38 
39 
40 // This file must be the first include on your page.
41 
42 /* used for tracking of generation-time */
43 {
44  $MICROTIME_START = microtime();
45  @$GLOBALS_initial_count = count($GLOBALS);
46 }
47 
62 class printAClass {
63 
64  // this can be changed to FALSE if you don't like the fancy string formatting
65  var $look_for_leading_tabs = TRUE;
66 
67  var $output;
68  var $iterations;
69  var $key_bg_color = '1E32C8';
70  var $value_bg_color = 'DDDDEE';
71  var $fontsize = '8pt';
72  var $keyalign = 'center';
73  var $fontfamily = 'Verdana';
74  var $export_flag;
75  var $show_object_vars;
76  var $export_dumper_path = 'http://tools.www.mdc.xmc.de/print_a_dumper/print_a_dumper.php';
77  /* i'm still working on the dumper! don't use it now
78  * put the next line into the print_a_dumper.php file (optional)
79  * print htmlspecialchars( stripslashes ( $_POST['array'] ) ); */
80  var $export_hash;
81 
85  function __construct()
86  {
87  $this->export_hash = uniqid('');
88  }
89 
90 
113  function print_a($array, $iteration = FALSE, $key_bg_color = FALSE)
114  {
115  if (!$key_bg_color) {
116  $key_bg_color = $this->key_bg_color;
117  }
118 
119  if (!$iteration && isset($this->export_flag)) {
120  $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>';
121  }
122 
123  // lighten up the background color for the key td's =)
124  if ($iteration) {
125  for ($i = 0; $i < 6; $i += 2) {
126  $c = substr( $key_bg_color, $i, 2 );
127  $c = hexdec( $c );
128  $c += 15;
129  if ($c > 255) {
130  $c = 255;
131  }
132  if (!isset($tmp_key_bg_color)) {
133  $tmp_key_bg_color = '';
134  }
135  $tmp_key_bg_color .= sprintf( "%02X", $c );
136  }
137  $key_bg_color = $tmp_key_bg_color;
138  }
139 
140  // build a single table ... may be nested
141  $this->output .= '<table style="border:none;" '.( !$iteration && $this->export_flag ? 'onClick="document.getElementById(\'pa_form_'.$this->export_hash.'\').submit();" )' : '' ).'>';
142  foreach ($array as $key => $value) {
143 
144  $value_style = 'color:black;';
145  $key_style = 'color:white;';
146 
147  $type = gettype($value);
148 
149  // change the color and format of the value
150  switch ($type) {
151  case 'array':
152  break;
153 
154  case 'object':
155  $key_style = 'color:#FF9B2F;';
156  break;
157 
158  case 'integer':
159  $value_style = 'color:green;';
160  break;
161 
162  case 'double':
163  $value_style = 'color:red;';
164  break;
165 
166  case 'bool':
167  $value_style = 'color:blue;';
168  break;
169 
170  case 'resource':
171  $value_style = 'color:darkblue;';
172  break;
173 
174  case 'string':
175  if ($this->look_for_leading_tabs && preg_match('/^\t/m', $value)) {
176  $search = array('/\t/', "/\n/");
177  $replace = array('&nbsp;&nbsp;&nbsp;','<br />');
178  $value = preg_replace( $search, $replace, htmlspecialchars($value));
179  $value_style = 'color:black;border:1px gray dotted;';
180  } else {
181  $value_style = 'color:black;';
182  $value = nl2br(htmlspecialchars($value));
183  }
184  break;
185  }
186 
187  $this->output .= '<tr>';
188  $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.']">';
189  $this->output .= $key;
190  $this->output .= '</td>';
191  $this->output .= '<td nowrap="nowrap" style="background-color:#'.$this->value_bg_color.';font: '.$this->fontsize.' '.$this->fontfamily.'; color:black;">';
192 
193  // value output
194  if ($type == 'array') {
195  if (count($value)) {
196  $this->print_a( $value, TRUE, $key_bg_color );
197  } else {
198  $this->output .= '<div style="color:blue;">Array (empty)</div>';
199  }
200  } elseif ($type == 'object') {
201  if ($this->show_object_vars) {
202  $this->print_a( get_object_vars( $value ), TRUE, $key_bg_color );
203  } else {
204  $this->output .= '<div style="'.$value_style.'">OBJECT - '.get_class($value).'</div>';
205  }
206  } else {
207  $this->output .= '<div style="'.$value_style.'" title="'.$type.'">'.$value.'</div>';
208  }
209 
210  $this->output .= '</td>';
211  $this->output .= '</tr>';
212  }
213  $this->output .= '</table>';
214  }
215 }
216 
217 /*
218  * \brief helper function.. calls print_a() inside the printAClass
219  *
220  * \param array $array
221  *
222  * \param boolean $return_mode false
223  *
224  * \param boolean $show_object_vars false
225  *
226  * \param boolean $export_flag false
227  */
228 function print_a($array, $return_mode = FALSE, $show_object_vars = FALSE, $export_flag = FALSE )
229 {
230  $e = error_reporting(0);
231  if (is_array($array) || is_object($array)) {
232  $pa = new printAClass;
233  if ($show_object_vars) {
234  $pa->show_object_vars = TRUE;
235  }
236  if ($export_flag) {
237  $pa->export_flag = $export_flag;
238  }
239 
240  $pa->print_a($array);
241 
242  $output = &$pa->output;
243  } else {
244  $output = '<span style="color:red;font-size:small;">print_a( '.gettype( $array ).' )</span>';
245  }
246 
247  error_reporting ($e);
248  if ($return_mode) {
249  return $output;
250  } else {
251  print $output;
252  return TRUE;
253  }
254 }
255 
256 function _script_globals()
257 {
258  global $GLOBALS_initial_count;
259 
260  $varcount = 0;
261 
262  foreach ($GLOBALS as $GLOBALS_current_key => $GLOBALS_current_value) {
263  if (++$varcount > $GLOBALS_initial_count) {
264  /* die wollen wir nicht! */
265  if ($GLOBALS_current_key != 'HTTP_SESSION_VARS' && $GLOBALS_current_key != '_SESSION') {
266  $script_GLOBALS[$GLOBALS_current_key] = $GLOBALS_current_value;
267  }
268  }
269  }
270 
271  unset($script_GLOBALS['GLOBALS_initial_count']);
272  return $script_GLOBALS;
273 }
274 
278 function show_runtime()
279 {
280  $MICROTIME_END = microtime();
281  $MICROTIME_START = explode(' ', $GLOBALS['MICROTIME_START']);
282  $MICROTIME_END = explode(' ', $MICROTIME_END);
283  $GENERATIONSEC = $MICROTIME_END[1] - $MICROTIME_START[1];
284  $GENERATIONMSEC = $MICROTIME_END[0] - $MICROTIME_START[0];
285  $GENERATIONTIME = substr($GENERATIONSEC + $GENERATIONMSEC, 0, 8);
286 
287  return '<span style="color:red;font-weight:normal;font-size:9px;">(runtime: '.$GENERATIONTIME.' sec)</span>';
288 }
289 
290 
297 function show_vars($show_all_vars = FALSE, $show_object_vars = FALSE)
298 {
299  if (isset($GLOBALS['no_vars'])) {
300  return;
301  }
302 
303  $script_globals = _script_globals();
304  print '
305  <style type="text/css">
306  .vars-container {
307  font-family: Verdana, Arial, Helvetica, Geneva, Swiss, SunSans-Regular, sans-serif;
308  font-size: 8pt;
309  padding:5px;
310  }
311  .varsname {
312  font-weight:bold;
313  }
314  </style>
315  ';
316 
317  print '<br />
318  <div style="border-style:dotted;border-width:1px;padding:2px;font-family:Verdana;font-size:10pt;font-weight:bold;">
319  DEBUG '.show_runtime().'
320  ';
321 
322  $vars_arr['script_globals'] = array('global script variables', '#7ACCC8');
323 
324  $vars_arr['_GET'] = array('$_GET', '#7DA7D9');
325  $vars_arr['_POST'] = array('$_POST', '#F49AC1');
326  $vars_arr['_FILES'] = array('$_POST FILES', '#82CA9C');
327  $vars_arr['_SESSION'] = array('$_SESSION', '#FCDB26');
328  $vars_arr['_COOKIE'] = array('$_COOKIE', '#A67C52');
329 
330  if ($show_all_vars) {
331  $vars_arr['_SERVER'] = array('SERVER', '#A186BE');
332  $vars_arr['_ENV'] = array('ENV', '#7ACCC8');
333  }
334 
335  foreach ($vars_arr as $vars_name => $vars_data) {
336  if ($vars_name != 'script_globals') {
337  global $$vars_name;
338  }
339  if ($$vars_name) {
340  print '<div class="vars-container" style="background-color:'.$vars_data[1].';"><span class="varsname">'.$vars_data[0].'</span><br />';
341  print_a($$vars_name, FALSE, $show_object_vars, FALSE );
342  print '</div>';
343  }
344  }
345  print '</div>';
346 }
347 ?>
__construct()
printAClass constructor
print_a class and helper function prints out an array in a more readable way than print_r() ...
print_a($array, $iteration=FALSE, $key_bg_color=FALSE)