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