|
Result of submision of registration form
#!/usr/bin/perl -w
use CGI::Carp qw(fatalsToBrowser);
# sorin : do is deprecated si nu raise error !!! alternative "use" si "require" - require afiseaza eroarea ceea ce e OK. use nu o face!
require "const.inc";
require "func.inc";
use DBI;
use CGI;
use MIME::Lite;
$CONFIG{default_support}="1 YEAR";
$CONFIG{default_support_months}=12;
%MESSAGES=
(
bad_sn => "The serial number is not valid",
bad_contact => "Contact name field is missing",
bad_org => "Organization name field is missing",
bad_email => "Email field is not completed",
bad_country => "Country field is not completed",
bad_compid => "Computer id field is not completed",
dif_compid => "The previously entered Computer ID for this license is different.
You may go back and check the information you entered or ask for a reactivation.",
key_displ => "Your key is ",
bad_keygen => "Error contacting the registration server. Try again later. If the error persist contact us.",
);
$cgi=new CGI;
$sn=''; # remove a warning
$on_pc=$cgi->param('on_pc');
$on_pc_pref=$on_pc?"pj_":"";
if ($on_pc)
{
$cgi->param('step','final');
}
#print STDERR "$CONST{db_name},$CONST{db_user},$CONST{db_pass}";
$dbh=DBI->connect($CONST{db_name},$CONST{db_user},$CONST{db_pass}) or die "Could not connect to the database [$CONST{db_name}].";
#$dbh->trace(1,"/tmp/curu.log");
sub ApplyUpgrade
{
my ($linii,$module,$reseller_id,$sns,$final)=@_;
my ($order_info,$prod_info,$err,$this_sn,$extra_months);
$extra_months=0;
while ($sns=~/(\d{4}\-\d{4}\-\d{4})/g)
{
$this_sn=jumi;
$order_info=$dbh->selectrow_hashref("select * from orders where sn='$this_sn' and product_code LIKE 'PJ-%'");
$prod_info=$dbh->selectrow_hashref("select * from products where code=?",undef,$order_info->{product_code});
if ($order_info->{used} || !$order_info)
{
$err.="The upgrade license $this_sn is not available";
# } elsif ($reseller_id && ($order_info->{reseller_id}!=$reseller_id)) {
# $err.="You cannot use the upgrade license jumi (Error Code: 02-$order_info->{reseller_id}-$reseller_id)";
} else {
($prod_info->{linii},$prod_info->{module})=split(/\|/,$prod_info->{prod_code});
$linii+=$prod_info->{linii};
$module|=$prod_info->{module} if $prod_info->{module}!=1024;
$extra_months=12 if $prod_info->{module}==1024;
$dbh->do("update orders set used=? where sn=?",undef,$cgi->param('sn'),$this_sn) if $final;
}
}
return ($linii,$module,$extra_months,$err);
}
sub CheckLicenseFields
{
my $missing='';
# un serial valid are 12 digits dupa ce se stripeaza orice altceva.
$missing.="$MESSAGES{bad_sn}" unless $HTML{missing} || $cgi->param('sn')=~/^[\d\w-]{14}$/;
# un contact tb sa aiba minim 4 caractere
$missing.="$MESSAGES{bad_contact}" unless (defined $cgi->param('contact')) && $cgi->param('contact')=~/[\d\w\s]{4}/;
# firma >4 caractere
$missing.="$MESSAGES{bad_org}" unless (defined $cgi->param('organization')) && $cgi->param('organization')=~/[\d\w\s]{4}/;
# verificare email sa "para" valid
$missing.="$MESSAGES{bad_email}" unless (defined $cgi->param('email')) && $cgi->param('email')=~/^[\w\-\~\.]+\@[\w\-\~\.]+\.\w{2,4}$/;
# $missing.="$MESSAGES{bad_country}" unless $cgi->param('country')=~/[\d\w\s]{4}/;
# un computer id tb sa fie numeric si sa aiba minim 6 cifre
$missing.="$MESSAGES{bad_compid}" unless (defined $cgi->param('computerid')) && $cgi->param('computerid')=~/[\d\w\s]{6}/;
return $missing;
}
# TODO: intoarce true dc serialul este disponibil sau false
sub CheckSnAvailability
{
my $missing='';
my $sn = $cgi->param('sn');
$sn = '' if (!defined $sn);
$missing.="$MESSAGES{bad_sn}" unless $dbh->selectrow_array("select * from licenses where sn='$sn'") || $dbh->selectrow_array("select * from orders inner join pbxtools.products on orders.product_code=pbxtools.products.code where orders.sn='$sn' and orders.product_code LIKE 'PJ-%' and pbxtools.products.can_activate=1");
return $missing;
}
# TODO: intoarce true dc serialul este deja activat
sub LicenseIsRegistered($)
{
my ($sn)=@_;
my $tmp = $dbh->selectrow_array("select count(*) from activations where sn='$sn' and enabled=1");
# die "$tmp $sn";
return $tmp;
}
# TODO: perform new activation (primeste ca parametrii datele licentei)
sub RegisterLicense
{
my $activation = '';
my ($linii,$module,$extra_months) = (0,0,0);
($bogus,$parent_sn,$reseller_id,$datestart,$dateend) = $dbh->selectrow_array("select bogus,parent_sn,reseller_id,unix_timestamp(date_register),unix_timestamp(date_expire) from licenses where sn='".$cgi->param('sn')."'");
if ($parent_sn)
{
($bogus,$reseller_id) = $dbh->selectrow_array("select bogus,reseller_id from licenses where sn='$parent_sn'");
}
if (!$reseller_id)
{
($reseller_id) = $dbh->selectrow_array("SELECT reseller_id FROM orders WHERE sn='".$cgi->param('sn')."' and product_code LIKE 'PJ-%'");
}
if ($bogus)
{
($linii,$module)=split(/\|/,$bogus);
} else {
($linii,$module)=ApplyUpgrade(0,0,$reseller_id,$cgi->param('sn'),$cgi->param('step'));
}
($linii,$module,$extra_months,$HTML{upgrade_error})=&ApplyUpgrade($linii,$module,$reseller_id,$cgi->param('upg_sn'),$cgi->param('step'));
$module=0 if(!defined $module);
if ($cgi->param('step') eq 'final')
{
if (!$datestart)
{
$datestart = time() - 86400;
$dateend = $datestart + ($CONFIG{default_support_months} + $extra_months) * 31 * 86400;
}
$dbh->do("insert into licenses set date_register=NOW(), bogus='$linii|$module', date_expire=(NOW()+INTERVAL $CONFIG{default_support}) + INTERVAL $extra_months MONTH, reseller_id='$reseller_id', contact='".&SQLSafe($cgi->param('contact'))."', organization='".&SQLSafe($cgi->param('organization'))."', email='".&SQLSafe($cgi->param('email'))."', country='".&SQLSafe($cgi->param('country'))."', comments='".&SQLSafe($cgi->param('comments'))."', sn='".&SQLSafe($cgi->param('sn'))."'")
unless $dbh->selectrow_array("select * from licenses where sn='".SQLSafe($cgi->param('sn'))."'");
$activation=GenerateActivation($cgi->param('sn'),$cgi->param('computerid'),$cgi->param('organization'),$linii,$module,$datestart,$dateend);
} else {
$HTML{license_detail}=InterpretLicenseDetail("$linii\|$module");
return "Check";
}
if($activation !~ /Error|Check/)
{
# update expiration only if extra_month
if ($extra_months)
{
$dbh->do("update licenses set bogus='$linii|$module', date_expire=if(date_expire>now(),date_expire+interval $extra_months month,now()+interval $extra_months month) where sn='".&SQLSafe($cgi->param('sn'))."'");
} else
{
$dbh->do("update licenses set bogus='$linii|$module' where sn='".SQLSafe($cgi->param('sn'))."'");
}
# $dbh->do("update licenses set date_register=if(date_register is not null or date_register='0',date_register,NOW()), date_expire=if(date_expire is not null or date_expire='0',date_expire,NOW()+INTERVAL $CONFIG{default_support}), contact='".&SQLSafe($cgi->param('contact'))."', organization='".&SQLSafe($cgi->param('organization'))."', email='".&SQLSafe($cgi->param('email'))."', country='".&SQLSafe($cgi->param('country'))."', comments='".&SQLSafe($cgi->param('comments'))."' where sn='".&SQLSafe($cgi->param('sn'))."'");
$dbh->do("update activations set enabled=0 where sn='".SQLSafe($cgi->param('sn'))."'");
$dbh->do("insert into activations (sn,computerid,activation_key,enabled) values ('".SQLSafe($cgi->param('sn'))."','".SQLSafe($cgi->param('computerid'))."','$activation',1)");
$HTML{activation_key}=$activation;
return $activation;
}
else
{
return "Error";
}
}
sub SendConfirmationEmail
{
}
sub SendNotificationEmail
{
# my $reg=qq|REGEDIT4\r\n\r\n[HKEY_LOCAL_MACHINE\SOFTWARE\PbxTools\PhoneJournal\general]\r\n"Activation"="$HTML{activation_key}"\r\n|;
my ($contact,$organization,$sn)=($cgi->param('contact'),$cgi->param('organization'),$cgi->param('sn'));
my $body=<<"--END-MESSAGE";
Dear $contact,
Thank you for choosing our call accounting solution - PbxTools PhoneJournal. This is a confirmation of your registration placed today from IP $ENV{REMOTE_ADDR}.
The activation key is attached to this message. Opening it will store your activation key into your computer. This activation key will work only on the computer you made registration for.
Thank you for your purchase,
PBXTools.com
INTERSOL SRL
--END-MESSAGE
my $fname=$organization;
$fname=~s/\W+/\_/g;
my $msg_client = MIME::Lite->new(From=>'
This e-mail address is being protected from spam bots, you need JavaScript enabled to view it
',To=>$cgi->param('email'),Subject =>"$ENV{SERVER_NAME} registration",Type=>'multipart/related');
$msg_client->attach(Type=>'text/plain',Data=>$body);
$msg_client->attach(Type=>'text/plain',Filename=>"$fname.pjkey",Disposition=>'attachment',Data=>$HTML{activation_key});
$msg_client->send('smtp','localhost', Timeout=>60);
}
$HTML{missing}=CheckSnAvailability();
$HTML{missing}.=CheckLicenseFields();
if ($HTML{missing})
{ # the serial number is not valid
PrintPage("custom/activation_templates/${on_pc_pref}license_register_error.html");
} elsif ($HTML{count}=LicenseIsRegistered($cgi->param('sn')))
{ # already activated
$extra_months=0;
if ($cgi->param('upg_sn'))
{
($bogus,$parent_sn,$reseller_id,$datestart,$dateend) = $dbh->selectrow_array("select bogus,parent_sn,reseller_id,unix_timestamp(date_register),unix_timestamp(date_expire) from licenses where sn='".$cgi->param('sn')."'");
warn "select bogus,parent_sn,reseller_id from licenses where sn='".$cgi->param('sn')."'";
if ($parent_sn)
{
($bogus,$reseller_id) = $dbh->selectrow_array("select bogus,reseller_id from licenses where sn='$parent_sn'");
}
my ($linii,$module)=split(/\|/,$bogus);
($linii,$module,$extra_months,$HTML{upgrade_error})=ApplyUpgrade($linii,$module,$reseller_id,$cgi->param('upg_sn'),$cgi->param('step'));
$HTML{hidden}="";
foreach ($cgi->param())
{
$HTML{hidden}.=$cgi->hidden($_,$cgi->param($_));
$HTML{"form_content_$_"}=$cgi->param($_);
}
$HTML{hidden}.=$cgi->hidden('step','final');
if ($cgi->param('step') eq 'final')
{
$dateend = ($dateend > time()) ? ($dateend + $extra_months * 31 * 86400) : (time() + $extra_months * 31 * 86400);
$activation=&GenerateActivation($cgi->param('sn'),$cgi->param('computerid'),$cgi->param('organization'),$linii,$module,$datestart,$dateend);
if ($activation ne 'Error')
{
$dbh->do("update activations set enabled=0 where sn='".SQLSafe($cgi->param('sn'))."'");
$dbh->do("insert into activations (sn,computerid,activation_key,enabled) values ('".SQLSafe($cgi->param('sn'))."','".SQLSafe($cgi->param('computerid'))."','$activation',1)");
$dbh->do("update licenses set max_active=max_active+1, bogus='$linii|$module', date_expire=if(date_expire>now(),date_expire+interval $extra_months month,now()+interval $extra_months month) where sn='".$cgi->param('sn')."'");
$HTML{activation_key}=$activation;
PrintPage("custom/activation_templates/${on_pc_pref}license_register_ok.html");
} else {
$HTML{missing} = "$MESSAGES{bad_keygen}";
PrintPage("custom/activation_templates/${on_pc_pref}license_register_error.html");
}
} else {
$HTML{license_detail}=InterpretLicenseDetail("$linii\|$module");
PrintPage("custom/activation_templates/${on_pc_pref}license_register_confirm.html");
}
} else {
# my $activation=$dbh->selectrow_hashref("select * from activations where sn='".$cgi->param('sn')."' and enabled=1 order by id desc limit 1");
# if(!$activation or $activation->{count} == 0) {die "found no activations [$activation->{count}]"};
my $data=$dbh->selectrow_hashref("select *, date_format(date_expire,'%d/%m/%Y') as date_expire, date_format(date_register,'%d/%m/%Y') as date_register, if(date_expire>now(),0,1) as expired from licenses where sn='".$cgi->param('sn')."'");
# die ($data);
$HTML{exp_date}=$data->{date_expire};
$HTML{reg_date}=$data->{date_register};
$activation->{count}=$dbh->selectrow_array("select count(*) from activations where sn='$sn'");
$activation->{rest}=$data->{max_active}-$activation->{count};
$HTML{insert}=$data->{expired}?
($activation->{computerid} eq $cgi->param('computerid')?
ParsePage("custom/activation_templates/${on_pc_pref}license_expired1.html",$activation):
ParsePage("custom/activation_templates/${on_pc_pref}license_expired2.html")):
($activation->{computerid} eq $cgi->param('computerid')?
ParsePage("custom/activation_templates/${on_pc_pref}license_valid1.html",$activation):
ParsePage("custom/activation_templates/${on_pc_pref}license_valid2.html"));
PrintPage("custom/activation_templates/${on_pc_pref}license_already_registered.html");
}
}
else
{ # new activation
my $status = RegisterLicense();
if ($status eq "Error")
{
$HTML{missing} = "$MESSAGES{bad_keygen}";
PrintPage("custom/activation_templates/${on_pc_pref}license_register_error.html");
}
elsif ($status eq "Check")
{
$HTML{hidden}="";
foreach ($cgi->param())
{
$HTML{hidden}.=$cgi->hidden($_,$cgi->param($_));
$HTML{"form_content_$_"}=$cgi->param($_);
}
$HTML{hidden}.=$cgi->hidden('step','final');
PrintPage("custom/activation_templates/${on_pc_pref}license_register_confirm.html");
}
else
{
# &SendConfirmationEmail();
$HTML{activation_key}=$status;
SendNotificationEmail();
PrintPage("custom/activation_templates/${on_pc_pref}license_register_ok.html");
}
}
$dbh->disconnect();
KeepThis=true&TB_iframe=true&height=400&width=600
|