'; } $string .= $label; if ( $condition ) { $string .= ''; } return( $string ); } function buildSelect( $name, $contents, $onchange="" ) { 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(); ?> $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 ); } function getBrowser( &$browser, &$version ) { global $HTTP_SERVER_VARS; if (ereg( 'MSIE ([0-9].[0-9]{1,2})',$HTTP_SERVER_VARS['HTTP_USER_AGENT'],$log_version)) { $version = $log_version[1]; $browser = 'ie'; } elseif (ereg( 'Safari/([0-9.]+)',$HTTP_SERVER_VARS['HTTP_USER_AGENT'],$log_version)) { $version = $log_version[1]; $browser = 'safari'; } elseif (ereg( 'Opera ([0-9].[0-9]{1,2})',$HTTP_SERVER_VARS['HTTP_USER_AGENT'],$log_version)) { $version = $log_version[1]; $browser = 'opera'; } elseif (ereg( 'Mozilla/([0-9].[0-9]{1,2})',$HTTP_SERVER_VARS['HTTP_USER_AGENT'],$log_version)) { $version = $log_version[1]; $browser = 'mozilla'; } else { $version = 0; $browser = 'unknown'; } } function isNetscape() { getBrowser( $browser, $version ); return( $browser == "mozilla" ); } function isInternetExplorer() { getBrowser( $browser, $version ); return( $browser == "ie" ); } function isWindows() { global $HTTP_SERVER_VARS; return ( preg_match( '/Win/', $HTTP_SERVER_VARS['HTTP_USER_AGENT'] ) ); } function canStreamNative() { return( ZM_CAN_STREAM == "yes" || ( ZM_CAN_STREAM == "auto" && isNetscape() ) ); } function canStreamApplet() { return( (ZM_OPT_CAMBOZOLA && file_exists( ZM_PATH_WEB.'/'.ZM_PATH_CAMBOZOLA )) ); } function canStream() { return( canStreamNative() | canStreamApplet() ); } function fixDevices() { $string = ZM_PATH_BIN."/zmfix"; $string .= " 2>/dev/null >&- <&- >/dev/null"; exec( $string ); } function packageControl( $command ) { $string = ZM_PATH_BIN."/zmpkg.pl $command"; $string .= " 2>/dev/null >&- <&- >/dev/null"; exec( $string ); } function daemonControl( $command, $daemon=false, $args=false ) { $string = ZM_PATH_BIN."/zmdc.pl $command"; if ( $daemon ) { $string .= " $daemon"; if ( $args ) { $string .= " $args"; } } $string .= " 2>/dev/null >&- <&- >/dev/null"; exec( $string ); } function zmcControl( $monitor, $restart=false ) { if ( $monitor['Type'] == "Local" ) { $sql = "select count(if(Function!='None',1,NULL)) as ActiveCount from Monitors where Device = '".$monitor['Device']."'"; $zmc_args = "-d ".$monitor['Device']; } else { $sql = "select count(if(Function!='None',1,NULL)) as ActiveCount from Monitors where Id = '".$monitor['Id']."'"; $zmc_args = "-m ".$monitor['Id']; } $result = mysql_query( $sql ); if ( !$result ) echo mysql_error(); $row = mysql_fetch_assoc( $result ); $active_count = $row['ActiveCount']; if ( !$active_count ) { daemonControl( "stop", "zmc", $zmc_args ); } else { if ( $restart ) { daemonControl( "stop", "zmc", $zmc_args ); } daemonControl( "start", "zmc", $zmc_args ); } } function zmaControl( $monitor, $restart=false ) { if ( !is_array( $monitor ) ) { $sql = "select Id,Function,RunMode from Monitors where Id = '$monitor'"; $result = mysql_query( $sql ); if ( !$result ) echo mysql_error(); $monitor = mysql_fetch_assoc( $result ); } if ( $monitor['RunMode'] == 'Triggered' ) { // Don't touch anything that's triggered return; } switch ( $monitor['Function'] ) { case 'Modect' : case 'Record' : case 'Mocord' : { if ( $restart ) { daemonControl( "stop", "zma", "-m ".$monitor['Id'] ); if ( ZM_OPT_FRAME_SERVER ) { daemonControl( "stop", "zmf", "-m ".$monitor['Id'] ); } } if ( ZM_OPT_FRAME_SERVER ) { daemonControl( "start", "zmf", "-m ".$monitor['Id'] ); } daemonControl( "start", "zma", "-m ".$monitor['Id'] ); break; } default : { daemonControl( "stop", "zma", "-m ".$monitor['Id'] ); if ( ZM_OPT_FRAME_SERVER ) { daemonControl( "stop", "zmf", "-m ".$monitor['Id'] ); } break; } } } function daemonCheck( $daemon=false, $args=false ) { $string = ZM_PATH_BIN."/zmdc.pl check"; if ( $daemon ) { $string .= " $daemon"; if ( $args ) $string .= " $args"; } $result = exec( $string ); return( preg_match( '/running/', $result ) ); } function zmcCheck( $monitor ) { if ( $monitor['Type'] == 'Local' ) { $zmc_args = "-d ".$monitor['Device']; } else { $zmc_args = "-m ".$monitor['Id']; } return( daemonCheck( "zmc", $zmc_args ) ); } function zmaCheck( $monitor ) { if ( is_array( $monitor ) ) { $monitor = $monitor['Id']; } return( daemonCheck( "zma", "-m $monitor" ) ); } function createVideo( $event, $rate, $scale, $overwrite=0 ) { $command = ZM_PATH_BIN."/zmvideo.pl -e ".$event['Id']." -r ".sprintf( "%.2f", ($rate/RATE_SCALE) )." -s ".sprintf( "%.2f", ($scale/SCALE_SCALE) ); if ( $overwrite ) $command .= " -o"; $result = exec( $command, $output, $status ); return( $status?"":rtrim($result) ); } function createImage( $monitor, $scale ) { if ( is_array( $monitor ) ) { $monitor = $monitor['Id']; } chdir( ZM_DIR_IMAGES ); $command = ZMU_COMMAND." -m $monitor -i"; if ( !empty($scale) && $scale < 100 ) $command .= " -S $scale"; $status = exec( escapeshellcmd( $command ) ); chdir( '..' ); return( $status ); } function reScale( $dimension, $scale=SCALE_SCALE ) { if ( $scale == SCALE_SCALE ) return( $dimension ); return( (int)(($dimension*$scale)/SCALE_SCALE) ); } function parseSort() { global $sort_field, $sort_asc; // Inputs global $sort_query, $sort_column, $sort_order; // Outputs if ( !isset($sort_field) ) { $sort_field = "StartTime"; $sort_asc = false; } switch( $sort_field ) { case 'Id' : $sort_column = "E.Id"; break; case 'MonitorName' : $sort_column = "M.Name"; break; case 'Name' : $sort_column = "E.Name"; break; case 'StartTime' : $sort_column = "E.StartTime"; break; case 'Secs' : $sort_column = "E.Length"; break; case 'Frames' : $sort_column = "E.Frames"; break; case 'AlarmFrames' : $sort_column = "E.AlarmFrames"; break; case 'TotScore' : $sort_column = "E.TotScore"; break; case 'AvgScore' : $sort_column = "E.AvgScore"; break; case 'MaxScore' : $sort_column = "E.MaxScore"; break; default: $sort_column = "E.StartTime"; break; } $sort_order = $sort_asc?"asc":"desc"; if ( !$sort_asc ) $sort_asc = 0; $sort_query = "&sort_field=$sort_field&sort_asc=$sort_asc"; } function parseFilter() { global $filter_query, $filter_sql, $filter_fields; $filter_query = ''; $filter_sql = ''; $filter_fields = ''; global $trms; if ( $trms ) { $filter_query .= "&trms=$trms"; $filter_fields .= ''."\n"; } for ( $i = 1; $i <= $trms; $i++ ) { $conjunction_name = "cnj$i"; $obracket_name = "obr$i"; $cbracket_name = "cbr$i"; $attr_name = "attr$i"; $op_name = "op$i"; $value_name = "val$i"; global $$conjunction_name, $$obracket_name, $$cbracket_name, $$attr_name, $$op_name, $$value_name; if ( isset($$conjunction_name) ) { $filter_query .= "&$conjunction_name=".$$conjunction_name; $filter_sql .= " ".$$conjunction_name." "; $filter_fields .= ''."\n"; } if ( isset($$obracket_name) ) { $filter_query .= "&$obracket_name=".$$obracket_name; $filter_sql .= str_repeat( "(", $$obracket_name ); $filter_fields .= ''."\n"; } if ( isset($$attr_name) ) { $filter_query .= "&$attr_name=".$$attr_name; $filter_fields .= ''."\n"; $value = $$value_name; switch ( $$attr_name ) { case 'MonitorName': $filter_sql .= 'M.'.preg_replace( '/^Monitor/', '', $$attr_name ); break; case 'DateTime': $value = strftime( "%Y-%m-%d %H:%M:%S", strtotime( $$value_name ) ); $filter_sql .= "E.StartTime"; break; case 'Date': $value = "to_days( '".strftime( "%Y-%m-%d %H:%M:%S", strtotime( $$value_name ) )."' )"; $filter_sql .= "to_days( E.StartTime )"; break; case 'Time': $value = "extract( hour_second from '".strftime( "%Y-%m-%d %H:%M:%S", strtotime( $$value_name ) )."' )"; $filter_sql .= "extract( hour_second from E.StartTime )"; break; case 'Weekday': $value = "weekday( '".strftime( "%Y-%m-%d %H:%M:%S", strtotime( $$value_name ) )."' )"; $filter_sql .= "weekday( E.StartTime )"; break; case 'MonitorId': case 'Length': case 'Frames': case 'AlarmFrames': case 'TotScore': case 'AvgScore': case 'MaxScore': $filter_sql .= "E.".$$attr_name; break; case 'Archived': $filter_sql .= "E.Archived = ".$$value_name; break; } switch ( $$op_name ) { case '=' : case '!=' : case '>=' : case '>' : case '<' : case '<=' : $filter_sql .= " ".$$op_name." '$value'"; break; case '=~' : $filter_sql .= " regexp '$value'"; break; case '!~' : $filter_sql .= " not regexp '$value'"; break; case '=[]' : $filter_sql .= " in ('".join( "','", preg_split( '/["\'\s]*,["\'\s]*/', $value ) )."')"; break; case '![]' : $filter_sql .= " not in ('".join( "','", preg_split( '/["\'\s]*,["\'\s]*/', $value ) )."')"; break; } $filter_query .= "&$op_name=".urlencode($$op_name); $filter_fields .= ''."\n"; $filter_query .= "&$value_name=".urlencode($$value_name); $filter_fields .= ''."\n"; } if ( isset($$cbracket_name) ) { $filter_query .= "&$cbracket_name=".$$cbracket_name; $filter_sql .= str_repeat( ")", $$cbracket_name ); $filter_fields .= ''."\n"; } } $filter_sql = " and ( $filter_sql )"; } function getLoad() { $uptime = shell_exec( 'uptime' ); $load = ''; if ( preg_match( '/load average: ([\d.]+)/', $uptime, $matches ) ) $load = $matches[1]; return( $load ); } function getSpace() { $df = shell_exec( 'df '.ZM_DIR_EVENTS ); $load = ''; if ( preg_match( '/\s(\d+%)/ms', $df, $matches ) ) $space = $matches[1]; return( $space ); } ?>