summaryrefslogtreecommitdiff
path: root/rt/bin/mason_handler.scgi
blob: b9846c8989ef4bc5ddec63f8c62d04f4314b7305 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!!!PERL!! -w

#!/usr/bin/speedy -- -t600 -M8
# $Header: /home/cvs/cvsroot/freeside/rt/bin/mason_handler.scgi,v 1.1 2002-08-12 06:17:07 ivan Exp $
# RT is (c) 1996-2001 Jesse Vincent (jesse@fsck.com);
#
# Contains code derived from mason.cgi
# mason.cgi is Copyright December 2000 Joshua Kronengold (mneme@io.com, 
# mneme@cyberspace.org).  All Rights Reserved.

use strict;
# {{{ Clean out the environment a little bit
$ENV{'PATH'} = '/bin:/usr/bin';    # or whatever you need
$ENV{'CDPATH'} = '' if defined $ENV{'CDPATH'};
$ENV{'SHELL'} = '/bin/sh' if defined $ENV{'SHELL'};
$ENV{'ENV'} = '' if defined $ENV{'ENV'};
$ENV{'IFS'} = ''          if defined $ENV{'IFS'};
# }}}

package RT::Mason;
use HTML::Mason;  # brings in subpackages: Parser, Interp, etc.
use vars qw($VERSION %session $Nobody $SystemUser);

# List of modules that you want to use from components (see Admin
# manual for details)

$VERSION="!!RT_VERSION!!";

use lib "!!RT_LIB_PATH!!";
use lib "!!RT_ETC_PATH!!";


#This drags in  RT's config.pm
use config;
use Carp;

use HTML::Mason::FakeApache;
use CGI;

# {{{ Set up CGI environment and grab CGI params:

my $r=new HTML::Mason::FakeApache;

$|=1; # set output to non-buffered.

my %cgi;
CGI::ReadParse(\%cgi); # %cgi is now a tied hash containing our params.

my $q=$cgi{CGI}; # $q now contains the object tied to %cgi.
# }}}

# {{{ require what we need
{  
    package HTML::Mason::Commands;

    use vars qw(%session);

    use RT::Ticket;
    use RT::Tickets;
    use RT::Transaction;
    use RT::Transactions;
    use RT::User;
    use RT::Users;
    use RT::CurrentUser;
    use RT::Template;
    use RT::Templates;
    use RT::Queue;
    use RT::Queues;
    use RT::ScripAction;
    use RT::ScripActions;
    use RT::ScripCondition;
    use RT::ScripConditions;
    use RT::Scrip;
    use RT::Scrips;
    use RT::Group;
    use RT::Groups;
    use RT::Keyword;
    use RT::Keywords;
    use RT::ObjectKeyword;
    use RT::ObjectKeywords;
    use RT::KeywordSelect;
    use RT::KeywordSelects;
    use RT::GroupMember;
    use RT::GroupMembers;
    use RT::Watcher;
    use RT::Watchers;
    use RT::Handle;
    use RT::Interface::Web;    
    use MIME::Entity;
    use CGI::Cookie;
    use Date::Parse;
    use HTML::Entities;

    
    use Apache::Session::File;

    
}
# }}}

# {{{ RT Database setup
    $RT::Handle = new RT::Handle;
    
    $RT::Handle->Connect;
   
    use RT::CurrentUser;
    
    #RT's system user is a genuine database user. its id lives here
    $RT::SystemUser = new RT::CurrentUser();
    $RT::SystemUser->LoadByName('RT_System');

    #RT's "nobody user" is a genuine database user. its ID lives here.
    $RT::Nobody = new RT::CurrentUser();
    $RT::Nobody->LoadByName('Nobody'); 
     

# }}}




# {{{ Deal with cookies

my %cookies = fetch CGI::Cookie();
eval { 
    tie %HTML::Mason::Commands::session, 'Apache::Session::File',
      ( $cookies{'AF_SID'} ? $cookies{'AF_SID'}->value() : undef );
};

if ( $@ ) {
    # If the session is invalid, create a new session.
    if ( $@ =~ m#^Object does not exist in the data store# ) {
	 tie %HTML::Mason::Commands::session, 'Apache::Session::File', undef;
	 undef $cookies{'AF_SID'};
    }
}

if ( !$cookies{'AF_SID'} ) {
    my $cookie = new CGI::Cookie(
 	 -name=>'AF_SID', 
	 -value=>$HTML::Mason::Commands::session{_session_id}, 
	  -path => '/');
    print 'Set-Cookie: '. $cookie."\r\n";
}

# }}}

my $path=$ENV{PATH_INFO} || "/"; $path=~s/\'/\\\'/g;

my $type=`/usr/bin/file '$RT::MasonComponentRoot/$path'`;

# {{{ if it's a text file, handle it with mason.
if($type=~/text|directory/) { 
	my ($out, %mason_params);
        my $parser = RT::Interface::Web::NewParser(allow_globals=>[qw($r)]);
	$mason_params{parser}=$parser;
	$r->content_type('text/html');
	# (get cookies line) ...
	$r->access_hash('headers_in','Cookie',$ENV{HTTP_COOKIE});
	$r->{'args@'}=[];
	$mason_params{out_method}=\$out;

	my $interp = RT::Interface::Web::NewInterp(%mason_params);

	$interp->set_global(r=>$r);
	$interp->exec($path,%cgi);
	$r->send_http_header();
        print $out;
} 
# }}} 

# {{{ if it's not a text file, just stream it out.

else { # file is binary, damn it
	my $mime_type;
	if ( $mime_type=
	     eval{ use MIME::Types;
		   my($type,$encoding)=MIME::Types::by_suffix($path);
		   $type; }) {
	    print $q->header($mime_type);	
	    $path=~s/[\|\>\<\&]//g;
	    open F,"$RT::MasonComponentRoot/$path" or 
	      die "couldn't open $path -- $!";
	    print while <F>; 
	    close F;
	} else {
	    die "couldn't resolve type of non-text file (!@; $type) -- install Mime::Types\n";
	}
    }

# }}}

untie %HTML::Mason::Commands::session;