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