1e2555a76e9a284e6a3e70b375ad8ff2dba0a759
[freeside.git] / FS / FS / Mason / Request.pm
1 package FS::Mason::Request;
2
3 use strict;
4 use warnings;
5 use vars qw( $FSURL $QUERY_STRING );
6 use base 'HTML::Mason::Request';
7 use FS::Trace;
8
9 $FSURL = 'http://Set/FS_Mason_Request_FSURL/in_standalone_mode/';
10 $QUERY_STRING = '';
11
12 sub new {
13     my $class = shift;
14
15     FS::Trace->log('creating new FS::Mason::Request object');
16
17     my $superclass = $HTML::Mason::ApacheHandler::VERSION ?
18                      'HTML::Mason::Request::ApacheHandler' :
19                      $HTML::Mason::CGIHandler::VERSION ?
20                      'HTML::Mason::Request::CGI' :
21                      'HTML::Mason::Request';
22
23     FS::Trace->log('  altering superclass');
24     $class->alter_superclass( $superclass );
25
26     FS::Trace->log('  setting valid params');
27     #huh... shouldn't alter_superclass take care of this for us?
28     __PACKAGE__->valid_params( %{ $superclass->valid_params() } );
29
30     FS::Trace->log('  freeside_setup');
31     my %opt = @_;
32     my $mode = $superclass =~ /Apache/i ? 'apache' : 'standalone';
33     $class->freeside_setup($opt{'comp'}, $mode);
34
35     FS::Trace->log('  SUPER::new');
36     $class->SUPER::new(@_);
37
38 }
39
40 #override alter_superclass ala RT::Interface::Web::Request ??
41 # for Mason 1.39 vs. Perl 5.10.0
42
43 my $protect_fds;
44
45 sub freeside_setup {
46     my( $class, $filename, $mode ) = @_;
47
48     FS::Trace->log('    protecting fds');
49
50     #from rt/bin/webmux.pl(.in)
51     if ( !$protect_fds && $ENV{'MOD_PERL'} && exists $ENV{'MOD_PERL_API_VERSION'}
52         && $ENV{'MOD_PERL_API_VERSION'} >= 2
53     ) {
54         # under mod_perl2, STDIN and STDOUT get closed and re-opened,
55         # however they are not on FD 0 and 1.  In this case, the next
56         # socket that gets opened will occupy one of these FDs, and make
57         # all system() and open "|-" calls dangerous; for example, the
58         # DBI handle can get this FD, which later system() calls will
59         # close by putting garbage into the socket.
60         $protect_fds = [];
61         push @{$protect_fds}, IO::Handle->new_from_fd(0, "r")
62             if fileno(STDIN) != 0;
63         push @{$protect_fds}, IO::Handle->new_from_fd(1, "w")
64             if fileno(STDOUT) != 1;
65     }
66
67     if ( $filename =~ qr(/REST/\d+\.\d+/NoAuth/) ) {
68
69       FS::Trace->log('    handling RT REST/NoAuth file');
70
71       package HTML::Mason::Commands; #?
72       use FS::UID qw( adminsuidsetup setcgi );
73
74       #need to log somebody in for the mail gw
75
76       ##old installs w/fs_selfs or selfserv??
77       #&adminsuidsetup('fs_selfservice');
78
79       FS::Trace->log('    adminsuidsetup fs_queue');
80       &adminsuidsetup('fs_queue');
81
82     } else {
83
84       FS::Trace->log('    handling regular file');
85
86       package HTML::Mason::Commands;
87       use vars qw( $cgi $p $fsurl ); # $lh ); #not using /mt
88       use Encode;
89       #use FS::UID qw( cgisuidsetup );
90       use FS::CGI qw( popurl rooturl );
91
92       if ( $mode eq 'apache' ) {
93         $cgi = new CGI;
94         setcgi($cgi);
95
96         #cgisuidsetup is gone, adminsuidsetup is now done in AuthCookieHandler
97
98         $fsurl = rooturl();
99         $p = popurl(2);
100       } elsif ( $mode eq 'standalone' ) {
101         $cgi = new CGI $FS::Mason::Request::QUERY_STRING; #better keep setting
102                                                           #if you set it once
103         $FS::UID::cgi = $cgi;
104         $fsurl = $FS::Mason::Request::FSURL; #kludgy, but what the hell
105         $p = popurl(2, "$fsurl$filename");
106       } else {
107         die "unknown mode $mode";
108       }
109
110       FS::Trace->log('    UTF-8-decoding form data');
111       #
112       foreach my $param ( $cgi->param ) {
113         my @values = $cgi->param($param);
114         next if $cgi->uploadInfo($values[0]);
115         #warn $param;
116         @values = map decode(utf8=>$_), @values;
117         $cgi->param($param, @values);
118       }
119
120     }
121
122     FS::Trace->log('    done');
123
124 }
125
126 sub callback {
127   RT::Interface::Web::Request::callback(@_);
128 }
129
130 sub request_path {
131   RT::Interface::Web::Request::request_path(@_);
132 }
133
134 1;