| 1 | package CGI::okSession; | ||||||||||
| 2 | |||||||||||
| 3 | #use 5.008; | ||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | #use warnings; | ||||||||||
| 6 | 4 | ▶ | use Storable; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | ▶ | use Exporter; #Export functions | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 4 | our @ISA = qw(Exporter); | |||||||||
| 10 | |||||||||||
| 11 | # Items to export into callers namespace by default. Note: do not export | ||||||||||
| 12 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||||||
| 13 | # Do not simply export all your public functions/methods/constants. | ||||||||||
| 14 | |||||||||||
| 15 | # This allows declaration use CGI::okSession ':all'; | ||||||||||
| 16 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||||||||||
| 17 | # will save memory. | ||||||||||
| 18 | 4 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||
| 19 | |||||||||||
| 20 | ) ] ); | ||||||||||
| 21 | |||||||||||
| 22 | 4 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||
| 23 | |||||||||||
| 24 | 4 | our @EXPORT = qw( | |||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | 4 | our $VERSION = '0.02'; | |||||||||
| 28 | |||||||||||
| 29 | 4 | my @known_options = qw( | |||||||||
| 30 | dir | ||||||||||
| 31 | timout | ||||||||||
| 32 | app | ||||||||||
| 33 | id | ||||||||||
| 34 | ); | ||||||||||
| 35 | |||||||||||
| 36 | 4 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 37 | 4 | my $class = shift; | |||||||||
| 38 | 4 | my %config = ( | |||||||||
| 39 | dir => '/tmp', | ||||||||||
| 40 | timeout => 30*60, | ||||||||||
| 41 | app => 'default' | ||||||||||
| 42 | ,@_ | ||||||||||
| 43 | ); | ||||||||||
| 44 | 4 | _remove_expired(%config); | |||||||||
| 45 | 4 | my $self; | |||||||||
| 46 | 4 | ▶ | unless ($config{id}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 1 | require Time::HiRes; | |||||||||
| 48 | 1 | require Digest::MD5; | |||||||||
| 49 | 1 | $config{id} = Digest::MD5::md5_hex(Time::HiRes::time()); | |||||||||
| 50 | } | ||||||||||
| 51 | 4 | ▶ | if (-e "$config{dir}/$config{id}.sess") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 2 | $self = Storable::lock_retrieve("$config{dir}/$config{id}.sess"); | |||||||||
| 53 | } else { | ||||||||||
| 54 | 2 | $self->{___ID} = $config{id}; | |||||||||
| 55 | 2 | $self->{___config} = \%config; | |||||||||
| 56 | 2 | Storable::lock_nstore($self,"$config{dir}/$config{id}.sess"); | |||||||||
| 57 | } | ||||||||||
| 58 | 4 | $self->{___ID} = $config{id}; | |||||||||
| 59 | 4 | $self->{___expires} = time() + $config{timeout}; | |||||||||
| 60 | 4 | $self->{___config} = \%config; | |||||||||
| 61 | # $self->DESTROY = DESTROY; | ||||||||||
| 62 | 4 | _set_expiration_2_app($self->{___ID},$self->{___expires},%{$self->{___config}}); | |||||||||
| 63 | 4 | return bless($self,$class); | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 2 | ▶ | sub get_ID { | ||||||||
|
Subroutine
get_ID: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 67 | 2 | return shift->{___ID}; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 4 | ▶ | sub _remove_expired { | ||||||||
|
Subroutine
_remove_expired: called CC=6 SCAR=18.0
| |||||||||||
| 71 | 4 | my (%config) = @_; | |||||||||
| 72 | 4 | my $a; | |||||||||
| 73 | 4 | ▶ | if (-e "$config{dir}/$config{app}") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 3 | $a = Storable::lock_retrieve("$config{dir}/$config{app}"); | |||||||||
| 75 | } else { | ||||||||||
| 76 | 1 | $a->{Application} = $config{app}; | |||||||||
| 77 | 1 | Storable::lock_nstore($a,"$config{dir}/$config{app}"); | |||||||||
| 78 | 1 | return; | |||||||||
| 79 | } | ||||||||||
| 80 | 3 | ▶ | unless ($a->{Sessions}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | return; | |||||||||
| 82 | } | ||||||||||
| 83 | 3 | my $tim = time(); | |||||||||
| 84 | 3 | my $t; | |||||||||
| 85 | 3 | foreach $t (keys %{$a->{Sessions}}) { | |||||||||
| 86 | 3 | ▶ | if ($a->{Sessions}->{$t} < $tim) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 1 | ▶ | unlink("$config{dir}/$t.sess") || die "Can't delete Session"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 1 | delete $a->{Sessions}->{$t}; | |||||||||
| 89 | } | ||||||||||
| 90 | } | ||||||||||
| 91 | 3 | Storable::lock_nstore($a,"$config{dir}/$config{app}"); | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 0 | ▶ | sub expires { | ||||||||
|
Subroutine
expires: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 95 | 0 | my $self = shift; | |||||||||
| 96 | 0 | return $self->{___expires}; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 1 | ▶ | sub expires_www { | ||||||||
|
Subroutine
expires_www: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 100 | 1 | my $self = shift; | |||||||||
| 101 | 1 | my @t = gmtime($self->{___expires}); | |||||||||
| 102 | 1 | return sprintf('%3.3s, %02d-%3.3s-%04d %02d:%02d:%02d GMT',("Sun","Mon","Tue","Wed","Thu","Fri","Sut","Sun")[$t[6]],$t[3],("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")[$t[4]],$t[5]+1900,$t[2],$t[1],$t[0]); | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 4 | ▶ | sub _set_expiration_2_app { | ||||||||
|
Subroutine
_set_expiration_2_app: called CC=2 SCAR=7.0
| |||||||||||
| 106 | 4 | my ($id,$tim,%config) = @_; | |||||||||
| 107 | 4 | my $a = undef; | |||||||||
| 108 | 4 | ▶ | if (-e "$config{dir}/$config{app}") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 4 | $a = Storable::lock_retrieve("$config{dir}/$config{app}"); | |||||||||
| 110 | } | ||||||||||
| 111 | 4 | $a->{Sessions}->{$id} = $tim; | |||||||||
| 112 | 4 | Storable::lock_nstore($a,"$config{dir}/$config{app}"); | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | 4 | ▶ | DESTROY { | ||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||
| 116 | 4 | my $self = shift; | |||||||||
| 117 | 4 | my(%s) = (%$self); | |||||||||
| 118 | 4 | Storable::lock_store(\%s,"$s{___config}->{dir}/$s{___config}->{id}.sess"); | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 4 | 1; | |||||||||
| 122 | __END__ | ||||||||||