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