Spacing and quotes

This commit is contained in:
Isaac Connor 2021-08-18 10:50:45 -04:00
parent b9efe627f3
commit eb1124c406
1 changed files with 109 additions and 111 deletions

View File

@ -164,46 +164,46 @@ class Monitor extends ZM_Object {
); );
public function Control() { public function Control() {
if ( !property_exists($this, 'Control') ) { if (!property_exists($this, 'Control')) {
if ( $this->ControlId() ) if ($this->ControlId())
$this->{'Control'} = Control::find_one(array('Id'=>$this->{'ControlId'})); $this->{'Control'} = Control::find_one(array('Id'=>$this->{'ControlId'}));
if ( !(property_exists($this, 'Control') and $this->{'Control'}) ) if (!(property_exists($this, 'Control') and $this->{'Control'}))
$this->{'Control'} = new Control(); $this->{'Control'} = new Control();
} }
return $this->{'Control'}; return $this->{'Control'};
} }
public function Server() { public function Server() {
if ( !property_exists($this, 'Server') ) { if (!property_exists($this, 'Server')) {
if ( $this->ServerId() ) if ($this->ServerId())
$this->{'Server'} = Server::find_one(array('Id'=>$this->{'ServerId'})); $this->{'Server'} = Server::find_one(array('Id'=>$this->{'ServerId'}));
if ( !property_exists($this, 'Server') ) { if (!property_exists($this, 'Server')) {
$this->{'Server'} = new Server(); $this->{'Server'} = new Server();
} }
} }
return $this->{'Server'}; return $this->{'Server'};
} }
public function __call($fn, array $args){ public function __call($fn, array $args) {
if ( count($args) ) { if (count($args)) {
if ( is_array($this->defaults[$fn]) and $this->defaults[$fn]['type'] == 'set' ) { if (is_array($this->defaults[$fn]) and $this->defaults[$fn]['type'] == 'set') {
$this->{$fn} = is_array($args[0]) ? implode(',', $args[0]) : $args[0]; $this->{$fn} = is_array($args[0]) ? implode(',', $args[0]) : $args[0];
} else { } else {
$this->{$fn} = $args[0]; $this->{$fn} = $args[0];
} }
} }
if ( property_exists($this, $fn) ) { if (property_exists($this, $fn)) {
return $this->{$fn}; return $this->{$fn};
} else if ( array_key_exists($fn, $this->defaults) ) { } else if (array_key_exists($fn, $this->defaults)) {
if ( is_array($this->defaults[$fn]) ) { if ( is_array($this->defaults[$fn]) ) {
return $this->defaults[$fn]['default']; return $this->defaults[$fn]['default'];
} }
return $this->defaults[$fn]; return $this->defaults[$fn];
} else if ( array_key_exists($fn, $this->status_fields) ) { } else if (array_key_exists($fn, $this->status_fields)) {
$sql = 'SELECT * FROM `Monitor_Status` WHERE `MonitorId`=?'; $sql = 'SELECT * FROM `Monitor_Status` WHERE `MonitorId`=?';
$row = dbFetchOne($sql, NULL, array($this->{'Id'})); $row = dbFetchOne($sql, NULL, array($this->{'Id'}));
if ( !$row ) { if (!$row) {
Warning('Unable to load Monitor status record for Id='.$this->{'Id'}.' using '.$sql); Warning('Unable to load Monitor status record for Id='.$this->{'Id'}.' using '.$sql);
return null; return null;
} else { } else {
@ -212,10 +212,10 @@ class Monitor extends ZM_Object {
} }
} }
return $this->{$fn}; return $this->{$fn};
} else if ( array_key_exists($fn, $this->summary_fields) ) { } else if (array_key_exists($fn, $this->summary_fields)) {
$sql = 'SELECT * FROM `Event_Summaries` WHERE `MonitorId`=?'; $sql = 'SELECT * FROM `Event_Summaries` WHERE `MonitorId`=?';
$row = dbFetchOne($sql, NULL, array($this->{'Id'})); $row = dbFetchOne($sql, NULL, array($this->{'Id'}));
if ( !$row ) { if (!$row) {
Warning('Unable to load Event Summary record for Id='.$this->{'Id'}.' using '.$sql); Warning('Unable to load Event Summary record for Id='.$this->{'Id'}.' using '.$sql);
return null; return null;
} else { } else {
@ -233,7 +233,6 @@ class Monitor extends ZM_Object {
} }
public function getStreamSrc($args, $querySep='&') { public function getStreamSrc($args, $querySep='&') {
$streamSrc = $this->Server()->UrlToZMS( $streamSrc = $this->Server()->UrlToZMS(
ZM_MIN_STREAMING_PORT ? ZM_MIN_STREAMING_PORT ?
ZM_MIN_STREAMING_PORT+$this->{'Id'} : ZM_MIN_STREAMING_PORT+$this->{'Id'} :
@ -241,8 +240,8 @@ class Monitor extends ZM_Object {
$args['monitor'] = $this->{'Id'}; $args['monitor'] = $this->{'Id'};
if ( ZM_OPT_USE_AUTH ) { if (ZM_OPT_USE_AUTH) {
if ( ZM_AUTH_RELAY == 'hashed' ) { if (ZM_AUTH_RELAY == 'hashed') {
$args['auth'] = generateAuthHash(ZM_AUTH_HASH_IPS); $args['auth'] = generateAuthHash(ZM_AUTH_HASH_IPS);
} elseif ( ZM_AUTH_RELAY == 'plain' ) { } elseif ( ZM_AUTH_RELAY == 'plain' ) {
$args['user'] = $_SESSION['username']; $args['user'] = $_SESSION['username'];
@ -251,24 +250,24 @@ class Monitor extends ZM_Object {
$args['user'] = $_SESSION['username']; $args['user'] = $_SESSION['username'];
} }
} }
if ( (!isset($args['mode'])) or ( $args['mode'] != 'single' ) ) { if ((!isset($args['mode'])) or ($args['mode'] != 'single')) {
$args['connkey'] = $this->connKey(); $args['connkey'] = $this->connKey();
} }
if ( ZM_RAND_STREAM ) { if (ZM_RAND_STREAM) {
$args['rand'] = time(); $args['rand'] = time();
} }
# zms doesn't support width & height, so if no scale is set, default it # zms doesn't support width & height, so if no scale is set, default it
if ( ! isset($args['scale']) ) { if (!isset($args['scale'])) {
if ( isset($args['width']) and intval($args['width']) ) { if (isset($args['width']) and intval($args['width'])) {
$args['scale'] = intval((100*intval($args['width']))/$this->ViewWidth()); $args['scale'] = intval((100*intval($args['width']))/$this->ViewWidth());
} else if ( isset($args['height']) and intval($args['height']) ) { } else if (isset($args['height']) and intval($args['height'])) {
$args['scale'] = intval((100*intval($args['height']))/$this->ViewHeight()); $args['scale'] = intval((100*intval($args['height']))/$this->ViewHeight());
} }
} }
if ( isset($args['width']) ) if (isset($args['width']))
unset($args['width']); unset($args['width']);
if ( isset($args['height']) ) if (isset($args['height']))
unset($args['height']); unset($args['height']);
$streamSrc .= '?'.http_build_query($args, '', $querySep); $streamSrc .= '?'.http_build_query($args, '', $querySep);
@ -284,21 +283,21 @@ class Monitor extends ZM_Object {
} }
public function ViewWidth($new = null) { public function ViewWidth($new = null) {
if ( $new ) if ($new)
$this->{'Width'} = $new; $this->{'Width'} = $new;
$field = ( $this->Orientation() == 'ROTATE_90' or $this->Orientation() == 'ROTATE_270' ) ? 'Height' : 'Width'; $field = ( $this->Orientation() == 'ROTATE_90' or $this->Orientation() == 'ROTATE_270' ) ? 'Height' : 'Width';
if ( property_exists($this, $field) ) if (property_exists($this, $field))
return $this->{$field}; return $this->{$field};
return $this->defaults[$field]; return $this->defaults[$field];
} // end function Width } // end function Width
public function ViewHeight($new=null) { public function ViewHeight($new=null) {
if ( $new ) if ($new)
$this->{'Height'} = $new; $this->{'Height'} = $new;
$field = ( $this->Orientation() == 'ROTATE_90' or $this->Orientation() == 'ROTATE_270' ) ? 'Width' : 'Height'; $field = ( $this->Orientation() == 'ROTATE_90' or $this->Orientation() == 'ROTATE_270' ) ? 'Width' : 'Height';
if ( property_exists($this, $field) ) if (property_exists($this, $field))
return $this->{$field}; return $this->{$field};
return $this->defaults[$field]; return $this->defaults[$field];
} // end function Height } // end function Height
@ -311,50 +310,50 @@ class Monitor extends ZM_Object {
// Validate that it's a valid colour (we seem to allow color names, not just hex). // Validate that it's a valid colour (we seem to allow color names, not just hex).
// This also helps prevent XSS. // This also helps prevent XSS.
if ( property_exists($this, $field) && preg_match('/^[#0-9a-zA-Z]+$/', $this->{$field})) { if (property_exists($this, $field) && preg_match('/^[#0-9a-zA-Z]+$/', $this->{$field})) {
return $this->{$field}; return $this->{$field};
} }
return $this->defaults[$field]; return $this->defaults[$field];
} // end function SignalCheckColour } // end function SignalCheckColour
public static function find( $parameters = array(), $options = array() ) { public static function find($parameters = array(), $options = array()) {
return ZM_Object::_find(get_class(), $parameters, $options); return ZM_Object::_find(get_class(), $parameters, $options);
} }
public static function find_one( $parameters = array(), $options = array() ) { public static function find_one($parameters = array(), $options = array()) {
return ZM_Object::_find_one(get_class(), $parameters, $options); return ZM_Object::_find_one(get_class(), $parameters, $options);
} }
function zmcControl( $mode=false ) { function zmcControl($mode=false) {
if ( !(property_exists($this,'Id') and $this->{'Id'}) ) { if (!(property_exists($this,'Id') and $this->{'Id'})) {
Warning('Attempt to control a monitor with no Id'); Warning('Attempt to control a monitor with no Id');
return; return;
} }
if ( (!defined('ZM_SERVER_ID')) or ( property_exists($this, 'ServerId') and (ZM_SERVER_ID==$this->{'ServerId'}) ) ) { if ((!defined('ZM_SERVER_ID')) or ( property_exists($this, 'ServerId') and (ZM_SERVER_ID==$this->{'ServerId'}) )) {
if ( $this->Type() == 'Local' ) { if ($this->Type() == 'Local') {
$zmcArgs = '-d '.$this->{'Device'}; $zmcArgs = '-d '.$this->{'Device'};
} else { } else {
$zmcArgs = '-m '.$this->{'Id'}; $zmcArgs = '-m '.$this->{'Id'};
} }
if ( $mode == 'stop' ) { if ($mode == 'stop') {
daemonControl('stop', 'zmc', $zmcArgs); daemonControl('stop', 'zmc', $zmcArgs);
} else { } else {
if ( $mode == 'restart' ) { if ($mode == 'restart') {
daemonControl('stop', 'zmc', $zmcArgs); daemonControl('stop', 'zmc', $zmcArgs);
} }
if ( $this->{'Function'} != 'None' ) { if ($this->{'Function'} != 'None') {
daemonControl('start', 'zmc', $zmcArgs); daemonControl('start', 'zmc', $zmcArgs);
} }
} }
} else if ( $this->ServerId() ) { } else if ($this->ServerId()) {
$Server = $this->Server(); $Server = $this->Server();
$url = $Server->UrlToApi().'/monitors/daemonControl/'.$this->{'Id'}.'/'.$mode.'/zmc.json'; $url = $Server->UrlToApi().'/monitors/daemonControl/'.$this->{'Id'}.'/'.$mode.'/zmc.json';
if ( ZM_OPT_USE_AUTH ) { if (ZM_OPT_USE_AUTH) {
if ( ZM_AUTH_RELAY == 'hashed' ) { if (ZM_AUTH_RELAY == 'hashed') {
$url .= '?auth='.generateAuthHash(ZM_AUTH_HASH_IPS); $url .= '?auth='.generateAuthHash(ZM_AUTH_HASH_IPS);
} else if ( ZM_AUTH_RELAY == 'plain' ) { } else if (ZM_AUTH_RELAY == 'plain') {
$url .= '?user='.$_SESSION['username']; $url .= '?user='.$_SESSION['username'];
$url .= '?pass='.$_SESSION['password']; $url .= '?pass='.$_SESSION['password'];
} else { } else {
@ -367,10 +366,10 @@ class Monitor extends ZM_Object {
$context = stream_context_create(); $context = stream_context_create();
try { try {
$result = file_get_contents($url, false, $context); $result = file_get_contents($url, false, $context);
if ( $result === FALSE ) { /* Handle error */ if ($result === FALSE) { /* Handle error */
Error("Error restarting zmc using $url"); Error("Error restarting zmc using $url");
} }
} catch ( Exception $e ) { } catch (Exception $e) {
Error("Except $e thrown trying to restart zmc"); Error("Except $e thrown trying to restart zmc");
} }
} else { } else {
@ -378,19 +377,19 @@ class Monitor extends ZM_Object {
} }
} // end function zmcControl } // end function zmcControl
public function GroupIds( $new='' ) { public function GroupIds($new='') {
if ( $new != '' ) { if ($new != '') {
if ( !is_array($new) ) { if (!is_array($new)) {
$this->{'GroupIds'} = array($new); $this->{'GroupIds'} = array($new);
} else { } else {
$this->{'GroupIds'} = $new; $this->{'GroupIds'} = $new;
} }
} }
if ( !property_exists($this, 'GroupIds') ) { if (!property_exists($this, 'GroupIds')) {
if ( property_exists($this, 'Id') and $this->{'Id'} ) { if (property_exists($this, 'Id') and $this->{'Id'}) {
$this->{'GroupIds'} = dbFetchAll('SELECT `GroupId` FROM `Groups_Monitors` WHERE `MonitorId`=?', 'GroupId', array($this->{'Id'}) ); $this->{'GroupIds'} = dbFetchAll('SELECT `GroupId` FROM `Groups_Monitors` WHERE `MonitorId`=?', 'GroupId', array($this->{'Id'}));
if ( ! $this->{'GroupIds'} ) if (!$this->{'GroupIds'})
$this->{'GroupIds'} = array(); $this->{'GroupIds'} = array();
} else { } else {
$this->{'GroupIds'} = array(); $this->{'GroupIds'} = array();
@ -400,8 +399,8 @@ class Monitor extends ZM_Object {
} }
public function delete() { public function delete() {
if ( ! $this->{'Id'} ) { if (!$this->{'Id'}) {
Warning("Attempt to delete a monitor without id."); Warning('Attempt to delete a monitor without id.');
return; return;
} }
$this->zmcControl('stop'); $this->zmcControl('stop');
@ -409,17 +408,17 @@ class Monitor extends ZM_Object {
// If fast deletes are on, then zmaudit will clean everything else up later // 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 // 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 // well time out before completing, in which case zmaudit will still tidy up
if ( !ZM_OPT_FAST_DELETE ) { if (!ZM_OPT_FAST_DELETE) {
$markEids = dbFetchAll('SELECT Id FROM Events WHERE MonitorId=?', 'Id', array($this->{'Id'})); $markEids = dbFetchAll('SELECT Id FROM Events WHERE MonitorId=?', 'Id', array($this->{'Id'}));
foreach ($markEids as $markEid) foreach ($markEids as $markEid)
deleteEvent($markEid); deleteEvent($markEid);
if ( $this->{'Name'} ) if ($this->{'Name'})
deletePath(ZM_DIR_EVENTS.'/'.basename($this->{'Name'})); deletePath(ZM_DIR_EVENTS.'/'.basename($this->{'Name'}));
deletePath(ZM_DIR_EVENTS.'/'.$this->{'Id'}); deletePath(ZM_DIR_EVENTS.'/'.$this->{'Id'});
$Storage = $this->Storage(); $Storage = $this->Storage();
if ( $Storage->Path() != ZM_DIR_EVENTS ) { if ($Storage->Path() != ZM_DIR_EVENTS) {
if ( $this->{'Name'} ) if ($this->{'Name'})
deletePath($Storage->Path().'/'.basename($this->{'Name'})); deletePath($Storage->Path().'/'.basename($this->{'Name'}));
deletePath($Storage->Path().'/'.$this->{'Id'}); deletePath($Storage->Path().'/'.$this->{'Id'});
} }
@ -435,14 +434,14 @@ class Monitor extends ZM_Object {
} // end function delete } // end function delete
public function Storage($new = null) { public function Storage($new = null) {
if ( $new ) { if ($new) {
$this->{'Storage'} = $new; $this->{'Storage'} = $new;
} }
if ( ! ( property_exists($this, 'Storage') and $this->{'Storage'} ) ) { if (!(property_exists($this, 'Storage') and $this->{'Storage'})) {
$this->{'Storage'} = isset($this->{'StorageId'}) ? $this->{'Storage'} = isset($this->{'StorageId'}) ?
Storage::find_one(array('Id'=>$this->{'StorageId'})) : Storage::find_one(array('Id'=>$this->{'StorageId'})) :
new Storage(NULL); new Storage(NULL);
if ( ! $this->{'Storage'} ) if (!$this->{'Storage'})
$this->{'Storage'} = new Storage(NULL); $this->{'Storage'} = new Storage(NULL);
} }
return $this->{'Storage'}; return $this->{'Storage'};
@ -450,42 +449,42 @@ class Monitor extends ZM_Object {
public function Source( ) { public function Source( ) {
$source = ''; $source = '';
if ( $this->{'Type'} == 'Local' ) { if ($this->{'Type'} == 'Local') {
$source = $this->{'Device'}.' ('.$this->{'Channel'}.')'; $source = $this->{'Device'}.' ('.$this->{'Channel'}.')';
} else if ( $this->{'Type'} == 'Remote' ) { } else if ($this->{'Type'} == 'Remote') {
$source = preg_replace( '/^.*@/', '', $this->{'Host'} ); $source = preg_replace('/^.*@/', '', $this->{'Host'});
if ( $this->{'Port'} != '80' and $this->{'Port'} != '554' ) { if ($this->{'Port'} != '80' and $this->{'Port'} != '554') {
$source .= ':'.$this->{'Port'}; $source .= ':'.$this->{'Port'};
} }
} else if ( $this->{'Type'} == 'VNC' ) { } else if ($this->{'Type'} == 'VNC') {
$source = preg_replace( '/^.*@/', '', $this->{'Host'} ); $source = preg_replace( '/^.*@/', '', $this->{'Host'} );
if ( $this->{'Port'} != '5900' ) { if ($this->{'Port'} != '5900') {
$source .= ':'.$this->{'Port'}; $source .= ':'.$this->{'Port'};
} }
} else if ( $this->{'Type'} == 'Ffmpeg' || $this->{'Type'} == 'Libvlc' || $this->{'Type'} == 'WebSite' ) { } else if ($this->{'Type'} == 'Ffmpeg' || $this->{'Type'} == 'Libvlc' || $this->{'Type'} == 'WebSite') {
$url_parts = parse_url( $this->{'Path'} ); $url_parts = parse_url($this->{'Path'});
if ( ZM_WEB_FILTER_SOURCE == 'Hostname' ) { if (ZM_WEB_FILTER_SOURCE == 'Hostname') {
# Filter out everything but the hostname # Filter out everything but the hostname
if ( isset($url_parts['host']) ) { if (isset($url_parts['host'])) {
$source = $url_parts['host']; $source = $url_parts['host'];
} else { } else {
$source = $this->{'Path'}; $source = $this->{'Path'};
} }
} else if ( ZM_WEB_FILTER_SOURCE == 'NoCredentials' ) { } else if (ZM_WEB_FILTER_SOURCE == 'NoCredentials') {
# Filter out sensitive and common items # Filter out sensitive and common items
unset($url_parts['user']); unset($url_parts['user']);
unset($url_parts['pass']); unset($url_parts['pass']);
#unset($url_parts['scheme']); #unset($url_parts['scheme']);
unset($url_parts['query']); unset($url_parts['query']);
#unset($url_parts['path']); #unset($url_parts['path']);
if ( isset($url_parts['port']) and ( $url_parts['port'] == '80' or $url_parts['port'] == '554' ) ) if (isset($url_parts['port']) and ($url_parts['port'] == '80' or $url_parts['port'] == '554'))
unset($url_parts['port']); unset($url_parts['port']);
$source = unparse_url($url_parts); $source = unparse_url($url_parts);
} else { # Don't filter anything } else { # Don't filter anything
$source = $this->{'Path'}; $source = $this->{'Path'};
} }
} }
if ( $source == '' ) { if ($source == '') {
$source = 'Monitor ' . $this->{'Id'}; $source = 'Monitor ' . $this->{'Id'};
} }
return $source; return $source;
@ -493,7 +492,6 @@ class Monitor extends ZM_Object {
public function UrlToIndex($port=null) { public function UrlToIndex($port=null) {
return $this->Server()->UrlToIndex($port); return $this->Server()->UrlToIndex($port);
//ZM_MIN_STREAMING_PORT ? (ZM_MIN_STREAMING_PORT+$this->Id()) : null);
} }
public function sendControlCommand($command) { public function sendControlCommand($command) {
@ -501,78 +499,78 @@ class Monitor extends ZM_Object {
$options = array(); $options = array();
# Convert from a command line params to an option array # Convert from a command line params to an option array
foreach ( explode(' ', $command) as $option ) { foreach (explode(' ', $command) as $option) {
if ( preg_match('/--([^=]+)(?:=(.+))?/', $option, $matches) ) { if (preg_match('/--([^=]+)(?:=(.+))?/', $option, $matches)) {
$options[$matches[1]] = $matches[2]?$matches[2]:1; $options[$matches[1]] = $matches[2]?$matches[2]:1;
} else if ( $option != '' and $option != 'quit' and $option != 'start' and $option != 'stop' ) { } else if ($option != '' and $option != 'quit' and $option != 'start' and $option != 'stop') {
Warning("Ignored command for zmcontrol $option in $command"); Warning("Ignored command for zmcontrol $option in $command");
} }
} }
if ( !count($options) ) { if (!count($options)) {
if ( $command == 'quit' or $command == 'start' or $command == 'stop' ) { if ($command == 'quit' or $command == 'start' or $command == 'stop') {
# These are special as we now run zmcontrol as a daemon through zmdc. # These are special as we now run zmcontrol as a daemon through zmdc.
$status = daemonStatus('zmcontrol.pl', array('--id', $this->{'Id'})); $status = daemonStatus('zmcontrol.pl', array('--id', $this->{'Id'}));
Debug("Current status $status"); Debug("Current status $status");
if ( $status or ( (!defined('ZM_SERVER_ID')) or ( property_exists($this, 'ServerId') and (ZM_SERVER_ID==$this->{'ServerId'}) ) ) ) { if ($status or ((!defined('ZM_SERVER_ID')) or (property_exists($this, 'ServerId') and (ZM_SERVER_ID==$this->{'ServerId'})))) {
daemonControl($command, 'zmcontrol.pl', '--id '.$this->{'Id'}); daemonControl($command, 'zmcontrol.pl', '--id '.$this->{'Id'});
return; return;
} }
$options['command'] = $command; $options['command'] = $command;
} else { } else {
Warning("No commands to send to zmcontrol from $command"); Warning('No commands to send to zmcontrol from '.$command);
return false; return false;
} }
} }
if ( (!defined('ZM_SERVER_ID')) or ( property_exists($this, 'ServerId') and (ZM_SERVER_ID==$this->{'ServerId'}) ) ) { if ((!defined('ZM_SERVER_ID')) or (property_exists($this, 'ServerId') and (ZM_SERVER_ID==$this->{'ServerId'}))) {
# Local # Local
Debug('Trying to send options ' . print_r($options, true)); Debug('Trying to send options ' . print_r($options, true));
$optionString = jsonEncode($options); $optionString = jsonEncode($options);
Debug("Trying to send options $optionString"); Debug('Trying to send options '.$optionString);
// Either connects to running zmcontrol.pl or runs zmcontrol.pl to send the command. // Either connects to running zmcontrol.pl or runs zmcontrol.pl to send the command.
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0); $socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
if ( $socket < 0 ) { if ($socket < 0) {
Error('socket_create() failed: '.socket_strerror($socket)); Error('socket_create() failed: '.socket_strerror($socket));
return false; return false;
} }
$sockFile = ZM_PATH_SOCKS.'/zmcontrol-'.$this->{'Id'}.'.sock'; $sockFile = ZM_PATH_SOCKS.'/zmcontrol-'.$this->{'Id'}.'.sock';
if ( @socket_connect($socket, $sockFile) ) { if (@socket_connect($socket, $sockFile)) {
if ( !socket_write($socket, $optionString) ) { if (!socket_write($socket, $optionString)) {
Error('Can\'t write to control socket: '.socket_strerror(socket_last_error($socket))); Error('Can\'t write to control socket: '.socket_strerror(socket_last_error($socket)));
return false; return false;
} }
} else if ( $command != 'quit' ) { } else if ($command != 'quit') {
$command = ZM_PATH_BIN.'/zmcontrol.pl '.$command.' --id '.$this->{'Id'}; $command = ZM_PATH_BIN.'/zmcontrol.pl '.$command.' --id '.$this->{'Id'};
// Can't connect so use script // Can't connect so use script
$ctrlOutput = exec(escapeshellcmd($command)); $ctrlOutput = exec(escapeshellcmd($command));
} }
socket_close($socket); socket_close($socket);
} else if ( $this->ServerId() ) { } else if ($this->ServerId()) {
$Server = $this->Server(); $Server = $this->Server();
$url = $Server->UrlToApi().'/monitors/daemonControl/'.$this->{'Id'}.'/'.$command.'/zmcontrol.pl.json'; $url = $Server->UrlToApi().'/monitors/daemonControl/'.$this->{'Id'}.'/'.$command.'/zmcontrol.pl.json';
if ( ZM_OPT_USE_AUTH ) { if (ZM_OPT_USE_AUTH) {
if ( ZM_AUTH_RELAY == 'hashed' ) { if (ZM_AUTH_RELAY == 'hashed') {
$url .= '?auth='.generateAuthHash(ZM_AUTH_HASH_IPS); $url .= '?auth='.generateAuthHash(ZM_AUTH_HASH_IPS);
} else if ( ZM_AUTH_RELAY == 'plain' ) { } else if (ZM_AUTH_RELAY == 'plain') {
$url .= '?user='.$_SESSION['username']; $url .= '?user='.$_SESSION['username'];
$url .= '?pass='.$_SESSION['password']; $url .= '?pass='.$_SESSION['password'];
} else if ( ZM_AUTH_RELAY == 'none' ) { } else if (ZM_AUTH_RELAY == 'none') {
$url .= '?user='.$_SESSION['username']; $url .= '?user='.$_SESSION['username'];
} }
} }
Debug("sending command to $url"); Debug('sending command to '.$url);
$context = stream_context_create(); $context = stream_context_create();
try { try {
$result = file_get_contents($url, false, $context); $result = file_get_contents($url, false, $context);
if ( $result === FALSE ) { /* Handle error */ if ($result === FALSE) { /* Handle error */
Error("Error sending command using $url"); Error("Error sending command using $url");
return false; return false;
} }
} catch ( Exception $e ) { } catch (Exception $e) {
Error("Exception $e thrown trying to send command to $url"); Error("Exception $e thrown trying to send command to $url");
return false; return false;
} }
@ -584,18 +582,18 @@ class Monitor extends ZM_Object {
} // end function sendControlCommand($mid, $command) } // end function sendControlCommand($mid, $command)
function Groups($new='') { function Groups($new='') {
if ( $new != '' ) if ($new != '')
$this->Groups = $new; $this->Groups = $new;
if ( !property_exists($this, 'Groups') ) { if (!property_exists($this, 'Groups')) {
$this->Groups = Group::find(array('Id'=>$this->GroupIds())); $this->Groups = Group::find(array('Id'=>$this->GroupIds()));
} }
return $this->Groups; return $this->Groups;
} }
function connKey($new='') { function connKey($new='') {
if ( $new ) if ($new)
$this->connKey = $new; $this->connKey = $new;
if ( !isset($this->connKey) ) { if (!isset($this->connKey)) {
if ( !empty($GLOBALS['connkey']) ) { if (!empty($GLOBALS['connkey'])) {
$this->connKey = $GLOBALS['connkey']; $this->connKey = $GLOBALS['connkey'];
} else { } else {
$this->connKey = generateConnKey(); $this->connKey = generateConnKey();
@ -625,7 +623,7 @@ class Monitor extends ZM_Object {
} }
function AlarmCommand($cmd) { function AlarmCommand($cmd) {
if ( (!defined('ZM_SERVER_ID')) or ( property_exists($this, 'ServerId') and (ZM_SERVER_ID==$this->{'ServerId'}) ) ) { if ((!defined('ZM_SERVER_ID')) or (property_exists($this, 'ServerId') and (ZM_SERVER_ID==$this->{'ServerId'}))) {
switch ($cmd) { switch ($cmd) {
case 'on' : $cmd = ' -a'; break; case 'on' : $cmd = ' -a'; break;
case 'off': $cmd = ' -c'; break; case 'off': $cmd = ' -c'; break;
@ -642,7 +640,7 @@ class Monitor extends ZM_Object {
return $output; return $output;
} }
if ( $this->ServerId() ) { if ($this->ServerId()) {
$Server = $this->Server(); $Server = $this->Server();
$url = $Server->UrlToApi().'/monitors/alarm/id:'.$this->{'Id'}.'/command:'.$cmd.'.json'; $url = $Server->UrlToApi().'/monitors/alarm/id:'.$this->{'Id'}.'/command:'.$cmd.'.json';
@ -654,15 +652,15 @@ class Monitor extends ZM_Object {
$context = stream_context_create(); $context = stream_context_create();
try { try {
$result = file_get_contents($url, false, $context); $result = file_get_contents($url, false, $context);
if ( $result === FALSE ) { /* Handle error */ if ($result === FALSE) { /* Handle error */
Error('Error sending command using '.$url); Error('Error sending command using '.$url);
return false; return false;
} }
Debug("Result $result"); Debug('Result '.$result);
$json = json_decode($result, true); $json = json_decode($result, true);
return $json['status']; return $json['status'];
} catch ( Exception $e ) { } catch (Exception $e) {
Error("Exception $e thrown trying to send command to $url"); Error("Exception $e thrown trying to send command to $url");
return false; return false;
} }
@ -672,10 +670,10 @@ class Monitor extends ZM_Object {
} }
function TriggerOn() { function TriggerOn() {
$output = $this->AlarmCommand('on'); $output = $this->AlarmCommand('on');
if ( $output and preg_match('/Alarmed event id: (\d+)$/', $output, $matches) ) { if ($output and preg_match('/Alarmed event id: (\d+)$/', $output, $matches)) {
return $matches[1]; return $matches[1];
} }
Warning("No event returned from TriggerOn"); Warning('No event returned from TriggerOn');
} }
function TriggerOff() { function TriggerOff() {
$output = $this->AlarmCommand('off'); $output = $this->AlarmCommand('off');