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