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