| 1 |
package Lemonldap::NG::Common::Conf::Serializer; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use utf8; |
| 5 |
use Encode; |
| 6 |
use JSON; |
| 7 |
use Lemonldap::NG::Common::Conf::Constants; |
| 8 |
|
| 9 |
our $VERSION = '2.0.12'; |
| 10 |
|
| 11 |
BEGIN { |
| 12 |
*Lemonldap::NG::Common::Conf::normalize = \&normalize; |
| 13 |
*Lemonldap::NG::Common::Conf::unnormalize = \&unnormalize; |
| 14 |
*Lemonldap::NG::Common::Conf::serialize = \&serialize; |
| 15 |
*Lemonldap::NG::Common::Conf::unserialize = \&unserialize; |
| 16 |
*Lemonldap::NG::Common::Conf::oldUnserialize = \&oldUnserialize; |
| 17 |
} |
| 18 |
|
| 19 |
## @method string normalize(string value) |
| 20 |
# Change quotes, spaces and line breaks |
| 21 |
# @param value Input value |
| 22 |
# @return normalized string |
| 23 |
sub normalize { |
| 24 |
my ( $self, $value ) = @_; |
| 25 |
|
| 26 |
# trim white spaces |
| 27 |
$value =~ s/^\s*(.*?)\s*$/$1/; |
| 28 |
|
| 29 |
# Convert carriage returns (\r) and line feeds (\n) |
| 30 |
$value =~ s/\r/%0D/g; |
| 31 |
$value =~ s/\n/%0A/g; |
| 32 |
|
| 33 |
# Convert simple quotes |
| 34 |
$value =~ s/'/'/g; |
| 35 |
|
| 36 |
# Surround with simple quotes |
| 37 |
$value = "'$value'" unless ( $self->{noQuotes} ); |
| 38 |
|
| 39 |
return $value; |
| 40 |
} |
| 41 |
|
| 42 |
## @method string unnormalize(string value) |
| 43 |
# Revert quotes, spaces and line breaks |
| 44 |
# @param value Input value |
| 45 |
# @return unnormalized string |
| 46 |
sub unnormalize { |
| 47 |
my ( $self, $value ) = @_; |
| 48 |
|
| 49 |
# Convert simple quotes |
| 50 |
$value =~ s/&#?39;/'/g; |
| 51 |
|
| 52 |
# Convert carriage returns (\r) and line feeds (\n) |
| 53 |
$value =~ s/%0D/\r/g; |
| 54 |
$value =~ s/%0A/\n/g; |
| 55 |
|
| 56 |
# Keep number as numbers |
| 57 |
$value += 0 if ( $value =~ /^(?:0|(?:\-[0-9]|[1-9])[0-9]*)(?:\.[0-9]+)?$/ ); |
| 58 |
|
| 59 |
return $value; |
| 60 |
} |
| 61 |
|
| 62 |
## @method hashref serialize(hashref conf) |
| 63 |
# Parse configuration and convert it into fields |
| 64 |
# @param conf Configuration |
| 65 |
# @return fields |
| 66 |
sub serialize { |
| 67 |
my ( $self, $conf ) = @_; |
| 68 |
my $fields; |
| 69 |
|
| 70 |
# Parse configuration |
| 71 |
foreach my $k ( keys %$conf ) { |
| 72 |
my $v = $conf->{$k}; |
| 73 |
|
| 74 |
# 1.Hash ref |
| 75 |
if ( ref($v) ) { |
| 76 |
$fields->{$k} = to_json($v); |
| 77 |
} |
| 78 |
else { |
| 79 |
$fields->{$k} = $v; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return $fields; |
| 84 |
} |
| 85 |
|
| 86 |
## @method hashref unserialize(hashref fields) |
| 87 |
# Convert fields into configuration |
| 88 |
# @param fields Fields |
| 89 |
# @return configuration |
| 90 |
sub unserialize { |
| 91 |
my ( $self, $fields ) = @_; |
| 92 |
my $conf; |
| 93 |
|
| 94 |
# Parse fields |
| 95 |
foreach my $k ( keys %$fields ) { |
| 96 |
my $v = $fields->{$k}; |
| 97 |
if ( $k =~ $hashParameters ) { |
| 98 |
unless ( utf8::is_utf8($v) ) { |
| 99 |
$v = encode( 'UTF-8', $v ); |
| 100 |
} |
| 101 |
$conf->{$k} = ( |
| 102 |
$v =~ /./ |
| 103 |
? eval { from_json( $v, { allow_nonref => 1 } ) } |
| 104 |
: {} |
| 105 |
); |
| 106 |
if ($@) { |
| 107 |
$Lemonldap::NG::Common::Conf::msg .= |
| 108 |
"Unable to decode $k, switching to old format.\n"; |
| 109 |
return $self->oldUnserialize($fields); |
| 110 |
} |
| 111 |
} |
| 112 |
elsif ( $k =~ $arrayParameters ) { |
| 113 |
unless ( utf8::is_utf8($v) ) { |
| 114 |
$v = encode( 'UTF-8', $v ); |
| 115 |
} |
| 116 |
$conf->{$k} = ( |
| 117 |
$v =~ /./ |
| 118 |
? eval { from_json( $v, { allow_nonref => 1 } ) } |
| 119 |
: {} |
| 120 |
); |
| 121 |
if ($@) { |
| 122 |
$Lemonldap::NG::Common::Conf::msg .= |
| 123 |
"Unable to decode $k, switching to old format.\n"; |
| 124 |
return $self->oldUnserialize($fields); |
| 125 |
} |
| 126 |
} |
| 127 |
else { |
| 128 |
$conf->{$k} = $v; |
| 129 |
} |
| 130 |
} |
| 131 |
return $conf; |
| 132 |
} |
| 133 |
|
| 134 |
sub oldUnserialize { |
| 135 |
my ( $self, $fields ) = @_; |
| 136 |
my $conf; |
| 137 |
|
| 138 |
# Parse fields |
| 139 |
while ( my ( $k, $v ) = each(%$fields) ) { |
| 140 |
|
| 141 |
# Remove surrounding quotes |
| 142 |
$v =~ s/^'(.*)'$/$1/s; |
| 143 |
|
| 144 |
# Manage hashes |
| 145 |
|
| 146 |
if ( $k =~ $hashParameters and $v ||= {} and not ref($v) ) { |
| 147 |
$conf->{$k} = {}; |
| 148 |
|
| 149 |
# Value should be a Data::Dumper, else this is an old-old format |
| 150 |
if ( defined($v) and $v !~ /^\$/ ) { |
| 151 |
|
| 152 |
$Lemonldap::NG::Common::Conf::msg .= |
| 153 |
" Warning: configuration is in old format, you've to migrate!"; |
| 154 |
|
| 155 |
eval { require Storable; require MIME::Base64; }; |
| 156 |
if ($@) { |
| 157 |
$Lemonldap::NG::Common::Conf::msg .= " Error: $@"; |
| 158 |
return 0; |
| 159 |
} |
| 160 |
$conf->{$k} = Storable::thaw( MIME::Base64::decode_base64($v) ); |
| 161 |
} |
| 162 |
|
| 163 |
# Convert Data::Dumper |
| 164 |
else { |
| 165 |
my $data; |
| 166 |
$v =~ s/^\$([_a-zA-Z][_a-zA-Z0-9]*) *=/\$data =/; |
| 167 |
$v = $self->unnormalize($v); |
| 168 |
|
| 169 |
# Evaluate expression |
| 170 |
eval $v; |
| 171 |
|
| 172 |
if ($@) { |
| 173 |
$Lemonldap::NG::Common::Conf::msg .= |
| 174 |
" Error: cannot read configuration key $k: $@"; |
| 175 |
} |
| 176 |
|
| 177 |
# Store value in configuration object |
| 178 |
$conf->{$k} = $data; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
# Other fields type |
| 183 |
else { |
| 184 |
$conf->{$k} = $self->unnormalize($v); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
return $conf; |
| 189 |
} |
| 190 |
|
| 191 |
1; |
| 192 |
__END__ |