| 1 | package Cake::Plugins::Simple::Session; | ||
| 2 | use Cake 'Plugin'; | ||
| 3 | use Carp; | ||
| 4 | |||
| 5 | my $utils_class = 'Cake::Plugins::Simple::Session::Utils'; | ||
| 6 | |||
| 7 | sub setup { | ||
| 8 | my $self = shift; | ||
| 9 | ##only works with Cake::Plugins::Simple::Cache plugin | ||
| 10 | if (!settings('Cake::Plugins::Simple::Cache')){ | ||
| 11 | $self->error('You need to load Cake::Plugins::Simple::Cache plugin in order to use Simple Session plugin'); | ||
| 12 | } | ||
| 13 | $self->NEXT::setup(@_); | ||
| 14 | } | ||
| 15 | |||
| 16 | |||
| 17 | sub session { | ||
| 18 | my $self = shift; | ||
| 19 | ##set/get cookie | ||
| 20 | my ($cookie_name,$expire) = (!ref $_[0] ? shift : undef, !ref $_[1] ? $_[1] : undef); | ||
| 21 | my $options = $utils_class->get_options($self); | ||
| 22 | $cookie_name ||= $options->{cookie_name}; | ||
| 23 | $expire ||= $options->{session_expire_time}; | ||
| 24 | |||
| 25 | my $cookieValue = $utils_class->get_cookie($self, $cookie_name, $expire); | ||
| 26 | |||
| 27 | #get from cache | ||
| 28 | my $session = $utils_class->get_session($self, $cookieValue,$cookie_name); | ||
| 29 | if (@_) { | ||
| 30 | my $newsession = $_[0]; | ||
| 31 | croak('session takes a hash or hashref') unless ref $newsession; | ||
| 32 | for my $key (keys %$newsession) { | ||
| 33 | $session->{$key} = $newsession->{$key}; | ||
| 34 | } | ||
| 35 | ##set in cache | ||
| 36 | $self->cache($cookieValue,$session,$expire,'SESSIONS/'.$cookie_name); | ||
| 37 | } | ||
| 38 | return $session; | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | sub flash { | ||
| 44 | |||
| 45 | my $self = shift; | ||
| 46 | |||
| 47 | $self->{'app.flash'} ||= {}; | ||
| 48 | |||
| 49 | if (@_){ | ||
| 50 | |||
| 51 | ##only get requested | ||
| 52 | if (@_ == 1 and !ref $_[0]){ | ||
| 53 | my $value = $self->session->{'app.flash'}; | ||
| 54 | |||
| 55 | my $return = $value->{$_[0]}; | ||
| 56 | delete $value->{$_[0]}; | ||
| 57 | $self->session({ | ||
| 58 | 'app.flash' => $value | ||
| 59 | }); | ||
| 60 | |||
| 61 | return $return; | ||
| 62 | } | ||
| 63 | |||
| 64 | my $hash = @_ > 1 ? { @_ } : $_[0]; | ||
| 65 | #croak('flash takes a hash or hashref') unless ref $hash; | ||
| 66 | $self->{'app.flash'} = Cake::Plugins::Simple::Session::Utils::combine_hashes($self->{'app.flash'},$hash); | ||
| 67 | |||
| 68 | $self->session({ | ||
| 69 | 'app.flash' => $self->{'app.flash'} | ||
| 70 | }); | ||
| 71 | } | ||
| 72 | |||
| 73 | else { | ||
| 74 | if (my $flash = $self->session->{'app.flash'}){ | ||
| 75 | $self->session({ | ||
| 76 | 'app.flash' => undef | ||
| 77 | }); | ||
| 78 | return $flash; | ||
| 79 | } | ||
| 80 | |||
| 81 | else { | ||
| 82 | return $self->{'app.flash'}; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | register(); | ||
| 89 | |||
| 90 | |||
| 91 | package #hide from cake and cpan :) | ||
| 92 | Cake::Plugins::Simple::Session::Utils; | ||
| 93 | use strict; | ||
| 94 | use warnings; | ||
| 95 | |||
| 96 | my $options_singleton; | ||
| 97 | my $session; | ||
| 98 | my $cookie_jar = {}; | ||
| 99 | |||
| 100 | sub get_options { | ||
| 101 | shift; | ||
| 102 | $options_singleton ||= shift->config('Cake::Plugins::Simple::Session'); | ||
| 103 | return $options_singleton; | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | sub get_cookie { | ||
| 108 | |||
| 109 | shift; | ||
| 110 | my $c = shift; ##$c | ||
| 111 | my $cookie_name = shift; | ||
| 112 | my $expire = shift; | ||
| 113 | |||
| 114 | my $cookie = $c->cookie($cookie_name) || delete $cookie_jar->{$cookie_name}; | ||
| 115 | if (!$cookie){ | ||
| 116 | ###create random name for this cookie | ||
| 117 | $cookie = Cake::Utils::random_string(); | ||
| 118 | my $result = $c->cookie({ | ||
| 119 | name => $cookie_name, | ||
| 120 | value => $cookie, | ||
| 121 | path => '/', | ||
| 122 | length => $expire | ||
| 123 | }); | ||
| 124 | $cookie_jar->{$cookie_name} = $cookie; | ||
| 125 | } | ||
| 126 | return $cookie; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub get_session { | ||
| 130 | shift; | ||
| 131 | my ($c,$cookie,$cookie_name) = @_; | ||
| 132 | $session = $c->cache($cookie,'SESSIONS/'.$cookie_name); | ||
| 133 | return $session || {}; | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | sub combine_hashes { | ||
| 138 | my ($hash1,$hash2) = @_; | ||
| 139 | while (my($key,$value) = each(%{$hash2})) { | ||
| 140 | $hash1->{$key} = $value; | ||
| 141 | } | ||
| 142 | return $hash1; | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | 1; | ||
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | __END__ |