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