package File::Session; use 5.006000; use strict; use warnings; use Cache::FileCache; use CGI; use Digest::SHA ('sha256_hex'); our @ISA = qw(Cache::FileCache CGI); our $VERSION = '0.04'; sub new { my $class = shift; $class->SUPER::new(@_); } sub retrieve_session { my ($self) = @_; my $session_id = $self->cookie('session_id'); if ($session_id) { # Cookie vorhanden # Set the ID $self->id($session_id); return $session_id; } else { # kein Cookie vorhanden? Dann mach eines! my $cookie = $self->cookie(-name => 'session_id', -value => $self->generate_id() ); print $self->redirect(-cookie => $cookie, -uri => $self->self_url()); } } sub generate_id { my ($self) = @_; my $len = 24; my @session_chars = ('A' .. 'Z', 'a' .. 'z', 0 .. 9, '.', '-'); my $id; eval { open(RANDOM, "/dev/urandom") or die $!; read(RANDOM, $id, $len) == $len; close RANDOM; $id =~ s/(.)/$session_chars[ord($1) & 63]/esg; $id .= time() . $$; $id = sha256_hex($id); return $id; }; if ($@) { # if random is not avaible, create the SID as in CGI::Session # thanks for the inspiration ;-) my $sha = Digest::SHA->new(256); $sha->add($$ , time() , rand(time) ); $id = $sha->hexdigest(); } return $id; } sub id { my ($self, $new_id) = @_; my $old_id = $self->{'id'}; $self->{'id'} = $new_id if ($new_id); return $old_id; } sub get_data { my ($self) = @_; my $session_id = $self->id(); my $data = $self->get($session_id); $self->set($session_id,$data); return $data; } 1;