infrastructure for easier schema changes, and: add payment_gateway, payment_gateway_o...
[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 URI::Escape;
99       use HTML::Entities;
100       use IO::Handle;
101       use IO::File;
102       use IO::Scalar;
103       use Net::Whois::Raw qw(whois);
104       if ( $] < 5.006 ) {
105         eval "use Net::Whois::Raw 0.32 qw(whois)";
106         die $@ if $@;
107       }
108       use Text::CSV_XS;
109       use Spreadsheet::WriteExcel;
110       use Business::CreditCard;
111       use String::Approx qw(amatch);
112       use Chart::LinesPoints;
113       use HTML::Widgets::SelectLayers 0.03;
114       use FS;
115       use FS::UID qw(cgisuidsetup dbh getotaker datasrc driver_name);
116       use FS::Record qw(qsearch qsearchs fields dbdef);
117       use FS::Conf;
118       use FS::CGI qw(header menubar popurl table itable ntable idiot eidiot
119                      small_custview myexit http_header);
120       use FS::UI::Web;
121       use FS::Msgcat qw(gettext geterror);
122       use FS::Misc qw( send_email send_fax );
123       use FS::Report::Table::Monthly;
124       use FS::TicketSystem;
125
126       use FS::agent;
127       use FS::agent_type;
128       use FS::domain_record;
129       use FS::cust_bill;
130       use FS::cust_bill_pay;
131       use FS::cust_credit;
132       use FS::cust_credit_bill;
133       use FS::cust_main qw(smart_search);
134       use FS::cust_main_county;
135       use FS::cust_pay;
136       use FS::cust_pkg;
137       use FS::cust_refund;
138       use FS::cust_svc;
139       use FS::nas;
140       use FS::part_bill_event;
141       use FS::part_pkg;
142       use FS::part_referral;
143       use FS::part_svc;
144       use FS::part_svc_router;
145       use FS::part_virtual_field;
146       use FS::pkg_svc;
147       use FS::port;
148       use FS::queue qw(joblisting);
149       use FS::raddb;
150       use FS::session;
151       use FS::svc_acct;
152       use FS::svc_acct_pop qw(popselector);
153       use FS::svc_domain;
154       use FS::svc_forward;
155       use FS::svc_www;
156       use FS::router;
157       use FS::addr_block;
158       use FS::svc_broadband;
159       use FS::svc_external;
160       use FS::type_pkgs;
161       use FS::part_export;
162       use FS::part_export_option;
163       use FS::export_svc;
164       use FS::msgcat;
165       use FS::rate;
166       use FS::rate_region;
167       use FS::rate_prefix;
168       use FS::payment_gateway;
169       use FS::agent_payment_gateway;
170       use FS::XMLRPC;
171
172       if ( %%%RT_ENABLED%%% ) {
173         eval '
174           use RT::Tickets;
175           use RT::Transactions;
176           use RT::Users;
177           use RT::CurrentUser;
178           use RT::Templates;
179           use RT::Queues;
180           use RT::ScripActions;
181           use RT::ScripConditions;
182           use RT::Scrips;
183           use RT::Groups;
184           use RT::GroupMembers;
185           use RT::CustomFields;
186           use RT::CustomFieldValues;
187           use RT::TicketCustomFieldValues;
188
189           use RT::Interface::Web;
190           use MIME::Entity;
191           use Text::Wrapper;
192           use CGI::Cookie;
193           use Time::ParseDate;
194           use HTML::Scrubber;
195           use Text::Quoted;
196         ';
197         die $@ if $@;
198       }
199
200       *CGI::redirect = sub {
201         my( $self, $location ) = @_;
202         use vars qw($m);
203
204         if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
205
206           my $page =
207             qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
208             '<BR><BR><PRE>'.
209               ( UNIVERSAL::can(dbh, 'sprintProfile')
210                   ? encode_entities(dbh->sprintProfile())
211                   : 'DBIx::Profile missing sprintProfile method;'.
212                     'unpatched or too old?'                        ).
213             #"\n\n". &sprintAutoProfile().  '</PRE>'.
214             "\n\n".                         '</PRE>'.
215             '</BODY></HTML>';
216           dbh->{'private_profile'} = {};
217           return $page;
218
219         } else { #normal redirect
220
221           $m->redirect($location);
222           '';
223
224         }
225
226       };
227       
228       unless ( $HTML::Mason::r->filename =~ /\/rt\/.*NoAuth/ ) { #RT
229         $cgi = new CGI;
230         &cgisuidsetup($cgi);
231         #&cgisuidsetup($r);
232         $p = popurl(2);
233       }
234
235
236       sub include {
237         use vars qw($m);
238         $m->scomp(@_);
239       }
240
241       sub redirect {
242         my( $location ) = @_;
243         use vars qw($m);
244         $m->clear_buffer;
245         #false laziness w/above
246         if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
247
248           $m->print(
249             qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
250             '<BR><BR><PRE>'.
251               ( UNIVERSAL::can(dbh, 'sprintProfile')
252                   ? encode_entities(dbh->sprintProfile())
253                   : 'DBIx::Profile missing sprintProfile method;'.
254                     'unpatched or too old?'                        ).
255             #"\n\n". &sprintAutoProfile().  '</PRE>'.
256             "\n\n".                         '</PRE>'.
257             '</BODY></HTML>'
258           );
259           dbh->{'private_profile'} = {};
260
261           $m->abort(200);
262
263         } else { #normal redirect
264
265           $m->redirect($location);
266
267         }
268
269       }
270
271     } # end package HTML::Mason::Commands;
272
273     $r->content_type('text/html');
274     #eorar
275
276     my $headers = $r->headers_out;
277     $headers->{'Cache-control'} = 'no-cache';
278     #$r->no_cache(1);
279     $headers->{'Expires'} = '0';
280
281 #    $r->send_http_header;
282
283     #$ah->interp->remove_escape('h');
284
285     if ( $r->filename =~ /\/rt\// ) { #RT
286       #warn "processing RT file". $r->filename. "; escaping for RT\n";
287
288       # MasonX::Request::ExtendedCompRoot
289       #$ah->interp->comp_root( '/rt'. $ah->interp->comp_root() );
290
291       $ah->interp->set_escape( h => \&RT::Interface::Web::EscapeUTF8 );
292
293       local $SIG{__WARN__};
294       local $SIG{__DIE__};
295
296       RT::Init();
297
298       # We don't need to handle non-text, non-xml items
299       return -1 if defined( $r->content_type ) && $r->content_type !~ m!(^text/|\bxml\b)!io;
300
301     } else {
302       $ah->interp->set_escape( 'h' => sub { ${$_[0]}; } );
303     }
304
305     my %session;
306     my $status;
307     eval { $status = $ah->handle_request($r); };
308 #!!
309 #    if ( $@ ) {
310 #       $RT::Logger->crit($@);
311 #    }
312
313     undef %session;
314
315 #!!
316 #    if ($RT::Handle->TransactionDepth) {
317 #       $RT::Handle->ForceRollback;
318 #       $RT::Logger->crit(
319 #"Transaction not committed. Usually indicates a software fault. Data loss may have occurred"
320 #       );
321 #    }
322
323     $status;
324 }
325
326 1;