import of rt 3.0.9
[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 return if $m->is_subrequest; # avoid reentrancy, as suggested by masonbook
26
27 my %cookies = CGI::Cookie->fetch();
28 my $cookiename = "RT_SID_".$RT::rtname.".".$ENV{'SERVER_PORT'};
29 my %backends = (
30     mysql       => 'Apache::Session::MySQL',
31     Pg          => 'Apache::Session::Postgres',
32 #    Oracle     => 'Apache::Session::Oracle',
33 ) unless $RT::WebSessionClass;
34 my $session_class = $RT::WebSessionClass || $backends{$RT::DatabaseType} || 'Apache::Session::File';
35 my $pm = "$session_class.pm"; $pm =~ s|::|/|g; require $pm;
36
37     # morning bug avoidance attempt -- pdh 20030815
38     unless ($RT::Handle->dbh && $RT::Handle->dbh->ping) {
39         $RT::Handle->Connect();
40     }
41     eval {
42         tie %session, $session_class,
43           $SessionCookie || ( $cookies{$cookiename} ? $cookies{$cookiename}->value() : undef ),
44           $backends{$RT::DatabaseType} ? {
45             Handle     => $RT::Handle->dbh,
46             LockHandle => $RT::Handle->dbh,
47           } : {
48             Directory     => $RT::MasonSessionDir,
49             LockDirectory => $RT::MasonSessionDir,
50           };
51     };
52     if ($@) {
53
54         # If the session is invalid, create a new session.
55         if ( $@ =~ /Object does not/i ) {
56             tie %session, $session_class, undef,
57               $backends{$RT::DatabaseType} ? {
58                 Handle     => $RT::Handle->dbh,
59                 LockHandle => $RT::Handle->dbh,
60               } : {
61                 Directory     => $RT::MasonSessionDir,
62                 LockDirectory => $RT::MasonSessionDir,
63               };
64             undef $cookies{$cookiename};
65         }
66         else {
67             die "RT Couldn't write to session directory '$RT::MasonSessionDir': $@. Check that this dir ectory's permissions are correct.";
68         }
69     }
70
71     if ( !$cookies{$cookiename} ) {
72         my $cookie = new CGI::Cookie(
73             -name  => $cookiename,
74             -value => $session{_session_id},
75             -path  => '/',
76         );
77         $r->header_out('Set-Cookie', $cookie->as_string);
78
79     } 
80
81     return();
82 </%init>
83 <%args>
84 $SessionCookie => ''
85 </%args>