zoneminder/web/includes/functions.php

2331 lines
75 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
2017-05-30 21:10:41 +08:00
if ( version_compare( phpversion(), '4.3.0', '<') ) {
2016-05-12 22:17:41 +08:00
function ob_get_clean() {
$buffer = ob_get_contents();
ob_end_clean();
return( $buffer );
}
}
2017-05-31 00:04:12 +08:00
# We are requiring these because this file is getting included from the api, which hasn't already included them.
require_once( 'logger.php' );
require_once( 'database.php' );
2017-05-30 21:10:41 +08:00
function userLogin( $username, $password='', $passwordHashed=false ) {
2016-05-12 22:17:41 +08:00
global $user, $cookies;
2017-05-30 23:15:00 +08:00
$sql = 'SELECT * FROM Users WHERE Enabled = 1';
2016-05-12 22:17:41 +08:00
$sql_values = NULL;
2017-05-30 21:10:41 +08:00
if ( ZM_AUTH_TYPE == 'builtin' ) {
2016-05-12 22:17:41 +08:00
if ( $passwordHashed ) {
2017-05-30 21:10:41 +08:00
$sql .= ' AND Username=? AND Password=?';
} else {
2017-05-30 21:10:41 +08:00
$sql .= ' AND Username=? AND Password=password(?)';
}
2016-05-12 22:17:41 +08:00
$sql_values = array( $username, $password );
} else {
2017-05-30 21:10:41 +08:00
$sql .= ' AND Username = ?';
2016-05-12 22:17:41 +08:00
$sql_values = array( $username );
}
$_SESSION['username'] = $username;
2017-05-30 21:10:41 +08:00
if ( ZM_AUTH_RELAY == 'plain' ) {
2016-05-12 22:17:41 +08:00
// Need to save this in session
$_SESSION['password'] = $password;
}
$_SESSION['remoteAddr'] = $_SERVER['REMOTE_ADDR']; // To help prevent session hijacking
if ( $dbUser = dbFetchOne( $sql, NULL, $sql_values ) ) {
Info( "Login successful for user \"$username\"" );
$_SESSION['user'] = $user = $dbUser;
unset($_SESSION['loginFailed']);
2017-05-30 21:10:41 +08:00
if ( ZM_AUTH_TYPE == 'builtin' ) {
2016-05-12 22:17:41 +08:00
$_SESSION['passwordHash'] = $user['Password'];
}
session_regenerate_id();
2016-05-12 22:17:41 +08:00
} else {
Warning( "Login denied for user \"$username\"" );
$_SESSION['loginFailed'] = true;
2016-05-12 22:17:41 +08:00
unset( $user );
}
if ( $cookies )
session_write_close();
}
2016-05-12 22:17:41 +08:00
function userLogout() {
global $user;
$username = $user['Username'];
2016-05-12 22:17:41 +08:00
Info( "User \"$username\" logged out" );
2016-05-12 22:17:41 +08:00
unset( $_SESSION['user'] );
unset( $user );
2016-05-12 22:17:41 +08:00
session_destroy();
}
2016-05-12 22:17:41 +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
}
function CORSHeaders() {
2016-05-12 22:17:41 +08:00
if ( isset( $_SERVER['HTTP_ORIGIN'] ) ) {
# The following is left for future reference/use.
$valid = false;
$servers = dbFetchAll( 'SELECT * FROM Servers' );
if ( sizeof($servers) <= 1 ) {
# Only need CORSHeaders in the event that there are multiple servers in use.
return;
}
foreach( dbFetchAll( 'SELECT * FROM Servers' ) as $row ) {
$Server = new Server( $row );
if ( $_SERVER['HTTP_ORIGIN'] == $Server->Url() ) {
$valid = true;
2017-05-30 21:10:41 +08:00
header('Access-Control-Allow-Origin: ' . $Server->Url() );
header('Access-Control-Allow-Headers: x-requested-with,x-request');
2016-05-12 22:17:41 +08:00
}
}
if ( ! $valid ) {
2017-05-30 21:10:41 +08:00
Warning( $_SERVER['HTTP_ORIGIN'] . ' is not found in servers list.' );
}
2016-05-12 22:17:41 +08:00
}
}
2016-05-12 22:17:41 +08:00
function getAuthUser( $auth ) {
2017-05-30 21:10:41 +08:00
if ( ZM_OPT_USE_AUTH && ZM_AUTH_RELAY == 'hashed' && !empty($auth) ) {
$remoteAddr = '';
2016-05-12 22:17:41 +08:00
if ( ZM_AUTH_HASH_IPS ) {
$remoteAddr = $_SERVER['REMOTE_ADDR'];
if ( !$remoteAddr ) {
Error( "Can't determine remote address for authentication, using empty string" );
2017-05-30 21:10:41 +08:00
$remoteAddr = '';
2016-05-12 22:17:41 +08:00
}
}
2017-05-30 21:10:41 +08:00
$sql = 'select Username, Password, Enabled, Stream+0, Events+0, Control+0, Monitors+0, System+0, MonitorIds from Users where Enabled = 1';
2016-05-12 22:17:41 +08:00
foreach ( dbFetchAll( $sql ) as $user ) {
$now = time();
for ( $i = 0; $i < 2; $i++, $now -= (60*60) ) { // Try for last two hours
$time = localtime( $now );
$authKey = ZM_AUTH_HASH_SECRET.$user['Username'].$user['Password'].$remoteAddr.$time[2].$time[3].$time[4].$time[5];
$authHash = md5( $authKey );
2016-05-12 22:17:41 +08:00
if ( $auth == $authHash ) {
return( $user );
}
2016-05-12 22:17:41 +08:00
}
}
2016-05-12 22:17:41 +08:00
}
Error( "Unable to authenticate user from auth hash '$auth'" );
return( false );
}
2016-05-12 22:17:41 +08:00
function generateAuthHash( $useRemoteAddr ) {
2017-05-30 21:10:41 +08:00
if ( ZM_OPT_USE_AUTH && ZM_AUTH_RELAY == 'hashed' ) {
2016-05-12 22:17:41 +08:00
$time = localtime();
if ( $useRemoteAddr ) {
$authKey = ZM_AUTH_HASH_SECRET.$_SESSION['username'].$_SESSION['passwordHash'].$_SESSION['remoteAddr'].$time[2].$time[3].$time[4].$time[5];
} else {
$authKey = ZM_AUTH_HASH_SECRET.$_SESSION['username'].$_SESSION['passwordHash'].$time[2].$time[3].$time[4].$time[5];
}
2016-05-12 22:17:41 +08:00
$auth = md5( $authKey );
} else {
2017-05-30 21:10:41 +08:00
$auth = '';
2016-05-12 22:17:41 +08:00
}
return( $auth );
}
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'];
}
2016-05-12 22:17:41 +08:00
}
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
}
2016-05-12 22:17:41 +08:00
if ( count($args) ) {
2017-05-30 21:10:41 +08:00
$streamSrc .= '?'.join( $querySep, $args );
2016-05-12 22:17:41 +08:00
}
2016-05-12 22:17:41 +08:00
return( $streamSrc );
}
2016-05-12 22:17:41 +08:00
function getMimeType( $file ) {
if ( function_exists('mime_content_type') ) {
return( mime_content_type( $file ) );
} 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="http://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="http://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="http://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="http://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="http://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="http://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>';
}
2017-05-30 21:10:41 +08:00
function outputImageStream( $id, $src, $width, $height, $title='' ) {
echo getImageStream( $id, $src, $width, $height, $title );
}
2017-05-30 21:10:41 +08:00
function getImageStream( $id, $src, $width, $height, $title='' ) {
if ( canStreamIframe() ) {
2017-05-30 23:15:00 +08:00
return '<iframe id="'.$id.'" src="'.$src.'" alt="'. validHtmlStr($title) .'" '.($width? ' width="'. validInt($width).'"' : '').($height?' height="'.validInt($height).'"' : '' ).'/>';
} else {
2017-05-30 23:15:00 +08:00
return '<img id="'.$id.'" src="'.$src.'" alt="'. validHtmlStr($title) .'" style="'.($width? ' width:'. validInt($width) .'px;': '').($height ? ' height:'. validInt( $height ).'px;':'').'"/>';
}
}
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">
<?php
if ( $monitor['CanMoveMap'] ) {
?>
<input type="hidden" name="control" value="moveMap">
<?php
} elseif ( $monitor['CanMoveRel'] ) {
?>
<input type="hidden" name="control" value="movePseudoMap">
<?php
} elseif ( $monitor['CanMoveCon'] ) {
?>
<input type="hidden" name="control" value="moveConMap">
<?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 ?>">
</form>
<?php
}
2017-05-30 21:10:41 +08:00
function outputHelperStream( $id, $src, $width, $height, $title='' ) {
2017-05-30 23:15:00 +08:00
echo getHelperStream( $id, $src, $width, $height, $title );
}
function getHelperStream( $id, $src, $width, $height, $title='' ) {
return '<applet id="'.$id.'" code="com.charliemouse.cambozola.Viewer"
archive="'. ZM_PATH_CAMBOZOLA .'"
align="middle"
width="'. $width .'"
height="'. $height .'"
title="'. $title .'">
<param name="accessories" value="none"/>
<param name="url" value="'. $src .'"/>
</applet>';
}
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.'" width="'.$width.'" height="'.$height.'"/>';
}
2016-05-12 22:17:41 +08:00
function outputControlStill( $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">
<?php
if ( $monitor['CanMoveMap'] ) {
?>
<input type="hidden" name="control" value="moveMap">
<?php
} elseif ( $monitor['CanMoveRel'] ) {
?>
<input type="hidden" name="control" value="movePseudoMap">
<?php
} elseif ( $monitor['CanMoveCon'] ) {
?>
<input type="hidden" name="control" value="moveConMap">
<?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 ?>">
</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 );
} elseif ( ZM_AUTH_RELAY == 'plain' ) {
$zmuCommand .= ' -U ' .escapeshellarg($_SESSION['username']).' -P '.escapeshellarg($_SESSION['password']);
} elseif ( ZM_AUTH_RELAY == 'none' ) {
2016-05-12 22:17:41 +08:00
$zmuCommand .= " -U ".escapeshellarg($_SESSION['username']);
}
2016-05-12 22:17:41 +08:00
}
2016-05-12 22:17:41 +08:00
$zmuCommand .= $args;
2016-05-12 22:17:41 +08:00
return( $zmuCommand );
}
2016-05-12 22:17:41 +08:00
function visibleMonitor( $mid ) {
global $user;
2016-05-12 22:17:41 +08:00
return( empty($user['MonitorIds']) || in_array( $mid, explode( ',', $user['MonitorIds'] ) ) );
}
2016-05-12 22:17:41 +08:00
function canView( $area, $mid=false ) {
global $user;
2016-05-12 22:17:41 +08:00
return( ($user[$area] == 'View' || $user[$area] == 'Edit') && ( !$mid || visibleMonitor( $mid ) ) );
}
2016-05-12 22:17:41 +08:00
function canEdit( $area, $mid=false ) {
global $user;
2016-05-12 22:17:41 +08:00
return( $user[$area] == 'Edit' && ( !$mid || visibleMonitor( $mid ) ) );
}
2016-05-12 22:17:41 +08:00
function getEventPath( $event ) {
if ( ZM_USE_DEEP_STORAGE )
2017-05-30 21:10:41 +08:00
$eventPath = $event['MonitorId'].'/'.strftime( '%y/%m/%d/%H/%M/%S', strtotime($event['StartTime']) );
2016-05-12 22:17:41 +08:00
else
$eventPath = $event['MonitorId'].'/'.$event['Id'];
return( $eventPath );
}
Feature h264 videostorage (#1882) * Moved writing of configure options from Controller to Model. Fixes #191. * Initial commit for saving events as videos :) * Add zm_video.cpp to autotools * Add zm_video.h to autotools * Search for MP4V2 header file 3 times: mp4v2/mp4v2.h, mp4v2.h, mp4.h * Fix serve memory leak * Few minor code improvements * Added the ability to override preset, tune, profile and few other improvements * Correctly write SPS & PPS from x264 encoder headers * Remove unnessecary SPS & PPS writing code * Imported missing files from master to feature-h264-videostorage * Audio support including fixes for dts/pts, split on keyframe and update to mkv extension to prevent ffmpeg problems writing rtsp audio to mp4 containter (header problem) * Updates to make gcc happy * Add html5 video control to timeline and event to support mkv playback * Add zm_videostore.cpp to CMakeLists.txt * Remove Modern Branch for now * Fix minor bug * Option handled added in master, removing duplicate declaration * Add CaptureandRecord from zm_camera.h * Putting placeholder in for CaptureAndRecord function * Removed duplicate code and brackets * add digest auth file for cmake Conflicts: src/CMakeLists.txt * Add web dir back into Makefile.am Revert "Removed web from SUBDIRS in Makefile.am" This reverts commit d9bbcdf3a94cba4d8955fcd03bd965ed2772f34d. * Add CaptureAndRecord to vlc, still need to make it record * Resolve SegFault on videostore * Swap to mp4 container * mp4 changes * spaces to tabs, hide video stuff if video writer is turned off * Make timeline open event.mp4 instead of mkv * Missed mkv in timeline.js * Fix some issues from the merge conflict * Resolve post merge build issues with braces * Fix whitespace * Update Jpeg and Video options for passthrough options * Whitespace fix zm_camera.h * Fix array mkssing comma * Add support for Jpeg save options for h264 branch snapshot. Might remove altogether if snapshots not needed * Update VideoStoreData memory size comment * Change from config.use_mkv_storage to per monitor option VideoWriter from video branch * Fix bracket issues post merge * Clean up comments and add av_free_packet * Convert from event_directory to event file as per Video branch * Testing videojs for video playback * Fixed a missing bracket post merge and also SQL_values now used for EventID and Monitors * bring recent improvements in ffmpeg capture function into captureandrecord * Remove pict from writeAudioFramePacket as not used * Add translate options for h264 Storage options in Monitor and update en_gb file * Cherry-Pick from iconnor - make it compile on ubuntu 15.04. Which is libav 56.1.0 Conflicts: src/zm_ffmpeg.cpp src/zm_remote_camera_rtsp.cpp Conflicts: distros/ubuntu1204/changelog * Clean up videostore code and remove lots of unused code * proof of concept for dynamic/automatic video rotation using video-js plugin zoomrotate Conflicts: web/skins/classic/views/event.php * removed redundant field in sql query Conflicts: web/skins/classic/views/event.php * local storage of video js plugin * Beautify! Make the code somewhat readable. * added missing videojs.zoomrotate.js file added missing videojs.zoomrotate.js file * Typo added missing " * Added missing brackets * fix to display thumbnails when only storing snapshot.jpg * added control for video playback rate Conflicts: web/skins/classic/views/event.php * dynamically create jpegs from video file for viewing in browser * fix timeline view for SaveJPEGs monitors (without enabled VideoWriter) * only expose monitor info which are being used in client * fix segmentation fault in zma with ubuntu 14.04 and ffmpeg 2.5.8 (gcc 4.8) when libx264 is not installed * better way of detecting showing image or video in timeline and event view instead of Monitor.VideoWriter, Event.DefaultVideo is used, so even if VideoWriter/SaveJPEG option is changed, a valid image or video will always be displayed for historical events in both timeline and event view this also fixes loading videos in timeline view * Fixes problem of crashing zmc when bad packet arrives causing av_interleaved_write_frame() to return non-zero (-22). Prefilters common packet issues. Add metadata title to generated video file * Remove syslog.h * fixed SaveJPEGs are not working which is caused in errors introduced when merging with master * Update README.md * Fix build warnings specific to h264 branch, unused FrameImg, unused ret and int64_t snprintf issues * Fix PRId64 issue in travis, builds locally fine, but I can see a gcc version issue here * Fix PRId64 issue in travis, another try * Try "STDC_FORMAT_MACROS" to see if that helps Travis on gcc 4.6.3 * Revert space removal around PRId64 * video branch ffmpeg 2.9 fixes ffmpeg 2.9 patched removed SSE2 CPU * Add FFMPEGInit back * use webvvt to overlay timestamp (honoring Monitor.LabelFormat) to videos in timeline and event also fixed bug which prevented seeking in timeline video preview * ffmpeg 3.0 API build failure fixes * Update README.md * merge all the commits from the messed up iconnor_video branch * fix whitespace * revert * whitespace fixes * spelling fix * put back some text * add these back * fix spelling mistake * Steal some packet dumping routines from ffmpeg. Convert them to use our logging routines * add a test and error message if the codec is not h264 * these have been removed in master * add a view to check auth and just send the video * add some comments, and dump filename and AVFormatContext on failure to write header * add the toggle for RecordAudio so that the checkbox works to turn off Audio * Must init videoStore in constuctor * more debug and comments, return checking * Fix dropped part of sql query. * fix extra else and some whitespace * Fix missing } from merge that was preventing building. * fix tabs * get rid of use of separator, just use \n * Restore lost fixes for deprecation * Why are these failing * Respect record_audio flag when setting up video file so dont try and initiliase mp4 with unsupported audio * Forgot that I was trying to solve case of stream is true and record_audio is false. * Pass swscale_ctx back in to getCachedContext or it will create new context every frame and leak memory like a mofo. * Add libx264-dev and libmp4v2-dev to build requires to save hassle of ensuring they are installed before build. * Merge my Rotation/Orientation work and fixes for bad h264 streams * need arpa/inet for reverse lookups * pull in the new byte range code for viewing videos * Move our recording flag deeper into closeevent * add braces and only call closeEvent if there is an event * deprecate the z_frame_rate stuff which is deprecated in ffmpeg * remark out some debugging * fix for video on stream 1 * fix audio_stream to audio_st * Ignore bad decodes * fix problems with content-length causing viewing to not work in chrome/android * change logic of sending file contents to handle an off by one and be more readable * Some fixes pointed out by Maxim Romanov. Also simply the loading of events to not join the Monitors table * fix to sql for timeline * added RecordAudio to sql in README * Use sub queries instead of joins to fix errors when using new mysql defaults. * fix sql queries * Dockerfile to build feature-h264-videostorage * Must cast codec * add php-acpu as a dependency * require php5-acpu * fix typo * remove extra / * Add a line for out-of-tree builds to do api/lib/Cake/bootstrap.php * delete merge conflict files * delete merge conflict files
2017-05-16 10:02:48 +08:00
function getEventDefaultVideoPath( $event ) {
Add h264 event view functionality and new feature alarmCues (#2012) * Fix nearEventsQuery Removed dbEscape from getNearEvents previous event because it only returns 0. Now matches next. Changed getEventDefaultVideoPath function to return a web path rather than the absolute path based on ic0ns branch. Also added start times to allow for videoJS replaymode. * Unescape filters Filters need to be unescaped * Add initial values to page load * Add replay modes to videojs * rough in figuring out a frame in between bulk frames * Add alarmCues Add a graphical indication of where alarm frames happened in an event. Similar to what zmNinja shows. * Add remaining buttons to videojs Functionality for all buttons on videojs streams. FF/RW buttons show as active when they are active. * Whitespace and fix Bulkframe calcs * Fix zms events trying to generate with mp4 code ZMS events would attempt to generate frames as though they were an mp4/passthrough type because the full eventpath wasn't passed * ZMS scrub bar Move zms scrub bar to bottom of image feed. Make it simpler and more like videojs style. * Wrap event feeds properly * Fix dvrControls on watch view * Add scaleToFit Add a scaleToFit option to event view * Add navigation for videoJS streams Disables nav buttons at beginning and end of events. Handles switching from zms to videojs. If zms crashes changes next event function to reload page instead of ajax. * Add scaleToFit to watch and frame view Adds scaleToFit to watch view. Since frame view uses the watch cookie this required changes to frame view * Add transition to zoom * Change stills view to match stream Move stills slider bar to match scrub bar on streams. Allow it to resize, make it larger. Add alarmcues. * Add Stills for every event Add stills for every event. Match size to stream size * Progressbox transitions
2017-12-05 10:26:59 +08:00
$Event = new Event( $event );
return $Event->getStreamSrc( array( "mode"=>"mpeg", "format"=>"h264" ) );
Feature h264 videostorage (#1882) * Moved writing of configure options from Controller to Model. Fixes #191. * Initial commit for saving events as videos :) * Add zm_video.cpp to autotools * Add zm_video.h to autotools * Search for MP4V2 header file 3 times: mp4v2/mp4v2.h, mp4v2.h, mp4.h * Fix serve memory leak * Few minor code improvements * Added the ability to override preset, tune, profile and few other improvements * Correctly write SPS & PPS from x264 encoder headers * Remove unnessecary SPS & PPS writing code * Imported missing files from master to feature-h264-videostorage * Audio support including fixes for dts/pts, split on keyframe and update to mkv extension to prevent ffmpeg problems writing rtsp audio to mp4 containter (header problem) * Updates to make gcc happy * Add html5 video control to timeline and event to support mkv playback * Add zm_videostore.cpp to CMakeLists.txt * Remove Modern Branch for now * Fix minor bug * Option handled added in master, removing duplicate declaration * Add CaptureandRecord from zm_camera.h * Putting placeholder in for CaptureAndRecord function * Removed duplicate code and brackets * add digest auth file for cmake Conflicts: src/CMakeLists.txt * Add web dir back into Makefile.am Revert "Removed web from SUBDIRS in Makefile.am" This reverts commit d9bbcdf3a94cba4d8955fcd03bd965ed2772f34d. * Add CaptureAndRecord to vlc, still need to make it record * Resolve SegFault on videostore * Swap to mp4 container * mp4 changes * spaces to tabs, hide video stuff if video writer is turned off * Make timeline open event.mp4 instead of mkv * Missed mkv in timeline.js * Fix some issues from the merge conflict * Resolve post merge build issues with braces * Fix whitespace * Update Jpeg and Video options for passthrough options * Whitespace fix zm_camera.h * Fix array mkssing comma * Add support for Jpeg save options for h264 branch snapshot. Might remove altogether if snapshots not needed * Update VideoStoreData memory size comment * Change from config.use_mkv_storage to per monitor option VideoWriter from video branch * Fix bracket issues post merge * Clean up comments and add av_free_packet * Convert from event_directory to event file as per Video branch * Testing videojs for video playback * Fixed a missing bracket post merge and also SQL_values now used for EventID and Monitors * bring recent improvements in ffmpeg capture function into captureandrecord * Remove pict from writeAudioFramePacket as not used * Add translate options for h264 Storage options in Monitor and update en_gb file * Cherry-Pick from iconnor - make it compile on ubuntu 15.04. Which is libav 56.1.0 Conflicts: src/zm_ffmpeg.cpp src/zm_remote_camera_rtsp.cpp Conflicts: distros/ubuntu1204/changelog * Clean up videostore code and remove lots of unused code * proof of concept for dynamic/automatic video rotation using video-js plugin zoomrotate Conflicts: web/skins/classic/views/event.php * removed redundant field in sql query Conflicts: web/skins/classic/views/event.php * local storage of video js plugin * Beautify! Make the code somewhat readable. * added missing videojs.zoomrotate.js file added missing videojs.zoomrotate.js file * Typo added missing " * Added missing brackets * fix to display thumbnails when only storing snapshot.jpg * added control for video playback rate Conflicts: web/skins/classic/views/event.php * dynamically create jpegs from video file for viewing in browser * fix timeline view for SaveJPEGs monitors (without enabled VideoWriter) * only expose monitor info which are being used in client * fix segmentation fault in zma with ubuntu 14.04 and ffmpeg 2.5.8 (gcc 4.8) when libx264 is not installed * better way of detecting showing image or video in timeline and event view instead of Monitor.VideoWriter, Event.DefaultVideo is used, so even if VideoWriter/SaveJPEG option is changed, a valid image or video will always be displayed for historical events in both timeline and event view this also fixes loading videos in timeline view * Fixes problem of crashing zmc when bad packet arrives causing av_interleaved_write_frame() to return non-zero (-22). Prefilters common packet issues. Add metadata title to generated video file * Remove syslog.h * fixed SaveJPEGs are not working which is caused in errors introduced when merging with master * Update README.md * Fix build warnings specific to h264 branch, unused FrameImg, unused ret and int64_t snprintf issues * Fix PRId64 issue in travis, builds locally fine, but I can see a gcc version issue here * Fix PRId64 issue in travis, another try * Try "STDC_FORMAT_MACROS" to see if that helps Travis on gcc 4.6.3 * Revert space removal around PRId64 * video branch ffmpeg 2.9 fixes ffmpeg 2.9 patched removed SSE2 CPU * Add FFMPEGInit back * use webvvt to overlay timestamp (honoring Monitor.LabelFormat) to videos in timeline and event also fixed bug which prevented seeking in timeline video preview * ffmpeg 3.0 API build failure fixes * Update README.md * merge all the commits from the messed up iconnor_video branch * fix whitespace * revert * whitespace fixes * spelling fix * put back some text * add these back * fix spelling mistake * Steal some packet dumping routines from ffmpeg. Convert them to use our logging routines * add a test and error message if the codec is not h264 * these have been removed in master * add a view to check auth and just send the video * add some comments, and dump filename and AVFormatContext on failure to write header * add the toggle for RecordAudio so that the checkbox works to turn off Audio * Must init videoStore in constuctor * more debug and comments, return checking * Fix dropped part of sql query. * fix extra else and some whitespace * Fix missing } from merge that was preventing building. * fix tabs * get rid of use of separator, just use \n * Restore lost fixes for deprecation * Why are these failing * Respect record_audio flag when setting up video file so dont try and initiliase mp4 with unsupported audio * Forgot that I was trying to solve case of stream is true and record_audio is false. * Pass swscale_ctx back in to getCachedContext or it will create new context every frame and leak memory like a mofo. * Add libx264-dev and libmp4v2-dev to build requires to save hassle of ensuring they are installed before build. * Merge my Rotation/Orientation work and fixes for bad h264 streams * need arpa/inet for reverse lookups * pull in the new byte range code for viewing videos * Move our recording flag deeper into closeevent * add braces and only call closeEvent if there is an event * deprecate the z_frame_rate stuff which is deprecated in ffmpeg * remark out some debugging * fix for video on stream 1 * fix audio_stream to audio_st * Ignore bad decodes * fix problems with content-length causing viewing to not work in chrome/android * change logic of sending file contents to handle an off by one and be more readable * Some fixes pointed out by Maxim Romanov. Also simply the loading of events to not join the Monitors table * fix to sql for timeline * added RecordAudio to sql in README * Use sub queries instead of joins to fix errors when using new mysql defaults. * fix sql queries * Dockerfile to build feature-h264-videostorage * Must cast codec * add php-acpu as a dependency * require php5-acpu * fix typo * remove extra / * Add a line for out-of-tree builds to do api/lib/Cake/bootstrap.php * delete merge conflict files * delete merge conflict files
2017-05-16 10:02:48 +08:00
}
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 ) );
2016-05-12 22:17:41 +08:00
} else {
unlink( $path );
}
}
2016-05-12 22:17:41 +08:00
function deleteEvent( $event, $mid=false ) {
if ( empty($event) ) {
2017-05-30 21:10:41 +08:00
Error( 'Empty event passed to deleteEvent.');
2016-05-12 22:17:41 +08:00
return;
}
2016-05-12 22:17:41 +08:00
if ( gettype($event) != 'array' ) {
# $event could be an eid, so turn it into an event hash
$event = dbFetchOne( 'SELECT Id, MonitorId, StartTime FROM Events WHERE Id=?', NULL, array( $event ) );
}
2016-05-12 22:17:41 +08:00
global $user;
2016-05-12 22:17:41 +08:00
if ( !$mid )
$mid = $event['MonitorId'];
2016-05-12 22:17:41 +08:00
if ( $user['Events'] == 'Edit' ) {
dbQuery( 'DELETE FROM Events WHERE Id = ?', array($event['Id']) );
if ( !ZM_OPT_FAST_DELETE ) {
dbQuery( 'DELETE FROM Stats WHERE EventId = ?', array($event['Id']) );
dbQuery( 'DELETE FROM Frames WHERE EventId = ?', array($event['Id']) );
if ( ZM_USE_DEEP_STORAGE ) {
# Assumption: All events haev a start time
$start_date = date_parse( $event['StartTime'] );
$start_date['year'] = $start_date['year'] % 100;
2016-12-29 17:31:05 +08:00
# So this is because ZM creates a link under the day pointing to the time that the event happened.
2016-05-12 22:17:41 +08:00
$eventlink_path = sprintf('%s/%d/%02d/%02d/%02d/.%d', ZM_DIR_EVENTS, $mid, $start_date['year'], $start_date['month'], $start_date['day'], $event['Id'] );
if ( $id_files = glob( $eventlink_path ) ) {
# I know we are using arrays here, but really there can only ever be 1 in the array
$eventPath = preg_replace( '/\.'.$event['Id'].'$/', readlink($id_files[0]), $id_files[0] );
deletePath( $eventPath );
deletePath( $id_files[0] );
$pathParts = explode( '/', $eventPath );
for ( $i = count($pathParts)-1; $i >= 2; $i-- ) {
$deletePath = join( '/', array_slice( $pathParts, 0, $i ) );
if ( !glob( $deletePath."/*" ) ) {
deletePath( $deletePath );
}
}
} else {
Warning( "Found no event files under $eventlink_path" );
} # end if found files
} else {
$eventPath = implode( '/', array( ZM_DIR_EVENTS, $mid, $event['Id'] ) );
deletePath( $eventPath );
} # USE_DEEP_STORAGE OR NOT
} # ! ZM_OPT_FAST_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;
if ( $condition ) {
$string .= '</a>';
}
return( $string );
}
2017-05-30 21:10:41 +08:00
function makePopupLink( $url, $winName, $winSize, $label, $condition=1, $options='' ) {
$string = '';
2016-05-12 22:17:41 +08:00
if ( $condition ) {
if ( is_array( $winSize ) )
2016-05-12 22:17:41 +08:00
$popupParms = "'".$url."', '".$winName."', '".$winSize[0]."', ".$winSize[1].", ".$winSize[2];
else
2016-05-12 22:17:41 +08:00
$popupParms = "'".$url."', '".$winName."', '".$winSize."'";
$string .= '<a href="'.$url.'" onclick="createPopup( '.$popupParms.' ); return( false );"'.($options?(' '.$options):'').'>';
2017-05-30 23:15:00 +08:00
} else {
$string .= '<a>';
2016-05-12 22:17:41 +08:00
}
$string .= $label;
2017-05-30 23:15:00 +08:00
$string .= '</a>';
2016-05-12 22:17:41 +08:00
return( $string );
}
2017-05-30 21:10:41 +08:00
function makePopupButton( $url, $winName, $winSize, $buttonValue, $condition=1, $options='' ) {
2016-05-12 22:17:41 +08:00
if ( is_array( $winSize ) )
$popupParms = "'".$url."', '".$winName."', '".$winSize[0]."', ".$winSize[1].", ".$winSize[2];
else
$popupParms = "'".$url."', '".$winName."', '".$winSize."'";
$string = '<input type="button" value="'.$buttonValue.'" onclick="createPopup( '.$popupParms.' ); return( false );"'.($condition?'':' disabled="disabled"').($options?(' '.$options):'').'/>';
return( $string );
}
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.'"';
}
}
$html = "<select name=\"$name\" id=\"$name\"$behaviourText>";
foreach ( $contents as $value=>$text ) {
$selected = is_array( $values ) ? in_array( $value, $values ) : $value==$values;
$html .= "<option value=\"$value\"".($selected?" selected=\"selected\"":'').">$text</option>";
}
2017-05-30 21:10:41 +08:00
$html .= '</select>';
return $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 ) {
2016-05-12 22:17:41 +08:00
$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 ) ) {
2016-05-12 22:17:41 +08:00
Fatal( "Can't parse selector '$name'" );
}
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.'"';
}
} 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 ) {
?>
<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();
2016-05-12 22:17:41 +08:00
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 && !$columns[$key] )
continue;
2016-05-12 22:17:41 +08:00
if ( !isset($types[$key]) )
$types[$key] = false;
switch( $types[$key] ) {
case 'set' :
{
2016-05-12 22:17:41 +08:00
if ( is_array( $newValues[$key] ) ) {
if ( join(',',$newValues[$key]) != $values[$key] ) {
$changes[$key] = "$key = ".dbEscape(join(',',$newValues[$key]));
}
2016-05-12 22:17:41 +08:00
} elseif ( $values[$key] ) {
$changes[$key] = "$key = ''";
}
break;
}
case 'image' :
{
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];
2016-05-12 22:17:41 +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-05-12 22:17:41 +08:00
ob_start();
readfile( $newValues[$key]['tmp_name'] );
$changes[$key] = $key." = ".dbEscape( ob_get_contents() );
ob_end_clean();
} else {
$changes[$key] = "$key = ".dbEscape($value);
}
break;
}
case 'document' :
{
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'];
2016-05-12 22:17:41 +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-05-12 22:17:41 +08:00
ob_end_clean();
} else {
2017-05-30 21:10:41 +08:00
$changes[$key] = $key . ' = '.dbEscape($value);
2016-05-12 22:17:41 +08:00
}
break;
}
2016-05-12 22:17:41 +08:00
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-05-12 22:17:41 +08:00
ob_start();
readfile( $newValues[$key]['tmp_name'] );
$changes[$key] = $key." = '".dbEscape( ob_get_contents() )."'";
ob_end_clean();
break;
}
2016-05-12 22:17:41 +08:00
case 'raw' :
{
2016-05-12 22:17:41 +08:00
if ( $values[$key] != $value ) {
2017-05-30 21:10:41 +08:00
$changes[$key] = $key . ' = '.dbEscape($value);
2016-05-12 22:17:41 +08:00
}
break;
}
2016-05-12 22:17:41 +08:00
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));
}
2016-05-12 22:17:41 +08:00
}
break;
}
}
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";
}
} else if ( $types[$key] == 'set' ) {
$changes[$key] = "$key = ''";
}
2016-05-12 22:17:41 +08:00
}
}
2016-05-12 22:17:41 +08:00
}
return( $changes );
}
function getBrowser( &$browser, &$version ) {
if ( isset($_SESSION['browser']) ) {
$browser = $_SESSION['browser'];
$version = $_SESSION['version'];
} else {
if (( preg_match( '/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT'], $logVersion)) || (preg_match( '/.*Trident.*rv:(.*?)(;|\))/', $_SERVER['HTTP_USER_AGENT'], $logVersion))) {
$version = $logVersion[1];
$browser = 'ie';
} elseif ( preg_match( '/Chrome\/([0-9.]+)/', $_SERVER['HTTP_USER_AGENT'], $logVersion) ) {
$version = $logVersion[1];
// Check for old version of Chrome with bug 5876
if ( $version < 7 ) {
$browser = 'oldchrome';
} else {
$browser = 'chrome';
}
} elseif ( preg_match( '/Safari\/([0-9.]+)/', $_SERVER['HTTP_USER_AGENT'], $logVersion) ) {
$version = $logVersion[1];
$browser = 'safari';
} elseif ( preg_match( '/Opera[ \/]([0-9].[0-9]{1,2})/', $_SERVER['HTTP_USER_AGENT'], $logVersion) ) {
$version = $logVersion[1];
$browser = 'opera';
} elseif ( preg_match( '/Konqueror\/([0-9.]+)/', $_SERVER['HTTP_USER_AGENT'], $logVersion) ) {
$version = $logVersion[1];
$browser = 'konqueror';
} elseif ( preg_match( '/Mozilla\/([0-9].[0-9]{1,2})/', $_SERVER['HTTP_USER_AGENT'], $logVersion) ) {
$version = $logVersion[1];
$browser = 'mozilla';
} else {
$version = 0;
$browser = 'unknown';
}
2016-05-12 22:17:41 +08:00
$_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 );
2016-05-12 22:17:41 +08:00
}
2016-05-12 22:17:41 +08:00
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';
2016-05-12 22:17:41 +08:00
exec( $string );
}
2016-05-12 22:17:41 +08:00
function daemonControl( $command, $daemon=false, $args=false ) {
$string = ZM_PATH_BIN."/zmdc.pl $command";
if ( $daemon ) {
$string .= ' ' . $daemon;
2016-05-12 22:17:41 +08:00
if ( $args ) {
$string .= ' ' . $args;
}
2016-05-12 22:17:41 +08:00
}
$string = escapeshellcmd( $string );
$string .= ' 2>/dev/null >&- <&- >/dev/null';
2016-05-12 22:17:41 +08:00
exec( $string );
}
2016-05-12 22:17:41 +08:00
function zmcControl( $monitor, $mode=false ) {
2016-04-05 04:34:21 +08:00
if ( (!defined('ZM_SERVER_ID')) or ( ZM_SERVER_ID==$monitor['ServerId'] ) ) {
2016-05-12 22:17:41 +08:00
$row = NULL;
2017-05-30 21:10:41 +08:00
if ( $monitor['Type'] == 'Local' ) {
2017-05-30 23:15:00 +08:00
$row = dbFetchOne( "SELECT count(if(Function!='None',1,NULL)) AS ActiveCount FROM Monitors WHERE Device = ?", NULL, array($monitor['Device']) );
2017-05-30 21:10:41 +08:00
$zmcArgs = '-d '.$monitor['Device'];
2016-05-12 22:17:41 +08:00
} else {
2017-05-30 23:15:00 +08:00
$row = dbFetchOne( "SELECT count(if(Function!='None',1,NULL)) AS ActiveCount FROM Monitors WHERE Id = ?", NULL, array($monitor['Id']) );
2017-05-30 21:10:41 +08:00
$zmcArgs = '-m '.$monitor['Id'];
}
$activeCount = $row['ActiveCount'];
2017-05-30 23:15:00 +08:00
if ( (!$activeCount) || ($mode == 'stop') ) {
2017-05-30 21:10:41 +08:00
daemonControl( 'stop', 'zmc', $zmcArgs );
2016-05-12 22:17:41 +08:00
} else {
2017-05-30 21:10:41 +08:00
if ( $mode == 'restart' ) {
daemonControl( 'stop', 'zmc', $zmcArgs );
2016-05-12 22:17:41 +08:00
}
2017-05-30 21:10:41 +08:00
daemonControl( 'start', 'zmc', $zmcArgs );
}
2017-05-30 23:15:00 +08:00
} else {
$Server = new Server( $monitor['ServerId'] );
#$url = $Server->Url() . '/zm/api/monitors.json?auth='.generateAuthHash( $_SESSION['remoteAddr'] );
$url = $Server->Url() . '/zm/api/monitors.json?user='.$_SESSION['username'].'&pass='.$_SESSION['passwordHash'];
$data = array('Monitor[Function]' => $monitor['Function'] );
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
}
}
2016-05-12 22:17:41 +08:00
function zmaControl( $monitor, $mode=false ) {
if ( !is_array( $monitor ) ) {
2017-05-30 21:10:41 +08:00
$monitor = dbFetchOne( 'select C.*, M.* from Monitors as M left join Controls as C on (M.ControlId = C.Id ) where M.Id=?', NULL, array($monitor) );
}
2016-04-05 04:34:21 +08:00
if ( (!defined('ZM_SERVER_ID')) or ( ZM_SERVER_ID==$monitor['ServerId'] ) ) {
2017-05-30 21:10:41 +08:00
if ( !$monitor || $monitor['Function'] == 'None' || $monitor['Function'] == 'Monitor' || $mode == 'stop' ) {
2016-05-12 22:17:41 +08:00
if ( ZM_OPT_CONTROL ) {
2017-05-30 21:10:41 +08:00
daemonControl( 'stop', 'zmtrack.pl', '-m '.$monitor['Id'] );
2016-05-12 22:17:41 +08:00
}
2017-05-30 21:10:41 +08:00
daemonControl( 'stop', 'zma', '-m '.$monitor['Id'] );
2016-05-12 22:17:41 +08:00
} else {
2017-05-30 21:10:41 +08:00
if ( $mode == 'restart' ) {
2016-05-12 22:17:41 +08:00
if ( ZM_OPT_CONTROL ) {
2017-05-30 21:10:41 +08:00
daemonControl( 'stop', 'zmtrack.pl', '-m '.$monitor['Id'] );
2016-05-12 22:17:41 +08:00
}
2017-05-30 21:10:41 +08:00
daemonControl( 'stop', 'zma', '-m '.$monitor['Id'] );
2016-05-12 22:17:41 +08:00
}
2017-05-30 21:10:41 +08:00
daemonControl( 'start', 'zma', '-m '.$monitor['Id'] );
2016-05-12 22:17:41 +08:00
if ( ZM_OPT_CONTROL && $monitor['Controllable'] && $monitor['TrackMotion'] && ( $monitor['Function'] == 'Modect' || $monitor['Function'] == 'Mocord' ) ) {
2017-05-30 21:10:41 +08:00
daemonControl( 'start', 'zmtrack.pl', '-m '.$monitor['Id'] );
2016-05-12 22:17:41 +08:00
}
2017-05-30 21:10:41 +08:00
if ( $mode == 'reload' ) {
daemonControl( 'reload', 'zma', '-m '.$monitor['Id'] );
2016-05-12 22:17:41 +08:00
}
}
} // end if we are on the recording server
}
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 );
} else {
2017-05-30 21:10:41 +08:00
$daemon_status = '';
}
2016-05-12 22:17:41 +08:00
}
}
2016-05-12 22:17:41 +08:00
function daemonStatus( $daemon, $args=false ) {
global $daemon_status;
2016-05-12 22:17:41 +08:00
initDaemonStatus();
2017-05-30 23:15:00 +08:00
$string = $daemon;
2016-05-12 22:17:41 +08:00
if ( $args )
2017-05-30 23:15:00 +08:00
$string .= ' ' . $args;
2016-05-12 22:17:41 +08:00
return( strpos( $daemon_status, "'$string' running" ) !== false );
}
2016-05-12 22:17:41 +08:00
function zmcStatus( $monitor ) {
if ( $monitor['Type'] == 'Local' ) {
2017-05-30 21:10:41 +08:00
$zmcArgs = '-d '.$monitor['Device'];
2016-05-12 22:17:41 +08:00
} else {
2017-05-30 21:10:41 +08:00
$zmcArgs = '-m '.$monitor['Id'];
2016-05-12 22:17:41 +08:00
}
2017-05-30 21:10:41 +08:00
return( daemonStatus( 'zmc', $zmcArgs ) );
}
2016-05-12 22:17:41 +08:00
function zmaStatus( $monitor ) {
if ( is_array( $monitor ) ) {
$monitor = $monitor['Id'];
}
2017-05-30 21:10:41 +08:00
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;
2016-05-12 22:17:41 +08:00
if ( $args )
$string .= ' '. $args;
2016-05-12 22:17:41 +08:00
}
$string = escapeshellcmd( $string );
2016-05-12 22:17:41 +08:00
$result = exec( $string );
return( preg_match( '/running/', $result ) );
}
2016-05-12 22:17:41 +08:00
function zmcCheck( $monitor ) {
if ( $monitor['Type'] == 'Local' ) {
2017-05-30 21:10:41 +08:00
$zmcArgs = '-d '.$monitor['Device'];
2016-05-12 22:17:41 +08:00
} else {
2017-05-30 21:10:41 +08:00
$zmcArgs = '-m '.$monitor['Id'];
2016-05-12 22:17:41 +08:00
}
2017-05-30 21:10:41 +08:00
return( daemonCheck( 'zmc', $zmcArgs ) );
}
2016-05-12 22:17:41 +08:00
function zmaCheck( $monitor ) {
if ( is_array( $monitor ) ) {
$monitor = $monitor['Id'];
}
2017-05-30 21:10:41 +08:00
return( daemonCheck( 'zma', "-m $monitor" ) );
}
2016-05-12 22:17:41 +08:00
function getImageSrc( $event, $frame, $scale=SCALE_BASE, $captureOnly=false, $overwrite=false ) {
Add h264 event view functionality and new feature alarmCues (#2012) * Fix nearEventsQuery Removed dbEscape from getNearEvents previous event because it only returns 0. Now matches next. Changed getEventDefaultVideoPath function to return a web path rather than the absolute path based on ic0ns branch. Also added start times to allow for videoJS replaymode. * Unescape filters Filters need to be unescaped * Add initial values to page load * Add replay modes to videojs * rough in figuring out a frame in between bulk frames * Add alarmCues Add a graphical indication of where alarm frames happened in an event. Similar to what zmNinja shows. * Add remaining buttons to videojs Functionality for all buttons on videojs streams. FF/RW buttons show as active when they are active. * Whitespace and fix Bulkframe calcs * Fix zms events trying to generate with mp4 code ZMS events would attempt to generate frames as though they were an mp4/passthrough type because the full eventpath wasn't passed * ZMS scrub bar Move zms scrub bar to bottom of image feed. Make it simpler and more like videojs style. * Wrap event feeds properly * Fix dvrControls on watch view * Add scaleToFit Add a scaleToFit option to event view * Add navigation for videoJS streams Disables nav buttons at beginning and end of events. Handles switching from zms to videojs. If zms crashes changes next event function to reload page instead of ajax. * Add scaleToFit to watch and frame view Adds scaleToFit to watch view. Since frame view uses the watch cookie this required changes to frame view * Add transition to zoom * Change stills view to match stream Move stills slider bar to match scrub bar on streams. Allow it to resize, make it larger. Add alarmcues. * Add Stills for every event Add stills for every event. Match size to stream size * Progressbox transitions
2017-12-05 10:26:59 +08:00
$eventPath = ZM_DIR_EVENTS . '/' . getEventPath( $event );
2016-05-12 22:17:41 +08:00
if ( !is_array($frame) )
$frame = array( 'FrameId'=>$frame, 'Type'=>'' );
Feature h264 videostorage (#1882) * Moved writing of configure options from Controller to Model. Fixes #191. * Initial commit for saving events as videos :) * Add zm_video.cpp to autotools * Add zm_video.h to autotools * Search for MP4V2 header file 3 times: mp4v2/mp4v2.h, mp4v2.h, mp4.h * Fix serve memory leak * Few minor code improvements * Added the ability to override preset, tune, profile and few other improvements * Correctly write SPS & PPS from x264 encoder headers * Remove unnessecary SPS & PPS writing code * Imported missing files from master to feature-h264-videostorage * Audio support including fixes for dts/pts, split on keyframe and update to mkv extension to prevent ffmpeg problems writing rtsp audio to mp4 containter (header problem) * Updates to make gcc happy * Add html5 video control to timeline and event to support mkv playback * Add zm_videostore.cpp to CMakeLists.txt * Remove Modern Branch for now * Fix minor bug * Option handled added in master, removing duplicate declaration * Add CaptureandRecord from zm_camera.h * Putting placeholder in for CaptureAndRecord function * Removed duplicate code and brackets * add digest auth file for cmake Conflicts: src/CMakeLists.txt * Add web dir back into Makefile.am Revert "Removed web from SUBDIRS in Makefile.am" This reverts commit d9bbcdf3a94cba4d8955fcd03bd965ed2772f34d. * Add CaptureAndRecord to vlc, still need to make it record * Resolve SegFault on videostore * Swap to mp4 container * mp4 changes * spaces to tabs, hide video stuff if video writer is turned off * Make timeline open event.mp4 instead of mkv * Missed mkv in timeline.js * Fix some issues from the merge conflict * Resolve post merge build issues with braces * Fix whitespace * Update Jpeg and Video options for passthrough options * Whitespace fix zm_camera.h * Fix array mkssing comma * Add support for Jpeg save options for h264 branch snapshot. Might remove altogether if snapshots not needed * Update VideoStoreData memory size comment * Change from config.use_mkv_storage to per monitor option VideoWriter from video branch * Fix bracket issues post merge * Clean up comments and add av_free_packet * Convert from event_directory to event file as per Video branch * Testing videojs for video playback * Fixed a missing bracket post merge and also SQL_values now used for EventID and Monitors * bring recent improvements in ffmpeg capture function into captureandrecord * Remove pict from writeAudioFramePacket as not used * Add translate options for h264 Storage options in Monitor and update en_gb file * Cherry-Pick from iconnor - make it compile on ubuntu 15.04. Which is libav 56.1.0 Conflicts: src/zm_ffmpeg.cpp src/zm_remote_camera_rtsp.cpp Conflicts: distros/ubuntu1204/changelog * Clean up videostore code and remove lots of unused code * proof of concept for dynamic/automatic video rotation using video-js plugin zoomrotate Conflicts: web/skins/classic/views/event.php * removed redundant field in sql query Conflicts: web/skins/classic/views/event.php * local storage of video js plugin * Beautify! Make the code somewhat readable. * added missing videojs.zoomrotate.js file added missing videojs.zoomrotate.js file * Typo added missing " * Added missing brackets * fix to display thumbnails when only storing snapshot.jpg * added control for video playback rate Conflicts: web/skins/classic/views/event.php * dynamically create jpegs from video file for viewing in browser * fix timeline view for SaveJPEGs monitors (without enabled VideoWriter) * only expose monitor info which are being used in client * fix segmentation fault in zma with ubuntu 14.04 and ffmpeg 2.5.8 (gcc 4.8) when libx264 is not installed * better way of detecting showing image or video in timeline and event view instead of Monitor.VideoWriter, Event.DefaultVideo is used, so even if VideoWriter/SaveJPEG option is changed, a valid image or video will always be displayed for historical events in both timeline and event view this also fixes loading videos in timeline view * Fixes problem of crashing zmc when bad packet arrives causing av_interleaved_write_frame() to return non-zero (-22). Prefilters common packet issues. Add metadata title to generated video file * Remove syslog.h * fixed SaveJPEGs are not working which is caused in errors introduced when merging with master * Update README.md * Fix build warnings specific to h264 branch, unused FrameImg, unused ret and int64_t snprintf issues * Fix PRId64 issue in travis, builds locally fine, but I can see a gcc version issue here * Fix PRId64 issue in travis, another try * Try "STDC_FORMAT_MACROS" to see if that helps Travis on gcc 4.6.3 * Revert space removal around PRId64 * video branch ffmpeg 2.9 fixes ffmpeg 2.9 patched removed SSE2 CPU * Add FFMPEGInit back * use webvvt to overlay timestamp (honoring Monitor.LabelFormat) to videos in timeline and event also fixed bug which prevented seeking in timeline video preview * ffmpeg 3.0 API build failure fixes * Update README.md * merge all the commits from the messed up iconnor_video branch * fix whitespace * revert * whitespace fixes * spelling fix * put back some text * add these back * fix spelling mistake * Steal some packet dumping routines from ffmpeg. Convert them to use our logging routines * add a test and error message if the codec is not h264 * these have been removed in master * add a view to check auth and just send the video * add some comments, and dump filename and AVFormatContext on failure to write header * add the toggle for RecordAudio so that the checkbox works to turn off Audio * Must init videoStore in constuctor * more debug and comments, return checking * Fix dropped part of sql query. * fix extra else and some whitespace * Fix missing } from merge that was preventing building. * fix tabs * get rid of use of separator, just use \n * Restore lost fixes for deprecation * Why are these failing * Respect record_audio flag when setting up video file so dont try and initiliase mp4 with unsupported audio * Forgot that I was trying to solve case of stream is true and record_audio is false. * Pass swscale_ctx back in to getCachedContext or it will create new context every frame and leak memory like a mofo. * Add libx264-dev and libmp4v2-dev to build requires to save hassle of ensuring they are installed before build. * Merge my Rotation/Orientation work and fixes for bad h264 streams * need arpa/inet for reverse lookups * pull in the new byte range code for viewing videos * Move our recording flag deeper into closeevent * add braces and only call closeEvent if there is an event * deprecate the z_frame_rate stuff which is deprecated in ffmpeg * remark out some debugging * fix for video on stream 1 * fix audio_stream to audio_st * Ignore bad decodes * fix problems with content-length causing viewing to not work in chrome/android * change logic of sending file contents to handle an off by one and be more readable * Some fixes pointed out by Maxim Romanov. Also simply the loading of events to not join the Monitors table * fix to sql for timeline * added RecordAudio to sql in README * Use sub queries instead of joins to fix errors when using new mysql defaults. * fix sql queries * Dockerfile to build feature-h264-videostorage * Must cast codec * add php-acpu as a dependency * require php5-acpu * fix typo * remove extra / * Add a line for out-of-tree builds to do api/lib/Cake/bootstrap.php * delete merge conflict files * delete merge conflict files
2017-05-16 10:02:48 +08:00
if ( file_exists( $eventPath.'/snapshot.jpg' ) ) {
2017-05-30 21:10:41 +08:00
$captImage = 'snapshot.jpg';
Feature h264 videostorage (#1882) * Moved writing of configure options from Controller to Model. Fixes #191. * Initial commit for saving events as videos :) * Add zm_video.cpp to autotools * Add zm_video.h to autotools * Search for MP4V2 header file 3 times: mp4v2/mp4v2.h, mp4v2.h, mp4.h * Fix serve memory leak * Few minor code improvements * Added the ability to override preset, tune, profile and few other improvements * Correctly write SPS & PPS from x264 encoder headers * Remove unnessecary SPS & PPS writing code * Imported missing files from master to feature-h264-videostorage * Audio support including fixes for dts/pts, split on keyframe and update to mkv extension to prevent ffmpeg problems writing rtsp audio to mp4 containter (header problem) * Updates to make gcc happy * Add html5 video control to timeline and event to support mkv playback * Add zm_videostore.cpp to CMakeLists.txt * Remove Modern Branch for now * Fix minor bug * Option handled added in master, removing duplicate declaration * Add CaptureandRecord from zm_camera.h * Putting placeholder in for CaptureAndRecord function * Removed duplicate code and brackets * add digest auth file for cmake Conflicts: src/CMakeLists.txt * Add web dir back into Makefile.am Revert "Removed web from SUBDIRS in Makefile.am" This reverts commit d9bbcdf3a94cba4d8955fcd03bd965ed2772f34d. * Add CaptureAndRecord to vlc, still need to make it record * Resolve SegFault on videostore * Swap to mp4 container * mp4 changes * spaces to tabs, hide video stuff if video writer is turned off * Make timeline open event.mp4 instead of mkv * Missed mkv in timeline.js * Fix some issues from the merge conflict * Resolve post merge build issues with braces * Fix whitespace * Update Jpeg and Video options for passthrough options * Whitespace fix zm_camera.h * Fix array mkssing comma * Add support for Jpeg save options for h264 branch snapshot. Might remove altogether if snapshots not needed * Update VideoStoreData memory size comment * Change from config.use_mkv_storage to per monitor option VideoWriter from video branch * Fix bracket issues post merge * Clean up comments and add av_free_packet * Convert from event_directory to event file as per Video branch * Testing videojs for video playback * Fixed a missing bracket post merge and also SQL_values now used for EventID and Monitors * bring recent improvements in ffmpeg capture function into captureandrecord * Remove pict from writeAudioFramePacket as not used * Add translate options for h264 Storage options in Monitor and update en_gb file * Cherry-Pick from iconnor - make it compile on ubuntu 15.04. Which is libav 56.1.0 Conflicts: src/zm_ffmpeg.cpp src/zm_remote_camera_rtsp.cpp Conflicts: distros/ubuntu1204/changelog * Clean up videostore code and remove lots of unused code * proof of concept for dynamic/automatic video rotation using video-js plugin zoomrotate Conflicts: web/skins/classic/views/event.php * removed redundant field in sql query Conflicts: web/skins/classic/views/event.php * local storage of video js plugin * Beautify! Make the code somewhat readable. * added missing videojs.zoomrotate.js file added missing videojs.zoomrotate.js file * Typo added missing " * Added missing brackets * fix to display thumbnails when only storing snapshot.jpg * added control for video playback rate Conflicts: web/skins/classic/views/event.php * dynamically create jpegs from video file for viewing in browser * fix timeline view for SaveJPEGs monitors (without enabled VideoWriter) * only expose monitor info which are being used in client * fix segmentation fault in zma with ubuntu 14.04 and ffmpeg 2.5.8 (gcc 4.8) when libx264 is not installed * better way of detecting showing image or video in timeline and event view instead of Monitor.VideoWriter, Event.DefaultVideo is used, so even if VideoWriter/SaveJPEG option is changed, a valid image or video will always be displayed for historical events in both timeline and event view this also fixes loading videos in timeline view * Fixes problem of crashing zmc when bad packet arrives causing av_interleaved_write_frame() to return non-zero (-22). Prefilters common packet issues. Add metadata title to generated video file * Remove syslog.h * fixed SaveJPEGs are not working which is caused in errors introduced when merging with master * Update README.md * Fix build warnings specific to h264 branch, unused FrameImg, unused ret and int64_t snprintf issues * Fix PRId64 issue in travis, builds locally fine, but I can see a gcc version issue here * Fix PRId64 issue in travis, another try * Try "STDC_FORMAT_MACROS" to see if that helps Travis on gcc 4.6.3 * Revert space removal around PRId64 * video branch ffmpeg 2.9 fixes ffmpeg 2.9 patched removed SSE2 CPU * Add FFMPEGInit back * use webvvt to overlay timestamp (honoring Monitor.LabelFormat) to videos in timeline and event also fixed bug which prevented seeking in timeline video preview * ffmpeg 3.0 API build failure fixes * Update README.md * merge all the commits from the messed up iconnor_video branch * fix whitespace * revert * whitespace fixes * spelling fix * put back some text * add these back * fix spelling mistake * Steal some packet dumping routines from ffmpeg. Convert them to use our logging routines * add a test and error message if the codec is not h264 * these have been removed in master * add a view to check auth and just send the video * add some comments, and dump filename and AVFormatContext on failure to write header * add the toggle for RecordAudio so that the checkbox works to turn off Audio * Must init videoStore in constuctor * more debug and comments, return checking * Fix dropped part of sql query. * fix extra else and some whitespace * Fix missing } from merge that was preventing building. * fix tabs * get rid of use of separator, just use \n * Restore lost fixes for deprecation * Why are these failing * Respect record_audio flag when setting up video file so dont try and initiliase mp4 with unsupported audio * Forgot that I was trying to solve case of stream is true and record_audio is false. * Pass swscale_ctx back in to getCachedContext or it will create new context every frame and leak memory like a mofo. * Add libx264-dev and libmp4v2-dev to build requires to save hassle of ensuring they are installed before build. * Merge my Rotation/Orientation work and fixes for bad h264 streams * need arpa/inet for reverse lookups * pull in the new byte range code for viewing videos * Move our recording flag deeper into closeevent * add braces and only call closeEvent if there is an event * deprecate the z_frame_rate stuff which is deprecated in ffmpeg * remark out some debugging * fix for video on stream 1 * fix audio_stream to audio_st * Ignore bad decodes * fix problems with content-length causing viewing to not work in chrome/android * change logic of sending file contents to handle an off by one and be more readable * Some fixes pointed out by Maxim Romanov. Also simply the loading of events to not join the Monitors table * fix to sql for timeline * added RecordAudio to sql in README * Use sub queries instead of joins to fix errors when using new mysql defaults. * fix sql queries * Dockerfile to build feature-h264-videostorage * Must cast codec * add php-acpu as a dependency * require php5-acpu * fix typo * remove extra / * Add a line for out-of-tree builds to do api/lib/Cake/bootstrap.php * delete merge conflict files * delete merge conflict files
2017-05-16 10:02:48 +08:00
} else {
2017-05-30 21:10:41 +08:00
$captImage = sprintf( '%0'.ZM_EVENT_IMAGE_DIGITS.'d-capture.jpg', $frame['FrameId'] );
Feature h264 videostorage (#1882) * Moved writing of configure options from Controller to Model. Fixes #191. * Initial commit for saving events as videos :) * Add zm_video.cpp to autotools * Add zm_video.h to autotools * Search for MP4V2 header file 3 times: mp4v2/mp4v2.h, mp4v2.h, mp4.h * Fix serve memory leak * Few minor code improvements * Added the ability to override preset, tune, profile and few other improvements * Correctly write SPS & PPS from x264 encoder headers * Remove unnessecary SPS & PPS writing code * Imported missing files from master to feature-h264-videostorage * Audio support including fixes for dts/pts, split on keyframe and update to mkv extension to prevent ffmpeg problems writing rtsp audio to mp4 containter (header problem) * Updates to make gcc happy * Add html5 video control to timeline and event to support mkv playback * Add zm_videostore.cpp to CMakeLists.txt * Remove Modern Branch for now * Fix minor bug * Option handled added in master, removing duplicate declaration * Add CaptureandRecord from zm_camera.h * Putting placeholder in for CaptureAndRecord function * Removed duplicate code and brackets * add digest auth file for cmake Conflicts: src/CMakeLists.txt * Add web dir back into Makefile.am Revert "Removed web from SUBDIRS in Makefile.am" This reverts commit d9bbcdf3a94cba4d8955fcd03bd965ed2772f34d. * Add CaptureAndRecord to vlc, still need to make it record * Resolve SegFault on videostore * Swap to mp4 container * mp4 changes * spaces to tabs, hide video stuff if video writer is turned off * Make timeline open event.mp4 instead of mkv * Missed mkv in timeline.js * Fix some issues from the merge conflict * Resolve post merge build issues with braces * Fix whitespace * Update Jpeg and Video options for passthrough options * Whitespace fix zm_camera.h * Fix array mkssing comma * Add support for Jpeg save options for h264 branch snapshot. Might remove altogether if snapshots not needed * Update VideoStoreData memory size comment * Change from config.use_mkv_storage to per monitor option VideoWriter from video branch * Fix bracket issues post merge * Clean up comments and add av_free_packet * Convert from event_directory to event file as per Video branch * Testing videojs for video playback * Fixed a missing bracket post merge and also SQL_values now used for EventID and Monitors * bring recent improvements in ffmpeg capture function into captureandrecord * Remove pict from writeAudioFramePacket as not used * Add translate options for h264 Storage options in Monitor and update en_gb file * Cherry-Pick from iconnor - make it compile on ubuntu 15.04. Which is libav 56.1.0 Conflicts: src/zm_ffmpeg.cpp src/zm_remote_camera_rtsp.cpp Conflicts: distros/ubuntu1204/changelog * Clean up videostore code and remove lots of unused code * proof of concept for dynamic/automatic video rotation using video-js plugin zoomrotate Conflicts: web/skins/classic/views/event.php * removed redundant field in sql query Conflicts: web/skins/classic/views/event.php * local storage of video js plugin * Beautify! Make the code somewhat readable. * added missing videojs.zoomrotate.js file added missing videojs.zoomrotate.js file * Typo added missing " * Added missing brackets * fix to display thumbnails when only storing snapshot.jpg * added control for video playback rate Conflicts: web/skins/classic/views/event.php * dynamically create jpegs from video file for viewing in browser * fix timeline view for SaveJPEGs monitors (without enabled VideoWriter) * only expose monitor info which are being used in client * fix segmentation fault in zma with ubuntu 14.04 and ffmpeg 2.5.8 (gcc 4.8) when libx264 is not installed * better way of detecting showing image or video in timeline and event view instead of Monitor.VideoWriter, Event.DefaultVideo is used, so even if VideoWriter/SaveJPEG option is changed, a valid image or video will always be displayed for historical events in both timeline and event view this also fixes loading videos in timeline view * Fixes problem of crashing zmc when bad packet arrives causing av_interleaved_write_frame() to return non-zero (-22). Prefilters common packet issues. Add metadata title to generated video file * Remove syslog.h * fixed SaveJPEGs are not working which is caused in errors introduced when merging with master * Update README.md * Fix build warnings specific to h264 branch, unused FrameImg, unused ret and int64_t snprintf issues * Fix PRId64 issue in travis, builds locally fine, but I can see a gcc version issue here * Fix PRId64 issue in travis, another try * Try "STDC_FORMAT_MACROS" to see if that helps Travis on gcc 4.6.3 * Revert space removal around PRId64 * video branch ffmpeg 2.9 fixes ffmpeg 2.9 patched removed SSE2 CPU * Add FFMPEGInit back * use webvvt to overlay timestamp (honoring Monitor.LabelFormat) to videos in timeline and event also fixed bug which prevented seeking in timeline video preview * ffmpeg 3.0 API build failure fixes * Update README.md * merge all the commits from the messed up iconnor_video branch * fix whitespace * revert * whitespace fixes * spelling fix * put back some text * add these back * fix spelling mistake * Steal some packet dumping routines from ffmpeg. Convert them to use our logging routines * add a test and error message if the codec is not h264 * these have been removed in master * add a view to check auth and just send the video * add some comments, and dump filename and AVFormatContext on failure to write header * add the toggle for RecordAudio so that the checkbox works to turn off Audio * Must init videoStore in constuctor * more debug and comments, return checking * Fix dropped part of sql query. * fix extra else and some whitespace * Fix missing } from merge that was preventing building. * fix tabs * get rid of use of separator, just use \n * Restore lost fixes for deprecation * Why are these failing * Respect record_audio flag when setting up video file so dont try and initiliase mp4 with unsupported audio * Forgot that I was trying to solve case of stream is true and record_audio is false. * Pass swscale_ctx back in to getCachedContext or it will create new context every frame and leak memory like a mofo. * Add libx264-dev and libmp4v2-dev to build requires to save hassle of ensuring they are installed before build. * Merge my Rotation/Orientation work and fixes for bad h264 streams * need arpa/inet for reverse lookups * pull in the new byte range code for viewing videos * Move our recording flag deeper into closeevent * add braces and only call closeEvent if there is an event * deprecate the z_frame_rate stuff which is deprecated in ffmpeg * remark out some debugging * fix for video on stream 1 * fix audio_stream to audio_st * Ignore bad decodes * fix problems with content-length causing viewing to not work in chrome/android * change logic of sending file contents to handle an off by one and be more readable * Some fixes pointed out by Maxim Romanov. Also simply the loading of events to not join the Monitors table * fix to sql for timeline * added RecordAudio to sql in README * Use sub queries instead of joins to fix errors when using new mysql defaults. * fix sql queries * Dockerfile to build feature-h264-videostorage * Must cast codec * add php-acpu as a dependency * require php5-acpu * fix typo * remove extra / * Add a line for out-of-tree builds to do api/lib/Cake/bootstrap.php * delete merge conflict files * delete merge conflict files
2017-05-16 10:02:48 +08:00
if ( ! file_exists( $eventPath.'/'.$captImage ) ) {
# Generate the frame JPG
if ( $event['DefaultVideo'] ) {
$command ='ffmpeg -v 0 -i '.$eventPath.'/'.$event['DefaultVideo'].' -vf "select=gte(n\\,'.$frame['FrameId'].'),setpts=PTS-STARTPTS" '.$eventPath.'/'.$captImage;
system( $command, $retval );
} else {
Error("Can't create frame images from video because there is no video file for this event " );
}
}
}
2016-05-12 22:17:41 +08:00
$captPath = $eventPath.'/'.$captImage;
$thumbCaptPath = ZM_DIR_IMAGES.'/'.$event['Id'].'-'.$captImage;
//echo "CI:$captImage, CP:$captPath, TCP:$thumbCaptPath<br>";
2017-05-30 21:10:41 +08:00
$analImage = sprintf( '%0'.ZM_EVENT_IMAGE_DIGITS.'d-analyse.jpg', $frame['FrameId'] );
2016-05-12 22:17:41 +08:00
$analPath = $eventPath.'/'.$analImage;
$thumbAnalPath = ZM_DIR_IMAGES.'/'.$event['Id'].'-'.$analImage;
//echo "AI:$analImage, AP:$analPath, TAP:$thumbAnalPath<br>";
2016-05-12 22:17:41 +08:00
$alarmFrame = $frame['Type']=='Alarm';
Feature h264 videostorage (#1882) * Moved writing of configure options from Controller to Model. Fixes #191. * Initial commit for saving events as videos :) * Add zm_video.cpp to autotools * Add zm_video.h to autotools * Search for MP4V2 header file 3 times: mp4v2/mp4v2.h, mp4v2.h, mp4.h * Fix serve memory leak * Few minor code improvements * Added the ability to override preset, tune, profile and few other improvements * Correctly write SPS & PPS from x264 encoder headers * Remove unnessecary SPS & PPS writing code * Imported missing files from master to feature-h264-videostorage * Audio support including fixes for dts/pts, split on keyframe and update to mkv extension to prevent ffmpeg problems writing rtsp audio to mp4 containter (header problem) * Updates to make gcc happy * Add html5 video control to timeline and event to support mkv playback * Add zm_videostore.cpp to CMakeLists.txt * Remove Modern Branch for now * Fix minor bug * Option handled added in master, removing duplicate declaration * Add CaptureandRecord from zm_camera.h * Putting placeholder in for CaptureAndRecord function * Removed duplicate code and brackets * add digest auth file for cmake Conflicts: src/CMakeLists.txt * Add web dir back into Makefile.am Revert "Removed web from SUBDIRS in Makefile.am" This reverts commit d9bbcdf3a94cba4d8955fcd03bd965ed2772f34d. * Add CaptureAndRecord to vlc, still need to make it record * Resolve SegFault on videostore * Swap to mp4 container * mp4 changes * spaces to tabs, hide video stuff if video writer is turned off * Make timeline open event.mp4 instead of mkv * Missed mkv in timeline.js * Fix some issues from the merge conflict * Resolve post merge build issues with braces * Fix whitespace * Update Jpeg and Video options for passthrough options * Whitespace fix zm_camera.h * Fix array mkssing comma * Add support for Jpeg save options for h264 branch snapshot. Might remove altogether if snapshots not needed * Update VideoStoreData memory size comment * Change from config.use_mkv_storage to per monitor option VideoWriter from video branch * Fix bracket issues post merge * Clean up comments and add av_free_packet * Convert from event_directory to event file as per Video branch * Testing videojs for video playback * Fixed a missing bracket post merge and also SQL_values now used for EventID and Monitors * bring recent improvements in ffmpeg capture function into captureandrecord * Remove pict from writeAudioFramePacket as not used * Add translate options for h264 Storage options in Monitor and update en_gb file * Cherry-Pick from iconnor - make it compile on ubuntu 15.04. Which is libav 56.1.0 Conflicts: src/zm_ffmpeg.cpp src/zm_remote_camera_rtsp.cpp Conflicts: distros/ubuntu1204/changelog * Clean up videostore code and remove lots of unused code * proof of concept for dynamic/automatic video rotation using video-js plugin zoomrotate Conflicts: web/skins/classic/views/event.php * removed redundant field in sql query Conflicts: web/skins/classic/views/event.php * local storage of video js plugin * Beautify! Make the code somewhat readable. * added missing videojs.zoomrotate.js file added missing videojs.zoomrotate.js file * Typo added missing " * Added missing brackets * fix to display thumbnails when only storing snapshot.jpg * added control for video playback rate Conflicts: web/skins/classic/views/event.php * dynamically create jpegs from video file for viewing in browser * fix timeline view for SaveJPEGs monitors (without enabled VideoWriter) * only expose monitor info which are being used in client * fix segmentation fault in zma with ubuntu 14.04 and ffmpeg 2.5.8 (gcc 4.8) when libx264 is not installed * better way of detecting showing image or video in timeline and event view instead of Monitor.VideoWriter, Event.DefaultVideo is used, so even if VideoWriter/SaveJPEG option is changed, a valid image or video will always be displayed for historical events in both timeline and event view this also fixes loading videos in timeline view * Fixes problem of crashing zmc when bad packet arrives causing av_interleaved_write_frame() to return non-zero (-22). Prefilters common packet issues. Add metadata title to generated video file * Remove syslog.h * fixed SaveJPEGs are not working which is caused in errors introduced when merging with master * Update README.md * Fix build warnings specific to h264 branch, unused FrameImg, unused ret and int64_t snprintf issues * Fix PRId64 issue in travis, builds locally fine, but I can see a gcc version issue here * Fix PRId64 issue in travis, another try * Try "STDC_FORMAT_MACROS" to see if that helps Travis on gcc 4.6.3 * Revert space removal around PRId64 * video branch ffmpeg 2.9 fixes ffmpeg 2.9 patched removed SSE2 CPU * Add FFMPEGInit back * use webvvt to overlay timestamp (honoring Monitor.LabelFormat) to videos in timeline and event also fixed bug which prevented seeking in timeline video preview * ffmpeg 3.0 API build failure fixes * Update README.md * merge all the commits from the messed up iconnor_video branch * fix whitespace * revert * whitespace fixes * spelling fix * put back some text * add these back * fix spelling mistake * Steal some packet dumping routines from ffmpeg. Convert them to use our logging routines * add a test and error message if the codec is not h264 * these have been removed in master * add a view to check auth and just send the video * add some comments, and dump filename and AVFormatContext on failure to write header * add the toggle for RecordAudio so that the checkbox works to turn off Audio * Must init videoStore in constuctor * more debug and comments, return checking * Fix dropped part of sql query. * fix extra else and some whitespace * Fix missing } from merge that was preventing building. * fix tabs * get rid of use of separator, just use \n * Restore lost fixes for deprecation * Why are these failing * Respect record_audio flag when setting up video file so dont try and initiliase mp4 with unsupported audio * Forgot that I was trying to solve case of stream is true and record_audio is false. * Pass swscale_ctx back in to getCachedContext or it will create new context every frame and leak memory like a mofo. * Add libx264-dev and libmp4v2-dev to build requires to save hassle of ensuring they are installed before build. * Merge my Rotation/Orientation work and fixes for bad h264 streams * need arpa/inet for reverse lookups * pull in the new byte range code for viewing videos * Move our recording flag deeper into closeevent * add braces and only call closeEvent if there is an event * deprecate the z_frame_rate stuff which is deprecated in ffmpeg * remark out some debugging * fix for video on stream 1 * fix audio_stream to audio_st * Ignore bad decodes * fix problems with content-length causing viewing to not work in chrome/android * change logic of sending file contents to handle an off by one and be more readable * Some fixes pointed out by Maxim Romanov. Also simply the loading of events to not join the Monitors table * fix to sql for timeline * added RecordAudio to sql in README * Use sub queries instead of joins to fix errors when using new mysql defaults. * fix sql queries * Dockerfile to build feature-h264-videostorage * Must cast codec * add php-acpu as a dependency * require php5-acpu * fix typo * remove extra / * Add a line for out-of-tree builds to do api/lib/Cake/bootstrap.php * delete merge conflict files * delete merge conflict files
2017-05-16 10:02:48 +08:00
$hasAnalImage = $alarmFrame && file_exists( $analPath ) && filesize( $analPath );
2016-05-12 22:17:41 +08:00
$isAnalImage = $hasAnalImage && !$captureOnly;
2016-05-12 22:17:41 +08:00
if ( !ZM_WEB_SCALE_THUMBS || $scale >= SCALE_BASE || !function_exists( 'imagecreatefromjpeg' ) ) {
$imagePath = $thumbPath = $isAnalImage?$analPath:$captPath;
2017-05-30 21:10:41 +08:00
$imageFile = ZM_DIR_EVENTS.'/'.$imagePath;
$thumbFile = ZM_DIR_EVENTS.'/'.$thumbPath;
2016-05-12 22:17:41 +08:00
} else {
2017-05-30 21:10:41 +08:00
if ( version_compare( phpversion(), '4.3.10', '>=') )
$fraction = sprintf( '%.3F', $scale/SCALE_BASE );
else
2017-05-30 21:10:41 +08:00
$fraction = sprintf( '%.3f', $scale/SCALE_BASE );
2016-05-12 22:17:41 +08:00
$scale = (int)round( $scale );
2017-05-30 21:10:41 +08:00
$thumbCaptPath = preg_replace( '/\.jpg$/', "-$scale.jpg", $thumbCaptPath );
$thumbAnalPath = preg_replace( '/\.jpg$/', "-$scale.jpg", $thumbAnalPath );
2016-05-12 22:17:41 +08:00
if ( $isAnalImage ) {
$imagePath = $analPath;
$thumbPath = $thumbAnalPath;
} else {
$imagePath = $captPath;
$thumbPath = $thumbCaptPath;
}
2016-05-12 22:17:41 +08:00
$thumbFile = $thumbPath;
if ( $overwrite || !file_exists( $thumbFile ) || !filesize( $thumbFile ) ) {
// Get new dimensions
Feature h264 videostorage (#1882) * Moved writing of configure options from Controller to Model. Fixes #191. * Initial commit for saving events as videos :) * Add zm_video.cpp to autotools * Add zm_video.h to autotools * Search for MP4V2 header file 3 times: mp4v2/mp4v2.h, mp4v2.h, mp4.h * Fix serve memory leak * Few minor code improvements * Added the ability to override preset, tune, profile and few other improvements * Correctly write SPS & PPS from x264 encoder headers * Remove unnessecary SPS & PPS writing code * Imported missing files from master to feature-h264-videostorage * Audio support including fixes for dts/pts, split on keyframe and update to mkv extension to prevent ffmpeg problems writing rtsp audio to mp4 containter (header problem) * Updates to make gcc happy * Add html5 video control to timeline and event to support mkv playback * Add zm_videostore.cpp to CMakeLists.txt * Remove Modern Branch for now * Fix minor bug * Option handled added in master, removing duplicate declaration * Add CaptureandRecord from zm_camera.h * Putting placeholder in for CaptureAndRecord function * Removed duplicate code and brackets * add digest auth file for cmake Conflicts: src/CMakeLists.txt * Add web dir back into Makefile.am Revert "Removed web from SUBDIRS in Makefile.am" This reverts commit d9bbcdf3a94cba4d8955fcd03bd965ed2772f34d. * Add CaptureAndRecord to vlc, still need to make it record * Resolve SegFault on videostore * Swap to mp4 container * mp4 changes * spaces to tabs, hide video stuff if video writer is turned off * Make timeline open event.mp4 instead of mkv * Missed mkv in timeline.js * Fix some issues from the merge conflict * Resolve post merge build issues with braces * Fix whitespace * Update Jpeg and Video options for passthrough options * Whitespace fix zm_camera.h * Fix array mkssing comma * Add support for Jpeg save options for h264 branch snapshot. Might remove altogether if snapshots not needed * Update VideoStoreData memory size comment * Change from config.use_mkv_storage to per monitor option VideoWriter from video branch * Fix bracket issues post merge * Clean up comments and add av_free_packet * Convert from event_directory to event file as per Video branch * Testing videojs for video playback * Fixed a missing bracket post merge and also SQL_values now used for EventID and Monitors * bring recent improvements in ffmpeg capture function into captureandrecord * Remove pict from writeAudioFramePacket as not used * Add translate options for h264 Storage options in Monitor and update en_gb file * Cherry-Pick from iconnor - make it compile on ubuntu 15.04. Which is libav 56.1.0 Conflicts: src/zm_ffmpeg.cpp src/zm_remote_camera_rtsp.cpp Conflicts: distros/ubuntu1204/changelog * Clean up videostore code and remove lots of unused code * proof of concept for dynamic/automatic video rotation using video-js plugin zoomrotate Conflicts: web/skins/classic/views/event.php * removed redundant field in sql query Conflicts: web/skins/classic/views/event.php * local storage of video js plugin * Beautify! Make the code somewhat readable. * added missing videojs.zoomrotate.js file added missing videojs.zoomrotate.js file * Typo added missing " * Added missing brackets * fix to display thumbnails when only storing snapshot.jpg * added control for video playback rate Conflicts: web/skins/classic/views/event.php * dynamically create jpegs from video file for viewing in browser * fix timeline view for SaveJPEGs monitors (without enabled VideoWriter) * only expose monitor info which are being used in client * fix segmentation fault in zma with ubuntu 14.04 and ffmpeg 2.5.8 (gcc 4.8) when libx264 is not installed * better way of detecting showing image or video in timeline and event view instead of Monitor.VideoWriter, Event.DefaultVideo is used, so even if VideoWriter/SaveJPEG option is changed, a valid image or video will always be displayed for historical events in both timeline and event view this also fixes loading videos in timeline view * Fixes problem of crashing zmc when bad packet arrives causing av_interleaved_write_frame() to return non-zero (-22). Prefilters common packet issues. Add metadata title to generated video file * Remove syslog.h * fixed SaveJPEGs are not working which is caused in errors introduced when merging with master * Update README.md * Fix build warnings specific to h264 branch, unused FrameImg, unused ret and int64_t snprintf issues * Fix PRId64 issue in travis, builds locally fine, but I can see a gcc version issue here * Fix PRId64 issue in travis, another try * Try "STDC_FORMAT_MACROS" to see if that helps Travis on gcc 4.6.3 * Revert space removal around PRId64 * video branch ffmpeg 2.9 fixes ffmpeg 2.9 patched removed SSE2 CPU * Add FFMPEGInit back * use webvvt to overlay timestamp (honoring Monitor.LabelFormat) to videos in timeline and event also fixed bug which prevented seeking in timeline video preview * ffmpeg 3.0 API build failure fixes * Update README.md * merge all the commits from the messed up iconnor_video branch * fix whitespace * revert * whitespace fixes * spelling fix * put back some text * add these back * fix spelling mistake * Steal some packet dumping routines from ffmpeg. Convert them to use our logging routines * add a test and error message if the codec is not h264 * these have been removed in master * add a view to check auth and just send the video * add some comments, and dump filename and AVFormatContext on failure to write header * add the toggle for RecordAudio so that the checkbox works to turn off Audio * Must init videoStore in constuctor * more debug and comments, return checking * Fix dropped part of sql query. * fix extra else and some whitespace * Fix missing } from merge that was preventing building. * fix tabs * get rid of use of separator, just use \n * Restore lost fixes for deprecation * Why are these failing * Respect record_audio flag when setting up video file so dont try and initiliase mp4 with unsupported audio * Forgot that I was trying to solve case of stream is true and record_audio is false. * Pass swscale_ctx back in to getCachedContext or it will create new context every frame and leak memory like a mofo. * Add libx264-dev and libmp4v2-dev to build requires to save hassle of ensuring they are installed before build. * Merge my Rotation/Orientation work and fixes for bad h264 streams * need arpa/inet for reverse lookups * pull in the new byte range code for viewing videos * Move our recording flag deeper into closeevent * add braces and only call closeEvent if there is an event * deprecate the z_frame_rate stuff which is deprecated in ffmpeg * remark out some debugging * fix for video on stream 1 * fix audio_stream to audio_st * Ignore bad decodes * fix problems with content-length causing viewing to not work in chrome/android * change logic of sending file contents to handle an off by one and be more readable * Some fixes pointed out by Maxim Romanov. Also simply the loading of events to not join the Monitors table * fix to sql for timeline * added RecordAudio to sql in README * Use sub queries instead of joins to fix errors when using new mysql defaults. * fix sql queries * Dockerfile to build feature-h264-videostorage * Must cast codec * add php-acpu as a dependency * require php5-acpu * fix typo * remove extra / * Add a line for out-of-tree builds to do api/lib/Cake/bootstrap.php * delete merge conflict files * delete merge conflict files
2017-05-16 10:02:48 +08:00
list( $imageWidth, $imageHeight ) = getimagesize( $imagePath );
2016-05-12 22:17:41 +08:00
$thumbWidth = $imageWidth * $fraction;
$thumbHeight = $imageHeight * $fraction;
2016-05-12 22:17:41 +08:00
// Resample
$thumbImage = imagecreatetruecolor( $thumbWidth, $thumbHeight );
Feature h264 videostorage (#1882) * Moved writing of configure options from Controller to Model. Fixes #191. * Initial commit for saving events as videos :) * Add zm_video.cpp to autotools * Add zm_video.h to autotools * Search for MP4V2 header file 3 times: mp4v2/mp4v2.h, mp4v2.h, mp4.h * Fix serve memory leak * Few minor code improvements * Added the ability to override preset, tune, profile and few other improvements * Correctly write SPS & PPS from x264 encoder headers * Remove unnessecary SPS & PPS writing code * Imported missing files from master to feature-h264-videostorage * Audio support including fixes for dts/pts, split on keyframe and update to mkv extension to prevent ffmpeg problems writing rtsp audio to mp4 containter (header problem) * Updates to make gcc happy * Add html5 video control to timeline and event to support mkv playback * Add zm_videostore.cpp to CMakeLists.txt * Remove Modern Branch for now * Fix minor bug * Option handled added in master, removing duplicate declaration * Add CaptureandRecord from zm_camera.h * Putting placeholder in for CaptureAndRecord function * Removed duplicate code and brackets * add digest auth file for cmake Conflicts: src/CMakeLists.txt * Add web dir back into Makefile.am Revert "Removed web from SUBDIRS in Makefile.am" This reverts commit d9bbcdf3a94cba4d8955fcd03bd965ed2772f34d. * Add CaptureAndRecord to vlc, still need to make it record * Resolve SegFault on videostore * Swap to mp4 container * mp4 changes * spaces to tabs, hide video stuff if video writer is turned off * Make timeline open event.mp4 instead of mkv * Missed mkv in timeline.js * Fix some issues from the merge conflict * Resolve post merge build issues with braces * Fix whitespace * Update Jpeg and Video options for passthrough options * Whitespace fix zm_camera.h * Fix array mkssing comma * Add support for Jpeg save options for h264 branch snapshot. Might remove altogether if snapshots not needed * Update VideoStoreData memory size comment * Change from config.use_mkv_storage to per monitor option VideoWriter from video branch * Fix bracket issues post merge * Clean up comments and add av_free_packet * Convert from event_directory to event file as per Video branch * Testing videojs for video playback * Fixed a missing bracket post merge and also SQL_values now used for EventID and Monitors * bring recent improvements in ffmpeg capture function into captureandrecord * Remove pict from writeAudioFramePacket as not used * Add translate options for h264 Storage options in Monitor and update en_gb file * Cherry-Pick from iconnor - make it compile on ubuntu 15.04. Which is libav 56.1.0 Conflicts: src/zm_ffmpeg.cpp src/zm_remote_camera_rtsp.cpp Conflicts: distros/ubuntu1204/changelog * Clean up videostore code and remove lots of unused code * proof of concept for dynamic/automatic video rotation using video-js plugin zoomrotate Conflicts: web/skins/classic/views/event.php * removed redundant field in sql query Conflicts: web/skins/classic/views/event.php * local storage of video js plugin * Beautify! Make the code somewhat readable. * added missing videojs.zoomrotate.js file added missing videojs.zoomrotate.js file * Typo added missing " * Added missing brackets * fix to display thumbnails when only storing snapshot.jpg * added control for video playback rate Conflicts: web/skins/classic/views/event.php * dynamically create jpegs from video file for viewing in browser * fix timeline view for SaveJPEGs monitors (without enabled VideoWriter) * only expose monitor info which are being used in client * fix segmentation fault in zma with ubuntu 14.04 and ffmpeg 2.5.8 (gcc 4.8) when libx264 is not installed * better way of detecting showing image or video in timeline and event view instead of Monitor.VideoWriter, Event.DefaultVideo is used, so even if VideoWriter/SaveJPEG option is changed, a valid image or video will always be displayed for historical events in both timeline and event view this also fixes loading videos in timeline view * Fixes problem of crashing zmc when bad packet arrives causing av_interleaved_write_frame() to return non-zero (-22). Prefilters common packet issues. Add metadata title to generated video file * Remove syslog.h * fixed SaveJPEGs are not working which is caused in errors introduced when merging with master * Update README.md * Fix build warnings specific to h264 branch, unused FrameImg, unused ret and int64_t snprintf issues * Fix PRId64 issue in travis, builds locally fine, but I can see a gcc version issue here * Fix PRId64 issue in travis, another try * Try "STDC_FORMAT_MACROS" to see if that helps Travis on gcc 4.6.3 * Revert space removal around PRId64 * video branch ffmpeg 2.9 fixes ffmpeg 2.9 patched removed SSE2 CPU * Add FFMPEGInit back * use webvvt to overlay timestamp (honoring Monitor.LabelFormat) to videos in timeline and event also fixed bug which prevented seeking in timeline video preview * ffmpeg 3.0 API build failure fixes * Update README.md * merge all the commits from the messed up iconnor_video branch * fix whitespace * revert * whitespace fixes * spelling fix * put back some text * add these back * fix spelling mistake * Steal some packet dumping routines from ffmpeg. Convert them to use our logging routines * add a test and error message if the codec is not h264 * these have been removed in master * add a view to check auth and just send the video * add some comments, and dump filename and AVFormatContext on failure to write header * add the toggle for RecordAudio so that the checkbox works to turn off Audio * Must init videoStore in constuctor * more debug and comments, return checking * Fix dropped part of sql query. * fix extra else and some whitespace * Fix missing } from merge that was preventing building. * fix tabs * get rid of use of separator, just use \n * Restore lost fixes for deprecation * Why are these failing * Respect record_audio flag when setting up video file so dont try and initiliase mp4 with unsupported audio * Forgot that I was trying to solve case of stream is true and record_audio is false. * Pass swscale_ctx back in to getCachedContext or it will create new context every frame and leak memory like a mofo. * Add libx264-dev and libmp4v2-dev to build requires to save hassle of ensuring they are installed before build. * Merge my Rotation/Orientation work and fixes for bad h264 streams * need arpa/inet for reverse lookups * pull in the new byte range code for viewing videos * Move our recording flag deeper into closeevent * add braces and only call closeEvent if there is an event * deprecate the z_frame_rate stuff which is deprecated in ffmpeg * remark out some debugging * fix for video on stream 1 * fix audio_stream to audio_st * Ignore bad decodes * fix problems with content-length causing viewing to not work in chrome/android * change logic of sending file contents to handle an off by one and be more readable * Some fixes pointed out by Maxim Romanov. Also simply the loading of events to not join the Monitors table * fix to sql for timeline * added RecordAudio to sql in README * Use sub queries instead of joins to fix errors when using new mysql defaults. * fix sql queries * Dockerfile to build feature-h264-videostorage * Must cast codec * add php-acpu as a dependency * require php5-acpu * fix typo * remove extra / * Add a line for out-of-tree builds to do api/lib/Cake/bootstrap.php * delete merge conflict files * delete merge conflict files
2017-05-16 10:02:48 +08:00
$image = imagecreatefromjpeg( $imagePath );
2016-05-12 22:17:41 +08:00
imagecopyresampled( $thumbImage, $image, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imageWidth, $imageHeight );
Feature h264 videostorage (#1882) * Moved writing of configure options from Controller to Model. Fixes #191. * Initial commit for saving events as videos :) * Add zm_video.cpp to autotools * Add zm_video.h to autotools * Search for MP4V2 header file 3 times: mp4v2/mp4v2.h, mp4v2.h, mp4.h * Fix serve memory leak * Few minor code improvements * Added the ability to override preset, tune, profile and few other improvements * Correctly write SPS & PPS from x264 encoder headers * Remove unnessecary SPS & PPS writing code * Imported missing files from master to feature-h264-videostorage * Audio support including fixes for dts/pts, split on keyframe and update to mkv extension to prevent ffmpeg problems writing rtsp audio to mp4 containter (header problem) * Updates to make gcc happy * Add html5 video control to timeline and event to support mkv playback * Add zm_videostore.cpp to CMakeLists.txt * Remove Modern Branch for now * Fix minor bug * Option handled added in master, removing duplicate declaration * Add CaptureandRecord from zm_camera.h * Putting placeholder in for CaptureAndRecord function * Removed duplicate code and brackets * add digest auth file for cmake Conflicts: src/CMakeLists.txt * Add web dir back into Makefile.am Revert "Removed web from SUBDIRS in Makefile.am" This reverts commit d9bbcdf3a94cba4d8955fcd03bd965ed2772f34d. * Add CaptureAndRecord to vlc, still need to make it record * Resolve SegFault on videostore * Swap to mp4 container * mp4 changes * spaces to tabs, hide video stuff if video writer is turned off * Make timeline open event.mp4 instead of mkv * Missed mkv in timeline.js * Fix some issues from the merge conflict * Resolve post merge build issues with braces * Fix whitespace * Update Jpeg and Video options for passthrough options * Whitespace fix zm_camera.h * Fix array mkssing comma * Add support for Jpeg save options for h264 branch snapshot. Might remove altogether if snapshots not needed * Update VideoStoreData memory size comment * Change from config.use_mkv_storage to per monitor option VideoWriter from video branch * Fix bracket issues post merge * Clean up comments and add av_free_packet * Convert from event_directory to event file as per Video branch * Testing videojs for video playback * Fixed a missing bracket post merge and also SQL_values now used for EventID and Monitors * bring recent improvements in ffmpeg capture function into captureandrecord * Remove pict from writeAudioFramePacket as not used * Add translate options for h264 Storage options in Monitor and update en_gb file * Cherry-Pick from iconnor - make it compile on ubuntu 15.04. Which is libav 56.1.0 Conflicts: src/zm_ffmpeg.cpp src/zm_remote_camera_rtsp.cpp Conflicts: distros/ubuntu1204/changelog * Clean up videostore code and remove lots of unused code * proof of concept for dynamic/automatic video rotation using video-js plugin zoomrotate Conflicts: web/skins/classic/views/event.php * removed redundant field in sql query Conflicts: web/skins/classic/views/event.php * local storage of video js plugin * Beautify! Make the code somewhat readable. * added missing videojs.zoomrotate.js file added missing videojs.zoomrotate.js file * Typo added missing " * Added missing brackets * fix to display thumbnails when only storing snapshot.jpg * added control for video playback rate Conflicts: web/skins/classic/views/event.php * dynamically create jpegs from video file for viewing in browser * fix timeline view for SaveJPEGs monitors (without enabled VideoWriter) * only expose monitor info which are being used in client * fix segmentation fault in zma with ubuntu 14.04 and ffmpeg 2.5.8 (gcc 4.8) when libx264 is not installed * better way of detecting showing image or video in timeline and event view instead of Monitor.VideoWriter, Event.DefaultVideo is used, so even if VideoWriter/SaveJPEG option is changed, a valid image or video will always be displayed for historical events in both timeline and event view this also fixes loading videos in timeline view * Fixes problem of crashing zmc when bad packet arrives causing av_interleaved_write_frame() to return non-zero (-22). Prefilters common packet issues. Add metadata title to generated video file * Remove syslog.h * fixed SaveJPEGs are not working which is caused in errors introduced when merging with master * Update README.md * Fix build warnings specific to h264 branch, unused FrameImg, unused ret and int64_t snprintf issues * Fix PRId64 issue in travis, builds locally fine, but I can see a gcc version issue here * Fix PRId64 issue in travis, another try * Try "STDC_FORMAT_MACROS" to see if that helps Travis on gcc 4.6.3 * Revert space removal around PRId64 * video branch ffmpeg 2.9 fixes ffmpeg 2.9 patched removed SSE2 CPU * Add FFMPEGInit back * use webvvt to overlay timestamp (honoring Monitor.LabelFormat) to videos in timeline and event also fixed bug which prevented seeking in timeline video preview * ffmpeg 3.0 API build failure fixes * Update README.md * merge all the commits from the messed up iconnor_video branch * fix whitespace * revert * whitespace fixes * spelling fix * put back some text * add these back * fix spelling mistake * Steal some packet dumping routines from ffmpeg. Convert them to use our logging routines * add a test and error message if the codec is not h264 * these have been removed in master * add a view to check auth and just send the video * add some comments, and dump filename and AVFormatContext on failure to write header * add the toggle for RecordAudio so that the checkbox works to turn off Audio * Must init videoStore in constuctor * more debug and comments, return checking * Fix dropped part of sql query. * fix extra else and some whitespace * Fix missing } from merge that was preventing building. * fix tabs * get rid of use of separator, just use \n * Restore lost fixes for deprecation * Why are these failing * Respect record_audio flag when setting up video file so dont try and initiliase mp4 with unsupported audio * Forgot that I was trying to solve case of stream is true and record_audio is false. * Pass swscale_ctx back in to getCachedContext or it will create new context every frame and leak memory like a mofo. * Add libx264-dev and libmp4v2-dev to build requires to save hassle of ensuring they are installed before build. * Merge my Rotation/Orientation work and fixes for bad h264 streams * need arpa/inet for reverse lookups * pull in the new byte range code for viewing videos * Move our recording flag deeper into closeevent * add braces and only call closeEvent if there is an event * deprecate the z_frame_rate stuff which is deprecated in ffmpeg * remark out some debugging * fix for video on stream 1 * fix audio_stream to audio_st * Ignore bad decodes * fix problems with content-length causing viewing to not work in chrome/android * change logic of sending file contents to handle an off by one and be more readable * Some fixes pointed out by Maxim Romanov. Also simply the loading of events to not join the Monitors table * fix to sql for timeline * added RecordAudio to sql in README * Use sub queries instead of joins to fix errors when using new mysql defaults. * fix sql queries * Dockerfile to build feature-h264-videostorage * Must cast codec * add php-acpu as a dependency * require php5-acpu * fix typo * remove extra / * Add a line for out-of-tree builds to do api/lib/Cake/bootstrap.php * delete merge conflict files * delete merge conflict files
2017-05-16 10:02:48 +08:00
if ( !imagejpeg( $thumbImage, $thumbPath ) )
2016-05-12 22:17:41 +08:00
Error( "Can't create thumbnail '$thumbPath'" );
}
2016-05-12 22:17:41 +08:00
}
2016-05-12 22:17:41 +08:00
$imageData = array(
'eventPath' => $eventPath,
'imagePath' => $imagePath,
'thumbPath' => $thumbPath,
Feature h264 videostorage (#1882) * Moved writing of configure options from Controller to Model. Fixes #191. * Initial commit for saving events as videos :) * Add zm_video.cpp to autotools * Add zm_video.h to autotools * Search for MP4V2 header file 3 times: mp4v2/mp4v2.h, mp4v2.h, mp4.h * Fix serve memory leak * Few minor code improvements * Added the ability to override preset, tune, profile and few other improvements * Correctly write SPS & PPS from x264 encoder headers * Remove unnessecary SPS & PPS writing code * Imported missing files from master to feature-h264-videostorage * Audio support including fixes for dts/pts, split on keyframe and update to mkv extension to prevent ffmpeg problems writing rtsp audio to mp4 containter (header problem) * Updates to make gcc happy * Add html5 video control to timeline and event to support mkv playback * Add zm_videostore.cpp to CMakeLists.txt * Remove Modern Branch for now * Fix minor bug * Option handled added in master, removing duplicate declaration * Add CaptureandRecord from zm_camera.h * Putting placeholder in for CaptureAndRecord function * Removed duplicate code and brackets * add digest auth file for cmake Conflicts: src/CMakeLists.txt * Add web dir back into Makefile.am Revert "Removed web from SUBDIRS in Makefile.am" This reverts commit d9bbcdf3a94cba4d8955fcd03bd965ed2772f34d. * Add CaptureAndRecord to vlc, still need to make it record * Resolve SegFault on videostore * Swap to mp4 container * mp4 changes * spaces to tabs, hide video stuff if video writer is turned off * Make timeline open event.mp4 instead of mkv * Missed mkv in timeline.js * Fix some issues from the merge conflict * Resolve post merge build issues with braces * Fix whitespace * Update Jpeg and Video options for passthrough options * Whitespace fix zm_camera.h * Fix array mkssing comma * Add support for Jpeg save options for h264 branch snapshot. Might remove altogether if snapshots not needed * Update VideoStoreData memory size comment * Change from config.use_mkv_storage to per monitor option VideoWriter from video branch * Fix bracket issues post merge * Clean up comments and add av_free_packet * Convert from event_directory to event file as per Video branch * Testing videojs for video playback * Fixed a missing bracket post merge and also SQL_values now used for EventID and Monitors * bring recent improvements in ffmpeg capture function into captureandrecord * Remove pict from writeAudioFramePacket as not used * Add translate options for h264 Storage options in Monitor and update en_gb file * Cherry-Pick from iconnor - make it compile on ubuntu 15.04. Which is libav 56.1.0 Conflicts: src/zm_ffmpeg.cpp src/zm_remote_camera_rtsp.cpp Conflicts: distros/ubuntu1204/changelog * Clean up videostore code and remove lots of unused code * proof of concept for dynamic/automatic video rotation using video-js plugin zoomrotate Conflicts: web/skins/classic/views/event.php * removed redundant field in sql query Conflicts: web/skins/classic/views/event.php * local storage of video js plugin * Beautify! Make the code somewhat readable. * added missing videojs.zoomrotate.js file added missing videojs.zoomrotate.js file * Typo added missing " * Added missing brackets * fix to display thumbnails when only storing snapshot.jpg * added control for video playback rate Conflicts: web/skins/classic/views/event.php * dynamically create jpegs from video file for viewing in browser * fix timeline view for SaveJPEGs monitors (without enabled VideoWriter) * only expose monitor info which are being used in client * fix segmentation fault in zma with ubuntu 14.04 and ffmpeg 2.5.8 (gcc 4.8) when libx264 is not installed * better way of detecting showing image or video in timeline and event view instead of Monitor.VideoWriter, Event.DefaultVideo is used, so even if VideoWriter/SaveJPEG option is changed, a valid image or video will always be displayed for historical events in both timeline and event view this also fixes loading videos in timeline view * Fixes problem of crashing zmc when bad packet arrives causing av_interleaved_write_frame() to return non-zero (-22). Prefilters common packet issues. Add metadata title to generated video file * Remove syslog.h * fixed SaveJPEGs are not working which is caused in errors introduced when merging with master * Update README.md * Fix build warnings specific to h264 branch, unused FrameImg, unused ret and int64_t snprintf issues * Fix PRId64 issue in travis, builds locally fine, but I can see a gcc version issue here * Fix PRId64 issue in travis, another try * Try "STDC_FORMAT_MACROS" to see if that helps Travis on gcc 4.6.3 * Revert space removal around PRId64 * video branch ffmpeg 2.9 fixes ffmpeg 2.9 patched removed SSE2 CPU * Add FFMPEGInit back * use webvvt to overlay timestamp (honoring Monitor.LabelFormat) to videos in timeline and event also fixed bug which prevented seeking in timeline video preview * ffmpeg 3.0 API build failure fixes * Update README.md * merge all the commits from the messed up iconnor_video branch * fix whitespace * revert * whitespace fixes * spelling fix * put back some text * add these back * fix spelling mistake * Steal some packet dumping routines from ffmpeg. Convert them to use our logging routines * add a test and error message if the codec is not h264 * these have been removed in master * add a view to check auth and just send the video * add some comments, and dump filename and AVFormatContext on failure to write header * add the toggle for RecordAudio so that the checkbox works to turn off Audio * Must init videoStore in constuctor * more debug and comments, return checking * Fix dropped part of sql query. * fix extra else and some whitespace * Fix missing } from merge that was preventing building. * fix tabs * get rid of use of separator, just use \n * Restore lost fixes for deprecation * Why are these failing * Respect record_audio flag when setting up video file so dont try and initiliase mp4 with unsupported audio * Forgot that I was trying to solve case of stream is true and record_audio is false. * Pass swscale_ctx back in to getCachedContext or it will create new context every frame and leak memory like a mofo. * Add libx264-dev and libmp4v2-dev to build requires to save hassle of ensuring they are installed before build. * Merge my Rotation/Orientation work and fixes for bad h264 streams * need arpa/inet for reverse lookups * pull in the new byte range code for viewing videos * Move our recording flag deeper into closeevent * add braces and only call closeEvent if there is an event * deprecate the z_frame_rate stuff which is deprecated in ffmpeg * remark out some debugging * fix for video on stream 1 * fix audio_stream to audio_st * Ignore bad decodes * fix problems with content-length causing viewing to not work in chrome/android * change logic of sending file contents to handle an off by one and be more readable * Some fixes pointed out by Maxim Romanov. Also simply the loading of events to not join the Monitors table * fix to sql for timeline * added RecordAudio to sql in README * Use sub queries instead of joins to fix errors when using new mysql defaults. * fix sql queries * Dockerfile to build feature-h264-videostorage * Must cast codec * add php-acpu as a dependency * require php5-acpu * fix typo * remove extra / * Add a line for out-of-tree builds to do api/lib/Cake/bootstrap.php * delete merge conflict files * delete merge conflict files
2017-05-16 10:02:48 +08:00
'imageFile' => $imagePath,
2016-05-12 22:17:41 +08:00
'thumbFile' => $thumbFile,
2017-05-30 21:10:41 +08:00
'imageClass' => $alarmFrame?'alarm':'normal',
2016-05-12 22:17:41 +08:00
'isAnalImage' => $isAnalImage,
'hasAnalImage' => $hasAnalImage,
);
return( $imageData );
}
function viewImagePath( $path, $querySep='&amp;' ) {
if ( strncmp( $path, ZM_DIR_IMAGES, strlen(ZM_DIR_IMAGES) ) == 0 ) {
// Thumbnails
return( $path );
} elseif ( strpos( ZM_DIR_EVENTS, '/' ) === 0 ) {
return( '?view=image'.$querySep.'path='.$path );
}
return( ZM_DIR_EVENTS.'/'.$path );
}
2016-05-12 22:17:41 +08:00
function createListThumbnail( $event, $overwrite=false ) {
2017-05-30 23:15:00 +08:00
# Load the frame with the highest score to use as a thumbnail
2016-05-12 22:17:41 +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 );
2016-05-12 22:17:41 +08:00
$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 );
} 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 );
} else {
Fatal( "No thumbnail width or height specified, please check in Options->Web" );
}
2016-05-12 22:17:41 +08:00
$imageData = getImageSrc( $event, $frame, $scale, false, $overwrite );
2017-05-30 23:15:00 +08:00
if ( ! $imageData ) {
return ( false );
}
2016-05-12 22:17:41 +08:00
$thumbData = $frame;
$thumbData['Path'] = $imageData['thumbPath'];
$thumbData['Width'] = (int)$thumbWidth;
$thumbData['Height'] = (int)$thumbHeight;
2016-05-12 22:17:41 +08:00
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
2016-05-12 22:17:41 +08:00
$command .= " -s ".sprintf( "%.2f", ($scale/SCALE_BASE) );
if ( $overwrite )
$command .= " -o";
$result = exec( escapeshellcmd( $command ), $output, $status );
2017-05-30 21:10:41 +08:00
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;
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)/SCALE_BASE);
}
return( $new_dimension );
}
2016-05-12 22:17:41 +08:00
function deScale( $dimension, $dummy ) {
$new_dimension = $dimension;
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 );
}
2016-05-12 22:17:41 +08:00
function parseSort( $saveToSession=false, $querySep='&amp;' ) {
global $sortQuery, $sortColumn, $sortOrder; // Outputs
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';
2016-05-12 22:17:41 +08:00
break;
case 'MonitorName' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'M.Name';
2016-05-12 22:17:41 +08:00
break;
case 'Name' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.Name';
2016-05-12 22:17:41 +08:00
break;
case 'Cause' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.Cause';
2016-05-12 22:17:41 +08:00
break;
case 'DateTime' :
$_REQUEST['sort_field'] = 'StartTime';
case 'StartTime' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.StartTime';
2016-05-12 22:17:41 +08:00
break;
case 'Length' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.Length';
2016-05-12 22:17:41 +08:00
break;
case 'Frames' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.Frames';
2016-05-12 22:17:41 +08:00
break;
case 'AlarmFrames' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.AlarmFrames';
2016-05-12 22:17:41 +08:00
break;
case 'TotScore' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.TotScore';
2016-05-12 22:17:41 +08:00
break;
case 'AvgScore' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.AvgScore';
2016-05-12 22:17:41 +08:00
break;
case 'MaxScore' :
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.MaxScore';
2016-05-12 22:17:41 +08:00
break;
default:
2017-05-30 21:10:41 +08:00
$sortColumn = 'E.StartTime';
2016-05-12 22:17:41 +08:00
break;
}
2017-05-30 21:10:41 +08:00
$sortOrder = $_REQUEST['sort_asc']?'asc':'desc';
2016-05-12 22:17:41 +08:00
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']);
2016-05-12 22:17:41 +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 ) {
$_SESSION['sort_field'] = validHtmlStr($_REQUEST['sort_field']);
$_SESSION['sort_asc'] = validHtmlStr($_REQUEST['sort_asc']);
}
}
function parseFilter( &$filter, $saveToSession=false, $querySep='&amp;' ) {
$filter['query'] = '';
$filter['sql'] = '';
$filter['fields'] = '';
if ( isset($filter['terms']) && count($filter['terms']) ) {
for ( $i = 0; $i < count($filter['terms']); $i++ ) {
if ( isset($filter['terms'][$i]['cnj']) ) {
$filter['query'] .= $querySep.urlencode("filter[terms][$i][cnj]").'='.urlencode($filter['terms'][$i]['cnj']);
$filter['sql'] .= ' '.$filter['terms'][$i]['cnj'].' ';
2016-05-12 22:17:41 +08:00
$filter['fields'] .= "<input type=\"hidden\" name=\"filter[terms][$i][cnj]\" value=\"".htmlspecialchars($filter['terms'][$i]['cnj'])."\"/>\n";
}
if ( isset($filter['terms'][$i]['obr']) ) {
$filter['query'] .= $querySep.urlencode("filter[terms][$i][obr]").'='.urlencode($filter['terms'][$i]['obr']);
$filter['sql'] .= ' '.str_repeat( '(', $filter['terms'][$i]['obr'] ).' ';
2016-05-12 22:17:41 +08:00
$filter['fields'] .= "<input type=\"hidden\" name=\"filter[terms][$i][obr]\" value=\"".htmlspecialchars($filter['terms'][$i]['obr'])."\"/>\n";
}
if ( isset($filter['terms'][$i]['attr']) ) {
$filter['query'] .= $querySep.urlencode("filter[terms][$i][attr]").'='.urlencode($filter['terms'][$i]['attr']);
2016-05-12 22:17:41 +08:00
$filter['fields'] .= "<input type=\"hidden\" name=\"filter[terms][$i][attr]\" value=\"".htmlspecialchars($filter['terms'][$i]['attr'])."\"/>\n";
switch ( $filter['terms'][$i]['attr'] ) {
case 'MonitorName':
$filter['sql'] .= 'M.'.preg_replace( '/^Monitor/', '', $filter['terms'][$i]['attr'] );
break;
case 'ServerId':
$filter['sql'] .= 'M.ServerId';
break;
2016-05-12 22:17:41 +08:00
case 'DateTime':
2017-05-30 21:10:41 +08:00
$filter['sql'] .= 'E.StartTime';
break;
2016-05-12 22:17:41 +08:00
case 'Date':
2017-05-30 21:10:41 +08:00
$filter['sql'] .= 'to_days( E.StartTime )';
break;
2016-05-12 22:17:41 +08:00
case 'Time':
2017-05-30 21:10:41 +08:00
$filter['sql'] .= 'extract( hour_second from E.StartTime )';
break;
2016-05-12 22:17:41 +08:00
case 'Weekday':
2017-05-30 21:10:41 +08:00
$filter['sql'] .= 'weekday( E.StartTime )';
break;
2016-05-12 22:17:41 +08:00
case 'Id':
case 'Name':
case 'MonitorId':
case 'Length':
case 'Frames':
case 'AlarmFrames':
case 'TotScore':
case 'AvgScore':
case 'MaxScore':
case 'Cause':
case 'Notes':
case 'StateId':
2016-05-12 22:17:41 +08:00
case 'Archived':
$filter['sql'] .= 'E.'.$filter['terms'][$i]['attr'];
break;
2016-05-12 22:17:41 +08:00
case 'DiskPercent':
$filter['sql'] .= getDiskPercent();
break;
2016-05-12 22:17:41 +08:00
case 'DiskBlocks':
$filter['sql'] .= getDiskBlocks();
break;
2016-05-12 22:17:41 +08:00
case 'SystemLoad':
$filter['sql'] .= getLoad();
break;
2016-05-12 22:17:41 +08:00
}
$valueList = array();
foreach ( preg_split( '/["\'\s]*?,["\'\s]*?/', preg_replace( '/^["\']+?(.+)["\']+?$/', '$1', $filter['terms'][$i]['val'] ) ) as $value ) {
switch ( $filter['terms'][$i]['attr'] ) {
case 'MonitorName':
case 'Name':
case 'Cause':
case 'Notes':
$value = dbEscape($value);
break;
case 'ServerId':
if ( $value == 'ZM_SERVER_ID' ) {
$value = ZM_SERVER_ID;
} else {
$value = dbEscape($value);
}
break;
2016-05-12 22:17:41 +08:00
case 'DateTime':
$value = "'".strftime( STRF_FMT_DATETIME_DB, strtotime( $value ) )."'";
break;
case 'Date':
$value = "to_days( '".strftime( STRF_FMT_DATETIME_DB, strtotime( $value ) )."' )";
break;
case 'Time':
$value = "extract( hour_second from '".strftime( STRF_FMT_DATETIME_DB, strtotime( $value ) )."' )";
break;
default :
$value = dbEscape($value);
break;
}
$valueList[] = $value;
}
switch ( $filter['terms'][$i]['op'] ) {
case '=' :
case '!=' :
case '>=' :
case '>' :
case '<' :
case '<=' :
$filter['sql'] .= ' '.$filter['terms'][$i]['op'].' '. $value;
break;
2016-05-12 22:17:41 +08:00
case '=~' :
2017-05-30 21:10:41 +08:00
$filter['sql'] .= ' regexp '.$value;
break;
2016-05-12 22:17:41 +08:00
case '!~' :
2017-05-30 21:10:41 +08:00
$filter['sql'] .= ' not regexp '.$value;
break;
2016-05-12 22:17:41 +08:00
case '=[]' :
$filter['sql'] .= ' in ('.join( ',', $valueList ).')';
break;
2016-05-12 22:17:41 +08:00
case '![]' :
2017-05-30 21:10:41 +08:00
$filter['sql'] .= ' not in ('.join( ',', $valueList ).')';
break;
2016-05-12 22:17:41 +08:00
}
$filter['query'] .= $querySep.urlencode("filter[terms][$i][op]").'='.urlencode($filter['terms'][$i]['op']);
2016-05-12 22:17:41 +08:00
$filter['fields'] .= "<input type=\"hidden\" name=\"filter[terms][$i][op]\" value=\"".htmlspecialchars($filter['terms'][$i]['op'])."\"/>\n";
$filter['query'] .= $querySep.urlencode("filter[terms][$i][val]").'='.urlencode($filter['terms'][$i]['val']);
2016-05-12 22:17:41 +08:00
$filter['fields'] .= "<input type=\"hidden\" name=\"filter[terms][$i][val]\" value=\"".htmlspecialchars($filter['terms'][$i]['val'])."\"/>\n";
}
if ( isset($filter['terms'][$i]['cbr']) ) {
$filter['query'] .= $querySep.urlencode("filter[terms][$i][cbr]").'='.urlencode($filter['terms'][$i]['cbr']);
2017-05-30 21:10:41 +08:00
$filter['sql'] .= ' '.str_repeat( ')', $filter['terms'][$i]['cbr'] ).' ';
2016-05-12 22:17:41 +08:00
$filter['fields'] .= "<input type=\"hidden\" name=\"filter[terms][$i][cbr]\" value=\"".htmlspecialchars($filter['terms'][$i]['cbr'])."\"/>\n";
}
}
2016-05-12 22:17:41 +08:00
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;
}
2016-05-12 22:17:41 +08:00
}
}
2016-05-12 22:17:41 +08:00
function addFilterTerm( $filter, $position, $term=false ) {
if ( $position < 0 )
$position = 0;
elseif( $position > count($filter['terms']) )
$position = count($filter['terms']);
if ( $term && $position == 0 )
unset( $term['cnj'] );
array_splice( $filter['terms'], $position, 0, array( $term?$term:array() ) );
2016-05-12 22:17:41 +08:00
return( $filter );
}
2016-05-12 22:17:41 +08:00
function delFilterTerm( $filter, $position ) {
if ( $position < 0 )
$position = 0;
elseif( $position >= count($filter['terms']) )
$position = count($filter['terms']);
array_splice( $filter['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));
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 );
foreach ( $newPages as $newPage ) {
$pageText .= '<a href="?view='.$view.$querySep.'page='.$newPage.$query.'">'.$newPage.'</a>&nbsp;';
}
}
$pageText .= '-&nbsp;'.$page.'&nbsp;-';
if ( $page < $pages ) {
$newPages = array();
$pagesUsed = array();
$hi_exp = max(2,log($pages-$page)/log($maxShortcuts));
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 );
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>';
if ( false && $page < ($pages-1) ) {
$pageText .= '<a href="?view='.$view.$querySep.'page='.$pages.$query.'">&gt;&gt;</a>';
}
2016-05-12 22:17:41 +08:00
}
}
2016-05-12 22:17:41 +08:00
}
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'] )
2017-05-30 21:10:41 +08:00
return( '(^)' );
2016-05-12 22:17:41 +08:00
else
2017-05-30 21:10:41 +08:00
return( '(v)' );
2016-05-12 22:17:41 +08:00
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 ) {
2017-05-30 21:10:41 +08:00
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 ) {
2017-05-30 21:10:41 +08:00
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() {
$df = shell_exec( 'df '.escapeshellarg(ZM_DIR_EVENTS) );
2016-05-12 22:17:41 +08:00
$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
}
}
2016-05-12 22:17:41 +08:00
function verNum( $version ) {
2017-05-30 21:10:41 +08:00
$vNum = '';
2016-05-12 22:17:41 +08:00
$maxFields = 3;
2017-05-30 21:10:41 +08:00
$vFields = explode( '.', $version );
2016-05-12 22:17:41 +08:00
array_splice( $vFields, $maxFields );
while ( count($vFields) < $maxFields ) {
$vFields[] = 0;
}
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;
}
2016-05-12 22:17:41 +08:00
$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'] ) );
}
2016-05-12 22:17:41 +08:00
$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;
2016-05-12 22:17:41 +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-05-12 22:17:41 +08:00
// Checking if bounding boxes intersect
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>';
2016-05-12 22:17:41 +08:00
return( false );
}
2016-05-12 22:17:41 +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 ) {
$m1 = $dy1/$dx1;
$b1 = $line1[0]['y'] - ($m1 * $line1[0]['x']);
} else {
$b1 = $line1[0]['y'];
}
if ( $dx2 ) {
$m2 = $dy2/$dx2;
$b2 = $line2[0]['y'] - ($m2 * $line2[0]['x']);
} else {
$b2 = $line2[0]['y'];
}
if ( $dx1 && $dx2 ) { // Both not vertical
if ( $m1 != $m2 ) { // Not parallel or colinear
$x = ( $b2 - $b1 ) / ( $m1 - $m2 );
if ( $x >= $min_x1 && $x <= $max_x1 && $x >= $min_x2 && $x <= $max_x2 ) {
if ( $debug ) echo "Intersecting, at x $x<br>";
return( true );
} else {
if ( $debug ) echo "Not intersecting, out of range at x $x<br>";
return( false );
}
} 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>';
2016-05-12 22:17:41 +08:00
return( true );
} else {
// Parallel
2017-05-30 21:10:41 +08:00
if ( $debug ) echo 'Not intersecting, parallel<br>';
2016-05-12 22:17:41 +08:00
return( false );
}
} elseif ( !$dx1 ) { // Line 1 is vertical
$y = ( $m2 * $line1[0]['x'] ) * $b2;
if ( $y >= $min_y1 && $y <= $max_y1 ) {
if ( $debug ) echo "Intersecting, at y $y<br>";
return( true );
} else {
if ( $debug ) echo "Not intersecting, out of range at y $y<br>";
return( false );
}
} elseif ( !$dx2 ) { // Line 2 is vertical
$y = ( $m1 * $line2[0]['x'] ) * $b1;
if ( $y >= $min_y2 && $y <= $max_y2 ) {
if ( $debug ) echo "Intersecting, at y $y<br>";
return( true );
} else {
if ( $debug ) echo "Not intersecting, out of range at y $y<br>";
return( false );
}
} else { // Both lines are vertical
if ( $line1[0]['x'] == $line2[0]['x'] ) {
// Colinear, must overlap due to box check, intersect?
2017-05-30 21:10:41 +08:00
if ( $debug ) echo 'Intersecting, vertical, colinear<br>';
2016-05-12 22:17:41 +08:00
return( true );
} else {
// Parallel
2017-05-30 21:10:41 +08:00
if ( $debug ) echo 'Not intersecting, vertical, parallel<br>';
2016-05-12 22:17:41 +08:00
return( false );
}
2016-05-12 22:17:41 +08:00
}
2017-05-30 21:10:41 +08:00
if ( $debug ) echo 'Whoops, unexpected scenario<br>';
2016-05-12 22:17:41 +08:00
return( false );
}
2016-05-12 22:17:41 +08:00
function isSelfIntersecting( $points ) {
global $debug;
2016-05-12 22:17:41 +08:00
$n_coords = count($points);
$edges = array();
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>";
if ( linesIntersect( $edges[$i], $edges[$j] ) ) {
if ( $debug ) echo "Lines $i and $j intersect<br>";
return( true );
}
}
2016-05-12 22:17:41 +08:00
}
return( false );
}
function getPolyCentre( $points, $area=0 ) {
$cx = 0.0;
$cy = 0.0;
if ( !$area )
$area = getPolyArea( $points );
for ( $i = 0, $j = count($points)-1; $i < count($points); $j = $i++ ) {
$ct = ($points[$i]['x'] * $points[$j]['y']) - ($points[$j]['x'] * $points[$i]['y']);
$cx += ($points[$i]['x'] + $points[$j]['x']) * ct;
$cy += ($points[$i]['y'] + $points[$j]['y']) * ct;
}
$cx = intval(round(abs($cx/(6.0*$area))));
$cy = intval(round(abs($cy/(6.0*$area))));
printf( "X:%cx, Y:$cy<br>" );
return( array( 'x'=>$cx, 'y'=>$cy ) );
}
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;
2016-05-12 22:17:41 +08:00
$n_coords = count($points);
$global_edges = array();
for ( $j = 0, $i = $n_coords-1; $j < $n_coords; $i = $j++ ) {
$x1 = $points[$i]['x'];
$x2 = $points[$j]['x'];
$y1 = $points[$i]['y'];
$y2 = $points[$j]['y'];
//printf( "x1:%d,y1:%d x2:%d,y2:%d\n", x1, y1, x2, y2 );
if ( $y1 == $y2 )
continue;
2016-05-12 22:17:41 +08:00
$dx = $x2 - $x1;
$dy = $y2 - $y1;
2016-05-12 22:17:41 +08:00
$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,
);
2016-05-12 22:17:41 +08:00
}
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'] );
}
2016-05-12 22:17:41 +08:00
}
2016-05-12 22:17:41 +08:00
$area = 0.0;
$active_edges = array();
$y = $global_edges[0]['min_y'];
do {
for ( $i = 0; $i < count($global_edges); $i++ ) {
if ( $global_edges[$i]['min_y'] == $y ) {
2017-05-30 21:10:41 +08:00
if ( $debug ) printf( 'Moving global edge<br>' );
2016-05-12 22:17:41 +08:00
$active_edges[] = $global_edges[$i];
array_splice( $global_edges, $i, 1 );
$i--;
} 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'] );
2016-05-12 22:17:41 +08:00
}
}
$last_x = 0;
$row_area = 0;
$parity = false;
for ( $i = 0; $i < count($active_edges); $i++ ) {
$x = intval(round($active_edges[$i]['min_x']));
if ( $parity ) {
$row_area += ($x - $last_x)+1;
$area += $row_area;
}
if ( $active_edges[$i]['max_y'] != $y )
$parity = !$parity;
$last_x = $x;
}
2017-05-30 21:10:41 +08:00
if ( $debug ) printf( '%d: Area:%d<br>', $y, $row_area );
2016-05-12 22:17:41 +08:00
$y++;
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>' );
2016-05-12 22:17:41 +08:00
array_splice( $active_edges, $i, 1 );
$i--;
} else {
$active_edges[$i]['min_x'] += $active_edges[$i]['_1_m'];
}
}
} while ( count($global_edges) || count($active_edges) );
2017-05-30 21:10:41 +08:00
if ( $debug ) printf( 'Area:%d<br>', $area );
2016-05-12 22:17:41 +08:00
return( $area );
}
function getPolyAreaOld( $points ) {
$area = 0.0;
$edge = 0.0;
for ( $i = 0, $j = count($points)-1; $i < count($points); $j = $i++ ) {
$x_diff = ($points[$i]['x'] - $points[$j]['x']);
$y_diff = ($points[$i]['y'] - $points[$j]['y']);
$y_sum = ($points[$i]['y'] + $points[$j]['y']);
$trap_edge = sqrt(pow(abs($x_diff)+1,2) + pow(abs($y_diff)+1,2) );
$edge += $trap_edge;
$trap_area = ($x_diff * $y_sum );
$area += $trap_area;
printf( "%d->%d, %d-%d=%.2f, %d+%d=%.2f(%.2f), %.2f, %.2f<br>", i, j, $points[$i]['x'], $points[$j]['x'], $x_diff, $points[$i]['y'], $points[$j]['y'], $y_sum, $y_diff, $trap_area, $trap_edge );
}
$edge = intval(round(abs($edge)));
$area = intval(round((abs($area)+$edge)/2));
echo "E:$edge<br>";
echo "A:$area<br>";
return( $area );
}
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();
if ( preg_match_all( '/(\d+,\d+)+/', $coords, $matches ) ) {
for ( $i = 0; $i < count($matches[1]); $i++ ) {
if ( preg_match( '/(\d+),(\d+)/', $matches[1][$i], $cmatches ) ) {
$points[] = array( 'x'=>$cmatches[1], 'y'=>$cmatches[2] );
} else {
echo( "Bogus coordinates '".$matches[$i]."'" );
return( false );
2016-05-12 22:17:41 +08:00
}
}
2016-05-12 22:17:41 +08:00
} else {
echo( "Bogus coordinate string '$coords'" );
return( false );
}
return( $points );
}
2017-05-30 23:15:00 +08:00
function limitPoints( &$points, $min_x, $min_y, $max_x, $max_y ) {
foreach ( $points as &$point ) {
if ( $point['x'] < $min_x ) {
Logger::Debug('Limiting point x'.$point['x'].' to min_x ' . $min_x );
$point['x'] = $min_x;
} else if ( $point['x'] > $max_x ) {
Logger::Debug('Limiting point x'.$point['x'].' to max_x ' . $max_x );
$point['x'] = $max_x;
}
if ( $point['y'] < $min_y ) {
Logger::Debug('Limiting point y'.$point['y'].' to min_y ' . $min_y );
$point['y'] = $min_y;
} else if ( $point['y'] > $max_y ) {
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 ) {
2016-05-12 22:17:41 +08:00
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;
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;
}
2016-05-12 22:17:41 +08:00
}
$names = array();
foreach ( preg_split( '/\s*,\s*/', $ids ) as $id ) {
if ( visibleMonitor( $id ) ) {
if ( isset($mITN_monitors[$id]) ) {
$names[] = $mITN_monitors[$id]['Name'];
}
}
2016-05-12 22:17:41 +08:00
}
$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 );
} 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
}
}
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';
2016-05-12 22:17:41 +08:00
return( $status );
}
2016-05-12 22:17:41 +08:00
function setDeviceStatusX10( $key, $status ) {
$socket = socket_create( AF_UNIX, SOCK_STREAM, 0 );
if ( $socket < 0 ) {
2017-05-30 21:10:41 +08:00
Fatal( 'socket_create() failed: '.socket_strerror($socket) );
2016-05-12 22:17:41 +08:00
}
$sock_file = ZM_PATH_SOCKS.'/zmx10.sock';
if ( @socket_connect( $socket, $sock_file ) ) {
$command = "$status;$key";
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 );
} 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';
2016-05-12 22:17:41 +08:00
return( $status );
}
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 ),
);
$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 );
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';
}
2016-05-12 22:17:41 +08:00
}
return( $state );
}
2016-05-12 22:17:41 +08:00
function isVector ( &$array ) {
$next_key = 0;
foreach ( array_keys($array) as $key ) {
if ( !is_int( $key ) )
return( false );
if ( $key != $next_key++ )
return( false );
}
return( true );
}
function checkJsonError($value) {
if ( function_exists('json_last_error') ) {
$value = var_export($value,true);
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
}
}
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) ).']' );
else {
$result = '{';
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 );
}
2016-05-12 22:17:41 +08:00
$comment = false;
$unescape = false;
$out = '$result=';
for ( $i = 0; $i < strlen($value); $i++ ) {
if ( !$comment ) {
if ( ($value[$i] == '{') || ($value[$i] == '[') ) {
$out .= ' array(';
} 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;
}
2016-05-12 22:17:41 +08:00
if ( $value[$i] == '"' ) {
$comment = !$comment;
}
2016-05-12 22:17:41 +08:00
}
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();
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 ) );
2016-05-12 22:17:41 +08:00
}
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' );
if ( is_array( $result ) )
$response = array_merge( $response, $result );
elseif ( !empty($result) )
$response['message'] = $result;
2017-05-30 21:10:41 +08:00
header( 'Content-type: text/plain' );
2016-05-12 22:17:41 +08:00
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 getSkinFile( $file ) {
global $skinBase;
$skinFile = false;
foreach ( $skinBase as $skin ) {
$tempSkinFile = detaintPath( 'skins'.'/'.$skin.'/'.$file );
if ( file_exists( $tempSkinFile ) )
$skinFile = $tempSkinFile;
}
return( $skinFile );
}
2017-07-06 22:50:56 +08:00
function getSkinIncludes( $file, $includeBase=false, $asOverride=false ) {
2016-05-12 22:17:41 +08:00
global $skinBase;
$skinFile = false;
foreach ( $skinBase as $skin ) {
$tempSkinFile = detaintPath( 'skins'.'/'.$skin.'/'.$file );
if ( file_exists( $tempSkinFile ) )
$skinFile = $tempSkinFile;
}
$includeFiles = array();
if ( $asOverride ) {
if ( $skinFile )
$includeFiles[] = $skinFile;
else if ( $includeBase )
$includeFiles[] = $file;
} else {
if ( $includeBase )
$includeFiles[] = $file;
if ( $skinFile )
$includeFiles[] = $skinFile;
}
return( $includeFiles );
}
2017-05-30 21:10:41 +08:00
function requestVar( $name, $default='' ) {
2016-05-12 22:17:41 +08:00
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 ) );
}
2017-05-30 23:15:00 +08:00
function getStreamHTML( $monitor, $options = array() ) {
if ( isset($options['scale']) ) {
$options['width'] = reScale( $monitor->Width(), $options['scale'] );
$options['height'] = reScale( $monitor->Height(), $options['scale'] );
}
if ( ! isset($options['mode'] ) ) {
$options['mode'] = 'stream';
}
$options['maxfps'] = ZM_WEB_VIDEO_MAXFPS;
if ( $monitor->StreamReplayBuffer() )
$options['buffer'] = $monitor->StreamReplayBuffer();
//FIXME, the width and height of the image need to be scaled.
if ( ZM_WEB_STREAM_METHOD == 'mpeg' && ZM_MPEG_LIVE_FORMAT ) {
2017-05-30 23:57:20 +08:00
$streamSrc = $monitor->getStreamSrc( array( 'mode'=>'mpeg', 'scale'=>$options['scale'], 'bitrate'=>ZM_WEB_VIDEO_BITRATE, 'maxfps'=>ZM_WEB_VIDEO_MAXFPS, 'format'=>ZM_MPEG_LIVE_FORMAT ) );
2017-05-30 23:15:00 +08:00
return getVideoStream( 'liveStream'.$monitor->Id(), $streamSrc, $options, ZM_MPEG_LIVE_FORMAT, $monitor->Name() );
} else if ( $options['mode'] == 'stream' and canStream() ) {
$options['mode'] = 'jpeg';
$streamSrc = $monitor->getStreamSrc( $options );
if ( canStreamNative() )
return getImageStream( 'liveStream'.$monitor->Id(), $streamSrc,
( isset($options['width']) ? $options['width'] : NULL ),
( isset($options['height']) ? $options['height'] : NULL ),
$monitor->Name()
);
elseif ( canStreamApplet() )
return getHelperStream( 'liveStream'.$monitor->Id(), $streamSrc,
( isset($options['width']) ? $options['width'] : NULL ),
( isset($options['height']) ? $options['height'] : NULL ),
$monitor->Name()
);
} else {
$streamSrc = $monitor->getStreamSrc( $options );
if ( $mode == 'stream' ) {
Info( 'The system has fallen back to single jpeg mode for streaming. Consider enabling Cambozola or upgrading the client browser.' );
}
2017-05-30 23:15:00 +08:00
return getImageStill( 'liveStream'.$monitor->Id(), $streamSrc,
( isset($options['width']) ? $options['width'] : NULL ),
( isset($options['height']) ? $options['height'] : NULL ),
$monitor->Name()
);
}
} // end function getStreamHTML
2017-05-30 23:15:00 +08:00
function getStreamMode( ) {
$streamMode = '';
if ( ZM_WEB_STREAM_METHOD == 'mpeg' && ZM_MPEG_LIVE_FORMAT ) {
$streamMode = 'mpeg';
} elseif ( canStream() ) {
$streamMode = 'jpeg';
} else {
$streamMode = 'single';
Info( 'The system has fallen back to single jpeg mode for streaming. Consider enabling Cambozola or upgrading the client browser.' );
}
Add h264 event view functionality and new feature alarmCues (#2012) * Fix nearEventsQuery Removed dbEscape from getNearEvents previous event because it only returns 0. Now matches next. Changed getEventDefaultVideoPath function to return a web path rather than the absolute path based on ic0ns branch. Also added start times to allow for videoJS replaymode. * Unescape filters Filters need to be unescaped * Add initial values to page load * Add replay modes to videojs * rough in figuring out a frame in between bulk frames * Add alarmCues Add a graphical indication of where alarm frames happened in an event. Similar to what zmNinja shows. * Add remaining buttons to videojs Functionality for all buttons on videojs streams. FF/RW buttons show as active when they are active. * Whitespace and fix Bulkframe calcs * Fix zms events trying to generate with mp4 code ZMS events would attempt to generate frames as though they were an mp4/passthrough type because the full eventpath wasn't passed * ZMS scrub bar Move zms scrub bar to bottom of image feed. Make it simpler and more like videojs style. * Wrap event feeds properly * Fix dvrControls on watch view * Add scaleToFit Add a scaleToFit option to event view * Add navigation for videoJS streams Disables nav buttons at beginning and end of events. Handles switching from zms to videojs. If zms crashes changes next event function to reload page instead of ajax. * Add scaleToFit to watch and frame view Adds scaleToFit to watch view. Since frame view uses the watch cookie this required changes to frame view * Add transition to zoom * Change stills view to match stream Move stills slider bar to match scrub bar on streams. Allow it to resize, make it larger. Add alarmcues. * Add Stills for every event Add stills for every event. Match size to stream size * Progressbox transitions
2017-12-05 10:26:59 +08:00
return $streamMode;
2017-05-30 23:15:00 +08:00
} // 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) {
$size += is_file($each) ? filesize($each) : folderSize($each);
}
return $size;
2016-12-29 17:31:05 +08:00
} // end function folder_size
2016-07-20 05:34:01 +08:00
2017-05-30 23:15:00 +08:00
function human_filesize($bytes, $decimals = 2) {
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
2017-03-19 09:12:06 +08:00
function csrf_startup() {
csrf_conf('rewrite-js', 'includes/csrf/csrf-magic.js');
}
?>