| 1 | package Puzzle::Session; | ||
| 2 | |||
| 3 | our $VERSION = '0.02'; | ||
| 4 | |||
| 5 | |||
| 6 | use Params::Validate qw(:types);; | ||
| 7 | use base 'Class::Container'; | ||
| 8 | |||
| 9 | our $mod_version; | ||
| 10 | |||
| 11 | eval { require Apache2::Cookie }; | ||
| 12 | $modperl_version = $@ ? undef : 2; | ||
| 13 | unless ($modperl_version) { | ||
| 14 | eval {require Apache::Cookie }; | ||
| 15 | $modperl_version = $@ ? undef : 1; | ||
| 16 | } | ||
| 17 | unless ($modperl_version) { | ||
| 18 | die 'Prerequisite Apache::Cookie or Apache2::Cookie not found'; | ||
| 19 | } | ||
| 20 | |||
| 21 | |||
| 22 | BEGIN { | ||
| 23 | __PACKAGE__->valid_params( | ||
| 24 | user => { isa => 'Puzzle::Session::User' }, | ||
| 25 | auth => { isa => 'Puzzle::Session::Auth' }, | ||
| 26 | ); | ||
| 27 | |||
| 28 | __PACKAGE__->contained_objects ( | ||
| 29 | user => 'Puzzle::Session::User', | ||
| 30 | auth => 'Puzzle::Session::Auth', | ||
| 31 | ); | ||
| 32 | } | ||
| 33 | |||
| 34 | # all new valid_params are read&write methods | ||
| 35 | use HTML::Mason::MethodMaker( | ||
| 36 | read_only => [qw(_session_id user internal_session auth)], | ||
| 37 | read_write => [ | ||
| 38 | [date => { parse => 'string' , type => SCALAR }], | ||
| 39 | ] | ||
| 40 | |||
| 41 | ); | ||
| 42 | |||
| 43 | sub AUTOLOAD { | ||
| 44 | my $self = shift; | ||
| 45 | my $key = $AUTOLOAD; | ||
| 46 | $key =~ s/.*:://; | ||
| 47 | if (@_) { | ||
| 48 | $self->{internal_session}->{$key} = shift; | ||
| 49 | } else { | ||
| 50 | return $self->{internal_session}->{$key}; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | sub save { | ||
| 55 | my $self = shift; | ||
| 56 | # chiamo la stessa funzione sui contained objects | ||
| 57 | $self->date(localtime() . ''); | ||
| 58 | $self->_obj2hash; | ||
| 59 | $self->user->save; | ||
| 60 | untie %{$self->{internal_session}}; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub load { | ||
| 64 | my $self = shift; | ||
| 65 | my $dbcfg = $self->container()->cfg->db; | ||
| 66 | my $session_name = "puzzle.$ENV{SERVER_NAME}"; | ||
| 67 | my $r = $self->container->_mason->apache_req or die "Unable to get Apache request handler"; | ||
| 68 | my %c = $modperl_version == 2 ? Apache2::Cookie->fetch($r) : Apache::Cookie->fetch; | ||
| 69 | my $sid = exists $c{$session_name} ? $c{$session_name}->value : undef; | ||
| 70 | my %db_params = ( | ||
| 71 | Handle => $self->container()->dbh->storage->dbh, | ||
| 72 | LockHandle => $self->container()->dbh->storage->dbh | ||
| 73 | ); | ||
| 74 | # will get an existing session from a cookie, or create a new session | ||
| 75 | # and cookie if needed | ||
| 76 | eval { | ||
| 77 | tie %{$self->{internal_session}}, 'Apache::Session::MySQL',$sid,\%db_params; | ||
| 78 | }; | ||
| 79 | if ($@) { | ||
| 80 | die $@ unless $@ =~ /Object does not exist/; | ||
| 81 | # L'id non esiste piu' nel database, creo un nuovo id; | ||
| 82 | undef $sid; | ||
| 83 | tie %{$self->{internal_session}}, 'Apache::Session::MySQL', $sid,\%db_params | ||
| 84 | } | ||
| 85 | if ($modperl_version == 2) { | ||
| 86 | Apache2::Cookie->new( $r, | ||
| 87 | name => $session_name, | ||
| 88 | value => $self->{internal_session}->{_session_id}, | ||
| 89 | path => '/', | ||
| 90 | )->bake($r) unless (defined $sid); | ||
| 91 | } else { | ||
| 92 | Apache::Cookie->new( $r, | ||
| 93 | name => $session_name, | ||
| 94 | value => $self->{internal_session}->{_session_id}, | ||
| 95 | path => '/', | ||
| 96 | )->bake unless (defined $sid); | ||
| 97 | } | ||
| 98 | # sync tied hash with object methods | ||
| 99 | $self->_hash2obj; | ||
| 100 | # call same funzion on contained objects | ||
| 101 | $self->user->load; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub delete { | ||
| 105 | my $self = shift; | ||
| 106 | my $key = shift; | ||
| 107 | delete $self->internal_session->{$key}; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub _hash2obj { | ||
| 111 | my $self = shift; | ||
| 112 | foreach (qw/_session_id date/) { | ||
| 113 | if (exists $self->internal_session->{$_}) { | ||
| 114 | $self->{$_} = $self->internal_session->{$_}; | ||
| 115 | } else { | ||
| 116 | delete $self->{$_}; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | sub _obj2hash { | ||
| 122 | my $self = shift; | ||
| 123 | foreach (qw/date/) { | ||
| 124 | if (exists $self->{$_}) { | ||
| 125 | $self->internal_session->{$_} = $self->{$_}; | ||
| 126 | } else { | ||
| 127 | delete $self->internal_session->{$_}; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | } | ||
| 131 | 1; |