« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/CSRFDefender.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Module/SocialMedia/Rakuten.pm »
| 1 | package Dwarf::Plugin::PHP::Session; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use PHP::Session; | ||
| 5 | use Dwarf::Util qw/add_method/; | ||
| 6 | |||
| 7 | sub init { | ||
| 8 | my ($class, $c, $conf) = @_; | ||
| 9 | $conf ||= {}; | ||
| 10 | |||
| 11 | my $session_key = $conf->{session_key} || 'PHPSESSID'; | ||
| 12 | my $param_name = $conf->{param_name} || $session_key; | ||
| 13 | my $use_cookie = $conf->{use_cookie} || 1; | ||
| 14 | my $cookie_path = $conf->{cookie_path} || '/'; | ||
| 15 | my $cookie_domain = $conf->{cookie_domain} || undef; | ||
| 16 | my $cookie_expires = $conf->{cookie_expires} || undef; | ||
| 17 | my $on_init = $conf->{on_init} || sub {}; | ||
| 18 | |||
| 19 | add_method($c, session => sub { | ||
| 20 | my $self = shift; | ||
| 21 | |||
| 22 | $self->{'dwarf.session'} ||= do { | ||
| 23 | my $session_id = undef; | ||
| 24 | |||
| 25 | if ($use_cookie) { | ||
| 26 | my $cookie = $self->request->cookies->{$session_key}; | ||
| 27 | |||
| 28 | if (defined $cookie and $cookie ne '') { | ||
| 29 | $session_id = $cookie; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | my $query_param = $self->request->param($param_name); | ||
| 34 | |||
| 35 | if (defined $query_param and $query_param ne '') { | ||
| 36 | $session_id = $query_param; | ||
| 37 | } | ||
| 38 | |||
| 39 | my $session = PHP::Session->new($session_id); | ||
| 40 | $on_init->($session); | ||
| 41 | |||
| 42 | $session; | ||
| 43 | }; | ||
| 44 | }); | ||
| 45 | |||
| 46 | return unless $use_cookie; | ||
| 47 | |||
| 48 | $c->add_trigger('AFTER_DISPATCH' => sub { | ||
| 49 | my ($self, $res) = @_; | ||
| 50 | return unless exists $self->{'dwarf.session'}; | ||
| 51 | my $value = { value => $self->session->id }; | ||
| 52 | $value->{path} = $cookie_path if defined $cookie_path; | ||
| 53 | $value->{domain} = $cookie_domain if defined $cookie_domain; | ||
| 54 | $value->{expires} = time + $cookie_expires if defined $cookie_expires; | ||
| 55 | $res->cookies({ $session_key, $value }); | ||
| 56 | }); | ||
| 57 | } | ||
| 58 | |||
| 59 | 1; |