2013-05-02 22:20:06 +08:00
< ? php
//
// ZoneMinder web function library, $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.
//
// Compatibility functions
2016-04-02 01:02:31 +08:00
if ( version_compare ( phpversion (), " 4.3.0 " , " < " ) ) {
function ob_get_clean () {
$buffer = ob_get_contents ();
ob_end_clean ();
return ( $buffer );
}
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
function userLogin ( $username , $password = " " , $passwordHashed = false ) {
global $user , $cookies ;
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
$sql = " select * from Users where Enabled = 1 " ;
$sql_values = NULL ;
if ( ZM_AUTH_TYPE == " builtin " ) {
if ( $passwordHashed ) {
$sql .= " AND Username=? AND Password=? " ;
2013-11-05 01:43:34 +08:00
} else {
2016-04-02 01:02:31 +08:00
$sql .= " AND Username=? AND Password=password(?) " ;
}
$sql_values = array ( $username , $password );
} else {
$sql .= " AND Username = ? " ;
$sql_values = array ( $username );
}
$_SESSION [ 'username' ] = $username ;
if ( ZM_AUTH_RELAY == " plain " ) {
// Need to save this in session
$_SESSION [ 'password' ] = $password ;
}
$_SESSION [ 'remoteAddr' ] = $_SERVER [ 'REMOTE_ADDR' ]; // To help prevent session hijacking
if ( $dbUser = dbFetchOne ( $sql , NULL , $sql_values ) ) {
Info ( " Login successful for user \" $username\ " " );
$_SESSION [ 'user' ] = $user = $dbUser ;
if ( ZM_AUTH_TYPE == " builtin " ) {
$_SESSION [ 'passwordHash' ] = $user [ 'Password' ];
}
} else {
Warning ( " Login denied for user \" $username\ " " );
unset ( $user );
}
if ( $cookies )
session_write_close ();
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
function userLogout () {
global $user ;
$username = $user [ 'Username' ];
2015-08-18 06:54:17 +08:00
2016-04-02 01:02:31 +08:00
Info ( " User \" $username\ " logged out " );
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
unset ( $_SESSION [ 'user' ] );
unset ( $user );
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
session_destroy ();
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
function noCacheHeaders () {
header ( " Expires: Mon, 26 Jul 1997 05:00:00 GMT " ); // Date in the past
header ( " Last-Modified: " . gmdate ( " D, d M Y H:i:s " ) . " GMT " ); // always modified
header ( " Cache-Control: no-store, no-cache, must-revalidate " ); // HTTP/1.1
header ( " Cache-Control: post-check=0, pre-check=0 " , false );
header ( " Pragma: no-cache " ); // HTTP/1.0
2013-05-02 22:20:06 +08:00
}
2015-12-02 23:05:03 +08:00
function CORSHeaders () {
2016-04-02 01:02:31 +08:00
if ( isset ( $_SERVER [ 'HTTP_ORIGIN' ] ) ) {
# The following is left for future reference/use.
$valid = false ;
$servers = dbFetchAll ( 'SELECT * FROM Servers' );
if ( sizeof ( $servers ) <= 1 ) {
# Only need CORSHeaders in the event that there are multiple servers in use.
return ;
}
foreach ( dbFetchAll ( 'SELECT * FROM Servers' ) as $row ) {
$Server = new Server ( $row );
if ( $_SERVER [ 'HTTP_ORIGIN' ] == $Server -> Url () ) {
$valid = true ;
header ( " Access-Control-Allow-Origin: " . $Server -> Url () );
header ( " Access-Control-Allow-Headers: x-requested-with,x-request " );
}
}
if ( ! $valid ) {
Warning ( $_SERVER [ 'HTTP_ORIGIN' ] . " is not found in servers list. " );
}
}
}
function getAuthUser ( $auth ) {
2016-05-12 22:17:41 +08:00
if ( ZM_OPT_USE_AUTH && ZM_AUTH_RELAY == " hashed " && ! empty ( $auth ) ) {
2016-04-02 01:02:31 +08:00
$remoteAddr = " " ;
2016-05-12 22:17:41 +08:00
if ( ZM_AUTH_HASH_IPS ) {
2016-04-02 01:02:31 +08:00
$remoteAddr = $_SERVER [ 'REMOTE_ADDR' ];
2016-05-12 22:17:41 +08:00
if ( ! $remoteAddr ) {
2016-04-02 01:02:31 +08:00
Error ( " Can't determine remote address for authentication, using empty string " );
2013-05-02 22:20:06 +08:00
$remoteAddr = " " ;
2016-04-02 01:02:31 +08:00
}
}
$sql = " select Username, Password, Enabled, Stream+0, Events+0, Control+0, Monitors+0, System+0, MonitorIds from Users where Enabled = 1 " ;
2016-05-12 22:17:41 +08:00
foreach ( dbFetchAll ( $sql ) as $user ) {
2016-04-02 01:02:31 +08:00
$now = time ();
2016-05-12 22:17:41 +08:00
for ( $i = 0 ; $i < 2 ; $i ++ , $now -= ( 60 * 60 ) ) { // Try for last two hours
2016-04-02 01:02:31 +08:00
$time = localtime ( $now );
$authKey = ZM_AUTH_HASH_SECRET . $user [ 'Username' ] . $user [ 'Password' ] . $remoteAddr . $time [ 2 ] . $time [ 3 ] . $time [ 4 ] . $time [ 5 ];
$authHash = md5 ( $authKey );
2013-05-02 22:20:06 +08:00
2016-05-12 22:17:41 +08:00
if ( $auth == $authHash ) {
2016-04-02 01:02:31 +08:00
return ( $user );
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
Error ( " Unable to authenticate user from auth hash ' $auth ' " );
return ( false );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function generateAuthHash ( $useRemoteAddr ) {
if ( ZM_OPT_USE_AUTH && ZM_AUTH_RELAY == " hashed " ) {
2016-04-02 01:02:31 +08:00
$time = localtime ();
2016-05-12 22:17:41 +08:00
if ( $useRemoteAddr ) {
2016-04-02 01:02:31 +08:00
$authKey = ZM_AUTH_HASH_SECRET . $_SESSION [ 'username' ] . $_SESSION [ 'passwordHash' ] . $_SESSION [ 'remoteAddr' ] . $time [ 2 ] . $time [ 3 ] . $time [ 4 ] . $time [ 5 ];
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
$authKey = ZM_AUTH_HASH_SECRET . $_SESSION [ 'username' ] . $_SESSION [ 'passwordHash' ] . $time [ 2 ] . $time [ 3 ] . $time [ 4 ] . $time [ 5 ];
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
$auth = md5 ( $authKey );
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
$auth = " " ;
}
return ( $auth );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function getStreamSrc ( $args , $querySep = '&' ) {
2016-04-02 01:02:31 +08:00
$streamSrc = ZM_BASE_URL . ZM_PATH_ZMS ;
2013-05-02 22:20:06 +08:00
2016-05-12 22:17:41 +08:00
if ( ZM_OPT_USE_AUTH ) {
if ( ZM_AUTH_RELAY == " hashed " ) {
2016-04-02 01:02:31 +08:00
$args [] = " auth= " . generateAuthHash ( ZM_AUTH_HASH_IPS );
2016-05-12 22:17:41 +08:00
} elseif ( ZM_AUTH_RELAY == " plain " ) {
2016-04-02 01:02:31 +08:00
$args [] = " user= " . $_SESSION [ 'username' ];
$args [] = " pass= " . $_SESSION [ 'password' ];
2016-05-12 22:17:41 +08:00
} elseif ( ZM_AUTH_RELAY == " none " ) {
2016-04-02 01:02:31 +08:00
$args [] = " user= " . $_SESSION [ 'username' ];
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
2016-05-12 22:17:41 +08:00
if ( ! in_array ( " mode=single " , $args ) && ! empty ( $GLOBALS [ 'connkey' ]) ) {
2016-04-02 01:02:31 +08:00
$args [] = " connkey= " . $GLOBALS [ 'connkey' ];
}
2016-05-12 22:17:41 +08:00
if ( ZM_RAND_STREAM ) {
2016-04-02 01:02:31 +08:00
$args [] = " rand= " . time ();
}
2016-05-12 22:17:41 +08:00
if ( count ( $args ) ) {
2016-04-02 01:02:31 +08:00
$streamSrc .= " ? " . join ( $querySep , $args );
}
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
return ( $streamSrc );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function getMimeType ( $file ) {
if ( function_exists ( 'mime_content_type' ) ) {
2016-04-02 01:02:31 +08:00
return ( mime_content_type ( $file ) );
2016-05-12 22:17:41 +08:00
} elseif ( function_exists ( 'finfo_file' ) ) {
2016-04-02 01:02:31 +08:00
$finfo = finfo_open ( FILEINFO_MIME );
$mimeType = finfo_file ( $finfo , $file );
finfo_close ( $finfo );
return ( $mimeType );
}
return ( trim ( exec ( 'file -bi ' . escapeshellarg ( $file ) . ' 2>/dev/null' ) ) );
2013-05-02 22:20:06 +08:00
}
2016-04-29 22:44:46 +08:00
function outputVideoStream ( $id , $src , $width , $height , $format , $title = " " ) {
echo getVideoStreamHTML ( $id , $src , $width , $height , $format , $title );
}
function getVideoStreamHTML ( $id , $src , $width , $height , $format , $title = " " ) {
$html = '' ;
$width = validInt ( $width );
$height = validInt ( $height );
$title = validHtmlStr ( $title );
if ( file_exists ( $src ) ) {
2016-04-02 01:02:31 +08:00
$mimeType = getMimeType ( $src );
2016-04-29 22:44:46 +08:00
} else {
switch ( $format ) {
2016-04-02 01:02:31 +08:00
case 'asf' :
$mimeType = " video/x-ms-asf " ;
break ;
case 'avi' :
case 'wmv' :
$mimeType = " video/x-msvideo " ;
break ;
case 'mov' :
$mimeType = " video/quicktime " ;
break ;
case 'mpg' :
case 'mpeg' :
$mimeType = " video/mpeg " ;
break ;
case 'swf' :
$mimeType = " application/x-shockwave-flash " ;
break ;
case '3gp' :
$mimeType = " video/3gpp " ;
break ;
default :
$mimeType = " video/ $format " ;
break ;
}
}
if ( ! $mimeType || ( $mimeType == 'application/octet-stream' ) )
$mimeType = 'video/' . $format ;
2016-04-29 22:44:46 +08:00
if ( ZM_WEB_USE_OBJECT_TAGS ) {
switch ( $mimeType ) {
2016-04-02 01:02:31 +08:00
case " video/x-ms-asf " :
case " video/x-msvideo " :
case " video/mp4 " :
{
2016-04-29 22:44:46 +08:00
if ( isWindows () ) {
return '<object id="' . $id . '" width="' . $width . '" height="' . $height . '
2016-04-02 01:02:31 +08:00
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... "
2016-04-29 22:44:46 +08:00
type = " '. $mimeType .' " >
< param name = " FileName " value = " '. $src .' " />
2016-04-02 01:02:31 +08:00
< param name = " autoStart " value = " 1 " />
< param name = " showControls " value = " 0 " />
2016-04-29 22:44:46 +08:00
< embed type = " '. $mimeType .' "
2016-04-02 01:02:31 +08:00
pluginspage = " http://www.microsoft.com/Windows/MediaPlayer/ "
2016-04-29 22:44:46 +08:00
src = " '. $src .' "
name = " '. $title .' "
width = " '. $width .' "
height = " '. $height .' "
2016-04-02 01:02:31 +08:00
autostart = " 1 "
showcontrols = " 0 " >
</ embed >
2016-04-29 22:44:46 +08:00
</ object > ' ;
2016-04-02 01:02:31 +08:00
}
}
case " video/quicktime " :
{
2016-04-29 22:44:46 +08:00
return '<object id="' . $id . '" width="' . $width . '" height="' . $height . ' "
2016-04-02 01:02:31 +08:00
classid = " clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B "
codebase = " http://www.apple.com/qtactivex/qtplugin.cab "
2016-04-29 22:44:46 +08:00
type = " '. $mimeType .' " >
< param name = " src " value = " '. $src .' " />
2016-04-02 01:02:31 +08:00
< param name = " autoplay " VALUE = " true " />
< param name = " controller " VALUE = " false " />
2016-04-29 22:44:46 +08:00
< embed type = " '. $mimeType .' "
src = " '. $src .' "
2016-04-02 01:02:31 +08:00
pluginspage = " http://www.apple.com/quicktime/download/ "
2016-04-29 22:44:46 +08:00
name = " '. $title .' " width = " '. $width .' " height = " '. $height .' "
2016-04-02 01:02:31 +08:00
autoplay = " true "
controller = " true " >
</ embed >
2016-04-29 22:44:46 +08:00
</ object > ' ;
2016-04-02 01:02:31 +08:00
}
case " application/x-shockwave-flash " :
{
2016-04-29 22:44:46 +08:00
return '<object id="' . $id . '" width="' . $width . '" height="' . $height . ' "
2016-04-02 01:02:31 +08:00
classid = " clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 "
codebase = " http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0 "
2016-04-29 22:44:46 +08:00
type = " '. $mimeType .' " >
< param name = " movie " value = " '. $src .' " />
2016-04-02 01:02:31 +08:00
< param name = " quality " value = " high " />
< param name = " bgcolor " value = " #ffffff " />
2016-04-29 22:44:46 +08:00
< embed type = " '. $mimeType .' "
2016-04-02 01:02:31 +08:00
pluginspage = " http://www.macromedia.com/go/getflashplayer "
2016-04-29 22:44:46 +08:00
src = " '. $src .' "
name = " '. $title .' "
width = " '. $width .' "
height = " '. $height .' "
2016-04-02 01:02:31 +08:00
quality = " high "
bgcolor = " #ffffff " >
</ embed >
2016-04-29 22:44:46 +08:00
</ object > ' ;
2016-04-02 01:02:31 +08:00
}
2016-04-29 22:44:46 +08:00
} # end switch
} # end if use object tags
return '<embed' . ( isset ( $mimeType ) ? ( ' type="' . $mimeType . '"' ) : '' ) . '
src = " '. $src .' "
name = " '. $title .' "
width = " '. $width .' "
height = " '. $height .' "
2016-04-02 01:02:31 +08:00
autostart = " 1 "
autoplay = " 1 "
showcontrols = " 0 "
controller = " 0 " >
2016-04-29 22:44:46 +08:00
</ embed > ' ;
2013-05-02 22:20:06 +08:00
}
2016-04-29 22:44:46 +08:00
function outputImageStream ( $id , $src , $width , $height , $title = " " ) {
echo getImageStream ( $id , $src , $width , $height , $title );
}
function getImageStream ( $id , $src , $width , $height , $title = " " ) {
2016-04-02 01:02:31 +08:00
if ( canStreamIframe () ) {
2016-04-29 22:44:46 +08:00
return '<iframe id="' . $id . '" src="' . $src . '" alt="' . validHtmlStr ( $title ) . '" width="' . validInt ( $width ) . " height= " . validInt ( $height ) . '"/>' ;
2016-04-02 01:02:31 +08:00
} else {
2016-04-29 22:44:46 +08:00
return '<img id="' . $id . '" src="' . $src . '" alt="' . validHtmlStr ( $title ) . '" width="' . validInt ( $width ) . '" height="' . validInt ( $height ) . '"/>' ;
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function outputControlStream ( $src , $width , $height , $monitor , $scale , $target ) {
2013-05-02 22:20:06 +08:00
?>
2016-05-12 22:17:41 +08:00
< form name = " ctrlForm " method = " post " action = " <?php echo $_SERVER['PHP_SELF'] ?> " target = " <?php echo $target ?> " >
2016-04-02 01:02:31 +08:00
< input type = " hidden " name = " view " value = " blank " >
< input type = " hidden " name = " mid " value = " <?php echo $monitor['Id'] ?> " >
< input type = " hidden " name = " action " value = " control " >
2016-05-17 22:48:52 +08:00
< ? php if ( $monitor [ 'CanMoveMap' ] ) { ?>
< input type = " hidden " name = " control " value = " moveMap " >
< ? php } elseif ( $monitor [ 'CanMoveRel' ] ) { ?>
2016-04-02 01:02:31 +08:00
< input type = " hidden " name = " control " value = " movePseudoMap " >
2016-05-17 22:48:52 +08:00
< ? php } elseif ( $monitor [ 'CanMoveCon' ] ) { ?>
2016-04-02 01:02:31 +08:00
< input type = " hidden " name = " control " value = " moveConMap " >
2016-05-17 22:48:52 +08:00
< ? php } ?>
2016-04-02 01:02:31 +08:00
< input type = " hidden " name = " scale " value = " <?php echo $scale ?> " >
< input type = " image " src = " <?php echo $src ?> " width = " <?php echo $width ?> " height = " <?php echo $height ?> " >
2016-05-12 22:17:41 +08:00
</ form >
2013-05-02 22:20:06 +08:00
< ? php
}
2016-05-12 22:17:41 +08:00
function outputHelperStream ( $id , $src , $width , $height , $title = " " ) {
2016-05-25 03:10:37 +08:00
echo getOutputHelperStream ( $id , $src , $width , $height , $title );
}
function getHelperStream ( $id , $src , $width , $height , $title = " " ) {
return '<applet id="' . $id . ' " code= " com . charliemouse . cambozola . Viewer "
archive = " '. ZM_PATH_CAMBOZOLA .' "
2016-04-02 01:02:31 +08:00
align = " middle "
2016-05-25 03:10:37 +08:00
width = " '. $width .' "
height = " '. $height .' "
title = " '. $title .' " >
2016-04-02 01:02:31 +08:00
< param name = " accessories " value = " none " />
2016-05-25 03:10:37 +08:00
< param name = " url " value = " '. $src .' " />
</ applet > ' ;
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function outputImageStill ( $id , $src , $width , $height , $title = " " ) {
2016-05-25 03:50:22 +08:00
echo getImageStill ( $id , $src , $width , $height , $title = " " );
}
function getImageStill ( $id , $src , $width , $height , $title = " " ) {
return '<img id="' . $id . '" src="' . $src . '" alt="' . $title . '" width="' . $width . '" height="' . $height . '"/>' ;
2013-05-02 22:20:06 +08:00
}
function outputControlStill ( $src , $width , $height , $monitor , $scale , $target )
{
2016-04-02 01:02:31 +08:00
?>
< form name = " ctrlForm " method = " post " action = " <?php echo $_SERVER['PHP_SELF'] ?> " target = " <?php echo $target ?> " >
< input type = " hidden " name = " view " value = " blank " >
< input type = " hidden " name = " mid " value = " <?php echo $monitor['Id'] ?> " >
< input type = " hidden " name = " action " value = " control " >
< ? php
2016-05-12 22:17:41 +08:00
if ( $monitor [ 'CanMoveMap' ] ) {
?>
2016-05-17 22:48:52 +08:00
< input type = " hidden " name = " control " value = " moveMap " >
2016-05-12 22:17:41 +08:00
< ? php
} elseif ( $monitor [ 'CanMoveRel' ] ) {
2016-04-02 01:02:31 +08:00
?>
< input type = " hidden " name = " control " value = " movePseudoMap " >
2016-05-12 22:17:41 +08:00
< ? php
} elseif ( $monitor [ 'CanMoveCon' ] ) {
2016-04-02 01:02:31 +08:00
?>
< input type = " hidden " name = " control " value = " moveConMap " >
2016-05-12 22:17:41 +08:00
< ? php
}
?>
2016-04-02 01:02:31 +08:00
< input type = " hidden " name = " scale " value = " <?php echo $scale ?> " >
< input type = " image " src = " <?php echo $src ?> " width = " <?php echo $width ?> " height = " <?php echo $height ?> " >
</ form >
< ? php
2013-05-02 22:20:06 +08:00
}
2014-06-06 03:14:12 +08:00
// Incoming args are shell-escaped. This function must escape any further arguments it cannot guarantee.
2016-05-12 22:17:41 +08:00
function getZmuCommand ( $args ) {
2016-04-02 01:02:31 +08:00
$zmuCommand = ZMU_PATH ;
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
if ( ZM_OPT_USE_AUTH )
{
if ( ZM_AUTH_RELAY == " hashed " )
2013-05-02 22:20:06 +08:00
{
2016-04-02 01:02:31 +08:00
$zmuCommand .= " -A " . generateAuthHash ( false );
}
elseif ( ZM_AUTH_RELAY == " plain " )
{
$zmuCommand .= " -U " . escapeshellarg ( $_SESSION [ 'username' ]) . " -P " . escapeshellarg ( $_SESSION [ 'password' ]);
}
elseif ( ZM_AUTH_RELAY == " none " )
{
$zmuCommand .= " -U " . escapeshellarg ( $_SESSION [ 'username' ]);
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
$zmuCommand .= $args ;
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
return ( $zmuCommand );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function visibleMonitor ( $mid ) {
2016-04-02 01:02:31 +08:00
global $user ;
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
return ( empty ( $user [ 'MonitorIds' ]) || in_array ( $mid , explode ( ',' , $user [ 'MonitorIds' ] ) ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function canView ( $area , $mid = false ) {
2016-04-02 01:02:31 +08:00
global $user ;
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
return ( ( $user [ $area ] == 'View' || $user [ $area ] == 'Edit' ) && ( ! $mid || visibleMonitor ( $mid ) ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function canEdit ( $area , $mid = false ) {
2016-04-02 01:02:31 +08:00
global $user ;
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
return ( $user [ $area ] == 'Edit' && ( ! $mid || visibleMonitor ( $mid ) ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function getEventPath ( $event ) {
2016-04-02 01:02:31 +08:00
if ( ZM_USE_DEEP_STORAGE )
$eventPath = $event [ 'MonitorId' ] . '/' . strftime ( " %y/%m/%d/%H/%M/%S " , strtotime ( $event [ 'StartTime' ]) );
else
$eventPath = $event [ 'MonitorId' ] . '/' . $event [ 'Id' ];
return ( $eventPath );
2013-05-02 22:20:06 +08:00
}
2013-12-20 00:38:07 +08:00
function getEventDefaultVideoPath ( $event ) {
2016-04-01 23:35:38 +08:00
$Event = new Event ( $event );
2016-04-02 03:43:40 +08:00
return $Event -> getStreamSrc ( array ( " mode=mpeg&format=h264 " ) );
2016-04-02 01:02:31 +08:00
//$Event->Path().'/'.$event['DefaultVideo'];
2013-12-20 00:38:07 +08:00
}
2013-05-02 22:20:06 +08:00
2016-05-12 22:17:41 +08:00
function deletePath ( $path ) {
if ( is_dir ( $path ) ) {
2016-04-02 01:02:31 +08:00
system ( escapeshellcmd ( " rm -rf " . $path ) );
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
unlink ( $path );
}
2013-05-02 22:20:06 +08:00
}
2016-03-30 00:07:13 +08:00
function deleteEvent ( $event ) {
2015-02-07 00:41:37 +08:00
2016-04-02 01:02:31 +08:00
if ( empty ( $event ) ) {
Error ( " Empty event passed to deleteEvent. " );
return ;
}
2015-02-07 00:41:37 +08:00
2016-04-02 01:02:31 +08:00
if ( gettype ( $event ) != 'array' ) {
# $event could be an eid, so turn it into an event hash
$event = new Event ( $event );
}
2015-02-07 00:41:37 +08:00
2016-04-02 01:02:31 +08:00
global $user ;
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
if ( $user [ 'Events' ] == 'Edit' ) {
$event -> delete ();
} # CAN EDIT
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function makeLink ( $url , $label , $condition = 1 , $options = " " ) {
2016-04-02 01:02:31 +08:00
$string = " " ;
2016-05-12 22:17:41 +08:00
if ( $condition ) {
2016-04-02 01:02:31 +08:00
$string .= '<a href="' . $url . '"' . ( $options ? ( ' ' . $options ) : '' ) . '>' ;
}
$string .= $label ;
2016-05-12 22:17:41 +08:00
if ( $condition ) {
2016-04-02 01:02:31 +08:00
$string .= '</a>' ;
}
return ( $string );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function makePopupLink ( $url , $winName , $winSize , $label , $condition = 1 , $options = " " ) {
2016-04-02 01:02:31 +08:00
$string = " " ;
2016-05-12 22:17:41 +08:00
if ( $condition ) {
2016-04-02 01:02:31 +08:00
if ( is_array ( $winSize ) )
$popupParms = " ' " . $url . " ', ' " . $winName . " ', ' " . $winSize [ 0 ] . " ', " . $winSize [ 1 ] . " , " . $winSize [ 2 ];
else
$popupParms = " ' " . $url . " ', ' " . $winName . " ', ' " . $winSize . " ' " ;
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
$string .= '<a href="' . $url . '" onclick="createPopup( ' . $popupParms . ' ); return( false );"' . ( $options ? ( ' ' . $options ) : '' ) . '>' ;
}
$string .= $label ;
2016-05-12 22:17:41 +08:00
if ( $condition ) {
2016-04-02 01:02:31 +08:00
$string .= '</a>' ;
}
return ( $string );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function makePopupButton ( $url , $winName , $winSize , $buttonValue , $condition = 1 , $options = " " ) {
2016-04-02 01:02:31 +08:00
if ( is_array ( $winSize ) )
$popupParms = " ' " . $url . " ', ' " . $winName . " ', ' " . $winSize [ 0 ] . " ', " . $winSize [ 1 ] . " , " . $winSize [ 2 ];
else
$popupParms = " ' " . $url . " ', ' " . $winName . " ', ' " . $winSize . " ' " ;
$string = '<input type="button" value="' . $buttonValue . '" onclick="createPopup( ' . $popupParms . ' ); return( false );"' . ( $condition ? '' : ' disabled="disabled"' ) . ( $options ? ( ' ' . $options ) : '' ) . '/>' ;
return ( $string );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function truncText ( $text , $length , $deslash = 1 ) {
2016-04-02 01:02:31 +08:00
return ( preg_replace ( " /^(. { " . $length . " ,}?) \ b.* $ / " , " \\ 1… " , ( $deslash ? stripslashes ( $text ) : $text ) ) );
2013-05-02 22:20:06 +08:00
}
2016-04-02 03:43:40 +08:00
function htmlSelect ( $name , $contents , $values , $behaviours = false ) {
2016-04-01 22:35:59 +08:00
2016-04-02 01:02:31 +08:00
$behaviourText = " " ;
if ( ! empty ( $behaviours ) ) {
if ( is_array ( $behaviours ) ) {
foreach ( $behaviours as $event => $action ) {
$behaviourText .= ' ' . $event . '="' . $action . '"' ;
}
} else {
$behaviourText = ' onchange="' . $behaviours . '"' ;
2016-04-01 22:35:59 +08:00
}
2016-04-02 01:02:31 +08:00
}
2016-04-02 03:43:40 +08:00
2016-04-02 01:02:31 +08:00
$html = " <select name= \" $name\ " id = \ " $name\ " $behaviourText > " ;
foreach ( $contents as $value => $text ) {
$selected = is_array ( $values ) ? in_array ( $value , $values ) : $value == $values ;
$html .= " <option value= \" $value\ " " .( $selected ? " selected = \ " selected \" " : '' ) . " > $text </option> " ;
}
$html .= " </select> " ;
return $html ;
2016-04-01 22:35:59 +08:00
}
2013-05-02 22:20:06 +08:00
function buildSelect ( $name , $contents , $behaviours = false )
{
2016-04-02 01:02:31 +08:00
$value = " " ;
2016-05-12 22:17:41 +08:00
if ( preg_match ( " /^ \ s*( \ w+) \ s*( \ [.* \ ])? \ s* $ / " , $name , $matches ) && count ( $matches ) > 2 ) {
2016-04-02 01:02:31 +08:00
$arr = $matches [ 1 ];
if ( isset ( $GLOBALS [ $arr ]) )
$value = $GLOBALS [ $arr ];
elseif ( isset ( $_REQUEST [ $arr ]) )
$value = $_REQUEST [ $arr ];
2016-05-12 22:17:41 +08:00
if ( ! preg_match_all ( " / \ [ \ s*[' \" ]?( \ w+)[ \" ']? \ s* \ ]/ " , $matches [ 2 ], $matches ) ) {
2016-04-02 01:02:31 +08:00
Fatal ( " Can't parse selector ' $name ' " );
}
2016-05-12 22:17:41 +08:00
for ( $i = 0 ; $i < count ( $matches [ 1 ]); $i ++ ) {
2016-04-02 01:02:31 +08:00
$idx = $matches [ 1 ][ $i ];
$value = isset ( $value [ $idx ]) ? $value [ $idx ] : false ;
}
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
if ( isset ( $GLOBALS [ $name ]) )
$value = $GLOBALS [ $name ];
elseif ( isset ( $_REQUEST [ $name ]) )
$value = $_REQUEST [ $name ];
}
ob_start ();
$behaviourText = " " ;
2016-05-12 22:17:41 +08:00
if ( ! empty ( $behaviours ) ) {
if ( is_array ( $behaviours ) ) {
foreach ( $behaviours as $event => $action ) {
2016-04-02 01:02:31 +08:00
$behaviourText .= ' ' . $event . '="' . $action . '"' ;
}
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
$behaviourText = ' onchange="' . $behaviours . '"' ;
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
?>
2016-05-12 22:17:41 +08:00
< select name = " <?php echo $name ?> " id = " <?php echo $name ?> " < ? php echo $behaviourText ?> >
< ? php
foreach ( $contents as $contentValue => $contentText ) {
2016-04-02 01:02:31 +08:00
?>
2016-05-12 22:17:41 +08:00
< option value = " <?php echo $contentValue ?> " < ? php if ( $value == $contentValue ) { ?> selected="selected"<?php } ?>><?php echo validHtmlStr($contentText) ?></option>
< ? php
}
?>
</ select >
< ? php
$html = ob_get_contents ();
2016-04-02 01:02:31 +08:00
ob_end_clean ();
return ( $html );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function getFormChanges ( $values , $newValues , $types = false , $columns = false ) {
2016-04-02 01:02:31 +08:00
$changes = array ();
if ( ! $types )
$types = array ();
2013-05-02 22:20:06 +08:00
2016-05-12 22:17:41 +08:00
foreach ( $newValues as $key => $value ) {
2016-04-02 01:02:31 +08:00
if ( $columns && ! $columns [ $key ] )
continue ;
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
if ( ! isset ( $types [ $key ]) )
$types [ $key ] = false ;
2016-05-12 22:17:41 +08:00
switch ( $types [ $key ] ) {
2016-04-02 01:02:31 +08:00
case 'set' :
2013-05-02 22:20:06 +08:00
{
2016-05-12 22:17:41 +08:00
if ( is_array ( $newValues [ $key ] ) ) {
if ( join ( ',' , $newValues [ $key ]) != $values [ $key ] ) {
2016-04-02 01:02:31 +08:00
$changes [ $key ] = " $key = " . dbEscape ( join ( ',' , $newValues [ $key ]));
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
} elseif ( $values [ $key ] ) {
2016-04-02 01:02:31 +08:00
$changes [ $key ] = " $key = '' " ;
}
break ;
}
case 'image' :
{
2016-05-12 22:17:41 +08:00
if ( is_array ( $newValues [ $key ] ) ) {
2016-04-02 01:02:31 +08:00
$imageData = getimagesize ( $newValues [ $key ][ 'tmp_name' ] );
$changes [ $key . 'Width' ] = $key . " Width = " . $imageData [ 0 ];
$changes [ $key . 'Height' ] = $key . " Height = " . $imageData [ 1 ];
$changes [ $key . 'Type' ] = $key . " Type = ' " . $newValues [ $key ][ 'type' ] . " ' " ;
$changes [ $key . 'Size' ] = $key . " Size = " . $newValues [ $key ][ 'size' ];
ob_start ();
readfile ( $newValues [ $key ][ 'tmp_name' ] );
$changes [ $key ] = $key . " = " . dbEscape ( ob_get_contents () );
ob_end_clean ();
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
$changes [ $key ] = " $key = " . dbEscape ( $value );
}
break ;
}
case 'document' :
{
2016-05-12 22:17:41 +08:00
if ( is_array ( $newValues [ $key ] ) ) {
2016-04-02 01:02:31 +08:00
$imageData = getimagesize ( $newValues [ $key ][ 'tmp_name' ] );
$changes [ $key . 'Type' ] = $key . " Type = ' " . $newValues [ $key ][ 'type' ] . " ' " ;
$changes [ $key . 'Size' ] = $key . " Size = " . $newValues [ $key ][ 'size' ];
ob_start ();
readfile ( $newValues [ $key ][ 'tmp_name' ] );
$changes [ $key ] = $key . " = " . dbEscape ( ob_get_contents () );
ob_end_clean ();
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
$changes [ $key ] = " $key = " . dbEscape ( $value );
}
break ;
}
case 'file' :
{
$changes [ $key . 'Type' ] = $key . " Type = " . dbEscape ( $newValues [ $key ][ 'type' ]);
$changes [ $key . 'Size' ] = $key . " Size = " . dbEscape ( $newValues [ $key ][ 'size' ]);
ob_start ();
readfile ( $newValues [ $key ][ 'tmp_name' ] );
$changes [ $key ] = $key . " = ' " . dbEscape ( ob_get_contents () ) . " ' " ;
ob_end_clean ();
break ;
}
case 'raw' :
{
2016-05-12 22:17:41 +08:00
if ( $values [ $key ] != $value ) {
2016-04-02 01:02:31 +08:00
$changes [ $key ] = " $key = " . dbEscape ( $value );
}
break ;
}
default :
{
2016-05-12 22:17:41 +08:00
if ( ! isset ( $values [ $key ]) || ( $values [ $key ] != $value ) ) {
2016-05-10 07:27:42 +08:00
if ( ! isset ( $value ) || $value == '' ) {
$changes [ $key ] = " $key = NULL " ;
} else {
$changes [ $key ] = " $key = " . dbEscape ( trim ( $value ));
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
break ;
2013-05-02 22:20:06 +08:00
}
}
2016-04-02 01:02:31 +08:00
}
2016-05-12 22:17:41 +08:00
foreach ( $values as $key => $value ) {
if ( ! empty ( $columns [ $key ]) ) {
if ( ! empty ( $types [ $key ]) ) {
if ( $types [ $key ] == 'toggle' ) {
if ( ! isset ( $newValues [ $key ]) && ! empty ( $value ) ) {
2016-04-02 01:02:31 +08:00
$changes [ $key ] = " $key = 0 " ;
}
}
else if ( $types [ $key ] == 'set' )
{
$changes [ $key ] = " $key = '' " ;
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
return ( $changes );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function getBrowser ( & $browser , & $version ) {
if ( isset ( $_SESSION [ 'browser' ]) ) {
2016-04-02 01:02:31 +08:00
$browser = $_SESSION [ 'browser' ];
$version = $_SESSION [ 'version' ];
2016-05-12 22:17:41 +08:00
} else {
if (( preg_match ( '/MSIE (.*?);/' , $_SERVER [ 'HTTP_USER_AGENT' ], $logVersion )) || ( preg_match ( '/.*Trident.*rv:(.*?)(;|\))/' , $_SERVER [ 'HTTP_USER_AGENT' ], $logVersion ))) {
2016-04-02 01:02:31 +08:00
$version = $logVersion [ 1 ];
$browser = 'ie' ;
2016-05-12 22:17:41 +08:00
} elseif ( preg_match ( '/Chrome\/([0-9.]+)/' , $_SERVER [ 'HTTP_USER_AGENT' ], $logVersion ) ) {
2016-04-02 01:02:31 +08:00
$version = $logVersion [ 1 ];
// Check for old version of Chrome with bug 5876
2016-05-12 22:17:41 +08:00
if ( $version < 7 ) {
2016-04-02 01:02:31 +08:00
$browser = 'oldchrome' ;
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
$browser = 'chrome' ;
}
2016-05-12 22:17:41 +08:00
} elseif ( preg_match ( '/Safari\/([0-9.]+)/' , $_SERVER [ 'HTTP_USER_AGENT' ], $logVersion ) ) {
2016-04-02 01:02:31 +08:00
$version = $logVersion [ 1 ];
$browser = 'safari' ;
2016-05-12 22:17:41 +08:00
} elseif ( preg_match ( '/Opera[ \/]([0-9].[0-9]{1,2})/' , $_SERVER [ 'HTTP_USER_AGENT' ], $logVersion ) ) {
2016-04-02 01:02:31 +08:00
$version = $logVersion [ 1 ];
$browser = 'opera' ;
2016-05-12 22:17:41 +08:00
} elseif ( preg_match ( '/Konqueror\/([0-9.]+)/' , $_SERVER [ 'HTTP_USER_AGENT' ], $logVersion ) ) {
2016-04-02 01:02:31 +08:00
$version = $logVersion [ 1 ];
$browser = 'konqueror' ;
2016-05-12 22:17:41 +08:00
} elseif ( preg_match ( '/Mozilla\/([0-9].[0-9]{1,2})/' , $_SERVER [ 'HTTP_USER_AGENT' ], $logVersion ) ) {
2016-04-02 01:02:31 +08:00
$version = $logVersion [ 1 ];
$browser = 'mozilla' ;
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
$version = 0 ;
$browser = 'unknown' ;
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
$_SESSION [ 'browser' ] = $browser ;
$_SESSION [ 'version' ] = $version ;
}
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function isMozilla () {
2016-04-02 01:02:31 +08:00
getBrowser ( $browser , $version );
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
return ( $browser == " mozilla " );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function isKonqueror () {
2016-04-02 01:02:31 +08:00
getBrowser ( $browser , $version );
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
return ( $browser == " konqueror " );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function isInternetExplorer () {
2016-04-02 01:02:31 +08:00
getBrowser ( $browser , $version );
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
return ( $browser == " ie " );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function isOldChrome () {
2016-04-02 01:02:31 +08:00
getBrowser ( $browser , $version );
2013-09-22 03:00:50 +08:00
2016-04-02 01:02:31 +08:00
return ( $browser == " oldchrome " );
2013-09-22 03:00:50 +08:00
}
2016-05-12 22:17:41 +08:00
function isChrome () {
2016-04-02 01:02:31 +08:00
getBrowser ( $browser , $version );
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
return ( $browser == " chrome " );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function isOpera () {
2016-04-02 01:02:31 +08:00
getBrowser ( $browser , $version );
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
return ( $browser == " opera " );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function isSafari () {
2016-04-02 01:02:31 +08:00
getBrowser ( $browser , $version );
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
return ( $browser == " safari " );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function isWindows () {
2016-04-02 01:02:31 +08:00
return ( preg_match ( '/Win/' , $_SERVER [ 'HTTP_USER_AGENT' ] ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function canStreamIframe () {
2016-04-02 01:02:31 +08:00
return ( isKonqueror () );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function canStreamNative () {
2016-04-02 01:02:31 +08:00
// Old versions of Chrome can display the stream, but then it blocks everything else (Chrome bug 5876)
return ( ZM_WEB_CAN_STREAM == " yes " || ( ZM_WEB_CAN_STREAM == " auto " && ( ! isInternetExplorer () && ! isOldChrome ()) ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function canStreamApplet () {
if ( ( ZM_OPT_CAMBOZOLA && ! file_exists ( ZM_PATH_WEB . '/' . ZM_PATH_CAMBOZOLA )) ) {
2016-04-02 01:02:31 +08:00
Warning ( " ZM_OPT_CAMBOZOLA is enabled, but the system cannot find " . ZM_PATH_WEB . " / " . ZM_PATH_CAMBOZOLA );
}
2013-09-22 03:00:50 +08:00
2016-04-02 01:02:31 +08:00
return ( ( ZM_OPT_CAMBOZOLA && file_exists ( ZM_PATH_WEB . '/' . ZM_PATH_CAMBOZOLA )) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function canStream () {
2016-04-02 01:02:31 +08:00
return ( canStreamNative () | canStreamApplet () );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function packageControl ( $command ) {
2016-04-02 01:02:31 +08:00
$string = ZM_PATH_BIN . '/zmpkg.pl ' . escapeshellarg ( $command );
$string .= " 2>/dev/null >&- <&- >/dev/null " ;
exec ( $string );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function daemonControl ( $command , $daemon = false , $args = false ) {
2016-04-02 01:02:31 +08:00
$string = ZM_PATH_BIN . " /zmdc.pl $command " ;
2016-05-12 22:17:41 +08:00
if ( $daemon ) {
2016-04-02 01:02:31 +08:00
$string .= " $daemon " ;
2016-05-12 22:17:41 +08:00
if ( $args ) {
2016-04-02 01:02:31 +08:00
$string .= " $args " ;
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
$string .= " 2>/dev/null >&- <&- >/dev/null " ;
exec ( $string );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function zmcControl ( $monitor , $mode = false ) {
2016-04-05 04:33:46 +08:00
if ( ( ! defined ( 'ZM_SERVER_ID' )) or ( ZM_SERVER_ID == $monitor [ 'ServerId' ] ) ) {
2016-05-12 22:17:41 +08:00
$row = NULL ;
if ( $monitor [ 'Type' ] == " Local " ) {
2016-04-04 23:47:29 +08:00
$row = dbFetchOne ( " select count(if(Function!='None',1,NULL)) as ActiveCount from Monitors where Device = ? " , NULL , array ( $monitor [ 'Device' ]) );
$zmcArgs = " -d " . $monitor [ 'Device' ];
2016-05-12 22:17:41 +08:00
} else {
2016-04-04 23:47:29 +08:00
$row = dbFetchOne ( " select count(if(Function!='None',1,NULL)) as ActiveCount from Monitors where Id = ? " , NULL , array ( $monitor [ 'Id' ]) );
$zmcArgs = " -m " . $monitor [ 'Id' ];
2013-05-02 22:20:06 +08:00
}
$activeCount = $row [ 'ActiveCount' ];
2016-04-02 01:02:31 +08:00
2016-05-12 22:17:41 +08:00
if ( ! $activeCount || $mode == " stop " ) {
2016-04-02 01:02:31 +08:00
daemonControl ( " stop " , " zmc " , $zmcArgs );
2016-05-12 22:17:41 +08:00
} else {
if ( $mode == " restart " ) {
2016-04-04 23:47:29 +08:00
daemonControl ( " stop " , " zmc " , $zmcArgs );
}
daemonControl ( " start " , " zmc " , $zmcArgs );
2013-05-02 22:20:06 +08:00
}
2016-06-08 02:39:59 +08:00
} else {
$Server = new Server ( $monitor [ 'ServerId' ] );
2016-06-08 03:47:59 +08:00
#$url = $Server->Url() . '/zm/api/monitors.json?auth='.generateAuthHash( $_SESSION['remoteAddr'] );
$url = $Server -> Url () . '/zm/api/monitors.json?user=' . $_SESSION [ 'username' ] . '&pass=' . $_SESSION [ 'passwordHash' ];
2016-06-08 02:39:59 +08:00
$data = array ( 'Monitor[Function]' => $monitor [ 'Function' ] );
// use key 'http' even if you send the request to https://...
$options = array (
'http' => array (
'header' => " Content-type: application/x-www-form-urlencoded \r \n " ,
'method' => 'POST' ,
'content' => http_build_query ( $data )
)
);
$context = stream_context_create ( $options );
$result = file_get_contents ( $url , false , $context );
if ( $result === FALSE ) { /* Handle error */ }
2016-04-02 01:02:31 +08:00
}
}
2016-05-12 22:17:41 +08:00
function zmaControl ( $monitor , $mode = false ) {
2016-04-05 04:33:46 +08:00
if ( ( ! defined ( 'ZM_SERVER_ID' )) or ( ZM_SERVER_ID == $monitor [ 'ServerId' ] ) ) {
2016-05-12 22:17:41 +08:00
if ( ! is_array ( $monitor ) ) {
2016-04-04 23:47:29 +08:00
$monitor = dbFetchOne ( " select C.*, M.* from Monitors as M left join Controls as C on (M.ControlId = C.Id ) where M.Id=? " , NULL , array ( $monitor ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
if ( ! $monitor || $monitor [ 'Function' ] == 'None' || $monitor [ 'Function' ] == 'Monitor' || $mode == " stop " ) {
if ( ZM_OPT_CONTROL ) {
2016-04-02 01:02:31 +08:00
daemonControl ( " stop " , " zmtrack.pl " , " -m " . $monitor [ 'Id' ] );
}
daemonControl ( " stop " , " zma " , " -m " . $monitor [ 'Id' ] );
2016-05-12 22:17:41 +08:00
if ( ZM_OPT_FRAME_SERVER ) {
2016-04-02 01:02:31 +08:00
daemonControl ( " stop " , " zmf " , " -m " . $monitor [ 'Id' ] );
}
2016-05-12 22:17:41 +08:00
} else {
if ( $mode == " restart " ) {
if ( ZM_OPT_CONTROL ) {
2016-04-04 23:47:29 +08:00
daemonControl ( " stop " , " zmtrack.pl " , " -m " . $monitor [ 'Id' ] );
2013-05-02 22:20:06 +08:00
}
2016-04-04 23:47:29 +08:00
daemonControl ( " stop " , " zma " , " -m " . $monitor [ 'Id' ] );
2016-05-12 22:17:41 +08:00
if ( ZM_OPT_FRAME_SERVER ) {
2016-04-04 23:47:29 +08:00
daemonControl ( " stop " , " zmf " , " -m " . $monitor [ 'Id' ] );
}
}
2016-05-12 22:17:41 +08:00
if ( ZM_OPT_FRAME_SERVER ) {
2016-04-04 23:47:29 +08:00
daemonControl ( " start " , " zmf " , " -m " . $monitor [ 'Id' ] );
}
daemonControl ( " start " , " zma " , " -m " . $monitor [ 'Id' ] );
2016-05-12 22:17:41 +08:00
if ( ZM_OPT_CONTROL && $monitor [ 'Controllable' ] && $monitor [ 'TrackMotion' ] && ( $monitor [ 'Function' ] == 'Modect' || $monitor [ 'Function' ] == 'Mocord' ) ) {
2016-04-04 23:47:29 +08:00
daemonControl ( " start " , " zmtrack.pl " , " -m " . $monitor [ 'Id' ] );
}
2016-05-12 22:17:41 +08:00
if ( $mode == " reload " ) {
2016-04-04 23:47:29 +08:00
daemonControl ( " reload " , " zma " , " -m " . $monitor [ 'Id' ] );
}
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function initDaemonStatus () {
2016-04-02 01:02:31 +08:00
global $daemon_status ;
2016-05-12 22:17:41 +08:00
if ( ! isset ( $daemon_status ) ) {
if ( daemonCheck () ) {
2016-04-02 01:02:31 +08:00
$string = ZM_PATH_BIN . " /zmdc.pl status " ;
$daemon_status = shell_exec ( $string );
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
$daemon_status = " " ;
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function daemonStatus ( $daemon , $args = false ) {
2016-04-02 01:02:31 +08:00
global $daemon_status ;
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
initDaemonStatus ();
$string = " $daemon " ;
if ( $args )
$string .= " $args " ;
return ( strpos ( $daemon_status , " ' $string ' running " ) !== false );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function zmcStatus ( $monitor ) {
if ( $monitor [ 'Type' ] == 'Local' ) {
2016-04-02 01:02:31 +08:00
$zmcArgs = " -d " . $monitor [ 'Device' ];
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
$zmcArgs = " -m " . $monitor [ 'Id' ];
}
return ( daemonStatus ( " zmc " , $zmcArgs ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function zmaStatus ( $monitor ) {
if ( is_array ( $monitor ) ) {
2016-04-02 01:02:31 +08:00
$monitor = $monitor [ 'Id' ];
}
return ( daemonStatus ( " zma " , " -m $monitor " ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function daemonCheck ( $daemon = false , $args = false ) {
2016-04-02 01:02:31 +08:00
$string = ZM_PATH_BIN . " /zmdc.pl check " ;
2016-05-12 22:17:41 +08:00
if ( $daemon ) {
2016-04-02 01:02:31 +08:00
$string .= " $daemon " ;
if ( $args )
$string .= " $args " ;
}
$result = exec ( $string );
return ( preg_match ( '/running/' , $result ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function zmcCheck ( $monitor ) {
if ( $monitor [ 'Type' ] == 'Local' ) {
2016-04-02 01:02:31 +08:00
$zmcArgs = " -d " . $monitor [ 'Device' ];
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
$zmcArgs = " -m " . $monitor [ 'Id' ];
}
return ( daemonCheck ( " zmc " , $zmcArgs ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function zmaCheck ( $monitor ) {
if ( is_array ( $monitor ) ) {
2016-04-02 01:02:31 +08:00
$monitor = $monitor [ 'Id' ];
}
return ( daemonCheck ( " zma " , " -m $monitor " ) );
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
function getImageSrc ( $event , $frame , $scale = SCALE_BASE , $captureOnly = false , $overwrite = false ) {
$Storage = new Storage ( $event [ 'StorageId' ] );
$Event = new Event ( $event );
$eventPath = $Event -> Path ();
if ( ! is_array ( $frame ) )
$frame = array ( 'FrameId' => $frame , 'Type' => '' );
if ( file_exists ( $eventPath . '/snapshot.jpg' ) ) {
$captImage = " snapshot.jpg " ;
} else {
$captImage = sprintf ( " %0 " . ZM_EVENT_IMAGE_DIGITS . " d-capture.jpg " , $frame [ 'FrameId' ] );
if ( ! file_exists ( $eventPath . '/' . $captImage ) ) {
# Generate the frame JPG
if ( $Event -> DefaultVideo () ) {
$command = 'ffmpeg -v 0 -i ' . $eventPath . '/' . $Event -> DefaultVideo () . ' -vf "select=gte(n\\,' . $frame [ 'FrameId' ] . '),setpts=PTS-STARTPTS" ' . $eventPath . '/' . $captImage ;
Debug ( " Running $command " );
$output = array ();
$retval = 0 ;
exec ( $command , $output , $retval );
Debug ( " Retval: $retval , output: " . implode ( " \n " , $output ));
} else {
Error ( " Can't create frame images from video becuase there is no video file for this event ( " . $Event -> DefaultVideo () );
}
}
}
$captPath = $eventPath . '/' . $captImage ;
if ( ! file_exists ( $captPath ) ) {
Error ( " Capture file does not exist at $captPath " );
return '' ;
}
$thumbCaptPath = ZM_DIR_IMAGES . '/' . $event [ 'Id' ] . '-' . $captImage ;
//echo "CI:$captImage, CP:$captPath, TCP:$thumbCaptPath<br>";
$analImage = sprintf ( " %0 " . ZM_EVENT_IMAGE_DIGITS . " d-analyse.jpg " , $frame [ 'FrameId' ] );
$analPath = $eventPath . '/' . $analImage ;
$thumbAnalPath = ZM_DIR_IMAGES . '/' . $event [ 'Id' ] . '-' . $analImage ;
//echo "AI:$analImage, AP:$analPath, TAP:$thumbAnalPath<br>";
$alarmFrame = $frame [ 'Type' ] == 'Alarm' ;
$hasAnalImage = $alarmFrame && file_exists ( $analPath ) && filesize ( $analPath );
$isAnalImage = $hasAnalImage && ! $captureOnly ;
if ( ! ZM_WEB_SCALE_THUMBS || $scale >= SCALE_BASE || ! function_exists ( 'imagecreatefromjpeg' ) ) {
$imagePath = $thumbPath = $isAnalImage ? $analPath : $captPath ;
$imageFile = $imagePath ;
$thumbFile = $thumbPath ;
} else {
if ( version_compare ( phpversion (), " 4.3.10 " , " >= " ) )
$fraction = sprintf ( " %.3F " , $scale / SCALE_BASE );
else
$fraction = sprintf ( " %.3f " , $scale / SCALE_BASE );
$scale = ( int ) round ( $scale );
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
$thumbCaptPath = preg_replace ( " / \ .jpg $ / " , " - $scale .jpg " , $thumbCaptPath );
$thumbAnalPath = preg_replace ( " / \ .jpg $ / " , " - $scale .jpg " , $thumbAnalPath );
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
if ( $isAnalImage ) {
$imagePath = $analPath ;
$thumbPath = $thumbAnalPath ;
2016-04-01 23:35:38 +08:00
} else {
2016-04-02 01:02:31 +08:00
$imagePath = $captPath ;
$thumbPath = $thumbCaptPath ;
2016-04-01 23:35:38 +08:00
}
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
$imageFile = $imagePath ;
$thumbFile = $thumbPath ;
if ( $overwrite || ! file_exists ( $thumbFile ) || ! filesize ( $thumbFile ) )
2013-05-02 22:20:06 +08:00
{
2016-04-02 01:02:31 +08:00
// Get new dimensions
list ( $imageWidth , $imageHeight ) = getimagesize ( $imageFile );
$thumbWidth = $imageWidth * $fraction ;
$thumbHeight = $imageHeight * $fraction ;
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
// Resample
$thumbImage = imagecreatetruecolor ( $thumbWidth , $thumbHeight );
$image = imagecreatefromjpeg ( $imageFile );
imagecopyresampled ( $thumbImage , $image , 0 , 0 , 0 , 0 , $thumbWidth , $thumbHeight , $imageWidth , $imageHeight );
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
if ( ! imagejpeg ( $thumbImage , $thumbFile ) )
Error ( " Can't create thumbnail ' $thumbPath ' " );
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
$imageData = array (
'eventPath' => $eventPath ,
'imagePath' => $imagePath ,
'thumbPath' => $thumbPath ,
'imageFile' => $imageFile ,
'thumbFile' => $thumbFile ,
'imageClass' => $alarmFrame ? " alarm " : " normal " ,
'isAnalImage' => $isAnalImage ,
'hasAnalImage' => $hasAnalImage ,
);
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
return ( $imageData );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function viewImagePath ( $path , $querySep = '&' ) {
if ( strncmp ( $path , ZM_DIR_IMAGES , strlen ( ZM_DIR_IMAGES ) ) == 0 ) {
2016-04-02 01:02:31 +08:00
// Thumbnails
return ( $path );
2016-05-12 22:17:41 +08:00
} elseif ( strpos ( ZM_DIR_EVENTS , '/' ) === 0 ) {
2016-04-02 01:02:31 +08:00
return ( '?view=image' . $querySep . 'path=' . $path );
}
return ( ZM_DIR_EVENTS . '/' . $path );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function createListThumbnail ( $event , $overwrite = false ) {
2016-04-02 01:02:31 +08:00
# Load the frame with the highest score to use as a thumbnail
if ( ! ( $frame = dbFetchOne ( " SELECT * FROM Frames WHERE EventId=? AND Score=? ORDER BY FrameId LIMIT 1 " , NULL , array ( $event [ 'Id' ], $event [ 'MaxScore' ] ) )) )
return ( false );
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
$frameId = $frame [ 'FrameId' ];
2016-05-12 22:17:41 +08:00
if ( ZM_WEB_LIST_THUMB_WIDTH ) {
2016-04-02 01:02:31 +08:00
$thumbWidth = ZM_WEB_LIST_THUMB_WIDTH ;
$scale = ( SCALE_BASE * ZM_WEB_LIST_THUMB_WIDTH ) / $event [ 'Width' ];
$thumbHeight = reScale ( $event [ 'Height' ], $scale );
2016-05-12 22:17:41 +08:00
} elseif ( ZM_WEB_LIST_THUMB_HEIGHT ) {
2016-04-02 01:02:31 +08:00
$thumbHeight = ZM_WEB_LIST_THUMB_HEIGHT ;
$scale = ( SCALE_BASE * ZM_WEB_LIST_THUMB_HEIGHT ) / $event [ 'Height' ];
$thumbWidth = reScale ( $event [ 'Width' ], $scale );
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
Fatal ( " No thumbnail width or height specified, please check in Options->Web " );
}
$imageData = getImageSrc ( $event , $frame , $scale , false , $overwrite );
if ( ! $imageData ) {
2016-05-03 01:42:42 +08:00
return ( false );
2016-04-02 01:02:31 +08:00
}
$thumbData = $frame ;
$thumbData [ 'Path' ] = $imageData [ 'thumbPath' ];
$thumbData [ 'Width' ] = ( int ) $thumbWidth ;
$thumbData [ 'Height' ] = ( int ) $thumbHeight ;
return ( $thumbData );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function createVideo ( $event , $format , $rate , $scale , $overwrite = false ) {
2016-04-02 01:02:31 +08:00
$command = ZM_PATH_BIN . " /zmvideo.pl -e " . $event [ 'Id' ] . " -f " . $format . " -r " . sprintf ( " %.2F " , ( $rate / RATE_BASE ) );
if ( preg_match ( '/\d+x\d+/' , $scale ) )
$command .= " -S " . $scale ;
else
if ( version_compare ( phpversion (), " 4.3.10 " , " >= " ) )
$command .= " -s " . sprintf ( " %.2F " , ( $scale / SCALE_BASE ) );
2013-05-02 22:20:06 +08:00
else
2016-04-02 01:02:31 +08:00
$command .= " -s " . sprintf ( " %.2f " , ( $scale / SCALE_BASE ) );
if ( $overwrite )
$command .= " -o " ;
$result = exec ( escapeshellcmd ( $command ), $output , $status );
return ( $status ? " " : rtrim ( $result ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function executeFilter ( $filter ) {
2016-04-02 01:02:31 +08:00
$command = ZM_PATH_BIN . " /zmfilter.pl --filter " . escapeshellarg ( $filter );
$result = exec ( $command , $output , $status );
dbQuery ( " delete from Filters where Name like '_TempFilter%' " );
return ( $status );
2013-05-02 22:20:06 +08:00
}
2016-04-09 23:23:52 +08:00
# This takes more than one scale amount, so it runs through each and alters dimension.
# I can't imagine why you would want to do that.
2016-05-12 22:17:41 +08:00
function reScale ( $dimension , $dummy ) {
2016-04-10 02:52:28 +08:00
$new_dimension = $dimension ;
2016-05-12 22:17:41 +08:00
for ( $i = 1 ; $i < func_num_args (); $i ++ ) {
2016-04-02 01:02:31 +08:00
$scale = func_get_arg ( $i );
if ( ! empty ( $scale ) && $scale != SCALE_BASE )
2016-04-10 02:52:28 +08:00
$new_dimension = ( int )(( $new_dimension * $scale ) / SCALE_BASE );
2016-04-02 01:02:31 +08:00
}
2016-04-10 02:52:28 +08:00
return ( $new_dimension );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function deScale ( $dimension , $dummy ) {
2016-04-10 02:52:28 +08:00
$new_dimension = $dimension ;
2016-05-12 22:17:41 +08:00
for ( $i = 1 ; $i < func_num_args (); $i ++ ) {
2016-04-02 01:02:31 +08:00
$scale = func_get_arg ( $i );
if ( ! empty ( $scale ) && $scale != SCALE_BASE )
2016-04-10 02:52:28 +08:00
$new_dimension = ( int )(( $new_dimension * SCALE_BASE ) / $scale );
2016-04-02 01:02:31 +08:00
}
2016-04-10 02:52:28 +08:00
return ( $new_dimension );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function monitorLimitSql () {
2016-04-02 01:02:31 +08:00
global $user ;
if ( ! empty ( $user [ 'MonitorIds' ]) )
$midSql = " and MonitorId in ( " . join ( " , " , preg_split ( '/["\'\s]*,["\'\s]*/' , $user [ 'MonitorIds' ] ) ) . " ) " ;
else
$midSql = '' ;
return ( $midSql );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function parseSort ( $saveToSession = false , $querySep = '&' ) {
2016-04-02 01:02:31 +08:00
global $sortQuery , $sortColumn , $sortOrder ; // Outputs
2016-05-12 22:17:41 +08:00
if ( empty ( $_REQUEST [ 'sort_field' ]) ) {
2016-04-02 01:02:31 +08:00
$_REQUEST [ 'sort_field' ] = ZM_WEB_EVENT_SORT_FIELD ;
$_REQUEST [ 'sort_asc' ] = ( ZM_WEB_EVENT_SORT_ORDER == " asc " );
}
2016-05-12 22:17:41 +08:00
switch ( $_REQUEST [ 'sort_field' ] ) {
2016-04-02 01:02:31 +08:00
case 'Id' :
$sortColumn = " E.Id " ;
break ;
case 'MonitorName' :
$sortColumn = " M.Name " ;
break ;
case 'Name' :
$sortColumn = " E.Name " ;
break ;
case 'Cause' :
$sortColumn = " E.Cause " ;
break ;
case 'DateTime' :
$_REQUEST [ 'sort_field' ] = 'StartTime' ;
case 'StartTime' :
$sortColumn = " E.StartTime " ;
break ;
case 'Length' :
$sortColumn = " E.Length " ;
break ;
case 'Frames' :
$sortColumn = " E.Frames " ;
break ;
case 'AlarmFrames' :
$sortColumn = " E.AlarmFrames " ;
break ;
case 'TotScore' :
$sortColumn = " E.TotScore " ;
break ;
case 'AvgScore' :
$sortColumn = " E.AvgScore " ;
break ;
case 'MaxScore' :
$sortColumn = " E.MaxScore " ;
break ;
default :
$sortColumn = " E.StartTime " ;
break ;
}
$sortOrder = $_REQUEST [ 'sort_asc' ] ? " asc " : " desc " ;
if ( ! $_REQUEST [ 'sort_asc' ] )
$_REQUEST [ 'sort_asc' ] = 0 ;
$sortQuery = $querySep . " sort_field= " . validHtmlStr ( $_REQUEST [ 'sort_field' ]) . $querySep . " sort_asc= " . validHtmlStr ( $_REQUEST [ 'sort_asc' ]);
if ( ! isset ( $_REQUEST [ 'limit' ]) )
$_REQUEST [ 'limit' ] = " " ;
2016-05-12 22:17:41 +08:00
if ( $saveToSession ) {
2016-04-02 01:02:31 +08:00
$_SESSION [ 'sort_field' ] = validHtmlStr ( $_REQUEST [ 'sort_field' ]);
$_SESSION [ 'sort_asc' ] = validHtmlStr ( $_REQUEST [ 'sort_asc' ]);
}
}
2013-05-02 22:20:06 +08:00
2016-05-12 22:17:41 +08:00
function parseFilter ( & $filter , $saveToSession = false , $querySep = '&' ) {
2016-04-02 01:02:31 +08:00
$filter [ 'query' ] = '' ;
$filter [ 'sql' ] = '' ;
$filter [ 'fields' ] = '' ;
2016-05-12 22:17:41 +08:00
if ( isset ( $filter [ 'terms' ]) && count ( $filter [ 'terms' ]) ) {
for ( $i = 0 ; $i < count ( $filter [ 'terms' ]); $i ++ ) {
if ( isset ( $filter [ 'terms' ][ $i ][ 'cnj' ]) ) {
2016-04-02 01:02:31 +08:00
$filter [ 'query' ] .= $querySep . " filter[terms][ $i ][cnj]= " . urlencode ( $filter [ 'terms' ][ $i ][ 'cnj' ]);
$filter [ 'sql' ] .= " " . $filter [ 'terms' ][ $i ][ 'cnj' ] . " " ;
$filter [ 'fields' ] .= " <input type= \" hidden \" name= \" filter[terms][ $i ][cnj] \" value= \" " . htmlspecialchars ( $filter [ 'terms' ][ $i ][ 'cnj' ]) . " \" /> \n " ;
}
2016-05-12 22:17:41 +08:00
if ( isset ( $filter [ 'terms' ][ $i ][ 'obr' ]) ) {
2016-04-02 01:02:31 +08:00
$filter [ 'query' ] .= $querySep . " filter[terms][ $i ][obr]= " . urlencode ( $filter [ 'terms' ][ $i ][ 'obr' ]);
$filter [ 'sql' ] .= " " . str_repeat ( " ( " , $filter [ 'terms' ][ $i ][ 'obr' ] ) . " " ;
$filter [ 'fields' ] .= " <input type= \" hidden \" name= \" filter[terms][ $i ][obr] \" value= \" " . htmlspecialchars ( $filter [ 'terms' ][ $i ][ 'obr' ]) . " \" /> \n " ;
}
2016-05-12 22:17:41 +08:00
if ( isset ( $filter [ 'terms' ][ $i ][ 'attr' ]) ) {
2016-04-02 01:02:31 +08:00
$filter [ 'query' ] .= $querySep . " filter[terms][ $i ][attr]= " . urlencode ( $filter [ 'terms' ][ $i ][ 'attr' ]);
$filter [ 'fields' ] .= " <input type= \" hidden \" name= \" filter[terms][ $i ][attr] \" value= \" " . htmlspecialchars ( $filter [ 'terms' ][ $i ][ 'attr' ]) . " \" /> \n " ;
2016-05-12 22:17:41 +08:00
switch ( $filter [ 'terms' ][ $i ][ 'attr' ] ) {
2016-04-02 01:02:31 +08:00
case 'MonitorName' :
$filter [ 'sql' ] .= 'M.' . preg_replace ( '/^Monitor/' , '' , $filter [ 'terms' ][ $i ][ 'attr' ] );
2013-05-02 22:20:06 +08:00
break ;
2016-04-02 01:02:31 +08:00
case 'ServerId' :
$filter [ 'sql' ] .= 'M.ServerId' ;
2013-05-02 22:20:06 +08:00
break ;
2016-04-02 01:02:31 +08:00
case 'DateTime' :
$filter [ 'sql' ] .= " E.StartTime " ;
2013-05-02 22:20:06 +08:00
break ;
2016-04-02 01:02:31 +08:00
case 'Date' :
$filter [ 'sql' ] .= " to_days( E.StartTime ) " ;
2013-05-02 22:20:06 +08:00
break ;
2016-04-02 01:02:31 +08:00
case 'Time' :
$filter [ 'sql' ] .= " extract( hour_second from E.StartTime ) " ;
2013-05-02 22:20:06 +08:00
break ;
2016-04-02 01:02:31 +08:00
case 'Weekday' :
$filter [ 'sql' ] .= " weekday( E.StartTime ) " ;
2013-05-02 22:20:06 +08:00
break ;
2016-04-02 01:02:31 +08:00
case 'Id' :
case 'Name' :
case 'MonitorId' :
case 'StorageId' :
case 'Length' :
case 'Frames' :
case 'AlarmFrames' :
case 'TotScore' :
case 'AvgScore' :
case 'MaxScore' :
case 'Cause' :
case 'Notes' :
case 'Archived' :
$filter [ 'sql' ] .= 'E.' . $filter [ 'terms' ][ $i ][ 'attr' ];
2013-05-02 22:20:06 +08:00
break ;
2016-04-02 01:02:31 +08:00
case 'DiskPercent' :
$filter [ 'sql' ] .= getDiskPercent ();
2013-05-02 22:20:06 +08:00
break ;
2016-04-02 01:02:31 +08:00
case 'DiskBlocks' :
$filter [ 'sql' ] .= getDiskBlocks ();
2013-05-02 22:20:06 +08:00
break ;
2016-04-02 01:02:31 +08:00
case 'SystemLoad' :
$filter [ 'sql' ] .= getLoad ();
2013-05-02 22:20:06 +08:00
break ;
2016-04-02 01:02:31 +08:00
}
$valueList = array ();
2016-05-12 22:17:41 +08:00
foreach ( preg_split ( '/["\'\s]*?,["\'\s]*?/' , preg_replace ( '/^["\']+?(.+)["\']+?$/' , '$1' , $filter [ 'terms' ][ $i ][ 'val' ] ) ) as $value ) {
switch ( $filter [ 'terms' ][ $i ][ 'attr' ] ) {
2016-04-02 01:02:31 +08:00
case 'MonitorName' :
case 'Name' :
case 'Cause' :
case 'Notes' :
$value = dbEscape ( $value );
break ;
case 'ServerId' :
if ( $value == 'ZM_SERVER_ID' ) {
$value = ZM_SERVER_ID ;
} else {
$value = dbEscape ( $value );
}
case 'DateTime' :
$value = " ' " . strftime ( STRF_FMT_DATETIME_DB , strtotime ( $value ) ) . " ' " ;
break ;
case 'Date' :
$value = " to_days( ' " . strftime ( STRF_FMT_DATETIME_DB , strtotime ( $value ) ) . " ' ) " ;
break ;
case 'Time' :
$value = " extract( hour_second from ' " . strftime ( STRF_FMT_DATETIME_DB , strtotime ( $value ) ) . " ' ) " ;
break ;
default :
$value = dbEscape ( $value );
break ;
}
$valueList [] = $value ;
}
2016-05-12 22:17:41 +08:00
switch ( $filter [ 'terms' ][ $i ][ 'op' ] ) {
2016-04-02 01:02:31 +08:00
case '=' :
case '!=' :
case '>=' :
case '>' :
case '<' :
case '<=' :
$filter [ 'sql' ] .= " " . $filter [ 'terms' ][ $i ][ 'op' ] . " $value " ;
2013-05-02 22:20:06 +08:00
break ;
2016-04-02 01:02:31 +08:00
case '=~' :
$filter [ 'sql' ] .= " regexp " . $value ;
2013-05-02 22:20:06 +08:00
break ;
2016-04-02 01:02:31 +08:00
case '!~' :
$filter [ 'sql' ] .= " not regexp " . $value ;
break ;
case '=[]' :
$filter [ 'sql' ] .= " in ( " . join ( " , " , $valueList ) . " ) " ;
break ;
case '![]' :
$filter [ 'sql' ] .= " not in ( " . join ( " , " , $valueList ) . " ) " ;
break ;
}
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
$filter [ 'query' ] .= $querySep . " filter[terms][ $i ][op]= " . urlencode ( $filter [ 'terms' ][ $i ][ 'op' ]);
$filter [ 'fields' ] .= " <input type= \" hidden \" name= \" filter[terms][ $i ][op] \" value= \" " . htmlspecialchars ( $filter [ 'terms' ][ $i ][ 'op' ]) . " \" /> \n " ;
$filter [ 'query' ] .= $querySep . " filter[terms][ $i ][val]= " . urlencode ( $filter [ 'terms' ][ $i ][ 'val' ]);
$filter [ 'fields' ] .= " <input type= \" hidden \" name= \" filter[terms][ $i ][val] \" value= \" " . htmlspecialchars ( $filter [ 'terms' ][ $i ][ 'val' ]) . " \" /> \n " ;
}
2016-05-12 22:17:41 +08:00
if ( isset ( $filter [ 'terms' ][ $i ][ 'cbr' ]) ) {
2016-04-02 01:02:31 +08:00
$filter [ 'query' ] .= $querySep . " filter[terms][ $i ][cbr]= " . urlencode ( $filter [ 'terms' ][ $i ][ 'cbr' ]);
$filter [ 'sql' ] .= " " . str_repeat ( " ) " , $filter [ 'terms' ][ $i ][ 'cbr' ] ) . " " ;
$filter [ 'fields' ] .= " <input type= \" hidden \" name= \" filter[terms][ $i ][cbr] \" value= \" " . htmlspecialchars ( $filter [ 'terms' ][ $i ][ 'cbr' ]) . " \" /> \n " ;
}
}
if ( $filter [ 'sql' ] )
$filter [ 'sql' ] = " and ( " . $filter [ 'sql' ] . " ) " ;
2016-05-12 22:17:41 +08:00
if ( $saveToSession ) {
2016-04-02 01:02:31 +08:00
$_SESSION [ 'filter' ] = $filter ;
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function addFilterTerm ( $filter , $position , $term = false ) {
2016-04-02 01:02:31 +08:00
if ( $position < 0 )
$position = 0 ;
elseif ( $position > count ( $filter [ 'terms' ]) )
$position = count ( $filter [ 'terms' ]);
if ( $term && $position == 0 )
unset ( $term [ 'cnj' ] );
array_splice ( $filter [ 'terms' ], $position , 0 , array ( $term ? $term : array () ) );
return ( $filter );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function delFilterTerm ( $filter , $position ) {
2016-04-02 01:02:31 +08:00
if ( $position < 0 )
$position = 0 ;
elseif ( $position >= count ( $filter [ 'terms' ]) )
$position = count ( $filter [ 'terms' ]);
array_splice ( $filter [ 'terms' ], $position , 1 );
return ( $filter );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function getPagination ( $pages , $page , $maxShortcuts , $query , $querySep = '&' ) {
2016-04-02 01:02:31 +08:00
global $view ;
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
$pageText = " " ;
2016-05-12 22:17:41 +08:00
if ( $pages > 1 ) {
if ( $page ) {
2016-04-02 01:02:31 +08:00
if ( $page < 0 )
$page = 1 ;
if ( $page > $pages )
$page = $pages ;
2016-05-12 22:17:41 +08:00
if ( $page > 1 ) {
if ( false && $page > 2 ) {
2016-04-02 01:02:31 +08:00
$pageText .= '<a href="?view=' . $view . $querySep . 'page=1' . $query . '"><<</a>' ;
}
$pageText .= '<a href="?view=' . $view . $querySep . 'page=' . ( $page - 1 ) . $query . '"><</a>' ;
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
$newPages = array ();
$pagesUsed = array ();
$lo_exp = max ( 2 , log ( $page - 1 ) / log ( $maxShortcuts ));
2016-05-12 22:17:41 +08:00
for ( $i = 0 ; $i < $maxShortcuts ; $i ++ ) {
2016-04-02 01:02:31 +08:00
$newPage = round ( $page - pow ( $lo_exp , $i ));
if ( isset ( $pagesUsed [ $newPage ]) )
continue ;
if ( $newPage <= 1 )
break ;
$pagesUsed [ $newPage ] = true ;
array_unshift ( $newPages , $newPage );
}
if ( ! isset ( $pagesUsed [ 1 ]) )
array_unshift ( $newPages , 1 );
2013-05-02 22:20:06 +08:00
2016-05-12 22:17:41 +08:00
foreach ( $newPages as $newPage ) {
2016-04-02 01:02:31 +08:00
$pageText .= '<a href="?view=' . $view . $querySep . 'page=' . $newPage . $query . '">' . $newPage . '</a> ' ;
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
$pageText .= '- ' . $page . ' -' ;
2016-05-12 22:17:41 +08:00
if ( $page < $pages ) {
2016-04-02 01:02:31 +08:00
$newPages = array ();
$pagesUsed = array ();
$hi_exp = max ( 2 , log ( $pages - $page ) / log ( $maxShortcuts ));
2016-05-12 22:17:41 +08:00
for ( $i = 0 ; $i < $maxShortcuts ; $i ++ ) {
2016-04-02 01:02:31 +08:00
$newPage = round ( $page + pow ( $hi_exp , $i ));
if ( isset ( $pagesUsed [ $newPage ]) )
continue ;
if ( $newPage > $pages )
break ;
$pagesUsed [ $newPage ] = true ;
array_push ( $newPages , $newPage );
}
if ( ! isset ( $pagesUsed [ $pages ]) )
array_push ( $newPages , $pages );
2016-05-12 22:17:41 +08:00
foreach ( $newPages as $newPage ) {
2016-04-02 01:02:31 +08:00
$pageText .= ' <a href="?view=' . $view . $querySep . 'page=' . $newPage . $query . '">' . $newPage . '</a>' ;
}
$pageText .= '<a href="?view=' . $view . $querySep . 'page=' . ( $page + 1 ) . $query . '">></a>' ;
2016-05-12 22:17:41 +08:00
if ( false && $page < ( $pages - 1 ) ) {
2016-04-02 01:02:31 +08:00
$pageText .= '<a href="?view=' . $view . $querySep . 'page=' . $pages . $query . '">>></a>' ;
}
}
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
return ( $pageText );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function sortHeader ( $field , $querySep = '&' ) {
2016-04-02 01:02:31 +08:00
global $view ;
return ( '?view=' . $view . $querySep . 'page=1' . $_REQUEST [ 'filter' ][ 'query' ] . $querySep . 'sort_field=' . $field . $querySep . 'sort_asc=' . ( $_REQUEST [ 'sort_field' ] == $field ? ! $_REQUEST [ 'sort_asc' ] : 0 ) . $querySep . 'limit=' . $_REQUEST [ 'limit' ] );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function sortTag ( $field ) {
2016-04-02 01:02:31 +08:00
if ( $_REQUEST [ 'sort_field' ] == $field )
if ( $_REQUEST [ 'sort_asc' ] )
return ( " (^) " );
else
return ( " (v) " );
return ( false );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function getLoad () {
2016-04-02 01:02:31 +08:00
$load = sys_getloadavg ();
return ( $load [ 0 ] );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function getDiskPercent () {
2016-04-02 01:02:31 +08:00
$total = disk_total_space ( ZM_DIR_EVENTS );
2016-04-10 02:52:28 +08:00
if ( ! $total ) {
Error ( " disk_total_space returned false for " . ZM_DIR_EVENTS );
return 0 ;
}
2016-05-12 22:17:41 +08:00
$free = disk_free_space ( ZM_DIR_EVENTS );
if ( ! $free ) {
Error ( " disk_free_space returned false for " . ZM_DIR_EVENTS );
}
$space = round (( $total - $free ) / $total * 100 );
2016-04-02 01:02:31 +08:00
return ( $space );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function getDiskBlocks () {
2016-04-02 01:02:31 +08:00
$df = shell_exec ( 'df ' . ZM_DIR_EVENTS );
$space = - 1 ;
if ( preg_match ( '/\s(\d+)\s+\d+\s+\d+%/ms' , $df , $matches ) )
$space = $matches [ 1 ];
return ( $space );
2013-05-02 22:20:06 +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.
2016-05-12 22:17:41 +08:00
function sidField () {
if ( SID ) {
2016-04-02 01:02:31 +08:00
list ( $sessname , $sessid ) = explode ( " = " , SID );
2013-05-02 22:20:06 +08:00
?>
2016-05-12 22:17:41 +08:00
< input type = " hidden " name = " <?php echo $sessname ?> " value = " <?php echo $sessid ?> " />
2013-05-02 22:20:06 +08:00
< ? php
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function verNum ( $version ) {
2016-04-02 01:02:31 +08:00
$vNum = " " ;
$maxFields = 3 ;
$vFields = explode ( " . " , $version );
array_splice ( $vFields , $maxFields );
2016-05-12 22:17:41 +08:00
while ( count ( $vFields ) < $maxFields ) {
2016-04-02 01:02:31 +08:00
$vFields [] = 0 ;
}
2016-05-12 22:17:41 +08:00
foreach ( $vFields as $vField ) {
2016-04-02 01:02:31 +08:00
$vField = sprintf ( " %02d " , $vField );
2016-05-12 22:17:41 +08:00
while ( strlen ( $vField ) < 2 ) {
2016-04-02 01:02:31 +08:00
$vField = " 0 " . $vField ;
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
$vNum .= $vField ;
}
return ( $vNum );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function fixSequences () {
2016-04-02 01:02:31 +08:00
$sequence = 1 ;
$sql = " select * from Monitors order by Sequence asc, Id asc " ;
2016-05-12 22:17:41 +08:00
foreach ( dbFetchAll ( $sql ) as $monitor ) {
if ( $monitor [ 'Sequence' ] != $sequence ) {
2016-04-02 01:02:31 +08:00
dbQuery ( 'update Monitors set Sequence = ? WHERE Id=?' , array ( $sequence , $monitor [ 'Id' ] ) );
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
$sequence ++ ;
}
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function firstSet () {
foreach ( func_get_args () as $arg ) {
2016-04-02 01:02:31 +08:00
if ( ! empty ( $arg ) )
return ( $arg );
}
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function linesIntersect ( $line1 , $line2 ) {
2016-04-02 01:02:31 +08:00
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
2016-05-12 22:17:41 +08:00
if ( $max_x1 < $min_x2 || $max_x2 < $min_x1 || $max_y1 < $min_y2 || $max_y2 < $min_y1 ) {
2016-04-02 01:02:31 +08:00
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' ];
2016-05-12 22:17:41 +08:00
if ( $dx1 ) {
2016-04-02 01:02:31 +08:00
$m1 = $dy1 / $dx1 ;
$b1 = $line1 [ 0 ][ 'y' ] - ( $m1 * $line1 [ 0 ][ 'x' ]);
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
$b1 = $line1 [ 0 ][ 'y' ];
}
2016-05-12 22:17:41 +08:00
if ( $dx2 ) {
2016-04-02 01:02:31 +08:00
$m2 = $dy2 / $dx2 ;
$b2 = $line2 [ 0 ][ 'y' ] - ( $m2 * $line2 [ 0 ][ 'x' ]);
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
$b2 = $line2 [ 0 ][ 'y' ];
}
2016-05-12 22:17:41 +08:00
if ( $dx1 && $dx2 ) { // Both not vertical
if ( $m1 != $m2 ) { // Not parallel or colinear
2016-04-02 01:02:31 +08:00
$x = ( $b2 - $b1 ) / ( $m1 - $m2 );
2016-05-12 22:17:41 +08:00
if ( $x >= $min_x1 && $x <= $max_x1 && $x >= $min_x2 && $x <= $max_x2 ) {
2016-04-02 01:02:31 +08:00
if ( $debug ) echo " Intersecting, at x $x <br> " ;
return ( true );
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
if ( $debug ) echo " Not intersecting, out of range at x $x <br> " ;
2013-05-02 22:20:06 +08:00
return ( false );
2016-04-02 01:02:31 +08:00
}
2016-05-12 22:17:41 +08:00
} elseif ( $b1 == $b2 ) {
2016-04-02 01:02:31 +08:00
// Colinear, must overlap due to box check, intersect?
if ( $debug ) echo " Intersecting, colinear<br> " ;
return ( true );
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
// Parallel
if ( $debug ) echo " Not intersecting, parallel<br> " ;
return ( false );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
} elseif ( ! $dx1 ) { // Line 1 is vertical
2016-04-02 01:02:31 +08:00
$y = ( $m2 * $line1 [ 0 ][ 'x' ] ) * $b2 ;
2016-05-12 22:17:41 +08:00
if ( $y >= $min_y1 && $y <= $max_y1 ) {
2016-04-02 01:02:31 +08:00
if ( $debug ) echo " Intersecting, at y $y <br> " ;
return ( true );
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
if ( $debug ) echo " Not intersecting, out of range at y $y <br> " ;
return ( false );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
} elseif ( ! $dx2 ) { // Line 2 is vertical
2016-04-02 01:02:31 +08:00
$y = ( $m1 * $line2 [ 0 ][ 'x' ] ) * $b1 ;
2016-05-12 22:17:41 +08:00
if ( $y >= $min_y2 && $y <= $max_y2 ) {
2016-04-02 01:02:31 +08:00
if ( $debug ) echo " Intersecting, at y $y <br> " ;
return ( true );
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
if ( $debug ) echo " Not intersecting, out of range at y $y <br> " ;
return ( false );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
} else { // Both lines are vertical
if ( $line1 [ 0 ][ 'x' ] == $line2 [ 0 ][ 'x' ] ) {
2016-04-02 01:02:31 +08:00
// Colinear, must overlap due to box check, intersect?
if ( $debug ) echo " Intersecting, vertical, colinear<br> " ;
return ( true );
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
// Parallel
if ( $debug ) echo " Not intersecting, vertical, parallel<br> " ;
return ( false );
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
if ( $debug ) echo " Whoops, unexpected scenario<br> " ;
return ( false );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function isSelfIntersecting ( $points ) {
2016-04-02 01:02:31 +08:00
global $debug ;
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
$n_coords = count ( $points );
$edges = array ();
2016-05-12 22:17:41 +08:00
for ( $j = 0 , $i = $n_coords - 1 ; $j < $n_coords ; $i = $j ++ ) {
2016-04-02 01:02:31 +08:00
$edges [] = array ( $points [ $i ], $points [ $j ] );
}
2013-05-02 22:20:06 +08:00
2016-05-12 22:17:41 +08:00
for ( $i = 0 ; $i <= ( $n_coords - 2 ); $i ++ ) {
for ( $j = $i + 2 ; $j < $n_coords + min ( 0 , $i - 1 ); $j ++ ) {
2016-04-02 01:02:31 +08:00
if ( $debug ) echo " Checking $i and $j <br> " ;
2016-05-12 22:17:41 +08:00
if ( linesIntersect ( $edges [ $i ], $edges [ $j ] ) ) {
2016-04-02 01:02:31 +08:00
if ( $debug ) echo " Lines $i and $j intersect<br> " ;
return ( true );
}
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
return ( false );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function getPolyCentre ( $points , $area = 0 ) {
2016-04-02 01:02:31 +08:00
$cx = 0.0 ;
$cy = 0.0 ;
if ( ! $area )
$area = getPolyArea ( $points );
2016-05-12 22:17:41 +08:00
for ( $i = 0 , $j = count ( $points ) - 1 ; $i < count ( $points ); $j = $i ++ ) {
2016-04-02 01:02:31 +08:00
$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 ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function _CompareXY ( $a , $b ) {
2016-04-02 01:02:31 +08:00
if ( $a [ 'min_y' ] == $b [ 'min_y' ] )
return ( intval ( $a [ 'min_x' ] - $b [ 'min_x' ]) );
else
return ( intval ( $a [ 'min_y' ] - $b [ 'min_y' ]) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function _CompareX ( $a , $b ) {
2016-04-02 01:02:31 +08:00
return ( intval ( $a [ 'min_x' ] - $b [ 'min_x' ]) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function getPolyArea ( $points ) {
2016-04-02 01:02:31 +08:00
global $debug ;
$n_coords = count ( $points );
$global_edges = array ();
2016-05-12 22:17:41 +08:00
for ( $j = 0 , $i = $n_coords - 1 ; $j < $n_coords ; $i = $j ++ ) {
2016-04-02 01:02:31 +08:00
$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 ,
" max_y " => ( $y1 < $y2 ? $y2 : $y1 ) + 1 ,
" min_x " => $y1 < $y2 ? $x1 : $x2 ,
" _1_m " => $dx / $dy ,
2013-05-02 22:20:06 +08:00
);
2016-04-02 01:02:31 +08:00
}
usort ( $global_edges , " _CompareXY " );
2016-05-12 22:17:41 +08:00
if ( $debug ) {
for ( $i = 0 ; $i < count ( $global_edges ); $i ++ ) {
2016-04-02 01:02:31 +08:00
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' ];
2016-05-12 22:17:41 +08:00
do {
for ( $i = 0 ; $i < count ( $global_edges ); $i ++ ) {
if ( $global_edges [ $i ][ 'min_y' ] == $y ) {
2016-04-02 01:02:31 +08:00
if ( $debug ) printf ( " Moving global edge<br> " );
$active_edges [] = $global_edges [ $i ];
array_splice ( $global_edges , $i , 1 );
$i -- ;
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
break ;
}
}
usort ( $active_edges , " _CompareX " );
2016-05-12 22:17:41 +08:00
if ( $debug ) {
for ( $i = 0 ; $i < count ( $active_edges ); $i ++ ) {
2016-04-02 01:02:31 +08:00
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' ] );
}
}
$last_x = 0 ;
$row_area = 0 ;
$parity = false ;
2016-05-12 22:17:41 +08:00
for ( $i = 0 ; $i < count ( $active_edges ); $i ++ ) {
2016-04-02 01:02:31 +08:00
$x = intval ( round ( $active_edges [ $i ][ 'min_x' ]));
2016-05-12 22:17:41 +08:00
if ( $parity ) {
2016-04-02 01:02:31 +08:00
$row_area += ( $x - $last_x ) + 1 ;
$area += $row_area ;
}
if ( $active_edges [ $i ][ 'max_y' ] != $y )
$parity = ! $parity ;
$last_x = $x ;
}
if ( $debug ) printf ( " %d: Area:%d<br> " , $y , $row_area );
$y ++ ;
2016-05-12 22:17:41 +08:00
for ( $i = 0 ; $i < count ( $active_edges ); $i ++ ) {
if ( $y >= $active_edges [ $i ][ 'max_y' ] ) { // Or >= as per sheets
2016-04-02 01:02:31 +08:00
if ( $debug ) printf ( " Deleting active_edge<br> " );
array_splice ( $active_edges , $i , 1 );
$i -- ;
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
$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 );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function getPolyAreaOld ( $points ) {
2016-04-02 01:02:31 +08:00
$area = 0.0 ;
$edge = 0.0 ;
2016-05-12 22:17:41 +08:00
for ( $i = 0 , $j = count ( $points ) - 1 ; $i < count ( $points ); $j = $i ++ ) {
2016-04-02 01:02:31 +08:00
$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 );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function mapCoords ( $a ) {
2016-04-02 01:02:31 +08:00
return ( $a [ 'x' ] . " , " . $a [ 'y' ] );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function pointsToCoords ( $points ) {
2016-04-02 01:02:31 +08:00
return ( join ( " " , array_map ( " mapCoords " , $points ) ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function coordsToPoints ( $coords ) {
2016-04-02 01:02:31 +08:00
$points = array ();
2016-05-12 22:17:41 +08:00
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 ) ) {
2016-04-02 01:02:31 +08:00
$points [] = array ( 'x' => $cmatches [ 1 ], 'y' => $cmatches [ 2 ] );
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
echo ( " Bogus coordinates ' " . $matches [ $i ] . " ' " );
2013-05-02 22:20:06 +08:00
return ( false );
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
echo ( " Bogus coordinate string ' $coords ' " );
return ( false );
}
return ( $points );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function getLanguages () {
2016-04-02 01:02:31 +08:00
$langs = array ();
2016-05-12 22:17:41 +08:00
foreach ( glob ( " lang/*_*.php " ) as $file ) {
2016-04-02 01:02:31 +08:00
preg_match ( '/([^\/]+_.+)\.php/' , $file , $matches );
$langs [ $matches [ 1 ]] = $matches [ 1 ];
}
return ( $langs );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function trimString ( $string , $length ) {
2016-04-02 01:02:31 +08:00
return ( preg_replace ( '/^(.{' . $length . ',}?)\b.*$/' , '\\1…' , $string ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function monitorIdsToNames ( $ids ) {
2016-04-02 01:02:31 +08:00
global $mITN_monitors ;
2016-05-12 22:17:41 +08:00
if ( ! $mITN_monitors ) {
2016-04-02 01:02:31 +08:00
$sql = " select Id, Name from Monitors " ;
2016-05-12 22:17:41 +08:00
foreach ( dbFetchAll ( $sql ) as $monitor ) {
2016-04-02 01:02:31 +08:00
$mITN_monitors [ $monitor [ 'Id' ]] = $monitor ;
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
$names = array ();
2016-05-12 22:17:41 +08:00
foreach ( preg_split ( '/\s*,\s*/' , $ids ) as $id ) {
if ( visibleMonitor ( $id ) ) {
if ( isset ( $mITN_monitors [ $id ]) ) {
2016-04-02 01:02:31 +08:00
$names [] = $mITN_monitors [ $id ][ 'Name' ];
}
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
$name_string = join ( ', ' , $names );
return ( $name_string );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function initX10Status () {
2016-04-02 01:02:31 +08:00
global $x10_status ;
2013-05-02 22:20:06 +08:00
2016-05-12 22:17:41 +08:00
if ( ! isset ( $x10_status ) ) {
2016-04-02 01:02:31 +08:00
$socket = socket_create ( AF_UNIX , SOCK_STREAM , 0 );
2016-05-12 22:17:41 +08:00
if ( $socket < 0 ) {
2016-04-02 01:02:31 +08:00
Fatal ( " socket_create() failed: " . socket_strerror ( $socket ) );
}
$sock_file = ZM_PATH_SOCKS . '/zmx10.sock' ;
2016-05-12 22:17:41 +08:00
if ( @ socket_connect ( $socket , $sock_file ) ) {
2016-04-02 01:02:31 +08:00
$command = " status " ;
2016-05-12 22:17:41 +08:00
if ( ! socket_write ( $socket , $command ) ) {
2016-04-02 01:02:31 +08:00
Fatal ( " Can't write to control socket: " . socket_strerror ( socket_last_error ( $socket )) );
}
socket_shutdown ( $socket , 1 );
$x10Output = " " ;
2016-05-12 22:17:41 +08:00
while ( $x10Response = socket_read ( $socket , 256 ) ) {
2016-04-02 01:02:31 +08:00
$x10Output .= $x10Response ;
}
socket_close ( $socket );
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
// Can't connect so use script
$command = ZM_PATH_BIN . " /zmx10.pl --command status " ;
//$command .= " 2>/dev/null >&- <&- >/dev/null";
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
$x10Output = exec ( escapeshellcmd ( $command ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
foreach ( explode ( " \n " , $x10Output ) as $x10Response ) {
if ( preg_match ( " /^( \ d+) \ s+(.+) $ / " , $x10Response , $matches ) ) {
2016-04-02 01:02:31 +08:00
$x10_status [ $matches [ 1 ]] = $matches [ 2 ];
}
}
}
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function getDeviceStatusX10 ( $key ) {
2016-04-02 01:02:31 +08:00
global $x10_status ;
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
initX10Status ();
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
if ( empty ( $x10_status [ $key ]) || ! ( $status = $x10_status [ $key ]) )
$status = " unknown " ;
return ( $status );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function setDeviceStatusX10 ( $key , $status ) {
2016-04-02 01:02:31 +08:00
$socket = socket_create ( AF_UNIX , SOCK_STREAM , 0 );
2016-05-12 22:17:41 +08:00
if ( $socket < 0 ) {
2016-04-02 01:02:31 +08:00
Fatal ( " socket_create() failed: " . socket_strerror ( $socket ) );
}
$sock_file = ZM_PATH_SOCKS . '/zmx10.sock' ;
2016-05-12 22:17:41 +08:00
if ( @ socket_connect ( $socket , $sock_file ) ) {
2016-04-02 01:02:31 +08:00
$command = " $status ; $key " ;
2016-05-12 22:17:41 +08:00
if ( ! socket_write ( $socket , $command ) ) {
2016-04-02 01:02:31 +08:00
Fatal ( " Can't write to control socket: " . socket_strerror ( socket_last_error ( $socket )) );
}
socket_shutdown ( $socket , 1 );
$x10Response = socket_read ( $socket , 256 );
socket_close ( $socket );
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
// Can't connect so use script
$command = ZM_PATH_BIN . '/zmx10.pl --command ' . escapeshellarg ( $status );
$command .= ' --unit-code ' . escapeshellarg ( $key );
//$command .= " 2>/dev/null >&- <&- >/dev/null";
$x10Response = exec ( $command );
}
if ( preg_match ( '/^' . $key . '\s+(.*)/' , $x10Response , $matches ) )
$status = $matches [ 1 ];
else
$status = " unknown " ;
return ( $status );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function logState () {
2016-04-02 01:02:31 +08:00
$state = 'ok' ;
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
$levelCounts = array (
Logger :: FATAL => array ( ZM_LOG_ALERT_FAT_COUNT , ZM_LOG_ALARM_FAT_COUNT ),
Logger :: ERROR => array ( ZM_LOG_ALERT_ERR_COUNT , ZM_LOG_ALARM_ERR_COUNT ),
Logger :: WARNING => array ( ZM_LOG_ALERT_WAR_COUNT , ZM_LOG_ALARM_WAR_COUNT ),
);
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
$sql = " select Level, count(Level) as LevelCount from Logs where Level < " . Logger :: INFO . " and TimeKey > unix_timestamp(now() - interval " . ZM_LOG_CHECK_PERIOD . " second) group by Level order by Level asc " ;
$counts = dbFetchAll ( $sql );
2013-05-02 22:20:06 +08:00
2016-05-12 22:17:41 +08:00
foreach ( $counts as $count ) {
2016-04-02 01:02:31 +08:00
if ( $count [ 'Level' ] <= Logger :: PANIC )
$count [ 'Level' ] = Logger :: FATAL ;
2016-05-12 22:17:41 +08:00
if ( ! ( $levelCount = $levelCounts [ $count [ 'Level' ]]) ) {
2016-04-02 01:02:31 +08:00
Error ( " Unexpected Log level " . $count [ 'Level' ] );
next ;
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
if ( $levelCount [ 1 ] && $count [ 'LevelCount' ] >= $levelCount [ 1 ] ) {
2016-04-02 01:02:31 +08:00
$state = 'alarm' ;
break ;
2016-05-12 22:17:41 +08:00
} elseif ( $levelCount [ 0 ] && $count [ 'LevelCount' ] >= $levelCount [ 0 ] ) {
2016-04-02 01:02:31 +08:00
$state = 'alert' ;
}
}
return ( $state );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function isVector ( & $array ) {
2016-04-02 01:02:31 +08:00
$next_key = 0 ;
2016-05-12 22:17:41 +08:00
foreach ( array_keys ( $array ) as $key ) {
2016-04-02 01:02:31 +08:00
if ( ! is_int ( $key ) )
return ( false );
if ( $key != $next_key ++ )
return ( false );
}
return ( true );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function checkJsonError ( $value ) {
if ( function_exists ( 'json_last_error' ) ) {
2016-04-02 01:02:31 +08:00
$value = var_export ( $value , true );
2016-05-12 22:17:41 +08:00
switch ( json_last_error () ) {
2016-04-02 01:02:31 +08:00
case JSON_ERROR_DEPTH :
Fatal ( " Unable to decode JSON string ' $value ', maximum stack depth exceeded " );
case JSON_ERROR_CTRL_CHAR :
Fatal ( " Unable to decode JSON string ' $value ', unexpected control character found " );
case JSON_ERROR_STATE_MISMATCH :
Fatal ( " Unable to decode JSON string ' $value ', invalid or malformed JSON " );
case JSON_ERROR_SYNTAX :
Fatal ( " Unable to decode JSON string ' $value ', syntax error " );
default :
Fatal ( " Unable to decode JSON string ' $value ', unexpected error " . json_last_error () );
case JSON_ERROR_NONE :
break ;
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function jsonEncode ( & $value ) {
if ( function_exists ( 'json_encode' ) ) {
2016-04-02 01:02:31 +08:00
$string = json_encode ( $value );
checkJsonError ( $value );
return ( $string );
}
2016-05-12 22:17:41 +08:00
switch ( gettype ( $value ) ) {
2016-04-02 01:02:31 +08:00
case 'double' :
case 'integer' :
return ( $value );
case 'boolean' :
return ( $value ? 'true' : 'false' );
case 'string' :
return ( '"' . preg_replace ( " / \r ? \n / " , '\\n' , addcslashes ( $value , '"\\/' ) ) . '"' );
case 'NULL' :
return ( 'null' );
case 'object' :
return ( '"Object ' . addcslashes ( get_class ( $value ), '"\\/' ) . '"' );
case 'array' :
if ( isVector ( $value ) )
return ( '[' . join ( ',' , array_map ( 'jsonEncode' , $value ) ) . ']' );
2016-05-12 22:17:41 +08:00
else {
2016-04-02 01:02:31 +08:00
$result = '{' ;
2016-05-12 22:17:41 +08:00
foreach ( $value as $subkey => $subvalue ) {
2016-04-02 01:02:31 +08:00
if ( $result != '{' )
$result .= ',' ;
$result .= '"' . $subkey . '":' . jsonEncode ( $subvalue );
}
return ( $result . '}' );
}
default :
return ( '"' . addcslashes ( gettype ( $value ), '"\\/' ) . '"' );
}
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function jsonDecode ( $value ) {
if ( function_exists ( 'json_decode' ) ) {
2016-04-02 01:02:31 +08:00
$object = json_decode ( $value , true );
checkJsonError ( $value );
return ( $object );
}
$comment = false ;
$unescape = false ;
$out = '$result=' ;
2016-05-12 22:17:41 +08:00
for ( $i = 0 ; $i < strlen ( $value ); $i ++ ) {
if ( ! $comment ) {
if ( ( $value [ $i ] == '{' ) || ( $value [ $i ] == '[' ) ) {
2016-04-02 01:02:31 +08:00
$out .= ' array(' ;
2016-05-12 22:17:41 +08:00
} else if ( ( $value [ $i ] == '}' ) || ( $value [ $i ] == ']' ) ) {
$out .= ')' ;
} else if ( $value [ $i ] == ':' ) {
$out .= '=>' ;
} else {
$out .= $value [ $i ];
}
} else if ( ! $unescape ) {
if ( $value [ $i ] == '\\' )
$unescape = true ;
else
$out .= $value [ $i ];
} else {
if ( $value [ $i ] != '/' )
$out .= '\\' ;
$out .= $value [ $i ];
$unescape = false ;
}
if ( $value [ $i ] == '"' ) {
$comment = ! $comment ;
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
eval ( $out . ';' );
return ( $result );
2013-05-02 22:20:06 +08:00
}
define ( 'HTTP_STATUS_OK' , 200 );
define ( 'HTTP_STATUS_BAD_REQUEST' , 400 );
define ( 'HTTP_STATUS_FORBIDDEN' , 403 );
2016-05-12 22:17:41 +08:00
function ajaxError ( $message , $code = HTTP_STATUS_OK ) {
2016-04-02 01:02:31 +08:00
Error ( $message );
if ( function_exists ( 'ajaxCleanup' ) )
ajaxCleanup ();
2016-05-12 22:17:41 +08:00
if ( $code == HTTP_STATUS_OK ) {
2016-04-02 01:02:31 +08:00
$response = array ( 'result' => 'Error' , 'message' => $message );
header ( " Content-type: text/plain " );
exit ( jsonEncode ( $response ) );
}
header ( " HTTP/1.0 $code $message " );
exit ();
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function ajaxResponse ( $result = false ) {
2016-04-02 01:02:31 +08:00
if ( function_exists ( 'ajaxCleanup' ) )
ajaxCleanup ();
$response = array ( 'result' => 'Ok' );
if ( is_array ( $result ) )
$response = array_merge ( $response , $result );
elseif ( ! empty ( $result ) )
$response [ 'message' ] = $result ;
header ( " Content-type: text/plain " );
exit ( jsonEncode ( $response ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function generateConnKey () {
2016-04-02 01:02:31 +08:00
return ( rand ( 1 , 999999 ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function detaintPath ( $path ) {
2016-04-02 01:02:31 +08:00
// Remove any absolute paths, or relative ones that want to go up
$path = preg_replace ( '/\.(?:\.+[\\/][\\/]*)+/' , '' , $path );
$path = preg_replace ( '/^[\\/]+/' , '' , $path );
return ( $path );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function getSkinFile ( $file ) {
2016-04-02 01:02:31 +08:00
global $skinBase ;
$skinFile = false ;
2016-05-12 22:17:41 +08:00
foreach ( $skinBase as $skin ) {
2016-04-02 01:02:31 +08:00
$tempSkinFile = detaintPath ( 'skins' . '/' . $skin . '/' . $file );
if ( file_exists ( $tempSkinFile ) )
$skinFile = $tempSkinFile ;
}
return ( $skinFile );
2013-05-02 22:20:06 +08:00
}
function getSkinIncludes ( $file , $includeBase = false , $asOverride = false )
{
2016-04-02 01:02:31 +08:00
global $skinBase ;
$skinFile = false ;
2016-05-12 22:17:41 +08:00
foreach ( $skinBase as $skin ) {
2016-04-02 01:02:31 +08:00
$tempSkinFile = detaintPath ( 'skins' . '/' . $skin . '/' . $file );
if ( file_exists ( $tempSkinFile ) )
$skinFile = $tempSkinFile ;
}
$includeFiles = array ();
2016-05-12 22:17:41 +08:00
if ( $asOverride ) {
2016-04-02 01:02:31 +08:00
if ( $skinFile )
$includeFiles [] = $skinFile ;
else if ( $includeBase )
$includeFiles [] = $file ;
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
if ( $includeBase )
$includeFiles [] = $file ;
if ( $skinFile )
$includeFiles [] = $skinFile ;
}
return ( $includeFiles );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function requestVar ( $name , $default = " " ) {
2016-04-02 01:02:31 +08:00
return ( isset ( $_REQUEST [ $name ]) ? validHtmlStr ( $_REQUEST [ $name ]) : $default );
2013-05-02 22:20:06 +08:00
}
// For numbers etc in javascript or tags etc
2016-05-12 22:17:41 +08:00
function validInt ( $input ) {
2016-04-02 01:02:31 +08:00
return ( preg_replace ( '/\D/' , '' , $input ) );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function validNum ( $input ) {
2016-04-02 01:02:31 +08:00
return ( preg_replace ( '/[^\d.-]/' , '' , $input ) );
2013-05-02 22:20:06 +08:00
}
// For general strings
2016-05-12 22:17:41 +08:00
function validStr ( $input ) {
2016-04-02 01:02:31 +08:00
return ( strip_tags ( $input ) );
2013-05-02 22:20:06 +08:00
}
// For strings in javascript or tags etc, expected to be in quotes so further quotes escaped rather than converted
2016-05-12 22:17:41 +08:00
function validJsStr ( $input ) {
2016-04-02 01:02:31 +08:00
return ( strip_tags ( addslashes ( $input ) ) );
2013-05-02 22:20:06 +08:00
}
// For general text in pages outside of tags or quotes so quotes converted to entities
2016-05-12 22:17:41 +08:00
function validHtmlStr ( $input ) {
2016-04-02 01:02:31 +08:00
return ( htmlspecialchars ( $input , ENT_QUOTES ) );
2013-05-02 22:20:06 +08:00
}
2016-04-08 23:06:34 +08:00
function getStreamHTML ( $monitor , $scale = 100 ) {
2016-05-12 22:17:41 +08:00
//FIXME, the width and height of the image need to be scaled.
if ( ZM_WEB_STREAM_METHOD == 'mpeg' && ZM_MPEG_LIVE_FORMAT ) {
$streamSrc = $monitor -> getStreamSrc ( array ( " mode=mpeg " , " scale= " . $scale , " bitrate= " . ZM_WEB_VIDEO_BITRATE , " maxfps= " . ZM_WEB_VIDEO_MAXFPS , " format= " . ZM_MPEG_LIVE_FORMAT ) );
2016-06-15 00:38:37 +08:00
return getVideoStream ( " liveStream " . $monitor -> Id (), $streamSrc , reScale ( $monitor -> Width (), $scale ), reScale ( $monitor -> Height (), $scale ), ZM_MPEG_LIVE_FORMAT , $monitor -> Name () );
2016-05-12 22:17:41 +08:00
} else if ( canStream () ) {
$streamSrc = $monitor -> getStreamSrc ( array ( 'mode=jpeg' , 'scale=' . $scale , 'maxfps=' . ZM_WEB_VIDEO_MAXFPS , 'buffer=' . $monitor -> StreamReplayBuffer () ) );
if ( canStreamNative () )
2016-06-15 00:38:37 +08:00
return getImageStream ( " liveStream " . $monitor -> Id (), $streamSrc , reScale ( $monitor -> Width (), $scale ), reScale ( $monitor -> Height (), $scale ), $monitor -> Name () );
2016-05-12 22:17:41 +08:00
elseif ( canStreamApplet () )
2016-06-15 00:38:37 +08:00
return getHelperStream ( " liveStream " . $monitor -> Id (), $streamSrc , reScale ( $monitor -> Width (), $scale ), reScale ( $monitor -> Height (), $scale ), $monitor -> Name () );
2016-05-12 22:17:41 +08:00
} else {
$streamSrc = $monitor -> getStreamSrc ( array ( 'mode=single' , " scale= " . $scale ) );
Info ( " The system has fallen back to single jpeg mode for streaming. Consider enabling Cambozola or upgrading the client browser. " );
2016-06-15 00:38:37 +08:00
return getImageStill ( " liveStream " . $monitor -> Id (), $streamSrc , reScale ( $monitor -> Width (), $scale ), reScale ( $monitor -> Height (), $scale ), $monitor -> Name () );
2016-05-12 22:17:41 +08:00
}
2016-04-08 23:06:34 +08:00
} // end function getStreamHTML
2016-06-21 00:40:33 +08:00
function getStreamMode ( ) {
$streamMode = '' ;
if ( ZM_WEB_STREAM_METHOD == 'mpeg' && ZM_MPEG_LIVE_FORMAT ) {
$streamMode = 'mpeg' ;
} elseif ( canStream () ) {
$streamMode = 'jpeg' ;
} else {
$streamMode = 'single' ;
Info ( " The system has fallen back to single jpeg mode for streaming. Consider enabling Cambozola or upgrading the client browser. " );
}
} // end function getStreamMode
2013-05-02 22:20:06 +08:00
?>