This commit was generated by cvs2svn to compensate for changes in r2526,
[freeside.git] / rt / bin / mason_handler.fcgi
1 #!!!PERL!!
2 # $Header: /home/cvs/cvsroot/freeside/rt/bin/mason_handler.fcgi,v 1.1 2002-08-12 06:17:07 ivan Exp $
3 # RT is (c) 1996-2001 Jesse Vincent (jesse@fsck.com);
4
5 use strict;
6 $ENV{'PATH'} = '/bin:/usr/bin';    # or whatever you need
7 $ENV{'CDPATH'} = '' if defined $ENV{'CDPATH'};
8 $ENV{'SHELL'} = '/bin/sh' if defined $ENV{'SHELL'};
9 $ENV{'ENV'} = '' if defined $ENV{'ENV'};
10 $ENV{'IFS'} = ''          if defined $ENV{'IFS'};
11
12
13 # We really don't want apache to try to eat all vm
14 # see http://perl.apache.org/guide/control.html#Preventing_mod_perl_Processes_Fr
15
16
17 package RT::Mason;
18 #use CGI qw(-private_tempfiles);   # pull in CGI with the private tempfiles
19                                   #option predefined
20 use HTML::Mason;  # brings in subpackages: Parser, Interp, etc.
21
22 use vars qw($VERSION %session $Nobody $SystemUser $cgi);
23
24 # List of modules that you want to use from components (see Admin
25 # manual for details)
26
27 #Clean up our umask...so that the session files aren't world readable, writable or executable
28 umask(0077);
29
30
31   
32 $VERSION="!!RT_VERSION!!";
33
34 use lib "!!RT_LIB_PATH!!";
35 use lib "!!RT_ETC_PATH!!";
36
37 #This drags in  RT's config.pm
38 use config;
39 use Carp;
40
41 {  
42     package HTML::Mason::Commands;
43     use vars qw(%session $ContentType);
44     
45     use RT; 
46     use RT::Ticket;
47     use RT::Tickets;
48     use RT::Transaction;
49     use RT::Transactions;
50     use RT::User;
51     use RT::Users;
52     use RT::CurrentUser;
53     use RT::Template;
54     use RT::Templates;
55     use RT::Queue;
56     use RT::Queues;
57     use RT::ScripAction;
58     use RT::ScripActions;
59     use RT::ScripCondition;
60     use RT::ScripConditions;
61     use RT::Scrip;
62     use RT::Scrips;
63     use RT::Group;
64     use RT::Groups;
65     use RT::Keyword;
66     use RT::Keywords;
67     use RT::ObjectKeyword;
68     use RT::ObjectKeywords;
69     use RT::KeywordSelect;
70     use RT::KeywordSelects;
71     use RT::GroupMember;
72     use RT::GroupMembers;
73     use RT::Watcher;
74     use RT::Watchers;
75     use RT::Handle;
76     use RT::Interface::Web;    
77     use MIME::Entity;
78     use CGI::Cookie;
79     use Date::Parse;
80     use HTML::Entities;
81     use Text::Wrapper;
82  
83     #TODO: make this use DBI
84     use Apache::Session::File;
85     use CGI::Fast;
86
87     # set the page's content type.
88     # In this case, just save it to a variable that we can pull later;
89     sub SetContentType {
90         $ContentType = shift;
91     }
92     sub CGIObject {
93         return $RT::Mason::cgi;
94     }
95 }
96
97
98 my ($output, $parser, $interp);
99 if ($HTML::Mason::VERSION < 1.0902) {
100         require HTML::Mason::ApacheHandler;
101
102          $parser = &RT::Interface::Web::NewParser(allow_globals => [%session]);
103
104          $interp = &RT::Interface::Web::NewInterp(parser=>$parser,
105                                              allow_recursive_autohandlers => 1,
106                                             out_method => \$output);
107 }
108 else {
109          $interp = &RT::Interface::Web::NewInterp(
110                                                   allow_globals => [%session],
111                                                   default_escape_flags => 'h',
112
113                                             out_method => \$output);
114 }
115 # Die if WebSessionDir doesn't exist or we can't write to it
116
117 stat ($RT::MasonSessionDir);
118 die "Can't read and write $RT::MasonSessionDir"
119   unless (( -d _ ) and ( -r _ ) and ( -w _ ));
120
121
122 RT::Init();
123
124 # Response loop
125 while ($RT::Mason::cgi = new CGI::Fast) {
126     
127     $HTML::Mason::Commands::ContentType = 'text/html';
128         
129     # This routine comes from ApacheHandler.pm:
130     my (%args, $cookie);
131     foreach my $key ( $cgi->param ) {
132         foreach my $value ( $cgi->param($key) ) {
133             if (exists($args{$key})) {
134                 if (ref($args{$key})) {
135                     $args{$key} = [@{$args{$key}}, $value];
136                 } else {
137                     $args{$key} = [$args{$key}, $value];
138                 }
139             } else {
140                 $args{$key} = $value;
141             }
142
143         }
144         
145     }
146     
147
148     my $comp = $ENV{'PATH_INFO'};
149     
150     if ($comp =~ /^(.*)$/) {  # untaint the path info. apache should
151                               # never hand us a bogus path. 
152                               # We should be more careful here.
153         $comp = $1;
154     }    
155     
156     if ($comp =~ /\/$/) {
157         $comp .= "index.html";
158     }   
159     
160     #This is all largely cut and pasted from mason's session_handler.pl
161     
162     # {{{ Cookies
163     my %cookies = fetch CGI::Cookie();
164     
165     eval {
166         my $session_id = undef;
167
168         #Get the session id and untaint it
169         if ($cookies{'AF_SID'} && $cookies{'AF_SID'}->value() =~ /^(.*)$/) {
170                 $session_id = $1;
171         }
172  
173         tie %HTML::Mason::Commands::session, 'Apache::Session::File',
174                 $session_id, 
175             { Directory => $RT::MasonSessionDir,
176               LockDirectory => $RT::MasonSessionDir,
177             }   ;
178     };
179     
180     if ( $@ ) {
181         # If the session is invalid, create a new session.
182         if ( $@ =~ m#^Object does not exist in the data store# ) {
183              tie %HTML::Mason::Commands::session, 'Apache::Session::File', undef,
184              { Directory => $RT::MasonSessionDir,
185                LockDirectory => $RT::MasonSessionDir,
186              };
187              undef $cookies{'AF_SID'};
188         }
189           else {
190               die "$@ \nProbably means that RT Couldn't write to session directory '$RT::MasonSessionDir'. Check that this directory's permissions are correct.";
191           }
192     }
193     
194     if ( !$cookies{'AF_SID'} ) {
195         $cookie = new CGI::Cookie
196           (-name=>'AF_SID', 
197            -value=>$HTML::Mason::Commands::session{_session_id}, 
198            -path => '/',);
199         
200     } else {
201         $cookie = undef;
202     }
203     
204     # }}}
205     
206     $output = '';
207     eval {
208             my $status = $interp->exec($comp, %args);
209     };
210  
211     if ($@) {
212         $output = "<PRE>$@</PRE>";
213     }
214  
215     print "Content-Type: $HTML::Mason::Commands::ContentType\r\n";
216     print "Set-Cookie: $cookie\r\n" if ($cookie);
217     print "\r\n";
218     print $output;
219     untie %HTML::Mason::Commands::session;
220     
221 }