remove SCHEMA_CHANGE
[freeside.git] / htetc / handler.pl
1 #!/usr/bin/perl
2
3 package HTML::Mason;
4
5 use strict;
6 use vars qw($r);
7 use HTML::Mason 1.27; #http://www.masonhq.com/?ApacheModPerl2Redirect
8 use HTML::Mason::Interp;
9 use HTML::Mason::Compiler::ToObject;
10
11 # Bring in ApacheHandler, necessary for mod_perl integration.
12 # Uncomment the second line (and comment the first) to use
13 # Apache::Request instead of CGI.pm to parse arguments.
14 use HTML::Mason::ApacheHandler;
15 # use HTML::Mason::ApacheHandler (args_method=>'mod_perl');
16
17 ###use Module::Refresh;###
18
19 # List of modules that you want to use from components (see Admin
20 # manual for details)
21 #{  package HTML::Mason::Commands;
22 #   use CGI;
23 #}
24
25 if ( %%%RT_ENABLED%%% ) {
26  eval '
27    use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
28    use RT;
29    use vars qw($Nobody $SystemUser);
30    RT::LoadConfig();
31  ';
32  die $@ if $@;
33 }
34
35 # Create Mason objects
36
37 my %interp = (
38   request_class        => 'HTML::Mason::Request::ApacheHandler',
39   data_dir             => '%%%MASONDATA%%%',
40   error_mode           => 'output',
41   error_format         => 'html',
42   ignore_warnings_expr => '.',
43   comp_root            => [
44                             [ 'freeside' => '%%%FREESIDE_DOCUMENT_ROOT%%%'    ],
45                             [ 'rt'       => '%%%FREESIDE_DOCUMENT_ROOT%%%/rt' ],
46                           ],
47 );
48
49 my $fs_interp = new HTML::Mason::Interp (
50   %interp,
51   escape_flags => { 'js_string' => sub {
52                       #${$_[0]} =~ s/(['\\\n])/'\\'.($1 eq "\n" ? 'n' : $1)/ge;
53                       ${$_[0]} =~ s/(['\\])/\\$1/g;
54                       ${$_[0]} =~ s/\n/\\n/g;
55                       ${$_[0]} = "'". ${$_[0]}. "'";
56                     }
57                   },
58 );
59
60 my $rt_interp = new HTML::Mason::Interp (
61   %interp,
62   escape_flags => { 'h' => \&RT::Interface::Web::EscapeUTF8 },
63   compiler     => HTML::Mason::Compiler::ToObject->new(
64                     default_escape_flags => 'h',
65                     allow_globals        => [qw(%session)],
66                   ),
67 );
68
69 my $ah = new HTML::Mason::ApacheHandler (
70   interp      => $fs_interp,
71   args_method => 'CGI', #(and FS too)
72 );
73
74 # Activate the following if running httpd as root (the normal case).
75 # Resets ownership of all files created by Mason at startup.
76 #
77 #chown (Apache->server->uid, Apache->server->gid, $interp->files_written);
78
79 sub handler
80 {
81     ($r) = @_;
82
83     # If you plan to intermix images in the same directory as
84     # components, activate the following to prevent Mason from
85     # evaluating image files as components.
86     #
87     #return -1 if $r->content_type && $r->content_type !~ m|^text/|i;
88
89     #rar
90     { package HTML::Mason::Commands;
91       use strict;
92       use vars qw( $cgi $p $fsurl);
93       use vars qw( %session );
94       use CGI 2.47 qw(-private_tempfiles);
95       #use CGI::Carp qw(fatalsToBrowser);
96       use List::Util qw( max min );
97       use Date::Format;
98       use Date::Parse;
99       use Time::Local;
100       use Time::Duration;
101       use DateTime;
102       use DateTime::Format::Strptime;
103       use Lingua::EN::Inflect qw(PL);
104       use Tie::IxHash;
105       use URI::Escape;
106       use HTML::Entities;
107       use JSON;
108       use IO::Handle;
109       use IO::File;
110       use IO::Scalar;
111       use Net::Whois::Raw qw(whois);
112       if ( $] < 5.006 ) {
113         eval "use Net::Whois::Raw 0.32 qw(whois)";
114         die $@ if $@;
115       }
116       use Text::CSV_XS;
117       use Spreadsheet::WriteExcel;
118       use Business::CreditCard 0.30; #for mask-aware cardtype()
119       use NetAddr::IP;
120       use String::Approx qw(amatch);
121       use Chart::LinesPoints;
122       use Chart::Mountain;
123       use Color::Scheme;
124       use HTML::Widgets::SelectLayers 0.07;
125       use Locale::Country;
126       use FS;
127       use FS::UID qw(cgisuidsetup dbh getotaker datasrc driver_name);
128       use FS::Record qw(qsearch qsearchs fields dbdef str2time_sql);
129       use FS::Conf;
130       use FS::CGI qw(header menubar popurl rooturl table itable ntable idiot
131                      eidiot small_custview myexit http_header);
132       use FS::UI::Web qw(svc_url);
133       use FS::UI::bytecount;
134       use FS::Msgcat qw(gettext geterror);
135       use FS::Misc qw( send_email send_fax states_hash counties state_label );
136       use FS::Report::Table::Monthly;
137       use FS::TicketSystem;
138
139       use FS::agent;
140       use FS::agent_type;
141       use FS::domain_record;
142       use FS::cust_bill;
143       use FS::cust_bill_pay;
144       use FS::cust_credit;
145       use FS::cust_credit_bill;
146       use FS::cust_main qw(smart_search);
147       use FS::cust_main_county;
148       use FS::part_pkg_taxclass;
149       use FS::cust_pay;
150       use FS::cust_pkg;
151       use FS::cust_pkg_reason;
152       use FS::cust_refund;
153       use FS::cust_svc;
154       use FS::nas;
155       use FS::part_bill_event;
156       use FS::part_pkg;
157       use FS::part_referral;
158       use FS::part_svc;
159       use FS::part_svc_router;
160       use FS::part_virtual_field;
161       use FS::pay_batch;
162       use FS::pkg_svc;
163       use FS::port;
164       use FS::queue qw(joblisting);
165       use FS::raddb;
166       use FS::session;
167       use FS::svc_acct;
168       use FS::svc_acct_pop qw(popselector);
169       use FS::svc_domain;
170       use FS::svc_forward;
171       use FS::svc_www;
172       use FS::router;
173       use FS::addr_block;
174       use FS::svc_broadband;
175       use FS::svc_external;
176       use FS::type_pkgs;
177       use FS::part_export;
178       use FS::part_export_option;
179       use FS::export_svc;
180       use FS::msgcat;
181       use FS::rate;
182       use FS::rate_region;
183       use FS::rate_prefix;
184       use FS::payment_gateway;
185       use FS::agent_payment_gateway;
186       use FS::XMLRPC;
187       use FS::payby;
188       use FS::cdr;
189       use FS::inventory_class;
190       use FS::inventory_item;
191       use FS::pkg_class;
192       use FS::access_user;
193       use FS::access_group;
194       use FS::access_usergroup;
195       use FS::access_groupagent;
196       use FS::access_right;
197       use FS::AccessRight;
198       use FS::svc_phone;
199       use FS::reason_type;
200       use FS::reason;
201       use FS::cust_main_note;
202
203       if ( %%%RT_ENABLED%%% ) {
204         eval '
205           use RT::Tickets;
206           use RT::Transactions;
207           use RT::Users;
208           use RT::CurrentUser;
209           use RT::Templates;
210           use RT::Queues;
211           use RT::ScripActions;
212           use RT::ScripConditions;
213           use RT::Scrips;
214           use RT::Groups;
215           use RT::GroupMembers;
216           use RT::CustomFields;
217           use RT::CustomFieldValues;
218           use RT::ObjectCustomFieldValues;
219
220           use RT::Interface::Web;
221           use MIME::Entity;
222           use Text::Wrapper;
223           use CGI::Cookie;
224           use Time::ParseDate;
225           use HTML::Scrubber;
226           #use Text::Quoted; #slow, unreliable, segfaults and is optional
227           use Time::HiRes;
228         ';
229         die $@ if $@;
230       }
231
232       *CGI::redirect = sub {
233         my( $self, $location ) = @_;
234         use vars qw($m);
235
236         # false laziness w/below
237         if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
238
239           my $page =
240             qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
241             '<BR><BR><PRE>'.
242               ( UNIVERSAL::can(dbh, 'sprintProfile')
243                   ? encode_entities(dbh->sprintProfile())
244                   : 'DBIx::Profile missing sprintProfile method;'.
245                     'unpatched or too old?'                        ).
246             #"\n\n". &sprintAutoProfile().  '</PRE>'.
247             "\n\n".                         '</PRE>'.
248             '</BODY></HTML>';
249           dbh->{'private_profile'} = {};
250           return $page;
251
252         } else { #normal redirect
253
254           $m->redirect($location);
255           '';
256
257         }
258
259       };
260       
261       unless ( $HTML::Mason::r->filename =~ /\/rt\/.*NoAuth/ ) { #RT
262         $cgi = new CGI;
263         &cgisuidsetup($cgi);
264         #&cgisuidsetup($r);
265         $p = popurl(2);
266         $fsurl = rooturl();
267       }
268
269       sub include {
270         use vars qw($m);
271         $m->scomp(@_);
272       }
273
274       sub errorpage {
275         use vars qw($m);
276         $m->comp('/elements/errorpage.html', @_);
277       }
278
279       sub redirect {
280         my( $location ) = @_;
281         use vars qw($m);
282         $m->clear_buffer;
283         #false laziness w/above
284         if ( defined(@DBIx::Profile::ISA) ) { #profiling redirect
285
286           $m->print(
287             qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
288             '<BR><BR><PRE>'.
289               ( UNIVERSAL::can(dbh, 'sprintProfile')
290                   ? encode_entities(dbh->sprintProfile())
291                   : 'DBIx::Profile missing sprintProfile method;'.
292                     'unpatched or too old?'                        ).
293             #"\n\n". &sprintAutoProfile().  '</PRE>'.
294             "\n\n".                         '</PRE>'.
295             '</BODY></HTML>'
296           );
297           dbh->{'private_profile'} = {};
298
299           #whew.  removing this is all that's needed to fix the annoying
300           #blank-page-instead-of-profiling-redirect-when-called-from-an-include
301           #bug triggered by mason 1.32
302           #my $rv = $m->abort(200);
303
304         } else { #normal redirect
305
306           $m->redirect($location);
307
308         }
309
310       }
311
312     } # end package HTML::Mason::Commands;
313
314     ###Module::Refresh->refresh;###
315
316     $r->content_type('text/html');
317     #eorar
318
319     my $headers = $r->headers_out;
320     $headers->{'Cache-control'} = 'no-cache';
321     #$r->no_cache(1);
322     $headers->{'Expires'} = '0';
323
324 #    $r->send_http_header;
325
326     if ( $r->filename =~ /\/rt\// ) { #RT
327
328       $ah->interp($rt_interp);
329
330       local $SIG{__WARN__};
331       local $SIG{__DIE__};
332
333       RT::Init();
334
335       # We don't need to handle non-text, non-xml items
336       return -1 if defined( $r->content_type )
337                 && $r->content_type !~ m!(^text/|\bxml\b)!io;
338
339     } else {
340
341       $ah->interp($fs_interp);
342
343     }
344
345     my %session;
346     my $status;
347     eval { $status = $ah->handle_request($r); };
348 #!!
349 #    if ( $@ ) {
350 #       $RT::Logger->crit($@);
351 #    }
352     warn $@ if $@;
353
354     undef %session;
355
356 #!!
357 #    if ($RT::Handle->TransactionDepth) {
358 #       $RT::Handle->ForceRollback;
359 #       $RT::Logger->crit(
360 #"Transaction not committed. Usually indicates a software fault. Data loss may have occurred"
361 #       );
362 #    }
363
364     $status;
365 }
366
367 1;