Skip to content

Instantly share code, notes, and snippets.

@oranj
Created April 20, 2012 20:04
Show Gist options
  • Save oranj/2431437 to your computer and use it in GitHub Desktop.
Save oranj/2431437 to your computer and use it in GitHub Desktop.
Command line decode
#!/usr/bin/php
<?php
class ColorCLI {
static $foreground_colors = array(
'black' => '0;30', 'dark_gray' => '1;30',
'blue' => '0;34', 'light_blue' => '1;34',
'green' => '0;32', 'light_green' => '1;32',
'cyan' => '0;36', 'light_cyan' => '1;36',
'red' => '0;31', 'light_red' => '1;31',
'purple' => '0;35', 'light_purple' => '1;35',
'brown' => '0;33', 'yellow' => '1;33',
'light_gray' => '0;37', 'white' => '1;37',
);
static $background_colors = array(
'black' => '40', 'red' => '41',
'green' => '42', 'yellow' => '43',
'blue' => '44', 'magenta' => '45',
'cyan' => '46', 'light_gray' => '47',
);
public static function strip($text) {
$out = preg_replace('/\033\[[0-9;]+m/', '', $text);
return $out;
}
public static function blink($text) {
return "\033[5m".$text."\033[0m";
}
public static function __callStatic( $foreground_color, $args ) {
$string = $args[0];
$background_color = false;
if( isset($args[1]) ) {
$background_color = $args[1];
}
$colored_string = "";
// Check if given foreground color found
if( isset(self::$foreground_colors[$foreground_color]) ) {
$colored_string .= "\033[" . self::$foreground_colors[$foreground_color] . "m";
}
// Check if given background color found
if ($background_color && isset(self::$background_colors[$background_color])) {
$colored_string .= "\033[" . self::$background_colors[$background_color] . "m";
}
// Add string and end coloring
$colored_string .= $string . "\033[0m";
return $colored_string;
}
}
function get_tty_width() {
$output = exec('tput cols');
return $output;
}
function text_fill($text, $fill = '=', $align = 'left') {
if (! is_string($fill) || strlen($fill) != 1) {
throw new Exception("Fill parameter must be a single character");
}
$full_width = get_tty_width();
$text = ($align == 'left'?($text.' '):
($align == 'right'?(' '.$text):(' '.$text.' ')));
$remainder_width = $full_width - strlen($text);
switch ($align) {
case 'left':
$left_width = 0;
break;
case 'right':
$left_width = $remainder_width;
break;
case 'center':
default:
$left_width = ceil($remainder_width / 2);
break;
}
$right_width = $remainder_width - $left_width;
$str = str_repeat($fill, $left_width).$text.str_repeat($fill, $right_width)."\n";
return $str;
}
function decode_backtrace_block( $debug ) {
$debug = preg_replace('/[^a-z0-9=+\/]/si', '', $debug);
return json_decode( @gzinflate( base64_decode($debug) ), true );
}
if ($argc < 2) {
echo(ColorCLI::red("Please input an encoded message\n"));
exit;
}
$message = $argv[1];
$backtrace = decode_backtrace_block($message);
$longest_function_length = 10;
foreach ($backtrace as $backtrace_instance) {
$size = strlen(trim($backtrace_instance['function']));
if ($size > $longest_function_length) {
$longest_function_length = $size;
}
}
function get_remaining_width($width, $longest_key_length) {
return (($width - 4) - $longest_key_length) - 1;
}
function print_horizontal_separator($longest_key, $width, $location = 'middle') {
static $left = Array(
'top' => '╔',
'bottom' => '╚',
'middle' => '╠',
'sub' => '╬',
);
static $middle = Array(
'top' => '╦',
'bottom' => '╩',
'middle' => '╬',
'sub' => '╦'
);
static $right = Array(
'top'=> '╗',
'bottom' => '╝',
'middle' => '╣',
'sub' => '╣',
);
$remaining_width = get_remaining_width($width, $longest_key);
return $left[$location].str_repeat('═', $longest_key + 2).$middle[$location].str_repeat('═', $remaining_width).$right[$location]."\n";
}
function print_args($args, $width = NULL, $depth = 0) {
if (! $args) {
return '';
}
if (is_null($width)) {
$width = get_tty_width();
}
$str = '';
$longest_key = 0;
$padding = 1;
foreach ($args as $key => $arg) {
if (strlen($key) > $longest_key) {
$longest_key = strlen($key);
}
}
if ($depth ==0) {
$str .= print_horizontal_separator($longest_key, $width, 'top');
} else {
$str .= print_horizontal_separator($longest_key, $width, 'sub');
}
$remaining_width = get_remaining_width($width, $longest_key);
$right_inner_width = $remaining_width - 2;
$i = 1;
$p = str_repeat(' ', $padding);
$white_space = str_repeat(' ', 2 * $padding + $longest_key + 1);
$previous_arg = NULL;
foreach ($args as $key => $arg) {
if (is_array($arg) || is_object($arg)) {
$inner_output = print_args($arg, $remaining_width + 2, $depth + 1);
$lines = explode("\n", $inner_output);
$str .= "║".$p.sprintf('%-'.$longest_key.'s', $key).$p.'║'.$p.ColorCLI::cyan(sprintf('%-'.($right_inner_width + 2).'s', '('.$arg.') ↩')).$p."║\n";
foreach (array_filter($lines) as $j => $line) {
if ($j == 0) {
$str .= '╚═'.str_repeat('═', $longest_key).'═';
} else {
$str .= $white_space;
}
$str .= $line."\n";
}
} else {
if ($previous_arg && (is_array($previous_arg) || is_object($previous_arg))) {
$strs = array_filter(explode("\n", $str));
$first_n = join("\n", array_slice($strs, 0, sizeof($strs) - 1));
$last = $strs[sizeof($strs) - 1];
$last = '╔'.substr($last, 1);
$last = str_replace(
Array(
' ',
'╚',
'╝'),
Array(
'═',
'╬',
'╣',
),
$last);
$str = $first_n."\n".$last."\n";
}
$type = gettype($arg);
$type_string = '('.$type.') ';
$colored_type_string = ColorCLI::cyan('('.$type.') ');
$len = strlen($colored_type_string) - strlen($type_string);
$prefix = str_repeat('x', strlen($type_string) - 2);
$string = $prefix.$arg;
$lines = str_split($string, $right_inner_width - 2);
foreach ($lines as $k => $line) {
if ($k == 0) {
$line = str_replace($prefix, $colored_type_string, $line);
$str .= "║".$p.sprintf('%-'.$longest_key.'s', $key).$p.'║'.$p.sprintf('%-'.($right_inner_width + $len).'s', $line).$p."║\n";
} else {
$str .= "║".$p.str_repeat(' ', $longest_key).$p.'║'.$p.ColorCLI::yellow('↳ ').sprintf('%-'.($right_inner_width - 2).'s', $line).$p."║\n";
}
}
if ($i == sizeof($args)) {
$str .= print_horizontal_separator($longest_key, $width, 'bottom');
}
}
$previous_arg = $arg;
$i++;
}
return $str;
}
#$longest_function_length = max(array_map('strlen', array_map('reset', $backtrace)));
$print_format = '%'.$longest_function_length.'s';
foreach ($backtrace as $backtrace_instance) {
$contents = '';
echo("\n".ColorCLI::white(text_fill("Called Function: ".$backtrace_instance['function'].'()',' ', 'left'), 'blue'));
if (isset($backtrace_instance['file'])) {
echo(ColorCLI::cyan("File: ").$backtrace_instance['file']."\n");
}
if (isset($backtrace_instance['line'])) {
echo(ColorCLI::cyan("Line: ").$backtrace_instance['line']."\n");
}
if (isset($backtrace_instance['line']) && isset($backtrace_instance['file'])) {
$contents = @file_get_contents($backtrace_instance['file']);
$line_number = $backtrace_instance['line'] - 1;
$buffer = 3;
if ($contents) {
$lines = preg_split('/$\R?^/m', $contents);
$min = max($line_number - $buffer, 0);
$max = min($line_number + $buffer + 1, sizeof($lines));
$digits = strlen($max);
for ($i = $min; $i < $max; $i++) {
echo(ColorCLI::yellow(sprintf('%-'.$digits.'d: ', $i + 1)));
if ($i == $line_number) {
echo(preg_replace('/'.$backtrace_instance['function'].'/i', ColorCLI::green($backtrace_instance['function']), $lines[$i])."\n");
} else {
echo($lines[$i]."\n");
}
}
}
}
if (isset($backtrace_instance['args']) && $backtrace_instance['args']) {
echo(ColorCLI::cyan("Args: \n"));
echo print_args($backtrace_instance['args']);;
}
}
exit;
@oranj
Copy link
Author

oranj commented Apr 20, 2012

Made it more jesse friendly.

@oranj
Copy link
Author

oranj commented Apr 20, 2012

Fixed issue with arrays not being the last parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment