3 # $Id: signup.cgi,v 1.23 2002-04-17 12:14:37 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
12 $ieak_file $ieak_template $cck_file $cck_template
13 $signup_html $signup_template $success_html $success_template
17 use subs qw( print_form print_okay expselect signup_default success_default );
19 #use CGI::Carp qw(fatalsToBrowser);
21 use Business::CreditCard;
22 use HTTP::Headers::UserAgent 2.00;
23 use FS::SignupClient 0.03 qw( signup_info new_customer );
25 #acceptable payment methods
27 #@payby = qw( CARD BILL COMP );
28 #@payby = qw( CARD BILL );
30 @payby = qw( CARD PREPAY );
32 $ieak_file = '/usr/local/freeside/ieak.template';
33 $cck_file = '/usr/local/freeside/cck.template';
34 $signup_html = -e 'signup.html'
36 : '/usr/local/freeside/signup.html';
37 $success_html = -e 'success.html'
39 : '/usr/local/freeside/success.html';
41 if ( -e $ieak_file ) {
42 my $ieak_txt = Text::Template::_load_text($ieak_file)
43 or die $Text::Template::ERROR;
44 $ieak_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
46 $ieak_template = new Text::Template ( TYPE => 'STRING', SOURCE => $ieak_txt )
47 or die $Text::Template::ERROR;
53 my $cck_txt = Text::Template::_load_text($cck_file)
54 or die $Text::Template::ERROR;
55 $cck_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
57 $cck_template = new Text::Template ( TYPE => 'STRING', SOURCE => $cck_txt )
58 or die $Text::Template::ERROR;
63 if ( -e $signup_html ) {
64 my $signup_txt = Text::Template::_load_text($signup_html)
65 or die $Text::Template::ERROR;
66 $signup_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
68 $signup_template = new Text::Template ( TYPE => 'STRING',
69 SOURCE => $signup_txt,
70 DELIMITERS => [ '<%=', '%>' ]
72 or die $Text::Template::ERROR;
74 $signup_template = new Text::Template ( TYPE => 'STRING',
75 SOURCE => &signup_default,
76 DELIMITERS => [ '<%=', '%>' ]
78 or die $Text::Template::ERROR;
81 if ( -e $success_html ) {
82 my $success_txt = Text::Template::_load_text($success_html)
83 or die $Text::Template::ERROR;
84 $success_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
86 $success_template = new Text::Template ( TYPE => 'STRING',
87 SOURCE => $success_txt,
88 DELIMITERS => [ '<%=', '%>' ],
90 or die $Text::Template::ERROR;
92 $success_template = new Text::Template ( TYPE => 'STRING',
93 SOURCE => &success_default,
94 DELIMITERS => [ '<%=', '%>' ],
96 or die $Text::Template::ERROR;
99 ( $locales, $packages, $pops, $init_data ) = signup_info();
100 @payby = @{$init_data->{'payby'}} if @{$init_data->{'payby'}};
104 if ( defined $cgi->param('magic') ) {
105 if ( $cgi->param('magic') eq 'process' ) {
107 $cgi->param('state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/
108 or die "Oops, illegal \"state\" param: ". $cgi->param('state');
113 $payby = $cgi->param('payby');
114 $payinfo = $cgi->param( $payby. '_payinfo' );
116 $cgi->param( $payby. '_month' ). '-'. $cgi->param( $payby. '_year' );
117 $payname = $cgi->param( $payby. '_payname' );
119 if ( $invoicing_list = $cgi->param('invoicing_list') ) {
120 $invoicing_list .= ', POST' if $cgi->param('invoicing_list_POST');
122 $invoicing_list = 'POST';
127 if ( $cgi->param('_password') ne $cgi->param('_password2') ) {
128 $error = $init_data->{msgcat}{passwords_dont_match}; #msgcat
132 $password2 = $cgi->param('_password2');
134 if ( $payby eq 'CARD' && $cgi->param('CARD_type') ) {
137 $payinfo =~ /^(\d{13,16})$/
138 or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
141 or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
142 cardtype($payinfo) eq $cgi->param('CARD_type')
143 or $error ||= $init_data->{msgcat}{not_a}. $cgi->param('CARD_type');
146 $error ||= new_customer ( {
147 'last' => $last = $cgi->param('last'),
148 'first' => $first = $cgi->param('first'),
149 'ss' => $ss = $cgi->param('ss'),
150 'company' => $company = $cgi->param('company'),
151 'address1' => $address1 = $cgi->param('address1'),
152 'address2' => $address2 = $cgi->param('address2'),
153 'city' => $city = $cgi->param('city'),
156 'zip' => $zip = $cgi->param('zip'),
157 'country' => $country,
158 'daytime' => $daytime = $cgi->param('daytime'),
159 'night' => $night = $cgi->param('night'),
160 'fax' => $fax = $cgi->param('fax'),
162 'payinfo' => $payinfo,
163 'paydate' => $paydate,
164 'payname' => $payname,
165 'invoicing_list' => $invoicing_list,
166 'referral_custnum' => $referral_custnum = $cgi->param('ref'),
167 'pkgpart' => $pkgpart = $cgi->param('pkgpart'),
168 'username' => $username = $cgi->param('username'),
169 'sec_phrase' => $sec_phrase = $cgi->param('sec_phrase'),
170 '_password' => $password = $cgi->param('_password'),
171 'popnum' => $popnum = $cgi->param('popnum'),
172 'agentnum' => $agentnum = $cgi->param('agentnum'),
184 die "unrecognized magic: ". $cgi->param('magic');
202 $invoicing_list = '';
213 $referral_custnum = $cgi->param('ref') || '';
220 $self_url = $cgi->self_url;
222 $error = "Error: $error" if $error;
224 print $cgi->header( '-expires' => 'now' ),
225 $signup_template->fill_in();
230 my $user_agent = new HTTP::Headers::UserAgent $ENV{HTTP_USER_AGENT};
232 $cgi->param('username') =~ /^(.+)$/
233 or die "fatal: invalid username got past FS::SignupClient::new_customer";
235 $cgi->param('_password') =~ /^(.+)$/
236 or die "fatal: invalid password got past FS::SignupClient::new_customer";
238 ( $cgi->param('first'). ' '. $cgi->param('last') ) =~ /^(.*)$/
239 or die "fatal: invalid email_name got past FS::SignupClient::new_customer";
242 my $pop = pop_info($cgi->param('popnum'));
243 #or die "fatal: invalid popnum got past FS::SignupClient::new_customer";
245 ( $ac, $exch, $loc ) = ( $pop->{'ac'}, $pop->{'exch'}, $pop->{'loc'} );
247 ( $ac, $exch, $loc ) = ( '', '', ''); #presumably you're not using them.
250 my $pkg = ( grep { $_->{'pkgpart'} eq $pkgpart } @$packages )[0]->{'pkg'};
253 && $user_agent->platform eq 'ia32'
254 && $user_agent->os =~ /^win/
255 && ($user_agent->browser)[0] eq 'IE'
257 { #send an IEAK config
258 print $cgi->header('application/x-Internet-signup'),
259 $ieak_template->fill_in();
260 } elsif ( $cck_template
261 && $user_agent->platform eq 'ia32'
262 && $user_agent->os =~ /^win/
263 && ($user_agent->browser)[0] eq 'Netscape'
265 { #send a Netscape config
266 my $cck_data = $cck_template->fill_in();
267 print $cgi->header('application/x-netscape-autoconfigure-dialer-v2'),
270 pack("N", length($1)). $1. pack("N", length($2)). $2;
271 } split(/\n/, $cck_data);
273 } else { #send a simple confirmation
274 print $cgi->header( '-expires' => 'now' ),
275 $success_template->fill_in();
282 foreach $pop ( @{$pops} ) {
283 if ( $pop->{'popnum'} == $popnum ) { return $pop; }
288 #horrible false laziness with FS/FS/svc_acct_pop.pm::popselector
290 my( $popnum, $state ) = @_;
292 return '<INPUT TYPE="hidden" NAME="popnum" VALUE="">' unless @$pops;
293 return $pops->[0]{city}. ', '. $pops->[0]{state}.
294 ' ('. $pops->[0]{ac}. ')/'. $pops->[0]{exch}.
295 '<INPUT TYPE="hidden" NAME="popnum" VALUE="'. $pops->[0]{popnum}. '">'
296 if scalar(@$pops) == 1;
299 push @{ $pop{$_->{state}} }, $_ foreach @$pops;
303 function opt(what,href,text) {
304 var optionName = new Option(text, href, false, false)
305 var length = what.length;
306 what.options[length] = optionName;
309 function popstate_changed(what) {
310 state = what.options[what.selectedIndex].text;
311 for (var i = what.form.popnum.length;i > 0;i--)
312 what.form.popnum.options[i] = null;
313 what.form.popnum.options[0] = new Option("", "", false, true);
316 foreach my $popstate ( sort { $a cmp $b } keys %pop ) {
317 $text .= "\nif ( state == \"$popstate\" ) {\n";
319 foreach my $pop ( @{$pop{$popstate}}) {
320 my $o_popnum = $pop->{popnum};
321 my $poptext = $pop->{city}. ', '. $pop->{state}.
322 ' ('. $pop->{ac}. ')/'. $pop->{exch};
324 $text .= "opt(what.form.popnum, \"$o_popnum\", \"$poptext\");\n"
329 $text .= "}\n</SCRIPT>\n";
332 qq!<SELECT NAME="popstate" SIZE=1 onChange="popstate_changed(this)">!.
334 $text .= "<OPTION>$_" foreach sort { $a cmp $b } keys %pop;
335 $text .= '</SELECT>'; #callback? return 3 html pieces? #'</TD><TD>';
337 $text .= qq!<SELECT NAME="popnum" SIZE=1><OPTION> !;
338 foreach my $pop ( @$pops ) {
339 $text .= qq!<OPTION VALUE="!. $pop->{popnum}. '"'.
340 ( ( $popnum && $pop->{popnum} == $popnum ) ? ' SELECTED' : '' ). ">".
341 $pop->{city}. ', '. $pop->{state}.
342 ' ('. $pop->{ac}. ')/'. $pop->{exch};
344 $text .= '</SELECT>';
351 my $date = shift || '';
352 my( $m, $y ) = ( 0, 0 );
353 if ( $date =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #PostgreSQL date format
354 ( $m, $y ) = ( $2, $1 );
355 } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
356 ( $m, $y ) = ( $1, $3 );
358 my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
360 $return .= "<OPTION";
361 $return .= " SELECTED" if $_ == $m;
364 $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
365 for ( 2001 .. 2037 ) {
366 $return .= "<OPTION";
367 $return .= " SELECTED" if $_ == $y;
370 $return .= "</SELECT>";
375 sub success_default { #html to use if you don't specify a success file
377 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
378 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
379 Thanks for signing up!
381 Signup information for <%= $email_name %>:
383 Username: <%= $username %><BR>
384 Password: <%= $password %><BR>
385 Access number: (<%= $ac %>) / $exch - $local<BR>
386 Package: <%= $pkg %><BR>
391 sub signup_default { #html to use if you don't specify a template file
393 <HTML><HEAD><TITLE>ISP Signup form</TITLE></HEAD>
394 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>ISP Signup form</FONT><BR><BR>
395 <FONT SIZE="+1" COLOR="#ff0000"><%= $error %></FONT>
396 <FORM ACTION="<%= $self_url %>" METHOD=POST>
397 <INPUT TYPE="hidden" NAME="magic" VALUE="process">
398 <INPUT TYPE="hidden" NAME="ref" VALUE="<%= $referral_custnum %>">
399 <INPUT TYPE="hidden" NAME="ss" VALUE="">
401 <TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
403 <TH ALIGN="right"><font color="#ff0000">*</font>Contact name<BR>(last, first)</TH>
404 <TD COLSPAN=5><INPUT TYPE="text" NAME="last" VALUE="<%= $last %>">,
405 <INPUT TYPE="text" NAME="first" VALUE="<%= $first %>"></TD>
408 <TD ALIGN="right">Company</TD>
409 <TD COLSPAN=5><INPUT TYPE="text" NAME="company" SIZE=70 VALUE="<%= $company %>"></TD>
412 <TH ALIGN="right"><font color="#ff0000">*</font>Address</TH>
413 <TD COLSPAN=5><INPUT TYPE="text" NAME="address1" SIZE=70 VALUE="<%= $address1 %>"></TD>
416 <TD ALIGN="right"> </TD>
417 <TD COLSPAN=5><INPUT TYPE="text" NAME="address2" SIZE=70 VALUE="<%= $address2 %>"></TD>
420 <TH ALIGN="right"><font color="#ff0000">*</font>City</TH>
421 <TD><INPUT TYPE="text" NAME="city" VALUE="<%= $city %>"></TD>
422 <TH ALIGN="right"><font color="#ff0000">*</font>State/Country</TH>
423 <TD><SELECT NAME="state" SIZE="1">
426 foreach ( @{$locales} ) {
428 $OUT .= ' SELECTED' if ( $state eq $_->{'state'}
429 && $county eq $_->{'county'}
430 && $country eq $_->{'country'}
432 $OUT .= '>'. $_->{'state'};
433 $OUT .= ' ('. $_->{'county'}. ')' if $_->{'county'};
434 $OUT .= ' / '. $_->{'country'};
439 <TH><font color="#ff0000">*</font>Zip</TH>
440 <TD><INPUT TYPE="text" NAME="zip" SIZE=10 VALUE="<%= $zip %>"></TD>
443 <TD ALIGN="right">Day Phone</TD>
444 <TD COLSPAN=5><INPUT TYPE="text" NAME="daytime" VALUE="<%= $daytime %>" SIZE=18></TD>
447 <TD ALIGN="right">Night Phone</TD>
448 <TD COLSPAN=5><INPUT TYPE="text" NAME="night" VALUE="<%= $night %>" SIZE=18></TD>
451 <TD ALIGN="right">Fax</TD>
452 <TD COLSPAN=5><INPUT TYPE="text" NAME="fax" VALUE="<%= $fax %>" SIZE=12></TD>
454 </TABLE><font color="#ff0000">*</font> required fields<BR>
455 <BR>Billing information<TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
459 $OUT .= '<INPUT TYPE="checkbox" NAME="invoicing_list_POST" VALUE="POST"';
460 my @invoicing_list = split(', ', $invoicing_list );
462 if ! @invoicing_list || grep { $_ eq 'POST' } @invoicing_list;
468 <TR><TD>Email invoice <INPUT TYPE="text" NAME="invoicing_list" VALUE="<%= join(', ', grep { $_ ne 'POST' } split(', ', $invoicing_list ) ) %>">
470 <%= scalar(@payby) > 1 ? '<TR><TD>Billing type</TD></TR>' : '' %>
472 <TABLE BGCOLOR="#c0c0c0" BORDER=1 WIDTH="100%">
477 my $cardselect = '<SELECT NAME="CARD_type"><OPTION></OPTION>';
479 'VISA' => 'VISA card',
480 'MasterCard' => 'MasterCard',
481 'Discover' => 'Discover card',
482 'American Express' => 'American Express card',
484 foreach ( keys %types ) {
485 $selected = $cgi->param('CARD_type') eq $types{$_} ? 'SELECTED' : '';
486 $cardselect .= qq!<OPTION $selected VALUE="$types{$_}">$_</OPTION>!;
488 $cardselect .= '</SELECT>';
491 '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="">!,
492 '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">!,
493 '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"),
494 'PREPAY' => qq!Prepaid card<BR><font color="#ff0000">*</font><INPUT TYPE="text" NAME="PREPAY_payinfo" VALUE="" MAXLENGTH=80>!,
498 '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">!,
499 '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">!,
500 '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),
501 'PREPAY' => qq!Prepaid card<BR><font color="#ff0000">*</font><INPUT TYPE="text" NAME="PREPAY_payinfo" VALUE="$payinfo" MAXLENGTH=80>!,
505 if ( scalar(@payby) == 1) {
506 $OUT .= '<TD VALIGN=TOP>'.
507 qq!<INPUT TYPE="hidden" NAME="payby" VALUE="$_">!.
508 "$paybychecked{$_}</TD>";
510 $OUT .= qq!<TD VALIGN=TOP><INPUT TYPE="radio" NAME="payby" VALUE="$_"!;
512 $OUT .= qq! CHECKED> $paybychecked{$_}</TD>!;
514 $OUT .= qq!> $payby{$_}</TD>!;
521 </TR></TABLE><font color="#ff0000">*</font> required fields for each billing type
522 <BR><BR>First package
523 <TABLE BGCOLOR="#c0c0c0" BORDER=0 CELLSPACING=0 WIDTH="100%">
525 <TD COLSPAN=2><SELECT NAME="pkgpart"><OPTION VALUE="">(none)
528 foreach my $package ( @{$packages} ) {
529 $OUT .= '<OPTION VALUE="'. $package->{'pkgpart'}. '"';
530 $OUT .= ' SELECTED' if $pkgpart && $package->{'pkgpart'} == $pkgpart;
531 $OUT .= '>'. $package->{'pkg'};
538 <TD ALIGN="right">Username</TD>
539 <TD><INPUT TYPE="text" NAME="username" VALUE="<%= $username %>"></TD>
542 <TD ALIGN="right">Password</TD>
543 <TD><INPUT TYPE="password" NAME="_password" VALUE="<%= $password %>"></TD>
546 <TD ALIGN="right">Re-enter Password</TD>
547 <TD><INPUT TYPE="password" NAME="_password2" VALUE="<%= $password2 %>"></TD>
550 if ( $init_data->{'security_phrase'} ) {
553 <TD ALIGN="right">Security Phrase</TD>
554 <TD><INPUT TYPE="text" NAME="sec_phrase" VALUE="$sec_phrase">
559 $OUT .= '<INPUT TYPE="hidden" NAME="sec_phrase" VALUE="">';
563 if ( scalar(@$pops) ) {
564 $OUT .= '<TR><TD ALIGN="right">Access number</TD><TD>'.
565 popselector($popnum). '</TD></TR>';
567 $OUT .= popselector($popnum);
571 <BR><BR><INPUT TYPE="submit" VALUE="Signup">
572 </FORM></BODY></HTML>