3c68e83ed7da4e2bd6c9d8a935ea506cb5910564
[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
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::Trace->log('handling request');
140     my %session;
141     my $status;
142     eval { $status = $ah->handle_request($r); };
143 #!!
144 #    if ( $@ ) {
145 #       $RT::Logger->crit($@);
146 #    }
147     warn $@ if $@;
148
149     undef %session;
150
151 #!!
152 #    if ($RT::Handle->TransactionDepth) {
153 #       $RT::Handle->ForceRollback;
154 #       $RT::Logger->crit(
155 #"Transaction not committed. Usually indicates a software fault. Data loss may have occurred"
156 #       );
157 #    }
158
159     FS::Trace->log('done');
160
161     FS::Trace->dumpfile( "%%%FREESIDE_EXPORT%%%/profile/$$.".time,
162                          FS::Trace->total. ' '. $r->filename
163                        )
164       if FS::Trace->total > 5; #10?
165
166     FS::Trace->reset;
167
168     $status;
169 }
170
171 sub my_rt_init {
172   return unless $RT::VERSION;
173   RT::ConnectToDatabase();
174   RT::InitSignalHandlers();
175 }
176
177 1;