« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/SQLBuilder/Query.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/URL.pm »
| 1 | package PHP::Session::Memcached; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Digest::MD5 qw/md5_hex/; | ||
| 5 | use Dwarf::Util qw/load_class/; | ||
| 6 | use Encode qw/decode_utf8/; | ||
| 7 | |||
| 8 | sub _croak { require Carp; Carp::croak(@_) } | ||
| 9 | sub _carp { require Carp; Carp::carp(@_) } | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my($class, $sid, $opt) = @_; | ||
| 13 | _croak "sid must be specified." unless defined $sid; | ||
| 14 | |||
| 15 | my %default = ( | ||
| 16 | serialize_handler => 'PHP::Session::Serializer::PHP', | ||
| 17 | create => 0, | ||
| 18 | auto_save => 0, | ||
| 19 | ); | ||
| 20 | $opt ||= {}; | ||
| 21 | |||
| 22 | my $self = bless { | ||
| 23 | %default, | ||
| 24 | %$opt, | ||
| 25 | _sid => $sid, | ||
| 26 | _data => {}, | ||
| 27 | _changed => 0, | ||
| 28 | }, $class; | ||
| 29 | |||
| 30 | $self->_validate_sid; | ||
| 31 | $self->_parse_session; | ||
| 32 | |||
| 33 | return $self; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub id { shift->{_sid} } | ||
| 37 | sub memcached { shift->{memcached} } | ||
| 38 | |||
| 39 | sub serializer { | ||
| 40 | my $self = shift; | ||
| 41 | my $impl = $self->{serialize_handler}; | ||
| 42 | load_class($impl); | ||
| 43 | return $impl->new; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub get { | ||
| 47 | my($self, $key) = @_; | ||
| 48 | return $self->{_data}->{$key}; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub set { | ||
| 52 | my($self, $key, $value) = @_; | ||
| 53 | $self->{_changed}++; | ||
| 54 | $self->{_data}->{$key} = $value; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub unregister { | ||
| 58 | my($self, $key) = @_; | ||
| 59 | delete $self->{_data}->{$key}; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub unset { | ||
| 63 | my $self = shift; | ||
| 64 | $self->{_data} = {}; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub is_registered { | ||
| 68 | my($self, $key) = @_; | ||
| 69 | return exists $self->{_data}->{$key}; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub decode { | ||
| 73 | my($self, $data) = @_; | ||
| 74 | my $ret = eval { $self->serializer->decode($data) }; | ||
| 75 | if ($@) { | ||
| 76 | warn "Something wrong in PHP::Session::Serializer::PHP"; | ||
| 77 | $ret = $self->serializer->decode(decode_utf8($data)); | ||
| 78 | } | ||
| 79 | return $ret; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub encode { | ||
| 83 | my($self, $data) = @_; | ||
| 84 | $self->serializer->encode($data); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub save { | ||
| 88 | my $self = shift; | ||
| 89 | $self->memcached->set( | ||
| 90 | $self->id, | ||
| 91 | $self->encode($self->{_data}) | ||
| 92 | ); | ||
| 93 | $self->{_changed} = 0; # init | ||
| 94 | } | ||
| 95 | |||
| 96 | sub destroy { | ||
| 97 | my $self = shift; | ||
| 98 | $self->memcached->delete($self->id); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub DESTROY { | ||
| 102 | my $self = shift; | ||
| 103 | if ($self->{_changed}) { | ||
| 104 | if ($self->{auto_save}) { | ||
| 105 | $self->save; | ||
| 106 | } else { | ||
| 107 | _carp("PHP::Session::Memcached: some keys are changed but not saved.") if $^W; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | sub _validate_sid { | ||
| 113 | my $self = shift; | ||
| 114 | $self->id =~ /^([0-9a-zA-Z]*)$/ | ||
| 115 | or _croak("Invalid session id: ", $self->id); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub _parse_session { | ||
| 119 | my $self = shift; | ||
| 120 | my $cont = $self->_slurp_content; | ||
| 121 | |||
| 122 | if (!$cont) { | ||
| 123 | if ($self->{create}) { | ||
| 124 | $self->{_data} = { __HTTP_Session_Info => 1 }; | ||
| 125 | $self->memcached->add( | ||
| 126 | $self->id, | ||
| 127 | $self->encode($self->{_data}) | ||
| 128 | ); | ||
| 129 | } else { | ||
| 130 | _croak($self->id, ": $!"); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | $self->{_data} = $self->decode($cont); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub _slurp_content { | ||
| 137 | my $self = shift; | ||
| 138 | return $self->memcached->get($self->id); | ||
| 139 | } | ||
| 140 | |||
| 141 | package Dwarf::Plugin::PHP::Session::Memcached; | ||
| 142 | use strict; | ||
| 143 | use warnings; | ||
| 144 | use Dwarf::Util; | ||
| 145 | |||
| 146 | sub init { | ||
| 147 | my ($class, $c, $conf) = @_; | ||
| 148 | $conf ||= {}; | ||
| 149 | |||
| 150 | my $memcached = $conf->{memcached} || undef; | ||
| 151 | my $session_key = $conf->{session_key} || 'SessionID'; | ||
| 152 | my $param_name = $conf->{param_name} || $session_key; | ||
| 153 | my $use_cookie = $conf->{use_cookie} || 1; | ||
| 154 | my $cookie_path = $conf->{cookie_path} || '/'; | ||
| 155 | my $cookie_domain = $conf->{cookie_domain} || undef; | ||
| 156 | my $cookie_expires = $conf->{cookie_expires} || undef; | ||
| 157 | my $on_init = $conf->{on_init} || sub {}; | ||
| 158 | |||
| 159 | add_method($c, session => sub { | ||
| 160 | my $self = shift; | ||
| 161 | |||
| 162 | $self->{'dwarf.session'} ||= do { | ||
| 163 | my $session_id = undef; | ||
| 164 | |||
| 165 | if ($use_cookie) { | ||
| 166 | my $cookie = $self->request->cookies->{$session_key}; | ||
| 167 | |||
| 168 | if (defined $cookie and $cookie ne '') { | ||
| 169 | $session_id = $cookie; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | my $query_param = $self->request->param($param_name); | ||
| 174 | |||
| 175 | if (defined $query_param and $query_param ne '') { | ||
| 176 | $session_id = $query_param; | ||
| 177 | } | ||
| 178 | |||
| 179 | my $session = PHP::Session::Memcached->new($session_id, { | ||
| 180 | memcached => $memcached || $self->memcached, | ||
| 181 | create => 1, | ||
| 182 | }); | ||
| 183 | $on_init->($session); | ||
| 184 | |||
| 185 | $session; | ||
| 186 | } | ||
| 187 | }); | ||
| 188 | |||
| 189 | return unless $use_cookie; | ||
| 190 | |||
| 191 | $c->add_trigger('AFTER_DISPATCH' => sub { | ||
| 192 | my ($self, $res) = @_; | ||
| 193 | return unless exists $self->{'dwarf.session'}; | ||
| 194 | my $value = { value => $self->session->id }; | ||
| 195 | $value->{path} = $cookie_path if defined $cookie_path; | ||
| 196 | $value->{domain} = $cookie_domain if defined $cookie_domain; | ||
| 197 | $value->{expires} = time + $cookie_expires if defined $cookie_expires; | ||
| 198 | $res->cookies({ $session_key, $value }); | ||
| 199 | }); | ||
| 200 | } | ||
| 201 | |||
| 202 | 1; |