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