signup server fix!
[freeside.git] / fs_signup / FS-SignupClient / cgi / signup.cgi
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: signup.cgi,v 1.13 2001-09-11 11:28:15 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 sub expselect {
247   my $prefix = shift;
248   my $date = shift || '';
249   my( $m, $y ) = ( 0, 0 );
250   if ( $date  =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #PostgreSQL date format
251     ( $m, $y ) = ( $2, $1 );
252   } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
253     ( $m, $y ) = ( $1, $3 );
254   }
255   my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
256   for ( 1 .. 12 ) {
257     $return .= "<OPTION";
258     $return .= " SELECTED" if $_ == $m;
259     $return .= ">$_";
260   }
261   $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
262   for ( 1999 .. 2037 ) {
263     $return .= "<OPTION";
264     $return .= " SELECTED" if $_ == $y;
265     $return .= ">$_";
266   }
267   $return .= "</SELECT>";
268
269   $return;
270 }
271
272 sub success_default { #html to use if you don't specify a success file
273   <<'END';
274 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
275 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
276 Thanks for signing up!
277 </BODY></HTML>
278 END
279 }
280
281 sub signup_default { #html to use if you don't specify a template file
282   <<'END';
283 <HTML><HEAD><TITLE>ISP Signup form</TITLE></HEAD>
284 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>ISP Signup form</FONT><BR><BR>
285 <FONT SIZE="+1" COLOR="#ff0000"><%= $error %></FONT>
286 <FORM ACTION="<%= $self_url %>" METHOD=POST>
287 <INPUT TYPE="hidden" NAME="magic" VALUE="process">
288 <INPUT TYPE="hidden" NAME="ref" VALUE="<%= $referral_custnum %>">
289 <INPUT TYPE="hidden" NAME="ss" VALUE="">
290 Contact Information
291 <TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
292 <TR>
293   <TH ALIGN="right"><font color="#ff0000">*</font>Contact name<BR>(last, first)</TH>
294   <TD COLSPAN=5><INPUT TYPE="text" NAME="last" VALUE="<%= $last %>">,
295                 <INPUT TYPE="text" NAME="first" VALUE="<%= $first %>"></TD>
296 </TR>
297 <TR>
298   <TD ALIGN="right">Company</TD>
299   <TD COLSPAN=5><INPUT TYPE="text" NAME="company" SIZE=70 VALUE="<%= $company %>"></TD>
300 </TR>
301 <TR>
302   <TH ALIGN="right"><font color="#ff0000">*</font>Address</TH>
303   <TD COLSPAN=5><INPUT TYPE="text" NAME="address1" SIZE=70 VALUE="<%= $address1 %>"></TD>
304 </TR>
305 <TR>
306   <TD ALIGN="right">&nbsp;</TD>
307   <TD COLSPAN=5><INPUT TYPE="text" NAME="address2" SIZE=70 VALUE="<%= $address2 %>"></TD>
308 </TR>
309 <TR>
310   <TH ALIGN="right"><font color="#ff0000">*</font>City</TH>
311   <TD><INPUT TYPE="text" NAME="city" VALUE="<%= $city %>"></TD>
312   <TH ALIGN="right"><font color="#ff0000">*</font>State/Country</TH>
313   <TD><SELECT NAME="state" SIZE="1">
314
315   <%=
316     foreach ( @{$locales} ) {
317       $OUT .= '<OPTION';
318       $OUT .= ' SELECTED' if ( $state eq $_->{'state'}
319                                && $county eq $_->{'county'}
320                                && $country eq $_->{'country'}
321                              );
322       $OUT .= '>'. $_->{'state'};
323       $OUT .= ' ('. $_->{'county'}. ')' if $_->{'county'};
324       $OUT .= ' / '. $_->{'country'};
325     }
326   %>
327
328   </SELECT></TD>
329   <TH><font color="#ff0000">*</font>Zip</TH>
330   <TD><INPUT TYPE="text" NAME="zip" SIZE=10 VALUE="<%= $zip %>"></TD>
331 </TR>
332 <TR>
333   <TD ALIGN="right">Day Phone</TD>
334   <TD COLSPAN=5><INPUT TYPE="text" NAME="daytime" VALUE="<%= $daytime %>" SIZE=18></TD>
335 </TR>
336 <TR>
337   <TD ALIGN="right">Night Phone</TD>
338   <TD COLSPAN=5><INPUT TYPE="text" NAME="night" VALUE="<%= $night %>" SIZE=18></TD>
339 </TR>
340 <TR>
341   <TD ALIGN="right">Fax</TD>
342   <TD COLSPAN=5><INPUT TYPE="text" NAME="fax" VALUE="<%= $fax %>" SIZE=12></TD>
343 </TR>
344 </TABLE><font color="#ff0000">*</font> required fields<BR>
345 <BR>Billing information<TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
346 <TR><TD>
347
348   <%=
349     $OUT .= '<INPUT TYPE="checkbox" NAME="invoicing_list_POST" VALUE="POST"';
350     my @invoicing_list = split(', ', $invoicing_list );
351     $OUT .= ' CHECKED'
352       if ! @invoicing_list || grep { $_ eq 'POST' } @invoicing_list;
353     $OUT .= '>';
354   %>
355
356   Postal mail invoice
357 </TD></TR>
358 <TR><TD>Email invoice <INPUT TYPE="text" NAME="invoicing_list" VALUE="<%= join(', ', grep { $_ ne 'POST' } split(', ', $invoicing_list ) ) %>">
359 </TD></TR>
360 <TR><TD>Billing type</TD></TR></TABLE>
361 <TABLE BGCOLOR="#c0c0c0" BORDER=1 WIDTH="100%">
362 <TR>
363
364   <%=
365     my %payby = (
366       '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="">!,
367       '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">!,
368       '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"),
369       'PREPAY' => qq!Prepaid card<BR><font color="#ff0000">*</font><INPUT TYPE="text" NAME="PREPAY_payinfo" VALUE="" MAXLENGTH=80>!,
370     );
371
372     my %paybychecked = (
373       '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">!,
374       '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">!,
375       '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),
376       'PREPAY' => qq!Prepaid card<BR><font color="#ff0000">*</font><INPUT TYPE="text" NAME="PREPAY_payinfo" VALUE="$payinfo" MAXLENGTH=80>!,
377     );
378
379     for (@payby) {
380       $OUT .= qq!<TD VALIGN=TOP><INPUT TYPE="radio" NAME="payby" VALUE="$_"!;
381       if ($payby eq $_) {
382         $OUT .= qq! CHECKED> $paybychecked{$_}</TD>!;
383       } else {
384         $OUT .= qq!> $payby{$_}</TD>!;
385       }
386     }
387   %>
388
389 </TR></TABLE><font color="#ff0000">*</font> required fields for each billing type
390 <BR><BR>First package
391 <TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
392 <TR>
393   <TD COLSPAN=2><SELECT NAME="pkgpart"><OPTION VALUE="">(none)
394
395   <%=
396     foreach my $package ( @{$packages} ) {
397       $OUT .= '<OPTION VALUE="'. $package->{'pkgpart'}. '"';
398       $OUT .= ' SELECTED' if $pkgpart && $package->{'pkgpart'} == $pkgpart;
399       $OUT .= '>'. $package->{'pkg'};
400     }
401   %>
402
403   </SELECT></TD>
404 </TR>
405 <TR>
406   <TD ALIGN="right">Username</TD>
407   <TD><INPUT TYPE="text" NAME="username" VALUE="<%= $username %>"></TD>
408 </TR>
409 <TR>
410   <TD ALIGN="right">Password</TD>
411   <TD><INPUT TYPE="text" NAME="_password" VALUE="<%= $password %>">
412   (blank to generate)</TD>
413 </TR>
414 <TR>
415   <TD ALIGN="right">POP</TD>
416   <TD><SELECT NAME="popnum" SIZE=1><OPTION> 
417
418   <%=
419     foreach my $pop ( @{$pops} ) {
420       $OUT .= '<OPTION VALUE="'. $pop->{'popnum'}. '"';
421       $OUT .= ' SELECTED' if $popnum && $pop->{'popnum'} == $popnum;
422       $OUT .= '>'. $pop->{'popnum'}. ': '. $pop->{'city'}. ', '.
423               $pop->{'state'}. ' ('. $pop->{'ac'}. ')/'. $pop->{'exch'}. "\n";
424     }
425   %>
426
427   </SELECT></TD>
428 </TR>
429 </TABLE>
430 <BR><BR><INPUT TYPE="submit" VALUE="Signup">
431 </FORM></BODY></HTML>
432 END
433 }