Netscape CCK autoconfiguration support
[freeside.git] / fs_signup / FS-SignupClient / cgi / signup.cgi
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: signup.cgi,v 1.7 2000-05-10 23:57:57 ivan Exp $
4
5 use strict;
6 use vars qw( @payby $cgi $locales $packages $pops $r $error
7              $last $first $ss $company $address1 $address2 $city $state $county
8              $country $zip $daytime $night $fax $invoicing_list $payby $payinfo
9              $paydate $payname $pkgpart $username $password $popnum
10              $ieak_file $ieak_template $cck_file $cck_template
11              $ac $exch $loc
12            );
13 use subs qw( print_form print_okay expselect );
14
15 use CGI;
16 use CGI::Carp qw(fatalsToBrowser);
17 use HTTP::Headers::UserAgent 2.00;
18 use FS::SignupClient qw( signup_info new_customer );
19 use Text::Template;
20
21 #acceptable payment methods
22 #
23 #@payby = qw( CARD BILL COMP );
24 #@payby = qw( CARD BILL );
25 #@payby = qw( CARD );
26 @payby = qw( CARD PREPAY );
27
28 $ieak_file = '/usr/local/freeside/ieak.template';
29 $cck_file = '/usr/local/freeside/cck.template';
30
31 if ( -e $ieak_file ) {
32   $ieak_template = new Text::Template ( TYPE => 'FILE', SOURCE => $ieak_file )
33     or die "Couldn't construct template: $Text::Template::ERROR";
34 } else {
35   $ieak_template = '';
36 }
37 if ( -e $cck_file ) {
38   $cck_template = new Text::Template ( TYPE => 'FILE', SOURCE => $cck_file )
39     or die "Couldn't construct template: $Text::Template::ERROR";
40 } else {
41   $cck_template = '';
42 }
43
44 ( $locales, $packages, $pops ) = signup_info();
45
46 $cgi = new CGI;
47
48 if ( defined $cgi->param('magic') ) {
49   if ( $cgi->param('magic') eq 'process' ) {
50
51     $cgi->param('state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/
52       or die "Oops, illegal \"state\" param: ". $cgi->param('state');
53     $state = $1;
54     $county = $3 || '';
55     $country = $4;
56
57     $payby = $cgi->param('payby');
58     $payinfo = $cgi->param( $payby. '_payinfo' );
59     $paydate =
60       $cgi->param( $payby. '_month' ). '-'. $cgi->param( $payby. '_year' );
61     $payname = $cgi->param( $payby. '_payname' );
62
63     if ( $invoicing_list = $cgi->param('invoicing_list') ) {
64       $invoicing_list .= ', POST' if $cgi->param('invoicing_list_POST');
65     } else {
66       $invoicing_list = 'POST';
67     }
68
69     ( $error = new_customer ( {
70       'last'           => $last            = $cgi->param('last'),
71       'first'          => $first           = $cgi->param('first'),
72       'ss'             => $ss              = $cgi->param('ss'),
73       'company'        => $company         = $cgi->param('company'),
74       'address1'       => $address1        = $cgi->param('address1'),
75       'address2'       => $address2        = $cgi->param('address2'),
76       'city'           => $city            = $cgi->param('city'),
77       'county'         => $county,
78       'state'          => $state,
79       'zip'            => $zip             = $cgi->param('zip'),
80       'country'        => $country,
81       'daytime'        => $daytime         = $cgi->param('daytime'),
82       'night'          => $night           = $cgi->param('night'),
83       'fax'            => $fax             = $cgi->param('fax'),
84       'payby'          => $payby,
85       'payinfo'        => $payinfo,
86       'paydate'        => $paydate,
87       'payname'        => $payname,
88       'invoicing_list' => $invoicing_list,
89       'pkgpart'        => $pkgpart         = $cgi->param('pkgpart'),
90       'username'       => $username        = $cgi->param('username'),
91       '_password'      => $password        = $cgi->param('_password'),
92       'popnum'         => $popnum          = $cgi->param('popnum'),
93     } ) )
94       ? print_form()
95       : print_okay();
96   } else {
97     die "unrecognized magic: ". $cgi->param('magic');
98   }
99 } else {
100   $error = '';
101   $last = '';
102   $first = '';
103   $ss = '';
104   $company = '';
105   $address1 = '';
106   $address2 = '';
107   $city = '';
108   $state = '';
109   $county = '';
110   $country = '';
111   $zip = '';
112   $daytime = '';
113   $night = '';
114   $fax = '';
115   $invoicing_list = '';
116   $payby = '';
117   $payinfo = '';
118   $paydate = '';
119   $payname = '';
120   $pkgpart = '';
121   $username = '';
122   $password = '';
123   $popnum = '';
124
125   print_form;
126 }
127
128 sub print_form {
129
130   my $r = qq!<font color="#ff0000">*</font>!;
131   my $self_url = $cgi->self_url;
132
133   print $cgi->header( '-expires' => 'now' ), <<END;
134 <HTML><HEAD><TITLE>ISP Signup form</TITLE></HEAD>
135 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>ISP Signup form</FONT><BR><BR>
136 END
137
138   print qq!<FONT SIZE="+1" COLOR="#ff0000">Error: $error</FONT>! if $error;
139
140   print <<END;
141 <FORM ACTION="$self_url" METHOD=POST>
142 <INPUT TYPE="hidden" NAME="magic" VALUE="process">
143 Contact Information
144 <TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
145 <TR>
146   <TH ALIGN="right">${r}Contact name<BR>(last, first)</TH>
147   <TD COLSPAN=3><INPUT TYPE="text" NAME="last" VALUE="$last">,
148                 <INPUT TYPE="text" NAME="first" VALUE="$first"></TD>
149   <TD ALIGN="right">SS#</TD>
150   <TD><INPUT TYPE="text" NAME="ss" SIZE=11 VALUE="$ss"></TD>
151 </TR>
152 <TR>
153   <TD ALIGN="right">Company</TD>
154   <TD COLSPAN=5><INPUT TYPE="text" NAME="company" SIZE=70 VALUE="$company"></TD>
155 </TR>
156 <TR>
157   <TH ALIGN="right">${r}Address</TH>
158   <TD COLSPAN=5><INPUT TYPE="text" NAME="address1" SIZE=70 VALUE="$address1"></TD>
159 </TR>
160 <TR>
161   <TD ALIGN="right">&nbsp;</TD>
162   <TD COLSPAN=5><INPUT TYPE="text" NAME="address2" SIZE=70 VALUE="$address2"></TD>
163 </TR>
164 <TR>
165   <TH ALIGN="right">${r}City</TH>
166   <TD><INPUT TYPE="text" NAME="city" VALUE="$city"></TD>
167   <TH ALIGN="right">${r}State/Country</TH>
168   <TD><SELECT NAME="state" SIZE="1">
169 END
170
171   foreach ( @{$locales} ) {
172     print "<OPTION";
173     print " SELECTED" if ( $state eq $_->{'state'}
174                            && $county eq $_->{'county'}
175                            && $country eq $_->{'country'}
176                          );
177     print ">", $_->{'state'};
178     print " (",$_->{'county'},")" if $_->{'county'};
179     print " / ", $_->{'country'};
180   }
181
182   print <<END;
183   </SELECT></TD>
184   <TH>${r}Zip</TH>
185   <TD><INPUT TYPE="text" NAME="zip" SIZE=10 VALUE="$zip"></TD>
186 </TR>
187 <TR>
188   <TD ALIGN="right">Day Phone</TD>
189   <TD COLSPAN=5><INPUT TYPE="text" NAME="daytime" VALUE="$daytime" SIZE=18></TD>
190 </TR>
191 <TR>
192   <TD ALIGN="right">Night Phone</TD>
193   <TD COLSPAN=5><INPUT TYPE="text" NAME="night" VALUE="$night" SIZE=18></TD>
194 </TR>
195 <TR>
196   <TD ALIGN="right">Fax</TD>
197   <TD COLSPAN=5><INPUT TYPE="text" NAME="fax" VALUE="$fax" SIZE=12></TD>
198 </TR>
199 </TABLE>$r required fields<BR>
200 <BR>Billing information<TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
201 <TR><TD>
202 END
203
204   print qq!<INPUT TYPE="checkbox" NAME="invoicing_list_POST" VALUE="POST"!;
205   my @invoicing_list = split(', ', $invoicing_list );
206   print ' CHECKED'
207     if ! @invoicing_list || grep { $_ eq 'POST' } @invoicing_list;
208   print '>Postal mail invoice</TD></TR><TR><TD>Email invoice ',
209          qq!<INPUT TYPE="text" NAME="invoicing_list" VALUE="!,
210          join(', ', grep { $_ ne 'POST' } @invoicing_list ),
211          qq!"></TD></TR>!;
212
213   print <<END;
214 <TR><TD>Billing type</TD></TR></TABLE>
215 <TABLE BGCOLOR="#c0c0c0" BORDER=1 WIDTH="100%">
216 <TR>
217 END
218
219   my %payby = (
220     'CARD' => qq!Credit card<BR>${r}<INPUT TYPE="text" NAME="CARD_payinfo" VALUE="" MAXLENGTH=19><BR>${r}Exp !. expselect("CARD"). qq!<BR>${r}Name on card<BR><INPUT TYPE="text" NAME="CARD_payname" VALUE="">!,
221     'BILL' => qq!Billing<BR>P.O. <INPUT TYPE="text" NAME="BILL_payinfo" VALUE=""><BR>${r}Exp !. expselect("BILL", "12-2037"). qq!<BR>${r}Attention<BR><INPUT TYPE="text" NAME="BILL_payname" VALUE="Accounts Payable">!,
222     'COMP' => qq!Complimentary<BR>${r}Approved by<INPUT TYPE="text" NAME="COMP_payinfo" VALUE=""><BR>${r}Exp !. expselect("COMP"),
223     'PREPAY' => qq!Prepaid card<BR>${r}<INPUT TYPE="text" NAME="PREPAY_payinfo" VALUE="" MAXLENGTH=80>!,
224   );
225
226   my %paybychecked = (
227     'CARD' => qq!Credit card<BR>${r}<INPUT TYPE="text" NAME="CARD_payinfo" VALUE="$payinfo" MAXLENGTH=19><BR>${r}Exp !. expselect("CARD", $paydate). qq!<BR>${r}Name on card<BR><INPUT TYPE="text" NAME="CARD_payname" VALUE="$payname">!,
228     'BILL' => qq!Billing<BR>P.O. <INPUT TYPE="text" NAME="BILL_payinfo" VALUE="$payinfo"><BR>${r}Exp !. expselect("BILL", $paydate). qq!<BR>${r}Attention<BR><INPUT TYPE="text" NAME="BILL_payname" VALUE="$payname">!,
229     'COMP' => qq!Complimentary<BR>${r}Approved by<INPUT TYPE="text" NAME="COMP_payinfo" VALUE="$payinfo"><BR>${r}Exp !. expselect("COMP", $paydate),
230     'PREPAY' => qq!Prepaid card<BR>${r}<INPUT TYPE="text" NAME="PREPAY_payinfo" VALUE="$payinfo" MAXLENGTH=80>!,
231   );
232
233   for (@payby) {
234     print qq!<TD VALIGN=TOP><INPUT TYPE="radio" NAME="payby" VALUE="$_"!;
235     if ($payby eq $_) {
236       print qq! CHECKED> $paybychecked{$_}</TD>!;
237     } else {
238       print qq!> $payby{$_}</TD>!;
239     }
240   }
241
242   print <<END;
243 </TR></TABLE>$r required fields for each billing type
244 <BR><BR>First package
245 <TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
246 <TR>
247   <TD COLSPAN=2><SELECT NAME="pkgpart"><OPTION VALUE="">(none)
248 END
249
250   foreach my $package ( @{$packages} ) {
251     print qq!<OPTION VALUE="!, $package->{'pkgpart'}, '"';
252     print " SELECTED" if $pkgpart && ( $package->{'pkgpart'} == $pkgpart );
253     print ">", $package->{'pkg'};
254   }
255
256   print <<END;
257   </SELECT></TD>
258 </TR>
259 <TR>
260   <TD ALIGN="right">Username</TD>
261   <TD><INPUT TYPE="text" NAME="username" VALUE="$username"></TD>
262 </TR>
263 <TR>
264   <TD ALIGN="right">Password</TD>
265   <TD><INPUT TYPE="text" NAME="_password" VALUE="$password">
266   (blank to generate)</TD>
267 </TR>
268 <TR>
269   <TD ALIGN="right">POP</TD>
270   <TD><SELECT NAME="popnum" SIZE=1><OPTION> 
271 END
272
273   foreach my $pop ( @{$pops} ) {
274     print qq!<OPTION VALUE="!, $pop->{'popnum'}, '"',
275           ( $popnum && $pop->{'popnum'} == $popnum ) ? ' SELECTED' : '', ">", 
276           $pop->{'popnum'}, ": ", 
277           $pop->{'city'}, ", ",
278           $pop->{'state'},
279           " (", $pop->{'ac'}, ")/",
280           $pop->{'exch'}, "\n"
281         ;
282   }
283   print <<END;
284   </SELECT></TD>
285 </TR>
286 </TABLE>
287 <BR><BR><INPUT TYPE="submit" VALUE="Signup">
288 </FORM></BODY></HTML>
289 END
290
291 }
292
293 sub print_okay {
294   my $user_agent = new HTTP::Headers::UserAgent $ENV{HTTP_USER_AGENT};
295
296   $cgi->param('username') =~ /^(.+)$/
297     or die "fatal: invalid username got past FS::SignupClient::new_customer";
298   my $username = $1;
299   $cgi->param('_password') =~ /^(.+)$/
300     or die "fatal: invalid password got past FS::SignupClient::new_customer";
301   my $password = $1;
302   ( $cgi->param('first'). ' '. $cgi->param('last') ) =~ /^(.*)$/
303     or die "fatal: invalid email_name got past FS::SignupCLient::new_customer";
304   my $email_name = $1;
305
306   my $pop = pop_info($cgi->param('popnum'))
307     or die "fatal: invalid popnum got past FS::SignupClient::new_customer";
308   my ( $ac, $exch, $loc ) = ( $pop->{'ac'}, $pop->{'exch'}, $pop->{'loc'} );
309
310   if ( $ieak_template
311        && $user_agent->platform eq 'ia32'
312        && $user_agent->os =~ /^win/
313        && ($user_agent->browser)[0] eq 'IE'
314      )
315   { #send an IEAK config
316     print $cgi->header('application/x-Internet-signup'),
317           $ieak_template->fill_in();
318   } elsif ( $cck_template
319             && $user_agent->platform eq 'ia32'
320             && $user_agent->os =~ /^win/
321             && ($user_agent->browser)[0] eq 'Netscape'
322           )
323   { #send a Netscape config
324     my $cck_data = $cck_template->fill_in();
325     print $cgi->header('application/x-netscape-autoconfigure-dialer-v2'),
326           map {
327             m/(.*)\s+(.*)$/;
328             pack("N", length($1)). $1. pack("N", length($2)). $2;
329           } split(/\n/, $cck_data);
330
331   } else { #send a simple confirmation
332     print $cgi->header( '-expires' => 'now' ), <<END;
333 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
334 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
335 blah blah blah
336 </BODY>
337 </HTML>
338 END
339   }
340 }
341
342 sub pop_info {
343   my $popnum = shift;
344   my $pop;
345   foreach $pop ( @{$pops} ) {
346     if ( $pop->{'popnum'} == $popnum ) { return $pop; }
347   }
348   '';
349 }
350
351 sub expselect {
352   my $prefix = shift;
353   my $date = shift || '';
354   my( $m, $y ) = ( 0, 0 );
355   if ( $date  =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #PostgreSQL date format
356     ( $m, $y ) = ( $2, $1 );
357   } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
358     ( $m, $y ) = ( $1, $3 );
359   }
360   my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
361   for ( 1 .. 12 ) {
362     $return .= "<OPTION";
363     $return .= " SELECTED" if $_ == $m;
364     $return .= ">$_";
365   }
366   $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
367   for ( 1999 .. 2037 ) {
368     $return .= "<OPTION";
369     $return .= " SELECTED" if $_ == $y;
370     $return .= ">$_";
371   }
372   $return .= "</SELECT>";
373
374   $return;
375 }
376