2019-09-24 00:54:27 +08:00
|
|
|
#!@PERL_EXECUTABLE@ -wT
|
2005-12-16 18:05:29 +08:00
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# ZoneMinder Event Filter Script, $Date$, $Revision$
|
2008-07-25 17:48:16 +08:00
|
|
|
# Copyright (C) 2001-2008 Philip Coombes
|
2005-12-16 18:05:29 +08:00
|
|
|
#
|
|
|
|
# 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
|
2016-12-26 23:23:16 +08:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2005-12-16 18:05:29 +08:00
|
|
|
#
|
|
|
|
# ==========================================================================
|
2015-04-11 09:14:29 +08:00
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
zmfilter.pl - ZoneMinder tool to filter events
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
2017-03-21 04:47:58 +08:00
|
|
|
zmfilter.pl [-f <filter name>,--filter=<filter name>] [--filter_id=<filter id>] | -v, --version
|
2015-04-11 09:14:29 +08:00
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
This script continuously monitors the recorded events for the given
|
|
|
|
monitor and applies any filters which would delete and/or upload
|
|
|
|
matching events.
|
|
|
|
|
|
|
|
=head1 OPTIONS
|
|
|
|
|
2017-03-21 04:47:58 +08:00
|
|
|
|
|
|
|
-f{filter name}, --filter={filter name} - The name of a specific filter to run
|
|
|
|
--filter_id={filter id} - The id of a specific filter to run
|
|
|
|
-v, --version - Print ZoneMinder version
|
2015-04-11 09:14:29 +08:00
|
|
|
|
|
|
|
=cut
|
2005-12-16 18:05:29 +08:00
|
|
|
use strict;
|
|
|
|
use bytes;
|
|
|
|
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# These are the elements you can edit to suit your installation
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
2005-12-16 21:16:37 +08:00
|
|
|
use constant START_DELAY => 5; # How long to wait before starting
|
|
|
|
|
2005-12-16 18:36:22 +08:00
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# You shouldn't need to change anything from here downwards
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
2009-06-08 17:11:56 +08:00
|
|
|
@EXTRA_PERL_LIB@
|
2005-12-16 18:36:22 +08:00
|
|
|
use ZoneMinder;
|
|
|
|
use DBI;
|
|
|
|
use POSIX;
|
|
|
|
use Time::HiRes qw/gettimeofday/;
|
2015-12-18 01:57:14 +08:00
|
|
|
#use Date::Manip;
|
2005-12-16 18:36:22 +08:00
|
|
|
use Getopt::Long;
|
2015-04-11 09:14:29 +08:00
|
|
|
use autouse 'Pod::Usage'=>qw(pod2usage);
|
|
|
|
use autouse 'Data::Dumper'=>qw(Dumper);
|
2018-10-12 04:57:25 +08:00
|
|
|
use File::Path qw(make_path);
|
2005-12-16 18:36:22 +08:00
|
|
|
|
2018-06-07 02:05:58 +08:00
|
|
|
my $daemon = 0;
|
2017-06-05 09:42:56 +08:00
|
|
|
my $filter_name = '';
|
2016-02-06 00:24:40 +08:00
|
|
|
my $filter_id;
|
|
|
|
my $version = 0;
|
2018-01-30 22:19:08 +08:00
|
|
|
my $zm_terminate = 0;
|
2016-02-06 00:24:40 +08:00
|
|
|
|
|
|
|
GetOptions(
|
2018-06-21 22:49:51 +08:00
|
|
|
daemon =>\$daemon,
|
|
|
|
'filter=s' =>\$filter_name,
|
|
|
|
'filter_id=s' =>\$filter_id,
|
|
|
|
version =>\$version
|
|
|
|
) or pod2usage(-exitstatus => -1);
|
2016-02-06 00:24:40 +08:00
|
|
|
|
|
|
|
if ( $version ) {
|
2017-03-21 04:47:58 +08:00
|
|
|
print ZoneMinder::Base::ZM_VERSION . "\n";
|
|
|
|
exit(0);
|
2016-02-06 00:24:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
require ZoneMinder::Event;
|
|
|
|
require ZoneMinder::Filter;
|
|
|
|
|
2015-04-11 09:14:29 +08:00
|
|
|
use constant EVENT_PATH => ($Config{ZM_DIR_EVENTS}=~m|/|)
|
2017-03-21 04:47:58 +08:00
|
|
|
? $Config{ZM_DIR_EVENTS}
|
|
|
|
: ($Config{ZM_PATH_WEB}.'/'.$Config{ZM_DIR_EVENTS})
|
|
|
|
;
|
|
|
|
|
2018-04-30 22:56:51 +08:00
|
|
|
logInit($filter_id?(id=>'zmfilter_'.$filter_id):());
|
2020-10-12 22:53:53 +08:00
|
|
|
|
2018-01-30 22:19:08 +08:00
|
|
|
sub HupHandler {
|
2018-08-02 18:52:36 +08:00
|
|
|
# This idea at this time is to just exit, freeing up the memory.
|
|
|
|
# zmfilter.pl will be respawned by zmdc.
|
|
|
|
TermHandler();
|
|
|
|
return;
|
|
|
|
|
2018-06-21 22:49:51 +08:00
|
|
|
Info('Received HUP, reloading');
|
|
|
|
ZoneMinder::Object::init_cache();
|
2018-01-30 22:19:08 +08:00
|
|
|
&ZoneMinder::Logger::logHupHandler();
|
|
|
|
}
|
|
|
|
sub TermHandler {
|
2018-06-21 22:49:51 +08:00
|
|
|
Info('Received TERM, exiting');
|
2018-01-30 22:19:08 +08:00
|
|
|
$zm_terminate = 1;
|
|
|
|
}
|
|
|
|
sub Term {
|
2018-06-21 22:49:51 +08:00
|
|
|
exit(0);
|
2018-01-30 22:19:08 +08:00
|
|
|
}
|
|
|
|
$SIG{HUP} = \&HupHandler;
|
|
|
|
$SIG{TERM} = \&TermHandler;
|
|
|
|
$SIG{INT} = \&TermHandler;
|
2017-03-21 04:47:58 +08:00
|
|
|
|
|
|
|
if ( $Config{ZM_OPT_UPLOAD} ) {
|
|
|
|
# Comment these out if you don't have them and don't want to upload
|
|
|
|
# or don't want to use that format
|
2017-06-02 23:54:40 +08:00
|
|
|
if ( $Config{ZM_UPLOAD_ARCH_FORMAT} eq 'zip' ) {
|
2017-03-21 04:47:58 +08:00
|
|
|
require Archive::Zip;
|
|
|
|
import Archive::Zip qw( :ERROR_CODES :CONSTANTS );
|
|
|
|
} else {
|
|
|
|
require Archive::Tar;
|
|
|
|
}
|
2017-06-02 23:54:40 +08:00
|
|
|
if ( $Config{ZM_UPLOAD_PROTOCOL} eq 'ftp' ) {
|
2017-03-21 04:47:58 +08:00
|
|
|
require Net::FTP;
|
|
|
|
} else {
|
|
|
|
require Net::SFTP::Foreign;
|
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
2017-03-21 04:47:58 +08:00
|
|
|
if ( $Config{ZM_OPT_EMAIL} ) {
|
|
|
|
if ( $Config{ZM_NEW_MAIL_MODULES} ) {
|
|
|
|
require MIME::Lite;
|
|
|
|
require Net::SMTP;
|
|
|
|
} else {
|
|
|
|
require MIME::Entity;
|
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
2017-03-21 04:47:58 +08:00
|
|
|
if ( $Config{ZM_OPT_MESSAGE} ) {
|
|
|
|
if ( $Config{ZM_NEW_MAIL_MODULES} ) {
|
|
|
|
require MIME::Lite;
|
|
|
|
require Net::SMTP;
|
|
|
|
} else {
|
|
|
|
require MIME::Entity;
|
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$| = 1;
|
|
|
|
|
2016-03-12 05:28:16 +08:00
|
|
|
$ENV{PATH} = '/bin:/usr/bin:/usr/local/bin';
|
2005-12-16 18:05:29 +08:00
|
|
|
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
|
|
|
|
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
|
|
|
|
|
2013-12-17 05:32:02 +08:00
|
|
|
my $delay = $Config{ZM_FILTER_EXECUTE_INTERVAL};
|
2005-12-16 18:05:29 +08:00
|
|
|
my $event_id = 0;
|
|
|
|
|
2021-01-15 01:56:38 +08:00
|
|
|
if ( !EVENT_PATH ) {
|
|
|
|
Error('No event path defined. Config was '.$Config{ZM_DIR_EVENTS});
|
2017-03-21 04:47:58 +08:00
|
|
|
die;
|
2013-12-20 02:41:55 +08:00
|
|
|
}
|
|
|
|
|
2017-06-05 05:30:39 +08:00
|
|
|
# In future, should not be neccessary wrt StorageAreas
|
2021-01-15 01:56:38 +08:00
|
|
|
chdir(EVENT_PATH);
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2018-04-30 22:56:51 +08:00
|
|
|
# Should not be neccessary... but nice to get a local var. What if it fails?
|
2007-09-07 23:39:44 +08:00
|
|
|
my $dbh = zmDbConnect();
|
2021-01-15 01:56:38 +08:00
|
|
|
$dbh->do('SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED');
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2016-02-06 00:24:40 +08:00
|
|
|
if ( $filter_name ) {
|
2018-10-11 23:28:07 +08:00
|
|
|
Info("Scanning for events using filter '$filter_name'");
|
2019-08-27 06:49:12 +08:00
|
|
|
} elsif ( defined($filter_id) ) {
|
|
|
|
if ( $filter_id ) {
|
|
|
|
Info("Scanning for events using filter id '$filter_id'");
|
|
|
|
} else {
|
2019-10-30 21:26:17 +08:00
|
|
|
Fatal('No filter_id specified');
|
2019-08-27 06:49:12 +08:00
|
|
|
}
|
2016-02-06 00:24:40 +08:00
|
|
|
} else {
|
2019-10-30 21:26:17 +08:00
|
|
|
Info('Scanning for events using all filters');
|
2006-10-23 23:32:22 +08:00
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2016-02-06 00:41:54 +08:00
|
|
|
if ( ! ( $filter_name or $filter_id ) ) {
|
2018-06-21 22:49:51 +08:00
|
|
|
Debug('Sleeping due to start delay: ' . START_DELAY . ' seconds...');
|
2018-04-30 22:56:51 +08:00
|
|
|
sleep(START_DELAY);
|
2006-10-23 23:32:22 +08:00
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2017-06-03 00:25:26 +08:00
|
|
|
my @filters;
|
2005-12-16 18:05:29 +08:00
|
|
|
my $last_action = 0;
|
|
|
|
|
2018-04-30 22:56:51 +08:00
|
|
|
while( !$zm_terminate ) {
|
2017-03-21 04:47:58 +08:00
|
|
|
my $now = time;
|
|
|
|
if ( ($now - $last_action) > $Config{ZM_FILTER_RELOAD_DELAY} ) {
|
2020-01-23 02:58:44 +08:00
|
|
|
Debug('Reloading filters');
|
2017-03-21 04:47:58 +08:00
|
|
|
$last_action = $now;
|
2018-04-30 22:56:51 +08:00
|
|
|
@filters = getFilters({ Name=>$filter_name, Id=>$filter_id });
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
|
2017-06-09 21:13:30 +08:00
|
|
|
foreach my $filter ( @filters ) {
|
2018-01-30 22:19:08 +08:00
|
|
|
last if $zm_terminate;
|
2017-03-21 04:47:58 +08:00
|
|
|
if ( $$filter{Concurrent} and ! ( $filter_id or $filter_name ) ) {
|
|
|
|
my ( $proc ) = $0 =~ /(\S+)/;
|
|
|
|
my ( $id ) = $$filter{Id} =~ /(\d+)/;
|
|
|
|
Debug("Running concurrent filter process $proc --filter_id $$filter{Id} => $id for $$filter{Name}");
|
|
|
|
|
2018-04-30 22:56:51 +08:00
|
|
|
system(qq`$proc --filter "$$filter{Name}" &`);
|
2017-03-21 04:47:58 +08:00
|
|
|
} else {
|
2018-04-30 22:56:51 +08:00
|
|
|
checkFilter($filter);
|
2007-09-04 22:52:26 +08:00
|
|
|
}
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2018-06-07 02:05:58 +08:00
|
|
|
last if (!$daemon and ($filter_name or $filter_id)) or $zm_terminate;
|
2006-10-23 23:32:22 +08:00
|
|
|
|
2018-10-11 23:28:07 +08:00
|
|
|
Debug("Sleeping for $delay seconds");
|
2018-04-30 22:56:51 +08:00
|
|
|
sleep($delay);
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
2017-03-21 04:47:58 +08:00
|
|
|
sub getFilters {
|
|
|
|
my $sql_filters = @_ ? shift : {};
|
|
|
|
my @sql_values;
|
|
|
|
|
|
|
|
my @filters;
|
2019-08-26 00:30:06 +08:00
|
|
|
my $sql = 'SELECT * FROM `Filters` WHERE';
|
2017-03-21 04:47:58 +08:00
|
|
|
if ( $$sql_filters{Name} ) {
|
2019-08-26 00:30:06 +08:00
|
|
|
$sql .= ' `Name` = ? AND';
|
2017-03-21 04:47:58 +08:00
|
|
|
push @sql_values, $$sql_filters{Name};
|
|
|
|
} elsif ( $$sql_filters{Id} ) {
|
2019-08-26 00:30:06 +08:00
|
|
|
$sql .= ' `Id` = ? AND';
|
2017-03-21 04:47:58 +08:00
|
|
|
push @sql_values, $$sql_filters{Id};
|
|
|
|
} else {
|
2019-08-26 00:30:06 +08:00
|
|
|
$sql .= ' `Background` = 1 AND';
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2019-08-26 00:30:06 +08:00
|
|
|
$sql .= '( `AutoArchive` = 1
|
2020-10-12 22:53:53 +08:00
|
|
|
or `AutoUnarchive` = 1
|
2019-08-26 00:30:06 +08:00
|
|
|
or `AutoVideo` = 1
|
|
|
|
or `AutoUpload` = 1
|
|
|
|
or `AutoEmail` = 1
|
|
|
|
or `AutoMessage` = 1
|
|
|
|
or `AutoExecute` = 1
|
|
|
|
or `AutoDelete` = 1
|
|
|
|
or `UpdateDiskSpace` = 1
|
|
|
|
or `AutoMove` = 1
|
|
|
|
or `AutoCopy` = 1
|
|
|
|
) ORDER BY `Name`';
|
2018-04-30 22:56:51 +08:00
|
|
|
my $sth = $dbh->prepare_cached($sql)
|
|
|
|
or Fatal("Unable to prepare '$sql': ".$dbh->errstr());
|
|
|
|
my $res = $sth->execute(@sql_values)
|
|
|
|
or Fatal("Unable to execute '$sql': ".$dbh->errstr());
|
2017-03-21 04:47:58 +08:00
|
|
|
FILTER: while( my $db_filter = $sth->fetchrow_hashref() ) {
|
2018-04-30 22:56:51 +08:00
|
|
|
my $filter = new ZoneMinder::Filter($$db_filter{Id}, $db_filter);
|
|
|
|
Debug("Found filter '$db_filter->{Name}'");
|
2018-04-14 23:09:01 +08:00
|
|
|
# The undef here is to make sure the Sql gets regenerated because the Filter object may be cached
|
|
|
|
my $filter_sql = $filter->Sql(undef);
|
2017-03-21 04:47:58 +08:00
|
|
|
|
2019-10-30 21:26:17 +08:00
|
|
|
if ( !$filter_sql ) {
|
2018-04-30 22:56:51 +08:00
|
|
|
Error("Error parsing Sql. skipping filter '$db_filter->{Name}'");
|
2017-03-21 04:47:58 +08:00
|
|
|
next FILTER;
|
|
|
|
}
|
2018-01-02 23:10:38 +08:00
|
|
|
push @filters, $filter;
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
$sth->finish();
|
|
|
|
if ( ! @filters ) {
|
|
|
|
Warning("No filter found for $sql with values(@sql_values)");
|
|
|
|
} else {
|
2018-04-30 22:56:51 +08:00
|
|
|
Debug("Got " . @filters . " filters");
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
|
2018-01-02 23:10:38 +08:00
|
|
|
return @filters;
|
2017-12-13 00:16:17 +08:00
|
|
|
} # end sub getFilters
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2017-03-21 04:47:58 +08:00
|
|
|
sub checkFilter {
|
|
|
|
my $filter = shift;
|
|
|
|
|
2020-10-27 06:47:40 +08:00
|
|
|
my $in_transaction = ZoneMinder::Database::start_transaction($dbh) if $$filter{LockRows};
|
2017-03-21 04:47:58 +08:00
|
|
|
my @Events = $filter->Execute();
|
2019-06-03 21:46:28 +08:00
|
|
|
Debug(
|
2017-10-28 00:09:12 +08:00
|
|
|
join(' ',
|
|
|
|
'Checking filter', $filter->{Name},
|
2018-06-21 22:49:51 +08:00
|
|
|
join(', ',
|
2017-10-28 00:09:12 +08:00
|
|
|
($filter->{AutoDelete}?'delete':()),
|
|
|
|
($filter->{AutoArchive}?'archive':()),
|
2020-10-12 22:53:53 +08:00
|
|
|
($filter->{AutoUnarchive}?'unarchive':()),
|
2017-10-28 00:09:12 +08:00
|
|
|
($filter->{AutoVideo}?'video':()),
|
|
|
|
($filter->{AutoUpload}?'upload':()),
|
|
|
|
($filter->{AutoEmail}?'email':()),
|
|
|
|
($filter->{AutoMessage}?'message':()),
|
|
|
|
($filter->{AutoExecute}?'execute':()),
|
2017-12-05 00:05:50 +08:00
|
|
|
($filter->{AutoMove}?'move':()),
|
2019-07-23 21:55:27 +08:00
|
|
|
($filter->{AutoCopy}?'copy':()),
|
2017-10-28 00:09:12 +08:00
|
|
|
($filter->{UpdateDiskSpace}?'update disk space':()),
|
|
|
|
),
|
|
|
|
'returned' , scalar @Events , 'events',
|
|
|
|
"\n",
|
|
|
|
) );
|
2017-03-21 04:47:58 +08:00
|
|
|
|
|
|
|
foreach my $event ( @Events ) {
|
2018-01-30 22:19:08 +08:00
|
|
|
last if $zm_terminate;
|
2018-04-30 22:56:51 +08:00
|
|
|
my $Event = new ZoneMinder::Event($$event{Id}, $event);
|
2018-04-21 03:22:31 +08:00
|
|
|
|
2020-10-12 22:53:53 +08:00
|
|
|
Debug('Checking event '.$Event->{Id});
|
2017-03-21 04:47:58 +08:00
|
|
|
my $delete_ok = !undef;
|
|
|
|
$dbh->ping();
|
|
|
|
if ( $filter->{AutoArchive} ) {
|
2018-10-08 21:25:17 +08:00
|
|
|
Info("Archiving event $Event->{Id}");
|
2017-12-05 00:05:50 +08:00
|
|
|
# Do it individually to avoid locking up the table for new events
|
2019-08-26 00:30:06 +08:00
|
|
|
my $sql = 'UPDATE `Events` SET `Archived` = 1 WHERE `Id` = ?';
|
2019-07-23 21:55:27 +08:00
|
|
|
my $sth = $dbh->prepare_cached($sql)
|
2018-04-30 22:56:51 +08:00
|
|
|
or Fatal("Unable to prepare '$sql': ".$dbh->errstr());
|
2019-07-23 21:55:27 +08:00
|
|
|
my $res = $sth->execute($Event->{Id})
|
2018-04-30 22:56:51 +08:00
|
|
|
or Error("Unable to execute '$sql': ".$dbh->errstr());
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2020-10-12 22:53:53 +08:00
|
|
|
if ( $filter->{AutoUnarchive} ) {
|
|
|
|
Info("Unarchiving event $Event->{Id}");
|
|
|
|
# Do it individually to avoid locking up the table for new events
|
|
|
|
my $sql = 'UPDATE `Events` SET `Archived` = 0 WHERE `Id` = ?';
|
|
|
|
my $sth = $dbh->prepare_cached($sql)
|
|
|
|
or Fatal("Unable to prepare '$sql': ".$dbh->errstr());
|
|
|
|
my $res = $sth->execute($Event->{Id})
|
|
|
|
or Error("Unable to execute '$sql': ".$dbh->errstr());
|
|
|
|
}
|
2017-03-21 04:47:58 +08:00
|
|
|
if ( $Config{ZM_OPT_FFMPEG} && $filter->{AutoVideo} ) {
|
2018-10-08 21:25:17 +08:00
|
|
|
if ( !$Event->{Videoed} ) {
|
|
|
|
$delete_ok = undef if !generateVideo($filter, $Event);
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $Config{ZM_OPT_EMAIL} && $filter->{AutoEmail} ) {
|
2018-10-08 21:25:17 +08:00
|
|
|
if ( !$Event->{Emailed} ) {
|
2018-04-30 22:56:51 +08:00
|
|
|
$delete_ok = undef if !sendEmail($filter, $Event);
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $Config{ZM_OPT_MESSAGE} && $filter->{AutoMessage} ) {
|
2018-10-08 21:25:17 +08:00
|
|
|
if ( !$Event->{Messaged} ) {
|
2018-06-09 03:14:11 +08:00
|
|
|
$delete_ok = undef if !sendMessage($filter, $Event);
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $Config{ZM_OPT_UPLOAD} && $filter->{AutoUpload} ) {
|
2018-10-08 21:25:17 +08:00
|
|
|
if ( !$Event->{Uploaded} ) {
|
2018-09-29 20:32:57 +08:00
|
|
|
$delete_ok = undef if !uploadArchFile($filter, $Event);
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $filter->{AutoExecute} ) {
|
2018-10-08 21:25:17 +08:00
|
|
|
if ( !$Event->{Executed} ) {
|
2018-10-08 20:31:16 +08:00
|
|
|
$delete_ok = undef if !executeCommand($filter, $Event);
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $filter->{AutoDelete} ) {
|
|
|
|
if ( $delete_ok ) {
|
|
|
|
$Event->delete();
|
|
|
|
} else {
|
2018-10-08 21:25:17 +08:00
|
|
|
Error("Unable to delete event $Event->{Id} as previous operations failed");
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
} # end if AutoDelete
|
2018-04-13 04:40:11 +08:00
|
|
|
|
2017-12-05 00:05:50 +08:00
|
|
|
if ( $filter->{AutoMove} ) {
|
2018-04-30 22:56:51 +08:00
|
|
|
my $NewStorage = new ZoneMinder::Storage($filter->{AutoMoveTo});
|
2021-02-11 06:31:10 +08:00
|
|
|
Info("Moving event $Event->{Id} to datastore $filter->{AutoMoveTo}");
|
2018-04-30 22:56:51 +08:00
|
|
|
$_ = $Event->MoveTo($NewStorage);
|
2017-12-05 00:05:50 +08:00
|
|
|
Error($_) if $_;
|
|
|
|
}
|
2019-07-23 21:55:27 +08:00
|
|
|
if ( $filter->{AutoCopy} ) {
|
2019-07-24 21:37:38 +08:00
|
|
|
# Copy To is different from MoveTo in that it JUST copies the files
|
|
|
|
# So we still need to update the Event object with the new SecondaryStorageId
|
2019-07-24 23:23:13 +08:00
|
|
|
my $NewStorage = ZoneMinder::Storage->find_one(Id=>$filter->{AutoCopyTo});
|
2019-07-24 21:37:38 +08:00
|
|
|
if ( $NewStorage ) {
|
2021-02-11 06:31:10 +08:00
|
|
|
Info("Copying event $Event->{Id} to datastore $filter->{AutoCopyTo}");
|
2019-07-24 21:37:38 +08:00
|
|
|
$_ = $Event->CopyTo($NewStorage);
|
|
|
|
if ( $_ ) {
|
2019-07-24 23:23:13 +08:00
|
|
|
$ZoneMinder::Database::dbh->commit();
|
2019-07-24 21:37:38 +08:00
|
|
|
Error($_);
|
|
|
|
} else {
|
|
|
|
$Event->save({SecondaryStorageId=>$$NewStorage{Id}});
|
2019-07-24 23:23:13 +08:00
|
|
|
$ZoneMinder::Database::dbh->commit();
|
2019-07-24 21:37:38 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Error("No storage area found for copy to operation. AutoCopyTo was $$filter{AutoCopyTo}");
|
|
|
|
}
|
|
|
|
} # end if AutoCopy
|
2017-12-05 00:05:50 +08:00
|
|
|
|
2017-10-27 23:03:09 +08:00
|
|
|
if ( $filter->{UpdateDiskSpace} ) {
|
2020-12-04 03:26:51 +08:00
|
|
|
if ( $$filter{LockRows} ) {
|
|
|
|
$ZoneMinder::Database::dbh->begin_work();
|
|
|
|
$Event->lock_and_load();
|
|
|
|
}
|
2018-01-19 00:38:08 +08:00
|
|
|
|
2018-01-18 04:53:23 +08:00
|
|
|
my $old_diskspace = $$Event{DiskSpace};
|
2018-04-20 02:22:19 +08:00
|
|
|
my $new_diskspace = $Event->DiskSpace(undef);
|
|
|
|
|
|
|
|
if (
|
|
|
|
( (!defined $old_diskspace) and defined $new_diskspace)
|
|
|
|
or
|
|
|
|
( (defined $old_diskspace) and (defined $new_diskspace) and ( $old_diskspace != $Event->DiskSpace(undef) ) )
|
|
|
|
) {
|
2018-01-18 04:53:23 +08:00
|
|
|
$Event->save();
|
|
|
|
}
|
2020-12-04 03:26:51 +08:00
|
|
|
$ZoneMinder::Database::dbh->commit() if !$$filter{LockRows};
|
2017-10-27 23:03:09 +08:00
|
|
|
} # end if UpdateDiskSpace
|
2017-03-21 04:47:58 +08:00
|
|
|
} # end foreach event
|
2020-10-27 06:47:40 +08:00
|
|
|
ZoneMinder::Database::end_transaction($dbh, $in_transaction) if $$filter{LockRows};
|
2019-07-24 21:37:38 +08:00
|
|
|
} # end sub checkFilter
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2017-05-18 05:47:39 +08:00
|
|
|
sub generateVideo {
|
2017-03-21 04:47:58 +08:00
|
|
|
my $filter = shift;
|
2018-10-08 21:25:17 +08:00
|
|
|
my $Event = shift;
|
2017-03-21 04:47:58 +08:00
|
|
|
my $phone = shift;
|
|
|
|
|
2020-11-14 06:04:22 +08:00
|
|
|
my $Monitor = $Event->Monitor();
|
|
|
|
my $rate = $$Monitor{DefaultRate}/100;
|
|
|
|
my $scale = $$Monitor{DefaultScale}/100;
|
2017-03-21 04:47:58 +08:00
|
|
|
my $format;
|
|
|
|
|
2018-04-30 22:56:51 +08:00
|
|
|
my @ffmpeg_formats = split(/\s+/, $Config{ZM_FFMPEG_FORMATS});
|
2017-03-21 04:47:58 +08:00
|
|
|
my $default_video_format;
|
|
|
|
my $default_phone_format;
|
2017-05-18 05:47:39 +08:00
|
|
|
foreach my $ffmpeg_format( @ffmpeg_formats ) {
|
|
|
|
if ( $ffmpeg_format =~ /^(.+)\*\*$/ ) {
|
2017-03-21 04:47:58 +08:00
|
|
|
$default_phone_format = $1;
|
2017-05-18 05:47:39 +08:00
|
|
|
} elsif ( $ffmpeg_format =~ /^(.+)\*$/ ) {
|
2017-03-21 04:47:58 +08:00
|
|
|
$default_video_format = $1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( $phone && $default_phone_format ) {
|
2017-03-21 04:47:58 +08:00
|
|
|
$format = $default_phone_format;
|
2017-05-18 05:47:39 +08:00
|
|
|
} elsif ( $default_video_format ) {
|
2017-03-21 04:47:58 +08:00
|
|
|
$format = $default_video_format;
|
2017-05-18 05:47:39 +08:00
|
|
|
} else {
|
2017-03-21 04:47:58 +08:00
|
|
|
$format = $ffmpeg_formats[0];
|
|
|
|
}
|
|
|
|
|
2018-06-21 22:49:51 +08:00
|
|
|
my $command = join('',
|
2017-06-02 23:39:35 +08:00
|
|
|
$Config{ZM_PATH_BIN},
|
|
|
|
'/zmvideo.pl -e ',
|
2018-10-08 21:25:17 +08:00
|
|
|
$Event->{Id},
|
2017-06-02 23:54:40 +08:00
|
|
|
' -r ',
|
|
|
|
$rate,
|
|
|
|
' -s ',
|
|
|
|
$scale,
|
|
|
|
' -f ',
|
|
|
|
$format,
|
2017-06-02 23:39:35 +08:00
|
|
|
);
|
2017-03-21 04:47:58 +08:00
|
|
|
my $output = qx($command);
|
2018-06-21 22:49:51 +08:00
|
|
|
chomp($output);
|
2017-03-21 04:47:58 +08:00
|
|
|
my $status = $? >> 8;
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( $status || logDebugging() ) {
|
2018-10-11 23:28:07 +08:00
|
|
|
Debug("Output: $output");
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( $status ) {
|
2018-10-11 23:28:07 +08:00
|
|
|
Error("Video generation '$command' failed with status: $status");
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( wantarray() ) {
|
2017-03-21 04:47:58 +08:00
|
|
|
return( undef, undef );
|
|
|
|
}
|
2018-04-21 03:22:31 +08:00
|
|
|
return 0;
|
2017-05-18 05:47:39 +08:00
|
|
|
} else {
|
2019-08-26 00:30:06 +08:00
|
|
|
my $sql = 'UPDATE `Events` SET `Videoed` = 1 WHERE `Id` = ?';
|
2018-04-30 22:56:51 +08:00
|
|
|
my $sth = $dbh->prepare_cached($sql)
|
|
|
|
or Fatal("Unable to prepare '$sql': ".$dbh->errstr());
|
2018-10-08 21:25:17 +08:00
|
|
|
my $res = $sth->execute($Event->{Id})
|
2018-10-05 21:45:47 +08:00
|
|
|
or Fatal("Unable to execute '$sql': ".$dbh->errstr());
|
2017-06-02 23:39:35 +08:00
|
|
|
if ( wantarray() ) {
|
2017-03-21 04:47:58 +08:00
|
|
|
return( $format, $output );
|
2007-09-04 22:52:26 +08:00
|
|
|
}
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2018-04-21 03:22:31 +08:00
|
|
|
return 1;
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2007-09-04 22:52:26 +08:00
|
|
|
|
2017-05-17 01:04:42 +08:00
|
|
|
# Returns an image absolute path for given event and frame
|
|
|
|
# Optionally an analyse image path may be returned if an analyse image exists
|
|
|
|
# If neither capture nor analyse image exists it will try to extract a frame from .mp4 file if exists
|
|
|
|
# An empty string is returned if no one from methods above works
|
2017-05-18 05:47:39 +08:00
|
|
|
sub generateImage {
|
2018-04-21 03:22:31 +08:00
|
|
|
my $Event = shift;
|
2017-06-02 23:39:35 +08:00
|
|
|
my $frame = shift;
|
2018-04-21 03:56:14 +08:00
|
|
|
my $analyse = @_ ? shift : 0; # don't return analyse image by default
|
2017-06-02 23:39:35 +08:00
|
|
|
|
2018-04-21 03:22:31 +08:00
|
|
|
my $event_path = $Event->Path();
|
|
|
|
my $capture_image_path = sprintf('%s/%0'.$Config{ZM_EVENT_IMAGE_DIGITS}.'d-capture.jpg', $event_path, $frame->{FrameId});
|
|
|
|
my $analyse_image_path = sprintf('%s/%0'.$Config{ZM_EVENT_IMAGE_DIGITS}.'d-analyse.jpg', $event_path, $frame->{FrameId}) if $analyse;
|
|
|
|
my $video_path = sprintf('%s/%d-video.mp4', $event_path, $Event->{Id});
|
2017-06-02 23:39:35 +08:00
|
|
|
my $image_path = '';
|
|
|
|
|
|
|
|
# check if the image file exists. If the file doesn't exist and we use H264 try to extract it from .mp4 video
|
|
|
|
if ( $analyse && -r $analyse_image_path ) {
|
|
|
|
$image_path = $analyse_image_path;
|
|
|
|
} elsif ( -r $capture_image_path ) {
|
|
|
|
$image_path = $capture_image_path;
|
2018-04-21 03:56:14 +08:00
|
|
|
} elsif ( -r $video_path ) {
|
2019-06-10 21:43:56 +08:00
|
|
|
my $command ="ffmpeg -nostdin -ss $$frame{Delta} -i '$video_path' -frames:v 1 '$capture_image_path'";
|
2018-04-21 03:56:14 +08:00
|
|
|
#$command = "ffmpeg -y -v 0 -i $video_path -vf 'select=gte(n\\,$$frame{FrameId}),setpts=PTS-STARTPTS' -vframes 1 -f image2 $capture_image_path";
|
|
|
|
my $output = qx($command);
|
2019-06-10 21:43:56 +08:00
|
|
|
chomp($output);
|
2018-04-21 03:56:14 +08:00
|
|
|
my $status = $? >> 8;
|
|
|
|
if ( $status || logDebugging() ) {
|
2018-10-11 23:28:07 +08:00
|
|
|
Debug("Output: $output");
|
2018-04-21 03:56:14 +08:00
|
|
|
}
|
|
|
|
if ( $status ) {
|
|
|
|
Error("Failed $command status $status");
|
|
|
|
} else {
|
2017-06-02 23:39:35 +08:00
|
|
|
$image_path = $capture_image_path;
|
2017-05-17 01:04:42 +08:00
|
|
|
}
|
2017-06-02 23:39:35 +08:00
|
|
|
}
|
|
|
|
return $image_path;
|
2017-05-17 01:04:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-18 05:47:39 +08:00
|
|
|
sub uploadArchFile {
|
2017-03-21 04:47:58 +08:00
|
|
|
my $filter = shift;
|
2018-10-08 21:25:17 +08:00
|
|
|
my $Event = shift;
|
2017-03-21 04:47:58 +08:00
|
|
|
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( ! $Config{ZM_UPLOAD_HOST} ) {
|
2018-04-30 22:56:51 +08:00
|
|
|
Error('Cannot upload archive as no upload host defined');
|
2017-03-21 04:47:58 +08:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2018-10-12 04:57:25 +08:00
|
|
|
# Try to make the path to the upload folder if it doesn't already exist
|
|
|
|
make_path( $Config{ZM_UPLOAD_LOC_DIR} );
|
|
|
|
|
|
|
|
# Complain if the upload folder is still not writable
|
|
|
|
if ( ! -w $Config{ZM_UPLOAD_LOC_DIR} ) {
|
|
|
|
Error("Upload folder either does not exist or is not writable: $Config{ZM_UPLOAD_LOC_DIR}");
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2018-10-08 21:25:17 +08:00
|
|
|
my $archFile = $Event->{MonitorName}.'-'.$Event->{Id};
|
|
|
|
my $archImagePath = $Event->Path()
|
2017-06-02 23:39:35 +08:00
|
|
|
.'/'
|
2017-03-21 04:47:58 +08:00
|
|
|
.(
|
|
|
|
( $Config{ZM_UPLOAD_ARCH_ANALYSE} )
|
2018-10-11 23:28:07 +08:00
|
|
|
? '{*analyse.jpg,*capture.jpg,snapshot.jpg,*.mp4}'
|
2018-10-12 03:32:40 +08:00
|
|
|
: '{*capture.jpg,snapshot.jpg,*.mp4}'
|
2017-03-21 04:47:58 +08:00
|
|
|
)
|
|
|
|
;
|
|
|
|
my @archImageFiles = glob($archImagePath);
|
|
|
|
my $archLocPath;
|
|
|
|
|
|
|
|
my $archError = 0;
|
2017-06-02 23:39:35 +08:00
|
|
|
if ( $Config{ZM_UPLOAD_ARCH_FORMAT} eq 'zip' ) {
|
2017-03-21 04:47:58 +08:00
|
|
|
$archFile .= '.zip';
|
|
|
|
$archLocPath = $Config{ZM_UPLOAD_LOC_DIR}.'/'.$archFile;
|
|
|
|
my $zip = Archive::Zip->new();
|
2018-10-11 23:28:07 +08:00
|
|
|
Info("Creating upload file '$archLocPath', ".int(@archImageFiles).' files');
|
2017-03-21 04:47:58 +08:00
|
|
|
|
|
|
|
my $status = &AZ_OK;
|
2017-05-18 05:47:39 +08:00
|
|
|
foreach my $imageFile ( @archImageFiles ) {
|
2018-10-11 23:28:07 +08:00
|
|
|
Debug("Adding $imageFile");
|
2018-04-30 22:56:51 +08:00
|
|
|
my $member = $zip->addFile($imageFile);
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( !$member ) {
|
2019-10-21 22:27:41 +08:00
|
|
|
Error("Unable to add image file $imageFile to zip archive $archLocPath");
|
2017-03-21 04:47:58 +08:00
|
|
|
$archError = 1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
$member->desiredCompressionMethod( $Config{ZM_UPLOAD_ARCH_COMPRESS}
|
|
|
|
? &COMPRESSION_DEFLATED
|
|
|
|
: &COMPRESSION_STORED
|
|
|
|
);
|
|
|
|
}
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( !$archError ) {
|
2017-03-21 04:47:58 +08:00
|
|
|
$status = $zip->writeToFileNamed( $archLocPath );
|
|
|
|
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( $archError = ($status != &AZ_OK) ) {
|
2018-04-30 22:56:51 +08:00
|
|
|
Error("Zip error: $status");
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2017-05-18 05:47:39 +08:00
|
|
|
} else {
|
2018-04-30 22:56:51 +08:00
|
|
|
Error("Error adding images to zip archive $archLocPath, not writing");
|
2007-09-04 22:52:26 +08:00
|
|
|
}
|
2017-06-02 23:39:35 +08:00
|
|
|
} elsif ( $Config{ZM_UPLOAD_ARCH_FORMAT} eq 'tar' ) {
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( $Config{ZM_UPLOAD_ARCH_COMPRESS} ) {
|
2017-03-21 04:47:58 +08:00
|
|
|
$archFile .= '.tar.gz';
|
2017-05-18 05:47:39 +08:00
|
|
|
} else {
|
2017-03-21 04:47:58 +08:00
|
|
|
$archFile .= '.tar';
|
2009-03-04 01:51:27 +08:00
|
|
|
}
|
2017-03-21 04:47:58 +08:00
|
|
|
$archLocPath = $Config{ZM_UPLOAD_LOC_DIR}.'/'.$archFile;
|
2018-10-11 23:28:07 +08:00
|
|
|
Info("Creating upload file '$archLocPath', ".int(@archImageFiles).' files');
|
2009-03-04 01:51:27 +08:00
|
|
|
|
2017-03-21 04:47:58 +08:00
|
|
|
if ( $archError = !Archive::Tar->create_archive(
|
|
|
|
$archLocPath,
|
|
|
|
$Config{ZM_UPLOAD_ARCH_COMPRESS},
|
|
|
|
@archImageFiles
|
|
|
|
)
|
2017-05-18 05:47:39 +08:00
|
|
|
) {
|
2018-04-30 22:56:51 +08:00
|
|
|
Error('Tar error: '.Archive::Tar->error());
|
2007-09-04 22:52:26 +08:00
|
|
|
}
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2007-09-04 22:52:26 +08:00
|
|
|
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( $archError ) {
|
2019-05-09 10:45:04 +08:00
|
|
|
return 0;
|
2017-05-18 05:47:39 +08:00
|
|
|
} else {
|
2017-06-02 23:39:35 +08:00
|
|
|
if ( $Config{ZM_UPLOAD_PROTOCOL} eq 'ftp' ) {
|
2018-10-05 21:45:47 +08:00
|
|
|
Info('Uploading to '.$Config{ZM_UPLOAD_HOST}.' using FTP');
|
2017-06-02 23:39:35 +08:00
|
|
|
my $ftp = Net::FTP->new(
|
|
|
|
$Config{ZM_UPLOAD_HOST},
|
|
|
|
Timeout=>$Config{ZM_UPLOAD_TIMEOUT},
|
|
|
|
Passive=>$Config{ZM_UPLOAD_FTP_PASSIVE},
|
|
|
|
Debug=>$Config{ZM_UPLOAD_DEBUG}
|
|
|
|
);
|
|
|
|
if ( !$ftp ) {
|
2018-10-05 21:45:47 +08:00
|
|
|
Error("Unable to create FTP connection: $@");
|
2018-04-30 22:56:51 +08:00
|
|
|
return 0;
|
2017-06-02 23:39:35 +08:00
|
|
|
}
|
2018-04-30 22:56:51 +08:00
|
|
|
$ftp->login($Config{ZM_UPLOAD_USER}, $Config{ZM_UPLOAD_PASS})
|
2018-10-05 21:45:47 +08:00
|
|
|
or Error("FTP - Unable to login");
|
2017-06-02 23:39:35 +08:00
|
|
|
$ftp->binary()
|
2018-10-05 21:45:47 +08:00
|
|
|
or Error("FTP - Unable to go binary");
|
2018-04-30 22:56:51 +08:00
|
|
|
$ftp->cwd($Config{ZM_UPLOAD_REM_DIR})
|
2018-10-05 21:45:47 +08:00
|
|
|
or Error("FTP - Unable to cwd")
|
2017-06-02 23:39:35 +08:00
|
|
|
if ( $Config{ZM_UPLOAD_REM_DIR} );
|
|
|
|
$ftp->put( $archLocPath )
|
2018-10-05 21:45:47 +08:00
|
|
|
or Error("FTP - Unable to upload '$archLocPath'");
|
2017-06-02 23:39:35 +08:00
|
|
|
$ftp->quit()
|
2018-10-05 21:45:47 +08:00
|
|
|
or Error("FTP - Unable to quit");
|
2017-06-02 23:39:35 +08:00
|
|
|
} else {
|
|
|
|
my $host = $Config{ZM_UPLOAD_HOST};
|
2018-04-30 22:56:51 +08:00
|
|
|
$host .= ':'.$Config{ZM_UPLOAD_PORT} if $Config{ZM_UPLOAD_PORT};
|
2018-10-05 21:45:47 +08:00
|
|
|
Info('Uploading to '.$host.' using SFTP');
|
2018-04-30 22:56:51 +08:00
|
|
|
my %sftpOptions = (
|
2018-10-25 23:24:23 +08:00
|
|
|
host=>$Config{ZM_UPLOAD_HOST},
|
|
|
|
user=>$Config{ZM_UPLOAD_USER},
|
2018-04-30 22:56:51 +08:00
|
|
|
($Config{ZM_UPLOAD_PASS} ? (password=>$Config{ZM_UPLOAD_PASS}) : ()),
|
|
|
|
($Config{ZM_UPLOAD_PORT} ? (port=>$Config{ZM_UPLOAD_PORT}) : ()),
|
|
|
|
($Config{ZM_UPLOAD_TIMEOUT} ? (timeout=>$Config{ZM_UPLOAD_TIMEOUT}) : ()),
|
|
|
|
);
|
2017-06-02 23:39:35 +08:00
|
|
|
my @more_ssh_args;
|
|
|
|
push @more_ssh_args, '-o'=>'StrictHostKeyChecking=no'
|
|
|
|
if ! $Config{ZM_UPLOAD_STRICT};
|
|
|
|
push @more_ssh_args, '-v'
|
|
|
|
if $Config{ZM_UPLOAD_DEBUG};
|
|
|
|
$sftpOptions{more} = [@more_ssh_args];
|
2018-04-30 22:56:51 +08:00
|
|
|
my $sftp = Net::SFTP::Foreign->new($Config{ZM_UPLOAD_HOST}, %sftpOptions);
|
2017-06-02 23:39:35 +08:00
|
|
|
if ( $sftp->error ) {
|
2018-10-05 21:45:47 +08:00
|
|
|
Error("Unable to create SFTP connection: ".$sftp->error);
|
2018-04-30 22:56:51 +08:00
|
|
|
return 0;
|
2017-06-02 23:39:35 +08:00
|
|
|
}
|
2018-04-30 22:56:51 +08:00
|
|
|
$sftp->setcwd($Config{ZM_UPLOAD_REM_DIR})
|
|
|
|
or Error("SFTP - Unable to setcwd: ".$sftp->error)
|
2017-06-02 23:39:35 +08:00
|
|
|
if $Config{ZM_UPLOAD_REM_DIR};
|
2018-04-30 22:56:51 +08:00
|
|
|
$sftp->put($archLocPath, $archFile)
|
|
|
|
or Error("SFTP - Unable to upload '$archLocPath': ".$sftp->error);
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2018-04-30 22:56:51 +08:00
|
|
|
unlink($archLocPath);
|
2019-08-26 00:30:06 +08:00
|
|
|
my $sql = 'UPDATE `Events` SET `Uploaded` = 1 WHERE `Id` = ?';
|
2018-04-30 22:56:51 +08:00
|
|
|
my $sth = $dbh->prepare_cached($sql)
|
2018-10-05 21:45:47 +08:00
|
|
|
or Fatal("Unable to prepare '$sql': ".$dbh->errstr());
|
2018-10-08 21:25:17 +08:00
|
|
|
my $res = $sth->execute($Event->{Id})
|
2018-10-05 21:45:47 +08:00
|
|
|
or Fatal("Unable to execute '$sql': ".$dbh->errstr());
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2018-04-30 22:56:51 +08:00
|
|
|
return 1;
|
|
|
|
} # end sub uploadArchFile
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2017-05-18 05:47:39 +08:00
|
|
|
sub substituteTags {
|
2017-03-21 04:47:58 +08:00
|
|
|
my $text = shift;
|
|
|
|
my $filter = shift;
|
2018-04-21 03:22:31 +08:00
|
|
|
my $Event = shift;
|
2017-03-21 04:47:58 +08:00
|
|
|
my $attachments_ref = shift;
|
|
|
|
|
|
|
|
# First we'd better check what we need to get
|
|
|
|
# We have a filter and an event, do we need any more
|
|
|
|
# monitor information?
|
2020-11-22 04:10:23 +08:00
|
|
|
my $need_monitor = $text =~ /%(?:MN|MET|MEH|MED|MEW|MEN|MEA)%/;
|
|
|
|
my $need_status = $text =~ /%(?:MET|MEH|MED|MEW|MEN|MEA)%/;
|
2017-03-21 04:47:58 +08:00
|
|
|
|
2018-04-21 03:22:31 +08:00
|
|
|
my $Monitor = $Event->Monitor() if $need_monitor;
|
2020-11-22 04:10:23 +08:00
|
|
|
my $Status = $Monitor->Status() if $need_status;
|
2017-03-21 04:47:58 +08:00
|
|
|
|
|
|
|
# Do we need the image information too?
|
2019-08-07 23:15:50 +08:00
|
|
|
my $need_images = $text =~ /%(?:EPI1|EPIM|EI1|EIM|EI1A|EIMA|EIMOD)%/;
|
2017-03-21 04:47:58 +08:00
|
|
|
my $first_alarm_frame;
|
|
|
|
my $max_alarm_frame;
|
|
|
|
my $max_alarm_score = 0;
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( $need_images ) {
|
2019-08-26 00:30:06 +08:00
|
|
|
my $sql = 'SELECT * FROM `Frames` WHERE `EventId`=? AND `Type`=? ORDER BY `FrameId`';
|
2018-04-30 22:56:51 +08:00
|
|
|
my $sth = $dbh->prepare_cached($sql)
|
2018-10-05 21:45:47 +08:00
|
|
|
or Fatal("Unable to prepare '$sql': ".$dbh->errstr());
|
2019-08-26 00:30:06 +08:00
|
|
|
my $res = $sth->execute($Event->{Id},'Alarm')
|
2018-10-05 21:45:47 +08:00
|
|
|
or Fatal("Unable to execute '$sql': ".$dbh->errstr());
|
2018-04-21 03:56:14 +08:00
|
|
|
my $rows = 0;
|
2017-05-18 05:47:39 +08:00
|
|
|
while( my $frame = $sth->fetchrow_hashref() ) {
|
|
|
|
if ( !$first_alarm_frame ) {
|
2017-03-21 04:47:58 +08:00
|
|
|
$first_alarm_frame = $frame;
|
|
|
|
}
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( $frame->{Score} > $max_alarm_score ) {
|
2017-03-21 04:47:58 +08:00
|
|
|
$max_alarm_frame = $frame;
|
|
|
|
$max_alarm_score = $frame->{Score};
|
|
|
|
}
|
2018-04-21 03:56:14 +08:00
|
|
|
$rows ++;
|
2007-09-04 22:52:26 +08:00
|
|
|
}
|
2018-04-21 03:56:14 +08:00
|
|
|
Debug("Frames: rows: $rows first alarm frame: $first_alarm_frame max_alaarm_frame: $max_alarm_frame, score: $max_alarm_score");
|
2017-03-21 04:47:58 +08:00
|
|
|
$sth->finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
my $url = $Config{ZM_URL};
|
|
|
|
$text =~ s/%ZP%/$url/g;
|
2020-11-22 04:10:23 +08:00
|
|
|
$text =~ s/%MN%/$Monitor->{Name}/g;
|
|
|
|
$text =~ s/%MET%/$Status->{TotalEvents}/g;
|
|
|
|
$text =~ s/%MEH%/$Status->{HourEvents}/g;
|
|
|
|
$text =~ s/%MED%/$Status->{DayEvents}/g;
|
|
|
|
$text =~ s/%MEW%/$Status->{WeekEvents}/g;
|
|
|
|
$text =~ s/%MEM%/$Status->{MonthEvents}/g;
|
|
|
|
$text =~ s/%MEA%/$Status->{ArchivedEvents}/g;
|
2018-04-21 03:22:31 +08:00
|
|
|
$text =~ s/%MP%/$url?view=watch&mid=$Event->{MonitorId}/g;
|
2018-04-23 23:38:25 +08:00
|
|
|
$text =~ s/%MPS%/$url?view=watch&mid=$Event->{MonitorId}&mode=stream/g;
|
|
|
|
$text =~ s/%MPI%/$url?view=watch&mid=$Event->{MonitorId}&mode=still/g;
|
2018-04-21 03:22:31 +08:00
|
|
|
$text =~ s/%EP%/$url?view=event&mid=$Event->{MonitorId}&eid=$Event->{Id}/g;
|
|
|
|
$text =~ s/%EPS%/$url?view=event&mode=stream&mid=$Event->{MonitorId}&eid=$Event->{Id}/g;
|
|
|
|
$text =~ s/%EPI%/$url?view=event&mode=still&mid=$Event->{MonitorId}&eid=$Event->{Id}/g;
|
|
|
|
$text =~ s/%EI%/$Event->{Id}/g;
|
|
|
|
$text =~ s/%EN%/$Event->{Name}/g;
|
|
|
|
$text =~ s/%EC%/$Event->{Cause}/g;
|
|
|
|
$text =~ s/%ED%/$Event->{Notes}/g;
|
2020-11-05 02:49:39 +08:00
|
|
|
$text =~ s/%ET%/$Event->{StartDateTime}/g;
|
2020-09-15 02:04:18 +08:00
|
|
|
$text =~ s/%EVF%/$$Event{DefaultVideo}/g; # Event video filename
|
2018-04-21 03:22:31 +08:00
|
|
|
$text =~ s/%EL%/$Event->{Length}/g;
|
|
|
|
$text =~ s/%EF%/$Event->{Frames}/g;
|
|
|
|
$text =~ s/%EFA%/$Event->{AlarmFrames}/g;
|
|
|
|
$text =~ s/%EST%/$Event->{TotScore}/g;
|
|
|
|
$text =~ s/%ESA%/$Event->{AvgScore}/g;
|
|
|
|
$text =~ s/%ESM%/$Event->{MaxScore}/g;
|
2017-06-02 23:39:35 +08:00
|
|
|
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( $first_alarm_frame ) {
|
2020-01-23 02:58:44 +08:00
|
|
|
$text =~ s/%EPF1%/$url?view=frame&mid=$Event->{MonitorId}&eid=$Event->{Id}&fid=$first_alarm_frame->{FrameId}/g;
|
|
|
|
$text =~ s/%EPFM%/$url?view=frame&mid=$Event->{MonitorId}&eid=$Event->{Id}&fid=$max_alarm_frame->{FrameId}/g;
|
|
|
|
$text =~ s/%EPI1%/$url?view=image&mid=$Event->{MonitorId}&eid=$Event->{Id}&fid=$first_alarm_frame->{FrameId}/g;
|
|
|
|
$text =~ s/%EPIM%/$url?view=image&mid=$Event->{MonitorId}&eid=$Event->{Id}&fid=$max_alarm_frame->{FrameId}/g;
|
2019-05-09 10:45:04 +08:00
|
|
|
if ( $attachments_ref ) {
|
|
|
|
if ( $text =~ s/%EI1%//g ) {
|
|
|
|
my $path = generateImage($Event, $first_alarm_frame);
|
|
|
|
if ( -e $path ) {
|
|
|
|
push @$attachments_ref, { type=>'image/jpeg', path=>$path };
|
2019-05-24 04:26:21 +08:00
|
|
|
} else {
|
|
|
|
Warning("Path to first image does not exist at $path");
|
2019-05-09 10:45:04 +08:00
|
|
|
}
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2017-06-02 23:39:35 +08:00
|
|
|
|
2019-05-09 10:45:04 +08:00
|
|
|
if ( $text =~ s/%EIM%//g ) {
|
|
|
|
# Don't attach the same image twice
|
|
|
|
if ( !@$attachments_ref
|
2018-04-21 03:56:14 +08:00
|
|
|
|| ( $first_alarm_frame->{FrameId} != $max_alarm_frame->{FrameId} )
|
2019-05-09 10:45:04 +08:00
|
|
|
) {
|
|
|
|
my $path = generateImage($Event, $max_alarm_frame);
|
|
|
|
if ( -e $path ) {
|
|
|
|
push @$attachments_ref, { type=>'image/jpeg', path=>$path };
|
|
|
|
} else {
|
2019-05-24 04:26:21 +08:00
|
|
|
Warning("No image for EIM at $path");
|
2019-05-09 10:45:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-24 04:26:21 +08:00
|
|
|
|
2019-05-09 10:45:04 +08:00
|
|
|
if ( $text =~ s/%EI1A%//g ) {
|
|
|
|
my $path = generateImage($Event, $first_alarm_frame, 'analyse');
|
2017-06-02 23:39:35 +08:00
|
|
|
if ( -e $path ) {
|
2018-04-21 03:22:31 +08:00
|
|
|
push @$attachments_ref, { type=>'image/jpeg', path=>$path };
|
2018-04-21 03:56:14 +08:00
|
|
|
} else {
|
2019-05-24 04:26:21 +08:00
|
|
|
Warning("No image for EI1A at $path");
|
2007-09-04 22:52:26 +08:00
|
|
|
}
|
2017-06-02 23:39:35 +08:00
|
|
|
}
|
2019-05-24 04:26:21 +08:00
|
|
|
|
2019-05-09 10:45:04 +08:00
|
|
|
if ( $text =~ s/%EIMA%//g ) {
|
|
|
|
# Don't attach the same image twice
|
|
|
|
if ( !@$attachments_ref
|
2018-04-30 22:56:51 +08:00
|
|
|
|| ($first_alarm_frame->{FrameId} != $max_alarm_frame->{FrameId})
|
2019-05-09 10:45:04 +08:00
|
|
|
) {
|
|
|
|
my $path = generateImage($Event, $max_alarm_frame, 'analyse');
|
|
|
|
if ( -e $path ) {
|
|
|
|
push @$attachments_ref, { type=>'image/jpeg', path=>$path };
|
|
|
|
} else {
|
2019-05-24 04:26:21 +08:00
|
|
|
Warning("No image for EIMA at $path");
|
2019-05-09 10:45:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-24 04:26:21 +08:00
|
|
|
|
2020-01-23 02:58:44 +08:00
|
|
|
if ( $text =~ s/%EIMOD%//g or $text =~ s/%EFMOD%//g ) {
|
|
|
|
$text =~ s/%EFMOD%/$url?view=frame&mid=$Event->{MonitorId}&eid=$Event->{Id}&fid=objdetect/g;
|
|
|
|
$text =~ s/%EIMOD%/$url?view=image&mid=$Event->{MonitorId}&eid=$Event->{Id}&fid=objdetect/g;
|
2019-05-09 10:45:04 +08:00
|
|
|
my $path = $Event->Path().'/objdetect.jpg';
|
2017-06-02 23:39:35 +08:00
|
|
|
if ( -e $path ) {
|
2018-04-21 03:22:31 +08:00
|
|
|
push @$attachments_ref, { type=>'image/jpeg', path=>$path };
|
2018-04-21 03:56:14 +08:00
|
|
|
} else {
|
2020-01-23 02:58:44 +08:00
|
|
|
Warning('No image for MOD at '.$path);
|
2017-05-17 01:04:42 +08:00
|
|
|
}
|
2017-06-02 23:39:35 +08:00
|
|
|
}
|
2019-05-09 10:45:04 +08:00
|
|
|
|
|
|
|
} # end if attachments_ref
|
2017-06-02 23:39:35 +08:00
|
|
|
} # end if $first_alarm_frame
|
|
|
|
|
2018-09-11 20:49:42 +08:00
|
|
|
if ( $attachments_ref ) {
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( $text =~ s/%EV%//g ) {
|
2018-09-11 20:49:42 +08:00
|
|
|
if ( $$Event{DefaultVideo} ) {
|
|
|
|
push @$attachments_ref, { type=>'video/mp4', path=>join('/',$Event->Path(), $Event->DefaultVideo()) };
|
|
|
|
} elsif ( $Config{ZM_OPT_FFMPEG} ) {
|
|
|
|
my ( $format, $path ) = generateVideo($filter, $Event);
|
|
|
|
if ( !$format ) {
|
|
|
|
return undef;
|
|
|
|
}
|
2019-05-09 10:45:04 +08:00
|
|
|
push @$attachments_ref, { type=>"video/$format", path=>$path };
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
}
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( $text =~ s/%EVM%//g ) {
|
2018-04-21 03:22:31 +08:00
|
|
|
my ( $format, $path ) = generateVideo($filter, $Event, 1);
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( !$format ) {
|
2018-04-21 03:22:31 +08:00
|
|
|
return undef;
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2018-04-21 03:22:31 +08:00
|
|
|
push @$attachments_ref, { type=>"video/$format", path=>$path };
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$text =~ s/%FN%/$filter->{Name}/g;
|
|
|
|
( my $filter_name = $filter->{Name} ) =~ s/ /+/g;
|
2018-04-21 03:22:31 +08:00
|
|
|
$text =~ s/%FP%/$url?view=filter&mid=$Event->{MonitorId}&filter_name=$filter_name/g;
|
2017-03-21 04:47:58 +08:00
|
|
|
|
2018-04-21 03:22:31 +08:00
|
|
|
return $text;
|
2017-06-02 23:39:35 +08:00
|
|
|
} # end subsitituteTags
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2017-05-18 05:47:39 +08:00
|
|
|
sub sendEmail {
|
2017-03-21 04:47:58 +08:00
|
|
|
my $filter = shift;
|
2018-04-21 03:22:31 +08:00
|
|
|
my $Event = shift;
|
2017-03-21 04:47:58 +08:00
|
|
|
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( ! $Config{ZM_FROM_EMAIL} ) {
|
2018-04-30 22:56:51 +08:00
|
|
|
Error('No from email address defined, not sending email');
|
2018-04-21 03:22:31 +08:00
|
|
|
return 0;
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2020-01-23 02:58:44 +08:00
|
|
|
if ( ! $$filter{EmailTo} ) {
|
2018-04-21 03:22:31 +08:00
|
|
|
Error('No email address defined, not sending email');
|
|
|
|
return 0;
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
|
2018-04-30 22:56:51 +08:00
|
|
|
Info('Creating notification email');
|
2017-03-21 04:47:58 +08:00
|
|
|
|
2020-01-23 02:58:44 +08:00
|
|
|
my $subject = substituteTags($$filter{EmailSubject}, $filter, $Event);
|
2018-04-21 03:22:31 +08:00
|
|
|
return 0 if !$subject;
|
2017-03-21 04:47:58 +08:00
|
|
|
my @attachments;
|
2020-01-23 02:58:44 +08:00
|
|
|
my $body = substituteTags($$filter{EmailBody}, $filter, $Event, \@attachments);
|
2018-04-21 03:22:31 +08:00
|
|
|
return 0 if !$body;
|
2017-03-21 04:47:58 +08:00
|
|
|
|
2018-04-30 22:56:51 +08:00
|
|
|
Info("Sending notification email '$subject'");
|
2017-03-21 04:47:58 +08:00
|
|
|
|
2017-05-18 05:47:39 +08:00
|
|
|
eval {
|
|
|
|
if ( $Config{ZM_NEW_MAIL_MODULES} ) {
|
2017-03-21 04:47:58 +08:00
|
|
|
### Create the multipart container
|
|
|
|
my $mail = MIME::Lite->new (
|
|
|
|
From => $Config{ZM_FROM_EMAIL},
|
2020-01-23 02:58:44 +08:00
|
|
|
To => $$filter{EmailTo},
|
2017-03-21 04:47:58 +08:00
|
|
|
Subject => $subject,
|
2017-06-02 23:39:35 +08:00
|
|
|
Type => 'multipart/mixed'
|
2017-03-21 04:47:58 +08:00
|
|
|
);
|
|
|
|
### Add the text message part
|
|
|
|
$mail->attach (
|
2017-06-02 23:39:35 +08:00
|
|
|
Type => 'TEXT',
|
2017-03-21 04:47:58 +08:00
|
|
|
Data => $body
|
|
|
|
);
|
|
|
|
### Add the attachments
|
2020-03-28 23:19:06 +08:00
|
|
|
my $total_size = 0;
|
2017-05-18 05:47:39 +08:00
|
|
|
foreach my $attachment ( @attachments ) {
|
2020-03-28 23:19:06 +08:00
|
|
|
my $size = -s $attachment->{path};
|
|
|
|
$total_size += $size;
|
|
|
|
Info("Attaching '$attachment->{path}' which is $size bytes");
|
2017-03-21 04:47:58 +08:00
|
|
|
$mail->attach(
|
|
|
|
Path => $attachment->{path},
|
|
|
|
Type => $attachment->{type},
|
2017-06-02 23:39:35 +08:00
|
|
|
Disposition => 'attachment'
|
2007-09-04 22:52:26 +08:00
|
|
|
);
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2020-03-28 23:19:06 +08:00
|
|
|
if ( $total_size > 10*1024*1024 ) {
|
|
|
|
Warning('Emails larger than 10Mb will often not be delivered! This one is '.int($total_size/(1024*1024)).'Mb');
|
|
|
|
}
|
2017-03-21 04:47:58 +08:00
|
|
|
### Send the Message
|
|
|
|
if ( $Config{ZM_SSMTP_MAIL} ) {
|
|
|
|
my $ssmtp_location = $Config{ZM_SSMTP_PATH};
|
|
|
|
if ( !$ssmtp_location ) {
|
|
|
|
if ( logDebugging() ) {
|
2018-04-30 22:56:51 +08:00
|
|
|
Debug("which ssmtp: $ssmtp_location - set ssmtp path in options to suppress this message");
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
$ssmtp_location = qx('which ssmtp');
|
2015-04-11 09:14:29 +08:00
|
|
|
}
|
2017-03-21 04:47:58 +08:00
|
|
|
if ( !$ssmtp_location ) {
|
2019-05-09 10:45:04 +08:00
|
|
|
Warning('Unable to find ssmtp, trying MIME::Lite->send');
|
2018-04-30 22:56:51 +08:00
|
|
|
MIME::Lite->send('smtp', $Config{ZM_EMAIL_HOST}, Timeout=>60);
|
2017-03-21 04:47:58 +08:00
|
|
|
$mail->send();
|
|
|
|
} else {
|
|
|
|
### Send using SSMTP
|
2020-01-23 02:58:44 +08:00
|
|
|
$mail->send('sendmail', $ssmtp_location, $$filter{EmailTo});
|
2007-09-04 22:52:26 +08:00
|
|
|
}
|
2017-03-21 04:47:58 +08:00
|
|
|
} else {
|
2018-04-30 22:56:51 +08:00
|
|
|
MIME::Lite->send('smtp', $Config{ZM_EMAIL_HOST}, Timeout=>60);
|
2017-03-21 04:47:58 +08:00
|
|
|
$mail->send();
|
|
|
|
}
|
2017-05-18 05:47:39 +08:00
|
|
|
} else {
|
2017-03-21 04:47:58 +08:00
|
|
|
my $mail = MIME::Entity->build(
|
2020-03-28 23:19:06 +08:00
|
|
|
From => $Config{ZM_FROM_EMAIL},
|
|
|
|
To => $$filter{EmailTo},
|
|
|
|
Subject => $subject,
|
|
|
|
Type => (($body=~/<html/)?'text/html':'text/plain'),
|
|
|
|
Data => $body
|
|
|
|
);
|
2017-03-21 04:47:58 +08:00
|
|
|
|
2020-03-28 23:19:06 +08:00
|
|
|
my $total_size = 0;
|
2017-05-18 05:47:39 +08:00
|
|
|
foreach my $attachment ( @attachments ) {
|
2020-03-28 23:19:06 +08:00
|
|
|
my $size = -s $attachment->{path};
|
|
|
|
$total_size += $size;
|
|
|
|
Info("Attaching '$attachment->{path}' which is $size bytes");
|
|
|
|
|
2017-03-21 04:47:58 +08:00
|
|
|
$mail->attach(
|
2020-03-28 23:19:06 +08:00
|
|
|
Path => $attachment->{path},
|
|
|
|
Type => $attachment->{type},
|
|
|
|
Encoding => 'base64'
|
|
|
|
);
|
|
|
|
} # end foreach attachment
|
|
|
|
if ( $total_size > 10*1024*1024 ) {
|
|
|
|
Warning('Emails larger than 10Mb will often not be delivered! This one is '.int($total_size/(1024*1024)).'Mb');
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2018-04-30 22:56:51 +08:00
|
|
|
$mail->smtpsend(Host => $Config{ZM_EMAIL_HOST}, MailFrom => $Config{ZM_FROM_EMAIL});
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
};
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( $@ ) {
|
2018-10-05 21:45:47 +08:00
|
|
|
Error("Unable to send email: $@");
|
2018-04-30 22:56:51 +08:00
|
|
|
return 0;
|
2017-05-18 05:47:39 +08:00
|
|
|
} else {
|
2018-04-30 22:56:51 +08:00
|
|
|
Info('Notification email sent');
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2019-08-26 00:30:06 +08:00
|
|
|
my $sql = 'UPDATE `Events` SET `Emailed` = 1 WHERE `Id` = ?';
|
2018-04-21 03:22:31 +08:00
|
|
|
my $sth = $dbh->prepare_cached($sql)
|
2018-10-05 21:45:47 +08:00
|
|
|
or Fatal("Unable to prepare '$sql': ".$dbh->errstr());
|
2018-04-21 03:22:31 +08:00
|
|
|
my $res = $sth->execute($Event->{Id})
|
2018-10-05 21:45:47 +08:00
|
|
|
or Fatal("Unable to execute '$sql': ".$dbh->errstr());
|
2017-03-21 04:47:58 +08:00
|
|
|
|
2018-04-30 22:56:51 +08:00
|
|
|
return 1;
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
2017-05-18 05:47:39 +08:00
|
|
|
sub sendMessage {
|
2017-03-21 04:47:58 +08:00
|
|
|
my $filter = shift;
|
2018-06-09 03:14:11 +08:00
|
|
|
my $Event = shift;
|
2017-03-21 04:47:58 +08:00
|
|
|
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( ! $Config{ZM_FROM_EMAIL} ) {
|
2018-04-30 22:56:51 +08:00
|
|
|
Error('No from email address defined, not sending message');
|
|
|
|
return 0;
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( ! $Config{ZM_MESSAGE_ADDRESS} ) {
|
2018-04-30 22:56:51 +08:00
|
|
|
Error('No message address defined, not sending message');
|
|
|
|
return 0;
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
|
2018-04-30 22:56:51 +08:00
|
|
|
Info('Creating notification message');
|
2017-03-21 04:47:58 +08:00
|
|
|
|
2018-06-09 03:14:11 +08:00
|
|
|
my $subject = substituteTags($Config{ZM_MESSAGE_SUBJECT}, $filter, $Event);
|
2018-04-30 22:56:51 +08:00
|
|
|
return 0 if !$subject;
|
2017-03-21 04:47:58 +08:00
|
|
|
my @attachments;
|
2018-06-09 03:14:11 +08:00
|
|
|
my $body = substituteTags($Config{ZM_MESSAGE_BODY}, $filter, $Event, \@attachments);
|
2018-04-30 22:56:51 +08:00
|
|
|
return 0 if !$body;
|
2017-03-21 04:47:58 +08:00
|
|
|
|
2018-04-30 22:56:51 +08:00
|
|
|
Info("Sending notification message '$subject'");
|
2017-03-21 04:47:58 +08:00
|
|
|
|
2017-05-18 05:47:39 +08:00
|
|
|
eval {
|
|
|
|
if ( $Config{ZM_NEW_MAIL_MODULES} ) {
|
2017-03-21 04:47:58 +08:00
|
|
|
### Create the multipart container
|
2018-04-30 22:56:51 +08:00
|
|
|
my $mail = MIME::Lite->new(
|
2017-03-21 04:47:58 +08:00
|
|
|
From => $Config{ZM_FROM_EMAIL},
|
|
|
|
To => $Config{ZM_MESSAGE_ADDRESS},
|
|
|
|
Subject => $subject,
|
2017-06-02 23:39:35 +08:00
|
|
|
Type => 'multipart/mixed'
|
2017-03-21 04:47:58 +08:00
|
|
|
);
|
|
|
|
### Add the text message part
|
|
|
|
$mail->attach (
|
2017-06-02 23:39:35 +08:00
|
|
|
Type => 'TEXT',
|
2017-03-21 04:47:58 +08:00
|
|
|
Data => $body
|
|
|
|
);
|
|
|
|
### Add the attachments
|
2017-05-18 05:47:39 +08:00
|
|
|
foreach my $attachment ( @attachments ) {
|
2018-04-30 22:56:51 +08:00
|
|
|
Info("Attaching '$attachment->{path}");
|
2017-03-21 04:47:58 +08:00
|
|
|
$mail->attach(
|
|
|
|
Path => $attachment->{path},
|
|
|
|
Type => $attachment->{type},
|
2017-06-02 23:39:35 +08:00
|
|
|
Disposition => 'attachment'
|
2007-09-04 22:52:26 +08:00
|
|
|
);
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
### Send the Message
|
|
|
|
if ( $Config{ZM_SSMTP_MAIL} ) {
|
|
|
|
my $ssmtp_location = $Config{ZM_SSMTP_PATH};
|
|
|
|
if ( !$ssmtp_location ) {
|
|
|
|
if ( logDebugging() ) {
|
2018-04-30 22:56:51 +08:00
|
|
|
Debug("which ssmtp: $ssmtp_location - set ssmtp path in options to suppress this message");
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
$ssmtp_location = qx('which ssmtp');
|
2015-04-11 09:14:29 +08:00
|
|
|
}
|
2017-03-21 04:47:58 +08:00
|
|
|
if ( !$ssmtp_location ) {
|
2018-10-05 21:45:47 +08:00
|
|
|
Debug('Unable to find ssmtp, trying MIME::Lite->send');
|
2018-04-30 22:56:51 +08:00
|
|
|
MIME::Lite->send(smtp=>$Config{ZM_EMAIL_HOST}, Timeout=>60);
|
2017-03-21 04:47:58 +08:00
|
|
|
$mail->send();
|
|
|
|
} else {
|
|
|
|
### Send using SSMTP
|
2018-04-30 22:56:51 +08:00
|
|
|
$mail->send('sendmail', $ssmtp_location, $Config{ZM_MESSAGE_ADDRESS});
|
2007-09-04 22:52:26 +08:00
|
|
|
}
|
2017-03-21 04:47:58 +08:00
|
|
|
} else {
|
2018-04-30 22:56:51 +08:00
|
|
|
MIME::Lite->send(smtp=>$Config{ZM_EMAIL_HOST}, Timeout=>60);
|
2017-03-21 04:47:58 +08:00
|
|
|
$mail->send();
|
|
|
|
}
|
2017-05-18 05:47:39 +08:00
|
|
|
} else {
|
2017-03-21 04:47:58 +08:00
|
|
|
my $mail = MIME::Entity->build(
|
|
|
|
From => $Config{ZM_FROM_EMAIL},
|
|
|
|
To => $Config{ZM_MESSAGE_ADDRESS},
|
|
|
|
Subject => $subject,
|
2020-02-26 22:42:17 +08:00
|
|
|
Type => (($body=~/<html/)?'text/html':'text/plain'),
|
2017-03-21 04:47:58 +08:00
|
|
|
Data => $body
|
|
|
|
);
|
|
|
|
|
2017-05-18 05:47:39 +08:00
|
|
|
foreach my $attachment ( @attachments ) {
|
2018-04-30 22:56:51 +08:00
|
|
|
Info("Attaching '$attachment->{path}'");
|
2017-03-21 04:47:58 +08:00
|
|
|
$mail->attach(
|
|
|
|
Path => $attachment->{path},
|
|
|
|
Type => $attachment->{type},
|
2017-06-02 23:39:35 +08:00
|
|
|
Encoding => 'base64'
|
2017-03-21 04:47:58 +08:00
|
|
|
);
|
|
|
|
}
|
2018-04-30 22:56:51 +08:00
|
|
|
$mail->smtpsend(
|
|
|
|
Host => $Config{ZM_EMAIL_HOST},
|
2017-03-21 04:47:58 +08:00
|
|
|
MailFrom => $Config{ZM_FROM_EMAIL}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( $@ ) {
|
2018-10-05 21:45:47 +08:00
|
|
|
Error("Unable to send email: $@");
|
2018-04-30 22:56:51 +08:00
|
|
|
return 0;
|
2017-05-18 05:47:39 +08:00
|
|
|
} else {
|
2018-04-30 22:56:51 +08:00
|
|
|
Info('Notification message sent');
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2019-08-26 00:30:06 +08:00
|
|
|
my $sql = 'UPDATE `Events` SET `Messaged` = 1 WHERE `Id` = ?';
|
2018-04-30 22:56:51 +08:00
|
|
|
my $sth = $dbh->prepare_cached($sql)
|
2018-10-05 21:45:47 +08:00
|
|
|
or Fatal("Unable to prepare '$sql': ".$dbh->errstr());
|
2018-06-09 03:14:11 +08:00
|
|
|
my $res = $sth->execute($Event->{Id})
|
2018-10-05 21:45:47 +08:00
|
|
|
or Fatal("Unable to execute '$sql': ".$dbh->errstr());
|
2017-03-21 04:47:58 +08:00
|
|
|
|
2018-04-30 22:56:51 +08:00
|
|
|
return 1;
|
2018-06-09 03:14:11 +08:00
|
|
|
} # end sub sendMessage
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2017-05-18 05:47:39 +08:00
|
|
|
sub executeCommand {
|
2017-03-21 04:47:58 +08:00
|
|
|
my $filter = shift;
|
2018-10-08 21:25:17 +08:00
|
|
|
my $Event = shift;
|
2007-09-04 22:52:26 +08:00
|
|
|
|
2018-10-08 21:25:17 +08:00
|
|
|
my $event_path = $Event->Path();
|
2007-09-04 22:52:26 +08:00
|
|
|
|
2020-09-15 02:04:18 +08:00
|
|
|
my $command = $filter->{AutoExecuteCmd}.' '.$event_path;
|
2018-10-08 21:25:17 +08:00
|
|
|
$command = substituteTags($command, $filter, $Event);
|
2007-09-04 22:52:26 +08:00
|
|
|
|
2018-04-30 22:56:51 +08:00
|
|
|
Info("Executing '$command'");
|
2017-03-21 04:47:58 +08:00
|
|
|
my $output = qx($command);
|
|
|
|
my $status = $? >> 8;
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( $status || logDebugging() ) {
|
2018-04-30 22:56:51 +08:00
|
|
|
chomp($output);
|
|
|
|
Debug("Output: $output");
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
2017-05-18 05:47:39 +08:00
|
|
|
if ( $status ) {
|
2018-04-30 22:56:51 +08:00
|
|
|
Error("Command '$command' exited with status: $status");
|
|
|
|
return 0;
|
2017-05-18 05:47:39 +08:00
|
|
|
} else {
|
2019-08-26 00:30:06 +08:00
|
|
|
my $sql = 'UPDATE `Events` SET `Executed` = 1 WHERE `Id` = ?';
|
2018-04-30 22:56:51 +08:00
|
|
|
my $sth = $dbh->prepare_cached($sql)
|
|
|
|
or Fatal("Unable to prepare '$sql': ".$dbh->errstr());
|
2018-10-08 21:25:17 +08:00
|
|
|
my $res = $sth->execute( $Event->{Id} )
|
2018-04-30 22:56:51 +08:00
|
|
|
or Fatal("Unable to execute '$sql': ".$dbh->errstr());
|
2017-03-21 04:47:58 +08:00
|
|
|
}
|
|
|
|
return( 1 );
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|