move batch customer import to its own file; add svc_external_svc_phone export format...
[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::Import;
107   use FS::cust_main_county;
108   use FS::cust_pay;
109   use FS::cust_pkg;
110   use FS::part_pkg_taxclass;
111   use FS::cust_pkg_reason;
112   use FS::cust_refund;
113   use FS::cust_credit_refund;
114   use FS::cust_pay_refund;
115   use FS::cust_svc;
116   use FS::nas;
117   use FS::part_bill_event;
118   use FS::part_event;
119   use FS::part_event_condition;
120   use FS::part_pkg;
121   use FS::part_referral;
122   use FS::part_svc;
123   use FS::part_svc_router;
124   use FS::part_virtual_field;
125   use FS::pay_batch;
126   use FS::pkg_svc;
127   use FS::port;
128   use FS::queue qw(joblisting);
129   use FS::raddb;
130   use FS::session;
131   use FS::svc_acct;
132   use FS::svc_acct_pop qw(popselector);
133   use FS::acct_rt_transaction;
134   use FS::svc_domain;
135   use FS::svc_forward;
136   use FS::svc_www;
137   use FS::router;
138   use FS::addr_block;
139   use FS::svc_broadband;
140   use FS::svc_external;
141   use FS::type_pkgs;
142   use FS::part_export;
143   use FS::part_export_option;
144   use FS::export_svc;
145   use FS::msgcat;
146   use FS::rate;
147   use FS::rate_region;
148   use FS::rate_prefix;
149   use FS::rate_detail;
150   use FS::usage_class;
151   use FS::payment_gateway;
152   use FS::agent_payment_gateway;
153   use FS::XMLRPC;
154   use FS::payby;
155   use FS::cdr;
156   use FS::inventory_class;
157   use FS::inventory_item;
158   use FS::pkg_category;
159   use FS::pkg_class;
160   use FS::access_user;
161   use FS::access_user_pref;
162   use FS::access_group;
163   use FS::access_usergroup;
164   use FS::access_groupagent;
165   use FS::access_right;
166   use FS::AccessRight;
167   use FS::svc_phone;
168   use FS::reason_type;
169   use FS::reason;
170   use FS::cust_main_note;
171   use FS::tax_class;
172   use FS::cust_tax_location;
173   use FS::part_pkg_taxproduct;
174   use FS::part_pkg_taxoverride;
175   use FS::part_pkg_taxrate;
176   use FS::tax_rate;
177
178   if ( %%%RT_ENABLED%%% ) {
179     eval '
180       use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
181       use vars qw($Nobody $SystemUser);
182       use RT;
183       use RT::Tickets;
184       use RT::Transactions;
185       use RT::Users;
186       use RT::CurrentUser;
187       use RT::Templates;
188       use RT::Queues;
189       use RT::ScripActions;
190       use RT::ScripConditions;
191       use RT::Scrips;
192       use RT::Groups;
193       use RT::GroupMembers;
194       use RT::CustomFields;
195       use RT::CustomFieldValues;
196       use RT::ObjectCustomFieldValues;
197
198       #blah.  manually updated from RT::Interface::Web::Handler
199       use RT::Interface::Web;
200       use MIME::Entity;
201       use Text::Wrapper;
202       use Time::ParseDate;
203       use Time::HiRes;
204       use HTML::Scrubber;
205
206       #blah.  not even in RT::Interface::Web::Handler, just in 
207       #html/NoAuth/css/dhandler and rt-test-dependencies.  ask for it here
208       #to throw a real error instead of just a mysterious unstyled RT
209       use CSS::Squish 0.06;
210
211       #slow, unreliable, segfaults and is optional
212       #see rt/html/Ticket/Elements/ShowTransactionAttachments
213       #use Text::Quoted;
214
215       #?#use File::Path qw( rmtree );
216       #?#use File::Glob qw( bsd_glob );
217       #?#use File::Spec::Unix;
218
219     ';
220     die $@ if $@;
221   }
222
223   *CGI::redirect = sub {
224     my $self = shift;
225     my $cookie = '';
226     if ( $_[0] eq '-cookie' ) { #this isn't actually used at the moment
227       (my $x, $cookie) = (shift, shift);
228       $HTML::Mason::r->err_headers_out->add( 'Set-cookie' => $cookie );
229     }
230     my $location = shift;
231
232     use vars qw($m);
233
234     # false laziness w/below
235     if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
236
237       my $page =
238         qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
239         '<BR><BR><PRE>'.
240           ( UNIVERSAL::can(dbh, 'sprintProfile')
241               ? encode_entities(dbh->sprintProfile())
242               : 'DBIx::Profile missing sprintProfile method;'.
243                 'unpatched or too old?'                        ).
244         #"\n\n". &sprintAutoProfile().  '</PRE>'.
245         "\n\n".                         '</PRE>'.
246         '</BODY></HTML>';
247       dbh->{'private_profile'} = {};
248       return $page;
249
250     } else { #normal redirect
251
252       $m->redirect($location);
253       '';
254
255     }
256
257   };
258   
259   sub include {
260     use vars qw($m);
261     $m->scomp(@_);
262   }
263
264   sub errorpage {
265     use vars qw($m);
266     $m->comp('/elements/errorpage.html', @_);
267   }
268
269   sub redirect {
270     my( $location ) = @_;
271     use vars qw($m);
272     $m->clear_buffer;
273     #false laziness w/above
274     if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
275
276       $m->print(
277         qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
278         '<BR><BR><PRE>'.
279           ( UNIVERSAL::can(dbh, 'sprintProfile')
280               ? encode_entities(dbh->sprintProfile())
281               : 'DBIx::Profile missing sprintProfile method;'.
282                 'unpatched or too old?'                        ).
283         #"\n\n". &sprintAutoProfile().  '</PRE>'.
284         "\n\n".                         '</PRE>'.
285         '</BODY></HTML>'
286       );
287       dbh->{'private_profile'} = {};
288
289     } else { #normal redirect
290
291       $m->redirect($location);
292
293     }
294
295   }
296
297 } # end package HTML::Mason::Commands;
298
299 =head1 SUBROUTINE
300
301 =over 4
302
303 =item mason_interps [ MODE ]
304
305 Returns a list consisting of two HTML::Mason::Interp objects, the first for
306 Freeside pages, and the second for RT pages.
307
308 #MODE can be 'apache' or 'standalone'.  If not specified, defaults to 'apache'.
309
310 =cut
311
312 sub mason_interps {
313   my $mode = shift || 'apache';
314   my %opt = @_;
315
316   #my $request_class = 'HTML::Mason::Request'.
317                       #( $mode eq 'apache' ? '::ApacheHandler' : '' );
318   my $request_class = 'FS::Mason::Request';
319
320   #not entirely sure it belongs here, but what the hey
321   if ( %%%RT_ENABLED%%% ) {
322     RT::LoadConfig();
323   }
324
325   my %interp = (
326     request_class        => $request_class,
327     data_dir             => '%%%MASONDATA%%%',
328     error_mode           => 'output',
329     error_format         => 'html',
330     ignore_warnings_expr => '.',
331     comp_root            => [
332                               [ 'freeside'=>'%%%FREESIDE_DOCUMENT_ROOT%%%'    ],
333                               [ 'rt'      =>'%%%FREESIDE_DOCUMENT_ROOT%%%/rt' ],
334                             ],
335   );
336
337   $interp{out_method} = $opt{outbuf} if $mode eq 'standalone' && $opt{outbuf};
338
339   my $fs_interp = new HTML::Mason::Interp (
340     %interp,
341     escape_flags => { 'js_string' => sub {
342                         #${$_[0]} =~ s/(['\\\n])/'\\'.($1 eq "\n" ? 'n' : $1)/ge;
343                         ${$_[0]} =~ s/(['\\])/\\$1/g;
344                         ${$_[0]} =~ s/\n/\\n/g;
345                         ${$_[0]} = "'". ${$_[0]}. "'";
346                       }
347                     },
348   );
349
350   my $rt_interp = new HTML::Mason::Interp (
351     %interp,
352     escape_flags => { 'h' => \&RT::Interface::Web::EscapeUTF8 },
353     compiler     => HTML::Mason::Compiler::ToObject->new(
354                       default_escape_flags => 'h',
355                       allow_globals        => [qw(%session)],
356                     ),
357   );
358
359   ( $fs_interp, $rt_interp );
360
361 }
362
363 =back
364
365 =head1 BUGS
366
367 Lurking in the darkness...
368
369 =head1 SEE ALSO
370
371 L<HTML::Mason>, L<FS>, L<RT>
372
373 =cut
374
375 1;