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