2009-10-20 15:20:53 +02:00
|
|
|
package Lemonldap::NG::Common::Conf::Serializer;
|
|
|
|
|
|
|
|
use Data::Dumper;
|
|
|
|
|
|
|
|
BEGIN {
|
2010-03-01 21:32:28 +01:00
|
|
|
*Lemonldap::NG::Common::Conf::serialize = \&serialize;
|
2009-10-20 15:20:53 +02:00
|
|
|
*Lemonldap::NG::Common::Conf::unserialize = \&unserialize;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub serialize {
|
|
|
|
my ( $self, $conf ) = @_;
|
|
|
|
my $fields;
|
2010-03-01 21:32:28 +01:00
|
|
|
local $Data::Dumper::Indent = 0;
|
2009-10-20 15:20:53 +02:00
|
|
|
local $Data::Dumper::Varname = "data";
|
|
|
|
while ( my ( $k, $v ) = each(%$conf) ) {
|
|
|
|
next if ( $k =~ /^(?:reVHosts|cipher)$/ );
|
|
|
|
if ( ref($v) ) {
|
|
|
|
$fields->{$k} = Dumper($v);
|
|
|
|
$fields->{$k} =~ s/'/'/g;
|
2009-12-15 17:31:13 +01:00
|
|
|
$fields->{$k} = "'$fields->{$k}'" unless ( $self->{noQuotes} );
|
2009-10-20 15:20:53 +02:00
|
|
|
}
|
|
|
|
elsif ( $v =~ /^\d+$/ ) {
|
|
|
|
$fields->{$k} = "$v";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2010-02-08 13:20:37 +01:00
|
|
|
# trim white spaces
|
2009-10-20 15:20:53 +02:00
|
|
|
$v =~ s/^\s*(.*?)\s*$/$1/;
|
2010-02-08 13:20:37 +01:00
|
|
|
|
|
|
|
# Convert carriage returns (\r) and line feeds (\n)
|
|
|
|
$v =~ s/\r/%0D/g;
|
|
|
|
$v =~ s/\n/%0A/g;
|
|
|
|
|
|
|
|
# Convert simple quotes
|
2009-12-15 17:31:13 +01:00
|
|
|
$v =~ s/'/'/g;
|
2010-02-08 13:20:37 +01:00
|
|
|
|
|
|
|
# Surround with simple quotes
|
2009-12-15 17:31:13 +01:00
|
|
|
$v = "'$v'" unless ( $self->{noQuotes} );
|
2010-02-08 13:20:37 +01:00
|
|
|
|
|
|
|
# Store value in field
|
2009-12-15 17:31:13 +01:00
|
|
|
$fields->{$k} = $v;
|
2009-10-20 15:20:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub unserialize {
|
|
|
|
my ( $self, $fields ) = @_;
|
|
|
|
my $conf;
|
|
|
|
while ( my ( $k, $v ) = each(%$fields) ) {
|
2010-02-08 13:20:37 +01:00
|
|
|
|
|
|
|
# Remove surrounding quotes
|
2009-10-20 15:20:53 +02:00
|
|
|
$v =~ s/^'(.*)'$/$1/s;
|
2010-02-08 13:20:37 +01:00
|
|
|
|
|
|
|
# Manage hashes
|
2009-10-20 15:20:53 +02:00
|
|
|
if ( $k =~
|
2010-03-15 17:47:50 +01:00
|
|
|
/^(?:exportedVars|locationRules|groups|exportedHeaders|macros|globalStorageOptions|remoteGlobalStorageOptions|notificationStorageOptions|samlIDPMetaDataXML|samlIDPMetaDataExportedAttributes|samlIDPMetaDataOptions)$/
|
2009-10-20 15:20:53 +02:00
|
|
|
and $v ||= {}
|
|
|
|
and not ref($v) )
|
|
|
|
{
|
|
|
|
$conf->{$k} = {};
|
|
|
|
if ( defined($v) and $v !~ /^\$/ ) {
|
|
|
|
print STDERR
|
|
|
|
"Lemonldap::NG : Warning: configuration is in old format, you've to migrate !\n";
|
|
|
|
eval { require Storable; require MIME::Base64; };
|
|
|
|
if ($@) {
|
|
|
|
$msg = "Error : $@";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$conf->{$k} = Storable::thaw( MIME::Base64::decode_base64($v) );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
my $data;
|
|
|
|
$v =~ s/^\$([_a-zA-Z][_a-zA-Z0-9]*) *=/\$data =/;
|
|
|
|
$v =~ s/&#?39;/'/g;
|
|
|
|
eval $v;
|
|
|
|
print STDERR
|
|
|
|
"Lemonldap::NG : Error while reading configuration with $k key: $@\n"
|
|
|
|
if ($@);
|
|
|
|
$conf->{$k} = $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2009-12-15 17:31:13 +01:00
|
|
|
$v =~ s/&#?39;/'/g;
|
2010-02-08 13:20:37 +01:00
|
|
|
$v =~ s/%0D/\r/g;
|
|
|
|
$v =~ s/%0A/\n/g;
|
2009-10-20 15:20:53 +02:00
|
|
|
$conf->{$k} = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
__END__
|