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