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