quiet warning: Subroutine Net::Ping::External::_ping_linux redefined
[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   {
85     no warnings 'redefine';
86     eval 'sub Net::Ping::External::_ping_linux { 
87             my %args = @_;
88             my $command = "ping -s $args{size} -c $args{count} -w $args{timeout} $args{host}";
89             return Net::Ping::External::_ping_system($command, 0);
90           }
91          ';
92     die $@ if $@;
93   }
94   use String::Approx qw(amatch);
95   use Chart::LinesPoints;
96   use Chart::Mountain;
97   use Color::Scheme;
98   use HTML::Widgets::SelectLayers 0.07; #should go away in favor of
99                                         #selectlayers.html
100   use Locale::Country;
101   use Business::US::USPS::WebTools::AddressStandardization;
102   use FS;
103   use FS::UID qw( getotaker dbh datasrc driver_name );
104   use FS::Record qw( qsearch qsearchs fields dbdef
105                     str2time_sql str2time_sql_closing
106                    );
107   use FS::Conf;
108   use FS::CGI qw(header menubar table itable ntable idiot
109                  eidiot myexit http_header);
110   use FS::UI::Web qw(svc_url);
111   use FS::UI::Web::small_custview qw(small_custview);
112   use FS::UI::bytecount;
113   use FS::Msgcat qw(gettext geterror);
114   use FS::Misc qw( send_email send_fax states_hash counties state_label );
115   use FS::Misc::eps2png qw( eps2png );
116   use FS::Report::Table::Monthly;
117   use FS::TicketSystem;
118   use FS::Tron qw( tron_lint );
119
120   use FS::agent;
121   use FS::agent_type;
122   use FS::domain_record;
123   use FS::cust_bill;
124   use FS::cust_bill_pay;
125   use FS::cust_credit;
126   use FS::cust_credit_bill;
127   use FS::cust_main qw(smart_search);
128   use FS::cust_main::Import;
129   use FS::cust_main_county;
130   use FS::cust_location;
131   use FS::cust_pay;
132   use FS::cust_pkg;
133   use FS::part_pkg_taxclass;
134   use FS::cust_pkg_reason;
135   use FS::cust_refund;
136   use FS::cust_credit_refund;
137   use FS::cust_pay_refund;
138   use FS::cust_svc;
139   use FS::nas;
140   use FS::part_bill_event;
141   use FS::part_event;
142   use FS::part_event_condition;
143   use FS::part_pkg;
144   use FS::part_referral;
145   use FS::part_svc;
146   use FS::part_svc_router;
147   use FS::part_virtual_field;
148   use FS::pay_batch;
149   use FS::pkg_svc;
150   use FS::port;
151   use FS::queue qw(joblisting);
152   use FS::raddb;
153   use FS::session;
154   use FS::svc_acct;
155   use FS::svc_acct_pop qw(popselector);
156   use FS::acct_rt_transaction;
157   use FS::svc_domain;
158   use FS::svc_forward;
159   use FS::svc_www;
160   use FS::router;
161   use FS::addr_block;
162   use FS::svc_broadband;
163   use FS::svc_external;
164   use FS::type_pkgs;
165   use FS::part_export;
166   use FS::part_export_option;
167   use FS::export_svc;
168   use FS::msgcat;
169   use FS::rate;
170   use FS::rate_region;
171   use FS::rate_prefix;
172   use FS::rate_detail;
173   use FS::usage_class;
174   use FS::payment_gateway;
175   use FS::agent_payment_gateway;
176   use FS::XMLRPC;
177   use FS::payby;
178   use FS::cdr;
179   use FS::inventory_class;
180   use FS::inventory_item;
181   use FS::pkg_category;
182   use FS::pkg_class;
183   use FS::access_user;
184   use FS::access_user_pref;
185   use FS::access_group;
186   use FS::access_usergroup;
187   use FS::access_groupagent;
188   use FS::access_right;
189   use FS::AccessRight;
190   use FS::svc_phone;
191   use FS::phone_device;
192   use FS::part_device;
193   use FS::reason_type;
194   use FS::reason;
195   use FS::cust_main_note;
196   use FS::tax_class;
197   use FS::cust_tax_location;
198   use FS::part_pkg_taxproduct;
199   use FS::part_pkg_taxoverride;
200   use FS::part_pkg_taxrate;
201   use FS::tax_rate;
202   use FS::part_pkg_report_option;
203   use FS::cust_attachment;
204   use FS::h_cust_pkg;
205   use FS::h_svc_acct;
206   use FS::h_svc_broadband;
207   use FS::h_svc_domain;
208   #use FS::h_domain_record;
209   use FS::h_svc_external;
210   use FS::h_svc_forward;
211   use FS::h_svc_phone;
212   #use FS::h_phone_device;
213   use FS::h_svc_www;
214   use FS::cust_statement;
215   # Sammath Naur
216
217   if ( %%%RT_ENABLED%%% ) {
218     eval '
219       use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
220       use vars qw($Nobody $SystemUser);
221       use RT;
222       use RT::Tickets;
223       use RT::Transactions;
224       use RT::Users;
225       use RT::CurrentUser;
226       use RT::Templates;
227       use RT::Queues;
228       use RT::ScripActions;
229       use RT::ScripConditions;
230       use RT::Scrips;
231       use RT::Groups;
232       use RT::GroupMembers;
233       use RT::CustomFields;
234       use RT::CustomFieldValues;
235       use RT::ObjectCustomFieldValues;
236
237       #blah.  manually updated from RT::Interface::Web::Handler
238       use RT::Interface::Web;
239       use MIME::Entity;
240       use Text::Wrapper;
241       use Time::ParseDate;
242       use Time::HiRes;
243       use HTML::Scrubber;
244
245       #blah.  not even in RT::Interface::Web::Handler, just in 
246       #html/NoAuth/css/dhandler and rt-test-dependencies.  ask for it here
247       #to throw a real error instead of just a mysterious unstyled RT
248       use CSS::Squish 0.06;
249
250       #slow, unreliable, segfaults and is optional
251       #see rt/html/Ticket/Elements/ShowTransactionAttachments
252       use Text::Quoted;
253
254       #?#use File::Path qw( rmtree );
255       #?#use File::Glob qw( bsd_glob );
256       #?#use File::Spec::Unix;
257
258     ';
259     die $@ if $@;
260   }
261
262   *CGI::redirect = sub {
263     my $self = shift;
264     my $cookie = '';
265     if ( $_[0] eq '-cookie' ) { #this isn't actually used at the moment
266       (my $x, $cookie) = (shift, shift);
267       $HTML::Mason::r->err_headers_out->add( 'Set-cookie' => $cookie );
268     }
269     my $location = shift;
270
271     use vars qw($m);
272
273     # false laziness w/below
274     if ( defined(@DBIx::Profile::ISA) ) {
275
276       if ( $FS::CurrentUser::CurrentUser->option('show_db_profile') ) {
277
278         #profiling redirect
279
280         my $page =
281           qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
282           '<BR><BR><PRE>'.
283             ( UNIVERSAL::can(dbh, 'sprintProfile')
284                 ? encode_entities(dbh->sprintProfile())
285                 : 'DBIx::Profile missing sprintProfile method;'.
286                   'unpatched or too old?'                        ).
287           #"\n\n". &sprintAutoProfile().  '</PRE>'.
288           "\n\n".                         '</PRE>'.
289           '</BODY></HTML>';
290
291
292         dbh->{'private_profile'} = {};
293         return $page;
294
295       } else {
296
297         #clear db profile, but normal redirect
298         dbh->{'private_profile'} = {};
299         $m->redirect($location);
300         '';
301
302       }
303
304     } else { #normal redirect
305
306       $m->redirect($location);
307       '';
308
309     }
310
311   };
312   
313   sub include {
314     use vars qw($m);
315     $m->scomp(@_);
316   }
317
318   sub errorpage {
319     use vars qw($m);
320     $m->comp('/elements/errorpage.html', @_);
321   }
322
323   sub redirect {
324     my( $location ) = @_;
325     use vars qw($m);
326     $m->clear_buffer;
327     #false laziness w/above
328     if ( defined(@DBIx::Profile::ISA) ) {
329
330       if ( $FS::CurrentUser::CurrentUser->option('show_db_profile') ) {
331
332         #profiling redirect
333
334         $m->print(
335           qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
336           '<BR><BR><PRE>'.
337             ( UNIVERSAL::can(dbh, 'sprintProfile')
338                 ? encode_entities(dbh->sprintProfile())
339                 : 'DBIx::Profile missing sprintProfile method;'.
340                   'unpatched or too old?'                        ).
341           #"\n\n". &sprintAutoProfile().  '</PRE>'.
342           "\n\n".                         '</PRE>'.
343           '</BODY></HTML>'
344         );
345
346         dbh->{'private_profile'} = {};
347
348       } else {
349
350         #clear db profile, but normal redirect
351         dbh->{'private_profile'} = {};
352         $m->redirect($location);
353
354       }
355
356     } else { #normal redirect
357
358       $m->redirect($location);
359
360     }
361
362   }
363
364 } # end package HTML::Mason::Commands;
365
366 =head1 SUBROUTINE
367
368 =over 4
369
370 =item mason_interps [ MODE ]
371
372 Returns a list consisting of two HTML::Mason::Interp objects, the first for
373 Freeside pages, and the second for RT pages.
374
375 #MODE can be 'apache' or 'standalone'.  If not specified, defaults to 'apache'.
376
377 =cut
378
379 sub mason_interps {
380   my $mode = shift || 'apache';
381   my %opt = @_;
382
383   #my $request_class = 'HTML::Mason::Request'.
384                       #( $mode eq 'apache' ? '::ApacheHandler' : '' );
385   my $request_class = 'FS::Mason::Request';
386
387   #not entirely sure it belongs here, but what the hey
388   if ( %%%RT_ENABLED%%% ) {
389     RT::LoadConfig();
390   }
391
392   my %interp = (
393     request_class        => $request_class,
394     data_dir             => '%%%MASONDATA%%%',
395     error_mode           => 'output',
396     error_format         => 'html',
397     ignore_warnings_expr => '.',
398     comp_root            => [
399                               [ 'freeside'=>'%%%FREESIDE_DOCUMENT_ROOT%%%'    ],
400                               [ 'rt'      =>'%%%FREESIDE_DOCUMENT_ROOT%%%/rt' ],
401                             ],
402   );
403
404   $interp{out_method} = $opt{outbuf} if $mode eq 'standalone' && $opt{outbuf};
405
406   my $fs_interp = new HTML::Mason::Interp (
407     %interp,
408     escape_flags => { 'js_string' => sub {
409                         #${$_[0]} =~ s/(['\\\n])/'\\'.($1 eq "\n" ? 'n' : $1)/ge;
410                         ${$_[0]} =~ s/(['\\])/\\$1/g;
411                         ${$_[0]} =~ s/\n/\\n/g;
412                         ${$_[0]} = "'". ${$_[0]}. "'";
413                       }
414                     },
415     compiler     => HTML::Mason::Compiler::ToObject->new(
416                       allow_globals        => [qw(%session)],
417                     ),
418   );
419
420   my $rt_interp = new HTML::Mason::Interp (
421     %interp,
422     escape_flags => { 'h' => \&RT::Interface::Web::EscapeUTF8 },
423     compiler     => HTML::Mason::Compiler::ToObject->new(
424                       default_escape_flags => 'h',
425                       allow_globals        => [qw(%session)],
426                     ),
427   );
428
429   ( $fs_interp, $rt_interp );
430
431 }
432
433 =back
434
435 =head1 BUGS
436
437 Lurking in the darkness...
438
439 =head1 SEE ALSO
440
441 L<HTML::Mason>, L<FS>, L<RT>
442
443 =cut
444
445 1;