remove just a tiny bit more cruft
[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 String::Approx qw(amatch);
124       use Chart::LinesPoints;
125       use Chart::Mountain;
126       use Color::Scheme;
127       use HTML::Widgets::SelectLayers 0.07;
128       use Locale::Country;
129       use FS;
130       use FS::UID qw(cgisuidsetup dbh getotaker datasrc driver_name);
131       use FS::Record qw(qsearch qsearchs fields dbdef str2time_sql);
132       use FS::Conf;
133       use FS::CGI qw(header menubar popurl rooturl table itable ntable idiot
134                      eidiot small_custview myexit http_header);
135       use FS::UI::Web qw(svc_url);
136       use FS::UI::bytecount;
137       use FS::Msgcat qw(gettext geterror);
138       use FS::Misc qw( send_email send_fax states_hash counties state_label );
139       use FS::Report::Table::Monthly;
140       use FS::TicketSystem;
141
142       use FS::agent;
143       use FS::agent_type;
144       use FS::domain_record;
145       use FS::cust_bill;
146       use FS::cust_bill_pay;
147       use FS::cust_credit;
148       use FS::cust_credit_bill;
149       use FS::cust_main qw(smart_search);
150       use FS::cust_main_county;
151       use FS::cust_pay;
152       use FS::cust_pkg;
153       use FS::cust_pkg_reason;
154       use FS::cust_refund;
155       use FS::cust_svc;
156       use FS::nas;
157       use FS::part_bill_event;
158       use FS::part_event;
159       use FS::part_event_condition;
160       use FS::part_pkg;
161       use FS::part_referral;
162       use FS::part_svc;
163       use FS::part_svc_router;
164       use FS::part_virtual_field;
165       use FS::pay_batch;
166       use FS::pkg_svc;
167       use FS::port;
168       use FS::queue qw(joblisting);
169       use FS::raddb;
170       use FS::session;
171       use FS::svc_acct;
172       use FS::svc_acct_pop qw(popselector);
173       use FS::acct_rt_transaction;
174       use FS::svc_domain;
175       use FS::svc_forward;
176       use FS::svc_www;
177       use FS::router;
178       use FS::addr_block;
179       use FS::svc_broadband;
180       use FS::svc_external;
181       use FS::type_pkgs;
182       use FS::part_export;
183       use FS::part_export_option;
184       use FS::export_svc;
185       use FS::msgcat;
186       use FS::rate;
187       use FS::rate_region;
188       use FS::rate_prefix;
189       use FS::payment_gateway;
190       use FS::agent_payment_gateway;
191       use FS::XMLRPC;
192       use FS::payby;
193       use FS::cdr;
194       use FS::inventory_class;
195       use FS::inventory_item;
196       use FS::pkg_class;
197       use FS::access_user;
198       use FS::access_user_pref;
199       use FS::access_group;
200       use FS::access_usergroup;
201       use FS::access_groupagent;
202       use FS::access_right;
203       use FS::AccessRight;
204       use FS::svc_phone;
205       use FS::reason_type;
206       use FS::reason;
207       use FS::cust_main_note;
208
209       if ( %%%RT_ENABLED%%% ) {
210         eval '
211           use RT::Tickets;
212           use RT::Transactions;
213           use RT::Users;
214           use RT::CurrentUser;
215           use RT::Templates;
216           use RT::Queues;
217           use RT::ScripActions;
218           use RT::ScripConditions;
219           use RT::Scrips;
220           use RT::Groups;
221           use RT::GroupMembers;
222           use RT::CustomFields;
223           use RT::CustomFieldValues;
224           use RT::ObjectCustomFieldValues;
225
226           use RT::Interface::Web;
227           use MIME::Entity;
228           use Text::Wrapper;
229           use Time::ParseDate;
230           use HTML::Scrubber;
231           use Text::Quoted;
232         ';
233         die $@ if $@;
234       }
235
236       *CGI::redirect = sub {
237         my $self = shift;
238         my $cookie = '';
239         if ( $_[0] eq '-cookie' ) { #this isn't actually used at the moment
240           (my $x, $cookie) = (shift, shift);
241           $HTML::Mason::r->err_headers_out->add( 'Set-cookie' => $cookie );
242         }
243         my $location = shift;
244
245         use vars qw($m);
246
247         # false laziness w/below
248         if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
249
250           my $page =
251             qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
252             '<BR><BR><PRE>'.
253               ( UNIVERSAL::can(dbh, 'sprintProfile')
254                   ? encode_entities(dbh->sprintProfile())
255                   : 'DBIx::Profile missing sprintProfile method;'.
256                     'unpatched or too old?'                        ).
257             #"\n\n". &sprintAutoProfile().  '</PRE>'.
258             "\n\n".                         '</PRE>'.
259             '</BODY></HTML>';
260           dbh->{'private_profile'} = {};
261           return $page;
262
263         } else { #normal redirect
264
265           $m->redirect($location);
266           '';
267
268         }
269
270       };
271       
272       unless ( $HTML::Mason::r->filename =~ /\/rt\/.*NoAuth/ ) { #RT
273         $cgi = new CGI;
274         &cgisuidsetup($cgi);
275         #&cgisuidsetup($r);
276         $p = popurl(2);
277         $fsurl = rooturl();
278       }
279
280       sub include {
281         use vars qw($m);
282         $m->scomp(@_);
283       }
284
285       sub errorpage {
286         use vars qw($m);
287         $m->comp('/elements/errorpage.html', @_);
288       }
289
290       sub redirect {
291         my( $location ) = @_;
292         use vars qw($m);
293         $m->clear_buffer;
294         #false laziness w/above
295         if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
296
297           $m->print(
298             qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
299             '<BR><BR><PRE>'.
300               ( UNIVERSAL::can(dbh, 'sprintProfile')
301                   ? encode_entities(dbh->sprintProfile())
302                   : 'DBIx::Profile missing sprintProfile method;'.
303                     'unpatched or too old?'                        ).
304             #"\n\n". &sprintAutoProfile().  '</PRE>'.
305             "\n\n".                         '</PRE>'.
306             '</BODY></HTML>'
307           );
308           dbh->{'private_profile'} = {};
309
310           #whew.  removing this is all that's needed to fix the annoying
311           #blank-page-instead-of-profiling-redirect-when-called-from-an-include
312           #bug triggered by mason 1.32
313           #my $rv = $m->abort(200);
314
315         } else { #normal redirect
316
317           $m->redirect($location);
318
319         }
320
321       }
322
323     } # end package HTML::Mason::Commands;
324
325     ###Module::Refresh->refresh;###
326
327     $r->content_type('text/html');
328     #eorar
329
330     my $headers = $r->headers_out;
331     $headers->{'Cache-control'} = 'no-cache';
332     #$r->no_cache(1);
333     $headers->{'Expires'} = '0';
334
335 #    $r->send_http_header;
336
337     if ( $r->filename =~ /\/rt\// ) { #RT
338
339       $ah->interp($rt_interp);
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     warn $@ if $@;
364
365     undef %session;
366
367 #!!
368 #    if ($RT::Handle->TransactionDepth) {
369 #       $RT::Handle->ForceRollback;
370 #       $RT::Logger->crit(
371 #"Transaction not committed. Usually indicates a software fault. Data loss may have occurred"
372 #       );
373 #    }
374
375     $status;
376 }
377
378 1;