4 use vars qw( @ISA @EXPORT_OK $addl_handler_use );
7 use File::Slurp qw( slurp );
8 use HTML::Mason 1.27; #http://www.masonhq.com/?ApacheModPerl2Redirect
9 use HTML::Mason::Interp;
10 use HTML::Mason::Compiler::ToObject;
12 @ISA = qw( Exporter );
13 @EXPORT_OK = qw( mason_interps );
17 FS::Mason - Initialize the Mason environment
21 use FS::Mason qw( mason_interps );
23 my( $fs_interp, $rt_interp ) = mason_interps('apache');
27 my( $fs_interp, $rt_interp ) = mason_interps('standalone'); #XXX name?
31 Initializes the Mason environment, loads all Freeside and RT libraries, etc.
35 $addl_handler_use = '';
36 my $addl_handler_use_file = '%%%FREESIDE_CONF%%%/addl_handler_use.pl';
37 if ( -e $addl_handler_use_file ) {
38 $addl_handler_use = slurp( $addl_handler_use_file );
41 # List of modules that you want to use from components (see Admin
44 package HTML::Mason::Commands;
47 use vars qw( %session );
48 use CGI 4.08 qw(-private_tempfiles); #4.08 for multi_param
50 #use CGI::Carp qw(fatalsToBrowser);
52 use List::Util qw( max min sum );
53 use List::MoreUtils qw( first_index uniq );
54 use Scalar::Util qw( blessed looks_like_number );
61 use DateTime::Format::Strptime;
62 use FS::Misc::DateTime qw( parse_datetime );
63 use FS::Misc::Geo qw( get_district );
64 use Lingua::EN::Inflect qw(PL);
65 Lingua::EN::Inflect::classical names=>0; #Categorys
70 use HTML::TreeBuilder;
71 use HTML::TableExtract qw(tree);
80 use File::Slurp qw( slurp );
81 #not actually using this yet anyway...# use IPC::Run3 0.036;
82 use Net::Whois::Raw qw(whois);
84 eval "use Net::Whois::Raw 0.32 qw(whois)";
89 use Spreadsheet::WriteExcel;
90 use Spreadsheet::WriteExcel::Utility;
91 use OLE::Storage_Lite;
92 use Excel::Writer::XLSX;
93 #use Excel::Writer::XLSX::Utility; #redundant with above
95 use Business::CreditCard 0.36; #for best-effort cardtype() (60xx as Discover)
99 use Net::Ping::External;
100 #if CPAN #7815 ever gets fixed# if ( $Net::Ping::External::VERSION <= 0.12 )
102 no warnings 'redefine';
103 eval 'sub Net::Ping::External::_ping_linux {
105 my $command = "ping -s $args{size} -c $args{count} -w $args{timeout} $args{host}";
106 return Net::Ping::External::_ping_system($command, 0);
111 use String::Approx qw(amatch);
112 use Chart::LinesPoints;
116 use HTML::Widgets::SelectLayers 0.07; #should go away in favor of
119 #use FS::geocode_Mixin; #for its code2country
120 use Locale::Currency;
121 use Locale::Currency::Format;
122 use Number::Phone::Country qw( noexport );
123 use Business::US::USPS::WebTools::AddressStandardization;
124 use Geo::GoogleEarth::Pluggable 0.16;
127 use Geo::JSON::FeatureCollection;
129 use Storable qw( nfreeze thaw );
131 use FS::UID qw( dbh datasrc driver_name );
132 use FS::Record qw( qsearch qsearchs fields dbdef
133 str2time_sql str2time_sql_closing
134 midnight_sql regexp_sql
137 use FS::ConfDefaults;
138 use FS::CGI qw(header menubar table itable ntable idiot
139 eidiot myexit http_header);
140 use FS::UI::Web qw(svc_url random_id
141 get_page_pref set_page_pref);
142 use FS::UI::Web::small_custview qw(small_custview);
143 use FS::UI::bytecount;
144 use FS::UI::REST qw( rest_auth rest_uri_remain encode_rest );
145 use FS::Msgcat qw(gettext geterror);
146 use FS::Misc qw( send_email send_fax ocr_image
147 states_hash counties cities state_label
150 use FS::Misc::eps2png qw( eps2png );
151 use FS::Report::FCC_477;
152 use FS::Report::Table;
153 use FS::Report::Table::Monthly;
154 use FS::Report::Table::Daily;
155 use FS::Report::Tax::ByName;
156 use FS::Report::Tax::All;
157 use FS::TicketSystem;
158 use FS::NetworkMonitoringSystem;
159 use FS::Tron qw( tron_lint );
161 use FS::Maketext qw( mt emt js_mt );
167 use FS::domain_record;
169 use FS::cust_bill_pay;
171 use FS::cust_credit_bill;
174 use FS::cust_main::Search qw(smart_search);
175 use FS::cust_main::Import;
176 use FS::cust_main::Import_Charges;
177 use FS::cust_main_county;
178 use FS::cust_location;
181 use FS::cust_pkg::Import;
182 use FS::part_pkg_taxclass;
183 use FS::cust_pkg_reason;
185 use FS::cust_credit_refund;
186 use FS::cust_pay_refund;
190 use FS::part_event_condition;
192 use FS::part_referral;
194 use FS::part_svc_router;
195 use FS::part_virtual_field;
199 use FS::queue qw(joblisting);
203 use FS::svc_acct_pop qw(popselector);
204 use FS::acct_rt_transaction;
210 use FS::svc_broadband;
211 use FS::svc_external;
214 use FS::part_export_option;
216 use FS::export_device;
223 use FS::payment_gateway;
224 use FS::agent_payment_gateway;
228 use FS::inventory_class;
229 use FS::inventory_item;
230 use FS::pkg_category;
233 use FS::access_user_pref;
234 use FS::access_group;
235 use FS::access_usergroup;
236 use FS::access_groupagent;
237 use FS::access_right;
240 use FS::phone_device;
244 use FS::cust_main_note;
246 use FS::cust_tax_location;
247 use FS::part_pkg_taxproduct;
248 use FS::part_pkg_taxoverride;
249 use FS::part_pkg_taxrate;
251 use FS::part_pkg_report_option;
252 use FS::cust_attachment;
254 use FS::h_inventory_item;
256 use FS::h_svc_broadband;
257 use FS::h_svc_domain;
258 #use FS::h_domain_record;
259 use FS::h_svc_external;
260 use FS::h_svc_forward;
262 #use FS::h_phone_device;
264 use FS::cust_statement;
266 use FS::cust_category;
267 use FS::prospect_main;
272 use FS::cust_pkg_discount;
273 use FS::cust_bill_pkg_discount;
274 use FS::svc_mailinglist;
276 use FS::cgp_rule_condition;
277 use FS::cgp_rule_action;
279 use FS::cust_bill_batch;
281 use FS::rate_time_interval;
282 use FS::msg_template;
285 use FS::part_pkg_discount;
291 use FS::part_pkg_vendor;
292 use FS::cust_note_class;
297 use FS::torrus_srvderive;
298 use FS::torrus_srvderive_component;
302 use FS::svc_hardware;
303 use FS::h_svc_hardware;
304 use FS::hardware_class;
305 use FS::hardware_type;
306 use FS::hardware_status;
307 use FS::did_order_item;
311 use FS::radius_group;
312 use FS::template_content;
317 use FS::legacy_cust_bill;
319 use FS::rate_tier_detail;
321 use FS::discount_plan;
323 use FS::tower_sector;
325 use FS::contact_class;
326 use FS::part_svc_class;
327 use FS::upload_target;
329 use FS::quotation_pkg;
330 use FS::quotation_pkg_discount;
331 use FS::cust_bill_void;
332 use FS::cust_bill_pkg_void;
333 use FS::cust_bill_pkg_detail_void;
334 use FS::cust_bill_pkg_display_void;
335 use FS::cust_bill_pkg_tax_location_void;
336 use FS::cust_bill_pkg_tax_rate_location_void;
337 use FS::cust_tax_exempt_pkg_void;
338 use FS::cust_bill_pkg_discount_void;
339 use FS::agent_pkg_class;
340 use FS::svc_export_machine;
341 use FS::GeocodeCache;
344 use FS::part_pkg_usage_class;
345 use FS::cust_pkg_usage;
346 use FS::part_pkg_usage_class;
347 use FS::part_pkg_usage;
348 use FS::cdr_cust_pkg_usage;
349 use FS::part_pkg_msgcat;
351 use FS::agent_currency;
352 use FS::currency_exchange;
353 use FS::part_pkg_currency;
359 use FS::vend_bill_pay;
360 use FS::sales_pkg_class;
363 use FS::invoice_mode;
364 use FS::invoice_conf;
365 use FS::cable_provider;
366 use FS::cust_credit_void;
367 use FS::discount_class;
368 use FS::alarm_system;
370 use FS::alarm_station;
372 use FS::svc_conferencing;
373 use FS::conferencing_type;
374 use FS::conferencing_quality;
376 use FS::part_pkg_usageprice;
377 use FS::cust_pkg_usageprice;
378 use FS::pbx_extension;
380 use FS::extension_device;
381 use FS::cust_main_credit_limit;
382 use FS::cust_event_fee;
384 use FS::cust_bill_pkg_fee;
385 use FS::part_fee_msgcat;
386 use FS::part_fee_usage;
389 use FS::export_batch;
390 use FS::export_batch_item;
391 use FS::part_pkg_fcc_option;
395 use FS::deploy_zone_block;
396 use FS::deploy_zone_vertex;
399 use FS::circuit_type;
400 use FS::circuit_provider;
401 use FS::circuit_termination;
403 use FS::cust_credit_source_bill_pkg;
404 use FS::prospect_contact;
405 use FS::cust_contact;
406 use FS::legacy_cust_history;
407 use FS::quotation_pkg_tax;
408 use FS::cust_pkg_reason_fee;
409 use FS::part_svc_link;
410 use FS::access_user_log;
411 use FS::report_batch;
412 use FS::report_batch;
413 use FS::report_batch;
414 use FS::report_batch;
415 use FS::password_history;
419 use FS::access_user_page_pref;
420 use FS::part_svc_msgcat;
421 use FS::saved_search;
423 use FS::Misc::DepositSlip qw( deposit_slip_pdf );
426 if ( $FS::Mason::addl_handler_use ) {
427 eval $FS::Mason::addl_handler_use;
431 if ( %%%RT_ENABLED%%% ) {
433 use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
434 use vars qw($Nobody $SystemUser);
438 use RT::Transactions;
443 use RT::ScripActions;
444 use RT::ScripConditions;
447 use RT::GroupMembers;
448 use RT::CustomFields;
449 use RT::CustomFieldValues;
450 use RT::ObjectCustomFieldValues;
452 use RT::Interface::Web::Handler;
454 #blah. not even in RT::Interface::Web::Handler, just in
455 #html/NoAuth/css/dhandler and rt-test-dependencies. ask for it here
456 #to throw a real error instead of just a mysterious unstyled RT
457 use CSS::Squish 0.06;
459 #another undeclared web UI dep (for ticket links graph)
460 use IPC::Run::SafeHandles;
462 #slow, unreliable, segfaults and is optional
463 #see rt/html/Ticket/Elements/ShowTransactionAttachments
466 #?#use File::Path qw( rmtree );
467 #?#use File::Glob qw( bsd_glob );
468 #?#use File::Spec::Unix;
474 no warnings 'redefine';
475 *CGI::redirect = sub {
481 if ( $_[0] =~ /^-/ ) {
483 $location = $opt{'-uri'};
484 my $cookie = $opt{'-cookie'};
485 $m->apache_req->err_headers_out->{'Set-cookie'} = $cookie if $cookie;
490 # false laziness w/below
491 if ( @DBIx::Profile::ISA ) {
493 if ( $FS::CurrentUser::CurrentUser->option('show_db_profile') ) {
498 qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
500 ( UNIVERSAL::can(dbh, 'sprintProfile')
501 ? encode_entities(dbh->sprintProfile())
502 : 'DBIx::Profile missing sprintProfile method;'.
503 'unpatched or too old?' ).
504 #"\n\n". &sprintAutoProfile(). '</PRE>'.
509 dbh->{'private_profile'} = {};
514 #clear db profile, but normal redirect
515 dbh->{'private_profile'} = {};
516 $m->redirect($location);
521 } else { #normal redirect
523 $m->redirect($location);
532 #warn 'include deprecated; use an HTML::Mason <& &> style include (or $m->scomp) at '. $m->callers(0)->path. "\n";
538 $m->comp('/elements/errorpage.html', @_);
541 sub errorpage_popup {
543 $m->comp('/elements/errorpage-popup.html', @_);
547 my( $location ) = @_;
550 #false laziness w/above
551 if ( @DBIx::Profile::ISA ) {
553 if ( $FS::CurrentUser::CurrentUser->option('show_db_profile') ) {
558 qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
560 ( UNIVERSAL::can(dbh, 'sprintProfile')
561 ? encode_entities(dbh->sprintProfile())
562 : 'DBIx::Profile missing sprintProfile method;'.
563 'unpatched or too old?' ).
564 #"\n\n". &sprintAutoProfile(). '</PRE>'.
569 dbh->{'private_profile'} = {};
573 #clear db profile, but normal redirect
574 dbh->{'private_profile'} = {};
575 $m->redirect($location);
579 } else { #normal redirect
581 $m->redirect($location);
587 } # end package HTML::Mason::Commands;
593 =item mason_interps [ MODE [ OPTION => VALUE ... ] ]
595 Returns a list consisting of two HTML::Mason::Interp objects, the first for
596 Freeside pages, and the second for RT pages.
598 MODE can be 'apache' or 'standalone'. If not specified, defaults to 'apache'.
600 Options and values can be passed following mode. Currently available options
603 I<outbuf> should be set to a scalar reference in standalone mode.
607 my %defang_opts = ( attribs_to_callback => ['src'], attribs_callback => sub { 1 });
610 my $mode = shift || 'apache';
613 #my $request_class = 'HTML::Mason::Request'.
614 #( $mode eq 'apache' ? '::ApacheHandler' : '' );
615 my $request_class = $mode eq 'standalone' ? 'FS::Mason::StandaloneRequest'
616 : 'FS::Mason::Request';
618 #not entirely sure it belongs here, but what the hey
619 if ( %%%RT_ENABLED%%% && $mode ne 'standalone' ) {
623 my $fs_comp_root = '%%%FREESIDE_DOCUMENT_ROOT%%%';
626 request_class => $request_class,
627 data_dir => '%%%MASONDATA%%%',
628 error_mode => 'output',
629 error_format => 'html',
630 ignore_warnings_expr => '.',
633 $interp{out_method} = $opt{outbuf} if $mode eq 'standalone' && $opt{outbuf};
635 my $html_defang = new HTML::Defang (%defang_opts);
637 #false laziness w/ FS::Maketext js_mt
638 my $js_string_sub = sub {
639 #${$_[0]} =~ s/(['\\\n])/'\\'.($1 eq "\n" ? 'n' : $1)/ge;
640 ${$_[0]} =~ s/(['\\])/\\$1/g;
641 ${$_[0]} =~ s/\r/\\r/g;
642 ${$_[0]} =~ s/\n/\\n/g;
643 # prevent premature termination of the script
644 ${$_[0]} =~ s[</script>][<\\/script>]ig;
645 ${$_[0]} = "'". ${$_[0]}. "'";
648 my $defang_sub = sub {
649 ${$_[0]} = $html_defang->defang(${$_[0]});
652 my $fs_interp = new HTML::Mason::Interp (
654 comp_root => $fs_comp_root,
655 escape_flags => { 'js_string' => $js_string_sub,
656 'defang' => $defang_sub,
658 compiler => HTML::Mason::Compiler::ToObject->new(
659 allow_globals => [qw(%session)],
663 my $rt_interp = new HTML::Mason::Interp (
666 [ 'rt' => '%%%FREESIDE_DOCUMENT_ROOT%%%/rt' ],
667 [ 'freeside' => '%%%FREESIDE_DOCUMENT_ROOT%%%' ],
669 escape_flags => { 'h' => \&RT::Interface::Web::EscapeHTML,
670 #u and j aren't used anymore? :/
671 'u' => \&RT::Interface::Web::EscapeURI,
672 'j' => \&RT::Interface::Web::EscapeJS,
673 'js_string' => $js_string_sub,
675 compiler => HTML::Mason::Compiler::ToObject->new(
676 default_escape_flags => 'h',
677 allow_globals => [qw(%session $DECODED_ARGS)],
681 ( $fs_interp, $rt_interp );
687 Per-process Apache child initialization code.
689 Calls srand() to re-seed Perl's PRNG so that multiple children do not generate
690 the same "random" numbers.
692 Works around a Net::SSLeay connection error by creating and deleting an SSL
693 context, so subsequent connections do not error out with a CTX_new (900 NET OR
694 SSL ERROR). See http://bugs.debian.org/830152
699 #my ($pool, $server) = @_; #the child process pool (APR::Pool) and the server object (Apache2::ServerRec).
707 my $bad_ctx = new_x_ctx();
708 while ( ERR_get_error() ) {}; #print_errs('CTX_new');
718 Lurking in the darkness...
722 L<HTML::Mason>, L<FS>, L<RT>