2007-08-30 02:11:09 +08:00
< ? php
2009-09-28 21:34:10 +08:00
require_once ( 'includes/control_functions.php' );
2007-08-30 02:11:09 +08:00
// Monitor control actions, require a monitor id and control view permissions for that monitor
2008-10-17 00:12:23 +08:00
if ( empty ( $_REQUEST [ 'id' ]) )
ajaxError ( " No monitor id supplied " );
2007-08-30 02:11:09 +08:00
2008-10-17 00:12:23 +08:00
if ( canView ( 'Control' , $_REQUEST [ 'id' ] ) )
2007-08-30 02:11:09 +08:00
{
2008-10-17 00:12:23 +08:00
$monitor = dbFetchOne ( " select C.*,M.* from Monitors as M inner join Controls as C on (M.ControlId = C.Id ) where M.Id = ' " . dbEscape ( $_REQUEST [ 'id' ]) . " ' " );
2007-08-30 02:11:09 +08:00
2009-09-28 21:34:10 +08:00
$ctrlCommand = buildControlCommand ( $monitor );
2007-08-30 02:11:09 +08:00
2009-09-28 22:16:17 +08:00
if ( $ctrlCommand )
2007-08-30 18:43:06 +08:00
{
2008-10-17 00:12:23 +08:00
$socket = socket_create ( AF_UNIX , SOCK_STREAM , 0 );
if ( ! $socket )
ajaxError ( " socket_create() failed: " . socket_strerror ( socket_last_error ()) );
$sock_file = ZM_PATH_SOCKS . '/zmcontrol-' . $monitor [ 'Id' ] . '.sock' ;
if ( @ socket_connect ( $socket , $sock_file ) )
2007-08-30 18:43:06 +08:00
{
2008-10-17 00:12:23 +08:00
$options = array ();
2011-02-06 23:28:25 +08:00
foreach ( explode ( " " , $ctrlCommand ) as $option )
2007-08-30 18:43:06 +08:00
{
2008-10-17 00:12:23 +08:00
if ( preg_match ( '/--([^=]+)(?:=(.+))?/' , $option , $matches ) )
{
$options [ $matches [ 1 ]] = ! empty ( $matches [ 2 ]) ? $matches [ 2 ] : 1 ;
}
2007-08-30 18:43:06 +08:00
}
2008-10-17 00:12:23 +08:00
$option_string = serialize ( $options );
if ( ! socket_write ( $socket , $option_string ) )
ajaxError ( " socket_write() failed: " . socket_strerror ( socket_last_error ()) );
ajaxResponse ( 'Used socket' );
//socket_close( $socket );
2007-08-30 18:43:06 +08:00
}
else
{
2008-10-17 00:12:23 +08:00
$ctrlCommand .= " --id= " . $monitor [ 'Id' ];
// Can't connect so use script
$ctrlStatus = '' ;
$ctrlOutput = array ();
exec ( escapeshellcmd ( $ctrlCommand ), $ctrlOutput , $ctrlStatus );
if ( $ctrlStatus )
ajaxError ( $ctrlCommand . '=>' . join ( ' // ' , $ctrlOutput ) );
ajaxResponse ( 'Used script' );
2007-08-30 18:43:06 +08:00
}
}
2007-08-30 02:11:09 +08:00
else
2007-08-30 18:43:06 +08:00
{
2008-10-17 00:12:23 +08:00
ajaxError ( " No command received " );
2007-08-30 18:43:06 +08:00
}
2007-08-30 02:11:09 +08:00
}
2008-10-17 00:12:23 +08:00
ajaxError ( 'Unrecognised action or insufficient permissions' );
2007-08-30 02:11:09 +08:00
2008-10-17 00:12:23 +08:00
function ajaxCleanup ()
{
global $socket ;
if ( ! empty ( $socket ) )
@ socket_close ( $socket );
}
2007-08-30 02:11:09 +08:00
?>