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