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