lemonldap-ng/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Serializer.pm

186 lines
4.2 KiB
Perl
Raw Normal View History

package Lemonldap::NG::Common::Conf::Serializer;
use Data::Dumper;
our $VERSION = '1.4.0';
BEGIN {
2010-06-25 15:51:09 +02:00
*Lemonldap::NG::Common::Conf::normalize = \&normalize;
*Lemonldap::NG::Common::Conf::unnormalize = \&unnormalize;
2010-03-01 21:32:28 +01:00
*Lemonldap::NG::Common::Conf::serialize = \&serialize;
*Lemonldap::NG::Common::Conf::unserialize = \&unserialize;
}
2010-06-25 15:51:09 +02:00
## @method string normalize(string value)
# Change quotes, spaces and line breaks
# @param value Input value
# @return normalized string
sub normalize {
my ( $self, $value ) = splice @_;
# trim white spaces
$value =~ s/^\s*(.*?)\s*$/$1/;
# Convert carriage returns (\r) and line feeds (\n)
$value =~ s/\r/%0D/g;
$value =~ s/\n/%0A/g;
# Convert simple quotes
$value =~ s/'/'/g;
# Surround with simple quotes
$value = "'$value'" unless ( $self->{noQuotes} );
return $value;
}
## @method string unnormalize(string value)
# Revert quotes, spaces and line breaks
# @param value Input value
# @return unnormalized string
sub unnormalize {
my ( $self, $value ) = splice @_;
# Convert simple quotes
$value =~ s/&#?39;/'/g;
# Convert carriage returns (\r) and line feeds (\n)
$value =~ s/%0D/\r/g;
$value =~ s/%0A/\n/g;
return $value;
}
## @method hashref serialize(hashref conf)
# Parse configuration and convert it into fields
# @param conf Configuration
# @return fields
sub serialize {
2010-06-25 15:51:09 +02:00
my ( $self, $conf ) = splice @_;
my $fields;
2010-06-25 15:51:09 +02:00
# Data::Dumper options
2010-03-01 21:32:28 +01:00
local $Data::Dumper::Indent = 0;
local $Data::Dumper::Varname = "data";
2010-06-25 15:51:09 +02:00
# Parse configuration
while ( my ( $k, $v ) = each(%$conf) ) {
2010-06-25 15:51:09 +02:00
# 1.Hash ref
if ( ref($v) ) {
2010-06-25 15:51:09 +02:00
$fields->{$k} = $self->normalize( Dumper($v) );
}
2010-06-25 15:51:09 +02:00
# 2. Numeric values
elsif ( $v =~ /^\d+$/ ) {
$fields->{$k} = "$v";
}
2010-06-25 15:51:09 +02:00
# 3. Standard values
else {
$fields->{$k} = $self->normalize($v);
}
}
2010-06-25 15:51:09 +02:00
return $fields;
}
2010-06-25 15:51:09 +02:00
## @method hashref unserialize(hashref fields)
# Convert fields into configuration
# @param fields Fields
# @return configuration
sub unserialize {
2010-06-25 15:51:09 +02:00
my ( $self, $fields ) = splice @_;
my $conf;
2010-06-25 15:51:09 +02:00
# Parse fields
while ( my ( $k, $v ) = each(%$fields) ) {
# Remove surrounding quotes
$v =~ s/^'(.*)'$/$1/s;
# Manage hashes
if (
$k =~ /^(?x:
applicationList
|authChoiceModules
|CAS_proxiedServices
|casStorageOptions
|dbiExportedVars
|demoExportedVars
|exportedHeaders
|exportedVars
|globalStorageOptions
|grantSessionRules
|groups
|ldapExportedVars
|locationRules
|logoutServices
|macros
|notificationStorageOptions
|persistentStorageOptions
2013-01-16 16:43:24 +01:00
|portalSkinRules
|post
2012-01-11 12:42:42 +01:00
|reloadUrls
|remoteGlobalStorageOptions
|samlIDPMetaDataExportedAttributes
|samlIDPMetaDataOptions
|samlIDPMetaDataXML
|samlSPMetaDataExportedAttributes
|samlSPMetaDataOptions
|samlSPMetaDataXML
|samlStorageOptions
|sessionDataToRemember
|slaveExportedVars
|vhostOptions
|webIDExportedVars
)$/
and $v ||= {} and not ref($v)
)
{
$conf->{$k} = {};
2010-06-25 15:51:09 +02:00
# Value should be a Data::Dumper, else this is an old format
if ( defined($v) and $v !~ /^\$/ ) {
2010-06-25 15:51:09 +02:00
$msg .=
" Warning: configuration is in old format, you've to migrate!";
eval { require Storable; require MIME::Base64; };
if ($@) {
2010-06-25 15:51:09 +02:00
$msg .= " Error: $@";
return 0;
}
$conf->{$k} = Storable::thaw( MIME::Base64::decode_base64($v) );
}
2010-06-25 15:51:09 +02:00
# Convert Data::Dumper
else {
my $data;
$v =~ s/^\$([_a-zA-Z][_a-zA-Z0-9]*) *=/\$data =/;
2010-06-25 15:51:09 +02:00
$v = $self->unnormalize($v);
# Evaluate expression
eval $v;
2010-06-25 15:51:09 +02:00
if ($@) {
$msg .= " Error: cannot read configuration key $k: $@";
}
# Store value in configuration object
$conf->{$k} = $data;
}
}
2010-06-25 15:51:09 +02:00
# Other fields type
else {
2010-06-25 15:51:09 +02:00
$conf->{$k} = $self->unnormalize($v);
}
}
2010-06-25 15:51:09 +02:00
return $conf;
}
1;
__END__