1a81c4b1cebedd37fdf1c10d74dcd47d158f3c11
[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.05;
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 use FS::payment_gateway;
86 use FS::agent_payment_gateway;
87
88 sub Script_OnStart {
89   $Response->AddHeader('Cache-control' => 'no-cache');
90 #  $Response->AddHeader('Expires' => 0);
91   $Response->{Expires} = -36288000;
92
93   $cgi = new CGI;
94   &cgisuidsetup($cgi);
95   $p = popurl(2);
96   #print $cgi->header( '-expires' => 'now' );
97   #dbh->{'private_profile'} = {} if dbh->can('sprintProfile');
98   dbh->{'private_profile'} = {} if UNIVERSAL::can(dbh, 'sprintProfile');
99
100   #really should check for FS::Profiler or something
101     # Devel::AutoProfiler _our_ VERSION?  thanks a fucking lot
102   if ( Devel::AutoProfiler->can('__recursively_fetch_subs_in_package') ) {
103     #should check to see it's my special version.  well, switch to FS::Profiler
104
105     #nicked from Devel::AutoProfiler::INIT
106     my %subs = Devel::AutoProfiler::__recursively_fetch_subs_in_package('main');
107
108
109     SUB : while( my ($name, $ref) = each(%subs) )
110       {
111         #next if $name =~ /^(main::)?Apache::/;
112         next unless $name =~ /FS/;
113         foreach my $sub (@Devel::AutoProfiler::do_not_instrument_this_sub)
114           {
115             if ($name =~ /$sub/)
116               {
117                 next SUB;
118               }
119           }
120         next if ($Devel::AutoProfiler::do_not_instrument_this_sub{$name});
121         #warn "INIT name is $name \n";
122         Devel::AutoProfiler::__instrument_sub($name, $ref);
123       }
124
125   }
126
127 }
128
129 sub Script_OnFlush {
130   my $ref = $Response->{BinaryRef};
131   #$$ref = $cgi->header( @FS::CGI::header ) . $$ref;
132   #$$ref = $cgi->header() . $$ref;
133   #warn "Script_OnFlush called with dbh ". dbh. "\n";
134   #if ( dbh->can('sprintProfile') ) {
135   if ( UNIVERSAL::can(dbh, 'sprintProfile') ) {
136     #warn "dbh can sprintProfile\n";
137     if ( lc($Response->{ContentType}) eq 'text/html' ) { #con
138       #warn "contenttype is sprintProfile\n";
139       $$ref =~ s/<\/BODY>[\s\n]*<\/HTML>[\s\n]*$//i
140         or warn "can't remove";
141   
142       #$$ref .= '<PRE>'. ("\n"x96). encode_entities(dbh->sprintProfile()). '</PRE>';
143       #  wtf?  konqueror...
144       $$ref .= '<PRE>'. ("\n"x4096). encode_entities(dbh->sprintProfile()).
145                "\n\n". &sprintAutoProfile(). '</PRE>';
146
147       $$ref .= '</BODY></HTML>';
148     }
149     dbh->{'private_profile'} = {};
150   }
151 }
152
153 #if ( defined(@DBIx::Profile::ISA) && DBIx::Profile::db->can('sprintProfile') ) {
154 #if ( defined(@DBIx::Profile::ISA) && UNIVERSAL::can('DBIx::Profile::db', 'sprintProfile') ) {
155 if ( defined(@DBIx::Profile::ISA) ) {
156
157   #warn "enabling profiling redirects";
158   *CGI::redirect = sub {
159     my( $self, $location) = @_;
160     my $page =
161       $cgi->header.
162       qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
163       '<BR><BR><PRE>'.
164         ( UNIVERSAL::can(dbh, 'sprintProfile')
165             ? encode_entities(dbh->sprintProfile())
166             : 'DBIx::Profile missing sprintProfile method;'.
167               'unpatched or too old?'                        ).
168       "\n\n". &sprintAutoProfile().  '</PRE>'.
169       '</BODY></HTML>';
170     dbh->{'private_profile'} = {};
171     return $page;
172   };
173
174 }
175
176 sub by_total_time 
177
178   return $a->{total_time_in_sub} <=> $b->{total_time_in_sub}; 
179 }
180
181 sub sprintAutoProfile {
182   my %caller_info = %Devel::AutoProfiler::caller_info;
183   return unless keys %caller_info;
184
185   %Devel::AutoProfiler::caller_info = ();
186
187   my @keys = keys(%caller_info);
188
189   foreach my $key (@keys)
190     {
191       my $href = $caller_info{$key};
192
193       $href->{who_am_i} = $key;
194     }
195
196   my @subs = values(%caller_info);
197
198   #my @sorted = sort by_total_time ( @subs );
199   my @sorted = reverse sort by_total_time ( @subs );
200
201   # print Dumper \@sorted;
202
203   my @readable_info;
204
205   foreach my $sort (@sorted)
206     {
207       push(@readable_info, delete($sort->{who_am_i}));
208       push(@readable_info, $sort);
209     }
210
211   use Data::Dumper;
212   return encode_entities(Dumper(\@readable_info));
213
214 }
215
216 sub include {
217   my $file = shift;
218   my $shift = 0;
219   if ( $file =~ m(^([^/].*)/[^/]+) ) {
220     unshift @{$Response->{asp}{includes_dir}}, "./$1";
221     $shift = 1;
222   }
223   $file =~ s(^/)(%%%FREESIDE_DOCUMENT_ROOT%%%/);
224   #broken in 5.005# ${$Response->TrapInclude($file, @_)};
225   my $ref = $Response->TrapInclude($file, @_);
226   shift @{$Response->{asp}{includes_dir}} if $shift;
227   $$ref;
228 }
229
230 if ( defined(@DBIx::Profile::ISA) ) {
231
232   #false laziness w/above
233   *redirect = sub {
234     my($location) = @_;
235
236     ${$Response->{BinaryRef}} = 
237       $cgi->header.
238       qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
239       '<BR><BR><PRE>'.
240         ( UNIVERSAL::can(dbh, 'sprintProfile')
241             ? encode_entities(dbh->sprintProfile())
242             : 'DBIx::Profile missing sprintProfile method;'.
243               'unpatched or too old?'                        ).
244       "\n\n". &sprintAutoProfile().  '</PRE>'.
245       '</BODY></HTML>';
246
247     dbh->{'private_profile'} = {};
248
249     $Response->End;
250
251   };
252
253 } else {
254
255   *redirect = sub {
256     $Response->Redirect(@_);
257   }
258
259 }
260
261 1;
262