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