This repository has been archived by the owner on Jul 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSession.pm
602 lines (420 loc) · 14.3 KB
/
Session.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
package Session;
=head1 NAME
Session (ses)
=head1 DESCRIPTION
This is a Wrapper class for Apache::Session. Manages the session id
via cookies.
=head1 VERSION
$Id: Session.pm,v 1.15 2010/04/28 18:11:33 pfarber Exp $
=head1 SYNOPSIS
my $ses = Session::start_session($C);
my $sid = $ses->get_session_id();
$ses->set_persistent($key, $value);
=head1 METHODS
=over 8
=cut
BEGIN
{
if ($ENV{'HT_DEV'})
{
require "strict.pm";
strict::import();
}
}
use CGI;
use CGI::Cookie;
use Apache::Session::MySQL::InnoDB;
my $g_session_store = 'Apache::Session::MySQL::InnoDB';
use Debug::DUtils;
use Utils;
use Database;
# ---------------------------------------------------------------------
=item start_session
Class method which acts like a mini-factory to create a Session object.
Cookie Logic: Update cookie obj on session object with a possibly
different session id returned from the "new Session" call. Get session
id from cookie or create new cookie and store it on the session.
If you want to run using a new session instead of the session whose id
is stored in your cookie set newsid=1 on the URL,
No expiration time is set on the cookie making it a session cookie
which expires when the browser is closed. Hence the session cleaner
run interval the life of the session since the browser could remain
open and inactive for longer than the database session.
bin/managesessions.pl runs once every hour and deletes sessions that have
been inactive for 2 hours or more so an inactive session will expire
in a maximum of 2:59:59.
=cut
# ---------------------------------------------------------------------
sub start_session
{
my $C = shift;
my $commit = shift;
my $cgi = $C->get_object('CGI');
my $cookie_name = $C->get_object('MdpConfig')->get('cookie_name');
my ($sid, $previous_sid);
if ($previous_sid = $sid = $cgi->param('newsid'))
{
$previous_sid = $sid = undef
if ($sid eq "1");
# Otherwise use the value of 'newsid' from the URL and recover
# that session if it is still available in the database
}
else
{
my %cookies = fetch CGI::Cookie;
if (defined($cookies{$cookie_name}))
{
$previous_sid = $sid = $cookies{$cookie_name}->value;
}
}
my $ses;
eval
{
$ses = new Session($C, $sid, 0, $commit);
};
ASSERT(!$@, qq{Error creating session: $@});
$sid = $ses->get_session_id();
# No expiration time set on the cookie makes it a browser session
# cookie that expires when the browser is closed. Therefore the
# session lifetime is the min of browser session lifetime and
# database record lifetime. managemdpsessions.pl runs once every
# hour and deletes sessions that have been inactive for 2 hours or
# more so an inactive session will expire in a maximum of 2:59:59.
my $session_cookie =
new CGI::Cookie(
-name => $cookie_name,
-value => $sid,
-path => '/',
-domain => Utils::get_cookie_domain($cgi),
-httponly => 1,
);
$ses->set_cookie($session_cookie);
#
# Establish the debugging environment. This updates the global
# session variable in Utils::DUtils to retrieve the save debug
# messages. No trans-session debugging calls will work before this
# call.
Debug::DUtils::setup_debug_environment($ses);
#
# Call debugging_enabled() to emit auth debug text after
# environment is set up
Debug::DUtils::debugging_enabled();
DEBUG('session,all',
sub
{
my $t = ($previous_sid eq $sid) ? q{EXISTING session } : q{NEW session };
return $t . qq{sid=$sid};
});
$C->get_object('MdpConfig')->__config_debug();
return $ses;
}
# ---------------------------------------------------------------------
=item new
Create new Session object. If $empty is true, return only an empty,
but blessed Session object for use in managemdpsessions.pl
=cut
# ---------------------------------------------------------------------
sub new
{
my $class = shift;
my $self = {};
bless $self, $class;
$self->_initialize(@_);
return $self;
}
# ---------------------------------------------------------------------
=item _initialize
If there was no incoming session id, pass Apache::Session "undef" as
session id; it will create a new id
Sometimes a 'valid' sid (after a crash or in the debugger) corresponds
to a zero-length session file (never populated due to the crash or
interruption?). In these cases the sid is worthless so we need to
generate a new one.
=cut
# ---------------------------------------------------------------------
sub _initialize
{
my ($self, $C, $sid, $empty, $commit) = @_;
if ($empty)
{
$self->{'persistent_data'} = {};
$self->{'transient_data'} = {};
$self->{'empty'} = 1;
}
else
{
my %session_hash = ();
if (! $sid ||
$sid !~ m,[0-9a-zA-Z]{16},)
{
_make_persistent($C, undef, \%session_hash, $commit);
}
else
{
# putatively valid sid provided
_make_persistent($C, $sid, \%session_hash, $commit);
}
if (! $session_hash{_session_id})
{
_make_persistent($C, undef, \%session_hash, $commit);
}
# Set the session id for future use
$sid = $session_hash{_session_id};
# save the Apache::Session::DBI id and tied hash in this
# Session wrapper object
$self->{'id'} = $sid;
$self->{'persistent_data'} = \%session_hash;
}
}
# ---------------------------------------------------------------------
=item _make_persistent
Serialize and store the session hash in the database.
=cut
# ---------------------------------------------------------------------
sub _make_persistent
{
my ($C, $sid, $session_hash_ref, $commit) = @_;
unless ( defined $commit ) { $commit = 1; }
my $config = $C->get_object('MdpConfig');
my $db = $C->get_object('Database');
my $dbh = $db->get_DBH($C);
ASSERT($dbh->{Active}, qq{database handle not valid for session creation});
my $session_store_attrsref;
if ( $g_session_store eq 'Apache::Session::MySQL' ) {
$session_store_attrsref = {
Handle => $dbh,
LockHandle => $dbh,
};
} else {
$Apache::Session::Store::DBI::TableName = 'ht_sessions';
$session_store_attrsref = {
Handle => $dbh,
Commit => $commit # for InnoDB transactions
};
}
eval
{
tie %$session_hash_ref, $g_session_store, $sid, $session_store_attrsref;
};
if ($@)
{
# sid provided is not in the session database. re-submit
# with undef to get a new session
if ($@ =~ m,Object does not exist in the data store,)
{
tie %$session_hash_ref, $g_session_store, undef, $session_store_attrsref;
}
}
elsif (! $$session_hash_ref{'_session_id'})
{
die(qq{session hash returned empty from tie. BLOB size problem?});
}
}
# ---------------------------------------------------------------------
=item get_session_id
Return the session's id.
=cut
# ---------------------------------------------------------------------
sub get_session_id
{
my $self = shift;
return $self->{'id'};
}
# ---------------------------------------------------------------------
=item get_cookie
Return the session cookie.
=cut
# ---------------------------------------------------------------------
sub get_cookie
{
my $self = shift;
return $self->{'cookie'};
}
# ---------------------------------------------------------------------
=item set_cookie
Set the session cookie
=cut
# ---------------------------------------------------------------------
sub set_cookie
{
my $self = shift;
my $cookie = shift;
$self->{'cookie'} = $cookie;
}
# ---------------------------------------------------------------------
=item close
Update time stamp on this Session and close it. Calling untie on
Apache::Session hash causes it to be serialized to the database
=cut
# ---------------------------------------------------------------------
sub close
{
my $self = shift;
my $session_ref = $self->{'persistent_data'};
$session_ref->{'timestamp'} = time;
if (! $self->{'empty'})
{
untie (%{$session_ref})
}
}
# ---------------------------------------------------------------------
=item set_persistent_session_hash
Overwrites the entire persistent_data hash in this object. Used by
managemdpsessions.pl to assign a "thawed" hash ref to the member data of
an empty, blessed Session object.
=cut
# ---------------------------------------------------------------------
sub set_persistent_session_hash
{
my $self = shift;
my $hash_ref = shift;
$self->{'persistent_data'} = $hash_ref;
}
# ---------------------------------------------------------------------
=item set_persistent
Store hash key and value persistently.
=cut
# ---------------------------------------------------------------------
sub set_persistent
{
my $self = shift;
my ($key, $item) = @_;
my $session_ref = $self->{'persistent_data'};
${$session_ref}{$key} = $item;
}
# ---------------------------------------------------------------------
=item set_persistent_subkey
Store hash key, subkey and value persistently.
=cut
# ---------------------------------------------------------------------
sub set_persistent_subkey
{
my $self = shift;
my ($key, $subkey, $item) = @_;
my $session_ref = $self->{'persistent_data'};
${$session_ref}{$key}{$subkey} = $item;
}
# ---------------------------------------------------------------------
=item get_persistent
Get persistent value by key.
=cut
# ---------------------------------------------------------------------
sub get_persistent
{
my $self = shift;
my $key = shift;
my $session_ref = $self->{'persistent_data'};
return ${$session_ref}{$key};
}
# ---------------------------------------------------------------------
=item get_persistent_subkey
Get persistent value by key, subkey.
=cut
# ---------------------------------------------------------------------
sub get_persistent_subkey
{
my $self = shift;
my ($key, $subkey) = @_;
my $session_ref = $self->{'persistent_data'};
return ${$session_ref}{$key}{$subkey};
}
# ---------------------------------------------------------------------
=item set_transient
Store hash key and value transiently.
=cut
# ---------------------------------------------------------------------
sub set_transient
{
my $self = shift;
my ($key, $item) = @_;
$self->{'transient_data'} = {} unless ( ref($self->{'transient_data'}) );
my $session_ref = $self->{'transient_data'};
${$session_ref}{$key} = $item;
}
# ---------------------------------------------------------------------
=item set_transient_subkey
Store hash key, subkey and value persistently.
=cut
# ---------------------------------------------------------------------
sub set_transient_subkey
{
my $self = shift;
my ($key, $subkey, $item) = @_;
$self->{'transient_data'} = {} unless ( ref($self->{'transient_data'}) );
my $session_ref = $self->{'transient_data'};
${$session_ref}{$key}{$subkey} = $item;
}
# ---------------------------------------------------------------------
=item get_transient
Get transient value by key.
=cut
# ---------------------------------------------------------------------
sub get_transient
{
my $self = shift;
my $key = shift;
my $session_ref = $self->{'transient_data'};
return ${$session_ref}{$key};
}
# ---------------------------------------------------------------------
=item get_transient_subkey
Get transient value by key, subkey.
=cut
# ---------------------------------------------------------------------
sub get_transient_subkey
{
my $self = shift;
my ($key, $subkey) = @_;
my $session_ref = $self->{'transient_data'};
return ${$session_ref}{$key}{$subkey};
}
# ---------------------------------------------------------------------
=item session_dumper
What is says
=cut
# ---------------------------------------------------------------------
sub session_dumper
{
my $self = shift;
require Data::Dumper;
$Data::Dumper::Indent = 2;
my $dump = Data::Dumper->Dump( [$self], [qw($self)] );
# protect entities and turn naked & into &
$dump =~ s/&([^;]+);/ENTITY:$1:ENTITY/gis;
$dump =~ s/&/&/gis;
$dump =~ s/ENTITY:([a-z0-9]+):ENTITY/&$1;/gis;
return qq{<pre style="text-align: left">$dump</pre>};
}
# ---------------------------------------------------------------------
=item dispose
Session cleanup causing serialization via Session::close
=cut
# ---------------------------------------------------------------------
sub dispose
{
my $self = shift;
$self->close();
}
1;
__END__
=head1 AUTHOR
Phillip Farber, University of Michigan, pfarber@umich.edu
=head1 COPYRIGHT
Copyright 2007 ©, The Regents of The University of Michigan, All Rights Reserved
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
=cut