2013-03-17 07:45:21 +08:00
< ? php
//
// ZoneMinder web action file, $Date$, $Revision$
// Copyright (C) 2001-2008 Philip Coombes
//
// 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.
//
2015-08-30 03:13:15 +08:00
// PP - POST request handler for PHP which does not need extensions
// credit: http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/
2015-09-25 03:44:35 +08:00
2016-07-23 04:03:39 +08:00
function do_post_request ( $url , $data , $optional_headers = null ) {
2015-08-30 03:13:15 +08:00
$params = array ( 'http' => array (
2016-07-23 04:03:39 +08:00
'method' => 'POST' ,
'content' => $data
));
2015-08-30 03:13:15 +08:00
if ( $optional_headers !== null ) {
$params [ 'http' ][ 'header' ] = $optional_headers ;
}
$ctx = stream_context_create ( $params );
$fp = @ fopen ( $url , 'rb' , false , $ctx );
if ( ! $fp ) {
throw new Exception ( " Problem with $url , $php_errormsg " );
}
$response = @ stream_get_contents ( $fp );
if ( $response === false ) {
throw new Exception ( " Problem reading data from $url , $php_errormsg " );
}
return $response ;
}
2016-07-23 04:03:39 +08:00
function getAffectedIds ( $name ) {
2016-10-04 09:58:57 +08:00
$names = $name . 's' ;
2016-07-23 04:03:39 +08:00
$ids = array ();
if ( isset ( $_REQUEST [ $names ]) || isset ( $_REQUEST [ $name ]) ) {
if ( isset ( $_REQUEST [ $names ]) )
$ids = validInt ( $_REQUEST [ $names ]);
else if ( isset ( $_REQUEST [ $name ]) )
$ids [] = validInt ( $_REQUEST [ $name ]);
}
return ( $ids );
2013-03-17 07:45:21 +08:00
}
2016-09-28 21:08:36 +08:00
if ( ZM_OPT_USE_AUTH && ZM_AUTH_HASH_LOGINS ) {
if ( empty ( $user ) && ! empty ( $_REQUEST [ 'auth' ]) ) {
if ( $authUser = getAuthUser ( $_REQUEST [ 'auth' ] ) ) {
userLogin ( $authUser [ 'Username' ], $authUser [ 'Password' ], true );
}
2016-10-03 02:13:54 +08:00
} else if ( ! empty ( $user ) ) {
2016-09-28 21:08:36 +08:00
// generate it once here, while session is open. Value will be cached in session and return when called later on
generateAuthHash ( ZM_AUTH_HASH_IPS );
2016-07-23 04:03:39 +08:00
}
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
if ( ! empty ( $action ) ) {
2016-10-04 09:58:57 +08:00
if ( $action == 'login' && isset ( $_REQUEST [ 'username' ]) && ( ZM_AUTH_TYPE == 'remote' || isset ( $_REQUEST [ 'password' ]) ) ) {
2016-07-23 04:03:39 +08:00
// if true, a popup will display after login
// PP - lets validate reCaptcha if it exists
2016-10-04 09:58:57 +08:00
if ( defined ( 'ZM_OPT_USE_GOOG_RECAPTCHA' )
2016-07-23 04:03:39 +08:00
&& defined ( 'ZM_OPT_GOOG_RECAPTCHA_SECRETKEY' )
&& defined ( 'ZM_OPT_GOOG_RECAPTCHA_SITEKEY' )
&& ZM_OPT_USE_GOOG_RECAPTCHA && ZM_OPT_GOOG_RECAPTCHA_SECRETKEY
&& ZM_OPT_GOOG_RECAPTCHA_SITEKEY )
2015-09-25 23:11:15 +08:00
{
2016-07-23 04:03:39 +08:00
$url = 'https://www.google.com/recaptcha/api/siteverify' ;
$fields = array (
'secret' => ZM_OPT_GOOG_RECAPTCHA_SECRETKEY ,
'response' => $_REQUEST [ 'g-recaptcha-response' ],
'remoteip' => $_SERVER [ 'REMOTE_ADDR' ]
);
$res = do_post_request ( $url , http_build_query ( $fields ));
$responseData = json_decode ( $res , true );
// PP - credit: https://github.com/google/recaptcha/blob/master/src/ReCaptcha/Response.php
// if recaptcha resulted in error, we might have to deny login
if ( isset ( $responseData [ 'success' ]) && $responseData [ 'success' ] == false ) {
// PP - before we deny auth, let's make sure the error was not 'invalid secret'
// because that means the user did not configure the secret key correctly
// in this case, we prefer to let him login in and display a message to correct
// the key. Unfortunately, there is no way to check for invalid site key in code
// as it produces the same error as when you don't answer a recaptcha
if ( isset ( $responseData [ 'error-codes' ]) && is_array ( $responseData [ 'error-codes' ])) {
if ( ! in_array ( 'invalid-input-secret' , $responseData [ 'error-codes' ])) {
2016-10-04 09:58:57 +08:00
Error ( 'reCaptcha authentication failed' );
2016-07-23 04:03:39 +08:00
userLogout ();
$view = 'login' ;
$refreshParent = true ;
} else {
//Let them login but show an error
echo '<script type="text/javascript">alert("' . translate ( 'RecaptchaWarning' ) . '"); </script>' ;
Error ( " Invalid recaptcha secret detected " );
}
}
} // end if success==false
} // end if using reCaptcha
$username = validStr ( $_REQUEST [ 'username' ] );
$password = isset ( $_REQUEST [ 'password' ]) ? validStr ( $_REQUEST [ 'password' ]) : '' ;
userLogin ( $username , $password );
$refreshParent = true ;
$view = 'console' ;
2016-10-04 09:58:57 +08:00
} else if ( $action == 'logout' ) {
2016-07-23 04:03:39 +08:00
userLogout ();
$refreshParent = true ;
$view = 'none' ;
2016-10-04 09:58:57 +08:00
} else if ( $action == 'bandwidth' && isset ( $_REQUEST [ 'newBandwidth' ]) ) {
2016-07-23 04:03:39 +08:00
$_COOKIE [ 'zmBandwidth' ] = validStr ( $_REQUEST [ 'newBandwidth' ]);
2016-10-04 09:58:57 +08:00
setcookie ( 'zmBandwidth' , validStr ( $_REQUEST [ 'newBandwidth' ]), time () + 3600 * 24 * 30 * 12 * 10 );
2016-07-23 04:03:39 +08:00
$refreshParent = true ;
}
// Event scope actions, view permissions only required
2016-09-21 00:17:19 +08:00
if ( canView ( 'Events' ) ) {
if ( $action == 'filter' ) {
if ( ! empty ( $_REQUEST [ 'subaction' ]) ) {
2016-10-04 09:58:57 +08:00
if ( $_REQUEST [ 'subaction' ] == 'addterm' )
2016-07-23 04:03:39 +08:00
$_REQUEST [ 'filter' ] = addFilterTerm ( $_REQUEST [ 'filter' ], $_REQUEST [ 'line' ] );
2016-10-04 09:58:57 +08:00
elseif ( $_REQUEST [ 'subaction' ] == 'delterm' )
2016-07-23 04:03:39 +08:00
$_REQUEST [ 'filter' ] = delFilterTerm ( $_REQUEST [ 'filter' ], $_REQUEST [ 'line' ] );
2016-09-21 00:17:19 +08:00
} elseif ( canEdit ( 'Events' ) ) {
$sql = '' ;
$endSql = '' ;
$filterName = '' ;
if ( ! empty ( $_REQUEST [ 'execute' ]) ) {
// TempFilterName is used in event listing later on
2016-10-04 09:58:57 +08:00
$tempFilterName = $filterName = '_TempFilter' . time ();
2016-09-21 00:17:19 +08:00
} elseif ( ! empty ( $_REQUEST [ 'newFilterName' ]) ) {
2016-07-23 04:03:39 +08:00
$filterName = $_REQUEST [ 'newFilterName' ];
2016-09-14 23:27:59 +08:00
}
2016-09-21 00:17:19 +08:00
if ( $filterName ) {
$sql = " REPLACE INTO Filters SET Name = " . dbEscape ( $filterName ) . " , " ;
} else {
$sql = 'UPDATE Filters SET' ;
$endSql = " where Id = " . $_REQUEST [ 'Id' ];
2016-09-14 23:27:59 +08:00
}
2016-09-21 00:17:19 +08:00
if ( ! empty ( $filterName ) || $endSql ) {
2016-07-23 04:03:39 +08:00
$_REQUEST [ 'filter' ][ 'sort_field' ] = validStr ( $_REQUEST [ 'sort_field' ]);
$_REQUEST [ 'filter' ][ 'sort_asc' ] = validStr ( $_REQUEST [ 'sort_asc' ]);
$_REQUEST [ 'filter' ][ 'limit' ] = validInt ( $_REQUEST [ 'limit' ]);
2016-09-14 23:27:59 +08:00
$sql .= " Query = " . dbEscape ( jsonEncode ( $_REQUEST [ 'filter' ]));
2016-07-23 04:03:39 +08:00
if ( ! empty ( $_REQUEST [ 'AutoArchive' ]) )
$sql .= " , AutoArchive = " . dbEscape ( $_REQUEST [ 'AutoArchive' ]);
if ( ! empty ( $_REQUEST [ 'AutoVideo' ]) )
$sql .= " , AutoVideo = " . dbEscape ( $_REQUEST [ 'AutoVideo' ]);
if ( ! empty ( $_REQUEST [ 'AutoUpload' ]) )
$sql .= " , AutoUpload = " . dbEscape ( $_REQUEST [ 'AutoUpload' ]);
if ( ! empty ( $_REQUEST [ 'AutoEmail' ]) )
$sql .= " , AutoEmail = " . dbEscape ( $_REQUEST [ 'AutoEmail' ]);
if ( ! empty ( $_REQUEST [ 'AutoMessage' ]) )
$sql .= " , AutoMessage = " . dbEscape ( $_REQUEST [ 'AutoMessage' ]);
if ( ! empty ( $_REQUEST [ 'AutoExecute' ]) && ! empty ( $_REQUEST [ 'AutoExecuteCmd' ]) )
$sql .= " , AutoExecute = " . dbEscape ( $_REQUEST [ 'AutoExecute' ]) . " , AutoExecuteCmd = " . dbEscape ( $_REQUEST [ 'AutoExecuteCmd' ]);
if ( ! empty ( $_REQUEST [ 'AutoDelete' ]) )
$sql .= " , AutoDelete = " . dbEscape ( $_REQUEST [ 'AutoDelete' ]);
if ( ! empty ( $_REQUEST [ 'background' ]) )
$sql .= " , Background = " . dbEscape ( $_REQUEST [ 'background' ]);
if ( ! empty ( $_REQUEST [ 'concurrent' ]) )
$sql .= " , Concurrent = " . dbEscape ( $_REQUEST [ 'concurrent' ]);
2016-09-14 23:27:59 +08:00
$sql .= $endSql ;
2016-07-23 04:03:39 +08:00
dbQuery ( $sql );
$refreshParent = true ;
}
2016-09-21 00:17:19 +08:00
} // end if canedit events
} // end if action == filter
} // end if canview events
2016-07-23 04:03:39 +08:00
// Event scope actions, edit permissions required
2016-10-04 09:58:57 +08:00
if ( canEdit ( 'Events' ) ) {
if ( $action == 'rename' && isset ( $_REQUEST [ 'eventName' ]) && ! empty ( $_REQUEST [ 'eid' ]) ) {
2016-07-23 04:03:39 +08:00
dbQuery ( 'UPDATE Events SET Name=? WHERE Id=?' , array ( $_REQUEST [ 'eventName' ], $_REQUEST [ 'eid' ] ) );
2016-10-04 09:58:57 +08:00
} else if ( $action == 'eventdetail' ) {
if ( ! empty ( $_REQUEST [ 'eid' ]) ) {
2016-07-23 04:03:39 +08:00
dbQuery ( 'UPDATE Events SET Cause=?, Notes=? WHERE Id=?' , array ( $_REQUEST [ 'newEvent' ][ 'Cause' ], $_REQUEST [ 'newEvent' ][ 'Notes' ], $_REQUEST [ 'eid' ] ) );
2013-03-17 07:45:21 +08:00
$refreshParent = true ;
2016-10-04 09:58:57 +08:00
} else {
foreach ( getAffectedIds ( 'markEid' ) as $markEid ) {
2016-07-23 04:03:39 +08:00
dbQuery ( 'UPDATE Events SET Cause=?, Notes=? WHERE Id=?' , array ( $_REQUEST [ 'newEvent' ][ 'Cause' ], $_REQUEST [ 'newEvent' ][ 'Notes' ], $markEid ) );
$refreshParent = true ;
}
}
2016-10-04 09:58:57 +08:00
} elseif ( $action == 'archive' || $action == 'unarchive' ) {
$archiveVal = ( $action == 'archive' ) ? 1 : 0 ;
if ( ! empty ( $_REQUEST [ 'eid' ]) ) {
2016-07-23 04:03:39 +08:00
dbQuery ( 'UPDATE Events SET Archived=? WHERE Id=?' , array ( $archiveVal , $_REQUEST [ 'eid' ]) );
2016-10-04 09:58:57 +08:00
} else {
foreach ( getAffectedIds ( 'markEid' ) as $markEid ) {
2016-07-23 04:03:39 +08:00
dbQuery ( 'UPDATE Events SET Archived=? WHERE Id=?' , array ( $archiveVal , $markEid ) );
$refreshParent = true ;
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
}
2016-10-04 09:58:57 +08:00
} elseif ( $action == 'delete' ) {
foreach ( getAffectedIds ( 'markEid' ) as $markEid ) {
2016-07-23 04:03:39 +08:00
deleteEvent ( $markEid );
$refreshParent = true ;
}
2016-09-21 00:17:19 +08:00
if ( isset ( $_REQUEST [ 'object' ] ) and ( $_REQUEST [ 'object' ] == 'filter' ) ) {
if ( ! empty ( $_REQUEST [ 'Id' ]) ) {
dbQuery ( 'DELETE FROM Filters WHERE Id=?' , array ( $_REQUEST [ 'Id' ] ) );
//$refreshParent = true;
}
2016-07-23 04:03:39 +08:00
}
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
}
// Monitor control actions, require a monitor id and control view permissions for that monitor
2016-10-04 09:58:57 +08:00
if ( ! empty ( $_REQUEST [ 'mid' ]) && canView ( 'Control' , $_REQUEST [ 'mid' ] ) ) {
2016-07-23 04:03:39 +08:00
require_once ( 'control_functions.php' );
require_once ( 'Monitor.php' );
$mid = validInt ( $_REQUEST [ 'mid' ]);
2016-10-04 09:58:57 +08:00
if ( $action == 'control' ) {
2016-07-23 04:03:39 +08:00
$monitor = new Monitor ( $mid );
2013-03-17 07:45:21 +08:00
2016-07-23 04:03:39 +08:00
$ctrlCommand = buildControlCommand ( $monitor );
sendControlCommand ( $monitor -> Id (), $ctrlCommand );
2016-10-04 09:58:57 +08:00
} elseif ( $action == 'settings' ) {
2016-07-23 04:03:39 +08:00
$args = " -m " . escapeshellarg ( $mid );
$args .= " -B " . escapeshellarg ( $_REQUEST [ 'newBrightness' ]);
$args .= " -C " . escapeshellarg ( $_REQUEST [ 'newContrast' ]);
$args .= " -H " . escapeshellarg ( $_REQUEST [ 'newHue' ]);
$args .= " -O " . escapeshellarg ( $_REQUEST [ 'newColour' ]);
$zmuCommand = getZmuCommand ( $args );
$zmuOutput = exec ( $zmuCommand );
list ( $brightness , $contrast , $hue , $colour ) = explode ( ' ' , $zmuOutput );
2016-10-04 09:58:57 +08:00
dbQuery ( 'UPDATE Monitors SET Brightness = ?, Contrast = ?, Hue = ?, Colour = ? WHERE Id = ?' , array ( $brightness , $contrast , $hue , $colour , $mid ));
2016-07-23 04:03:39 +08:00
}
}
// Control capability actions, require control edit permissions
2016-10-04 09:58:57 +08:00
if ( canEdit ( 'Control' ) ) {
if ( $action == 'controlcap' ) {
if ( ! empty ( $_REQUEST [ 'cid' ]) ) {
$control = dbFetchOne ( 'SELECT * FROM Controls WHERE Id = ?' , NULL , array ( $_REQUEST [ 'cid' ]) );
} else {
2016-07-23 04:03:39 +08:00
$control = array ();
}
// Define a field type for anything that's not simple text equivalent
$types = array (
// Empty
);
$columns = getTableColumns ( 'Controls' );
2016-10-04 09:58:57 +08:00
foreach ( $columns as $name => $type ) {
if ( preg_match ( '/^(Can|Has)/' , $name ) ) {
2016-07-23 04:03:39 +08:00
$types [ $name ] = 'toggle' ;
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
}
$changes = getFormChanges ( $control , $_REQUEST [ 'newControl' ], $types , $columns );
2016-10-04 09:58:57 +08:00
if ( count ( $changes ) ) {
if ( ! empty ( $_REQUEST [ 'cid' ]) ) {
2016-07-23 04:03:39 +08:00
dbQuery ( " update Controls set " . implode ( " , " , $changes ) . " where Id = ? " , array ( $_REQUEST [ 'cid' ]) );
2016-10-04 09:58:57 +08:00
} else {
2016-07-23 04:03:39 +08:00
dbQuery ( " insert into Controls set " . implode ( " , " , $changes ) );
//$_REQUEST['cid'] = dbInsertId();
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
$refreshParent = true ;
}
$view = 'none' ;
2016-10-04 09:58:57 +08:00
} elseif ( $action == 'delete' ) {
if ( isset ( $_REQUEST [ 'markCids' ]) ) {
foreach ( $_REQUEST [ 'markCids' ] as $markCid ) {
2016-07-23 04:03:39 +08:00
dbQuery ( " delete from Controls where Id = ? " , array ( $markCid ) );
dbQuery ( " update Monitors set Controllable = 0, ControlId = 0 where ControlId = ? " , array ( $markCid ) );
$refreshParent = true ;
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
}
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
}
2013-03-17 07:45:21 +08:00
2016-07-23 04:03:39 +08:00
// Monitor edit actions, require a monitor id and edit permissions for that monitor
2016-10-04 09:58:57 +08:00
if ( ! empty ( $_REQUEST [ 'mid' ]) && canEdit ( 'Monitors' , $_REQUEST [ 'mid' ] ) ) {
2016-07-23 04:03:39 +08:00
$mid = validInt ( $_REQUEST [ 'mid' ]);
2016-10-04 09:58:57 +08:00
if ( $action == 'function' ) {
2016-07-23 04:03:39 +08:00
$monitor = dbFetchOne ( " SELECT * FROM Monitors WHERE Id=? " , NULL , array ( $mid ) );
$newFunction = validStr ( $_REQUEST [ 'newFunction' ]);
2016-10-04 09:58:57 +08:00
$newEnabled = isset ( $_REQUEST [ 'newEnabled' ] ) and $_REQUEST [ 'newEnabled' ] != '1' ? '0' : '1' ;
2016-07-23 04:03:39 +08:00
$oldFunction = $monitor [ 'Function' ];
$oldEnabled = $monitor [ 'Enabled' ];
2016-10-04 09:58:57 +08:00
if ( $newFunction != $oldFunction || $newEnabled != $oldEnabled ) {
2016-07-23 04:03:39 +08:00
dbQuery ( " update Monitors set Function=?, Enabled=? where Id=? " , array ( $newFunction , $newEnabled , $mid ) );
$monitor [ 'Function' ] = $newFunction ;
$monitor [ 'Enabled' ] = $newEnabled ;
//if ( $cookies ) session_write_close();
2016-10-04 09:58:57 +08:00
if ( daemonCheck () ) {
2016-07-23 04:03:39 +08:00
$restart = ( $oldFunction == 'None' ) || ( $newFunction == 'None' ) || ( $newEnabled != $oldEnabled );
2016-10-04 09:58:57 +08:00
zmaControl ( $monitor , 'stop' );
zmcControl ( $monitor , $restart ? 'restart' : '' );
zmaControl ( $monitor , 'start' );
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
$refreshParent = true ;
}
2016-10-04 09:58:57 +08:00
} elseif ( $action == 'zone' && isset ( $_REQUEST [ 'zid' ] ) ) {
2016-07-23 04:03:39 +08:00
$zid = validInt ( $_REQUEST [ 'zid' ]);
2016-10-04 09:58:57 +08:00
$monitor = dbFetchOne ( 'SELECT * FROM Monitors WHERE Id=?' , NULL , array ( $mid ) );
2016-07-23 04:03:39 +08:00
2016-10-04 09:58:57 +08:00
if ( ! empty ( $zid ) ) {
$zone = dbFetchOne ( 'SELECT * FROM Zones WHERE MonitorId=? AND Id=?' , NULL , array ( $mid , $zid ) );
} else {
2016-07-23 04:03:39 +08:00
$zone = array ();
}
2016-10-04 09:58:57 +08:00
if ( $_REQUEST [ 'newZone' ][ 'Units' ] == 'Percent' ) {
2016-07-23 04:03:39 +08:00
$_REQUEST [ 'newZone' ][ 'MinAlarmPixels' ] = intval (( $_REQUEST [ 'newZone' ][ 'MinAlarmPixels' ] * $_REQUEST [ 'newZone' ][ 'Area' ]) / 100 );
$_REQUEST [ 'newZone' ][ 'MaxAlarmPixels' ] = intval (( $_REQUEST [ 'newZone' ][ 'MaxAlarmPixels' ] * $_REQUEST [ 'newZone' ][ 'Area' ]) / 100 );
if ( isset ( $_REQUEST [ 'newZone' ][ 'MinFilterPixels' ]) )
$_REQUEST [ 'newZone' ][ 'MinFilterPixels' ] = intval (( $_REQUEST [ 'newZone' ][ 'MinFilterPixels' ] * $_REQUEST [ 'newZone' ][ 'Area' ]) / 100 );
if ( isset ( $_REQUEST [ 'newZone' ][ 'MaxFilterPixels' ]) )
$_REQUEST [ 'newZone' ][ 'MaxFilterPixels' ] = intval (( $_REQUEST [ 'newZone' ][ 'MaxFilterPixels' ] * $_REQUEST [ 'newZone' ][ 'Area' ]) / 100 );
if ( isset ( $_REQUEST [ 'newZone' ][ 'MinBlobPixels' ]) )
$_REQUEST [ 'newZone' ][ 'MinBlobPixels' ] = intval (( $_REQUEST [ 'newZone' ][ 'MinBlobPixels' ] * $_REQUEST [ 'newZone' ][ 'Area' ]) / 100 );
if ( isset ( $_REQUEST [ 'newZone' ][ 'MaxBlobPixels' ]) )
$_REQUEST [ 'newZone' ][ 'MaxBlobPixels' ] = intval (( $_REQUEST [ 'newZone' ][ 'MaxBlobPixels' ] * $_REQUEST [ 'newZone' ][ 'Area' ]) / 100 );
}
unset ( $_REQUEST [ 'newZone' ][ 'Points' ] );
$types = array ();
$changes = getFormChanges ( $zone , $_REQUEST [ 'newZone' ], $types );
2016-10-04 09:58:57 +08:00
if ( count ( $changes ) ) {
if ( $zid > 0 ) {
2016-07-23 04:03:39 +08:00
dbQuery ( " UPDATE Zones SET " . implode ( " , " , $changes ) . " WHERE MonitorId=? AND Id=? " , array ( $mid , $zid ) );
2016-10-04 09:58:57 +08:00
} else {
2016-07-23 04:03:39 +08:00
dbQuery ( " INSERT INTO Zones SET MonitorId=?, " . implode ( " , " , $changes ), array ( $mid ) );
}
//if ( $cookies ) session_write_close();
2016-10-04 09:58:57 +08:00
if ( daemonCheck () ) {
if ( $_REQUEST [ 'newZone' ][ 'Type' ] == 'Privacy' ) {
zmaControl ( $monitor , 'stop' );
zmcControl ( $monitor , 'restart' );
zmaControl ( $monitor , 'start' );
} else {
zmaControl ( $mid , 'restart' );
2016-07-23 04:03:39 +08:00
}
}
if ( $_REQUEST [ 'newZone' ][ 'Type' ] == 'Privacy' && $monitor [ 'Controllable' ] ) {
require_once ( 'control_functions.php' );
sendControlCommand ( $mid , 'quit' );
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
$refreshParent = true ;
}
$view = 'none' ;
2016-10-04 09:58:57 +08:00
} elseif ( $action == 'plugin' && isset ( $_REQUEST [ 'pl' ])) {
$sql = 'SELECT * FROM PluginsConfig WHERE MonitorId=? AND ZoneId=? AND pluginName=?' ;
2016-07-23 04:03:39 +08:00
$pconfs = dbFetchAll ( $sql , NULL , array ( $mid , $_REQUEST [ 'zid' ], $_REQUEST [ 'pl' ] ) );
$changes = 0 ;
2016-10-04 09:58:57 +08:00
foreach ( $pconfs as $pconf ) {
2016-07-23 04:03:39 +08:00
$value = $_REQUEST [ 'pluginOpt' ][ $pconf [ 'Name' ]];
2016-10-04 09:58:57 +08:00
if ( array_key_exists ( $pconf [ 'Name' ], $_REQUEST [ 'pluginOpt' ]) && ( $pconf [ 'Value' ] != $value )) {
2016-07-23 04:03:39 +08:00
dbQuery ( " UPDATE PluginsConfig SET Value=? WHERE id=? " , array ( $value , $pconf [ 'Id' ] ) );
$changes ++ ;
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
}
2016-10-04 09:58:57 +08:00
if ( $changes > 0 ) {
if ( daemonCheck () ) {
zmaControl ( $mid , 'restart' );
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
$refreshParent = true ;
}
$view = 'none' ;
2016-10-04 09:58:57 +08:00
} elseif ( $action == 'sequence' && isset ( $_REQUEST [ 'smid' ]) ) {
2016-07-23 04:03:39 +08:00
$smid = validInt ( $_REQUEST [ 'smid' ]);
2016-10-04 09:58:57 +08:00
$monitor = dbFetchOne ( 'select * from Monitors where Id = ?' , NULL , array ( $mid ) );
$smonitor = dbFetchOne ( 'select * from Monitors where Id = ?' , NULL , array ( $smid ) );
2013-03-17 07:45:21 +08:00
2016-10-04 09:58:57 +08:00
dbQuery ( 'update Monitors set Sequence=? where Id=?' , array ( $smonitor [ 'Sequence' ], $monitor [ 'Id' ] ) );
dbQuery ( 'update Monitors set Sequence=? WHERE Id=?' , array ( $monitor [ 'Sequence' ], $smonitor [ 'Id' ] ) );
2016-07-23 04:03:39 +08:00
$refreshParent = true ;
fixSequences ();
}
2016-10-04 09:58:57 +08:00
if ( $action == 'delete' ) {
if ( isset ( $_REQUEST [ 'markZids' ]) ) {
2016-07-23 04:03:39 +08:00
$deletedZid = 0 ;
2016-10-04 09:58:57 +08:00
foreach ( $_REQUEST [ 'markZids' ] as $markZid ) {
$zone = dbFetchOne ( 'select * from Zones where Id=?' , NULL , array ( $markZid ) );
dbQuery ( 'delete from Zones WHERE MonitorId=? AND Id=?' , array ( $mid , $markZid ) );
2016-07-23 04:03:39 +08:00
$deletedZid = 1 ;
2013-03-17 07:45:21 +08:00
}
2016-10-04 09:58:57 +08:00
if ( $deletedZid ) {
2016-07-23 04:03:39 +08:00
//if ( $cookies )
//session_write_close();
2016-10-04 09:58:57 +08:00
if ( daemonCheck () ) {
if ( $zone [ 'Type' ] == 'Privacy' ) {
zmaControl ( $mid , 'stop' );
zmcControl ( $mid , 'restart' );
zmaControl ( $mid , 'start' );
} else {
zmaControl ( $mid , 'restart' );
2013-03-17 07:45:21 +08:00
}
2016-10-04 09:58:57 +08:00
} // end if daemonCheck()
2016-07-23 04:03:39 +08:00
$refreshParent = true ;
2016-10-04 09:58:57 +08:00
} // end if deletedzid
} // end if isset($_REQUEST['markZids'])
} // end if action == delete
2016-07-23 04:03:39 +08:00
}
2013-03-17 07:45:21 +08:00
2016-07-23 04:03:39 +08:00
// Monitor edit actions, monitor id derived, require edit permissions for that monitor
2016-10-04 09:58:57 +08:00
if ( canEdit ( 'Monitors' ) ) {
if ( $action == 'monitor' ) {
if ( ! empty ( $_REQUEST [ 'mid' ]) ) {
2016-07-23 04:03:39 +08:00
$mid = validInt ( $_REQUEST [ 'mid' ]);
2016-10-04 09:58:57 +08:00
$monitor = dbFetchOne ( 'SELECT * FROM Monitors WHERE Id = ?' , NULL , array ( $mid ) );
2013-03-17 07:45:21 +08:00
2016-10-04 09:58:57 +08:00
if ( ZM_OPT_X10 ) {
$x10Monitor = dbFetchOne ( 'SELECT * FROM TriggersX10 WHERE MonitorId=?' , NULL , array ( $mid ) );
2016-07-23 04:03:39 +08:00
if ( ! $x10Monitor )
$x10Monitor = array ();
}
2016-10-04 09:58:57 +08:00
} else {
2016-07-23 04:03:39 +08:00
$monitor = array ();
2016-10-04 09:58:57 +08:00
if ( ZM_OPT_X10 ) {
2016-07-23 04:03:39 +08:00
$x10Monitor = array ();
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
}
// Define a field type for anything that's not simple text equivalent
$types = array (
'Triggers' => 'set' ,
'Controllable' => 'toggle' ,
'TrackMotion' => 'toggle' ,
'Enabled' => 'toggle' ,
'DoNativeMotDet' => 'toggle' ,
'Exif' => 'toggle' ,
'RTSPDescribe' => 'toggle' ,
'RecordAudio' => 'toggle' ,
);
$columns = getTableColumns ( 'Monitors' );
$changes = getFormChanges ( $monitor , $_REQUEST [ 'newMonitor' ], $types , $columns );
2016-10-04 09:58:57 +08:00
if ( count ( $changes ) ) {
if ( ! empty ( $_REQUEST [ 'mid' ]) ) {
2016-07-23 04:03:39 +08:00
$mid = validInt ( $_REQUEST [ 'mid' ]);
dbQuery ( " update Monitors set " . implode ( " , " , $changes ) . " where Id =? " , array ( $mid ) );
2016-10-04 09:58:57 +08:00
if ( isset ( $changes [ 'Name' ]) ) {
2016-07-23 04:03:39 +08:00
$saferOldName = basename ( $monitor [ 'Name' ] );
$saferNewName = basename ( $_REQUEST [ 'newMonitor' ][ 'Name' ] );
rename ( ZM_DIR_EVENTS . " / " . $saferOldName , ZM_DIR_EVENTS . " / " . $saferNewName );
}
2016-10-04 09:58:57 +08:00
if ( isset ( $changes [ 'Width' ]) || isset ( $changes [ 'Height' ]) ) {
2016-07-23 04:03:39 +08:00
$newW = $_REQUEST [ 'newMonitor' ][ 'Width' ];
$newH = $_REQUEST [ 'newMonitor' ][ 'Height' ];
$newA = $newW * $newH ;
$oldW = $monitor [ 'Width' ];
$oldH = $monitor [ 'Height' ];
$oldA = $oldW * $oldH ;
2016-10-04 09:58:57 +08:00
$zones = dbFetchAll ( 'SELECT * FROM Zones WHERE MonitorId=?' , NULL , array ( $mid ) );
foreach ( $zones as $zone ) {
2016-07-23 04:03:39 +08:00
$newZone = $zone ;
$points = coordsToPoints ( $zone [ 'Coords' ] );
2016-10-04 09:58:57 +08:00
for ( $i = 0 ; $i < count ( $points ); $i ++ ) {
2016-07-23 04:03:39 +08:00
$points [ $i ][ 'x' ] = intval (( $points [ $i ][ 'x' ] * ( $newW - 1 )) / ( $oldW - 1 ));
$points [ $i ][ 'y' ] = intval (( $points [ $i ][ 'y' ] * ( $newH - 1 )) / ( $oldH - 1 ));
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
$newZone [ 'Coords' ] = pointsToCoords ( $points );
$newZone [ 'Area' ] = intval ( round (( $zone [ 'Area' ] * $newA ) / $oldA ));
$newZone [ 'MinAlarmPixels' ] = intval ( round (( $newZone [ 'MinAlarmPixels' ] * $newA ) / $oldA ));
$newZone [ 'MaxAlarmPixels' ] = intval ( round (( $newZone [ 'MaxAlarmPixels' ] * $newA ) / $oldA ));
$newZone [ 'MinFilterPixels' ] = intval ( round (( $newZone [ 'MinFilterPixels' ] * $newA ) / $oldA ));
$newZone [ 'MaxFilterPixels' ] = intval ( round (( $newZone [ 'MaxFilterPixels' ] * $newA ) / $oldA ));
$newZone [ 'MinBlobPixels' ] = intval ( round (( $newZone [ 'MinBlobPixels' ] * $newA ) / $oldA ));
$newZone [ 'MaxBlobPixels' ] = intval ( round (( $newZone [ 'MaxBlobPixels' ] * $newA ) / $oldA ));
$changes = getFormChanges ( $zone , $newZone , $types );
2016-10-04 09:58:57 +08:00
if ( count ( $changes ) ) {
2016-07-23 04:03:39 +08:00
dbQuery ( " update Zones set " . implode ( " , " , $changes ) . " WHERE MonitorId=? AND Id=? " , array ( $mid , $zone [ 'Id' ] ) );
}
}
}
2016-10-04 09:58:57 +08:00
} elseif ( ! $user [ 'MonitorIds' ] ) {
2016-07-23 04:03:39 +08:00
# FIXME This is actually a race condition. Should lock the table.
2016-10-04 09:58:57 +08:00
$maxSeq = dbFetchOne ( 'SELECT max(Sequence) AS MaxSequence FROM Monitors' , 'MaxSequence' );
$changes [] = 'Sequence = ' . ( $maxSeq + 1 );
2016-07-23 04:03:39 +08:00
dbQuery ( " insert into Monitors set " . implode ( " , " , $changes ) );
$mid = dbInsertId ();
$zoneArea = $_REQUEST [ 'newMonitor' ][ 'Width' ] * $_REQUEST [ 'newMonitor' ][ 'Height' ];
dbQuery ( " insert into Zones set MonitorId = ?, Name = 'All', Type = 'Active', Units = 'Percent', NumCoords = 4, Coords = ?, Area=?, AlarmRGB = 0xff0000, CheckMethod = 'Blobs', MinPixelThreshold = 25, MinAlarmPixels=?, MaxAlarmPixels=?, FilterX = 3, FilterY = 3, MinFilterPixels=?, MaxFilterPixels=?, MinBlobPixels=?, MinBlobs = 1 " , array ( $mid , sprintf ( " %d,%d %d,%d %d,%d %d,%d " , 0 , 0 , $_REQUEST [ 'newMonitor' ][ 'Width' ] - 1 , 0 , $_REQUEST [ 'newMonitor' ][ 'Width' ] - 1 , $_REQUEST [ 'newMonitor' ][ 'Height' ] - 1 , 0 , $_REQUEST [ 'newMonitor' ][ 'Height' ] - 1 ), $zoneArea , intval (( $zoneArea * 3 ) / 100 ), intval (( $zoneArea * 75 ) / 100 ), intval (( $zoneArea * 3 ) / 100 ), intval (( $zoneArea * 75 ) / 100 ), intval (( $zoneArea * 2 ) / 100 ) ) );
//$view = 'none';
mkdir ( ZM_DIR_EVENTS . '/' . $mid , 0755 );
$saferName = basename ( $_REQUEST [ 'newMonitor' ][ 'Name' ]);
symlink ( $mid , ZM_DIR_EVENTS . '/' . $saferName );
2016-10-04 09:58:57 +08:00
if ( isset ( $_COOKIE [ 'zmGroup' ]) ) {
2016-07-23 04:03:39 +08:00
dbQuery ( " UPDATE Groups SET MonitorIds = concat(MonitorIds,', " . $mid . " ') WHERE Id=? " , array ( $_COOKIE [ 'zmGroup' ]) );
}
}
$restart = true ;
}
2013-03-17 07:45:21 +08:00
2016-10-04 09:58:57 +08:00
if ( ZM_OPT_X10 ) {
2016-07-23 04:03:39 +08:00
$x10Changes = getFormChanges ( $x10Monitor , $_REQUEST [ 'newX10Monitor' ] );
2013-03-17 07:45:21 +08:00
2016-10-04 09:58:57 +08:00
if ( count ( $x10Changes ) ) {
if ( $x10Monitor && isset ( $_REQUEST [ 'newX10Monitor' ]) ) {
2016-07-23 04:03:39 +08:00
dbQuery ( " update TriggersX10 set " . implode ( " , " , $x10Changes ) . " where MonitorId=? " , array ( $mid ) );
2016-10-04 09:58:57 +08:00
} elseif ( ! $user [ 'MonitorIds' ] ) {
if ( ! $x10Monitor ) {
2016-07-23 04:03:39 +08:00
dbQuery ( " insert into TriggersX10 set MonitorId = ? " . implode ( " , " , $x10Changes ), array ( $mid ) );
2016-10-04 09:58:57 +08:00
} else {
2016-07-23 04:03:39 +08:00
dbQuery ( " delete from TriggersX10 where MonitorId = ? " , array ( $mid ) );
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
}
$restart = true ;
}
}
2016-10-04 09:58:57 +08:00
if ( $restart ) {
$monitor = dbFetchOne ( 'SELECT * FROM Monitors WHERE Id = ?' , NULL , array ( $mid ) );
2016-07-23 04:03:39 +08:00
//fixDevices();
//if ( $cookies )
//session_write_close();
2016-10-04 09:58:57 +08:00
if ( daemonCheck () ) {
zmaControl ( $monitor , 'stop' );
zmcControl ( $monitor , 'restart' );
zmaControl ( $monitor , 'start' );
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
if ( $monitor [ 'Controllable' ] ) {
require_once ( 'control_functions.php' );
sendControlCommand ( $mid , 'quit' );
}
//daemonControl( 'restart', 'zmwatch.pl' );
$refreshParent = true ;
2016-10-04 09:58:57 +08:00
} // end if restart
2016-07-23 04:03:39 +08:00
$view = 'none' ;
2013-03-17 07:45:21 +08:00
}
2016-10-04 09:58:57 +08:00
if ( $action == 'delete' ) {
if ( isset ( $_REQUEST [ 'markMids' ]) && ! $user [ 'MonitorIds' ] ) {
foreach ( $_REQUEST [ 'markMids' ] as $markMid ) {
if ( canEdit ( 'Monitors' , $markMid ) ) {
if ( $monitor = dbFetchOne ( 'SELECT * FROM Monitors WHERE Id = ?' , NULL , array ( $markMid ) ) ) {
if ( daemonCheck () ) {
zmaControl ( $monitor , 'stop' );
zmcControl ( $monitor , 'stop' );
2016-07-23 04:03:39 +08:00
}
2013-03-17 07:45:21 +08:00
2016-07-23 04:03:39 +08:00
// This is the important stuff
2016-10-04 09:58:57 +08:00
dbQuery ( 'DELETE FROM Monitors WHERE Id = ?' , array ( $markMid ) );
dbQuery ( 'DELETE FROM Zones WHERE MonitorId = ?' , array ( $markMid ) );
2016-07-23 04:03:39 +08:00
if ( ZM_OPT_X10 )
2016-10-04 09:58:57 +08:00
dbQuery ( 'DELETE FROM TriggersX10 WHERE MonitorId=?' , array ( $markMid ) );
2013-03-17 07:45:21 +08:00
2016-07-23 04:03:39 +08:00
fixSequences ();
2013-03-17 07:45:21 +08:00
2016-07-23 04:03:39 +08:00
// If fast deletes are on, then zmaudit will clean everything else up later
// If fast deletes are off and there are lots of events then this step may
// well time out before completing, in which case zmaudit will still tidy up
2016-10-04 09:58:57 +08:00
if ( ! ZM_OPT_FAST_DELETE ) {
2016-07-23 04:03:39 +08:00
// Slight hack, we maybe should load *, but we happen to know that the deleteEvent function uses Id and StartTime.
2016-10-04 09:58:57 +08:00
$markEids = dbFetchAll ( 'SELECT Id,StartTime FROM Events WHERE MonitorId=?' , NULL , array ( $markMid ) );
2016-07-23 04:03:39 +08:00
foreach ( $markEids as $markEid )
deleteEvent ( $markEid , $markMid );
2016-10-04 09:58:57 +08:00
deletePath ( ZM_DIR_EVENTS . '/' . basename ( $monitor [ 'Name' ]) );
deletePath ( ZM_DIR_EVENTS . '/' . $monitor [ 'Id' ] ); // I'm trusting the Id.
2016-07-23 04:03:39 +08:00
}
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
}
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
}
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
}
2013-03-17 07:45:21 +08:00
2016-07-23 04:03:39 +08:00
// Device view actions
2016-10-04 09:58:57 +08:00
if ( canEdit ( 'Devices' ) ) {
if ( $action == 'device' ) {
if ( ! empty ( $_REQUEST [ 'command' ]) ) {
2016-07-23 04:03:39 +08:00
setDeviceStatusX10 ( $_REQUEST [ 'key' ], $_REQUEST [ 'command' ] );
2016-10-04 09:58:57 +08:00
} elseif ( isset ( $_REQUEST [ 'newDevice' ] ) ) {
if ( isset ( $_REQUEST [ 'did' ]) ) {
2016-07-23 04:03:39 +08:00
dbQuery ( " update Devices set Name=?, KeyString=? where Id=? " , array ( $_REQUEST [ 'newDevice' ][ 'Name' ], $_REQUEST [ 'newDevice' ][ 'KeyString' ], $_REQUEST [ 'did' ]) );
2015-08-16 02:22:13 +08:00
} else {
2016-07-23 04:03:39 +08:00
dbQuery ( " insert into Devices set Name=?, KeyString=? " , array ( $_REQUEST [ 'newDevice' ][ 'Name' ], $_REQUEST [ 'newDevice' ][ 'KeyString' ] ) );
}
$refreshParent = true ;
$view = 'none' ;
}
2016-10-04 09:58:57 +08:00
} elseif ( $action == 'delete' ) {
if ( isset ( $_REQUEST [ 'markDids' ]) ) {
foreach ( $_REQUEST [ 'markDids' ] as $markDid ) {
2016-07-23 04:03:39 +08:00
dbQuery ( " delete from Devices where Id=? " , array ( $markDid ) );
$refreshParent = true ;
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
}
2016-10-04 09:58:57 +08:00
} // end if action
} // end if canedit devices
2013-03-17 07:45:21 +08:00
2016-07-23 04:03:39 +08:00
// Group view actions
2016-10-04 09:58:57 +08:00
if ( canView ( 'Groups' ) && $action == 'setgroup' ) {
2016-07-23 04:03:39 +08:00
if ( ! empty ( $_REQUEST [ 'gid' ]) ) {
2016-10-04 09:58:57 +08:00
setcookie ( 'zmGroup' , validInt ( $_REQUEST [ 'gid' ]), time () + 3600 * 24 * 30 * 12 * 10 );
2016-07-23 04:03:39 +08:00
} else {
2016-10-04 09:58:57 +08:00
setcookie ( 'zmGroup' , '' , time () - 3600 * 24 * 2 );
2016-07-23 04:03:39 +08:00
}
$refreshParent = true ;
}
// Group edit actions
if ( canEdit ( 'Groups' ) ) {
2016-10-04 09:58:57 +08:00
if ( $action == 'group' ) {
2016-07-23 04:03:39 +08:00
# Should probably verfy that each monitor id is a valid monitor, that we have access to. HOwever at the moment, you have to have System permissions to do this
$monitors = empty ( $_POST [ 'newGroup' ][ 'MonitorIds' ] ) ? NULL : implode ( ',' , $_POST [ 'newGroup' ][ 'MonitorIds' ]);
if ( ! empty ( $_POST [ 'gid' ]) ) {
dbQuery ( " UPDATE Groups SET Name=?, MonitorIds=? WHERE Id=? " , array ( $_POST [ 'newGroup' ][ 'Name' ], $monitors , $_POST [ 'gid' ]) );
} else {
dbQuery ( " INSERT INTO Groups SET Name=?, MonitorIds=? " , array ( $_POST [ 'newGroup' ][ 'Name' ], $monitors ) );
}
$view = 'none' ;
}
2016-10-04 09:58:57 +08:00
if ( ! empty ( $_REQUEST [ 'gid' ]) && $action == 'delete' ) {
dbQuery ( 'DELETE FROM Groups WHERE Id = ?' , array ( $_REQUEST [ 'gid' ]) );
if ( isset ( $_COOKIE [ 'zmGroup' ]) ) {
if ( $_REQUEST [ 'gid' ] == $_COOKIE [ 'zmGroup' ] ) {
2016-07-23 04:03:39 +08:00
unset ( $_COOKIE [ 'zmGroup' ] );
2016-10-04 09:58:57 +08:00
setcookie ( 'zmGroup' , '' , time () - 3600 * 24 * 2 );
2016-07-23 04:03:39 +08:00
$refreshParent = true ;
2015-08-16 02:22:13 +08:00
}
2016-07-23 04:03:39 +08:00
}
2015-08-16 02:22:13 +08:00
}
2016-07-23 04:03:39 +08:00
$refreshParent = true ;
2016-10-04 09:58:57 +08:00
} // end if can edit groups
2015-08-16 02:22:13 +08:00
2016-07-23 04:03:39 +08:00
// System edit actions
2016-10-04 09:58:57 +08:00
if ( canEdit ( 'System' ) ) {
2016-07-23 04:03:39 +08:00
if ( isset ( $_REQUEST [ 'object' ] ) ) {
if ( $_REQUEST [ 'object' ] == 'server' ) {
2016-10-04 09:58:57 +08:00
if ( $action == 'Save' ) {
2016-07-23 04:03:39 +08:00
if ( ! empty ( $_REQUEST [ 'id' ]) )
2016-10-04 09:58:57 +08:00
$dbServer = dbFetchOne ( 'SELECT * FROM Servers WHERE Id=?' , NULL , array ( $_REQUEST [ 'id' ]) );
2016-07-23 04:03:39 +08:00
else
$dbServer = array ();
$types = array ();
$changes = getFormChanges ( $dbServer , $_REQUEST [ 'newServer' ], $types );
if ( count ( $changes ) ) {
if ( ! empty ( $_REQUEST [ 'id' ]) ) {
dbQuery ( " UPDATE Servers SET " . implode ( " , " , $changes ) . " WHERE Id = ? " , array ( $_REQUEST [ 'id' ]) );
2015-08-16 03:03:11 +08:00
} else {
2016-07-23 04:03:39 +08:00
dbQuery ( " INSERT INTO Servers set " . implode ( " , " , $changes ) );
2015-08-16 03:03:11 +08:00
}
2016-07-23 04:03:39 +08:00
$refreshParent = true ;
}
$view = 'none' ;
} else if ( $action == 'delete' ) {
if ( ! empty ( $_REQUEST [ 'markIds' ]) ) {
foreach ( $_REQUEST [ 'markIds' ] as $Id )
dbQuery ( " DELETE FROM Servers WHERE Id=? " , array ( $Id ) );
}
$refreshParent = true ;
} else {
Error ( " Unknown action $action in saving Server " );
2015-08-16 03:03:11 +08:00
}
2016-07-23 04:03:39 +08:00
} else if ( $_REQUEST [ 'object' ] == 'storage' ) {
2016-10-04 09:58:57 +08:00
if ( $action == 'Save' ) {
2016-07-23 04:03:39 +08:00
if ( ! empty ( $_REQUEST [ 'id' ]) )
2016-10-04 09:58:57 +08:00
$dbStorage = dbFetchOne ( 'SELECT * FROM Storage WHERE Id=?' , NULL , array ( $_REQUEST [ 'id' ]) );
2016-07-23 04:03:39 +08:00
else
$dbStorage = array ();
$types = array ();
$changes = getFormChanges ( $dbStorage , $_REQUEST [ 'newStorage' ], $types );
if ( count ( $changes ) ) {
if ( ! empty ( $_REQUEST [ 'id' ]) ) {
dbQuery ( " UPDATE Storage SET " . implode ( " , " , $changes ) . " WHERE Id = ? " , array ( $_REQUEST [ 'id' ]) );
} else {
dbQuery ( " INSERT INTO Storage set " . implode ( " , " , $changes ) );
}
$refreshParent = true ;
}
$view = 'none' ;
} else if ( $action == 'delete' ) {
if ( ! empty ( $_REQUEST [ 'markIds' ]) ) {
foreach ( $_REQUEST [ 'markIds' ] as $Id )
2016-10-04 09:58:57 +08:00
dbQuery ( 'DELETE FROM Storage WHERE Id=?' , array ( $Id ) );
2016-07-23 04:03:39 +08:00
}
$refreshParent = true ;
} else {
Error ( " Unknown action $action in saving Storage " );
2015-08-16 02:22:13 +08:00
}
2016-07-23 04:03:39 +08:00
} # end if isset($_REQUEST['object'] )
2013-03-17 07:45:21 +08:00
2016-10-04 09:58:57 +08:00
} else if ( $action == 'version' && isset ( $_REQUEST [ 'option' ]) ) {
2016-07-23 04:03:39 +08:00
$option = $_REQUEST [ 'option' ];
2016-10-04 09:58:57 +08:00
switch ( $option ) {
2016-07-23 04:03:39 +08:00
case 'go' :
{
// Ignore this, the caller will open the page itself
break ;
}
case 'ignore' :
{
dbQuery ( " update Config set Value = ' " . ZM_DYN_LAST_VERSION . " ' where Name = 'ZM_DYN_CURR_VERSION' " );
break ;
}
case 'hour' :
case 'day' :
case 'week' :
{
$nextReminder = time ();
2016-10-04 09:58:57 +08:00
if ( $option == 'hour' ) {
2016-07-23 04:03:39 +08:00
$nextReminder += 60 * 60 ;
2016-10-04 09:58:57 +08:00
} elseif ( $option == 'day' ) {
2016-07-23 04:03:39 +08:00
$nextReminder += 24 * 60 * 60 ;
2016-10-04 09:58:57 +08:00
} elseif ( $option == 'week' ) {
2016-07-23 04:03:39 +08:00
$nextReminder += 7 * 24 * 60 * 60 ;
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
dbQuery ( " update Config set Value = ' " . $nextReminder . " ' where Name = 'ZM_DYN_NEXT_REMINDER' " );
break ;
}
case 'never' :
{
dbQuery ( " update Config set Value = '0' where Name = 'ZM_CHECK_FOR_UPDATES' " );
break ;
}
}
}
2016-10-04 09:58:57 +08:00
if ( $action == 'donate' && isset ( $_REQUEST [ 'option' ]) ) {
2016-07-23 04:03:39 +08:00
$option = $_REQUEST [ 'option' ];
2016-10-04 09:58:57 +08:00
switch ( $option ) {
2016-07-23 04:03:39 +08:00
case 'go' :
{
// Ignore this, the caller will open the page itself
break ;
}
case 'hour' :
case 'day' :
case 'week' :
case 'month' :
{
$nextReminder = time ();
2016-10-04 09:58:57 +08:00
if ( $option == 'hour' ) {
2016-07-23 04:03:39 +08:00
$nextReminder += 60 * 60 ;
2016-10-04 09:58:57 +08:00
} elseif ( $option == 'day' ) {
2016-07-23 04:03:39 +08:00
$nextReminder += 24 * 60 * 60 ;
2016-10-04 09:58:57 +08:00
} elseif ( $option == 'week' ) {
2016-07-23 04:03:39 +08:00
$nextReminder += 7 * 24 * 60 * 60 ;
2016-10-04 09:58:57 +08:00
} elseif ( $option == 'month' ) {
2016-07-23 04:03:39 +08:00
$nextReminder += 30 * 24 * 60 * 60 ;
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
dbQuery ( " update Config set Value = ' " . $nextReminder . " ' where Name = 'ZM_DYN_DONATE_REMINDER_TIME' " );
break ;
}
case 'never' :
case 'already' :
{
dbQuery ( " update Config set Value = '0' where Name = 'ZM_DYN_SHOW_DONATE_REMINDER' " );
break ;
}
2016-10-04 09:58:57 +08:00
} // end switch option
2016-07-23 04:03:39 +08:00
}
2016-10-04 09:58:57 +08:00
if ( $action == 'options' && isset ( $_REQUEST [ 'tab' ]) ) {
2016-07-23 04:03:39 +08:00
$configCat = $configCats [ $_REQUEST [ 'tab' ]];
$changed = false ;
2016-10-04 09:58:57 +08:00
foreach ( $configCat as $name => $value ) {
2016-07-23 04:03:39 +08:00
unset ( $newValue );
2016-10-04 09:58:57 +08:00
if ( $value [ 'Type' ] == 'boolean' && empty ( $_REQUEST [ 'newConfig' ][ $name ]) )
2016-07-23 04:03:39 +08:00
$newValue = 0 ;
elseif ( isset ( $_REQUEST [ 'newConfig' ][ $name ]) )
$newValue = preg_replace ( " / \r \n / " , " \n " , stripslashes ( $_REQUEST [ 'newConfig' ][ $name ] ) );
2016-10-04 09:58:57 +08:00
if ( isset ( $newValue ) && ( $newValue != $value [ 'Value' ]) ) {
2016-07-23 04:03:39 +08:00
dbQuery ( 'UPDATE Config SET Value=? WHERE Name=?' , array ( $newValue , $name ) );
$changed = true ;
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
}
2016-10-04 09:58:57 +08:00
if ( $changed ) {
switch ( $_REQUEST [ 'tab' ] ) {
case 'system' :
case 'config' :
case 'paths' :
2016-07-23 04:03:39 +08:00
$restartWarning = true ;
break ;
2016-10-04 09:58:57 +08:00
case 'web' :
case 'tools' :
2016-07-23 04:03:39 +08:00
break ;
2016-10-04 09:58:57 +08:00
case 'logging' :
case 'network' :
case 'mail' :
case 'upload' :
2016-07-23 04:03:39 +08:00
$restartWarning = true ;
break ;
2016-10-04 09:58:57 +08:00
case 'highband' :
case 'medband' :
case 'lowband' :
2016-07-23 04:03:39 +08:00
break ;
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
}
loadConfig ( false );
2016-10-04 09:58:57 +08:00
} elseif ( $action == 'user' ) {
2016-07-23 04:03:39 +08:00
if ( ! empty ( $_REQUEST [ 'uid' ]) )
$dbUser = dbFetchOne ( " SELECT * FROM Users WHERE Id=? " , NULL , array ( $_REQUEST [ 'uid' ]) );
else
$dbUser = array ();
$types = array ();
$changes = getFormChanges ( $dbUser , $_REQUEST [ 'newUser' ], $types );
if ( $_REQUEST [ 'newUser' ][ 'Password' ] )
$changes [ 'Password' ] = " Password = password( " . dbEscape ( $_REQUEST [ 'newUser' ][ 'Password' ]) . " ) " ;
else
unset ( $changes [ 'Password' ] );
2016-10-04 09:58:57 +08:00
if ( count ( $changes ) ) {
if ( ! empty ( $_REQUEST [ 'uid' ]) ) {
2016-07-23 04:03:39 +08:00
dbQuery ( " update Users set " . implode ( " , " , $changes ) . " where Id = ? " , array ( $_REQUEST [ 'uid' ]) );
2016-10-04 09:58:57 +08:00
} else {
2016-07-23 04:03:39 +08:00
dbQuery ( " insert into Users set " . implode ( " , " , $changes ) );
}
$refreshParent = true ;
if ( $dbUser [ 'Username' ] == $user [ 'Username' ] )
userLogin ( $dbUser [ 'Username' ], $dbUser [ 'Password' ] );
}
$view = 'none' ;
2016-10-04 09:58:57 +08:00
} elseif ( $action == 'state' ) {
if ( ! empty ( $_REQUEST [ 'runState' ]) ) {
2013-03-17 07:45:21 +08:00
//if ( $cookies ) session_write_close();
2016-07-23 04:03:39 +08:00
packageControl ( $_REQUEST [ 'runState' ] );
$refreshParent = true ;
}
2016-10-04 09:58:57 +08:00
} elseif ( $action == 'save' ) {
if ( ! empty ( $_REQUEST [ 'runState' ]) || ! empty ( $_REQUEST [ 'newState' ]) ) {
$sql = 'SELECT Id,Function,Enabled FROM Monitors ORDER BY Id' ;
2016-07-23 04:03:39 +08:00
$definitions = array ();
foreach ( dbFetchAll ( $sql ) as $monitor )
{
$definitions [] = $monitor [ 'Id' ] . " : " . $monitor [ 'Function' ] . " : " . $monitor [ 'Enabled' ];
}
$definition = join ( ',' , $definitions );
if ( $_REQUEST [ 'newState' ] )
$_REQUEST [ 'runState' ] = $_REQUEST [ 'newState' ];
dbQuery ( " replace into States set Name=?, Definition=? " , array ( $_REQUEST [ 'runState' ], $definition ) );
}
2016-10-04 09:58:57 +08:00
} elseif ( $action == 'delete' ) {
2016-07-23 04:03:39 +08:00
if ( isset ( $_REQUEST [ 'runState' ]) )
dbQuery ( " delete from States where Name=? " , array ( $_REQUEST [ 'runState' ]) );
2016-10-04 09:58:57 +08:00
if ( isset ( $_REQUEST [ 'markUids' ]) ) {
2016-07-23 04:03:39 +08:00
foreach ( $_REQUEST [ 'markUids' ] as $markUid )
dbQuery ( " delete from Users where Id = ? " , array ( $markUid ) );
if ( $markUid == $user [ 'Id' ] )
userLogout ();
}
}
2016-10-04 09:58:57 +08:00
} else {
if ( ZM_USER_SELF_EDIT && $action == 'user' ) {
2016-07-23 04:03:39 +08:00
$uid = $user [ 'Id' ];
2016-10-04 09:58:57 +08:00
$dbUser = dbFetchOne ( 'SELECT Id, Password, Language FROM Users WHERE Id = ?' , NULL , array ( $uid ) );
2016-07-23 04:03:39 +08:00
$types = array ();
$changes = getFormChanges ( $dbUser , $_REQUEST [ 'newUser' ], $types );
if ( ! empty ( $_REQUEST [ 'newUser' ][ 'Password' ]) )
$changes [ 'Password' ] = " Password = password( " . dbEscape ( $_REQUEST [ 'newUser' ][ 'Password' ]) . " ) " ;
else
unset ( $changes [ 'Password' ] );
2016-10-04 09:58:57 +08:00
if ( count ( $changes ) ) {
2016-07-23 04:03:39 +08:00
dbQuery ( " update Users set " . implode ( " , " , $changes ) . " where Id=? " , array ( $uid ) );
$refreshParent = true ;
}
$view = 'none' ;
2013-03-17 07:45:21 +08:00
}
2016-07-23 04:03:39 +08:00
}
2016-10-04 09:58:57 +08:00
if ( $action == 'reset' ) {
2016-07-23 04:03:39 +08:00
$_SESSION [ 'zmEventResetTime' ] = strftime ( STRF_FMT_DATETIME_DB );
2016-10-04 09:58:57 +08:00
setcookie ( 'zmEventResetTime' , $_SESSION [ 'zmEventResetTime' ], time () + 3600 * 24 * 30 * 12 * 10 );
2016-07-23 04:03:39 +08:00
//if ( $cookies ) session_write_close();
}
2013-03-17 07:45:21 +08:00
}
?>