2002-10-11 17:45:06 +08:00
< ? php
//
2004-01-08 19:45:57 +08:00
// ZoneMinder web function library, $Date$, $Revision$
2003-01-12 02:22:27 +08:00
// Copyright (C) 2003 Philip Coombes
2002-10-11 17:45:06 +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.
//
2003-06-26 19:12:00 +08:00
function userLogin ( $username , $password )
{
2003-06-30 04:53:55 +08:00
global $user , $HTTP_SESSION_VARS ;
2003-06-26 19:12:00 +08:00
2003-07-05 19:00:48 +08:00
$sql = " select * from Users where Username = ' $username ' and Password = password(' $password ') and Enabled = 1 " ;
2003-06-26 19:12:00 +08:00
$result = mysql_query ( $sql );
if ( ! $result )
echo mysql_error ();
2004-01-08 18:09:35 +08:00
$HTTP_SESSION_VARS [ 'username' ] = $username ;
$HTTP_SESSION_VARS [ 'password' ] = $password ;
2004-01-21 22:20:33 +08:00
if ( $db_user = mysql_fetch_assoc ( $result ) )
2003-06-26 19:12:00 +08:00
{
2004-01-21 22:20:33 +08:00
$HTTP_SESSION_VARS [ 'user' ] = $user = $db_user ;
2003-06-26 19:12:00 +08:00
}
2003-06-30 04:53:55 +08:00
else
{
2004-01-21 22:20:33 +08:00
unset ( $user );
2003-06-30 04:53:55 +08:00
}
2003-09-23 17:52:45 +08:00
session_write_close ();
2003-06-26 19:12:00 +08:00
}
2003-06-30 04:53:55 +08:00
function userLogout ()
{
global $user , $HTTP_SESSION_VARS ;
2004-01-08 18:09:35 +08:00
unset ( $HTTP_SESSION_VARS [ 'user' ] );
2003-06-30 04:53:55 +08:00
unset ( $user );
session_destroy ();
}
function visibleMonitor ( $mid )
2003-06-27 20:10:10 +08:00
{
global $user ;
2004-01-08 18:09:35 +08:00
return ( empty ( $user [ 'MonitorIds' ]) || in_array ( $mid , split ( ',' , $user [ 'MonitorIds' ] ) ) );
2003-06-27 20:10:10 +08:00
}
2003-06-30 04:53:55 +08:00
function canView ( $area , $mid = false )
2003-06-27 20:10:10 +08:00
{
global $user ;
2003-06-30 04:53:55 +08:00
return ( ( $user [ $area ] == 'View' || $user [ $area ] == 'Edit' ) && ( ! $mid || visibleMonitor ( $mid ) ) );
2003-06-27 20:10:10 +08:00
}
2003-06-30 04:53:55 +08:00
function canEdit ( $area , $mid = false )
2003-06-26 19:12:00 +08:00
{
global $user ;
2003-06-30 04:53:55 +08:00
return ( $user [ $area ] == 'Edit' && ( ! $mid || visibleMonitor ( $mid ) ) );
2003-06-26 19:12:00 +08:00
}
2002-11-22 20:12:16 +08:00
function deleteEvent ( $eid )
{
2003-06-27 20:10:10 +08:00
global $user ;
2004-01-08 18:09:35 +08:00
if ( $user [ 'Events' ] == 'Edit' && $eid )
2002-12-10 20:51:27 +08:00
{
2002-12-24 19:56:01 +08:00
$result = mysql_query ( " delete from Events where Id = ' $eid ' " );
2002-12-10 20:51:27 +08:00
if ( ! $result )
die ( mysql_error () );
2003-01-11 01:12:23 +08:00
if ( ! ZM_OPT_FAST_DELETE )
2002-12-24 19:56:01 +08:00
{
$result = mysql_query ( " delete from Stats where EventId = ' $eid ' " );
if ( ! $result )
die ( mysql_error () );
$result = mysql_query ( " delete from Frames where EventId = ' $eid ' " );
if ( ! $result )
die ( mysql_error () );
2003-03-21 17:40:46 +08:00
system ( escapeshellcmd ( " rm -rf " . ZM_PATH_EVENTS . " /*/ " . sprintf ( " %d " , $eid ) ) );
2002-12-24 19:56:01 +08:00
}
2002-12-10 20:51:27 +08:00
}
2002-11-22 20:12:16 +08:00
}
2003-06-26 19:12:00 +08:00
function makeLink ( $url , $label , $condition = 1 )
{
$string = " " ;
if ( $condition )
{
2003-06-27 20:10:10 +08:00
$string .= '<a href="' . $url . '">' ;
2003-06-26 19:12:00 +08:00
}
$string .= $label ;
if ( $condition )
{
$string .= '</a>' ;
}
return ( $string );
}
2003-06-27 20:10:10 +08:00
function buildSelect ( $name , $contents , $onchange = " " )
{
2003-10-28 01:07:04 +08:00
if ( preg_match ( " /^( \ w+) \ s* \ [ \ s*[' \" ]?( \ w+)[ \" ']? \ s*] $ / " , $name , $matches ) )
{
$arr = $matches [ 1 ];
$idx = $matches [ 2 ];
global $$arr ;
$value = ${$arr} [ $idx ];
}
else
{
global $$name ;
$value = $$name ;
}
ob_start ();
2003-06-27 20:10:10 +08:00
?>
< select name = " <?= $name ?> " class = " form " < ? php if ( $onchange ) { echo " onChange= \" $onchange\ " " ; } ?>>
< ? php
foreach ( $contents as $content_value => $content_text )
{
?>
2003-10-28 01:07:04 +08:00
< option value = " <?= $content_value ?> " < ? php if ( $value == $content_value ) { echo " selected " ; } ?> ><?= $content_text ?></option>
2003-06-27 20:10:10 +08:00
< ? php
}
?>
</ select >
< ? php
2003-10-28 01:07:04 +08:00
$html = ob_get_contents ();
ob_end_clean ();
return ( $html );
}
function getFormChanges ( $values , $new_values , $types = false )
{
$changes = array ();
if ( ! $types )
$types = array ();
foreach ( $new_values as $key => $value )
{
switch ( $types [ $key ] )
{
case 'set' :
{
if ( is_array ( $new_values [ $key ] ) )
{
if ( join ( ',' , $new_values [ $key ]) != $values [ $key ] )
{
$changes [] = " $key = ' " . join ( ',' , $new_values [ $key ]) . " ' " ;
}
}
elseif ( $values [ $key ] )
{
$changes [] = " $key = '' " ;
}
break ;
}
default :
{
if ( $values [ $key ] != $value )
{
$changes [] = " $key = ' $value ' " ;
}
break ;
}
}
}
return ( $changes );
2003-06-27 20:10:10 +08:00
}
2002-10-11 17:45:06 +08:00
function getBrowser ( & $browser , & $version )
{
global $HTTP_SERVER_VARS ;
2004-01-08 18:09:35 +08:00
if ( ereg ( 'MSIE ([0-9].[0-9]{1,2})' , $HTTP_SERVER_VARS [ 'HTTP_USER_AGENT' ], $log_version ))
2002-10-11 17:45:06 +08:00
{
$version = $log_version [ 1 ];
$browser = 'ie' ;
}
2004-01-08 18:09:35 +08:00
elseif ( ereg ( 'Safari/([0-9.]+)' , $HTTP_SERVER_VARS [ 'HTTP_USER_AGENT' ], $log_version ))
2003-12-16 17:52:36 +08:00
{
$version = $log_version [ 1 ];
$browser = 'safari' ;
}
2004-01-08 18:09:35 +08:00
elseif ( ereg ( 'Opera ([0-9].[0-9]{1,2})' , $HTTP_SERVER_VARS [ 'HTTP_USER_AGENT' ], $log_version ))
2002-10-11 17:45:06 +08:00
{
$version = $log_version [ 1 ];
$browser = 'opera' ;
}
2004-01-08 18:09:35 +08:00
elseif ( ereg ( 'Mozilla/([0-9].[0-9]{1,2})' , $HTTP_SERVER_VARS [ 'HTTP_USER_AGENT' ], $log_version ))
2002-10-11 17:45:06 +08:00
{
$version = $log_version [ 1 ];
$browser = 'mozilla' ;
}
else
{
$version = 0 ;
$browser = 'unknown' ;
}
}
function isNetscape ()
{
getBrowser ( $browser , $version );
return ( $browser == " mozilla " );
}
function canStream ()
{
2003-03-21 18:54:25 +08:00
return ( ZM_CAN_STREAM || isNetscape () || ( ZM_OPT_CAMBOZOLA && file_exists ( ZM_PATH_WEB . '/' . ZM_PATH_CAMBOZOLA )) );
2002-10-11 17:45:06 +08:00
}
2003-01-07 18:44:25 +08:00
function fixDevices ()
{
2003-01-15 23:06:15 +08:00
$string = ZM_PATH_BIN . " /zmfix " ;
2003-01-07 18:44:25 +08:00
$string .= " 2>/dev/null >&- <&- >/dev/null " ;
exec ( $string );
}
2003-01-14 21:07:57 +08:00
function packageControl ( $command )
{
2003-01-15 23:06:15 +08:00
$string = ZM_PATH_BIN . " /zmpkg.pl $command " ;
2003-01-14 21:07:57 +08:00
$string .= " 2>/dev/null >&- <&- >/dev/null " ;
exec ( $string );
}
2002-12-24 19:56:01 +08:00
function daemonControl ( $command , $daemon = false , $args = false )
2002-12-12 06:43:00 +08:00
{
2003-01-15 23:06:15 +08:00
$string = ZM_PATH_BIN . " /zmdc.pl $command " ;
2002-12-12 06:43:00 +08:00
if ( $daemon )
2002-12-24 20:43:13 +08:00
{
$string .= " $daemon " ;
if ( $args )
{
$string .= " $args " ;
}
}
2002-12-12 06:43:00 +08:00
$string .= " 2>/dev/null >&- <&- >/dev/null " ;
exec ( $string );
}
2003-03-26 19:41:22 +08:00
function zmcControl ( $monitor , $restart = false )
2002-10-11 17:45:06 +08:00
{
2004-01-08 18:09:35 +08:00
if ( $monitor [ 'Type' ] == " Local " )
2002-12-24 19:56:01 +08:00
{
2004-01-08 18:09:35 +08:00
$sql = " select count(if(Function!='None',1,NULL)) as ActiveCount from Monitors where Device = ' " . $monitor [ 'Device' ] . " ' " ;
$zmc_args = " -d " . $monitor [ 'Device' ];
2003-03-26 19:41:22 +08:00
}
else
{
2004-01-08 18:09:35 +08:00
$sql = " select count(if(Function!='None',1,NULL)) as ActiveCount from Monitors where Host = ' " . $monitor [ 'Host' ] . " ' and Port = ' " . $monitor [ 'Port' ] . " ' and Path = ' " . $monitor [ 'Path' ] . " ' " ;
$zmc_args = " -H " . $monitor [ 'Host' ] . " -P " . $monitor [ 'Port' ] . " -p ' " . $monitor [ 'Path' ] . " ' " ;
2002-12-24 19:56:01 +08:00
}
$result = mysql_query ( $sql );
if ( ! $result )
echo mysql_error ();
$row = mysql_fetch_assoc ( $result );
2004-01-08 18:09:35 +08:00
$active_count = $row [ 'ActiveCount' ];
2002-12-11 08:22:51 +08:00
2003-11-08 19:52:12 +08:00
if ( ! $active_count )
2002-10-11 17:45:06 +08:00
{
2003-03-26 19:41:22 +08:00
daemonControl ( " stop " , " zmc " , $zmc_args );
2002-10-11 17:45:06 +08:00
}
2002-12-24 19:56:01 +08:00
else
2002-10-11 17:45:06 +08:00
{
2002-12-24 19:56:01 +08:00
if ( $restart )
{
2003-03-26 19:41:22 +08:00
daemonControl ( " stop " , " zmc " , $zmc_args );
2002-12-24 19:56:01 +08:00
}
2003-03-26 19:41:22 +08:00
daemonControl ( " start " , " zmc " , $zmc_args );
2002-10-11 17:45:06 +08:00
}
}
2002-12-24 19:56:01 +08:00
function zmaControl ( $monitor , $restart = false )
2002-10-11 17:45:06 +08:00
{
2002-12-24 19:56:01 +08:00
if ( ! is_array ( $monitor ) )
2002-10-11 17:45:06 +08:00
{
2003-11-08 19:52:12 +08:00
$sql = " select Id,Function,RunMode from Monitors where Id = ' $monitor ' " ;
2002-12-24 19:56:01 +08:00
$result = mysql_query ( $sql );
if ( ! $result )
echo mysql_error ();
$monitor = mysql_fetch_assoc ( $result );
2002-10-11 17:45:06 +08:00
}
2004-01-08 18:09:35 +08:00
if ( $monitor [ 'RunMode' ] == 'Triggered' )
2003-11-08 19:52:12 +08:00
{
// Don't touch anything that's triggered
return ;
}
2003-09-23 17:52:45 +08:00
switch ( $monitor [ 'Function' ] )
2002-10-11 17:45:06 +08:00
{
2003-09-23 17:52:45 +08:00
case 'Modect' :
case 'Record' :
case 'Mocord' :
{
if ( $restart )
{
2004-01-08 18:09:35 +08:00
daemonControl ( " stop " , " zmfilter.pl " , " -m " . $monitor [ 'Id' ] . " -e -1 " );
daemonControl ( " stop " , " zma " , " -m " . $monitor [ 'Id' ] );
2003-09-23 17:52:45 +08:00
if ( ZM_OPT_FRAME_SERVER )
{
2004-01-08 18:09:35 +08:00
daemonControl ( " stop " , " zmf " , " -m " . $monitor [ 'Id' ] );
2003-09-23 17:52:45 +08:00
}
}
if ( ZM_OPT_FRAME_SERVER )
{
2004-01-08 18:09:35 +08:00
daemonControl ( " start " , " zmf " , " -m " . $monitor [ 'Id' ] );
2003-09-23 17:52:45 +08:00
}
2004-01-08 18:09:35 +08:00
daemonControl ( " start " , " zma " , " -m " . $monitor [ 'Id' ] );
daemonControl ( " start " , " zmfilter.pl " , " -m " . $monitor [ 'Id' ] . " -e -1 " );
2003-09-23 17:52:45 +08:00
break ;
}
default :
2002-12-24 19:56:01 +08:00
{
2004-01-08 18:09:35 +08:00
daemonControl ( " stop " , " zmfilter.pl " , " -m " . $monitor [ 'Id' ] . " -e -1 " );
daemonControl ( " stop " , " zma " , " -m " . $monitor [ 'Id' ] );
2003-04-22 22:18:20 +08:00
if ( ZM_OPT_FRAME_SERVER )
{
2004-01-08 18:09:35 +08:00
daemonControl ( " stop " , " zmf " , " -m " . $monitor [ 'Id' ] );
2003-04-22 22:18:20 +08:00
}
2003-09-23 17:52:45 +08:00
break ;
2003-04-22 22:18:20 +08:00
}
2002-10-11 17:45:06 +08:00
}
}
2002-12-24 19:56:01 +08:00
function daemonCheck ( $daemon = false , $args = false )
2002-10-11 17:45:06 +08:00
{
2003-01-15 23:06:15 +08:00
$string = ZM_PATH_BIN . " /zmdc.pl check " ;
2002-12-24 19:56:01 +08:00
if ( $daemon )
2002-10-11 17:45:06 +08:00
{
2002-12-24 19:56:01 +08:00
$string .= " $daemon " ;
if ( $args )
$string .= " $args " ;
2002-10-11 17:45:06 +08:00
}
2002-12-24 19:56:01 +08:00
$result = exec ( $string );
return ( preg_match ( '/running/' , $result ) );
}
2003-03-26 19:41:22 +08:00
function zmcCheck ( $monitor )
2002-12-24 19:56:01 +08:00
{
2004-01-08 18:09:35 +08:00
if ( $monitor [ 'Type' ] == 'Local' )
2003-03-26 19:41:22 +08:00
{
2004-01-08 18:09:35 +08:00
$zmc_args = " -d " . $monitor [ 'Device' ];
2003-03-26 19:41:22 +08:00
}
else
2002-10-11 17:45:06 +08:00
{
2004-01-08 18:09:35 +08:00
$zmc_args = " -H " . $monitor [ 'Host' ] . " -P " . $monitor [ 'Port' ] . " -p ' " . $monitor [ 'Path' ] . " ' " ;
2002-10-11 17:45:06 +08:00
}
2003-03-26 19:41:22 +08:00
return ( daemonCheck ( " zmc " , $zmc_args ) );
2002-12-24 19:56:01 +08:00
}
function zmaCheck ( $monitor )
{
if ( is_array ( $monitor ) )
2002-10-11 17:45:06 +08:00
{
2004-01-08 18:09:35 +08:00
$monitor = $monitor [ 'Id' ];
2002-10-11 17:45:06 +08:00
}
2002-12-24 19:56:01 +08:00
return ( daemonCheck ( " zma " , " -m $monitor " ) );
2002-10-11 17:45:06 +08:00
}
2003-04-14 06:20:22 +08:00
2003-10-10 23:46:36 +08:00
function createVideo ( $event , $rate , $scale , $overwrite = 0 )
2003-04-14 06:20:22 +08:00
{
2004-01-08 18:09:35 +08:00
$command = ZM_PATH_BIN . " /zmvideo.pl -e " . $event [ 'Id' ] . " -r $rate -s $scale " ;
2003-11-05 23:06:44 +08:00
if ( $overwrite )
2003-10-10 23:46:36 +08:00
$command .= " -o " ;
2003-04-14 06:20:22 +08:00
$result = exec ( $command , $output , $status );
2003-04-14 07:07:07 +08:00
return ( $status ? " " : rtrim ( $result ) );
2003-04-14 06:20:22 +08:00
}
2003-10-19 17:47:32 +08:00
function reScale ( $dimension , $scale = 1 )
{
if ( $scale == 1 )
return ( $dimension );
if ( $scale > 0 )
{
return ( ( int )( $dimension * $scale ) );
}
else
{
return ( ( int )( 1 + (( $dimension - 1 ) /- $scale )) );
}
}
2002-10-11 17:45:06 +08:00
?>