-
Notifications
You must be signed in to change notification settings - Fork 2
/
os.php
309 lines (257 loc) · 7.17 KB
/
os.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?
/** This class has functions which handels OS-specific functions. */
class knj_os{
/** Runs a command as a pipe and returns the output. */
static function shellCMD($cmd){
//Send command to Unix-prompt.
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a file to write to
);
$process = proc_open($cmd, $descriptorspec, $pipes);
if (!$process){
throw new exception("Could not start process from command: '" . $cmd . "'.");
}
//Read result.
$result = "";
while(!feof($pipes[1])){
$result .= fread($pipes[1], 4096);
}
//Read errors.
$error = "";
while(!feof($pipes[2])){
$error .= fread($pipes[2], 4096);
}
return array(
"result" => $result,
"error" => $error
);
}
/** Returns runnning processes. */
static function getProcs($args = null){
if (is_array($args) && $args["grep"]){
$grep = $args["grep"];
$command = "ps aux | " . $grep;
}elseif(is_string($args) && strlen($args) > 0){
require_once "knj/functions_knj_strings.php";
$grep = "grep " . knj_string_unix_safe($args);
$command = "ps aux | " . $grep;
}else{
$command = "ps aux";
}
$command .= " | grep -vir grep";
$psaux = knj_os::shellCMD($command);
$procs = explode("\n", $psaux["result"]);
$return = array();
foreach($procs AS $proc){
$proc = trim($proc);
if (strlen($proc) > 0 && substr($proc, 0, 4) != "USER"){
if (preg_match("/^(\S+)\s+([0-9]+)\s+([0-9.]+)\s+([0-9.]+)\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+ ([\s\S]+)$/", $proc, $match)){
$cmd = $match[5];
if ($cmd != $command && $cmd != $grep && $cmd != "sh -c " . $command){
$user = $match[1];
$pid = $match[2];
$cpu = $match[3];
$ram = $match[4];
$return[] = array(
"user" => $user,
"pid" => $pid,
"cpu" => $cpu,
"ram" => $ram,
"cmd" => $cmd
);
}
}else{
echo "One of the processes wasnt not read: \"" . $proc . "\".\n";
}
}
}
return $return;
}
/** Runs a command with system() and returns the output. */
static function systemCMD($cmd){
ob_start();
system($cmd, $return);
if (!$return){
$return = ob_get_contents();
}
ob_end_clean();
return $return;
}
/** Returns the path of the a graphical sudo, if installed. */
static function getGraphicalSudo($cmd){
$tests = array(
"/usr/bin/gksu",
"/usr/bin/gksudo"
);
foreach($tests AS $value){
if (file_exists($value)){
$sudo = $value;
break;
}
}
if ($cmd){
$sudo .= " \"" . $cmd . "\"";
}
return $sudo;
}
/** Returns the type of running client-browser. */
static function getBrowser(){
$useragent = $_SERVER[HTTP_USER_AGENT];
if (strpos($_SERVER[HTTP_USER_AGENT], "MSIE") !== false){
return "ie";
}elseif(strpos($_SERVER[HTTP_USER_AGENT], "Opera") !== false){
return "opera";
}elseif(strpos($_SERVER[HTTP_USER_AGENT], "Firefox") !== false){
return "mozilla";
}else{
return "unknown";
}
}
/** Returns the user, which is running the script. */
static function whoAmI(){
global $knj_whoami;
if ($knj_whoami){
return $knj_whoami;
}
$os = knj_os::getOS();
if ($os["os"] == "linux"){
$knj_whoami = trim(knj_os::systemCMD("whoami"));
}else{
throw new Exception("Unsupported OS: " . $os["os"]);
}
return $knj_whoami;
}
static function getHomeDir(){
if ($_SERVER["HOME"]){ //linux
return $_SERVER["HOME"];
}elseif($_SERVER["USERPROFILE"]){ //windows
return $_SERVER["USERPROFILE"];
}
$os = knj_os::getOS();
if ($os == "linux"){
$res = knj_os::shellCMD("echo \$HOME");
return trim($res["result"]);
}
throw new Exception("Could not find out home-dir.");
}
/** Returns the type of running OS ("windows", "linux"...). */
static function getOS(){
global $knj_getos;
if (!$knj_getos){
if (array_key_exists("OS", $_SERVER) and strpos(strtolower($_SERVER["OS"]), "windows") !== false){
$knj_getos["os"] = "windows";
}else{
$knj_getos["os"] = "linux";
}
}
return $knj_getos;
}
/** Returns the hosts-file. */
static function getHosts(){
$os = knj_os::getOS();
if ($os == "windows"){
$sysroot = str_replace("\\", "/", $_SERVER["SystemRoot"]);
return $sysroot . "/system32/drivers/etc/hosts";
}else{
return "/etc/hosts";
}
}
/** Returns the supported newline string. */
static function getNewLine(){
$os = knj_os::getOS();
if ($os == "windows"){
return "\r\n";
}else{
return "\n";
}
}
/** Returns the PATH-dirs for the CLI as string in an array. */
static function getPaths(){
$os = knj_os::getOS();
if ($os[os] != "linux"){
throw new Exception("This function only works on Linux.");
}
$paths = knj_os::shellCMD("echo \$PATH");
$dirs = explode(":", $paths["result"]);
return $dirs;
}
/** Checks if a command is registered in PATH. */
static function checkCmd($cmd){
$paths = knj_os::getPaths();
foreach($paths AS $dir){
if (file_exists($dir . "/" . $cmd)){
return array("status" => true, "filepath" => $dir . "/" . $cmd);
}
}
return array("status" => false);
}
/** Adds a new path to the PHP-include_path. */
static function phpPathAdd($path){
$os = knj_os::getOS();
$oldpath = ini_get("include_path");
if ($os[os] == "windows"){
$betw = ";";
}else{
$betw = ":";
}
$paths = explode($betw, $oldpath);
//Check if it already exists.
foreach($paths AS $oldpath){
if (strtolower($oldpath) == strtolower($path)){
return true;
}
}
//The path does not exist - add it.
$paths[] = $path;
$newpaths = implode($betw, $paths);
if (!ini_set("include_path", $newpaths)){
throw new Exception("Could not set the include_path.");
}
return true;
}
/** Fixes "knj FrameWork" to the php-include_path. */
static function phpPathAddKnjFrameWork(){
$dirname = dirname(__FILE__);
$dirname = str_replace("\\", "/", $dirname);
$dirname = explode("/", $dirname);
unset($dirname[count($dirname) - 1]);
$dirname = implode("/", $dirname);
knj_os::phpPathAdd($dirname);
}
/** Returns the path to the PHP-executable, which you need if you want to start new processes. */
static function getPHPExec($version = 5){
$os = knj_os::getOS();
if ($os["os"] == "linux"){
$test_paths = array(
"/usr/bin/php" . $version,
"/usr/local/bin/php" . $version,
"/usr/bin/php",
"/usr/local/bin/php"
);
foreach($test_paths AS $path){
if (file_exists($path)){
return $path;
}
}
if ($_SERVER["_"] && file_exists($_SERVER["_"])){
return $_SERVER["_"];
}
}elseif($os["os"] == "windows"){
//A hack to make this function work with packages created with knjPackageCreater.
if (file_exists("../php5gtk2/php.exe")){
return realpath("../php5gtk2/php.exe");
}
}else{
throw new Exception("Unsupported OS: \"" . $os["os"] . "\".");
}
throw new Exception("Could not find the PHP-executable.");
}
static function using_ssl(){
if ($_SERVER["HTTP_SSL_ENABLED"] or $_SERVER["HTTPS"] or $_SERVER["SSL"]){
return true;
}
return false;
}
}