Labyrinth/Request.pm

filter: Statementstmt 28.2% 35 / 124 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 47.3% 9 / 19 Podpod 100% 6 / 6 totaltotal 24.7% 50 / 202 CC 27 SCAR 35.7
CC
27
Cov
20%
reset_request
90.0
_read_config
56.0
redirect
42.0
CRAP 35.7
« Labyrinth/Plugin/Base.pm ↑ Summary Labyrinth/Groups.pm »
1 package Labyrinth::Request;
2  
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use vars qw($VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 $VERSION = '5.32';
8  
9 =head1 NAME
10  
11 Labyrinth::Request - Request Manager for Labyrinth
12  
13 =head1 SYNOPSIS
14  
15 use Labyrinth::Request;
16  
17 # database object creation
18 my ($content,@actions) = Request($realm);
19  
20 =head1 DESCRIPTION
21  
22 The Request package, given a request string (or defaults), will retrieve
23 the appropriate actions, template file and continuations for that request.
24  
25 The configuration of request settings can either be held within INI files
26 within a designated path or in request table within a database.
27  
28 If using INI files, each file represents a collection of commands within a
29 single section. There is one special section, 'realm', which describes the
30 overall layout files, actions and continuations for the type of account.
31 Typically there are at least two realms, 'public' and 'admin'. To describe
32 the path to these request files, the following should exist within your global
33 settings file:
34  
35 requests=/path/to/request/files
36  
37 Alternative if you wish to use the request settings from a database table, in
38 your globale settings file, you will need the following setting:
39  
40 requests=dbi
41  
42 For more information for the database method, please see the
43 L<Labyrinth::Request> distribution.
44  
45 =cut
46  
47 # -------------------------------------
48 # Library Modules
49  
50 7 ▶use Config::IniFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 7 ▶use Labyrinth::Audit;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 7 ▶use Labyrinth::Globals;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 7 ▶use Labyrinth::Variables;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 7 ▶use Labyrinth::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 # -------------------------------------
58 # Variables
59  
60 7 my @configkeys = qw(layout actions content onsuccess onfailure onerror secure rewrite);
61 7 my %resetkeys = (onsuccess => 1, onfailure => 1, onerror => 1);
62 7 my %stored;
63  
64 7 my @autosubs = qw(
65 layout
66 content
67 onsuccess
68 onerror
69 onfailure
70 );
71 7 my %autosubs = map {$_ => 1} @autosubs;
72  
73 # -------------------------------------
74 # The Subs
75  
76 =head1 FUNCTIONS
77  
78 =head2 Constructor
79  
80 =over 4
81  
82 =item new()
83  
84 Create a new request object.
85  
86 =back
87  
88 =cut
89  
90 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
91 0 my $self = shift;
92 0 my $realm = shift;
93 0 ▶ my $request = shift || 'home-'.$realm;
Condition0%0/2
shift() || 'home-' . $realm
l!l
00
MC/DC0%0/1
shift() || 'home-' . $realm
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
94 0 my @actions;
95  
96 ## split the reset request into it's component parts
97 0 my ($section,$command) = split("-",$request);
98 0 $tvars{request} = $request;
99 0 $tvars{section} = $section;
100 0 $tvars{command} = $command;
101  
102 # sort the realm out
103 0 my ($layout,$actions,$content,$onsuccess,$onfailure,$onerror)
104 = $self->_read_config('realm',$realm,@configkeys);
105  
106 0 $onsuccess = $request;
107 0 ▶ @actions = split(",",$actions) if($actions);
Branch: 1 branch
BranchTrueFalseTotal
if $actions 0 0 0
108  
109 # create an attributes hash
110 0 my $atts = {
111 'actions' => \@actions,
112 'layout' => $layout,
113 'content' => $content,
114 'onsuccess' => $onsuccess,
115 'onfailure' => $onfailure,
116 'onerror' => $onerror,
117 };
118  
119 0 LogDebug("--new:actions=[@actions]");
120 # LogDebug("--new:layout=[$layout]");
121 # LogDebug("--new:content=[$content]");
122  
123 # create the object
124 0 bless $atts, $self;
125 0 return $atts;
126 }
127  
128 =head2 Methods
129  
130 =head3 Handling Actions
131  
132 =over 4
133  
134 =item next_action()
135  
136 For the current command request, return the next action within its action list.
137  
138 =item add_actions(@actions)
139  
140 Add actions to the action list for the current command request.
141  
142 =back
143  
144 =cut
145  
146 0 ▶sub next_action { my $self = shift; shift @{$self->{actions}} }
Subroutine next_action: not called CC=1 SCAR=6.9
Pod documented
147 0 ▶sub add_actions { my $self = shift; push @{$self->{actions}}, @_ }
Subroutine add_actions: not called CC=1 SCAR=6.9
Pod documented
148  
149 =head3 Handling Command Resets
150  
151 =over 4
152  
153 =item reset_realm($realm)
154  
155 Reloads settings for a new realm setting.
156  
157 =item reset_request($request)
158  
159 Reloads settings for a new command request.
160  
161 =item redirect
162  
163 Instead of a local template file or a continuation, a redirect may be used.
164 This method reformats the URL within a redirect request.
165  
166 =back
167  
168 =cut
169  
170 0 ▶sub reset_realm {
Subroutine reset_realm: not called CC=4 SCAR=30.0
Pod documented
171 0 my $self = shift;
172 0 my $realm = shift;
173 0 my %hash;
174  
175 0 @hash{@configkeys} = $self->_read_config('realm',$realm,@configkeys);
176  
177 0 $self->{section} = 'realm';
178 0 $self->{command} = $realm; # needed to check onsuccess, etc.
179  
180 0 for(@configkeys) {
181 0 ▶ next unless($hash{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless $hash{$_} 0 0 0
182 0 ▶ if($_ eq 'actions') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq 'actions') { } 0 0 0
183 0 my @actions = split(",",$hash{$_});
184 0 $self->add_actions(@actions);
185 0 LogDebug("--reset_realm:actions=@actions");
186 } else {
187 0 $self->{$_} = $hash{$_};
188 #LogDebug("--reset_realm:$_=$self->{$_}");
189 }
190 }
191 }
192  
193 0 ▶sub reset_request {
Subroutine reset_request: not called CC=9 SCAR=45.0
Pod documented
194 0 my $self = shift;
195 0 my $request = shift;
196 0 my %hash;
197  
198 ## split the reset request into it's component parts
199 0 my ($section,$command) = split("-",$request);
200 0 $tvars{request} = $request;
201 0 $tvars{section} = $section;
202 0 $tvars{command} = $command;
203 0 ▶ return unless($section && $command);
Branch: 1 branch
BranchTrueFalseTotal
unless $section and $command 0 0 0
Condition0%0/3
$section and $command
!ll&&!rl&&r
000
MC/DC0%0/2
$section and $command
$section $command
Truth table0%0/3
A and B
A= $sectionB= $command
ABresult
0X0
100
111
204  
205 # remove any remaining actiona
206 0 ▶ while($self->next_action){};
Branch: 1 branch
BranchTrueFalseTotal
if ($self->next_action) 0 0 0
207  
208 0 @hash{@configkeys} = $self->_read_config($section,$command,@configkeys);
209  
210 #if($settings{$protocol} eq 'https' && !$hash{secure} || $hash{secure} !~ /^(on|either|both)$/) {
211 # # redirect to HTTP string
212 # $self->redirect('http',$hash{rewrite},$request);
213 # return;
214 #} elsif($settings{$protocol} eq 'http' && $hash{secure} && $hash{secure} =~ /^(on|either|both)$/) {
215 # # redirect to HTTPS string
216 # $self->redirect('https',$hash{rewrite});
217 # return;
218 #}
219  
220 0 for(@configkeys) {
221 0 ▶ next unless($hash{$_} || $resetkeys{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless $hash{$_} or $resetkeys{$_} 0 0 0
Condition0%0/3
$hash{$_} or $resetkeys{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
$hash{$_} or $resetkeys{$_}
$hash{$_} $resetkeys{$_}
Truth table0%0/3
A or B
A= $hash{$_}B= $resetkeys{$_}
ABresult
1X1
011
000
222 0 ▶ if($_ eq 'actions') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq 'actions') { } 0 0 0
223 0 my @actions = split(",",$hash{$_});
224 0 $self->add_actions(@actions);
225 0 LogDebug("--reset_request:actions=@actions");
226 } else {
227 0 $self->{$_} = $hash{$_};
228 0 ▶LogDebug("--reset_request:$_=" . (defined $self->{$_} ? $self->{$_} : ''));
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$_} ? : 0 0 0
229 }
230 }
231 }
232  
233 0 ▶sub redirect {
Subroutine redirect: not called CC=6 SCAR=37.4
Pod documented
234 0 my ($self,$protocol,$rewrite,$request) = @_;
235  
236 0 ▶ return $tvars{redirect} if(!$protocol);
Branch: 1 branch
BranchTrueFalseTotal
unless $protocol 0 0 0
237  
238 # set to existing query string, with new protocol
239 0 $tvars{redirect} = "$protocol://$ENV{HTTP_HOST}";
240  
241 0 ▶ if(defined $rewrite) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rewrite) { } 0 0 0
242 0 $tvars{redirect} .= $rewrite;
243 } else {
244 0 ▶ $tvars{redirect} .= $ENV{REQUEST_URI} if($ENV{REQUEST_URI});
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"REQUEST_URI"} 0 0 0
245  
246 # rewrite query string
247 0 ▶ if(defined $request) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $request) 0 0 0
248 0 $tvars{redirect} =~ s/\?.*//;
249 0 ▶ $tvars{redirect} .= "?act=$request" if($request);
Branch: 1 branch
BranchTrueFalseTotal
if $request 0 0 0
250 }
251 }
252 }
253  
254 # private method to read config data
255  
256 0 ▶sub _read_config {
Subroutine _read_config: not called CC=7 SCAR=40.3
257 0 my ($self,$section,$command,@keys) = @_;
258 0 my @values;
259  
260 0 LogDebug("--read_config:section=$section,command=$command,request=$settings{requests}");
261  
262 0 ▶ if($settings{requests} eq 'dbi') {
Branch: 1 branch
BranchTrueFalseTotal
if ($Labyrinth::Request::settings{'requests'} eq 'dbi') { } 0 0 0
263 0 my @rows = $dbi->GetQuery('hash','GetRequest',$section,$command);
264 0 ▶ if(@rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rows) { } 0 0 0
265 0 push @values, map {$rows[0]->{$_}} @keys;
266 } else {
267 0 push @values, map {''} @keys;
268 }
269 } else {
270 0 my $file = "$settings{requests}/$section.ini";
271 0 ▶ Croak("Cannot read configuration file [$file]\n") unless(-r $file);
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 0 0
272 0 my $cfg = Config::IniFiles->new( -file => $file );
273 0 ▶ Croak("Cannot access configuration file [$file]: @Config::IniFiles::errors\n") unless($cfg);
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg 0 0 0
274  
275 0 for my $key (@keys) {
276 0 my $value = $cfg->val( $command, $key );
277 #LogDebug("--_read_config:[$command-$key]=[$value], file=[$file]");
278 0 ▶ push @values, ($value ? $value : undef);
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
279 }
280 }
281 0 return @values;
282 }
283  
284 =head2 Accessor Methods
285  
286 =over 4
287  
288 =item layout
289  
290 Layout template to be used
291  
292 =item content
293  
294 Content template to be used
295  
296 =item onsuccess
297  
298 Command to execute if this command succeeds.
299  
300 =item onerror
301  
302 Command to execute if this command fails.
303  
304 =item onfailure
305  
306 Command to execute if this command fails with an unrecoverable error.
307  
308 =back
309  
310 =cut
311  
312 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=14.4
313 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
314 0 my $name = $AUTOLOAD;
315 0 $name =~ s/^.*:://;
316 0 ▶ die "Unknown sub $AUTOLOAD\n" unless($autosubs{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless $autosubs{$name} 0 0 0
317  
318 0 ▶ *$name = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
319 0 my $self = shift;
320 0 my $value = $self->{$name};
321 0 ▶ if($name =~ /^on/) { $self->{$name} = undef } # once seen, forget it
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^on/) 0 0 0
322 0 return $value;
323 0 };
324 0 goto &$name;
325 }
326  
327 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
328  
329 7 1;
330  
331 __END__
« Labyrinth/Plugin/Base.pm ↑ Summary Labyrinth/Groups.pm »