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