zoneminder/web/includes/functions.php

2455 lines
80 KiB
PHP
Raw Normal View History

<?php
//
// ZoneMinder web function library, $Date$, $Revision$
// Copyright (C) 2001-2008 Philip Coombes
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Compatibility functions
if ( version_compare( phpversion(), '4.3.0', '<') ) {
function ob_get_clean() {
$buffer = ob_get_contents();
ob_end_clean();
return( $buffer );
}
}
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
}
function CSPHeaders($view, $nonce) {
$additionalScriptSrc = "";
switch ($view) {
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) {
$additionalScriptSrc = "https://www.google.com";
}
// fall through
}
2019-01-17 03:04:24 +08:00
case 'bandwidth':
case 'blank':
case 'console':
case 'controlcap':
2019-01-17 03:04:24 +08:00
case 'function':
case 'log':
case 'logout':
case 'options':
case 'privacy':
case 'storage':
2019-01-17 03:04:24 +08:00
case 'version': {
// Enforce script-src on pages where inline scripts and event handlers have been fixed.
// 'unsafe-inline' is only for backwards compatibility with browsers which
// only support CSP 1 (with no nonce-* support).
header("Content-Security-Policy: script-src 'unsafe-inline' 'self' 'nonce-$nonce' $additionalScriptSrc");
break;
}
default: {
// Use Report-Only mode on all other pages.
header("Content-Security-Policy-Report-Only: script-src 'unsafe-inline' 'self' 'nonce-$nonce' $additionalScriptSrc");
break;
}
}
}
function CORSHeaders() {
if ( isset($_SERVER['HTTP_ORIGIN']) ) {
# The following is left for future reference/use.
$valid = false;
$Servers = Server::find();
if ( sizeof($Servers) < 1 ) {
# Only need CORSHeaders in the event that there are multiple servers in use.
# ICON: Might not be true. multi-port?
if ( ZM_MIN_STREAMING_PORT ) {
Logger::Debug("Setting default Access-Control-Allow-Origin from " . $_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Headers: x-requested-with,x-request');
}
return;
}
foreach( $Servers as $Server ) {
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;
2018-11-27 05:20:15 +08:00
Logger::Debug("Setting Access-Control-Allow-Origin from " . $_SERVER['HTTP_ORIGIN']);
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');
break;
}
}
if ( !$valid ) {
Warning($_SERVER['HTTP_ORIGIN'] . ' is not found in servers list.');
}
}
}
2016-05-12 22:17:41 +08:00
function getStreamSrc( $args, $querySep='&amp;' ) {
$streamSrc = ZM_BASE_URL.ZM_PATH_ZMS;
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' ) {
$args[] = 'auth='.generateAuthHash( ZM_AUTH_HASH_IPS );
} elseif ( ZM_AUTH_RELAY == 'plain' ) {
$args[] = 'user='.$_SESSION['username'];
$args[] = 'pass='.$_SESSION['password'];
} elseif ( ZM_AUTH_RELAY == 'none' ) {
$args[] = 'user='.$_SESSION['username'];
}
}
2017-05-30 21:10:41 +08:00
if ( !in_array( 'mode=single', $args ) && !empty($GLOBALS['connkey']) ) {
$args[] = 'connkey='.$GLOBALS['connkey'];
}
2016-05-12 22:17:41 +08:00
if ( ZM_RAND_STREAM ) {
2017-05-30 21:10:41 +08:00
$args[] = 'rand='.time();
}
2016-05-12 22:17:41 +08:00
if ( count($args) ) {
2017-05-30 21:10:41 +08:00
$streamSrc .= '?'.join( $querySep, $args );
}
return( $streamSrc );
}
2016-05-12 22:17:41 +08:00
function getMimeType( $file ) {
if ( function_exists('mime_content_type') ) {
return( mime_content_type( $file ) );
2016-05-12 22:17:41 +08:00
} elseif ( function_exists('finfo_file') ) {
$finfo = finfo_open( FILEINFO_MIME );
$mimeType = finfo_file( $finfo, $file );
finfo_close($finfo);
return( $mimeType );
}
return( trim( exec( 'file -bi '.escapeshellarg( $file ).' 2>/dev/null' ) ) );
}
2017-05-30 21:10:41 +08:00
function outputVideoStream( $id, $src, $width, $height, $format, $title='' ) {
echo getVideoStreamHTML( $id, $src, $width, $height, $format, $title );
}
2017-05-30 21:10:41 +08:00
function getVideoStreamHTML( $id, $src, $width, $height, $format, $title='' ) {
$html = '';
$width = validInt($width);
$height = validInt($height);
$title = validHtmlStr($title);
if ( file_exists( $src ) ) {
$mimeType = getMimeType( $src );
} else {
switch( $format ) {
case 'asf' :
2017-05-30 21:10:41 +08:00
$mimeType = 'video/x-ms-asf';
break;
case 'avi' :
case 'wmv' :
2017-05-30 21:10:41 +08:00
$mimeType = 'video/x-msvideo';
break;
case 'mov' :
2017-05-30 21:10:41 +08:00
$mimeType = 'video/quicktime';
break;
case 'mpg' :
case 'mpeg' :
2017-05-30 21:10:41 +08:00
$mimeType = 'video/mpeg';
break;
case 'swf' :
2017-05-30 21:10:41 +08:00
$mimeType = 'application/x-shockwave-flash';
break;
case '3gp' :
2017-05-30 21:10:41 +08:00
$mimeType = 'video/3gpp';
break;
default :
$mimeType = "video/$format";
break;
}
}
if ( !$mimeType || ($mimeType == 'application/octet-stream') )
$mimeType = 'video/'.$format;
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' :
{
if ( isWindows() ) {
return '<object id="'.$id.'" width="'.$width.'" height="'.$height.'
classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"
codebase="'.ZM_BASE_PROTOCOL.'://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902"
standby="Loading Microsoft Windows Media Player components..."
type="'.$mimeType.'">
<param name="FileName" value="'.$src.'"/>
<param name="autoStart" value="1"/>
<param name="showControls" value="0"/>
<embed type="'.$mimeType.'"
pluginspage="'.ZM_BASE_PROTOCOL.'://www.microsoft.com/Windows/MediaPlayer/"
src="'.$src.'"
name="'.$title.'"
width="'.$width.'"
height="'.$height.'"
autostart="1"
showcontrols="0">
</embed>
</object>';
}
}
2017-05-30 21:10:41 +08:00
case 'video/quicktime' :
{
return '<object id="'.$id.'" width="'.$width.'" height="'.$height.'"
classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
codebase="'.ZM_BASE_PROTOCOL.'://www.apple.com/qtactivex/qtplugin.cab"
type="'.$mimeType.'">
<param name="src" value="'.$src.'"/>
<param name="autoplay" VALUE="true"/>
<param name="controller" VALUE="false"/>
<embed type="'.$mimeType.'"
src="'.$src.'"
pluginspage="'.ZM_BASE_PROTOCOL.'://www.apple.com/quicktime/download/"
name="'.$title.'" width="'.$width.'" height="'.$height.'"
autoplay="true"
controller="true">
</embed>
</object>';
}
2017-05-30 21:10:41 +08:00
case 'application/x-shockwave-flash' :
{
return '<object id="'.$id.'" width="'.$width.'" height="'.$height.'"
classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="'.ZM_BASE_PROTOCOL.'://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"
type="'.$mimeType.'">
<param name="movie" value="'.$src.'"/>
<param name="quality" value="high"/>
<param name="bgcolor" value="#ffffff"/>
<embed type="'.$mimeType.'"
pluginspage="'.ZM_BASE_PROTOCOL.'://www.macromedia.com/go/getflashplayer"
src="'.$src.'"
name="'.$title.'"
width="'.$width.'"
height="'.$height.'"
quality="high"
bgcolor="#ffffff">
</embed>
</object>';
}
} # end switch
} # end if use object tags
return '<embed'. ( isset($mimeType)?(' type="'.$mimeType.'"'):'' ). '
src="'.$src.'"
name="'.$title.'"
width="'.$width.'"
height="'.$height.'"
autostart="1"
autoplay="1"
showcontrols="0"
controller="0">
</embed>';
}
function outputImageStream( $id, $src, $width, $height, $title='' ) {
echo getImageStreamHTML( $id, $src, $width, $height, $title );
}
function getImageStreamHTML( $id, $src, $width, $height, $title='' ) {
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).'"' : '' ).'/>';
} else {
2017-11-23 01:33:34 +08:00
return '<img id="'.$id.'" src="'.$src.'" alt="'. validHtmlStr($title) .'" style="'.($width? ' width:'.$width.';' : '' ).($height ? ' height:'. $height.';' : '' ).'"/>';
}
}
2016-05-12 22:17:41 +08:00
function outputControlStream( $src, $width, $height, $monitor, $scale, $target ) {
?>
2016-05-12 22:17:41 +08:00
<form name="ctrlForm" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>" target="<?php echo $target ?>">
<input type="hidden" name="view" value="blank">
<input type="hidden" name="mid" value="<?php echo $monitor['Id'] ?>">
<input type="hidden" name="action" value="control">
2017-05-30 23:14:22 +08:00
<?php
if ( $monitor['CanMoveMap'] ) {
?>
<input type="hidden" name="control" value="moveMap">
2017-05-30 23:14:22 +08:00
<?php
} elseif ( $monitor['CanMoveRel'] ) {
?>
<input type="hidden" name="control" value="movePseudoMap">
2017-05-30 23:14:22 +08:00
<?php
} elseif ( $monitor['CanMoveCon'] ) {
?>
<input type="hidden" name="control" value="moveConMap">
2017-05-30 23:14:22 +08:00
<?php
}
?>
<input type="hidden" name="scale" value="<?php echo $scale ?>">
<input type="image" src="<?php echo $src ?>" width="<?php echo $width ?>" height="<?php echo $height ?>">
2016-05-12 22:17:41 +08:00
</form>
<?php
}
2017-05-30 21:10:41 +08:00
function outputHelperStream( $id, $src, $width, $height, $title='' ) {
echo getHelperStream( $id, $src, $width, $height, $title );
2016-05-25 03:10:37 +08:00
}
2017-05-30 23:15:00 +08:00
function getHelperStream( $id, $src, $width, $height, $title='' ) {
return '<object type="application/x-java-applet" id="'.$id.'" code="com.charliemouse.cambozola.Viewer"
2016-05-25 03:10:37 +08:00
archive="'. ZM_PATH_CAMBOZOLA .'"
align="middle"
2016-05-25 03:10:37 +08:00
width="'. $width .'"
height="'. $height .'"
title="'. $title .'">
<param name="accessories" value="none"/>
2016-05-25 03:10:37 +08:00
<param name="url" value="'. $src .'"/>
</object>';
}
2017-05-30 21:10:41 +08:00
function outputImageStill( $id, $src, $width, $height, $title='' ) {
echo getImageStill( $id, $src, $width, $height, $title='' );
}
2017-05-30 21:10:41 +08:00
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 )
Warning("Web site $src has X-Frame-Options set to sameorigin. An X-Frame-Options browser plugin is required to display this site.");
}
}
return '<object id="'.$id.'" data="'.$src.'" alt="'.$title.'" width="'.$width.'" height="'.$height.'"></object>';
}
2016-05-12 22:17:41 +08:00
function outputControlStill( $src, $width, $height, $monitor, $scale, $target ) {
?>
2017-05-30 23:14:22 +08:00
<form name="ctrlForm" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>" target="<?php echo $target ?>">
<input type="hidden" name="view" value="blank">
<input type="hidden" name="mid" value="<?php echo $monitor['Id'] ?>">
<input type="hidden" name="action" value="control">
<?php
2016-05-12 22:17:41 +08:00
if ( $monitor['CanMoveMap'] ) {
?>
2017-05-30 23:14:22 +08:00
<input type="hidden" name="control" value="moveMap">
2016-05-12 22:17:41 +08:00
<?php
} elseif ( $monitor['CanMoveRel'] ) {
?>
2017-05-30 23:14:22 +08:00
<input type="hidden" name="control" value="movePseudoMap">
2016-05-12 22:17:41 +08:00
<?php
} elseif ( $monitor['CanMoveCon'] ) {
?>
2017-05-30 23:14:22 +08:00
<input type="hidden" name="control" value="moveConMap">
2016-05-12 22:17:41 +08:00
<?php
}
?>
<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
}
// Incoming args are shell-escaped. This function must escape any further arguments it cannot guarantee.
2016-05-12 22:17:41 +08:00
function getZmuCommand( $args ) {
$zmuCommand = ZMU_PATH;
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' ) {
$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' ) {
$zmuCommand .= ' -U '.escapeshellarg($_SESSION['username']);
}
}
$zmuCommand .= $args;
return( $zmuCommand );
}
function getEventDefaultVideoPath( $event ) {
$Event = new Event( $event );
return $Event->getStreamSrc( array( 'mode'=>'mpeg', 'format'=>'h264' ) );
}
2016-05-12 22:17:41 +08:00
function deletePath( $path ) {
if ( is_dir( $path ) ) {
2017-05-30 21:10:41 +08:00
system( escapeshellcmd( 'rm -rf '.$path ) );
} else if ( file_exists($path) ) {
unlink( $path );
}
}
function deleteEvent( $event ) {
if ( empty($event) ) {
2017-05-30 21:10:41 +08:00
Error( 'Empty event passed to deleteEvent.');
return;
}
if ( gettype($event) != 'array' ) {
# $event could be an eid, so turn it into an event hash
$event = new Event( $event );
2016-10-13 03:38:59 +08:00
} else {
2017-05-19 00:49:59 +08:00
Logger::Debug("Event type: " . gettype($event));
}
global $user;
if ( $user['Events'] == 'Edit' ) {
$event->delete();
} # CAN EDIT
}
2017-05-30 21:10:41 +08:00
function makeLink( $url, $label, $condition=1, $options='' ) {
$string = '';
2016-05-12 22:17:41 +08:00
if ( $condition ) {
$string .= '<a href="'.$url.'"'.($options?(' '.$options):'').'>';
}
$string .= $label;
2016-05-12 22:17:41 +08:00
if ( $condition ) {
$string .= '</a>';
}
return( $string );
}
2017-05-30 21:10:41 +08:00
function makePopupLink( $url, $winName, $winSize, $label, $condition=1, $options='' ) {
// Avoid double-encoding since some consumers incorrectly pass a pre-escaped URL.
$string = '<a class="popup-link" href="' . htmlspecialchars($url, ENT_COMPAT | ENT_HTML401, ini_get("default_charset"), false) . '"';
$string .= ' data-window-name="' . htmlspecialchars($winName) . '"';
2016-05-12 22:17:41 +08:00
if ( $condition ) {
if ( is_array( $winSize ) ) {
$string .= ' data-window-tag="' . htmlspecialchars($winSize[0]) . '"';
$string .= ' data-window-width="' . htmlspecialchars($winSize[1]) . '"';
$string .= ' data-window-height="' . htmlspecialchars($winSize[2]) . '"';
} else {
$string .= ' data-window-tag="' . htmlspecialchars($winSize) . '"';
}
$string .= ($options ? (' ' . $options ) : '') . '>';
} else {
$string .= '<a>';
}
$string .= $label;
2017-05-30 23:14:22 +08:00
$string .= '</a>';
return( $string );
}
2017-05-30 21:10:41 +08:00
function makePopupButton( $url, $winName, $winSize, $buttonValue, $condition=1, $options='' ) {
$string = '<input type="button" class="popup-link" value="' . htmlspecialchars($buttonValue) . '"';
$string .= ' data-url="' . htmlspecialchars($url, ENT_COMPAT | ENT_HTML401, ini_get("default_charset"), false) . '"';
$string .= ' data-window-name="' . htmlspecialchars($winName) . '"';
if ( is_array( $winSize ) ) {
$string .= ' data-window-tag="' . htmlspecialchars($winSize[0]) . '"';
$string .= ' data-window-width="' . htmlspecialchars($winSize[1]) . '"';
$string .= ' data-window-height="' . htmlspecialchars($winSize[2]) . '"';
} else {
$string .= ' data-window-tag="' . htmlspecialchars($winSize) . '"';
}
if (!$condition) {
$string .= ' disabled="disabled"';
}
$string .= ($options ? (' ' . $options) : '') . '/>';
return( $string );
}
function htmlSelect( $name, $contents, $values, $behaviours=false ) {
2017-05-30 21:10:41 +08:00
$behaviourText = '';
if ( !empty($behaviours) ) {
if ( is_array($behaviours) ) {
foreach ( $behaviours as $event=>$action ) {
$behaviourText .= ' '.$event.'="'.$action.'"';
}
} else {
$behaviourText = ' onchange="'.$behaviours.'"';
}
}
return "<select name=\"$name\" id=\"$name\"$behaviourText>".htmlOptions($contents, $values).'</select>';
2018-02-27 09:08:30 +08:00
}
function htmlOptions($contents, $values) {
$options_html = '';
foreach ( $contents as $value=>$option ) {
$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'];
Error("Setting to disabled");
}
} else if ( is_object($option) ) {
$text = $option->Name();
} else {
$text = $option;
}
$selected = is_array($values) ? in_array($value, $values) : !strcmp($value, $values);
$options_html .= "<option value=\"$value\"".
($selected?' selected="selected"':'').
($disabled?' disabled="disabled"':'').
">$text</option>";
}
return $options_html;
}
2016-05-12 22:17:41 +08:00
function truncText( $text, $length, $deslash=1 ) {
2017-05-30 21:10:41 +08:00
return( preg_replace( '/^(.{'.$length.',}?)\b.*$/', '\\1&hellip;', ($deslash?stripslashes($text):$text) ) );
}
2016-05-12 22:17:41 +08:00
function buildSelect( $name, $contents, $behaviours=false ) {
2017-05-30 21:10:41 +08:00
$value = '';
if ( preg_match( '/^\s*(\w+)\s*(\[.*\])?\s*$/', $name, $matches ) && count($matches) > 2 ) {
$arr = $matches[1];
if ( isset($GLOBALS[$arr]) )
$value = $GLOBALS[$arr];
elseif ( isset($_REQUEST[$arr]) )
$value = $_REQUEST[$arr];
2017-05-30 21:10:41 +08:00
if ( !preg_match_all( '/\[\s*[\'"]?(\w+)["\']?\s*\]/', $matches[2], $matches ) ) {
Fatal( "Can't parse selector '$name'" );
}
2016-05-12 22:17:41 +08:00
for ( $i = 0; $i < count($matches[1]); $i++ ) {
$idx = $matches[1][$i];
$value = isset($value[$idx])?$value[$idx]:false;
}
2016-05-12 22:17:41 +08:00
} else {
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 ) {
$behaviourText .= ' '.$event.'="'.$action.'"';
}
2016-05-12 22:17:41 +08:00
} else {
$behaviourText = ' onchange="'.$behaviours.'"';
}
}
?>
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-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();
ob_end_clean();
return( $html );
}
2016-05-12 22:17:41 +08:00
function getFormChanges( $values, $newValues, $types=false, $columns=false ) {
$changes = array();
if ( !$types )
$types = array();
2016-05-12 22:17:41 +08:00
foreach( $newValues as $key=>$value ) {
if ( $columns && !isset($columns[$key]) )
continue;
if ( !isset($types[$key]) )
$types[$key] = false;
2016-05-12 22:17:41 +08:00
switch( $types[$key] ) {
case 'set' :
{
if ( is_array($newValues[$key]) ) {
if ( (!isset($values[$key])) or ( join(',',$newValues[$key]) != $values[$key] ) ) {
$changes[$key] = "`$key` = ".dbEscape(join(',',$newValues[$key]));
}
} else if ( (!isset($values[$key])) or $values[$key] ) {
$changes[$key] = "`$key` = ''";
}
break;
}
case 'image' :
{
2016-05-12 22:17:41 +08:00
if ( is_array( $newValues[$key] ) ) {
$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];
$changes[$key.'Type'] = $key."Type = '".$newValues[$key]['type']."'";
2017-05-30 21:10:41 +08:00
$changes[$key.'Size'] = $key.'Size = '.$newValues[$key]['size'];
ob_start();
readfile( $newValues[$key]['tmp_name'] );
$changes[$key] = $key." = ".dbEscape( ob_get_contents() );
ob_end_clean();
2016-05-12 22:17:41 +08:00
} else {
$changes[$key] = "$key = ".dbEscape($value);
}
break;
}
case 'document' :
{
2016-05-12 22:17:41 +08:00
if ( is_array( $newValues[$key] ) ) {
$imageData = getimagesize( $newValues[$key]['tmp_name'] );
$changes[$key.'Type'] = $key."Type = '".$newValues[$key]['type']."'";
2017-05-30 21:10:41 +08:00
$changes[$key.'Size'] = $key.'Size = '.$newValues[$key]['size'];
ob_start();
readfile( $newValues[$key]['tmp_name'] );
2017-05-30 21:10:41 +08:00
$changes[$key] = $key.' = '.dbEscape( ob_get_contents() );
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);
}
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']);
ob_start();
readfile( $newValues[$key]['tmp_name'] );
$changes[$key] = $key." = '".dbEscape( ob_get_contents() )."'";
ob_end_clean();
break;
}
case 'raw' :
{
if ( (!isset($values[$key])) or ($values[$key] != $value) ) {
2017-05-30 21:10:41 +08:00
$changes[$key] = $key . ' = '.dbEscape($value);
}
break;
}
case 'toggle' :
if ( (!isset($values[$key])) or $values[$key] != $value ) {
if ( empty($value) ) {
$changes[$key] = "$key = 0";
} else {
2018-03-11 09:48:53 +08:00
$changes[$key] = "$key = 1";
//$changes[$key] = $key . ' = '.dbEscape(trim($value));
}
}
break;
case 'integer' :
if ( (!isset($values[$key])) or $values[$key] != $value ) {
$changes[$key] = $key . ' = '.intval($value);
}
break;
default :
{
2016-05-12 22:17:41 +08:00
if ( !isset($values[$key]) || ($values[$key] != $value) ) {
if ( ! isset($value) || $value == '' ) {
$changes[$key] = "`$key` = NULL";
} else {
$changes[$key] = "`$key` = ".dbEscape(trim($value));
}
}
break;
}
2017-05-31 01:51:00 +08:00
} // end switch
} // end foreach newvalues
2016-05-12 22:17:41 +08:00
foreach( $values as $key=>$value ) {
if ( !empty($columns[$key]) ) {
if ( !empty($types[$key]) ) {
if ( $types[$key] == 'toggle' ) {
if ( !isset($newValues[$key]) && !empty($value) ) {
$changes[$key] = "$key = 0";
}
2017-05-30 23:53:21 +08:00
} else if ( $types[$key] == 'set' ) {
$changes[$key] = "$key = ''";
}
}
}
}
return( $changes );
}
2016-05-12 22:17:41 +08:00
function getBrowser( &$browser, &$version ) {
if ( isset($_SESSION['browser']) ) {
$browser = $_SESSION['browser'];
$version = $_SESSION['version'];
2016-05-12 22:17:41 +08:00
} else {
if (( preg_match( '/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT'], $logVersion)) || (preg_match( '/.*Trident.*rv:(.*?)(;|\))/', $_SERVER['HTTP_USER_AGENT'], $logVersion))) {
$version = $logVersion[1];
$browser = 'ie';
2016-05-12 22:17:41 +08:00
} elseif ( preg_match( '/Chrome\/([0-9.]+)/', $_SERVER['HTTP_USER_AGENT'], $logVersion) ) {
$version = $logVersion[1];
// Check for old version of Chrome with bug 5876
2016-05-12 22:17:41 +08:00
if ( $version < 7 ) {
$browser = 'oldchrome';
2016-05-12 22:17:41 +08:00
} else {
$browser = 'chrome';
}
2016-05-12 22:17:41 +08:00
} elseif ( preg_match( '/Safari\/([0-9.]+)/', $_SERVER['HTTP_USER_AGENT'], $logVersion) ) {
$version = $logVersion[1];
$browser = 'safari';
2016-05-12 22:17:41 +08:00
} elseif ( preg_match( '/Opera[ \/]([0-9].[0-9]{1,2})/', $_SERVER['HTTP_USER_AGENT'], $logVersion) ) {
$version = $logVersion[1];
$browser = 'opera';
2016-05-12 22:17:41 +08:00
} elseif ( preg_match( '/Konqueror\/([0-9.]+)/', $_SERVER['HTTP_USER_AGENT'], $logVersion) ) {
$version = $logVersion[1];
$browser = 'konqueror';
2016-05-12 22:17:41 +08:00
} elseif ( preg_match( '/Mozilla\/([0-9].[0-9]{1,2})/', $_SERVER['HTTP_USER_AGENT'], $logVersion) ) {
$version = $logVersion[1];
$browser = 'mozilla';
2016-05-12 22:17:41 +08:00
} else {
$version = 0;
$browser = 'unknown';
}
$_SESSION['browser'] = $browser;
$_SESSION['version'] = $version;
}
}
2016-05-12 22:17:41 +08:00
function isMozilla() {
getBrowser( $browser, $version );
2017-05-30 21:10:41 +08:00
return( $browser == 'mozilla' );
}
2016-05-12 22:17:41 +08:00
function isKonqueror() {
getBrowser( $browser, $version );
2017-05-30 21:10:41 +08:00
return( $browser == 'konqueror' );
}
2016-05-12 22:17:41 +08:00
function isInternetExplorer() {
getBrowser( $browser, $version );
2017-05-30 21:10:41 +08:00
return( $browser == 'ie' );
}
2016-05-12 22:17:41 +08:00
function isOldChrome() {
getBrowser( $browser, $version );
2017-05-30 21:10:41 +08:00
return( $browser == 'oldchrome' );
}
2016-05-12 22:17:41 +08:00
function isChrome() {
getBrowser( $browser, $version );
2017-05-30 21:10:41 +08:00
return( $browser == 'chrome' );
}
2016-05-12 22:17:41 +08:00
function isOpera() {
getBrowser( $browser, $version );
2017-05-30 21:10:41 +08:00
return( $browser == 'opera' );
}
2016-05-12 22:17:41 +08:00
function isSafari() {
getBrowser( $browser, $version );
2017-05-30 21:10:41 +08:00
return( $browser == 'safari' );
}
2016-05-12 22:17:41 +08:00
function isWindows() {
return ( preg_match( '/Win/', $_SERVER['HTTP_USER_AGENT'] ) );
}
2016-05-12 22:17:41 +08:00
function canStreamIframe() {
return( isKonqueror() );
}
2016-05-12 22:17:41 +08:00
function canStreamNative() {
// Old versions of Chrome can display the stream, but then it blocks everything else (Chrome bug 5876)
2017-05-30 21:10:41 +08:00
return( ZM_WEB_CAN_STREAM == 'yes' || ( ZM_WEB_CAN_STREAM == 'auto' && (!isInternetExplorer() && !isOldChrome()) ) );
}
2016-05-12 22:17:41 +08:00
function canStreamApplet() {
if ( (ZM_OPT_CAMBOZOLA && !file_exists( ZM_PATH_WEB.'/'.ZM_PATH_CAMBOZOLA )) ) {
2017-05-30 21:10:41 +08:00
Warning ( 'ZM_OPT_CAMBOZOLA is enabled, but the system cannot find '.ZM_PATH_WEB.'/'.ZM_PATH_CAMBOZOLA );
}
return( (ZM_OPT_CAMBOZOLA && file_exists( ZM_PATH_WEB.'/'.ZM_PATH_CAMBOZOLA )) );
}
2016-05-12 22:17:41 +08:00
function canStream() {
return( canStreamNative() | canStreamApplet() );
}
2016-05-12 22:17:41 +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';
exec( $string );
}
2016-05-12 22:17:41 +08:00
function daemonControl( $command, $daemon=false, $args=false ) {
$string = escapeshellcmd(ZM_PATH_BIN).'/zmdc.pl '.$command;
2016-05-12 22:17:41 +08:00
if ( $daemon ) {
$string .= ' ' . $daemon;
2016-05-12 22:17:41 +08:00
if ( $args ) {
$string .= ' ' . $args;
}
}
$string = escapeshellcmd( $string );
#$string .= ' 2>/dev/null >&- <&- >/dev/null';
Logger::Debug("daemonControl $string");
exec( $string );
}
2018-04-17 23:36:14 +08:00
function zmcControl($monitor, $mode=false) {
$Monitor = new Monitor( $monitor );
return $Monitor->zmcControl($mode);
}
2018-04-17 23:36:14 +08:00
function zmaControl($monitor, $mode=false) {
$Monitor = new Monitor($monitor);
return $Monitor->zmaControl($mode);
}
2016-05-12 22:17:41 +08:00
function initDaemonStatus() {
global $daemon_status;
2016-05-12 22:17:41 +08:00
if ( !isset($daemon_status) ) {
if ( daemonCheck() ) {
$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 = '';
}
}
}
2016-05-12 22:17:41 +08:00
function daemonStatus( $daemon, $args=false ) {
global $daemon_status;
initDaemonStatus();
$string = $daemon;
if ( $args )
$string .= ' ' . $args;
return( strpos( $daemon_status, "'$string' running" ) !== false );
}
2016-05-12 22:17:41 +08:00
function zmcStatus( $monitor ) {
if ( $monitor['Type'] == 'Local' ) {
$zmcArgs = '-d '.$monitor['Device'];
2016-05-12 22:17:41 +08:00
} else {
$zmcArgs = '-m '.$monitor['Id'];
}
return( daemonStatus( 'zmc', $zmcArgs ) );
}
2016-05-12 22:17:41 +08:00
function zmaStatus( $monitor ) {
if ( is_array( $monitor ) ) {
$monitor = $monitor['Id'];
}
return( daemonStatus( 'zma', "-m $monitor" ) );
}
2016-05-12 22:17:41 +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 ) {
$string .= ' ' . $daemon;
if ( $args )
$string .= ' '. $args;
}
$string = escapeshellcmd( $string );
$result = exec( $string );
return( preg_match( '/running/', $result ) );
}
2016-05-12 22:17:41 +08:00
function zmcCheck( $monitor ) {
if ( $monitor['Type'] == 'Local' ) {
$zmcArgs = '-d '.$monitor['Device'];
2016-05-12 22:17:41 +08:00
} else {
$zmcArgs = '-m '.$monitor['Id'];
}
return( daemonCheck( 'zmc', $zmcArgs ) );
}
2016-05-12 22:17:41 +08:00
function zmaCheck( $monitor ) {
if ( is_array( $monitor ) ) {
$monitor = $monitor['Id'];
}
return( daemonCheck( 'zma', "-m $monitor" ) );
}
function getImageSrc( $event, $frame, $scale=SCALE_BASE, $captureOnly=false, $overwrite=false ) {
$Event = new Event( $event );
return $Event->getImageSrc( $frame, $scale, $captureOnly, $overwrite );
}
2016-05-12 22:17:41 +08:00
function viewImagePath( $path, $querySep='&amp;' ) {
2018-12-28 23:38:39 +08:00
return( '?view=image'.$querySep.'path='.$path );
}
2016-05-12 22:17:41 +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
if ( !($frame = dbFetchOne( "SELECT * FROM Frames WHERE EventId=? AND Score=? ORDER BY FrameId LIMIT 1", NULL, array( $event['Id'], $event['MaxScore'] ) )) )
return( false );
$frameId = $frame['FrameId'];
2016-05-12 22:17:41 +08:00
if ( ZM_WEB_LIST_THUMB_WIDTH ) {
$thumbWidth = ZM_WEB_LIST_THUMB_WIDTH;
$scale = (SCALE_BASE*ZM_WEB_LIST_THUMB_WIDTH)/$event['Width'];
$thumbHeight = reScale( $event['Height'], $scale );
2016-05-12 22:17:41 +08:00
} elseif ( ZM_WEB_LIST_THUMB_HEIGHT ) {
$thumbHeight = ZM_WEB_LIST_THUMB_HEIGHT;
$scale = (SCALE_BASE*ZM_WEB_LIST_THUMB_HEIGHT)/$event['Height'];
$thumbWidth = reScale( $event['Width'], $scale );
2016-05-12 22:17:41 +08:00
} else {
Fatal( "No thumbnail width or height specified, please check in Options->Web" );
}
$imageData = getImageSrc( $event, $frame, $scale, false, $overwrite );
if ( ! $imageData ) {
2016-05-03 01:42:42 +08:00
return ( false );
}
2017-05-30 23:14:22 +08:00
$thumbData = $frame;
$thumbData['Path'] = $imageData['thumbPath'];
$thumbData['Width'] = (int)$thumbWidth;
$thumbData['Height'] = (int)$thumbHeight;
return( $thumbData );
}
2016-05-12 22:17:41 +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;
else
if ( version_compare( phpversion(), "4.3.10", ">=") )
$command .= " -s ".sprintf( "%.2F", ($scale/SCALE_BASE) );
else
$command .= " -s ".sprintf( "%.2f", ($scale/SCALE_BASE) );
if ( $overwrite )
$command .= " -o";
2017-05-06 05:53:03 +08:00
$command = escapeshellcmd( $command );
$result = exec( $command, $output, $status );
2017-05-19 00:49:59 +08:00
Logger::Debug("generating Video $command: result($result outptu:(".implode("\n", $output )." status($status");
return( $status?"":rtrim($result) );
}
2016-05-12 22:17:41 +08:00
function executeFilter( $filter ) {
$command = ZM_PATH_BIN."/zmfilter.pl --filter ".escapeshellarg($filter);
$result = exec( $command, $output, $status );
dbQuery( "delete from Filters where Name like '_TempFilter%'" );
return( $status );
}
# This takes more than one scale amount, so it runs through each and alters dimension.
# I can't imagine why you would want to do that.
2016-05-12 22:17:41 +08:00
function reScale( $dimension, $dummy ) {
$new_dimension = $dimension;
2016-05-12 22:17:41 +08:00
for ( $i = 1; $i < func_num_args(); $i++ ) {
$scale = func_get_arg( $i );
2018-09-15 04:57:28 +08:00
if ( !empty($scale) && ($scale != 'auto') && ($scale != SCALE_BASE) )
$new_dimension = (int)(($new_dimension*$scale)/SCALE_BASE);
}
return( $new_dimension );
}
2016-05-12 22:17:41 +08:00
function deScale( $dimension, $dummy ) {
$new_dimension = $dimension;
2016-05-12 22:17:41 +08:00
for ( $i = 1; $i < func_num_args(); $i++ ) {
$scale = func_get_arg( $i );
if ( !empty($scale) && $scale != SCALE_BASE )
$new_dimension = (int)(($new_dimension*SCALE_BASE)/$scale);
}
return( $new_dimension );
}
2016-05-12 22:17:41 +08:00
function monitorLimitSql() {
global $user;
if ( !empty($user['MonitorIds']) )
$midSql = " and MonitorId in (".join( ",", preg_split( '/["\'\s]*,["\'\s]*/', $user['MonitorIds'] ) ).")";
else
$midSql = '';
return( $midSql );
}
function parseSort( $saveToSession=false, $querySep='&amp;' ) {
global $sortQuery, $sortColumn, $sortOrder, $limitQuery; // Outputs
if (isset($_REQUEST['filter']['Query']['sort_field'])) { //Handle both new and legacy filter passing
$_REQUEST['sort_field'] = $_REQUEST['filter']['Query']['sort_field'];
}
if (isset($_REQUEST['filter']['Query']['sort_asc'])) {
$_REQUEST['sort_asc'] = $_REQUEST['filter']['Query']['sort_asc'];
}
if (isset($_REQUEST['filter']['Query']['limit'])) {
$_REQUEST['limit'] = $_REQUEST['filter']['Query']['limit'];
}
2016-05-12 22:17:41 +08:00
if ( empty($_REQUEST['sort_field']) ) {
$_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-05-12 22:17:41 +08:00
switch( $_REQUEST['sort_field'] ) {
case 'Id' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.Id';
break;
case 'MonitorName' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'M.Name';
break;
case 'Name' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.Name';
break;
case 'Cause' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.Cause';
break;
case 'DateTime' :
2017-10-26 02:11:19 +08:00
$sortColumn = 'E.StartTime';
$_REQUEST['sort_field'] = 'StartTime';
2017-10-26 02:11:19 +08:00
break;
case 'DiskSpace' :
$sortColumn = 'E.DiskSpace';
break;
case 'StartTime' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.StartTime';
break;
2017-10-26 02:11:19 +08:00
case 'EndTime' :
$sortColumn = 'E.EndTime';
break;
case 'Length' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.Length';
break;
case 'Frames' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.Frames';
break;
case 'AlarmFrames' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.AlarmFrames';
break;
case 'TotScore' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.TotScore';
break;
case 'AvgScore' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.AvgScore';
break;
case 'MaxScore' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.MaxScore';
break;
default:
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.StartTime';
break;
}
2017-05-30 21:10:41 +08:00
$sortOrder = $_REQUEST['sort_asc']?'asc':'desc';
if ( !$_REQUEST['sort_asc'] )
$_REQUEST['sort_asc'] = 0;
2017-05-30 21:10:41 +08:00
$sortQuery = $querySep.'sort_field='.validHtmlStr($_REQUEST['sort_field']).$querySep.'sort_asc='.validHtmlStr($_REQUEST['sort_asc']);
if ( !isset($_REQUEST['limit']) )
2017-05-30 21:10:41 +08:00
$_REQUEST['limit'] = '';
2016-05-12 22:17:41 +08:00
if ( $saveToSession ) {
$_SESSION['sort_field'] = validHtmlStr($_REQUEST['sort_field']);
$_SESSION['sort_asc'] = validHtmlStr($_REQUEST['sort_asc']);
}
if ($_REQUEST['limit'] != '') {
$limitQuery = "&limit=".$_REQUEST['limit'];
}
}
2018-04-21 03:22:45 +08:00
function parseFilter(&$filter, $saveToSession=false, $querySep='&amp;') {
$filter['query'] = '';
$filter['sql'] = '';
$filter['fields'] = '';
$StorageArea = NULL;
$terms = isset($filter['Query']) ? $filter['Query']['terms'] : NULL;
if ( isset($terms) && count($terms) ) {
for ( $i = 0; $i < count($terms); $i++ ) {
if ( isset($terms[$i]['cnj']) ) {
$filter['query'] .= $querySep.urlencode("filter[Query][terms][$i][cnj]").'='.urlencode($terms[$i]['cnj']);
$filter['sql'] .= ' '.$terms[$i]['cnj'].' ';
$filter['fields'] .= "<input type=\"hidden\" name=\"filter[Query][terms][$i][cnj]\" value=\"".htmlspecialchars($terms[$i]['cnj'])."\"/>\n";
}
if ( isset($terms[$i]['obr']) ) {
$filter['query'] .= $querySep.urlencode("filter[Query][terms][$i][obr]").'='.urlencode($terms[$i]['obr']);
2018-04-21 03:22:45 +08:00
$filter['sql'] .= ' '.str_repeat('(', $terms[$i]['obr']).' ';
$filter['fields'] .= "<input type=\"hidden\" name=\"filter[Query][terms][$i][obr]\" value=\"".htmlspecialchars($terms[$i]['obr'])."\"/>\n";
}
if ( isset($terms[$i]['attr']) ) {
$filter['query'] .= $querySep.urlencode("filter[Query][terms][$i][attr]").'='.urlencode($terms[$i]['attr']);
$filter['fields'] .= "<input type=\"hidden\" name=\"filter[Query][terms][$i][attr]\" value=\"".htmlspecialchars($terms[$i]['attr'])."\"/>\n";
switch ( $terms[$i]['attr'] ) {
case 'MonitorName':
2018-04-21 03:22:45 +08:00
$filter['sql'] .= 'M.'.preg_replace('/^Monitor/', '', $terms[$i]['attr']);
break;
case 'ServerId':
case 'MonitorServerId':
$filter['sql'] .= 'M.ServerId';
break;
case 'StorageServerId':
$filter['sql'] .= 'S.ServerId';
break;
case 'FilterServerId':
$filter['sql'] .= ZM_SERVER_ID;
break;
2017-10-26 02:11:19 +08:00
# Unspecified start or end, so assume start, this is to support legacy filters
case 'DateTime':
2017-05-30 21:10:41 +08:00
$filter['sql'] .= 'E.StartTime';
break;
case 'Date':
2017-05-30 21:10:41 +08:00
$filter['sql'] .= 'to_days( E.StartTime )';
break;
case 'Time':
2017-05-30 21:10:41 +08:00
$filter['sql'] .= 'extract( hour_second from E.StartTime )';
break;
case 'Weekday':
2017-05-30 21:10:41 +08:00
$filter['sql'] .= 'weekday( E.StartTime )';
break;
2017-10-26 02:11:19 +08:00
# Starting Time
case 'StartDateTime':
$filter['sql'] .= 'E.StartTime';
break;
case 'StartDate':
$filter['sql'] .= 'to_days( E.StartTime )';
break;
case 'StartTime':
$filter['sql'] .= 'extract( hour_second from E.StartTime )';
break;
case 'StartWeekday':
$filter['sql'] .= 'weekday( E.StartTime )';
break;
# Ending Time
case 'EndDateTime':
$filter['sql'] .= 'E.EndTime';
break;
case 'EndDate':
$filter['sql'] .= 'to_days( E.EndTime )';
break;
case 'EndTime':
$filter['sql'] .= 'extract( hour_second from E.EndTime )';
break;
case 'EndWeekday':
$filter['sql'] .= 'weekday( E.EndTime )';
break;
case 'Id':
case 'Name':
2017-10-26 02:11:19 +08:00
case 'DiskSpace':
case 'MonitorId':
case 'StorageId':
case 'Length':
case 'Frames':
case 'AlarmFrames':
case 'TotScore':
case 'AvgScore':
case 'MaxScore':
case 'Cause':
case 'Notes':
case 'StateId':
case 'Archived':
$filter['sql'] .= 'E.'.$terms[$i]['attr'];
break;
case 'DiskPercent':
// Need to specify a storage area, so need to look through other terms looking for a storage area, else we default to ZM_EVENTS_PATH
if ( ! $StorageArea ) {
for ( $j = 0; $j < count($terms); $j++ ) {
2018-01-12 05:53:53 +08:00
if ( isset($terms[$j]['attr']) and $terms[$j]['attr'] == 'StorageId' and isset($terms[$j]['val']) ) {
$StorageArea = new Storage($terms[$j]['val']);
2018-04-21 03:22:45 +08:00
break;
}
} // end foreach remaining term
if ( ! $StorageArea ) $StorageArea = new Storage();
} // end no StorageArea found yet
$filter['sql'] .= getDiskPercent( $StorageArea->Path() );
break;
case 'DiskBlocks':
// Need to specify a storage area, so need to look through other terms looking for a storage area, else we default to ZM_EVENTS_PATH
if ( ! $StorageArea ) {
for ( $j = $i; $j < count($terms); $j++ ) {
2018-01-12 05:53:53 +08:00
if ( isset($terms[$i]['attr']) and $terms[$i]['attr'] == 'StorageId' and isset($terms[$j]['val']) ) {
$StorageArea = new Storage($terms[$i]['val']);
}
} // end foreach remaining term
} // end no StorageArea found yet
$filter['sql'] .= getDiskBlocks( $StorageArea );
break;
case 'SystemLoad':
$filter['sql'] .= getLoad();
break;
}
$valueList = array();
foreach ( preg_split( '/["\'\s]*?,["\'\s]*?/', preg_replace( '/^["\']+?(.+)["\']+?$/', '$1', $terms[$i]['val'] ) ) as $value ) {
switch ( $terms[$i]['attr'] ) {
case 'MonitorName':
case 'Name':
case 'Cause':
case 'Notes':
$value = dbEscape($value);
break;
case 'MonitorServerId':
case 'FilterServerId':
case 'StorageServerId':
case 'ServerId':
if ( $value == 'ZM_SERVER_ID' ) {
$value = ZM_SERVER_ID;
} else if ( $value == 'NULL' ) {
} else {
$value = dbEscape($value);
}
2016-10-18 22:20:42 +08:00
break;
case 'StorageId':
$StorageArea = new Storage( $value );
if ( $value != 'NULL' )
$value = dbEscape($value);
break;
case 'DateTime':
case 'StartDateTime':
case 'EndDateTime':
if ( $value != 'NULL' )
$value = "'".strftime( STRF_FMT_DATETIME_DB, strtotime( $value ) )."'";
break;
case 'Date':
case 'StartDate':
case 'EndDate':
if ( $value != 'NULL' )
$value = "to_days( '".strftime( STRF_FMT_DATETIME_DB, strtotime( $value ) )."' )";
break;
case 'Time':
case 'StartTime':
case 'EndTime':
if ( $value != 'NULL' )
$value = "extract( hour_second from '".strftime( STRF_FMT_DATETIME_DB, strtotime( $value ) )."' )";
break;
default :
if ( $value != 'NULL' )
$value = dbEscape($value);
break;
}
$valueList[] = $value;
}
switch ( $terms[$i]['op'] ) {
case '=' :
case '!=' :
case '>=' :
case '>' :
case '<' :
case '<=' :
$filter['sql'] .= ' '.$terms[$i]['op'].' '. $value;
break;
case '=~' :
2017-05-30 21:10:41 +08:00
$filter['sql'] .= ' regexp '.$value;
break;
case '!~' :
2017-05-30 21:10:41 +08:00
$filter['sql'] .= ' not regexp '.$value;
break;
case '=[]' :
case 'IN' :
$filter['sql'] .= ' in ('.join( ',', $valueList ).')';
break;
case '![]' :
2017-05-30 21:10:41 +08:00
$filter['sql'] .= ' not in ('.join( ',', $valueList ).')';
break;
case 'IS' :
if ( $value == 'Odd' ) {
$filter['sql'] .= ' % 2 = 1';
} else if ( $value == 'Even' ) {
$filter['sql'] .= ' % 2 = 0';
} else {
$filter['sql'] .= " IS $value";
}
2017-11-04 03:44:53 +08:00
break;
case 'IS NOT' :
$filter['sql'] .= " IS NOT $value";
2017-11-04 03:44:53 +08:00
break;
default:
Warning("Invalid operator in filter: " . $terms[$i]['op'] );
}
$filter['query'] .= $querySep.urlencode("filter[Query][terms][$i][op]").'='.urlencode($terms[$i]['op']);
$filter['fields'] .= "<input type=\"hidden\" name=\"filter[Query][terms][$i][op]\" value=\"".htmlspecialchars($terms[$i]['op'])."\"/>\n";
2018-01-12 05:53:53 +08:00
if ( isset($terms[$i]['val']) ) {
$filter['query'] .= $querySep.urlencode("filter[Query][terms][$i][val]").'='.urlencode($terms[$i]['val']);
$filter['fields'] .= "<input type=\"hidden\" name=\"filter[Query][terms][$i][val]\" value=\"".htmlspecialchars($terms[$i]['val'])."\"/>\n";
}
} // end foreach term
if ( isset($terms[$i]['cbr']) ) {
$filter['query'] .= $querySep.urlencode("filter[Query][terms][$i][cbr]").'='.urlencode($terms[$i]['cbr']);
$filter['sql'] .= ' '.str_repeat( ')', $terms[$i]['cbr'] ).' ';
$filter['fields'] .= "<input type=\"hidden\" name=\"filter[Query][terms][$i][cbr]\" value=\"".htmlspecialchars($terms[$i]['cbr'])."\"/>\n";
}
}
if ( $filter['sql'] )
2017-05-30 21:10:41 +08:00
$filter['sql'] = ' and ( '.$filter['sql'].' )';
2016-05-12 22:17:41 +08:00
if ( $saveToSession ) {
$_SESSION['filter'] = $filter;
}
}
}
// Please note that the filter is passed in by copy, so you need to use the return value from this function.
//
2016-05-12 22:17:41 +08:00
function addFilterTerm( $filter, $position, $term=false ) {
if ( $position < 0 )
$position = 0;
2017-06-20 01:24:02 +08:00
if ( ! isset( $filter['Query']['terms'] ) )
$filter['Query']['terms'] = array();
elseif( $position > count($filter['Query']['terms']) )
$position = count($filter['Query']['terms']);
if ( $term && $position == 0 )
unset( $term['cnj'] );
array_splice( $filter['Query']['terms'], $position, 0, array( $term?$term:array() ) );
return( $filter );
}
2016-05-12 22:17:41 +08:00
function delFilterTerm( $filter, $position ) {
if ( $position < 0 )
$position = 0;
elseif( $position >= count($filter['Query']['terms']) )
$position = count($filter['Query']['terms']);
array_splice( $filter['Query']['terms'], $position, 1 );
return( $filter );
}
2016-05-12 22:17:41 +08:00
function getPagination( $pages, $page, $maxShortcuts, $query, $querySep='&amp;' ) {
global $view;
2017-05-30 21:10:41 +08:00
$pageText = '';
2016-05-12 22:17:41 +08:00
if ( $pages > 1 ) {
if ( $page ) {
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 ) {
$pageText .= '<a href="?view='.$view.$querySep.'page=1'.$query.'">&lt;&lt;</a>';
}
$pageText .= '<a href="?view='.$view.$querySep.'page='.($page-1).$query.'">&lt;</a>';
$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++ ) {
$newPage = round($page-pow($lo_exp,$i));
if ( isset($pagesUsed[$newPage]) )
continue;
if ( $newPage <= 1 )
break;
$pagesUsed[$newPage] = true;
array_unshift( $newPages, $newPage );
}
if ( !isset($pagesUsed[1]) )
array_unshift( $newPages, 1 );
2016-05-12 22:17:41 +08:00
foreach ( $newPages as $newPage ) {
$pageText .= '<a href="?view='.$view.$querySep.'page='.$newPage.$query.'">'.$newPage.'</a>&nbsp;';
}
}
$pageText .= '-&nbsp;'.$page.'&nbsp;-';
2016-05-12 22:17:41 +08:00
if ( $page < $pages ) {
$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++ ) {
$newPage = round($page+pow($hi_exp,$i));
if ( isset($pagesUsed[$newPage]) )
continue;
if ( $newPage > $pages )
break;
$pagesUsed[$newPage] = true;
array_push( $newPages, $newPage );
}
if ( !isset($pagesUsed[$pages]) )
array_push( $newPages, $pages );
2016-05-12 22:17:41 +08:00
foreach ( $newPages as $newPage ) {
$pageText .= '&nbsp;<a href="?view='.$view.$querySep.'page='.$newPage.$query.'">'.$newPage.'</a>';
}
$pageText .= '<a href="?view='.$view.$querySep.'page='.($page+1).$query.'">&gt;</a>';
2016-05-12 22:17:41 +08:00
if ( false && $page < ($pages-1) ) {
$pageText .= '<a href="?view='.$view.$querySep.'page='.$pages.$query.'">&gt;&gt;</a>';
}
}
}
}
return( $pageText );
}
2016-05-12 22:17:41 +08:00
function sortHeader( $field, $querySep='&amp;' ) {
global $view;
return( '?view='.$view.$querySep.'page=1'.$_REQUEST['filter']['query'].$querySep.'sort_field='.$field.$querySep.'sort_asc='.($_REQUEST['sort_field'] == $field?!$_REQUEST['sort_asc']:0).$querySep.'limit='.$_REQUEST['limit'] );
}
2016-05-12 22:17:41 +08:00
function sortTag( $field ) {
if ( $_REQUEST['sort_field'] == $field )
if ( $_REQUEST['sort_asc'] )
return( '(^)' );
else
return( '(v)' );
return( false );
}
2016-05-12 22:17:41 +08:00
function getLoad() {
$load = sys_getloadavg();
return( $load[0] );
}
function getDiskPercent($path = ZM_DIR_EVENTS) {
$total = disk_total_space($path);
if ( $total === false ) {
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;
} elseif ( $total == 0 ) {
2017-05-30 21:10:41 +08:00
Error('disk_total_space indicates the following path has a filesystem size of zero bytes ' . $path );
return 100;
2016-05-12 22:17:41 +08:00
}
$free = disk_free_space($path);
if ( $free === false ) {
Error('disk_free_space returned false. Verify the web account user has access to ' . $path );
2016-05-12 22:17:41 +08:00
}
$space = round((($total - $free) / $total) * 100);
2016-05-12 22:17:41 +08:00
return( $space );
}
2016-05-12 22:17:41 +08:00
function getDiskBlocks() {
if ( ! $StorageArea ) $StorageArea = new Storage();
$df = shell_exec( 'df '.escapeshellarg($StorageArea->Path() ));
$space = -1;
if ( preg_match( '/\s(\d+)\s+\d+\s+\d+%/ms', $df, $matches ) )
$space = $matches[1];
return( $space );
}
function systemStats() {
$load = getLoad();
$diskPercent = getDiskPercent();
$pathMapPercent = getDiskPercent(ZM_PATH_MAP);
$cpus = getcpus();
$normalized_load = $load / $cpus;
# Colorize the system load stat
if ( $normalized_load <= 0.75 ) {
$htmlLoad=$load;
} elseif ( $normalized_load <= 0.9 ) {
$htmlLoad="<span class=\"warning\">$load</span>";
} elseif ( $normalized_load <= 1.1 ) {
$htmlLoad="<span class=\"error\">$load</span>";
} else {
$htmlLoad="<span class=\"critical\">$load</span>";
}
# Colorize the disk space stat
if ( $diskPercent < 98 ) {
$htmlDiskPercent="$diskPercent%";
} elseif ( $diskPercent <= 99 ) {
$htmlDiskPercent="<span class=\"warning\">$diskPercent%</span>";
} else {
$htmlDiskPercent="<span class=\"error\">$diskPercent%</span>";
}
# 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%";
} else {
$htmlPathMapPercent="<span class=\"warning\">$pathMapPercent%</span>";
}
} elseif ( $pathMapPercent < 100 ) {
$htmlPathMapPercent="<span class=\"warning\">$pathMapPercent%</span>";
} else {
$htmlPathMapPercent="<span class=\"critical\">$pathMapPercent%</span>";
}
$htmlString = translate('Load').": $htmlLoad - ".translate('Disk').": $htmlDiskPercent - ".ZM_PATH_MAP.": $htmlPathMapPercent";
return( $htmlString );
}
function getcpus() {
if (is_readable("/proc/cpuinfo") ) { # Works on Linux
preg_match_all('/^processor/m', file_get_contents('/proc/cpuinfo'), $matches);
$num_cpus = count($matches[0]);
} else { # Works on BSD
$matches = explode(":", shell_exec("sysctl hw.ncpu"));
$num_cpus = trim($matches[1]);
}
return( $num_cpus );
}
// Function to fix a problem whereby the built in PHP session handling
// features want to put the sid as a hidden field after the form or
// fieldset tag, neither of which will work with strict XHTML Basic.
2016-05-12 22:17:41 +08:00
function sidField() {
if ( SID ) {
list( $sessname, $sessid ) = explode( "=", SID );
?>
2016-05-12 22:17:41 +08:00
<input type="hidden" name="<?php echo $sessname ?>" value="<?php echo $sessid ?>"/>
<?php
}
}
2016-05-12 22:17:41 +08:00
function verNum( $version ) {
2017-05-30 21:10:41 +08:00
$vNum = '';
$maxFields = 3;
2017-05-30 21:10:41 +08:00
$vFields = explode( '.', $version );
array_splice( $vFields, $maxFields );
2016-05-12 22:17:41 +08:00
while ( count($vFields) < $maxFields ) {
$vFields[] = 0;
}
2016-05-12 22:17:41 +08:00
foreach ( $vFields as $vField ) {
2017-05-30 21:10:41 +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;
}
$vNum .= $vField;
}
return( $vNum );
}
2016-05-12 22:17:41 +08:00
function fixSequences() {
$sequence = 1;
2017-05-30 21:10:41 +08:00
$sql = 'select * from Monitors order by Sequence asc, Id asc';
2016-05-12 22:17:41 +08:00
foreach( dbFetchAll( $sql ) as $monitor ) {
if ( $monitor['Sequence'] != $sequence ) {
dbQuery( 'update Monitors set Sequence = ? WHERE Id=?', array( $sequence, $monitor['Id'] ) );
}
$sequence++;
}
}
2016-05-12 22:17:41 +08:00
function firstSet() {
foreach ( func_get_args() as $arg ) {
if ( !empty( $arg ) )
return( $arg );
}
}
2016-05-12 22:17:41 +08:00
function linesIntersect( $line1, $line2 ) {
global $debug;
$min_x1 = min( $line1[0]['x'], $line1[1]['x'] );
$max_x1 = max( $line1[0]['x'], $line1[1]['x'] );
$min_x2 = min( $line2[0]['x'], $line2[1]['x'] );
$max_x2 = max( $line2[0]['x'], $line2[1]['x'] );
$min_y1 = min( $line1[0]['y'], $line1[1]['y'] );
$max_y1 = max( $line1[0]['y'], $line1[1]['y'] );
$min_y2 = min( $line2[0]['y'], $line2[1]['y'] );
$max_y2 = max( $line2[0]['y'], $line2[1]['y'] );
// Checking if bounding boxes intersect
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>';
return( false );
}
$dx1 = $line1[1]['x'] - $line1[0]['x'];
$dy1 = $line1[1]['y'] - $line1[0]['y'];
$dx2 = $line2[1]['x'] - $line2[0]['x'];
$dy2 = $line2[1]['y'] - $line2[0]['y'];
2016-05-12 22:17:41 +08:00
if ( $dx1 ) {
$m1 = $dy1/$dx1;
$b1 = $line1[0]['y'] - ($m1 * $line1[0]['x']);
2016-05-12 22:17:41 +08:00
} else {
$b1 = $line1[0]['y'];
}
2016-05-12 22:17:41 +08:00
if ( $dx2 ) {
$m2 = $dy2/$dx2;
$b2 = $line2[0]['y'] - ($m2 * $line2[0]['x']);
2016-05-12 22:17:41 +08:00
} else {
$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
$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 ) {
if ( $debug ) echo "Intersecting, at x $x<br>";
return( true );
2016-05-12 22:17:41 +08:00
} else {
if ( $debug ) echo "Not intersecting, out of range at x $x<br>";
return( false );
}
2016-05-12 22:17:41 +08:00
} elseif ( $b1 == $b2 ) {
// Colinear, must overlap due to box check, intersect?
2017-05-30 21:10:41 +08:00
if ( $debug ) echo 'Intersecting, colinear<br>';
return( true );
2016-05-12 22:17:41 +08:00
} else {
// Parallel
2017-05-30 21:10:41 +08:00
if ( $debug ) echo 'Not intersecting, parallel<br>';
return( false );
}
2016-05-12 22:17:41 +08:00
} elseif ( !$dx1 ) { // Line 1 is vertical
$y = ( $m2 * $line1[0]['x'] ) * $b2;
2016-05-12 22:17:41 +08:00
if ( $y >= $min_y1 && $y <= $max_y1 ) {
if ( $debug ) echo "Intersecting, at y $y<br>";
return( true );
2016-05-12 22:17:41 +08:00
} else {
if ( $debug ) echo "Not intersecting, out of range at y $y<br>";
return( false );
}
2016-05-12 22:17:41 +08:00
} elseif ( !$dx2 ) { // Line 2 is vertical
$y = ( $m1 * $line2[0]['x'] ) * $b1;
2016-05-12 22:17:41 +08:00
if ( $y >= $min_y2 && $y <= $max_y2 ) {
if ( $debug ) echo "Intersecting, at y $y<br>";
return( true );
2016-05-12 22:17:41 +08:00
} else {
if ( $debug ) echo "Not intersecting, out of range at y $y<br>";
return( false );
}
2016-05-12 22:17:41 +08:00
} else { // Both lines are vertical
if ( $line1[0]['x'] == $line2[0]['x'] ) {
// Colinear, must overlap due to box check, intersect?
2017-05-30 21:10:41 +08:00
if ( $debug ) echo 'Intersecting, vertical, colinear<br>';
return( true );
2016-05-12 22:17:41 +08:00
} else {
// Parallel
2017-05-30 21:10:41 +08:00
if ( $debug ) echo 'Not intersecting, vertical, parallel<br>';
return( false );
}
}
2017-05-30 21:10:41 +08:00
if ( $debug ) echo 'Whoops, unexpected scenario<br>';
return( false );
}
2016-05-12 22:17:41 +08:00
function isSelfIntersecting( $points ) {
global $debug;
$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++ ) {
$edges[] = array( $points[$i], $points[$j] );
}
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++ ) {
if ( $debug ) echo "Checking $i and $j<br>";
2016-05-12 22:17:41 +08:00
if ( linesIntersect( $edges[$i], $edges[$j] ) ) {
if ( $debug ) echo "Lines $i and $j intersect<br>";
return( true );
}
}
}
return( false );
}
2016-05-12 22:17:41 +08:00
function getPolyCentre( $points, $area=0 ) {
$cx = 0.0;
$cy = 0.0;
if ( !$area )
$area = getPolyArea( $points );
2016-05-12 22:17:41 +08:00
for ( $i = 0, $j = count($points)-1; $i < count($points); $j = $i++ ) {
$ct = ($points[$i]['x'] * $points[$j]['y']) - ($points[$j]['x'] * $points[$i]['y']);
$cx += ($points[$i]['x'] + $points[$j]['x']) * ct;
$cy += ($points[$i]['y'] + $points[$j]['y']) * ct;
}
$cx = intval(round(abs($cx/(6.0*$area))));
$cy = intval(round(abs($cy/(6.0*$area))));
printf( "X:%cx, Y:$cy<br>" );
return( array( 'x'=>$cx, 'y'=>$cy ) );
}
2016-05-12 22:17:41 +08:00
function _CompareXY( $a, $b ) {
if ( $a['min_y'] == $b['min_y'] )
return( intval($a['min_x'] - $b['min_x']) );
else
return( intval($a['min_y'] - $b['min_y']) );
}
2016-05-12 22:17:41 +08:00
function _CompareX( $a, $b ) {
return( intval($a['min_x'] - $b['min_x']) );
}
2016-05-12 22:17:41 +08:00
function getPolyArea( $points ) {
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++ ) {
$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,
);
}
2017-05-30 21:10:41 +08:00
usort( $global_edges, '_CompareXY' );
2016-05-12 22:17:41 +08:00
if ( $debug ) {
for ( $i = 0; $i < count($global_edges); $i++ ) {
2017-05-30 21:10:41 +08:00
printf( '%d: min_y: %d, max_y:%d, min_x:%.2f, 1/m:%.2f<br>', $i, $global_edges[$i]['min_y'], $global_edges[$i]['max_y'], $global_edges[$i]['min_x'], $global_edges[$i]['_1_m'] );
}
}
$area = 0.0;
$active_edges = array();
$y = $global_edges[0]['min_y'];
2016-05-12 22:17:41 +08:00
do {
for ( $i = 0; $i < count($global_edges); $i++ ) {
if ( $global_edges[$i]['min_y'] == $y ) {
2017-05-30 21:10:41 +08:00
if ( $debug ) printf( 'Moving global edge<br>' );
$active_edges[] = $global_edges[$i];
array_splice( $global_edges, $i, 1 );
$i--;
2016-05-12 22:17:41 +08:00
} else {
break;
}
}
2017-05-30 21:10:41 +08:00
usort( $active_edges, '_CompareX' );
2016-05-12 22:17:41 +08:00
if ( $debug ) {
for ( $i = 0; $i < count($active_edges); $i++ ) {
2017-05-30 21:10:41 +08:00
printf( '%d - %d: min_y: %d, max_y:%d, min_x:%.2f, 1/m:%.2f<br>', $y, $i, $active_edges[$i]['min_y'], $active_edges[$i]['max_y'], $active_edges[$i]['min_x'], $active_edges[$i]['_1_m'] );
}
}
$last_x = 0;
$row_area = 0;
$parity = false;
2016-05-12 22:17:41 +08:00
for ( $i = 0; $i < count($active_edges); $i++ ) {
$x = intval(round($active_edges[$i]['min_x']));
2016-05-12 22:17:41 +08:00
if ( $parity ) {
$row_area += ($x - $last_x)+1;
$area += $row_area;
}
if ( $active_edges[$i]['max_y'] != $y )
$parity = !$parity;
$last_x = $x;
}
2017-05-30 21:10:41 +08:00
if ( $debug ) printf( '%d: Area:%d<br>', $y, $row_area );
$y++;
2016-05-12 22:17:41 +08:00
for ( $i = 0; $i < count($active_edges); $i++ ) {
if ( $y >= $active_edges[$i]['max_y'] ) { // Or >= as per sheets
2017-05-30 21:10:41 +08:00
if ( $debug ) printf( 'Deleting active_edge<br>' );
array_splice( $active_edges, $i, 1 );
$i--;
2016-05-12 22:17:41 +08:00
} else {
$active_edges[$i]['min_x'] += $active_edges[$i]['_1_m'];
}
}
} while ( count($global_edges) || count($active_edges) );
2017-05-30 21:10:41 +08:00
if ( $debug ) printf( 'Area:%d<br>', $area );
return( $area );
}
2016-05-12 22:17:41 +08:00
function getPolyAreaOld( $points ) {
$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++ ) {
$x_diff = ($points[$i]['x'] - $points[$j]['x']);
$y_diff = ($points[$i]['y'] - $points[$j]['y']);
$y_sum = ($points[$i]['y'] + $points[$j]['y']);
$trap_edge = sqrt(pow(abs($x_diff)+1,2) + pow(abs($y_diff)+1,2) );
$edge += $trap_edge;
$trap_area = ($x_diff * $y_sum );
$area += $trap_area;
printf( "%d->%d, %d-%d=%.2f, %d+%d=%.2f(%.2f), %.2f, %.2f<br>", i, j, $points[$i]['x'], $points[$j]['x'], $x_diff, $points[$i]['y'], $points[$j]['y'], $y_sum, $y_diff, $trap_area, $trap_edge );
}
$edge = intval(round(abs($edge)));
$area = intval(round((abs($area)+$edge)/2));
echo "E:$edge<br>";
echo "A:$area<br>";
return( $area );
}
2016-05-12 22:17:41 +08:00
function mapCoords( $a ) {
return( $a['x'].",".$a['y'] );
}
2016-05-12 22:17:41 +08:00
function pointsToCoords( $points ) {
2017-05-30 21:10:41 +08:00
return( join( ' ', array_map( 'mapCoords', $points ) ) );
}
2016-05-12 22:17:41 +08:00
function coordsToPoints( $coords ) {
$points = array();
2016-05-12 22:17:41 +08:00
if ( preg_match_all( '/(\d+,\d+)+/', $coords, $matches ) ) {
for ( $i = 0; $i < count($matches[1]); $i++ ) {
if ( preg_match( '/(\d+),(\d+)/', $matches[1][$i], $cmatches ) ) {
$points[] = array( 'x'=>$cmatches[1], 'y'=>$cmatches[2] );
2016-05-12 22:17:41 +08:00
} else {
echo( "Bogus coordinates '".$matches[$i]."'" );
return( false );
}
}
2016-05-12 22:17:41 +08:00
} else {
echo( "Bogus coordinate string '$coords'" );
return( false );
}
return( $points );
}
function limitPoints( &$points, $min_x, $min_y, $max_x, $max_y ) {
foreach ( $points as &$point ) {
if ( $point['x'] < $min_x ) {
2017-05-19 00:49:59 +08:00
Logger::Debug('Limiting point x'.$point['x'].' to min_x ' . $min_x );
$point['x'] = $min_x;
} else if ( $point['x'] > $max_x ) {
2017-05-19 00:49:59 +08:00
Logger::Debug('Limiting point x'.$point['x'].' to max_x ' . $max_x );
$point['x'] = $max_x;
}
2017-05-30 23:15:00 +08:00
if ( $point['y'] < $min_y ) {
2017-05-19 00:49:59 +08:00
Logger::Debug('Limiting point y'.$point['y'].' to min_y ' . $min_y );
$point['y'] = $min_y;
} else if ( $point['y'] > $max_y ) {
2017-05-19 00:49:59 +08:00
Logger::Debug('Limiting point y'.$point['y'].' to max_y ' . $max_y );
$point['y'] = $max_y;
}
} // end foreach point
} // end function limitPoints( $points, $min_x, $min_y, $max_x, $max_y )
function scalePoints( &$points, $scale ) {
foreach ( $points as &$point ) {
$point['x'] = reScale( $point['x'], $scale );
$point['y'] = reScale( $point['y'], $scale );
}
}
2016-05-12 22:17:41 +08:00
function getLanguages() {
$langs = array();
2017-05-30 21:10:41 +08:00
foreach ( glob('lang/*_*.php') as $file ) {
preg_match( '/([^\/]+_.+)\.php/', $file, $matches );
$langs[$matches[1]] = $matches[1];
}
return( $langs );
}
2016-05-12 22:17:41 +08:00
function trimString( $string, $length ) {
return( preg_replace( '/^(.{'.$length.',}?)\b.*$/', '\\1&hellip;', $string ) );
}
2016-05-12 22:17:41 +08:00
function monitorIdsToNames( $ids ) {
global $mITN_monitors;
2016-05-12 22:17:41 +08:00
if ( !$mITN_monitors ) {
2017-05-30 21:10:41 +08:00
$sql = 'select Id, Name from Monitors';
2016-05-12 22:17:41 +08:00
foreach( dbFetchAll( $sql ) as $monitor ) {
$mITN_monitors[$monitor['Id']] = $monitor;
}
}
$names = array();
if ( ! is_array($ids) ) {
$ids = preg_split( '/\s*,\s*/', $ids );
}
foreach ( $ids as $id ) {
2016-05-12 22:17:41 +08:00
if ( visibleMonitor( $id ) ) {
if ( isset($mITN_monitors[$id]) ) {
$names[] = $mITN_monitors[$id]['Name'];
}
}
}
$name_string = join( ', ', $names );
return( $name_string );
}
2016-05-12 22:17:41 +08:00
function initX10Status() {
global $x10_status;
2016-05-12 22:17:41 +08:00
if ( !isset($x10_status) ) {
$socket = socket_create( AF_UNIX, SOCK_STREAM, 0 );
2016-05-12 22:17:41 +08:00
if ( $socket < 0 ) {
2017-05-30 21:10:41 +08:00
Fatal( 'socket_create() failed: '.socket_strerror($socket) );
}
$sock_file = ZM_PATH_SOCKS.'/zmx10.sock';
2016-05-12 22:17:41 +08:00
if ( @socket_connect( $socket, $sock_file ) ) {
2017-05-30 21:10:41 +08:00
$command = 'status';
2016-05-12 22:17:41 +08:00
if ( !socket_write( $socket, $command ) ) {
Fatal( "Can't write to control socket: ".socket_strerror(socket_last_error($socket)) );
}
socket_shutdown( $socket, 1 );
2017-05-30 21:10:41 +08:00
$x10Output = '';
2016-05-12 22:17:41 +08:00
while ( $x10Response = socket_read( $socket, 256 ) ) {
$x10Output .= $x10Response;
}
socket_close( $socket );
2016-05-12 22:17:41 +08:00
} else {
// Can't connect so use script
$command = ZM_PATH_BIN."/zmx10.pl --command status";
//$command .= " 2>/dev/null >&- <&- >/dev/null";
$x10Output = exec( escapeshellcmd( $command ) );
}
2016-05-12 22:17:41 +08:00
foreach ( explode( "\n", $x10Output ) as $x10Response ) {
if ( preg_match( "/^(\d+)\s+(.+)$/", $x10Response, $matches ) ) {
$x10_status[$matches[1]] = $matches[2];
}
}
}
}
2016-05-12 22:17:41 +08:00
function getDeviceStatusX10( $key ) {
global $x10_status;
initX10Status();
if ( empty($x10_status[$key]) || !($status = $x10_status[$key]) )
2017-05-30 21:10:41 +08:00
$status = 'unknown';
return( $status );
}
2016-05-12 22:17:41 +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 ) {
2017-05-30 21:10:41 +08:00
Fatal( 'socket_create() failed: '.socket_strerror($socket) );
}
$sock_file = ZM_PATH_SOCKS.'/zmx10.sock';
2016-05-12 22:17:41 +08:00
if ( @socket_connect( $socket, $sock_file ) ) {
$command = "$status;$key";
2016-05-12 22:17:41 +08:00
if ( !socket_write( $socket, $command ) ) {
Fatal( "Can't write to control socket: ".socket_strerror(socket_last_error($socket)) );
}
socket_shutdown( $socket, 1 );
$x10Response = socket_read( $socket, 256 );
socket_close( $socket );
2016-05-12 22:17:41 +08:00
} else {
// Can't connect so use script
$command = ZM_PATH_BIN.'/zmx10.pl --command '.escapeshellarg( $status );
$command .= ' --unit-code '.escapeshellarg( $key );
//$command .= " 2>/dev/null >&- <&- >/dev/null";
$x10Response = exec( $command );
}
if ( preg_match( '/^'.$key.'\s+(.*)/', $x10Response, $matches ) )
$status = $matches[1];
else
2017-05-30 21:10:41 +08:00
$status = 'unknown';
return( $status );
}
2016-05-12 22:17:41 +08:00
function logState() {
$state = 'ok';
$levelCounts = array(
Logger::FATAL => array( ZM_LOG_ALERT_FAT_COUNT, ZM_LOG_ALARM_FAT_COUNT ),
Logger::ERROR => array( ZM_LOG_ALERT_ERR_COUNT, ZM_LOG_ALARM_ERR_COUNT ),
Logger::WARNING => array( ZM_LOG_ALERT_WAR_COUNT, ZM_LOG_ALARM_WAR_COUNT ),
);
2017-10-26 02:11:19 +08:00
# This is an expensive request, as it has to hit every row of the Logs Table
$sql = 'SELECT Level, COUNT(Level) AS LevelCount FROM Logs WHERE Level < '.Logger::INFO.' AND TimeKey > unix_timestamp(now() - interval '.ZM_LOG_CHECK_PERIOD.' second) GROUP BY Level ORDER BY Level ASC';
$counts = dbFetchAll($sql);
if ( $counts ) {
foreach ( $counts as $count ) {
if ( $count['Level'] <= Logger::PANIC )
$count['Level'] = Logger::FATAL;
if ( !($levelCount = $levelCounts[$count['Level']]) ) {
Error('Unexpected Log level '.$count['Level']);
next;
}
if ( $levelCount[1] && $count['LevelCount'] >= $levelCount[1] ) {
$state = 'alarm';
break;
} elseif ( $levelCount[0] && $count['LevelCount'] >= $levelCount[0] ) {
$state = 'alert';
}
}
}
return $state;
}
2016-05-12 22:17:41 +08:00
function isVector ( &$array ) {
$next_key = 0;
2016-05-12 22:17:41 +08:00
foreach ( array_keys($array) as $key ) {
if ( !is_int( $key ) )
return( false );
if ( $key != $next_key++ )
return( false );
}
return( true );
}
2016-05-12 22:17:41 +08:00
function checkJsonError($value) {
if ( function_exists('json_last_error') ) {
$value = var_export($value,true);
2016-05-12 22:17:41 +08:00
switch( json_last_error() ) {
case JSON_ERROR_DEPTH :
Fatal( "Unable to decode JSON string '$value', maximum stack depth exceeded" );
case JSON_ERROR_CTRL_CHAR :
Fatal( "Unable to decode JSON string '$value', unexpected control character found" );
case JSON_ERROR_STATE_MISMATCH :
Fatal( "Unable to decode JSON string '$value', invalid or malformed JSON" );
case JSON_ERROR_SYNTAX :
Fatal( "Unable to decode JSON string '$value', syntax error" );
default :
Fatal( "Unable to decode JSON string '$value', unexpected error ".json_last_error() );
case JSON_ERROR_NONE:
break;
}
}
}
2016-05-12 22:17:41 +08:00
function jsonEncode( &$value ) {
if ( function_exists('json_encode') ) {
$string = json_encode( $value );
checkJsonError($value);
return( $string );
}
2016-05-12 22:17:41 +08:00
switch ( gettype($value) ) {
case 'double':
case 'integer':
return( $value );
case 'boolean':
return( $value?'true':'false' );
case 'string':
return( '"'.preg_replace( "/\r?\n/", '\\n', addcslashes($value,'"\\/') ).'"' );
case 'NULL':
return( 'null' );
case 'object':
return( '"Object '.addcslashes(get_class($value),'"\\/').'"' );
case 'array':
if ( isVector( $value ) )
return( '['.join( ',', array_map( 'jsonEncode', $value) ).']' );
2016-05-12 22:17:41 +08:00
else {
$result = '{';
2016-05-12 22:17:41 +08:00
foreach ($value as $subkey => $subvalue ) {
if ( $result != '{' )
$result .= ',';
$result .= '"'.$subkey.'":'.jsonEncode( $subvalue );
}
return( $result.'}' );
}
default:
return( '"'.addcslashes(gettype($value),'"\\/').'"' );
}
}
2016-05-12 22:17:41 +08:00
function jsonDecode( $value ) {
if ( function_exists('json_decode') ) {
$object = json_decode( $value, true );
checkJsonError($value);
return( $object );
}
$comment = false;
$unescape = false;
$out = '$result=';
2016-05-12 22:17:41 +08:00
for ( $i = 0; $i < strlen($value); $i++ ) {
if ( !$comment ) {
if ( ($value[$i] == '{') || ($value[$i] == '[') ) {
$out .= ' array(';
2016-05-12 22:17:41 +08:00
} else if ( ($value[$i] == '}') || ($value[$i] == ']') ) {
$out .= ')';
} else if ( $value[$i] == ':' ) {
$out .= '=>';
} else {
$out .= $value[$i];
}
} else if ( !$unescape ) {
if ( $value[$i] == '\\' )
$unescape = true;
else
$out .= $value[$i];
} else {
if ( $value[$i] != '/' )
$out .= '\\';
$out .= $value[$i];
$unescape = false;
}
if ( $value[$i] == '"' ) {
$comment = !$comment;
}
}
eval( $out.';' );
return( $result );
}
define( 'HTTP_STATUS_OK', 200 );
define( 'HTTP_STATUS_BAD_REQUEST', 400 );
define( 'HTTP_STATUS_FORBIDDEN', 403 );
2016-05-12 22:17:41 +08:00
function ajaxError( $message, $code=HTTP_STATUS_OK ) {
Error( $message );
if ( function_exists( 'ajaxCleanup' ) )
ajaxCleanup();
2016-05-12 22:17:41 +08:00
if ( $code == HTTP_STATUS_OK ) {
$response = array( 'result'=>'Error', 'message'=>$message );
2017-05-30 21:10:41 +08:00
header( 'Content-type: text/plain' );
exit( jsonEncode( $response ) );
}
header( "HTTP/1.0 $code $message" );
exit();
}
2016-05-12 22:17:41 +08:00
function ajaxResponse( $result=false ) {
if ( function_exists( 'ajaxCleanup' ) )
ajaxCleanup();
$response = array( 'result'=>'Ok' );
2018-01-26 01:13:31 +08:00
if ( is_array( $result ) ) {
$response = array_merge( $response, $result );
2018-01-26 01:13:31 +08:00
} elseif ( !empty($result) ) {
$response['message'] = $result;
2018-01-26 01:13:31 +08:00
}
2017-05-30 21:10:41 +08:00
header( 'Content-type: text/plain' );
exit( jsonEncode( $response ) );
}
2016-05-12 22:17:41 +08:00
function generateConnKey() {
return( rand( 1, 999999 ) );
}
2016-05-12 22:17:41 +08:00
function detaintPath( $path ) {
// Remove any absolute paths, or relative ones that want to go up
$path = preg_replace( '/\.(?:\.+[\\/][\\/]*)+/', '', $path );
$path = preg_replace( '/^[\\/]+/', '', $path );
return( $path );
}
function cache_bust( $file ) {
# Use the last modified timestamp to create a link that gets a different filename
# To defeat caching. Should probably use md5 hash
$parts = pathinfo($file);
global $css;
2017-12-22 10:46:21 +08:00
$dirname = preg_replace( '/\//', '_', $parts['dirname'] );
$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) ) {
return 'cache/'.$cacheFile;
} else {
Warning("Failed linking $file to $cacheFile");
}
return $file;
}
2016-05-12 22:17:41 +08:00
function getSkinFile( $file ) {
global $skinBase;
$skinFile = false;
2016-05-12 22:17:41 +08:00
foreach ( $skinBase as $skin ) {
$tempSkinFile = detaintPath( 'skins'.'/'.$skin.'/'.$file );
if ( file_exists( $tempSkinFile ) )
$skinFile = $tempSkinFile;
}
return $skinFile;
}
2017-05-31 01:51:00 +08:00
function getSkinIncludes( $file, $includeBase=false, $asOverride=false ) {
global $skinBase;
$skinFile = false;
2016-05-12 22:17:41 +08:00
foreach ( $skinBase as $skin ) {
$tempSkinFile = detaintPath( 'skins'.'/'.$skin.'/'.$file );
if ( file_exists( $tempSkinFile ) )
$skinFile = $tempSkinFile;
}
$includeFiles = array();
2016-05-12 22:17:41 +08:00
if ( $asOverride ) {
if ( $skinFile )
$includeFiles[] = $skinFile;
else if ( $includeBase )
$includeFiles[] = $file;
2016-05-12 22:17:41 +08:00
} else {
if ( $includeBase )
$includeFiles[] = $file;
if ( $skinFile )
$includeFiles[] = $skinFile;
}
return( $includeFiles );
}
2017-05-30 21:10:41 +08:00
function requestVar( $name, $default='' ) {
return( isset($_REQUEST[$name])?validHtmlStr($_REQUEST[$name]):$default );
}
// For numbers etc in javascript or tags etc
2016-05-12 22:17:41 +08:00
function validInt( $input ) {
return( preg_replace( '/\D/', '', $input ) );
}
2016-05-12 22:17:41 +08:00
function validNum( $input ) {
return( preg_replace( '/[^\d.-]/', '', $input ) );
}
// For general strings
2016-05-12 22:17:41 +08:00
function validStr( $input ) {
return( strip_tags( $input ) );
}
// For strings in javascript or tags etc, expected to be in quotes so further quotes escaped rather than converted
2016-05-12 22:17:41 +08:00
function validJsStr( $input ) {
return( strip_tags( addslashes( $input ) ) );
}
// For general text in pages outside of tags or quotes so quotes converted to entities
2016-05-12 22:17:41 +08:00
function validHtmlStr( $input ) {
return( htmlspecialchars( $input, ENT_QUOTES ) );
}
function getStreamHTML( $monitor, $options = array() ) {
if ( isset($options['scale']) and $options['scale'] and ( $options['scale'] != 100 ) ) {
2017-11-09 00:26:13 +08:00
//Warning("Scale to " . $options['scale'] );
2017-11-23 01:33:34 +08:00
$options['width'] = reScale( $monitor->Width(), $options['scale'] ) . 'px';
$options['height'] = reScale( $monitor->Height(), $options['scale'] ) . 'px';
} else {
# 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'] ) ) {
2017-11-23 01:33:34 +08:00
$options['width'] = $monitor->Width() . 'px';
if ( ! ( isset($options['height']) and $options['height'] ) ) {
2017-11-23 01:33:34 +08:00
$options['height'] = $monitor->Height() . 'px';
}
2017-11-23 01:33:34 +08:00
} else if ( ! isset($options['height']) ) {
$options['height'] = '';
}
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'] );
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.
} else if ( ZM_WEB_STREAM_METHOD == 'mpeg' && ZM_MPEG_LIVE_FORMAT ) {
2017-11-09 00:26:13 +08:00
$streamSrc = $monitor->getStreamSrc( array(
'mode'=>'mpeg',
'scale'=>(isset($options['scale'])?$options['scale']:100),
2017-11-09 00:26:13 +08:00
'bitrate'=>ZM_WEB_VIDEO_BITRATE,
'maxfps'=>ZM_WEB_VIDEO_MAXFPS,
'format' => ZM_MPEG_LIVE_FORMAT
) );
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';
$streamSrc = $monitor->getStreamSrc($options);
2017-03-02 04:26:40 +08:00
2016-05-12 22:17:41 +08:00
if ( canStreamNative() )
return getImageStreamHTML( 'liveStream'.$monitor->Id(), $streamSrc, $options['width'], $options['height'], $monitor->Name());
2016-05-12 22:17:41 +08:00
elseif ( canStreamApplet() )
// Helper, empty widths and heights really don't work.
return getHelperStream( 'liveStream'.$monitor->Id(), $streamSrc,
$options['width'] ? $options['width'] : $monitor->Width(),
$options['height'] ? $options['height'] : $monitor->Height(),
$monitor->Name());
2016-05-12 22:17:41 +08:00
} else {
if ( $options['mode'] == 'stream' ) {
Info( 'The system has fallen back to single jpeg mode for streaming. Consider enabling Cambozola or upgrading the client browser.' );
}
$options['mode'] = 'single';
$streamSrc = $monitor->getStreamSrc( $options );
return getImageStill( 'liveStream'.$monitor->Id(), $streamSrc, $options['width'], $options['height'], $monitor->Name());
2016-05-12 22:17:41 +08:00
}
} // end function getStreamHTML
function getStreamMode( ) {
$streamMode = '';
if ( ZM_WEB_STREAM_METHOD == 'mpeg' && ZM_MPEG_LIVE_FORMAT ) {
$streamMode = 'mpeg';
} elseif ( canStream() ) {
$streamMode = 'jpeg';
} else {
$streamMode = 'single';
Info( 'The system has fallen back to single jpeg mode for streaming. Consider enabling Cambozola or upgrading the client browser.' );
}
2017-11-19 03:05:11 +08:00
return $streamMode;
} // end function getStreamMode
2016-07-20 05:34:01 +08:00
function folder_size($dir) {
$size = 0;
foreach (glob(rtrim($dir, '/').'/*', GLOB_NOSORT) as $each) {
2017-10-11 01:39:17 +08:00
$size += is_file($each) ? filesize($each) : folder_size($each);
2016-07-20 05:34:01 +08:00
}
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) {
$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() {
csrf_conf('rewrite-js', 'includes/csrf/csrf-magic.js');
}
function check_timezone() {
$now = new DateTime();
$sys_tzoffset = trim(shell_exec('date "+%z"'));
$php_tzoffset = trim($now->format('O'));
$mysql_tzoffset = trim(dbFetchOne("SELECT TIME_FORMAT(TIMEDIFF(NOW(), UTC_TIMESTAMP),'%H%i');",'TIME_FORMAT(TIMEDIFF(NOW(), UTC_TIMESTAMP),\'%H%i\')'));
#Logger::Debug("System timezone offset determine to be: $sys_tzoffset,\x20
#PHP timezone offset determine to be: $php_tzoffset,\x20
#Mysql timezone offset determine to be: $mysql_tzoffset
#");
if ( $sys_tzoffset != $php_tzoffset )
Fatal("ZoneMinder is not installed properly: php's date.timezone does not match the system timezone!");
if ( $sys_tzoffset != $mysql_tzoffset )
Error("ZoneMinder is not installed properly: mysql's timezone does not match the system timezone! Event lists will display incorrect times.");
if (!ini_get('date.timezone') || !date_default_timezone_set(ini_get('date.timezone')))
Fatal( "ZoneMinder is not installed properly: php's date.timezone is not set to a valid timezone" );
}
function unparse_url($parsed_url, $substitutions = array() ) {
$fields = array('scheme','host','port','user','pass','path','query','fragment');
foreach ( $fields as $field ) {
if ( isset( $substitutions[$field] ) ) {
$parsed_url[$field] = $substitutions[$field];
}
}
$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";
}
// 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 ) {
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;
}
// The following works around php not being built with semaphore functions.
if (!function_exists('sem_get')) {
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);
}
}
if( !function_exists('ftok') ) {
function ftok($filename = "", $proj = "") {
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));
}
}
}
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;
}
function format_duration($time, $separator=':') {
return sprintf('%02d%s%02d%s%02d', floor($time/3600), $separator, ($time/60)%60, $separator, $time%60);
}
?>