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