import of rt 3.0.4
[freeside.git] / rt / html / Elements / SetupSessionCookie
1 %# BEGIN LICENSE BLOCK
2 %# 
3 %# Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
4 %# 
5 %# (Except where explictly superceded by other copyright notices)
6 %# 
7 %# This work is made available to you under the terms of Version 2 of
8 %# the GNU General Public License. A copy of that license should have
9 %# been provided with this software, but in any event can be snarfed
10 %# from www.gnu.org.
11 %# 
12 %# This work is distributed in the hope that it will be useful, but
13 %# WITHOUT ANY WARRANTY; without even the implied warranty of
14 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 %# General Public License for more details.
16 %# 
17 %# Unless otherwise specified, all modifications, corrections or
18 %# extensions to this work which alter its source code become the
19 %# property of Best Practical Solutions, LLC when submitted for
20 %# inclusion in the work.
21 %# 
22 %# 
23 %# END LICENSE BLOCK
24 <%init>
25 my %cookies = CGI::Cookie->fetch();
26 my %backends = (
27     mysql       => 'Apache::Session::MySQL',
28     Pg          => 'Apache::Session::Postgres',
29     Oracle      => 'Apache::Session::Oracle',
30 ) unless $RT::WebSessionClass;
31 my $session_class = $RT::WebSessionClass || $backends{$RT::DatabaseType} || 'Apache::Session::File';
32 my $pm = "$session_class.pm"; $pm =~ s|::|/|g; require $pm;
33
34     eval {
35         tie %session, $session_class,
36           $SessionCookie || ( $cookies{'RT_SID'} ? $cookies{'RT_SID'}->value() : undef ),
37           $backends{$RT::DatabaseType} ? {
38             Handle     => $RT::Handle->dbh,
39             LockHandle => $RT::Handle->dbh,
40           } : {
41             Directory     => $RT::MasonSessionDir,
42             LockDirectory => $RT::MasonSessionDir,
43           };
44     };
45     if ($@) {
46
47         # If the session is invalid, create a new session.
48         if ( $@ =~ /Object does not/i ) {
49             tie %session, $session_class, undef,
50               $backends{$RT::DatabaseType} ? {
51                 Handle     => $RT::Handle->dbh,
52                 LockHandle => $RT::Handle->dbh,
53               } : {
54                 Directory     => $RT::MasonSessionDir,
55                 LockDirectory => $RT::MasonSessionDir,
56               };
57             undef $cookies{'RT_SID'};
58         }
59         else {
60             die "RT Couldn't write to session directory '$RT::MasonSessionDir': $@. Check that this dir ectory's permissions are correct.";
61         }
62     }
63
64     if ( !$cookies{'RT_SID'} ) {
65         my $cookie = new CGI::Cookie(
66             -name  => 'RT_SID',
67             -value => $session{_session_id},
68             -path  => '/',
69         );
70         $r->header_out('Set-Cookie', $cookie->as_string);
71
72     } 
73     return();
74 </%init>
75 <%args>
76 $SessionCookie => ''
77 </%args>