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