reduce duplicated db code from Config. Use fully namespace'd variables so we can use require instead of use
This commit is contained in:
parent
1828ea2d6f
commit
016fda22ce
|
@ -30,6 +30,7 @@ use warnings;
|
||||||
|
|
||||||
require Exporter;
|
require Exporter;
|
||||||
require ZoneMinder::Base;
|
require ZoneMinder::Base;
|
||||||
|
|
||||||
use ZoneMinder::ConfigData qw(:all);
|
use ZoneMinder::ConfigData qw(:all);
|
||||||
|
|
||||||
our @ISA = qw(Exporter ZoneMinder::Base);
|
our @ISA = qw(Exporter ZoneMinder::Base);
|
||||||
|
@ -57,20 +58,22 @@ our %EXPORT_TAGS = (
|
||||||
push( @{$EXPORT_TAGS{config}}, @EXPORT_CONFIG );
|
push( @{$EXPORT_TAGS{config}}, @EXPORT_CONFIG );
|
||||||
push( @{$EXPORT_TAGS{all}}, @{$EXPORT_TAGS{$_}} ) foreach keys %EXPORT_TAGS;
|
push( @{$EXPORT_TAGS{all}}, @{$EXPORT_TAGS{$_}} ) foreach keys %EXPORT_TAGS;
|
||||||
|
|
||||||
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
|
our @EXPORT_OK = @{ $EXPORT_TAGS{all} };
|
||||||
|
|
||||||
our @EXPORT = qw();
|
our @EXPORT = qw();
|
||||||
|
|
||||||
our $VERSION = $ZoneMinder::Base::VERSION;
|
our $VERSION = $ZoneMinder::Base::VERSION;
|
||||||
|
|
||||||
use constant ZM_PID => "@ZM_PID@"; # Path to the ZoneMinder run pid file
|
use constant ZM_PID => '@ZM_PID@'; # Path to the ZoneMinder run pid file
|
||||||
use constant ZM_CONFIG => "@ZM_CONFIG@"; # Path to the ZoneMinder config file
|
use constant ZM_CONFIG => '@ZM_CONFIG@'; # Path to the ZoneMinder config file
|
||||||
use constant ZM_CONFIG_SUBDIR => "@ZM_CONFIG_SUBDIR@"; # Path to the ZoneMinder config subfolder
|
use constant ZM_CONFIG_SUBDIR => '@ZM_CONFIG_SUBDIR@'; # Path to the ZoneMinder config subfolder
|
||||||
|
|
||||||
use Carp;
|
use Carp;
|
||||||
|
require ZoneMinder::Database;
|
||||||
|
|
||||||
# Load the config from the database into the symbol table
|
# Load the config from the database into the symbol table
|
||||||
BEGIN {
|
BEGIN {
|
||||||
|
require ZoneMinder::Database;
|
||||||
|
|
||||||
# Process name, value pairs from the main config file first
|
# Process name, value pairs from the main config file first
|
||||||
my $config_file = ZM_CONFIG;
|
my $config_file = ZM_CONFIG;
|
||||||
|
@ -78,52 +81,25 @@ BEGIN {
|
||||||
|
|
||||||
# Search for user created config files. If one or more are found then
|
# Search for user created config files. If one or more are found then
|
||||||
# update the Config hash with those values
|
# update the Config hash with those values
|
||||||
if ( -d ZM_CONFIG_SUBDIR ) {
|
if ( ZM_CONFIG_SUBDIR and -d ZM_CONFIG_SUBDIR ) {
|
||||||
if ( -R ZM_CONFIG_SUBDIR ) {
|
if ( -R ZM_CONFIG_SUBDIR ) {
|
||||||
foreach my $filename ( glob ZM_CONFIG_SUBDIR."/*.conf" ) {
|
foreach my $filename ( glob ZM_CONFIG_SUBDIR.'/*.conf' ) {
|
||||||
process_configfile($filename);
|
process_configfile($filename);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
print( STDERR "WARNING: ZoneMinder configuration subfolder found but is not readable. Check folder permissions on ".ZM_CONFIG_SUBDIR.".\n" );
|
print( STDERR 'WARNING: ZoneMinder configuration subfolder found but is not readable. Check folder permissions on '.ZM_CONFIG_SUBDIR.".\n" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
use DBI;
|
my $dbh = ZoneMinder::Database::zmDbConnect();
|
||||||
my $socket;
|
|
||||||
my ( $host, $portOrSocket ) = ( $Config{ZM_DB_HOST} =~ /^([^:]+)(?::(.+))?$/ );
|
|
||||||
|
|
||||||
if ( defined($portOrSocket) ) {
|
|
||||||
if ( $portOrSocket =~ /^\// ) {
|
|
||||||
$socket = ';mysql_socket='.$portOrSocket;
|
|
||||||
} else {
|
|
||||||
$socket = ';host='.$host.';port='.$portOrSocket;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$socket = ';host='.$Config{ZM_DB_HOST};
|
|
||||||
}
|
|
||||||
my $sslOptions = '';
|
|
||||||
if ( $Config{ZM_DB_SSL_CA_CERT} ) {
|
|
||||||
$sslOptions = ';'.join(';',
|
|
||||||
"mysql_ssl=1",
|
|
||||||
"mysql_ssl_ca_file=".$Config{ZM_DB_SSL_CA_CERT},
|
|
||||||
"mysql_ssl_client_key=".$Config{ZM_DB_SSL_CLIENT_KEY},
|
|
||||||
"mysql_ssl_client_cert=".$Config{ZM_DB_SSL_CLIENT_CERT}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
my $dbh = DBI->connect( "DBI:mysql:database=".$Config{ZM_DB_NAME}
|
|
||||||
.$socket.$sslOptions
|
|
||||||
, $Config{ZM_DB_USER}
|
|
||||||
, $Config{ZM_DB_PASS}
|
|
||||||
) or croak( "Can't connect to db" );
|
|
||||||
my $sql = 'SELECT Name,Value FROM Config';
|
my $sql = 'SELECT Name,Value FROM Config';
|
||||||
my $sth = $dbh->prepare_cached( $sql ) or croak( "Can't prepare '$sql': ".$dbh->errstr() );
|
my $sth = $dbh->prepare_cached($sql) or croak("Can't prepare '$sql': ".$dbh->errstr());
|
||||||
my $res = $sth->execute() or croak( "Can't execute: ".$sth->errstr() );
|
my $res = $sth->execute() or croak("Can't execute: ".$sth->errstr());
|
||||||
while( my $config = $sth->fetchrow_hashref() ) {
|
while( my $config = $sth->fetchrow_hashref() ) {
|
||||||
$Config{$config->{Name}} = $config->{Value};
|
$Config{$config->{Name}} = $config->{Value};
|
||||||
}
|
}
|
||||||
$sth->finish();
|
$sth->finish();
|
||||||
#$dbh->disconnect();
|
|
||||||
#
|
|
||||||
if ( ! $Config{ZM_SERVER_ID} ) {
|
if ( ! $Config{ZM_SERVER_ID} ) {
|
||||||
$Config{ZM_SERVER_ID} = undef;
|
$Config{ZM_SERVER_ID} = undef;
|
||||||
$sth = $dbh->prepare_cached( 'SELECT * FROM Servers WHERE Name=?' );
|
$sth = $dbh->prepare_cached( 'SELECT * FROM Servers WHERE Name=?' );
|
||||||
|
@ -143,25 +119,30 @@ BEGIN {
|
||||||
sub process_configfile {
|
sub process_configfile {
|
||||||
my $config_file = shift;
|
my $config_file = shift;
|
||||||
|
|
||||||
if ( -R $config_file ) {
|
if ( ! -R $config_file ) {
|
||||||
|
print(STDERR "WARNING: ZoneMinder configuration file found but is not readable. Check file permissions on $config_file\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
open( my $CONFIG, '<', $config_file )
|
open( my $CONFIG, '<', $config_file )
|
||||||
or croak( "Can't open config file '$config_file': $!" );
|
or croak("Can't open config file '$config_file': $!");
|
||||||
foreach my $str ( <$CONFIG> ) {
|
foreach my $str ( <$CONFIG> ) {
|
||||||
next if ( $str =~ /^\s*$/ );
|
next if ( $str =~ /^\s*$/ );
|
||||||
next if ( $str =~ /^\s*#/ );
|
next if ( $str =~ /^\s*#/ );
|
||||||
my ( $name, $value ) = $str =~ /^\s*([^=\s]+)\s*=\s*[\'"]*(.*?)[\'"]*\s*$/;
|
my ( $name, $value ) = $str =~ /^\s*([^=\s]+)\s*=\s*[\'"]*(.*?)[\'"]*\s*$/;
|
||||||
if ( ! $name ) {
|
if ( !$name ) {
|
||||||
print( STDERR "Warning, bad line in $config_file: $str\n" );
|
print(STDERR "Warning, bad line in $config_file: $str\n");
|
||||||
next;
|
next;
|
||||||
} # end if
|
} # end if
|
||||||
$name =~ tr/a-z/A-Z/;
|
$name =~ tr/a-z/A-Z/;
|
||||||
|
#if ( !$ZoneMinder::ConfigData::options_hash{$name} ) {
|
||||||
|
#print(STDERR "Warning, unknown config option name $name in $config_file\n");
|
||||||
|
#} else {
|
||||||
|
#print(STDERR "Warning, known config option name $name in $config_file\n");
|
||||||
|
#}
|
||||||
$Config{$name} = $value;
|
$Config{$name} = $value;
|
||||||
}
|
} # end foreach config line
|
||||||
close( $CONFIG );
|
close($CONFIG);
|
||||||
} else {
|
} # end sub process_configfile
|
||||||
print( STDERR "WARNING: ZoneMinder configuration file found but is not readable. Check file permissions on $config_file\n" );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} # end BEGIN
|
} # end BEGIN
|
||||||
|
|
||||||
|
|
|
@ -3952,7 +3952,7 @@ our %options_hash = map { ( $_->{name}, $_ ) } @options;
|
||||||
# This function should never need to be called explicitly, except if
|
# This function should never need to be called explicitly, except if
|
||||||
# this module is 'require'd rather than 'use'd. See zmconfgen.pl.
|
# this module is 'require'd rather than 'use'd. See zmconfgen.pl.
|
||||||
sub initialiseConfig {
|
sub initialiseConfig {
|
||||||
return if ( $configInitialised );
|
return if $configInitialised;
|
||||||
|
|
||||||
# Do some initial data munging to finish the data structures
|
# Do some initial data munging to finish the data structures
|
||||||
# Create option ids
|
# Create option ids
|
||||||
|
|
|
@ -65,7 +65,8 @@ our $VERSION = $ZoneMinder::Base::VERSION;
|
||||||
# ==========================================================================
|
# ==========================================================================
|
||||||
|
|
||||||
use ZoneMinder::Logger qw(:all);
|
use ZoneMinder::Logger qw(:all);
|
||||||
use ZoneMinder::Config qw(:all);
|
|
||||||
|
require ZoneMinder::Config;
|
||||||
|
|
||||||
our $dbh = undef;
|
our $dbh = undef;
|
||||||
|
|
||||||
|
@ -87,25 +88,25 @@ sub zmDbConnect {
|
||||||
$socket = ';host='.$host.';port='.$portOrSocket;
|
$socket = ';host='.$host.';port='.$portOrSocket;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$socket = ';host='.$Config{ZM_DB_HOST};
|
$socket = ';host='.$ZoneMinder::Config::Config{ZM_DB_HOST};
|
||||||
}
|
}
|
||||||
|
|
||||||
my $sslOptions = '';
|
my $sslOptions = '';
|
||||||
if ( $Config{ZM_DB_SSL_CA_CERT} ) {
|
if ( $ZoneMinder::Config::Config{ZM_DB_SSL_CA_CERT} ) {
|
||||||
$sslOptions = join(';','',
|
$sslOptions = join(';', '',
|
||||||
'mysql_ssl=1',
|
'mysql_ssl=1',
|
||||||
'mysql_ssl_ca_file='.$Config{ZM_DB_SSL_CA_CERT},
|
'mysql_ssl_ca_file='.$ZoneMinder::Config::Config{ZM_DB_SSL_CA_CERT},
|
||||||
'mysql_ssl_client_key='.$Config{ZM_DB_SSL_CLIENT_KEY},
|
'mysql_ssl_client_key='.$ZoneMinder::Config::Config{ZM_DB_SSL_CLIENT_KEY},
|
||||||
'mysql_ssl_client_cert='.$Config{ZM_DB_SSL_CLIENT_CERT}
|
'mysql_ssl_client_cert='.$ZoneMinder::Config::Config{ZM_DB_SSL_CLIENT_CERT}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
eval {
|
eval {
|
||||||
$dbh = DBI->connect(
|
$dbh = DBI->connect(
|
||||||
'DBI:mysql:database='.$Config{ZM_DB_NAME}
|
'DBI:mysql:database='.$ZoneMinder::Config::Config{ZM_DB_NAME}
|
||||||
.$socket . $sslOptions . ($options?join(';', '', map { $_.'='.$$options{$_} } keys %{$options} ) : '')
|
.$socket . $sslOptions . ($options?join(';', '', map { $_.'='.$$options{$_} } keys %{$options} ) : '')
|
||||||
, $Config{ZM_DB_USER}
|
, $ZoneMinder::Config::Config{ZM_DB_USER}
|
||||||
, $Config{ZM_DB_PASS}
|
, $ZoneMinder::Config::Config{ZM_DB_PASS}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
if ( !$dbh or $@ ) {
|
if ( !$dbh or $@ ) {
|
||||||
|
@ -124,7 +125,7 @@ sub zmDbConnect {
|
||||||
} # end sub zmDbConnect
|
} # end sub zmDbConnect
|
||||||
|
|
||||||
sub zmDbDisconnect {
|
sub zmDbDisconnect {
|
||||||
if ( defined( $dbh ) ) {
|
if ( defined($dbh) ) {
|
||||||
$dbh->disconnect() or Error('Error disconnecting db? ' . $dbh->errstr());
|
$dbh->disconnect() or Error('Error disconnecting db? ' . $dbh->errstr());
|
||||||
$dbh = undef;
|
$dbh = undef;
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,7 +87,7 @@ our $VERSION = $ZoneMinder::Base::VERSION;
|
||||||
#
|
#
|
||||||
# ==========================================================================
|
# ==========================================================================
|
||||||
|
|
||||||
use ZoneMinder::Config qw(:all);
|
require ZoneMinder::Config;
|
||||||
|
|
||||||
use DBI;
|
use DBI;
|
||||||
use Carp;
|
use Carp;
|
||||||
|
@ -156,7 +156,7 @@ sub new {
|
||||||
$this->{autoFlush} = 1;
|
$this->{autoFlush} = 1;
|
||||||
|
|
||||||
( $this->{fileName} = $0 ) =~ s|^.*/||;
|
( $this->{fileName} = $0 ) =~ s|^.*/||;
|
||||||
$this->{logPath} = $Config{ZM_PATH_LOGS};
|
$this->{logPath} = $ZoneMinder::Config::Config{ZM_PATH_LOGS};
|
||||||
$this->{logFile} = $this->{logPath}.'/'.$this->{id}.'.log';
|
$this->{logFile} = $this->{logPath}.'/'.$this->{id}.'.log';
|
||||||
($this->{logFile}) = $this->{logFile} =~ /^([\w\.\/]+)$/;
|
($this->{logFile}) = $this->{logFile} =~ /^([\w\.\/]+)$/;
|
||||||
|
|
||||||
|
@ -169,7 +169,7 @@ sub new {
|
||||||
sub BEGIN {
|
sub BEGIN {
|
||||||
# Fake the config variables that are used in case they are not defined yet
|
# Fake the config variables that are used in case they are not defined yet
|
||||||
# Only really necessary to support upgrade from previous version
|
# Only really necessary to support upgrade from previous version
|
||||||
if ( !eval('defined($Config{ZM_LOG_DEBUG})') ) {
|
if ( !eval('defined($ZoneMinder::Config::Config{ZM_LOG_DEBUG})') ) {
|
||||||
no strict 'subs';
|
no strict 'subs';
|
||||||
no strict 'refs';
|
no strict 'refs';
|
||||||
my %dbgConfig = (
|
my %dbgConfig = (
|
||||||
|
@ -221,17 +221,17 @@ sub initialise( @ ) {
|
||||||
if ( defined($options{databaseLevel}) ) {
|
if ( defined($options{databaseLevel}) ) {
|
||||||
$tempDatabaseLevel = $options{databaseLevel};
|
$tempDatabaseLevel = $options{databaseLevel};
|
||||||
} else {
|
} else {
|
||||||
$tempDatabaseLevel = $Config{ZM_LOG_LEVEL_DATABASE};
|
$tempDatabaseLevel = $ZoneMinder::Config::Config{ZM_LOG_LEVEL_DATABASE};
|
||||||
}
|
}
|
||||||
if ( defined($options{fileLevel}) ) {
|
if ( defined($options{fileLevel}) ) {
|
||||||
$tempFileLevel = $options{fileLevel};
|
$tempFileLevel = $options{fileLevel};
|
||||||
} else {
|
} else {
|
||||||
$tempFileLevel = $Config{ZM_LOG_LEVEL_FILE};
|
$tempFileLevel = $ZoneMinder::Config::Config{ZM_LOG_LEVEL_FILE};
|
||||||
}
|
}
|
||||||
if ( defined($options{syslogLevel}) ) {
|
if ( defined($options{syslogLevel}) ) {
|
||||||
$tempSyslogLevel = $options{syslogLevel};
|
$tempSyslogLevel = $options{syslogLevel};
|
||||||
} else {
|
} else {
|
||||||
$tempSyslogLevel = $Config{ZM_LOG_LEVEL_SYSLOG};
|
$tempSyslogLevel = $ZoneMinder::Config::Config{ZM_LOG_LEVEL_SYSLOG};
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( defined($ENV{LOG_PRINT}) ) {
|
if ( defined($ENV{LOG_PRINT}) ) {
|
||||||
|
@ -245,19 +245,19 @@ sub initialise( @ ) {
|
||||||
$tempFileLevel = $level if defined($level = $this->getTargettedEnv('LOG_LEVEL_FILE'));
|
$tempFileLevel = $level if defined($level = $this->getTargettedEnv('LOG_LEVEL_FILE'));
|
||||||
$tempSyslogLevel = $level if defined($level = $this->getTargettedEnv('LOG_LEVEL_SYSLOG'));
|
$tempSyslogLevel = $level if defined($level = $this->getTargettedEnv('LOG_LEVEL_SYSLOG'));
|
||||||
|
|
||||||
if ( $Config{ZM_LOG_DEBUG} ) {
|
if ( $ZoneMinder::Config::Config{ZM_LOG_DEBUG} ) {
|
||||||
# Splitting on an empty string doesn't return an empty string, it returns an empty array
|
# Splitting on an empty string doesn't return an empty string, it returns an empty array
|
||||||
foreach my $target ( $Config{ZM_LOG_DEBUG_TARGET} ? split(/\|/, $Config{ZM_LOG_DEBUG_TARGET}) : '' ) {
|
foreach my $target ( $ZoneMinder::Config::Config{ZM_LOG_DEBUG_TARGET} ? split(/\|/, $ZoneMinder::Config::Config{ZM_LOG_DEBUG_TARGET}) : '' ) {
|
||||||
if ( $target eq $this->{id}
|
if ( $target eq $this->{id}
|
||||||
|| $target eq '_'.$this->{id}
|
|| $target eq '_'.$this->{id}
|
||||||
|| $target eq $this->{idRoot}
|
|| $target eq $this->{idRoot}
|
||||||
|| $target eq '_'.$this->{idRoot}
|
|| $target eq '_'.$this->{idRoot}
|
||||||
|| $target eq ''
|
|| $target eq ''
|
||||||
) {
|
) {
|
||||||
if ( $Config{ZM_LOG_DEBUG_LEVEL} > NOLOG ) {
|
if ( $ZoneMinder::Config::Config{ZM_LOG_DEBUG_LEVEL} > NOLOG ) {
|
||||||
$tempLevel = $this->limit( $Config{ZM_LOG_DEBUG_LEVEL} );
|
$tempLevel = $this->limit( $ZoneMinder::Config::Config{ZM_LOG_DEBUG_LEVEL} );
|
||||||
if ( $Config{ZM_LOG_DEBUG_FILE} ne '' ) {
|
if ( $ZoneMinder::Config::Config{ZM_LOG_DEBUG_FILE} ne '' ) {
|
||||||
$tempLogFile = $Config{ZM_LOG_DEBUG_FILE};
|
$tempLogFile = $ZoneMinder::Config::Config{ZM_LOG_DEBUG_FILE};
|
||||||
$tempFileLevel = $tempLevel;
|
$tempFileLevel = $tempLevel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -501,8 +501,8 @@ sub openFile {
|
||||||
if ( open($LOGFILE, '>>', $this->{logFile}) ) {
|
if ( open($LOGFILE, '>>', $this->{logFile}) ) {
|
||||||
$LOGFILE->autoflush() if $this->{autoFlush};
|
$LOGFILE->autoflush() if $this->{autoFlush};
|
||||||
|
|
||||||
my $webUid = (getpwnam($Config{ZM_WEB_USER}))[2];
|
my $webUid = (getpwnam($ZoneMinder::Config::Config{ZM_WEB_USER}))[2];
|
||||||
my $webGid = (getgrnam($Config{ZM_WEB_GROUP}))[2];
|
my $webGid = (getgrnam($ZoneMinder::Config::Config{ZM_WEB_GROUP}))[2];
|
||||||
if ( $> == 0 ) {
|
if ( $> == 0 ) {
|
||||||
chown( $webUid, $webGid, $this->{logFile} )
|
chown( $webUid, $webGid, $this->{logFile} )
|
||||||
or Fatal("Can't change permissions on log file $$this{logFile}: $!");
|
or Fatal("Can't change permissions on log file $$this{logFile}: $!");
|
||||||
|
@ -577,7 +577,7 @@ sub logPrint {
|
||||||
my $res = $this->{sth}->execute(
|
my $res = $this->{sth}->execute(
|
||||||
$seconds+($microseconds/1000000.0),
|
$seconds+($microseconds/1000000.0),
|
||||||
$this->{id},
|
$this->{id},
|
||||||
($Config{ZM_SERVER_ID} ? $Config{ZM_SERVER_ID} : undef),
|
($ZoneMinder::Config::Config{ZM_SERVER_ID} ? $ZoneMinder::Config::Config{ZM_SERVER_ID} : undef),
|
||||||
$$,
|
$$,
|
||||||
$level,
|
$level,
|
||||||
$codes{$level},
|
$codes{$level},
|
||||||
|
|
Loading…
Reference in New Issue