FusionDirectory
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 * Happy debugging and feel free to email me your comments.
21 *
22 * History: (starting with version 0.5.3 at 2003-02-24)
23 *
24 * - added tooltips to the td's showing the type of keys and values (thanks Itomic)
25 * 2003-07-16
26 * - pre() function now trims trailing tabs
27 ----------------------------------------------------------------------*/
28 
44 {
45 
46  // this can be changed to FALSE if you don't like the fancy string formatting
47  var $look_for_leading_tabs = TRUE;
48 
49  var $output;
50  var $iterations;
51  var $key_bg_color = '1E32C8';
52  var $value_bg_color = 'DDDDEE';
53  var $fontsize = '8pt';
54  var $keyalign = 'center';
55  var $fontfamily = 'Verdana';
56  var $export_flag;
57  var $show_object_vars;
58  var $export_dumper_path = 'http://tools.www.mdc.xmc.de/print_a_dumper/print_a_dumper.php';
59  /* i'm still working on the dumper! don't use it now
60  * put the next line into the print_a_dumper.php file (optional)
61  * print htmlspecialchars( stripslashes ( $_POST['array'] ) ); */
62  var $export_hash;
63 
67  function __construct ()
68  {
69  $this->export_hash = uniqid('');
70  }
71 
72 
95  function print_a ($array, $iteration = FALSE, $key_bg_color = FALSE)
96  {
97  if (!$key_bg_color) {
98  $key_bg_color = $this->key_bg_color;
99  }
100 
101  if (!$iteration && isset($this->export_flag)) {
102  $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>';
103  }
104 
105  // lighten up the background color for the key td's =)
106  if ($iteration) {
107  $tmp_key_bg_color = '';
108  for ($i = 0; $i < 6; $i += 2) {
109  $c = substr($key_bg_color, $i, 2);
110  $c = hexdec($c);
111  $c += 15;
112  if ($c > 255) {
113  $c = 255;
114  }
115  $tmp_key_bg_color .= sprintf("%02X", $c);
116  }
117  $key_bg_color = $tmp_key_bg_color;
118  }
119 
120  // build a single table ... may be nested
121  $this->output .= '<table style="border:none;" '.(!$iteration && $this->export_flag ? 'onClick="document.getElementById(\'pa_form_'.$this->export_hash.'\').submit();" )' : '').'>';
122  foreach ($array as $key => $value) {
123  $value_style = 'color:black;';
124  $key_style = 'color:white;';
125 
126  $type = gettype($value);
127 
128  // change the color and format of the value
129  switch ($type) {
130  case 'array':
131  break;
132 
133  case 'integer':
134  $value_style = 'color:green;';
135  break;
136 
137  case 'double':
138  $value_style = 'color:red;';
139  break;
140 
141  case 'bool':
142  $value_style = 'color:blue;';
143  break;
144 
145  case 'resource':
146  $value_style = 'color:darkblue;';
147  break;
148 
149  case 'string':
150  if ($this->look_for_leading_tabs && preg_match('/^\t/m', $value)) {
151  $search = ['/\t/', "/\n/"];
152  $replace = ['&nbsp;&nbsp;&nbsp;','<br />'];
153  $value = preg_replace($search, $replace, htmlspecialchars($value));
154  $value_style = 'color:black;border:1px gray dotted;';
155  } else {
156  $value_style = 'color:black;';
157  $value = nl2br(htmlspecialchars($value));
158  }
159  break;
160 
161  case 'object':
162  default:
163  $key_style = 'color:#FF9B2F;';
164  break;
165  }
166 
167  $this->output .= '<tr>';
168  $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.']">';
169  $this->output .= $key;
170  $this->output .= '</td>';
171  $this->output .= '<td nowrap="nowrap" style="background-color:#'.$this->value_bg_color.';font: '.$this->fontsize.' '.$this->fontfamily.'; color:black;">';
172 
173  // value output
174  if ($type == 'array') {
175  if (count($value)) {
176  $this->print_a($value, TRUE, $key_bg_color);
177  } else {
178  $this->output .= '<div style="color:blue;">Array (empty)</div>';
179  }
180  } elseif ($type == 'object') {
181  if ($this->show_object_vars) {
182  $this->print_a(get_object_vars($value), TRUE, $key_bg_color);
183  } else {
184  $this->output .= '<div style="'.$value_style.'">OBJECT - '.get_class($value).'</div>';
185  }
186  } else {
187  $this->output .= '<div style="'.$value_style.'" title="'.$type.'">'.$value.'</div>';
188  }
189 
190  $this->output .= '</td>';
191  $this->output .= '</tr>';
192  }
193  $this->output .= '</table>';
194  }
195 }
196 
197 /*
198  * \brief helper function.. calls print_a() inside the printAClass
199  *
200  * \param array $array
201  *
202  * \param boolean $return_mode false
203  *
204  * \param boolean $show_object_vars false
205  *
206  * \param boolean $export_flag false
207  */
208 function print_a ($array, $return_mode = FALSE, $show_object_vars = FALSE, $export_flag = FALSE)
209 {
210  $e = error_reporting(0);
211  if (is_array($array) || is_object($array)) {
212  $pa = new printAClass;
213  if ($show_object_vars) {
214  $pa->show_object_vars = TRUE;
215  }
216  if ($export_flag) {
217  $pa->export_flag = $export_flag;
218  }
219 
220  $pa->print_a($array);
221 
222  $output = &$pa->output;
223  } else {
224  $output = '<span style="color:red;font-size:small;">print_a( '.gettype($array).' )</span>';
225  }
226 
227  error_reporting($e);
228  if ($return_mode) {
229  return $output;
230  } else {
231  print $output;
232  return TRUE;
233  }
234 }
__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)