finish adding a feature to easily list all email addresses for an agent & send them...
[freeside.git] / htetc / handler.pl
1 #!/usr/bin/perl
2
3 package HTML::Mason;
4
5 use strict;
6 use vars qw($r);
7 use HTML::Mason 1.27; #http://www.masonhq.com/?ApacheModPerl2Redirect
8 use HTML::Mason::Interp;
9 use HTML::Mason::Compiler::ToObject;
10
11 # Bring in ApacheHandler, necessary for mod_perl integration.
12 # Uncomment the second line (and comment the first) to use
13 # Apache::Request instead of CGI.pm to parse arguments.
14 use HTML::Mason::ApacheHandler;
15 # use HTML::Mason::ApacheHandler (args_method=>'mod_perl');
16
17 ###use Module::Refresh;###
18
19 # List of modules that you want to use from components (see Admin
20 # manual for details)
21 #{  package HTML::Mason::Commands;
22 #   use CGI;
23 #}
24
25 if ( %%%RT_ENABLED%%% ) {
26  eval '
27    use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
28    use RT;
29    use vars qw($Nobody $SystemUser);
30    RT::LoadConfig();
31  ';
32  die $@ if $@;
33 }
34
35 # Create Mason objects
36
37 my %interp = (
38   request_class        => 'HTML::Mason::Request::ApacheHandler',
39   data_dir             => '%%%MASONDATA%%%',
40   error_mode           => 'output',
41   error_format         => 'html',
42   ignore_warnings_expr => '.',
43   comp_root            => [
44                             [ 'freeside' => '%%%FREESIDE_DOCUMENT_ROOT%%%'    ],
45                             [ 'rt'       => '%%%FREESIDE_DOCUMENT_ROOT%%%/rt' ],
46                           ],
47 );
48
49 my $fs_interp = new HTML::Mason::Interp (
50   %interp,
51   escape_flags => { 'js_string' => sub {
52                       #${$_[0]} =~ s/(['\\\n])/'\\'.($1 eq "\n" ? 'n' : $1)/ge;
53                       ${$_[0]} =~ s/(['\\])/\\$1/g;
54                       ${$_[0]} =~ s/\n/\\n/g;
55                       ${$_[0]} = "'". ${$_[0]}. "'";
56                     }
57                   },
58 );
59
60 my $rt_interp = new HTML::Mason::Interp (
61   %interp,
62   escape_flags => { 'h' => \&RT::Interface::Web::EscapeUTF8 },
63   compiler     => HTML::Mason::Compiler::ToObject->new(
64                     default_escape_flags => 'h',
65                     allow_globals        => [qw(%session)],
66                   ),
67 );
68
69 my $ah = new HTML::Mason::ApacheHandler (
70   interp      => $fs_interp,
71   args_method => 'CGI', #(and FS too)
72 );
73
74 # Activate the following if running httpd as root (the normal case).
75 # Resets ownership of all files created by Mason at startup.
76 #
77 #chown (Apache->server->uid, Apache->server->gid, $interp->files_written);
78
79 sub handler
80 {
81     ($r) = @_;
82
83     # If you plan to intermix images in the same directory as
84     # components, activate the following to prevent Mason from
85     # evaluating image files as components.
86     #
87     #return -1 if $r->content_type && $r->content_type !~ m|^text/|i;
88
89     #rar
90     { package HTML::Mason::Commands;
91       use strict;
92       use vars qw( $cgi $p $fsurl);
93       use vars qw( %session );
94       use CGI 3.29 qw(-private_tempfiles); #3.29 to fix RT attachment problems
95       #use CGI::Carp qw(fatalsToBrowser);
96       use List::Util qw( max min );
97       use Date::Format;
98       use Date::Parse;
99       use Time::Local;
100       use Time::Duration;
101       use DateTime;
102       use DateTime::Format::Strptime;
103       use Lingua::EN::Inflect qw(PL);
104       use Tie::IxHash;
105       use URI::URL;
106       use URI::Escape;
107       use HTML::Entities;
108       use HTML::TreeBuilder;
109       use HTML::FormatText;
110       use JSON;
111       use IO::Handle;
112       use IO::File;
113       use IO::Scalar;
114       use Net::Whois::Raw qw(whois);
115       if ( $] < 5.006 ) {
116         eval "use Net::Whois::Raw 0.32 qw(whois)";
117         die $@ if $@;
118       }
119       use Text::CSV_XS;
120       use Spreadsheet::WriteExcel;
121       use Business::CreditCard 0.30; #for mask-aware cardtype()
122       use NetAddr::IP;
123       use String::Approx qw(amatch);
124       use Chart::LinesPoints;
125       use Chart::Mountain;
126       use Color::Scheme;
127       use HTML::Widgets::SelectLayers 0.07;
128       use Locale::Country;
129       use FS;
130       use FS::UID qw( adminsuidsetup cgisuidsetup getotaker
131                       dbh datasrc driver_name
132                     );
133       use FS::Record qw(qsearch qsearchs fields dbdef str2time_sql);
134       use FS::Conf;
135       use FS::CGI qw(header menubar popurl rooturl table itable ntable idiot
136                      eidiot myexit http_header);
137       use FS::UI::Web qw(svc_url);
138       use FS::UI::Web::small_custview qw(small_custview);
139       use FS::UI::bytecount;
140       use FS::Msgcat qw(gettext geterror);
141       use FS::Misc qw( send_email send_fax states_hash counties state_label );
142       use FS::Report::Table::Monthly;
143       use FS::TicketSystem;
144
145       use FS::agent;
146       use FS::agent_type;
147       use FS::domain_record;
148       use FS::cust_bill;
149       use FS::cust_bill_pay;
150       use FS::cust_credit;
151       use FS::cust_credit_bill;
152       use FS::cust_main qw(smart_search);
153       use FS::cust_main_county;
154       use FS::part_pkg_taxclass;
155       use FS::cust_pay;
156       use FS::cust_pkg;
157       use FS::cust_pkg_reason;
158       use FS::cust_refund;
159       use FS::cust_credit_refund;
160       use FS::cust_pay_refund;
161       use FS::cust_svc;
162       use FS::nas;
163       use FS::part_bill_event;
164       use FS::part_pkg;
165       use FS::part_referral;
166       use FS::part_svc;
167       use FS::part_svc_router;
168       use FS::part_virtual_field;
169       use FS::pay_batch;
170       use FS::pkg_svc;
171       use FS::port;
172       use FS::queue qw(joblisting);
173       use FS::raddb;
174       use FS::session;
175       use FS::svc_acct;
176       use FS::svc_acct_pop qw(popselector);
177       use FS::svc_domain;
178       use FS::svc_forward;
179       use FS::svc_www;
180       use FS::router;
181       use FS::addr_block;
182       use FS::svc_broadband;
183       use FS::svc_external;
184       use FS::type_pkgs;
185       use FS::part_export;
186       use FS::part_export_option;
187       use FS::export_svc;
188       use FS::msgcat;
189       use FS::rate;
190       use FS::rate_region;
191       use FS::rate_prefix;
192       use FS::payment_gateway;
193       use FS::agent_payment_gateway;
194       use FS::XMLRPC;
195       use FS::payby;
196       use FS::cdr;
197       use FS::inventory_class;
198       use FS::inventory_item;
199       use FS::pkg_class;
200       use FS::access_user;
201       use FS::access_group;
202       use FS::access_usergroup;
203       use FS::access_groupagent;
204       use FS::access_right;
205       use FS::AccessRight;
206       use FS::svc_phone;
207       use FS::reason_type;
208       use FS::reason;
209       use FS::cust_main_note;
210
211       if ( %%%RT_ENABLED%%% ) {
212         eval '
213           use RT::Tickets;
214           use RT::Transactions;
215           use RT::Users;
216           use RT::CurrentUser;
217           use RT::Templates;
218           use RT::Queues;
219           use RT::ScripActions;
220           use RT::ScripConditions;
221           use RT::Scrips;
222           use RT::Groups;
223           use RT::GroupMembers;
224           use RT::CustomFields;
225           use RT::CustomFieldValues;
226           use RT::ObjectCustomFieldValues;
227
228           use RT::Interface::Web;
229           use MIME::Entity;
230           use Text::Wrapper;
231           use CGI::Cookie;
232           use Time::ParseDate;
233           use HTML::Scrubber;
234           #use Text::Quoted; #slow, unreliable, segfaults and is optional
235           use Time::HiRes;
236         ';
237         die $@ if $@;
238       }
239
240       *CGI::redirect = sub {
241         my( $self, $location ) = @_;
242         use vars qw($m);
243
244         # false laziness w/below
245         if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
246
247           my $page =
248             qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
249             '<BR><BR><PRE>'.
250               ( UNIVERSAL::can(dbh, 'sprintProfile')
251                   ? encode_entities(dbh->sprintProfile())
252                   : 'DBIx::Profile missing sprintProfile method;'.
253                     'unpatched or too old?'                        ).
254             #"\n\n". &sprintAutoProfile().  '</PRE>'.
255             "\n\n".                         '</PRE>'.
256             '</BODY></HTML>';
257           dbh->{'private_profile'} = {};
258           return $page;
259
260         } else { #normal redirect
261
262           $m->redirect($location);
263           '';
264
265         }
266
267       };
268       
269       if ( $HTML::Mason::r->filename !~ /\/rt\/.*NoAuth/ ) { #not RT images/JS
270
271         $cgi = new CGI;
272         &cgisuidsetup($cgi);
273         #&cgisuidsetup($r);
274         $p = popurl(2);
275         $fsurl = rooturl();
276
277       } elsif ( $HTML::Mason::r->filename =~ /\/rt\/REST\/.*NoAuth/ ) {
278
279         #need to log somebody in for the mail gw
280
281         ##old installs w/fs_selfs or selfserv??
282         #&adminsuidsetup('fs_selfservice');
283
284         &adminsuidsetup('fs_queue');
285
286       }
287
288       sub include {
289         use vars qw($m);
290         $m->scomp(@_);
291       }
292
293       sub errorpage {
294         use vars qw($m);
295         $m->comp('/elements/errorpage.html', @_);
296       }
297
298       sub redirect {
299         my( $location ) = @_;
300         use vars qw($m);
301         $m->clear_buffer;
302         #false laziness w/above
303         if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
304
305           $m->print(
306             qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
307             '<BR><BR><PRE>'.
308               ( UNIVERSAL::can(dbh, 'sprintProfile')
309                   ? encode_entities(dbh->sprintProfile())
310                   : 'DBIx::Profile missing sprintProfile method;'.
311                     'unpatched or too old?'                        ).
312             #"\n\n". &sprintAutoProfile().  '</PRE>'.
313             "\n\n".                         '</PRE>'.
314             '</BODY></HTML>'
315           );
316           dbh->{'private_profile'} = {};
317
318           #whew.  removing this is all that's needed to fix the annoying
319           #blank-page-instead-of-profiling-redirect-when-called-from-an-include
320           #bug triggered by mason 1.32
321           #my $rv = $m->abort(200);
322
323         } else { #normal redirect
324
325           $m->redirect($location);
326
327         }
328
329       }
330
331     } # end package HTML::Mason::Commands;
332
333     ###Module::Refresh->refresh;###
334
335     $r->content_type('text/html');
336     #eorar
337
338     my $headers = $r->headers_out;
339     $headers->{'Cache-control'} = 'no-cache';
340     #$r->no_cache(1);
341     $headers->{'Expires'} = '0';
342
343 #    $r->send_http_header;
344
345     if ( $r->filename =~ /\/rt\// ) { #RT
346
347       $ah->interp($rt_interp);
348
349       local $SIG{__WARN__};
350       local $SIG{__DIE__};
351
352       RT::Init();
353
354       # We don't need to handle non-text, non-xml items
355       return -1 if defined( $r->content_type )
356                 && $r->content_type !~ m!(^text/|\bxml\b)!io;
357
358     } else {
359
360       $ah->interp($fs_interp);
361
362     }
363
364     my %session;
365     my $status;
366     eval { $status = $ah->handle_request($r); };
367 #!!
368 #    if ( $@ ) {
369 #       $RT::Logger->crit($@);
370 #    }
371     warn $@ if $@;
372
373     undef %session;
374
375 #!!
376 #    if ($RT::Handle->TransactionDepth) {
377 #       $RT::Handle->ForceRollback;
378 #       $RT::Logger->crit(
379 #"Transaction not committed. Usually indicates a software fault. Data loss may have occurred"
380 #       );
381 #    }
382
383     $status;
384 }
385
386 1;