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