dcbe72733e7d145864f443674ea4afc03ca1149d
[freeside.git] / htetc / handler.pl
1 #!/usr/bin/perl
2 #
3 # This is a basic, fairly fuctional Mason handler.pl.
4 #
5 # For something a little more involved, check out session_handler.pl
6
7 package HTML::Mason;
8
9 # Bring in main Mason package.
10 use HTML::Mason 1.1;
11
12 # Bring in ApacheHandler, necessary for mod_perl integration.
13 # Uncomment the second line (and comment the first) to use
14 # Apache::Request instead of CGI.pm to parse arguments.
15 use HTML::Mason::ApacheHandler;
16 # use HTML::Mason::ApacheHandler (args_method=>'mod_perl');
17
18 # Uncomment the next line if you plan to use the Mason previewer.
19 #use HTML::Mason::Preview;
20
21 use strict;
22
23 ###use Module::Refresh;###
24
25 # List of modules that you want to use from components (see Admin
26 # manual for details)
27 #{  package HTML::Mason::Commands;
28 #   use CGI;
29 #}
30
31 # Create Mason objects
32 #
33
34 #my $parser = new HTML::Mason::Parser;
35 #my $interp = new HTML::Mason::Interp (parser=>$parser,
36 #                                      comp_root=>'/var/www/masondocs',
37 #                                      data_dir=>'/usr/local/etc/freeside/masondata',
38 #                                      out_mode=>'stream',
39 #                                     );
40
41 use vars qw($r);
42
43 if ( %%%RT_ENABLED%%% ) {
44  eval '
45    use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
46    use RT;
47    use vars qw($Nobody $SystemUser);
48    RT::LoadConfig();
49  ';
50  die $@ if $@;
51
52
53 }
54
55
56 my $ah = new HTML::Mason::ApacheHandler (
57   #interp => $interp,
58   #auto_send_headers => 0,
59   comp_root=> [
60                 [ 'freeside' => '%%%FREESIDE_DOCUMENT_ROOT%%%'    ],
61                 [ 'rt'       => '%%%FREESIDE_DOCUMENT_ROOT%%%/rt' ],
62               ],
63   data_dir=>'/usr/local/etc/freeside/masondata',
64   #out_mode=>'stream',
65
66   #RT
67   args_method => 'CGI',
68   default_escape_flags => 'h',
69   allow_globals => [qw(%session)],
70   #autoflush => 1,
71 );
72
73 # Activate the following if running httpd as root (the normal case).
74 # Resets ownership of all files created by Mason at startup.
75 #
76 #chown (Apache->server->uid, Apache->server->gid, $interp->files_written);
77
78 sub handler
79 {
80     ($r) = @_;
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     #rar
89     { package HTML::Mason::Commands;
90       use strict;
91       use vars qw( $cgi $p $fsurl);
92       use vars qw( %session );
93       use CGI 2.47 qw(-private_tempfiles);
94       #use CGI::Carp qw(fatalsToBrowser);
95       use Date::Format;
96       use Date::Parse;
97       use Time::Local;
98       use Time::Duration;
99       use Tie::IxHash;
100       use URI::Escape;
101       use HTML::Entities;
102       use JSON;
103       use IO::Handle;
104       use IO::File;
105       use IO::Scalar;
106       use Net::Whois::Raw qw(whois);
107       if ( $] < 5.006 ) {
108         eval "use Net::Whois::Raw 0.32 qw(whois)";
109         die $@ if $@;
110       }
111       use Text::CSV_XS;
112       use Spreadsheet::WriteExcel;
113       use Business::CreditCard;
114       use String::Approx qw(amatch);
115       use Chart::LinesPoints;
116       use HTML::Widgets::SelectLayers 0.05;
117       use Locale::Country;
118       use FS;
119       use FS::UID qw(cgisuidsetup dbh getotaker datasrc driver_name);
120       use FS::Record qw(qsearch qsearchs fields dbdef);
121       use FS::Conf;
122       use FS::CGI qw(header menubar popurl rooturl table itable ntable idiot
123                      eidiot small_custview myexit http_header);
124       use FS::UI::Web;
125       use FS::Msgcat qw(gettext geterror);
126       use FS::Misc qw( send_email send_fax states_hash state_label );
127       use FS::Report::Table::Monthly;
128       use FS::TicketSystem;
129
130       use FS::agent;
131       use FS::agent_type;
132       use FS::domain_record;
133       use FS::cust_bill;
134       use FS::cust_bill_pay;
135       use FS::cust_credit;
136       use FS::cust_credit_bill;
137       use FS::cust_main qw(smart_search);
138       use FS::cust_main_county;
139       use FS::cust_pay;
140       use FS::cust_pkg;
141       use FS::cust_refund;
142       use FS::cust_svc;
143       use FS::nas;
144       use FS::part_bill_event;
145       use FS::part_pkg;
146       use FS::part_referral;
147       use FS::part_svc;
148       use FS::part_svc_router;
149       use FS::part_virtual_field;
150       use FS::pkg_svc;
151       use FS::port;
152       use FS::queue qw(joblisting);
153       use FS::raddb;
154       use FS::session;
155       use FS::svc_acct;
156       use FS::svc_acct_pop qw(popselector);
157       use FS::svc_domain;
158       use FS::svc_forward;
159       use FS::svc_www;
160       use FS::router;
161       use FS::addr_block;
162       use FS::svc_broadband;
163       use FS::svc_external;
164       use FS::type_pkgs;
165       use FS::part_export;
166       use FS::part_export_option;
167       use FS::export_svc;
168       use FS::msgcat;
169       use FS::rate;
170       use FS::rate_region;
171       use FS::rate_prefix;
172       use FS::payment_gateway;
173       use FS::agent_payment_gateway;
174       use FS::XMLRPC;
175       use FS::payby;
176       use FS::cdr;
177       use FS::inventory_class;
178       use FS::inventory_item;
179       use FS::pkg_class;
180
181       if ( %%%RT_ENABLED%%% ) {
182         eval '
183           use RT::Tickets;
184           use RT::Transactions;
185           use RT::Users;
186           use RT::CurrentUser;
187           use RT::Templates;
188           use RT::Queues;
189           use RT::ScripActions;
190           use RT::ScripConditions;
191           use RT::Scrips;
192           use RT::Groups;
193           use RT::GroupMembers;
194           use RT::CustomFields;
195           use RT::CustomFieldValues;
196           use RT::ObjectCustomFieldValues;
197
198           use RT::Interface::Web;
199           use MIME::Entity;
200           use Text::Wrapper;
201           use CGI::Cookie;
202           use Time::ParseDate;
203           use HTML::Scrubber;
204           use Text::Quoted;
205         ';
206         die $@ if $@;
207       }
208
209       *CGI::redirect = sub {
210         my( $self, $location ) = @_;
211         use vars qw($m);
212
213         # false laziness w/below
214         if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
215
216           my $page =
217             qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
218             '<BR><BR><PRE>'.
219               ( UNIVERSAL::can(dbh, 'sprintProfile')
220                   ? encode_entities(dbh->sprintProfile())
221                   : 'DBIx::Profile missing sprintProfile method;'.
222                     'unpatched or too old?'                        ).
223             #"\n\n". &sprintAutoProfile().  '</PRE>'.
224             "\n\n".                         '</PRE>'.
225             '</BODY></HTML>';
226           dbh->{'private_profile'} = {};
227           return $page;
228
229         } else { #normal redirect
230
231           $m->redirect($location);
232           '';
233
234         }
235
236       };
237       
238       unless ( $HTML::Mason::r->filename =~ /\/rt\/.*NoAuth/ ) { #RT
239         $cgi = new CGI;
240         &cgisuidsetup($cgi);
241         #&cgisuidsetup($r);
242         $p = popurl(2);
243         $fsurl = rooturl();
244       }
245
246       sub include {
247         use vars qw($m);
248         $m->scomp(@_);
249       }
250
251       sub redirect {
252         my( $location ) = @_;
253         use vars qw($m);
254         $m->clear_buffer;
255         #false laziness w/above
256         if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
257
258           $m->print(
259             qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
260             '<BR><BR><PRE>'.
261               ( UNIVERSAL::can(dbh, 'sprintProfile')
262                   ? encode_entities(dbh->sprintProfile())
263                   : 'DBIx::Profile missing sprintProfile method;'.
264                     'unpatched or too old?'                        ).
265             #"\n\n". &sprintAutoProfile().  '</PRE>'.
266             "\n\n".                         '</PRE>'.
267             '</BODY></HTML>'
268           );
269           dbh->{'private_profile'} = {};
270
271           #whew.  removing this is all that's needed to fix the annoying
272           #blank-page-instead-of-profiling-redirect-when-called-from-an-include
273           #bug triggered by mason 1.32
274           #my $rv = $m->abort(200);
275
276         } else { #normal redirect
277
278           $m->redirect($location);
279
280         }
281
282       }
283
284     } # end package HTML::Mason::Commands;
285
286     ###Module::Refresh->refresh;###
287
288     $r->content_type('text/html');
289     #eorar
290
291     my $headers = $r->headers_out;
292     $headers->{'Cache-control'} = 'no-cache';
293     #$r->no_cache(1);
294     $headers->{'Expires'} = '0';
295
296 #    $r->send_http_header;
297
298     #$ah->interp->remove_escape('h');
299
300     if ( $r->filename =~ /\/rt\// ) { #RT
301       #warn "processing RT file". $r->filename. "; escaping for RT\n";
302
303       # MasonX::Request::ExtendedCompRoot
304       #$ah->interp->comp_root( '/rt'. $ah->interp->comp_root() );
305
306       $ah->interp->set_escape( h => \&RT::Interface::Web::EscapeUTF8 );
307
308       local $SIG{__WARN__};
309       local $SIG{__DIE__};
310
311       RT::Init();
312
313       # We don't need to handle non-text, non-xml items
314       return -1 if defined( $r->content_type ) && $r->content_type !~ m!(^text/|\bxml\b)!io;
315
316     } else {
317       $ah->interp->set_escape( 'h' => sub { ${$_[0]}; } );
318     }
319
320     my %session;
321     my $status;
322     eval { $status = $ah->handle_request($r); };
323 #!!
324 #    if ( $@ ) {
325 #       $RT::Logger->crit($@);
326 #    }
327
328     undef %session;
329
330 #!!
331 #    if ($RT::Handle->TransactionDepth) {
332 #       $RT::Handle->ForceRollback;
333 #       $RT::Logger->crit(
334 #"Transaction not committed. Usually indicates a software fault. Data loss may have occurred"
335 #       );
336 #    }
337
338     $status;
339 }
340
341 1;