2015-09-18 03:29:36 +08:00
|
|
|
<?php
|
2019-02-22 22:19:07 +08:00
|
|
|
namespace ZM;
|
2018-05-16 01:45:47 +08:00
|
|
|
require_once('database.php');
|
|
|
|
require_once('Server.php');
|
2015-09-18 03:29:36 +08:00
|
|
|
|
2018-09-15 21:52:08 +08:00
|
|
|
$monitor_cache = array();
|
|
|
|
|
2016-06-21 00:46:23 +08:00
|
|
|
class Monitor {
|
2016-06-21 00:41:14 +08:00
|
|
|
|
2017-10-27 09:56:10 +08:00
|
|
|
private $defaults = array(
|
2018-01-29 23:27:46 +08:00
|
|
|
'Id' => null,
|
|
|
|
'Name' => '',
|
|
|
|
'ServerId' => 0,
|
2018-11-08 02:18:53 +08:00
|
|
|
'StorageId' => 0,
|
2018-07-31 22:15:26 +08:00
|
|
|
'Type' => 'Ffmpeg',
|
2018-01-29 23:27:46 +08:00
|
|
|
'Function' => 'None',
|
|
|
|
'Enabled' => 1,
|
2018-07-31 22:15:26 +08:00
|
|
|
'LinkedMonitors' => null,
|
2018-11-08 02:18:53 +08:00
|
|
|
'Triggers' => null,
|
|
|
|
'Device' => '',
|
|
|
|
'Channel' => 0,
|
|
|
|
'Format' => '0',
|
|
|
|
'V4LMultiBuffer' => null,
|
|
|
|
'V4LCapturesPerFrame' => null,
|
|
|
|
'Protocol' => null,
|
|
|
|
'Method' => '',
|
|
|
|
'Host' => null,
|
|
|
|
'Port' => '',
|
|
|
|
'SubPath' => '',
|
|
|
|
'Path' => null,
|
|
|
|
'Options' => null,
|
|
|
|
'User' => null,
|
|
|
|
'Pass' => null,
|
|
|
|
// These are NOT NULL default 0 in the db, but 0 is not a valid value. FIXME
|
2018-01-29 23:27:46 +08:00
|
|
|
'Width' => null,
|
|
|
|
'Height' => null,
|
2018-11-08 02:18:53 +08:00
|
|
|
'Colours' => 1,
|
|
|
|
'Palette' => '0',
|
2018-01-29 23:27:46 +08:00
|
|
|
'Orientation' => null,
|
2018-11-08 02:18:53 +08:00
|
|
|
'Deinterlacing' => 0,
|
|
|
|
'SaveJPEGs' => 3,
|
|
|
|
'VideoWriter' => '0',
|
|
|
|
'OutputCodec' => null,
|
|
|
|
'OutputContainer' => null,
|
|
|
|
'EncoderParameters' => null,
|
|
|
|
'RecordAudio' => 0,
|
|
|
|
'RTSPDescribe' => null,
|
|
|
|
'Brightness' => -1,
|
|
|
|
'Contrast' => -1,
|
|
|
|
'Hue' => -1,
|
|
|
|
'Colour' => -1,
|
|
|
|
'EventPrefix' => 'Event-',
|
|
|
|
'LabelFormat' => null,
|
|
|
|
'LabelX' => 0,
|
|
|
|
'LabelY' => 0,
|
|
|
|
'LabelSize' => 1,
|
|
|
|
'ImageBufferCount' => 100,
|
|
|
|
'WarmupCount' => 0,
|
|
|
|
'PreEventCount' => 0,
|
|
|
|
'PostEventCount' => 0,
|
|
|
|
'StreamReplayBuffer' => 0,
|
|
|
|
'AlarmFrameCount' => 1,
|
|
|
|
'SectionLength' => 600,
|
|
|
|
'FrameSkip' => 0,
|
2018-01-29 23:27:46 +08:00
|
|
|
'AnalysisFPSLimit' => null,
|
2019-03-05 23:55:27 +08:00
|
|
|
'AnalysisUpdateDelay' => 0,
|
2018-03-02 11:23:27 +08:00
|
|
|
'MaxFPS' => null,
|
|
|
|
'AlarmMaxFPS' => null,
|
2019-03-05 23:58:23 +08:00
|
|
|
'FPSReportInterval' => 100,
|
2019-03-06 00:01:39 +08:00
|
|
|
'RefBlendPerc' => 6,
|
2018-11-08 02:18:53 +08:00
|
|
|
'AlarmRefBlendPerc' => 6,
|
|
|
|
'Controllable' => 0,
|
|
|
|
'ControlId' => null,
|
|
|
|
'ControlDevice' => null,
|
|
|
|
'ControlAddress' => null,
|
|
|
|
'AutoStopTimeout' => null,
|
|
|
|
'TrackMotion' => 0,
|
|
|
|
'TrackDelay' => null,
|
|
|
|
'ReturnLocation' => -1,
|
|
|
|
'ReturnDelay' => null,
|
|
|
|
'DefaultRate' => 100,
|
|
|
|
'DefaultScale' => 100,
|
|
|
|
'SignalCheckPoints' => 0,
|
|
|
|
'SignalCheckColour' => '#0000BE',
|
|
|
|
'WebColour' => 'red',
|
|
|
|
'Exif' => 0,
|
|
|
|
'Sequence' => null,
|
|
|
|
'TotalEvents' => null,
|
|
|
|
'TotalEventDiskSpace' => null,
|
|
|
|
'HourEvents' => null,
|
|
|
|
'HourEventDiskSpace' => null,
|
|
|
|
'DayEvents' => null,
|
|
|
|
'DayEventDiskSpace' => null,
|
|
|
|
'WeekEvents' => null,
|
|
|
|
'WeekEventDiskSpace' => null,
|
|
|
|
'MonthEvents' => null,
|
|
|
|
'MonthEventDiskSpace' => null,
|
|
|
|
'ArchivedEvents' => null,
|
|
|
|
'ArchivedEventDiskSpace' => null,
|
|
|
|
'ZoneCount' => 0,
|
2018-07-31 22:15:26 +08:00
|
|
|
'Refresh' => null,
|
2018-09-11 00:22:55 +08:00
|
|
|
'DefaultCodec' => 'auto',
|
2018-01-29 23:27:46 +08:00
|
|
|
);
|
2018-01-29 23:53:48 +08:00
|
|
|
private $status_fields = array(
|
2018-01-29 23:27:46 +08:00
|
|
|
'AnalysisFPS' => null,
|
|
|
|
'CaptureFPS' => null,
|
2018-04-25 02:11:27 +08:00
|
|
|
'CaptureBandwidth' => null,
|
2017-10-24 08:00:59 +08:00
|
|
|
);
|
2016-06-21 00:46:23 +08:00
|
|
|
private $control_fields = array(
|
2016-06-21 00:41:14 +08:00
|
|
|
'Name' => '',
|
|
|
|
'Type' => 'Local',
|
|
|
|
'Protocol' => NULL,
|
|
|
|
'CanWake' => '0',
|
|
|
|
'CanSleep' => '0',
|
|
|
|
'CanReset' => '0',
|
|
|
|
'CanZoom' => '0',
|
|
|
|
'CanAutoZoom' => '0',
|
|
|
|
'CanZoomAbs' => '0',
|
|
|
|
'CanZoomRel' => '0',
|
|
|
|
'CanZoomCon' => '0',
|
|
|
|
'MinZoomRange' => NULL,
|
|
|
|
'MaxZoomRange' => NULL,
|
|
|
|
'MinZoomStep' => NULL,
|
|
|
|
'MaxZoomStep' => NULL,
|
|
|
|
'HasZoomSpeed' => '0',
|
|
|
|
'MinZoomSpeed' => NULL,
|
|
|
|
'MaxZoomSpeed' => NULL,
|
|
|
|
'CanFocus' => '0',
|
|
|
|
'CanAutoFocus' => '0',
|
|
|
|
'CanFocusAbs' => '0',
|
|
|
|
'CanFocusRel' => '0',
|
|
|
|
'CanFocusCon' => '0',
|
|
|
|
'MinFocusRange' => NULL,
|
|
|
|
'MaxFocusRange' => NULL,
|
|
|
|
'MinFocusStep' => NULL,
|
|
|
|
'MaxFocusStep' => NULL,
|
|
|
|
'HasFocusSpeed' => '0',
|
|
|
|
'MinFocusSpeed' => NULL,
|
|
|
|
'MaxFocusSpeed' => NULL,
|
|
|
|
'CanIris' => '0',
|
|
|
|
'CanAutoIris' => '0',
|
|
|
|
'CanIrisAbs' => '0',
|
|
|
|
'CanIrisRel' => '0',
|
|
|
|
'CanIrisCon' => '0',
|
|
|
|
'MinIrisRange' => NULL,
|
|
|
|
'MaxIrisRange' => NULL,
|
|
|
|
'MinIrisStep' => NULL,
|
|
|
|
'MaxIrisStep' => NULL,
|
|
|
|
'HasIrisSpeed' => '0',
|
|
|
|
'MinIrisSpeed' => NULL,
|
|
|
|
'MaxIrisSpeed' => NULL,
|
|
|
|
'CanGain' => '0',
|
|
|
|
'CanAutoGain' => '0',
|
|
|
|
'CanGainAbs' => '0',
|
|
|
|
'CanGainRel' => '0',
|
|
|
|
'CanGainCon' => '0',
|
|
|
|
'MinGainRange' => NULL,
|
|
|
|
'MaxGainRange' => NULL,
|
|
|
|
'MinGainStep' => NULL,
|
|
|
|
'MaxGainStep' => NULL,
|
|
|
|
'HasGainSpeed' => '0',
|
|
|
|
'MinGainSpeed' => NULL,
|
|
|
|
'MaxGainSpeed' => NULL,
|
|
|
|
'CanWhite' => '0',
|
|
|
|
'CanAutoWhite' => '0',
|
|
|
|
'CanWhiteAbs' => '0',
|
|
|
|
'CanWhiteRel' => '0',
|
|
|
|
'CanWhiteCon' => '0',
|
|
|
|
'MinWhiteRange' => NULL,
|
|
|
|
'MaxWhiteRange' => NULL,
|
|
|
|
'MinWhiteStep' => NULL,
|
|
|
|
'MaxWhiteStep' => NULL,
|
|
|
|
'HasWhiteSpeed' => '0',
|
|
|
|
'MinWhiteSpeed' => NULL,
|
|
|
|
'MaxWhiteSpeed' => NULL,
|
|
|
|
'HasPresets' => '0',
|
|
|
|
'NumPresets' => '0',
|
|
|
|
'HasHomePreset' => '0',
|
|
|
|
'CanSetPresets' => '0',
|
|
|
|
'CanMove' => '0',
|
|
|
|
'CanMoveDiag' => '0',
|
|
|
|
'CanMoveMap' => '0',
|
|
|
|
'CanMoveAbs' => '0',
|
|
|
|
'CanMoveRel' => '0',
|
|
|
|
'CanMoveCon' => '0',
|
|
|
|
'CanPan' => '0',
|
|
|
|
'MinPanRange' => NULL,
|
|
|
|
'MaxPanRange' => NULL,
|
|
|
|
'MinPanStep' => NULL,
|
|
|
|
'MaxPanStep' => NULL,
|
|
|
|
'HasPanSpeed' => '0',
|
|
|
|
'MinPanSpeed' => NULL,
|
|
|
|
'MaxPanSpeed' => NULL,
|
|
|
|
'HasTurboPan' => '0',
|
|
|
|
'TurboPanSpeed' => NULL,
|
|
|
|
'CanTilt' => '0',
|
|
|
|
'MinTiltRange' => NULL,
|
|
|
|
'MaxTiltRange' => NULL,
|
|
|
|
'MinTiltStep' => NULL,
|
|
|
|
'MaxTiltStep' => NULL,
|
|
|
|
'HasTiltSpeed' => '0',
|
|
|
|
'MinTiltSpeed' => NULL,
|
|
|
|
'MaxTiltSpeed' => NULL,
|
|
|
|
'HasTurboTilt' => '0',
|
|
|
|
'TurboTiltSpeed' => NULL,
|
|
|
|
'CanAutoScan' => '0',
|
|
|
|
'NumScanPaths' => '0',
|
|
|
|
);
|
|
|
|
|
2016-08-17 22:31:16 +08:00
|
|
|
public function __construct( $IdOrRow = NULL ) {
|
2016-05-06 03:30:24 +08:00
|
|
|
if ( $IdOrRow ) {
|
2016-08-09 22:43:51 +08:00
|
|
|
$row = NULL;
|
2018-05-16 01:45:47 +08:00
|
|
|
if ( is_integer($IdOrRow) or is_numeric($IdOrRow) ) {
|
|
|
|
$row = dbFetchOne('SELECT * FROM Monitors WHERE Id=?', NULL, array($IdOrRow));
|
2016-05-06 03:30:24 +08:00
|
|
|
if ( ! $row ) {
|
2018-05-16 01:45:47 +08:00
|
|
|
Error("Unable to load Monitor record for Id=" . $IdOrRow);
|
2016-05-06 03:30:24 +08:00
|
|
|
}
|
2018-05-16 01:45:47 +08:00
|
|
|
} elseif ( is_array($IdOrRow) ) {
|
2016-05-06 03:30:24 +08:00
|
|
|
$row = $IdOrRow;
|
|
|
|
} else {
|
|
|
|
Error("Unknown argument passed to Monitor Constructor ($IdOrRow)");
|
|
|
|
return;
|
|
|
|
}
|
2015-12-02 03:03:55 +08:00
|
|
|
|
2016-08-09 22:43:51 +08:00
|
|
|
if ( $row ) {
|
|
|
|
foreach ($row as $k => $v) {
|
|
|
|
$this->{$k} = $v;
|
|
|
|
}
|
|
|
|
if ( $this->{'Controllable'} ) {
|
2018-05-16 01:45:47 +08:00
|
|
|
$s = dbFetchOne('SELECT * FROM Controls WHERE Id=?', NULL, array($this->{'ControlId'}) );
|
2018-10-11 07:04:00 +08:00
|
|
|
if ( $s ) {
|
|
|
|
foreach ($s as $k => $v) {
|
|
|
|
if ( $k == 'Id' ) {
|
|
|
|
continue;
|
|
|
|
# The reason for these is that the name overlaps Monitor fields.
|
|
|
|
} else if ( $k == 'Protocol' ) {
|
|
|
|
$this->{'ControlProtocol'} = $v;
|
|
|
|
} else if ( $k == 'Name' ) {
|
|
|
|
$this->{'ControlName'} = $v;
|
|
|
|
} else if ( $k == 'Type' ) {
|
|
|
|
$this->{'ControlType'} = $v;
|
|
|
|
} else {
|
|
|
|
$this->{$k} = $v;
|
|
|
|
}
|
2016-08-09 22:43:51 +08:00
|
|
|
}
|
2018-10-11 07:04:00 +08:00
|
|
|
} else {
|
|
|
|
Warning('No Controls found for monitor '.$this->{'Id'} . ' ' . $this->{'Name'}.' althrough it is marked as controllable');
|
2016-05-06 03:30:24 +08:00
|
|
|
}
|
2015-09-18 03:29:36 +08:00
|
|
|
}
|
2018-09-15 21:52:08 +08:00
|
|
|
global $monitor_cache;
|
2018-09-20 22:07:15 +08:00
|
|
|
$monitor_cache[$row['Id']] = $this;
|
2016-05-06 03:30:24 +08:00
|
|
|
|
2016-08-09 22:43:51 +08:00
|
|
|
} else {
|
2018-05-16 01:45:47 +08:00
|
|
|
Error('No row for Monitor ' . $IdOrRow);
|
2016-08-09 22:43:51 +08:00
|
|
|
}
|
|
|
|
} # end if isset($IdOrRow)
|
2016-05-06 03:30:24 +08:00
|
|
|
} // end function __construct
|
2018-04-18 01:51:20 +08:00
|
|
|
|
2016-05-06 03:30:24 +08:00
|
|
|
public function Server() {
|
2018-05-16 01:45:47 +08:00
|
|
|
return new Server($this->{'ServerId'});
|
2016-05-06 03:30:24 +08:00
|
|
|
}
|
2017-10-27 09:56:10 +08:00
|
|
|
public function __call($fn, array $args){
|
|
|
|
if ( count($args) ) {
|
2017-06-06 03:21:27 +08:00
|
|
|
$this->{$fn} = $args[0];
|
|
|
|
}
|
2017-10-27 09:56:10 +08:00
|
|
|
if ( array_key_exists($fn, $this) ) {
|
2016-05-06 03:30:24 +08:00
|
|
|
return $this->{$fn};
|
|
|
|
#array_unshift($args, $this);
|
|
|
|
#call_user_func_array( $this->{$fn}, $args);
|
2018-04-25 21:27:43 +08:00
|
|
|
} else {
|
2017-10-27 09:56:10 +08:00
|
|
|
if ( array_key_exists($fn, $this->control_fields) ) {
|
2016-06-21 00:46:23 +08:00
|
|
|
return $this->control_fields{$fn};
|
2017-10-27 09:56:10 +08:00
|
|
|
} else if ( array_key_exists( $fn, $this->defaults ) ) {
|
|
|
|
return $this->defaults{$fn};
|
2016-06-21 00:41:14 +08:00
|
|
|
} else {
|
2016-08-22 23:28:53 +08:00
|
|
|
$backTrace = debug_backtrace();
|
|
|
|
$file = $backTrace[1]['file'];
|
|
|
|
$line = $backTrace[1]['line'];
|
|
|
|
Warning( "Unknown function call Monitor->$fn from $file:$line" );
|
2016-06-21 00:41:14 +08:00
|
|
|
}
|
2016-06-15 00:38:17 +08:00
|
|
|
}
|
2016-05-06 03:30:24 +08:00
|
|
|
}
|
2017-02-28 10:48:08 +08:00
|
|
|
|
2018-07-10 00:09:29 +08:00
|
|
|
public function getStreamSrc($args, $querySep='&') {
|
|
|
|
|
2018-11-30 03:26:30 +08:00
|
|
|
$streamSrc = $this->Server()->UrlToZMS(
|
2018-07-10 00:09:29 +08:00
|
|
|
ZM_MIN_STREAMING_PORT ?
|
|
|
|
ZM_MIN_STREAMING_PORT+$this->{'Id'} :
|
|
|
|
null);
|
2015-09-18 03:29:36 +08:00
|
|
|
|
2017-02-28 10:48:08 +08:00
|
|
|
$args['monitor'] = $this->{'Id'};
|
2015-09-18 03:29:36 +08:00
|
|
|
|
2016-05-06 03:30:24 +08:00
|
|
|
if ( ZM_OPT_USE_AUTH ) {
|
2017-02-28 10:48:08 +08:00
|
|
|
if ( ZM_AUTH_RELAY == 'hashed' ) {
|
2018-06-26 02:50:54 +08:00
|
|
|
$args['auth'] = generateAuthHash(ZM_AUTH_HASH_IPS);
|
2017-02-28 10:48:08 +08:00
|
|
|
} elseif ( ZM_AUTH_RELAY == 'plain' ) {
|
|
|
|
$args['user'] = $_SESSION['username'];
|
|
|
|
$args['pass'] = $_SESSION['password'];
|
|
|
|
} elseif ( ZM_AUTH_RELAY == 'none' ) {
|
|
|
|
$args['user'] = $_SESSION['username'];
|
2016-05-06 03:30:24 +08:00
|
|
|
}
|
|
|
|
}
|
2017-02-28 10:48:08 +08:00
|
|
|
if ( ( (!isset($args['mode'])) or ( $args['mode'] != 'single' ) ) && !empty($GLOBALS['connkey']) ) {
|
|
|
|
$args['connkey'] = $GLOBALS['connkey'];
|
2016-05-06 03:30:24 +08:00
|
|
|
}
|
|
|
|
if ( ZM_RAND_STREAM ) {
|
2017-02-28 10:48:08 +08:00
|
|
|
$args['rand'] = time();
|
2016-05-06 03:30:24 +08:00
|
|
|
}
|
2015-09-18 03:29:36 +08:00
|
|
|
|
2018-07-10 00:09:29 +08:00
|
|
|
$streamSrc .= '?'.http_build_query($args,'', $querySep);
|
2015-09-18 03:29:36 +08:00
|
|
|
|
2018-07-10 00:09:29 +08:00
|
|
|
return $streamSrc;
|
2016-05-06 03:30:24 +08:00
|
|
|
} // end function getStreamSrc
|
2016-06-15 00:38:17 +08:00
|
|
|
|
2018-05-06 00:49:00 +08:00
|
|
|
public function Width($new = null) {
|
2017-10-27 09:56:10 +08:00
|
|
|
if ( $new )
|
|
|
|
$this->{'Width'} = $new;
|
|
|
|
|
2018-05-06 00:49:00 +08:00
|
|
|
$field = ( $this->Orientation() == '90' or $this->Orientation() == '270' ) ? 'Height' : 'Width';
|
|
|
|
if ( array_key_exists($field, $this) )
|
|
|
|
return $this->{$field};
|
|
|
|
return $this->defaults{$field};
|
|
|
|
} // end function Width
|
2016-06-15 00:38:17 +08:00
|
|
|
|
2018-05-06 00:49:00 +08:00
|
|
|
public function Height($new=null) {
|
2017-10-27 09:56:10 +08:00
|
|
|
if ( $new )
|
|
|
|
$this->{'Height'} = $new;
|
2018-05-06 00:49:00 +08:00
|
|
|
|
|
|
|
$field = ( $this->Orientation() == '90' or $this->Orientation() == '270' ) ? 'Width' : 'Height';
|
|
|
|
if ( array_key_exists($field, $this) )
|
|
|
|
return $this->{$field};
|
|
|
|
return $this->defaults{$field};
|
|
|
|
} // end function Height
|
2016-06-15 00:38:17 +08:00
|
|
|
|
2019-02-10 08:41:54 +08:00
|
|
|
public function SignalCheckColour($new=null) {
|
|
|
|
$field = 'SignalCheckColour';
|
|
|
|
if ($new) {
|
|
|
|
$this->{$field} = $new;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate that it's a valid colour (we seem to allow color names, not just hex).
|
|
|
|
// This also helps prevent XSS.
|
|
|
|
if (array_key_exists($field, $this) && preg_match('/^[#0-9a-zA-Z]+$/', $this->{$field})) {
|
|
|
|
return $this->{$field};
|
|
|
|
}
|
|
|
|
return $this->defaults{$field};
|
|
|
|
} // end function SignalCheckColour
|
|
|
|
|
2018-04-25 21:27:43 +08:00
|
|
|
public function set($data) {
|
2016-05-06 03:30:24 +08:00
|
|
|
foreach ($data as $k => $v) {
|
2018-04-25 21:27:43 +08:00
|
|
|
if ( method_exists($this, $k) ) {
|
|
|
|
$this->{$k}($v);
|
2016-05-06 03:30:24 +08:00
|
|
|
} else {
|
2018-04-25 21:27:43 +08:00
|
|
|
if ( is_array( $v ) ) {
|
|
|
|
# perhaps should turn into a comma-separated string
|
|
|
|
$this->{$k} = implode(',',$v);
|
|
|
|
} else if ( is_string( $v ) ) {
|
|
|
|
$this->{$k} = trim( $v );
|
|
|
|
} else if ( is_integer( $v ) ) {
|
|
|
|
$this->{$k} = $v;
|
|
|
|
} else if ( is_bool( $v ) ) {
|
|
|
|
$this->{$k} = $v;
|
|
|
|
} else {
|
|
|
|
Error( "Unknown type $k => $v of var " . gettype( $v ) );
|
|
|
|
$this->{$k} = $v;
|
|
|
|
}
|
|
|
|
} # end if method_exists
|
|
|
|
} # end foreach $data as $k=>$v
|
2016-05-06 03:30:24 +08:00
|
|
|
}
|
2018-09-08 04:31:11 +08:00
|
|
|
public static function find( $parameters = null, $options = null ) {
|
2017-10-24 08:00:59 +08:00
|
|
|
$sql = 'SELECT * FROM Monitors ';
|
|
|
|
$values = array();
|
|
|
|
|
|
|
|
if ( $parameters ) {
|
|
|
|
$fields = array();
|
|
|
|
$sql .= 'WHERE ';
|
|
|
|
foreach ( $parameters as $field => $value ) {
|
|
|
|
if ( $value == null ) {
|
|
|
|
$fields[] = $field.' IS NULL';
|
2018-09-08 04:31:11 +08:00
|
|
|
} else if ( is_array($value) ) {
|
2017-10-24 08:00:59 +08:00
|
|
|
$func = function(){return '?';};
|
2018-09-08 04:31:11 +08:00
|
|
|
$fields[] = $field.' IN ('.implode(',', array_map($func, $value)). ')';
|
2017-10-24 08:00:59 +08:00
|
|
|
$values += $value;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$fields[] = $field.'=?';
|
|
|
|
$values[] = $value;
|
|
|
|
}
|
|
|
|
}
|
2018-09-08 04:31:11 +08:00
|
|
|
$sql .= implode(' AND ', $fields);
|
2017-10-24 08:00:59 +08:00
|
|
|
}
|
2018-09-08 04:31:11 +08:00
|
|
|
if ( $options ) {
|
|
|
|
if ( isset($options['order']) ) {
|
|
|
|
$sql .= ' ORDER BY ' . $options['order'];
|
|
|
|
}
|
|
|
|
if ( isset($options['limit']) ) {
|
|
|
|
if ( is_integer($options['limit']) or ctype_digit($options['limit']) ) {
|
2018-09-15 21:52:08 +08:00
|
|
|
$sql .= ' LIMIT ' . $options['limit'];
|
2018-09-08 04:31:11 +08:00
|
|
|
} else {
|
|
|
|
$backTrace = debug_backtrace();
|
|
|
|
$file = $backTrace[1]['file'];
|
|
|
|
$line = $backTrace[1]['line'];
|
2018-09-15 21:52:08 +08:00
|
|
|
Error("Invalid value for limit(".$options['limit'].") passed to Control::find from $file:$line");
|
2018-09-10 01:59:10 +08:00
|
|
|
return array();
|
2018-09-08 04:31:11 +08:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:00:59 +08:00
|
|
|
}
|
2018-09-08 04:31:11 +08:00
|
|
|
$monitors = array();
|
2017-10-24 08:00:59 +08:00
|
|
|
$result = dbQuery($sql, $values);
|
2018-09-15 21:52:08 +08:00
|
|
|
$results = $result->fetchALL();
|
|
|
|
foreach ( $results as $row ) {
|
|
|
|
$monitors[] = new Monitor($row);
|
2017-10-24 08:00:59 +08:00
|
|
|
}
|
2018-09-08 04:31:11 +08:00
|
|
|
return $monitors;
|
|
|
|
} # end find
|
|
|
|
|
|
|
|
public static function find_one( $parameters = array() ) {
|
2018-09-15 21:52:08 +08:00
|
|
|
global $monitor_cache;
|
|
|
|
if (
|
|
|
|
( count($parameters) == 1 ) and
|
|
|
|
isset($parameters['Id']) and
|
|
|
|
isset($monitor_cache[$parameters['Id']]) ) {
|
|
|
|
return $monitor_cache[$parameters['Id']];
|
|
|
|
}
|
2018-09-08 04:31:11 +08:00
|
|
|
$results = Monitor::find( $parameters, array('limit'=>1) );
|
|
|
|
if ( ! sizeof($results) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return $results[0];
|
|
|
|
} # end find_one
|
2017-10-24 08:00:59 +08:00
|
|
|
|
2018-04-25 21:31:40 +08:00
|
|
|
public function save($new_values = null) {
|
2017-10-24 08:00:59 +08:00
|
|
|
|
|
|
|
if ( $new_values ) {
|
|
|
|
foreach ( $new_values as $k=>$v ) {
|
|
|
|
$this->{$k} = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 21:31:40 +08:00
|
|
|
$fields = array_keys($this->defaults);
|
2017-12-22 23:12:34 +08:00
|
|
|
|
2018-04-25 21:31:40 +08:00
|
|
|
$sql = 'UPDATE Monitors SET '.implode(', ', array_map(function($field) {return $field.'=?';}, $fields )) . ' WHERE Id=?';
|
|
|
|
$values = array_map(function($field){return $this->{$field};}, $fields);
|
2017-10-24 08:00:59 +08:00
|
|
|
$values[] = $this->{'Id'};
|
2018-04-25 21:31:40 +08:00
|
|
|
dbQuery($sql, $values);
|
2017-10-24 08:00:59 +08:00
|
|
|
} // end function save
|
2017-11-06 00:54:00 +08:00
|
|
|
|
2017-10-24 08:00:59 +08:00
|
|
|
function zmcControl( $mode=false ) {
|
2018-01-30 02:40:50 +08:00
|
|
|
if ( (!defined('ZM_SERVER_ID')) or ( array_key_exists('ServerId', $this) and (ZM_SERVER_ID==$this->{'ServerId'}) ) ) {
|
2017-10-24 08:00:59 +08:00
|
|
|
if ( $this->{'Type'} == 'Local' ) {
|
|
|
|
$zmcArgs = '-d '.$this->{'Device'};
|
|
|
|
} else {
|
|
|
|
$zmcArgs = '-m '.$this->{'Id'};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $mode == 'stop' ) {
|
2018-06-12 04:34:23 +08:00
|
|
|
daemonControl('stop', 'zmc', $zmcArgs);
|
2017-10-24 08:00:59 +08:00
|
|
|
} else {
|
|
|
|
if ( $mode == 'restart' ) {
|
2018-06-12 04:34:23 +08:00
|
|
|
daemonControl('stop', 'zmc', $zmcArgs);
|
2017-10-24 08:00:59 +08:00
|
|
|
}
|
2018-01-19 04:38:48 +08:00
|
|
|
if ( $this->{'Function'} != 'None' ) {
|
2018-06-12 04:34:23 +08:00
|
|
|
daemonControl('start', 'zmc', $zmcArgs);
|
2018-01-19 04:38:48 +08:00
|
|
|
}
|
2017-10-24 08:00:59 +08:00
|
|
|
}
|
2018-01-29 23:27:46 +08:00
|
|
|
} else if ( $this->ServerId() ) {
|
2017-10-24 08:00:59 +08:00
|
|
|
$Server = $this->Server();
|
|
|
|
|
2018-12-11 23:21:22 +08:00
|
|
|
$url = $Server->UrlToApi().'/monitors/daemonControl/'.$this->{'Id'}.'/'.$mode.'/zmc.json';
|
2017-10-24 08:00:59 +08:00
|
|
|
if ( ZM_OPT_USE_AUTH ) {
|
|
|
|
if ( ZM_AUTH_RELAY == 'hashed' ) {
|
2018-04-05 23:24:47 +08:00
|
|
|
$url .= '?auth='.generateAuthHash( ZM_AUTH_HASH_IPS );
|
2017-10-24 08:00:59 +08:00
|
|
|
} elseif ( ZM_AUTH_RELAY == 'plain' ) {
|
2018-04-05 23:24:47 +08:00
|
|
|
$url = '?user='.$_SESSION['username'];
|
|
|
|
$url = '?pass='.$_SESSION['password'];
|
2017-10-24 08:00:59 +08:00
|
|
|
} elseif ( ZM_AUTH_RELAY == 'none' ) {
|
2018-04-05 23:24:47 +08:00
|
|
|
$url = '?user='.$_SESSION['username'];
|
2017-10-24 08:00:59 +08:00
|
|
|
}
|
|
|
|
}
|
2018-04-11 04:05:37 +08:00
|
|
|
Logger::Debug("sending command to $url");
|
2018-11-15 02:00:19 +08:00
|
|
|
|
|
|
|
$context = stream_context_create();
|
2018-01-19 04:38:48 +08:00
|
|
|
try {
|
|
|
|
$result = file_get_contents($url, false, $context);
|
|
|
|
if ($result === FALSE) { /* Handle error */
|
|
|
|
Error("Error restarting zmc using $url");
|
|
|
|
}
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
Error("Except $e thrown trying to restart zmc");
|
|
|
|
}
|
2018-06-12 04:34:23 +08:00
|
|
|
} else {
|
|
|
|
Error("Server not assigned to Monitor in a multi-server setup. Please assign a server to the Monitor.");
|
2017-10-24 08:00:59 +08:00
|
|
|
}
|
|
|
|
} // end function zmcControl
|
2018-01-19 04:38:48 +08:00
|
|
|
|
2017-10-24 08:00:59 +08:00
|
|
|
function zmaControl( $mode=false ) {
|
2018-01-30 02:40:50 +08:00
|
|
|
if ( (!defined('ZM_SERVER_ID')) or ( array_key_exists('ServerId', $this) and (ZM_SERVER_ID==$this->{'ServerId'}) ) ) {
|
2017-10-24 08:00:59 +08:00
|
|
|
if ( $this->{'Function'} == 'None' || $this->{'Function'} == 'Monitor' || $mode == 'stop' ) {
|
|
|
|
if ( ZM_OPT_CONTROL ) {
|
2018-06-12 04:34:23 +08:00
|
|
|
daemonControl('stop', 'zmtrack.pl', '-m '.$this->{'Id'});
|
2017-10-24 08:00:59 +08:00
|
|
|
}
|
2018-06-12 04:34:23 +08:00
|
|
|
daemonControl('stop', 'zma', '-m '.$this->{'Id'});
|
2017-10-24 08:00:59 +08:00
|
|
|
} else {
|
|
|
|
if ( $mode == 'restart' ) {
|
|
|
|
if ( ZM_OPT_CONTROL ) {
|
|
|
|
daemonControl( 'stop', 'zmtrack.pl', '-m '.$this->{'Id'} );
|
|
|
|
}
|
|
|
|
daemonControl( 'stop', 'zma', '-m '.$this->{'Id'} );
|
|
|
|
}
|
|
|
|
daemonControl( 'start', 'zma', '-m '.$this->{'Id'} );
|
|
|
|
if ( ZM_OPT_CONTROL && $this->{'Controllable'} && $this->{'TrackMotion'} && ( $this->{'Function'} == 'Modect' || $this->{'Function'} == 'Mocord' ) ) {
|
|
|
|
daemonControl( 'start', 'zmtrack.pl', '-m '.$this->{'Id'} );
|
|
|
|
}
|
|
|
|
if ( $mode == 'reload' ) {
|
|
|
|
daemonControl( 'reload', 'zma', '-m '.$this->{'Id'} );
|
|
|
|
}
|
|
|
|
}
|
2018-11-15 02:00:19 +08:00
|
|
|
} else if ( $this->ServerId() ) {
|
|
|
|
$Server = $this->Server();
|
|
|
|
|
|
|
|
$url = ZM_BASE_PROTOCOL . '://'.$Server->Hostname().'/zm/api/monitors/daemonControl/'.$this->{'Id'}.'/'.$mode.'/zma.json';
|
|
|
|
if ( ZM_OPT_USE_AUTH ) {
|
|
|
|
if ( ZM_AUTH_RELAY == 'hashed' ) {
|
|
|
|
$url .= '?auth='.generateAuthHash( ZM_AUTH_HASH_IPS );
|
|
|
|
} elseif ( ZM_AUTH_RELAY == 'plain' ) {
|
|
|
|
$url = '?user='.$_SESSION['username'];
|
|
|
|
$url = '?pass='.$_SESSION['password'];
|
|
|
|
} elseif ( ZM_AUTH_RELAY == 'none' ) {
|
|
|
|
$url = '?user='.$_SESSION['username'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Logger::Debug("sending command to $url");
|
|
|
|
|
|
|
|
$context = stream_context_create();
|
|
|
|
try {
|
|
|
|
$result = file_get_contents($url, false, $context);
|
|
|
|
if ($result === FALSE) { /* Handle error */
|
|
|
|
Error("Error restarting zma using $url");
|
|
|
|
}
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
Error("Except $e thrown trying to restart zma");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Error("Server not assigned to Monitor in a multi-server setup. Please assign a server to the Monitor.");
|
2017-10-24 08:00:59 +08:00
|
|
|
} // end if we are on the recording server
|
2018-06-12 04:34:23 +08:00
|
|
|
} // end public function zmaControl
|
|
|
|
|
2018-04-25 21:27:43 +08:00
|
|
|
public function GroupIds( $new='') {
|
|
|
|
if ( $new != '' ) {
|
|
|
|
if(!is_array($new)) {
|
|
|
|
$this->{'GroupIds'} = array($new);
|
|
|
|
} else {
|
|
|
|
$this->{'GroupIds'} = $new;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 03:19:20 +08:00
|
|
|
if ( !array_key_exists('GroupIds', $this) ) {
|
|
|
|
if ( array_key_exists('Id', $this) and $this->{'Id'} ) {
|
2018-04-25 21:27:43 +08:00
|
|
|
$this->{'GroupIds'} = dbFetchAll('SELECT GroupId FROM Groups_Monitors WHERE MonitorId=?', 'GroupId', array($this->{'Id'}) );
|
|
|
|
if ( ! $this->{'GroupIds'} )
|
|
|
|
$this->{'GroupIds'} = array();
|
2018-02-28 03:19:20 +08:00
|
|
|
} else {
|
2018-03-24 04:34:46 +08:00
|
|
|
$this->{'GroupIds'} = array();
|
2018-02-28 03:19:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->{'GroupIds'};
|
|
|
|
}
|
2018-03-21 03:18:07 +08:00
|
|
|
public function delete() {
|
|
|
|
$this->zmaControl('stop');
|
|
|
|
$this->zmcControl('stop');
|
|
|
|
|
|
|
|
// If fast deletes are on, then zmaudit will clean everything else up later
|
|
|
|
// If fast deletes are off and there are lots of events then this step may
|
|
|
|
// well time out before completing, in which case zmaudit will still tidy up
|
|
|
|
if ( !ZM_OPT_FAST_DELETE ) {
|
2018-03-22 02:32:54 +08:00
|
|
|
$markEids = dbFetchAll('SELECT Id FROM Events WHERE MonitorId=?', 'Id', array($this->{'Id'}));
|
2018-03-21 03:18:07 +08:00
|
|
|
foreach($markEids as $markEid)
|
2018-03-22 02:32:54 +08:00
|
|
|
deleteEvent($markEid);
|
2018-03-21 03:18:07 +08:00
|
|
|
|
|
|
|
deletePath(ZM_DIR_EVENTS.'/'.basename($this->{'Name'}));
|
|
|
|
deletePath(ZM_DIR_EVENTS.'/'.$this->{'Id'});
|
|
|
|
$Storage = $this->Storage();
|
|
|
|
if ( $Storage->Path() != ZM_DIR_EVENTS ) {
|
2018-03-22 02:32:54 +08:00
|
|
|
deletePath($Storage->Path().'/'.basename($this->{'Name'}));
|
2018-03-21 03:18:07 +08:00
|
|
|
deletePath($Storage->Path().'/'.$this->{'Id'});
|
|
|
|
}
|
|
|
|
} // end if ZM_OPT_FAST_DELETE
|
|
|
|
|
|
|
|
// This is the important stuff
|
|
|
|
dbQuery('DELETE FROM Zones WHERE MonitorId = ?', array($this->{'Id'}));
|
|
|
|
if ( ZM_OPT_X10 )
|
|
|
|
dbQuery('DELETE FROM TriggersX10 WHERE MonitorId=?', array($this->{'Id'}));
|
|
|
|
dbQuery('DELETE FROM Monitors WHERE Id = ?', array($this->{'Id'}));
|
|
|
|
|
|
|
|
// Deleting a Monitor does not affect the order, just creates a gap in the sequence. Who cares?
|
|
|
|
// fixSequences();
|
|
|
|
|
|
|
|
} // end function delete
|
2018-03-22 02:32:54 +08:00
|
|
|
|
|
|
|
public function Storage( $new = null ) {
|
|
|
|
if ( $new ) {
|
|
|
|
$this->{'Storage'} = $new;
|
|
|
|
}
|
|
|
|
if ( ! ( array_key_exists('Storage', $this) and $this->{'Storage'} ) ) {
|
2018-04-11 04:05:37 +08:00
|
|
|
$this->{'Storage'} = isset($this->{'StorageId'}) ?
|
|
|
|
Storage::find_one(array('Id'=>$this->{'StorageId'})) :
|
|
|
|
new Storage(NULL);
|
2018-05-24 21:54:45 +08:00
|
|
|
if ( ! $this->{'Storage'} )
|
|
|
|
$this->{'Storage'} = new Storage(NULL);
|
2018-03-22 02:32:54 +08:00
|
|
|
}
|
|
|
|
return $this->{'Storage'};
|
|
|
|
}
|
2018-05-04 21:46:46 +08:00
|
|
|
|
|
|
|
public function Source( ) {
|
|
|
|
$source = '';
|
|
|
|
if ( $this->{'Type'} == 'Local' ) {
|
|
|
|
$source = $this->{'Device'}.' ('.$this->{'Channel'}.')';
|
|
|
|
} elseif ( $this->{'Type'} == 'Remote' ) {
|
|
|
|
$source = preg_replace( '/^.*@/', '', $this->{'Host'} );
|
|
|
|
if ( $this->{'Port'} != '80' and $this->{'Port'} != '554' ) {
|
|
|
|
$source .= ':'.$this->{'Port'};
|
|
|
|
}
|
|
|
|
} elseif ( $this->{'Type'} == 'File' || $this->{'Type'} == 'cURL' ) {
|
|
|
|
$source = preg_replace( '/^.*\//', '', $this->{'Path'} );
|
|
|
|
} elseif ( $this->{'Type'} == 'Ffmpeg' || $this->{'Type'} == 'Libvlc' || $this->{'Type'} == 'WebSite' ) {
|
|
|
|
$url_parts = parse_url( $this->{'Path'} );
|
2018-11-22 23:04:33 +08:00
|
|
|
if ( ZM_WEB_FILTER_SOURCE == 'Hostname' ) {
|
|
|
|
# Filter out everything but the hostname
|
2018-09-15 02:26:45 +08:00
|
|
|
if ( isset($url_parts['host']) ) {
|
|
|
|
$source = $url_parts['host'];
|
|
|
|
} else {
|
|
|
|
$source = $this->{'Path'};
|
|
|
|
}
|
2018-11-22 23:04:33 +08:00
|
|
|
} elseif ( ZM_WEB_FILTER_SOURCE == "NoCredentials" ) {
|
2018-07-10 00:09:29 +08:00
|
|
|
# Filter out sensitive and common items
|
2018-06-20 23:12:43 +08:00
|
|
|
unset($url_parts['user']);
|
|
|
|
unset($url_parts['pass']);
|
|
|
|
#unset($url_parts['scheme']);
|
|
|
|
unset($url_parts['query']);
|
|
|
|
#unset($url_parts['path']);
|
|
|
|
if ( isset($url_parts['port']) and ( $url_parts['port'] == '80' or $url_parts['port'] == '554' ) )
|
|
|
|
unset($url_parts['port']);
|
|
|
|
$source = unparse_url($url_parts);
|
|
|
|
} else { # Don't filter anything
|
|
|
|
$source = $this->{'Path'};
|
|
|
|
}
|
2018-05-04 21:46:46 +08:00
|
|
|
}
|
|
|
|
if ( $source == '' ) {
|
|
|
|
$source = 'Monitor ' . $this->{'Id'};
|
|
|
|
}
|
|
|
|
return $source;
|
|
|
|
} // end function Source
|
2018-09-08 04:31:11 +08:00
|
|
|
|
2018-11-30 03:26:30 +08:00
|
|
|
public function UrlToIndex() {
|
2019-03-02 06:25:17 +08:00
|
|
|
return $this->Server()->UrlToIndex();
|
|
|
|
//ZM_MIN_STREAMING_PORT ? (ZM_MIN_STREAMING_PORT+$this->Id()) : null);
|
2018-09-15 02:26:45 +08:00
|
|
|
}
|
2018-09-08 04:31:11 +08:00
|
|
|
|
2017-10-24 08:00:59 +08:00
|
|
|
} // end class Monitor
|
2015-09-18 03:29:36 +08:00
|
|
|
?>
|