add sendControlCommand function to do the heavy work of sending zmcontrol a command

pull/666/head
Isaac Connor 2015-01-07 16:24:07 -05:00
parent e9fc7cf33a
commit 2e48c2ba3d
1 changed files with 27 additions and 0 deletions

View File

@ -963,3 +963,30 @@ function buildControlCommand( $monitor )
$ctrlCommand .= " --command=".$_REQUEST['control'];
return( $ctrlCommand );
}
function sendControlCommand($mid,$command) {
// Either connects to running zmcontrol.pl or runs zmcontrol.pl to send the command.
$socket = socket_create( AF_UNIX, SOCK_STREAM, 0 );
if ( $socket < 0 ) {
Fatal( "socket_create() failed: ".socket_strerror($socket) );
}
$sockFile = ZM_PATH_SOCKS.'/zmcontrol-'.$mid.'.sock';
if ( @socket_connect( $socket, $sockFile ) ) {
$options = array();
foreach ( explode( " ", $command ) as $option ) {
if ( preg_match( '/--([^=]+)(?:=(.+))?/', $option, $matches ) ) {
$options[$matches[1]] = $matches[2]?$matches[2]:1;
}
}
$optionString = jsonEncode( $options );
if ( !socket_write( $socket, $optionString ) ) {
Fatal( "Can't write to control socket: ".socket_strerror(socket_last_error($socket)) );
}
socket_close( $socket );
} else if ( $command != 'quit' ) {
$command .= ' --id='.$mid;
// Can't connect so use script
$ctrlOutput = exec( escapeshellcmd( $command ) );
}
} // end function sendControlCommand( $mid, $command )