This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathhelpers.php
72 lines (56 loc) · 1.53 KB
/
helpers.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
<?php
function is_empty_string($string)
{
return strlen($string) <= 0;
}
function is_equal_strings($string1, $string2)
{
return strcmp($string1, $string2) == 0;
}
function get_hash($algorithm, $string)
{
return hash($algorithm, trim((string) $string));
}
// Command execution
function execute_command($command)
{
$descriptors = [
0 => ['pipe', 'r'], // STDIN
1 => ['pipe', 'w'], // STDOUT
2 => ['pipe', 'w'], // STDERR
];
$process = proc_open($command.' 2>&1', $descriptors, $pipes);
if (! is_resource($process)) {
exit("Can't execute command.");
}
// Nothing to push to STDIN
fclose($pipes[0]);
$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$error = stream_get_contents($pipes[2]);
fclose($pipes[2]);
// All pipes must be closed before "proc_close"
$code = proc_close($process);
return $output;
}
// Command parsing
function parse_command($command)
{
$value = ltrim((string) $command);
if (! is_empty_string($value)) {
$values = explode(' ', $value);
$values_total = count($values);
if ($values_total > 1) {
$value = $values[$values_total - 1];
for ($index = $values_total - 2; $index >= 0; $index--) {
$value_item = $values[$index];
if (substr($value_item, -1) == '\\') {
$value = $value_item.' '.$value;
} else {
break;
}
}
}
}
return $value;
}