77ff66822e3b0e7d822c014c3ccfa7c68151f8a4
[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   #use CGI::Carp qw(fatalsToBrowser);
42   use CGI::Cookie;
43   use List::Util qw( max min );
44   use Data::Dumper;
45   use Date::Format;
46   use Date::Parse;
47   use Time::Local;
48   use Time::Duration;
49   use DateTime;
50   use DateTime::Format::Strptime;
51   use Lingua::EN::Inflect qw(PL);
52   use Tie::IxHash;
53   use URI::URL;
54   use URI::Escape;
55   use HTML::Entities;
56   use HTML::TreeBuilder;
57   use HTML::FormatText;
58   use JSON;
59   use MIME::Base64;
60   use IO::Handle;
61   use IO::File;
62   use IO::Scalar;
63   #not actually using this yet anyway...# use IPC::Run3 0.036;
64   use Net::Whois::Raw qw(whois);
65   if ( $] < 5.006 ) {
66     eval "use Net::Whois::Raw 0.32 qw(whois)";
67     die $@ if $@;
68   }
69   use Text::CSV_XS;
70   use Spreadsheet::WriteExcel;
71   use Business::CreditCard 0.30; #for mask-aware cardtype()
72   use NetAddr::IP;
73   use String::Approx qw(amatch);
74   use Chart::LinesPoints;
75   use Chart::Mountain;
76   use Color::Scheme;
77   use HTML::Widgets::SelectLayers 0.07; #should go away in favor of
78                                         #selectlayers.html
79   use Locale::Country;
80   use Business::US::USPS::WebTools::AddressStandardization;
81   use FS;
82   use FS::UID qw( getotaker dbh datasrc driver_name );
83   use FS::Record qw( qsearch qsearchs fields dbdef
84                     str2time_sql str2time_sql_closing
85                    );
86   use FS::Conf;
87   use FS::CGI qw(header menubar table itable ntable idiot
88                  eidiot myexit http_header);
89   use FS::UI::Web qw(svc_url);
90   use FS::UI::Web::small_custview qw(small_custview);
91   use FS::UI::bytecount;
92   use FS::Msgcat qw(gettext geterror);
93   use FS::Misc qw( send_email send_fax states_hash counties state_label );
94   use FS::Report::Table::Monthly;
95   use FS::TicketSystem;
96   use FS::Tron qw( tron_lint );
97
98   use FS::agent;
99   use FS::agent_type;
100   use FS::domain_record;
101   use FS::cust_bill;
102   use FS::cust_bill_pay;
103   use FS::cust_credit;
104   use FS::cust_credit_bill;
105   use FS::cust_main qw(smart_search);
106   use FS::cust_main_county;
107   use FS::cust_pay;
108   use FS::cust_pkg;
109   use FS::part_pkg_taxclass;
110   use FS::cust_pkg_reason;
111   use FS::cust_refund;
112   use FS::cust_credit_refund;
113   use FS::cust_pay_refund;
114   use FS::cust_svc;
115   use FS::nas;
116   use FS::part_bill_event;
117   use FS::part_event;
118   use FS::part_event_condition;
119   use FS::part_pkg;
120   use FS::part_referral;
121   use FS::part_svc;
122   use FS::part_svc_router;
123   use FS::part_virtual_field;
124   use FS::pay_batch;
125   use FS::pkg_svc;
126   use FS::port;
127   use FS::queue qw(joblisting);
128   use FS::raddb;
129   use FS::session;
130   use FS::svc_acct;
131   use FS::svc_acct_pop qw(popselector);
132   use FS::acct_rt_transaction;
133   use FS::svc_domain;
134   use FS::svc_forward;
135   use FS::svc_www;
136   use FS::router;
137   use FS::addr_block;
138   use FS::svc_broadband;
139   use FS::svc_external;
140   use FS::type_pkgs;
141   use FS::part_export;
142   use FS::part_export_option;
143   use FS::export_svc;
144   use FS::msgcat;
145   use FS::rate;
146   use FS::rate_region;
147   use FS::rate_prefix;
148   use FS::payment_gateway;
149   use FS::agent_payment_gateway;
150   use FS::XMLRPC;
151   use FS::payby;
152   use FS::cdr;
153   use FS::inventory_class;
154   use FS::inventory_item;
155   use FS::pkg_category;
156   use FS::pkg_class;
157   use FS::access_user;
158   use FS::access_user_pref;
159   use FS::access_group;
160   use FS::access_usergroup;
161   use FS::access_groupagent;
162   use FS::access_right;
163   use FS::AccessRight;
164   use FS::svc_phone;
165   use FS::reason_type;
166   use FS::reason;
167   use FS::cust_main_note;
168   use FS::tax_class;
169   use FS::cust_tax_location;
170   use FS::part_pkg_taxproduct;
171   use FS::part_pkg_taxoverride;
172   use FS::part_pkg_taxrate;
173   use FS::tax_rate;
174
175   if ( %%%RT_ENABLED%%% ) {
176     eval '
177       use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
178       use vars qw($Nobody $SystemUser);
179       use RT;
180       use RT::Tickets;
181       use RT::Transactions;
182       use RT::Users;
183       use RT::CurrentUser;
184       use RT::Templates;
185       use RT::Queues;
186       use RT::ScripActions;
187       use RT::ScripConditions;
188       use RT::Scrips;
189       use RT::Groups;
190       use RT::GroupMembers;
191       use RT::CustomFields;
192       use RT::CustomFieldValues;
193       use RT::ObjectCustomFieldValues;
194
195       #blah.  manually updated from RT::Interface::Web::Handler
196       use RT::Interface::Web;
197       use MIME::Entity;
198       use Text::Wrapper;
199       use Time::ParseDate;
200       use Time::HiRes;
201       use HTML::Scrubber;
202
203       #blah.  not even in RT::Interface::Web::Handler, just in 
204       #html/NoAuth/css/dhandler and rt-test-dependencies.  ask for it here
205       #to throw a real error instead of just a mysterious unstyled RT
206       use CSS::Squish 0.06;
207
208       #slow, unreliable, segfaults and is optional
209       #see rt/html/Ticket/Elements/ShowTransactionAttachments
210       #use Text::Quoted;
211
212       #?#use File::Path qw( rmtree );
213       #?#use File::Glob qw( bsd_glob );
214       #?#use File::Spec::Unix;
215
216     ';
217     die $@ if $@;
218   }
219
220   *CGI::redirect = sub {
221     my $self = shift;
222     my $cookie = '';
223     if ( $_[0] eq '-cookie' ) { #this isn't actually used at the moment
224       (my $x, $cookie) = (shift, shift);
225       $HTML::Mason::r->err_headers_out->add( 'Set-cookie' => $cookie );
226     }
227     my $location = shift;
228
229     use vars qw($m);
230
231     # false laziness w/below
232     if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
233
234       my $page =
235         qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
236         '<BR><BR><PRE>'.
237           ( UNIVERSAL::can(dbh, 'sprintProfile')
238               ? encode_entities(dbh->sprintProfile())
239               : 'DBIx::Profile missing sprintProfile method;'.
240                 'unpatched or too old?'                        ).
241         #"\n\n". &sprintAutoProfile().  '</PRE>'.
242         "\n\n".                         '</PRE>'.
243         '</BODY></HTML>';
244       dbh->{'private_profile'} = {};
245       return $page;
246
247     } else { #normal redirect
248
249       $m->redirect($location);
250       '';
251
252     }
253
254   };
255   
256   sub include {
257     use vars qw($m);
258     $m->scomp(@_);
259   }
260
261   sub errorpage {
262     use vars qw($m);
263     $m->comp('/elements/errorpage.html', @_);
264   }
265
266   sub redirect {
267     my( $location ) = @_;
268     use vars qw($m);
269     $m->clear_buffer;
270     #false laziness w/above
271     if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
272
273       $m->print(
274         qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
275         '<BR><BR><PRE>'.
276           ( UNIVERSAL::can(dbh, 'sprintProfile')
277               ? encode_entities(dbh->sprintProfile())
278               : 'DBIx::Profile missing sprintProfile method;'.
279                 'unpatched or too old?'                        ).
280         #"\n\n". &sprintAutoProfile().  '</PRE>'.
281         "\n\n".                         '</PRE>'.
282         '</BODY></HTML>'
283       );
284       dbh->{'private_profile'} = {};
285
286     } else { #normal redirect
287
288       $m->redirect($location);
289
290     }
291
292   }
293
294 } # end package HTML::Mason::Commands;
295
296 =head1 SUBROUTINE
297
298 =over 4
299
300 =item mason_interps [ MODE ]
301
302 Returns a list consisting of two HTML::Mason::Interp objects, the first for
303 Freeside pages, and the second for RT pages.
304
305 #MODE can be 'apache' or 'standalone'.  If not specified, defaults to 'apache'.
306
307 =cut
308
309 sub mason_interps {
310   my $mode = shift || 'apache';
311   my %opt = @_;
312
313   #my $request_class = 'HTML::Mason::Request'.
314                       #( $mode eq 'apache' ? '::ApacheHandler' : '' );
315   my $request_class = 'FS::Mason::Request';
316
317   #not entirely sure it belongs here, but what the hey
318   if ( %%%RT_ENABLED%%% ) {
319     RT::LoadConfig();
320   }
321
322   my %interp = (
323     request_class        => $request_class,
324     data_dir             => '%%%MASONDATA%%%',
325     error_mode           => 'output',
326     error_format         => 'html',
327     ignore_warnings_expr => '.',
328     comp_root            => [
329                               [ 'freeside'=>'%%%FREESIDE_DOCUMENT_ROOT%%%'    ],
330                               [ 'rt'      =>'%%%FREESIDE_DOCUMENT_ROOT%%%/rt' ],
331                             ],
332   );
333
334   $interp{out_method} = $opt{outbuf} if $mode eq 'standalone' && $opt{outbuf};
335
336   my $fs_interp = new HTML::Mason::Interp (
337     %interp,
338     escape_flags => { 'js_string' => sub {
339                         #${$_[0]} =~ s/(['\\\n])/'\\'.($1 eq "\n" ? 'n' : $1)/ge;
340                         ${$_[0]} =~ s/(['\\])/\\$1/g;
341                         ${$_[0]} =~ s/\n/\\n/g;
342                         ${$_[0]} = "'". ${$_[0]}. "'";
343                       }
344                     },
345   );
346
347   my $rt_interp = new HTML::Mason::Interp (
348     %interp,
349     escape_flags => { 'h' => \&RT::Interface::Web::EscapeUTF8 },
350     compiler     => HTML::Mason::Compiler::ToObject->new(
351                       default_escape_flags => 'h',
352                       allow_globals        => [qw(%session)],
353                     ),
354   );
355
356   ( $fs_interp, $rt_interp );
357
358 }
359
360 =back
361
362 =head1 BUGS
363
364 Lurking in the darkness...
365
366 =head1 SEE ALSO
367
368 L<HTML::Mason>, L<FS>, L<RT>
369
370 =cut
371
372 1;