-w flag spewing too much to STDOUT
[freeside.git] / fs_signup / FS-SignupClient / cgi / signup.cgi
1 #!/usr/bin/perl -T
2 #!/usr/bin/perl -Tw
3 #
4 # $Id: signup.cgi,v 1.29.2.22 2004-10-01 01:38:03 ivan Exp $
5
6 use strict;
7 use vars qw( @payby $cgi $locales $packages
8              $pops %pop %popnum2pop
9              $init_data $error
10              $last $first $ss $company $address1 $address2 $city $state $county
11              $country $zip $daytime $night $fax $invoicing_list $payby $payinfo
12              $paycvv $paydate $payname $referral_custnum $init_popstate
13              $pkgpart $username $password $password2 $sec_phrase $popnum
14              $agentnum $refnum
15              $ieak_file $ieak_template
16              $signup_html $signup_template
17              $success_html $success_template
18              $decline_html $decline_template
19              $ac $exch $loc
20              $email_name $pkg
21              $self_url
22            );
23 use subs qw( print_form print_okay print_decline
24              success_default decline_default
25              expselect );
26 use CGI;
27 #use CGI::Carp qw(fatalsToBrowser);
28 use Text::Template;
29 use Business::CreditCard;
30 use HTTP::BrowserDetect;
31 use FS::SignupClient 0.03 qw( signup_info new_customer );
32
33 #acceptable payment methods
34 #
35 #@payby = qw( CARD BILL COMP );
36 #@payby = qw( CARD BILL );
37 #@payby = qw( CARD );
38 @payby = qw( CARD PREPAY );
39
40 $ieak_file = '/usr/local/freeside/ieak.template';
41 $signup_html = -e 'signup.html'
42                  ? 'signup.html'
43                  : '/usr/local/freeside/signup.html';
44 $success_html = -e 'success.html'
45                   ? 'success.html'
46                   : '/usr/local/freeside/success.html';
47 $decline_html = -e 'decline.html'
48                   ? 'decline.html'
49                   : '/usr/local/freeside/decline.html';
50
51
52 if ( -e $ieak_file ) {
53   my $ieak_txt = Text::Template::_load_text($ieak_file)
54     or die $Text::Template::ERROR;
55   $ieak_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
56   $ieak_txt = $1;
57   $ieak_txt =~ s/\r//g; # don't double \r on old templates
58   $ieak_txt =~ s/\n/\r\n/g;
59   $ieak_template = new Text::Template ( TYPE => 'STRING', SOURCE => $ieak_txt )
60     or die $Text::Template::ERROR;
61 } else {
62   $ieak_template = '';
63 }
64
65 $agentnum = '';
66 if ( -e $signup_html ) {
67   my $signup_txt = Text::Template::_load_text($signup_html)
68     or die $Text::Template::ERROR;
69   $signup_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
70   $signup_txt = $1;
71   $signup_template = new Text::Template ( TYPE => 'STRING',
72                                           SOURCE => $signup_txt,
73                                           DELIMITERS => [ '<%=', '%>' ]
74                                         )
75     or die $Text::Template::ERROR;
76   if ( $signup_txt =~
77          /<\s*INPUT TYPE="?hidden"?\s+NAME="?agentnum"?\s+VALUE="?(\d+)"?\s*>/si
78   ) {
79     $agentnum = $1;
80   }
81 } else {
82   #too much maintenance hassle to keep in this file
83   die "can't find ./signup.html or /usr/local/freeside/signup.html";
84   #$signup_template = new Text::Template ( TYPE => 'STRING',
85   #                                        SOURCE => &signup_default,
86   #                                        DELIMITERS => [ '<%=', '%>' ]
87   #                                      )
88   #  or die $Text::Template::ERROR;
89 }
90
91 if ( -e $success_html ) {
92   my $success_txt = Text::Template::_load_text($success_html)
93     or die $Text::Template::ERROR;
94   $success_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
95   $success_txt = $1;
96   $success_template = new Text::Template ( TYPE => 'STRING',
97                                            SOURCE => $success_txt,
98                                            DELIMITERS => [ '<%=', '%>' ],
99                                          )
100     or die $Text::Template::ERROR;
101 } else {
102   $success_template = new Text::Template ( TYPE => 'STRING',
103                                            SOURCE => &success_default,
104                                            DELIMITERS => [ '<%=', '%>' ],
105                                          )
106     or die $Text::Template::ERROR;
107 }
108
109 if ( -e $decline_html ) {
110   my $decline_txt = Text::Template::_load_text($decline_html)
111     or die $Text::Template::ERROR;
112   $decline_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
113   $decline_txt = $1;
114   $decline_template = new Text::Template ( TYPE => 'STRING',
115                                            SOURCE => $decline_txt,
116                                            DELIMITERS => [ '<%=', '%>' ],
117                                          )
118     or die $Text::Template::ERROR;
119 } else {
120   $decline_template = new Text::Template ( TYPE => 'STRING',
121                                            SOURCE => &decline_default,
122                                            DELIMITERS => [ '<%=', '%>' ],
123                                          )
124     or die $Text::Template::ERROR;
125 }
126
127
128 ( $locales, $packages, $pops, $init_data ) = signup_info();
129 @payby = @{$init_data->{'payby'}} if @{$init_data->{'payby'}};
130 $packages = $init_data->{agentnum2part_pkg}{$agentnum} if $agentnum;
131 %pop = ();
132 %popnum2pop = ();
133 foreach (@$pops) {
134   #push @{ $pop{ $_->{state} }->{ $_->{ac} } }, $_;
135   push @{ $pop{ $_->{state} } }, $_;
136   $popnum2pop{$_->{popnum}} = $_;
137 }
138
139 $cgi = new CGI;
140
141 if ( defined $cgi->param('magic') ) {
142   if ( $cgi->param('magic') eq 'process' ) {
143
144     if ( $cgi->param('state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/ ) {
145       $state = $1;
146       $county = $3 || '';
147       $country = $4;
148     } elsif ( $cgi->param('state') =~ /^(\w*)$/ ) {
149       $state = $1;
150       $cgi->param('county') =~ /^([\w ]*)$/
151         or die "illegal county: ". $cgi->param('county');
152       $county = $1;
153       $cgi->param('country') =~ /^(\w+)$/
154         or die "illegal country: ". $cgi->param('country');
155       $country = $1;
156     } else {
157       die "illegal state: ". $cgi->param('state');
158     }
159
160     $payby = $cgi->param('payby');
161     if ( $payby eq 'CHEK' ) {
162       #$payinfo = join('@', map { $cgi->param( $payby. "_payinfo$_" ) } (1,2) );
163       $payinfo = $cgi->param('CHEK_payinfo1').'@'.$cgi->param('CHEK_payinfo2');
164     } else {
165       $payinfo = $cgi->param( $payby. '_payinfo' );
166     }
167     $paydate =
168       $cgi->param( $payby. '_month' ). '-'. $cgi->param( $payby. '_year' );
169     $payname = $cgi->param( $payby. '_payname' );
170     $paycvv = defined $cgi->param( $payby. '_paycvv' )
171                 ? $cgi->param( $payby. '_paycvv' )
172                 : '';
173
174     if ( $invoicing_list = $cgi->param('invoicing_list') ) {
175       $invoicing_list .= ', POST' if $cgi->param('invoicing_list_POST');
176     } else {
177       $invoicing_list = 'POST';
178     }
179
180     $error = '';
181
182     $last             = $cgi->param('last');
183     $first            = $cgi->param('first');
184     $ss               = $cgi->param('ss');
185     $company          = $cgi->param('company');
186     $address1         = $cgi->param('address1');
187     $address2         = $cgi->param('address2');
188     $city             = $cgi->param('city');
189     #$county,
190     #$state,
191     $zip              = $cgi->param('zip');
192     #$country,
193     $daytime          = $cgi->param('daytime');
194     $night            = $cgi->param('night');
195     $fax              = $cgi->param('fax');
196     #$payby,
197     #$payinfo,
198     #$paydate,
199     #$payname,
200     #$invoicing_list,
201     $referral_custnum = $cgi->param('ref');
202     $pkgpart          = $cgi->param('pkgpart');
203     $username         = $cgi->param('username');
204     $sec_phrase       = $cgi->param('sec_phrase');
205     $password         = $cgi->param('_password');
206     $popnum           = $cgi->param('popnum');
207     #$agentnum, #         = $cgi->param('agentnum'),
208     $agentnum         ||= $cgi->param('agentnum');
209     $init_popstate    = $cgi->param('init_popstate');
210     $refnum           = $cgi->param('refnum');
211
212     if ( $cgi->param('_password') ne $cgi->param('_password2') ) {
213       $error = $init_data->{msgcat}{passwords_dont_match}; #msgcat
214       $password  = '';
215       $password2 = '';
216     } else {
217       $password2 = $cgi->param('_password2');
218
219       if ( $payby eq 'CARD' && $cgi->param('CARD_type') ) {
220         $payinfo =~ s/\D//g;
221
222         $payinfo =~ /^(\d{13,16})$/
223           or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
224         $payinfo = $1;
225         validate($payinfo)
226           or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
227         cardtype($payinfo) eq $cgi->param('CARD_type')
228           or $error ||= $init_data->{msgcat}{not_a}. $cgi->param('CARD_type');
229       }
230
231       $error ||= new_customer ( {
232         'last'             => $last,
233         'first'            => $first,
234         'ss'               => $ss,
235         'company'          => $company,
236         'address1'         => $address1,
237         'address2'         => $address2,
238         'city'             => $city,
239         'county'           => $county,
240         'state'            => $state,
241         'zip'              => $zip,
242         'country'          => $country,
243         'daytime'          => $daytime,
244         'night'            => $night,
245         'fax'              => $fax,
246         'payby'            => $payby,
247         'payinfo'          => $payinfo,
248         'paycvv'           => $paycvv,
249         'paydate'          => $paydate,
250         'payname'          => $payname,
251         'invoicing_list'   => $invoicing_list,
252         'referral_custnum' => $referral_custnum,
253         'pkgpart'          => $pkgpart,
254         'username'         => $username,
255         'sec_phrase'       => $sec_phrase,
256         '_password'        => $password,
257         'popnum'           => $popnum,
258         'agentnum'         => $agentnum,
259         'refnum'           => $refnum,
260         map { $_ => $cgi->param($_) } grep { /^snarf_/ } $cgi->param
261       } );
262
263     }
264     
265     if ( $error eq '_decline' ) {
266       print_decline();
267     } elsif ( $error ) {
268       #fudge the snarf info
269       no strict 'refs';
270       ${$_} = $cgi->param($_) foreach grep { /^snarf_/ } $cgi->param;
271       print_form();
272     } else {
273       print_okay();
274     }
275
276   } else {
277     die "unrecognized magic: ". $cgi->param('magic');
278   }
279 } else {
280   $error = '';
281   $last = '';
282   $first = '';
283   $ss = '';
284   $company = '';
285   $address1 = '';
286   $address2 = '';
287   $city = '';
288   $state = $cgi->param('init_popstate') || $init_data->{statedefault};
289   $county = '';
290   $country = $init_data->{countrydefault};
291   $zip = '';
292   $daytime = '';
293   $night = '';
294   $fax = '';
295   $invoicing_list = '';
296   $payby = '';
297   $payinfo = '';
298   $paydate = '';
299   $payname = '';
300   $pkgpart = '';
301   $username = '';
302   $password = '';
303   $password2 = '';
304   $sec_phrase = '';
305   $popnum = '';
306   $referral_custnum = $cgi->param('ref') || '';
307   $init_popstate = $cgi->param('init_popstate') || '';
308   $refnum = $init_data->{'refnum'};
309   print_form;
310 }
311
312 sub print_form {
313
314   $cgi->delete('ref');
315   $cgi->delete('init_popstate');
316   $self_url = $cgi->self_url;
317
318   $error = "Error: $error" if $error;
319
320   print $cgi->header( '-expires' => 'now' ),
321         $signup_template->fill_in();
322
323 }
324
325 sub print_decline {
326   print $cgi->header( '-expires' => 'now' ),
327         $decline_template->fill_in();
328 }
329
330 sub print_okay {
331   my $user_agent = new HTTP::BrowserDetect $ENV{HTTP_USER_AGENT};
332
333   $cgi->param('username') =~ /^(.+)$/
334     or die "fatal: invalid username got past FS::SignupClient::new_customer";
335   my $username = $1;
336   $cgi->param('_password') =~ /^(.+)$/
337     or die "fatal: invalid password got past FS::SignupClient::new_customer";
338   my $password = $1;
339   ( $cgi->param('first'). ' '. $cgi->param('last') ) =~ /^(.*)$/
340     or die "fatal: invalid email_name got past FS::SignupClient::new_customer";
341   $email_name = $1; #global for template
342
343   my $pop = $popnum2pop{$cgi->param('popnum')};
344     #or die "fatal: invalid popnum got past FS::SignupClient::new_customer";
345   if ( $pop ) {
346     ( $ac, $exch, $loc ) = ( $pop->{'ac'}, $pop->{'exch'}, $pop->{'loc'} );
347   } else {
348     ( $ac, $exch, $loc ) = ( '', '', ''); #presumably you're not using them.
349   }
350
351   #global for template
352   $pkg = ( grep { $_->{'pkgpart'} eq $pkgpart } @$packages )[0]->{'pkg'};
353
354   if ( $ieak_template && $user_agent->windows && $user_agent->ie ) {
355     #send an IEAK config
356     print $cgi->header('application/x-Internet-signup'),
357           $ieak_template->fill_in();
358   } else { #send a simple confirmation
359     print $cgi->header( '-expires' => 'now' ),
360           $success_template->fill_in();
361   }
362 }
363
364 #horrible false laziness with FS/FS/svc_acct_pop.pm::popselector
365 sub popselector {
366   my( $popnum, $state ) = @_;
367
368   return '<INPUT TYPE="hidden" NAME="popnum" VALUE="">' unless @$pops;
369   return $pops->[0]{city}. ', '. $pops->[0]{state}.
370          ' ('. $pops->[0]{ac}. ')/'. $pops->[0]{exch}. '-'. $pops->[0]{loc}.
371          '<INPUT TYPE="hidden" NAME="popnum" VALUE="'. $pops->[0]{popnum}. '">'
372     if scalar(@$pops) == 1;
373
374   #my %pop = ();
375   #push @{ $pop{$_->{state}} }, $_ foreach @$pops;
376
377   my $text;
378
379   if ( $init_popstate ) {
380     $text .= '<INPUT TYPE="hidden" NAME="init_popstate" VALUE="'.
381              $init_popstate. '">';
382   } else {
383     $text .= <<END;
384       <SCRIPT>
385       function opt(what,href,text) {
386         var optionName = new Option(text, href, false, false)
387         var length = what.length;
388         what.options[length] = optionName;
389       }
390       function popstate_changed(what) {
391         state = what.options[what.selectedIndex].text;
392         what.form.popnum.options.length = 0;
393         what.form.popnum.options[0] = new Option("", "", false, true);
394 END
395
396     foreach my $popstate ( sort { $a cmp $b } keys %pop ) {
397       $text .= "\nif ( state == \"$popstate\" ) {\n";
398
399       foreach my $pop ( @{$pop{$popstate}}) {
400         my $o_popnum = $pop->{popnum};
401         my $poptext =  $pop->{city}. ', '. $pop->{state}.
402                        ' ('. $pop->{ac}. ')/'. $pop->{exch}. '-'. $pop->{loc};
403
404         $text .= "opt(what.form.popnum, \"$o_popnum\", \"$poptext\");\n"
405       }
406       $text .= "}\n";
407     }
408
409     $text .= "}\n</SCRIPT>\n";
410
411     $text .=
412       qq!<SELECT NAME="popstate" SIZE=1 onChange="popstate_changed(this)">!.
413       qq!<OPTION> !;
414     $text .= "<OPTION>$_" foreach sort { $a cmp $b } keys %pop;
415     $text .= '</SELECT>'; #callback? return 3 html pieces?  #'</TD><TD>';
416
417   }
418   $text .= qq!<SELECT NAME="popnum" SIZE=1><OPTION> !;
419
420   #comment this block to disable initial list population
421   my @initial_select = ();
422   if ( $init_popstate ) {
423     @initial_select = grep { $_->{state} eq $init_popstate } @$pops;
424   } else {
425     if ( scalar( @$pops ) > 100 ) {
426       push @initial_select, $popnum2pop{$popnum} if $popnum2pop{$popnum};
427     } else {
428       @initial_select = @$pops;
429     }
430   }
431   foreach my $pop ( sort { $a->{state} cmp $b->{state} } @initial_select ) {
432     $text .= qq!<OPTION VALUE="!. $pop->{popnum}. '"'.
433              ( ( $popnum && $pop->{popnum} == $popnum ) ? ' SELECTED' : '' ). ">".
434              $pop->{city}. ', '. $pop->{state}.
435                ' ('. $pop->{ac}. ')/'. $pop->{exch}. '-'. $pop->{loc};
436   }
437   $text .= '</SELECT>';
438
439   $text;
440 }
441
442 sub expselect {
443   my $prefix = shift;
444   my $date = shift || '';
445   my( $m, $y ) = ( 0, 0 );
446   if ( $date  =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #PostgreSQL date format
447     ( $m, $y ) = ( $2, $1 );
448   } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
449     ( $m, $y ) = ( $1, $3 );
450   }
451   my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
452   for ( 1 .. 12 ) {
453     $return .= "<OPTION";
454     $return .= " SELECTED" if $_ == $m;
455     $return .= ">$_";
456   }
457   $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
458   for ( 2001 .. 2037 ) {
459     $return .= "<OPTION";
460     $return .= " SELECTED" if $_ == $y;
461     $return .= ">$_";
462   }
463   $return .= "</SELECT>";
464
465   $return;
466 }
467
468 #false laziness w/FS::cust_main_county
469 sub regionselector {
470   my ( $selected_county, $selected_state, $selected_country,
471        $prefix, $onchange ) = @_;
472
473   $prefix = '' unless defined $prefix;
474
475   my $countyflag = 0;
476
477   my %cust_main_county;
478
479 #  unless ( @cust_main_county ) { #cache 
480     #@cust_main_county = qsearch('cust_main_county', {} );
481     #foreach my $c ( @cust_main_county ) {
482     foreach my $c ( @$locales ) {
483       #$countyflag=1 if $c->county;
484       $countyflag=1 if $c->{county};
485       #push @{$cust_main_county{$c->country}{$c->state}}, $c->county;
486       #$cust_main_county{$c->country}{$c->state}{$c->county} = 1;
487       $cust_main_county{$c->{country}}{$c->{state}}{$c->{county}} = 1;
488     }
489 #  }
490   $countyflag=1 if $selected_county;
491
492   my $script_html = <<END;
493     <SCRIPT>
494     function opt(what,value,text) {
495       var optionName = new Option(text, value, false, false);
496       var length = what.length;
497       what.options[length] = optionName;
498     }
499     function ${prefix}country_changed(what) {
500       country = what.options[what.selectedIndex].text;
501       for ( var i = what.form.${prefix}state.length; i >= 0; i-- )
502           what.form.${prefix}state.options[i] = null;
503 END
504       #what.form.${prefix}state.options[0] = new Option('', '', false, true);
505
506   foreach my $country ( sort keys %cust_main_county ) {
507     $script_html .= "\nif ( country == \"$country\" ) {\n";
508     foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
509       my $text = $state || '(n/a)';
510       $script_html .= qq!opt(what.form.${prefix}state, "$state", "$text");\n!;
511     }
512     $script_html .= "}\n";
513   }
514
515   $script_html .= <<END;
516     }
517     function ${prefix}state_changed(what) {
518 END
519
520   if ( $countyflag ) {
521     $script_html .= <<END;
522       state = what.options[what.selectedIndex].text;
523       country = what.form.${prefix}country.options[what.form.${prefix}country.selectedIndex].text;
524       for ( var i = what.form.${prefix}county.length; i >= 0; i-- )
525           what.form.${prefix}county.options[i] = null;
526 END
527
528     foreach my $country ( sort keys %cust_main_county ) {
529       $script_html .= "\nif ( country == \"$country\" ) {\n";
530       foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
531         $script_html .= "\nif ( state == \"$state\" ) {\n";
532           #foreach my $county ( sort @{$cust_main_county{$country}{$state}} ) {
533           foreach my $county ( sort keys %{$cust_main_county{$country}{$state}} ) {
534             my $text = $county || '(n/a)';
535             $script_html .=
536               qq!opt(what.form.${prefix}county, "$county", "$text");\n!;
537           }
538         $script_html .= "}\n";
539       }
540       $script_html .= "}\n";
541     }
542   }
543
544   $script_html .= <<END;
545     }
546     </SCRIPT>
547 END
548
549   my $county_html = $script_html;
550   if ( $countyflag ) {
551     $county_html .= qq!<SELECT NAME="${prefix}county" onChange="$onchange">!;
552     $county_html .= '</SELECT>';
553   } else {
554     $county_html .=
555       qq!<INPUT TYPE="hidden" NAME="${prefix}county" VALUE="$selected_county">!;
556   }
557
558   my $state_html = qq!<SELECT NAME="${prefix}state" !.
559                    qq!onChange="${prefix}state_changed(this); $onchange">!;
560   foreach my $state ( sort keys %{ $cust_main_county{$selected_country} } ) {
561     my $text = $state || '(n/a)';
562     my $selected = $state eq $selected_state ? 'SELECTED' : '';
563     $state_html .= "\n<OPTION $selected VALUE=$state>$text</OPTION>"
564   }
565   $state_html .= '</SELECT>';
566
567   $state_html .= '</SELECT>';
568
569   my $country_html = qq!<SELECT NAME="${prefix}country" !.
570                      qq!onChange="${prefix}country_changed(this); $onchange">!;
571   my $countrydefault = $init_data->{countrydefault} || 'US';
572   foreach my $country (
573     sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
574       keys %cust_main_county
575   ) {
576     my $selected = $country eq $selected_country ? ' SELECTED' : '';
577     $country_html .= "\n<OPTION$selected>$country</OPTION>"
578   }
579   $country_html .= '</SELECT>';
580
581   ($county_html, $state_html, $country_html);
582
583 }
584
585 sub success_default { #html to use if you don't specify a success file
586   <<'END';
587 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
588 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
589 Thanks for signing up!
590 <BR><BR>
591 Signup information for <%= $email_name %>:
592 <BR><BR>
593 Username: <%= $username %><BR>
594 Password: <%= $password %><BR>
595 Access number: (<%= $ac %>) / <%= $exch %> - <%= $local %><BR>
596 Package: <%= $pkg %><BR>
597 </BODY></HTML>
598 END
599 }
600
601 sub decline_default { #html to use if there is a decline
602   <<'END';
603 <HTML><HEAD><TITLE>Processing error</TITLE></HEAD>
604 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Processing error</FONT><BR><BR>
605 There has been an error processing your account.  Please contact customer
606 support.
607 </BODY></HTML>
608 END
609 }
610