our
$VERSION
=
'2.0.12'
;
BEGIN {
*Lemonldap::NG::Common::Conf::normalize
= \
&normalize
;
*Lemonldap::NG::Common::Conf::unnormalize
= \
&unnormalize
;
*Lemonldap::NG::Common::Conf::serialize
= \
&serialize
;
*Lemonldap::NG::Common::Conf::unserialize
= \
&unserialize
;
*Lemonldap::NG::Common::Conf::oldUnserialize
= \
&oldUnserialize
;
}
sub
normalize {
my
(
$self
,
$value
) =
@_
;
$value
=~ s/^\s*(.*?)\s*$/$1/;
$value
=~ s/\r/%0D/g;
$value
=~ s/\n/%0A/g;
$value
=~ s/'/&
$value
=
"'$value'"
unless
(
$self
->{noQuotes} );
return
$value
;
}
sub
unnormalize {
my
(
$self
,
$value
) =
@_
;
$value
=~ s/&
$value
=~ s/%0D/\r/g;
$value
=~ s/%0A/\n/g;
$value
+= 0
if
(
$value
=~ /^(?:0|(?:\-[0-9]|[1-9])[0-9]*)(?:\.[0-9]+)?$/ );
return
$value
;
}
sub
serialize {
my
(
$self
,
$conf
) =
@_
;
my
$fields
;
foreach
my
$k
(
keys
%$conf
) {
my
$v
=
$conf
->{
$k
};
if
(
ref
(
$v
) ) {
$fields
->{
$k
} = to_json(
$v
);
}
else
{
$fields
->{
$k
} =
$v
;
}
}
return
$fields
;
}
sub
unserialize {
my
(
$self
,
$fields
) =
@_
;
my
$conf
;
foreach
my
$k
(
keys
%$fields
) {
my
$v
=
$fields
->{
$k
};
if
(
$k
=~
$hashParameters
) {
unless
( utf8::is_utf8(
$v
) ) {
$v
= encode(
'UTF-8'
,
$v
);
}
$conf
->{
$k
} = (
$v
=~ /./
?
eval
{ from_json(
$v
, {
allow_nonref
=> 1 } ) }
: {}
);
if
($@) {
$Lemonldap::NG::Common::Conf::msg
.=
"Unable to decode $k, switching to old format.\n"
;
return
$self
->oldUnserialize(
$fields
);
}
}
elsif
(
$k
=~
$arrayParameters
) {
unless
( utf8::is_utf8(
$v
) ) {
$v
= encode(
'UTF-8'
,
$v
);
}
$conf
->{
$k
} = (
$v
=~ /./
?
eval
{ from_json(
$v
, {
allow_nonref
=> 1 } ) }
: {}
);
if
($@) {
$Lemonldap::NG::Common::Conf::msg
.=
"Unable to decode $k, switching to old format.\n"
;
return
$self
->oldUnserialize(
$fields
);
}
}
else
{
$conf
->{
$k
} =
$v
;
}
}
return
$conf
;
}
sub
oldUnserialize {
my
(
$self
,
$fields
) =
@_
;
my
$conf
;
while
(
my
(
$k
,
$v
) =
each
(
%$fields
) ) {
$v
=~ s/^
'(.*)'
$/$1/s;
if
(
$k
=~
$hashParameters
and
$v
||= {} and not
ref
(
$v
) ) {
$conf
->{
$k
} = {};
if
(
defined
(
$v
) and
$v
!~ /^\$/ ) {
$Lemonldap::NG::Common::Conf::msg
.=
" Warning: configuration is in old format, you've to migrate!"
;
if
($@) {
$Lemonldap::NG::Common::Conf::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
=
$self
->unnormalize(
$v
);
eval
$v
;
if
($@) {
$Lemonldap::NG::Common::Conf::msg
.=
" Error: cannot read configuration key $k: $@"
;
}
$conf
->{
$k
} =
$data
;
}
}
else
{
$conf
->{
$k
} =
$self
->unnormalize(
$v
);
}
}
return
$conf
;
}
1;