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