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