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