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