line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CGI::Lazy::Session; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1741
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
52
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
6
|
use JSON; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
9
|
|
6
|
1
|
|
|
1
|
|
815
|
use CGI::Lazy::ID; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
7
|
1
|
|
|
1
|
|
601
|
use CGI::Lazy::CookieMonster; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
29
|
|
8
|
1
|
|
|
1
|
|
559
|
use CGI::Lazy::Session::Data; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
29
|
|
9
|
1
|
|
|
1
|
|
7
|
use CGI::Lazy::Globals; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
1283
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------- |
12
|
|
|
|
|
|
|
sub cookiemonster { |
13
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
14
|
|
|
|
|
|
|
|
15
|
0
|
|
|
|
|
|
return $self->{_cookiemonster}; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------- |
19
|
|
|
|
|
|
|
sub config { |
20
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
21
|
|
|
|
|
|
|
|
22
|
0
|
|
|
|
|
|
my $q = $self->q; |
23
|
0
|
|
|
|
|
|
return $q->config; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
#---------------------------------------------------------------------------------------------- |
27
|
|
|
|
|
|
|
sub data { |
28
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
29
|
0
|
|
|
|
|
|
return $self->{_data}; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------- |
33
|
|
|
|
|
|
|
sub db { |
34
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
return $self->q->db; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------- |
41
|
|
|
|
|
|
|
sub expired { |
42
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
my $table = $self->sessionTable; |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
my $now = time(); |
47
|
0
|
|
|
|
|
|
my $expiry = $self->data->expires; |
48
|
|
|
|
|
|
|
|
49
|
0
|
0
|
|
|
|
|
if ($self->data->terminated) { |
|
|
0
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
return 1; |
51
|
|
|
|
|
|
|
} elsif ($expiry > $now) { |
52
|
0
|
|
|
|
|
|
return; |
53
|
|
|
|
|
|
|
} else { |
54
|
0
|
|
|
|
|
|
return 1; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
#---------------------------------------------------------------------------------------------- |
59
|
|
|
|
|
|
|
sub expires { |
60
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
61
|
0
|
|
|
|
|
|
return $self->{_expires}; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------------- |
65
|
|
|
|
|
|
|
sub getData { |
66
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
my $sessionTable = $self->config->plugins->{session}->{sessionTable}; |
69
|
0
|
|
|
|
|
|
my $results = $self->db->getarray("select data from $sessionTable where sessionID = ?", $self->sessionID); |
70
|
0
|
|
|
|
|
|
my $data = $results->[0]->[0]; |
71
|
|
|
|
|
|
|
|
72
|
0
|
0
|
|
|
|
|
if ($data) { |
73
|
0
|
|
|
|
|
|
return from_json($data); |
74
|
|
|
|
|
|
|
} else { |
75
|
0
|
|
|
|
|
|
return; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------- |
80
|
|
|
|
|
|
|
sub id { |
81
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
return $self->{_id}; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------------- |
87
|
|
|
|
|
|
|
sub new { |
88
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
89
|
0
|
|
|
|
|
|
my $sessionID = shift; |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
my $sessionTable = $self->sessionTable; |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
my $query = "insert into $sessionTable (sessionID, data) values (?, ?)"; |
94
|
0
|
|
|
|
|
|
my $now = time(); |
95
|
0
|
|
|
|
|
|
my $expires = $self->parseExpiry($now); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
#set creation time, expiry time, last accessed time |
98
|
0
|
|
|
|
|
|
my $var = { |
99
|
|
|
|
|
|
|
created => $now, |
100
|
|
|
|
|
|
|
updated => $now, |
101
|
|
|
|
|
|
|
expires => $expires, |
102
|
|
|
|
|
|
|
}; |
103
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
my $data = to_json($var); |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
$self->db->do($query, $sessionID, $data); |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
return $sessionID; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------- |
112
|
|
|
|
|
|
|
sub open { |
113
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
114
|
0
|
|
|
|
|
|
my $q = shift; |
115
|
0
|
|
|
|
|
|
my $sessionID = shift; |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
my $self = {}; |
118
|
0
|
|
|
|
|
|
$self->{_q} = $q; |
119
|
0
|
|
|
|
|
|
$self->{_sessionTable} = $q->plugin->session->{sessionTable}; |
120
|
0
|
|
|
|
|
|
$self->{_sessionCookie} = $q->plugin->session->{sessionCookie}; |
121
|
0
|
|
|
|
|
|
$self->{_expires} = $q->plugin->session->{expires}; |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
bless $self, $class; |
124
|
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
|
$self->{_cookiemonster} = CGI::Lazy::CookieMonster->new($q); |
126
|
0
|
|
|
|
|
|
$self->{_id} = CGI::Lazy::ID->new($self); |
127
|
|
|
|
|
|
|
|
128
|
0
|
0
|
|
|
|
|
$sessionID = $self->cookiemonster->getCookie($self->sessionCookie) unless $sessionID; |
129
|
|
|
|
|
|
|
|
130
|
0
|
0
|
|
|
|
|
if ($sessionID) { #check sessionID against db, compare expiry time to last accessed time, reopen if valid |
131
|
|
|
|
|
|
|
#$q->util->debug->edump("have sessionID"); |
132
|
0
|
|
|
|
|
|
$self->{_sessionID} = $sessionID; |
133
|
0
|
|
|
|
|
|
$self->{_data} = CGI::Lazy::Session::Data->new($self); |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
|
my $now = time(); |
136
|
0
|
|
|
|
|
|
my $expiry = $self->data->expires; |
137
|
|
|
|
|
|
|
|
138
|
0
|
0
|
|
|
|
|
$sessionID = $self->new($self->id->generate) if $self->expired; |
139
|
|
|
|
|
|
|
|
140
|
0
|
0
|
|
|
|
|
if ($expiry > $now) { #valid sesion, update expiration |
141
|
|
|
|
|
|
|
#$q->util->debug->edump("not expired", "now: ".$now, "expires: ".$expiry, "difference: ".($expiry - $now)); |
142
|
0
|
|
|
|
|
|
$self->data->expires($self->parseExpiry($now)); #reset expiry time |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
} else { |
145
|
|
|
|
|
|
|
#$q->util->debug->edump("expired"); |
146
|
0
|
|
|
|
|
|
$self->terminate(); |
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
$sessionID = $self->new($self->id->generate); #session expired. create a new one |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
} else { # create new session |
152
|
0
|
|
|
|
|
|
$sessionID = $self->new($self->id->generate); #if we don't have a valid sessionID, we'll generate one |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
0
|
0
|
|
|
|
|
$q->errorHandler->badSession($sessionID) unless $self->id->valid($sessionID); #error out if we still don't have something we can work with. |
156
|
|
|
|
|
|
|
|
157
|
0
|
|
|
|
|
|
$self->{_sessionID} = $sessionID; |
158
|
0
|
|
|
|
|
|
$self->{_data} = CGI::Lazy::Session::Data->new($self); |
159
|
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
|
return $self; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------- |
165
|
|
|
|
|
|
|
sub parseExpiry { |
166
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
167
|
0
|
|
|
|
|
|
my $time = shift; |
168
|
|
|
|
|
|
|
|
169
|
0
|
|
|
|
|
|
my $expirestring = $self->expires; |
170
|
|
|
|
|
|
|
|
171
|
0
|
|
|
|
|
|
$expirestring =~ /([+-])(\d+)(\w)/; |
172
|
0
|
|
|
|
|
|
my ($sign, $num, $unit) = ($1, $2, $3); |
173
|
|
|
|
|
|
|
|
174
|
0
|
0
|
0
|
|
|
|
unless ($sign && $num && $unit) { |
|
|
|
0
|
|
|
|
|
175
|
0
|
|
|
|
|
|
$self->q->errorHandler->badSessionExpiry; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
my $minute = 60; #seconds in a minute |
179
|
0
|
|
|
|
|
|
my $hour = 3600; #seconds in an hour |
180
|
0
|
|
|
|
|
|
my $day = 43200; #seconds in a day |
181
|
0
|
|
|
|
|
|
my $factor; |
182
|
|
|
|
|
|
|
my $expiry; |
183
|
|
|
|
|
|
|
|
184
|
0
|
0
|
|
|
|
|
if ($unit eq 'm') { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
185
|
0
|
|
|
|
|
|
$factor = $minute * $num; |
186
|
|
|
|
|
|
|
} elsif ($unit eq 'h') { |
187
|
0
|
|
|
|
|
|
$factor = $hour * $num; |
188
|
|
|
|
|
|
|
} elsif ($unit eq 'd') { |
189
|
0
|
|
|
|
|
|
$factor = $day * $num; |
190
|
|
|
|
|
|
|
} else { #we'll assume it's seconds then (why would someone do this?) |
191
|
0
|
|
|
|
|
|
$factor = $num; |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
0
|
0
|
|
|
|
|
if ($sign eq '+') { |
|
|
0
|
|
|
|
|
|
195
|
0
|
|
|
|
|
|
$expiry = $time + $factor; |
196
|
|
|
|
|
|
|
} elsif ($sign eq '-') { |
197
|
0
|
|
|
|
|
|
$expiry = $time - $factor; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
0
|
|
|
|
|
|
return $expiry; |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------- |
204
|
|
|
|
|
|
|
sub q { |
205
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
206
|
|
|
|
|
|
|
|
207
|
0
|
|
|
|
|
|
return $self->{_q}; |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------- |
211
|
|
|
|
|
|
|
sub sessionCookie { |
212
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
213
|
|
|
|
|
|
|
|
214
|
0
|
|
|
|
|
|
return $self->{_sessionCookie}; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------- |
218
|
|
|
|
|
|
|
sub sessionID { |
219
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
220
|
|
|
|
|
|
|
|
221
|
0
|
|
|
|
|
|
return $self->{_sessionID}; |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------- |
225
|
|
|
|
|
|
|
sub sessionTable { |
226
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
227
|
|
|
|
|
|
|
|
228
|
0
|
|
|
|
|
|
return $self->{_sessionTable}; |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------- |
232
|
|
|
|
|
|
|
sub save { |
233
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
234
|
|
|
|
|
|
|
|
235
|
0
|
|
|
|
|
|
my $sessionID = $self->sessionID; |
236
|
0
|
|
|
|
|
|
my $datastring = to_json($self->data->{_data}); |
237
|
0
|
|
|
|
|
|
my $sessionTable = $self->q->plugin->session->{sessionTable}; |
238
|
|
|
|
|
|
|
|
239
|
0
|
|
|
|
|
|
$self->db->do("update $sessionTable set data = ? where sessionID = ?", $datastring, $sessionID); |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------- |
243
|
|
|
|
|
|
|
sub terminate { |
244
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
# my $table = $self->sessionTable; |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
# $self->db->do("update $table set expired = 1 where sessionID = ?", $self->sessionID); |
249
|
|
|
|
|
|
|
|
250
|
0
|
|
|
|
|
|
$self->data->terminated(1); |
251
|
0
|
|
|
|
|
|
return; |
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
1 |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
__END__ |