d44782f9c6a6dbc6cef57fd294a5f05d296638c2
[freeside.git] / fs_signup / FS-SignupClient / cgi / signup.cgi
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: signup.cgi,v 1.16 2002-04-06 20:37:38 ivan Exp $
4
5 use strict;
6 use vars qw( @payby $cgi $locales $packages $pops $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 $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.02 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 ) = 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         '_password'        => $password         = $cgi->param('_password'),
151         'popnum'           => $popnum           = $cgi->param('popnum'),
152       } );
153
154     }
155     
156     if ( $error ) {
157       print_form();
158     } else {
159       print_okay();
160     }
161
162   } else {
163     die "unrecognized magic: ". $cgi->param('magic');
164   }
165 } else {
166   $error = '';
167   $last = '';
168   $first = '';
169   $ss = '';
170   $company = '';
171   $address1 = '';
172   $address2 = '';
173   $city = '';
174   $state = '';
175   $county = '';
176   $country = '';
177   $zip = '';
178   $daytime = '';
179   $night = '';
180   $fax = '';
181   $invoicing_list = '';
182   $payby = '';
183   $payinfo = '';
184   $paydate = '';
185   $payname = '';
186   $pkgpart = '';
187   $username = '';
188   $password = '';
189   $password2 = '';
190   $popnum = '';
191   $referral_custnum = $cgi->param('ref') || '';
192   print_form;
193 }
194
195 sub print_form {
196
197   $cgi->delete('ref');
198   $self_url = $cgi->self_url;
199
200   $error = "Error: $error" if $error;
201
202   print $cgi->header( '-expires' => 'now' ),
203         $signup_template->fill_in();
204
205 }
206
207 sub print_okay {
208   my $user_agent = new HTTP::Headers::UserAgent $ENV{HTTP_USER_AGENT};
209
210   $cgi->param('username') =~ /^(.+)$/
211     or die "fatal: invalid username got past FS::SignupClient::new_customer";
212   my $username = $1;
213   $cgi->param('_password') =~ /^(.+)$/
214     or die "fatal: invalid password got past FS::SignupClient::new_customer";
215   my $password = $1;
216   ( $cgi->param('first'). ' '. $cgi->param('last') ) =~ /^(.*)$/
217     or die "fatal: invalid email_name got past FS::SignupCLient::new_customer";
218   my $email_name = $1;
219
220   my $pop = pop_info($cgi->param('popnum'))
221     or die "fatal: invalid popnum got past FS::SignupClient::new_customer";
222   ( $ac, $exch, $loc ) = ( $pop->{'ac'}, $pop->{'exch'}, $pop->{'loc'} );
223
224   if ( $ieak_template
225        && $user_agent->platform eq 'ia32'
226        && $user_agent->os =~ /^win/
227        && ($user_agent->browser)[0] eq 'IE'
228      )
229   { #send an IEAK config
230     print $cgi->header('application/x-Internet-signup'),
231           $ieak_template->fill_in();
232   } elsif ( $cck_template
233             && $user_agent->platform eq 'ia32'
234             && $user_agent->os =~ /^win/
235             && ($user_agent->browser)[0] eq 'Netscape'
236           )
237   { #send a Netscape config
238     my $cck_data = $cck_template->fill_in();
239     print $cgi->header('application/x-netscape-autoconfigure-dialer-v2'),
240           map {
241             m/(.*)\s+(.*)$/;
242             pack("N", length($1)). $1. pack("N", length($2)). $2;
243           } split(/\n/, $cck_data);
244
245   } else { #send a simple confirmation
246     print $cgi->header( '-expires' => 'now' ),
247           $success_template->fill_in();
248   }
249 }
250
251 sub pop_info {
252   my $popnum = shift;
253   my $pop;
254   foreach $pop ( @{$pops} ) {
255     if ( $pop->{'popnum'} == $popnum ) { return $pop; }
256   }
257   '';
258 }
259
260 #horrible false laziness with FS/FS/svc_acct_pop.pm::popselector
261 sub popselector {
262   my( $popnum, $state ) = @_;
263
264   my %pop = ();
265   push @{ $pop{$_->{state}} }, $_ foreach @$pops;
266
267   my $text = <<END;
268     <SCRIPT>
269     function opt(what,href,text) {
270       var optionName = new Option(text, href, false, false)
271       var length = what.length;
272       what.options[length] = optionName;
273     }
274     
275     function popstate_changed(what) {
276       state = what.options[what.selectedIndex].text;
277       for (var i = what.form.popnum.length;i > 0;i--)
278                 what.form.popnum.options[i] = null;
279       what.form.popnum.options[0] = new Option("", "", false, true);
280 END
281
282   foreach my $popstate ( sort { $a cmp $b } keys %pop ) {
283     $text .= "\nif ( state == \"$popstate\" ) {\n";
284
285     foreach my $pop ( @{$pop{$popstate}}) {
286       my $o_popnum = $pop->{popnum};
287       my $poptext =  $pop->{city}. ', '. $pop->{state}.
288                      ' ('. $pop->{ac}. ')/'. $pop->{exch};
289
290       $text .= "opt(what.form.popnum, \"$o_popnum\", \"$poptext\");\n"
291     }
292     $text .= "}\n";
293   }
294
295   $text .= "}\n</SCRIPT>\n";
296
297   $text .=
298     qq!<SELECT NAME="popstate" SIZE=1 onChange="popstate_changed(this)">!.
299     qq!<OPTION> !;
300   $text .= "<OPTION>$_" foreach sort { $a cmp $b } keys %pop;
301   $text .= '</SELECT>'; #callback? return 3 html pieces?  #'</TD><TD>';
302
303   $text .= qq!<SELECT NAME="popnum" SIZE=1><OPTION> !;
304   foreach my $pop ( @$pops ) {
305     $text .= qq!<OPTION VALUE="!. $pop->{popnum}. '"'.
306              ( ( $popnum && $pop->{popnum} == $popnum ) ? ' SELECTED' : '' ). ">".
307              $pop->{city}. ', '. $pop->{state}.
308                ' ('. $pop->{ac}. ')/'. $pop->{exch};
309   }
310   $text .= '</SELECT>';
311
312   $text;
313 }
314
315 sub expselect {
316   my $prefix = shift;
317   my $date = shift || '';
318   my( $m, $y ) = ( 0, 0 );
319   if ( $date  =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #PostgreSQL date format
320     ( $m, $y ) = ( $2, $1 );
321   } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
322     ( $m, $y ) = ( $1, $3 );
323   }
324   my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
325   for ( 1 .. 12 ) {
326     $return .= "<OPTION";
327     $return .= " SELECTED" if $_ == $m;
328     $return .= ">$_";
329   }
330   $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
331   for ( 2001 .. 2037 ) {
332     $return .= "<OPTION";
333     $return .= " SELECTED" if $_ == $y;
334     $return .= ">$_";
335   }
336   $return .= "</SELECT>";
337
338   $return;
339 }
340
341 sub success_default { #html to use if you don't specify a success file
342   <<'END';
343 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
344 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
345 Thanks for signing up!
346 </BODY></HTML>
347 END
348 }
349
350 sub signup_default { #html to use if you don't specify a template file
351   <<'END';
352 <HTML><HEAD><TITLE>ISP Signup form</TITLE></HEAD>
353 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>ISP Signup form</FONT><BR><BR>
354 <FONT SIZE="+1" COLOR="#ff0000"><%= $error %></FONT>
355 <FORM ACTION="<%= $self_url %>" METHOD=POST>
356 <INPUT TYPE="hidden" NAME="magic" VALUE="process">
357 <INPUT TYPE="hidden" NAME="ref" VALUE="<%= $referral_custnum %>">
358 <INPUT TYPE="hidden" NAME="ss" VALUE="">
359 Contact Information
360 <TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
361 <TR>
362   <TH ALIGN="right"><font color="#ff0000">*</font>Contact name<BR>(last, first)</TH>
363   <TD COLSPAN=5><INPUT TYPE="text" NAME="last" VALUE="<%= $last %>">,
364                 <INPUT TYPE="text" NAME="first" VALUE="<%= $first %>"></TD>
365 </TR>
366 <TR>
367   <TD ALIGN="right">Company</TD>
368   <TD COLSPAN=5><INPUT TYPE="text" NAME="company" SIZE=70 VALUE="<%= $company %>"></TD>
369 </TR>
370 <TR>
371   <TH ALIGN="right"><font color="#ff0000">*</font>Address</TH>
372   <TD COLSPAN=5><INPUT TYPE="text" NAME="address1" SIZE=70 VALUE="<%= $address1 %>"></TD>
373 </TR>
374 <TR>
375   <TD ALIGN="right">&nbsp;</TD>
376   <TD COLSPAN=5><INPUT TYPE="text" NAME="address2" SIZE=70 VALUE="<%= $address2 %>"></TD>
377 </TR>
378 <TR>
379   <TH ALIGN="right"><font color="#ff0000">*</font>City</TH>
380   <TD><INPUT TYPE="text" NAME="city" VALUE="<%= $city %>"></TD>
381   <TH ALIGN="right"><font color="#ff0000">*</font>State/Country</TH>
382   <TD><SELECT NAME="state" SIZE="1">
383
384   <%=
385     foreach ( @{$locales} ) {
386       $OUT .= '<OPTION';
387       $OUT .= ' SELECTED' if ( $state eq $_->{'state'}
388                                && $county eq $_->{'county'}
389                                && $country eq $_->{'country'}
390                              );
391       $OUT .= '>'. $_->{'state'};
392       $OUT .= ' ('. $_->{'county'}. ')' if $_->{'county'};
393       $OUT .= ' / '. $_->{'country'};
394     }
395   %>
396
397   </SELECT></TD>
398   <TH><font color="#ff0000">*</font>Zip</TH>
399   <TD><INPUT TYPE="text" NAME="zip" SIZE=10 VALUE="<%= $zip %>"></TD>
400 </TR>
401 <TR>
402   <TD ALIGN="right">Day Phone</TD>
403   <TD COLSPAN=5><INPUT TYPE="text" NAME="daytime" VALUE="<%= $daytime %>" SIZE=18></TD>
404 </TR>
405 <TR>
406   <TD ALIGN="right">Night Phone</TD>
407   <TD COLSPAN=5><INPUT TYPE="text" NAME="night" VALUE="<%= $night %>" SIZE=18></TD>
408 </TR>
409 <TR>
410   <TD ALIGN="right">Fax</TD>
411   <TD COLSPAN=5><INPUT TYPE="text" NAME="fax" VALUE="<%= $fax %>" SIZE=12></TD>
412 </TR>
413 </TABLE><font color="#ff0000">*</font> required fields<BR>
414 <BR>Billing information<TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
415 <TR><TD>
416
417   <%=
418     $OUT .= '<INPUT TYPE="checkbox" NAME="invoicing_list_POST" VALUE="POST"';
419     my @invoicing_list = split(', ', $invoicing_list );
420     $OUT .= ' CHECKED'
421       if ! @invoicing_list || grep { $_ eq 'POST' } @invoicing_list;
422     $OUT .= '>';
423   %>
424
425   Postal mail invoice
426 </TD></TR>
427 <TR><TD>Email invoice <INPUT TYPE="text" NAME="invoicing_list" VALUE="<%= join(', ', grep { $_ ne 'POST' } split(', ', $invoicing_list ) ) %>">
428 </TD></TR>
429 <TR><TD>Billing type</TD></TR></TABLE>
430 <TABLE BGCOLOR="#c0c0c0" BORDER=1 WIDTH="100%">
431 <TR>
432
433   <%=
434     my %payby = (
435       '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="">!,
436       '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">!,
437       '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"),
438       'PREPAY' => qq!Prepaid card<BR><font color="#ff0000">*</font><INPUT TYPE="text" NAME="PREPAY_payinfo" VALUE="" MAXLENGTH=80>!,
439     );
440
441     my %paybychecked = (
442       '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">!,
443       '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">!,
444       '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),
445       'PREPAY' => qq!Prepaid card<BR><font color="#ff0000">*</font><INPUT TYPE="text" NAME="PREPAY_payinfo" VALUE="$payinfo" MAXLENGTH=80>!,
446     );
447
448     for (@payby) {
449       $OUT .= qq!<TD VALIGN=TOP><INPUT TYPE="radio" NAME="payby" VALUE="$_"!;
450       if ($payby eq $_) {
451         $OUT .= qq! CHECKED> $paybychecked{$_}</TD>!;
452       } else {
453         $OUT .= qq!> $payby{$_}</TD>!;
454       }
455     }
456   %>
457
458 </TR></TABLE><font color="#ff0000">*</font> required fields for each billing type
459 <BR><BR>First package
460 <TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
461 <TR>
462   <TD COLSPAN=2><SELECT NAME="pkgpart"><OPTION VALUE="">(none)
463
464   <%=
465     foreach my $package ( @{$packages} ) {
466       $OUT .= '<OPTION VALUE="'. $package->{'pkgpart'}. '"';
467       $OUT .= ' SELECTED' if $pkgpart && $package->{'pkgpart'} == $pkgpart;
468       $OUT .= '>'. $package->{'pkg'};
469     }
470   %>
471
472   </SELECT></TD>
473 </TR>
474 <TR>
475   <TD ALIGN="right">Username</TD>
476   <TD><INPUT TYPE="text" NAME="username" VALUE="<%= $username %>"></TD>
477 </TR>
478 <TR>
479   <TD ALIGN="right">Password</TD>
480   <TD><INPUT TYPE="password" NAME="_password" VALUE="<%= $password %>">
481   (blank to generate)</TD>
482 </TR>
483 <TR>
484   <TD ALIGN="right">Re-enter Password</TD>
485   <TD><INPUT TYPE="password" NAME="_password2" VALUE="<%= $password2 %>">
486   </TD>
487 </TR>
488 <TR>
489   <TD ALIGN="right">Access number</TD>
490   <TD><%= popselector($popnum) %></TD>
491 </TR>
492 </TABLE>
493 <BR><BR><INPUT TYPE="submit" VALUE="Signup">
494 </FORM></BODY></HTML>
495 END
496 }