3 # $Id: signup.cgi,v 1.20 2002-04-10 13:42:48 ivan Exp $
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
16 use subs qw( print_form print_okay expselect signup_default success_default );
18 #use CGI::Carp qw(fatalsToBrowser);
20 use Business::CreditCard;
21 use HTTP::Headers::UserAgent 2.00;
22 use FS::SignupClient 0.03 qw( signup_info new_customer );
24 #acceptable payment methods
26 #@payby = qw( CARD BILL COMP );
27 #@payby = qw( CARD BILL );
29 @payby = qw( CARD PREPAY );
31 $ieak_file = '/usr/local/freeside/ieak.template';
32 $cck_file = '/usr/local/freeside/cck.template';
33 $signup_html = '/usr/local/freeside/signup.html';
34 $success_html = '/usr/local/freeside/success.html';
36 if ( -e $ieak_file ) {
37 my $ieak_txt = Text::Template::_load_text($ieak_file)
38 or die $Text::Template::ERROR;
39 $ieak_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
41 $ieak_template = new Text::Template ( TYPE => 'STRING', SOURCE => $ieak_txt )
42 or die $Text::Template::ERROR;
48 my $cck_txt = Text::Template::_load_text($cck_file)
49 or die $Text::Template::ERROR;
50 $cck_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
52 $cck_template = new Text::Template ( TYPE => 'STRING', SOURCE => $cck_txt )
53 or die $Text::Template::ERROR;
58 if ( -e $signup_html ) {
59 my $signup_txt = Text::Template::_load_text($signup_html)
60 or die $Text::Template::ERROR;
61 $signup_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
63 $signup_template = new Text::Template ( TYPE => 'STRING',
64 SOURCE => $signup_txt,
65 DELIMITERS => [ '<%=', '%>' ]
67 or die $Text::Template::ERROR;
69 $signup_template = new Text::Template ( TYPE => 'STRING',
70 SOURCE => &signup_default,
71 DELIMITERS => [ '<%=', '%>' ]
73 or die $Text::Template::ERROR;
76 if ( -e $success_html ) {
77 my $success_txt = Text::Template::_load_text($success_html)
78 or die $Text::Template::ERROR;
79 $success_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
81 $success_template = new Text::Template ( TYPE => 'STRING',
82 SOURCE => $success_txt,
83 DELIMITERS => [ '<%=', '%>' ],
85 or die $Text::Template::ERROR;
87 $success_template = new Text::Template ( TYPE => 'STRING',
88 SOURCE => &success_default,
89 DELIMITERS => [ '<%=', '%>' ],
91 or die $Text::Template::ERROR;
94 ( $locales, $packages, $pops, $init_data ) = signup_info();
95 @payby = @{$init_data->{'payby'}} if @{$init_data->{'payby'}};
99 if ( defined $cgi->param('magic') ) {
100 if ( $cgi->param('magic') eq 'process' ) {
102 $cgi->param('state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/
103 or die "Oops, illegal \"state\" param: ". $cgi->param('state');
108 $payby = $cgi->param('payby');
109 $payinfo = $cgi->param( $payby. '_payinfo' );
111 $cgi->param( $payby. '_month' ). '-'. $cgi->param( $payby. '_year' );
112 $payname = $cgi->param( $payby. '_payname' );
114 if ( $invoicing_list = $cgi->param('invoicing_list') ) {
115 $invoicing_list .= ', POST' if $cgi->param('invoicing_list_POST');
117 $invoicing_list = 'POST';
122 if ( $cgi->param('_password') ne $cgi->param('_password2') ) {
123 $error = $init_data->{msgcat}{passwords_dont_match}; #msgcat
127 $password2 = $cgi->param('_password2');
129 if ( $payby eq 'CARD' && $cgi->param('CARD_type') ) {
132 $payinfo =~ /^(\d{13,16})$/
133 or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
136 or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
137 cardtype($payinfo) eq $cgi->param('CARD_type')
138 or $error ||= $init_data->{msgcat}{not_a}. $cgi->param('CARD_type');
141 $error ||= new_customer ( {
142 'last' => $last = $cgi->param('last'),
143 'first' => $first = $cgi->param('first'),
144 'ss' => $ss = $cgi->param('ss'),
145 'company' => $company = $cgi->param('company'),
146 'address1' => $address1 = $cgi->param('address1'),
147 'address2' => $address2 = $cgi->param('address2'),
148 'city' => $city = $cgi->param('city'),
151 'zip' => $zip = $cgi->param('zip'),
152 'country' => $country,
153 'daytime' => $daytime = $cgi->param('daytime'),
154 'night' => $night = $cgi->param('night'),
155 'fax' => $fax = $cgi->param('fax'),
157 'payinfo' => $payinfo,
158 'paydate' => $paydate,
159 'payname' => $payname,
160 'invoicing_list' => $invoicing_list,
161 'referral_custnum' => $referral_custnum = $cgi->param('ref'),
162 'pkgpart' => $pkgpart = $cgi->param('pkgpart'),
163 'username' => $username = $cgi->param('username'),
164 'sec_phrase' => $sec_phrase = $cgi->param('sec_phrase'),
165 '_password' => $password = $cgi->param('_password'),
166 'popnum' => $popnum = $cgi->param('popnum'),
178 die "unrecognized magic: ". $cgi->param('magic');
196 $invoicing_list = '';
207 $referral_custnum = $cgi->param('ref') || '';
214 $self_url = $cgi->self_url;
216 $error = "Error: $error" if $error;
218 print $cgi->header( '-expires' => 'now' ),
219 $signup_template->fill_in();
224 my $user_agent = new HTTP::Headers::UserAgent $ENV{HTTP_USER_AGENT};
226 $cgi->param('username') =~ /^(.+)$/
227 or die "fatal: invalid username got past FS::SignupClient::new_customer";
229 $cgi->param('_password') =~ /^(.+)$/
230 or die "fatal: invalid password got past FS::SignupClient::new_customer";
232 ( $cgi->param('first'). ' '. $cgi->param('last') ) =~ /^(.*)$/
233 or die "fatal: invalid email_name got past FS::SignupClient::new_customer";
236 my $pop = pop_info($cgi->param('popnum'))
237 or die "fatal: invalid popnum got past FS::SignupClient::new_customer";
238 ( $ac, $exch, $loc ) = ( $pop->{'ac'}, $pop->{'exch'}, $pop->{'loc'} );
240 my $pkg = ( grep { $_->{'pkgpart'} eq $pkgpart } @$packages )[0]->{'pkg'};
243 && $user_agent->platform eq 'ia32'
244 && $user_agent->os =~ /^win/
245 && ($user_agent->browser)[0] eq 'IE'
247 { #send an IEAK config
248 print $cgi->header('application/x-Internet-signup'),
249 $ieak_template->fill_in();
250 } elsif ( $cck_template
251 && $user_agent->platform eq 'ia32'
252 && $user_agent->os =~ /^win/
253 && ($user_agent->browser)[0] eq 'Netscape'
255 { #send a Netscape config
256 my $cck_data = $cck_template->fill_in();
257 print $cgi->header('application/x-netscape-autoconfigure-dialer-v2'),
260 pack("N", length($1)). $1. pack("N", length($2)). $2;
261 } split(/\n/, $cck_data);
263 } else { #send a simple confirmation
264 print $cgi->header( '-expires' => 'now' ),
265 $success_template->fill_in();
272 foreach $pop ( @{$pops} ) {
273 if ( $pop->{'popnum'} == $popnum ) { return $pop; }
278 #horrible false laziness with FS/FS/svc_acct_pop.pm::popselector
280 my( $popnum, $state ) = @_;
282 return '<INPUT TYPE="hidden" NAME="popnum" VALUE="">' unless @$pops;
283 return $pops->[0]{city}. ', '. $pops->[0]{state}.
284 ' ('. $pops->[0]{ac}. ')/'. $pops->[0]{exch}.
285 '<INPUT TYPE="hidden" NAME="popnum" VALUE="'. $pops->[0]{popnum}. '">'
286 if scalar(@$pops) == 1;
289 push @{ $pop{$_->{state}} }, $_ foreach @$pops;
293 function opt(what,href,text) {
294 var optionName = new Option(text, href, false, false)
295 var length = what.length;
296 what.options[length] = optionName;
299 function popstate_changed(what) {
300 state = what.options[what.selectedIndex].text;
301 for (var i = what.form.popnum.length;i > 0;i--)
302 what.form.popnum.options[i] = null;
303 what.form.popnum.options[0] = new Option("", "", false, true);
306 foreach my $popstate ( sort { $a cmp $b } keys %pop ) {
307 $text .= "\nif ( state == \"$popstate\" ) {\n";
309 foreach my $pop ( @{$pop{$popstate}}) {
310 my $o_popnum = $pop->{popnum};
311 my $poptext = $pop->{city}. ', '. $pop->{state}.
312 ' ('. $pop->{ac}. ')/'. $pop->{exch};
314 $text .= "opt(what.form.popnum, \"$o_popnum\", \"$poptext\");\n"
319 $text .= "}\n</SCRIPT>\n";
322 qq!<SELECT NAME="popstate" SIZE=1 onChange="popstate_changed(this)">!.
324 $text .= "<OPTION>$_" foreach sort { $a cmp $b } keys %pop;
325 $text .= '</SELECT>'; #callback? return 3 html pieces? #'</TD><TD>';
327 $text .= qq!<SELECT NAME="popnum" SIZE=1><OPTION> !;
328 foreach my $pop ( @$pops ) {
329 $text .= qq!<OPTION VALUE="!. $pop->{popnum}. '"'.
330 ( ( $popnum && $pop->{popnum} == $popnum ) ? ' SELECTED' : '' ). ">".
331 $pop->{city}. ', '. $pop->{state}.
332 ' ('. $pop->{ac}. ')/'. $pop->{exch};
334 $text .= '</SELECT>';
341 my $date = shift || '';
342 my( $m, $y ) = ( 0, 0 );
343 if ( $date =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #PostgreSQL date format
344 ( $m, $y ) = ( $2, $1 );
345 } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
346 ( $m, $y ) = ( $1, $3 );
348 my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
350 $return .= "<OPTION";
351 $return .= " SELECTED" if $_ == $m;
354 $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
355 for ( 2001 .. 2037 ) {
356 $return .= "<OPTION";
357 $return .= " SELECTED" if $_ == $y;
360 $return .= "</SELECT>";
365 sub success_default { #html to use if you don't specify a success file
367 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
368 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
369 Thanks for signing up!
371 Signup information for <%= $email_name %>:
373 Username: <%= $username %><BR>
374 Password: <%= $password %><BR>
375 Access number: (<%= $ac %>) / $exch - $local<BR>
376 Package: <%= $pkg %><BR>
381 sub signup_default { #html to use if you don't specify a template file
383 <HTML><HEAD><TITLE>ISP Signup form</TITLE></HEAD>
384 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>ISP Signup form</FONT><BR><BR>
385 <FONT SIZE="+1" COLOR="#ff0000"><%= $error %></FONT>
386 <FORM ACTION="<%= $self_url %>" METHOD=POST>
387 <INPUT TYPE="hidden" NAME="magic" VALUE="process">
388 <INPUT TYPE="hidden" NAME="ref" VALUE="<%= $referral_custnum %>">
389 <INPUT TYPE="hidden" NAME="ss" VALUE="">
391 <TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
393 <TH ALIGN="right"><font color="#ff0000">*</font>Contact name<BR>(last, first)</TH>
394 <TD COLSPAN=5><INPUT TYPE="text" NAME="last" VALUE="<%= $last %>">,
395 <INPUT TYPE="text" NAME="first" VALUE="<%= $first %>"></TD>
398 <TD ALIGN="right">Company</TD>
399 <TD COLSPAN=5><INPUT TYPE="text" NAME="company" SIZE=70 VALUE="<%= $company %>"></TD>
402 <TH ALIGN="right"><font color="#ff0000">*</font>Address</TH>
403 <TD COLSPAN=5><INPUT TYPE="text" NAME="address1" SIZE=70 VALUE="<%= $address1 %>"></TD>
406 <TD ALIGN="right"> </TD>
407 <TD COLSPAN=5><INPUT TYPE="text" NAME="address2" SIZE=70 VALUE="<%= $address2 %>"></TD>
410 <TH ALIGN="right"><font color="#ff0000">*</font>City</TH>
411 <TD><INPUT TYPE="text" NAME="city" VALUE="<%= $city %>"></TD>
412 <TH ALIGN="right"><font color="#ff0000">*</font>State/Country</TH>
413 <TD><SELECT NAME="state" SIZE="1">
416 foreach ( @{$locales} ) {
418 $OUT .= ' SELECTED' if ( $state eq $_->{'state'}
419 && $county eq $_->{'county'}
420 && $country eq $_->{'country'}
422 $OUT .= '>'. $_->{'state'};
423 $OUT .= ' ('. $_->{'county'}. ')' if $_->{'county'};
424 $OUT .= ' / '. $_->{'country'};
429 <TH><font color="#ff0000">*</font>Zip</TH>
430 <TD><INPUT TYPE="text" NAME="zip" SIZE=10 VALUE="<%= $zip %>"></TD>
433 <TD ALIGN="right">Day Phone</TD>
434 <TD COLSPAN=5><INPUT TYPE="text" NAME="daytime" VALUE="<%= $daytime %>" SIZE=18></TD>
437 <TD ALIGN="right">Night Phone</TD>
438 <TD COLSPAN=5><INPUT TYPE="text" NAME="night" VALUE="<%= $night %>" SIZE=18></TD>
441 <TD ALIGN="right">Fax</TD>
442 <TD COLSPAN=5><INPUT TYPE="text" NAME="fax" VALUE="<%= $fax %>" SIZE=12></TD>
444 </TABLE><font color="#ff0000">*</font> required fields<BR>
445 <BR>Billing information<TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
449 $OUT .= '<INPUT TYPE="checkbox" NAME="invoicing_list_POST" VALUE="POST"';
450 my @invoicing_list = split(', ', $invoicing_list );
452 if ! @invoicing_list || grep { $_ eq 'POST' } @invoicing_list;
458 <TR><TD>Email invoice <INPUT TYPE="text" NAME="invoicing_list" VALUE="<%= join(', ', grep { $_ ne 'POST' } split(', ', $invoicing_list ) ) %>">
460 <%= scalar(@payby) > 1 ? '<TR><TD>Billing type</TD></TR>' : '' %>
462 <TABLE BGCOLOR="#c0c0c0" BORDER=1 WIDTH="100%">
467 my $cardselect = '<SELECT NAME="CARD_type"><OPTION></OPTION>';
469 'VISA' => 'VISA card',
470 'MasterCard' => 'MasterCard',
471 'Discover' => 'Discover card',
472 'American Express' => 'American Express card',
474 foreach ( keys %types ) {
475 $selected = $cgi->param('CARD_type') eq $types{$_} ? 'SELECTED' : '';
476 $cardselect .= qq!<OPTION $selected VALUE="$types{$_}">$_</OPTION>!;
478 $cardselect .= '</SELECT>';
481 'CARD' => qq!Credit card<BR><font color="#ff0000">*</font>$cardselect<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="">!,
482 '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">!,
483 '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"),
484 'PREPAY' => qq!Prepaid card<BR><font color="#ff0000">*</font><INPUT TYPE="text" NAME="PREPAY_payinfo" VALUE="" MAXLENGTH=80>!,
488 'CARD' => qq!Credit card<BR><font color="#ff0000">*</font>$cardselect<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">!,
489 '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">!,
490 '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),
491 'PREPAY' => qq!Prepaid card<BR><font color="#ff0000">*</font><INPUT TYPE="text" NAME="PREPAY_payinfo" VALUE="$payinfo" MAXLENGTH=80>!,
495 if ( scalar(@payby) == 1) {
496 $OUT .= '<TD VALIGN=TOP>'.
497 qq!<INPUT TYPE="hidden" NAME="payby" VALUE="$_">!.
498 "$paybychecked{$_}</TD>";
500 $OUT .= qq!<TD VALIGN=TOP><INPUT TYPE="radio" NAME="payby" VALUE="$_"!;
502 $OUT .= qq! CHECKED> $paybychecked{$_}</TD>!;
504 $OUT .= qq!> $payby{$_}</TD>!;
511 </TR></TABLE><font color="#ff0000">*</font> required fields for each billing type
512 <BR><BR>First package
513 <TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
515 <TD COLSPAN=2><SELECT NAME="pkgpart"><OPTION VALUE="">(none)
518 foreach my $package ( @{$packages} ) {
519 $OUT .= '<OPTION VALUE="'. $package->{'pkgpart'}. '"';
520 $OUT .= ' SELECTED' if $pkgpart && $package->{'pkgpart'} == $pkgpart;
521 $OUT .= '>'. $package->{'pkg'};
528 <TD ALIGN="right">Username</TD>
529 <TD><INPUT TYPE="text" NAME="username" VALUE="<%= $username %>"></TD>
532 <TD ALIGN="right">Password</TD>
533 <TD><INPUT TYPE="password" NAME="_password" VALUE="<%= $password %>"></TD>
536 <TD ALIGN="right">Re-enter Password</TD>
537 <TD><INPUT TYPE="password" NAME="_password2" VALUE="<%= $password2 %>"></TD>
540 if ( $init_data->{'security_phrase'} ) {
543 <TD ALIGN="right">Security Phrase</TD>
544 <TD><INPUT TYPE="text" NAME="sec_phrase" VALUE="$sec_phrase">
549 $OUT .= '<INPUT TYPE="hidden" NAME="sec_phrase" VALUE="">';
553 if ( scalar(@$pops) ) {
554 $OUT .= '<TR><TD ALIGN="right">Access number</TD><TD>'.
555 popselector($popnum). '</TD></TR>';
557 $OUT .= popselector($popnum);
561 <BR><BR><INPUT TYPE="submit" VALUE="Signup">
562 </FORM></BODY></HTML>