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