From e32b432fea28a524fa4805887035a32681a9ba59 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 11 Jun 2018 14:35:54 -0400 Subject: [PATCH 01/22] 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 02/22] 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 03/22] 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 702143e51bd8f0f52d9700491b8ca7e96665f66c Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 7 Nov 2018 12:33:54 -0500 Subject: [PATCH 04/22] Create a function called getBodyTopHTML that outputs the body tag and anything else that should go at the top. Things like the we require javascript message, and any other messages like error messages. Use this on the monitor and console view to stick an error message at the top when saving a monitor fails. This is a pretty quick, crude implementation. --- web/includes/actions.php | 4 +++- web/index.php | 1 + web/skins/classic/includes/functions.php | 22 +++++++++++++++++----- web/skins/classic/views/console.php | 2 +- web/skins/classic/views/monitor.php | 2 +- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/web/includes/actions.php b/web/includes/actions.php index de0861fe0..48082a585 100644 --- a/web/includes/actions.php +++ b/web/includes/actions.php @@ -554,7 +554,8 @@ if ( canEdit('Monitors') ) { $maxSeq = dbFetchOne('SELECT MAX(Sequence) AS MaxSequence FROM Monitors', 'MaxSequence'); $changes[] = 'Sequence = '.($maxSeq+1); - if ( dbQuery('INSERT INTO Monitors SET '.implode(', ', $changes)) ) { + $sql = 'INSERT INTO Monitors SET '.implode(', ', $changes); + if ( dbQuery($sql) ) { $mid = dbInsertId(); $zoneArea = $_REQUEST['newMonitor']['Width'] * $_REQUEST['newMonitor']['Height']; dbQuery("INSERT INTO Zones SET MonitorId = ?, Name = 'All', Type = 'Active', Units = 'Percent', NumCoords = 4, Coords = ?, Area=?, AlarmRGB = 0xff0000, CheckMethod = 'Blobs', MinPixelThreshold = 25, MinAlarmPixels=?, MaxAlarmPixels=?, FilterX = 3, FilterY = 3, MinFilterPixels=?, MaxFilterPixels=?, MinBlobPixels=?, MinBlobs = 1", array( $mid, sprintf( "%d,%d %d,%d %d,%d %d,%d", 0, 0, $_REQUEST['newMonitor']['Width']-1, 0, $_REQUEST['newMonitor']['Width']-1, $_REQUEST['newMonitor']['Height']-1, 0, $_REQUEST['newMonitor']['Height']-1 ), $zoneArea, intval(($zoneArea*3)/100), intval(($zoneArea*75)/100), intval(($zoneArea*3)/100), intval(($zoneArea*75)/100), intval(($zoneArea*2)/100) ) ); @@ -566,6 +567,7 @@ if ( canEdit('Monitors') ) { } else { Error('Error saving new Monitor.'); + $error_message = dbError($sql); return; } } else { diff --git a/web/index.php b/web/index.php index addddce06..cbda1096a 100644 --- a/web/index.php +++ b/web/index.php @@ -170,6 +170,7 @@ if ( !is_writable(ZM_DIR_EVENTS) || !is_writable(ZM_DIR_IMAGES) ) { } # Globals +$error_message = null; $redirect = null; $view = null; if ( isset($_REQUEST['view']) ) diff --git a/web/skins/classic/includes/functions.php b/web/skins/classic/includes/functions.php index 7847e37c7..8fd9e2a8e 100644 --- a/web/skins/classic/includes/functions.php +++ b/web/skins/classic/includes/functions.php @@ -208,6 +208,23 @@ echo output_link_if_exists( array( + +'; + global $error_message; + if ( $error_message ) { + echo '
'.$error_message.'
'; + } +} // end function getBodyTopHTML + function getNavBarHTML($reload = null) { # Provide a facility to turn off the headers if you put headers=0 into the url if ( isset($_REQUEST['navbar']) and $_REQUEST['navbar']=='0' ) @@ -235,11 +252,6 @@ function getNavBarHTML($reload = null) { $running = daemonCheck(); $status = $running?translate('Running'):translate('Stopped'); ?> -