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