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