3 # $Id: signup.cgi,v 1.29.2.3 2002-11-28 10:54:28 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 $cgi->param('state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/
147 or die "Oops, illegal \"state\" param: ". $cgi->param('state');
152 $payby = $cgi->param('payby');
153 $payinfo = $cgi->param( $payby. '_payinfo' );
155 $cgi->param( $payby. '_month' ). '-'. $cgi->param( $payby. '_year' );
156 $payname = $cgi->param( $payby. '_payname' );
158 if ( $invoicing_list = $cgi->param('invoicing_list') ) {
159 $invoicing_list .= ', POST' if $cgi->param('invoicing_list_POST');
161 $invoicing_list = 'POST';
166 $last = $cgi->param('last');
167 $first = $cgi->param('first');
168 $ss = $cgi->param('ss');
169 $company = $cgi->param('company');
170 $address1 = $cgi->param('address1');
171 $address2 = $cgi->param('address2');
172 $city = $cgi->param('city');
175 $zip = $cgi->param('zip');
177 $daytime = $cgi->param('daytime');
178 $night = $cgi->param('night');
179 $fax = $cgi->param('fax');
185 $referral_custnum = $cgi->param('ref');
186 $pkgpart = $cgi->param('pkgpart');
187 $username = $cgi->param('username');
188 $sec_phrase = $cgi->param('sec_phrase');
189 $password = $cgi->param('_password');
190 $popnum = $cgi->param('popnum');
191 #$agentnum, # = $cgi->param('agentnum'),
193 if ( $cgi->param('_password') ne $cgi->param('_password2') ) {
194 $error = $init_data->{msgcat}{passwords_dont_match}; #msgcat
198 $password2 = $cgi->param('_password2');
200 if ( $payby eq 'CARD' && $cgi->param('CARD_type') ) {
203 $payinfo =~ /^(\d{13,16})$/
204 or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
207 or $error ||= $init_data->{msgcat}{invalid_card}; #. $self->payinfo;
208 cardtype($payinfo) eq $cgi->param('CARD_type')
209 or $error ||= $init_data->{msgcat}{not_a}. $cgi->param('CARD_type');
212 $error ||= new_customer ( {
216 'company' => $company,
217 'address1' => $address1,
218 'address2' => $address2,
223 'country' => $country,
224 'daytime' => $daytime,
228 'payinfo' => $payinfo,
229 'paydate' => $paydate,
230 'payname' => $payname,
231 'invoicing_list' => $invoicing_list,
232 'referral_custnum' => $referral_custnum,
233 'pkgpart' => $pkgpart,
234 'username' => $username,
235 'sec_phrase' => $sec_phrase,
236 '_password' => $password,
238 'agentnum' => $agentnum,
243 if ( $error eq '_decline' ) {
252 die "unrecognized magic: ". $cgi->param('magic');
263 $state = $init_data->{statedefault};
265 $country = $init_data->{countrydefault};
270 $invoicing_list = '';
281 $referral_custnum = $cgi->param('ref') || '';
288 $self_url = $cgi->self_url;
290 $error = "Error: $error" if $error;
292 print $cgi->header( '-expires' => 'now' ),
293 $signup_template->fill_in();
298 print $cgi->header( '-expires' => 'now' ),
299 $decline_template->fill_in();
303 my $user_agent = new HTTP::Headers::UserAgent $ENV{HTTP_USER_AGENT};
305 $cgi->param('username') =~ /^(.+)$/
306 or die "fatal: invalid username got past FS::SignupClient::new_customer";
308 $cgi->param('_password') =~ /^(.+)$/
309 or die "fatal: invalid password got past FS::SignupClient::new_customer";
311 ( $cgi->param('first'). ' '. $cgi->param('last') ) =~ /^(.*)$/
312 or die "fatal: invalid email_name got past FS::SignupClient::new_customer";
313 $email_name = $1; #global for template
315 my $pop = pop_info($cgi->param('popnum'));
316 #or die "fatal: invalid popnum got past FS::SignupClient::new_customer";
318 ( $ac, $exch, $loc ) = ( $pop->{'ac'}, $pop->{'exch'}, $pop->{'loc'} );
320 ( $ac, $exch, $loc ) = ( '', '', ''); #presumably you're not using them.
324 $pkg = ( grep { $_->{'pkgpart'} eq $pkgpart } @$packages )[0]->{'pkg'};
327 && $user_agent->platform eq 'ia32'
328 && $user_agent->os =~ /^win/
329 && ($user_agent->browser)[0] eq 'IE'
331 { #send an IEAK config
332 print $cgi->header('application/x-Internet-signup'),
333 $ieak_template->fill_in();
334 } elsif ( $cck_template
335 && $user_agent->platform eq 'ia32'
336 && $user_agent->os =~ /^win/
337 && ($user_agent->browser)[0] eq 'Netscape'
339 { #send a Netscape config
340 my $cck_data = $cck_template->fill_in();
341 print $cgi->header('application/x-netscape-autoconfigure-dialer-v2'),
344 pack("N", length($1)). $1. pack("N", length($2)). $2;
345 } split(/\n/, $cck_data);
347 } else { #send a simple confirmation
348 print $cgi->header( '-expires' => 'now' ),
349 $success_template->fill_in();
356 foreach $pop ( @{$pops} ) {
357 if ( $pop->{'popnum'} == $popnum ) { return $pop; }
362 #horrible false laziness with FS/FS/svc_acct_pop.pm::popselector
364 my( $popnum, $state ) = @_;
366 return '<INPUT TYPE="hidden" NAME="popnum" VALUE="">' unless @$pops;
367 return $pops->[0]{city}. ', '. $pops->[0]{state}.
368 ' ('. $pops->[0]{ac}. ')/'. $pops->[0]{exch}.
369 '<INPUT TYPE="hidden" NAME="popnum" VALUE="'. $pops->[0]{popnum}. '">'
370 if scalar(@$pops) == 1;
373 push @{ $pop{$_->{state}} }, $_ foreach @$pops;
377 function opt(what,href,text) {
378 var optionName = new Option(text, href, false, false)
379 var length = what.length;
380 what.options[length] = optionName;
383 function popstate_changed(what) {
384 state = what.options[what.selectedIndex].text;
385 for (var i = what.form.popnum.length;i > 0;i--)
386 what.form.popnum.options[i] = null;
387 what.form.popnum.options[0] = new Option("", "", false, true);
390 foreach my $popstate ( sort { $a cmp $b } keys %pop ) {
391 $text .= "\nif ( state == \"$popstate\" ) {\n";
393 foreach my $pop ( @{$pop{$popstate}}) {
394 my $o_popnum = $pop->{popnum};
395 my $poptext = $pop->{city}. ', '. $pop->{state}.
396 ' ('. $pop->{ac}. ')/'. $pop->{exch};
398 $text .= "opt(what.form.popnum, \"$o_popnum\", \"$poptext\");\n"
403 $text .= "}\n</SCRIPT>\n";
406 qq!<SELECT NAME="popstate" SIZE=1 onChange="popstate_changed(this)">!.
408 $text .= "<OPTION>$_" foreach sort { $a cmp $b } keys %pop;
409 $text .= '</SELECT>'; #callback? return 3 html pieces? #'</TD><TD>';
411 $text .= qq!<SELECT NAME="popnum" SIZE=1><OPTION> !;
412 foreach my $pop ( @$pops ) {
413 $text .= qq!<OPTION VALUE="!. $pop->{popnum}. '"'.
414 ( ( $popnum && $pop->{popnum} == $popnum ) ? ' SELECTED' : '' ). ">".
415 $pop->{city}. ', '. $pop->{state}.
416 ' ('. $pop->{ac}. ')/'. $pop->{exch};
418 $text .= '</SELECT>';
425 my $date = shift || '';
426 my( $m, $y ) = ( 0, 0 );
427 if ( $date =~ /^(\d{4})-(\d{2})-\d{2}$/ ) { #PostgreSQL date format
428 ( $m, $y ) = ( $2, $1 );
429 } elsif ( $date =~ /^(\d{1,2})-(\d{1,2}-)?(\d{4}$)/ ) {
430 ( $m, $y ) = ( $1, $3 );
432 my $return = qq!<SELECT NAME="$prefix!. qq!_month" SIZE="1">!;
434 $return .= "<OPTION";
435 $return .= " SELECTED" if $_ == $m;
438 $return .= qq!</SELECT>/<SELECT NAME="$prefix!. qq!_year" SIZE="1">!;
439 for ( 2001 .. 2037 ) {
440 $return .= "<OPTION";
441 $return .= " SELECTED" if $_ == $y;
444 $return .= "</SELECT>";
449 #false laziness w/FS::cust_main_county
451 my ( $selected_county, $selected_state, $selected_country,
452 $prefix, $onchange ) = @_;
454 my $prefix = '' unless defined $prefix;
458 my %cust_main_county;
460 # unless ( @cust_main_county ) { #cache
461 #@cust_main_county = qsearch('cust_main_county', {} );
462 #foreach my $c ( @cust_main_county ) {
463 foreach my $c ( @$locales ) {
464 #$countyflag=1 if $c->county;
465 $countyflag=1 if $c->{county};
466 #push @{$cust_main_county{$c->country}{$c->state}}, $c->county;
467 #$cust_main_county{$c->country}{$c->state}{$c->county} = 1;
468 $cust_main_county{$c->{country}}{$c->{state}}{$c->{county}} = 1;
471 $countyflag=1 if $selected_county;
473 my $script_html = <<END;
475 function opt(what,value,text) {
476 var optionName = new Option(text, value, false, false);
477 var length = what.length;
478 what.options[length] = optionName;
480 function ${prefix}country_changed(what) {
481 country = what.options[what.selectedIndex].text;
482 for ( var i = what.form.${prefix}state.length; i >= 0; i-- )
483 what.form.${prefix}state.options[i] = null;
485 #what.form.${prefix}state.options[0] = new Option('', '', false, true);
487 foreach my $country ( sort keys %cust_main_county ) {
488 $script_html .= "\nif ( country == \"$country\" ) {\n";
489 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
490 my $text = $state || '(n/a)';
491 $script_html .= qq!opt(what.form.${prefix}state, "$state", "$text");\n!;
493 $script_html .= "}\n";
496 $script_html .= <<END;
498 function ${prefix}state_changed(what) {
502 $script_html .= <<END;
503 state = what.options[what.selectedIndex].text;
504 country = what.form.${prefix}country.options[what.form.${prefix}country.selectedIndex].text;
505 for ( var i = what.form.${prefix}county.length; i >= 0; i-- )
506 what.form.${prefix}county.options[i] = null;
509 foreach my $country ( sort keys %cust_main_county ) {
510 $script_html .= "\nif ( country == \"$country\" ) {\n";
511 foreach my $state ( sort keys %{$cust_main_county{$country}} ) {
512 $script_html .= "\nif ( state == \"$state\" ) {\n";
513 #foreach my $county ( sort @{$cust_main_county{$country}{$state}} ) {
514 foreach my $county ( sort keys %{$cust_main_county{$country}{$state}} ) {
515 my $text = $county || '(n/a)';
517 qq!opt(what.form.${prefix}county, "$county", "$text");\n!;
519 $script_html .= "}\n";
521 $script_html .= "}\n";
525 $script_html .= <<END;
530 my $county_html = $script_html;
532 $county_html .= qq!<SELECT NAME="${prefix}county" onChange="$onchange">!;
533 $county_html .= '</SELECT>';
536 qq!<INPUT TYPE="hidden" NAME="${prefix}county" VALUE="$selected_county">!;
539 my $state_html = qq!<SELECT NAME="${prefix}state" !.
540 qq!onChange="${prefix}state_changed(this); $onchange">!;
541 foreach my $state ( sort keys %{ $cust_main_county{$selected_country} } ) {
542 my $text = $state || '(n/a)';
543 my $selected = $state eq $selected_state ? 'SELECTED' : '';
544 $state_html .= "\n<OPTION $selected VALUE=$state>$text</OPTION>"
546 $state_html .= '</SELECT>';
548 $state_html .= '</SELECT>';
550 my $country_html = qq!<SELECT NAME="${prefix}country" !.
551 qq!onChange="${prefix}country_changed(this); $onchange">!;
552 my $countrydefault = $init_data->{countrydefault} || 'US';
553 foreach my $country (
554 sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
555 keys %cust_main_county
557 my $selected = $country eq $selected_country ? ' SELECTED' : '';
558 $country_html .= "\n<OPTION$selected>$country</OPTION>"
560 $country_html .= '</SELECT>';
562 ($county_html, $state_html, $country_html);
566 sub success_default { #html to use if you don't specify a success file
568 <HTML><HEAD><TITLE>Signup successful</TITLE></HEAD>
569 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Signup successful</FONT><BR><BR>
570 Thanks for signing up!
572 Signup information for <%= $email_name %>:
574 Username: <%= $username %><BR>
575 Password: <%= $password %><BR>
576 Access number: (<%= $ac %>) / $exch - $local<BR>
577 Package: <%= $pkg %><BR>
582 sub decline_default { #html to use if there is a decline
584 <HTML><HEAD><TITLE>Processing error</TITLE></HEAD>
585 <BODY BGCOLOR="#e8e8e8"><FONT SIZE=7>Processing error</FONT><BR><BR>
586 There has been an error processing your account. Please contact customer