-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCliHacker.php
54 lines (49 loc) · 1.46 KB
/
CliHacker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace Codepunker\Cli;
/**
* PHP CLI enhancements
* colors and bold styles
* don't display password when typing in STDIN
*/
class CliHacker
{
/**
* receives a string and human readable styles
* and returns a styled text to be displayed in
* the terminal...
* @param string $str [the string to be styled]
* @param string $style [style to be applied]
* @return string [styled string]
*/
public static function style($str, $style)
{
$ANSI_CODES = array(
"off" => 0,
"bold" => 1,
"red" => 31,
"green" => 32,
"yellow" => 33,
);
$color_attrs = explode("+", $style);
$ansi_str = "";
foreach ($color_attrs as $attr) {
$ansi_str .= "\033[" . $ANSI_CODES[$attr] . "m";
}
$ansi_str .= $str . "\033[" . $ANSI_CODES["off"] . "m";
return $ansi_str;
}
/**
* asks for password and get's it from stdin
* - nothing is displayed in CLI -
* @return string
*/
public static function pass($again = false)
{
echo ($again===false) ? "Type your password: " . PHP_EOL : "Confirm your password: " . PHP_EOL;
$oldStyle = exec('stty -g'); //cache old style
shell_exec('stty -echo'); //remove echo
$key = rtrim(fgets(STDIN), "\n");
exec('stty ' . $oldStyle); //put echo back
return $key;
}
}