diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Config.pm.in b/scripts/ZoneMinder/lib/ZoneMinder/Config.pm.in index 66d94ee8ed..1da7b0ed26 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Config.pm.in +++ b/scripts/ZoneMinder/lib/ZoneMinder/Config.pm.in @@ -31,8 +31,6 @@ use warnings; require Exporter; require ZoneMinder::Base; -use ZoneMinder::ConfigData qw(:all); - our @ISA = qw(Exporter ZoneMinder::Base); use vars qw( %Config ); @@ -178,7 +176,16 @@ require ZoneMinder::Database; } # end BEGIN +# ConfigData holds the definition and help text of every option. Only the functions +# that work on the Config table itself need it, so it is loaded when they run +# rather than by every script. Its INIT block does not run when it is required. +sub loadConfigData { + require ZoneMinder::ConfigData; + ZoneMinder::ConfigData::initialiseConfig(); +} + sub loadConfigFromDB { + loadConfigData(); print('Loading config from DB'); my $dbh = ZoneMinder::Database::zmDbConnect(); if ( !$dbh ) { @@ -199,7 +206,7 @@ sub loadConfigFromDB { next; } if ( defined($value) ) { - if ( $option->{type} == $types{boolean} ) { + if ( $option->{type} == $ZoneMinder::ConfigData::types{boolean} ) { # Booleans are 1/0 in the table and in ConfigData; coerce rather than # take the column verbatim so an empty or absent value lands on '0'. $option->{value} = $value ? '1' : '0'; @@ -218,6 +225,7 @@ sub loadConfigFromDB { # shipped default is public, so replace it, or an empty value, with a random secret the # first time the config is saved on this system. refs GHSA-wmcc-x64g-jr84 sub generateAuthHashSecret { + loadConfigData(); my $option = $ZoneMinder::ConfigData::options_hash{ZM_AUTH_HASH_SECRET}; return if !$option; return if defined($option->{value}) and $option->{value} ne '' @@ -242,6 +250,7 @@ sub generateAuthHashSecret { # them all 0, which showed ZM_AUTH_HASH_SECRET to anyone. zmupdate.pl calls this on # every update; saveConfigToDB() (zmupdate.pl -f) writes them along with the rest. sub syncConfigFlags { + loadConfigData(); my $dbh = ZoneMinder::Database::zmDbConnect(); if ( !$dbh ) { print( "Error: unable to update option flags in database: $DBI::errstr\n" ); @@ -252,7 +261,7 @@ sub syncConfigFlags { my $sth = $dbh->prepare_cached('UPDATE Config SET Private=?, `System`=? WHERE Name=?') or croak( "Can't prepare: ".$dbh->errstr() ); my $changed = 0; - foreach my $option ( @options ) { + foreach my $option ( @ZoneMinder::ConfigData::options ) { my $row = $rows->{$option->{name}} or next; my ( $private, $system ) = ( $option->{private} ? 1 : 0, $option->{system} ? 1 : 0 ); next if ( ($row->{Private} // 0) == $private ) and ( ($row->{System} // 0) == $system ); @@ -265,7 +274,8 @@ sub syncConfigFlags { } # end sub syncConfigFlags sub saveConfigToDB { - print( 'Saving config to DB ' . @options . " entries\n" ); + loadConfigData(); + print( 'Saving config to DB ' . @ZoneMinder::ConfigData::options . " entries\n" ); my $dbh = ZoneMinder::Database::zmDbConnect(); if ( !$dbh ) { print( "Error: unable to save options to database: $DBI::errstr\n" ); @@ -287,15 +297,15 @@ sub saveConfigToDB { $sql = 'REPLACE INTO Config SET Id=?, Name=?, Value=?, Type=?, DefaultValue=?, Hint=?, Pattern=?, Format=?, Prompt=?, Help=?, Category=?, Readonly=?, Private=?, `System`=?, Requires=?'; my $sth = $dbh->prepare_cached( $sql ) or croak( "Can't prepare '$sql': ".$dbh->errstr() ); - foreach my $option ( @options ) { + foreach my $option ( @ZoneMinder::ConfigData::options ) { #next if ( $option->{category} eq 'hidden' ); #print( $option->{name}."\n" ) if ( !$option->{category} ); $option->{db_type} = $option->{type}->{db_type}; $option->{db_hint} = $option->{type}->{hint}; $option->{db_pattern} = $option->{type}->{pattern}; $option->{db_format} = $option->{type}->{format}; - $option->{db_value} = dbValue($option->{db_type}, $option->{value}); - $option->{db_default} = dbValue($option->{db_type}, $option->{default}); + $option->{db_value} = ZoneMinder::ConfigData::dbValue($option->{db_type}, $option->{value}); + $option->{db_default} = ZoneMinder::ConfigData::dbValue($option->{db_type}, $option->{default}); if ( my $requires = $option->{requires} ) { # Requires values are already in stored form, booleans included. $option->{db_requires} = join(';', map { "$_->{name}=$_->{value}" } @$requires); diff --git a/scripts/ZoneMinder/t/config_lazy_load.t b/scripts/ZoneMinder/t/config_lazy_load.t new file mode 100644 index 0000000000..03dbbf61ac --- /dev/null +++ b/scripts/ZoneMinder/t/config_lazy_load.t @@ -0,0 +1,56 @@ +use strict; +use warnings; +use Test::More; + +# ZoneMinder::Config loads ZoneMinder::ConfigData, the definition and help text +# of every option, only in the functions that work on the Config table, rather +# than in every script that loads the config. + +eval { require ZoneMinder::Config; 1 } or plan skip_all => "cannot load ZoneMinder::Config: $@"; +plan tests => 9; + +ok(!$INC{'ZoneMinder/ConfigData.pm'}, 'loading the config does not load ConfigData'); + +# Records what would go to the database. +my (@executed, @done); +{ + package FakeSth; + sub new { my ($class, $rows) = @_; bless { rows => [ @{$rows || []} ] }, $class } + sub execute { my $self = shift; push @executed, [ @_ ]; 1 } + sub fetchrow_hashref { shift @{$_[0]{rows}} } + sub finish { 1 } + package FakeDbh; + sub new { bless { AutoCommit => 1, rows => $_[1] }, $_[0] } + sub prepare_cached { FakeSth->new($_[0]{rows}) } + sub selectall_hashref { + return { ZM_AUTH_HASH_SECRET => { Name => 'ZM_AUTH_HASH_SECRET', Private => 0, System => 0 } }; + } + sub do { push @done, $_[1]; 1 } + sub errstr { '' } +} +my $rows = [ + { Name => 'ZM_OPT_USE_AUTH', Value => '' }, + { Name => 'ZM_LANG_DEFAULT', Value => 'de_de' }, +]; +{ + no warnings 'redefine', 'once'; + *ZoneMinder::Database::zmDbConnect = sub { FakeDbh->new($rows) }; +} + +local *STDOUT; +open(STDOUT, '>', '/dev/null'); + +is(ZoneMinder::Config::loadConfigFromDB(), 2, 'loadConfigFromDB reads the rows'); +ok($INC{'ZoneMinder/ConfigData.pm'}, 'and loads ConfigData'); +is($ZoneMinder::ConfigData::options_hash{ZM_OPT_USE_AUTH}{value}, '0', 'an empty boolean is read as 0'); +is($ZoneMinder::ConfigData::options_hash{ZM_LANG_DEFAULT}{value}, 'de_de', 'a string is read as is'); +ok(defined $ZoneMinder::ConfigData::options[0]{id}, 'the options were initialised'); + +@executed = (); +is(ZoneMinder::Config::syncConfigFlags(), 1, 'syncConfigFlags updates the flags that differ'); + +@executed = (); +ZoneMinder::Config::saveConfigToDB(); +is(scalar @executed, scalar @ZoneMinder::ConfigData::options, 'saveConfigToDB writes every option'); +my ($auth) = grep { $_->[1] eq 'ZM_OPT_USE_AUTH' } @executed; +is($auth->[2], '0', 'and writes the boolean through dbValue');