3 #!/usr/bin/speedy -- -t600 -M8
4 # $Header: /home/cvs/cvsroot/freeside/rt/bin/mason_handler.scgi,v 1.1 2002-08-12 06:17:07 ivan Exp $
5 # RT is (c) 1996-2001 Jesse Vincent (jesse@fsck.com);
7 # Contains code derived from mason.cgi
8 # mason.cgi is Copyright December 2000 Joshua Kronengold (mneme@io.com,
9 # mneme@cyberspace.org). All Rights Reserved.
12 # {{{ Clean out the environment a little bit
13 $ENV{'PATH'} = '/bin:/usr/bin'; # or whatever you need
14 $ENV{'CDPATH'} = '' if defined $ENV{'CDPATH'};
15 $ENV{'SHELL'} = '/bin/sh' if defined $ENV{'SHELL'};
16 $ENV{'ENV'} = '' if defined $ENV{'ENV'};
17 $ENV{'IFS'} = '' if defined $ENV{'IFS'};
21 use HTML::Mason; # brings in subpackages: Parser, Interp, etc.
22 use vars qw($VERSION %session $Nobody $SystemUser);
24 # List of modules that you want to use from components (see Admin
27 $VERSION="!!RT_VERSION!!";
29 use lib "!!RT_LIB_PATH!!";
30 use lib "!!RT_ETC_PATH!!";
33 #This drags in RT's config.pm
37 use HTML::Mason::FakeApache;
40 # {{{ Set up CGI environment and grab CGI params:
42 my $r=new HTML::Mason::FakeApache;
44 $|=1; # set output to non-buffered.
47 CGI::ReadParse(\%cgi); # %cgi is now a tied hash containing our params.
49 my $q=$cgi{CGI}; # $q now contains the object tied to %cgi.
52 # {{{ require what we need
54 package HTML::Mason::Commands;
56 use vars qw(%session);
71 use RT::ScripCondition;
72 use RT::ScripConditions;
79 use RT::ObjectKeyword;
80 use RT::ObjectKeywords;
81 use RT::KeywordSelect;
82 use RT::KeywordSelects;
88 use RT::Interface::Web;
95 use Apache::Session::File;
101 # {{{ RT Database setup
102 $RT::Handle = new RT::Handle;
104 $RT::Handle->Connect;
108 #RT's system user is a genuine database user. its id lives here
109 $RT::SystemUser = new RT::CurrentUser();
110 $RT::SystemUser->LoadByName('RT_System');
112 #RT's "nobody user" is a genuine database user. its ID lives here.
113 $RT::Nobody = new RT::CurrentUser();
114 $RT::Nobody->LoadByName('Nobody');
122 # {{{ Deal with cookies
124 my %cookies = fetch CGI::Cookie();
126 tie %HTML::Mason::Commands::session, 'Apache::Session::File',
127 ( $cookies{'AF_SID'} ? $cookies{'AF_SID'}->value() : undef );
131 # If the session is invalid, create a new session.
132 if ( $@ =~ m#^Object does not exist in the data store# ) {
133 tie %HTML::Mason::Commands::session, 'Apache::Session::File', undef;
134 undef $cookies{'AF_SID'};
138 if ( !$cookies{'AF_SID'} ) {
139 my $cookie = new CGI::Cookie(
141 -value=>$HTML::Mason::Commands::session{_session_id},
143 print 'Set-Cookie: '. $cookie."\r\n";
148 my $path=$ENV{PATH_INFO} || "/"; $path=~s/\'/\\\'/g;
150 my $type=`/usr/bin/file '$RT::MasonComponentRoot/$path'`;
152 # {{{ if it's a text file, handle it with mason.
153 if($type=~/text|directory/) {
154 my ($out, %mason_params);
155 my $parser = RT::Interface::Web::NewParser(allow_globals=>[qw($r)]);
156 $mason_params{parser}=$parser;
157 $r->content_type('text/html');
158 # (get cookies line) ...
159 $r->access_hash('headers_in','Cookie',$ENV{HTTP_COOKIE});
161 $mason_params{out_method}=\$out;
163 my $interp = RT::Interface::Web::NewInterp(%mason_params);
165 $interp->set_global(r=>$r);
166 $interp->exec($path,%cgi);
167 $r->send_http_header();
172 # {{{ if it's not a text file, just stream it out.
174 else { # file is binary, damn it
177 eval{ use MIME::Types;
178 my($type,$encoding)=MIME::Types::by_suffix($path);
180 print $q->header($mime_type);
181 $path=~s/[\|\>\<\&]//g;
182 open F,"$RT::MasonComponentRoot/$path" or
183 die "couldn't open $path -- $!";
187 die "couldn't resolve type of non-text file (!@; $type) -- install Mime::Types\n";
193 untie %HTML::Mason::Commands::session;