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