This commit was generated by cvs2svn to compensate for changes in r5562,
[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::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 redirect {
284         my( $location ) = @_;
285         use vars qw($m);
286         $m->clear_buffer;
287         #false laziness w/above
288         if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
289
290           $m->print(
291             qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
292             '<BR><BR><PRE>'.
293               ( UNIVERSAL::can(dbh, 'sprintProfile')
294                   ? encode_entities(dbh->sprintProfile())
295                   : 'DBIx::Profile missing sprintProfile method;'.
296                     'unpatched or too old?'                        ).
297             #"\n\n". &sprintAutoProfile().  '</PRE>'.
298             "\n\n".                         '</PRE>'.
299             '</BODY></HTML>'
300           );
301           dbh->{'private_profile'} = {};
302
303           #whew.  removing this is all that's needed to fix the annoying
304           #blank-page-instead-of-profiling-redirect-when-called-from-an-include
305           #bug triggered by mason 1.32
306           #my $rv = $m->abort(200);
307
308         } else { #normal redirect
309
310           $m->redirect($location);
311
312         }
313
314       }
315
316     } # end package HTML::Mason::Commands;
317
318     ###Module::Refresh->refresh;###
319
320     $r->content_type('text/html');
321     #eorar
322
323     my $headers = $r->headers_out;
324     $headers->{'Cache-control'} = 'no-cache';
325     #$r->no_cache(1);
326     $headers->{'Expires'} = '0';
327
328 #    $r->send_http_header;
329
330     #$ah->interp->remove_escape('h');
331
332     if ( $r->filename =~ /\/rt\// ) { #RT
333       #warn "processing RT file". $r->filename. "; escaping for RT\n";
334
335       # MasonX::Request::ExtendedCompRoot
336       #$ah->interp->comp_root( '/rt'. $ah->interp->comp_root() );
337
338       $ah->interp->set_escape( h => \&RT::Interface::Web::EscapeUTF8 );
339
340       local $SIG{__WARN__};
341       local $SIG{__DIE__};
342
343       RT::Init();
344
345       # We don't need to handle non-text, non-xml items
346       return -1 if defined( $r->content_type ) && $r->content_type !~ m!(^text/|\bxml\b)!io;
347
348     } else {
349       #$ah->interp->set_escape( 'h' => sub { ${$_[0]}; } );
350       $ah->interp->set_escape( 'h' => sub {} );
351
352       $ah->interp->set_escape( 'js_string' => sub {
353         #${$_[0]} =~ s/(['\\\n])/'\\'.($1 eq "\n" ? 'n' : $1)/ge;
354         ${$_[0]} =~ s/(['\\])/\\$1/g;
355         ${$_[0]} =~ s/\n/\\n/g;
356         ${$_[0]} = "'". ${$_[0]}. "'";
357       } );
358     }
359
360     $ah->interp->ignore_warnings_expr('.');
361
362     my %session;
363     my $status;
364     eval { $status = $ah->handle_request($r); };
365 #!!
366 #    if ( $@ ) {
367 #       $RT::Logger->crit($@);
368 #    }
369
370     undef %session;
371
372 #!!
373 #    if ($RT::Handle->TransactionDepth) {
374 #       $RT::Handle->ForceRollback;
375 #       $RT::Logger->crit(
376 #"Transaction not committed. Usually indicates a software fault. Data loss may have occurred"
377 #       );
378 #    }
379
380     $status;
381 }
382
383 1;