Labyrinth/Globals.pm

filter: Statementstmt 34.5% 96 / 278 Branchbran 5.8% 8 / 138 Conditioncond 25.0% 9 / 36 MC/DCmcdc 5.0% 1 / 20 Subroutinesub 71.8% 23 / 32 Podpod 100% 8 / 8 totaltotal 28.3% 145 / 512 CC 95 SCAR 62.3
CC
95
Cov
25%
LoadSettings
887.6
LoadRules
756.0
ParseParams
240.0
CRAP 509.4
↑ Summary Labyrinth/Writer.pm »
1 package Labyrinth::Globals;
2  
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use vars qw($VERSION @ISA %EXPORT_TAGS @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 $VERSION = '5.32';
8  
9 =head1 NAME
10  
11 Labyrinth::Globals - Configuration and Parameter Handler for Labyrinth
12  
13 =head1 SYNOPSIS
14  
15 use Labyrinth::Globals qw(:all);
16  
17 # database object creation
18 DBConnect();
19  
20 # Interface (CGI) parameter handling
21 ParseParams();
22  
23 =head1 DESCRIPTION
24  
25 The Globals package contains a number of variables and functions that are
26 used across the system. The variables contain input and output values,
27 and the functions are generic.
28  
29 =head1 EXPORT
30  
31 All by default.
32  
33 use Labyrinth::Globals qw(:all); # all methods
34  
35 =cut
36  
37 # -------------------------------------
38 # Export Details
39  
40 12 require Exporter;
41 12 @ISA = qw(Exporter);
42  
43 12 %EXPORT_TAGS = (
44 'all' => [ qw(
45 LoadAll LoadSettings LoadRules ParseParams
46 DBConnect dbh
47 ScriptPath ScriptFile
48 ) ]
49 );
50  
51 12 @EXPORT_OK = ( @{$EXPORT_TAGS{'all'}} );
52 12 @EXPORT = ( @{$EXPORT_TAGS{'all'}} );
53  
54 # -------------------------------------
55 # Library Modules
56  
57 12 ▶use Config::IniFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 12 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 12 ▶use Data::FormValidator;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 12 ▶use Data::FormValidator::Constraints::Upload;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 12 ▶use Data::FormValidator::Constraints::Words;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 12 ▶use Data::FormValidator::Filters::Demoroniser qw(demoroniser);
Subroutine BEGIN: called CC=1 SCAR=0.0
63 12 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 12 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 12 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 12 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 12 ▶use Labyrinth::Audit;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 12 ▶use Labyrinth::Constraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 12 ▶use Labyrinth::Constraints::Emails;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 12 ▶use Labyrinth::DBUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 12 ▶use Labyrinth::DIUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 12 ▶use Labyrinth::Filters qw(float2 float3 float5);
Subroutine BEGIN: called CC=1 SCAR=0.0
74 12 ▶use Labyrinth::Media;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 12 ▶use Labyrinth::Variables;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 12 ▶use Labyrinth::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 # -------------------------------------
79 # Variables
80  
81 12 my %rules; # internal rules hash
82  
83 # -------------------------------------
84 # The Subs
85  
86 =head1 FUNCTIONS
87  
88 =head2 Loaders
89  
90 =over 4
91  
92 =item LoadAll([$dir])
93  
94 LoadAll() automatically loads and instatiates many global variables. The
95 method assumes default values are required. Can be called with a base install
96 directory path, which is then used by LoadSettings().
97  
98 This method should be called at the beginning of any script.
99  
100 =item LoadSettings($dir)
101  
102 LoadSettings() loads a settings file (config/settings.ini) and stores them
103 in an internal hash. Typical settings are database settings (eq driver,
104 database, user, password) and general settings (eg administrator email).
105  
106 LoadSettings() can be passed the name of the base install directory, or it will
107 attempt to figure it out via the current working directory.
108  
109 =item LoadRules()
110  
111 LoadRules() loads a rules file (default is parserules.ini or the name of the
112 'parsefile' in the settings configuration file) and store the rules in an
113 internal hash. This hash is then used to verify the contains of any interface
114 (CGI) parameters passed to the script.
115  
116 Note that as LoadRules() can be called many times with different rules
117 files, only the last value of a given rule is stored. This is useful if
118 you wish to have a standard rules file and wish to load further or
119 different rules dependant upon the script being used.
120  
121 =back
122  
123 =cut
124  
125 0 ▶sub LoadAll {
Subroutine LoadAll: not called CC=1 SCAR=6.9
Pod documented
126 0 my $settings = shift;
127  
128 0 LoadSettings($settings);
129 0 ParseParams();
130 0 DBConnect();
131 }
132  
133 2 ▶sub LoadSettings {
Subroutine LoadSettings: called CC=45 SCAR=67.9
Pod documented
134 2 my $settings = shift;
135 2 ▶ $settings ||= '';
Condition100%2/2
$settings ||= ""
l!l
11
MC/DC100%1/1
$settings ||= ""
$settings
Truth table100%2/2
A
A= $settings
Aresult
11
00
136  
137 # default file names
138 2 my $LOGFILE = 'audit.log';
139 2 my $PHRASEBOOK = 'phrasebook.ini';
140 2 my $PARSEFILE = 'parserules.ini';
141  
142 #print STDERR "# ENV $_ => $ENV{$_}\n" for('HTTP_HOST', 'REMOTE_ADDR', 'SERVER_PROTOCOL', 'SERVER_PORT');
143 # Server/HTTP values
144 2 ▶ my $host = $ENV{'HTTP_HOST'} || '';
Condition50%1/2
$ENV{'HTTP_HOST'} || ''
l!l
02
MC/DC0%0/1
$ENV{'HTTP_HOST'} || ''
$ENV{'HTTP_HOST'}
Truth table50%1/2
A
A= $ENV{'HTTP_HOST'}
Aresult
11
00
145 2 ▶ my $ipaddr = $ENV{'REMOTE_ADDR'} || '';
Condition50%1/2
$ENV{'REMOTE_ADDR'} || ''
l!l
02
MC/DC0%0/1
$ENV{'REMOTE_ADDR'} || ''
$ENV{'REMOTE_ADDR'}
Truth table50%1/2
A
A= $ENV{'REMOTE_ADDR'}
Aresult
11
00
146 my ($protocol) = $ENV{'SERVER_PROTOCOL'}
147 ? ($ENV{'SERVER_PROTOCOL'} =~ m!^(\w+)\b!)
148 2 ▶ : $ENV{'SERVER_PORT'} && $ENV{'SERVER_PORT'} eq '443'
Branch: 2 branches
BranchTrueFalseTotal
$ENV{'SERVER_PROTOCOL'} ? : 0 2 2
$ENV{'SERVER_PORT'} && $ENV{'SERVER_PORT'} eq '443' ? : 0 2 2
Condition33%1/3
$ENV{"SERVER_PORT"} and $ENV{"SERVER_PORT"} eq 443
!ll&&!rl&&r
200
MC/DC0%0/2
$ENV{"SERVER_PORT"} and $ENV{"SERVER_PORT"} eq 443
$ENV{"SERVER_PORT"} $ENV{"SERVER_PORT"} eq 443
Truth table33%1/3
A and B
A= $ENV{"SERVER_PORT"}B= $ENV{"SERVER_PORT"} eq 443
ABresult
0X0
100
111
149 ? ('https')
150 : ('http');
151 2 $protocol = lc($protocol);
152  
153 2 ▶ my $path = $ENV{'REQUEST_URI'} ? 'REQUEST_URI' : 'PATH_INFO';
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'REQUEST_URI'} ? : 0 2 2
154 2 ▶ my ($req,$script) = ($ENV{$path} && $ENV{$path} =~ m|^(.*)/([^?]+)|) ? ($1,$2) : ('','');
Branch: 1 branch
BranchTrueFalseTotal
$ENV{$path} && $ENV{$path} =~ m[^(.*)/([^?]+)] ? : 0 2 2
Condition33%1/3
$ENV{$path} and $ENV{$path} =~ m[^(.*)/([^?]+)]
!ll&&!rl&&r
200
MC/DC0%0/2
$ENV{$path} and $ENV{$path} =~ m[^(.*)/([^?]+)]
$ENV{$path} $ENV{$path} =~ m[^(.*)/([^?]+)]
Truth table33%1/3
A and B
A= $ENV{$path}B= $ENV{$path} =~ m[^(.*)/([^?]+)]
ABresult
0X0
100
111
155 2 ▶ my $cgiroot = ($req =~ /^$protocol:/) ? $req : $protocol . '://' . ($ENV{'HTTP_HOST'} ? $ENV{'HTTP_HOST'} : '') . $req;
Branch: 2 branches
BranchTrueFalseTotal
$req =~ /^$protocol:/ ? : 0 2 2
$ENV{'HTTP_HOST'} ? : 0 2 2
156 2 ▶ my $docroot = ($req && $cgiroot =~ m!^((.*)/.*?)! ? $1 : $cgiroot);
Branch: 1 branch
BranchTrueFalseTotal
$req && $cgiroot =~ m[^((.*)/.*?)] ? : 0 2 2
Condition33%1/3
$req and $cgiroot =~ m[^((.*)/.*?)]
!ll&&!rl&&r
200
MC/DC0%0/2
$req and $cgiroot =~ m[^((.*)/.*?)]
$req $cgiroot =~ m[^((.*)/.*?)]
Truth table33%1/3
A and B
A= $reqB= $cgiroot =~ m[^((.*)/.*?)]
ABresult
0X0
100
111
157 2 $cgiroot =~ s!/$!!;
158 2 $docroot =~ s!/$!!;
159  
160 # set defaults
161 2 my ($cgipath,$webpath) = ($cgiroot,$docroot);
162  
163 # load the configuration data
164 2 ▶ unless($settings && -r $settings) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($settings and -r $settings) 2 0 2
Condition66%2/3
$settings and -r $settings
!ll&&!rl&&r
110
MC/DC0%0/2
$settings and -r $settings
$settings -r $settings
Truth table66%2/3
A and B
A= $settingsB= -r $settings
ABresult
0X0
100
111
165 2 LogError("Cannot read settings file [$settings]");
166 2 SetError('ERROR',"Cannot read settings file");
167 2 return;
168 }
169  
170 0 my $cfg = Config::IniFiles->new( -file => $settings );
171 0 ▶ unless(defined $cfg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cfg) 0 0 0
172 0 LogError("Unable to load settings file [$settings]: @Config::IniFiles::errors");
173 0 SetError('ERROR',"Unable to load settings file");
174 0 return;
175 }
176  
177 # load the configuration data
178 0 for my $sect ($cfg->Sections()) {
179 0 for my $name ($cfg->Parameters($sect)) {
180 0 my @value = $cfg->val($sect,$name);
181 0 ▶ next unless(@value);
Branch: 1 branch
BranchTrueFalseTotal
unless @value 0 0 0
182 0 ▶ if(@value > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@value > 1) { } 0 0 0
elsif (@value == 1) { } 0 0 0
183 0 $settings{$name} = \@value;
184 0 ▶ $tvars{$name} = \@value if($sect =~ /^(PROJECT|HTTP|CMS)$/);
Branch: 1 branch
BranchTrueFalseTotal
if $sect =~ /^(PROJECT|HTTP|CMS)$/ 0 0 0
185 } elsif(@value == 1) {
186 0 $settings{$name} = $value[0];
187 0 ▶ $tvars{$name} = $value[0] if($sect =~ /^(PROJECT|HTTP|CMS)$/);
Branch: 1 branch
BranchTrueFalseTotal
if $sect =~ /^(PROJECT|HTTP|CMS)$/ 0 0 0
188 }
189 }
190 }
191 0 $cfg = undef;
192  
193 SetLogFile( FILE => $settings{'logfile'},
194 USER => 'labyrinth',
195 LEVEL => ($settings{'loglevel'} || 0),
196 CLEAR => (defined $settings{'logclear'} ? $settings{'logclear'} : 1),
197 0 ▶ CALLER => (defined $settings{'logcaller'} ? $settings{'logcaller'} : 1)
Branch: 2 branches
BranchTrueFalseTotal
defined $Labyrinth::Globals::settings{'logclear'} ? : 0 0 0
defined $Labyrinth::Globals::settings{'logcaller'} ? : 0 0 0
Condition0%0/2
$Labyrinth::Globals::settings{'loglevel'} || 0
l!l
00
MC/DC0%0/1
$Labyrinth::Globals::settings{'loglevel'} || 0
$Labyrinth::Globals::settings{'loglevel'}
Truth table0%0/2
A
A= $Labyrinth::Globals::settings{'loglevel'}
Aresult
11
00
198 );
199  
200 # evaluate standard path settings
201 0 $settings{'protocol'} = $protocol;
202 0 $settings{'host'} = $host;
203 0 $settings{'ipaddr'} = $ipaddr;
204 0 $settings{'docroot'} = $docroot;
205 0 $settings{'cgiroot'} = $cgiroot;
206 0 $settings{'script'} = $script;
207 0 ▶ $settings{'logdir'} = "$settings{'webdir'}/cache" unless($settings{'logdir'});
Branch: 1 branch
BranchTrueFalseTotal
unless $Labyrinth::Globals::settings{"logdir"} 0 0 0
208 0 ▶ $settings{'config'} = "$settings{'cgidir'}/config" unless($settings{'config'});
Branch: 1 branch
BranchTrueFalseTotal
unless $Labyrinth::Globals::settings{"config"} 0 0 0
209 0 ▶ $settings{'templates'} = "$settings{'cgidir'}/templates" unless($settings{'templates'});
Branch: 1 branch
BranchTrueFalseTotal
unless $Labyrinth::Globals::settings{"templates"} 0 0 0
210 0 ▶ $settings{'webpath'} = $webpath unless(exists $settings{'webpath'});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Labyrinth::Globals::settings{"webpath"} 0 0 0
211 0 ▶ $settings{'cgipath'} = $cgipath unless(exists $settings{'cgipath'});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Labyrinth::Globals::settings{"cgipath"} 0 0 0
212  
213 0 $tvars{$_} = $settings{$_} for(qw(host docroot cgiroot webpath cgipath script ipaddr));
214  
215 0 ▶ $settings{'logfile'} = "$settings{'logdir'}/$LOGFILE" unless($settings{'logfile'});
Branch: 1 branch
BranchTrueFalseTotal
unless $Labyrinth::Globals::settings{"logfile"} 0 0 0
216 0 ▶ $settings{'phrasebook'} = "$settings{'config'}/$PHRASEBOOK" unless($settings{'phrasebook'});
Branch: 1 branch
BranchTrueFalseTotal
unless $Labyrinth::Globals::settings{"phrasebook"} 0 0 0
217 0 ▶ $settings{'parsefile'} = "$settings{'config'}/$PARSEFILE" unless($settings{'parsefile'});
Branch: 1 branch
BranchTrueFalseTotal
unless $Labyrinth::Globals::settings{"parsefile"} 0 0 0
218  
219 # generate the absolute path, in the event of errors
220 0 foreach my $key (qw(logfile phrasebook parsefile)) {
221 0 ▶ next unless $settings{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $Labyrinth::Globals::settings{$key} 0 0 0
222 0 ▶ next if $settings{$key} =~ m|^/|;
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::Globals::settings{$key} =~ m[^/] 0 0 0
223 0 $settings{$key} = File::Spec->rel2abs( $settings{$key} ) ;
224 }
225  
226 # path & title mappings
227 0 for my $map (qw(path title)) {
228 0 ▶ next unless($settings{$map . 'maps'});
Branch: 1 branch
BranchTrueFalseTotal
unless $Labyrinth::Globals::settings{$map . 'maps'} 0 0 0
229 0 ▶ if( ref($settings{$map . 'maps'}) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $Labyrinth::Globals::settings{$map . 'maps'} eq 'ARRAY') { } 0 0 0
elsif ($Labyrinth::Globals::settings{$map . 'maps'}) { } 0 0 0
230 0 for(@{ $settings{$map . 'maps'} }) {
231 0 my ($name,$value) = split(/=/,$_,2);
232 0 $settings{$map . 'map'}{$name} = $value;
233 }
234 } elsif($settings{$map . 'maps'}) {
235 0 my ($name,$value) = split(/=/,$settings{$map . 'maps'},2);
236 0 $settings{$map . 'map'}{$name} = $value;
237 }
238 }
239  
240 #LogDebug("settings=".Dumper(\%settings));
241  
242 # set image processing driver, if specified
243 0 ▶ Labyrinth::DIUtils::Tool($settings{diutils}) if($settings{diutils});
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::Globals::settings{"diutils"} 0 0 0
244  
245 0 $settings{settingsloaded} = 1;
246 }
247  
248 0 ▶sub LoadRules {
Subroutine LoadRules: not called CC=27 SCAR=66.3
Pod documented
249 0 ▶ return if($settings{rulesloaded});
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::Globals::settings{"rulesloaded"} 0 0 0
250  
251 # ensure we can access the rules file
252 0 ▶ my $rules = shift || $settings{'parsefile'} || '';
Condition0%0/5
shift() || $Labyrinth::Globals::settings{'parsefile'} || ''
l!l
00
shift() or $Labyrinth::Globals::settings{"parsefile"}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $Labyrinth::Globals::settings{'parsefile'} || ''
shift() $Labyrinth::Globals::settings{"parsefile"}
Truth table0%0/3
A or B
A= shift()B= $Labyrinth::Globals::settings{"parsefile"}
ABresult
1X1
011
000
253 0 ▶ if(!$rules || !-f $rules || !-r $rules) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rules and -f $rules and -r $rules) 0 0 0
Condition0%0/6
$rules and -f $rules and -r $rules
!ll&&!rl&&r
000
$rules and -f $rules
!ll&&!rl&&r
000
MC/DC0%0/3
$rules and -f $rules and -r $rules
$rules -f $rules -r $rules
Truth table0%0/4
A and B and C
A= $rulesB= -f $rulesC= -r $rules
ABCresult
0XX0
10X0
1100
1111
254 0 LogError("Cannot read rules file [$rules]");
255 0 SetError('ERROR',"Cannot read rules file");
256 0 return;
257 }
258  
259 0 my $fh = IO::File->new($rules, 'r');
260 0 ▶ unless(defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 0 0
261 0 LogError("Cannot open rules file [$rules]: $!");
262 0 SetError('ERROR',"Cannot open rules file");
263 0 return;
264 }
265  
266 %rules = (
267 0 validator_packages => [qw( Data::FormValidator::Constraints::Upload
268 Data::FormValidator::Constraints::Words
269 Labyrinth::Constraints::Emails
270 Labyrinth::Constraints
271 Labyrinth::Filters
272 )],
273 filters => ['trim', demoroniser()],
274 msgs => {prefix=> 'err_'}, # set a custom error prefix
275 # untaint_all_constraints => 1,
276 missing_optional_valid => 1,
277 constraint_methods => {
278 realname => \&realname,
279 basicwords => \&basicwords,
280 simplewords => \&simplewords,
281 paragraph => \&paragraph,
282 emails => \&emails,
283 url => \&url,
284 ddmmyy => \&ddmmyy
285 },
286 );
287  
288 0 my ($required_regex,$optional_regex);
289  
290 0 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
291 0 s/\s+$//;
292  
293 0 my ($name,$required,$default,$filters,$constraint,$regex) = split(',',$_,6);
294 0 ▶ next unless($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
295  
296 0 ▶ $name =~ s/\s+$// if(defined $name);
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 0 0 0
297 0 ▶ $required =~ s/\s+$// if(defined $required);
Branch: 1 branch
BranchTrueFalseTotal
if defined $required 0 0 0
298 0 ▶ $default =~ s/\s+$// if(defined $default);
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 0 0 0
299 0 ▶ $filters =~ s/\s+$// if(defined $filters);
Branch: 1 branch
BranchTrueFalseTotal
if defined $filters 0 0 0
300 0 ▶ $constraint =~ s/\s+$// if(defined $constraint);
Branch: 1 branch
BranchTrueFalseTotal
if defined $constraint 0 0 0
301  
302 # $rules{$name}->{required} = $required;
303 # $rules{$name}->{default} = $default;
304 # $rules{$name}->{constraint} = $constraint;
305 # $rules{$name}->{regex} = "@regex";
306  
307 0 ▶ if($name =~ /^:(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^:(.*)/) { } 0 0 0
308 0 $name = qr/$1/;
309 0 ▶ if($required) { $required_regex .= "$name|" }
Branch: 1 branch
BranchTrueFalseTotal
if ($required) { } 0 0 0
310 0 else { $optional_regex .= "$name|" }
311 0 ▶ if($constraint) { $rules{constraint_regexp_map}->{$name} = _constraint($constraint) }
Branch: 2 branches
BranchTrueFalseTotal
if ($constraint) { } 0 0 0
elsif ($regex) { } 0 0 0
312 0 elsif($regex) { $rules{constraint_regexp_map}->{$name} = qr/^$regex$/ }
313 0 else { die "no constraint or regex for entry: $name" }
314 0 ▶ if($filters) { $rules{field_filter_regexp_map}->{$name} = [split(":",$filters)] }
Branch: 1 branch
BranchTrueFalseTotal
if ($filters) 0 0 0
315 } else {
316 0 ▶ if($required) { push @{$rules{required}}, $name }
Branch: 1 branch
BranchTrueFalseTotal
if ($required) { } 0 0 0
317 0 else { push @{$rules{optional}}, $name }
318 0 ▶ if($constraint) { $rules{constraints}->{$name} = _constraint($constraint) }
Branch: 2 branches
BranchTrueFalseTotal
if ($constraint) { } 0 0 0
elsif ($regex) { } 0 0 0
319 0 elsif($regex) { $rules{constraints}->{$name} = qr/^$regex$/ }
320 0 else { die "no constraint or regex for entry: $name" }
321 0 ▶ if($default) { $rules{defaults}->{$name} = $default }
Branch: 1 branch
BranchTrueFalseTotal
if ($default) 0 0 0
322 0 ▶ if($filters) { $rules{field_filters}->{$name} = [split(":",$filters)] }
Branch: 1 branch
BranchTrueFalseTotal
if ($filters) 0 0 0
323 }
324 }
325 0 $fh->close;
326  
327 #LogDebug("Constraints: rules=" . Dumper(\%rules));
328  
329 0 ▶ if($required_regex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($required_regex) 0 0 0
330 0 $required_regex =~ s/|$//;
331 0 $rules{required_regexp} = qr/^$required_regex$/;
332 }
333  
334 0 ▶ if($optional_regex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optional_regex) 0 0 0
335 0 $optional_regex =~ s/|$//;
336 0 $rules{optional_regexp} = qr/^$optional_regex$/;
337 }
338  
339 0 $rules{debug} = 0;
340  
341 0 $settings{rulesloaded} = 1;
342 }
343  
344 0 ▶sub _constraint {
Subroutine _constraint: not called CC=2 SCAR=17.9
345 0 my $constraint = shift;
346 0 ▶ if($constraint eq 'imagefile') {
Branch: 1 branch
BranchTrueFalseTotal
if ($constraint eq 'imagefile') { } 0 0 0
347 0 my %hash = (
348 constraint_method => 'file_format',
349 params => [mime_types => [qw!image/jpe image/jpg image/jpeg image/gif image/png!]],
350 );
351 0 return \%hash;
352 } else {
353 0 my %hash = (
354 constraint_method => $constraint,
355 );
356 0 return \%hash;
357 }
358  
359 0 return $constraint;
360 }
361  
362 =head2 Script Name
363  
364 =over 4
365  
366 =item ScriptPath()
367  
368 =item ScriptFile()
369  
370 =back
371  
372 =cut
373  
374 0 ▶sub ScriptPath {
Subroutine ScriptPath: not called CC=2 SCAR=17.9
Pod documented
375 0 ▶ return $settings{cgipath} if($settings{cgipath} =~ m!^http!); # we're assuming only http/https
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::Globals::settings{"cgipath"} =~ /^http/ 0 0 0
376 0 return $settings{cgiroot};
377 }
378  
379 0 ▶sub ScriptFile {
Subroutine ScriptFile: not called CC=4 SCAR=30.0
Pod documented
380 0 my %hash = @_;
381 0 ▶ my $path = ScriptPath() || '';
Condition0%0/2
ScriptPath() || ''
l!l
00
MC/DC0%0/1
ScriptPath() || ''
ScriptPath()
Truth table0%0/2
A
A= ScriptPath()
Aresult
11
00
382 0 ▶ my $file = $hash{file} || $settings{script};
Condition0%0/3
$hash{'file'} || $Labyrinth::Globals::settings{'script'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$hash{'file'} || $Labyrinth::Globals::settings{'script'}
$hash{'file'} $Labyrinth::Globals::settings{'script'}
Truth table0%0/3
A || B
A= $hash{'file'}B= $Labyrinth::Globals::settings{'script'}
ABresult
1X1
011
000
383 0 ▶ my $query = $hash{query} ? '?' . $hash{query} : '';
Branch: 1 branch
BranchTrueFalseTotal
$hash{'query'} ? : 0 0 0
384  
385 0 return "$path/$file$query";
386 }
387  
388 =head2 Database Handling
389  
390 =over 4
391  
392 =item DBConnect()
393  
394 The method to initiate the Database access object. The method passes the
395 values held within the internal settings (set LoadSettings()), to the DB
396 access object constructor. It returns and stores internally the object
397 reference, which can be accessed across the system via the $dbi scalar.
398  
399 =item dbh
400  
401 Returns the reference to the DB access object, as created by the DBConnect()
402 method, or calls DBConnect() if not previously invoked.
403  
404 =back
405  
406 =cut
407  
408 0 ▶sub DBConnect {
Subroutine DBConnect: not called CC=2 SCAR=17.9
Pod documented
409 0 ▶ return $dbi if $dbi; # object already exists
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::Globals::dbi 0 0 0
410  
411 # use settings or defaults
412 0 my $logfile = $settings{logfile};
413 0 my $phrasebook = $settings{phrasebook};
414 0 my $dictionary = $settings{dictionary};
415  
416 $dbi = Labyrinth::DBUtils->new({
417 driver => $settings{driver},
418 database => $settings{database},
419 dbfile => $settings{dbfile},
420 dbhost => $settings{dbhost},
421 dbport => $settings{dbport},
422 dbuser => $settings{dbuser},
423 dbpass => $settings{dbpass},
424 autocommit => $settings{autocommit},
425 0 logfile => $logfile,
426 phrasebook => $phrasebook,
427 dictionary => $dictionary,
428 });
429 0 LogDebug("DBConnect DONE");
430  
431 0 $dbi;
432 }
433  
434 # used by the DB access object
435 0 ▶sub _errors {
Subroutine _errors: not called CC=4 SCAR=30.0
436 0 my $err = shift;
437 0 my $sql = shift;
438 0 my $message = '';
439  
440 0 ▶ $message = "$err<br />" if($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
441 0 ▶ $message .= "<br />SQL=$sql<br />" if($sql);
Branch: 1 branch
BranchTrueFalseTotal
if $sql 0 0 0
442 0 ▶ $message .= "ARGS=[".join(",",@_)."]" if(@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
443  
444 0 $tvars{failures} = [ { code => 'DB', message => $message } ];
445 0 PublishCode('MESSAGE');
446 0 exit;
447 }
448  
449 0 ▶sub dbh {
Subroutine dbh: not called CC=2 SCAR=17.9
Pod documented
450 0 ▶ $dbi || DBConnect;
Branch: 1 branch
BranchTrueFalseTotal
$Labyrinth::Globals::dbi or DBConnect() 0 0 0
451 }
452  
453 =head2 Interface Parameter Handling
454  
455 =over 4
456  
457 =item ParseParams($rules)
458  
459 ParseParams() reads and validates the interface (CGI) parameters that are sent
460 via a HTTP request, before storing them in the %cgiparams hash. Each parameter
461 must have a rule for it to be accepted.
462  
463 The rules file (default is parserules.ini) is automatically loaded and stored.
464  
465 All valid input parameter values (scalars only) are also automatically stored
466 in the template variable hash, %tvars. This is to enable templates to be
467 reparsed in the event of an error, and retain the user's valid entries.
468  
469 =back
470  
471 =cut
472  
473 0 ▶sub ParseParams {
Subroutine ParseParams: not called CC=15 SCAR=54.8
Pod documented
474 0 ▶ LoadRules($_[0]) unless($settings{rulesloaded});
Branch: 1 branch
BranchTrueFalseTotal
unless $Labyrinth::Globals::settings{"rulesloaded"} 0 0 0
475  
476 0 my $results;
477  
478 # LogDebug("rules=".Dumper(\%rules));
479  
480 0 ▶ if(!defined $ENV{'SERVER_SOFTWARE'}) { # commandline testing
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ENV{'SERVER_SOFTWARE'}) { } 0 0 0
481 0 my $file = "$settings{'config'}/cgiparams.nfo";
482 0 ▶ if(-r $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $file) 0 0 0
483 0 ▶ my $fh = IO::File->new($file, 'r') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($file, "r") or return 0 0 0
484 0 my (%params,$params);
485 0 { local $/ = undef; $params = <$fh>; }
486 0 $fh->close;
487 0 foreach my $param (split(/[\r\n]+/,$params)) {
488 0 my ($name,$value) = $param =~ /(\w+)=(.*)/;
489 0 ▶ next unless($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
490  
491 0 ▶ if($value =~ /\[([^\]]+)\]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\[([^\]]+)\]/) { } 0 0 0
492 0 @{$params{$name}} = split(",",$1);
493 } else {
494 0 $params{$name} = $value;
495 }
496 }
497  
498 0 LogDebug("params=".Dumper(\%params));
499 0 $results = Data::FormValidator->check(\%params, \%rules);
500 0 $settings{testing} = 1;
501 }
502  
503 } else {
504 0 my %fdat = $cgi->Vars;
505 0 LogDebug("fdat=".Dumper(\%fdat));
506  
507 # Due to a problem with DFV, we handle images separately
508 0 for my $param ( grep { /^IMAGEUPLOAD/ } keys %fdat ) {
509 0 ▶ if( $cgi->param($param) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Labyrinth::Globals::cgi->param($param)) 0 0 0
510 0 CGIFile($param);
511 0 $settings{cgiimages}{$param} = 1;
512 }
513 0 $cgi->delete($param)
514 }
515  
516 # my %fields = map {$_ => 1} @{$rules{required}}, @{$rules{optional}};
517 # for (keys %fdat) {
518 # LogDebug("NO RULE: $_")
519 # unless( $fields{$_} ||
520 # ($rules{required_regexp} && $_ =~ $rules{required_regexp}) ||
521 # ($rules{optional_regexp} && $_ =~ $rules{optional_regexp})
522 # );
523 # }
524  
525 0 $results = Data::FormValidator->check($cgi, \%rules);
526 }
527  
528 0 ▶ if($results) {
Branch: 1 branch
BranchTrueFalseTotal
if ($results) { } 0 0 0
529 # LogDebug("results=".Dumper($results));
530 0 my $values = $results->valid;
531 0 %cgiparams = %$values;
532 0 $values = $results->msgs;
533 0 foreach my $key (keys %$values) {
534 0 ▶ $tvars{$key} = $values->{$key} if($key =~ /^err_/);
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^err_/ 0 0 0
535 }
536  
537 # LogDebug("GOT RULE: env=" . Dumper(\%ENV));
538 # LogDebug("GOT RULE: rules=" . Dumper(\%rules));
539 } else {
540 0 LogDebug("NO Data::FormValidator RESULTS!");
541 0 my( $valids, $missings, $invalids, $unknowns ) = Data::FormValidator->validate($cgi, \%rules);
542 0 LogDebug("NO RULE: valids=" . Dumper($valids));
543 0 LogDebug("NO RULE: invalids=" . Dumper($invalids));
544 # LogDebug("NO RULE: missings=" . Dumper($missings));
545 # LogDebug("NO RULE: unknowns=" . Dumper($unknowns));
546 # LogDebug("NO RULE: env=" . Dumper(\%ENV));
547 # LogDebug("NO RULE: rules=" . Dumper(\%rules));
548 0 %cgiparams = %$valids;
549 0 $cgiparams{'err_'.$_} = 'Invalid' for(@$invalids);
550 }
551  
552 0 $cgiparams{$_} = 1 for(keys %{$settings{cgiimages}});
553  
554 0 LogDebug("cgiparams=".Dumper(\%cgiparams));
555 0 LogInfo("ParseParams DONE");
556 }
557  
558  
559 12 1;
560  
561 __END__
↑ Summary Labyrinth/Writer.pm »