3 # $Id: signup.cgi,v 1.29.2.18 2003-10-24 19:28:46 ivan Exp $
6 use vars qw( @payby $cgi $locales $packages
9 $last $first $ss $company $address1 $address2 $city $state $county
10 $country $zip $daytime $night $fax $invoicing_list $payby $payinfo
11 $paycvv $paydate $payname $referral_custnum $init_popstate
12 $pkgpart $username $password $password2 $sec_phrase $popnum
14 $ieak_file $ieak_template $cck_file $cck_template
15 $signup_html $signup_template
16 $success_html $success_template
17 $decline_html $decline_template
22 use subs qw( print_form print_okay print_decline
23 success_default decline_default
26 #use CGI::Carp qw(fatalsToBrowser);
28 use Business::CreditCard;
29 use HTTP::Headers::UserAgent 2.00;
30 use FS::SignupClient 0.03 qw( signup_info new_customer );
32 #acceptable payment methods
34 #@payby = qw( CARD BILL COMP );
35 #@payby = qw( CARD BILL );
37 @payby = qw( CARD PREPAY );
39 $ieak_file = '/usr/local/freeside/ieak.template';
40 $cck_file = '/usr/local/freeside/cck.template';
41 $signup_html = -e 'signup.html'
43 : '/usr/local/freeside/signup.html';
44 $success_html = -e 'success.html'
46 : '/usr/local/freeside/success.html';
47 $decline_html = -e 'decline.html'
49 : '/usr/local/freeside/decline.html';
52 if ( -e $ieak_file ) {
53 my $ieak_txt = Text::Template::_load_text($ieak_file)
54 or die $Text::Template::ERROR;
55 $ieak_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
57 $ieak_txt =~ s/\r//g; # don't double \r on old templates
58 $ieak_txt =~ s/\n/\r\n/g;
59 $ieak_template = new Text::Template ( TYPE => 'STRING', SOURCE => $ieak_txt )
60 or die $Text::Template::ERROR;
66 my $cck_txt = Text::Template::_load_text($cck_file)
67 or die $Text::Template::ERROR;
68 $cck_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
70 $cck_template = new Text::Template ( TYPE => 'STRING', SOURCE => $cck_txt )
71 or die $Text::Template::ERROR;
77 if ( -e $signup_html ) {
78 my $signup_txt = Text::Template::_load_text($signup_html)
79 or die $Text::Template::ERROR;
80 $signup_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
82 $signup_template = new Text::Template ( TYPE => 'STRING',
83 SOURCE => $signup_txt,
84 DELIMITERS => [ '<%=', '%>' ]
86 or die $Text::Template::ERROR;
88 /<\s*INPUT TYPE="?hidden"?\s+NAME="?agentnum"?\s+VALUE="?(\d+)"?\s*>/si
93 #too much maintenance hassle to keep in this file
94 die "can't find ./signup.html or /usr/local/freeside/signup.html";
95 #$signup_template = new Text::Template ( TYPE => 'STRING',
96 # SOURCE => &signup_default,
97 # DELIMITERS => [ '<%=', '%>' ]
99 # or die $Text::Template::ERROR;
102 if ( -e $success_html ) {
103 my $success_txt = Text::Template::_load_text($success_html)
104 or die $Text::Template::ERROR;
105 $success_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
107 $success_template = new Text::Template ( TYPE => 'STRING',
108 SOURCE => $success_txt,
109 DELIMITERS => [ '<%=', '%>' ],
111 or die $Text::Template::ERROR;
113 $success_template = new Text::Template ( TYPE => 'STRING',
114 SOURCE => &success_default,
115 DELIMITERS => [ '<%=', '%>' ],
117 or die $Text::Template::ERROR;
120 if ( -e $decline_html ) {
121 my $decline_txt = Text::Template::_load_text($decline_html)
122 or die $Text::Template::ERROR;
123 $decline_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
125 $decline_template = new Text::Template ( TYPE => 'STRING',
126 SOURCE => $decline_txt,
127 DELIMITERS => [ '<%=', '%>' ],
129 or die $Text::Template::ERROR;
131 $decline_template = new Text::Template ( TYPE => 'STRING',
132 SOURCE => &decline_default,
133 DELIMITERS => [ '<%=', '%>' ],
135 or die $Text::Template::ERROR;
139 ( $locales, $packages, $pops, $init_data ) = signup_info();
140 @payby = @{$init_data->{'payby'}} if @{$init_data->{'payby'}};
141 $packages = $init_data->{agentnum2part_pkg}{$agentnum} if $agentnum;
145 #push @{ $pop{ $_->{state} }->{ $_->{ac} } }, $_;
146 push @{ $pop{ $_->{state} } }, $_;
147 $popnum2pop{$_->{popnum}} = $_;
152 if ( defined $cgi->param('magic') ) {
153 if ( $cgi->param('magic') eq 'process' ) {
155 if ( $cgi->param('state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/ ) {
159 } elsif ( $cgi->param('state') =~ /^(\w*)$/ ) {
161 $cgi->param('county') =~ /^([\w ]*)$/
162 or die "illegal county: ". $cgi->param('county');
164 $cgi->param('country') =~ /^(\w+)$/
165 or die "illegal country: ". $cgi->param('country');
168 die "illegal state: ". $cgi->param('state');
171 $payby = $cgi->param('payby');
172 if ( $payby eq 'CHEK' ) {
173 #$payinfo = join('@', map { $cgi->param( $payby. "_payinfo$_" ) } (1,2) );
174 $payinfo = $cgi->param('CHEK_payinfo1').'@'.$cgi->param('CHEK_payinfo2');
176 $payinfo = $cgi->param( $payby. '_payinfo' );
179 $cgi->param( $payby. '_month' ). '-'. $cgi->param( $payby. '_year' );
180 $payname = $cgi->param( $payby. '_payname' );
181 $paycvv = defined $cgi->param( $payby. '_paycvv' )
182 ? $cgi->param( $payby. '_paycvv' )
185 if ( $invoicing_list = $cgi->param('invoicing_list') ) {
186 $invoicing_list .= ', POST' if $cgi->param('invoicing_list_POST');
188 $invoicing_list = 'POST';
193 $last = $cgi->param('last');
194 $first = $cgi->param('first');
195 $ss = $cgi->param('ss');
196 $company = $cgi->param('company');
197 $address1 = $cgi->param('address1');
198 $address2 = $cgi->param('address2');
199 $city = $cgi->param('city');
202 $zip = $cgi->param('zip');
204 $daytime = $cgi->param('daytime');
205 $night = $cgi->param('night');
206 $fax = $cgi->param('fax');
212 $referral_custnum = $cgi->param('ref');
213 $pkgpart = $cgi->param('pkgpart');
214 $username = $cgi->param('username');
215 $sec_phrase = $cgi->param('sec_phrase');
216 $password = $cgi->param('_password');
217 $popnum = $cgi->param('popnum');
218 #$agentnum, # = $cgi->param('agentnum'),
219 $agentnum ||= $cgi->param('agentnum');
220 $init_popstate = $cgi->param('init_popstate');
222 if ( $cgi->param('_password') ne $cgi->param('_password2') ) {
223 $error = $init_data->{msgcat}{passwords_dont_match}; #msgcat
227 $password2 = $cgi->param('_password2');
229 if ( $payby eq 'CARD' && $cgi->param('CARD_type') ) {
232 $payinfo =~ /^(\d{13,16})$/
233 or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
236 or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
237 cardtype($payinfo) eq $cgi->param('CARD_type')
238 or $error ||= $init_data->{msgcat}{not_a}. $cgi->param('CARD_type');
241 $error ||= new_customer ( {
245 'company' => $company,
246 'address1' => $address1,
247 'address2' => $address2,
252 'country' => $country,
253 'daytime' => $daytime,
257 'payinfo' => $payinfo,
259 'paydate' => $paydate,
260 'payname' => $payname,
261 'invoicing_list' => $invoicing_list,
262 'referral_custnum' => $referral_custnum,
263 'pkgpart' => $pkgpart,
264 'username' => $username,
265 'sec_phrase' => $sec_phrase,
266 '_password' => $password,
268 'agentnum' => $agentnum,
273 if ( $error eq '_decline' ) {
282 die "unrecognized magic: ". $cgi->param('magic');
293 $state = $cgi->param('init_popstate') || $init_data->{statedefault};
295 $country = $init_data->{countrydefault};
300 $invoicing_list = '';
311 $referral_custnum = $cgi->param('ref') || '';
312 $init_popstate = $cgi->param('init_popstate') || '';
319 $cgi->delete('init_popstate');
320 $self_url = $cgi->self_url;
322 $error = "Error: $error" if $error;
324 print $cgi->header( '-expires' => 'now' ),
325 $signup_template->fill_in();
330 print $cgi->header( '-expires' => 'now' ),
331 $decline_template->fill_in();
335 my $user_agent = new HTTP::Headers::UserAgent $ENV{HTTP_USER_AGENT};
337 $cgi->param('username') =~ /^(.+)$/
338 or die "fatal: invalid username got past FS::SignupClient::new_customer";
340 $cgi->param('_password') =~ /^(.+)$/
341 or die "fatal: invalid password got past FS::SignupClient::new_customer";
343 ( $cgi->param('first'). ' '. $cgi->param('last') ) =~ /^(.*)$/
344 or die "fatal: invalid email_name got past FS::SignupClient::new_customer";
345 $email_name = $1; #global for template
347 my $pop = $popnum2pop{$cgi->param('popnum')};
348 #or die "fatal: invalid popnum got past FS::SignupClient::new_customer";
350 ( $ac, $exch, $loc ) = ( $pop->{'ac'}, $pop->{'exch'}, $pop->{'loc'} );
352 ( $ac, $exch, $loc ) = ( '', '', ''); #presumably you're not using them.
356 $pkg = ( grep { $_->{'pkgpart'} eq $pkgpart } @$packages )[0]->{'pkg'};
359 && $user_agent->platform eq 'ia32'
360 && $user_agent->os =~ /^win/
361 && ($user_agent->browser)[0] eq 'IE'
363 { #send an IEAK config
364 print $cgi->header('application/x-Internet-signup'),
365 $ieak_template->fill_in();
366 } elsif ( $cck_template
367 && $user_agent->platform eq 'ia32'
368 && $user_agent->os =~ /^win/
369 && ($user_agent->browser)[0] eq 'Netscape'
371 { #send a Netscape config
372 my $cck_data = $cck_template->fill_in();
373 print $cgi->header('application/x-netscape-autoconfigure-dialer-v2'),
376 pack("N", length($1)). $1. pack("N", length($2)). $2;
377 } split(/\n/, $cck_data);
379 } else { #send a simple confirmation
380 print $cgi->header( '-expires' => 'now' ),
381 $success_template->fill_in();
385 #horrible false laziness with FS/FS/svc_acct_pop.pm::popselector
387 my( $popnum, $state ) = @_;
389 return '<INPUT TYPE="hidden" NAME="popnum" VALUE="">' unless @$pops;
390 return $pops->[0]{city}. ', '. $pops->[0]{state}.
391 ' ('. $pops->[0]{ac}. ')/'. $pops->[0]{exch}. '-'. $pops->[0]{loc}.
392 '<INPUT TYPE="hidden" NAME="popnum" VALUE="'. $pops->[0]{popnum}. '">'
393 if scalar(@$pops) == 1;
396 #push @{ $pop{$_->{state}} }, $_ foreach @$pops;
400 if ( $init_popstate ) {
401 $text .= '<INPUT TYPE="hidden" NAME="init_popstate" VALUE="'.
402 $init_popstate. '">';
406 function opt(what,href,text) {
407 var optionName = new Option(text, href, false, false)
408 var length = what.length;
409 what.options[length] = optionName;
411 function popstate_changed(what) {
412 state = what.options[what.selectedIndex].text;
413 what.form.popnum.options.length = 0;
414 what.form.popnum.options[0] = new Option("", "", false, true);
417 foreach my $popstate ( sort { $a cmp $b } keys %pop ) {
418 $text .= "\nif ( state == \"$popstate\" ) {\n";
420 foreach my $pop ( @{$pop{$popstate}}) {
421 my $o_popnum = $pop->{popnum};
422 my $poptext = $pop->{city}. ', '. $pop->{state}.
423 ' ('. $pop->{ac}. ')/'. $pop->{exch}. '-'. $pop->{loc};
425 $text .= "opt(what.form.popnum, \"$o_popnum\", \"$poptext\");\n"
430 $text .= "}\n</SCRIPT>\n";
433 qq!<SELECT NAME="popstate" SIZE=1 onChange="popstate_changed(this)">!.
435 $text .= "<OPTION>$_" foreach sort { $a cmp $b } keys %pop;
436 $text .= '</SELECT>'; #callback? return 3 html pieces? #'</TD><TD>';
439 $text .= qq!<SELECT NAME="popnum" SIZE=1><OPTION> !;
441 #comment this block to disable initial list population
442 my @initial_select = ();
443 if ( $init_popstate ) {
444 @initial_select = grep { $_->{state} eq $init_popstate } @$pops;
446 if ( scalar( @$pops ) > 100 ) {
447 push @initial_select, $popnum2pop{$popnum} if $popnum2pop{$popnum};
449 @initial_select = @$pops;
452 foreach my $pop ( sort { $a->{state} cmp $b->{state} } @initial_select ) {
453 $text .= qq!<OPTION VALUE="!. $pop->{popnum}. '"'.
454 ( ( $popnum && $pop->{popnum} == $popnum ) ? ' SELECTED' : '' ). ">".
455 $pop->{city}. ', '. $pop->{state}.
456 ' ('. $pop->{ac}. ')/'. $pop->{exch}. '-'. $pop->{loc};
458 $text .= '</SELECT>';
465 my $date = shift || '';
466 my( $m, $y ) = ( 0, 0 );
467 if ( $date =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #PostgreSQL date format
468 ( $m, $y ) = ( $2, $1 );
469 } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
470 ( $m, $y ) = ( $1, $3 );
472 my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
474 $return .= "<OPTION";
475 $return .= " SELECTED" if $_ == $m;
478 $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
479 for ( 2001 .. 2037 ) {
480 $return .= "<OPTION";
481 $return .= " SELECTED" if $_ == $y;
484 $return .= "</SELECT>";
489 #false laziness w/FS::cust_main_county
491 my ( $selected_county, $selected_state, $selected_country,
492 $prefix, $onchange ) = @_;
494 $prefix = '' unless defined $prefix;
498 my %cust_main_county;
500 # unless ( @cust_main_county ) { #cache
501 #@cust_main_county = qsearch('cust_main_county', {} );
502 #foreach my $c ( @cust_main_county ) {
503 foreach my $c ( @$locales ) {
504 #$countyflag=1 if $c->county;
505 $countyflag=1 if $c->{county};
506 #push @{$cust_main_county{$c->country}{$c->state}}, $c->county;
507 #$cust_main_county{$c->country}{$c->state}{$c->county} = 1;
508 $cust_main_county{$c->{country}}{$c->{state}}{$c->{county}} = 1;
511 $countyflag=1 if $selected_county;
513 my $script_html = <<END;
515 function opt(what,value,text) {
516 var optionName = new Option(text, value, false, false);
517 var length = what.length;
518 what.options[length] = optionName;
520 function ${prefix}country_changed(what) {
521 country = what.options[what.selectedIndex].text;
522 for ( var i = what.form.${prefix}state.length; i >= 0; i-- )
523 what.form.${prefix}state.options[i] = null;
525 #what.form.${prefix}state.options[0] = new Option('', '', false, true);
527 foreach my $country ( sort keys %cust_main_county ) {
528 $script_html .= "\nif ( country == \"$country\" ) {\n";
529 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
530 my $text = $state || '(n/a)';
531 $script_html .= qq!opt(what.form.${prefix}state, "$state", "$text");\n!;
533 $script_html .= "}\n";
536 $script_html .= <<END;
538 function ${prefix}state_changed(what) {
542 $script_html .= <<END;
543 state = what.options[what.selectedIndex].text;
544 country = what.form.${prefix}country.options[what.form.${prefix}country.selectedIndex].text;
545 for ( var i = what.form.${prefix}county.length; i >= 0; i-- )
546 what.form.${prefix}county.options[i] = null;
549 foreach my $country ( sort keys %cust_main_county ) {
550 $script_html .= "\nif ( country == \"$country\" ) {\n";
551 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
552 $script_html .= "\nif ( state == \"$state\" ) {\n";
553 #foreach my $county ( sort @{$cust_main_county{$country}{$state}} ) {
554 foreach my $county ( sort keys %{$cust_main_county{$country}{$state}} ) {
555 my $text = $county || '(n/a)';
557 qq!opt(what.form.${prefix}county, "$county", "$text");\n!;
559 $script_html .= "}\n";
561 $script_html .= "}\n";
565 $script_html .= <<END;
570 my $county_html = $script_html;
572 $county_html .= qq!<SELECT NAME="${prefix}county" onChange="$onchange">!;
573 $county_html .= '</SELECT>';
576 qq!<INPUT TYPE="hidden" NAME="${prefix}county" VALUE="$selected_county">!;
579 my $state_html = qq!<SELECT NAME="${prefix}state" !.
580 qq!onChange="${prefix}state_changed(this); $onchange">!;
581 foreach my $state ( sort keys %{ $cust_main_county{$selected_country} } ) {
582 my $text = $state || '(n/a)';
583 my $selected = $state eq $selected_state ? 'SELECTED' : '';
584 $state_html .= "\n<OPTION $selected VALUE=$state>$text</OPTION>"
586 $state_html .= '</SELECT>';
588 $state_html .= '</SELECT>';
590 my $country_html = qq!<SELECT NAME="${prefix}country" !.
591 qq!onChange="${prefix}country_changed(this); $onchange">!;
592 my $countrydefault = $init_data->{countrydefault} || 'US';
593 foreach my $country (
594 sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
595 keys %cust_main_county
597 my $selected = $country eq $selected_country ? ' SELECTED' : '';
598 $country_html .= "\n<OPTION$selected>$country</OPTION>"
600 $country_html .= '</SELECT>';
602 ($county_html, $state_html, $country_html);
606 sub success_default { #html to use if you don't specify a success file
608 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
609 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
610 Thanks for signing up!
612 Signup information for <%= $email_name %>:
614 Username: <%= $username %><BR>
615 Password: <%= $password %><BR>
616 Access number: (<%= $ac %>) / <%= $exch %> - <%= $local %><BR>
617 Package: <%= $pkg %><BR>
622 sub decline_default { #html to use if there is a decline
624 <HTML><HEAD><TITLE>Processing error</TITLE></HEAD>
625 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Processing error</FONT><BR><BR>
626 There has been an error processing your account. Please contact customer