92564350efd7469a8306e8f9e38a1d9ee61d5a56
[freeside.git] / htetc / handler.pl
1 #!/usr/bin/perl
2
3 package HTML::Mason;
4
5 use strict;
6 use vars qw($r);
7 use HTML::Mason 1.27; #http://www.masonhq.com/?ApacheModPerl2Redirect
8 use HTML::Mason::Interp;
9 use HTML::Mason::Compiler::ToObject;
10
11 # Bring in ApacheHandler, necessary for mod_perl integration.
12 # Uncomment the second line (and comment the first) to use
13 # Apache::Request instead of CGI.pm to parse arguments.
14 use HTML::Mason::ApacheHandler;
15 # use HTML::Mason::ApacheHandler (args_method=>'mod_perl');
16
17 ###use Module::Refresh;###
18
19 # List of modules that you want to use from components (see Admin
20 # manual for details)
21 #{  package HTML::Mason::Commands;
22 #   use CGI;
23 #}
24
25 if ( %%%RT_ENABLED%%% ) {
26  eval '
27    use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
28    use RT;
29    use vars qw($Nobody $SystemUser);
30    RT::LoadConfig();
31  ';
32  die $@ if $@;
33 }
34
35 # Create Mason objects
36
37 my %interp = (
38   request_class        => 'HTML::Mason::Request::ApacheHandler',
39   data_dir             => '%%%MASONDATA%%%',
40   error_mode           => 'output',
41   error_format         => 'html',
42   ignore_warnings_expr => '.',
43   comp_root            => [
44                             [ 'freeside' => '%%%FREESIDE_DOCUMENT_ROOT%%%'    ],
45                             [ 'rt'       => '%%%FREESIDE_DOCUMENT_ROOT%%%/rt' ],
46                           ],
47 );
48
49 my $fs_interp = new HTML::Mason::Interp (
50   %interp,
51   escape_flags => { 'js_string' => sub {
52                       #${$_[0]} =~ s/(['\\\n])/'\\'.($1 eq "\n" ? 'n' : $1)/ge;
53                       ${$_[0]} =~ s/(['\\])/\\$1/g;
54                       ${$_[0]} =~ s/\n/\\n/g;
55                       ${$_[0]} = "'". ${$_[0]}. "'";
56                     }
57                   },
58 );
59
60 my $rt_interp = new HTML::Mason::Interp (
61   %interp,
62   escape_flags => { 'h' => \&RT::Interface::Web::EscapeUTF8 },
63   compiler     => HTML::Mason::Compiler::ToObject->new(
64                     default_escape_flags => 'h',
65                     allow_globals        => [qw(%session)],
66                   ),
67 );
68
69 my $ah = new HTML::Mason::ApacheHandler (
70   interp      => $fs_interp,
71   args_method => 'CGI', #(and FS too)
72 );
73
74 # Activate the following if running httpd as root (the normal case).
75 # Resets ownership of all files created by Mason at startup.
76 #
77 #chown (Apache->server->uid, Apache->server->gid, $interp->files_written);
78
79 sub handler
80 {
81     ($r) = @_;
82
83     # If you plan to intermix images in the same directory as
84     # components, activate the following to prevent Mason from
85     # evaluating image files as components.
86     #
87     #return -1 if $r->content_type && $r->content_type !~ m|^text/|i;
88
89     #rar
90     { package HTML::Mason::Commands;
91       use strict;
92       use vars qw( $cgi $p $fsurl);
93       use vars qw( %session );
94       use CGI 2.47 qw(-private_tempfiles);
95       #use CGI::Carp qw(fatalsToBrowser);
96       use CGI::Cookie;
97       use List::Util qw( max min );
98       use Data::Dumper;
99       use Date::Format;
100       use Date::Parse;
101       use Time::Local;
102       use Time::Duration;
103       use DateTime;
104       use DateTime::Format::Strptime;
105       use Lingua::EN::Inflect qw(PL);
106       use Tie::IxHash;
107       use URI::Escape;
108       use HTML::Entities;
109       use JSON;
110       use MIME::Base64;
111       use IO::Handle;
112       use IO::File;
113       use IO::Scalar;
114       #not actually using this yet anyway...# use IPC::Run3 0.036;
115       use Net::Whois::Raw qw(whois);
116       if ( $] < 5.006 ) {
117         eval "use Net::Whois::Raw 0.32 qw(whois)";
118         die $@ if $@;
119       }
120       use Text::CSV_XS;
121       use Spreadsheet::WriteExcel;
122       use Business::CreditCard 0.30; #for mask-aware cardtype()
123       use NetAddr::IP;
124       use String::Approx qw(amatch);
125       use Chart::LinesPoints;
126       use Chart::Mountain;
127       use Color::Scheme;
128       use HTML::Widgets::SelectLayers 0.07; #should go away in favor of
129                                             #selectlayers.html
130       use Locale::Country;
131       use Business::US::USPS::WebTools::AddressStandardization;
132       use FS;
133       use FS::UID qw(cgisuidsetup dbh getotaker datasrc driver_name);
134       use FS::Record qw(qsearch qsearchs fields dbdef str2time_sql);
135       use FS::Conf;
136       use FS::CGI qw(header menubar popurl rooturl table itable ntable idiot
137                      eidiot small_custview myexit http_header);
138       use FS::UI::Web qw(svc_url);
139       use FS::UI::bytecount;
140       use FS::Msgcat qw(gettext geterror);
141       use FS::Misc qw( send_email send_fax states_hash counties state_label );
142       use FS::Report::Table::Monthly;
143       use FS::TicketSystem;
144
145       use FS::agent;
146       use FS::agent_type;
147       use FS::domain_record;
148       use FS::cust_bill;
149       use FS::cust_bill_pay;
150       use FS::cust_credit;
151       use FS::cust_credit_bill;
152       use FS::cust_main qw(smart_search);
153       use FS::cust_main_county;
154       use FS::cust_pay;
155       use FS::cust_pkg;
156       use FS::part_pkg_taxclass;
157       use FS::cust_pkg_reason;
158       use FS::cust_refund;
159       use FS::cust_svc;
160       use FS::nas;
161       use FS::part_bill_event;
162       use FS::part_event;
163       use FS::part_event_condition;
164       use FS::part_pkg;
165       use FS::part_referral;
166       use FS::part_svc;
167       use FS::part_svc_router;
168       use FS::part_virtual_field;
169       use FS::pay_batch;
170       use FS::pkg_svc;
171       use FS::port;
172       use FS::queue qw(joblisting);
173       use FS::raddb;
174       use FS::session;
175       use FS::svc_acct;
176       use FS::svc_acct_pop qw(popselector);
177       use FS::acct_rt_transaction;
178       use FS::svc_domain;
179       use FS::svc_forward;
180       use FS::svc_www;
181       use FS::router;
182       use FS::addr_block;
183       use FS::svc_broadband;
184       use FS::svc_external;
185       use FS::type_pkgs;
186       use FS::part_export;
187       use FS::part_export_option;
188       use FS::export_svc;
189       use FS::msgcat;
190       use FS::rate;
191       use FS::rate_region;
192       use FS::rate_prefix;
193       use FS::payment_gateway;
194       use FS::agent_payment_gateway;
195       use FS::XMLRPC;
196       use FS::payby;
197       use FS::cdr;
198       use FS::inventory_class;
199       use FS::inventory_item;
200       use FS::pkg_class;
201       use FS::access_user;
202       use FS::access_user_pref;
203       use FS::access_group;
204       use FS::access_usergroup;
205       use FS::access_groupagent;
206       use FS::access_right;
207       use FS::AccessRight;
208       use FS::svc_phone;
209       use FS::reason_type;
210       use FS::reason;
211       use FS::cust_main_note;
212       use FS::tax_class;
213       use FS::cust_tax_location;
214       use FS::part_pkg_taxproduct;
215       use FS::part_pkg_taxoverride;
216       use FS::part_pkg_taxrate;
217       use FS::tax_rate;
218
219       if ( %%%RT_ENABLED%%% ) {
220         eval '
221           use RT::Tickets;
222           use RT::Transactions;
223           use RT::Users;
224           use RT::CurrentUser;
225           use RT::Templates;
226           use RT::Queues;
227           use RT::ScripActions;
228           use RT::ScripConditions;
229           use RT::Scrips;
230           use RT::Groups;
231           use RT::GroupMembers;
232           use RT::CustomFields;
233           use RT::CustomFieldValues;
234           use RT::ObjectCustomFieldValues;
235
236           #blah.  manually updated from RT::Interface::Web::Handler
237           use RT::Interface::Web;
238           use MIME::Entity;
239           use Text::Wrapper;
240           use Time::ParseDate;
241           use Time::HiRes;
242           use HTML::Scrubber;
243
244           #slow, unreliable, segfaults and is optional
245           #see rt/html/Ticket/Elements/ShowTransactionAttachments
246           #use Text::Quoted;
247
248           #?#use File::Path qw( rmtree );
249           #?#use File::Glob qw( bsd_glob );
250           #?#use File::Spec::Unix;
251
252         ';
253         die $@ if $@;
254       }
255
256       *CGI::redirect = sub {
257         my $self = shift;
258         my $cookie = '';
259         if ( $_[0] eq '-cookie' ) { #this isn't actually used at the moment
260           (my $x, $cookie) = (shift, shift);
261           $HTML::Mason::r->err_headers_out->add( 'Set-cookie' => $cookie );
262         }
263         my $location = shift;
264
265         use vars qw($m);
266
267         # false laziness w/below
268         if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
269
270           my $page =
271             qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
272             '<BR><BR><PRE>'.
273               ( UNIVERSAL::can(dbh, 'sprintProfile')
274                   ? encode_entities(dbh->sprintProfile())
275                   : 'DBIx::Profile missing sprintProfile method;'.
276                     'unpatched or too old?'                        ).
277             #"\n\n". &sprintAutoProfile().  '</PRE>'.
278             "\n\n".                         '</PRE>'.
279             '</BODY></HTML>';
280           dbh->{'private_profile'} = {};
281           return $page;
282
283         } else { #normal redirect
284
285           $m->redirect($location);
286           '';
287
288         }
289
290       };
291       
292       #is this conditional a bad idea entirely, hmm
293       unless ( 
294            $HTML::Mason::r->filename =~ /\/rt\/.*NoAuth/ #RT images/JS
295         && $HTML::Mason::r->filename !~ /\/rt\/REST\//   #but NOT mail gw
296       ) {
297         $cgi = new CGI;
298         &cgisuidsetup($cgi);
299         #&cgisuidsetup($r);
300         $p = popurl(2);
301         $fsurl = rooturl();
302       }
303
304       sub include {
305         use vars qw($m);
306         $m->scomp(@_);
307       }
308
309       sub errorpage {
310         use vars qw($m);
311         $m->comp('/elements/errorpage.html', @_);
312       }
313
314       sub redirect {
315         my( $location ) = @_;
316         use vars qw($m);
317         $m->clear_buffer;
318         #false laziness w/above
319         if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
320
321           $m->print(
322             qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
323             '<BR><BR><PRE>'.
324               ( UNIVERSAL::can(dbh, 'sprintProfile')
325                   ? encode_entities(dbh->sprintProfile())
326                   : 'DBIx::Profile missing sprintProfile method;'.
327                     'unpatched or too old?'                        ).
328             #"\n\n". &sprintAutoProfile().  '</PRE>'.
329             "\n\n".                         '</PRE>'.
330             '</BODY></HTML>'
331           );
332           dbh->{'private_profile'} = {};
333
334           #whew.  removing this is all that's needed to fix the annoying
335           #blank-page-instead-of-profiling-redirect-when-called-from-an-include
336           #bug triggered by mason 1.32
337           #my $rv = $m->abort(200);
338
339         } else { #normal redirect
340
341           $m->redirect($location);
342
343         }
344
345       }
346
347     } # end package HTML::Mason::Commands;
348
349     ###Module::Refresh->refresh;###
350
351     $r->content_type('text/html');
352     #eorar
353
354     my $headers = $r->headers_out;
355     $headers->{'Cache-control'} = 'no-cache';
356     #$r->no_cache(1);
357     $headers->{'Expires'} = '0';
358
359 #    $r->send_http_header;
360
361     if ( $r->filename =~ /\/rt\// ) { #RT
362
363       $ah->interp($rt_interp);
364
365       local $SIG{__WARN__};
366       local $SIG{__DIE__};
367
368       RT::Init();
369
370       # We don't need to handle non-text, non-xml items
371       return -1 if defined( $r->content_type )
372                 && $r->content_type !~ m!(^text/|\bxml\b)!io;
373
374     } else {
375
376       $ah->interp($fs_interp);
377
378     }
379
380     my %session;
381     my $status;
382     eval { $status = $ah->handle_request($r); };
383 #!!
384 #    if ( $@ ) {
385 #       $RT::Logger->crit($@);
386 #    }
387     warn $@ if $@;
388
389     undef %session;
390
391 #!!
392 #    if ($RT::Handle->TransactionDepth) {
393 #       $RT::Handle->ForceRollback;
394 #       $RT::Logger->crit(
395 #"Transaction not committed. Usually indicates a software fault. Data loss may have occurred"
396 #       );
397 #    }
398
399     $status;
400 }
401
402 1;