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