blib/lib/ZM/Session.pm

filter: Statementstmt 2.7% 5 / 184 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 5.2% 1 / 19 Podpod 55.5% 10 / 18 totaltotal 4.9% 16 / 326 CC 52 SCAR 35.4
CC
52
Cov
2%
start
110.0
set
42.0
newID
42.0
CRAP 34.6
↑ Summary
1 package ZM::Session;
2 1 $ZM::Session::VERSION = '0.2.1';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 sub new
6 0 ▶{
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
7 0 my ($c, %args) = @_;
8 0 ▶ my $class = ref($c) || $c;
Condition0%0/3
ref $c || $c
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $c || $c
ref $c $c
Truth table0%0/3
A || B
A= ref $cB= $c
ABresult
1X1
011
000
9 0 $args{SID} = $args{id};
10 0 bless \%args, $class;
11 }
12  
13 sub start
14 0 ▶{
Subroutine start: not called CC=10 SCAR=47.0
Pod documented
15 0 my ($cl,$print_content,$no_cookie, $double_enter) = @_;
16 0 ▶ if (!defined($cl->{lifetime}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cl->{"lifetime"}) 0 0 0
17 {
18 0 $cl->{lifetime} = 600;
19 }
20 0 ▶ if (!defined($cl->{path}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cl->{"path"}) 0 0 0
21 {
22 0 $cl->{path} = "/tmp/";
23 }
24 # Set ID if not defined
25 0 ▶ if ((!defined($cl->{SID})) || ((length($cl->{SID}) == 0)))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cl->{"SID"} or length $cl->{"SID"} == 0) 0 0 0
Condition0%0/3
not defined $cl->{"SID"} or length $cl->{"SID"} == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $cl->{"SID"} or length $cl->{"SID"} == 0
not defined $cl->{"SID"} length $cl->{"SID"} == 0
Truth table0%0/3
not(A) or B
A= not defined $cl->{"SID"}B= length $cl->{"SID"} == 0
ABresult
1X1
011
000
26 {
27 0 $cl->id($cl->newID());
28 }
29 0 ▶ if($no_cookie eq "")
Branch: 1 branch
BranchTrueFalseTotal
if ($no_cookie eq "") 0 0 0
30 {
31 #SET COOKIE
32 0 my @week=("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
33 0 my @months=("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
34 0 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday)=gmtime(time()+$cl->{lifetime});
35 0 my $t=sprintf("%s, %02d-%s-%02d %02d:%02d:%02d GMT",$week[$wday],$mday,$months[$mon],$year % 100,$hour,$min,$sec);
36 0 ▶ print "Content-type: text/html\n" if($print_content ne "nocontent");
Branch: 1 branch
BranchTrueFalseTotal
if $print_content ne "nocontent" 0 0 0
37 0 print $cl->{head};
38 0 print "Set-Cookie: SID=".$cl->id."; expires=".$t."; path=/\n";
39 0 ▶ print "\n" if $double_enter eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $double_enter eq "" 0 0 0
40 }
41 0 ▶ if (-e $cl->getfile())
Branch: 1 branch
BranchTrueFalseTotal
if (-e $cl->getfile) 0 0 0
42 {
43 0 return 0;
44 }
45 0 open(SF,">".$cl->getfile());
46 0 close(SF);
47 0 $cl->check_sessions();
48 #SET IP
49 0 ▶ if (defined($cl->{check_ip}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cl->{"check_ip"}) 0 0 0
50 {
51 0 $cl->set("SID_IP",$ENV{REMOTE_ADDR});
52 }
53 0 return 1;
54 }
55  
56 sub check_sessions
57 0 ▶{
Subroutine check_sessions: not called CC=4 SCAR=30.0
Pod undocumented
58 0 my $cl = shift;
59 0 opendir(SD,$cl->{path});
60 0 my @files = readdir(SD);
61 # shift @files;
62 # shift @files;
63 0 foreach my $f (@files)
64 {
65 0 ▶ next if $f!~/^zm_sess_/;
Branch: 1 branch
BranchTrueFalseTotal
unless $f =~ /^zm_sess_/ 0 0 0
66 0 ▶ if (((stat($cl->{path}.$f))[9] + $cl->{lifetime}) < time())
Branch: 1 branch
BranchTrueFalseTotal
if ((stat $cl->{"path"} . $f)[9] + $cl->{"lifetime"} < time) 0 0 0
67 {
68 0 unlink($cl->{path}.$f);
69 }
70 }
71 0 closedir(SD);
72 }
73  
74 sub destroy
75 0 ▶{
Subroutine destroy: not called CC=4 SCAR=30.0
Pod documented
76 0 my $cl = shift;
77 0 ▶ if (!$cl->have_id())
Branch: 1 branch
BranchTrueFalseTotal
unless ($cl->have_id) 0 0 0
78 {
79 0 return -1;
80 }
81 0 ▶ if (-e $cl->getfile())
Branch: 1 branch
BranchTrueFalseTotal
if (-e $cl->getfile) 0 0 0
82 {
83 0 unlink($cl->getfile());
84 }
85 0 undef $cl->{SID};
86 0 ▶ if (defined($cl->{id}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cl->{"id"}) 0 0 0
87 {
88 0 undef $cl->{id};
89 }
90 0 return 1;
91 }
92  
93 sub exists
94 0 ▶{
Subroutine exists: not called CC=3 SCAR=24.8
Pod undocumented
95 0 my ($cl,$id) = @_;
96 0 ▶ if (!defined($id))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
97 {
98 0 return 0;
99 }
100 0 my $file = $cl->{path}."zm_sess_".$cl->{$id};
101 0 ▶ if (-e $file)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 0 0
102 {
103 0 return 1;
104 }
105 0 return 0;
106 }
107  
108 sub have_id
109 0 ▶{
Subroutine have_id: not called CC=2 SCAR=17.9
Pod undocumented
110 0 my $cl = shift;
111 0 ▶ if (!defined($cl->{SID}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cl->{"SID"}) 0 0 0
112 {
113 0 return 0;
114 }
115 0 return 1;
116 }
117  
118 sub set_path
119 0 ▶{
Subroutine set_path: not called CC=2 SCAR=17.9
Pod documented
120 0 my ($cl, $path) = @_;
121 0 ▶ if (defined($path)) { $cl->{path} = $path }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path) 0 0 0
122 0 return $cl->{path};
123 }
124  
125 sub id
126 0 ▶{
Subroutine id: not called CC=3 SCAR=24.8
Pod documented
127 0 my ($cl, $newid) = @_;
128 0 ▶ if (defined($newid))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newid) 0 0 0
129 {
130 0 $cl->{SID} = $newid;
131 }
132 0 ▶ if (!$cl->have_id())
Branch: 1 branch
BranchTrueFalseTotal
unless ($cl->have_id) 0 0 0
133 {
134 0 return -1;
135 }
136 0 return $cl->{SID};
137 }
138  
139 sub getfile
140 0 ▶{
Subroutine getfile: not called CC=1 SCAR=6.9
Pod undocumented
141 0 my $cl = shift;
142 0 return $cl->{path}."zm_sess_".$cl->{SID};
143 }
144  
145 sub is_set
146 0 ▶{
Subroutine is_set: not called CC=5 SCAR=34.0
Pod documented
147 0 my ($cl,$name) = @_;
148 0 ▶ if (!$cl->have_id())
Branch: 1 branch
BranchTrueFalseTotal
unless ($cl->have_id) 0 0 0
149 {
150 0 return -1;
151 }
152 0 ▶ if (-e $cl->getfile())
Branch: 1 branch
BranchTrueFalseTotal
if (-e $cl->getfile) 0 0 0
153 {
154 0 open(SF,$cl->getfile);
155 0 ▶ while (my $l = <SF>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline SF)) 0 0 0
156 {
157 0 my @line = split (/=/,$l);
158 0 ▶ if ($line[0] eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line[0] eq $name) 0 0 0
159 0 close(SF);
160 0 return 1;
161 }
162 }
163 0 close(SF);
164 }
165 0 return 0;
166 }
167  
168 sub list
169 0 ▶{
Subroutine list: not called CC=4 SCAR=30.0
Pod undocumented
170 0 my ($cl) = @_;
171 0 my %h;
172 0 ▶ if (!$cl->have_id())
Branch: 1 branch
BranchTrueFalseTotal
unless ($cl->have_id) 0 0 0
173 {
174 0 return -1;
175 }
176 0 ▶ if (-e $cl->getfile())
Branch: 1 branch
BranchTrueFalseTotal
if (-e $cl->getfile) 0 0 0
177 {
178 0 open(SF,$cl->getfile);
179 0 ▶ while (my $l = <SF>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline SF)) 0 0 0
180 {
181 0 my @line = split (/=/,$l);
182 0 $h{$line[0]}=$line[1];
183 }
184 0 close(SF);
185 0 return %h;
186 }
187 0 return 0;
188 }
189  
190 sub unset
191 0 ▶{
Subroutine unset: not called CC=4 SCAR=30.0
Pod documented
192 0 my ($cl,$name) = @_;
193 0 my $content = "";
194 0 ▶ if (!$cl->have_id())
Branch: 1 branch
BranchTrueFalseTotal
unless ($cl->have_id) 0 0 0
195 {
196 0 return -1;
197 }
198 0 ▶ if (!$cl->is_set($name))
Branch: 1 branch
BranchTrueFalseTotal
unless ($cl->is_set($name)) 0 0 0
199 {
200 0 return 0;
201 }
202 0 open(SF,$cl->getfile());
203 0 ▶ while (my $l = <SF>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline SF)) 0 0 0
204 {
205 0 $l =~ s/^$name=(.*?)\n//i;
206 0 $content .= $l;
207 }
208 0 close(SF);
209 0 open(SF,">".$cl->getfile());
210 0 print SF $content;
211 0 close(SF);
212 }
213  
214 sub unsetall
215 0 ▶{
Subroutine unsetall: not called CC=3 SCAR=24.8
Pod documented
216 0 my ($cl,$name) = @_;
217 0 my $content = "";
218 0 ▶ if (!$cl->have_id())
Branch: 1 branch
BranchTrueFalseTotal
unless ($cl->have_id) 0 0 0
219 {
220 0 return -1;
221 }
222 0 ▶ if (-e $cl->getfile())
Branch: 1 branch
BranchTrueFalseTotal
if (-e $cl->getfile) 0 0 0
223 {
224 0 return 0;
225 }
226 0 open(SF,">".$cl->getfile());
227 0 close(SF);
228 0 $cl->check_sessions();
229
230 0 return 1;
231 }
232  
233 sub get
234 0 ▶{
Subroutine get: not called CC=5 SCAR=34.0
Pod documented
235 0 my ($cl,$name) = @_;
236 0 $name=~s/(\(|\))/\\$1/;
237 0 ▶ if (!$cl->have_id())
Branch: 1 branch
BranchTrueFalseTotal
unless ($cl->have_id) 0 0 0
238 {
239 0 return -1;
240 }
241 0 ▶ if (-e $cl->getfile())
Branch: 1 branch
BranchTrueFalseTotal
if (-e $cl->getfile) { } 0 0 0
242 {
243 0 open(SF,$cl->getfile());
244 0 ▶ while (my $l = <SF>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline SF)) 0 0 0
245 {
246 0 ▶ if ($l =~ /^$name=(.*?)\n/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /^$name=(.*?)\n/i) 0 0 0
247 {
248 0 close(SF);
249 0 return $1;
250 }
251 }
252 0 close(SF);
253 }
254 else
255 {
256 0 return -1;
257 }
258 0 return "";
259 }
260  
261 sub set
262 0 ▶{
Subroutine set: not called CC=6 SCAR=37.4
Pod documented
263 0 my ($cl,$name,$value) = @_;
264 0 ▶ if (!$cl->have_id()) { return -1; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($cl->have_id) 0 0 0
265 0 ▶ if (-e $cl->getfile())
Branch: 1 branch
BranchTrueFalseTotal
if (-e $cl->getfile) 0 0 0
266 {
267 0 my $content = "";
268 0 my $flag=0;
269 0 open(SF,$cl->getfile());
270 0 ▶ while (my $l = <SF>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline SF)) 0 0 0
271 {
272 0 ▶ $flag=1 if($l =~ s/^$name=(.*?)\n/$name=$value\n/gis);
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ s/^$name=(.*?)\n/$name=$value\n/sig 0 0 0
273 0 $content .= $l;
274 }
275 0 close(SF);
276 0 ▶ if(!$flag)
Branch: 1 branch
BranchTrueFalseTotal
unless ($flag) 0 0 0
277 {
278 0 $content.="$name=$value\n";
279 }
280 0 open(SF,">".$cl->getfile());
281 0 print SF $content;
282 0 close(SF);
283 0 return 1;
284 }
285 0 return 0;
286 }
287  
288  
289 sub newID
290 0 ▶{
Subroutine newID: not called CC=6 SCAR=37.4
Pod undocumented
291 0 my $cl = shift;
292 #GET COOKIE
293 0 my %COOKIES=$cl->parse_COOKIE();
294 0 ▶ if($COOKIES{SID} ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($COOKIES{"SID"} ne "") 0 0 0
295 {
296 0 $cl->{SID}=$COOKIES{SID};
297 }
298 # print "Content-type: text/html\n\n";
299 # print "COOKIE: ".$COOKIES{SID}."<br>";
300 0 ▶ if(($COOKIES{SID} eq "") || !($cl->check_ip_addr))
Branch: 1 branch
BranchTrueFalseTotal
if ($COOKIES{"SID"} eq "" or not $cl->check_ip_addr) 0 0 0
Condition0%0/3
$COOKIES{"SID"} eq "" or not $cl->check_ip_addr
l!l&&r!l&&!r
000
MC/DC0%0/2
$COOKIES{"SID"} eq "" or not $cl->check_ip_addr
$COOKIES{"SID"} eq "" not $cl->check_ip_addr
Truth table0%0/3
A or not(B)
A= $COOKIES{"SID"} eq ""B= not $cl->check_ip_addr
ABresult
1X1
011
000
301 {
302 do
303 {
304 0 my $ary = "0123456789abcdefghijABCDEFGH"; # replace with the set of characters
305 0 $cl->{SID} = "";
306 0 my $arylen = length($ary);
307 0 for my $i (0 .. 23)
308 {
309 0 my $idx = int(rand(time) % $arylen);
310 0 my $dig = substr($ary, $idx, 1);
311 0 $cl->{SID} .= $dig;
312 }
313 }
314 0 ▶ while($cl->exists($cl->{SID}));
Branch: 1 branch
BranchTrueFalseTotal
$cl->exists($cl->{"SID"}) and do { do { my $ary = "0123456789abcdefghijABCDEFGH"; $cl->{"SID"} = ""; my $arylen = length $ary; foreach my $i (0 .. 23) { my $idx = int rand(time) % $arylen; my $dig = substr($ary, $idx, 1); $cl->{"SID"} .= $dig; } } } 0 0 0
315 }
316 0 return $cl->{SID};
317 }
318  
319 sub check_ip_addr
320 0 ▶{
Subroutine check_ip_addr: not called CC=3 SCAR=24.8
Pod undocumented
321 0 my $cl = shift;
322 0 ▶ return 1 if(!defined($cl->{check_ip}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cl->{"check_ip"} 0 0 0
323 # print "Content-type: text/html\n\n";
324 # print "SID_IP: ".$cl->get("SID_IP")." IP: ".$ENV{REMOTE_ADDR};
325 # print " GETFILE: ".$cl->getfile();
326 0 ▶ return 0 if($cl->get("SID_IP") ne $ENV{REMOTE_ADDR});
Branch: 1 branch
BranchTrueFalseTotal
if $cl->get("SID_IP") ne $ENV{"REMOTE_ADDR"} 0 0 0
327 0 return 1
328 }
329  
330 sub parse_COOKIE
331 0 ▶{
Subroutine parse_COOKIE: not called CC=2 SCAR=17.9
Pod undocumented
332 0 my @keypairs = split(/;/,$ENV{HTTP_COOKIE});
333 0 my %COOKIE;
334 0 foreach my $keyvalue (@keypairs)
335 {
336 0 $keyvalue=~s/^\s+//;
337 0 my ($key,$value) = split(/=/,$keyvalue);
338 0 $key =~ tr/+/ /;
339 0 $key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
340 0 $value =~ tr/+/ /;
341 0 $value =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack("C",hex($1))/eg;
342 0 $value=~s/\r//g;
343 0 $COOKIE{$key} = $value;
344 }
345 0 return(%COOKIE);
346 }
347  
348 #############################
349  
350 1 1;
351  
352 __END__
↑ Summary