5 use BSE::CfgInfo qw/custom_class/;
7 our $VERSION = "1.002";
12 my $class = _session_class($cfg);
21 return $cfg->entry('basic', 'session_class', "Apache::Session::MySQL");
24 sub _send_session_cookie {
25 my ($self, $session, $cfg) = @_;
27 my $debug = $cfg->entry('debug', 'cookies');
29 my $cookie_name = $cfg->entry('basic', 'cookie_name', 'sessionid');
31 if ($cfg->entry("basic", "http_only_session", 1)) {
32 $extras{httponly} = 1;
34 if ($cfg->entry("basic", "secure_session")) {
37 my $cookie = $self->make_cookie($cfg, $cookie_name => $session->{_session_id}, \%extras);
38 BSE::Session->send_cookie($cookie);
40 print STDERR "Sent cookie: $cookie\n" if $debug;
42 my $custom = custom_class($cfg);
43 if ($custom->can('send_session_cookie')) {
44 $custom->send_session_cookie($cookie_name, $session, $session->{_session_id}, $cfg);
49 my ($self, $session, $cfg) = @_;
51 my $require = _session_require($cfg);
54 my $cookie_name = $cfg->entry('basic', 'cookie_name', 'sessionid');
55 my $lifetime = $cfg->entry('basic', 'cookie_lifetime') || '+3h';
56 my $debug = $cfg->entry('debug', 'cookies');
57 my %cookies = fetch CGI::Cookie;
60 print STDERR "Received cookies: ", Data::Dumper::Dumper(\%cookies);
63 $sessionid = $cookies{$cookie_name}->value if exists $cookies{$cookie_name};
65 my $dh = BSE::DB->single;
67 tie %$session, _session_class($cfg), $sessionid,
70 LockHandle=>$dh->{dbh}
73 print STDERR "Error getting session: $@\n" if $@ && $debug;
74 if ($@ && $@ =~ /Object does not exist/) {
77 tie %$session, _session_class($cfg), $sessionid,
80 LockHandle=>$dh->{dbh}
84 # save the new sessionid
85 $self->_send_session_cookie($session, $cfg);
88 if ($cfg->entry('debug', 'dump_session')) {
90 print STDERR Data::Dumper->Dump([ $session ], [ 'session' ]);
95 my ($self, $session, $cfg, $sessionid, $newsession) = @_;
97 #my $cookie_name = $cfg->entry('basic', 'cookie_name', 'sessionid');
98 #BSE::Session->send_cookie($self->make_cookie($cfg, $cookie_name, $sessionid));
99 my $dh = BSE::DB->single;
101 tie %$newsession, _session_class($cfg), $sessionid,
104 LockHandle=>$dh->{dbh}
108 $self->_send_session_cookie($newsession, $cfg);
112 my ($self, $cfg, $name, $value, $extras) = @_;
115 $extras->{lifetime} ||= $cfg->entry('basic', 'cookie_lifetime') || '+3h';
116 $name = $cfg->entry('cookie names', $name, $name);
122 map {; "-$_" => $extras->{$_} } keys %$extras,
124 my $domain = $ENV{HTTP_HOST};
125 $domain =~ s/:\d+$//;
126 $domain = $cfg->entry('basic', 'cookie_domain', $domain);
127 if ($domain !~ /^\d+\.\d+\.\d+\.\d+$/) {
128 $opts{"-domain"} = $domain;
131 return CGI::Cookie->new(%opts);
135 my ($class, $cookie) = @_;
137 if (exists $ENV{GATEWAY_INTERFACE}
138 && $ENV{GATEWAY_INTERFACE} =~ /^CGI-Perl\//) {
139 my $r = Apache->request or die;
140 $r->header_out('Set-Cookie' => "$cookie");
143 print "Set-Cookie: $cookie\n";
148 my ($class, $session) = @_;
150 my $tie = tied(%$session);
160 BSE::Session - wrapper around Apache::Session for BSE.
167 my $cfg = BSE::Cfg->new;
168 BSE::Session->tie_it(\%session, $cfg);
170 BSE::Session->clear($session);
174 Provides a thinnish wrapper around Apache::Session, providing the interface
175 to BSE's database abstraction, configuration, retries and cookie setup.
183 cart - the customer's shopping cart, should only be set on the secure side
187 custom - custom values set by shopping cart processing, should only be
188 set on the secure side
192 userid - id of the logged on normal user.
196 adminuserid - id of the logged on admin user.
200 affiliate_code - id of the affiliate set by affiliate.pl
206 Tony Cook <tony@develop-help.com>