- bring prepaid support into this century (close: Bug#1124)
[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 # List of modules that you want to use from components (see Admin
24 # manual for details)
25 #{  package HTML::Mason::Commands;
26 #   use CGI;
27 #}
28
29 # Create Mason objects
30 #
31
32 #my $parser = new HTML::Mason::Parser;
33 #my $interp = new HTML::Mason::Interp (parser=>$parser,
34 #                                      comp_root=>'/var/www/masondocs',
35 #                                      data_dir=>'/usr/local/etc/freeside/masondata',
36 #                                      out_mode=>'stream',
37 #                                     );
38
39 use vars qw($r);
40
41 if ( %%%RT_ENABLED%%% ) {
42  eval '
43    use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
44    use RT;
45    use vars qw($Nobody $SystemUser);
46    RT::LoadConfig();
47  ';
48  die $@ if $@;
49
50
51 }
52
53
54 my $ah = new HTML::Mason::ApacheHandler (
55   #interp => $interp,
56   #auto_send_headers => 0,
57   comp_root=> [
58                 [ 'freeside' => '%%%FREESIDE_DOCUMENT_ROOT%%%'    ],
59                 [ 'rt'       => '%%%FREESIDE_DOCUMENT_ROOT%%%/rt' ],
60               ],
61   data_dir=>'/usr/local/etc/freeside/masondata',
62   #out_mode=>'stream',
63
64   #RT
65   args_method => 'CGI',
66   default_escape_flags => 'h',
67   allow_globals => [qw(%session)],
68   #autoflush => 1,
69 );
70
71 # Activate the following if running httpd as root (the normal case).
72 # Resets ownership of all files created by Mason at startup.
73 #
74 #chown (Apache->server->uid, Apache->server->gid, $interp->files_written);
75
76 sub handler
77 {
78     ($r) = @_;
79
80     # If you plan to intermix images in the same directory as
81     # components, activate the following to prevent Mason from
82     # evaluating image files as components.
83     #
84     #return -1 if $r->content_type && $r->content_type !~ m|^text/|i;
85
86     #rar
87     { package HTML::Mason::Commands;
88       use strict;
89       use vars qw( $cgi $p );
90       use vars qw( %session );
91       use CGI 2.47 qw(-private_tempfiles);
92       #use CGI::Carp qw(fatalsToBrowser);
93       use Date::Format;
94       use Date::Parse;
95       use Time::Local;
96       use Time::Duration;
97       use Tie::IxHash;
98       use HTML::Entities;
99       use IO::Handle;
100       use IO::File;
101       use IO::Scalar;
102       use Net::Whois::Raw qw(whois);
103       if ( $] < 5.006 ) {
104         eval "use Net::Whois::Raw 0.32 qw(whois)";
105         die $@ if $@;
106       }
107       use Text::CSV_XS;
108       use Spreadsheet::WriteExcel;
109       use Business::CreditCard;
110       use String::Approx qw(amatch);
111       use Chart::LinesPoints;
112       use HTML::Widgets::SelectLayers 0.03;
113       use FS;
114       use FS::UID qw(cgisuidsetup dbh getotaker datasrc driver_name);
115       use FS::Record qw(qsearch qsearchs fields dbdef);
116       use FS::Conf;
117       use FS::CGI qw(header menubar popurl table itable ntable idiot eidiot
118                      small_custview myexit http_header);
119       use FS::UI::Web;
120       use FS::Msgcat qw(gettext geterror);
121       use FS::Misc qw( send_email );
122       use FS::Report::Table::Monthly;
123       use FS::TicketSystem;
124
125       use FS::agent;
126       use FS::agent_type;
127       use FS::domain_record;
128       use FS::cust_bill;
129       use FS::cust_bill_pay;
130       use FS::cust_credit;
131       use FS::cust_credit_bill;
132       use FS::cust_main qw(smart_search);
133       use FS::cust_main_county;
134       use FS::cust_pay;
135       use FS::cust_pkg;
136       use FS::cust_refund;
137       use FS::cust_svc;
138       use FS::nas;
139       use FS::part_bill_event;
140       use FS::part_pkg;
141       use FS::part_referral;
142       use FS::part_svc;
143       use FS::part_svc_router;
144       use FS::part_virtual_field;
145       use FS::pkg_svc;
146       use FS::port;
147       use FS::queue qw(joblisting);
148       use FS::raddb;
149       use FS::session;
150       use FS::svc_acct;
151       use FS::svc_acct_pop qw(popselector);
152       use FS::svc_domain;
153       use FS::svc_forward;
154       use FS::svc_www;
155       use FS::router;
156       use FS::addr_block;
157       use FS::svc_broadband;
158       use FS::svc_external;
159       use FS::type_pkgs;
160       use FS::part_export;
161       use FS::part_export_option;
162       use FS::export_svc;
163       use FS::msgcat;
164       use FS::rate;
165       use FS::rate_region;
166       use FS::rate_prefix;
167       use FS::XMLRPC;
168
169       if ( %%%RT_ENABLED%%% ) {
170         eval '
171           use RT::Tickets;
172           use RT::Transactions;
173           use RT::Users;
174           use RT::CurrentUser;
175           use RT::Templates;
176           use RT::Queues;
177           use RT::ScripActions;
178           use RT::ScripConditions;
179           use RT::Scrips;
180           use RT::Groups;
181           use RT::GroupMembers;
182           use RT::CustomFields;
183           use RT::CustomFieldValues;
184           use RT::TicketCustomFieldValues;
185
186           use RT::Interface::Web;
187           use MIME::Entity;
188           use Text::Wrapper;
189           use CGI::Cookie;
190           use Time::ParseDate;
191           use HTML::Scrubber;
192           use Text::Quoted;
193         ';
194         die $@ if $@;
195       }
196
197       *CGI::redirect = sub {
198         my( $self, $location ) = @_;
199         use vars qw($m);
200
201         if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
202
203           my $page =
204             qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
205             '<BR><BR><PRE>'.
206               ( UNIVERSAL::can(dbh, 'sprintProfile')
207                   ? encode_entities(dbh->sprintProfile())
208                   : 'DBIx::Profile missing sprintProfile method;'.
209                     'unpatched or too old?'                        ).
210             #"\n\n". &sprintAutoProfile().  '</PRE>'.
211             "\n\n".                         '</PRE>'.
212             '</BODY></HTML>';
213           dbh->{'private_profile'} = {};
214           return $page;
215
216         } else { #normal redirect
217
218           $m->redirect($location);
219           '';
220
221         }
222
223       };
224       
225       unless ( $HTML::Mason::r->filename =~ /\/rt\/.*NoAuth/ ) { #RT
226         $cgi = new CGI;
227         &cgisuidsetup($cgi);
228         #&cgisuidsetup($r);
229         $p = popurl(2);
230       }
231
232
233       sub include {
234         use vars qw($m);
235         $m->scomp(@_);
236       }
237
238       sub redirect {
239         my( $location ) = @_;
240         use vars qw($m);
241         $m->clear_buffer;
242         #false laziness w/above
243         if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
244
245           $m->print(
246             qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
247             '<BR><BR><PRE>'.
248               ( UNIVERSAL::can(dbh, 'sprintProfile')
249                   ? encode_entities(dbh->sprintProfile())
250                   : 'DBIx::Profile missing sprintProfile method;'.
251                     'unpatched or too old?'                        ).
252             #"\n\n". &sprintAutoProfile().  '</PRE>'.
253             "\n\n".                         '</PRE>'.
254             '</BODY></HTML>'
255           );
256           dbh->{'private_profile'} = {};
257
258           $m->abort(200);
259
260         } else { #normal redirect
261
262           $m->redirect($location);
263
264         }
265
266       }
267
268     } # end package HTML::Mason::Commands;
269
270     $r->content_type('text/html');
271     #eorar
272
273     my $headers = $r->headers_out;
274     $headers->{'Cache-control'} = 'no-cache';
275     #$r->no_cache(1);
276     $headers->{'Expires'} = '0';
277
278 #    $r->send_http_header;
279
280     #$ah->interp->remove_escape('h');
281
282     if ( $r->filename =~ /\/rt\// ) { #RT
283       #warn "processing RT file". $r->filename. "; escaping for RT\n";
284
285       # MasonX::Request::ExtendedCompRoot
286       #$ah->interp->comp_root( '/rt'. $ah->interp->comp_root() );
287
288       $ah->interp->set_escape( h => \&RT::Interface::Web::EscapeUTF8 );
289
290       local $SIG{__WARN__};
291       local $SIG{__DIE__};
292
293       RT::Init();
294
295       # We don't need to handle non-text, non-xml items
296       return -1 if defined( $r->content_type ) && $r->content_type !~ m!(^text/|\bxml\b)!io;
297
298     } else {
299       $ah->interp->set_escape( 'h' => sub { ${$_[0]}; } );
300     }
301
302     my %session;
303     my $status;
304     eval { $status = $ah->handle_request($r); };
305 #!!
306 #    if ( $@ ) {
307 #       $RT::Logger->crit($@);
308 #    }
309
310     undef %session;
311
312 #!!
313 #    if ($RT::Handle->TransactionDepth) {
314 #       $RT::Handle->ForceRollback;
315 #       $RT::Logger->crit(
316 #"Transaction not committed. Usually indicates a software fault. Data loss may have occurred"
317 #       );
318 #    }
319
320     $status;
321 }
322
323 1;