2002-10-03 19:39:36 +08:00
< ? php
2002-10-11 17:44:13 +08:00
//
2002-12-10 21:23:22 +08:00
// ZoneMinder web action file, $Date$, $Revision$
2006-01-17 19:32:47 +08:00
// Copyright (C) 2003, 2004, 2005, 2006 Philip Coombes
2002-10-11 17:44:13 +08:00
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
2006-01-23 23:57:07 +08:00
if ( ! empty ( $action ) )
2002-10-03 19:39:36 +08:00
{
2002-11-28 00:21:49 +08:00
//phpinfo( INFO_VARIABLES );
2006-01-23 23:57:07 +08:00
// General scope actions
2005-10-20 23:42:41 +08:00
if ( $action == " login " && $username && ( ZM_AUTH_TYPE == " remote " || $password ) )
2003-06-26 19:12:00 +08:00
{
userLogin ( $username , $password );
}
2003-06-30 04:53:55 +08:00
elseif ( $action == " logout " )
{
userLogout ();
$refresh_parent = true ;
$view = 'none' ;
}
elseif ( $action == " bandwidth " && $new_bandwidth )
{
$bandwidth = $new_bandwidth ;
setcookie ( " bandwidth " , $new_bandwidth , time () + 3600 * 24 * 30 * 12 * 10 );
$refresh_parent = true ;
$view = 'none' ;
}
2006-01-23 23:57:07 +08:00
// Event scope actions, view permissions only required
if ( canView ( 'Events' ) )
{
if ( $action == " addterm " )
{
for ( $i = $trms ; $i > $subaction ; $i -- )
{
$conjunction_name1 = " cnj " . ( $i + 1 );
$obracket_name1 = " obr " . ( $i + 1 );
$cbracket_name1 = " cbr " . ( $i + 1 );
$attr_name1 = " attr " . ( $i + 1 );
$op_name1 = " op " . ( $i + 1 );
$value_name1 = " val " . ( $i + 1 );
$conjunction_name2 = " cnj $i " ;
$obracket_name2 = " obr $i " ;
$cbracket_name2 = " cbr $i " ;
$attr_name2 = " attr $i " ;
$op_name2 = " op $i " ;
$value_name2 = " val $i " ;
$$conjunction_name1 = $$conjunction_name2 ;
$$obracket_name1 = $$obracket_name2 ;
$$cbracket_name1 = $$cbracket_name2 ;
$$attr_name1 = $$attr_name2 ;
$$op_name1 = $$op_name2 ;
$$value_name1 = $$value_name2 ;
}
$$conjunction_name2 = false ;
$$obracket_name2 = false ;
$$cbracket_name2 = false ;
$$attr_name2 = false ;
$$op_name2 = false ;
$$value_name2 = false ;
$trms ++ ;
}
elseif ( $action == " delterm " )
{
$trms -- ;
for ( $i = $subaction ; $i <= $trms ; $i ++ )
{
$conjunction_name1 = " cnj $i " ;
$obracket_name1 = " obr $i " ;
$cbracket_name1 = " cbr $i " ;
$attr_name1 = " attr $i " ;
$op_name1 = " op $i " ;
$value_name1 = " val $i " ;
$conjunction_name2 = " cnj " . ( $i + 1 );
$obracket_name2 = " obr " . ( $i + 1 );
$cbracket_name2 = " cbr " . ( $i + 1 );
$attr_name2 = " attr " . ( $i + 1 );
$op_name2 = " op " . ( $i + 1 );
$value_name2 = " val " . ( $i + 1 );
$$conjunction_name1 = $$conjunction_name2 ;
$$obracket_name1 = $$obracket_name2 ;
$$cbracket_name1 = $$cbracket_name2 ;
$$attr_name1 = $$attr_name2 ;
$$op_name1 = $$op_name2 ;
$$value_name1 = $$value_name2 ;
}
$$conjunction_name2 = false ;
$$obracket_name2 = false ;
$$cbracket_name2 = false ;
$$attr_name2 = false ;
$$op_name2 = false ;
$$value_name2 = false ;
}
}
// Event scope actions, edit permissions required
2003-06-30 04:53:55 +08:00
if ( canEdit ( 'Events' ) )
2002-10-03 19:39:36 +08:00
{
2003-06-27 20:10:10 +08:00
if ( $action == " rename " && $event_name && $eid )
2002-10-03 19:39:36 +08:00
{
2005-01-16 23:59:23 +08:00
simpleQuery ( " update Events set Name = ' $event_name ' where Id = ' $eid ' " );
2002-10-03 19:39:36 +08:00
}
2005-05-16 23:16:36 +08:00
else if ( $action == " eventdetail " )
2005-05-16 20:03:28 +08:00
{
2005-05-16 23:16:36 +08:00
if ( $eid )
{
simpleQuery ( " update Events set Cause = ' " . addslashes ( $new_event [ 'Cause' ]) . " ', Notes = ' " . addslashes ( $new_event [ 'Notes' ]) . " ' where Id = ' $eid ' " );
$refresh_parent = true ;
}
elseif ( $mark_eids || $mark_eid )
{
if ( ! $mark_eids && $mark_eid )
{
$mark_eids [] = $mark_eid ;
}
if ( $mark_eids )
{
foreach ( $mark_eids as $mark_eid )
{
simpleQuery ( " update Events set Cause = ' " . addslashes ( $new_event [ 'Cause' ]) . " ', Notes = ' " . addslashes ( $new_event [ 'Notes' ]) . " ' where Id = ' $mark_eid ' " );
}
$refresh_parent = true ;
}
}
2002-10-03 19:39:36 +08:00
}
2005-05-16 23:16:36 +08:00
elseif ( $action == " archive " || $action == " unarchive " )
2002-10-03 19:39:36 +08:00
{
2005-05-16 23:16:36 +08:00
$archive_val = ( $action == " archive " ) ? 1 : 0 ;
if ( $eid )
{
simpleQuery ( " update Events set Archived = $archive_val where Id = ' $eid ' " );
}
elseif ( $mark_eids || $mark_eid )
{
if ( ! $mark_eids && $mark_eid )
{
$mark_eids [] = $mark_eid ;
}
if ( $mark_eids )
{
foreach ( $mark_eids as $mark_eid )
{
simpleQuery ( " update Events set Archived = $archive_val where Id = ' $mark_eid ' " );
}
$refresh_parent = true ;
}
}
2002-10-03 19:39:36 +08:00
}
2003-06-27 20:10:10 +08:00
elseif ( $action == " filter " )
2002-10-03 19:39:36 +08:00
{
2003-06-27 20:10:10 +08:00
if ( $filter_name || $new_filter_name )
2002-10-03 19:39:36 +08:00
{
2003-06-27 20:10:10 +08:00
if ( $new_filter_name )
$filter_name = $new_filter_name ;
$filter_query = array ();
2004-01-08 18:09:35 +08:00
$filter_query [ 'trms' ] = $trms ;
2003-06-27 20:10:10 +08:00
for ( $i = 1 ; $i <= $trms ; $i ++ )
2002-10-03 19:39:36 +08:00
{
2003-06-27 20:10:10 +08:00
$conjunction_name = " cnj $i " ;
$obracket_name = " obr $i " ;
$cbracket_name = " cbr $i " ;
$attr_name = " attr $i " ;
$op_name = " op $i " ;
$value_name = " val $i " ;
if ( $i > 1 )
{
$filter_query [ $conjunction_name ] = $$conjunction_name ;
}
$filter_query [ $obracket_name ] = $$obracket_name ;
$filter_query [ $cbracket_name ] = $$cbracket_name ;
$filter_query [ $attr_name ] = $$attr_name ;
$filter_query [ $op_name ] = $$op_name ;
$filter_query [ $value_name ] = $$value_name ;
2002-10-03 19:39:36 +08:00
}
2003-06-27 20:10:10 +08:00
$filter_parms = array ();
while ( list ( $key , $value ) = each ( $filter_query ) )
{
$filter_parms [] = " $key = $value " ;
}
2004-04-21 17:20:28 +08:00
$filter_parms [] = " sort_field= $sort_field " ;
$filter_parms [] = " sort_asc= $sort_asc " ;
$filter_parms [] = " limit= $limit " ;
2003-06-27 20:10:10 +08:00
$filter_query_string = join ( '&' , $filter_parms );
2005-11-23 00:32:07 +08:00
simpleQuery ( " replace into Filters set Name = ' $filter_name ', Query = ' $filter_query_string ', AutoArchive = ' $auto_archive ', AutoVideo = ' $auto_video ', AutoUpload = ' $auto_upload ', AutoEmail = ' $auto_email ', AutoMessage = ' $auto_message ', AutoExecute = ' $auto_execute ', AutoExecuteCmd = ' " . addslashes ( $auto_execute_cmd ) . " ', AutoDelete = ' $auto_delete ' " );
2003-06-27 20:10:10 +08:00
$refresh_parent = true ;
}
}
elseif ( $action == " delete " )
{
if ( ! $mark_eids && $mark_eid )
{
$mark_eids [] = $mark_eid ;
}
if ( $mark_eids )
{
2002-11-07 20:23:54 +08:00
foreach ( $mark_eids as $mark_eid )
2002-10-03 19:39:36 +08:00
{
2002-11-22 20:12:16 +08:00
deleteEvent ( $mark_eid );
2002-10-03 19:39:36 +08:00
}
2005-05-16 23:16:36 +08:00
$refresh_parent = true ;
2002-11-07 20:23:54 +08:00
}
2003-06-27 20:10:10 +08:00
if ( $fid )
2002-11-07 20:23:54 +08:00
{
2005-01-16 23:59:23 +08:00
simpleQuery ( " delete from Filters where Name = ' $fid ' " );
2003-06-27 20:10:10 +08:00
//$refresh_parent = true;
2002-10-03 19:39:36 +08:00
}
}
}
2006-01-23 23:57:07 +08:00
// Monitor control actions, require a monitor id and control view permissions for that monitor
if ( ! empty ( $mid ) && canView ( 'Control' , $mid ) )
2002-10-03 19:39:36 +08:00
{
2006-01-23 23:57:07 +08:00
if ( $action == " control " )
2005-02-24 18:43:29 +08:00
{
$result = mysql_query ( " select * from Monitors as M inner join Controls as C on (M.ControlId = C.Id ) where M.Id = ' $mid ' " );
if ( ! $result )
die ( mysql_error () );
$monitor = mysql_fetch_assoc ( $result );
2006-01-15 21:09:30 +08:00
mysql_free_result ( $result );
2005-02-24 18:43:29 +08:00
$ctrl_command = $monitor [ 'Command' ];
2005-10-04 18:33:58 +08:00
if ( ! preg_match ( '/^\//' , $ctrl_command ) )
$ctrl_command = ZM_PATH_BIN . '/' . $ctrl_command ;
2005-02-24 18:43:29 +08:00
if ( $monitor [ 'ControlDevice' ] )
$ctrl_command .= " --device= " . $monitor [ 'ControlDevice' ];
if ( $monitor [ 'ControlAddress' ] )
$ctrl_command .= " --address= " . $monitor [ 'ControlAddress' ];
//$ctrl_command .= " --command=".$control;
if ( isset ( $x ) && isset ( $y ) )
{
if ( $control == " move_map " )
{
$x = deScale ( $x , $scale );
$y = deScale ( $y , $scale );
switch ( $monitor [ 'Orientation' ] )
{
case '0' :
case '180' :
2005-05-06 00:46:26 +08:00
case 'hori' :
case 'vert' :
2005-02-24 18:43:29 +08:00
$width = $monitor [ 'Width' ];
$height = $monitor [ 'Height' ];
break ;
case '90' :
case '270' :
$width = $monitor [ 'Height' ];
$height = $monitor [ 'Width' ];
break ;
}
switch ( $monitor [ 'Orientation' ] )
{
case '90' :
$temp_y = $y ;
$y = $height - $x ;
$x = $temp_y ;
break ;
case '180' :
$x = $width - $x ;
$y = $height - $y ;
break ;
case '270' :
$temp_x = $x ;
$x = $width - $y ;
$y = $temp_x ;
break ;
2005-05-06 00:46:26 +08:00
case 'hori' :
$x = $width - $x ;
break ;
case 'vert' :
$y = $height - $y ;
break ;
2005-02-24 18:43:29 +08:00
}
$ctrl_command .= " -xcoord $x -ycoord $y -width $width -height $height " ;
}
elseif ( $control == " move_pseudo_map " )
{
$x = deScale ( $x , $scale );
$y = deScale ( $y , $scale );
$half_width = $monitor [ 'Width' ] / 2 ;
$half_height = $monitor [ 'Height' ] / 2 ;
$x_factor = ( $x - $half_width ) / $half_width ;
$y_factor = ( $y - $half_height ) / $half_height ;
switch ( $monitor [ 'Orientation' ] )
{
case '90' :
$temp_y_factor = $y ;
$y_factor = - $x_factor ;
$x_factor = $temp_y_factor ;
break ;
case '180' :
$x_factor = - $x_factor ;
$y_factor = - $y_factor ;
break ;
case '270' :
$temp_x_factor = $x ;
$x_factor = - $y_factor ;
$y_factor = $tenp_x_factor ;
break ;
2005-05-06 00:46:26 +08:00
case 'hori' :
$x_factor = - $x_factor ;
break ;
case 'vert' :
$y_factor = - $y_factor ;
break ;
2005-02-24 18:43:29 +08:00
}
$turbo = 0.9 ; // Threshold for turbo speed
$blind = 0.1 ; // Threshold for blind spot
$pan_control = '' ;
$tilt_control = '' ;
if ( $x_factor > $blind )
{
$pan_control = 'right' ;
}
elseif ( $x_factor < - $blind )
{
$pan_control = 'left' ;
}
if ( $y_factor > $blind )
{
$tilt_control = 'down' ;
}
elseif ( $y_factor < - $blind )
{
$tilt_control = 'up' ;
}
$dirn = $tilt_control . $pan_control ;
if ( ! $dirn )
{
// No command, probably in blind spot in middle
$control = 'null' ;
}
else
{
$control = 'move_rel_' . $dirn ;
$x_factor = abs ( $x_factor );
$y_factor = abs ( $y_factor );
if ( $monitor [ 'HasPanSpeed' ] && $x_factor )
{
if ( $monitor [ 'HasTurboPan' ] )
{
if ( $x_factor >= $turbo )
{
$pan_speed = $monitor [ 'TurboPanSpeed' ];
}
else
{
$x_factor = $x_factor / $turbo ;
$pan_speed = intval ( round ( $monitor [ 'MinPanSpeed' ] + (( $monitor [ 'MaxPanSpeed' ] - $monitor [ 'MinPanSpeed' ]) * $x_factor )));
}
}
else
{
$pan_speed = intval ( round ( $monitor [ 'MinPanSpeed' ] + (( $monitor [ 'MaxPanSpeed' ] - $monitor [ 'MinPanSpeed' ]) * $x_factor )));
}
}
if ( $monitor [ 'HasTiltSpeed' ] && $y_factor )
{
if ( $monitor [ 'HasTurboTilt' ] )
{
if ( $y_factor >= $turbo )
{
$tilt_speed = $monitor [ 'TurboTiltSpeed' ];
}
else
{
$y_factor = $y_factor / $turbo ;
$tilt_speed = intval ( round ( $monitor [ 'MinTiltSpeed' ] + (( $monitor [ 'MaxTiltSpeed' ] - $monitor [ 'MinTiltSpeed' ]) * $y_factor )));
}
}
else
{
$tilt_speed = intval ( round ( $monitor [ 'MinTiltSpeed' ] + (( $monitor [ 'MaxTiltSpeed' ] - $monitor [ 'MinTiltSpeed' ]) * $y_factor )));
}
}
if ( preg_match ( '/(left|right)$/' , $dirn ) )
{
$pan_step = intval ( round ( $monitor [ 'MinPanStep' ] + (( $monitor [ 'MaxPanStep' ] - $monitor [ 'MinPanStep' ]) * $x_factor )));
$ctrl_command .= " --panstep= " . $pan_step . " --panspeed= " . $pan_speed ;
}
if ( preg_match ( '/^(up|down)/' , $dirn ) )
{
$tilt_step = intval ( round ( $monitor [ 'MinTiltStep' ] + (( $monitor [ 'MaxTiltStep' ] - $monitor [ 'MinTiltStep' ]) * $y_factor )));
$ctrl_command .= " --tiltstep= " . $tilt_step . " --tiltspeed= " . $tilt_speed ;
}
}
}
2005-11-10 20:04:37 +08:00
elseif ( $control == " move_con_map " )
{
$x = deScale ( $x , $scale );
$y = deScale ( $y , $scale );
$half_width = $monitor [ 'Width' ] / 2 ;
$half_height = $monitor [ 'Height' ] / 2 ;
$x_factor = ( $x - $half_width ) / $half_width ;
$y_factor = ( $y - $half_height ) / $half_height ;
switch ( $monitor [ 'Orientation' ] )
{
case '90' :
$temp_y_factor = $y ;
$y_factor = - $x_factor ;
$x_factor = $temp_y_factor ;
break ;
case '180' :
$x_factor = - $x_factor ;
$y_factor = - $y_factor ;
break ;
case '270' :
$temp_x_factor = $x ;
$x_factor = - $y_factor ;
$y_factor = $tenp_x_factor ;
break ;
case 'hori' :
$x_factor = - $x_factor ;
break ;
case 'vert' :
$y_factor = - $y_factor ;
break ;
}
2005-12-09 01:11:33 +08:00
$slow = 0.9 ; // Threshold for slow speed/timeouts
2005-11-10 20:04:37 +08:00
$turbo = 0.9 ; // Threshold for turbo speed
$blind = 0.1 ; // Threshold for blind spot
$pan_control = '' ;
$tilt_control = '' ;
if ( $x_factor > $blind )
{
$pan_control = 'right' ;
}
elseif ( $x_factor < - $blind )
{
$pan_control = 'left' ;
}
if ( $y_factor > $blind )
{
$tilt_control = 'down' ;
}
elseif ( $y_factor < - $blind )
{
$tilt_control = 'up' ;
}
$dirn = $tilt_control . $pan_control ;
if ( ! $dirn )
{
// No command, probably in blind spot in middle
$control = 'move_stop' ;
}
else
{
$control = 'move_con_' . $dirn ;
$x_factor = abs ( $x_factor );
$y_factor = abs ( $y_factor );
if ( $monitor [ 'HasPanSpeed' ] && $x_factor )
{
if ( $monitor [ 'HasTurboPan' ] )
{
if ( $x_factor >= $turbo )
{
$pan_speed = $monitor [ 'TurboPanSpeed' ];
}
else
{
$x_factor = $x_factor / $turbo ;
$pan_speed = intval ( round ( $monitor [ 'MinPanSpeed' ] + (( $monitor [ 'MaxPanSpeed' ] - $monitor [ 'MinPanSpeed' ]) * $x_factor )));
}
}
else
{
$pan_speed = intval ( round ( $monitor [ 'MinPanSpeed' ] + (( $monitor [ 'MaxPanSpeed' ] - $monitor [ 'MinPanSpeed' ]) * $x_factor )));
}
}
if ( $monitor [ 'HasTiltSpeed' ] && $y_factor )
{
if ( $monitor [ 'HasTurboTilt' ] )
{
if ( $y_factor >= $turbo )
{
$tilt_speed = $monitor [ 'TurboTiltSpeed' ];
}
else
{
$y_factor = $y_factor / $turbo ;
$tilt_speed = intval ( round ( $monitor [ 'MinTiltSpeed' ] + (( $monitor [ 'MaxTiltSpeed' ] - $monitor [ 'MinTiltSpeed' ]) * $y_factor )));
}
}
else
{
$tilt_speed = intval ( round ( $monitor [ 'MinTiltSpeed' ] + (( $monitor [ 'MaxTiltSpeed' ] - $monitor [ 'MinTiltSpeed' ]) * $y_factor )));
}
}
if ( preg_match ( '/(left|right)$/' , $dirn ) )
{
$ctrl_command .= " --panspeed= " . $pan_speed ;
}
if ( preg_match ( '/^(up|down)/' , $dirn ) )
{
$ctrl_command .= " --tiltspeed= " . $tilt_speed ;
}
2005-12-09 01:11:33 +08:00
if ( $monitor [ 'AutoStopTimeout' ] )
{
$slow_pan_speed = intval ( round ( $monitor [ 'MinPanSpeed' ] + (( $monitor [ 'MaxPanSpeed' ] - $monitor [ 'MinPanSpeed' ]) * $slow )));
$slow_tilt_speed = intval ( round ( $monitor [ 'MinTiltSpeed' ] + (( $monitor [ 'MaxTiltSpeed' ] - $monitor [ 'MinTiltSpeed' ]) * $slow )));
if ( ( ! isset ( $pan_speed ) || ( $pan_speed < $slow_pan_speed )) && ( ! isset ( $tilt_speed ) || ( $tilt_speed < $slow_tilt_speed )) )
{
$ctrl_command .= " --autostop= " . $monitor [ 'AutoStopTimeout' ];
}
}
2005-11-10 20:04:37 +08:00
}
}
2005-02-24 18:43:29 +08:00
else
{
2005-12-09 01:11:33 +08:00
$slow = 0.9 ; // Threshold for slow speed/timeouts
2005-02-24 18:43:29 +08:00
$turbo = 0.9 ; // Threshold for turbo speed
$long_y = 48 ;
$short_x = 32 ;
$short_y = 32 ;
if ( preg_match ( '/^([^_]+)_([^_]+)_([^_]+)$/' , $control , $matches ) )
{
$command = $matches [ 1 ];
$mode = $matches [ 2 ];
$dirn = $matches [ 3 ];
switch ( $command )
{
case 'focus' :
{
switch ( $dirn )
{
case 'near' :
{
$factor = ( $long_y - ( $y + 1 )) / $long_y ;
break ;
}
case 'far' :
{
$factor = ( $y + 1 ) / $long_y ;
break ;
}
}
if ( $monitor [ 'HasFocusSpeed' ] )
{
$speed = intval ( round ( $monitor [ 'MinFocusSpeed' ] + (( $monitor [ 'MaxFocusSpeed' ] - $monitor [ 'MinFocusSpeed' ]) * $factor )));
$ctrl_command .= " --speed= " . $speed ;
}
switch ( $mode )
{
case 'abs' :
case 'rel' :
{
2005-05-25 02:32:33 +08:00
$step = intval ( round ( $monitor [ 'MinFocusStep' ] + (( $monitor [ 'MaxFocusStep' ] - $monitor [ 'MinFocusStep' ]) * $factor )));
2005-02-24 18:43:29 +08:00
$ctrl_command .= " --step= " . $step ;
break ;
}
2005-05-23 01:44:15 +08:00
case 'con' :
{
if ( $monitor [ 'AutoStopTimeout' ] )
{
$slow_speed = intval ( round ( $monitor [ 'MinFocusSpeed' ] + (( $monitor [ 'MaxFocusSpeed' ] - $monitor [ 'MinFocusSpeed' ]) * $slow )));
if ( $speed < $slow_speed )
{
$ctrl_command .= " --autostop= " . $monitor [ 'AutoStopTimeout' ];
}
}
break ;
}
2005-02-24 18:43:29 +08:00
}
break ;
}
case 'zoom' :
{
switch ( $dirn )
{
case 'tele' :
{
$factor = ( $long_y - ( $y + 1 )) / $long_y ;
break ;
}
case 'wide' :
{
$factor = ( $y + 1 ) / $long_y ;
break ;
}
}
if ( $monitor [ 'HasZoomSpeed' ] )
{
$speed = intval ( round ( $monitor [ 'MinZoomSpeed' ] + (( $monitor [ 'MaxZoomSpeed' ] - $monitor [ 'MinZoomSpeed' ]) * $factor )));
$ctrl_command .= " --speed= " . $speed ;
}
switch ( $mode )
{
case 'abs' :
case 'rel' :
{
2005-05-25 02:32:33 +08:00
$step = intval ( round ( $monitor [ 'MinZoomStep' ] + (( $monitor [ 'MaxZoomStep' ] - $monitor [ 'MinZoomStep' ]) * $factor )));
2005-02-24 18:43:29 +08:00
$ctrl_command .= " --step= " . $step ;
break ;
}
2005-05-23 01:44:15 +08:00
case 'con' :
{
if ( $monitor [ 'AutoStopTimeout' ] )
{
$slow_speed = intval ( round ( $monitor [ 'MinZoomSpeed' ] + (( $monitor [ 'MaxZoomSpeed' ] - $monitor [ 'MinZoomSpeed' ]) * $slow )));
if ( $speed < $slow_speed )
{
$ctrl_command .= " --autostop= " . $monitor [ 'AutoStopTimeout' ];
}
}
break ;
}
2005-02-24 18:43:29 +08:00
}
break ;
}
case 'iris' :
{
switch ( $dirn )
{
case 'open' :
{
$factor = ( $long_y - ( $y + 1 )) / $long_y ;
break ;
}
case 'close' :
{
$factor = ( $y + 1 ) / $long_y ;
break ;
}
}
if ( $monitor [ 'HasIrisSpeed' ] )
{
$speed = intval ( round ( $monitor [ 'MinIrisSpeed' ] + (( $monitor [ 'MaxIrisSpeed' ] - $monitor [ 'MinIrisSpeed' ]) * $factor )));
$ctrl_command .= " --speed= " . $speed ;
}
switch ( $mode )
{
case 'abs' :
case 'rel' :
{
2005-05-25 02:32:33 +08:00
$step = intval ( round ( $monitor [ 'MinIrisStep' ] + (( $monitor [ 'MaxIrisStep' ] - $monitor [ 'MinIrisStep' ]) * $factor )));
2005-02-24 18:43:29 +08:00
$ctrl_command .= " --step= " . $step ;
break ;
}
}
break ;
}
case 'white' :
{
switch ( $dirn )
{
case 'in' :
{
$factor = ( $long_y - ( $y + 1 )) / $long_y ;
break ;
}
case 'out' :
{
$factor = ( $y + 1 ) / $long_y ;
break ;
}
}
if ( $monitor [ 'HasWhiteSpeed' ] )
{
$speed = intval ( round ( $monitor [ 'MinWhiteSpeed' ] + (( $monitor [ 'MaxWhiteSpeed' ] - $monitor [ 'MinWhiteSpeed' ]) * $factor )));
$ctrl_command .= " --speed= " . $speed ;
}
switch ( $mode )
{
case 'abs' :
case 'rel' :
{
2005-05-25 02:32:33 +08:00
$step = intval ( round ( $monitor [ 'MinWhiteStep' ] + (( $monitor [ 'MaxWhiteStep' ] - $monitor [ 'MinWhiteStep' ]) * $factor )));
2005-02-24 18:43:29 +08:00
$ctrl_command .= " --step= " . $step ;
break ;
}
}
break ;
}
case 'gain' :
{
switch ( $dirn )
{
case 'up' :
{
$factor = ( $long_y - ( $y + 1 )) / $long_y ;
break ;
}
case 'down' :
{
$factor = ( $y + 1 ) / $long_y ;
break ;
}
}
if ( $monitor [ 'HasGainSpeed' ] )
{
$speed = intval ( round ( $monitor [ 'MinGainSpeed' ] + (( $monitor [ 'MaxGainSpeed' ] - $monitor [ 'MinGainSpeed' ]) * $factor )));
$ctrl_command .= " --speed= " . $speed ;
}
switch ( $mode )
{
case 'abs' :
case 'rel' :
{
2005-05-25 02:32:33 +08:00
$step = intval ( round ( $monitor [ 'MinGainStep' ] + (( $monitor [ 'MaxGainStep' ] - $monitor [ 'MinGainStep' ]) * $factor )));
2005-02-24 18:43:29 +08:00
$ctrl_command .= " --step= " . $step ;
break ;
}
}
break ;
}
case 'move' :
{
$x_factor = 0 ;
$y_factor = 0 ;
if ( preg_match ( '/^up/' , $dirn ) )
{
$y_factor = ( $short_y - ( $y + 1 )) / $short_y ;
}
elseif ( preg_match ( '/^down/' , $dirn ) )
{
$y_factor = ( $y + 1 ) / $short_y ;
}
if ( preg_match ( '/left$/' , $dirn ) )
{
$x_factor = ( $short_x - ( $x + 1 )) / $short_x ;
}
elseif ( preg_match ( '/right$/' , $dirn ) )
{
$x_factor = ( $x + 1 ) / $short_x ;
}
if ( $monitor [ 'Orientation' ] != '0' )
{
$conversions = array (
'90' => array (
'up' => 'left' ,
'down' => 'right' ,
'left' => 'down' ,
'right' => 'up' ,
'upleft' => 'downleft' ,
'upright' => 'upleft' ,
'downleft' => 'downright' ,
'downright' => 'upright' ,
),
'180' => array (
'up' => 'down' ,
'down' => 'up' ,
'left' => 'right' ,
'right' => 'left' ,
'upleft' => 'downright' ,
'upright' => 'downleft' ,
'downleft' => 'upright' ,
'downright' => 'upleft' ,
),
'270' => array (
'up' => 'right' ,
'down' => 'left' ,
'left' => 'up' ,
'right' => 'down' ,
'upleft' => 'upright' ,
'upright' => 'downright' ,
'downleft' => 'upleft' ,
'downright' => 'downleft' ,
),
2005-05-06 00:46:26 +08:00
'hori' => array (
'up' => 'up' ,
'down' => 'down' ,
'left' => 'right' ,
'right' => 'left' ,
'upleft' => 'upright' ,
'upright' => 'upleft' ,
'downleft' => 'downright' ,
'downright' => 'downleft' ,
),
'vert' => array (
'up' => 'down' ,
'down' => 'up' ,
'left' => 'left' ,
'right' => 'right' ,
'upleft' => 'downleft' ,
'upright' => 'downright' ,
'downleft' => 'upleft' ,
'downright' => 'upright' ,
),
2005-02-24 18:43:29 +08:00
);
$new_dirn = $conversions [ $monitor [ 'Orientation' ]][ $dirn ];
$control = preg_replace ( " /_ $dirn\ $ / " , " _ $new_dirn " , $control );
$dirn = $new_dirn ;
}
if ( $monitor [ 'HasPanSpeed' ] && $x_factor )
{
if ( $monitor [ 'HasTurboPan' ] )
{
if ( $x_factor >= $turbo )
{
$pan_speed = $monitor [ 'TurboPanSpeed' ];
}
else
{
$x_factor = $x_factor / $turbo ;
$pan_speed = intval ( round ( $monitor [ 'MinPanSpeed' ] + (( $monitor [ 'MaxPanSpeed' ] - $monitor [ 'MinPanSpeed' ]) * $x_factor )));
}
}
else
{
$pan_speed = intval ( round ( $monitor [ 'MinPanSpeed' ] + (( $monitor [ 'MaxPanSpeed' ] - $monitor [ 'MinPanSpeed' ]) * $x_factor )));
}
$ctrl_command .= " --panspeed= " . $pan_speed ;
}
if ( $monitor [ 'HasTiltSpeed' ] && $y_factor )
{
if ( $monitor [ 'HasTurboTilt' ] )
{
if ( $y_factor >= $turbo )
{
$tilt_speed = $monitor [ 'TurboTiltSpeed' ];
}
else
{
$y_factor = $y_factor / $turbo ;
$tilt_speed = intval ( round ( $monitor [ 'MinTiltSpeed' ] + (( $monitor [ 'MaxTiltSpeed' ] - $monitor [ 'MinTiltSpeed' ]) * $y_factor )));
}
}
else
{
$tilt_speed = intval ( round ( $monitor [ 'MinTiltSpeed' ] + (( $monitor [ 'MaxTiltSpeed' ] - $monitor [ 'MinTiltSpeed' ]) * $y_factor )));
}
$ctrl_command .= " --tiltspeed= " . $tilt_speed ;
}
switch ( $mode )
{
case 'rel' :
case 'abs' :
{
if ( preg_match ( '/(left|right)$/' , $dirn ) )
{
$pan_step = intval ( round ( $monitor [ 'MinPanStep' ] + (( $monitor [ 'MaxPanStep' ] - $monitor [ 'MinPanStep' ]) * $x_factor )));
$ctrl_command .= " --panstep= " . $pan_step ;
}
if ( preg_match ( '/^(up|down)/' , $dirn ) )
{
$tilt_step = intval ( round ( $monitor [ 'MinTiltStep' ] + (( $monitor [ 'MaxTiltStep' ] - $monitor [ 'MinTiltStep' ]) * $y_factor )));
$ctrl_command .= " --tiltstep= " . $tilt_step ;
}
break ;
}
2005-05-23 01:44:15 +08:00
case 'con' :
{
if ( $monitor [ 'AutoStopTimeout' ] )
{
$slow_pan_speed = intval ( round ( $monitor [ 'MinPanSpeed' ] + (( $monitor [ 'MaxPanSpeed' ] - $monitor [ 'MinPanSpeed' ]) * $slow )));
$slow_tilt_speed = intval ( round ( $monitor [ 'MinTiltSpeed' ] + (( $monitor [ 'MaxTiltSpeed' ] - $monitor [ 'MinTiltSpeed' ]) * $slow )));
2005-12-09 01:11:33 +08:00
if ( ( ! isset ( $pan_speed ) || ( $pan_speed < $slow_pan_speed )) && ( ! isset ( $tilt_speed ) || ( $tilt_speed < $slow_tilt_speed )) )
2005-05-23 01:44:15 +08:00
{
$ctrl_command .= " --autostop= " . $monitor [ 'AutoStopTimeout' ];
}
}
break ;
}
2005-02-24 18:43:29 +08:00
}
}
}
}
}
}
else
{
if ( preg_match ( '/^preset_goto_(\d+)$/' , $control , $matches ) )
{
$control = 'preset_goto' ;
$ctrl_command .= " --preset " . $matches [ 1 ];
}
2005-05-22 23:39:14 +08:00
elseif ( $control == " preset_set " )
{
$ctrl_command .= " --preset " . $preset ;
2006-02-08 20:24:52 +08:00
$view = 'none' ;
2005-05-22 23:39:14 +08:00
}
2005-02-24 18:43:29 +08:00
elseif ( $control == " move_map " )
{
$ctrl_command .= " -xcoord $x -ycoord $y " ;
}
}
if ( $control != 'null' )
{
2005-10-17 05:11:44 +08:00
//if ( $monitor['Function'] == 'Modect' || $monitor['Function'] == 'Mocord' )
//{
//$zmu_command = getZmuCommand( " -m $mid -r" );
//$zmu_output = exec( escapeshellcmd( $zmu_command ) );
//}
2005-02-24 18:43:29 +08:00
$ctrl_command .= " --command= " . $control ;
//echo $ctrl_command;
$ctrl_output = exec ( escapeshellcmd ( $ctrl_command ) );
//echo $ctrl_output;
}
}
2006-01-23 23:57:07 +08:00
elseif ( $action == " settings " )
2005-11-07 20:10:49 +08:00
{
$zmu_command = getZmuCommand ( " -m $mid -B $new_brightness -C $new_contrast -H $new_hue -O $new_colour " );
$zmu_output = exec ( escapeshellcmd ( $zmu_command ) );
list ( $brightness , $contrast , $hue , $colour ) = split ( ' ' , $zmu_output );
$sql = " update Monitors set Brightness = ' $brightness ', Contrast = ' $contrast ', Hue = ' $hue ', Colour = ' $colour ' where Id = ' $mid ' " ;
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
}
2005-10-17 05:11:44 +08:00
}
2006-01-23 23:57:07 +08:00
// Control capability actions, require control edit permissions
if ( canEdit ( 'Control' ) )
2005-10-17 05:11:44 +08:00
{
2006-01-23 23:57:07 +08:00
if ( $action == " controlcap " )
2005-10-17 05:11:44 +08:00
{
2006-01-23 23:57:07 +08:00
if ( ! empty ( $cid ) )
2005-10-17 05:11:44 +08:00
{
$result = mysql_query ( " select * from Controls where Id = ' $cid ' " );
if ( ! $result )
die ( mysql_error () );
$control = mysql_fetch_assoc ( $result );
2006-01-15 21:09:30 +08:00
mysql_free_result ( $result );
2005-10-17 05:11:44 +08:00
}
else
{
$control = array ();
}
// Define a field type for anything that's not simple text equivalent
$types = array (
// Empty
);
$columns = getTableColumns ( 'Controls' );
foreach ( $columns as $name => $type )
{
if ( preg_match ( '/^(Can|Has)/' , $name ) )
{
$types [ $name ] = 'toggle' ;
}
}
$changes = getFormChanges ( $control , $new_control , $types , $columns );
if ( count ( $changes ) )
{
2006-01-23 23:57:07 +08:00
if ( ! empty ( $cid ) )
2005-10-17 05:11:44 +08:00
{
simpleQuery ( " update Controls set " . implode ( " , " , $changes ) . " where Id = ' $cid ' " );
$refresh_parent = true ;
}
else
{
$sql = " insert into Controls set " . implode ( " , " , $changes );
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
$cid = mysql_insert_id ();
}
$refresh_parent = true ;
}
}
elseif ( $action == " delete " )
{
if ( $mark_cids )
{
foreach ( $mark_cids as $mark_cid )
{
simpleQuery ( " delete from Controls where Id = ' $mark_cid ' " );
simpleQuery ( " update Monitors set Controllable = 0, ControlId = 0 where ControlId = ' $mark_cid ' " );
$refresh_parent = true ;
}
}
}
}
2006-01-23 23:57:07 +08:00
// Monitor edit actions, require a monitor id and edit permissions for that monitor
if ( ! empty ( $mid ) && canEdit ( 'Monitors' , $mid ) )
2005-10-17 05:11:44 +08:00
{
2006-01-23 23:57:07 +08:00
if ( $action == " function " )
2005-10-17 05:11:44 +08:00
{
$sql = " select * from Monitors where Id = ' $mid ' " ;
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
$monitor = mysql_fetch_assoc ( $result );
2006-01-15 21:09:30 +08:00
mysql_free_result ( $result );
2005-10-17 05:11:44 +08:00
$old_function = $monitor [ 'Function' ];
2005-12-23 00:47:19 +08:00
$old_enabled = $monitor [ 'Enabled' ];
if ( $new_function != $old_function || $new_enabled != $old_enabled )
2005-10-17 05:11:44 +08:00
{
2005-12-23 00:47:19 +08:00
simpleQuery ( " update Monitors set Function = ' $new_function ', Enabled = ' $new_enabled ' where Id = ' $mid ' " );
2005-10-17 05:11:44 +08:00
$monitor [ 'Function' ] = $new_function ;
2005-12-23 00:47:19 +08:00
$monitor [ 'Enabled' ] = $new_enabled ;
2005-10-17 05:11:44 +08:00
if ( $cookies ) session_write_close ();
2005-11-22 23:18:57 +08:00
if ( daemonCheck () )
{
2005-12-23 00:47:19 +08:00
zmcControl ( $monitor );
zmaControl ( $monitor , " reload " );
2005-11-22 23:18:57 +08:00
}
2005-10-17 05:11:44 +08:00
$refresh_parent = true ;
}
}
2006-02-08 20:24:52 +08:00
elseif ( $action == " zone " && isset ( $zid ) )
2005-10-17 05:11:44 +08:00
{
$result = mysql_query ( " select * from Monitors where Id = ' $mid ' " );
if ( ! $result )
die ( mysql_error () );
$monitor = mysql_fetch_assoc ( $result );
2006-01-15 21:09:30 +08:00
mysql_free_result ( $result );
2005-10-17 05:11:44 +08:00
if ( $zid > 0 )
{
$result = mysql_query ( " select * from Zones where MonitorId = ' $mid ' and Id = ' $zid ' " );
if ( ! $result )
die ( mysql_error () );
$zone = mysql_fetch_assoc ( $result );
2006-01-15 21:09:30 +08:00
mysql_free_result ( $result );
2005-10-17 05:11:44 +08:00
}
else
{
$zone = array ();
}
2005-11-30 01:51:06 +08:00
if ( false && $points )
{
$zone [ 'NumCoords' ] = count ( $points );
$zone [ 'Coords' ] = pointsToCoords ( $points );
$zone [ 'Area' ] = getPolyArea ( $points );
}
if ( $new_zone [ 'Units' ] == 'Percent' )
{
$new_zone [ 'MinAlarmPixels' ] = intval (( $new_zone [ 'MinAlarmPixels' ] * $new_zone [ 'Area' ]) / 100 );
$new_zone [ 'MaxAlarmPixels' ] = intval (( $new_zone [ 'MaxAlarmPixels' ] * $new_zone [ 'Area' ]) / 100 );
$new_zone [ 'MinFilterPixels' ] = intval (( $new_zone [ 'MinFilterPixels' ] * $new_zone [ 'Area' ]) / 100 );
$new_zone [ 'MaxFilterPixels' ] = intval (( $new_zone [ 'MaxFilterPixels' ] * $new_zone [ 'Area' ]) / 100 );
$new_zone [ 'MinBlobPixels' ] = intval (( $new_zone [ 'MinBlobPixels' ] * $new_zone [ 'Area' ]) / 100 );
$new_zone [ 'MaxBlobPixels' ] = intval (( $new_zone [ 'MaxBlobPixels' ] * $new_zone [ 'Area' ]) / 100 );
}
2005-10-17 05:11:44 +08:00
2005-12-05 20:19:46 +08:00
unset ( $new_zone [ 'Points' ] );
2005-10-17 05:11:44 +08:00
$types = array ();
$changes = getFormChanges ( $zone , $new_zone , $types );
if ( count ( $changes ) )
{
if ( $zid > 0 )
{
$sql = " update Zones set " . implode ( " , " , $changes ) . " where MonitorId = ' $mid ' and Id = ' $zid ' " ;
}
else
{
$sql = " insert into Zones set MonitorId = ' $mid ', " . implode ( " , " , $changes );
}
//echo "<html>$sql</html>";
simpleQuery ( $sql );
if ( $cookies ) session_write_close ();
2005-11-22 23:18:57 +08:00
if ( daemonCheck () )
{
2005-12-23 00:47:19 +08:00
zmaControl ( $mid , " restart " );
2005-11-22 23:18:57 +08:00
}
2005-10-17 05:11:44 +08:00
$refresh_parent = true ;
}
2005-12-02 00:35:02 +08:00
$view = 'none' ;
2005-10-17 05:11:44 +08:00
}
2006-01-23 23:57:07 +08:00
elseif ( $action == " sequence " && isset ( $smid ) )
2005-10-17 05:11:44 +08:00
{
2006-01-23 23:57:07 +08:00
$result = mysql_query ( " select * from Monitors where Id = ' $mid ' " );
if ( ! $result )
die ( mysql_error () );
$monitor = mysql_fetch_assoc ( $result );
mysql_free_result ( $result );
$result = mysql_query ( " select * from Monitors where Id = ' $smid ' " );
if ( ! $result )
die ( mysql_error () );
$smonitor = mysql_fetch_assoc ( $result );
mysql_free_result ( $result );
$sql = " update Monitors set Sequence = ' " . $smonitor [ 'Sequence' ] . " ' where Id = ' " . $monitor [ 'Id' ] . " ' " ;
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
$sql = " update Monitors set Sequence = ' " . $monitor [ 'Sequence' ] . " ' where Id = ' " . $smonitor [ 'Id' ] . " ' " ;
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
$refresh_parent = true ;
fixSequences ();
}
if ( $action == " delete " )
{
if ( $mark_zids )
{
$deleted_zid = 0 ;
foreach ( $mark_zids as $mark_zid )
{
$result = mysql_query ( " delete from Zones where MonitorId = ' $mid ' && Id = ' $mark_zid ' " );
if ( ! $result )
die ( mysql_error () );
$deleted_zid = 1 ;
}
if ( $deleted_zid )
{
if ( $cookies ) session_write_close ();
if ( daemonCheck () )
{
zmaControl ( $mid , " restart " );
}
$refresh_parent = true ;
}
}
}
}
// Monitor edit actions, monitor id derived, require edit permissions for that monitor
if ( canEdit ( 'Monitors' ) )
{
if ( $action == " monitor " )
{
if ( ! empty ( $mid ) )
2005-10-17 05:11:44 +08:00
{
$result = mysql_query ( " select * from Monitors where Id = ' $mid ' " );
if ( ! $result )
die ( mysql_error () );
$monitor = mysql_fetch_assoc ( $result );
2006-01-15 21:09:30 +08:00
mysql_free_result ( $result );
2005-10-17 05:11:44 +08:00
if ( ZM_OPT_X10 )
{
$result = mysql_query ( " select * from TriggersX10 where MonitorId = ' $mid ' " );
if ( ! $result )
die ( mysql_error () );
2005-11-20 20:15:25 +08:00
if ( ! ( $x10_monitor = mysql_fetch_assoc ( $result )) )
{
$x10_monitor = array ();
}
2006-01-15 21:09:30 +08:00
mysql_free_result ( $result );
2005-10-17 05:11:44 +08:00
}
}
else
{
$monitor = array ();
if ( ZM_OPT_X10 )
{
$x10_monitor = array ();
}
}
// Define a field type for anything that's not simple text equivalent
$types = array (
'Triggers' => 'set' ,
'Controllable' => 'toggle' ,
'TrackMotion' => 'toggle' ,
);
$columns = getTableColumns ( 'Monitors' );
$changes = getFormChanges ( $monitor , $new_monitor , $types , $columns );
if ( count ( $changes ) )
{
2006-01-23 23:57:07 +08:00
if ( ! empty ( $mid ) )
2005-10-17 05:11:44 +08:00
{
simpleQuery ( " update Monitors set " . implode ( " , " , $changes ) . " where Id = ' $mid ' " );
if ( $changes [ 'Name' ] )
{
2005-11-30 01:51:06 +08:00
exec ( escapeshellcmd ( " mv " . ZM_DIR_EVENTS . " / " . $monitor [ 'Name' ] . " " . ZM_DIR_EVENTS . " / " . $new_monitor [ 'Name' ] ) );
}
if ( $changes [ 'Width' ] || $changes [ 'Height' ] )
{
$new_w = $new_monitor [ 'Width' ];
$new_h = $new_monitor [ 'Height' ];
$new_a = $new_w * $new_h ;
$old_w = $monitor [ 'Width' ];
$old_h = $monitor [ 'Height' ];
$old_a = $old_w * $old_h ;
$result = mysql_query ( " select * from Zones where MonitorId = ' $mid ' " );
if ( ! $result )
die ( mysql_error () );
$zones = array ();
while ( $zone = mysql_fetch_assoc ( $result ) )
2005-11-30 00:32:06 +08:00
{
2005-11-30 01:51:06 +08:00
$zones [] = $zone ;
2005-11-30 00:32:06 +08:00
}
2006-01-15 21:09:30 +08:00
mysql_free_result ( $result );
2005-11-30 01:51:06 +08:00
foreach ( $zones as $zone )
2005-11-30 00:32:06 +08:00
{
2005-11-30 01:51:06 +08:00
$new_zone = $zone ;
$points = coordsToPoints ( $zone [ 'Coords' ] );
for ( $i = 0 ; $i < count ( $points ); $i ++ )
{
$points [ $i ][ 'x' ] = intval ( round ((( $points [ $i ][ 'x' ] * $new_w ) / $old_w )));
$points [ $i ][ 'y' ] = intval ( round ((( $points [ $i ][ 'y' ] * $new_h ) / $old_h )));
}
$new_zone [ 'Coords' ] = pointsToCoords ( $points );
$new_zone [ 'Area' ] = intval ( round (( $zone [ 'Area' ] * $new_a ) / $old_a ));
$new_zone [ 'MinAlarmPixels' ] = intval ( round (( $new_zone [ 'MinAlarmPixels' ] * $new_a ) / $old_a ));
$new_zone [ 'MaxAlarmPixels' ] = intval ( round (( $new_zone [ 'MaxAlarmPixels' ] * $new_a ) / $old_a ));
$new_zone [ 'MinFilterPixels' ] = intval ( round (( $new_zone [ 'MinFilterPixels' ] * $new_a ) / $old_a ));
$new_zone [ 'MaxFilterPixels' ] = intval ( round (( $new_zone [ 'MaxFilterPixels' ] * $new_a ) / $old_a ));
$new_zone [ 'MinBlobPixels' ] = intval ( round (( $new_zone [ 'MinBlobPixels' ] * $new_a ) / $old_a ));
$new_zone [ 'MaxBlobPixels' ] = intval ( round (( $new_zone [ 'MaxBlobPixels' ] * $new_a ) / $old_a ));
$changes = getFormChanges ( $zone , $new_zone , $types );
if ( count ( $changes ) )
{
$sql = " update Zones set " . implode ( " , " , $changes ) . " where MonitorId = ' $mid ' and Id = ' " . $zone [ 'Id' ] . " ' " ;
//echo "<html>$sql</html>";
simpleQuery ( $sql );
}
2005-11-30 00:32:06 +08:00
}
2005-10-17 05:11:44 +08:00
}
}
elseif ( ! $user [ 'MonitorIds' ] )
{
2005-10-17 18:12:53 +08:00
$sql = " select max(Sequence) as MaxSequence from Monitors " ;
2005-11-03 18:47:43 +08:00
$result = mysql_query ( $sql );
2005-10-17 18:12:53 +08:00
if ( ! $result )
die ( mysql_error () );
$row = mysql_fetch_assoc ( $result );
2006-01-15 21:09:30 +08:00
mysql_free_result ( $result );
2005-10-17 18:12:53 +08:00
$changes [] = " Sequence = " . ( $row [ 'MaxSequence' ] + 1 );
2005-10-17 05:11:44 +08:00
$sql = " insert into Monitors set " . implode ( " , " , $changes );
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
$mid = mysql_insert_id ();
2006-04-07 19:49:57 +08:00
$zone_area = $new_monitor [ 'Width' ] * $new_monitor [ 'Height' ];
$sql = " insert into Zones set MonitorId = $mid , Name = 'All', Type = 'Active', Units = 'Percent', NumCoords = 4, Coords = ' " . sprintf ( " %d,%d %d,%d %d,%d %d,%d " , 0 , 0 , $new_monitor [ 'Width' ] - 1 , 0 , $new_monitor [ 'Width' ] - 1 , $new_monitor [ 'Height' ] - 1 , 0 , $new_monitor [ 'Height' ] - 1 ) . " ', Area = " . $zone_area . " , AlarmRGB = 0xff0000, CheckMethod = 'Blobs', MinPixelThreshold = 25, MinAlarmPixels = " . intval (( $zone_area * 3 ) / 100 ) . " , MaxAlarmPixels = " . intval (( $zone_area * 75 ) / 100 ) . " , FilterX = 3, FilterY = 3, MinFilterPixels = " . intval (( $zone_area * 3 ) / 100 ) . " , MaxFilterPixels = " . intval (( $zone_area * 75 ) / 100 ) . " , MinBlobPixels = " . intval (( $zone_area * 2 ) / 100 ) . " , MinBlobs = 1 " ;
2005-10-17 05:11:44 +08:00
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
//$view = 'none';
mkdir ( ZM_DIR_EVENTS . " / " . $mid , 0755 );
chdir ( ZM_DIR_EVENTS );
symlink ( $mid , $new_monitor [ 'Name' ] );
chdir ( " .. " );
}
$restart = true ;
}
if ( ZM_OPT_X10 )
{
$x10_changes = getFormChanges ( $x10_monitor , $new_x10_monitor );
if ( count ( $x10_changes ) )
{
if ( $x10_monitor && $new_x10_monitor )
{
$sql = " update TriggersX10 set " . implode ( " , " , $x10_changes ) . " where MonitorId = ' $mid ' " ;
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
}
elseif ( ! $user [ 'MonitorIds' ] )
{
if ( ! $x10_monitor )
{
$sql = " insert into TriggersX10 set MonitorId = ' $mid ', " . implode ( " , " , $x10_changes );
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
}
else
{
$sql = " delete from TriggersX10 where MonitorId = ' $mid ' " ;
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
}
}
$restart = true ;
}
}
if ( $restart )
{
$result = mysql_query ( " select * from Monitors where Id = ' $mid ' " );
if ( ! $result )
die ( mysql_error () );
$monitor = mysql_fetch_assoc ( $result );
2006-01-15 21:09:30 +08:00
mysql_free_result ( $result );
2005-10-17 05:11:44 +08:00
fixDevices ();
if ( $cookies ) session_write_close ();
2005-11-22 23:18:57 +08:00
if ( daemonCheck () )
{
2005-12-23 00:47:19 +08:00
zmcControl ( $monitor , " restart " );
zmaControl ( $monitor , " restart " );
2005-11-22 23:18:57 +08:00
}
2005-10-17 05:11:44 +08:00
//daemonControl( 'restart', 'zmwatch.pl' );
$refresh_parent = true ;
}
}
2006-01-23 23:57:07 +08:00
if ( $action == " delete " )
2005-10-17 18:12:53 +08:00
{
2004-01-08 18:09:35 +08:00
if ( $mark_mids && ! $user [ 'MonitorIds' ] )
2002-11-29 19:21:37 +08:00
{
2003-06-27 20:10:10 +08:00
foreach ( $mark_mids as $mark_mid )
{
2006-01-23 23:57:07 +08:00
if ( canEdit ( 'Monitors' , $mark_mid ) )
2003-06-27 20:10:10 +08:00
{
2006-01-23 23:57:07 +08:00
zmaControl ( $monitor , " stop " );
zmcControl ( $monitor , " stop " );
2003-06-27 20:10:10 +08:00
2006-01-23 23:57:07 +08:00
$sql = " select * from Monitors where Id = ' $mark_mid ' " ;
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
if ( ! ( $monitor = mysql_fetch_assoc ( $result )) )
{
continue ;
}
mysql_free_result ( $result );
2003-06-27 20:10:10 +08:00
2006-01-23 23:57:07 +08:00
$sql = " select Id from Events where MonitorId = ' $mark_mid ' " ;
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
2003-06-27 20:10:10 +08:00
2006-01-23 23:57:07 +08:00
$mark_eids = array ();
while ( $row = mysql_fetch_assoc ( $result ) )
{
$mark_eids [] = $row [ 'Id' ];
}
mysql_free_result ( $result );
foreach ( $mark_eids as $mark_eid )
{
deleteEvent ( $mark_eid );
}
unlink ( ZM_DIR_EVENTS . " / " . $monitor [ 'Name' ] );
system ( " rm -rf " . ZM_DIR_EVENTS . " / " . $monitor [ 'Id' ] );
$result = mysql_query ( " delete from Zones where MonitorId = ' $mark_mid ' " );
if ( ! $result )
die ( mysql_error () );
if ( ZM_OPT_X10 )
{
$result = mysql_query ( " delete from TriggersX10 where MonitorId = ' $mark_mid ' " );
if ( ! $result )
die ( mysql_error () );
}
$result = mysql_query ( " delete from Monitors where Id = ' $mark_mid ' " );
2003-10-28 01:06:21 +08:00
if ( ! $result )
die ( mysql_error () );
2006-01-23 23:57:07 +08:00
fixSequences ();
}
2003-06-27 20:10:10 +08:00
}
2002-11-29 19:21:37 +08:00
}
2002-11-28 00:21:49 +08:00
}
}
2006-01-23 23:57:07 +08:00
// System view actions
2004-10-13 17:49:11 +08:00
if ( canView ( 'System' ) )
{
2006-01-24 00:25:01 +08:00
if ( $action == " cgroup " )
2004-10-13 17:49:11 +08:00
{
2005-11-21 23:30:07 +08:00
if ( ! empty ( $gid ) )
2004-10-13 17:49:11 +08:00
{
2005-11-21 23:30:07 +08:00
setcookie ( " cgroup " , $gid , time () + 3600 * 24 * 30 * 12 * 10 );
2004-10-13 17:49:11 +08:00
}
2004-12-28 23:48:45 +08:00
else
{
setcookie ( " cgroup " , " " , time () - 3600 * 24 * 2 );
}
$refresh_parent = true ;
2004-10-13 17:49:11 +08:00
}
}
2006-01-23 23:57:07 +08:00
// System edit actions
2003-06-30 04:53:55 +08:00
if ( canEdit ( 'System' ) )
{
2004-01-27 18:17:23 +08:00
if ( $action == " version " && isset ( $option ) )
{
switch ( $option )
{
case 'go' :
{
// Ignore this, the caller will open the page itself
break ;
}
case 'ignore' :
{
$sql = " update Config set Value = ' " . ZM_DYN_LAST_VERSION . " ' where Name = 'ZM_DYN_CURR_VERSION' " ;
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
break ;
}
case 'hour' :
case 'day' :
case 'week' :
{
$next_reminder = time ();
if ( $option == 'hour' )
{
$next_reminder += 60 * 60 ;
}
elseif ( $option == 'day' )
{
$next_reminder += 24 * 60 * 60 ;
}
elseif ( $option == 'week' )
{
$next_reminder += 7 * 24 * 60 * 60 ;
}
$sql = " update Config set Value = ' " . $next_reminder . " ' where Name = 'ZM_DYN_NEXT_REMINDER' " ;
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
break ;
}
case 'never' :
{
$sql = " update Config set Value = '0' where Name = 'ZM_CHECK_FOR_UPDATES' " ;
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
break ;
}
}
}
2005-10-04 18:58:55 +08:00
if ( $action == " donate " && isset ( $option ) )
{
switch ( $option )
{
case 'go' :
{
// Ignore this, the caller will open the page itself
break ;
}
case 'hour' :
case 'day' :
case 'week' :
case 'month' :
{
$next_reminder = time ();
if ( $option == 'hour' )
{
$next_reminder += 60 * 60 ;
}
elseif ( $option == 'day' )
{
$next_reminder += 24 * 60 * 60 ;
}
elseif ( $option == 'week' )
{
$next_reminder += 7 * 24 * 60 * 60 ;
}
elseif ( $option == 'month' )
{
$next_reminder += 30 * 24 * 60 * 60 ;
}
$sql = " update Config set Value = ' " . $next_reminder . " ' where Name = 'ZM_DYN_DONATE_REMINDER_TIME' " ;
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
break ;
}
case 'never' :
case 'already' :
{
$sql = " update Config set Value = '0' where Name = 'ZM_DYN_SHOW_DONATE_REMINDER' " ;
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
break ;
}
}
}
2003-07-04 04:39:02 +08:00
if ( $action == " options " && isset ( $tab ) )
{
$config_cat = $config_cats [ $tab ];
$changed = false ;
foreach ( $config_cat as $name => $value )
{
2004-01-08 18:09:35 +08:00
if ( $value [ 'Type' ] == " boolean " && ! $new_config [ $name ] )
2003-07-04 04:39:02 +08:00
{
$new_config [ $name ] = 0 ;
}
else
{
$new_config [ $name ] = preg_replace ( " / \r \n / " , " \n " , stripslashes ( $new_config [ $name ] ) );
}
2004-01-08 18:09:35 +08:00
if ( $value [ 'Value' ] != $new_config [ $name ] )
2003-07-04 04:39:02 +08:00
{
$sql = " update Config set Value = ' " . $new_config [ $name ] . " ' where Name = ' " . $name . " ' " ;
2003-07-04 21:00:03 +08:00
//echo $sql;
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
2003-07-04 04:39:02 +08:00
$changed = true ;
}
}
if ( $changed )
{
switch ( $tab )
{
case " system " :
2004-03-15 07:27:36 +08:00
case " config " :
2006-01-23 23:57:07 +08:00
case " paths " :
$restart = true ;
break ;
case " web " :
case " tools " :
break ;
case " debug " :
2003-07-04 04:39:02 +08:00
case " network " :
case " mail " :
case " ftp " :
$restart = true ;
break ;
case " highband " :
case " medband " :
case " lowband " :
case " phoneband " :
break ;
}
}
2003-07-07 21:26:50 +08:00
loadConfig ();
2003-07-04 04:39:02 +08:00
}
2006-01-23 23:57:07 +08:00
elseif ( $action == " user " )
2003-06-30 04:53:55 +08:00
{
2006-01-23 23:57:07 +08:00
if ( ! empty ( $uid ) )
2003-06-30 04:53:55 +08:00
{
$result = mysql_query ( " select * from Users where Id = ' $uid ' " );
if ( ! $result )
die ( mysql_error () );
2006-01-05 19:11:38 +08:00
$db_user = mysql_fetch_assoc ( $result );
2006-01-15 21:09:30 +08:00
mysql_free_result ( $result );
2003-06-30 04:53:55 +08:00
}
else
{
$zone = array ();
}
2006-01-05 19:11:38 +08:00
$types = array ();
$changes = getFormChanges ( $db_user , $new_user , $types );
2003-06-30 04:53:55 +08:00
2006-01-16 01:35:53 +08:00
if ( $new_user [ 'Password' ] )
$changes [ 'Password' ] = " Password = password(' " . $new_user [ 'Password' ] . " ') " ;
else
unset ( $changes [ 'Password' ] );
2003-06-30 04:53:55 +08:00
if ( count ( $changes ) )
{
if ( $uid > 0 )
{
$sql = " update Users set " . implode ( " , " , $changes ) . " where Id = ' $uid ' " ;
}
else
{
$sql = " insert into Users set " . implode ( " , " , $changes );
}
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
$refresh_parent = true ;
2006-01-05 19:11:38 +08:00
if ( $db_user [ 'Username' ] == $user [ 'Username' ] )
2003-06-30 04:53:55 +08:00
{
2006-01-05 19:11:38 +08:00
userLogin ( $db_user [ 'Username' ], $db_user [ 'Password' ] );
2003-06-30 04:53:55 +08:00
}
}
2006-01-16 01:35:53 +08:00
$view = 'none' ;
2003-06-30 04:53:55 +08:00
}
2003-12-08 01:13:19 +08:00
elseif ( $action == " state " )
{
if ( $run_state )
{
2004-12-29 02:20:11 +08:00
if ( $cookies ) session_write_close ();
2003-12-08 01:13:19 +08:00
packageControl ( $run_state );
$refresh_parent = true ;
}
}
elseif ( $action == " save " )
{
if ( $run_state || $new_state )
{
2006-02-08 20:24:52 +08:00
$sql = " select Id,Function,Enabled from Monitors order by Id " ;
2003-12-08 01:13:19 +08:00
$result = mysql_query ( $sql );
if ( ! $result )
die ( mysql_error () );
$definitions = array ();
while ( $monitor = mysql_fetch_assoc ( $result ) )
{
2005-12-23 00:47:19 +08:00
$definitions [] = $monitor [ 'Id' ] . " : " . $monitor [ 'Function' ] . " : " . $monitor [ 'Enabled' ];
2003-12-08 01:13:19 +08:00
}
2006-01-15 21:09:30 +08:00
mysql_free_result ( $result );
2003-12-08 01:13:19 +08:00
$definition = join ( ',' , $definitions );
if ( $new_state )
$run_state = $new_state ;
2005-01-16 23:59:23 +08:00
simpleQuery ( " replace into States set Name = ' $run_state ', Definition = ' $definition ' " );
2003-12-08 01:13:19 +08:00
}
}
2006-01-24 00:25:01 +08:00
elseif ( $action == " group " )
2004-10-13 17:49:11 +08:00
{
2006-01-24 18:06:34 +08:00
if ( $gid )
2004-10-13 17:49:11 +08:00
{
2006-01-24 00:25:01 +08:00
simpleQuery ( " update Groups set Name = ' " . addslashes ( $new_group [ 'Name' ]) . " ', MonitorIds = ' " . addslashes ( $new_group [ 'MonitorIds' ]) . " ' where Id = ' $gid ' " );
2004-10-13 17:49:11 +08:00
}
2006-01-24 00:25:01 +08:00
else
2004-10-13 17:49:11 +08:00
{
2006-01-24 00:25:01 +08:00
simpleQuery ( " insert into Groups set Name = ' " . addslashes ( $new_group [ 'Name' ]) . " ', MonitorIds = ' " . addslashes ( $new_group [ 'MonitorIds' ]) . " ' " );
2004-10-13 17:49:11 +08:00
}
$refresh_parent = true ;
2006-01-24 00:25:01 +08:00
$view = 'none' ;
2004-10-13 17:49:11 +08:00
}
2003-06-30 04:53:55 +08:00
elseif ( $action == " delete " )
{
2003-12-08 01:13:19 +08:00
if ( $run_state )
{
2005-01-16 23:59:23 +08:00
simpleQuery ( " delete from States where Name = ' $run_state ' " );
2003-12-08 01:13:19 +08:00
}
2003-06-30 04:53:55 +08:00
if ( $mark_uids )
{
foreach ( $mark_uids as $mark_uid )
{
2005-01-16 23:59:23 +08:00
simpleQuery ( " delete from Users where Id = ' $mark_uid ' " );
2003-06-30 04:53:55 +08:00
}
2006-01-05 19:11:38 +08:00
if ( $mark_uid == $user [ 'Id' ] )
2003-06-30 04:53:55 +08:00
{
userLogout ();
}
}
2005-11-21 23:30:07 +08:00
if ( ! empty ( $gid ) )
2004-10-13 17:49:11 +08:00
{
2005-11-21 23:30:07 +08:00
simpleQuery ( " delete from Groups where Id = ' $gid ' " );
if ( $gid == $cgroup )
2004-10-13 17:49:11 +08:00
{
2005-11-21 23:30:07 +08:00
unset ( $cgroup );
setcookie ( " cgroup " , " " , time () - 3600 * 24 * 2 );
$refresh_parent = true ;
2004-10-13 17:49:11 +08:00
}
}
2003-06-30 04:53:55 +08:00
}
}
2003-06-27 20:10:10 +08:00
if ( $action == " learn " )
2003-06-25 17:47:09 +08:00
{
2003-06-27 20:10:10 +08:00
if ( ! $mark_eids && $mark_eid )
{
$mark_eids [] = $mark_eid ;
$refresh_parent = true ;
}
if ( $mark_eids )
{
foreach ( $mark_eids as $mark_eid )
{
$result = mysql_query ( " update Events set LearnState = ' $learn_state ' where Id = ' $mark_eid ' " );
if ( ! $result )
die ( mysql_error () );
}
}
2003-06-25 17:47:09 +08:00
}
2002-10-05 05:40:55 +08:00
elseif ( $action == " reset " )
{
2006-03-28 06:01:33 +08:00
$_SESSION [ 'event_reset_time' ] = strftime ( STRF_FMT_DATETIME_DB );
2005-01-17 00:08:35 +08:00
setcookie ( " event_reset_time " , $_SESSION [ 'event_reset_time' ], time () + 3600 * 24 * 30 * 12 * 10 );
2004-12-29 02:20:11 +08:00
if ( $cookies ) session_write_close ();
2002-10-05 05:40:55 +08:00
}
2002-10-03 19:39:36 +08:00
}
?>