** Filename......: debuglib.php(s) ** Last changed..: 12.07.2004 14:13 ** License.......: Free to use. Postcardware ;) ** ************************************************* ** ** Functions in this library: ** ** print_a( array array [,int returnmode] [,bool show object vars] [,int max entries] ) ** ** prints arrays in a readable form. ** if mode is defined the function returns the output instead of printing it to the output buffer ** ** print_a( $array, #, 1 ) shows also object properties ** print_a( $array, 1, # ) returns the table as a string instead of printing it to the output buffer ** print_a( $array, 'WindowName', #) opens the output in a window indentified by the string. ** print_a( $array, '_WindowName', #) prints the array inside a frame (
...
) ** print_a( $array, 3, # ) opens a new browser window with a serialized version of your array (save as a textfile and can it for later use ;). ** ** show_vars( [bool verbose] [, bool show_object_vars ] [, int limit] ) ** ** use this function on the bottom of your script to see all ** superglobals and global variables in your script in a nice ** formated way ** ** show_vars() without parameter shows $_GET, $_POST, $_SESSION, ** $_FILES and all global variables you've defined in your script ** ** show_vars(1) shows $_SERVER and $_ENV in addition ** show_vars(#,1) shows also object properties ** show_vars(#, #, 15) shows only the first 15 entries in a numerical keyed array (or an array with more than 50 entries) ( standard is 5 ) ** show_vars(#, #, 0) shows all entries ** ** ** ** ** print_result( result_handle ) ** ** prints a mysql_result set returned by mysql_query() as a table ** this function is work in progress! use at your own risk ** ** ** Happy debugging and feel free to email me your comments. ** ** ** ** History: (starting at 2003-02-24) ** ** - added tooltips to the td's showing the type of keys and values (thanks Itomic) ** 2003-07-16 ** - pre() function now trims trailing tabulators ** 2003-08-01 ** - silly version removed.. who needs a version for such a thing ;) ** 2003-09-24 ** - changed the parameters of print_a() a bit ** see above ** - addet the types NULL and bolean to print_a() ** - print_a() now replaces trailing spaces in string values with red underscores ** 2003-09-24 (later that day ;) ** - oops.. fixed the print_a() documentation.. parameter order was wrong ** - added mode 3 to the second parameter ** 2003-09-25 ** - added a limit parameter to the show_vars() and print_a() functions ** default for show_vars() is 5 ** show_vars(#,#, n) changes that (0 means show all entries) ** print_a() allways shows all entries by default ** print_a(#,#,#, n) changes that ** ** this parameter is used to limit the output of arrays with a numerical index (like long lists of similiar elements) ** i added this option for performance reasons ** it has no effect on arrays where one ore more keys are not number-strings ** 2003-09-27 ** - reworked the pre() and _remove_exessive_leading_tabs() functions ** they now work like they should :) ** - some cosmetic changes ** 2003-10-28 ** - fixed multiline string display ** 2003-11-14 ** - argh! uploaded the wrong version :/ ... fixed.. sorry ** 2003-11-16 ** - fixed a warning triggered by _only_numeric_keys() ** thanx Peter Valdemar :) ** - fixed a warning when print_a was called directly on an object ** thanx Hilton :) ** 2003-12-01 ** - added slashes in front of the print_a(#,3) output ** 2004-03-17 ** - fixed a problem when print_a(#,2) was called on an array containing newlines ** 2004-03-26 ** - added a variation of mode 2 for print_a(). ** when a string is passed as the second parameter, a new window with the string as prefix gets opened for every differend string.. #TODO_COMMENT# ** 2004-07-12 ** - print_a($array, '_MyLabel') draws a frame with a label around the output ************************************************/ if (!defined('USE_DEBUGLIB')) define('USE_DEBUGLIB', true); if (USE_DEBUGLIB) { # This file must be the first include on your page. /* used for tracking of generation-time */ { $MICROTIME_START = microtime(); @$GLOBALS_initial_count = count($GLOBALS); } /************************************************ ** print_a class and helper function ** prints out an array in a more readable way ** than print_r() ** ** based on the print_a() function from ** Stephan Pirson (Saibot) ************************************************/ class Print_a_class { # this can be changed to true if you want var $look_for_leading_tabs = false; var $output; var $iterations; var $key_bg_color = '1E32C8'; var $value_bg_color = 'DDDDEE'; var $fontsize = '8pt'; var $keyalign = 'left'; var $fontfamily = 'Verdana'; var $show_object_vars; var $limit; // function Print_a_class() {} # recursive function! /* this internal function looks if the given array has only numeric values as */ function _only_numeric_keys( $array ) { $test = true; if (is_array($array)) { foreach ( array_keys( $array ) as $key ) { if( !is_numeric( $key ) ) $test = false; /* #TODO# */ } return $test; } else { return false; } } function _handle_whitespace( $string ) { $string = str_replace(' ', ' ', $string); $string = preg_replace(array('/ $/', '/^ /'), '_', $string); /* replace spaces at the start/end of the STRING with red underscores */ $string = preg_replace('/\t/', '   ', $string); /* replace tabulators with '_ _' */ return $string; } function print_a($array, $iteration = false, $key_bg_color = false) { $key_bg_color or $key_bg_color = $this->key_bg_color; # lighten up the background color for the key td's =) if( $iteration ) { for ($i=0; $i<6; $i+=2) { $c = substr( $key_bg_color, $i, 2 ); $c = hexdec( $c ); ( $c += 15 ) > 255 and $c = 255; isset($tmp_key_bg_color) or $tmp_key_bg_color = ''; $tmp_key_bg_color .= sprintf( "%02X", $c ); } $key_bg_color = $tmp_key_bg_color; } # build a single table ... may be nested $this->output .= ''; $only_numeric_keys = ($this->_only_numeric_keys( $array ) || count( $array ) > 50); $i = 0; foreach ($array as $key => $value) { if( $only_numeric_keys && $this->limit && $this->limit == $i++ ) break; /* if print_a() was called with a fourth parameter #TODO# */ $value_style_box = 'color:black;'; $key_style = 'color:white;'; $type = gettype( $value ); # print $type.'
'; # change the color and format of the value and set the values title $type_title = $type; $value_style_content = ''; switch( $type ) { case 'array': if( empty( $value ) ) $type_title = 'empty array'; break; case 'object': $key_style = 'color:#FF9B2F;'; break; case 'integer': $value_style_box = 'color:green;'; break; case 'double': $value_style_box = 'color:blue;'; break; case 'boolean': if( $value == true ) { $value_style_box = 'color:#D90081;'; } else { $value_style_box = 'color:#84009F;'; } break; case 'NULL': $value_style_box = 'color:darkorange;'; break; case 'string': if( $value == '' ) { $value_style_box = 'color:darkorange;'; $value = "''"; $type_title = 'empty string'; } else { $value_style_box = 'color:black;'; $value = htmlspecialchars( $value ); if( $this->look_for_leading_tabs && _check_for_leading_tabs( $value ) ) { $value = _remove_exessive_leading_tabs( $value ); } $value = $this->_handle_whitespace( $value ); $value = nl2br($value); /* use different color for string background */ if(strstr($value, "\n")) $value_style_content = 'background:#ECEDFE;'; } break; } $this->output .= ''; $this->output .= ''; $this->output .= ''; $this->output .= ''; } $entry_count = count( $array ); $skipped_count = $entry_count - $this->limit; if( $only_numeric_keys && $this->limit && count($array) > $this->limit) { $this->output .= ''; } $this->output .= '
'; $this->output .= $this->_handle_whitespace( $key ); $this->output .= ''; # value output if($type == 'array' && preg_match('/#RAS/', $key) ) { /* only used for special recursive array constructs which i use sometimes */ $this->output .= '
recursion!
'; } elseif($type == 'array') { if( ! empty( $value ) ) { $this->print_a( $value, true, $key_bg_color ); } else { $this->output .= '[]'; } } elseif($type == 'object') { if( $this->show_object_vars ) { $objects_class = get_class($value); $this->print_a( array('CLASS_NAME' => $objects_class), true, '204FB8' ); $this->print_a( array('CLASS_VARS' => get_class_vars( $objects_class )), true, '2066B8' ); $this->print_a( array('CLASS_METHODS' => get_class_methods( $objects_class )), true, '2067EB8' ); $this->print_a( array('OBJECT_VARS' => get_object_vars( $value )), true, '2095B8' ); } else { $this->output .= '
OBJECT
'; } } elseif($type == 'boolean') { $this->output .= '
'.($value ? 'true' : 'false').'
'; } elseif($type == 'NULL') { $this->output .= '
NULL
'; } else { $this->output .= '
'.$value.'
'; } $this->output .= '
...['.$skipped_count.' skipped]
'; } } # helper function.. calls print_a() inside the print_a_class function print_a( $array, $mode = 0, $show_object_vars = false, $limit = false ) { $output = ''; if( is_array( $array ) or is_object( $array ) ) { if( empty( $array ) ) { $output .= 'print_a( empty array )'; } $pa = &new Print_a_class; $show_object_vars and $pa->show_object_vars = true; if( $limit ) { $pa->limit = $limit; // $output .= 'showing only '.$limit.' entries for arrays with numeric keys'; } if ( is_object($array) ) { $pa->print_a( get_object_vars($array) ); } else { $pa->print_a( $array ); } # $output = $pa->output; unset($pa); $output .= $pa->output; } elseif( gettype($array) == 'boolean') { $output .= 'print_a( '.($array === true ? 'true' : 'false').' )'; } else { $output .= 'print_a( '.gettype( $array ).' )'; } if($mode === 0 || $mode == NULL || $mode == false) { print $output; return true; } if($mode == 1) { return $output; } if(is_string($mode) || $mode == 2 ) { $debugwindow_origin = $_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]; if(preg_match('/(.+)::(.+)/', $mode, $matches)) { $mode = $matches[1]; $remote_addr = gethostbyname($matches[2]); if($_SERVER['REMOTE_ADDR'] != $remote_addr) return; } if(preg_match('/^_(.*)/', $mode, $matches)) { #$output = "
{$matches[1]}
$output
"; $output = "
{$matches[1]}$output

"; print $output; } else { print ' '; } } if($mode == 3) { print ' '; } } // shows mysql-result as a table.. # not ready yet :( function print_result($RESULT) { if(!$RESULT) return; if(mysql_num_rows($RESULT) < 1) return; $fieldcount = mysql_num_fields($RESULT); for ($i=0; $i<$fieldcount; $i++) { $tables[mysql_field_table($RESULT, $i)]++; } print ' '; print ''; print ''; foreach ($tables as $tableName => $tableCount) { $col == '0054A6' ? $col = '003471' : $col = '0054A6'; print ''; } print ''; print ''; for ($i=0;$i < mysql_num_fields($RESULT);$i++) { $FIELD = mysql_field_name($RESULT, $i); $col == '0054A6' ? $col = '003471' : $col = '0054A6'; print ''; } print ''; mysql_data_seek($RESULT, 0); while ($DB_ROW = mysql_fetch_array($RESULT, MYSQL_NUM)) { $pointer++; if($toggle) { $col1 = "E6E6E6"; $col2 = "DADADA"; } else { $col1 = "E1F0FF"; $col2 = "DAE8F7"; } $toggle = !$toggle; print ''; foreach ($DB_ROW as $value) { $col == $col1 ? $col = $col2 : $col = $col1; print ''; } print ''; } print '
'.$tableName.'
'.$FIELD.'
'.nl2br($value).'
'; mysql_data_seek($RESULT, 0); } ###################### # reset the millisec timer # function reset_script_runtime() { $GLOBALS['MICROTIME_START'] = microtime(); } ###################### # function returns the milliseconds passed # function script_runtime() { $MICROTIME_END = microtime(); $MICROTIME_START = explode(' ', $GLOBALS['MICROTIME_START']); $MICROTIME_END = explode(' ', $MICROTIME_END); $GENERATIONSEC = $MICROTIME_END[1] - $MICROTIME_START[1]; $GENERATIONMSEC = $MICROTIME_END[0] - $MICROTIME_START[0]; $GENERATIONTIME = substr($GENERATIONSEC + $GENERATIONMSEC, 0, 8); return (float) $GENERATIONTIME; } function _script_globals() { global $GLOBALS_initial_count; $varcount = 0; foreach ($GLOBALS as $GLOBALS_current_key => $GLOBALS_current_value) { if(++$varcount > $GLOBALS_initial_count) { /* die wollen wir nicht! */ if ($GLOBALS_current_key != 'HTTP_SESSION_VARS' && $GLOBALS_current_key != '_SESSION') { $script_GLOBALS[$GLOBALS_current_key] = $GLOBALS_current_value; } } } unset($script_GLOBALS['GLOBALS_initial_count']); return $script_GLOBALS; } ###################### # function shows all superglobals and script defined global variables # show_vars() without the first parameter shows all superglobals except $_ENV and $_SERVER # show_vars(1) shows all # show_vars(#,1) shows object properties in addition # function show_vars($show_all_vars = false, $show_object_vars = false, $limit = 5) { if($limit === 0) $limit = false; if(isset($GLOBALS['no_vars'])) return; $script_globals = _script_globals(); print ' '; print '
DEBUG (runtime: '.script_runtime().' sec) '; $vars_arr['script_globals'] = array('global script variables', '#7ACCC8'); $vars_arr['_GET'] = array('$_GET', '#7DA7D9'); $vars_arr['_POST'] = array('$_POST', '#F49AC1'); $vars_arr['_FILES'] = array('$_FILES', '#82CA9C'); $vars_arr['_SESSION'] = array('$_SESSION', '#FCDB26'); $vars_arr['_COOKIE'] = array('$_COOKIE', '#A67C52'); if($show_all_vars) { $vars_arr['_SERVER'] = array('SERVER', '#A186BE'); $vars_arr['_ENV'] = array('ENV', '#7ACCC8'); } foreach ($vars_arr as $vars_name => $vars_data) { if($vars_name != 'script_globals') global $$vars_name; if($$vars_name) { print '
'.$vars_data[0].'
'; print_a($$vars_name, NULL, $show_object_vars, $limit); print '
'; } } print '
'; } ###################### # function prints/returns strings wrapped between

    #
    function pre( $string, $return_mode = false, $tabwidth = 3 ) {
        $tab = str_repeat(' ', $tabwidth);
        $string = preg_replace('/\t+/em', "str_repeat( ' ', strlen('\\0') * $tabwidth );", $string); /* replace all tabs with spaces */

        $out = '
'.$string."
\n"; if($return_mode) { return $out; } else { print $out; } } function _check_for_leading_tabs( $string ) { return preg_match('/^\t/m', $string); } function _remove_exessive_leading_tabs( $string ) { /* remove whitespace lines at start of the string */ $string = preg_replace('/^\s*\n/', '', $string); /* remove whitespace at end of the string */ $string = preg_replace('/\s*$/', '', $string); # kleinste Anzahl von f쨲enden TABS z䨬en preg_match_all('/^\t+/', $string, $matches); $minTabCount = strlen(@min($matches[0])); # und entfernen $string = preg_replace('/^\t{'.$minTabCount.'}/m', '', $string); return $string; } } // use debuglib // Define no-op functions in case debug functions were accidentally left // in the live system. else { function print_a() {} function print_result() {} function reset_script_runtime() {} function script_runtime() {} function show_vars() {} function pre() {} } // don't use debuglib ?>