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