18108ab11e6fd77f6cd8d26a0b177d84b380d314
[freeside.git] / htetc / handler.pl
1 #!/usr/bin/perl
2
3 package HTML::Mason;
4
5 use strict;
6 use warnings;
7 use FS::Mason qw( mason_interps );
8 use FS::Trace;
9 use FS::Conf;
10
11 $FS::Conf::conf_cache_enabled = 1; # enable FS::Conf caching for performance
12
13 if ( %%%RT_ENABLED%%% ) {
14
15   require RT;
16
17   $> = scalar(getpwnam('freeside'));
18
19   RT::LoadConfig();
20   RT::Init();
21
22   # disconnect DB before fork:
23   #   (avoid 'prepared statement "dbdpg_p\d+_\d+" already exists' errors?)
24   $RT::Handle->dbh(undef);
25   undef $RT::Handle;
26
27   $> = $<;
28 }
29
30 #use vars qw($r);
31
32 # Bring in ApacheHandler, necessary for mod_perl integration.
33 # Uncomment the second line (and comment the first) to use
34 # Apache::Request instead of CGI.pm to parse arguments.
35 use HTML::Mason::ApacheHandler;
36 # use HTML::Mason::ApacheHandler (args_method=>'mod_perl');
37
38 ###use Module::Refresh;###
39
40 # Create Mason objects
41
42 my( $fs_interp, $rt_interp ) = mason_interps('apache');
43
44 my $ah = new HTML::Mason::ApacheHandler (
45   interp        => $fs_interp,
46   request_class => 'FS::Mason::Request',
47   args_method   => 'CGI', #(and FS too)
48 );
49
50 # Activate the following if running httpd as root (the normal case).
51 # Resets ownership of all files created by Mason at startup.
52 #
53 #chown (Apache->server->uid, Apache->server->gid, $interp->files_written);
54
55 my $protect_fds;
56
57 sub handler
58 {
59     #($r) = @_;
60     my $r = shift;
61
62     FS::Trace->log('protecting fds');
63
64     #from rt/bin/webmux.pl(.in)
65     if ( !$protect_fds && $ENV{'MOD_PERL'} && exists $ENV{'MOD_PERL_API_VERSION'}
66         && $ENV{'MOD_PERL_API_VERSION'} >= 2
67     ) {
68         # under mod_perl2, STDIN and STDOUT get closed and re-opened,
69         # however they are not on FD 0 and 1.  In this case, the next
70         # socket that gets opened will occupy one of these FDs, and make
71         # all system() and open "|-" calls dangerous; for example, the
72         # DBI handle can get this FD, which later system() calls will
73         # close by putting garbage into the socket.
74         $protect_fds = [];
75         push @{$protect_fds}, IO::Handle->new_from_fd(0, "r")
76             if fileno(STDIN) != 0;
77         push @{$protect_fds}, IO::Handle->new_from_fd(1, "w")
78             if fileno(STDOUT) != 1;
79     }
80
81     # If you plan to intermix images in the same directory as
82     # components, activate the following to prevent Mason from
83     # evaluating image files as components.
84     #
85     #return -1 if $r->content_type && $r->content_type !~ m|^text/|i;
86
87     ###Module::Refresh->refresh;###
88
89     FS::Trace->log('setting content_type / headers');
90
91     $r->content_type('text/html; charset=utf-8');
92     #$r->content_type('text/html; charset=iso-8859-1');
93     #eorar
94
95     my $headers = $r->headers_out;
96     $headers->{'Cache-control'} = 'no-cache';
97     #$r->no_cache(1);
98     $headers->{'Expires'} = '0';
99
100 #    $r->send_http_header;
101
102     if ( $r->filename =~ /\/rt\// ) { #RT
103
104       FS::Trace->log('handling RT file');
105
106       # We don't need to handle non-text, non-xml items
107       return -1 if defined( $r->content_type )
108                 && $r->content_type !~ m!(^text/|\bxml\b)!io;
109
110
111       local $SIG{__WARN__};
112       local $SIG{__DIE__};
113
114       FS::Trace->log('initializing RT');
115       my_rt_init();
116
117       FS::Trace->log('setting RT interpreter');
118       $ah->interp($rt_interp);
119
120     } else {
121
122       FS::Trace->log('handling Freeside file');
123
124       local $SIG{__WARN__};
125       local $SIG{__DIE__};
126
127       FS::Trace->log('initializing RT');
128       my_rt_init();
129
130       #we don't want the RT error handlers under FS
131       {
132         no warnings 'uninitialized';
133         undef($SIG{__WARN__}) if defined($SIG{__WARN__});
134         undef($SIG{__DIE__})  if defined($SIG{__DIE__} );
135       }
136
137       FS::Trace->log('setting Freeside interpreter');
138       $ah->interp($fs_interp);
139
140     }
141
142     FS::Trace->log('handling request');
143     my %session;
144     my $status;
145     eval { $status = $ah->handle_request($r); };
146 #!!
147 #    if ( $@ ) {
148 #       $RT::Logger->crit($@);
149 #    }
150     warn $@ if $@;
151
152     undef %session;
153
154 #!!
155 #    if ($RT::Handle->TransactionDepth) {
156 #       $RT::Handle->ForceRollback;
157 #       $RT::Logger->crit(
158 #"Transaction not committed. Usually indicates a software fault. Data loss may have occurred"
159 #       );
160 #    }
161
162     FS::Trace->log('done');
163
164     FS::Trace->dumpfile( "%%%FREESIDE_EXPORT%%%/profile/$$.".time,
165                          FS::Trace->total. ' '. $r->filename
166                        )
167       if FS::Trace->total > 5; #10?
168
169     FS::Trace->reset;
170
171     $status;
172 }
173
174 sub my_rt_init {
175   return unless $RT::VERSION;
176   RT::ConnectToDatabase();
177   RT::InitSignalHandlers();
178 }
179
180 1;