-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbin_actions
executable file
·74 lines (62 loc) · 2.09 KB
/
bin_actions
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
#!/bin/php
<?php
/**
* Intercept calls to Hestia's bin/ directory and invoke plugin actions/filters
* before executing the original command.
*
* @version 1.0.0
* @license GPL-3.0
* @link https://github.com/virtuosoft-dev/hestiacp-pluginable
*
*/
require_once( '/usr/local/hestia/web/pluginable.php' );
global $hcpp;
// Remove the first argument, which is the path to the script
$cmd = '';
array_shift( $argv );
// Check for sudo
if ( $argv[0] == 'sudo' ) {
$cmd = '/usr/bin/sudo ';
array_shift( $argv );
}
// Reconstruct the original command
$hst_command = array_shift( $argv );
$cmd .= '/usr/local/hestia/bin/' . $hst_command;
// Strip legacy v- prefix from command name and use underscores instead of dashes
if ( substr( $hst_command, 0, 3 ) == '/v-' ) {
$hst_command = substr( $hst_command, 3 );
}
if ( substr( $hst_command, 0, 2 ) == 'v-' ) {
$hst_command = substr( $hst_command, 2 );
}
$hst_command = str_replace( '-', '_', $hst_command );
// Invoke any plugin pre_ filters
$argv = $hcpp->do_action( 'pre_' . $hst_command, $argv );
// Pass the modified arguments to the original command
foreach( $argv as $arg ) {
$cmd .= ' ' . escapeshellarg( $arg );
}
// Run the original command
$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, null, null );
fclose($pipes[0]);
// Obtain the original commands output and error stream content
$output = stream_get_contents($pipes[1]);
$error = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$return_val = proc_close( $process );
// Invoke any plugin post_ filters
$output = $hcpp->do_action( 'post_' . $hst_command, $output );
// Return the resulting output, error, and return value to the original caller
$hOut = fopen( 'php://stdout', 'w' );
$hErr = fopen( 'php://stderr', 'w' );
fwrite( $hOut, $output );
fwrite( $hErr, $error );
fclose( $hOut );
fclose( $hErr );
exit( $return_val );