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