blib/lib/CGI/Session/ExpireSessions.pm

filter: Statementstmt 70.3% 83 / 118 Branchbran 30.3% 20 / 66 Conditioncond 31.5% 6 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 86.6% 13 / 15 Podpod 60.0% 3 / 5 totaltotal 53.1% 125 / 235 CC 45 SCAR 37.2
CC
45
Cov
54%
expire_db_sessions
146.8
expire_file_sessions
30.8
_check_expiry
12.4
CRAP 41.4
↑ Summary
1 package CGI::Session::ExpireSessions;
2  
3 # Name:
4 # CGI::Session::ExpireSessions.
5 #
6 # Documentation:
7 # POD-style documentation is at the end. Extract it with pod2html.*.
8 #
9 # Reference:
10 # Object Oriented Perl
11 # Damian Conway
12 # Manning
13 # 1-884777-79-1
14 # P 114
15 #
16 # Note:
17 # o Tab = 4 spaces || die.
18 #
19 # Author:
20 # Ron Savage <ron@savage.net.au>
21 # Home page: http://savage.net.au/index.html
22 #
23 # Licence:
24 # Australian copyright (c) 2004 Ron Savage.
25 #
26 # All Programs of mine are 'OSI Certified Open Source Software';
27 # you can redistribute them and/or modify them under the terms of
28 # The Artistic License, a copy of which is available at:
29 # http://www.opensource.org/licenses/index.html
30  
31 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 4 require 5.005_62;
35  
36 4 require Exporter;
37  
38 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use CGI::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 4 our @ISA = qw(Exporter);
43  
44 # Items to export into callers namespace by default. Note: do not export
45 # names by default without a very good reason. Use EXPORT_OK instead.
46 # Do not simply export all your public functions/methods/constants.
47  
48 # This allows declaration use CGI::Session::ExpireSessions ':all';
49 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
50 # will save memory.
51 4 our %EXPORT_TAGS = ( 'all' => [ qw(
52  
53 ) ] );
54  
55 4 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
56  
57 4 our @EXPORT = qw(
58  
59 );
60 4 our $VERSION = '1.14';
61  
62 # -----------------------------------------------
63  
64 # Preloaded methods go here.
65  
66 # -----------------------------------------------
67  
68 # Encapsulated class data.
69  
70 {
71 4 my(%_attr_data) =
72 (
73 _cgi_session_dsn => undef,
74 _dbh => '',
75 _delta => 2 * 24 * 60 * 60, # Seconds.
76 _dsn_args => undef,
77 _table_name => 'sessions',
78 _temp_dir => '/tmp',
79 _time => time(),
80 _verbose => 0,
81 );
82  
83 sub _check_expiry
84 1 ▶ {
Subroutine _check_expiry: called CC=7 SCAR=25.1
85 1 my($self, $D) = @_;
86 1 my($expired) = 0;
87 1 my($time) = time();
88  
89 1 ▶ if ( ($time - $$D{'_SESSION_ATIME'}) >= $$self{'_delta'})
Branch: 1 branch
BranchTrueFalseTotal
if ($time - $D->{"_SESSION_ATIME"} >= $self->{"_delta"}) 1 0 1
90 {
91 1 $expired = 1;
92  
93 1 ▶ print STDOUT "Delta time: $$self{'_delta'}. Time elapsed: ", $time - $$D{'_SESSION_ATIME'}, ". Expired?: $expired. \n" if ($$self{'_verbose'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_verbose"} 1 0 1
94 }
95  
96 1 ▶ if ($$D{'_SESSION_ETIME'} && ! $expired)
Branch: 1 branch
BranchTrueFalseTotal
if ($D->{"_SESSION_ETIME"} and not $expired) 0 1 1
Condition33%1/3
$D->{"_SESSION_ETIME"} and not $expired
!ll&&!rl&&r
010
MC/DC0%0/2
$D->{"_SESSION_ETIME"} and not $expired
$D->{"_SESSION_ETIME"} not $expired
Truth table33%1/3
A and not(B)
A= $D->{"_SESSION_ETIME"}B= not $expired
ABresult
0X0
100
111
97 {
98 0 ▶ $expired = 1 if ($time >= ($$D{'_SESSION_ATIME'} + $$D{'_SESSION_ETIME'}) );
Branch: 1 branch
BranchTrueFalseTotal
if $time >= $D->{"_SESSION_ATIME"} + $D->{"_SESSION_ETIME"} 0 0 0
99  
100 0 ▶ print STDOUT "Last access time: $$D{'_SESSION_ATIME'}. Expiration time: $$D{'_SESSION_ETIME'}. Time elapsed: ", $time - $$D{'_SESSION_ATIME'}, ". Expired?: $expired. \n" if ($$self{'_verbose'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_verbose"} 0 0 0
101 }
102  
103 1 $expired;
104 }
105  
106 sub _default_for
107 10 ▶ {
Subroutine _default_for: called CC=1 SCAR=0.0
108 10 my($self, $attr_name) = @_;
109  
110 10 $_attr_data{$attr_name};
111 }
112  
113 # Warning: The args hashref passed in to sub _purge() has /no/ connexion
114 # with the $self hashref with belongs to the object instantiated by our client.
115 # The client code did something like this to create an object:
116 # my($expirer) = CGI::Session::ExpireSessions -> new(delta => 1);
117 # and we, the object, i.e. $expirer, are in fact the server.
118  
119 sub _purge
120 1 ▶ {
Subroutine _purge: called CC=5 SCAR=17.5
121 1 my($session, $args) = @_;
122  
123 1 ▶ return if ($session -> is_empty() );
Branch: 1 branch
BranchTrueFalseTotal
if $session->is_empty 0 1 1
124  
125 1 ▶ if ($session -> is_expired() || ($$args{'_time'} - $session -> atime() >= $$args{'_delta'}) )
Branch: 1 branch
BranchTrueFalseTotal
if ($session->is_expired or $args->{"_time"} - $session->atime >= $args->{"_delta"}) 1 0 1
Condition33%1/3
$session->is_expired or $args->{"_time"} - $session->atime >= $args->{"_delta"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$session->is_expired or $args->{"_time"} - $session->atime >= $args->{"_delta"}
$session->is_expired $args->{"_time"} - $session->atime >= $args->{"_delta"}
Truth table33%1/3
A or B
A= $session->is_expiredB= $args->{"_time"} - $session->atime >= $args->{"_delta"}
ABresult
1X1
011
000
126 {
127 1 ▶ print STDOUT "Expiring id @{[$session -> id()]}. \n" if ($$args{'_verbose'});
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"_verbose"} 1 0 1
128  
129 1 $session -> delete();
130 1 $session -> flush();
131 }
132 }
133  
134 sub _standard_keys
135 2 ▶ {
Subroutine _standard_keys: called CC=1 SCAR=0.0
136 2 keys %_attr_data;
137 }
138  
139 } # End of encapsulated class data.
140  
141 # -----------------------------------------------
142  
143 sub expire_db_sessions
144 4 ▶{
Subroutine expire_db_sessions: not called CC=12 SCAR=49.9
Pod documented
145 0 my($self, %arg) = @_;
146  
147 0 ▶ $self -> set(%arg) if (%arg);
Branch: 1 branch
BranchTrueFalseTotal
if %arg 0 0 0
148  
149 0 ▶ Carp::croak(__PACKAGE__ . ". You must specify a value for the parameter 'dbh'") if (! $$self{'_dbh'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_dbh"} 0 0 0
150  
151 0 my($sth) = $$self{'_dbh'} -> prepare("select * from $$self{'_table_name'}");
152  
153 0 $sth -> execute();
154  
155 0 my($serializer) = 'eval';
156  
157 0 ▶ if (defined $$self{_serializer})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_serializer"}) 0 0 0
158 {
159 0 $serializer = $$self{_serializer};
160 }
161  
162 0 my($data, $D, @id, $untainted_data);
163  
164 0 ▶ while ($data = $sth -> fetchrow_hashref() )
Branch: 1 branch
BranchTrueFalseTotal
if ($data = $sth->fetchrow_hashref) 0 0 0
165 {
166 # Untaint the data the brute force way.
167  
168 0 ($untainted_data) = $$data{'a_session'} =~ /(.*)/;
169  
170 0 ▶ if ($serializer eq 'eval')
Branch: 1 branch
BranchTrueFalseTotal
if ($serializer eq 'eval') { } 0 0 0
171 {
172 0 eval $untainted_data;
173  
174 0 ▶ push @id, $$data{id} if ($self -> _check_expiry($D) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_expiry($D) 0 0 0
175 }
176 else
177 {
178 0 my($serializer) = "CGI::Session::Serialize::$serializer";
179 0 my($thawed) = $serializer -> thaw($untainted_data);
180  
181 0 ▶ push @id, $$data{id} if ($self -> _check_expiry($thawed) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_expiry($thawed) 0 0 0
182 }
183 }
184  
185 0 for (@id)
186 {
187 0 ▶ print STDOUT "Expiring db id: $_. \n" if ($$self{'_verbose'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_verbose"} 0 0 0
188  
189 0 $sth = $$self{'_dbh'} -> prepare("delete from $$self{'_table_name'} where id = ?");
190  
191 0 $sth -> execute($_);
192  
193 0 $sth -> finish();
194 }
195  
196 0 ▶ if ( ($#id < 0) && $$self{'_verbose'})
Branch: 1 branch
BranchTrueFalseTotal
if ($#id < 0 and $self->{"_verbose"}) 0 0 0
Condition0%0/3
$#id < 0 and $self->{"_verbose"}
!ll&&!rl&&r
000
MC/DC0%0/2
$#id < 0 and $self->{"_verbose"}
$#id < 0 $self->{"_verbose"}
Truth table0%0/3
A and B
A= $#id < 0B= $self->{"_verbose"}
ABresult
0X0
100
111
197 {
198 0 print STDOUT "No db ids are due to expire. \n";
199 }
200  
201 } # End of expire_db_sessions.
202  
203 # -----------------------------------------------
204  
205 sub expire_file_sessions
206 1 ▶{
Subroutine expire_file_sessions: called CC=16 SCAR=34.3
Pod documented
207 1 my($self, %arg) = @_;
208  
209 1 ▶ $self -> set(%arg) if (%arg);
Branch: 1 branch
BranchTrueFalseTotal
if %arg 0 1 1
210  
211 1 ▶ Carp::croak(__PACKAGE__ . ". You must specify a value for the parameter 'temp_dir'") if (! $$self{'_temp_dir'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_temp_dir"} 0 1 1
212  
213 1 ▶ opendir(INX, $$self{'_temp_dir'}) || Carp::croak("Can't opendir($$self{'_temp_dir'}): $!");
Branch: 1 branch
BranchTrueFalseTotal
opendir INX, $self->{"_temp_dir"} or Carp::croak(qq[Can't opendir($self->{"_temp_dir"}): $!]) 1 0 1
214 1 my(@file) = map{File::Spec -> catfile($$self{'_temp_dir'}, $_)} grep{/cgisess_[0-9a-f]{32}/} readdir(INX);
215 1 closedir INX;
216  
217 1 my($count) = 0;
218 1 my($time) = time();
219  
220 1 my($file, @stat, $D);
221  
222 1 for my $file (@file)
223 {
224 1 @stat = stat($file);
225  
226 # Delete old, tiny files.
227  
228 1 ▶ if ( ( ($time - $stat[8]) >= $$self{'_delta'}) && ($stat[7] <= 5) )
Branch: 1 branch
BranchTrueFalseTotal
if ($time - $stat[8] >= $self->{"_delta"} and $stat[7] <= 5) 0 1 1
Condition33%1/3
$time - $stat[8] >= $self->{"_delta"} and $stat[7] <= 5
!ll&&!rl&&r
010
MC/DC0%0/2
$time - $stat[8] >= $self->{"_delta"} and $stat[7] <= 5
$time - $stat[8] >= $self->{"_delta"} $stat[7] <= 5
Truth table33%1/3
A and B
A= $time - $stat[8] >= $self->{"_delta"}B= $stat[7] <= 5
ABresult
0X0
100
111
229 {
230 0 $count++;
231  
232 0 ▶ print STDOUT "Delta time: $$self{'_delta'}. Size: $stat[7] bytes. Time elapsed: ", $time - $stat[8], ". Expired?: 1. \n" if ($$self{'_verbose'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_verbose"} 0 0 0
233  
234 0 unlink $file;
235  
236 0 next;
237 }
238  
239 # Ignore new, tiny files.
240  
241 1 ▶ next if ($stat[7] <= 5);
Branch: 1 branch
BranchTrueFalseTotal
if $stat[7] <= 5 0 1 1
242  
243 1 ▶ open(INX, $file) || Carp::croak("Can't open($file): $!");
Branch: 1 branch
BranchTrueFalseTotal
open INX, $file or Carp::croak("Can't open($file): $!") 1 0 1
244 1 binmode INX;
245 1 my(@session) = <INX>;
246 1 close INX;
247  
248 # Pod/perlfunc.html#item_eval
249 # This does not work:
250 # eval{no warnings 'all'; $session[0]};
251 # This was when I used to say 'eval $session[0];', but that fails
252 # when the session data contains \n characters. Hence the join.
253  
254 1 eval join('', @session);
255  
256 1 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
257 {
258 0 ▶ print STDOUT "Unable to parse contents of file: $file. \n" if ($$self{'_verbose'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_verbose"} 0 0 0
259  
260 0 next;
261 }
262  
263 1 ▶ if ($self -> _check_expiry($D) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_check_expiry($D)) 1 0 1
264 {
265 1 $count++;
266  
267 1 ▶ print STDOUT "Expiring file id: $$D{'_SESSION_ID'}. \n" if ($$self{'_verbose'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_verbose"} 1 0 1
268  
269 1 unlink $file;
270 }
271 }
272  
273 1 ▶ print STDOUT "No file ids are due to expire. \n" if ( ($count == 0) && $$self{'_verbose'});
Branch: 1 branch
BranchTrueFalseTotal
if $count == 0 and $self->{"_verbose"} 0 1 1
Condition33%1/3
$count == 0 and $self->{"_verbose"}
!ll&&!rl&&r
100
MC/DC0%0/2
$count == 0 and $self->{"_verbose"}
$count == 0 $self->{"_verbose"}
Truth table33%1/3
A and B
A= $count == 0B= $self->{"_verbose"}
ABresult
0X0
100
111
274  
275 } # End of expire_file_sessions.
276  
277 # -----------------------------------------------
278  
279 sub expire_sessions
280 1 ▶{
Subroutine expire_sessions: called CC=3 SCAR=11.8
Pod documented
281 1 my($self, %arg) = @_;
282  
283 1 ▶ return if (! CGI::Session -> can('find') );
Branch: 1 branch
BranchTrueFalseTotal
unless "CGI::Session"->can("find") 0 1 1
284  
285 # Return the result of find, which is:
286 # o Undef for failure
287 # o 1 for success
288  
289 1 ▶ $self -> set(%arg) if (%arg);
Branch: 1 branch
BranchTrueFalseTotal
if %arg 0 1 1
290  
291 return CGI::Session -> find
292 (
293 $$self{'_cgi_session_dsn'},
294 1 ▶ sub{_purge(@_,
Subroutine __ANON__: called CC=3 SCAR=12.7
295 { # This hashref is a parameter for _purge().
296 _delta => $$self{'_delta'} || 0, # These 2 defaults are in case the user sets them to undef!
297 _time => $$self{'_time'} || time(), # The defaults then stop Perl issuing warning messages about
298 1 ▶ _verbose => $$self{'_verbose'}, # uninitialized variables during the call to sub _purge().
Condition50%2/4
$self->{'_delta'} || 0
l!l
01
$self->{'_time'} || time
l!l
10
MC/DC0%0/1
$self->{'_delta'} || 0
$self->{'_delta'}
MC/DC0%0/1
$self->{'_time'} || time
$self->{'_time'}
Truth table50%1/2
A
A= $self->{'_delta'}
Aresult
11
00
Truth table50%1/2
A
A= $self->{'_time'}
Aresult
11
00
299 })},
300 1 $$self{'_dsn_args'}
301 );
302  
303 } # End of expire_sessions.
304  
305 # -----------------------------------------------
306  
307 sub new
308 2 ▶{
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
309 2 my($class, %arg) = @_;
310 2 my($self) = bless({}, $class);
311  
312 2 for my $attr_name ($self -> _standard_keys() )
313 {
314 16 my($arg_name) = $attr_name =~ /^_(.*)/;
315  
316 16 ▶ if (exists($arg{$arg_name}) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arg{$arg_name}) { } 6 10 16
317 {
318 6 $$self{$attr_name} = $arg{$arg_name};
319 }
320 else
321 {
322 10 $$self{$attr_name} = $self -> _default_for($attr_name);
323 }
324 }
325  
326 2 return $self;
327  
328 } # End of new.
329  
330 # -----------------------------------------------
331  
332 sub set
333 0 ▶{
Subroutine set: not called CC=3 SCAR=24.8
Pod undocumented
334 0 my($self, %arg) = @_;
335  
336 0 for my $arg (keys %arg)
337 {
338 0 ▶ $$self{"_$arg"} = $arg{$arg} if (exists($$self{"_$arg"}) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $$self{"_$arg"} 0 0 0
339 }
340  
341 } # End of set.
342  
343 # -----------------------------------------------
344  
345 4 1;
346  
347 __END__
↑ Summary