3 # $Id: signup.cgi,v 1.35 2002-12-24 23:03:27 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
14 $success_html $success_template
15 $decline_html $decline_template
20 use subs qw( print_form print_okay print_decline
21 success_default decline_default
24 #use CGI::Carp qw(fatalsToBrowser);
26 use Business::CreditCard;
27 use HTTP::Headers::UserAgent 2.00;
28 use FS::SignupClient 0.03 qw( signup_info new_customer );
30 #acceptable payment methods
32 #@payby = qw( CARD BILL COMP );
33 #@payby = qw( CARD BILL );
35 @payby = qw( CARD PREPAY );
37 $ieak_file = '/usr/local/freeside/ieak.template';
38 $cck_file = '/usr/local/freeside/cck.template';
39 $signup_html = -e 'signup.html'
41 : '/usr/local/freeside/signup.html';
42 $success_html = -e 'success.html'
44 : '/usr/local/freeside/success.html';
45 $decline_html = -e 'decline.html'
47 : '/usr/local/freeside/decline.html';
50 if ( -e $ieak_file ) {
51 my $ieak_txt = Text::Template::_load_text($ieak_file)
52 or die $Text::Template::ERROR;
53 $ieak_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
55 $ieak_txt =~ s/\r//g; # don't double \r on old templates
56 $ieak_txt =~ s/\n/\r\n/g;
57 $ieak_template = new Text::Template ( TYPE => 'STRING', SOURCE => $ieak_txt )
58 or die $Text::Template::ERROR;
64 my $cck_txt = Text::Template::_load_text($cck_file)
65 or die $Text::Template::ERROR;
66 $cck_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
68 $cck_template = new Text::Template ( TYPE => 'STRING', SOURCE => $cck_txt )
69 or die $Text::Template::ERROR;
75 if ( -e $signup_html ) {
76 my $signup_txt = Text::Template::_load_text($signup_html)
77 or die $Text::Template::ERROR;
78 $signup_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
80 $signup_template = new Text::Template ( TYPE => 'STRING',
81 SOURCE => $signup_txt,
82 DELIMITERS => [ '<%=', '%>' ]
84 or die $Text::Template::ERROR;
86 /<\s*INPUT TYPE="?hidden"?\s+NAME="?agentnum"?\s+VALUE="?(\d+)"?\s*>/si
91 #too much maintenance hassle to keep in this file
92 die "can't find ./signup.html or /usr/local/freeside/signup.html";
93 #$signup_template = new Text::Template ( TYPE => 'STRING',
94 # SOURCE => &signup_default,
95 # DELIMITERS => [ '<%=', '%>' ]
97 # or die $Text::Template::ERROR;
100 if ( -e $success_html ) {
101 my $success_txt = Text::Template::_load_text($success_html)
102 or die $Text::Template::ERROR;
103 $success_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
105 $success_template = new Text::Template ( TYPE => 'STRING',
106 SOURCE => $success_txt,
107 DELIMITERS => [ '<%=', '%>' ],
109 or die $Text::Template::ERROR;
111 $success_template = new Text::Template ( TYPE => 'STRING',
112 SOURCE => &success_default,
113 DELIMITERS => [ '<%=', '%>' ],
115 or die $Text::Template::ERROR;
118 if ( -e $decline_html ) {
119 my $decline_txt = Text::Template::_load_text($decline_html)
120 or die $Text::Template::ERROR;
121 $decline_txt =~ /^(.*)$/s; #untaint the template source - it's trusted
123 $decline_template = new Text::Template ( TYPE => 'STRING',
124 SOURCE => $decline_txt,
125 DELIMITERS => [ '<%=', '%>' ],
127 or die $Text::Template::ERROR;
129 $decline_template = new Text::Template ( TYPE => 'STRING',
130 SOURCE => &decline_default,
131 DELIMITERS => [ '<%=', '%>' ],
133 or die $Text::Template::ERROR;
137 ( $locales, $packages, $pops, $init_data ) = signup_info();
138 @payby = @{$init_data->{'payby'}} if @{$init_data->{'payby'}};
139 $packages = $init_data->{agentnum2part_pkg}{$agentnum} if $agentnum;
143 if ( defined $cgi->param('magic') ) {
144 if ( $cgi->param('magic') eq 'process' ) {
146 if ( $cgi->param('state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/ ) {
150 } elsif ( $cgi->param('state') =~ /^(\w*)$/ ) {
152 $cgi->param('county') =~ /^([\w ]*)$/
153 or die "illegal county: ". $cgi->param('county');
155 $cgi->param('country') =~ /^(\w+)$/
156 or die "illegal country: ". $cgi->param('country');
159 die "illegal state: ". $cgi->param('state');
162 $payby = $cgi->param('payby');
163 if ( $payby eq 'CHEK' ) {
164 #$payinfo = join('@', map { $cgi->param( $payby. "_payinfo$_" ) } (1,2) );
165 $payinfo = $cgi->param('CHEK_payinfo1').'@'.$cgi->param('CHEK_payinfo2');
167 $payinfo = $cgi->param( $payby. '_payinfo' );
170 $cgi->param( $payby. '_month' ). '-'. $cgi->param( $payby. '_year' );
171 $payname = $cgi->param( $payby. '_payname' );
173 if ( $invoicing_list = $cgi->param('invoicing_list') ) {
174 $invoicing_list .= ', POST' if $cgi->param('invoicing_list_POST');
176 $invoicing_list = 'POST';
181 $last = $cgi->param('last');
182 $first = $cgi->param('first');
183 $ss = $cgi->param('ss');
184 $company = $cgi->param('company');
185 $address1 = $cgi->param('address1');
186 $address2 = $cgi->param('address2');
187 $city = $cgi->param('city');
190 $zip = $cgi->param('zip');
192 $daytime = $cgi->param('daytime');
193 $night = $cgi->param('night');
194 $fax = $cgi->param('fax');
200 $referral_custnum = $cgi->param('ref');
201 $pkgpart = $cgi->param('pkgpart');
202 $username = $cgi->param('username');
203 $sec_phrase = $cgi->param('sec_phrase');
204 $password = $cgi->param('_password');
205 $popnum = $cgi->param('popnum');
206 #$agentnum, # = $cgi->param('agentnum'),
208 if ( $cgi->param('_password') ne $cgi->param('_password2') ) {
209 $error = $init_data->{msgcat}{passwords_dont_match}; #msgcat
213 $password2 = $cgi->param('_password2');
215 if ( $payby eq 'CARD' && $cgi->param('CARD_type') ) {
218 $payinfo =~ /^(\d{13,16})$/
219 or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
222 or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
223 cardtype($payinfo) eq $cgi->param('CARD_type')
224 or $error ||= $init_data->{msgcat}{not_a}. $cgi->param('CARD_type');
227 $error ||= new_customer ( {
231 'company' => $company,
232 'address1' => $address1,
233 'address2' => $address2,
238 'country' => $country,
239 'daytime' => $daytime,
243 'payinfo' => $payinfo,
244 'paydate' => $paydate,
245 'payname' => $payname,
246 'invoicing_list' => $invoicing_list,
247 'referral_custnum' => $referral_custnum,
248 'pkgpart' => $pkgpart,
249 'username' => $username,
250 'sec_phrase' => $sec_phrase,
251 '_password' => $password,
253 'agentnum' => $agentnum,
258 if ( $error eq '_decline' ) {
267 die "unrecognized magic: ". $cgi->param('magic');
278 $state = $init_data->{statedefault};
280 $country = $init_data->{countrydefault};
285 $invoicing_list = '';
296 $referral_custnum = $cgi->param('ref') || '';
303 $self_url = $cgi->self_url;
305 $error = "Error: $error" if $error;
307 print $cgi->header( '-expires' => 'now' ),
308 $signup_template->fill_in();
313 print $cgi->header( '-expires' => 'now' ),
314 $decline_template->fill_in();
318 my $user_agent = new HTTP::Headers::UserAgent $ENV{HTTP_USER_AGENT};
320 $cgi->param('username') =~ /^(.+)$/
321 or die "fatal: invalid username got past FS::SignupClient::new_customer";
323 $cgi->param('_password') =~ /^(.+)$/
324 or die "fatal: invalid password got past FS::SignupClient::new_customer";
326 ( $cgi->param('first'). ' '. $cgi->param('last') ) =~ /^(.*)$/
327 or die "fatal: invalid email_name got past FS::SignupClient::new_customer";
328 $email_name = $1; #global for template
330 my $pop = pop_info($cgi->param('popnum'));
331 #or die "fatal: invalid popnum got past FS::SignupClient::new_customer";
333 ( $ac, $exch, $loc ) = ( $pop->{'ac'}, $pop->{'exch'}, $pop->{'loc'} );
335 ( $ac, $exch, $loc ) = ( '', '', ''); #presumably you're not using them.
339 $pkg = ( grep { $_->{'pkgpart'} eq $pkgpart } @$packages )[0]->{'pkg'};
342 && $user_agent->platform eq 'ia32'
343 && $user_agent->os =~ /^win/
344 && ($user_agent->browser)[0] eq 'IE'
346 { #send an IEAK config
347 print $cgi->header('application/x-Internet-signup'),
348 $ieak_template->fill_in();
349 } elsif ( $cck_template
350 && $user_agent->platform eq 'ia32'
351 && $user_agent->os =~ /^win/
352 && ($user_agent->browser)[0] eq 'Netscape'
354 { #send a Netscape config
355 my $cck_data = $cck_template->fill_in();
356 print $cgi->header('application/x-netscape-autoconfigure-dialer-v2'),
359 pack("N", length($1)). $1. pack("N", length($2)). $2;
360 } split(/\n/, $cck_data);
362 } else { #send a simple confirmation
363 print $cgi->header( '-expires' => 'now' ),
364 $success_template->fill_in();
371 foreach $pop ( @{$pops} ) {
372 if ( $pop->{'popnum'} == $popnum ) { return $pop; }
377 #horrible false laziness with FS/FS/svc_acct_pop.pm::popselector
382 return '<INPUT TYPE="hidden" NAME="popnum" VALUE="">' unless @$pops;
383 return $pops->[0]{city}. ', '. $pops->[0]{state}.
384 ' ('. $pops->[0]{ac}. ')/'. $pops->[0]{exch}.
385 '<INPUT TYPE="hidden" NAME="popnum" VALUE="'. $pops->[0]{popnum}. '">'
386 if scalar(@$pops) == 1;
390 push @{ $pop{ $_->{state} }->{ $_->{ac} } }, $_;
395 function opt(what,href,text) {
396 var optionName = new Option(text, href, false, false)
397 var length = what.length;
398 what.options[length] = optionName;
401 function acstate_changed(what) {
402 state = what.options[what.selectedIndex].text;
403 for (var i = what.form.popac.length;i > 0;i--)
404 what.form.popac.options[i] = null;
405 what.form.popac.options[0] = new Option("Area code", "-1", false, true);
408 foreach my $state ( sort { $a cmp $b } keys %pop ) {
409 $text .= "\nif ( state == \"$state\" ) {\n";
411 foreach my $ac ( sort { $a cmp $b } keys %{ $pop{$state} }) {
412 $text .= "opt(what.form.popac, \"$ac\", \"$ac\");\n";
413 if ($ac eq $cgi->param('popac')) {
414 $text .= "what.form.popac.options[what.form.popac.length-1].selected = true;\n";
419 $text .= "popac_changed(what.form.popac)}\n";
422 function popac_changed(what) {
423 ac = what.options[what.selectedIndex].text;
424 for (var i = what.form.popnum.length;i > 0;i--)
425 what.form.popnum.options[i] = null;
426 what.form.popnum.options[0] = new Option("City", "-1", false, true);
430 foreach my $state ( keys %pop ) {
431 foreach my $popac ( keys %{ $pop{$state} } ) {
432 $text .= "\nif ( ac == \"$popac\" ) {\n";
434 foreach my $pop ( @{$pop{$state}->{$popac}}) {
435 my $o_popnum = $pop->{popnum};
436 my $poptext = $pop->{city}. ', '. $pop->{state}.
437 ' ('. $pop->{ac}. ')/'. $pop->{exch};
439 $text .= "opt(what.form.popnum, \"$o_popnum\", \"$poptext\");\n";
440 if ($popnum == $o_popnum) {
441 $text .= "what.form.popnum.options[what.form.popnum.length-1].selected = true;\n";
449 $text .= "}\n</SCRIPT>\n";
452 qq!<TABLE CELLPADDING="0"><TR><TD><SELECT NAME="acstate"! .
453 qq!SIZE=1 onChange="acstate_changed(this)"><OPTION VALUE=-1>State!;
454 $text .= "<OPTION" . ($_ eq $cgi->param('acstate') ? " SELECTED" : "") .
455 ">$_" foreach sort { $a cmp $b } keys %pop;
456 $text .= '</SELECT>'; #callback? return 3 html pieces? #'</TD>';
459 qq!<SELECT NAME="popac" SIZE=1 onChange="popac_changed(this)">!.
460 qq!<OPTION>Area code</SELECT></TR><TR VALIGN="top">!;
462 $text .= qq!<TR><TD><SELECT NAME="popnum" SIZE=1 STYLE="width: 20em"><OPTION>City!;
464 #comment this block to disable initial list polulation
465 foreach my $pop ( sort { $a->{state} cmp $b->{state} } @$pops ) {
466 $text .= qq!<OPTION VALUE="!. $pop->{popnum}. '"'.
467 ( ( $popnum && $pop->{popnum} == $popnum ) ? ' SELECTED' : '' ). ">".
468 $pop->{city}. ', '. $pop->{state}.
469 ' ('. $pop->{ac}. ')/'. $pop->{exch};
472 $text .= qq!</SELECT></TD></TR></TABLE>!;
480 my $date = shift || '';
481 my( $m, $y ) = ( 0, 0 );
482 if ( $date =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #PostgreSQL date format
483 ( $m, $y ) = ( $2, $1 );
484 } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
485 ( $m, $y ) = ( $1, $3 );
487 my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
489 $return .= "<OPTION";
490 $return .= " SELECTED" if $_ == $m;
493 $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
494 for ( 2001 .. 2037 ) {
495 $return .= "<OPTION";
496 $return .= " SELECTED" if $_ == $y;
499 $return .= "</SELECT>";
504 #false laziness w/FS::cust_main_county
506 my ( $selected_county, $selected_state, $selected_country,
507 $prefix, $onchange ) = @_;
509 my $prefix = '' unless defined $prefix;
513 my %cust_main_county;
515 # unless ( @cust_main_county ) { #cache
516 #@cust_main_county = qsearch('cust_main_county', {} );
517 #foreach my $c ( @cust_main_county ) {
518 foreach my $c ( @$locales ) {
519 #$countyflag=1 if $c->county;
520 $countyflag=1 if $c->{county};
521 #push @{$cust_main_county{$c->country}{$c->state}}, $c->county;
522 #$cust_main_county{$c->country}{$c->state}{$c->county} = 1;
523 $cust_main_county{$c->{country}}{$c->{state}}{$c->{county}} = 1;
526 $countyflag=1 if $selected_county;
528 my $script_html = <<END;
530 function opt(what,value,text) {
531 var optionName = new Option(text, value, false, false);
532 var length = what.length;
533 what.options[length] = optionName;
535 function ${prefix}country_changed(what) {
536 country = what.options[what.selectedIndex].text;
537 for ( var i = what.form.${prefix}state.length; i >= 0; i-- )
538 what.form.${prefix}state.options[i] = null;
540 #what.form.${prefix}state.options[0] = new Option('', '', false, true);
542 foreach my $country ( sort keys %cust_main_county ) {
543 $script_html .= "\nif ( country == \"$country\" ) {\n";
544 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
545 my $text = $state || '(n/a)';
546 $script_html .= qq!opt(what.form.${prefix}state, "$state", "$text");\n!;
548 $script_html .= "}\n";
551 $script_html .= <<END;
553 function ${prefix}state_changed(what) {
557 $script_html .= <<END;
558 state = what.options[what.selectedIndex].text;
559 country = what.form.${prefix}country.options[what.form.${prefix}country.selectedIndex].text;
560 for ( var i = what.form.${prefix}county.length; i >= 0; i-- )
561 what.form.${prefix}county.options[i] = null;
564 foreach my $country ( sort keys %cust_main_county ) {
565 $script_html .= "\nif ( country == \"$country\" ) {\n";
566 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
567 $script_html .= "\nif ( state == \"$state\" ) {\n";
568 #foreach my $county ( sort @{$cust_main_county{$country}{$state}} ) {
569 foreach my $county ( sort keys %{$cust_main_county{$country}{$state}} ) {
570 my $text = $county || '(n/a)';
572 qq!opt(what.form.${prefix}county, "$county", "$text");\n!;
574 $script_html .= "}\n";
576 $script_html .= "}\n";
580 $script_html .= <<END;
585 my $county_html = $script_html;
587 $county_html .= qq!<SELECT NAME="${prefix}county" onChange="$onchange">!;
588 $county_html .= '</SELECT>';
591 qq!<INPUT TYPE="hidden" NAME="${prefix}county" VALUE="$selected_county">!;
594 my $state_html = qq!<SELECT NAME="${prefix}state" !.
595 qq!onChange="${prefix}state_changed(this); $onchange">!;
596 foreach my $state ( sort keys %{ $cust_main_county{$selected_country} } ) {
597 my $text = $state || '(n/a)';
598 my $selected = $state eq $selected_state ? 'SELECTED' : '';
599 $state_html .= "\n<OPTION $selected VALUE=$state>$text</OPTION>"
601 $state_html .= '</SELECT>';
603 $state_html .= '</SELECT>';
605 my $country_html = qq!<SELECT NAME="${prefix}country" !.
606 qq!onChange="${prefix}country_changed(this); $onchange">!;
607 my $countrydefault = $init_data->{countrydefault} || 'US';
608 foreach my $country (
609 sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
610 keys %cust_main_county
612 my $selected = $country eq $selected_country ? ' SELECTED' : '';
613 $country_html .= "\n<OPTION$selected>$country</OPTION>"
615 $country_html .= '</SELECT>';
617 ($county_html, $state_html, $country_html);
621 sub success_default { #html to use if you don't specify a success file
623 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
624 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
625 Thanks for signing up!
627 Signup information for <%= $email_name %>:
629 Username: <%= $username %><BR>
630 Password: <%= $password %><BR>
631 Access number: (<%= $ac %>) / $exch - $local<BR>
632 Package: <%= $pkg %><BR>
637 sub decline_default { #html to use if there is a decline
639 <HTML><HEAD><TITLE>Processing error</TITLE></HEAD>
640 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Processing error</FONT><BR><BR>
641 There has been an error processing your account. Please contact customer