2013-05-02 22:20:06 +08:00
< ? php
//
// ZoneMinder web function library, $Date$, $Revision$
// Copyright (C) 2001-2008 Philip Coombes
2020-07-04 21:09:24 +08:00
//
2013-05-02 22:20:06 +08:00
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
2020-07-04 21:09:24 +08:00
//
2013-05-02 22:20:06 +08:00
// 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.
2020-07-04 21:09:24 +08:00
//
2013-05-02 22:20:06 +08:00
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
2016-12-26 23:23:16 +08:00
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2020-07-04 21:09:24 +08:00
//
2020-08-17 01:02:34 +08:00
//
2020-08-18 04:57:14 +08:00
require_once ( 'Filter.php' );
2020-08-17 01:02:34 +08:00
require_once ( 'FilterTerm.php' );
2013-05-02 22:20:06 +08:00
// Compatibility functions
2019-10-22 01:18:09 +08:00
if ( version_compare ( phpversion (), '4.3.0' , '<' ) ) {
2016-04-02 01:02:31 +08:00
function ob_get_clean () {
$buffer = ob_get_contents ();
ob_end_clean ();
2019-10-22 01:18:09 +08:00
return $buffer ;
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
function noCacheHeaders () {
2017-05-30 21:10:41 +08:00
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
}
2019-01-16 22:59:58 +08:00
function CSPHeaders ( $view , $nonce ) {
2020-03-05 00:03:07 +08:00
global $Servers ;
if ( ! $Servers )
$Servers = ZM\Server :: find ();
2020-04-05 04:57:52 +08:00
2020-08-10 10:18:07 +08:00
$additionalScriptSrc = implode ( ' ' , array_map ( function ( $S ){ return $S -> Hostname ();}, $Servers ));
2019-01-16 22:59:58 +08:00
switch ( $view ) {
2019-01-19 22:47:04 +08:00
case 'login' : {
if ( defined ( 'ZM_OPT_USE_GOOG_RECAPTCHA' )
&& defined ( 'ZM_OPT_GOOG_RECAPTCHA_SITEKEY' )
&& defined ( 'ZM_OPT_GOOG_RECAPTCHA_SECRETKEY' )
&& ZM_OPT_USE_GOOG_RECAPTCHA && ZM_OPT_GOOG_RECAPTCHA_SITEKEY && ZM_OPT_GOOG_RECAPTCHA_SECRETKEY ) {
2020-04-05 04:57:52 +08:00
$additionalScriptSrc .= ' https://www.google.com' ;
2019-01-19 22:47:04 +08:00
}
// fall through
}
2019-01-17 03:04:24 +08:00
case 'bandwidth' :
2019-01-18 22:51:06 +08:00
case 'blank' :
2019-01-22 00:11:40 +08:00
case 'console' :
2019-01-19 22:41:53 +08:00
case 'controlcap' :
2019-01-25 21:34:29 +08:00
case 'cycle' :
case 'donate' :
2019-02-09 17:16:32 +08:00
case 'download' :
2019-01-25 21:34:29 +08:00
case 'error' :
2019-02-10 09:34:59 +08:00
case 'events' :
2019-02-09 18:01:26 +08:00
case 'export' :
2019-02-10 07:10:42 +08:00
case 'frame' :
2019-01-17 03:04:24 +08:00
case 'function' :
case 'log' :
case 'logout' :
2019-01-25 21:34:29 +08:00
case 'optionhelp' :
2019-01-17 03:04:24 +08:00
case 'options' :
2019-01-24 11:45:57 +08:00
case 'plugin' :
2019-01-25 21:34:29 +08:00
case 'postlogin' :
2019-01-18 22:51:06 +08:00
case 'privacy' :
2019-01-25 21:34:29 +08:00
case 'server' :
case 'state' :
case 'status' :
2019-01-19 22:41:53 +08:00
case 'storage' :
2019-01-17 03:04:24 +08:00
case 'version' : {
2019-01-16 22:59:58 +08:00
// Enforce script-src on pages where inline scripts and event handlers have been fixed.
2020-09-11 04:25:20 +08:00
header ( " Content-Security-Policy: script-src 'self' 'nonce- $nonce ' $additionalScriptSrc " );
2019-01-16 22:59:58 +08:00
break ;
}
default : {
// Use Report-Only mode on all other pages.
2020-09-11 04:25:20 +08:00
header ( " Content-Security-Policy-Report-Only: script-src 'self' 'nonce- $nonce ' $additionalScriptSrc ; " .
2019-11-09 04:18:08 +08:00
( ZM_CSP_REPORT_URI ? ' report-uri ' . ZM_CSP_REPORT_URI : '' )
);
2019-01-16 22:59:58 +08:00
break ;
}
}
}
2015-12-02 23:05:03 +08:00
function CORSHeaders () {
2018-09-15 02:52:33 +08:00
if ( isset ( $_SERVER [ 'HTTP_ORIGIN' ]) ) {
2016-04-02 01:02:31 +08:00
# The following is left for future reference/use.
$valid = false ;
2020-03-05 00:03:07 +08:00
global $Servers ;
if ( ! $Servers )
$Servers = ZM\Server :: find ();
2018-10-30 00:50:50 +08:00
if ( sizeof ( $Servers ) < 1 ) {
2016-04-02 01:02:31 +08:00
# Only need CORSHeaders in the event that there are multiple servers in use.
2018-10-11 02:01:36 +08:00
# ICON: Might not be true. multi-port?
2019-01-17 02:44:57 +08:00
if ( ZM_MIN_STREAMING_PORT ) {
2020-10-14 22:39:25 +08:00
ZM\Debug ( 'Setting default Access-Control-Allow-Origin from ' . $_SERVER [ 'HTTP_ORIGIN' ]);
2019-01-17 02:44:57 +08:00
header ( 'Access-Control-Allow-Origin: ' . $_SERVER [ 'HTTP_ORIGIN' ]);
header ( 'Access-Control-Allow-Headers: x-requested-with,x-request' );
}
2016-04-02 01:02:31 +08:00
return ;
}
2019-10-22 01:18:09 +08:00
foreach ( $Servers as $Server ) {
2018-11-16 01:23:52 +08:00
if (
preg_match ( '/^(https?:\/\/)?' . preg_quote ( $Server -> Hostname (), '/' ) . '/i' , $_SERVER [ 'HTTP_ORIGIN' ])
or
preg_match ( '/^(https?:\/\/)?' . preg_quote ( $Server -> Name (), '/' ) . '/i' , $_SERVER [ 'HTTP_ORIGIN' ])
) {
2017-01-19 10:12:54 +08:00
$valid = true ;
2020-10-14 22:39:25 +08:00
ZM\Debug ( 'Setting Access-Control-Allow-Origin from ' . $_SERVER [ 'HTTP_ORIGIN' ]);
2018-09-15 02:52:33 +08:00
header ( 'Access-Control-Allow-Origin: ' . $_SERVER [ 'HTTP_ORIGIN' ]);
2017-05-30 21:10:41 +08:00
header ( 'Access-Control-Allow-Headers: x-requested-with,x-request' );
2018-09-15 02:56:26 +08:00
break ;
2016-04-02 01:02:31 +08:00
}
}
2018-09-15 02:56:26 +08:00
if ( ! $valid ) {
2019-02-22 22:19:07 +08:00
ZM\Warning ( $_SERVER [ 'HTTP_ORIGIN' ] . ' is not found in servers list.' );
2016-04-02 01:02:31 +08:00
}
}
}
2016-05-12 22:17:41 +08:00
function getMimeType ( $file ) {
if ( function_exists ( 'mime_content_type' ) ) {
2019-10-22 01:18:09 +08:00
return mime_content_type ( $file );
2016-05-12 22:17:41 +08:00
} elseif ( function_exists ( 'finfo_file' ) ) {
2019-10-22 01:18:09 +08:00
$finfo = finfo_open ( FILEINFO_MIME );
$mimeType = finfo_file ( $finfo , $file );
2016-04-02 01:02:31 +08:00
finfo_close ( $finfo );
2019-10-22 01:18:09 +08:00
return $mimeType ;
2016-04-02 01:02:31 +08:00
}
2019-09-18 23:10:25 +08:00
return trim ( exec ( 'file -bi ' . escapeshellarg ( $file ) . ' 2>/dev/null' ));
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function outputVideoStream ( $id , $src , $width , $height , $format , $title = '' ) {
echo getVideoStreamHTML ( $id , $src , $width , $height , $format , $title );
2016-04-29 22:44:46 +08:00
}
2019-10-22 01:18:09 +08:00
function getVideoStreamHTML ( $id , $src , $width , $height , $format , $title = '' ) {
2016-04-29 22:44:46 +08:00
$html = '' ;
$width = validInt ( $width );
$height = validInt ( $height );
$title = validHtmlStr ( $title );
2019-09-18 23:10:25 +08:00
if ( file_exists ( $src ) ) {
$mimeType = getMimeType ( $src );
2016-04-29 22:44:46 +08:00
} else {
switch ( $format ) {
2016-04-02 01:02:31 +08:00
case 'asf' :
2017-05-30 21:10:41 +08:00
$mimeType = 'video/x-ms-asf' ;
2016-04-02 01:02:31 +08:00
break ;
case 'avi' :
case 'wmv' :
2017-05-30 21:10:41 +08:00
$mimeType = 'video/x-msvideo' ;
2016-04-02 01:02:31 +08:00
break ;
case 'mov' :
2017-05-30 21:10:41 +08:00
$mimeType = 'video/quicktime' ;
2016-04-02 01:02:31 +08:00
break ;
case 'mpg' :
case 'mpeg' :
2017-05-30 21:10:41 +08:00
$mimeType = 'video/mpeg' ;
2016-04-02 01:02:31 +08:00
break ;
case 'swf' :
2017-05-30 21:10:41 +08:00
$mimeType = 'application/x-shockwave-flash' ;
2016-04-02 01:02:31 +08:00
break ;
case '3gp' :
2017-05-30 21:10:41 +08:00
$mimeType = 'video/3gpp' ;
2016-04-02 01:02:31 +08:00
break ;
default :
2020-09-06 00:49:14 +08:00
$mimeType = 'video/' . $format ;
2016-04-02 01:02:31 +08:00
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 ) {
2017-05-30 21:10:41 +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 "
2018-02-01 03:35:23 +08:00
codebase = " '.ZM_BASE_PROTOCOL.'://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902 "
2016-04-02 01:02:31 +08:00
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 .' "
2018-02-01 03:35:23 +08:00
pluginspage = " '.ZM_BASE_PROTOCOL.'://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
}
2017-05-30 21:10:41 +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 "
2018-02-01 03:35:23 +08:00
codebase = " '.ZM_BASE_PROTOCOL.'://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 .' "
2018-02-01 03:35:23 +08:00
pluginspage = " '.ZM_BASE_PROTOCOL.'://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 > ' ;
2017-05-30 21:10:41 +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 "
2018-02-01 03:35:23 +08:00
codebase = " '.ZM_BASE_PROTOCOL.'://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 .' "
2018-02-01 03:35:23 +08:00
pluginspage = " '.ZM_BASE_PROTOCOL.'://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 > ' ;
} # 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
}
2017-05-19 23:20:33 +08:00
function outputImageStream ( $id , $src , $width , $height , $title = '' ) {
2017-11-09 00:17:30 +08:00
echo getImageStreamHTML ( $id , $src , $width , $height , $title );
2016-04-29 22:44:46 +08:00
}
2019-03-22 02:14:30 +08:00
// width and height MUST be valid and include the px
2017-11-09 00:17:30 +08:00
function getImageStreamHTML ( $id , $src , $width , $height , $title = '' ) {
2016-04-02 01:02:31 +08:00
if ( canStreamIframe () ) {
2017-03-02 04:26:40 +08:00
return '<iframe id="' . $id . '" src="' . $src . '" alt="' . validHtmlStr ( $title ) . '" ' . ( $width ? ' width="' . validInt ( $width ) . '"' : '' ) . ( $height ? ' height="' . validInt ( $height ) . '"' : '' ) . '/>' ;
2016-04-02 01:02:31 +08:00
} else {
2019-03-05 02:35:40 +08:00
return '<img id="' . $id . '" src="' . $src . '" alt="' . validHtmlStr ( $title ) . '" style="' . ( $width ? 'width:' . $width . ';' : '' ) . ( $height ? ' height:' . $height . ';' : '' ) . '"/>' ;
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function outputControlStream ( $src , $width , $height , $monitor , $scale , $target ) {
2013-05-02 22:20:06 +08:00
?>
2019-02-10 14:12:36 +08:00
< form name = " ctrlForm " method = " post " action = " ? " target = " <?php echo $target ?> " >
2019-09-18 23:10:25 +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 " />
2017-05-30 23:14:22 +08:00
< ? php
if ( $monitor [ 'CanMoveMap' ] ) {
?>
2019-09-18 23:10:25 +08:00
< input type = " hidden " name = " control " value = " moveMap " />
2017-05-30 23:14:22 +08:00
< ? php
2019-09-18 23:10:25 +08:00
} else if ( $monitor [ 'CanMoveRel' ] ) {
2017-05-30 23:14:22 +08:00
?>
2019-09-18 23:10:25 +08:00
< input type = " hidden " name = " control " value = " movePseudoMap " />
2017-05-30 23:14:22 +08:00
< ? php
2019-09-18 23:10:25 +08:00
} else if ( $monitor [ 'CanMoveCon' ] ) {
2017-05-30 23:14:22 +08:00
?>
2019-09-18 23:10:25 +08:00
< input type = " hidden " name = " control " value = " moveConMap " />
2017-05-30 23:14:22 +08:00
< ? php
}
?>
2019-09-18 23:10:25 +08:00
< input type = " hidden " name = " scale " value = " <?php echo $scale ?> " />
2016-04-02 01:02:31 +08:00
< 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
}
2019-10-22 01:18:09 +08:00
function outputHelperStream ( $id , $src , $width , $height , $title = '' ) {
echo getHelperStream ( $id , $src , $width , $height , $title );
2016-05-25 03:10:37 +08:00
}
2019-10-22 01:18:09 +08:00
function getHelperStream ( $id , $src , $width , $height , $title = '' ) {
2017-06-29 03:17:55 +08:00
return '<object type="application/x-java-applet" id="' . $id . ' " code= " com . charliemouse . cambozola . Viewer "
2020-07-04 21:09:24 +08:00
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 .' " />
2017-06-29 03:17:55 +08:00
</ object > ' ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function outputImageStill ( $id , $src , $width , $height , $title = '' ) {
echo getImageStill ( $id , $src , $width , $height , $title = '' );
}
function getImageStill ( $id , $src , $width , $height , $title = '' ) {
return '<img id="' . $id . '" src="' . $src . '" alt="' . $title . '"' .
( validInt ( $width ) ? ' width="' . $width . '"' : '' ) .
( validInt ( $height ) ? ' height="' . $height . '"' : '' ) . '/>' ;
}
function getWebSiteUrl ( $id , $src , $width , $height , $title = '' ) {
# Prevent unsightly warnings when php cannot verify the ssl certificate
stream_context_set_default ( [
'ssl' => [
'verify_peer' => false ,
'verify_peer_name' => false ,
],
]);
# The End User can turn off the following warning under Options -> Web
if ( ZM_WEB_XFRAME_WARN ) {
$header = get_headers ( $src , 1 );
# If the target website has set X-Frame-Options, check it for "sameorigin" and warn the end user
if ( array_key_exists ( 'X-Frame-Options' , $header ) ) {
$header = $header [ 'X-Frame-Options' ];
if ( stripos ( $header , 'sameorigin' ) === 0 )
2020-09-06 00:49:14 +08:00
ZM\Warning ( 'Web site ' . $src . ' has X-Frame-Options set to sameorigin. An X-Frame-Options browser plugin is required to display this site.' );
2018-04-27 05:18:36 +08:00
}
2019-10-22 01:18:09 +08:00
}
return '<object id="' . $id . '" data="' . $src . '" alt="' . $title . '" width="' . $width . '" height="' . $height . '"></object>' ;
2018-04-27 05:18:36 +08:00
}
2019-10-22 01:18:09 +08:00
function outputControlStill ( $src , $width , $height , $monitor , $scale , $target ) {
2016-04-02 01:02:31 +08:00
?>
2019-02-10 14:12:36 +08:00
< form name = " ctrlForm " method = " post " action = " ? " target = " <?php echo $target ?> " >
2019-09-18 23:10:25 +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-04-02 01:02:31 +08:00
< ? php
2016-05-12 22:17:41 +08:00
if ( $monitor [ 'CanMoveMap' ] ) {
?>
2019-09-18 23:10:25 +08:00
< input type = " hidden " name = " control " value = " moveMap " />
2016-05-12 22:17:41 +08:00
< ? php
2019-09-18 23:10:25 +08:00
} else if ( $monitor [ 'CanMoveRel' ] ) {
2016-04-02 01:02:31 +08:00
?>
2019-09-18 23:10:25 +08:00
< input type = " hidden " name = " control " value = " movePseudoMap " />
2016-05-12 22:17:41 +08:00
< ? php
2019-09-18 23:10:25 +08:00
} else if ( $monitor [ 'CanMoveCon' ] ) {
2016-04-02 01:02:31 +08:00
?>
2019-09-18 23:10:25 +08:00
< input type = " hidden " name = " control " value = " moveConMap " />
2016-05-12 22:17:41 +08:00
< ? php
}
?>
2019-09-18 23:10:25 +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 ?> " />
2017-05-30 23:14:22 +08:00
</ 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.
2019-10-22 01:18:09 +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-05-12 22:17:41 +08:00
if ( ZM_OPT_USE_AUTH ) {
2017-05-30 21:10:41 +08:00
if ( ZM_AUTH_RELAY == 'hashed' ) {
2018-06-26 01:43:08 +08:00
$zmuCommand .= ' -A ' . generateAuthHash ( false , true );
2017-05-30 21:10:41 +08:00
} elseif ( ZM_AUTH_RELAY == 'plain' ) {
$zmuCommand .= ' -U ' . escapeshellarg ( $_SESSION [ 'username' ]) . ' -P ' . escapeshellarg ( $_SESSION [ 'password' ]);
} elseif ( ZM_AUTH_RELAY == 'none' ) {
2018-06-26 01:43:08 +08:00
$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
2019-09-18 23:10:25 +08:00
return $zmuCommand ;
2013-05-02 22:20:06 +08:00
}
2019-09-18 23:10:25 +08:00
function getEventDefaultVideoPath ( $event ) {
$Event = new ZM\Event ( $event );
return $Event -> getStreamSrc ( array ( 'mode' => 'mpeg' , 'format' => 'h264' ));
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 ) {
2020-10-14 22:39:25 +08:00
ZM\Debug ( 'Deleting ' . $path );
2019-10-22 01:18:09 +08:00
if ( is_dir ( $path ) ) {
system ( escapeshellcmd ( 'rm -rf ' . $path ));
2018-05-24 21:54:45 +08:00
} else if ( file_exists ( $path ) ) {
2019-10-22 01:18:09 +08:00
unlink ( $path );
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function deleteEvent ( $event ) {
2015-02-07 00:41:37 +08:00
2016-04-02 01:02:31 +08:00
if ( empty ( $event ) ) {
2019-02-22 22:19:07 +08:00
ZM\Error ( 'Empty event passed to deleteEvent.' );
2016-04-02 01:02:31 +08:00
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
2019-10-22 01:18:09 +08:00
$event = new ZM\Event ( $event );
2016-04-02 01:02:31 +08:00
}
2015-02-07 00:41:37 +08:00
2019-12-15 23:35:43 +08:00
if ( $event -> Archived () ) {
ZM\Info ( 'Cannot delete Archived event.' );
return ;
} # end if Archived
2020-09-06 00:49:14 +08:00
global $user ;
2016-04-02 01:02:31 +08:00
if ( $user [ 'Events' ] == 'Edit' ) {
$event -> delete ();
} # CAN EDIT
2013-05-02 22:20:06 +08:00
}
2020-10-13 01:20:21 +08:00
/**
* $label must be already escaped . It can ' t be done here since it sometimes contains HTML tags .
*/
2019-10-22 01:18:09 +08:00
function makeLink ( $url , $label , $condition = 1 , $options = '' ) {
2017-05-30 21:10:41 +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>' ;
}
2019-10-22 01:18:09 +08:00
return $string ;
2013-05-02 22:20:06 +08:00
}
2020-09-11 06:06:27 +08:00
//Make it slightly easier to create a link to help text modal
function makeHelpLink ( $ohndx ) {
2020-09-11 20:51:00 +08:00
$string = ' (<a id="' . $ohndx . '" class="optionhelp" href="#">?</a>)' ;
2020-09-11 06:06:27 +08:00
return $string ;
}
2020-09-25 23:52:13 +08:00
function makeButton ( $url , $buttonValue , $condition = 1 , $options = '' ) {
$string = '<button type="button" data-on-click-this="' . $buttonValue . '"' ;
$string .= ' data-url="' . $url . '"' ;
if ( ! $condition ) {
$string .= ' disabled="disabled"' ;
}
$string .= ( $options ? ( ' ' . $options ) : '' ) . '/>' . translate ( $buttonValue ) . '</button>' . PHP_EOL ;
return $string ;
}
2019-10-22 01:18:09 +08:00
function htmlSelect ( $name , $contents , $values , $behaviours = false ) {
2017-05-30 21:10:41 +08:00
$behaviourText = '' ;
2016-05-12 21:43:37 +08:00
if ( ! empty ( $behaviours ) ) {
if ( is_array ( $behaviours ) ) {
foreach ( $behaviours as $event => $action ) {
$behaviourText .= ' ' . $event . '="' . $action . '"' ;
}
} else {
$behaviourText = ' onchange="' . $behaviours . '"' ;
}
}
2020-10-16 05:19:56 +08:00
return '<select name="' . $name . '" ' . $behaviourText . '>' . PHP_EOL . htmlOptions ( $contents , $values ) . '</select>' ;
2018-02-27 09:08:30 +08:00
}
2020-09-03 22:42:33 +08:00
function htmlOptions ( $options , $values ) {
2018-08-03 22:02:42 +08:00
$options_html = '' ;
2020-09-03 22:42:33 +08:00
$has_selected = false ;
foreach ( $options as $value => $option ) {
2018-08-03 22:02:42 +08:00
$disabled = 0 ;
$text = '' ;
if ( is_array ( $option ) ) {
if ( isset ( $option [ 'Name' ]) )
$text = $option [ 'Name' ];
else if ( isset ( $option [ 'text' ]) )
$text = $option [ 'text' ];
if ( isset ( $option [ 'disabled' ]) ) {
$disabled = $option [ 'disabled' ];
}
} else if ( is_object ( $option ) ) {
$text = $option -> Name ();
} else {
$text = $option ;
}
2020-09-04 06:21:38 +08:00
$selected = is_array ( $values ) ? in_array ( $value , $values ) : ( ! strcmp ( $value , $values ));
if ( ! $has_selected )
$has_selected = $selected ;
2020-09-03 22:42:33 +08:00
2019-10-22 01:18:09 +08:00
$options_html .= '<option value="' . htmlspecialchars ( $value , ENT_COMPAT | ENT_HTML401 , ini_get ( 'default_charset' ), false ) . '"' .
2018-08-03 22:02:42 +08:00
( $selected ? ' selected="selected"' : '' ) .
( $disabled ? ' disabled="disabled"' : '' ) .
2020-10-16 05:16:14 +08:00
'>' . htmlspecialchars ( $text , ENT_COMPAT | ENT_HTML401 , ini_get ( 'default_charset' ), false ) . '</option>' . PHP_EOL ;
2020-09-03 22:42:33 +08:00
} # end foreach options
2020-10-07 23:22:42 +08:00
if ( $values and (( ! is_array ( $values )) or count ( $values ) ) and ! $has_selected ) {
2020-10-01 22:13:50 +08:00
ZM\Warning ( 'Specified value ' . print_r ( $values , true ) . ' not in contents: ' . print_r ( $options , true ));
2016-05-12 21:43:37 +08:00
}
2018-08-03 22:02:42 +08:00
return $options_html ;
2020-09-03 22:42:33 +08:00
} # end function htmlOptions
2013-05-02 22:20:06 +08:00
2019-10-22 01:18:09 +08:00
function truncText ( $text , $length , $deslash = 1 ) {
return preg_replace ( '/^(.{' . $length . ',}?)\b.*$/' , '\\1…' , ( $deslash ? stripslashes ( $text ) : $text ));
}
2013-05-02 22:20:06 +08:00
2019-10-22 01:18:09 +08:00
function buildSelect ( $name , $contents , $behaviours = false ) {
2017-05-30 21:10:41 +08:00
$value = '' ;
2019-10-22 01:18:09 +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 ];
2019-10-22 01:18:09 +08:00
if ( ! preg_match_all ( '/\[\s*[\'"]?(\w+)["\']?\s*\]/' , $matches [ 2 ], $matches ) ) {
ZM\Fatal ( " Can't parse selector ' $name ' " );
2016-04-02 01:02:31 +08:00
}
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 ();
2017-05-30 21:10:41 +08:00
$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 {
2020-10-04 21:04:11 +08:00
$behaviourText = ' data-on-change-this="' . $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 ();
2019-10-22 01:18:09 +08:00
return $html ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +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
2020-07-26 02:28:08 +08:00
foreach ( $newValues as $key => $value ) {
2018-04-27 05:18:36 +08:00
if ( $columns && ! isset ( $columns [ $key ]) )
2016-04-02 01:02:31 +08:00
continue ;
2013-05-02 22:20:06 +08:00
2016-04-02 01:02:31 +08:00
if ( ! isset ( $types [ $key ]) )
$types [ $key ] = false ;
2018-02-26 23:29:49 +08:00
2020-07-26 02:28:08 +08:00
switch ( $types [ $key ] ) {
2016-04-02 01:02:31 +08:00
case 'set' :
2018-04-27 05:18:36 +08:00
if ( is_array ( $newValues [ $key ]) ) {
if ( ( ! isset ( $values [ $key ])) or ( join ( ',' , $newValues [ $key ]) != $values [ $key ] ) ) {
2019-02-18 00:31:28 +08:00
$changes [ $key ] = " ` $key ` = " . dbEscape ( join ( ',' , $newValues [ $key ]));
2013-05-02 22:20:06 +08:00
}
2018-04-27 05:18:36 +08:00
} else if ( ( ! isset ( $values [ $key ])) or $values [ $key ] ) {
2018-01-13 03:25:15 +08:00
$changes [ $key ] = " ` $key ` = '' " ;
2016-04-02 01:02:31 +08:00
}
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' ] );
2017-05-30 21:10:41 +08:00
$changes [ $key . 'Width' ] = $key . 'Width = ' . $imageData [ 0 ];
$changes [ $key . 'Height' ] = $key . 'Height = ' . $imageData [ 1 ];
2020-09-06 00:49:14 +08:00
$changes [ $key . 'Type' ] = $key . 'Type = \'' . $newValues [ $key ][ 'type' ] . '\'' ;
2017-05-30 21:10:41 +08:00
$changes [ $key . 'Size' ] = $key . 'Size = ' . $newValues [ $key ][ 'size' ];
2016-04-02 01:02:31 +08:00
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' ] );
2020-09-06 00:49:14 +08:00
$changes [ $key . 'Type' ] = $key . 'Type = \'' . $newValues [ $key ][ 'type' ] . '\'' ;
2017-05-30 21:10:41 +08:00
$changes [ $key . 'Size' ] = $key . 'Size = ' . $newValues [ $key ][ 'size' ];
2016-04-02 01:02:31 +08:00
ob_start ();
readfile ( $newValues [ $key ][ 'tmp_name' ] );
2017-05-30 21:10:41 +08:00
$changes [ $key ] = $key . ' = ' . dbEscape ( ob_get_contents () );
2016-04-02 01:02:31 +08:00
ob_end_clean ();
2016-05-12 22:17:41 +08:00
} else {
2017-05-30 21:10:41 +08:00
$changes [ $key ] = $key . ' = ' . dbEscape ( $value );
2016-04-02 01:02:31 +08:00
}
break ;
case 'file' :
2017-05-30 21:10:41 +08:00
$changes [ $key . 'Type' ] = $key . 'Type = ' . dbEscape ( $newValues [ $key ][ 'type' ]);
$changes [ $key . 'Size' ] = $key . 'Size = ' . dbEscape ( $newValues [ $key ][ 'size' ]);
2016-04-02 01:02:31 +08:00
ob_start ();
readfile ( $newValues [ $key ][ 'tmp_name' ] );
2020-09-06 00:49:14 +08:00
$changes [ $key ] = $key . ' = \'' . dbEscape ( ob_get_contents () ) . '\'' ;
2016-04-02 01:02:31 +08:00
ob_end_clean ();
break ;
case 'raw' :
2018-04-27 05:18:36 +08:00
if ( ( ! isset ( $values [ $key ])) or ( $values [ $key ] != $value ) ) {
2017-05-30 21:10:41 +08:00
$changes [ $key ] = $key . ' = ' . dbEscape ( $value );
2016-04-02 01:02:31 +08:00
}
break ;
2018-02-26 23:29:49 +08:00
case 'toggle' :
if ( ( ! isset ( $values [ $key ])) or $values [ $key ] != $value ) {
if ( empty ( $value ) ) {
2020-09-06 00:49:14 +08:00
$changes [ $key ] = $key . ' = 0' ;
2018-02-26 23:29:49 +08:00
} else {
2020-09-06 00:49:14 +08:00
$changes [ $key ] = $key . ' = 1' ;
2018-02-26 23:29:49 +08:00
//$changes[$key] = $key . ' = '.dbEscape(trim($value));
}
}
break ;
2018-12-29 22:53:31 +08:00
case 'integer' :
if ( ( ! isset ( $values [ $key ])) or $values [ $key ] != $value ) {
$changes [ $key ] = $key . ' = ' . intval ( $value );
}
break ;
2016-04-02 01:02:31 +08:00
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 == '' ) {
2018-01-13 03:25:15 +08:00
$changes [ $key ] = " ` $key ` = NULL " ;
2016-05-10 07:27:42 +08:00
} else {
2018-01-13 03:25:15 +08:00
$changes [ $key ] = " ` $key ` = " . dbEscape ( trim ( $value ));
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
break ;
2017-05-31 01:51:00 +08:00
} // end switch
} // end foreach newvalues
2020-07-26 02:28:08 +08:00
2019-10-22 01:18:09 +08:00
foreach ( $values as $key => $value ) {
2016-05-12 22:17:41 +08:00
if ( ! empty ( $columns [ $key ]) ) {
if ( ! empty ( $types [ $key ]) ) {
if ( $types [ $key ] == 'toggle' ) {
if ( ! isset ( $newValues [ $key ]) && ! empty ( $value ) ) {
2020-09-06 00:49:14 +08:00
$changes [ $key ] = " ` $key ` = 0 " ;
2016-04-02 01:02:31 +08:00
}
2017-05-30 23:53:21 +08:00
} else if ( $types [ $key ] == 'set' ) {
2020-09-06 00:49:14 +08:00
$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
}
2019-10-22 01:18:09 +08:00
return $changes ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function getBrowser ( & $browser , & $version ) {
2016-05-12 22:17:41 +08:00
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 {
2019-10-22 01:18:09 +08:00
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' ;
2019-10-22 01:18:09 +08:00
} else if ( 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' ;
}
2019-10-22 01:18:09 +08:00
} else if ( preg_match ( '/Safari\/([0-9.]+)/' , $_SERVER [ 'HTTP_USER_AGENT' ], $logVersion ) ) {
2016-04-02 01:02:31 +08:00
$version = $logVersion [ 1 ];
$browser = 'safari' ;
2019-10-22 01:18:09 +08:00
} else if ( 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' ;
2019-10-22 01:18:09 +08:00
} else if ( preg_match ( '/Konqueror\/([0-9.]+)/' , $_SERVER [ 'HTTP_USER_AGENT' ], $logVersion ) ) {
2016-04-02 01:02:31 +08:00
$version = $logVersion [ 1 ];
$browser = 'konqueror' ;
2019-10-22 01:18:09 +08:00
} else if ( 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 () {
2019-10-22 01:18:09 +08:00
getBrowser ( $browser , $version );
2013-05-02 22:20:06 +08:00
2019-10-22 01:18:09 +08:00
return $browser == 'mozilla' ;
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function isKonqueror () {
2019-10-22 01:18:09 +08:00
getBrowser ( $browser , $version );
2013-05-02 22:20:06 +08:00
2019-10-22 01:18:09 +08:00
return $browser == 'konqueror' ;
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function isInternetExplorer () {
2019-10-22 01:18:09 +08:00
getBrowser ( $browser , $version );
2013-05-02 22:20:06 +08:00
2019-10-22 01:18:09 +08:00
return $browser == 'ie' ;
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function isOldChrome () {
2019-10-22 01:18:09 +08:00
getBrowser ( $browser , $version );
2013-09-22 03:00:50 +08:00
2019-10-22 01:18:09 +08:00
return $browser == 'oldchrome' ;
2013-09-22 03:00:50 +08:00
}
2016-05-12 22:17:41 +08:00
function isChrome () {
2019-10-22 01:18:09 +08:00
getBrowser ( $browser , $version );
2013-05-02 22:20:06 +08:00
2019-10-22 01:18:09 +08:00
return $browser == 'chrome' ;
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function isOpera () {
2019-10-22 01:18:09 +08:00
getBrowser ( $browser , $version );
2013-05-02 22:20:06 +08:00
2019-10-22 01:18:09 +08:00
return $browser == 'opera' ;
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function isSafari () {
2019-10-22 01:18:09 +08:00
getBrowser ( $browser , $version );
2013-05-02 22:20:06 +08:00
2019-10-22 01:18:09 +08:00
return $browser == 'safari' ;
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function isWindows () {
2019-10-22 01:18:09 +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 () {
2019-10-22 01:18:09 +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)
2020-02-26 00:12:49 +08:00
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 )) ) {
2019-02-26 04:11:08 +08:00
ZM\Warning ( 'ZM_OPT_CAMBOZOLA is enabled, but the system cannot find ' . ZM_PATH_WEB . '/' . ZM_PATH_CAMBOZOLA );
2016-04-02 01:02:31 +08:00
}
2013-09-22 03:00:50 +08:00
2019-10-22 01:18:09 +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 () {
2019-10-22 01:18:09 +08:00
return canStreamNative () | canStreamApplet ();
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function packageControl ( $command ) {
$string = ZM_PATH_BIN . '/zmpkg.pl ' . escapeshellarg ( $command );
2017-05-30 21:10:41 +08:00
$string .= ' 2>/dev/null >&- <&- >/dev/null' ;
2019-10-22 01:18:09 +08:00
exec ( $string );
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function daemonControl ( $command , $daemon = false , $args = false ) {
2017-03-31 23:59:55 +08:00
$string = escapeshellcmd ( ZM_PATH_BIN ) . '/zmdc.pl ' . $command ;
2016-05-12 22:17:41 +08:00
if ( $daemon ) {
2017-03-31 23:59:55 +08:00
$string .= ' ' . $daemon ;
2016-05-12 22:17:41 +08:00
if ( $args ) {
2017-03-31 23:59:55 +08:00
$string .= ' ' . $args ;
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
2019-10-22 01:18:09 +08:00
$string = escapeshellcmd ( $string );
2017-04-19 00:43:04 +08:00
#$string .= ' 2>/dev/null >&- <&- >/dev/null';
2020-10-14 22:39:25 +08:00
ZM\Debug ( 'daemonControl ' . $string );
2019-05-31 22:34:53 +08:00
exec ( $string );
2013-05-02 22:20:06 +08:00
}
2018-04-17 23:36:14 +08:00
function zmcControl ( $monitor , $mode = false ) {
2019-05-31 22:34:53 +08:00
$Monitor = new ZM\Monitor ( $monitor );
2017-10-24 08:00:59 +08:00
return $Monitor -> zmcControl ( $mode );
2016-04-02 01:02:31 +08:00
}
2018-04-17 23:36:14 +08:00
function zmaControl ( $monitor , $mode = false ) {
2019-02-22 22:19:07 +08:00
$Monitor = new ZM\Monitor ( $monitor );
2018-04-17 23:36:14 +08:00
return $Monitor -> zmaControl ( $mode );
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 () ) {
2019-05-31 22:34:53 +08:00
$string = ZM_PATH_BIN . '/zmdc.pl status' ;
$daemon_status = shell_exec ( $string );
2016-05-12 22:17:41 +08:00
} else {
2017-05-30 21:10:41 +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
}
2019-10-22 01:18:09 +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 ();
2017-03-31 23:59:55 +08:00
$string = $daemon ;
2020-06-26 04:07:16 +08:00
if ( $args ) {
if ( is_array ( $args ) ) {
$string .= join ( ' ' , $args );
} else {
$string .= ' ' . $args ;
}
}
return ( strpos ( $daemon_status , " ' $string ' running " ) !== false );
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function zmcStatus ( $monitor ) {
2016-05-12 22:17:41 +08:00
if ( $monitor [ 'Type' ] == 'Local' ) {
2017-03-31 23:59:55 +08:00
$zmcArgs = '-d ' . $monitor [ 'Device' ];
2016-05-12 22:17:41 +08:00
} else {
2017-03-31 23:59:55 +08:00
$zmcArgs = '-m ' . $monitor [ 'Id' ];
2016-04-02 01:02:31 +08:00
}
2019-10-22 01:18:09 +08:00
return daemonStatus ( 'zmc' , $zmcArgs );
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function zmaStatus ( $monitor ) {
if ( is_array ( $monitor ) ) {
2016-04-02 01:02:31 +08:00
$monitor = $monitor [ 'Id' ];
}
2020-09-06 00:49:14 +08:00
return daemonStatus ( 'zma' , '-m ' . $monitor );
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function daemonCheck ( $daemon = false , $args = false ) {
2017-05-30 21:10:41 +08:00
$string = ZM_PATH_BIN . '/zmdc.pl check' ;
2016-05-12 22:17:41 +08:00
if ( $daemon ) {
2017-04-19 00:31:20 +08:00
$string .= ' ' . $daemon ;
2016-04-02 01:02:31 +08:00
if ( $args )
2017-04-19 00:31:20 +08:00
$string .= ' ' . $args ;
2016-04-02 01:02:31 +08:00
}
2019-10-22 01:18:09 +08:00
$string = escapeshellcmd ( $string );
$result = exec ( $string );
return preg_match ( '/running/' , $result );
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function zmcCheck ( $monitor ) {
2016-05-12 22:17:41 +08:00
if ( $monitor [ 'Type' ] == 'Local' ) {
2017-03-31 23:59:55 +08:00
$zmcArgs = '-d ' . $monitor [ 'Device' ];
2016-05-12 22:17:41 +08:00
} else {
2017-03-31 23:59:55 +08:00
$zmcArgs = '-m ' . $monitor [ 'Id' ];
2016-04-02 01:02:31 +08:00
}
2019-10-22 01:18:09 +08:00
return daemonCheck ( 'zmc' , $zmcArgs );
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function zmaCheck ( $monitor ) {
if ( is_array ( $monitor ) ) {
2016-04-02 01:02:31 +08:00
$monitor = $monitor [ 'Id' ];
}
2020-09-06 00:49:14 +08:00
return daemonCheck ( 'zma' , '-m ' . $monitor );
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function getImageSrc ( $event , $frame , $scale = SCALE_BASE , $captureOnly = false , $overwrite = false ) {
$Event = new ZM\Event ( $event );
return $Event -> getImageSrc ( $frame , $scale , $captureOnly , $overwrite );
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function viewImagePath ( $path , $querySep = '&' ) {
return '?view=image' . $querySep . 'path=' . $path ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function createListThumbnail ( $event , $overwrite = false ) {
2017-05-30 23:14:22 +08:00
# Load the frame with the highest score to use as a thumbnail
2019-10-22 01:18:09 +08:00
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' ];
2020-02-26 00:12:49 +08:00
$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' ];
2020-02-26 00:12:49 +08:00
$thumbWidth = reScale ( $event [ 'Width' ], $scale );
2016-05-12 22:17:41 +08:00
} else {
2019-10-22 01:18:09 +08:00
ZM\Fatal ( 'No thumbnail width or height specified, please check in Options->Web' );
2016-04-02 01:02:31 +08:00
}
2019-10-22 01:18:09 +08:00
$imageData = getImageSrc ( $event , $frame , $scale , false , $overwrite );
if ( ! $imageData ) {
return false ;
2016-04-02 01:02:31 +08:00
}
2017-05-30 23:14:22 +08:00
2016-04-02 01:02:31 +08:00
$thumbData = $frame ;
$thumbData [ 'Path' ] = $imageData [ 'thumbPath' ];
$thumbData [ 'Width' ] = ( int ) $thumbWidth ;
$thumbData [ 'Height' ] = ( int ) $thumbHeight ;
2019-10-22 01:18:09 +08:00
return $thumbData ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function createVideo ( $event , $format , $rate , $scale , $overwrite = false ) {
$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 ;
2016-04-02 01:02:31 +08:00
else
2019-10-22 01:18:09 +08:00
if ( version_compare ( phpversion (), '4.3.10' , '>=' ) )
$command .= ' -s ' . sprintf ( '%.2F' , ( $scale / SCALE_BASE ));
2013-05-02 22:20:06 +08:00
else
2019-10-22 01:18:09 +08:00
$command .= ' -s ' . sprintf ( '%.2f' , ( $scale / SCALE_BASE ));
2016-04-02 01:02:31 +08:00
if ( $overwrite )
2019-10-22 01:18:09 +08:00
$command .= ' -o' ;
$command = escapeshellcmd ( $command );
$result = exec ( $command , $output , $status );
2020-10-14 22:39:25 +08:00
ZM\Debug ( " generating Video $command : result( $result outptu:( " . implode ( " \n " , $output ) . " status( $status " );
2019-10-22 01:18:09 +08:00
return $status ? '' : rtrim ( $result );
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.
2020-02-26 00:12:49 +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 ++ ) {
2020-02-26 00:12:49 +08:00
$scale = func_get_arg ( $i );
2020-03-25 04:15:24 +08:00
if ( ! empty ( $scale ) && ( $scale != '0' ) && ( $scale != 'auto' ) && ( $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
}
2019-10-22 01:18:09 +08:00
return $new_dimension ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +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 ++ ) {
2019-10-22 01:18:09 +08:00
$scale = func_get_arg ( $i );
2016-04-02 01:02:31 +08:00
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
}
2019-10-22 01:18:09 +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' ]) )
2019-10-22 01:18:09 +08:00
$midSql = ' AND MonitorId IN (' . join ( ',' , preg_split ( '/["\'\s]*,["\'\s]*/' , $user [ 'MonitorIds' ])) . ')' ;
2016-04-02 01:02:31 +08:00
else
$midSql = '' ;
2019-10-22 01:18:09 +08:00
return $midSql ;
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
2019-10-22 01:18:09 +08:00
function parseSort ( $saveToSession = false , $querySep = '&' ) {
2017-12-15 21:45:49 +08:00
global $sortQuery , $sortColumn , $sortOrder , $limitQuery ; // Outputs
2019-10-22 01:18:09 +08:00
if ( isset ( $_REQUEST [ 'filter' ][ 'Query' ][ 'sort_field' ]) ) { //Handle both new and legacy filter passing
2017-12-15 21:45:49 +08:00
$_REQUEST [ 'sort_field' ] = $_REQUEST [ 'filter' ][ 'Query' ][ 'sort_field' ];
}
2019-10-22 01:18:09 +08:00
if ( isset ( $_REQUEST [ 'filter' ][ 'Query' ][ 'sort_asc' ]) ) {
2017-12-15 21:45:49 +08:00
$_REQUEST [ 'sort_asc' ] = $_REQUEST [ 'filter' ][ 'Query' ][ 'sort_asc' ];
}
2019-10-22 01:18:09 +08:00
if ( isset ( $_REQUEST [ 'filter' ][ 'Query' ][ 'limit' ]) ) {
2017-12-15 21:45:49 +08:00
$_REQUEST [ 'limit' ] = $_REQUEST [ 'filter' ][ 'Query' ][ 'limit' ];
}
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 ;
2017-05-30 21:10:41 +08:00
$_REQUEST [ 'sort_asc' ] = ( ZM_WEB_EVENT_SORT_ORDER == 'asc' );
2016-04-02 01:02:31 +08:00
}
2016-05-12 22:17:41 +08:00
switch ( $_REQUEST [ 'sort_field' ] ) {
2016-04-02 01:02:31 +08:00
case 'Id' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.Id' ;
2016-04-02 01:02:31 +08:00
break ;
case 'MonitorName' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'M.Name' ;
2016-04-02 01:02:31 +08:00
break ;
case 'Name' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.Name' ;
2016-04-02 01:02:31 +08:00
break ;
case 'Cause' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.Cause' ;
2016-04-02 01:02:31 +08:00
break ;
case 'DateTime' :
2020-11-05 02:52:50 +08:00
$sortColumn = 'E.StartDateTime' ;
$_REQUEST [ 'sort_field' ] = 'StartDateTime' ;
2017-10-26 02:11:19 +08:00
break ;
case 'DiskSpace' :
$sortColumn = 'E.DiskSpace' ;
break ;
2016-04-02 01:02:31 +08:00
case 'StartTime' :
2020-11-13 22:51:54 +08:00
# legacy
$_REQUEST [ 'sort_field' ] = 'StartDateTime' ;
$sortColumn = 'E.StartDateTime' ;
break ;
2019-05-24 22:02:15 +08:00
case 'StartDateTime' :
2020-11-05 02:52:50 +08:00
$sortColumn = 'E.StartDateTime' ;
2019-05-24 22:02:15 +08:00
break ;
2017-10-26 02:11:19 +08:00
case 'EndTime' :
2020-11-13 22:51:54 +08:00
#legacy
$_REQUEST [ 'sort_field' ] = 'EndDateTime' ;
$sortColumn = 'E.EndDateTime' ;
break ;
2019-05-24 22:02:15 +08:00
case 'EndDateTime' :
2020-11-05 02:52:50 +08:00
$sortColumn = 'E.EndDateTime' ;
2019-05-24 22:02:15 +08:00
break ;
2016-04-02 01:02:31 +08:00
case 'Length' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.Length' ;
2016-04-02 01:02:31 +08:00
break ;
case 'Frames' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.Frames' ;
2016-04-02 01:02:31 +08:00
break ;
case 'AlarmFrames' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.AlarmFrames' ;
2016-04-02 01:02:31 +08:00
break ;
case 'TotScore' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.TotScore' ;
2016-04-02 01:02:31 +08:00
break ;
case 'AvgScore' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.AvgScore' ;
2016-04-02 01:02:31 +08:00
break ;
case 'MaxScore' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.MaxScore' ;
2016-04-02 01:02:31 +08:00
break ;
2019-06-17 00:02:00 +08:00
case 'FramesFrameId' :
$sortColumn = 'F.FrameId' ;
break ;
case 'FramesType' :
$sortColumn = 'F.Type' ;
break ;
case 'FramesTimeStamp' :
$sortColumn = 'F.TimeStamp' ;
break ;
case 'FramesDelta' :
$sortColumn = 'F.Delta' ;
break ;
case 'FramesScore' :
$sortColumn = 'F.Score' ;
break ;
2016-04-02 01:02:31 +08:00
default :
2020-11-05 02:52:50 +08:00
$sortColumn = 'E.StartDateTime' ;
2016-04-02 01:02:31 +08:00
break ;
}
2020-04-10 23:13:30 +08:00
if ( ! isset ( $_REQUEST [ 'sort_asc' ]) )
2016-04-02 01:02:31 +08:00
$_REQUEST [ 'sort_asc' ] = 0 ;
2019-10-22 01:18:09 +08:00
$sortOrder = $_REQUEST [ 'sort_asc' ] ? 'asc' : 'desc' ;
2017-05-30 21:10:41 +08:00
$sortQuery = $querySep . 'sort_field=' . validHtmlStr ( $_REQUEST [ 'sort_field' ]) . $querySep . 'sort_asc=' . validHtmlStr ( $_REQUEST [ 'sort_asc' ]);
2016-04-02 01:02:31 +08:00
if ( ! isset ( $_REQUEST [ 'limit' ]) )
2017-05-30 21:10:41 +08:00
$_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' ]);
}
2017-12-15 21:45:49 +08:00
if ( $_REQUEST [ 'limit' ] != '' ) {
2019-10-22 01:18:09 +08:00
$limitQuery = '&limit=' . validInt ( $_REQUEST [ 'limit' ]);
2017-12-15 21:45:49 +08:00
}
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
2020-08-17 01:02:34 +08:00
# Historically this function has just modified the passed in filter array.
# This would normally be $_REQUEST['filter']; We don't like modifying
# request parameters. For now we will keep this behaviour, but note that we
# now return the resulting array and other code should by modified to use that.
#
# Please note that I will be removing the savetosession code as well.
2018-04-21 03:22:45 +08:00
function parseFilter ( & $filter , $saveToSession = false , $querySep = '&' ) {
2016-04-02 01:02:31 +08:00
2020-08-18 04:57:14 +08:00
$Filter = ZM\Filter :: parse ( $filter , $querySep );
2016-09-21 00:18:20 +08:00
2020-08-18 04:57:14 +08:00
$filter [ 'sql' ] = $Filter -> sql ();
2020-10-24 05:56:38 +08:00
$filter [ 'querystring' ] = $Filter -> querystring ( 'filter' , $querySep );
2020-08-18 04:57:14 +08:00
$filter [ 'hidden_fields' ] = $Filter -> hidden_fields ();
$filter [ 'pre_sql_conditions' ] = $Filter -> pre_sql_conditions ();
$filter [ 'post_sql_conditions' ] = $Filter -> post_sql_conditions ();
2020-08-17 01:02:34 +08:00
if ( $filter [ 'sql' ] )
$filter [ 'sql' ] = ' AND ( ' . $filter [ 'sql' ] . ' )' ;
2019-03-21 02:26:48 +08:00
#if ( 0 ) {
#// ICON I feel like these should be here, but not yet
#if ( isset($filter['Query']['sort_field']) ) {
#$filter['sql'] .= ' ORDER BY ' . $filter['Query']['sort_field'] . (
#( $filter['Query']['sort_asc'] ? ' ASC' : ' DESC' ) );
#}
#}
2020-08-17 01:02:34 +08:00
return $filter ;
2020-01-19 05:09:33 +08:00
} // end function parseFilter(&$filter, $saveToSession=false, $querySep='&')
2013-05-02 22:20:06 +08:00
2017-11-09 00:17:30 +08:00
// Please note that the filter is passed in by copy, so you need to use the return value from this function.
//
2019-10-22 01:18:09 +08:00
function addFilterTerm ( $filter , $position , $term = false ) {
2016-04-02 01:02:31 +08:00
if ( $position < 0 )
$position = 0 ;
2020-07-04 21:09:24 +08:00
2019-10-22 01:18:09 +08:00
if ( ! isset ( $filter [ 'Query' ][ 'terms' ]) )
2017-06-27 09:58:11 +08:00
$filter [ 'Query' ][ 'terms' ] = array ();
2019-10-22 01:18:09 +08:00
else if ( $position > count ( $filter [ 'Query' ][ 'terms' ]) )
2017-06-27 09:58:11 +08:00
$position = count ( $filter [ 'Query' ][ 'terms' ]);
2019-10-22 01:18:09 +08:00
2016-04-02 01:02:31 +08:00
if ( $term && $position == 0 )
2019-10-22 01:18:09 +08:00
unset ( $term [ 'cnj' ]);
array_splice ( $filter [ 'Query' ][ 'terms' ], $position , 0 , array ( $term ? $term : array ()));
2016-04-02 01:02:31 +08:00
2019-10-22 01:18:09 +08:00
return $filter ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function delFilterTerm ( $filter , $position ) {
2016-04-02 01:02:31 +08:00
if ( $position < 0 )
$position = 0 ;
2019-10-22 01:18:09 +08:00
else if ( $position >= count ( $filter [ 'Query' ][ 'terms' ]) )
2017-06-27 09:58:11 +08:00
$position = count ( $filter [ 'Query' ][ 'terms' ]);
2019-10-22 01:18:09 +08:00
array_splice ( $filter [ 'Query' ][ 'terms' ], $position , 1 );
2016-04-02 01:02:31 +08:00
2019-10-22 01:18:09 +08:00
return $filter ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +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
2017-05-30 21:10:41 +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 ;
2019-10-22 01:18:09 +08:00
array_unshift ( $newPages , $newPage );
2016-04-02 01:02:31 +08:00
}
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
}
2019-09-25 22:35:57 +08:00
} # end if page > 1
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 ;
2019-10-22 01:18:09 +08:00
array_push ( $newPages , $newPage );
2016-04-02 01:02:31 +08:00
}
if ( ! isset ( $pagesUsed [ $pages ]) )
2019-10-22 01:18:09 +08:00
array_push ( $newPages , $pages );
2016-04-02 01:02:31 +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>' ;
}
$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>' ;
}
2019-09-25 22:35:57 +08:00
} # end if $page < $pages
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
2019-09-25 22:35:57 +08:00
return $pageText ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function sortHeader ( $field , $querySep = '&' ) {
2016-04-02 01:02:31 +08:00
global $view ;
2019-08-09 01:34:10 +08:00
return implode ( $querySep , array (
'?view=' . $view ,
2019-12-03 01:30:15 +08:00
'page=1' . ( isset ( $_REQUEST [ 'filter' ]) ? $_REQUEST [ 'filter' ][ 'query' ] : '' ),
2019-08-09 01:34:10 +08:00
'sort_field=' . $field ,
'sort_asc=' . ( $_REQUEST [ 'sort_field' ] == $field ? ! $_REQUEST [ 'sort_asc' ] : 0 ),
'limit=' . validInt ( $_REQUEST [ 'limit' ]),
2019-08-20 22:28:19 +08:00
( isset ( $_REQUEST [ 'eid' ]) ? 'eid=' . $_REQUEST [ 'eid' ] : '' ),
2019-08-09 01:34:10 +08:00
));
2013-05-02 22:20:06 +08:00
}
2020-08-18 06:30:44 +08:00
function sortTag ( $field ) {
if ( isset ( $_REQUEST [ 'sort_field' ]) ) {
if ( $_REQUEST [ 'sort_field' ] == $field )
if ( $_REQUEST [ 'sort_asc' ] )
return '(^)' ;
else
return '(v)' ;
}
2019-10-22 01:18:09 +08:00
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 ();
2019-10-22 01:18:09 +08:00
return $load [ 0 ];
2013-05-02 22:20:06 +08:00
}
2016-11-11 21:47:08 +08:00
function getDiskPercent ( $path = ZM_DIR_EVENTS ) {
$total = disk_total_space ( $path );
2017-02-22 02:33:05 +08:00
if ( $total === false ) {
2020-06-04 05:41:03 +08:00
ZM\Error ( 'disk_total_space returned false. Verify the web account user has access to ' . $path );
2016-05-12 22:17:41 +08:00
return 0 ;
2017-02-22 03:10:41 +08:00
} elseif ( $total == 0 ) {
2020-06-04 05:41:03 +08:00
ZM\Error ( 'disk_total_space indicates the following path has a filesystem size of zero bytes ' . $path );
2017-02-22 03:10:41 +08:00
return 100 ;
2016-05-12 22:17:41 +08:00
}
2016-11-11 21:47:08 +08:00
$free = disk_free_space ( $path );
2017-02-22 02:33:05 +08:00
if ( $free === false ) {
2020-06-04 05:41:03 +08:00
ZM\Error ( 'disk_free_space returned false. Verify the web account user has access to ' . $path );
2016-05-12 22:17:41 +08:00
}
2017-02-22 03:10:41 +08:00
$space = round ((( $total - $free ) / $total ) * 100 );
2019-10-22 01:18:09 +08:00
return $space ;
2013-05-02 22:20:06 +08:00
}
2020-08-18 04:57:14 +08:00
function getDiskBlocks ( $path = ZM_DIR_EVENTS ) {
$df = shell_exec ( 'df ' . escapeshellarg ( $path ));
2016-04-02 01:02:31 +08:00
$space = - 1 ;
2019-10-22 01:18:09 +08:00
if ( preg_match ( '/\s(\d+)\s+\d+\s+\d+%/ms' , $df , $matches ) )
2016-04-02 01:02:31 +08:00
$space = $matches [ 1 ];
2019-10-22 01:18:09 +08:00
return $space ;
2013-05-02 22:20:06 +08:00
}
2017-10-08 06:42:39 +08:00
function systemStats () {
2019-10-22 01:18:09 +08:00
$load = getLoad ();
$diskPercent = getDiskPercent ();
$pathMapPercent = getDiskPercent ( ZM_PATH_MAP );
$cpus = getcpus ();
2017-10-08 06:42:39 +08:00
2019-10-22 01:18:09 +08:00
$normalized_load = $load / $cpus ;
2017-10-08 06:42:39 +08:00
2019-10-22 01:18:09 +08:00
# Colorize the system load stat
if ( $normalized_load <= 0.75 ) {
$htmlLoad = $load ;
} else if ( $normalized_load <= 0.9 ) {
2020-09-06 00:49:14 +08:00
$htmlLoad = '<span class="warning">' . $load . '</span>' ;
2019-10-22 01:18:09 +08:00
} else if ( $normalized_load <= 1.1 ) {
2020-09-06 00:49:14 +08:00
$htmlLoad = '<span class="error">' . $load . '</span>' ;
2019-10-22 01:18:09 +08:00
} else {
2020-09-06 00:49:14 +08:00
$htmlLoad = '<span class="critical">' . $load . '</span>' ;
2019-10-22 01:18:09 +08:00
}
2017-10-08 06:42:39 +08:00
2019-10-22 01:18:09 +08:00
# Colorize the disk space stat
if ( $diskPercent < 98 ) {
$htmlDiskPercent = $diskPercent . '%' ;
} else if ( $diskPercent <= 99 ) {
2020-09-06 00:49:14 +08:00
$htmlDiskPercent = '<span class="warning">' . $diskPercent . '%</span>' ;
2019-10-22 01:18:09 +08:00
} else {
2020-09-06 00:49:14 +08:00
$htmlDiskPercent = '<span class="error">' . $diskPercent . '%</span>' ;
2019-10-22 01:18:09 +08:00
}
2017-10-08 06:42:39 +08:00
2019-10-22 01:18:09 +08:00
# Colorize the PATH_MAP (usually /dev/shm) stat
if ( $pathMapPercent < 90 ) {
if ( disk_free_space ( ZM_PATH_MAP ) > 209715200 ) { # have to always have at least 200MiB free
$htmlPathMapPercent = $pathMapPercent . '%' ;
2017-10-08 06:42:39 +08:00
} else {
2020-09-06 00:49:14 +08:00
$htmlPathMapPercent = '<span class="warning">' . $pathMapPercent . '%</span>' ;
2017-10-08 06:42:39 +08:00
}
2019-10-22 01:18:09 +08:00
} else if ( $pathMapPercent < 100 ) {
2020-09-06 00:49:14 +08:00
$htmlPathMapPercent = '<span class="warning">' . $pathMapPercent . '%</span>' ;
2019-10-22 01:18:09 +08:00
} else {
2020-09-06 00:49:14 +08:00
$htmlPathMapPercent = '<span class="critical">' . $pathMapPercent . '%</span>' ;
2019-10-22 01:18:09 +08:00
}
2017-10-08 06:42:39 +08:00
2020-09-06 00:49:14 +08:00
$htmlString = translate ( 'Load' ) . ': ' . $htmlLoad . ' - ' . translate ( 'Disk' ) . ': ' . $htmlDiskPercent . ' - ' . ZM_PATH_MAP . ': ' . $htmlPathMapPercent ;
2017-10-08 06:42:39 +08:00
2019-10-22 01:18:09 +08:00
return $htmlString ;
2017-10-08 06:42:39 +08:00
}
function getcpus () {
2019-10-22 01:18:09 +08:00
if ( is_readable ( '/proc/cpuinfo' ) ) { # Works on Linux
2020-07-04 21:09:24 +08:00
preg_match_all ( '/^processor/m' , file_get_contents ( '/proc/cpuinfo' ), $matches );
2019-10-22 01:18:09 +08:00
$num_cpus = count ( $matches [ 0 ]);
} else { # Works on BSD
$matches = explode ( ':' , shell_exec ( 'sysctl hw.ncpu' ));
$num_cpus = trim ( $matches [ 1 ]);
}
2017-10-08 06:42:39 +08:00
2019-10-22 01:18:09 +08:00
return $num_cpus ;
2017-10-08 06:42:39 +08:00
}
2020-07-04 21:09:24 +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
2013-05-02 22:20:06 +08:00
// fieldset tag, neither of which will work with strict XHTML Basic.
2016-05-12 22:17:41 +08:00
function sidField () {
if ( SID ) {
2019-10-22 01:18:09 +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
}
2019-10-22 01:18:09 +08:00
function verNum ( $version ) {
2017-05-30 21:10:41 +08:00
$vNum = '' ;
2016-04-02 01:02:31 +08:00
$maxFields = 3 ;
2019-10-22 01:18:09 +08:00
$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 ) {
2019-10-22 01:18:09 +08:00
$vField = sprintf ( '%02d' , $vField );
2016-05-12 22:17:41 +08:00
while ( strlen ( $vField ) < 2 ) {
2017-05-30 21:10:41 +08:00
$vField = '0' . $vField ;
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
$vNum .= $vField ;
}
2019-10-22 01:18:09 +08:00
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 ;
2019-10-22 01:18:09 +08:00
$sql = 'SELECT * FROM Monitors ORDER BY Sequence ASC, Id ASC' ;
foreach ( dbFetchAll ( $sql ) as $monitor ) {
2016-05-12 22:17:41 +08:00
if ( $monitor [ 'Sequence' ] != $sequence ) {
2019-10-22 01:18:09 +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 ) {
2019-10-22 01:18:09 +08:00
if ( ! empty ( $arg ) )
return $arg ;
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function linesIntersect ( $line1 , $line2 ) {
2016-04-02 01:02:31 +08:00
global $debug ;
2019-10-22 01:18:09 +08:00
$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' ]);
2016-04-02 01:02:31 +08:00
// 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 ) {
2017-05-30 21:10:41 +08:00
if ( $debug ) echo 'Not intersecting, out of bounds<br>' ;
2019-10-22 01:18:09 +08:00
return false ;
2016-04-02 01:02:31 +08:00
}
$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> " ;
2019-10-22 01:18:09 +08:00
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> " ;
2019-10-22 01:18:09 +08:00
return false ;
2016-04-02 01:02:31 +08:00
}
2016-05-12 22:17:41 +08:00
} elseif ( $b1 == $b2 ) {
2020-07-04 21:09:24 +08:00
// Colinear, must overlap due to box check, intersect?
2017-05-30 21:10:41 +08:00
if ( $debug ) echo 'Intersecting, colinear<br>' ;
2019-10-22 01:18:09 +08:00
return true ;
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
// Parallel
2017-05-30 21:10:41 +08:00
if ( $debug ) echo 'Not intersecting, parallel<br>' ;
2019-10-22 01:18:09 +08:00
return false ;
2013-05-02 22:20:06 +08:00
}
2020-07-04 21:09:24 +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> " ;
2019-10-22 01:18:09 +08:00
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> " ;
2019-10-22 01:18:09 +08:00
return false ;
2013-05-02 22:20:06 +08:00
}
2020-07-04 21:09:24 +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> " ;
2019-10-22 01:18:09 +08:00
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> " ;
2019-10-22 01:18:09 +08:00
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' ] ) {
2020-07-04 21:09:24 +08:00
// Colinear, must overlap due to box check, intersect?
2017-05-30 21:10:41 +08:00
if ( $debug ) echo 'Intersecting, vertical, colinear<br>' ;
2019-10-22 01:18:09 +08:00
return true ;
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
// Parallel
2017-05-30 21:10:41 +08:00
if ( $debug ) echo 'Not intersecting, vertical, parallel<br>' ;
2019-10-22 01:18:09 +08:00
return false ;
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
2017-05-30 21:10:41 +08:00
if ( $debug ) echo 'Whoops, unexpected scenario<br>' ;
2019-10-22 01:18:09 +08:00
return false ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +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> " ;
2019-10-22 01:18:09 +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> " ;
2019-10-22 01:18:09 +08:00
return true ;
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
}
2016-04-02 01:02:31 +08:00
}
2019-10-22 01:18:09 +08:00
return false ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function getPolyCentre ( $points , $area = 0 ) {
2016-04-02 01:02:31 +08:00
$cx = 0.0 ;
$cy = 0.0 ;
if ( ! $area )
2019-10-22 01:18:09 +08:00
$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' ]);
2020-09-06 00:49:14 +08:00
$cx += ( $points [ $i ][ 'x' ] + $points [ $j ][ 'x' ]) * $ct ;
$cy += ( $points [ $i ][ 'y' ] + $points [ $j ][ 'y' ]) * $ct ;
2016-04-02 01:02:31 +08:00
}
$cx = intval ( round ( abs ( $cx / ( 6.0 * $area ))));
$cy = intval ( round ( abs ( $cy / ( 6.0 * $area ))));
2019-10-22 01:18:09 +08:00
return array ( 'x' => $cx , 'y' => $cy );
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function _CompareXY ( $a , $b ) {
2016-04-02 01:02:31 +08:00
if ( $a [ 'min_y' ] == $b [ 'min_y' ] )
2019-10-22 01:18:09 +08:00
return intval ( $a [ 'min_x' ] - $b [ 'min_x' ]);
2016-04-02 01:02:31 +08:00
else
2019-10-22 01:18:09 +08:00
return intval ( $a [ 'min_y' ] - $b [ 'min_y' ]);
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function _CompareX ( $a , $b ) {
return intval ( $a [ 'min_x' ] - $b [ 'min_x' ]);
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +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 (
2017-05-30 21:10:41 +08:00
'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
}
2019-10-22 01:18:09 +08:00
usort ( $global_edges , '_CompareXY' );
2016-04-02 01:02:31 +08:00
2016-05-12 22:17:41 +08:00
if ( $debug ) {
for ( $i = 0 ; $i < count ( $global_edges ); $i ++ ) {
2019-10-22 01:18:09 +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' ]);
2016-04-02 01:02:31 +08:00
}
}
$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 ) {
2019-10-22 01:18:09 +08:00
if ( $debug ) printf ( 'Moving global edge<br>' );
2016-04-02 01:02:31 +08:00
$active_edges [] = $global_edges [ $i ];
2019-10-22 01:18:09 +08:00
array_splice ( $global_edges , $i , 1 );
2016-04-02 01:02:31 +08:00
$i -- ;
2016-05-12 22:17:41 +08:00
} else {
2016-04-02 01:02:31 +08:00
break ;
}
}
2019-10-22 01:18:09 +08:00
usort ( $active_edges , '_CompareX' );
2016-05-12 22:17:41 +08:00
if ( $debug ) {
for ( $i = 0 ; $i < count ( $active_edges ); $i ++ ) {
2019-10-22 01:18:09 +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' ]);
2016-04-02 01:02:31 +08:00
}
}
$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 ;
}
2019-10-22 01:18:09 +08:00
if ( $debug ) printf ( '%d: Area:%d<br>' , $y , $row_area );
2016-04-02 01:02:31 +08:00
$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
2019-10-22 01:18:09 +08:00
if ( $debug ) printf ( 'Deleting active_edge<br>' );
array_splice ( $active_edges , $i , 1 );
2016-04-02 01:02:31 +08:00
$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 ) );
2019-10-22 01:18:09 +08:00
if ( $debug ) printf ( 'Area:%d<br>' , $area );
return $area ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +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 ;
2019-10-22 01:18:09 +08:00
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 );
2016-04-02 01:02:31 +08:00
}
$edge = intval ( round ( abs ( $edge )));
$area = intval ( round (( abs ( $area ) + $edge ) / 2 ));
echo " E: $edge <br> " ;
echo " A: $area <br> " ;
2019-10-22 01:18:09 +08:00
return $area ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function mapCoords ( $a ) {
return $a [ 'x' ] . ',' . $a [ 'y' ];
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function pointsToCoords ( $points ) {
return join ( ' ' , array_map ( 'mapCoords' , $points ));
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function coordsToPoints ( $coords ) {
2016-04-02 01:02:31 +08:00
$points = array ();
2019-10-22 01:18:09 +08:00
if ( preg_match_all ( '/(\d+,\d+)+/' , $coords , $matches ) ) {
2016-05-12 22:17:41 +08:00
for ( $i = 0 ; $i < count ( $matches [ 1 ]); $i ++ ) {
2019-10-22 01:18:09 +08:00
if ( preg_match ( '/(\d+),(\d+)/' , $matches [ 1 ][ $i ], $cmatches ) ) {
$points [] = array ( 'x' => $cmatches [ 1 ], 'y' => $cmatches [ 2 ]);
2016-05-12 22:17:41 +08:00
} else {
2020-09-06 00:49:14 +08:00
echo ( 'Bogus coordinates (' . $matches [ $i ] . ')' );
2019-10-22 01:18:09 +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 {
2020-09-06 00:49:14 +08:00
echo ( 'Bogus coordinate string ' . $coords );
2019-10-22 01:18:09 +08:00
return false ;
2016-04-02 01:02:31 +08:00
}
2019-10-22 01:18:09 +08:00
return $points ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function limitPoints ( & $points , $min_x , $min_y , $max_x , $max_y ) {
2017-04-20 04:12:12 +08:00
foreach ( $points as & $point ) {
if ( $point [ 'x' ] < $min_x ) {
2020-10-14 22:39:25 +08:00
ZM\Debug ( 'Limiting point x' . $point [ 'x' ] . ' to min_x ' . $min_x );
2017-04-20 04:12:12 +08:00
$point [ 'x' ] = $min_x ;
} else if ( $point [ 'x' ] > $max_x ) {
2020-10-14 22:39:25 +08:00
ZM\Debug ( 'Limiting point x' . $point [ 'x' ] . ' to max_x ' . $max_x );
2017-04-20 04:12:12 +08:00
$point [ 'x' ] = $max_x ;
}
2017-05-30 23:15:00 +08:00
if ( $point [ 'y' ] < $min_y ) {
2020-10-14 22:39:25 +08:00
ZM\Debug ( 'Limiting point y' . $point [ 'y' ] . ' to min_y ' . $min_y );
2017-04-20 04:12:12 +08:00
$point [ 'y' ] = $min_y ;
} else if ( $point [ 'y' ] > $max_y ) {
2020-10-14 22:39:25 +08:00
ZM\Debug ( 'Limiting point y' . $point [ 'y' ] . ' to max_y ' . $max_y );
2017-04-20 04:12:12 +08:00
$point [ 'y' ] = $max_y ;
}
} // end foreach point
} // end function limitPoints( $points, $min_x, $min_y, $max_x, $max_y )
2019-10-22 01:18:09 +08:00
function scalePoints ( & $points , $scale ) {
2017-04-24 23:11:44 +08:00
foreach ( $points as & $point ) {
2019-10-22 01:18:09 +08:00
$point [ 'x' ] = reScale ( $point [ 'x' ], $scale );
$point [ 'y' ] = reScale ( $point [ 'y' ], $scale );
2017-04-24 23:11:44 +08:00
}
}
2016-05-12 22:17:41 +08:00
function getLanguages () {
2016-04-02 01:02:31 +08:00
$langs = array ();
2017-05-30 21:10:41 +08:00
foreach ( glob ( 'lang/*_*.php' ) as $file ) {
2019-10-22 01:18:09 +08:00
preg_match ( '/([^\/]+_.+)\.php/' , $file , $matches );
2016-04-02 01:02:31 +08:00
$langs [ $matches [ 1 ]] = $matches [ 1 ];
}
2019-10-22 01:18:09 +08:00
return $langs ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function trimString ( $string , $length ) {
return preg_replace ( '/^(.{' . $length . ',}?)\b.*$/' , '\\1…' , $string );
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +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 ) {
2019-10-22 01:18:09 +08:00
$sql = 'SELECT Id, Name FROM Monitors' ;
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 ();
2017-12-05 04:52:16 +08:00
if ( ! is_array ( $ids ) ) {
2019-10-22 01:18:09 +08:00
$ids = preg_split ( '/\s*,\s*/' , $ids );
2017-12-05 04:52:16 +08:00
}
foreach ( $ids as $id ) {
2019-10-22 01:18:09 +08:00
if ( visibleMonitor ( $id ) ) {
2016-05-12 22:17:41 +08:00
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
}
2019-10-22 01:18:09 +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 ) ) {
2019-10-22 01:18:09 +08:00
$socket = socket_create ( AF_UNIX , SOCK_STREAM , 0 );
2016-05-12 22:17:41 +08:00
if ( $socket < 0 ) {
2019-10-22 01:18:09 +08:00
ZM\Fatal ( 'socket_create() failed: ' . socket_strerror ( $socket ));
2016-04-02 01:02:31 +08:00
}
$sock_file = ZM_PATH_SOCKS . '/zmx10.sock' ;
2019-10-22 01:18:09 +08:00
if ( @ socket_connect ( $socket , $sock_file ) ) {
2017-05-30 21:10:41 +08:00
$command = 'status' ;
2019-10-22 01:18:09 +08:00
if ( ! socket_write ( $socket , $command ) ) {
2020-09-06 00:49:14 +08:00
ZM\Fatal ( 'Can\'t write to control socket: ' . socket_strerror ( socket_last_error ( $socket )));
2016-04-02 01:02:31 +08:00
}
2019-10-22 01:18:09 +08:00
socket_shutdown ( $socket , 1 );
2017-05-30 21:10:41 +08:00
$x10Output = '' ;
2019-10-22 01:18:09 +08:00
while ( $x10Response = socket_read ( $socket , 256 ) ) {
2016-04-02 01:02:31 +08:00
$x10Output .= $x10Response ;
}
2019-10-22 01:18:09 +08:00
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
2019-10-22 01:18:09 +08:00
$command = ZM_PATH_BIN . '/zmx10.pl --command status' ;
2016-04-02 01:02:31 +08:00
//$command .= " 2>/dev/null >&- <&- >/dev/null";
2013-05-02 22:20:06 +08:00
2019-10-22 01:18:09 +08:00
$x10Output = exec ( escapeshellcmd ( $command ));
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +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
}
2019-10-22 01:18:09 +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 ]) )
2017-05-30 21:10:41 +08:00
$status = 'unknown' ;
2019-10-22 01:18:09 +08:00
return $status ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function setDeviceStatusX10 ( $key , $status ) {
$socket = socket_create ( AF_UNIX , SOCK_STREAM , 0 );
2016-05-12 22:17:41 +08:00
if ( $socket < 0 ) {
2020-09-06 00:49:14 +08:00
ZM\Fatal ( 'socket_create() failed: ' . socket_strerror ( $socket ));
2016-04-02 01:02:31 +08:00
}
$sock_file = ZM_PATH_SOCKS . '/zmx10.sock' ;
2019-10-22 01:18:09 +08:00
if ( @ socket_connect ( $socket , $sock_file ) ) {
2016-04-02 01:02:31 +08:00
$command = " $status ; $key " ;
2019-10-22 01:18:09 +08:00
if ( ! socket_write ( $socket , $command ) ) {
ZM\Fatal ( 'Can\'t write to control socket: ' . socket_strerror ( socket_last_error ( $socket )));
2016-04-02 01:02:31 +08:00
}
2019-10-22 01:18:09 +08:00
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
2019-10-22 01:18:09 +08:00
$command = ZM_PATH_BIN . '/zmx10.pl --command ' . escapeshellarg ( $status );
2016-04-02 01:02:31 +08:00
$command .= ' --unit-code ' . escapeshellarg ( $key );
//$command .= " 2>/dev/null >&- <&- >/dev/null";
2019-10-22 01:18:09 +08:00
$x10Response = exec ( $command );
2016-04-02 01:02:31 +08:00
}
2019-10-22 01:18:09 +08:00
if ( preg_match ( '/^' . $key . '\s+(.*)/' , $x10Response , $matches ) )
2016-04-02 01:02:31 +08:00
$status = $matches [ 1 ];
else
2017-05-30 21:10:41 +08:00
$status = 'unknown' ;
2019-10-22 01:18:09 +08:00
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 (
2019-02-22 22:19:07 +08:00
ZM\Logger :: FATAL => array ( ZM_LOG_ALERT_FAT_COUNT , ZM_LOG_ALARM_FAT_COUNT ),
ZM\Logger :: ERROR => array ( ZM_LOG_ALERT_ERR_COUNT , ZM_LOG_ALARM_ERR_COUNT ),
ZM\Logger :: WARNING => array ( ZM_LOG_ALERT_WAR_COUNT , ZM_LOG_ALARM_WAR_COUNT ),
2016-04-02 01:02:31 +08:00
);
2013-05-02 22:20:06 +08:00
2017-10-26 02:11:19 +08:00
# This is an expensive request, as it has to hit every row of the Logs Table
2019-02-22 22:19:07 +08:00
$sql = 'SELECT Level, COUNT(Level) AS LevelCount FROM Logs WHERE Level < ' . ZM\Logger :: INFO . ' AND TimeKey > unix_timestamp(now() - interval ' . ZM_LOG_CHECK_PERIOD . ' second) GROUP BY Level ORDER BY Level ASC' ;
2018-10-29 21:59:26 +08:00
$counts = dbFetchAll ( $sql );
if ( $counts ) {
foreach ( $counts as $count ) {
2019-02-22 22:19:07 +08:00
if ( $count [ 'Level' ] <= ZM\Logger :: PANIC )
$count [ 'Level' ] = ZM\Logger :: FATAL ;
2018-10-29 21:59:26 +08:00
if ( ! ( $levelCount = $levelCounts [ $count [ 'Level' ]]) ) {
2020-06-04 05:41:03 +08:00
ZM\Error ( 'Unexpected Log level ' . $count [ 'Level' ]);
2018-10-29 21:59:26 +08:00
next ;
}
if ( $levelCount [ 1 ] && $count [ 'LevelCount' ] >= $levelCount [ 1 ] ) {
$state = 'alarm' ;
break ;
} elseif ( $levelCount [ 0 ] && $count [ 'LevelCount' ] >= $levelCount [ 0 ] ) {
$state = 'alert' ;
}
2016-04-02 01:02:31 +08:00
}
}
2018-10-29 21:59:26 +08:00
return $state ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +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 ) {
2019-10-22 01:18:09 +08:00
if ( ! is_int ( $key ) )
return false ;
2016-04-02 01:02:31 +08:00
if ( $key != $next_key ++ )
2019-10-22 01:18:09 +08:00
return false ;
2016-04-02 01:02:31 +08:00
}
2019-10-22 01:18:09 +08:00
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' ) ) {
2020-07-30 04:40:31 +08:00
$value = var_export ( $value , true );
switch ( json_last_error () ) {
2016-04-02 01:02:31 +08:00
case JSON_ERROR_DEPTH :
2020-07-30 04:40:31 +08:00
ZM\Error ( " Unable to decode JSON string ' $value ', maximum stack depth exceeded " );
break ;
2016-04-02 01:02:31 +08:00
case JSON_ERROR_CTRL_CHAR :
2020-07-30 04:40:31 +08:00
ZM\Error ( " Unable to decode JSON string ' $value ', unexpected control character found " );
break ;
2016-04-02 01:02:31 +08:00
case JSON_ERROR_STATE_MISMATCH :
2020-07-30 04:40:31 +08:00
ZM\Error ( " Unable to decode JSON string ' $value ', invalid or malformed JSON " );
break ;
2016-04-02 01:02:31 +08:00
case JSON_ERROR_SYNTAX :
2020-07-30 04:40:31 +08:00
ZM\Error ( " Unable to decode JSON string ' $value ', syntax error " );
break ;
2016-04-02 01:02:31 +08:00
default :
2020-07-30 04:40:31 +08:00
ZM\Error ( " Unable to decode JSON string ' $value ', unexpected error " . json_last_error ());
break ;
2016-04-02 01:02:31 +08:00
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
}
2019-10-22 01:18:09 +08:00
function jsonEncode ( & $value ) {
2016-05-12 22:17:41 +08:00
if ( function_exists ( 'json_encode' ) ) {
2016-04-02 01:02:31 +08:00
$string = json_encode ( $value );
checkJsonError ( $value );
2019-10-22 01:18:09 +08:00
return $string ;
2016-04-02 01:02:31 +08:00
}
2016-05-12 22:17:41 +08:00
switch ( gettype ( $value ) ) {
2016-04-02 01:02:31 +08:00
case 'double' :
case 'integer' :
2019-10-22 01:18:09 +08:00
return $value ;
2016-04-02 01:02:31 +08:00
case 'boolean' :
2019-10-22 01:18:09 +08:00
return $value ? 'true' : 'false' ;
2016-04-02 01:02:31 +08:00
case 'string' :
2019-10-22 01:18:09 +08:00
return '"' . preg_replace ( " / \r ? \n / " , '\\n' , addcslashes ( $value , '"\\/' )) . '"' ;
2016-04-02 01:02:31 +08:00
case 'NULL' :
2019-10-22 01:18:09 +08:00
return 'null' ;
2016-04-02 01:02:31 +08:00
case 'object' :
2019-10-22 01:18:09 +08:00
return '"Object ' . addcslashes ( get_class ( $value ), '"\\/' ) . '"' ;
2016-04-02 01:02:31 +08:00
case 'array' :
if ( isVector ( $value ) )
2019-10-22 01:18:09 +08:00
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 .= ',' ;
2019-10-22 01:18:09 +08:00
$result .= '"' . $subkey . '":' . jsonEncode ( $subvalue );
2016-04-02 01:02:31 +08:00
}
2019-10-22 01:18:09 +08:00
return $result . '}' ;
2016-04-02 01:02:31 +08:00
}
default :
2019-10-22 01:18:09 +08:00
return '"' . addcslashes ( gettype ( $value ), '"\\/' ) . '"' ;
2016-04-02 01:02:31 +08:00
}
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function jsonDecode ( $value ) {
2016-05-12 22:17:41 +08:00
if ( function_exists ( 'json_decode' ) ) {
2019-10-22 01:18:09 +08:00
$object = json_decode ( $value , true );
2016-04-02 01:02:31 +08:00
checkJsonError ( $value );
2019-10-22 01:18:09 +08:00
return $object ;
2016-04-02 01:02:31 +08:00
}
$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 {
2020-07-04 21:09:24 +08:00
$out .= $value [ $i ];
2016-05-12 22:17:41 +08:00
}
} 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
}
2019-10-22 01:18:09 +08:00
eval ( $out . ';' );
return $result ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
define ( 'HTTP_STATUS_OK' , 200 );
define ( 'HTTP_STATUS_BAD_REQUEST' , 400 );
define ( 'HTTP_STATUS_FORBIDDEN' , 403 );
2013-05-02 22:20:06 +08:00
2019-10-22 01:18:09 +08:00
function ajaxError ( $message , $code = HTTP_STATUS_OK ) {
ZM\Error ( $message );
if ( function_exists ( 'ajaxCleanup' ) )
2016-04-02 01:02:31 +08:00
ajaxCleanup ();
2016-05-12 22:17:41 +08:00
if ( $code == HTTP_STATUS_OK ) {
2019-10-22 01:18:09 +08:00
$response = array ( 'result' => 'Error' , 'message' => $message );
2020-03-03 06:21:58 +08:00
header ( 'Content-type: application/json' );
2019-10-22 01:18:09 +08:00
exit ( jsonEncode ( $response ));
2016-04-02 01:02:31 +08:00
}
2019-10-22 01:18:09 +08:00
header ( " HTTP/1.0 $code $message " );
2016-04-02 01:02:31 +08:00
exit ();
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function ajaxResponse ( $result = false ) {
if ( function_exists ( 'ajaxCleanup' ) )
2016-04-02 01:02:31 +08:00
ajaxCleanup ();
2019-10-22 01:18:09 +08:00
$response = array ( 'result' => 'Ok' );
if ( is_array ( $result ) ) {
$response = array_merge ( $response , $result );
} else if ( ! empty ( $result ) ) {
2016-04-02 01:02:31 +08:00
$response [ 'message' ] = $result ;
2018-01-26 01:13:31 +08:00
}
2020-03-03 06:21:58 +08:00
header ( 'Content-type: application/json' );
2019-10-22 01:18:09 +08:00
exit ( jsonEncode ( $response ));
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function generateConnKey () {
2019-10-22 01:18:09 +08:00
return rand ( 1 , 999999 );
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +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
2020-08-14 00:14:18 +08:00
$path = str_replace ( '../' , '' , $path );
$path = ltrim ( $path , '/' );
2019-10-22 01:18:09 +08:00
return $path ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function cache_bust ( $file ) {
2017-11-01 21:41:19 +08:00
# Use the last modified timestamp to create a link that gets a different filename
# To defeat caching. Should probably use md5 hash
2017-11-01 23:52:32 +08:00
$parts = pathinfo ( $file );
2017-12-14 00:21:50 +08:00
global $css ;
2020-08-14 00:14:18 +08:00
$dirname = str_replace ( '/' , '_' , $parts [ 'dirname' ]);
2018-04-20 03:01:46 +08:00
$cacheFile = $dirname . '_' . $parts [ 'filename' ] . '-' . $css . '-' . filemtime ( $file ) . '.' . $parts [ 'extension' ];
2018-07-05 02:46:22 +08:00
if ( file_exists ( ZM_DIR_CACHE . '/' . $cacheFile ) or symlink ( ZM_PATH_WEB . '/' . $file , ZM_DIR_CACHE . '/' . $cacheFile ) ) {
2018-07-12 03:48:01 +08:00
return 'cache/' . $cacheFile ;
2017-11-01 21:41:19 +08:00
} else {
2020-09-06 00:49:14 +08:00
ZM\Warning ( 'Failed linking ' . $file . ' to ' . $cacheFile );
2017-11-01 21:41:19 +08:00
}
return $file ;
}
2019-10-22 01:18:09 +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 ) {
2020-08-14 00:14:18 +08:00
$tempSkinFile = detaintPath ( 'skins/' . $skin . '/' . $file );
2019-10-22 01:18:09 +08:00
if ( file_exists ( $tempSkinFile ) )
2016-04-02 01:02:31 +08:00
$skinFile = $tempSkinFile ;
}
2019-10-22 01:18:09 +08:00
return $skinFile ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +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 ) {
2020-08-14 00:14:18 +08:00
$tempSkinFile = detaintPath ( 'skins/' . $skin . '/' . $file );
2019-10-22 01:18:09 +08:00
if ( file_exists ( $tempSkinFile ) )
2016-04-02 01:02:31 +08:00
$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 ;
}
2019-10-22 01:18:09 +08:00
return $includeFiles ;
2013-05-02 22:20:06 +08:00
}
2019-10-22 01:18:09 +08:00
function requestVar ( $name , $default = '' ) {
return isset ( $_REQUEST [ $name ]) ? validHtmlStr ( $_REQUEST [ $name ]) : $default ;
2013-05-02 22:20:06 +08:00
}
// For numbers etc in javascript or tags etc
2019-09-20 02:57:28 +08:00
function validInt ( $input ) {
return preg_replace ( '/\D/' , '' , $input );
2013-05-02 22:20:06 +08:00
}
2016-05-12 22:17:41 +08:00
function validNum ( $input ) {
2019-09-20 02:57:28 +08:00
return preg_replace ( '/[^\d.-]/' , '' , $input );
2013-05-02 22:20:06 +08:00
}
// For general strings
2019-09-20 02:57:28 +08:00
function validStr ( $input ) {
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
2019-09-20 02:57:28 +08:00
function validJsStr ( $input ) {
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
2019-09-20 02:57:28 +08:00
function validHtmlStr ( $input ) {
return htmlspecialchars ( $input , ENT_QUOTES );
2013-05-02 22:20:06 +08:00
}
2020-08-27 06:40:31 +08:00
/* options [ 'width' ] is the desired view width not necessarily the image width requested .
* It can be % in which case we us it to set the scale
* It can be px in which case we can use it to calculate the scale
* Same width height . If both are set we should calculate the smaller resulting scale
*/
2019-03-22 02:14:30 +08:00
function getStreamHTML ( $monitor , $options = array ()) {
2017-02-28 09:56:14 +08:00
2020-08-27 06:40:31 +08:00
if ( isset ( $options [ 'scale' ]) and $options [ 'scale' ] != '' ) {
if ( $options [ 'scale' ] != 'auto' && $options [ 'scale' ] != '0' ) {
2020-09-06 00:49:14 +08:00
#ZM\Warning('Setting dimensions from scale:'.$options['scale']);
2020-02-26 00:12:49 +08:00
$options [ 'width' ] = reScale ( $monitor -> ViewWidth (), $options [ 'scale' ]) . 'px' ;
$options [ 'height' ] = reScale ( $monitor -> ViewHeight (), $options [ 'scale' ]) . 'px' ;
2020-08-27 06:40:31 +08:00
} else if ( ! ( isset ( $options [ 'width' ]) or isset ( $options [ 'height' ]) ) ) {
2020-02-26 00:12:49 +08:00
$options [ 'width' ] = '100%' ;
2020-02-28 00:35:40 +08:00
$options [ 'height' ] = 'auto' ;
2020-02-26 00:12:49 +08:00
}
2017-06-29 03:17:55 +08:00
} else {
2020-08-27 06:40:31 +08:00
$options [ 'scale' ] = 100 ;
2017-11-09 00:17:30 +08:00
# scale is empty or 100
# There may be a fixed width applied though, in which case we need to leave the height empty
if ( ! ( isset ( $options [ 'width' ]) and $options [ 'width' ] ) ) {
2020-08-27 06:40:31 +08:00
# Havn't specified width. If we specified height, then we should
# use a width that keeps the aspect ratio, otherwise no scaling,
# no dimensions, so assume the dimensions of the Monitor
if ( ! ( isset ( $options [ 'height' ]) and $options [ 'height' ]) ) {
2020-09-03 02:37:28 +08:00
# If we havn't specified any scale or dimensions, then we must be using CSS to scale it in a dynamic way. Can't make any assumptions.
#$options['width'] = $monitor->ViewWidth().'px';
#$options['height'] = $monitor->ViewHeight().'px';
2017-11-09 00:17:30 +08:00
}
2020-08-27 06:40:31 +08:00
} else {
2020-09-06 00:49:14 +08:00
#ZM\Warning("Have width ".$options['width']);
2020-08-27 06:40:31 +08:00
if ( preg_match ( '/^(\d+)px$/' , $options [ 'width' ], $matches ) ) {
$scale = intval ( 100 * $matches [ 1 ] / $monitor -> ViewWidth ());
2020-09-06 00:49:14 +08:00
#ZM\Warning("Scale is $scale");
2020-08-27 06:40:31 +08:00
if ( $scale < $options [ 'scale' ] )
$options [ 'scale' ] = $scale ;
} else if ( preg_match ( '/^(\d+)%$/' , $options [ 'width' ], $matches ) ) {
$scale = intval ( $matches [ 1 ]);
if ( $scale < $options [ 'scale' ] )
$options [ 'scale' ] = $scale ;
} else {
2020-10-13 20:42:22 +08:00
ZM\Warning ( 'Invalid value for width: ' . $options [ 'width' ]);
2020-08-27 06:40:31 +08:00
}
2020-09-06 00:49:14 +08:00
}
2017-06-09 22:36:38 +08:00
}
if ( ! isset ( $options [ 'mode' ] ) ) {
$options [ 'mode' ] = 'stream' ;
}
2017-03-02 04:26:40 +08:00
$options [ 'maxfps' ] = ZM_WEB_VIDEO_MAXFPS ;
if ( $monitor -> StreamReplayBuffer () )
$options [ 'buffer' ] = $monitor -> StreamReplayBuffer ();
2017-11-09 00:26:13 +08:00
//Warning("width: " . $options['width'] . ' height: ' . $options['height']. ' scale: ' . $options['scale'] );
2017-02-28 09:56:14 +08:00
2018-06-26 02:50:54 +08:00
if ( $monitor -> Type () == 'WebSite' ) {
return getWebSiteUrl (
'liveStream' . $monitor -> Id (), $monitor -> Path (),
( isset ( $options [ 'width' ]) ? $options [ 'width' ] : NULL ),
( isset ( $options [ 'height' ]) ? $options [ 'height' ] : NULL ),
$monitor -> Name ()
);
2016-05-12 22:17:41 +08:00
//FIXME, the width and height of the image need to be scaled.
2018-04-27 05:18:36 +08:00
} else if ( ZM_WEB_STREAM_METHOD == 'mpeg' && ZM_MPEG_LIVE_FORMAT ) {
2017-11-09 00:26:13 +08:00
$streamSrc = $monitor -> getStreamSrc ( array (
2019-03-22 02:14:30 +08:00
'mode' => 'mpeg' ,
'scale' => ( isset ( $options [ 'scale' ]) ? $options [ 'scale' ] : 100 ),
'bitrate' => ZM_WEB_VIDEO_BITRATE ,
'maxfps' => ZM_WEB_VIDEO_MAXFPS ,
2017-11-09 00:26:13 +08:00
'format' => ZM_MPEG_LIVE_FORMAT
) );
2017-11-09 00:17:30 +08:00
return getVideoStreamHTML ( 'liveStream' . $monitor -> Id (), $streamSrc , $options [ 'width' ], $options [ 'height' ], ZM_MPEG_LIVE_FORMAT , $monitor -> Name () );
2017-03-02 04:26:40 +08:00
} else if ( $options [ 'mode' ] == 'stream' and canStream () ) {
$options [ 'mode' ] = 'jpeg' ;
2018-06-26 02:50:54 +08:00
$streamSrc = $monitor -> getStreamSrc ( $options );
2017-03-02 04:26:40 +08:00
2016-05-12 22:17:41 +08:00
if ( canStreamNative () )
2017-11-09 00:17:30 +08:00
return getImageStreamHTML ( 'liveStream' . $monitor -> Id (), $streamSrc , $options [ 'width' ], $options [ 'height' ], $monitor -> Name ());
2016-05-12 22:17:41 +08:00
elseif ( canStreamApplet () )
2017-06-29 03:17:55 +08:00
// Helper, empty widths and heights really don't work.
2020-07-04 21:09:24 +08:00
return getHelperStream ( 'liveStream' . $monitor -> Id (), $streamSrc ,
$options [ 'width' ] ? $options [ 'width' ] : $monitor -> ViewWidth (),
2019-10-30 05:42:48 +08:00
$options [ 'height' ] ? $options [ 'height' ] : $monitor -> ViewHeight (),
2017-06-29 03:17:55 +08:00
$monitor -> Name ());
2016-05-12 22:17:41 +08:00
} else {
2017-06-29 03:17:55 +08:00
if ( $options [ 'mode' ] == 'stream' ) {
2019-10-22 01:18:09 +08:00
ZM\Info ( 'The system has fallen back to single jpeg mode for streaming. Consider enabling Cambozola or upgrading the client browser.' );
2016-07-14 23:55:27 +08:00
}
2017-06-29 03:17:55 +08:00
$options [ 'mode' ] = 'single' ;
2019-03-22 02:14:30 +08:00
$streamSrc = $monitor -> getStreamSrc ( $options );
2020-09-18 08:57:14 +08:00
return getImageStill ( 'liveStream' . $monitor -> Id (), $streamSrc ,
( isset ( $options [ 'width' ]) ? $options [ 'width' ] : null ),
( isset ( $options [ 'height' ]) ? $options [ 'height' ] : null ),
$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 = '' ;
2019-10-22 01:18:09 +08:00
if ( ( ZM_WEB_STREAM_METHOD == 'mpeg' ) && ZM_MPEG_LIVE_FORMAT ) {
2016-06-21 00:40:33 +08:00
$streamMode = 'mpeg' ;
} elseif ( canStream () ) {
$streamMode = 'jpeg' ;
} else {
$streamMode = 'single' ;
2019-10-22 01:18:09 +08:00
ZM\Info ( 'The system has fallen back to single jpeg mode for streaming. Consider enabling Cambozola or upgrading the client browser.' );
2016-06-21 00:40:33 +08:00
}
2017-11-19 03:05:11 +08:00
return $streamMode ;
2016-06-21 00:40:33 +08:00
} // end function getStreamMode
2016-07-20 05:34:01 +08:00
function folder_size ( $dir ) {
2019-10-22 01:18:09 +08:00
$size = 0 ;
foreach ( glob ( rtrim ( $dir , '/' ) . '/*' , GLOB_NOSORT ) as $each ) {
$size += is_file ( $each ) ? filesize ( $each ) : folder_size ( $each );
}
return $size ;
2016-12-29 17:31:05 +08:00
} // end function folder_size
2016-07-20 05:34:01 +08:00
2017-10-11 03:39:36 +08:00
function human_filesize ( $size , $precision = 2 ) {
2019-10-22 01:18:09 +08:00
$units = array ( 'B' , 'kB' , 'MB' , 'GB' , 'TB' , 'PB' , 'EB' , 'ZB' , 'YB' );
$step = 1024 ;
$i = 0 ;
while (( $size / $step ) > 0.9 ) {
$size = $size / $step ;
$i ++ ;
}
return round ( $size , $precision ) . $units [ $i ];
2016-07-21 03:20:21 +08:00
}
2016-07-20 05:34:01 +08:00
2017-03-19 09:12:06 +08:00
function csrf_startup () {
2019-10-22 01:18:09 +08:00
csrf_conf ( 'rewrite-js' , 'includes/csrf/csrf-magic.js' );
2017-03-19 09:12:06 +08:00
}
2019-01-15 22:05:11 +08:00
function check_timezone () {
$now = new DateTime ();
$sys_tzoffset = trim ( shell_exec ( 'date "+%z"' ));
$php_tzoffset = trim ( $now -> format ( 'O' ));
2019-10-22 01:18:09 +08:00
$mysql_tzoffset = trim ( dbFetchOne (
'SELECT TIME_FORMAT(TIMEDIFF(NOW(), UTC_TIMESTAMP),\'%H%i\');' ,
'TIME_FORMAT(TIMEDIFF(NOW(), UTC_TIMESTAMP),\'%H%i\')'
));
2019-01-15 22:05:11 +08:00
2020-10-14 22:39:25 +08:00
#Debug("System timezone offset determine to be: $sys_tzoffset,\x20
2020-07-04 21:09:24 +08:00
#PHP timezone offset determine to be: $php_tzoffset,\x20
2019-01-17 02:44:57 +08:00
#Mysql timezone offset determine to be: $mysql_tzoffset
#");
2019-01-15 22:05:11 +08:00
if ( $sys_tzoffset != $php_tzoffset )
2019-11-06 01:40:11 +08:00
ZM\Error ( " ZoneMinder is not configured properly: php's date.timezone $php_tzoffset does not match the system timezone $sys_tzoffset ! Please check Options->System->Timezone. " );
2019-01-15 22:05:11 +08:00
if ( $sys_tzoffset != $mysql_tzoffset )
2020-09-06 00:49:14 +08:00
ZM\Error ( 'ZoneMinder is not configured properly: mysql\'s timezone does not match the system timezone! Event lists will display incorrect times.' );
2019-01-15 22:05:11 +08:00
if ( ! ini_get ( 'date.timezone' ) || ! date_default_timezone_set ( ini_get ( 'date.timezone' )))
2020-09-06 00:49:14 +08:00
ZM\Error ( 'ZoneMinder is not configured properly: php\'s date.timezone is not set to a valid timezone. Please check Options->System->Timezone' );
2019-01-15 22:05:11 +08:00
}
2020-07-04 21:09:24 +08:00
function unparse_url ( $parsed_url , $substitutions = array () ) {
2018-01-19 04:39:08 +08:00
$fields = array ( 'scheme' , 'host' , 'port' , 'user' , 'pass' , 'path' , 'query' , 'fragment' );
foreach ( $fields as $field ) {
if ( isset ( $substitutions [ $field ] ) ) {
$parsed_url [ $field ] = $substitutions [ $field ];
}
}
2020-07-04 21:09:24 +08:00
$scheme = isset ( $parsed_url [ 'scheme' ]) ? $parsed_url [ 'scheme' ] . '://' : '' ;
$host = isset ( $parsed_url [ 'host' ]) ? $parsed_url [ 'host' ] : '' ;
$port = isset ( $parsed_url [ 'port' ]) ? ':' . $parsed_url [ 'port' ] : '' ;
$user = isset ( $parsed_url [ 'user' ]) ? $parsed_url [ 'user' ] : '' ;
$pass = isset ( $parsed_url [ 'pass' ]) ? ':' . $parsed_url [ 'pass' ] : '' ;
$pass = ( $user || $pass ) ? $pass . '@' : '' ;
$path = isset ( $parsed_url [ 'path' ]) ? $parsed_url [ 'path' ] : '' ;
$query = isset ( $parsed_url [ 'query' ]) ? '?' . $parsed_url [ 'query' ] : '' ;
$fragment = isset ( $parsed_url [ 'fragment' ]) ? '#' . $parsed_url [ 'fragment' ] : '' ;
return $scheme . $user . $pass . $host . $port . $path . $query . $fragment ;
2018-01-19 04:39:08 +08:00
}
2018-10-09 21:39:04 +08:00
// PP - POST request handler for PHP which does not need extensions
// credit: http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/
function do_request ( $method , $url , $data = array (), $optional_headers = null ) {
global $php_errormsg ;
$params = array ( 'http' => array (
'method' => $method ,
'content' => $data
));
if ( $optional_headers !== null ) {
$params [ 'http' ][ 'header' ] = $optional_headers ;
}
$ctx = stream_context_create ( $params );
$fp = @ fopen ( $url , 'rb' , false , $ctx );
if ( ! $fp ) {
throw new Exception ( " Problem with $url , $php_errormsg " );
}
$response = @ stream_get_contents ( $fp );
if ( $response === false ) {
throw new Exception ( " Problem reading data from $url , $php_errormsg " );
}
return $response ;
}
function do_post_request ( $url , $data , $optional_headers = null ) {
$params = array ( 'http' => array (
'method' => 'POST' ,
'content' => $data
));
if ( $optional_headers !== null ) {
$params [ 'http' ][ 'header' ] = $optional_headers ;
}
$ctx = stream_context_create ( $params );
$fp = @ fopen ( $url , 'rb' , false , $ctx );
if ( ! $fp ) {
2020-09-06 00:49:14 +08:00
throw new Exception ( 'Problem with ' . $url . ', '
2019-07-20 04:32:40 +08:00
. print_r ( error_get_last (), true ));
2018-10-09 21:39:04 +08:00
}
$response = @ stream_get_contents ( $fp );
if ( $response === false ) {
2020-09-06 00:49:14 +08:00
throw new Exception ( 'Problem reading data from ' . $url . ', data: ' . print_r ( $params , true )
2019-07-20 04:32:40 +08:00
. print_r ( error_get_last (), true ));
2018-10-09 21:39:04 +08:00
}
return $response ;
}
2018-10-30 00:52:06 +08:00
2018-10-26 03:40:12 +08:00
// The following works around php not being built with semaphore functions.
2018-10-30 00:52:06 +08:00
if ( ! function_exists ( 'sem_get' ) ) {
2018-10-26 03:40:12 +08:00
function sem_get ( $key ) {
return fopen ( __FILE__ . '.sem.' . $key , 'w+' );
}
function sem_acquire ( $sem_id ) {
return flock ( $sem_id , LOCK_EX );
}
function sem_release ( $sem_id ) {
return flock ( $sem_id , LOCK_UN );
}
}
2018-10-30 00:52:06 +08:00
if ( ! function_exists ( 'ftok' ) ) {
2020-09-06 00:49:14 +08:00
function ftok ( $filename = '' , $proj = '' ) {
2018-10-26 03:40:12 +08:00
if ( empty ( $filename ) || ! file_exists ( $filename ) ) {
return - 1 ;
} else {
$filename = $filename . ( string ) $proj ;
for ( $key = array (); sizeof ( $key ) < strlen ( $filename ); $key [] = ord ( substr ( $filename , sizeof ( $key ), 1 )));
return dechex ( array_sum ( $key ));
}
}
}
2019-01-04 22:26:34 +08:00
function getAffectedIds ( $name ) {
$names = $name . 's' ;
$ids = array ();
if ( isset ( $_REQUEST [ $names ]) ) {
if ( is_array ( $_REQUEST [ $names ]) ) {
$ids = $_REQUEST [ $names ];
} else {
$ids = array ( $_REQUEST [ $names ]);
}
} else if ( isset ( $_REQUEST [ $name ]) ) {
if ( is_array ( $_REQUEST [ $name ]) ) {
$ids = $_REQUEST [ $name ];
} else {
$ids = array ( $_REQUEST [ $name ]);
}
}
return $ids ;
}
2019-01-22 00:16:14 +08:00
function format_duration ( $time , $separator = ':' ) {
return sprintf ( '%02d%s%02d%s%02d' , floor ( $time / 3600 ), $separator , ( $time / 60 ) % 60 , $separator , $time % 60 );
}
2019-07-24 23:24:37 +08:00
function array_recursive_diff ( $aArray1 , $aArray2 ) {
$aReturn = array ();
2019-11-30 03:49:10 +08:00
if ( ! ( is_array ( $aArray1 ) and is_array ( $aArray2 ) ) ) {
2020-09-06 00:49:14 +08:00
$backTrace = debug_backtrace ();
ZM\Warning ( 'Bad arrays passed 1:' . print_r ( $aArray1 , true ) . PHP_EOL . '2: ' . print_r ( $aArray2 , true ) . PHP_EOL . ' from: ' . print_r ( $backTrace , true ));
return ;
2019-11-30 03:49:10 +08:00
}
2019-07-24 23:24:37 +08:00
2019-10-22 01:18:09 +08:00
foreach ( $aArray1 as $mKey => $mValue ) {
2019-07-24 23:24:37 +08:00
if ( array_key_exists ( $mKey , $aArray2 ) ) {
if ( is_array ( $mValue ) ) {
2019-11-30 03:49:10 +08:00
if ( is_array ( $aArray2 [ $mKey ]) ) {
$aRecursiveDiff = array_recursive_diff ( $mValue , $aArray2 [ $mKey ]);
if ( count ( $aRecursiveDiff ) ) {
$aReturn [ $mKey ] = $aRecursiveDiff ;
}
} else {
$aReturn [ $mKey ] = $mValue ;
2019-07-24 23:24:37 +08:00
}
} else {
if ( $mValue != $aArray2 [ $mKey ] ) {
$aReturn [ $mKey ] = $mValue ;
}
}
} else {
$aReturn [ $mKey ] = $mValue ;
}
}
# Now check for keys in array2 that are not in array1
foreach ( $aArray2 as $mKey => $mValue ) {
if ( array_key_exists ( $mKey , $aArray1 ) ) {
# Already checked it... I think.
#if ( is_array($mValue) ) {
#$aRecursiveDiff = array_recursive_diff($mValue, $aArray2[$mKey]);
#if ( count($aRecursiveDiff) ) {
#$aReturn[$mKey] = $aRecursiveDiff;
#}
#} else {
#if ( $mValue != $aArray2[$mKey] ) {
#$aReturn[$mKey] = $mValue;
#}
#}
} else {
$aReturn [ $mKey ] = $mValue ;
}
}
return $aReturn ;
}
2019-12-03 01:30:15 +08:00
function html_radio ( $name , $values , $selected = null , $options = array (), $attrs = array ()) {
$html = '' ;
if ( isset ( $options [ 'default' ]) and ( $selected == null ) ) {
$selected = $options [ 'default' ];
} # end if
foreach ( $values as $value => $label ) {
if ( isset ( $options [ 'container' ]) ) {
$html .= $options [ 'container' ][ 0 ];
}
2019-12-04 20:10:33 +08:00
$attributes = array_map (
function ( $attr , $value ){ return $attr . '="' . $value . '"' ;},
array_keys ( $attrs ),
array_values ( $attrs )
);
$attributes_string = implode ( ' ' , $attributes );
2019-12-03 01:30:15 +08:00
$html .= sprintf ( '
< div class = " form-check%7 $s " >
< label class = " form-check-label radio%7 $s " for = " %1 $s %6 $s %2 $s " >
< input class = " form-check-input " type = " radio " name = " %1 $s " value = " %2 $s " id = " %1 $s %6 $s %2 $s " % 4 $s % 5 $s />
% 3 $s </ label ></ div >
', $name, $value, $label, ($value==$selected?' checked = " checked " ':' ' ),
2019-12-04 20:10:33 +08:00
$attributes_string ,
( isset ( $options [ 'id' ]) ? $options [ 'id' ] : '' ),
2019-12-04 22:00:20 +08:00
( ( ( ! isset ( $options [ 'inline' ])) or $options [ 'inline' ] ) ? '-inline' : '' )
2019-12-04 20:10:33 +08:00
);
2019-12-03 01:30:15 +08:00
if ( isset ( $options [ 'container' ]) ) {
$html .= $options [ 'container' ][ 1 ];
}
} # end foreach value
return $html ;
} # end sub html_radio
2019-12-27 00:22:42 +08:00
2020-01-01 08:10:29 +08:00
function random_colour () {
return '#' .
2020-09-06 00:49:14 +08:00
str_pad ( dechex ( mt_rand ( 0 , 255 ) ), 2 , '0' , STR_PAD_LEFT ) .
str_pad ( dechex ( mt_rand ( 0 , 255 ) ), 2 , '0' , STR_PAD_LEFT ) .
str_pad ( dechex ( mt_rand ( 0 , 255 ) ), 2 , '0' , STR_PAD_LEFT );
2019-12-27 00:22:42 +08:00
}
2020-09-06 00:49:14 +08:00
function zm_random_bytes ( $length = 32 ) {
2020-03-22 03:28:18 +08:00
if ( ! isset ( $length ) || intval ( $length ) <= 8 ) {
$length = 32 ;
}
if ( function_exists ( 'random_bytes' ) ) {
return random_bytes ( $length );
}
if ( function_exists ( 'mcrypt_create_iv' ) ) {
return mcrypt_create_iv ( $length , MCRYPT_DEV_URANDOM );
}
if ( function_exists ( 'openssl_random_pseudo_bytes' ) ) {
return openssl_random_pseudo_bytes ( $length );
}
ZM\Error ( 'No random_bytes function found.' );
}
2020-10-17 20:28:14 +08:00
function i18n () {
$string = explode ( '_' , ZM_LANG_DEFAULT , 2 );
$string [ 1 ] = strtoupper ( $string [ 1 ]);
return implode ( '-' , $string );
}
2013-05-02 22:20:06 +08:00
?>