This commit was generated by cvs2svn to compensate for changes in r2523,
[freeside.git] / rt / bin / mason_handler.scgi
1 #!!!PERL!! -w
2
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);
6 #
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.
10
11 use strict;
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'};
18 # }}}
19
20 package RT::Mason;
21 use HTML::Mason;  # brings in subpackages: Parser, Interp, etc.
22 use vars qw($VERSION %session $Nobody $SystemUser);
23
24 # List of modules that you want to use from components (see Admin
25 # manual for details)
26
27 $VERSION="!!RT_VERSION!!";
28
29 use lib "!!RT_LIB_PATH!!";
30 use lib "!!RT_ETC_PATH!!";
31
32
33 #This drags in  RT's config.pm
34 use config;
35 use Carp;
36
37 use HTML::Mason::FakeApache;
38 use CGI;
39
40 # {{{ Set up CGI environment and grab CGI params:
41
42 my $r=new HTML::Mason::FakeApache;
43
44 $|=1; # set output to non-buffered.
45
46 my %cgi;
47 CGI::ReadParse(\%cgi); # %cgi is now a tied hash containing our params.
48
49 my $q=$cgi{CGI}; # $q now contains the object tied to %cgi.
50 # }}}
51
52 # {{{ require what we need
53 {  
54     package HTML::Mason::Commands;
55
56     use vars qw(%session);
57
58     use RT::Ticket;
59     use RT::Tickets;
60     use RT::Transaction;
61     use RT::Transactions;
62     use RT::User;
63     use RT::Users;
64     use RT::CurrentUser;
65     use RT::Template;
66     use RT::Templates;
67     use RT::Queue;
68     use RT::Queues;
69     use RT::ScripAction;
70     use RT::ScripActions;
71     use RT::ScripCondition;
72     use RT::ScripConditions;
73     use RT::Scrip;
74     use RT::Scrips;
75     use RT::Group;
76     use RT::Groups;
77     use RT::Keyword;
78     use RT::Keywords;
79     use RT::ObjectKeyword;
80     use RT::ObjectKeywords;
81     use RT::KeywordSelect;
82     use RT::KeywordSelects;
83     use RT::GroupMember;
84     use RT::GroupMembers;
85     use RT::Watcher;
86     use RT::Watchers;
87     use RT::Handle;
88     use RT::Interface::Web;    
89     use MIME::Entity;
90     use CGI::Cookie;
91     use Date::Parse;
92     use HTML::Entities;
93
94     
95     use Apache::Session::File;
96
97     
98 }
99 # }}}
100
101 # {{{ RT Database setup
102     $RT::Handle = new RT::Handle;
103     
104     $RT::Handle->Connect;
105    
106     use RT::CurrentUser;
107     
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');
111
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'); 
115      
116
117 # }}}
118
119
120
121
122 # {{{ Deal with cookies
123
124 my %cookies = fetch CGI::Cookie();
125 eval { 
126     tie %HTML::Mason::Commands::session, 'Apache::Session::File',
127       ( $cookies{'AF_SID'} ? $cookies{'AF_SID'}->value() : undef );
128 };
129
130 if ( $@ ) {
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'};
135     }
136 }
137
138 if ( !$cookies{'AF_SID'} ) {
139     my $cookie = new CGI::Cookie(
140          -name=>'AF_SID', 
141          -value=>$HTML::Mason::Commands::session{_session_id}, 
142           -path => '/');
143     print 'Set-Cookie: '. $cookie."\r\n";
144 }
145
146 # }}}
147
148 my $path=$ENV{PATH_INFO} || "/"; $path=~s/\'/\\\'/g;
149
150 my $type=`/usr/bin/file '$RT::MasonComponentRoot/$path'`;
151
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});
160         $r->{'args@'}=[];
161         $mason_params{out_method}=\$out;
162
163         my $interp = RT::Interface::Web::NewInterp(%mason_params);
164
165         $interp->set_global(r=>$r);
166         $interp->exec($path,%cgi);
167         $r->send_http_header();
168         print $out;
169
170 # }}} 
171
172 # {{{ if it's not a text file, just stream it out.
173
174 else { # file is binary, damn it
175         my $mime_type;
176         if ( $mime_type=
177              eval{ use MIME::Types;
178                    my($type,$encoding)=MIME::Types::by_suffix($path);
179                    $type; }) {
180             print $q->header($mime_type);       
181             $path=~s/[\|\>\<\&]//g;
182             open F,"$RT::MasonComponentRoot/$path" or 
183               die "couldn't open $path -- $!";
184             print while <F>; 
185             close F;
186         } else {
187             die "couldn't resolve type of non-text file (!@; $type) -- install Mime::Types\n";
188         }
189     }
190
191 # }}}
192
193 untie %HTML::Mason::Commands::session;