#!/usr/bin/perl
package RVL::LicenseCycle;
##LICENSE##
use strict;
use warnings;
use Cwd qw'realpath cwd';
use File::Basename;
use File::Copy;
use IPC::Open3;
use RVL::Serialize ();
use MIME::Base64;
use Socket;
no strict 'subs';
use Class::Std::Utils;
{
sub new {
my ($class, $opt) = @_;
my $self = {};
$self->{timeNow} = time();
$self->{progName} = lc(RVL::Config::get('site.rvproduct'));
$self->{lcConfigFile} = __CONSTANT__::RVL_VAR_DIR.'/rvlicense.ini';
$self->{lcFile} = __CONSTANT__::RVL_VAR_DIR.'/license/'.$self->{progName}.'.lic';
$self->{lcConfig} = {};
bless $self, $class;
return $self;
}
sub checkFileConfigLicenseCycle {
my $self = shift;
my ($force) = @_;
return;
RVL::logMessage('', __CONSTANT__::RVL_LOG_DEBUG);
RVL::logMessage("Product Name: " . $self->{progName}, __CONSTANT__::RVL_LOG_DEBUG);
return if (!RVL::CpHandle::singleton->isRoot());
#Only ROOT can save file
my $checkTime = 0;
if (-e $self->{lcConfigFile}) {
$self->{lcConfig} = RVL::File::parse_ini_file($self->{lcConfigFile});
if (!defined($self->{lcConfig}->{'expire'})) {
$self->{lcConfig}->{'expire'} = 0;
}
if (defined($self->{lcConfig}->{'checktime'}) && int($self->{lcConfig}->{'checktime'}) > 0) {
$checkTime = $self->{lcConfig}->{'checktime'};
}
}
my $ctime = $checkTime - $self->{timeNow};
my $t24h = 86400; # 24 hours, 60*60*24
if ($checkTime <= $self->{timeNow} || !-e $self->{lcFile} || $force) {
RVL::logMessage('Cycle license now!', __CONSTANT__::RVL_LOG_DEBUG);
#getlicense /validate
my $data = RVL::LicenseCycle::checkLicenseInfo($self->{progName});
my $expireOn = 0;
if (!defined($self->{progName})) {
$expireOn = ($self->{lcConfig}->{'expire'} - $self->{timeNow}) / $t24h;
} elsif (defined($data->{'on error'}) && $data->{'on error'}) {
RVL::logMessage($data->{'error msg'}, __CONSTANT__::RVL_LOG_DEBUG);
undef $expireOn;
if (-e $self->{lcFile}) {
unlink($self->{lcFile});
}
} else {
$data = (defined($data->{$self->{progName}})) ? $data->{$self->{progName}} : {};
$self->{lcConfig}->{'expire'} = (defined($data->{'expire-date'})) ? $data->{'expire-date'} : 0;
$self->validateLicense($data, $force);
$expireOn = ($self->{lcConfig}->{'expire'} - $self->{timeNow}) / $t24h;
}
if (defined($expireOn) && $expireOn > 1) {
$self->{lcConfig}->{'checktime'} = $self->{timeNow} + $t24h;
} elsif (defined($expireOn) && $expireOn == 0) {
my $expSet = $data->{'expire-date'} - $self->{timeNow};
$self->{lcConfig}->{'checktime'} = int(($self->{timeNow} + ($expSet / 2)));
} else {
$self->{lcConfig}->{'checktime'} = $self->{timeNow};
}
if (!-e $self->{lcFile}) {
RVL::logMessage('Cannot locate license file.', __CONSTANT__::RVL_LOG_DEBUG);
}
$self->saveConfig();
}
}
sub checkLicenseInfo {
my $progName = shift;
my $url = 'license.rvglobalsoft.com';
my $rvcode = RVL::LicenseCycle::getrvcode($progName);
my $uri = '/licensemgr/license/getlicense/?rvcode='.$rvcode;
my $site = 'http://'.$url.$uri;
my $content = RVL::Net::getContentByUrl($site);
my $data = RVL::LicenseCycle::rvcodedecode(URI::Escape::uri_unescape($content));
return $data;
}
sub validateLicense {
my $self = shift;
my $data = shift;
my $force = shift;
$data->{'expire-date'} = (defined($data->{'expire-date'})) ? $data->{'expire-date'} : 0;
if ($data->{'expire-date'} <= $self->{timeNow}) {
if (-e $self->{lcFile}) {
unlink($self->{lcFile});
}
#date
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=(localtime($data->{'expire-date'}));
my $expiredate = sprintf("%02d/%02d/%04d", $mday, ($mon+1), ($year+1900));
RVL::logMessage(sprintf('Sorry, your license has been expired on %s.' . $expiredate), __CONSTANT__::RVL_LOG_DEBUG);
} else {
my $expireConf = (defined($self->{lcConfig}->{'expire'})) ? $self->{lcConfig}->{'expire'} : 0;
if ($expireConf ne $data->{'expire-date'} || !-e $self->{lcFile} || $force) {
RVL::logMessage("Not have file license", __CONSTANT__::RVL_LOG_DEBUG);
RVL::logMessage("force = 1", __CONSTANT__::RVL_LOG_DEBUG);
$self->getLicenseFile($data);
}
}
}
sub getLicenseFile {
my $self = shift;
my $data = shift;
my $host = 'license.rvglobalsoft.com';
my $rvcode = RVL::LicenseCycle::getrvcode($self->{progName});
my $uri = '/licensemgr/license/genlicense/?rvcode='.$rvcode;
my $saveAs = __CONSTANT__::RVL_VAR_DIR.'/tmp/'.$self->{progName}.'-lic.tar';
my $TARPATH = __CONSTANT__::RVL_VAR_DIR.'/tmp';
my $isSaveFile = 0;
my $site = 'http://'.$host.$uri;
my $content = RVL::Net::getContentByUrl($site);
my $filePointer;
if (defined($saveAs) && $saveAs ne '') {
if (!-e $TARPATH) {
mkdir($TARPATH, 0755);
}
if (-e $saveAs) {
unlink($saveAs);
}
if (open($filePointer, '>', $saveAs)) {
$isSaveFile = 1;
}
}
if ($isSaveFile) {
print $filePointer $content;
close($filePointer);
}
if (-s $saveAs) {
my $oldPath = cwd();
my $tarPath = dirname($saveAs);
chdir($tarPath);
#extract file
my $pid3 = open3(*CMD_IN, *CMD_OUT, *CMD_ERR, 'tar -xopf ' . $saveAs);
waitpid($pid3, 0);
close(CMD_IN);
my @errs = <CMD_ERR>;
close(CMD_ERR);
close(CMD_OUT);
my $licFile = '';
my $installFilePath = $self->{lcFile};
$licFile = $tarPath.'/'.$self->{progName}.'.lic';
if (-e $licFile) {
if (!-e dirname($installFilePath)) {
mkdir(dirname($installFilePath), 0755);
}
#copy file to program path
if (copy($licFile, $installFilePath)) {
unlink($licFile);
RVL::logMessage('Update license key has completed.', __CONSTANT__::RVL_LOG_DEBUG);
}
$self->{lcConfig}->{'expire'} = $data->{'expire-date'};
}
chdir($oldPath);
unlink($saveAs);
}
}
sub saveConfig {
my $self = shift;
RVL::logMessage('License cycle Save IP configuration. '.$self->{lcConfigFile}, __CONSTANT__::RVL_LOG_DEBUG);
my $saveData = {};
$saveData->{'expire'} = (defined($self->{lcConfig}->{'expire'}))
? $self->{lcConfig}->{'expire'}
: '';
$saveData->{'checktime'} = $self->{lcConfig}->{'checktime'};
RVL::File::write_ini_file($self->{lcConfigFile}, $saveData);
if (-e $self->{lcConfigFile} && -w $self->{lcConfigFile}) {
chmod(0777, $self->{lcConfigFile});
}
RVL::logMessage('License cycle Save IP configuration has been completed.', __CONSTANT__::RVL_LOG_DEBUG);
}
sub getrvcode {
my $progName = shift;
# primary-ip
# program
# all-ip
my %rvcode = ();
my $allIp = join(',', RVL::System::getIPList(1));
$rvcode{'timestamp'} = time();
$rvcode{'primary-ip'} = RVL::System::getIPList();
$rvcode{'program'} = $progName;
$rvcode{'all-ip'} = $allIp;
$rvcode{'cp'} = lc(RVL::CpHandle::singleton->getCpMode());
$rvcode{'vps'} = '';#RVL::LicenseCycle::isVps();
$rvcode{'license-code'} = RVL::LicenseCycle::getAdapterData();
$rvcode{'rand'} = int(rand(9000)) + 1000; #range: 1000-9999
#serialize data
return URI::Escape::uri_escape(RVL::LicenseCycle::rvcodeencode(\%rvcode));
}
sub getAdapterData {
#ioncube
my $page = '';
my $inifile = __CONSTANT__::RVL_PATH.'/modules/scripts/php.ini';
my $phpfile = __CONSTANT__::RVL_PATH.'/modules/scripts/ioncube.php';
if (-z $inifile && -z $phpfile) {
my $cmdIoncube = "php -c $inifile $phpfile 2>/dev/null";
$page = RVManager::callBackticks($cmdIoncube);
}
return $page;
}
sub isVps {
return '' unless $^O;
# use Sys::Detect::Virtualization;
#
# my $detector = eval { Sys::Detect::Virtualization->new({verbose => 0}) };
# return '' if( $@ );
#
# my @found = $detector->detect();
# if( @found ) {
# return 1;
# } else {
# return 0;
# }
}
sub rvcodeencode {
return MIME::Base64::encode(RVL::Serialize::serialize($_[0]));
}
sub rvcodedecode{
return RVL::Serialize::unserialize(MIME::Base64::decode($_[0]));
}
}
1;
Copyright 2K16 - 2K18 Indonesian Hacker Rulez