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