blib/lib/Embperl/Session.pm untested

filter: Statementstmt 0.0% 0 / 194 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 381 CC 48 SCAR 47.5
CC
48
Cov
0%
require_modules
380.0
init
90.0
populate
72.0
CRAP 116.1
1 #############################################################################
2 #
3 # HTML::Embperl::Session
4 # A bridge between Apache::Session and Embperl's %udat hash
5 # Copyright(c) 1999 Gerald Richter (richter at embperl dot org)
6 # Copyright(c) 1998, 1999 Jeffrey William Baker (jeffrey@kathyandjeffrey.net)
7 # Distribute under the Artistic License
8 #
9 #############################################################################
10  
11 =head1 NAME
12  
13 HTML::Embperl::Session - adaptation of Apache::Session to work with HTML::Embperl
14  
15 =head1 DESCRIPTION
16  
17 An adaptation of Apache::Session to work with HTML::Embperl
18  
19 =head1 SYNOPSIS
20  
21 =head2 Addtional Attributes for TIE
22  
23 =over 4
24  
25 =item lazy
26  
27 By Specifyng this attribute, you tell Apache::Session to not do any
28 access to the object store, until the first read or write access to
29 the tied hash. Otherwise the B<tie> function will make sure the hash
30 exist or creates a new one.
31  
32 =item create_unknown
33  
34 Setting this to one causes Apache::Session to create a new session
35 with the given id (or a new id, depending on C<recreate_id>)
36 when the specified session id does not exists. Otherwise it will die.
37  
38 =item recreate_id
39  
40 Setting this to one causes Apache::Session to create a new session id
41 when the specified session id does not exists.
42  
43 =item object_store
44  
45 Specify the class for the object store. (The Apache::Session:: prefix is
46 optional) Only for Apache::Session 1.00.
47  
48 =item lock_manager
49  
50 Specify the class for the lock manager. (The Apache::Session:: prefix is
51 optional) Only for Apache::Session 1.00.
52  
53 =item Store
54  
55 Specify the class for the object store. (The Apache::Session::Store prefix is
56 optional) Only for Apache::Session 1.5x.
57  
58 =item Lock
59  
60 Specify the class for the lock manager. (The Apache::Session::Lock prefix is
61 optional) Only for Apache::Session 1.5x.
62  
63 =item Generate
64  
65 Specify the class for the id generator. (The Apache::Session::Generate prefix is
66 optional) Only for Apache::Session 1.5x.
67  
68 =item Serialize
69  
70 Specify the class for the data serializer. (The Apache::Session::Serialize prefix is
71 optional) Only for Apache::Session 1.5x.
72  
73  
74 =back
75  
76 Example using attrubtes to specfiy store and object classes instead of
77 a derived class:
78  
79 use HTML::Embperl::Session;
80  
81 tie %session, 'HTML::Embperl::Session', undef,
82 {
83 object_store => 'DBIStore',
84 lock_manager => 'SysVSemaphoreLocker',
85 DataSource => 'dbi:Oracle:db'
86 };
87  
88 NOTE: HTML::Embperl::Session will require the necessary additional perl modules for you.
89  
90  
91 =head2 Addtional Methods
92  
93 =over 4
94  
95 =item setid
96  
97 Set the session id for further accesses.
98  
99 =item getid
100  
101 Get the session id. The difference to using $session{_session_id} is,
102 that in lazy mode, getid will B<not> create a new session id, if it
103 doesn't exists.
104  
105 =item cleanup
106  
107 Writes any pending data, releases all locks and deletes all data from memory.
108  
109 =back
110  
111 =head1 AUTHORS
112  
113 Gerald Richter <richter at embperl dot org> is the current maintainer.
114  
115 This class was written by Jeffrey Baker (jeffrey@kathyandjeffrey.net)
116 but it is taken wholesale from a patch that Gerald Richter
117 (richter at embperl dot org) sent me against Apache::Session.
118  
119  
120 =cut
121  
122 package HTML::Embperl::Session;
123  
124 use strict;
125 use vars qw(@ISA $VERSION);
126  
127 $VERSION = '3.0.1';
128 @ISA = qw(Apache::Session);
129  
130 use Apache::Session;
131  
132 no warnings "uninitialized" ;
133  
134 use constant NEW => Apache::Session::NEW () ;
135 use constant MODIFIED => Apache::Session::MODIFIED () ;
136 use constant DELETED => Apache::Session::DELETED () ;
137 use constant SYNCED => Apache::Session::SYNCED () ;
138  
139  
140 sub TIEHASH {
141 my $class = shift;
142
143 my $session_id = shift;
144 my $args = shift || {};
145  
146 if(ref $args ne "HASH")
147 {
148 die "Additional arguments should be in the form of a hash reference";
149 }
150  
151 #Set-up the data structure and make it an object
152 #of our class
153  
154  
155 #$args -> {IDLength} ||= 32 ;
156 my $self =
157 {
158 args => $args,
159 data => { _session_id => $session_id },
160 initial_session_id => $session_id,
161 lock => 0,
162 lock_manager => undef,
163 object_store => undef,
164 status => 0,
165 serialized => undef,
166 };
167
168 bless $self, $class;
169  
170 $self -> require_modules ($args) ;
171  
172 $self -> init if (!$args -> {'lazy'}) ;
173  
174  
175 return $self ;
176 }
177  
178  
179 sub require_modules
180 {
181 my $self = shift ;
182 my $args = shift ;
183  
184 # check object_store and lock_manager classes (Apache::Session 1.00)
185
186 if ($args -> {'object_store'})
187 {
188 $args -> {'object_store'} = "Apache::Session::$args->{'object_store'}" if (!($args -> {'object_store'} =~ /::/)) ;
189 eval "require $args->{'object_store'}" ;
190 die "Cannot require $args->{'object_store'}" if ($@) ;
191 }
192  
193 if ($args -> {'lock_manager'})
194 {
195 $args -> {'lock_manager'} = "Apache::Session::$args->{'lock_manager'}" if (!($args -> {'lock_manager'} =~ /::/)) ;
196 eval "require $args->{'lock_manager'}" ;
197 die "Cannot require $args->{'lock_manager'}" if ($@) ;
198 }
199  
200 # check Store, Lock, Generate, Serialize classes (Apache::Session 1.5x)
201
202 if ($args -> {'Store'})
203 {
204 $args -> {'Store'} = "Apache::Session::Store::$args->{'Store'}" if (!($args -> {'Store'} =~ /::/)) ;
205 eval "require $args->{'Store'}" ;
206 die "Cannot require $args->{'Store'}" if ($@) ;
207 }
208  
209 if ($args -> {'Lock'})
210 {
211 $args -> {'Lock'} = "Apache::Session::Lock::$args->{'Lock'}" if (!($args -> {'Lock'} =~ /::/)) ;
212 eval "require $args->{'Lock'}" ;
213 die "Cannot require $args->{'Lock'}" if ($@) ;
214 }
215  
216 if ($args -> {'Generate'})
217 {
218 $args -> {'Generate'} = "Apache::Session::Generate::$args->{'Generate'}" if (!($args -> {'Generate'} =~ /::/)) ;
219 eval "require $args->{'Generate'}" ;
220 die "Cannot require $args->{'Generate'}" if ($@) ;
221 }
222  
223 if ($args -> {'Serialize'})
224 {
225 $args -> {'Serialize'} = "Apache::Session::Serialize::$args->{'Serialize'}" if (!($args -> {'Serialize'} =~ /::/)) ;
226 eval "require $args->{'Serialize'}" ;
227 die "Cannot require $args->{'Serialize'}" if ($@) ;
228 }
229 }
230  
231  
232  
233  
234  
235 sub init
236 {
237 my $self = shift ;
238  
239 #If a session ID was passed in, this is an old hash.
240 #If not, it is a fresh one.
241  
242 my $session_id = $self->{data}->{_session_id} ;
243  
244 if (!$session_id && $self -> {idfrom})
245 {
246 $session_id = $self->{data}->{_session_id} = &{$self->{generate}}($self, $self -> {idfrom}) ;
247 }
248  
249 $self->{initial_session_id} ||= $session_id ;
250  
251 $self->populate;
252  
253 if (defined $session_id && $session_id)
254 {
255 #check the session ID for remote exploitation attempts
256 #this will die() on suspicious session IDs.
257  
258 eval { &{$self->{validate}}($self); } ;
259 if (!$@)
260 { # session id is ok
261 if (exists $self -> {'args'}->{Transaction} && $self -> {'args'}->{Transaction})
262 {
263 $self->acquire_write_lock;
264 }
265  
266 $self->{status} &= ($self->{status} ^ NEW);
267  
268 if ($self -> {'args'}{'create_unknown'})
269 {
270 eval { $self -> restore } ;
271 #warn "Try to load session: $@" if ($@) ;
272 $@ = "" ;
273 $session_id = $self->{data}->{_session_id} ;
274 }
275 else
276 {
277 $self->restore;
278 }
279 }
280 }
281  
282 $@ = '' ;
283  
284 if (!($self->{status} & SYNCED))
285 {
286 $self->{status} |= NEW();
287 if (!$self->{data}->{_session_id} || $self -> {'args'}{'recreate_id'})
288 {
289 if (exists ($self->{generate}))
290 { # Apache::Session >= 1.50
291 $self->{data}->{_session_id} = &{$self->{generate}}($self) ;
292 }
293 else
294 {
295 $self->{data}->{_session_id} = $self -> generate_id() ;
296 }
297 }
298 $self->save;
299 }
300
301 #warn "Session INIT $self->{initial_session_id};$self->{data}->{_session_id};" ;
302  
303 return $self;
304 }
305  
306 sub FETCH {
307 my $self = shift;
308 my $key = shift;
309  
310 $self -> init if (!$self -> {'status'}) ;
311  
312 return $self->{data}->{$key};
313 }
314  
315 sub STORE {
316 my $self = shift;
317 my $key = shift;
318 my $value = shift;
319
320 $self -> init if (!$self -> {'status'}) ;
321  
322 $self->{data}->{$key} = $value;
323
324 $self->{status} |= MODIFIED;
325
326 return $self->{data}->{$key};
327 }
328  
329 sub DELETE {
330 my $self = shift;
331 my $key = shift;
332
333 $self -> init if (!$self -> {'status'}) ;
334  
335 $self->{status} |= MODIFIED;
336
337 delete $self->{data}->{$key};
338 }
339  
340 sub CLEAR {
341 my $self = shift;
342  
343 $self -> init if (!$self -> {'status'}) ;
344  
345 $self->{status} |= MODIFIED;
346
347 $self->{data} = {};
348 }
349  
350 sub EXISTS {
351 my $self = shift;
352 my $key = shift;
353
354 $self -> init if (!$self -> {'status'}) ;
355  
356 return exists $self->{data}->{$key};
357 }
358  
359 sub FIRSTKEY {
360 my $self = shift;
361
362 $self -> init if (!$self -> {'status'}) ;
363  
364 my $reset = keys %{$self->{data}};
365 return each %{$self->{data}};
366 }
367  
368 sub NEXTKEY {
369 my $self = shift;
370
371 $self -> init if (!$self -> {'status'}) ;
372  
373 return each %{$self->{data}};
374 }
375  
376 sub DESTROY {
377 my $self = shift;
378
379 return if (!$self -> {'status'}) ;
380  
381 $self->save;
382 $self->release_all_locks;
383 }
384  
385 sub cleanup
386 {
387 my $self = shift;
388
389 $self->{initial_session_id} = undef ;
390 if (!$self -> {'status'})
391 {
392 $self->{data} = {} ;
393 $self->{serialized} = undef ;
394 return ;
395 }
396  
397 $self->save;
398 {
399 local $SIG{__WARN__} = 'IGNORE' ;
400 local $SIG{__DIE__} = 'IGNORE' ;
401 eval { $self -> {object_store} -> close } ; # Try to close file storage
402 $@ = "" ;
403 }
404 $self->release_all_locks;
405  
406 $self->{'status'} = 0 ;
407 $self->{data} = {} ;
408 $self->{serialized} = undef ;
409 }
410  
411  
412 sub setid {
413 my $self = shift;
414  
415 $self->{'status'} = 0 ;
416 $self->{data}->{_session_id} = $self->{initial_session_id} = shift ;
417  
418 }
419  
420 sub setidfrom {
421 my $self = shift;
422  
423 $self->{'status'} = 0 ;
424 $self->{data}->{_session_id} = $self->{initial_session_id} = undef ;
425 $self->{idfrom} = shift ;
426  
427 }
428  
429  
430 sub getid {
431 my $self = shift;
432  
433 return $self->{data}->{_session_id} || $self->{'ID'} ;
434 }
435  
436 sub getids {
437 my $self = shift;
438  
439 return ($self->{initial_session_id}, $self->{data}->{_session_id} || $self->{'ID'}, $self->{status} & MODIFIED) ;
440 }
441  
442 sub delete {
443 my $self = shift;
444
445 return if ($self->{status} & NEW);
446
447 $self->{initial_session_id} = "!DELETE" ;
448  
449 $self -> init if (!$self -> {'status'}) ;
450  
451 $self->{status} |= DELETED;
452 $self->save;
453 $self->{data} = {} ; # Throw away the data
454 }
455  
456  
457 #
458 # For Apache::Session 1.00
459 #
460  
461 sub get_object_store {
462 my $self = shift;
463  
464 return new {$self -> {'args'}{'object_store'}} $self;
465 }
466  
467 sub get_lock_manager {
468 my $self = shift;
469
470 return new {$self -> {'args'}{'lock_manager'}} $self;
471 }
472  
473 #
474 # Default validate for Apache::Session < 1.53
475 #
476  
477 sub validate {
478 #This routine checks to ensure that the session ID is in the form
479 #we expect. This must be called before we start diddling around
480 #in the database or the disk.
481  
482 my $session = shift;
483
484 if ($session->{data}->{_session_id} !~ /^[a-fA-F0-9]+$/) {
485 die;
486 }
487 }
488  
489 #
490 # For Apache::Session >= 1.50
491 #
492  
493 sub populate
494 {
495 my $self = shift;
496  
497 my $store = $self->{args}->{Store};
498 my $lock = $self->{args}->{Lock};
499 my $gen = $self->{args}->{Generate};
500 my $ser = $self->{args}->{Serialize};
501  
502  
503 $self->{object_store} = new $store $self if ($store) ;
504 $self->{lock_manager} = new $lock $self if ($lock);
505 $self->{generate} = \&{$gen . '::generate'} if ($gen);
506 $self->{'validate'} = \&{$gen . '::validate'} if ($gen && defined (&{$gen . '::validate'}));
507 $self->{serialize} = \&{$ser . '::serialize'} if ($ser);
508 $self->{unserialize} = \&{$ser . '::unserialize'} if ($ser) ;
509  
510 if (!defined ($self->{'validate'}))
511 {
512 $self->{'validate'} = \&validate ;
513 }
514  
515 return $self;
516 }
517  
518  
519  
520 1 ;