Labyrinth/Variables.pm

filter: Statementstmt 65.5% 76 / 116 Branchbran 13.6% 3 / 22 Conditioncond 18.1% 2 / 11 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 72.2% 13 / 18 Podpod 100% 7 / 7 totaltotal 56.6% 102 / 180 CC 21 SCAR 30.3
CC
21
Cov
54%
LoadProfiles
72.0
CGIArray
20.0
ParamsCheck
12.0
CRAP 20.6
« Labyrinth/Writer/Parser/TT.pm ↑ Summary Labyrinth/DIUtils.pm »
1 package Labyrinth::Variables;
2  
3 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 18 ▶use vars qw($VERSION @ISA %EXPORT_TAGS @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 $VERSION = '5.32';
8  
9 =head1 NAME
10  
11 Labyrinth::Variables - Generic Variables for Labyrinth
12  
13 =head1 SYNOPSIS
14  
15 use Labyrinth::Variables;
16  
17 # output values
18 $tvars{title} = 'My Title';
19  
20 =head1 DESCRIPTION
21  
22 The Variables package contains a number of variables that are
23 used across the system. The variables contain input and output values,
24 and the functions are generic.
25  
26 =head1 EXPORT
27  
28 use Labyrinth::Variables; # default (:all) = (:vars :subs)
29 use Labyrinth::Variables qw(:vars); # all variable containers
30 use Labyrinth::Variables qw(:subs); # all standard subroutines
31 use Labyrinth::Variables qw(:xsub); # all extended subroutines
32  
33 =cut
34  
35 # -------------------------------------
36 # Constants
37  
38 18 ▶use constant PUBLIC => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 18 ▶use constant USER => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 18 ▶use constant EDITOR => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 18 ▶use constant PUBLISHER => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 18 ▶use constant ADMIN => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 18 ▶use constant MASTER => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 # -------------------------------------
46 # Export Details
47  
48 18 require Exporter;
49 18 @ISA = qw(Exporter);
50  
51 18 %EXPORT_TAGS = (
52 'vars' => [ qw(
53 PUBLIC USER EDITOR PUBLISHER ADMIN MASTER
54 $dbi %cgiparams %tvars %settings $cgi
55 ) ],
56 'subs' => [ qw(
57 CGIArray ParamsCheck SetError SetCommand
58 ) ],
59 'all' => [ qw(
60 PUBLIC USER EDITOR PUBLISHER ADMIN MASTER
61 $dbi %cgiparams %tvars %settings $cgi
62 CGIArray ParamsCheck SetError SetCommand
63 LoadProfiles LoadAccess
64 ) ],
65 );
66  
67 18 @EXPORT_OK = ( @{$EXPORT_TAGS{'all'}} );
68 18 @EXPORT = ( @{$EXPORT_TAGS{'all'}} );
69  
70 # -------------------------------------
71 # Library Modules
72  
73 18 ▶use Config::IniFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 18 ▶use Regexp::Assemble;
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 # -------------------------------------
77 # Variables
78  
79 =head2 Global Variables
80  
81 =over 4
82  
83 =item %cgiparams
84  
85 Holds all the scalar CGI parameter values. Access parameters as:
86  
87 my $value = $cgiparams{$name};
88  
89 =item %tvars
90  
91 Holds all the template variable values, for use with the template parser.
92 Access template variables as:
93  
94 my $value = $tvars{$name}; # get the named variable
95 $tvars{$name} = $value; # set scalar variable
96 $tvars{$hash} = \%hash; # set hash variable
97 $tvars{$list} = \@list; # set array variable
98  
99 =item $dbi
100  
101 Holds the reference to the DB access object. Created by the DBConnect()
102 method, which must be called before any database activity commences.
103  
104 =back
105  
106 =cut
107  
108 18 our %cgiparams; # contains valid CGI parameters
109 18 our %tvars; # template variable container
110 18 our %settings; # internal settings hash
111 18 our $dbi; # database object
112 18 our $cgi; # CGI object
113  
114 # -------------------------------------
115 # Variable Functions
116  
117 =head2 Initialisation
118  
119 =over 4
120  
121 =item init
122  
123 Prepares the standard variable values, so that they are only called once on setup.
124  
125 =back
126  
127 =cut
128  
129 7 ▶sub init {
Subroutine init: called CC=6 SCAR=18.6
Pod documented
130 7 my $prot = qr{(?:http|https|ftp|afs|news|nntp|mid|cid|mailto|wais|prospero|telnet|gopher|git|file)://};
131 7 my $atom = qr{[a-z\d]}i;
132 7 my $host = qr{(?:$atom(?:(?:$atom|-)*$atom)?)};
133 7 my $domain = qr{(?:(?:(?:$host(?:\.$host)*))*(?:\.[a-zA-Z](?:$atom)*$atom)+)};
134 7 my $ip = qr{(?:(?:\d+)(?:\.(?:\d+)){3})(?::(?:\d+))?};
135 7 my $enc = qr{%[a-fA-F\d]{2}};
136 7 my $legal1 = qr{[a-zA-Z\d\$_.+!*\'(),~\#-]};
137 7 my $legal2 = qr{[\/;:@&=]};
138 7 my $legal3 = qr{(?:(?:$legal1|$enc)+(?:(?:$legal2)+(?:$legal1|$enc)+)*)};
139 7 my $path = qr{\/(?:$legal3)+};
140 7 my $query = qr{(?:\?$legal3)+};
141 7 my $local = qr{[-\w\'=.]+};
142  
143 7 my $url1 = qr{(?: ($prot)? ($domain|$ip|\/$|$path) ($path)* ($query)? ) (\#[-\w.]+)?}x;
144 7 my $url2 = qr{(?: (?:$prot) (?:$domain|$ip|\/$|$path) (?:$path)* (?:$query)? ) (?:\#[-\w.]+)?}x;
145 7 my $email = qr{$local\@(?:$domain|$ip)};
146  
147 7 $settings{protregex} = $prot;
148 7 $settings{urlregex} = $url1; #qr{\b$url1\b};
149 7 $settings{urlstrict} = $url2; #qr{\b$url2\b};
150 7 $settings{emailregex} = $email;
151  
152  
153 7 $settings{crawler} = 0;
154 7 ▶ if($settings{crawlers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Labyrinth::Variables::settings{"crawlers"}) 0 7 7
155 0 my $ra = Regexp::Assemble->new;
156 0 $ra->add( '\b' . quotemeta( $_ ) . '\b' ) for(@{ $settings{crawlers} });
157 0 my $re = $ra->re;
158 0 ▶ $settings{crawler} = 1 if($ENV{'HTTP_USER_AGENT'} =~ $re);
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"HTTP_USER_AGENT"} =~ /$re/ 0 0 0
159 }
160  
161  
162 7 ▶ $settings{'query-parser'} ||= 'CGI';
Condition100%2/2
$Labyrinth::Variables::settings{"query-parser"} ||= "CGI"
l!l
16
MC/DC100%1/1
$Labyrinth::Variables::settings{"query-parser"} ||= "CGI"
$Labyrinth::Variables::settings{"query-parser"}
Truth table100%2/2
A
A= $Labyrinth::Variables::settings{"query-parser"}
Aresult
11
00
163 7 my $class = 'Labyrinth::Query::' . $settings{'query-parser'};
164  
165 7 eval {
166 7 eval "CORE::require $class";
167 7 $cgi = $class->new();
168 };
169  
170 7 ▶ die "Cannot load Query package for '$settings{'query-parser'}': $@" if($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
171 }
172  
173 =head2 CGI Parameter Handling
174  
175 =over 4
176  
177 =item CGIArray($name)
178  
179 ParseParams only handles the scalar interface (CGI) parameters. In the event
180 an array is required, CGIArray() is used to find and validate the parameter,
181 before returning the list of values.
182  
183 =item ParamsCheck
184  
185 Given a list of fields, checks whether the interface (CGI) parameters have
186 been set. Sets error conditions if any are missing.
187  
188 =back
189  
190 =cut
191  
192 0 ▶sub CGIArray {
Subroutine CGIArray: not called CC=4 SCAR=30.0
Pod documented
193 0 my $name = shift;
194 0 ▶ return () unless(defined $cgiparams{$name} && $cgiparams{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Labyrinth::Variables::cgiparams{$name} and $Labyrinth::Variables::cgiparams{$name} 0 0 0
Condition0%0/3
defined $Labyrinth::Variables::cgiparams{$name} and $Labyrinth::Variables::cgiparams{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Labyrinth::Variables::cgiparams{$name} and $Labyrinth::Variables::cgiparams{$name}
defined $Labyrinth::Variables::cgiparams{$name} $Labyrinth::Variables::cgiparams{$name}
Truth table0%0/3
A and B
A= defined $Labyrinth::Variables::cgiparams{$name}B= $Labyrinth::Variables::cgiparams{$name}
ABresult
0X0
100
111
195 0 ▶ return ($cgiparams{$name}) unless(ref $cgiparams{$name} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $Labyrinth::Variables::cgiparams{$name} eq "ARRAY" 0 0 0
196 0 return @{$cgiparams{$name}};
197 }
198  
199 0 ▶sub ParamsCheck {
Subroutine ParamsCheck: not called CC=3 SCAR=24.8
Pod documented
200 0 for my $field (@_) {
201 0 ▶ next if($cgiparams{$field});
Branch: 1 branch
BranchTrueFalseTotal
if $Labyrinth::Variables::cgiparams{$field} 0 0 0
202 0 $tvars{errcode} = 'MESSAGE';
203 0 $tvars{errmess} = "Missing parameter ($field)";
204 0 return 0;
205 }
206  
207 0 return 1;
208 }
209  
210 =head2 Process Flow Handling
211  
212 =over
213  
214 =item SetError
215  
216 Sets the error condition as given.
217  
218 =item SetCommand
219  
220 Set the next commmand to be run.
221  
222 =back
223  
224 =cut
225  
226 2 ▶sub SetError {
Subroutine SetError: called CC=2 SCAR=7.2
Pod documented
227 2 $tvars{errcode} = shift;
228 2 ▶ $tvars{errmess} = shift if(@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 0 2
229 }
230  
231 0 ▶sub SetCommand {
Subroutine SetCommand: not called CC=1 SCAR=6.9
Pod documented
232 0 $tvars{errcode} = 'NEXT';
233 0 $tvars{command} = shift;
234 }
235  
236 =head2 Default Variable Loaders
237  
238 =over
239  
240 =item LoadProfiles
241  
242 Loads the permissions profiles, as stored in profiles config file.
243  
244 =item LoadAccess
245  
246 Loads the access permissions, as stored in the database.
247  
248 =back
249  
250 =cut
251  
252 0 ▶sub LoadProfiles {
Subroutine LoadProfiles: not called CC=8 SCAR=42.8
Pod documented
253 0 ▶ return if(defined $settings{profiles});
Branch: 1 branch
BranchTrueFalseTotal
if defined $Labyrinth::Variables::settings{"profiles"} 0 0 0
254  
255 # ensure we can access the profile file
256 0 ▶ if(!$settings{profile} || !-f $settings{profile} || !-r $settings{profile}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Labyrinth::Variables::settings{"profile"} and -f $Labyrinth::Variables::settings{"profile"} and -r $Labyrinth::Variables::settings{"profile"}) 0 0 0
Condition0%0/6
$Labyrinth::Variables::settings{"profile"} and -f $Labyrinth::Variables::settings{"profile"} and -r $Labyrinth::Variables::settings{"profile"}
!ll&&!rl&&r
000
$Labyrinth::Variables::settings{"profile"} and -f $Labyrinth::Variables::settings{"profile"}
!ll&&!rl&&r
000
MC/DC0%0/3
$Labyrinth::Variables::settings{"profile"} and -f $Labyrinth::Variables::settings{"profile"} and -r $Labyrinth::Variables::settings{"profile"}
$Labyrinth::Variables::settings{"profile"} -f $Labyrinth::Variables::settings{"profile"} -r $Labyrinth::Variables::settings{"profile"}
Truth table0%0/4
A and B and C
A= $Labyrinth::Variables::settings{"profile"}B= -f $Labyrinth::Variables::settings{"profile"}C= -r $Labyrinth::Variables::settings{"profile"}
ABCresult
0XX0
10X0
1100
1111
257 0 LogError("Cannot read profile file [$settings{profile}]");
258 0 $tvars{errcode} = 'ERROR';
259 0 return;
260 }
261  
262 0 my $cfg = Config::IniFiles->new( -file => $settings{profile} );
263 0 ▶ unless(defined $cfg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cfg) 0 0 0
264 0 LogError("Unable to load profile file [$settings{profile}]: @Config::IniFiles::errors");
265 0 $tvars{errcode} = 'ERROR';
266 0 return;
267 }
268  
269 # load the configuration data
270 0 my $value = $cfg->val('MAIN','default');
271 0 my @value = $cfg->val('MAIN','profiles');
272  
273 0 $settings{profiles}{default} = $value;
274  
275 0 for my $profile (@value) {
276 0 for my $name ($cfg->Parameters($profile)) {
277 0 $value = $cfg->val($profile,$name);
278 0 $settings{profiles}{profiles}{$profile}{$name} = $value;
279 }
280 }
281 }
282  
283 0 ▶sub LoadAccess {
Subroutine LoadAccess: not called CC=3 SCAR=24.8
Pod documented
284 0 ▶ return if(defined $settings{access});
Branch: 1 branch
BranchTrueFalseTotal
if defined $Labyrinth::Variables::settings{"access"} 0 0 0
285  
286 0 my @rows = $dbi->GetQuery('hash','AllAccess',9);
287 0 for my $row (@rows) {
288 0 $settings{access}{names}{$row->{accessname}} = $row->{accessid};
289 0 $settings{access}{ids}{$row->{accessid}} = $row->{accessname};
290 }
291 }
292  
293 18 1;
294  
295 __END__
« Labyrinth/Writer/Parser/TT.pm ↑ Summary Labyrinth/DIUtils.pm »