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