closes: bug#384 - adds security phrase to signup server also
[freeside.git] / fs_signup / FS-SignupClient / cgi / signup.cgi
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: signup.cgi,v 1.17 2002-04-06 21:39:22 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              $ieak_file $ieak_template $cck_file $cck_template
12              $signup_html $signup_template $success_html $success_template
13              $ac $exch $loc
14              $self_url
15            );
16 use subs qw( print_form print_okay expselect signup_default success_default );
17 use CGI;
18 use CGI::Carp qw(fatalsToBrowser);
19 use HTTP::Headers::UserAgent 2.00;
20 use FS::SignupClient 0.03 qw( signup_info new_customer );
21 use Text::Template;
22
23 #acceptable payment methods
24 #
25 #@payby = qw( CARD BILL COMP );
26 #@payby = qw( CARD BILL );
27 #@payby = qw( CARD );
28 @payby = qw( CARD PREPAY );
29
30 $ieak_file = '/usr/local/freeside/ieak.template';
31 $cck_file = '/usr/local/freeside/cck.template';
32 $signup_html = '/usr/local/freeside/signup.html';
33 $success_html = '/usr/local/freeside/success.html';
34
35 if ( -e $ieak_file ) {
36   my $ieak_txt = Text::Template::_load_text($ieak_file)
37     or die $Text::Template::ERROR;
38   $ieak_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
39   $ieak_txt = $1;
40   $ieak_template = new Text::Template ( TYPE => 'STRING', SOURCE => $ieak_txt )
41     or die $Text::Template::ERROR;
42 } else {
43   $ieak_template = '';
44 }
45
46 if ( -e $cck_file ) {
47   my $cck_txt = Text::Template::_load_text($cck_file)
48     or die $Text::Template::ERROR;
49   $cck_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
50   $cck_txt = $1;
51   $cck_template = new Text::Template ( TYPE => 'STRING', SOURCE => $cck_txt )
52     or die $Text::Template::ERROR;
53 } else {
54   $cck_template = '';
55 }
56
57 if ( -e $signup_html ) {
58   my $signup_txt = Text::Template::_load_text($signup_html)
59     or die $Text::Template::ERROR;
60   $signup_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
61   $signup_txt = $1;
62   $signup_template = new Text::Template ( TYPE => 'STRING',
63                                           SOURCE => $signup_txt,
64                                           DELIMITERS => [ '<%=', '%>' ]
65                                         )
66     or die $Text::Template::ERROR;
67 } else {
68   $signup_template = new Text::Template ( TYPE => 'STRING',
69                                           SOURCE => &signup_default,
70                                           DELIMITERS => [ '<%=', '%>' ]
71                                         )
72     or die $Text::Template::ERROR;
73 }
74
75 if ( -e $success_html ) {
76   my $success_txt = Text::Template::_load_text($success_html)
77     or die $Text::Template::ERROR;
78   $success_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
79   $success_txt = $1;
80   $success_template = new Text::Template ( TYPE => 'STRING',
81                                            SOURCE => $success_txt,
82                                            DELIMITERS => [ '<%=', '%>' ],
83                                          )
84     or die $Text::Template::ERROR;
85 } else {
86   $success_template = new Text::Template ( TYPE => 'STRING',
87                                            SOURCE => &success_default,
88                                            DELIMITERS => [ '<%=', '%>' ],
89                                          )
90     or die $Text::Template::ERROR;
91 }
92
93 ( $locales, $packages, $pops, $init_data ) = signup_info();
94
95 $cgi = new CGI;
96
97 if ( defined $cgi->param('magic') ) {
98   if ( $cgi->param('magic') eq 'process' ) {
99
100     $cgi->param('state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/
101       or die "Oops, illegal \"state\" param: ". $cgi->param('state');
102     $state = $1;
103     $county = $3 || '';
104     $country = $4;
105
106     $payby = $cgi->param('payby');
107     $payinfo = $cgi->param( $payby. '_payinfo' );
108     $paydate =
109       $cgi->param( $payby. '_month' ). '-'. $cgi->param( $payby. '_year' );
110     $payname = $cgi->param( $payby. '_payname' );
111
112     if ( $invoicing_list = $cgi->param('invoicing_list') ) {
113       $invoicing_list .= ', POST' if $cgi->param('invoicing_list_POST');
114     } else {
115       $invoicing_list = 'POST';
116     }
117
118     $error = '';
119
120     if ( $cgi->param('_password') ne $cgi->param('_password2') ) {
121       $error = "Passwords don't match";
122       $password  = '';
123       $password2 = '';
124     } else {
125       $password2 = $cgi->param('_password2');
126
127       $error = new_customer ( {
128         'last'             => $last             = $cgi->param('last'),
129         'first'            => $first            = $cgi->param('first'),
130         'ss'               => $ss               = $cgi->param('ss'),
131         'company'          => $company          = $cgi->param('company'),
132         'address1'         => $address1         = $cgi->param('address1'),
133         'address2'         => $address2         = $cgi->param('address2'),
134         'city'             => $city             = $cgi->param('city'),
135         'county'           => $county,
136         'state'            => $state,
137         'zip'              => $zip              = $cgi->param('zip'),
138         'country'          => $country,
139         'daytime'          => $daytime          = $cgi->param('daytime'),
140         'night'            => $night            = $cgi->param('night'),
141         'fax'              => $fax              = $cgi->param('fax'),
142         'payby'            => $payby,
143         'payinfo'          => $payinfo,
144         'paydate'          => $paydate,
145         'payname'          => $payname,
146         'invoicing_list'   => $invoicing_list,
147         'referral_custnum' => $referral_custnum = $cgi->param('ref'),
148         'pkgpart'          => $pkgpart          = $cgi->param('pkgpart'),
149         'username'         => $username         = $cgi->param('username'),
150         'sec_phrase'       => $sec_phrase       = $cgi->param('sec_phrase'),
151         '_password'        => $password         = $cgi->param('_password'),
152         'popnum'           => $popnum           = $cgi->param('popnum'),
153       } );
154
155     }
156     
157     if ( $error ) {
158       print_form();
159     } else {
160       print_okay();
161     }
162
163   } else {
164     die "unrecognized magic: ". $cgi->param('magic');
165   }
166 } else {
167   $error = '';
168   $last = '';
169   $first = '';
170   $ss = '';
171   $company = '';
172   $address1 = '';
173   $address2 = '';
174   $city = '';
175   $state = '';
176   $county = '';
177   $country = '';
178   $zip = '';
179   $daytime = '';
180   $night = '';
181   $fax = '';
182   $invoicing_list = '';
183   $payby = '';
184   $payinfo = '';
185   $paydate = '';
186   $payname = '';
187   $pkgpart = '';
188   $username = '';
189   $password = '';
190   $password2 = '';
191   $sec_phrase = '';
192   $popnum = '';
193   $referral_custnum = $cgi->param('ref') || '';
194   print_form;
195 }
196
197 sub print_form {
198
199   $cgi->delete('ref');
200   $self_url = $cgi->self_url;
201
202   $error = "Error: $error" if $error;
203
204   print $cgi->header( '-expires' => 'now' ),
205         $signup_template->fill_in();
206
207 }
208
209 sub print_okay {
210   my $user_agent = new HTTP::Headers::UserAgent $ENV{HTTP_USER_AGENT};
211
212   $cgi->param('username') =~ /^(.+)$/
213     or die "fatal: invalid username got past FS::SignupClient::new_customer";
214   my $username = $1;
215   $cgi->param('_password') =~ /^(.+)$/
216     or die "fatal: invalid password got past FS::SignupClient::new_customer";
217   my $password = $1;
218   ( $cgi->param('first'). ' '. $cgi->param('last') ) =~ /^(.*)$/
219     or die "fatal: invalid email_name got past FS::SignupCLient::new_customer";
220   my $email_name = $1;
221
222   my $pop = pop_info($cgi->param('popnum'))
223     or die "fatal: invalid popnum got past FS::SignupClient::new_customer";
224   ( $ac, $exch, $loc ) = ( $pop->{'ac'}, $pop->{'exch'}, $pop->{'loc'} );
225
226   if ( $ieak_template
227        && $user_agent->platform eq 'ia32'
228        && $user_agent->os =~ /^win/
229        && ($user_agent->browser)[0] eq 'IE'
230      )
231   { #send an IEAK config
232     print $cgi->header('application/x-Internet-signup'),
233           $ieak_template->fill_in();
234   } elsif ( $cck_template
235             && $user_agent->platform eq 'ia32'
236             && $user_agent->os =~ /^win/
237             && ($user_agent->browser)[0] eq 'Netscape'
238           )
239   { #send a Netscape config
240     my $cck_data = $cck_template->fill_in();
241     print $cgi->header('application/x-netscape-autoconfigure-dialer-v2'),
242           map {
243             m/(.*)\s+(.*)$/;
244             pack("N", length($1)). $1. pack("N", length($2)). $2;
245           } split(/\n/, $cck_data);
246
247   } else { #send a simple confirmation
248     print $cgi->header( '-expires' => 'now' ),
249           $success_template->fill_in();
250   }
251 }
252
253 sub pop_info {
254   my $popnum = shift;
255   my $pop;
256   foreach $pop ( @{$pops} ) {
257     if ( $pop->{'popnum'} == $popnum ) { return $pop; }
258   }
259   '';
260 }
261
262 #horrible false laziness with FS/FS/svc_acct_pop.pm::popselector
263 sub popselector {
264   my( $popnum, $state ) = @_;
265
266   my %pop = ();
267   push @{ $pop{$_->{state}} }, $_ foreach @$pops;
268
269   my $text = <<END;
270     <SCRIPT>
271     function opt(what,href,text) {
272       var optionName = new Option(text, href, false, false)
273       var length = what.length;
274       what.options[length] = optionName;
275     }
276     
277     function popstate_changed(what) {
278       state = what.options[what.selectedIndex].text;
279       for (var i = what.form.popnum.length;i > 0;i--)
280                 what.form.popnum.options[i] = null;
281       what.form.popnum.options[0] = new Option("", "", false, true);
282 END
283
284   foreach my $popstate ( sort { $a cmp $b } keys %pop ) {
285     $text .= "\nif ( state == \"$popstate\" ) {\n";
286
287     foreach my $pop ( @{$pop{$popstate}}) {
288       my $o_popnum = $pop->{popnum};
289       my $poptext =  $pop->{city}. ', '. $pop->{state}.
290                      ' ('. $pop->{ac}. ')/'. $pop->{exch};
291
292       $text .= "opt(what.form.popnum, \"$o_popnum\", \"$poptext\");\n"
293     }
294     $text .= "}\n";
295   }
296
297   $text .= "}\n</SCRIPT>\n";
298
299   $text .=
300     qq!<SELECT NAME="popstate" SIZE=1 onChange="popstate_changed(this)">!.
301     qq!<OPTION> !;
302   $text .= "<OPTION>$_" foreach sort { $a cmp $b } keys %pop;
303   $text .= '</SELECT>'; #callback? return 3 html pieces?  #'</TD><TD>';
304
305   $text .= qq!<SELECT NAME="popnum" SIZE=1><OPTION> !;
306   foreach my $pop ( @$pops ) {
307     $text .= qq!<OPTION VALUE="!. $pop->{popnum}. '"'.
308              ( ( $popnum && $pop->{popnum} == $popnum ) ? ' SELECTED' : '' ). ">".
309              $pop->{city}. ', '. $pop->{state}.
310                ' ('. $pop->{ac}. ')/'. $pop->{exch};
311   }
312   $text .= '</SELECT>';
313
314   $text;
315 }
316
317 sub expselect {
318   my $prefix = shift;
319   my $date = shift || '';
320   my( $m, $y ) = ( 0, 0 );
321   if ( $date  =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #PostgreSQL date format
322     ( $m, $y ) = ( $2, $1 );
323   } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
324     ( $m, $y ) = ( $1, $3 );
325   }
326   my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
327   for ( 1 .. 12 ) {
328     $return .= "<OPTION";
329     $return .= " SELECTED" if $_ == $m;
330     $return .= ">$_";
331   }
332   $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
333   for ( 2001 .. 2037 ) {
334     $return .= "<OPTION";
335     $return .= " SELECTED" if $_ == $y;
336     $return .= ">$_";
337   }
338   $return .= "</SELECT>";
339
340   $return;
341 }
342
343 sub success_default { #html to use if you don't specify a success file
344   <<'END';
345 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
346 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
347 Thanks for signing up!
348 </BODY></HTML>
349 END
350 }
351
352 sub signup_default { #html to use if you don't specify a template file
353   <<'END';
354 <HTML><HEAD><TITLE>ISP Signup form</TITLE></HEAD>
355 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>ISP Signup form</FONT><BR><BR>
356 <FONT SIZE="+1" COLOR="#ff0000"><%= $error %></FONT>
357 <FORM ACTION="<%= $self_url %>" METHOD=POST>
358 <INPUT TYPE="hidden" NAME="magic" VALUE="process">
359 <INPUT TYPE="hidden" NAME="ref" VALUE="<%= $referral_custnum %>">
360 <INPUT TYPE="hidden" NAME="ss" VALUE="">
361 Contact Information
362 <TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
363 <TR>
364   <TH ALIGN="right"><font color="#ff0000">*</font>Contact name<BR>(last, first)</TH>
365   <TD COLSPAN=5><INPUT TYPE="text" NAME="last" VALUE="<%= $last %>">,
366                 <INPUT TYPE="text" NAME="first" VALUE="<%= $first %>"></TD>
367 </TR>
368 <TR>
369   <TD ALIGN="right">Company</TD>
370   <TD COLSPAN=5><INPUT TYPE="text" NAME="company" SIZE=70 VALUE="<%= $company %>"></TD>
371 </TR>
372 <TR>
373   <TH ALIGN="right"><font color="#ff0000">*</font>Address</TH>
374   <TD COLSPAN=5><INPUT TYPE="text" NAME="address1" SIZE=70 VALUE="<%= $address1 %>"></TD>
375 </TR>
376 <TR>
377   <TD ALIGN="right">&nbsp;</TD>
378   <TD COLSPAN=5><INPUT TYPE="text" NAME="address2" SIZE=70 VALUE="<%= $address2 %>"></TD>
379 </TR>
380 <TR>
381   <TH ALIGN="right"><font color="#ff0000">*</font>City</TH>
382   <TD><INPUT TYPE="text" NAME="city" VALUE="<%= $city %>"></TD>
383   <TH ALIGN="right"><font color="#ff0000">*</font>State/Country</TH>
384   <TD><SELECT NAME="state" SIZE="1">
385
386   <%=
387     foreach ( @{$locales} ) {
388       $OUT .= '<OPTION';
389       $OUT .= ' SELECTED' if ( $state eq $_->{'state'}
390                                && $county eq $_->{'county'}
391                                && $country eq $_->{'country'}
392                              );
393       $OUT .= '>'. $_->{'state'};
394       $OUT .= ' ('. $_->{'county'}. ')' if $_->{'county'};
395       $OUT .= ' / '. $_->{'country'};
396     }
397   %>
398
399   </SELECT></TD>
400   <TH><font color="#ff0000">*</font>Zip</TH>
401   <TD><INPUT TYPE="text" NAME="zip" SIZE=10 VALUE="<%= $zip %>"></TD>
402 </TR>
403 <TR>
404   <TD ALIGN="right">Day Phone</TD>
405   <TD COLSPAN=5><INPUT TYPE="text" NAME="daytime" VALUE="<%= $daytime %>" SIZE=18></TD>
406 </TR>
407 <TR>
408   <TD ALIGN="right">Night Phone</TD>
409   <TD COLSPAN=5><INPUT TYPE="text" NAME="night" VALUE="<%= $night %>" SIZE=18></TD>
410 </TR>
411 <TR>
412   <TD ALIGN="right">Fax</TD>
413   <TD COLSPAN=5><INPUT TYPE="text" NAME="fax" VALUE="<%= $fax %>" SIZE=12></TD>
414 </TR>
415 </TABLE><font color="#ff0000">*</font> required fields<BR>
416 <BR>Billing information<TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
417 <TR><TD>
418
419   <%=
420     $OUT .= '<INPUT TYPE="checkbox" NAME="invoicing_list_POST" VALUE="POST"';
421     my @invoicing_list = split(', ', $invoicing_list );
422     $OUT .= ' CHECKED'
423       if ! @invoicing_list || grep { $_ eq 'POST' } @invoicing_list;
424     $OUT .= '>';
425   %>
426
427   Postal mail invoice
428 </TD></TR>
429 <TR><TD>Email invoice <INPUT TYPE="text" NAME="invoicing_list" VALUE="<%= join(', ', grep { $_ ne 'POST' } split(', ', $invoicing_list ) ) %>">
430 </TD></TR>
431 <TR><TD>Billing type</TD></TR></TABLE>
432 <TABLE BGCOLOR="#c0c0c0" BORDER=1 WIDTH="100%">
433 <TR>
434
435   <%=
436     my %payby = (
437       'CARD' => qq!Credit card<BR><font color="#ff0000">*</font><INPUT TYPE="text" NAME="CARD_payinfo" VALUE="" MAXLENGTH=19><BR><font color="#ff0000">*</font>Exp !. expselect("CARD"). qq!<BR><font color="#ff0000">*</font>Name on card<BR><INPUT TYPE="text" NAME="CARD_payname" VALUE="">!,
438       'BILL' => qq!Billing<BR>P.O. <INPUT TYPE="text" NAME="BILL_payinfo" VALUE=""><BR><font color="#ff0000">*</font>Exp !. expselect("BILL", "12-2037"). qq!<BR><font color="#ff0000">*</font>Attention<BR><INPUT TYPE="text" NAME="BILL_payname" VALUE="Accounts Payable">!,
439       'COMP' => qq!Complimentary<BR><font color="#ff0000">*</font>Approved by<INPUT TYPE="text" NAME="COMP_payinfo" VALUE=""><BR><font color="#ff0000">*</font>Exp !. expselect("COMP"),
440       'PREPAY' => qq!Prepaid card<BR><font color="#ff0000">*</font><INPUT TYPE="text" NAME="PREPAY_payinfo" VALUE="" MAXLENGTH=80>!,
441     );
442
443     my %paybychecked = (
444       'CARD' => qq!Credit card<BR><font color="#ff0000">*</font><INPUT TYPE="text" NAME="CARD_payinfo" VALUE="$payinfo" MAXLENGTH=19><BR><font color="#ff0000">*</font>Exp !. expselect("CARD", $paydate). qq!<BR><font color="#ff0000">*</font>Name on card<BR><INPUT TYPE="text" NAME="CARD_payname" VALUE="$payname">!,
445       'BILL' => qq!Billing<BR>P.O. <INPUT TYPE="text" NAME="BILL_payinfo" VALUE="$payinfo"><BR><font color="#ff0000">*</font>Exp !. expselect("BILL", $paydate). qq!<BR><font color="#ff0000">*</font>Attention<BR><INPUT TYPE="text" NAME="BILL_payname" VALUE="$payname">!,
446       'COMP' => qq!Complimentary<BR><font color="#ff0000">*</font>Approved by<INPUT TYPE="text" NAME="COMP_payinfo" VALUE="$payinfo"><BR><font color="#ff0000">*</font>Exp !. expselect("COMP", $paydate),
447       'PREPAY' => qq!Prepaid card<BR><font color="#ff0000">*</font><INPUT TYPE="text" NAME="PREPAY_payinfo" VALUE="$payinfo" MAXLENGTH=80>!,
448     );
449
450     for (@payby) {
451       $OUT .= qq!<TD VALIGN=TOP><INPUT TYPE="radio" NAME="payby" VALUE="$_"!;
452       if ($payby eq $_) {
453         $OUT .= qq! CHECKED> $paybychecked{$_}</TD>!;
454       } else {
455         $OUT .= qq!> $payby{$_}</TD>!;
456       }
457     }
458   %>
459
460 </TR></TABLE><font color="#ff0000">*</font> required fields for each billing type
461 <BR><BR>First package
462 <TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
463 <TR>
464   <TD COLSPAN=2><SELECT NAME="pkgpart"><OPTION VALUE="">(none)
465
466   <%=
467     foreach my $package ( @{$packages} ) {
468       $OUT .= '<OPTION VALUE="'. $package->{'pkgpart'}. '"';
469       $OUT .= ' SELECTED' if $pkgpart && $package->{'pkgpart'} == $pkgpart;
470       $OUT .= '>'. $package->{'pkg'};
471     }
472   %>
473
474   </SELECT></TD>
475 </TR>
476 <TR>
477   <TD ALIGN="right">Username</TD>
478   <TD><INPUT TYPE="text" NAME="username" VALUE="<%= $username %>"></TD>
479 </TR>
480 <TR>
481   <TD ALIGN="right">Password</TD>
482   <TD><INPUT TYPE="password" NAME="_password" VALUE="<%= $password %>">
483   (blank to generate)</TD>
484 </TR>
485 <TR>
486   <TD ALIGN="right">Re-enter Password</TD>
487   <TD><INPUT TYPE="password" NAME="_password2" VALUE="<%= $password2 %>">
488   </TD>
489 </TR>
490 <%=
491   if ( $init_data->{'security_phrase'} ) {
492     $OUT .= <<ENDOUT;
493 <TR>
494   <TD ALIGN="right">Security Phrase</TD>
495   <TD><INPUT TYPE="text" NAME="sec_phrase" VALUE="$sec_phrase">
496   </TD>
497 </TR>
498 ENDOUT
499   } else {
500     $OUT .= '<INPUT TYPE="hidden" NAME="sec_phrase" VALUE="">';
501   }
502 %>
503 <TR>
504   <TD ALIGN="right">Access number</TD>
505   <TD><%= popselector($popnum) %></TD>
506 </TR>
507 </TABLE>
508 <BR><BR><INPUT TYPE="submit" VALUE="Signup">
509 </FORM></BODY></HTML>
510 END
511 }