Restructured ajax to be consistent.

git-svn-id: http://svn.zoneminder.com/svn/zm/trunk@2668 e3e1d417-86f3-4887-817a-d78f3d33393f
This commit is contained in:
stan 2008-10-16 16:12:23 +00:00
parent 3aedc486b7
commit 81fd3aaa41
7 changed files with 925 additions and 1022 deletions

View File

@ -1,54 +1,42 @@
<?php
if ( !canView( 'Stream' ) )
{
$view = "error";
return;
}
define( "MSG_TIMEOUT", 2.0 );
define( "MSG_DATA_SIZE", 4+256 );
header("Content-type: text/plain" );
if ( canEdit( 'Monitors' ) )
{
$zmu_command = getZmuCommand( " -m ".validInt($_REQUEST['id']) );
$zmuCommand = getZmuCommand( " -m ".validInt($_REQUEST['id']) );
switch ( validJsStr($_REQUEST['command']) )
{
case "disableAlarms" :
{
$zmu_command .= " -n";
$zmuCommand .= " -n";
break;
}
case "enableAlarms" :
{
$zmu_command .= " -c";
$zmuCommand .= " -c";
break;
}
case "forceAlarm" :
{
$zmu_command .= " -a";
$zmuCommand .= " -a";
break;
}
case "cancelForcedAlarm" :
{
$zmu_command .= " -c";
$zmuCommand .= " -c";
break;
}
default :
{
$response['result'] = 'Error';
$response['message'] = "Unexpected command '".validJsStr($_REQUEST['command'])."'";
echo jsValue( $response );
exit;
ajaxError( "Unexpected command '".validJsStr($_REQUEST['command'])."'" );
}
}
ajaxResponse( exec( escapeshellcmd( $zmuCommand ) ) );
}
$response['result'] = 'Ok';
//error_log( $zmu_command );
$response['message'] = exec( escapeshellcmd( $zmu_command ) );
echo jsValue( $response );
ajaxError( 'Unrecognised action or insufficient permissions' );
?>

File diff suppressed because it is too large Load Diff

View File

@ -1,162 +1,122 @@
<?php
header("Content-type: text/plain" );
$response = array(
'result' => 'Error',
);
if ( empty($_REQUEST['id']) )
{
$response['message'] = 'No event id(s) supplied';
ajaxError( "No event id(s) supplied" );
}
else
if ( canView( 'Events' ) )
{
$refreshEvent = false;
$refreshParent = false;
if ( canEdit( 'Events' ) )
switch ( $_REQUEST['action'] )
{
switch ( $_REQUEST['action'] )
case "video" :
{
case "rename" :
if ( empty($_REQUEST['videoFormat']) )
{
if ( !empty($_REQUEST['eventName']) )
{
dbQuery( "update Events set Name = '".dbEscape($_REQUEST['eventName'])."' where Id = '".dbEscape($_REQUEST['id'])."'" );
$response['result'] = 'Ok';
}
ajaxError( "Video Generation Failure, no format given" );
}
elseif ( empty($_REQUEST['rate']) )
{
ajaxError( "Video Generation Failure, no rate given" );
}
elseif ( empty($_REQUEST['scale']) )
{
ajaxError( "Video Generation Failure, no scale given" );
}
else
{
$sql = "select E.*,M.Name as MonitorName,M.DefaultRate,M.DefaultScale from Events as E inner join Monitors as M on E.MonitorId = M.Id where E.Id = ".dbEscape($_REQUEST['id']).monitorLimitSql();
if ( !($event = dbFetchOne( $sql )) )
ajaxError( "Video Generation Failure, can't load event" );
else
{
$response['message'] = 'No new event name supplied';
}
break;
}
case "eventdetail" :
{
dbQuery( "update Events set Cause = '".dbEscape($_REQUEST['newEvent']['Cause'])."', Notes = '".dbEscape($_REQUEST['newEvent']['Notes'])."' where Id = '".dbEscape($_REQUEST['id'])."'" );
$response['result'] = 'Ok';
$refreshEvent = true;
$refreshParent = true;
break;
}
case "archive" :
case "unarchive" :
{
$archiveVal = ($_REQUEST['action'] == "archive")?1:0;
dbQuery( "update Events set Archived = ".$archiveVal." where Id = '".dbEscape($_REQUEST['id'])."'" );
$response['result'] = 'Ok';
$refreshEvent = true;
break;
}
case "delete" :
{
deleteEvent( dbEscape($_REQUEST['id']) );
$response['result'] = 'Ok';
break;
}
}
}
if ( canView( 'Events' ) )
{
switch ( $_REQUEST['action'] )
{
case "video" :
{
if ( empty($_REQUEST['videoFormat']) )
{
$response['message'] = "Video Generation Failure, no format given";
}
elseif ( empty($_REQUEST['rate']) )
{
$response['message'] = "Video Generation Failure, no rate given";
}
elseif ( empty($_REQUEST['scale']) )
{
$response['message'] = "Video Generation Failure, no scale given";
}
else
{
$sql = "select E.*,M.Name as MonitorName,M.DefaultRate,M.DefaultScale from Events as E inner join Monitors as M on E.MonitorId = M.Id where E.Id = ".dbEscape($_REQUEST['id']).monitorLimitSql();
if ( !($event = dbFetchOne( $sql )) )
{
$response['message'] = "Video Generation Failure, can't load event";
}
if ( $videoFile = createVideo( $event, $_REQUEST['videoFormat'], $_REQUEST['rate'], $_REQUEST['scale'], !empty($_REQUEST['overwrite']) ) )
ajaxResponse( array( 'response'=>$videoFile ) );
else
{
if ( $videoFile = createVideo( $event, $_REQUEST['videoFormat'], $_REQUEST['rate'], $_REQUEST['scale'], !empty($_REQUEST['overwrite']) ) )
{
//$eventPath = getEventPath( $event );
//$response['videoPath'] = $eventPath.'/'.$videoFile;
$response['result'] = 'Ok';
$response['videoPath'] = $videoFile;
}
else
{
$response['message'] = "Video Generation Failed";
}
}
}
break;
ajaxError( "Video Generation Failed" );
}
case 'deleteVideo' :
{
unlink( $videoFiles[$_REQUEST['id']] );
unset( $videoFiles[$_REQUEST['id']] );
$response['result'] = 'Ok';
break;
}
case "export" :
{
require_once( ZM_SKIN_PATH.'/includes/export_functions.php' );
$ok = true;
break;
}
case 'deleteVideo' :
{
unlink( $videoFiles[$_REQUEST['id']] );
unset( $videoFiles[$_REQUEST['id']] );
ajaxResponse();
break;
}
case "export" :
{
require_once( ZM_SKIN_PATH.'/includes/export_functions.php' );
if ( !empty($_REQUEST['exportDetail']) )
$exportDetail = $_SESSION['export']['detail'] = $_REQUEST['exportDetail'];
else
$exportDetail = false;
if ( !empty($_REQUEST['exportFrames']) )
$exportFrames = $_SESSION['export']['frames'] = $_REQUEST['exportFrames'];
else
$exportFrames = false;
if ( !empty($_REQUEST['exportImages']) )
$exportImages = $_SESSION['export']['images'] = $_REQUEST['exportImages'];
else
$exportImages = false;
if ( !empty($_REQUEST['exportVideo']) )
$exportVideo = $_SESSION['export']['video'] = $_REQUEST['exportVideo'];
else
$exportVideo = false;
if ( !empty($_REQUEST['exportMisc']) )
$exportMisc = $_SESSION['export']['misc'] = $_REQUEST['exportMisc'];
else
$exportMisc = false;
if ( !empty($_REQUEST['exportFormat']) )
$exportFormat = $_SESSION['export']['format'] = $_REQUEST['exportFormat'];
else
$exportFormat = '';
if ( !empty($_REQUEST['exportDetail']) )
$exportDetail = $_SESSION['export']['detail'] = $_REQUEST['exportDetail'];
else
$exportDetail = false;
if ( !empty($_REQUEST['exportFrames']) )
$exportFrames = $_SESSION['export']['frames'] = $_REQUEST['exportFrames'];
else
$exportFrames = false;
if ( !empty($_REQUEST['exportImages']) )
$exportImages = $_SESSION['export']['images'] = $_REQUEST['exportImages'];
else
$exportImages = false;
if ( !empty($_REQUEST['exportVideo']) )
$exportVideo = $_SESSION['export']['video'] = $_REQUEST['exportVideo'];
else
$exportVideo = false;
if ( !empty($_REQUEST['exportMisc']) )
$exportMisc = $_SESSION['export']['misc'] = $_REQUEST['exportMisc'];
else
$exportMisc = false;
if ( !empty($_REQUEST['exportFormat']) )
$exportFormat = $_SESSION['export']['format'] = $_REQUEST['exportFormat'];
else
$exportFormat = '';
if ( $exportFile = exportEvents( $_REQUEST['id'], $exportDetail, $exportFrames, $exportImages, $exportVideo, $exportMisc, $exportFormat ) )
{
$response['exportFile'] = $exportFile;
$response['result'] = 'Ok';
}
else
{
$response['message'] = 'Export Failed';
}
break;
}
if ( $exportFile = exportEvents( $_REQUEST['id'], $exportDetail, $exportFrames, $exportImages, $exportVideo, $exportMisc, $exportFormat ) )
ajaxResponse( array( 'exportFile'=>$exportFile ) );
else
ajaxError( "Export Failed" );
break;
}
}
if ( $response['result'] == 'Ok' )
}
if ( canEdit( 'Events' ) )
{
switch ( $_REQUEST['action'] )
{
$response['refreshParent'] = $refreshParent;
$response['refreshEvent'] = $refreshEvent;
case "rename" :
{
if ( !empty($_REQUEST['eventName']) )
dbQuery( "update Events set Name = '".dbEscape($_REQUEST['eventName'])."' where Id = '".dbEscape($_REQUEST['id'])."'" );
else
ajaxError( "No new event name supplied" );
ajaxResponse( array( 'refreshEvent'=>true, 'refreshParent'=>true ) );
break;
}
case "eventdetail" :
{
dbQuery( "update Events set Cause = '".dbEscape($_REQUEST['newEvent']['Cause'])."', Notes = '".dbEscape($_REQUEST['newEvent']['Notes'])."' where Id = '".dbEscape($_REQUEST['id'])."'" );
ajaxResponse( array( 'refreshEvent'=>true, 'refreshParent'=>true ) );
break;
}
case "archive" :
case "unarchive" :
{
$archiveVal = ($_REQUEST['action'] == "archive")?1:0;
dbQuery( "update Events set Archived = ".$archiveVal." where Id = '".dbEscape($_REQUEST['id'])."'" );
ajaxResponse( array( 'refreshEvent'=>true, 'refreshParent'=>false ) );
break;
}
case "delete" :
{
deleteEvent( dbEscape($_REQUEST['id']) );
ajaxResponse( array( 'refreshEvent'=>false, 'refreshParent'=>true ) );
break;
}
}
elseif ( empty($response['message']) )
$response['message'] = 'Unrecognised action or insufficient permissions';
}
echo jsValue( $response );
ajaxError( 'Unrecognised action or insufficient permissions' );
?>

View File

@ -163,13 +163,6 @@ $statusData = array(
)
);
function throwError( $message, $code=400 )
{
error_log( $message );
header( "HTTP/1.0 $code $message" );
exit();
}
function collectData()
{
global $statusData;
@ -177,10 +170,7 @@ function collectData()
$entitySpec = &$statusData[strtolower(validJsStr($_REQUEST['entity']))];
#print_r( $entitySpec );
if ( !canView( $entitySpec['permission'] ) )
{
error_log( "Invalid permissions" );
return;
}
ajaxError( 'Unrecognised action or insufficient permissions' );
if ( !empty($entitySpec['func']) )
{
@ -220,7 +210,7 @@ function collectData()
foreach ( $_REQUEST['element'] as $element )
{
if ( !($elementData = $lc_elements[strtolower($element)]) )
throwError( "Bad ".validJsStr($_REQUEST['entity'])." element ".$element );
ajaxError( "Bad ".validJsStr($_REQUEST['entity'])." element ".$element );
if ( isset($elementData['func']) )
$data[$element] = eval( "return( ".$elementData['func']." );" );
else if ( isset($elementData['postFunc']) )
@ -323,11 +313,10 @@ switch( $_REQUEST['layout'] )
}
case 'json' :
{
header("Content-type: text/plain" );
$response = array( 'result'=>'Ok', strtolower(validJsStr($_REQUEST['entity'])) => $data );
$response = array( strtolower(validJsStr($_REQUEST['entity'])) => $data );
if ( isset($_REQUEST['loopback']) )
$response['loopback'] = validJsStr($_REQUEST['loopback']);
echo jsValue( $response );
ajaxResponse( $response );
break;
}
case 'text' :

View File

@ -1,32 +1,21 @@
<?php
if ( !canView( 'Stream' ) )
{
$view = "error";
return;
}
error_reporting( E_ALL );
define( "MSG_TIMEOUT", 2.0 );
define( "MSG_DATA_SIZE", 4+256 );
header("Content-type: text/plain" );
if ( !($_REQUEST['connkey'] && $_REQUEST['command']) )
{
error_log( "No connection key or command supplied" );
return;
ajaxError( "Unexpected received message type '$type'" );
}
if ( !($socket = socket_create( AF_UNIX, SOCK_DGRAM, 0 )) )
if ( !($socket = @socket_create( AF_UNIX, SOCK_DGRAM, 0 )) )
{
error_log( "socket_create() failed: ".socket_strerror(socket_last_error()) );
return;
ajaxError( "socket_create() failed: ".socket_strerror(socket_last_error()) );
}
$locSockFile = ZM_PATH_SOCKS.'/zms-'.sprintf("%06d",$_REQUEST['connkey']).'w.sock';
if ( !socket_bind( $socket, $locSockFile ) )
if ( !@socket_bind( $socket, $locSockFile ) )
{
error_log( "socket_bind() failed: ".socket_strerror(socket_last_error()) );
return;
ajaxError( "socket_bind() failed: ".socket_strerror(socket_last_error()) );
}
switch ( $_REQUEST['command'] )
@ -55,48 +44,40 @@ switch ( $_REQUEST['command'] )
$remSockFile = ZM_PATH_SOCKS.'/zms-'.sprintf("%06d",$_REQUEST['connkey']).'s.sock';
if ( !@socket_sendto( $socket, $msg, strlen($msg), 0, $remSockFile ) )
{
error_log( "socket_sendto() failed: ".socket_strerror(socket_last_error()) );
return;
ajaxError( "socket_sendto() failed: ".socket_strerror(socket_last_error()) );
}
$rSockets = array( $socket );
$wSockets = NULL;
$eSockets = NULL;
$numSockets = socket_select( $rSockets, $wSockets, $eSockets, MSG_TIMEOUT );
$numSockets = @socket_select( $rSockets, $wSockets, $eSockets, MSG_TIMEOUT );
if ( $numSockets === false)
{
error_log( "Timed out waiting for msg" );
return;
ajaxError( "Timed out waiting for msg" );
}
else if ( $numSockets > 0 )
{
if ( count($rSockets) != 1 )
{
error_log( "Bogus return from select" );
return;
}
ajaxError( "Bogus return from select" );
}
switch( $nbytes = socket_recvfrom( $socket, $msg, MSG_DATA_SIZE, 0, $rem_addr ) )
switch( $nbytes = @socket_recvfrom( $socket, $msg, MSG_DATA_SIZE, 0, $rem_addr ) )
{
case -1 :
{
error_log( "socket_recvfrom() failed: ".socket_strerror(socket_last_error()) );
return;
ajaxError( "socket_sendto() failed: ".socket_strerror(socket_last_error()) );
break;
}
case 0 :
{
error_log( "No data to read from socket" );
return;
ajaxError( "No data to read from socket" );
break;
}
default :
{
if ( $nbytes != MSG_DATA_SIZE )
{
error_log( "Got unexpected message size, got $nbytes, expected ".MSG_DATA_SIZE );
return;
}
ajaxError( "Got unexpected message size, got $nbytes, expected ".MSG_DATA_SIZE );
break;
}
}
@ -111,6 +92,7 @@ switch ( $data['type'] )
$data['rate'] /= 100;
$data['delay'] = sprintf( "%.2f", $data['delay'] );
$data['zoom'] = sprintf( "%.1f", $data['zoom']/100 );
ajaxResponse( array( 'status'=>$data ) );
break;
}
case MSG_DATA_EVENT :
@ -119,21 +101,23 @@ switch ( $data['type'] )
//$data['progress'] = sprintf( "%.2f", $data['progress'] );
$data['rate'] /= 100;
$data['zoom'] = sprintf( "%.1f", $data['zoom']/100 );
ajaxResponse( array( 'status'=>$data ) );
break;
}
default :
{
error_log( "Unexpected received message type '$type'" );
$response = array( 'result'=>'Error', 'message' => "Unexpected received message type '$type'" );
echo jsValue( $response );
return;
ajaxError( "Unexpected received message type '$type'" );
}
}
$response = array( 'result'=>'Ok', 'status' => $data );
echo jsValue( $response );
socket_close( $socket );
unlink( $locSockFile );
ajaxError( 'Unrecognised action or insufficient permissions' );
function ajaxCleanup()
{
global $socket, $locSockFile;
if ( !empty( $socket ) )
@socket_close( $socket );
if ( !empty( $locSockFile ) )
@unlink( $locSockFile );
}
?>

View File

@ -1,84 +1,45 @@
<?php
header("Content-type: text/plain" );
$response = array(
'result' => 'Ok',
'x' => 1
);
if ( empty($_REQUEST['mid']) )
{
$response['result'] = 'Error';
$response['message'] = 'No monitor id supplied';
ajaxError( 'No monitor id supplied' );
}
elseif ( !isset($_REQUEST['zid']) )
{
$response['result'] = 'Error';
$response['message'] = 'No zone id(s) supplied';
ajaxError( 'No zone id(s) supplied' );
}
if ( $response['result'] != 'Error' )
if ( canView( 'Monitors' ) )
{
$refreshEvent = false;
$refreshParent = false;
if ( canEdit( 'Monitors' ) )
switch ( $_REQUEST['action'] )
{
$response['result'] = 'Ok';
switch ( $_REQUEST['action'] )
case "zoneImage" :
{
default :
{
$response['result'] = 'Error';
break;
}
$wd = getcwd();
chdir( ZM_DIR_IMAGES );
$hiColor = "0x00ff00";
$command = getZmuCommand( " -m ".$_REQUEST['mid']." -z" );
if ( !isset($_REQUEST['zid']) )
$_REQUEST['zid'] = 0;
$command .= "'".$_REQUEST['zid'].' '.$hiColor.' '.$_REQUEST['coords']."'";
$status = exec( escapeshellcmd($command) );
chdir( $wd );
$monitor = dbFetchOne( "select * from Monitors where Id = '".dbEscape($_REQUEST['mid'])."'" );
$points = coordsToPoints( $_REQUEST['coords'] );
ajaxResponse( array(
'zoneImage' => ZM_DIR_IMAGES.'/Zones'.$monitor['Id'].'.jpg?'.time(),
'selfIntersecting' => isSelfIntersecting( $points ),
'area' => getPolyArea( $points )
) );
break;
}
}
if ( canView( 'Monitors' ) )
{
$response['result'] = 'Ok';
switch ( $_REQUEST['action'] )
{
case "zoneImage" :
{
$wd = getcwd();
chdir( ZM_DIR_IMAGES );
$hicolor = "0x00ff00";
$command = getZmuCommand( " -m ".$_REQUEST['mid']." -z" );
if ( !isset($_REQUEST['zid']) )
$_REQUEST['zid'] = 0;
$command .= "'".$_REQUEST['zid'].' '.$hicolor.' '.$_REQUEST['coords']."'";
$status = exec( escapeshellcmd($command) );
chdir( $wd );
//$response['zoneImage'] = ZM_DIR_IMAGES.'/Zones'.$_REQUEST['mid'].'.jpg?'.time();
$monitor = dbFetchOne( "select * from Monitors where Id = '".dbEscape($_REQUEST['mid'])."'" );
$response['zoneImage'] = ZM_DIR_IMAGES.'/Zones'.$monitor['Id'].'.jpg?'.time();
$points = coordsToPoints( $_REQUEST['coords'] );
$response['selfIntersecting'] = isSelfIntersecting( $points );
$response['area'] = getPolyArea( $points );
break;
}
default :
{
$response['result'] = 'Error';
break;
}
}
}
if ( $response['result'] == 'Ok' )
{
$response['refreshParent'] = $refreshParent;
$response['refreshEvent'] = $refreshEvent;
}
elseif ( !$response['message'] )
$response['message'] = 'Unrecognised action or insufficient permissions';
}
echo jsValue( $response );
ajaxError( 'Unrecognised action or insufficient permissions' );
?>

View File

@ -2134,6 +2134,41 @@ function jsValue( &$value )
}
}
define( 'HTTP_STATUS_OK', 200 );
define( 'HTTP_STATUS_BAD_REQUEST', 400 );
define( 'HTTP_STATUS_FORBIDDEN', 403 );
function ajaxError( $message, $code=HTTP_STATUS_OK )
{
error_log( $message );
if ( function_exists( 'debug_backtrace' ) )
error_log( var_export( debug_backtrace(), true ) );
if ( function_exists( 'ajaxCleanup' ) )
ajaxCleanup();
if ( $code == HTTP_STATUS_OK )
{
$response = array( 'result'=>'Error', 'message'=>$message );
header( "Content-type: text/plain" );
exit( jsValue( $response ) );
}
header( "HTTP/1.0 $code $message" );
exit();
}
function ajaxResponse( $result=false )
{
if ( function_exists( 'ajaxCleanup' ) )
ajaxCleanup();
$response = array( 'result'=>'Ok' );
if ( is_array( $result ) )
$response = array_merge( $response, $result );
elseif ( !empty($result) )
$response['message'] = $result;
//error_log( var_export( $response, true ) );
header( "Content-type: text/plain" );
exit( jsValue( $response ) );
}
function generateConnKey()
{
return( rand( 1, 999999 ) );