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