File Coverage

blib/lib/Lemonldap/NG/Common/Conf/Serializer.pm
Criterion Covered Total %
statement 7 53 13.2
branch 0 14 0.0
condition 0 11 0.0
subroutine 2 6 33.3
pod 0 4 0.0
total 9 88 10.2


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