ping tool, RT#5845
[freeside.git] / FS / FS / Mason.pm
1 package FS::Mason;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK );
5 use Exporter;
6 use HTML::Mason 1.27; #http://www.masonhq.com/?ApacheModPerl2Redirect
7 use HTML::Mason::Interp;
8 use HTML::Mason::Compiler::ToObject;
9
10 @ISA = qw( Exporter );
11 @EXPORT_OK = qw( mason_interps );
12
13 =head1 NAME
14
15 FS::Mason - Initialize the Mason environment
16
17 =head1 SYNOPSIS
18
19   use FS::Mason qw( mason_interps );
20
21   my( $fs_interp, $rt_interp ) = mason_interps('apache');
22
23   #OR
24
25   my( $fs_interp, $rt_interp ) = mason_interps('standalone'); #XXX name?
26
27 =head1 DESCRIPTION
28
29 Initializes the Mason environment, loads all Freeside and RT libraries, etc.
30
31 =cut
32
33 # List of modules that you want to use from components (see Admin
34 # manual for details)
35 {
36   package HTML::Mason::Commands;
37
38   use strict;
39   use vars qw( %session );
40   use CGI 3.29 qw(-private_tempfiles); #3.29 to fix RT attachment problems
41
42   #breaks quick payment entry
43   #http://rt.cpan.org/Public/Bug/Display.html?id=37365
44   die "CGI.pm v3.38 is broken, use any other version >= 3.29".
45       " (Debian 5.0?  aptitude remove libcgi-pm-perl)"
46     if $CGI::VERSION == 3.38;
47
48   #use CGI::Carp qw(fatalsToBrowser);
49   use CGI::Cookie;
50   use List::Util qw( max min );
51   use Data::Dumper;
52   use Date::Format;
53   use Date::Parse;
54   use Time::Local;
55   use Time::HiRes;
56   use Time::Duration;
57   use DateTime;
58   use DateTime::Format::Strptime;
59   use Lingua::EN::Inflect qw(PL);
60   use Tie::IxHash;
61   use URI;
62   use URI::Escape;
63   use HTML::Entities;
64   use HTML::TreeBuilder;
65   use HTML::FormatText;
66   use JSON;
67   use MIME::Base64;
68   use IO::Handle;
69   use IO::File;
70   use IO::Scalar;
71   #not actually using this yet anyway...# use IPC::Run3 0.036;
72   use Net::Whois::Raw qw(whois);
73   if ( $] < 5.006 ) {
74     eval "use Net::Whois::Raw 0.32 qw(whois)";
75     die $@ if $@;
76   }
77   use Text::CSV_XS;
78   use Spreadsheet::WriteExcel;
79   use Business::CreditCard 0.30; #for mask-aware cardtype()
80   use NetAddr::IP;
81   use Net::Ping;
82   use Net::Ping::External;
83   #if CPAN #7815 ever gets fixed# if ( $Net::Ping::External::VERSION <= 0.12 ) {
84     eval 'sub Net::Ping::External::_ping_linux { 
85             my %args = @_;
86             my $command = "ping -s $args{size} -c $args{count} -w $args{timeout} $args{host}";
87             return Net::Ping::External::_ping_system($command, 0);
88           }
89          ';
90     die $@ if $@;
91   #}
92   use String::Approx qw(amatch);
93   use Chart::LinesPoints;
94   use Chart::Mountain;
95   use Color::Scheme;
96   use HTML::Widgets::SelectLayers 0.07; #should go away in favor of
97                                         #selectlayers.html
98   use Locale::Country;
99   use Business::US::USPS::WebTools::AddressStandardization;
100   use FS;
101   use FS::UID qw( getotaker dbh datasrc driver_name );
102   use FS::Record qw( qsearch qsearchs fields dbdef
103                     str2time_sql str2time_sql_closing
104                    );
105   use FS::Conf;
106   use FS::CGI qw(header menubar table itable ntable idiot
107                  eidiot myexit http_header);
108   use FS::UI::Web qw(svc_url);
109   use FS::UI::Web::small_custview qw(small_custview);
110   use FS::UI::bytecount;
111   use FS::Msgcat qw(gettext geterror);
112   use FS::Misc qw( send_email send_fax states_hash counties state_label );
113   use FS::Misc::eps2png qw( eps2png );
114   use FS::Report::Table::Monthly;
115   use FS::TicketSystem;
116   use FS::Tron qw( tron_lint );
117
118   use FS::agent;
119   use FS::agent_type;
120   use FS::domain_record;
121   use FS::cust_bill;
122   use FS::cust_bill_pay;
123   use FS::cust_credit;
124   use FS::cust_credit_bill;
125   use FS::cust_main qw(smart_search);
126   use FS::cust_main::Import;
127   use FS::cust_main_county;
128   use FS::cust_location;
129   use FS::cust_pay;
130   use FS::cust_pkg;
131   use FS::part_pkg_taxclass;
132   use FS::cust_pkg_reason;
133   use FS::cust_refund;
134   use FS::cust_credit_refund;
135   use FS::cust_pay_refund;
136   use FS::cust_svc;
137   use FS::nas;
138   use FS::part_bill_event;
139   use FS::part_event;
140   use FS::part_event_condition;
141   use FS::part_pkg;
142   use FS::part_referral;
143   use FS::part_svc;
144   use FS::part_svc_router;
145   use FS::part_virtual_field;
146   use FS::pay_batch;
147   use FS::pkg_svc;
148   use FS::port;
149   use FS::queue qw(joblisting);
150   use FS::raddb;
151   use FS::session;
152   use FS::svc_acct;
153   use FS::svc_acct_pop qw(popselector);
154   use FS::acct_rt_transaction;
155   use FS::svc_domain;
156   use FS::svc_forward;
157   use FS::svc_www;
158   use FS::router;
159   use FS::addr_block;
160   use FS::svc_broadband;
161   use FS::svc_external;
162   use FS::type_pkgs;
163   use FS::part_export;
164   use FS::part_export_option;
165   use FS::export_svc;
166   use FS::msgcat;
167   use FS::rate;
168   use FS::rate_region;
169   use FS::rate_prefix;
170   use FS::rate_detail;
171   use FS::usage_class;
172   use FS::payment_gateway;
173   use FS::agent_payment_gateway;
174   use FS::XMLRPC;
175   use FS::payby;
176   use FS::cdr;
177   use FS::inventory_class;
178   use FS::inventory_item;
179   use FS::pkg_category;
180   use FS::pkg_class;
181   use FS::access_user;
182   use FS::access_user_pref;
183   use FS::access_group;
184   use FS::access_usergroup;
185   use FS::access_groupagent;
186   use FS::access_right;
187   use FS::AccessRight;
188   use FS::svc_phone;
189   use FS::phone_device;
190   use FS::part_device;
191   use FS::reason_type;
192   use FS::reason;
193   use FS::cust_main_note;
194   use FS::tax_class;
195   use FS::cust_tax_location;
196   use FS::part_pkg_taxproduct;
197   use FS::part_pkg_taxoverride;
198   use FS::part_pkg_taxrate;
199   use FS::tax_rate;
200   use FS::part_pkg_report_option;
201   use FS::cust_attachment;
202   use FS::h_cust_pkg;
203   use FS::h_svc_acct;
204   use FS::h_svc_broadband;
205   use FS::h_svc_domain;
206   #use FS::h_domain_record;
207   use FS::h_svc_external;
208   use FS::h_svc_forward;
209   use FS::h_svc_phone;
210   #use FS::h_phone_device;
211   use FS::h_svc_www;
212   use FS::cust_statement;
213   # Sammath Naur
214
215   if ( %%%RT_ENABLED%%% ) {
216     eval '
217       use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
218       use vars qw($Nobody $SystemUser);
219       use RT;
220       use RT::Tickets;
221       use RT::Transactions;
222       use RT::Users;
223       use RT::CurrentUser;
224       use RT::Templates;
225       use RT::Queues;
226       use RT::ScripActions;
227       use RT::ScripConditions;
228       use RT::Scrips;
229       use RT::Groups;
230       use RT::GroupMembers;
231       use RT::CustomFields;
232       use RT::CustomFieldValues;
233       use RT::ObjectCustomFieldValues;
234
235       #blah.  manually updated from RT::Interface::Web::Handler
236       use RT::Interface::Web;
237       use MIME::Entity;
238       use Text::Wrapper;
239       use Time::ParseDate;
240       use Time::HiRes;
241       use HTML::Scrubber;
242
243       #blah.  not even in RT::Interface::Web::Handler, just in 
244       #html/NoAuth/css/dhandler and rt-test-dependencies.  ask for it here
245       #to throw a real error instead of just a mysterious unstyled RT
246       use CSS::Squish 0.06;
247
248       #slow, unreliable, segfaults and is optional
249       #see rt/html/Ticket/Elements/ShowTransactionAttachments
250       use Text::Quoted;
251
252       #?#use File::Path qw( rmtree );
253       #?#use File::Glob qw( bsd_glob );
254       #?#use File::Spec::Unix;
255
256     ';
257     die $@ if $@;
258   }
259
260   *CGI::redirect = sub {
261     my $self = shift;
262     my $cookie = '';
263     if ( $_[0] eq '-cookie' ) { #this isn't actually used at the moment
264       (my $x, $cookie) = (shift, shift);
265       $HTML::Mason::r->err_headers_out->add( 'Set-cookie' => $cookie );
266     }
267     my $location = shift;
268
269     use vars qw($m);
270
271     # false laziness w/below
272     if ( defined(@DBIx::Profile::ISA) ) {
273
274       if ( $FS::CurrentUser::CurrentUser->option('show_db_profile') ) {
275
276         #profiling redirect
277
278         my $page =
279           qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
280           '<BR><BR><PRE>'.
281             ( UNIVERSAL::can(dbh, 'sprintProfile')
282                 ? encode_entities(dbh->sprintProfile())
283                 : 'DBIx::Profile missing sprintProfile method;'.
284                   'unpatched or too old?'                        ).
285           #"\n\n". &sprintAutoProfile().  '</PRE>'.
286           "\n\n".                         '</PRE>'.
287           '</BODY></HTML>';
288
289
290         dbh->{'private_profile'} = {};
291         return $page;
292
293       } else {
294
295         #clear db profile, but normal redirect
296         dbh->{'private_profile'} = {};
297         $m->redirect($location);
298         '';
299
300       }
301
302     } else { #normal redirect
303
304       $m->redirect($location);
305       '';
306
307     }
308
309   };
310   
311   sub include {
312     use vars qw($m);
313     $m->scomp(@_);
314   }
315
316   sub errorpage {
317     use vars qw($m);
318     $m->comp('/elements/errorpage.html', @_);
319   }
320
321   sub redirect {
322     my( $location ) = @_;
323     use vars qw($m);
324     $m->clear_buffer;
325     #false laziness w/above
326     if ( defined(@DBIx::Profile::ISA) ) {
327
328       if ( $FS::CurrentUser::CurrentUser->option('show_db_profile') ) {
329
330         #profiling redirect
331
332         $m->print(
333           qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
334           '<BR><BR><PRE>'.
335             ( UNIVERSAL::can(dbh, 'sprintProfile')
336                 ? encode_entities(dbh->sprintProfile())
337                 : 'DBIx::Profile missing sprintProfile method;'.
338                   'unpatched or too old?'                        ).
339           #"\n\n". &sprintAutoProfile().  '</PRE>'.
340           "\n\n".                         '</PRE>'.
341           '</BODY></HTML>'
342         );
343
344         dbh->{'private_profile'} = {};
345
346       } else {
347
348         #clear db profile, but normal redirect
349         dbh->{'private_profile'} = {};
350         $m->redirect($location);
351
352       }
353
354     } else { #normal redirect
355
356       $m->redirect($location);
357
358     }
359
360   }
361
362 } # end package HTML::Mason::Commands;
363
364 =head1 SUBROUTINE
365
366 =over 4
367
368 =item mason_interps [ MODE ]
369
370 Returns a list consisting of two HTML::Mason::Interp objects, the first for
371 Freeside pages, and the second for RT pages.
372
373 #MODE can be 'apache' or 'standalone'.  If not specified, defaults to 'apache'.
374
375 =cut
376
377 sub mason_interps {
378   my $mode = shift || 'apache';
379   my %opt = @_;
380
381   #my $request_class = 'HTML::Mason::Request'.
382                       #( $mode eq 'apache' ? '::ApacheHandler' : '' );
383   my $request_class = 'FS::Mason::Request';
384
385   #not entirely sure it belongs here, but what the hey
386   if ( %%%RT_ENABLED%%% ) {
387     RT::LoadConfig();
388   }
389
390   my %interp = (
391     request_class        => $request_class,
392     data_dir             => '%%%MASONDATA%%%',
393     error_mode           => 'output',
394     error_format         => 'html',
395     ignore_warnings_expr => '.',
396     comp_root            => [
397                               [ 'freeside'=>'%%%FREESIDE_DOCUMENT_ROOT%%%'    ],
398                               [ 'rt'      =>'%%%FREESIDE_DOCUMENT_ROOT%%%/rt' ],
399                             ],
400   );
401
402   $interp{out_method} = $opt{outbuf} if $mode eq 'standalone' && $opt{outbuf};
403
404   my $fs_interp = new HTML::Mason::Interp (
405     %interp,
406     escape_flags => { 'js_string' => sub {
407                         #${$_[0]} =~ s/(['\\\n])/'\\'.($1 eq "\n" ? 'n' : $1)/ge;
408                         ${$_[0]} =~ s/(['\\])/\\$1/g;
409                         ${$_[0]} =~ s/\n/\\n/g;
410                         ${$_[0]} = "'". ${$_[0]}. "'";
411                       }
412                     },
413     compiler     => HTML::Mason::Compiler::ToObject->new(
414                       allow_globals        => [qw(%session)],
415                     ),
416   );
417
418   my $rt_interp = new HTML::Mason::Interp (
419     %interp,
420     escape_flags => { 'h' => \&RT::Interface::Web::EscapeUTF8 },
421     compiler     => HTML::Mason::Compiler::ToObject->new(
422                       default_escape_flags => 'h',
423                       allow_globals        => [qw(%session)],
424                     ),
425   );
426
427   ( $fs_interp, $rt_interp );
428
429 }
430
431 =back
432
433 =head1 BUGS
434
435 Lurking in the darkness...
436
437 =head1 SEE ALSO
438
439 L<HTML::Mason>, L<FS>, L<RT>
440
441 =cut
442
443 1;