Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
dev.inc
1 <?php
42 function array_contents(&$array, $max_depth=0, $ignore_ints=FALSE, $depth=0)
43 {
44  $string = $indent = '';
45  for ($i = 0; $i < $depth; $i++) {
46  $indent .= "\t";
47  }
48  if (!empty($max_depth) && $depth >= $max_depth) {
49  return $indent."[Max Depth Reached]\n";
50  }
51  if (count($array) == 0) return $indent."[Empty]\n";
52  reset($array);
53  while (list($key,$value) = each($array)) {
54  $print_key = str_replace("\n","\\n",str_replace("\r","\\r",str_replace("\t","\\t",addslashes($key))));
55  if ($ignore_ints && gettype($key) == 'integer') {
56  continue;
57  }
58  $type = gettype($value);
59  if ($type == 'array' || $type == 'object') {
60  $string .= $indent
61  .((is_string($key)) ? "\"$print_key\"": $key).' => '
62  .(($type == 'array') ? "array (\n" : '')
63  .(($type == 'object') ? 'new '.get_class($value)." Object (\n" : '');
64  $string .= array_contents($value, $max_depth, $ignore_ints, $depth + 1);
65  $string .= $indent."),\n";
66  } else {
67  if (is_string($value)) {
68  $value = str_replace("\n","\\n",str_replace("\r","\\r",str_replace("\t","\\t",addslashes($value))));
69  }
70  $string .= $indent
71  .((is_string($key)) ? "\"$print_key\"" : $key).' => '
72  .((is_string($value)) ? "\"$value\"" : $value).",\n";
73  }
74  }
75  $string[ strlen($string) - 2 ] = ' ';
76  return $string;
77 
78 }//end array_contents()
79 
80 
88 function ascii_chart()
89 {
90  for ($i=0; $i<256; $i++) {
91  echo(($i==0) ? '<table><tr>' : '')."<td>$i:</td><td>".chr($i).'</td><td>&nbsp;</td>'.((7==($i%8)) ? '</tr><tr>' : '').(($i==255) ? '</tr></table>' : '');
92  }
93 
94 }//end ascii_chart()
95 
96 
104 function inject_beef()
105 {
106  $check = '';
107  list($msec, $sec) = explode(' ',microtime());
108  $BAM = ((double) $sec) + ((double) $msec);
109  // Record time (note we ignore time spent IN this function)
110  global $SPEED_CHECK_LAST_HIT, $SPEED_CHECK_TOTAL;
111  if ($SPEED_CHECK_LAST_HIT) {
112  $elapsed = $BAM - $SPEED_CHECK_LAST_HIT;
113  $SPEED_CHECK_TOTAL += $elapsed;
114  $check = sprintf('%.5f / %.5f',$elapsed,$SPEED_CHECK_TOTAL);
115  }
116 
117  list($msec, $sec) = explode(' ',microtime());
118  // initialize
119  $SPEED_CHECK_LAST_HIT = ((double) $sec) + ((double) $msec);
120 
121  $len = strlen($check);
122  if (!$len) return;
123 
124  static $i;
125  $i++;
126  $string = '
127  .--, '.str_repeat(' ',$len).' ,--.
128  l_/ \ _'.str_repeat('_',$len).'_ / \_J
129  \ `-./__'.str_repeat('_',$len).'__\.-\' /
130  `..---'.str_repeat('-',$len).'----..\'
131  `-/ '.str_repeat(' ',$len).' \'
132  | '.$check.' |
133  `.__'.str_repeat('_',$len).'___.\'
134  ';
135  pre_echo($string);
136 
137 }//end inject_beef()
138 
139 
152 function bam($x='BAM!', $max_depth=0, $style='')
153 {
154  pre_echo($x, $max_depth, $style);
155 
156 }//end bam()
157 
158 
174 function minibam($contents, $title='BAM', $max_depth=0, $auto_expand=FALSE)
175 {
176  if (defined('SQ_DEBUG_HIDE_MINIBAM') && SQ_DEBUG_HIDE_MINIBAM) {
177  return;
178  }
179 
180  if (php_sapi_name() == 'cli') {
181 
182  echo "\n------------------------------------\n";
183  echo $title;
184  pre_echo($contents, $max_depth);
185 
186  } else {
187 
188  $id = rawurlencode(strtolower($title)).'_'.microtime();
189  ?>
190  <table border="1" cellspacing="0" cellpadding="2" style="background-color: #FFDDDD; color: #000000;">
191  <tr>
192  <td style="cursor: pointer; cursor: hand; text-decoration: underline; font-weight: bold" onclick="elt = document.getElementById('<?php echo $id; ?>'); elt.style.display = (elt.style.display == 'none') ? '' : 'none';"><?php echo $title; ?></a></td>
193  </tr>
194  <tr>
195  <td id="<?php echo $id; ?>" style="display: <?php echo ($auto_expand ? '' : 'none'); ?>">
196  <?php pre_echo($contents, $max_depth); ?>
197  </td>
198  </tr>
199  </table>
200  <?php
201 
202  }//end if (php_sapi_name())
203 
204 }//end minibam()
205 
206 
217 function pre_echo($x, $max_depth=0, $style='')
218 {
219  if (php_sapi_name() == 'cli') {
220  echo "\n------------------------------------\n";
221  } else {
222  echo "\n", '<div align="left"><pre style="', $style, 'font-family: courier, monospace;">';
223  }
224 
225  $type = gettype($x);
226  if ($type == 'object' && !$max_depth) {
227  print_r($x);
228  } else {
229  if ($type == 'object' || $type == 'array') {
230  // get the contents, then
231  if (!$max_depth) $max_depth = 10;
232  $x = array_contents($x, $max_depth);
233  }
234  $x = str_replace("\t", ' ', $x);
235 
236  echo (php_sapi_name() == 'cli') ? $x : htmlspecialchars($x);
237  }
238 
239  if (php_sapi_name() == 'cli') {
240  echo "\n------------------------------------\n";
241  } else {
242  echo '</pre></div>', "\n";
243  }
244 
245 }//end pre_echo()
246 
247 
258 function speed_check($str='', $html=TRUE, $log_error=TRUE, $log_file = '')
259 {
260  list($msec, $sec) = explode(' ',microtime());
261  // Record time (note we ignore time spent IN this function)
262  $BAM = ((double) $sec) + ((double) $msec);
263 
264  global $SPEED_CHECK_LAST_HIT, $SPEED_CHECK_TOTAL;
265  if ($SPEED_CHECK_LAST_HIT) {
266  $elapsed = $BAM - $SPEED_CHECK_LAST_HIT;
267  $SPEED_CHECK_TOTAL += $elapsed;
268  if ($html) {
269  echo('<table cellpadding=0 cellspacing=0 border=0 bgcolor="#ffff88"><tr><td><p style="color:red;font-weight:bold;font-family:sans-serif;font-size:13px;">'.sprintf('%.5f&nbsp;/&nbsp;%.5f',$elapsed,$SPEED_CHECK_TOTAL)."&nbsp;&nbsp;$str</p></td></tr></table>");
270  } else {
271  if (!$log_error) {
272  echo sprintf('%.5f/%.5f', $elapsed, $SPEED_CHECK_TOTAL).' '.$str;
273  } else {
274  if ($log_file == '') {
275  error_log(sprintf('%.5f/%.5f', $elapsed, $SPEED_CHECK_TOTAL).' '.$str);
276  } else {
277  log_write(sprintf('%.5f/%.5f', $elapsed, $SPEED_CHECK_TOTAL).' '.$str, $log_file);
278  }
279  }
280  }
281 
282  }
283  list($msec, $sec) = explode(' ',microtime());
284  // initialize
285  $SPEED_CHECK_LAST_HIT = ((double) $sec) + ((double) $msec);
286 
287 }//end speed_check()
288 
289 
300 function mem_check($print=NULL, $return_value=FALSE, $start_over=FALSE)
301 {
302  global $MEM_CHECK_FIRST_HIT, $MEM_CHECK_LAST_HIT;
303  if ($start_over) {
304  $MEM_CHECK_FIRST_HIT = 0;
305  $MEM_CHECK_LAST_HIT = 0;
306  }
307 
308  $hit = (int) (memory_get_usage() / 1024);
309  $mem = '';
310  if (is_null($print) || in_array('current', $print)) {
311  $mem = $hit.' KB';
312  }
313 
314  if ($MEM_CHECK_LAST_HIT) {
315  if (is_null($print) || in_array('change', $print)) {
316  if ($hit > $MEM_CHECK_LAST_HIT) {
317  $mem .= ' (+ '.($hit - $MEM_CHECK_LAST_HIT).' KB)';
318  }
319  if ($hit < $MEM_CHECK_LAST_HIT) {
320  $mem .= ' (- '.($MEM_CHECK_LAST_HIT - $hit).' KB)';
321  }
322  }
323  }
324 
325  if ($MEM_CHECK_FIRST_HIT) {
326  if (is_null($print) || in_array('total', $print)) {
327  if ($hit > $MEM_CHECK_FIRST_HIT) {
328  $mem .= ' [+ '.($hit - $MEM_CHECK_FIRST_HIT).' KB TOTAL]';
329  }
330  if ($hit < $MEM_CHECK_FIRST_HIT) {
331  $mem .= ' [- '.($MEM_CHECK_FIRST_HIT - $hit).' KB TOTAL]';
332  }
333  }
334  } else {
335  $MEM_CHECK_FIRST_HIT = $hit;
336  }
337 
338  if (!$return_value) {
339  echo("<br><table cellpadding=0 cellspacing=0 border=0 bgcolor=#ffff88><tr><td><p style=\"color:red;font-weight:bold;font-family:sans-serif;font-size:13px;\">Mem: $mem</td></tr></table><br>");
340  }
341 
342  $MEM_CHECK_LAST_HIT = $hit;
343  if ($return_value) return $mem;
344 
345 }//end mem_check()
346 
347 
355 function print_function_list()
356 {
357  echo '<pre>';
358  foreach (get_loaded_extensions() as $extension) {
359  echo "<b>Extension: $extension</b><br>";
360  print_r (get_extension_funcs($extension));
361  }
362  echo '<b>User-defined</b><br>';
363  $f = get_defined_functions();
364  print_r ($f['user']);
365  foreach (get_declared_classes() as $class) {
366  echo "<b>Class Methods: $class</b><br>";
367  print_r(get_class_methods($class));
368  }
369  echo '</pre>';
370 
371 }//end print_function_list()
372 
373 
383 function pts_spew($i,$s)
384 {
385  if ($i != (int) $i || !file_exists("/dev/pts/$i")) {
386  trigger_error("Can't spew to pts, \"$i\" is not a valid pts number.", E_USER_WARNING);
387  return;
388  }
389  require_once(dirname(__FILE__).'/../general/file_system.inc');
390  if (!string_to_file("$s\n","/dev/pts/$i")) {
391  trigger_error("Try ensuring that /dev/pts/[your_pts_number] is writable by the web server.<br>$ w # To find your pts<br>$ chmod a+w /dev/pts/[your_pts_number]<br>", E_USER_WARNING);
392  }
393 
394 }//end pts_spew()
395 
396 
403 function get_calling_func()
404 {
405 
406  $bt = debug_backtrace();
407  $file = substr($bt[1]['file'], strrpos($bt[1]['file']+1, '/'));
408  $res = '';
409  if (isset($bt[2])) {
410  if (isset($bt[2]['class'])) {
411  $res = $bt[2]['class'].'::'.$bt[2]['function'];
412  } else {
413  $res = $bt[2]['function'];
414  }
415  }
416  $res .= ' ('.$file.' line '.$bt[1]['line'].')';
417  return $res;
418 
419 }//end get_calling_func()
420 
421 
434 function get_backtrace()
435 {
436  $bt = debug_backtrace();
437  // remove this function call from the stack
438  array_shift($bt);
439  $stack = Array();
440 
441  foreach ($bt as $elem) {
442  $info = '';
443  if (isset($elem['line'])) $info = 'line='.$elem['line'];
444  $file = '';
445 
446  if (isset($elem['file'])) {
447  $file = basename($elem['file']);
448  substr($file, 0, strpos($file, '.'));
449  }
450 
451  $class_name = substr($file, 0, strpos($file, '.'));
452  if (empty($elem['class'])) {
453  $info .= ' file='.$file;
454  } else {
455  if ($class_name != $elem['class']) {
456  $info .= ' class='.$class_name.' object='.$elem['class'];
457  } else {
458  $info .= ' class='.$elem['class'];
459  }
460  }
461  $stack[] = 'function '.$elem['function'].'('.$info.')';
462  }
463  return $stack;
464 
465 }//end get_backtrace()
466 
467 
474 function get_microtime()
475 {
476  list($usec, $sec) = explode(' ', microtime());
477  return $sec + $usec;
478 
479 }//end get_microtime()
480 
481 
490 function log_dump($var=NULL)
491 {
492  static $count = 0;
493  if (is_null($var)) {
494  $var = '---------- Debugging ----------';
495  }
496  error_log("\n".$count++.': '.get_calling_func().": \n".print_r($var, 1));
497 
498 }//end log_dump()
499 
500 
501 ?>