add DateTime as a proper rather than hidden dependency
[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::Format::Strptime;
101       use Lingua::EN::Inflect qw(PL);
102       use Tie::IxHash;
103       use URI::Escape;
104       use HTML::Entities;
105       use JSON;
106       use IO::Handle;
107       use IO::File;
108       use IO::Scalar;
109       use Net::Whois::Raw qw(whois);
110       if ( $] < 5.006 ) {
111         eval "use Net::Whois::Raw 0.32 qw(whois)";
112         die $@ if $@;
113       }
114       use Text::CSV_XS;
115       use Spreadsheet::WriteExcel;
116       use Business::CreditCard;
117       use String::Approx qw(amatch);
118       use Chart::LinesPoints;
119       use Chart::Mountain;
120       use Color::Scheme;
121       use HTML::Widgets::SelectLayers 0.05;
122       use Locale::Country;
123       use FS;
124       use FS::UID qw(cgisuidsetup dbh getotaker datasrc driver_name);
125       use FS::Record qw(qsearch qsearchs fields dbdef);
126       use FS::Conf;
127       use FS::CGI qw(header menubar popurl rooturl table itable ntable idiot
128                      eidiot small_custview myexit http_header);
129       use FS::UI::Web;
130       use FS::Msgcat qw(gettext geterror);
131       use FS::Misc qw( send_email send_fax states_hash state_label );
132       use FS::Report::Table::Monthly;
133       use FS::TicketSystem;
134
135       use FS::agent;
136       use FS::agent_type;
137       use FS::domain_record;
138       use FS::cust_bill;
139       use FS::cust_bill_pay;
140       use FS::cust_credit;
141       use FS::cust_credit_bill;
142       use FS::cust_main qw(smart_search);
143       use FS::cust_main_county;
144       use FS::cust_pay;
145       use FS::cust_pkg;
146       use FS::cust_pkg_reason;
147       use FS::cust_refund;
148       use FS::cust_svc;
149       use FS::nas;
150       use FS::part_bill_event;
151       use FS::part_pkg;
152       use FS::part_referral;
153       use FS::part_svc;
154       use FS::part_svc_router;
155       use FS::part_virtual_field;
156       use FS::pay_batch;
157       use FS::pkg_svc;
158       use FS::port;
159       use FS::queue qw(joblisting);
160       use FS::raddb;
161       use FS::session;
162       use FS::svc_acct;
163       use FS::svc_acct_pop qw(popselector);
164       use FS::svc_domain;
165       use FS::svc_forward;
166       use FS::svc_www;
167       use FS::router;
168       use FS::addr_block;
169       use FS::svc_broadband;
170       use FS::svc_external;
171       use FS::type_pkgs;
172       use FS::part_export;
173       use FS::part_export_option;
174       use FS::export_svc;
175       use FS::msgcat;
176       use FS::rate;
177       use FS::rate_region;
178       use FS::rate_prefix;
179       use FS::payment_gateway;
180       use FS::agent_payment_gateway;
181       use FS::XMLRPC;
182       use FS::payby;
183       use FS::cdr;
184       use FS::inventory_class;
185       use FS::inventory_item;
186       use FS::pkg_class;
187       use FS::access_user;
188       use FS::access_group;
189       use FS::access_usergroup;
190       use FS::access_groupagent;
191       use FS::access_right;
192       use FS::AccessRight;
193       use FS::svc_phone;
194       use FS::reason_type;
195       use FS::reason;
196       use FS::cust_main_note;
197
198       if ( %%%RT_ENABLED%%% ) {
199         eval '
200           use RT::Tickets;
201           use RT::Transactions;
202           use RT::Users;
203           use RT::CurrentUser;
204           use RT::Templates;
205           use RT::Queues;
206           use RT::ScripActions;
207           use RT::ScripConditions;
208           use RT::Scrips;
209           use RT::Groups;
210           use RT::GroupMembers;
211           use RT::CustomFields;
212           use RT::CustomFieldValues;
213           use RT::ObjectCustomFieldValues;
214
215           use RT::Interface::Web;
216           use MIME::Entity;
217           use Text::Wrapper;
218           use CGI::Cookie;
219           use Time::ParseDate;
220           use HTML::Scrubber;
221           use Text::Quoted;
222         ';
223         die $@ if $@;
224       }
225
226       *CGI::redirect = sub {
227         my( $self, $location ) = @_;
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       unless ( $HTML::Mason::r->filename =~ /\/rt\/.*NoAuth/ ) { #RT
256         $cgi = new CGI;
257         &cgisuidsetup($cgi);
258         #&cgisuidsetup($r);
259         $p = popurl(2);
260         $fsurl = rooturl();
261       }
262
263       sub include {
264         use vars qw($m);
265         $m->scomp(@_);
266       }
267
268       sub redirect {
269         my( $location ) = @_;
270         use vars qw($m);
271         $m->clear_buffer;
272         #false laziness w/above
273         if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
274
275           $m->print(
276             qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
277             '<BR><BR><PRE>'.
278               ( UNIVERSAL::can(dbh, 'sprintProfile')
279                   ? encode_entities(dbh->sprintProfile())
280                   : 'DBIx::Profile missing sprintProfile method;'.
281                     'unpatched or too old?'                        ).
282             #"\n\n". &sprintAutoProfile().  '</PRE>'.
283             "\n\n".                         '</PRE>'.
284             '</BODY></HTML>'
285           );
286           dbh->{'private_profile'} = {};
287
288           #whew.  removing this is all that's needed to fix the annoying
289           #blank-page-instead-of-profiling-redirect-when-called-from-an-include
290           #bug triggered by mason 1.32
291           #my $rv = $m->abort(200);
292
293         } else { #normal redirect
294
295           $m->redirect($location);
296
297         }
298
299       }
300
301     } # end package HTML::Mason::Commands;
302
303     ###Module::Refresh->refresh;###
304
305     $r->content_type('text/html');
306     #eorar
307
308     my $headers = $r->headers_out;
309     $headers->{'Cache-control'} = 'no-cache';
310     #$r->no_cache(1);
311     $headers->{'Expires'} = '0';
312
313 #    $r->send_http_header;
314
315     #$ah->interp->remove_escape('h');
316
317     if ( $r->filename =~ /\/rt\// ) { #RT
318       #warn "processing RT file". $r->filename. "; escaping for RT\n";
319
320       # MasonX::Request::ExtendedCompRoot
321       #$ah->interp->comp_root( '/rt'. $ah->interp->comp_root() );
322
323       $ah->interp->set_escape( h => \&RT::Interface::Web::EscapeUTF8 );
324
325       local $SIG{__WARN__};
326       local $SIG{__DIE__};
327
328       RT::Init();
329
330       # We don't need to handle non-text, non-xml items
331       return -1 if defined( $r->content_type ) && $r->content_type !~ m!(^text/|\bxml\b)!io;
332
333     } else {
334       $ah->interp->set_escape( 'h' => sub { ${$_[0]}; } );
335     }
336
337     my %session;
338     my $status;
339     eval { $status = $ah->handle_request($r); };
340 #!!
341 #    if ( $@ ) {
342 #       $RT::Logger->crit($@);
343 #    }
344
345     undef %session;
346
347 #!!
348 #    if ($RT::Handle->TransactionDepth) {
349 #       $RT::Handle->ForceRollback;
350 #       $RT::Logger->crit(
351 #"Transaction not committed. Usually indicates a software fault. Data loss may have occurred"
352 #       );
353 #    }
354
355     $status;
356 }
357
358 1;