2002-10-11 17:45:06 +08:00
< ? php
//
2004-01-08 19:45:57 +08:00
// ZoneMinder web function library, $Date$, $Revision$
2005-02-24 22:40:14 +08:00
// Copyright (C) 2003, 2004, 2005 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.
//
2005-02-24 18:43:29 +08:00
// Compatibility functions
if ( version_compare ( phpversion (), " 4.3.0 " , " < " ) )
{
function ob_get_clean ()
{
$buffer = ob_get_contents ();
ob_end_clean ();
return ( $buffer );
}
}
2005-10-20 23:42:41 +08:00
function userLogin ( $username , $password = " " )
2003-06-26 19:12:00 +08:00
{
2005-02-24 07:46:35 +08:00
global $user , $cookies ;
if ( version_compare ( phpversion (), " 4.1.0 " , " < " ) )
{
global $_SESSION , $_SERVER ;
}
2003-06-26 19:12:00 +08:00
2005-10-20 23:42:41 +08:00
if ( ZM_AUTH_TYPE == " builtin " )
{
$sql = " select * from Users where Username = ' " . mysql_escape_string ( $username ) . " ' and Password = password(' " . mysql_escape_string ( $password ) . " ') and Enabled = 1 " ;
}
else
{
$sql = " select * from Users where Username = ' " . mysql_escape_string ( $username ) . " ' and Enabled = 1 " ;
}
2003-06-26 19:12:00 +08:00
$result = mysql_query ( $sql );
if ( ! $result )
echo mysql_error ();
2005-01-17 00:08:35 +08:00
$_SESSION [ 'username' ] = $username ;
2005-10-20 23:42:41 +08:00
if ( ZM_AUTH_RELAY == " plain " )
2005-01-19 09:50:24 +08:00
{
// Need to save this in session
$_SESSION [ 'password' ] = $password ;
}
2005-01-17 00:08:35 +08:00
$_SESSION [ 'remote_addr' ] = $_SERVER [ 'REMOTE_ADDR' ]; // To help prevent session hijacking
2004-01-21 22:20:33 +08:00
if ( $db_user = mysql_fetch_assoc ( $result ) )
2003-06-26 19:12:00 +08:00
{
2005-01-17 00:08:35 +08:00
$_SESSION [ 'user' ] = $user = $db_user ;
2005-10-20 23:42:41 +08:00
if ( ZM_AUTH_TYPE == " builtin " )
{
$_SESSION [ 'password_hash' ] = $user [ 'Password' ];
}
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
}
2004-12-29 02:20:11 +08:00
if ( $cookies ) session_write_close ();
2003-06-26 19:12:00 +08:00
}
2003-06-30 04:53:55 +08:00
function userLogout ()
{
2005-02-24 07:46:35 +08:00
global $user ;
if ( version_compare ( phpversion (), " 4.1.0 " , " < " ) )
{
global $_SESSION ;
}
2003-06-30 04:53:55 +08:00
2005-01-17 00:08:35 +08:00
unset ( $_SESSION [ 'user' ] );
2003-06-30 04:53:55 +08:00
unset ( $user );
session_destroy ();
}
2005-10-21 00:05:09 +08:00
function authHash ( $use_remote_addr )
2004-12-30 02:03:13 +08:00
{
2005-02-24 07:46:35 +08:00
if ( version_compare ( phpversion (), " 4.1.0 " , " < " ) )
{
global $_SESSION ;
}
2004-12-30 02:03:13 +08:00
2005-10-20 23:42:41 +08:00
if ( ZM_OPT_USE_AUTH && ZM_AUTH_RELAY == " hashed " )
2004-12-30 02:03:13 +08:00
{
$time = localtime ();
2005-01-19 09:50:24 +08:00
if ( $use_remote_addr )
{
2005-10-21 00:05:09 +08:00
$auth_key = ZM_AUTH_HASH_SECRET . $_SESSION [ 'username' ] . $_SESSION [ 'password_hash' ] . $_SESSION [ 'remote_addr' ] . $time [ 2 ] . $time [ 3 ] . $time [ 4 ] . $time [ 5 ];
2005-01-19 09:50:24 +08:00
}
else
{
2005-10-21 00:05:09 +08:00
$auth_key = ZM_AUTH_HASH_SECRET . $_SESSION [ 'username' ] . $_SESSION [ 'password_hash' ] . $time [ 2 ] . $time [ 3 ] . $time [ 4 ] . $time [ 5 ];
2005-01-19 09:50:24 +08:00
}
2004-12-30 02:03:13 +08:00
$auth = md5 ( $auth_key );
}
else
{
2005-01-19 09:50:24 +08:00
$auth = " " ;
2004-12-30 02:03:13 +08:00
}
return ( $auth );
}
function getStreamSrc ( $args )
{
2005-02-24 07:46:35 +08:00
if ( version_compare ( phpversion (), " 4.1.0 " , " < " ) )
{
2005-06-13 00:52:03 +08:00
global $_SESSION , $_SERVER ;
2005-02-24 07:46:35 +08:00
}
2004-12-30 02:03:13 +08:00
2005-11-07 05:21:35 +08:00
$stream_src = ZM_URL . ZM_PATH_ZMS ;
2004-12-30 02:03:13 +08:00
if ( ZM_OPT_USE_AUTH )
{
2005-10-20 23:42:41 +08:00
if ( ZM_AUTH_RELAY == " hashed " )
2005-01-19 09:50:24 +08:00
{
2005-10-20 23:42:41 +08:00
$args [] = " auth= " . authHash ( ZM_AUTH_HASH_IPS );
2005-01-19 09:50:24 +08:00
}
2005-10-20 23:42:41 +08:00
elseif ( ZM_AUTH_RELAY == " plain " )
2005-01-19 09:50:24 +08:00
{
$args [] = " user= " . $_SESSION [ 'username' ];
$args [] = " pass= " . $_SESSION [ 'password' ];
}
2004-12-30 02:03:13 +08:00
}
2005-01-27 04:52:59 +08:00
if ( ZM_RAND_STREAM )
{
$args [] = " rand= " . time ();
}
2004-12-30 02:03:13 +08:00
if ( count ( $args ) )
{
$stream_src .= " ? " . join ( " & " , $args );
}
return ( $stream_src );
}
2005-12-08 19:10:44 +08:00
function outputVideoStream ( $src , $width , $height , $name , $format )
{
switch ( $format )
{
case " asf " :
$mime_type = " video/x-ms-asf " ;
break ;
case " swf " :
$mime_type = " application/x-shockwave-flash " ;
break ;
case " mp4 " :
$mime_type = " video/mp4 " ;
break ;
case " mov " :
$mime_type = " video/quicktime " ;
break ;
default :
$mime_type = " video/ $format " ;
break ;
}
$object_tag = false ;
if ( ZM_WEB_USE_OBJECT_TAGS )
{
switch ( $format )
{
case " asf " :
case " wmv " :
{
if ( isWindows () )
{
?>
< object id = " <?= $name ?> " width = " <?= $width ?> " height = " <?= $height ?> "
classid = " CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95 "
codebase = " http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902 "
standby = " Loading Microsoft Windows Media Player components... "
2005-12-09 06:18:17 +08:00
type = " <?= $mime_type ?> " >
2005-12-08 19:10:44 +08:00
< param name = " FileName " value = " <?= $src ?> " >
< param name = " autoStart " value = " 1 " >
< param name = " showControls " value = " 0 " >
< embed type = " <?= $mime_type ?> "
2005-12-09 06:18:17 +08:00
pluginspage = " http://www.microsoft.com/Windows/MediaPlayer/ "
2005-12-08 19:10:44 +08:00
src = " <?= $src ?> "
name = " <?= $name ?> "
width = " <?= $width ?> "
height = " <?= $height ?> "
autostart = " 1 "
showcontrols = " 0 " >
</ embed >
</ object >
< ? php
$object_tag = true ;
}
break ;
}
case " mov " :
{
?>
< object id = " <?= $name ?> " width = " <?= $width ?> " height = " <?= $height ?> "
classid = " clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B "
codebase = " http://www.apple.com/qtactivex/qtplugin.cab "
2005-12-09 06:18:17 +08:00
type = " <?= $mime_type ?> " >
2005-12-08 19:10:44 +08:00
< param name = " src " value = " <?= $src ?> " >
2005-12-09 06:18:17 +08:00
< param name = " autoplay " VALUE = " true " >
< param name = " controller " VALUE = " false " >
2005-12-08 19:10:44 +08:00
< embed type = " <?= $mime_type ?> "
src = " <?= $src ?> "
pluginspage = " http://www.apple.com/quicktime/download/ "
name = " <?= $name ?> "
width = " <?= $width ?> "
height = " <?= $height ?> "
autoplay = " true "
controller = " true "
</ embed >
</ object >
< ? php
$object_tag = true ;
break ;
}
case " swf " :
{
?>
< object id = " <?= $name ?> " width = " <?= $width ?> " height = " <?= $height ?> "
classid = " clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 "
codebase = " http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0 "
2005-12-09 06:18:17 +08:00
type = " <?= $mime_type ?> " >
2005-12-08 19:10:44 +08:00
< param name = " movie " value = " <?= $src ?> " >
< param name = quality value = " high " >
< param name = bgcolor value = " #ffffff " >
< embed type = " <?= $mime_type ?> "
pluginspage = " http://www.macromedia.com/go/getflashplayer "
src = " <?= $src ?> "
width = " <?= $width ?> "
height = " <?= $height ?> "
name = " <?= $name ?> "
quality = " high "
bgcolor = " #ffffff "
</ embed >
</ object >
< ? php
$object_tag = true ;
break ;
}
}
}
if ( ! $object_tag )
{
?>
< embed < ? = isset ( $mime_type ) ? ( ' type="' . $mime_type . '"' ) : " " ?>
src = " <?= $src ?> "
width = " <?= $width ?> "
height = " <?= $height ?> "
name = " <?= $name ?> "
autostart = " 1 "
autoplay = " 1 "
showcontrols = " 0 "
controller = " 0 " >
</ embed >
< ? php
}
}
function outputImageStream ( $src , $width , $height , $name = " " )
{
?>
< img src = " <?= $src ?> " alt = " <?= $name ?> " border = " 0 " width = " <?= $width ?> " height = " <?= $height ?> " >
< ? php
}
function outputControlStream ( $src , $width , $height , $monitor , $scale , $target )
{
global $PHP_SELF ;
?>
< form name = " ctrl_form " method = " get " action = " <?= $PHP_SELF ?> " target = " <?= $target ?> " >
< input type = " hidden " name = " view " value = " blank " >
< input type = " hidden " name = " mid " value = " <?= $monitor['Id'] ?> " >
< input type = " hidden " name = " action " value = " control " >
< ? php
if ( $monitor [ 'CanMoveMap' ] )
{
?>
< input type = " hidden " name = " control " value = " move_map " >
< ? php
}
elseif ( $monitor [ 'CanMoveRel' ] )
{
?>
< input type = " hidden " name = " control " value = " move_pseudo_map " >
< ? php
}
elseif ( $monitor [ 'CanMoveCon' ] )
{
?>
< input type = " hidden " name = " control " value = " move_con_map " >
< ? php
}
?>
< input type = " hidden " name = " scale " value = " <?= $scale ?> " >
< input type = " image " src = " <?= $src ?> " border = " 0 " width = " <?= $width ?> " height = " <?= $height ?> " >
</ form >
< ? php
}
function outputHelperStream ( $src , $width , $height , $name = " " )
{
?>
< applet code = " com.charliemouse.cambozola.Viewer "
archive = " <?= ZM_PATH_CAMBOZOLA ?> "
align = " middle "
width = " <?= $width ?> "
height = " <?= $height ?> " >
< param name = " url " value = " <?= $src ?> " >
</ applet >
< ? php
}
function outputImageStill ( $src , $width , $height , $name = " " )
{
?>
< img name = " zmImage " src = " <?= $src ?> " alt = " <?= $name ?> " border = " 0 " width = " <?= $width ?> " height = " <?= $height ?> " >
< ? php
}
function outputControlStill ( $src , $width , $height , $monitor , $scale , $target )
{
global $PHP_SELF ;
?>
< form name = " ctrl_form " method = " get " action = " <?= $PHP_SELF ?> " target = " <?= $target ?> " >
< input type = " hidden " name = " view " value = " blank " >
< input type = " hidden " name = " mid " value = " <?= $monitor['Id'] ?> " >
< input type = " hidden " name = " action " value = " control " >
< ? php
if ( $monitor [ 'CanMoveMap' ] )
{
?>
< input type = " hidden " name = " control " value = " move_map " >
< ? php
}
elseif ( $monitor [ 'CanMoveRel' ] )
{
?>
< input type = " hidden " name = " control " value = " move_pseudo_map " >
< ? php
}
elseif ( $monitor [ 'CanMoveCon' ] )
{
?>
< input type = " hidden " name = " control " value = " move_con_map " >
< ? php
}
?>
< input type = " hidden " name = " scale " value = " <?= $scale ?> " >
< input type = " image " src = " <?= $src ?> " border = " 0 " width = " <?= $width ?> " height = " <?= $height ?> " >
</ form >
< ? php
}
2005-01-19 09:50:24 +08:00
function getZmuCommand ( $args )
{
2005-02-24 07:46:35 +08:00
if ( version_compare ( phpversion (), " 4.1.0 " , " < " ) )
{
global $_SESSION ;
}
2005-01-19 09:50:24 +08:00
$zmu_command = ZMU_PATH ;
if ( ZM_OPT_USE_AUTH )
{
2005-10-20 23:42:41 +08:00
if ( ZM_AUTH_RELAY == " hashed " )
2005-01-19 09:50:24 +08:00
{
$zmu_command .= " -A " . authHash ( false );
}
2005-10-20 23:42:41 +08:00
elseif ( ZM_AUTH_RELAY == " plain " )
2005-01-19 09:50:24 +08:00
{
$zmu_command .= " -U " . $_SESSION [ 'username' ] . " -P " . $_SESSION [ 'password' ];
}
}
$zmu_command .= $args ;
return ( $zmu_command );
}
2003-06-30 04:53:55 +08:00
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 () );
2005-05-18 05:42:40 +08:00
system ( escapeshellcmd ( " rm -rf " . ZM_DIR_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
}
2004-12-29 02:20:11 +08:00
function makeLink ( $url , $label , $condition = 1 , $options = " " )
2003-06-26 19:12:00 +08:00
{
$string = " " ;
if ( $condition )
{
2004-12-29 02:20:11 +08:00
$string .= '<a href="' . $url . '"' . ( $options ? ( ' ' . $options ) : '' ) . '>' ;
2003-06-26 19:12:00 +08:00
}
$string .= $label ;
if ( $condition )
{
$string .= '</a>' ;
}
return ( $string );
}
2004-12-29 01:41:55 +08:00
function truncText ( $text , $length , $deslash = 1 )
{
return ( preg_replace ( " /^(. { " . $length . " ,}?) \ b.* $ / " , " \\ 1… " , ( $deslash ? stripslashes ( $text ) : $text ) ) );
}
2005-12-05 23:11:08 +08:00
function buildSelect ( $name , $contents , $behaviours = false )
2003-06-27 20:10:10 +08:00
{
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 ();
2005-12-05 23:11:08 +08:00
$behaviour_text = " " ;
2005-12-20 00:52:30 +08:00
if ( ! empty ( $behaviours ) )
2005-12-05 23:11:08 +08:00
{
if ( is_array ( $behaviours ) )
{
foreach ( $behaviours as $event => $action )
{
$behaviour_text .= ' ' . $event . '="' . $action . '"' ;
}
}
else
{
$behaviour_text = ' onChange="' . $behaviours . '"' ;
}
}
2003-06-27 20:10:10 +08:00
?>
2005-12-05 23:11:08 +08:00
< select name = " <?= $name ?> " class = " form " < ? = $behaviour_text ?> >
2003-06-27 20:10:10 +08:00
< ? php
foreach ( $contents as $content_value => $content_text )
{
?>
2005-10-20 01:51:34 +08:00
< option value = " <?= $content_value ?> " < ? php if ( $value == $content_value ) { echo " selected " ; } ?> ><?= htmlentities($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 );
}
2005-03-14 04:40:49 +08:00
function getFormChanges ( $values , $new_values , $types = false , $columns = false )
2003-10-28 01:07:04 +08:00
{
$changes = array ();
if ( ! $types )
$types = array ();
foreach ( $new_values as $key => $value )
{
2005-03-14 04:40:49 +08:00
if ( $columns && ! $columns [ $key ] )
continue ;
2003-10-28 01:07:04 +08:00
switch ( $types [ $key ] )
{
case 'set' :
{
if ( is_array ( $new_values [ $key ] ) )
{
if ( join ( ',' , $new_values [ $key ]) != $values [ $key ] )
{
2005-07-12 22:24:24 +08:00
$changes [ $key ] = " $key = ' " . join ( ',' , $new_values [ $key ]) . " ' " ;
2003-10-28 01:07:04 +08:00
}
}
elseif ( $values [ $key ] )
{
2005-07-12 22:24:24 +08:00
$changes [ $key ] = " $key = '' " ;
2003-10-28 01:07:04 +08:00
}
break ;
}
2005-03-14 04:40:49 +08:00
case 'image' :
{
if ( is_array ( $new_values [ $key ] ) )
{
$image_data = getimagesize ( $new_values [ $key ][ 'tmp_name' ] );
$changes [ $key . 'Width' ] = $key . " Width = " . $image_data [ 0 ];
$changes [ $key . 'Height' ] = $key . " Height = " . $image_data [ 1 ];
$changes [ $key . 'Type' ] = $key . " Type = ' " . $new_values [ $key ][ 'type' ] . " ' " ;
$changes [ $key . 'Size' ] = $key . " Size = " . $new_values [ $key ][ 'size' ];
ob_start ();
readfile ( $new_values [ $key ][ 'tmp_name' ] );
$changes [ $key ] = $key . " = ' " . addslashes ( ob_get_contents () ) . " ' " ;
ob_end_clean ();
}
else
{
2005-07-12 22:24:24 +08:00
$changes [ $key ] = " $key = ' $value ' " ;
2005-03-14 04:40:49 +08:00
}
break ;
}
case 'document' :
{
if ( is_array ( $new_values [ $key ] ) )
{
$image_data = getimagesize ( $new_values [ $key ][ 'tmp_name' ] );
$changes [ $key . 'Type' ] = $key . " Type = ' " . $new_values [ $key ][ 'type' ] . " ' " ;
$changes [ $key . 'Size' ] = $key . " Size = " . $new_values [ $key ][ 'size' ];
ob_start ();
readfile ( $new_values [ $key ][ 'tmp_name' ] );
$changes [ $key ] = $key . " = ' " . addslashes ( ob_get_contents () ) . " ' " ;
ob_end_clean ();
}
else
{
2005-07-12 22:24:24 +08:00
$changes [ $key ] = " $key = ' $value ' " ;
2005-03-14 04:40:49 +08:00
}
break ;
}
case 'file' :
{
$changes [ $key . 'Type' ] = $key . " Type = ' " . $new_values [ $key ][ 'type' ] . " ' " ;
$changes [ $key . 'Size' ] = $key . " Size = " . $new_values [ $key ][ 'size' ];
ob_start ();
readfile ( $new_values [ $key ][ 'tmp_name' ] );
$changes [ $key ] = $key . " = ' " . addslashes ( ob_get_contents () ) . " ' " ;
ob_end_clean ();
break ;
}
case 'raw' :
{
if ( $values [ $key ] != $value )
{
2005-07-12 22:24:24 +08:00
$changes [ $key ] = " $key = $value " ;
2005-03-14 04:40:49 +08:00
}
break ;
}
2003-10-28 01:07:04 +08:00
default :
{
if ( $values [ $key ] != $value )
{
2005-07-12 22:24:24 +08:00
$changes [ $key ] = " $key = ' $value ' " ;
2003-10-28 01:07:04 +08:00
}
break ;
}
}
}
2005-03-14 04:40:49 +08:00
foreach ( $values as $key => $value )
{
if ( $columns [ $key ] && $types [ $key ] == 'toggle' )
{
if ( ! isset ( $new_values [ $key ]) && ! empty ( $value ) )
{
2005-07-12 22:24:24 +08:00
$changes [ $key ] = " $key = 0 " ;
2005-03-14 04:40:49 +08:00
}
}
}
2003-10-28 01:07:04 +08:00
return ( $changes );
2003-06-27 20:10:10 +08:00
}
2002-10-11 17:45:06 +08:00
function getBrowser ( & $browser , & $version )
{
2005-02-24 07:46:35 +08:00
if ( version_compare ( phpversion (), " 4.1.0 " , " < " ) )
{
global $_SERVER ;
}
2002-10-11 17:45:06 +08:00
2005-01-17 00:08:35 +08:00
if ( ereg ( 'MSIE ([0-9].[0-9]{1,2})' , $_SERVER [ 'HTTP_USER_AGENT' ], $log_version ))
2002-10-11 17:45:06 +08:00
{
$version = $log_version [ 1 ];
$browser = 'ie' ;
}
2005-01-17 00:08:35 +08:00
elseif ( ereg ( 'Safari/([0-9.]+)' , $_SERVER [ 'HTTP_USER_AGENT' ], $log_version ))
2003-12-16 17:52:36 +08:00
{
$version = $log_version [ 1 ];
$browser = 'safari' ;
}
2005-01-17 00:08:35 +08:00
elseif ( ereg ( 'Opera ([0-9].[0-9]{1,2})' , $_SERVER [ 'HTTP_USER_AGENT' ], $log_version ))
2002-10-11 17:45:06 +08:00
{
$version = $log_version [ 1 ];
$browser = 'opera' ;
}
2005-01-17 00:08:35 +08:00
elseif ( ereg ( 'Mozilla/([0-9].[0-9]{1,2})' , $_SERVER [ '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 " );
}
2004-03-04 23:05:54 +08:00
function isInternetExplorer ()
{
getBrowser ( $browser , $version );
return ( $browser == " ie " );
}
function isWindows ()
{
2005-02-24 07:46:35 +08:00
if ( version_compare ( phpversion (), " 4.1.0 " , " < " ) )
{
global $_SERVER ;
}
2004-03-04 23:05:54 +08:00
2005-01-17 00:08:35 +08:00
return ( preg_match ( '/Win/' , $_SERVER [ 'HTTP_USER_AGENT' ] ) );
2004-03-04 23:05:54 +08:00
}
2004-01-27 17:57:12 +08:00
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 )) );
}
2002-10-11 17:45:06 +08:00
function canStream ()
{
2004-01-27 17:57:12 +08:00
return ( canStreamNative () | canStreamApplet () );
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 );
}
2005-12-23 00:47:19 +08:00
function zmcControl ( $monitor , $mode = 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-02-16 03:17:38 +08:00
$sql = " select count(if(Function!='None',1,NULL)) as ActiveCount from Monitors where Id = ' " . $monitor [ 'Id' ] . " ' " ;
$zmc_args = " -m " . $monitor [ 'Id' ];
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
{
2005-12-23 00:47:19 +08:00
if ( $mode == " restart " )
2002-12-24 19:56:01 +08:00
{
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
}
}
2005-12-23 00:47:19 +08:00
function zmaControl ( $monitor , $mode = 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
{
2005-12-23 00:47:19 +08:00
$sql = " select Id,Function,Enabled 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
}
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' :
2004-12-29 01:41:55 +08:00
case 'Nodect' :
2003-09-23 17:52:45 +08:00
{
2005-12-23 00:47:19 +08:00
if ( $mode == restart )
2003-09-23 17:52:45 +08:00
{
2005-02-24 18:43:29 +08:00
if ( ZM_OPT_CONTROL )
{
daemonControl ( " stop " , " zmtrack.pl " , " -m " . $monitor [ 'Id' ] );
}
2004-01-08 18:09:35 +08:00
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' ] );
2005-02-24 18:43:29 +08:00
if ( ZM_OPT_CONTROL && $monitor [ 'Controllable' ] && $monitor [ 'TrackMotion' ] && ( $monitor [ 'Function' ] == 'Modect' || $monitor [ 'Function' ] == 'Mocord' ) )
{
daemonControl ( " start " , " zmtrack.pl " , " -m " . $monitor [ 'Id' ] );
}
2005-12-23 00:47:19 +08:00
if ( $mode == " reload " )
{
daemonControl ( " reload " , " zma " , " -m " . $monitor [ 'Id' ] );
}
2003-09-23 17:52:45 +08:00
break ;
}
default :
2002-12-24 19:56:01 +08:00
{
2005-02-24 18:43:29 +08:00
if ( ZM_OPT_CONTROL )
{
daemonControl ( " stop " , " zmtrack.pl " , " -m " . $monitor [ 'Id' ] );
}
2004-01-08 18:09:35 +08:00
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
}
}
2005-10-02 00:13:48 +08:00
function initDaemonStatus ()
{
global $daemon_status ;
if ( ! $daemon_status )
{
$string = ZM_PATH_BIN . " /zmdc.pl status " ;
$daemon_status = shell_exec ( $string );
}
}
function daemonStatus ( $daemon , $args = false )
{
global $daemon_status ;
2005-11-22 23:18:57 +08:00
if ( daemonCheck () )
{
initDaemonStatus ();
}
else
{
$daemon_status = " " ;
}
2005-10-02 00:13:48 +08:00
2005-11-22 23:18:57 +08:00
$string = " $daemon " ;
2005-10-02 00:13:48 +08:00
if ( $args )
$string .= " $args " ;
return ( strpos ( $daemon_status , " ' $string ' running " ) !== false );
}
function zmcStatus ( $monitor )
{
if ( $monitor [ 'Type' ] == 'Local' )
{
$zmc_args = " -d " . $monitor [ 'Device' ];
}
else
{
$zmc_args = " -m " . $monitor [ 'Id' ];
}
return ( daemonStatus ( " zmc " , $zmc_args ) );
}
function zmaStatus ( $monitor )
{
if ( is_array ( $monitor ) )
{
$monitor = $monitor [ 'Id' ];
}
return ( daemonStatus ( " zma " , " -m $monitor " ) );
}
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-02-16 03:17:38 +08:00
$zmc_args = " -m " . $monitor [ 'Id' ];
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
2004-09-23 20:06:21 +08:00
function createListThumbnail ( $event , $overwrite = false )
{
$sql = " select * from Frames where EventId = ' " . $event [ 'Id' ] . " ' and Score = ' " . $event [ 'MaxScore' ] . " ' order by FrameId limit 0,1 " ;
if ( ! ( $result = mysql_query ( $sql )) )
die ( mysql_error () );
$frame = mysql_fetch_assoc ( $result );
$frame_id = $frame [ 'FrameId' ];
if ( ZM_WEB_LIST_THUMB_WIDTH )
{
$thumb_width = ZM_WEB_LIST_THUMB_WIDTH ;
$fraction = ZM_WEB_LIST_THUMB_WIDTH / $event [ 'Width' ];
$thumb_height = $event [ 'Height' ] * $fraction ;
}
elseif ( ZM_WEB_LIST_THUMB_HEIGHT )
{
$thumb_height = ZM_WEB_LIST_THUMB_HEIGHT ;
$fraction = ZM_WEB_LIST_THUMB_HEIGHT / $event [ 'Height' ];
$thumb_width = $event [ 'Width' ] * $fraction ;
}
else
{
die ( " No thumbnail width or height specified, please check in Options->Web " );
}
$event_path = ZM_DIR_EVENTS . '/' . $event [ 'MonitorId' ] . '/' . $event [ 'Id' ];
$image_path = sprintf ( " %s/%0 " . ZM_EVENT_IMAGE_DIGITS . " d-capture.jpg " , $event_path , $frame_id );
$capt_image = $image_path ;
if ( $scale == 1 || ! file_exists ( ZM_PATH_NETPBM . " /jpegtopnm " ) )
{
$anal_image = preg_replace ( " /capture/ " , " analyse " , $image_path );
if ( file_exists ( $anal_image ) && filesize ( $anal_image ) )
{
$thumb_image = $anal_image ;
}
else
{
$thumb_image = $capt_image ;
}
}
else
{
$thumb_image = preg_replace ( " /capture/ " , " mini " , $capt_image );
if ( ! file_exists ( $thumb_image ) || ! filesize ( $thumb_image ) )
{
$anal_image = preg_replace ( " /capture/ " , " analyse " , $capt_image );
if ( file_exists ( $anal_image ) )
$command = ZM_PATH_NETPBM . " /jpegtopnm -dct fast $anal_image | " . ZM_PATH_NETPBM . " /pnmscalefixed $fraction | " . ZM_PATH_NETPBM . " /ppmtojpeg --dct=fast > $thumb_image " ;
else
$command = ZM_PATH_NETPBM . " /jpegtopnm -dct fast $capt_image | " . ZM_PATH_NETPBM . " /pnmscalefixed $fraction | " . ZM_PATH_NETPBM . " /ppmtojpeg --dct=fast > $thumb_image " ;
#exec( escapeshellcmd( $command ) );
exec ( $command );
}
}
$thumb_data = $frame ;
$thumb_data [ 'Path' ] = $thumb_image ;
2005-05-16 23:16:36 +08:00
$thumb_data [ 'Width' ] = ( int ) $thumb_width ;
$thumb_data [ 'Height' ] = ( int ) $thumb_height ;
2004-09-23 20:06:21 +08:00
return ( $thumb_data );
}
2005-10-17 05:33:10 +08:00
function createVideo ( $event , $format , $rate , $scale , $overwrite = false )
2003-04-14 06:20:22 +08:00
{
2005-10-17 05:33:10 +08:00
$command = ZM_PATH_BIN . " /zmvideo.pl -e " . $event [ 'Id' ] . " -f " . $format . " -r " . sprintf ( " %.2f " , ( $rate / RATE_SCALE ) ) . " -s " . sprintf ( " %.2f " , ( $scale / SCALE_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
}
2004-05-05 19:22:50 +08:00
// Now deprecated
2004-02-19 18:48:33 +08:00
function createImage ( $monitor , $scale )
{
if ( is_array ( $monitor ) )
{
$monitor = $monitor [ 'Id' ];
}
chdir ( ZM_DIR_IMAGES );
2005-01-19 09:50:24 +08:00
$command = getZmuCommand ( " -m $monitor -i " );
2004-02-19 18:48:33 +08:00
if ( ! empty ( $scale ) && $scale < 100 )
$command .= " -S $scale " ;
$status = exec ( escapeshellcmd ( $command ) );
chdir ( '..' );
return ( $status );
}
2005-10-20 00:52:28 +08:00
function reScale ( $dimension , $dummy )
2003-10-19 17:47:32 +08:00
{
2005-10-20 00:52:28 +08:00
for ( $i = 1 ; $i < func_num_args (); $i ++ )
{
$scale = func_get_arg ( $i );
if ( ! empty ( $scale ) && $scale != SCALE_SCALE )
$dimension = ( int )(( $dimension * $scale ) / SCALE_SCALE );
}
return ( $dimension );
2003-10-19 17:47:32 +08:00
}
2005-10-20 00:52:28 +08:00
function deScale ( $dimension , $dummy )
2005-02-24 18:43:29 +08:00
{
2005-10-20 00:52:28 +08:00
for ( $i = 1 ; $i < func_num_args (); $i ++ )
{
$scale = func_get_arg ( $i );
if ( ! empty ( $scale ) && $scale != SCALE_SCALE )
$dimension = ( int )(( $dimension * SCALE_SCALE ) / $scale );
}
return ( $dimension );
2005-02-24 18:43:29 +08:00
}
2005-11-09 01:57:41 +08:00
function parseSort ( $save_to_session = false , $term_sep = '&' )
2004-03-10 22:23:22 +08:00
{
global $sort_field , $sort_asc ; // Inputs
global $sort_query , $sort_column , $sort_order ; // Outputs
2005-02-24 07:46:35 +08:00
if ( version_compare ( phpversion (), " 4.1.0 " , " < " ) )
{
global $_SESSION ;
}
2004-03-10 22:23:22 +08:00
if ( ! isset ( $sort_field ) )
{
2005-10-20 01:51:34 +08:00
$sort_field = ZM_EVENT_SORT_FIELD ;
$sort_asc = ( ZM_EVENT_SORT_ORDER == " asc " );
2004-03-10 22:23:22 +08:00
}
switch ( $sort_field )
{
case 'Id' :
$sort_column = " E.Id " ;
break ;
case 'MonitorName' :
$sort_column = " M.Name " ;
break ;
case 'Name' :
$sort_column = " E.Name " ;
break ;
2004-12-29 01:41:55 +08:00
case 'Cause' :
$sort_column = " E.Cause " ;
break ;
2004-09-24 06:21:05 +08:00
case 'DateTime' :
2004-12-29 01:41:55 +08:00
case 'StartTime' :
2004-03-10 22:23:22 +08:00
$sort_column = " E.StartTime " ;
break ;
2004-09-24 06:21:05 +08:00
case 'Length' :
2004-03-10 22:23:22 +08:00
$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 ;
2005-11-09 01:57:41 +08:00
$sort_query = $term_sep . " sort_field= " . $sort_field . $term_sep . " sort_asc= " . $sort_asc ;
2004-12-29 02:20:11 +08:00
if ( $save_to_session )
{
2005-01-17 00:08:35 +08:00
$_SESSION [ 'sort_field' ] = $sort_field ;
$_SESSION [ 'sort_asc' ] = $sort_asc ;
2004-12-29 02:20:11 +08:00
}
2004-03-10 22:23:22 +08:00
}
2005-11-09 01:57:41 +08:00
function parseFilter ( $save_to_session = false , $term_sep = '&' )
2004-03-10 22:23:22 +08:00
{
2004-12-29 02:20:11 +08:00
global $trms ; // Inputs
global $filter_query , $filter_sql , $filter_fields ; // Outputs
2005-02-24 07:46:35 +08:00
if ( version_compare ( phpversion (), " 4.1.0 " , " < " ) )
{
global $_SESSION ;
}
2004-03-10 22:23:22 +08:00
$filter_query = '' ;
$filter_sql = '' ;
$filter_fields = '' ;
if ( $trms )
{
2004-12-29 02:20:11 +08:00
if ( $save_to_session )
{
2005-01-17 00:08:35 +08:00
$_SESSION [ 'trms' ] = $trms ;
2004-12-29 02:20:11 +08:00
}
2005-11-09 01:57:41 +08:00
$filter_query .= $term_sep . " trms= " . $trms ;
2004-12-29 02:20:11 +08:00
$filter_fields .= '<input type="hidden" name="trms" value="' . $trms . '"/>' . " \n " ;
2004-03-10 22:23:22 +08:00
2004-04-21 17:20:28 +08:00
for ( $i = 1 ; $i <= $trms ; $i ++ )
2004-03-10 22:23:22 +08:00
{
2004-04-21 17:20:28 +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 " ;
global $$conjunction_name , $$obracket_name , $$cbracket_name , $$attr_name , $$op_name , $$value_name ;
if ( isset ( $$conjunction_name ) )
{
2005-11-09 01:57:41 +08:00
$filter_query .= $term_sep . $conjunction_name . " = " . $$conjunction_name ;
2004-04-21 17:20:28 +08:00
$filter_sql .= " " . $$conjunction_name . " " ;
2004-12-29 02:20:11 +08:00
$filter_fields .= '<input type="hidden" name="' . $conjunction_name . '" value="' . $$conjunction_name . '"/>' . " \n " ;
if ( $save_to_session )
{
2005-01-17 00:08:35 +08:00
$_SESSION [ $conjunction_name ] = $$conjunction_name ;
2004-12-29 02:20:11 +08:00
}
2004-04-21 17:20:28 +08:00
}
if ( isset ( $$obracket_name ) )
2004-03-10 22:23:22 +08:00
{
2005-11-09 01:57:41 +08:00
$filter_query .= $term_sep . $obracket_name . " = " . $$obracket_name ;
2004-04-21 17:20:28 +08:00
$filter_sql .= str_repeat ( " ( " , $$obracket_name );
2004-12-29 02:20:11 +08:00
$filter_fields .= '<input type="hidden" name="' . $obracket_name . '" value="' . $$obracket_name . '"/>' . " \n " ;
if ( $save_to_session )
{
2005-01-17 00:08:35 +08:00
$_SESSION [ $obracket_name ] = $$obracket_name ;
2004-12-29 02:20:11 +08:00
}
2004-03-10 22:23:22 +08:00
}
2004-04-21 17:20:28 +08:00
if ( isset ( $$attr_name ) )
2004-03-22 17:57:02 +08:00
{
2005-11-09 01:57:41 +08:00
$filter_query .= $term_sep . $attr_name . " = " . $$attr_name ;
2004-12-29 02:20:11 +08:00
$filter_fields .= '<input type="hidden" name="' . $attr_name . '" value="' . $$attr_name . '"/>' . " \n " ;
2004-03-22 17:57:02 +08:00
switch ( $$attr_name )
{
case 'MonitorName' :
2004-04-21 17:20:28 +08:00
$filter_sql .= 'M.' . preg_replace ( '/^Monitor/' , '' , $$attr_name );
2004-03-22 17:57:02 +08:00
break ;
case 'DateTime' :
2004-04-21 17:20:28 +08:00
$filter_sql .= " E.StartTime " ;
2004-03-22 17:57:02 +08:00
break ;
case 'Date' :
2004-04-21 17:20:28 +08:00
$filter_sql .= " to_days( E.StartTime ) " ;
2004-03-22 17:57:02 +08:00
break ;
case 'Time' :
2004-04-21 17:20:28 +08:00
$filter_sql .= " extract( hour_second from E.StartTime ) " ;
2004-03-22 17:57:02 +08:00
break ;
case 'Weekday' :
2004-04-21 17:20:28 +08:00
$filter_sql .= " weekday( E.StartTime ) " ;
break ;
2004-09-24 06:21:05 +08:00
case 'Id' :
case 'Name' :
2004-04-21 17:20:28 +08:00
case 'MonitorId' :
case 'Length' :
case 'Frames' :
case 'AlarmFrames' :
case 'TotScore' :
case 'AvgScore' :
case 'MaxScore' :
2005-11-20 20:16:42 +08:00
case 'Cause' :
case 'Notes' :
2004-04-21 17:20:28 +08:00
case 'Archived' :
2005-11-04 23:39:18 +08:00
$filter_sql .= " E. " . $$attr_name ;
2004-04-21 17:20:28 +08:00
break ;
case 'DiskPercent' :
$filter_sql .= getDiskPercent ();
break ;
case 'DiskBlocks' :
$filter_sql .= getDiskBlocks ();
2004-03-22 17:57:02 +08:00
break ;
}
2004-04-21 17:20:28 +08:00
$value_list = array ();
foreach ( preg_split ( '/["\'\s]*?,["\'\s]*?/' , preg_replace ( '/^["\']+?(.+)["\']+?$/' , '$1' , $$value_name ) ) as $value )
{
switch ( $$attr_name )
{
case 'MonitorName' :
2004-12-29 01:41:55 +08:00
case 'Name' :
case 'Cause' :
2005-11-20 20:16:42 +08:00
case 'Notes' :
2004-04-21 17:20:28 +08:00
$value = " ' $value ' " ;
break ;
case 'DateTime' :
$value = " ' " . strftime ( " %Y-%m-%d %H:%M:%S " , strtotime ( $value ) ) . " ' " ;
break ;
case 'Date' :
$value = " to_days( ' " . strftime ( " %Y-%m-%d %H:%M:%S " , strtotime ( $value ) ) . " ' ) " ;
break ;
case 'Time' :
$value = " extract( hour_second from ' " . strftime ( " %Y-%m-%d %H:%M:%S " , strtotime ( $value ) ) . " ' ) " ;
break ;
case 'Weekday' :
$value = " weekday( ' " . strftime ( " %Y-%m-%d %H:%M:%S " , strtotime ( $value ) ) . " ' ) " ;
break ;
}
$value_list [] = $value ;
}
2004-03-10 22:23:22 +08:00
2004-04-21 17:20:28 +08:00
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 ( " , " , $value_list ) . " ) " ;
break ;
case '![]' :
$filter_sql .= " not in ( " . join ( " , " , $value_list ) . " ) " ;
break ;
}
2005-11-09 01:57:41 +08:00
$filter_query .= $term_sep . $op_name . " = " . urlencode ( $$op_name );
2004-12-29 02:20:11 +08:00
$filter_fields .= '<input type="hidden" name="' . $op_name . '" value="' . $$op_name . '"/>' . " \n " ;
2005-11-09 01:57:41 +08:00
$filter_query .= $term_sep . $value_name . " = " . urlencode ( $$value_name );
2004-12-29 02:20:11 +08:00
$filter_fields .= '<input type="hidden" name="' . $value_name . '" value="' . $$value_name . '"/>' . " \n " ;
if ( $save_to_session )
{
2005-01-17 00:08:35 +08:00
$_SESSION [ $attr_name ] = $$attr_name ;
$_SESSION [ $op_name ] = $$op_name ;
$_SESSION [ $value_name ] = $$value_name ;
2004-12-29 02:20:11 +08:00
}
2004-04-21 17:20:28 +08:00
}
if ( isset ( $$cbracket_name ) )
2004-03-10 22:23:22 +08:00
{
2005-11-09 01:57:41 +08:00
$filter_query .= $term_sep . $cbracket_name . " = " . $$cbracket_name ;
2004-04-21 17:20:28 +08:00
$filter_sql .= str_repeat ( " ) " , $$cbracket_name );
2004-12-29 02:20:11 +08:00
$filter_fields .= '<input type="hidden" name="' . $cbracket_name . '" value="' . $$cbracket_name . '"/>' . " \n " ;
if ( $save_to_session )
{
2005-01-17 00:08:35 +08:00
$_SESSION [ $cbracket_name ] = $$cbracket_name ;
2004-12-29 02:20:11 +08:00
}
2004-03-10 22:23:22 +08:00
}
}
2004-04-21 17:20:28 +08:00
$filter_sql = " and ( $filter_sql ) " ;
2004-03-10 22:23:22 +08:00
}
}
2004-03-18 06:54:29 +08:00
function getLoad ()
{
$uptime = shell_exec ( 'uptime' );
$load = '' ;
if ( preg_match ( '/load average: ([\d.]+)/' , $uptime , $matches ) )
$load = $matches [ 1 ];
return ( $load );
}
2004-04-21 17:20:28 +08:00
function getDiskPercent ()
2004-03-18 06:54:29 +08:00
{
$df = shell_exec ( 'df ' . ZM_DIR_EVENTS );
2004-04-21 17:20:28 +08:00
$space = - 1 ;
if ( preg_match ( '/\s(\d+)%/ms' , $df , $matches ) )
$space = $matches [ 1 ];
return ( $space );
}
function getDiskBlocks ()
{
$df = shell_exec ( 'df ' . ZM_DIR_EVENTS );
$space = - 1 ;
if ( preg_match ( '/\s(\d+)\s+\d+\s+\d+%/ms' , $df , $matches ) )
2004-03-18 06:54:29 +08:00
$space = $matches [ 1 ];
return ( $space );
}
2004-12-29 02:20:11 +08:00
// Function to fix a problem whereby the built in PHP session handling
// features want to put the sid as a hidden field after the form or
// fieldset tag, neither of which will work with strict XHTML Basic.
function sidField ()
{
if ( SID )
{
list ( $sessname , $sessid ) = split ( " = " , SID );
?>
< input type = " hidden " name = " <?= $sessname ?> " value = " <?= $sessid ?> " />
< ? php
}
}
2005-06-13 00:52:03 +08:00
function verNum ( $version )
{
$vNum = " " ;
$maxFields = 3 ;
$vFields = explode ( " . " , $version );
array_splice ( $vFields , $maxFields );
while ( count ( $vFields ) < $maxFields )
{
$vFields [] = 0 ;
}
foreach ( $vFields as $vField )
{
$vField = sprintf ( " %02d " , $vField );
while ( strlen ( $vField ) < 2 )
{
$vField = " 0 " . $vField ;
}
$vNum .= $vField ;
}
return ( $vNum );
}
2005-10-17 18:12:53 +08:00
function fixSequences ()
{
$sql = " select * from Monitors order by Sequence asc, Id asc " ;
$result = mysql_query ( $sql );
if ( ! $result )
echo mysql_error ();
$sequence = 1 ;
while ( $monitor = mysql_fetch_assoc ( $result ) )
{
if ( $monitor [ 'Sequence' ] != $sequence )
{
$sql2 = " update Monitors set Sequence = ' " . $sequence . " ' where Id = ' " . $monitor [ 'Id' ] . " ' " ;
$result2 = mysql_query ( $sql2 );
if ( ! $result2 )
echo mysql_error ();
}
$sequence ++ ;
}
}
2005-10-20 00:52:28 +08:00
function firstSet ()
{
foreach ( func_get_args () as $arg )
{
2005-11-30 01:51:06 +08:00
if ( ! empty ( $arg ) )
return ( $arg );
2005-10-20 00:52:28 +08:00
}
}
2005-12-05 20:18:39 +08:00
function linesIntersect ( $line1 , $line2 )
{
global $debug ;
$min_x1 = min ( $line1 [ 0 ][ 'x' ], $line1 [ 1 ][ 'x' ] );
$max_x1 = max ( $line1 [ 0 ][ 'x' ], $line1 [ 1 ][ 'x' ] );
$min_x2 = min ( $line2 [ 0 ][ 'x' ], $line2 [ 1 ][ 'x' ] );
$max_x2 = max ( $line2 [ 0 ][ 'x' ], $line2 [ 1 ][ 'x' ] );
$min_y1 = min ( $line1 [ 0 ][ 'y' ], $line1 [ 1 ][ 'y' ] );
$max_y1 = max ( $line1 [ 0 ][ 'y' ], $line1 [ 1 ][ 'y' ] );
$min_y2 = min ( $line2 [ 0 ][ 'y' ], $line2 [ 1 ][ 'y' ] );
$max_y2 = max ( $line2 [ 0 ][ 'y' ], $line2 [ 1 ][ 'y' ] );
// Checking if bounding boxes intersect
if ( $max_x1 < $min_x2 || $max_x2 < $min_x1 || $max_y1 < $min_y2 || $max_y2 < $min_y1 )
{
if ( $debug ) echo " Not intersecting, out of bounds<br> " ;
return ( false );
}
$dx1 = $line1 [ 1 ][ 'x' ] - $line1 [ 0 ][ 'x' ];
$dy1 = $line1 [ 1 ][ 'y' ] - $line1 [ 0 ][ 'y' ];
$dx2 = $line2 [ 1 ][ 'x' ] - $line2 [ 0 ][ 'x' ];
$dy2 = $line2 [ 1 ][ 'y' ] - $line2 [ 0 ][ 'y' ];
if ( $dx1 )
{
$m1 = $dy1 / $dx1 ;
$b1 = $line1 [ 0 ][ 'y' ] - ( $m1 * $line1 [ 0 ][ 'x' ]);
}
else
{
$b1 = $line1 [ 0 ][ 'y' ];
}
if ( $dx2 )
{
$m2 = $dy2 / $dx2 ;
$b2 = $line2 [ 0 ][ 'y' ] - ( $m2 * $line2 [ 0 ][ 'x' ]);
}
else
{
$b2 = $line2 [ 0 ][ 'y' ];
}
if ( $dx1 && $dx2 ) // Both not vertical
{
2005-12-08 19:10:44 +08:00
if ( $m1 != $m2 ) // Not parallel or colinear
2005-12-05 20:18:39 +08:00
{
2005-12-08 19:10:44 +08:00
$x = ( $b2 - $b1 ) / ( $m1 - $m2 );
2005-12-05 20:18:39 +08:00
2005-12-08 19:10:44 +08:00
if ( $x >= $min_x1 && $x <= $max_x1 && $x >= $min_x2 && $x <= $max_x2 )
2005-12-05 20:18:39 +08:00
{
2005-12-08 19:10:44 +08:00
if ( $debug ) echo " Intersecting, at x $x <br> " ;
2005-12-05 20:18:39 +08:00
return ( true );
}
else
{
2005-12-08 19:10:44 +08:00
if ( $debug ) echo " Not intersecting, out of range at x $x <br> " ;
2005-12-05 20:18:39 +08:00
return ( false );
}
}
2005-12-08 19:10:44 +08:00
elseif ( $b1 == $b2 )
{
// Colinear, must overlap due to box check, intersect?
if ( $debug ) echo " Intersecting, colinear<br> " ;
return ( true );
}
else
{
// Parallel
if ( $debug ) echo " Not intersecting, parallel<br> " ;
return ( false );
}
2005-12-05 20:18:39 +08:00
}
elseif ( ! $dx1 ) // Line 1 is vertical
{
$y = ( $m2 * $line1 [ 0 ][ 'x' ] ) * $b2 ;
if ( $y >= $min_y1 && $y <= $max_y1 )
{
if ( $debug ) echo " Intersecting, at y $y <br> " ;
return ( true );
}
else
{
if ( $debug ) echo " Not intersecting, out of range at y $y <br> " ;
return ( false );
}
}
elseif ( ! $dx2 ) // Line 2 is vertical
{
$y = ( $m1 * $line2 [ 0 ][ 'x' ] ) * $b1 ;
if ( $y >= $min_y2 && $y <= $max_y2 )
{
if ( $debug ) echo " Intersecting, at y $y <br> " ;
return ( true );
}
else
{
if ( $debug ) echo " Not intersecting, out of range at y $y <br> " ;
return ( false );
}
}
else // Both lines are vertical
{
if ( $line1 [ 0 ][ 'x' ] == $line2 [ 0 ][ 'x' ] )
{
// Colinear, must overlap due to box check, intersect?
if ( $debug ) echo " Intersecting, vertical, colinear<br> " ;
return ( true );
}
else
{
// Parallel
if ( $debug ) echo " Not intersecting, vertical, parallel<br> " ;
return ( false );
}
}
if ( $debug ) echo " Whoops, unexpected scenario<br> " ;
return ( false );
}
function isSelfIntersecting ( $points )
{
global $debug ;
$n_coords = count ( $points );
$edges = array ();
for ( $j = 0 , $i = $n_coords - 1 ; $j < $n_coords ; $i = $j ++ )
{
$edges [] = array ( $points [ $i ], $points [ $j ] );
}
for ( $i = 0 ; $i <= ( $n_coords - 2 ); $i ++ )
{
for ( $j = $i + 2 ; $j < $n_coords + min ( 0 , $i - 1 ); $j ++ )
{
if ( $debug ) echo " Checking $i and $j <br> " ;
if ( linesIntersect ( $edges [ $i ], $edges [ $j ] ) )
{
if ( $debug ) echo " Lines $i and $j intersect<br> " ;
return ( true );
}
}
}
return ( false );
}
2005-11-30 01:51:06 +08:00
function getPolyCentre ( $points , $area = 0 )
{
$cx = 0.0 ;
$cy = 0.0 ;
if ( ! $area )
$area = getPolyArea ( $points );
for ( $i = 0 , $j = count ( $points ) - 1 ; $i < count ( $points ); $j = $i ++ )
{
$ct = ( $points [ $i ][ 'x' ] * $points [ $j ][ 'y' ]) - ( $points [ $j ][ 'x' ] * $points [ $i ][ 'y' ]);
$cx += ( $points [ $i ][ 'x' ] + $points [ $j ][ 'x' ]) * ct ;
$cy += ( $points [ $i ][ 'y' ] + $points [ $j ][ 'y' ]) * ct ;
}
$cx = intval ( round ( abs ( $cx / ( 6.0 * $area ))));
$cy = intval ( round ( abs ( $cy / ( 6.0 * $area ))));
printf ( " X:%cx, Y: $cy <br> " );
return ( array ( 'x' => $cx , 'y' => $cy ) );
}
function _CompareXY ( $a , $b )
{
if ( $a [ 'min_y' ] == $b [ 'min_y' ] )
return ( intval ( $a [ 'min_x' ] - $b [ 'min_x' ]) );
else
return ( intval ( $a [ 'min_y' ] - $b [ 'min_y' ]) );
}
function _CompareX ( $a , $b )
{
return ( intval ( $a [ 'min_x' ] - $b [ 'min_x' ]) );
}
function getPolyArea ( $points )
{
//error_reporting( E_ALL );
2005-12-05 20:18:39 +08:00
global $debug ;
2005-11-30 01:51:06 +08:00
$n_coords = count ( $points );
$global_edges = array ();
for ( $j = 0 , $i = $n_coords - 1 ; $j < $n_coords ; $i = $j ++ )
{
$x1 = $points [ $i ][ 'x' ];
$x2 = $points [ $j ][ 'x' ];
$y1 = $points [ $i ][ 'y' ];
$y2 = $points [ $j ][ 'y' ];
//printf( "x1:%d,y1:%d x2:%d,y2:%d\n", x1, y1, x2, y2 );
if ( $y1 == $y2 )
continue ;
$dx = $x2 - $x1 ;
$dy = $y2 - $y1 ;
$global_edges [] = array (
" min_y " => $y1 < $y2 ? $y1 : $y2 ,
2005-12-05 20:18:39 +08:00
" max_y " => ( $y1 < $y2 ? $y2 : $y1 ) + 1 ,
2005-11-30 01:51:06 +08:00
" min_x " => $y1 < $y2 ? $x1 : $x2 ,
" _1_m " => $dx / $dy ,
);
}
usort ( $global_edges , " _CompareXY " );
if ( $debug )
{
for ( $i = 0 ; $i < count ( $global_edges ); $i ++ )
{
printf ( " %d: min_y: %d, max_y:%d, min_x:%.2f, 1/m:%.2f<br> " , $i , $global_edges [ $i ][ 'min_y' ], $global_edges [ $i ][ 'max_y' ], $global_edges [ $i ][ 'min_x' ], $global_edges [ $i ][ '_1_m' ] );
}
}
$area = 0.0 ;
$active_edges = array ();
$y = $global_edges [ 0 ][ 'min_y' ];
do
{
for ( $i = 0 ; $i < count ( $global_edges ); $i ++ )
{
if ( $global_edges [ $i ][ 'min_y' ] == $y )
{
if ( $debug ) printf ( " Moving global edge<br> " );
$active_edges [] = $global_edges [ $i ];
array_splice ( $global_edges , $i , 1 );
$i -- ;
}
else
{
break ;
}
}
usort ( $active_edges , " _CompareX " );
if ( $debug )
{
for ( $i = 0 ; $i < count ( $active_edges ); $i ++ )
{
printf ( " %d - %d: min_y: %d, max_y:%d, min_x:%.2f, 1/m:%.2f<br> " , $y , $i , $active_edges [ $i ][ 'min_y' ], $active_edges [ $i ][ 'max_y' ], $active_edges [ $i ][ 'min_x' ], $active_edges [ $i ][ '_1_m' ] );
}
}
2005-12-05 20:18:39 +08:00
$last_x = 0 ;
$row_area = 0 ;
$parity = false ;
for ( $i = 0 ; $i < count ( $active_edges ); $i ++ )
2005-11-30 01:51:06 +08:00
{
2005-12-05 20:18:39 +08:00
$x = intval ( round ( $active_edges [ $i ][ 'min_x' ]));
if ( $parity )
{
$row_area += ( $x - $last_x ) + 1 ;
$area += $row_area ;
}
if ( $active_edges [ $i ][ 'max_y' ] != $y )
$parity = ! $parity ;
$last_x = $x ;
2005-11-30 01:51:06 +08:00
}
2005-12-05 20:18:39 +08:00
if ( $debug ) printf ( " %d: Area:%d<br> " , $y , $row_area );
2005-11-30 01:51:06 +08:00
$y ++ ;
for ( $i = 0 ; $i < count ( $active_edges ); $i ++ )
{
2005-12-05 20:18:39 +08:00
if ( $y >= $active_edges [ $i ][ 'max_y' ] ) // Or >= as per sheets
2005-11-30 01:51:06 +08:00
{
if ( $debug ) printf ( " Deleting active_edge<br> " );
array_splice ( $active_edges , $i , 1 );
$i -- ;
}
else
{
$active_edges [ $i ][ 'min_x' ] += $active_edges [ $i ][ '_1_m' ];
}
}
} while ( count ( $global_edges ) || count ( $active_edges ) );
if ( $debug ) printf ( " Area:%d<br> " , $area );
return ( $area );
}
function getPolyAreaOld ( $points )
{
$area = 0.0 ;
$edge = 0.0 ;
for ( $i = 0 , $j = count ( $points ) - 1 ; $i < count ( $points ); $j = $i ++ )
{
$x_diff = ( $points [ $i ][ 'x' ] - $points [ $j ][ 'x' ]);
$y_diff = ( $points [ $i ][ 'y' ] - $points [ $j ][ 'y' ]);
$y_sum = ( $points [ $i ][ 'y' ] + $points [ $j ][ 'y' ]);
$trap_edge = sqrt ( pow ( abs ( $x_diff ) + 1 , 2 ) + pow ( abs ( $y_diff ) + 1 , 2 ) );
$edge += $trap_edge ;
$trap_area = ( $x_diff * $y_sum );
$area += $trap_area ;
printf ( " %d->%d, %d-%d=%.2f, %d+%d=%.2f(%.2f), %.2f, %.2f<br> " , i , j , $points [ $i ][ 'x' ], $points [ $j ][ 'x' ], $x_diff , $points [ $i ][ 'y' ], $points [ $j ][ 'y' ], $y_sum , $y_diff , $trap_area , $trap_edge );
}
$edge = intval ( round ( abs ( $edge )));
$area = intval ( round (( abs ( $area ) + $edge ) / 2 ));
echo " E: $edge <br> " ;
echo " A: $area <br> " ;
return ( $area );
}
function mapCoords ( $a )
{
return ( $a [ 'x' ] . " , " . $a [ 'y' ] );
}
function pointsToCoords ( $points )
{
return ( join ( " " , array_map ( " mapCoords " , $points ) ) );
}
function coordsToPoints ( $coords )
{
$points = array ();
if ( preg_match_all ( '/(\d+,\d+)+/' , $coords , $matches ) )
{
for ( $i = 0 ; $i < count ( $matches [ 1 ]); $i ++ )
{
if ( preg_match ( '/(\d+),(\d+)/' , $matches [ 1 ][ $i ], $cmatches ) )
{
$points [] = array ( 'x' => $cmatches [ 1 ], 'y' => $cmatches [ 2 ] );
}
else
{
echo ( " Bogus coordinates ' " . $matches [ $i ] . " ' " );
return ( false );
}
}
}
else
{
echo ( " Bogus coordinate string ' $coords ' " );
return ( false );
}
return ( $points );
}
2002-10-11 17:45:06 +08:00
?>