fix bank name showing up on invoices
[freeside.git] / htetc / global.asa
1 BEGIN { eval "use Devel::AutoProfiler;"; } #only if installed...
2 #BEGIN { package Devel::AutoProfiler; use vars qw(%caller_info); }
3 #use Devel::AutoProfiler;
4
5 use strict;
6 use vars qw( $cgi $p );
7 use Apache::ASP 2.55;
8 use CGI 2.47;
9 #use CGI::Carp qw(fatalsToBrowser);
10 use Date::Format;
11 use Date::Parse;
12 use Time::Local;
13 use Tie::IxHash;
14 use HTML::Entities;
15 use IO::Handle;
16 use IO::File;
17 use Net::Whois::Raw qw(whois);
18 if ( $] < 5.006 ) {
19   eval "use Net::Whois::Raw 0.32 qw(whois)";
20   die $@ if $@;
21 }
22 use Business::CreditCard;
23 use String::Approx qw(amatch);
24 use Chart::LinesPoints;
25 use HTML::Widgets::SelectLayers 0.03;
26 use FS::UID qw(cgisuidsetup dbh getotaker datasrc driver_name);
27 use FS::Record qw(qsearch qsearchs fields dbdef);
28 use FS::Conf;
29 use FS::CGI qw(header menubar popurl table itable ntable idiot eidiot
30                small_custview myexit http_header);
31 use FS::Msgcat qw(gettext geterror);
32 use FS::Misc qw( send_email );
33 use FS::Report::Table::Monthly;
34
35 use FS::agent;
36 use FS::agent_type;
37 use FS::domain_record;
38 use FS::cust_bill;
39 use FS::cust_bill_pay;
40 use FS::cust_credit;
41 use FS::cust_credit_bill;
42 use FS::cust_main;
43 use FS::cust_main_county;
44 use FS::cust_pay;
45 use FS::cust_pkg;
46 use FS::cust_refund;
47 use FS::cust_svc;
48 use FS::nas;
49 use FS::part_bill_event;
50 use FS::part_pkg;
51 use FS::part_referral;
52 use FS::part_svc;
53 use FS::part_svc_router;
54 use FS::part_virtual_field;
55 use FS::pkg_svc;
56 use FS::port;
57 use FS::queue qw(joblisting);
58 use FS::raddb;
59 use FS::session;
60 use FS::svc_acct;
61 use FS::svc_acct_pop qw(popselector);
62 use FS::svc_domain;
63 use FS::svc_forward;
64 use FS::svc_www;
65 use FS::router;
66 use FS::addr_block;
67 use FS::svc_broadband;
68 use FS::svc_external;
69 use FS::type_pkgs;
70 use FS::part_export;
71 use FS::part_export_option;
72 use FS::export_svc;
73 use FS::msgcat;
74
75 sub Script_OnStart {
76   $Response->AddHeader('Cache-control' => 'no-cache');
77 #  $Response->AddHeader('Expires' => 0);
78   $Response->{Expires} = -36288000;
79
80   $cgi = new CGI;
81   &cgisuidsetup($cgi);
82   $p = popurl(2);
83   #print $cgi->header( '-expires' => 'now' );
84   #dbh->{'private_profile'} = {} if dbh->can('sprintProfile');
85   dbh->{'private_profile'} = {} if UNIVERSAL::can(dbh, 'sprintProfile');
86
87   #really should check for FS::Profiler or something
88     # Devel::AutoProfiler _our_ VERSION?  thanks a fucking lot
89   if ( Devel::AutoProfiler->can('__recursively_fetch_subs_in_package') ) {
90     #should check to see it's my special version.  well, switch to FS::Profiler
91
92     #nicked from Devel::AutoProfiler::INIT
93     my %subs = Devel::AutoProfiler::__recursively_fetch_subs_in_package('main');
94
95
96     SUB : while( my ($name, $ref) = each(%subs) )
97       {
98         #next if $name =~ /^(main::)?Apache::/;
99         next unless $name =~ /FS/;
100         foreach my $sub (@Devel::AutoProfiler::do_not_instrument_this_sub)
101           {
102             if ($name =~ /$sub/)
103               {
104                 next SUB;
105               }
106           }
107         next if ($Devel::AutoProfiler::do_not_instrument_this_sub{$name});
108         #warn "INIT name is $name \n";
109         Devel::AutoProfiler::__instrument_sub($name, $ref);
110       }
111
112   }
113
114 }
115
116 sub Script_OnFlush {
117   my $ref = $Response->{BinaryRef};
118   #$$ref = $cgi->header( @FS::CGI::header ) . $$ref;
119   #$$ref = $cgi->header() . $$ref;
120   #warn "Script_OnFlush called with dbh ". dbh. "\n";
121   #if ( dbh->can('sprintProfile') ) {
122   if ( UNIVERSAL::can(dbh, 'sprintProfile') ) {
123     #warn "dbh can sprintProfile\n";
124     if ( lc($Response->{ContentType}) eq 'text/html' ) { #con
125       #warn "contenttype is sprintProfile\n";
126       $$ref =~ s/<\/BODY>[\s\n]*<\/HTML>[\s\n]*$//i
127         or warn "can't remove";
128   
129       #$$ref .= '<PRE>'. ("\n"x96). encode_entities(dbh->sprintProfile()). '</PRE>';
130       #  wtf?  konqueror...
131       $$ref .= '<PRE>'. ("\n"x4096). encode_entities(dbh->sprintProfile()).
132                "\n\n". &sprintAutoProfile(). '</PRE>';
133
134       $$ref .= '</BODY></HTML>';
135     }
136     dbh->{'private_profile'} = {};
137   }
138 }
139
140 #if ( defined(@DBIx::Profile::ISA) && DBIx::Profile::db->can('sprintProfile') ) {
141 #if ( defined(@DBIx::Profile::ISA) && UNIVERSAL::can('DBIx::Profile::db', 'sprintProfile') ) {
142 if ( defined(@DBIx::Profile::ISA) ) {
143
144   #warn "enabling profiling redirects";
145   *CGI::redirect = sub {
146     my( $self, $location) = @_;
147     my $page =
148       $cgi->header.
149       qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
150       '<BR><BR><PRE>'.
151         ( UNIVERSAL::can(dbh, 'sprintProfile')
152             ? encode_entities(dbh->sprintProfile())
153             : 'DBIx::Profile missing sprintProfile method;'.
154               'unpatched or too old?'                        ).
155       "\n\n". &sprintAutoProfile().  '</PRE>'.
156       '</BODY></HTML>';
157     dbh->{'private_profile'} = {};
158     return $page;
159   };
160
161 }
162
163 sub by_total_time 
164
165   return $a->{total_time_in_sub} <=> $b->{total_time_in_sub}; 
166 }
167
168 sub sprintAutoProfile {
169   my %caller_info = %Devel::AutoProfiler::caller_info;
170   return unless keys %caller_info;
171
172   %Devel::AutoProfiler::caller_info = ();
173
174   my @keys = keys(%caller_info);
175
176   foreach my $key (@keys)
177     {
178       my $href = $caller_info{$key};
179
180       $href->{who_am_i} = $key;
181     }
182
183   my @subs = values(%caller_info);
184
185   #my @sorted = sort by_total_time ( @subs );
186   my @sorted = reverse sort by_total_time ( @subs );
187
188   # print Dumper \@sorted;
189
190   my @readable_info;
191
192   foreach my $sort (@sorted)
193     {
194       push(@readable_info, delete($sort->{who_am_i}));
195       push(@readable_info, $sort);
196     }
197
198   use Data::Dumper;
199   return encode_entities(Dumper(\@readable_info));
200
201 }
202
203 sub include {
204   ( my $file = shift ) =~ s(^/)(%%%FREESIDE_DOCUMENT_ROOT%%%/);
205   #broken in 5.005# ${$Response->TrapInclude($file, @_)};
206   my $ref = $Response->TrapInclude($file, @_);
207   $$ref;
208 }
209
210 if ( defined(@DBIx::Profile::ISA) ) {
211
212   #false laziness w/above
213   *redirect = sub {
214     my($location) = @_;
215
216     ${$Response->{BinaryRef}} = 
217       $cgi->header.
218       qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
219       '<BR><BR><PRE>'.
220         ( UNIVERSAL::can(dbh, 'sprintProfile')
221             ? encode_entities(dbh->sprintProfile())
222             : 'DBIx::Profile missing sprintProfile method;'.
223               'unpatched or too old?'                        ).
224       "\n\n". &sprintAutoProfile().  '</PRE>'.
225       '</BODY></HTML>';
226
227     dbh->{'private_profile'} = {};
228
229     $Response->End;
230
231   };
232
233 } else {
234
235   *redirect = sub {
236     $Response->Redirect(@_);
237   }
238
239 }
240
241 1;
242