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