From e32b432fea28a524fa4805887035a32681a9ba59 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 11 Jun 2018 14:35:54 -0400 Subject: [PATCH 001/182] Always re-connect to the db on initialize --- scripts/ZoneMinder/lib/ZoneMinder/Logger.pm | 22 ++++++++++----------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm b/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm index f19d60bda..84e6c6012 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm @@ -439,16 +439,12 @@ sub databaseLevel { my $databaseLevel = shift; if ( defined($databaseLevel) ) { $databaseLevel = $this->limit($databaseLevel); - if ( $this->{databaseLevel} != $databaseLevel ) { - if ( $databaseLevel > NOLOG and $this->{databaseLevel} <= NOLOG ) { - if ( !$this->{dbh} ) { - $this->{dbh} = ZoneMinder::Database::zmDbConnect(); - } - } elsif ( $databaseLevel <= NOLOG && $this->{databaseLevel} > NOLOG ) { - undef($this->{dbh}); - } - $this->{databaseLevel} = $databaseLevel; + if ( $databaseLevel > NOLOG ) { + $this->{dbh} = ZoneMinder::Database::zmDbConnect(); + } elsif ( $databaseLevel <= NOLOG && $this->{databaseLevel} > NOLOG ) { + undef($this->{dbh}); } + $this->{databaseLevel} = $databaseLevel; } return $this->{databaseLevel}; } @@ -557,11 +553,13 @@ sub logPrint { if ( $level <= $this->{databaseLevel} ) { if ( ! ( $this->{dbh} and $this->{dbh}->ping() ) ) { $this->{sth} = undef; - if ( ! ( $this->{dbh} = ZoneMinder::Database::zmDbConnect() ) ) { - #print(STDERR "Can't log to database: "); - $this->{databaseLevel} = NOLOG; + + my $databaseLevel = $this->{databaseLevel}; + $this->{databaseLevel} = NOLOG; + if ( ! ( $this->{dbh} = ZoneMinder::Database::zmDbConnect(1) ) ) { return; } + $this->{databaseLevel} = $databaseLevel; } my $sql = 'INSERT INTO Logs ( TimeKey, Component, Pid, Level, Code, Message, File, Line ) VALUES ( ?, ?, ?, ?, ?, ?, ?, NULL )'; From d5d206f00a103868ce31fe616bf9db2f65d7efef Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 11 Jun 2018 15:57:38 -0400 Subject: [PATCH 002/182] Force disconnect when reinitialize Logging --- scripts/ZoneMinder/lib/ZoneMinder/Logger.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm b/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm index 84e6c6012..3dc0592d4 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm @@ -440,7 +440,7 @@ sub databaseLevel { if ( defined($databaseLevel) ) { $databaseLevel = $this->limit($databaseLevel); if ( $databaseLevel > NOLOG ) { - $this->{dbh} = ZoneMinder::Database::zmDbConnect(); + $this->{dbh} = ZoneMinder::Database::zmDbConnect(1); } elsif ( $databaseLevel <= NOLOG && $this->{databaseLevel} > NOLOG ) { undef($this->{dbh}); } From f5026542f88149e33461b1fb3026a0a80bed8bd6 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 11 Jun 2018 16:05:57 -0400 Subject: [PATCH 003/182] retest STDERR for terminal output on reinit. Simplify databaseLevel code to clear dbh always when not NOLOG. --- scripts/ZoneMinder/lib/ZoneMinder/Logger.pm | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm b/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm index 3dc0592d4..4bf3c5a16 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Logger.pm @@ -310,7 +310,7 @@ sub reinitialise { # Bit of a nasty hack to reopen connections to log files and the DB my $syslogLevel = $this->syslogLevel(); - $this->syslogLevel( NOLOG ); + $this->syslogLevel(NOLOG); $this->syslogLevel($syslogLevel) if $syslogLevel > NOLOG; my $logfileLevel = $this->fileLevel(); @@ -321,11 +321,10 @@ sub reinitialise { $this->databaseLevel(NOLOG); $this->databaseLevel($databaseLevel) if $databaseLevel > NOLOG; - my $screenLevel = $this->termLevel(); + $this->{hasTerm} = -t STDERR; + my $termLevel = $this->termLevel(); $this->termLevel(NOLOG); - $this->termLevel($screenLevel) if $screenLevel > NOLOG; - - $this->{sth} = undef; + $this->termLevel($termLevel) if $termLevel > NOLOG; } # Prevents undefined logging levels @@ -440,10 +439,11 @@ sub databaseLevel { if ( defined($databaseLevel) ) { $databaseLevel = $this->limit($databaseLevel); if ( $databaseLevel > NOLOG ) { - $this->{dbh} = ZoneMinder::Database::zmDbConnect(1); - } elsif ( $databaseLevel <= NOLOG && $this->{databaseLevel} > NOLOG ) { + $this->{dbh} = ZoneMinder::Database::zmDbConnect(); + } else { undef($this->{dbh}); } + $this->{sth} = undef; $this->{databaseLevel} = $databaseLevel; } return $this->{databaseLevel}; From 90e4c2632da458751c81cb6a4caae87feac4eeae Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 9 Jul 2018 12:08:50 -0400 Subject: [PATCH 004/182] Use Server->Url() more,, moving the logic into Server->Url() --- web/includes/Event.php | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/web/includes/Event.php b/web/includes/Event.php index d7ce25dc4..9c00b3670 100644 --- a/web/includes/Event.php +++ b/web/includes/Event.php @@ -189,25 +189,19 @@ class Event { public function getStreamSrc( $args=array(), $querySep='&' ) { - $streamSrc = ZM_BASE_PROTOCOL.'://'; + $streamSrc = ''; if ( $this->Storage()->ServerId() ) { $Server = $this->Storage()->Server(); - $streamSrc .= $Server->Hostname(); - if ( ZM_MIN_STREAMING_PORT ) { - $streamSrc .= ':'.(ZM_MIN_STREAMING_PORT+$this->{'MonitorId'}); - } } else if ( $this->Monitor()->ServerId() ) { # Assume that the server that recorded it has it $Server = $this->Monitor()->Server(); - $streamSrc .= $Server->Hostname(); - if ( ZM_MIN_STREAMING_PORT ) { - $streamSrc .= ':'.(ZM_MIN_STREAMING_PORT+$this->{'MonitorId'}); - } - } else if ( ZM_MIN_STREAMING_PORT ) { - $streamSrc .= $_SERVER['SERVER_NAME'].':'.(ZM_MIN_STREAMING_PORT+$this->{'MonitorId'}); } else { - $streamSrc .= $_SERVER['HTTP_HOST']; + $Server = new Server; } + $streamSrc .= $Server->Url( + ZM_MIN_STREAMING_PORT ? + ZM_MIN_STREAMING_PORT+$this->{'MonitorId'} : + null); if ( $this->{'DefaultVideo'} and $args['mode'] != 'jpeg' ) { $streamSrc .= ( ZM_BASE_PATH != '/' ? ZM_BASE_PATH : '' ).'/index.php'; @@ -319,25 +313,19 @@ class Event { # The thumbnail is theoretically the image with the most motion. # We always store at least 1 image when capturing - $streamSrc = ZM_BASE_PROTOCOL.'://'; + $streamSrc = ''; if ( $this->Storage()->ServerId() ) { $Server = $this->Storage()->Server(); - $streamSrc .= $Server->Hostname(); - if ( ZM_MIN_STREAMING_PORT ) { - $streamSrc .= ':'.(ZM_MIN_STREAMING_PORT+$this->{'MonitorId'}); - } } else if ( $this->Monitor()->ServerId() ) { + # Assume that the server that recorded it has it $Server = $this->Monitor()->Server(); - $streamSrc .= $Server->Hostname(); - if ( ZM_MIN_STREAMING_PORT ) { - $streamSrc .= ':'.(ZM_MIN_STREAMING_PORT+$this->{'MonitorId'}); - } - - } else if ( ZM_MIN_STREAMING_PORT ) { - $streamSrc .= $_SERVER['SERVER_NAME'].':'.(ZM_MIN_STREAMING_PORT+$this->{'MonitorId'}); } else { - $streamSrc .= $_SERVER['HTTP_HOST']; - } + $Server = new Server; + } + $streamSrc .= $Server->Url( + ZM_MIN_STREAMING_PORT ? + ZM_MIN_STREAMING_PORT+$this->{'MonitorId'} : + null); $streamSrc .= ( ZM_BASE_PATH != '/' ? ZM_BASE_PATH : '' ).'/index.php'; $args['eid'] = $this->{'Id'}; From 34299aff2c94e15c76e2d59743b44da943175e4e Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 9 Jul 2018 12:09:10 -0400 Subject: [PATCH 005/182] Add PathPrefix to Servers Table --- db/zm_create.sql.in | 1 + 1 file changed, 1 insertion(+) diff --git a/db/zm_create.sql.in b/db/zm_create.sql.in index 83b75f768..d0482c479 100644 --- a/db/zm_create.sql.in +++ b/db/zm_create.sql.in @@ -556,6 +556,7 @@ DROP TABLE IF EXISTS `Servers`; CREATE TABLE `Servers` ( `Id` int(10) unsigned NOT NULL auto_increment, `Hostname` TEXT, + `PathPrefix` TEXT, `Name` varchar(64) NOT NULL default '', `State_Id` int(10) unsigned, `Status` enum('Unknown','NotRunning','Running') NOT NULL default 'Unknown', From 661876b9986ad2a567b8b9ee09fb3bbfab6ea861 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 9 Jul 2018 12:09:29 -0400 Subject: [PATCH 006/182] Use Server->Url() more,, moving the logic into Server->Url() --- web/includes/Monitor.php | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/web/includes/Monitor.php b/web/includes/Monitor.php index 8fc69a9cd..bc9d92f8d 100644 --- a/web/includes/Monitor.php +++ b/web/includes/Monitor.php @@ -195,20 +195,13 @@ private $control_fields = array( } } - public function getStreamSrc( $args, $querySep='&' ) { + public function getStreamSrc($args, $querySep='&') { + + $streamSrc = $this->Server()->Url( + ZM_MIN_STREAMING_PORT ? + ZM_MIN_STREAMING_PORT+$this->{'Id'} : + null); - $streamSrc = ZM_BASE_PROTOCOL.'://'; - if ( isset($this->{'ServerId'}) and $this->{'ServerId'} ) { - $Server = new Server( $this->{'ServerId'} ); - $streamSrc .= $Server->Hostname(); - if ( ZM_MIN_STREAMING_PORT ) { - $streamSrc .= ':'.(ZM_MIN_STREAMING_PORT+$this->{'Id'}); - } - } else if ( ZM_MIN_STREAMING_PORT ) { - $streamSrc .= $_SERVER['SERVER_NAME'].':'.(ZM_MIN_STREAMING_PORT+$this->{'Id'}); - } else { - $streamSrc .= $_SERVER['HTTP_HOST']; - } $streamSrc .= ZM_PATH_ZMS; $args['monitor'] = $this->{'Id'}; @@ -230,9 +223,9 @@ private $control_fields = array( $args['rand'] = time(); } - $streamSrc .= '?'.http_build_query( $args,'', $querySep ); + $streamSrc .= '?'.http_build_query($args,'', $querySep); - return( $streamSrc ); + return $streamSrc; } // end function getStreamSrc public function Width($new = null) { @@ -487,9 +480,10 @@ private $control_fields = array( $source = preg_replace( '/^.*\//', '', $this->{'Path'} ); } elseif ( $this->{'Type'} == 'Ffmpeg' || $this->{'Type'} == 'Libvlc' || $this->{'Type'} == 'WebSite' ) { $url_parts = parse_url( $this->{'Path'} ); - if ( ZM_WEB_FILTER_SOURCE == "Hostname" ) { # Filter out everything but the hostname + if ( ZM_WEB_FILTER_SOURCE == 'Hostname' ) { # Filter out everything but the hostname $source = $url_parts['host']; - } elseif ( ZM_WEB_FILTER_SOURCE == "NoCredentials" ) { # Filter out sensitive and common items + } elseif ( ZM_WEB_FILTER_SOURCE == 'NoCredentials' ) { + # Filter out sensitive and common items unset($url_parts['user']); unset($url_parts['pass']); #unset($url_parts['scheme']); From 62f45b430e8b463b4cfadeff026ffa5a967121c0 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 9 Jul 2018 12:09:57 -0400 Subject: [PATCH 007/182] Add PathPrefix and use it in Url. Make Url() smarter so it can do more of the heavy lifting. --- web/includes/Server.php | 114 ++++++++++++++++++++-------------------- 1 file changed, 58 insertions(+), 56 deletions(-) diff --git a/web/includes/Server.php b/web/includes/Server.php index d76984598..591b26f90 100644 --- a/web/includes/Server.php +++ b/web/includes/Server.php @@ -1,24 +1,26 @@ null, - 'Name' => '', - 'Hostname' => '', - 'zmaudit' => 1, - 'zmstats' => 1, - 'zmtrigger' => 0, + 'Id' => null, + 'Name' => '', + 'Hostname' => '', + 'PathPrefix' => '/zm', + 'zmaudit' => 1, + 'zmstats' => 1, + 'zmtrigger' => 0, ); - public function __construct( $IdOrRow = NULL ) { + + public function __construct($IdOrRow = NULL) { $row = NULL; if ( $IdOrRow ) { - if ( is_integer( $IdOrRow ) or ctype_digit( $IdOrRow ) ) { - $row = dbFetchOne( 'SELECT * FROM Servers WHERE Id=?', NULL, array( $IdOrRow ) ); - if ( ! $row ) { - Error("Unable to load Server record for Id=" . $IdOrRow ); + if ( is_integer($IdOrRow) or ctype_digit($IdOrRow) ) { + $row = dbFetchOne('SELECT * FROM Servers WHERE Id=?', NULL, array($IdOrRow)); + if ( !$row ) { + Error('Unable to load Server record for Id='.$IdOrRow); } - } elseif ( is_array( $IdOrRow ) ) { + } elseif ( is_array($IdOrRow) ) { $row = $IdOrRow; } } # end if isset($IdOrRow) @@ -27,11 +29,12 @@ class Server { $this->{$k} = $v; } } else { - $this->{'Name'} = ''; - $this->{'Hostname'} = ''; + # Set defaults + foreach ( $this->defaults as $k => $v ) $this->{$k} = $v; } } - public static function find_all( $parameters = null, $options = null ) { + + public static function find_all($parameters = null, $options = null) { $filters = array(); $sql = 'SELECT * FROM Servers '; $values = array(); @@ -42,9 +45,9 @@ class Server { foreach ( $parameters as $field => $value ) { if ( $value == null ) { $fields[] = $field.' IS NULL'; - } else if ( is_array( $value ) ) { + } else if ( is_array($value) ) { $func = function(){return '?';}; - $fields[] = $field.' IN ('.implode(',', array_map( $func, $value ) ). ')'; + $fields[] = $field.' IN ('.implode(',', array_map($func, $value) ). ')'; $values += $value; } else { @@ -52,10 +55,10 @@ class Server { $values[] = $value; } } - $sql .= implode(' AND ', $fields ); + $sql .= implode(' AND ', $fields); } if ( $options and isset($options['order']) ) { - $sql .= ' ORDER BY ' . $options['order']; + $sql .= ' ORDER BY ' . $options['order']; } $result = dbQuery($sql, $values); $results = $result->fetchALL(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Server'); @@ -65,20 +68,19 @@ class Server { return $filters; } - public function Url() { - if ( $this->Id() ) { - return ZM_BASE_PROTOCOL . '://'. $this->Hostname(); - } else { - return ZM_BASE_PROTOCOL . '://'. $_SERVER['SERVER_NAME']; - return ''; - } - } - public function Hostname() { - if ( isset( $this->{'Hostname'} ) and ( $this->{'Hostname'} != '' ) ) { - return $this->{'Hostname'}; - } - return $this->{'Name'}; - } + public function Url($port=null) { + return ZM_BASE_PROTOCOL.'://'.$this->Hostname().($port ? ':'.$port : '').$this->{'PathPrefix'}; + } + + public function Hostname() { + if ( isset( $this->{'Hostname'}) and ( $this->{'Hostname'} != '' ) ) { + return $this->{'Hostname'}; + } else if ( $this->Id() ) { + return $this->{'Name'}; + } + return $_SERVER['SERVER_NAME']; + } + public function __call($fn, array $args){ if ( count($args) ) { $this->{$fn} = $args[0]; @@ -86,49 +88,49 @@ class Server { if ( array_key_exists($fn, $this) ) { return $this->{$fn}; } else { - if ( array_key_exists( $fn, $this->defaults ) ) { + if ( array_key_exists($fn, $this->defaults) ) { return $this->defaults{$fn}; } else { $backTrace = debug_backtrace(); $file = $backTrace[1]['file']; $line = $backTrace[1]['line']; - Warning( "Unknown function call Server->$fn from $file:$line" ); + Warning("Unknown function call Server->$fn from $file:$line"); } } } - public static function find( $parameters = array(), $limit = NULL ) { + public static function find($parameters = array(), $limit = NULL) { $sql = 'SELECT * FROM Servers'; $values = array(); if ( sizeof($parameters) ) { - $sql .= ' WHERE ' . implode( ' AND ', array_map( + $sql .= ' WHERE ' . implode(' AND ', array_map( function($v){ return $v.'=?'; }, - array_keys( $parameters ) - ) ); - $values = array_values( $parameters ); + array_keys($parameters) + )); + $values = array_values($parameters); } - if ( is_integer( $limit ) or ctype_digit( $limit ) ) { - $sql .= ' LIMIT ' . $limit; - } else { - $backTrace = debug_backtrace(); - $file = $backTrace[1]['file']; - $line = $backTrace[1]['line']; - Error("Invalid value for limit($limit) passed to Server::find from $file:$line"); - return; - } - $results = dbFetchAll( $sql, NULL, $values ); + if ( is_integer($limit) or ctype_digit($limit) ) { + $sql .= ' LIMIT ' . $limit; + } else { + $backTrace = debug_backtrace(); + $file = $backTrace[1]['file']; + $line = $backTrace[1]['line']; + Error("Invalid value for limit($limit) passed to Server::find from $file:$line"); + return; + } + $results = dbFetchAll($sql, NULL, $values); if ( $results ) { - return array_map( function($id){ return new Server($id); }, $results ); + return array_map(function($id){ return new Server($id); }, $results); } } - public static function find_one( $parameters = array() ) { - $results = Server::find( $parameters, 1 ); - if ( ! sizeof( $results ) ) { + public static function find_one($parameters = array()) { + $results = Server::find($parameters, 1); + if ( !sizeof($results) ) { return; } return $results[0]; } -} +} # end class Server ?> From d051342e9f629e8904e10763b517802c4a464b11 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 9 Jul 2018 12:10:29 -0400 Subject: [PATCH 008/182] Add PathPrefix --- web/skins/classic/views/server.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/web/skins/classic/views/server.php b/web/skins/classic/views/server.php index ae78d622f..2791a3f64 100644 --- a/web/skins/classic/views/server.php +++ b/web/skins/classic/views/server.php @@ -24,7 +24,7 @@ if ( !canEdit( 'System' ) ) { } if ( $_REQUEST['id'] ) { - if ( !($newServer = dbFetchOne( 'SELECT * FROM Servers WHERE Id = ?', NULL, ARRAY($_REQUEST['id'])) ) ) { + if ( !($newServer = dbFetchOne('SELECT * FROM Servers WHERE Id = ?', NULL, ARRAY($_REQUEST['id']))) ) { $view = 'error'; return; } @@ -32,6 +32,7 @@ if ( $_REQUEST['id'] ) { $newServer = array(); $newServer['Name'] = translate('NewServer'); $newServer['Hostname'] = ''; + $newServer['PathPrefix'] = '/zm'; $newServer['zmstats'] = ''; $newServer['zmaudit'] = ''; $newServer['zmtrigger'] = ''; @@ -39,7 +40,7 @@ if ( $_REQUEST['id'] ) { $focusWindow = true; -xhtmlHeaders(__FILE__, translate('Server').' - '.$newServer['Name'] ); +xhtmlHeaders(__FILE__, translate('Server').' - '.$newServer['Name']); ?>
@@ -47,7 +48,7 @@ xhtmlHeaders(__FILE__, translate('Server').' - '.$newServer['Name'] );

-
+ @@ -61,6 +62,10 @@ xhtmlHeaders(__FILE__, translate('Server').' - '.$newServer['Name'] ); + + + + From acab621f2c43369ae3d01ec0df7c1847152afae5 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 9 Jul 2018 12:18:14 -0400 Subject: [PATCH 009/182] bump version for db update --- version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version b/version index 856307b22..6701dcb1c 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.31.44 +1.31.45 From ebe55cf6a45762ee49fb0ac5d4ad6f99770fbc76 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 9 Jul 2018 14:10:06 -0400 Subject: [PATCH 010/182] Include new Server.js --- web/js/Server.js | 12 ++++++++++++ web/skins/classic/includes/functions.php | 2 ++ 2 files changed, 14 insertions(+) create mode 100644 web/js/Server.js diff --git a/web/js/Server.js b/web/js/Server.js new file mode 100644 index 000000000..6e3254f65 --- /dev/null +++ b/web/js/Server.js @@ -0,0 +1,12 @@ +class Server { + constructor(json) { + for( var k in json ) { + this[k] = json[k]; + } + } + url(port=0){ + return location.protocol+'//'+this.Hostname+ + (port ? ':'+port : '') + + ( ( this.PathPrefix && this.PathPrefix != 'null') ? this.PathPrefix : ''); + } +}; diff --git a/web/skins/classic/includes/functions.php b/web/skins/classic/includes/functions.php index 66243daf3..e39df3ebe 100644 --- a/web/skins/classic/includes/functions.php +++ b/web/skins/classic/includes/functions.php @@ -119,6 +119,8 @@ echo output_link_if_exists( array( + +