-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.php
More file actions
148 lines (136 loc) · 4.39 KB
/
cli.php
File metadata and controls
148 lines (136 loc) · 4.39 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* The CLIPHP class is an easy to use php-cli class that allows your php cli
* scripts to gather user input and manage arguments for your script. Please
* refer to the README.txt for documentation on usage.
*/
class CLIPHP {
// The arguments for this script.
var $args = array();
// Constructor.
public function __construct() {
global $argv;
set_time_limit(0);
// Try the config file first.
$handle = fopen( dirname(__FILE__) . '/config', 'a+');
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$values = explode('=', $buffer);
$this->args[trim($values[0])] = trim($values[1]);
}
fclose($handle);
}
// Now try the arguments.
$name = '';
foreach( $argv as $arg ) {
if (substr($arg, 0, 1) === '-') {
$name = substr($arg, 1);
}
else if( $name ) {
$this->args[$name] = $arg;
$name = '';
}
}
}
/**
* Reads input from the user.
*
* @return string - The user entered input.
*/
public function read() {
$fp = fopen("php://stdin", "r");
$in = fgets($fp, 4094);
fclose($fp);
return trim($in);
}
/**
* Reads silent input from the user. Good for passwords, etc.
*
* Should work on UNIX/DOS
* http://blogs.sitepoint.com/interactive-cli-password-prompt-in-php/
*
* @param string - The prompt to give to the user to enter in their data.
* @return string - The user entered input.
*/
public function readSilent($prompt) {
if (preg_match('/^win/i', PHP_OS)) {
$vbscript = sys_get_temp_dir() . 'prompt_password.vbs';
file_put_contents(
$vbscript, 'wscript.echo(InputBox("'
. addslashes($prompt)
. '", "", "password here"))');
$command = "cscript //nologo " . escapeshellarg($vbscript);
$in = rtrim(shell_exec($command));
unlink($vbscript);
} else {
$command = "/usr/bin/env bash -c 'echo OK'";
if (rtrim(shell_exec($command)) !== 'OK') {
trigger_error("Can't invoke bash");
return;
}
$command = "/usr/bin/env bash -c 'read -s -p \""
. addslashes($prompt)
. "\" mypassword && echo \$mypassword'";
$in = rtrim(shell_exec($command));
}
return $in;
}
/**
* Caches user input based on the name of the parameter and the value.
* This will add the key=value pairs to a config file within the cliphp
* library directory.
*
* @param string - The name of the parameter.
* @param string - The value of that parameter.
*/
public function cache( $name, $value ) {
$handle = fopen( dirname(__FILE__) . '/config', 'a+');
if ($handle) {
fwrite($handle, $name . '=' . $value . "\n");
fclose($handle);
}
}
/**
* Sets the value of an argument within the args array and caches that value
* if the user provided to do so.
*
* @param string - The name of the parameter.
* @param string - The value of the parameter.
* @param string - Whether or not we should cache the response.
*/
public function set( $name, $value, $cache = FALSE ) {
if( !isset($this->args[$name]) ) {
if( $cache ) {
$this->cache( $name, $value );
}
}
$this->args[$name] = $value;
}
/**
* The main API for this class. This will get a response from the user, or
* return the cached value for a parameter based on whether or not it exists
* in cache or was passed directly to the script.
*
* @param string - The name of the parameter to get.
* @param string - What should be asked to the user if the parameter is not set.
* @param string - Whether the response should be cached or not.
* @param string - If the input should be silenced meaning they cannot see what they type.
*
* @return string - The value of the parameter to get.
*/
public function get( $name, $prompt, $cache = FALSE, $silent = FALSE ) {
// See if we already have this argument set.
if (!isset($this->args[$name])) {
if( $silent ) {
$this->set( $name, $this->readSilent($prompt), $cache );
}
else {
echo $prompt;
$this->set( $name, $this->read(), $cache );
}
}
// Return the value of this argument.
return $this->args[$name];
}
}
?>