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