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