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