« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/URL.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/App/Model/Email.pm »
| 1 | package PHP::HTTP::Session; | ||
| 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 | lifetime => 1440, | ||
| 20 | table => 'sessiondata', | ||
| 21 | ); | ||
| 22 | $opt ||= {}; | ||
| 23 | |||
| 24 | my $self = bless { | ||
| 25 | %default, | ||
| 26 | %$opt, | ||
| 27 | _sid => $sid, | ||
| 28 | _data => {}, | ||
| 29 | _changed => 0, | ||
| 30 | }, $class; | ||
| 31 | |||
| 32 | $self->_validate_sid; | ||
| 33 | $self->_parse_session; | ||
| 34 | |||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub id { shift->{_sid} } | ||
| 39 | sub dbh { shift->{dbh} } | ||
| 40 | sub table { shift->{table} } | ||
| 41 | |||
| 42 | sub serializer { | ||
| 43 | my $self = shift; | ||
| 44 | my $impl = $self->{serialize_handler}; | ||
| 45 | load_class($impl); | ||
| 46 | return $impl->new; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub get { | ||
| 50 | my($self, $key) = @_; | ||
| 51 | return $self->{_data}->{$key}; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub set { | ||
| 55 | my($self, $key, $value) = @_; | ||
| 56 | $self->{_changed}++; | ||
| 57 | $self->{_data}->{$key} = $value; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub unregister { | ||
| 61 | my($self, $key) = @_; | ||
| 62 | delete $self->{_data}->{$key}; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub unset { | ||
| 66 | my $self = shift; | ||
| 67 | $self->{_data} = {}; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub is_registered { | ||
| 71 | my($self, $key) = @_; | ||
| 72 | return exists $self->{_data}->{$key}; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub decode { | ||
| 76 | my($self, $data) = @_; | ||
| 77 | my $ret = eval { $self->serializer->decode($data) }; | ||
| 78 | if ($@) { | ||
| 79 | warn "Something wrong in PHP::Session::Serializer::PHP"; | ||
| 80 | $ret = $self->serializer->decode(decode_utf8($data)); | ||
| 81 | } | ||
| 82 | return $ret; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub encode { | ||
| 86 | my($self, $data) = @_; | ||
| 87 | $self->serializer->encode($data); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub save { | ||
| 91 | my $self = shift; | ||
| 92 | my $table = $self->table; | ||
| 93 | my $sth = $self->dbh->prepare("UPDATE $table SET expiry = ?, data = ? WHERE id = ?"); | ||
| 94 | $sth->execute(time + $self->{lifetime}, $self->encode($self->{_data}), md5_hex($self->id)); | ||
| 95 | |||
| 96 | $self->{_changed} = 0; # init | ||
| 97 | } | ||
| 98 | |||
| 99 | sub destroy { | ||
| 100 | my $self = shift; | ||
| 101 | my $table = $self->table; | ||
| 102 | my $sth = $self->dbh->prepare("DELETE FROM $table WHERE id = ?"); | ||
| 103 | $sth->execute(md5_hex($self->id)); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub DESTROY { | ||
| 107 | my $self = shift; | ||
| 108 | if ($self->{_changed}) { | ||
| 109 | if ($self->{auto_save}) { | ||
| 110 | $self->save; | ||
| 111 | } else { | ||
| 112 | _carp("PHP::Session: some keys are changed but not saved.") if $^W; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | sub _validate_sid { | ||
| 118 | my $self = shift; | ||
| 119 | $self->id =~ /^([0-9a-zA-Z]*)$/ | ||
| 120 | or _croak("Invalid session id: ", $self->id); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub _parse_session { | ||
| 124 | my $self = shift; | ||
| 125 | my $cont = $self->_slurp_content; | ||
| 126 | |||
| 127 | if (!$cont) { | ||
| 128 | if ($self->{create}) { | ||
| 129 | $self->{_data} = { __HTTP_Session_Info => 1 }; | ||
| 130 | my $table = $self->table; | ||
| 131 | my $sth = $self->dbh->prepare("INSERT $table (id, expiry, data) VALUES (?, ?, ?)"); | ||
| 132 | $sth->execute(md5_hex($self->id), time + $self->{lifetime}, $self->encode($self->{_data})); | ||
| 133 | } else { | ||
| 134 | _croak($self->id, ": $!"); | ||
| 135 | } | ||
| 136 | } | ||
| 137 | $self->{_data} = $self->decode($cont); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub _slurp_content { | ||
| 141 | my $self = shift; | ||
| 142 | my $table = $self->table; | ||
| 143 | my $sth = $self->dbh->prepare("SELECT * FROM $table WHERE id = ?"); | ||
| 144 | $sth->execute(md5_hex($self->id)); | ||
| 145 | |||
| 146 | my @row = $sth->fetchrow_array; | ||
| 147 | return unless @row; | ||
| 148 | |||
| 149 | # # セッションの期限を確認 | ||
| 150 | # if ($row[1] < time) { | ||
| 151 | # $self->destroy; | ||
| 152 | # return; | ||
| 153 | # } | ||
| 154 | |||
| 155 | return $row[2]; | ||
| 156 | } | ||
| 157 | |||
| 158 | package Dwarf::Plugin::PHP::HTTP::Session; | ||
| 159 | use strict; | ||
| 160 | use warnings; | ||
| 161 | use Dwarf::Util qw/add_method/; | ||
| 162 | |||
| 163 | sub init { | ||
| 164 | my ($class, $c, $conf) = @_; | ||
| 165 | $conf ||= {}; | ||
| 166 | |||
| 167 | my $dbh = $conf->{dbh} || undef; | ||
| 168 | my $table = $conf->{table} || 'sessiondata'; | ||
| 169 | my $session_key = $conf->{session_key} || 'SessionID'; | ||
| 170 | my $session_expires = $conf->{session_expires} || 1440; | ||
| 171 | my $param_name = $conf->{param_name} || $session_key; | ||
| 172 | my $use_cookie = $conf->{use_cookie} || 1; | ||
| 173 | my $cookie_path = $conf->{cookie_path} || '/'; | ||
| 174 | my $cookie_domain = $conf->{cookie_domain} || undef; | ||
| 175 | my $cookie_expires = $conf->{cookie_expires} || undef; | ||
| 176 | my $on_init = $conf->{on_init} || sub {}; | ||
| 177 | |||
| 178 | add_method($c, session => sub { | ||
| 179 | my $self = shift; | ||
| 180 | |||
| 181 | $self->{'dwarf.session'} ||= do { | ||
| 182 | my $session_id = undef; | ||
| 183 | |||
| 184 | if ($use_cookie) { | ||
| 185 | my $cookie = $self->request->cookies->{$session_key}; | ||
| 186 | |||
| 187 | if (defined $cookie and $cookie ne '') { | ||
| 188 | $session_id = $cookie; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | my $query_param = $self->request->param($param_name); | ||
| 193 | |||
| 194 | if (defined $query_param and $query_param ne '') { | ||
| 195 | $session_id = $query_param; | ||
| 196 | } | ||
| 197 | |||
| 198 | my $session = PHP::HTTP::Session->new($session_id, { | ||
| 199 | dbh => $dbh || $self->dbh, | ||
| 200 | table => $table, | ||
| 201 | create => 1, | ||
| 202 | }); | ||
| 203 | $on_init->($session); | ||
| 204 | |||
| 205 | $session; | ||
| 206 | }; | ||
| 207 | }); | ||
| 208 | |||
| 209 | return unless $use_cookie; | ||
| 210 | |||
| 211 | $c->add_trigger('AFTER_DISPATCH' => sub { | ||
| 212 | my ($self, $res) = @_; | ||
| 213 | return unless exists $self->{'dwarf.session'}; | ||
| 214 | my $value = { value => $self->session->id }; | ||
| 215 | $value->{path} = $cookie_path if defined $cookie_path; | ||
| 216 | $value->{domain} = $cookie_domain if defined $cookie_domain; | ||
| 217 | $value->{expires} = time + $cookie_expires if defined $cookie_expires; | ||
| 218 | $res->cookies({ $session_key, $value }); | ||
| 219 | }); | ||
| 220 | } | ||
| 221 | |||
| 222 | 1; |