Skip to content

Commit

Permalink
Fixes #76 - Add hex dump support for trace output wth helper function…
Browse files Browse the repository at this point in the history
… bw_trace_hexdump
  • Loading branch information
bobbingwide committed Nov 22, 2019
1 parent de9f18d commit 4e953a3
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 57 deletions.
19 changes: 18 additions & 1 deletion includes/bwtrace.php
Expand Up @@ -1037,7 +1037,24 @@ function bw_list_trace_levels() {
function bw_trace_trace_startup() {
global $bw_trace;
$bw_trace->trace_startup();
}
}

/**
* Returns a hexdump version of the string
*
* @param string $string
* @return string
*/
function bw_trace_hexdump( $string ) {
if ( !function_exists( "oik_hexdump") ) {
oik_require_lib( 'hexdump' );
}
if ( function_exists( "oik_hexdump" ) ) {
$string = oik_hexdump( $string );
}
return $string;

}


}
66 changes: 66 additions & 0 deletions libs/hexdump.php
@@ -0,0 +1,66 @@
<?php
if ( !defined( "HEXDUMP_INCLUDED" ) ) {
define( "HEXDUMP_INCLUDED", "1.0.0" );

/**
* Hexadecimal dumping functions
*
* Library: hexdump
* Provides: hexdump
* Depends:
*
* Helps you to find those pesky control characters
*
* I first wrote this code in PHP in Sep 2013. It echoed the output.
* Now I need it to be returned as a string, so rewriting as oik_hexdump().
* Quite a few people look for something similar. https://stackoverflow.com/questions/1057572/how-can-i-get-a-hex-dump-of-a-string-in-php
*
*/

/**
* Returns the hex dump of the string
*/
function oik_hexdump( $string ) {
$hexdump = null;
$count = strlen( $string );
$hexdump .= $count;
$hexdump .= PHP_EOL;
$lineo = "";
$hexo = "";
for ( $i = 1; $i <= $count; $i ++ ) {
$ch = $string[ $i - 1 ];
if ( ctype_cntrl( $ch ) ) {
$lineo .= ".";
} else {
$lineo .= $ch;
}
$hexo .= bin2hex( $ch );
$hexo .= " ";
if ( 0 == $i % 20 ) {
$hexdump .= $lineo . " " . $hexo . PHP_EOL;
$lineo = "";
$hexo = "";
}
}
$hexdump .= substr( $lineo . str_repeat( ".", 20 ), 0, 20 );
$hexdump .= " ";
$hexdump .= $hexo;
$hexdump .= PHP_EOL;

return $hexdump;
}

/**
* Echoes the hex dump of a string
* @param $string
*/
function hexdump( $string ) {
echo oik_hexdump( $string );
}

}





2 changes: 1 addition & 1 deletion oik-bwtrace.php
Expand Up @@ -178,7 +178,7 @@ function oik_bwtrace_plugins_loaded() {
*/
function oik_bwtrace_query_libs( $libraries ) {
$lib_args = array();
$libs = array( "bobbfunc" => null, "bobbforms" => "bobbfunc", "oik-admin" => "bobbforms" );
$libs = array( "bobbfunc" => null, "bobbforms" => "bobbfunc", "oik-admin" => "bobbforms", 'hexdump' => null );
$versions = array( "bobbfunc" => "3.2.0", "bobbforms" => "3.2.0", "oik-admin" => "3.2.0" );
foreach ( $libs as $library => $depends ) {
$lib_args['library'] = $library;
Expand Down

0 comments on commit 4e953a3

Please sign in to comment.