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