line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package ZM::Session; |
2
|
|
|
|
|
|
|
$ZM::Session::VERSION = '0.2.1'; |
3
|
1
|
|
|
1
|
|
654
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2557
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
sub new |
6
|
|
|
|
|
|
|
{ |
7
|
0
|
|
|
0
|
1
|
|
my ($c, %args) = @_; |
8
|
0
|
|
0
|
|
|
|
my $class = ref($c) || $c; |
9
|
0
|
|
|
|
|
|
$args{SID} = $args{id}; |
10
|
0
|
|
|
|
|
|
bless \%args, $class; |
11
|
|
|
|
|
|
|
} |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub start |
14
|
|
|
|
|
|
|
{ |
15
|
0
|
|
|
0
|
1
|
|
my ($cl,$print_content,$no_cookie, $double_enter) = @_; |
16
|
0
|
0
|
|
|
|
|
if (!defined($cl->{lifetime})) |
17
|
|
|
|
|
|
|
{ |
18
|
0
|
|
|
|
|
|
$cl->{lifetime} = 600; |
19
|
|
|
|
|
|
|
} |
20
|
0
|
0
|
|
|
|
|
if (!defined($cl->{path})) |
21
|
|
|
|
|
|
|
{ |
22
|
0
|
|
|
|
|
|
$cl->{path} = "/tmp/"; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
# Set ID if not defined |
25
|
0
|
0
|
0
|
|
|
|
if ((!defined($cl->{SID})) || ((length($cl->{SID}) == 0))) |
26
|
|
|
|
|
|
|
{ |
27
|
0
|
|
|
|
|
|
$cl->id($cl->newID()); |
28
|
|
|
|
|
|
|
} |
29
|
0
|
0
|
|
|
|
|
if($no_cookie eq "") |
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
|
0
|
|
|
|
|
print "Content-type: text/html\n" if($print_content ne "nocontent"); |
37
|
0
|
|
|
|
|
|
print $cl->{head}; |
38
|
0
|
|
|
|
|
|
print "Set-Cookie: SID=".$cl->id."; expires=".$t."; path=/\n"; |
39
|
0
|
0
|
|
|
|
|
print "\n" if $double_enter eq ""; |
40
|
|
|
|
|
|
|
} |
41
|
0
|
0
|
|
|
|
|
if (-e $cl->getfile()) |
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
|
0
|
|
|
|
|
if (defined($cl->{check_ip})) |
50
|
|
|
|
|
|
|
{ |
51
|
0
|
|
|
|
|
|
$cl->set("SID_IP",$ENV{REMOTE_ADDR}); |
52
|
|
|
|
|
|
|
} |
53
|
0
|
|
|
|
|
|
return 1; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub check_sessions |
57
|
|
|
|
|
|
|
{ |
58
|
0
|
|
|
0
|
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
|
0
|
|
|
|
|
next if $f!~/^zm_sess_/; |
66
|
0
|
0
|
|
|
|
|
if (((stat($cl->{path}.$f))[9] + $cl->{lifetime}) < time()) |
67
|
|
|
|
|
|
|
{ |
68
|
0
|
|
|
|
|
|
unlink($cl->{path}.$f); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
0
|
|
|
|
|
|
closedir(SD); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub destroy |
75
|
|
|
|
|
|
|
{ |
76
|
0
|
|
|
0
|
1
|
|
my $cl = shift; |
77
|
0
|
0
|
|
|
|
|
if (!$cl->have_id()) |
78
|
|
|
|
|
|
|
{ |
79
|
0
|
|
|
|
|
|
return -1; |
80
|
|
|
|
|
|
|
} |
81
|
0
|
0
|
|
|
|
|
if (-e $cl->getfile()) |
82
|
|
|
|
|
|
|
{ |
83
|
0
|
|
|
|
|
|
unlink($cl->getfile()); |
84
|
|
|
|
|
|
|
} |
85
|
0
|
|
|
|
|
|
undef $cl->{SID}; |
86
|
0
|
0
|
|
|
|
|
if (defined($cl->{id})) |
87
|
|
|
|
|
|
|
{ |
88
|
0
|
|
|
|
|
|
undef $cl->{id}; |
89
|
|
|
|
|
|
|
} |
90
|
0
|
|
|
|
|
|
return 1; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub exists |
94
|
|
|
|
|
|
|
{ |
95
|
0
|
|
|
0
|
0
|
|
my ($cl,$id) = @_; |
96
|
0
|
0
|
|
|
|
|
if (!defined($id)) |
97
|
|
|
|
|
|
|
{ |
98
|
0
|
|
|
|
|
|
return 0; |
99
|
|
|
|
|
|
|
} |
100
|
0
|
|
|
|
|
|
my $file = $cl->{path}."zm_sess_".$cl->{$id}; |
101
|
0
|
0
|
|
|
|
|
if (-e $file) |
102
|
|
|
|
|
|
|
{ |
103
|
0
|
|
|
|
|
|
return 1; |
104
|
|
|
|
|
|
|
} |
105
|
0
|
|
|
|
|
|
return 0; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub have_id |
109
|
|
|
|
|
|
|
{ |
110
|
0
|
|
|
0
|
0
|
|
my $cl = shift; |
111
|
0
|
0
|
|
|
|
|
if (!defined($cl->{SID})) |
112
|
|
|
|
|
|
|
{ |
113
|
0
|
|
|
|
|
|
return 0; |
114
|
|
|
|
|
|
|
} |
115
|
0
|
|
|
|
|
|
return 1; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub set_path |
119
|
|
|
|
|
|
|
{ |
120
|
0
|
|
|
0
|
1
|
|
my ($cl, $path) = @_; |
121
|
0
|
0
|
|
|
|
|
if (defined($path)) { $cl->{path} = $path } |
|
0
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
return $cl->{path}; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub id |
126
|
|
|
|
|
|
|
{ |
127
|
0
|
|
|
0
|
1
|
|
my ($cl, $newid) = @_; |
128
|
0
|
0
|
|
|
|
|
if (defined($newid)) |
129
|
|
|
|
|
|
|
{ |
130
|
0
|
|
|
|
|
|
$cl->{SID} = $newid; |
131
|
|
|
|
|
|
|
} |
132
|
0
|
0
|
|
|
|
|
if (!$cl->have_id()) |
133
|
|
|
|
|
|
|
{ |
134
|
0
|
|
|
|
|
|
return -1; |
135
|
|
|
|
|
|
|
} |
136
|
0
|
|
|
|
|
|
return $cl->{SID}; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub getfile |
140
|
|
|
|
|
|
|
{ |
141
|
0
|
|
|
0
|
0
|
|
my $cl = shift; |
142
|
0
|
|
|
|
|
|
return $cl->{path}."zm_sess_".$cl->{SID}; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub is_set |
146
|
|
|
|
|
|
|
{ |
147
|
0
|
|
|
0
|
1
|
|
my ($cl,$name) = @_; |
148
|
0
|
0
|
|
|
|
|
if (!$cl->have_id()) |
149
|
|
|
|
|
|
|
{ |
150
|
0
|
|
|
|
|
|
return -1; |
151
|
|
|
|
|
|
|
} |
152
|
0
|
0
|
|
|
|
|
if (-e $cl->getfile()) |
153
|
|
|
|
|
|
|
{ |
154
|
0
|
|
|
|
|
|
open(SF,$cl->getfile); |
155
|
0
|
|
|
|
|
|
while (my $l = ) |
156
|
|
|
|
|
|
|
{ |
157
|
0
|
|
|
|
|
|
my @line = split (/=/,$l); |
158
|
0
|
0
|
|
|
|
|
if ($line[0] eq $name) { |
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
|
|
|
|
|
|
|
{ |
170
|
0
|
|
|
0
|
0
|
|
my ($cl) = @_; |
171
|
0
|
|
|
|
|
|
my %h; |
172
|
0
|
0
|
|
|
|
|
if (!$cl->have_id()) |
173
|
|
|
|
|
|
|
{ |
174
|
0
|
|
|
|
|
|
return -1; |
175
|
|
|
|
|
|
|
} |
176
|
0
|
0
|
|
|
|
|
if (-e $cl->getfile()) |
177
|
|
|
|
|
|
|
{ |
178
|
0
|
|
|
|
|
|
open(SF,$cl->getfile); |
179
|
0
|
|
|
|
|
|
while (my $l = ) |
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
|
|
|
|
|
|
|
{ |
192
|
0
|
|
|
0
|
1
|
|
my ($cl,$name) = @_; |
193
|
0
|
|
|
|
|
|
my $content = ""; |
194
|
0
|
0
|
|
|
|
|
if (!$cl->have_id()) |
195
|
|
|
|
|
|
|
{ |
196
|
0
|
|
|
|
|
|
return -1; |
197
|
|
|
|
|
|
|
} |
198
|
0
|
0
|
|
|
|
|
if (!$cl->is_set($name)) |
199
|
|
|
|
|
|
|
{ |
200
|
0
|
|
|
|
|
|
return 0; |
201
|
|
|
|
|
|
|
} |
202
|
0
|
|
|
|
|
|
open(SF,$cl->getfile()); |
203
|
0
|
|
|
|
|
|
while (my $l = ) |
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
|
|
|
|
|
|
|
{ |
216
|
0
|
|
|
0
|
1
|
|
my ($cl,$name) = @_; |
217
|
0
|
|
|
|
|
|
my $content = ""; |
218
|
0
|
0
|
|
|
|
|
if (!$cl->have_id()) |
219
|
|
|
|
|
|
|
{ |
220
|
0
|
|
|
|
|
|
return -1; |
221
|
|
|
|
|
|
|
} |
222
|
0
|
0
|
|
|
|
|
if (-e $cl->getfile()) |
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
|
|
|
|
|
|
|
{ |
235
|
0
|
|
|
0
|
1
|
|
my ($cl,$name) = @_; |
236
|
0
|
|
|
|
|
|
$name=~s/(\(|\))/\\$1/; |
237
|
0
|
0
|
|
|
|
|
if (!$cl->have_id()) |
238
|
|
|
|
|
|
|
{ |
239
|
0
|
|
|
|
|
|
return -1; |
240
|
|
|
|
|
|
|
} |
241
|
0
|
0
|
|
|
|
|
if (-e $cl->getfile()) |
242
|
|
|
|
|
|
|
{ |
243
|
0
|
|
|
|
|
|
open(SF,$cl->getfile()); |
244
|
0
|
|
|
|
|
|
while (my $l = ) |
245
|
|
|
|
|
|
|
{ |
246
|
0
|
0
|
|
|
|
|
if ($l =~ /^$name=(.*?)\n/i) |
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
|
|
|
|
|
|
|
{ |
263
|
0
|
|
|
0
|
1
|
|
my ($cl,$name,$value) = @_; |
264
|
0
|
0
|
|
|
|
|
if (!$cl->have_id()) { return -1; } |
|
0
|
|
|
|
|
|
|
265
|
0
|
0
|
|
|
|
|
if (-e $cl->getfile()) |
266
|
|
|
|
|
|
|
{ |
267
|
0
|
|
|
|
|
|
my $content = ""; |
268
|
0
|
|
|
|
|
|
my $flag=0; |
269
|
0
|
|
|
|
|
|
open(SF,$cl->getfile()); |
270
|
0
|
|
|
|
|
|
while (my $l = ) |
271
|
|
|
|
|
|
|
{ |
272
|
0
|
0
|
|
|
|
|
$flag=1 if($l =~ s/^$name=(.*?)\n/$name=$value\n/gis); |
273
|
0
|
|
|
|
|
|
$content .= $l; |
274
|
|
|
|
|
|
|
} |
275
|
0
|
|
|
|
|
|
close(SF); |
276
|
0
|
0
|
|
|
|
|
if(!$flag) |
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
|
|
|
|
|
|
|
{ |
291
|
0
|
|
|
0
|
0
|
|
my $cl = shift; |
292
|
|
|
|
|
|
|
#GET COOKIE |
293
|
0
|
|
|
|
|
|
my %COOKIES=$cl->parse_COOKIE(); |
294
|
0
|
0
|
|
|
|
|
if($COOKIES{SID} ne "") |
295
|
|
|
|
|
|
|
{ |
296
|
0
|
|
|
|
|
|
$cl->{SID}=$COOKIES{SID}; |
297
|
|
|
|
|
|
|
} |
298
|
|
|
|
|
|
|
# print "Content-type: text/html\n\n"; |
299
|
|
|
|
|
|
|
# print "COOKIE: ".$COOKIES{SID}." "; |
300
|
0
|
0
|
0
|
|
|
|
if(($COOKIES{SID} eq "") || !($cl->check_ip_addr)) |
301
|
|
|
|
|
|
|
{ |
302
|
|
|
|
|
|
|
do |
303
|
0
|
|
|
|
|
|
{ |
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
|
|
|
|
|
|
|
while($cl->exists($cl->{SID})); |
315
|
|
|
|
|
|
|
} |
316
|
0
|
|
|
|
|
|
return $cl->{SID}; |
317
|
|
|
|
|
|
|
} |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
sub check_ip_addr |
320
|
|
|
|
|
|
|
{ |
321
|
0
|
|
|
0
|
0
|
|
my $cl = shift; |
322
|
0
|
0
|
|
|
|
|
return 1 if(!defined($cl->{check_ip})); |
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
|
0
|
|
|
|
|
return 0 if($cl->get("SID_IP") ne $ENV{REMOTE_ADDR}); |
327
|
0
|
|
|
|
|
|
return 1 |
328
|
|
|
|
|
|
|
} |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
sub parse_COOKIE |
331
|
|
|
|
|
|
|
{ |
332
|
0
|
|
|
0
|
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; |
|
0
|
|
|
|
|
|
|
340
|
0
|
|
|
|
|
|
$value =~ tr/+/ /; |
341
|
0
|
|
|
|
|
|
$value =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack("C",hex($1))/eg; |
|
0
|
|
|
|
|
|
|
342
|
0
|
|
|
|
|
|
$value=~s/\r//g; |
343
|
0
|
|
|
|
|
|
$COOKIE{$key} = $value; |
344
|
|
|
|
|
|
|
} |
345
|
0
|
|
|
|
|
|
return(%COOKIE); |
346
|
|
|
|
|
|
|
} |
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
############################# |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
1; |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
__END__ |