CHips L MINI SHELL

CHips L pro

Current Path : /proc/2/root/usr/local/rvglobalsoft/rvglobalsoftmanager/modules/
Upload File :
Current File : //proc/2/root/usr/local/rvglobalsoft/rvglobalsoftmanager/modules/RvglobalsoftManager.pm

#!/usr/local/cpanel/3rdparty/bin/perl
package RvglobalsoftManager;
##LICENSE##

use strict;
use Carp;
use warnings;
use Cwd;
use Cwd 'realpath';
use File::Basename qw(&basename &dirname);
#use CGI ':standard';
use CGI;
use Data::Dumper;
use File::Basename;
use Socket;
use JSON;

use lib dirname(realpath(__FILE__));
use lib dirname(realpath(__FILE__)).'/lib';
use lib dirname(realpath(__FILE__)).'/modules';

use Template;
use vars qw ( @ISA @EXPORT_OK @EXPORT $INSTANCE %CONSTANTS %GLOBALS $PARAM $CONF);
use RvglobalsoftManager::Config();
use RvglobalsoftManager::Rvlibary();
use RvglobalsoftManager::ServerInfo();
use CPHandle;

use Cpanel::Version;
use Cpanel::Version::Compare;

require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(
  $OUTPUT $ERROR $CONF $VERSIONCACHE
);
@EXPORT = qw( mainProcess setRequestType);

sub mainProcess {
	buildHtmlPageHeader();
	loadConfig();
	$CONF->{CPHandle} = new CPHandle();
	getParam();
	validateConfig();
	getServerInfo();
	buildData();
	buildBody();
	buildFooter();
	buildOutput();
	rvDumper($CONF) if($CONF->{debug});
	rvDumper(\%ENV) if($CONF->{debug});
	
}

sub mainAjaxProcess {
	buildAjaxPageHeader();
	loadConfig();
	$CONF->{CPHandle} = new CPHandle();
	getParam();
	validateConfig();
	validateAjax();
	buildDataAjax();
	buildAlaxOutput();
	rvDumper($CONF) if($CONF->{debug});
}

sub loadConfig {
	RvglobalsoftManager::logmessage(__PACKAGE__,__LINE__);
	$CONF = RvglobalsoftManager::Config::getConfig();
	if($CONF->{debug} && getRequestType() eq 'Browser'){
		print "<br>===============loadConfig===============<br>";
		rvDumper($CONF) ;
		print "<br>===============loadConfig END===============<br>";
	}
}

sub validateConfig {
	
	#Clean showRootOnly For reseller
	if($CONF->{CPHandle}->isReseller()){
		foreach my $eachProduct (keys %{$CONF->{product}}){
			if(defined $CONF->{product}->{$eachProduct}->{showRootOnly} && $CONF->{product}->{$eachProduct}->{showRootOnly} == 1){
				delete $CONF->{product}->{$eachProduct};
			}
		}
	}
	
	my $baseUrl = $CONF->{baseUrl} = $CONF->{CPHandle}->getBaseUrl();
	
	#For Product
	foreach my $eachProduct (keys %{$CONF->{product}}){
		#Make manager url
		if(defined $CONF->{product}->{$eachProduct}->{mgrUrl} && $CONF->{product}->{$eachProduct}->{mgrUrl} ne ''
			&& ($CONF->{product}->{$eachProduct}->{mgrUrl} !~ /^https:/ || $CONF->{product}->{$eachProduct}->{mgrUrl} !~ /^http:/)
		){
			$CONF->{product}->{$eachProduct}->{mgrUrl} = $baseUrl . $CONF->{product}->{$eachProduct}->{mgrUrl}
		}
		
		#Make update url
		if(defined $CONF->{product}->{$eachProduct}->{updateUrl} && $CONF->{product}->{$eachProduct}->{updateUrl} ne '' ){
			if ($CONF->{product}->{$eachProduct}->{updateUrl} !~ /^https/ || $CONF->{product}->{$eachProduct}->{updateUrl} !~ /^http/) {
				$CONF->{product}->{$eachProduct}->{updateUrl} = $baseUrl . $CONF->{product}->{$eachProduct}->{updateUrl}
			} else {
				$CONF->{product}->{$eachProduct}->{updateUrl} = $CONF->{product}->{$eachProduct}->{updateUrl}
			}
		}
	}
	
	#Make Url For Api
	if(defined $CONF->{apimanager}->{mgrUrl} && $CONF->{apimanager}->{mgrUrl} ne ''
		&& ($CONF->{apimanager}->{mgrUrl} !~ /^https:/ || $CONF->{apimanager}{mgrUrl} !~ /^http:/)
	){
		$CONF->{apimanager}->{mgrUrl} = $baseUrl . $CONF->{apimanager}->{mgrUrl}
	}
	
	#For Reseller Platform
	foreach my $eachProductPF (keys %{$CONF->{resellerpf}}){
		#Make manager url
		if(defined $CONF->{resellerpf}->{$eachProductPF}->{mgrUrl} && $CONF->{resellerpf}->{$eachProductPF}->{mgrUrl} ne ''
			&& ($CONF->{resellerpf}->{$eachProductPF}->{mgrUrl} !~ /^https:/ || $CONF->{resellerpf}->{$eachProductPF}->{mgrUrl} !~ /^http:/)
		){
			$CONF->{resellerpf}->{$eachProductPF}->{mgrUrl} = $baseUrl . $CONF->{resellerpf}->{$eachProductPF}->{mgrUrl}
		}
	}
}

sub getServerInfo {
	$CONF->{CPHandle} = new CPHandle() if(!defined $CONF->{CPHandle});
	$CONF->{ServerInfo} = {
		'Os' => RvglobalsoftManager::ServerInfo::getOS(),
		'ControlPanelName' => RvglobalsoftManager::ServerInfo::getControlPanelName(),
		'IpAddress' => $CONF->{CPHandle}->getIPList(),
		'perlVersion' => $CONF->{CPHandle}->getPerlVersion(),
		'phpVersion' => $CONF->{CPHandle}->getPhpVersion(),
		'ControlPanelVersion' => $CONF->{CPHandle}->getPanelVersion(),
		'serverType' => RvglobalsoftManager::ServerInfo::getServerType(),
		'versionNo' => RvglobalsoftManager::ServerInfo::getVersionNo(),
	};
}

sub getAccountInfo {
	$CONF->{accountInfo} = {
		'username' => '',
		'level' => '',
	};
}

sub validateAjax {
	my $action = (defined $PARAM->{'action'} && $PARAM->{'action'} ne '')?'ajax_' . $PARAM->{'action'}:'ajax_view';
	my ($resAjax);
	if(function_exists($action)){
		print "eval(" . $action . ");\n" if($CONF->{debug});
		eval("$action()");
		print dirname(realpath(__FILE__)) . '/template/' .$PARAM->{'action'} . 'Template.tmpl' . "\n" if($CONF->{debug});
		$CONF->{'template'} = $PARAM->{'action'} . 'Template.tmpl' if(-f dirname(realpath(__FILE__)) . '/template/' .$PARAM->{'action'} . 'Template.tmpl'); 
	}else{
		ajax_view();
	}
}

sub ajax_view {
	print "=====>Call ajax_view();\n" if($CONF->{debug});
}

sub ajax_getproductlist {
	print "=====>Call ajax_getproductlist();\n" if($CONF->{debug});
	my $contentAjax = '';
	my $productRefAjax = {};
	my $json = JSON->new->allow_nonref;
	
	$contentAjax = RvglobalsoftManager::Rvlibary::getDataFromURL('download.rvglobalsoft.com','prodouctlist.php');
	$productRefAjax = $json->decode($contentAjax);

	if($CONF->{debug}){
		print "getContentUrl('https://download.rvglobalsoft.com/prodouctlist.php')\n";
	 	rvDumper($contentAjax);
	 	print "=====ajax_getproductlist \$productRef========\n";
	 	rvDumper($productRefAjax);
	 }
	
	foreach my $eachProductAjax (keys %{$productRefAjax}){
		if(!defined $CONF->{'product'}->{$eachProductAjax}){
			$CONF->{'productAjax'}->{$eachProductAjax} = $productRefAjax->{$eachProductAjax};
		}
	}
}

sub ajax_getdoclist {
	print "=====>Call ajax_getdoclist();\n" if($CONF->{debug});
	my $content = '';
	my $productRef = {};
	my $json = JSON->new->allow_nonref;
	
	$content = RvglobalsoftManager::Rvlibary::getDataFromURL('download.rvglobalsoft.com','prodouctlist.php');
	$productRef = $json->decode($content);
	if($CONF->{debug}){
	 	print "=====ajax_getdoclist \$productRef========\n";
	 	rvDumper($productRef);
	 }
	
	foreach my $eachProductAjax (keys %{$productRef}){
		#if(!defined $CONF->{'product'}->{$eachProductAjax}){
			$CONF->{'productAjax'}->{$eachProductAjax} = $productRef->{$eachProductAjax};
		#}
	}
}

sub ajax_getLatestVersion {
	#print "=====>Call ajax_getLatestVersion();\n" if($CONF->{debug});	
	print "=====>Call ajax_getLatestVersion('product=" . $PARAM->{product}."','appname=". $PARAM->{appname}."');<br>\n" if($CONF->{debug});
	return '-' if(!defined $PARAM->{appname} || $PARAM->{appname} eq '');
	my $appName = $PARAM->{appname};
	my $productName = $PARAM->{product};
	my $latestVersion = '-';
	my $eachConfig = {};
	my $eachProduct = {};
		    
	if(validateMySoftware($PARAM->{product})){
		$CONF->{mySoftwareAjax}->{$productName} = $CONF->{product}->{$productName};
		#Get Latest Version
		if(defined $CONF->{product}->{$productName}->{latestVersion} && $CONF->{product}->{$productName}->{latestVersion} ne ''){
			my $fileProduct = dirname(realpath(__FILE__)) .'/RvglobalsoftManager/ProductConfig/'. $appName . '.pm';
		    #print "\$fileProduct=$fileProduct<br>";
		    if (-f $fileProduct && defined $CONF->{product}->{$productName}->{latestVersion} && $CONF->{product}->{$productName}->{latestVersion} ne '') {
		    	my $code = 'require \'' .$fileProduct . '\'; ';
		    	$code .= '$latestVersion =' . $CONF->{product}->{$productName}->{latestVersion};
		    	$latestVersion = eval $code;
		    	#print "\$latestVersion=$latestVersion\n";
		    	
			}
		}

		#Show buttom update
		$CONF->{OutputAjax}->{showUpdate} = 0;
		$CONF->{OutputAjax}->{msgAjax} = '-';
		if($CONF->{debug}){
			print "Latest=$latestVersion<br>\n";
			print "Current=" . $CONF->{product}->{$productName}->{version} . "<br>\n";
		}
		if(($latestVersion && $latestVersion ne '-') 
			&& defined $CONF->{product}->{$productName}->{version} && $CONF->{product}->{$productName}->{version} ne ''
		){	
			if( $latestVersion ne $CONF->{product}->{$productName}->{version} ){
				$CONF->{OutputAjax}->{showUpdate} = 1;
				$CONF->{OutputAjax}->{changelogUrl} = $CONF->{product}->{$productName}->{changelogUrl};
				$CONF->{OutputAjax}->{updateUrl} = $CONF->{product}->{$productName}->{updateUrl};
			}else{
				$CONF->{OutputAjax}->{showUpdate} = 0;
				$CONF->{OutputAjax}->{msgAjax} = 'Latest Version';
				$CONF->{OutputAjax}->{changelogUrl} = $CONF->{product}->{$productName}->{changelogUrl};
			}
		}
	}
	
	#Validate Reseller Platform installed
	
	if(defined $CONF->{resellerpf}->{$productName} && $CONF->{resellerpf}->{$productName} ne ''){
		#print "Validate Reseller Platform installed\n";
		my $fileProduct = dirname(realpath(__FILE__)) .'/RvglobalsoftManager/ResellerPFConfig/'. $appName . '.pm';
		#print "\$fileProduct=$fileProduct<br>\n";
		if (-f $fileProduct && defined $CONF->{resellerpf}->{$productName}->{latestVersion} && $CONF->{resellerpf}->{$productName}->{latestVersion} ne '') {
			my $code = 'require \'' .$fileProduct . '\'; ';
		    $code .= '$latestVersion =' . $CONF->{resellerpf}->{$productName}->{latestVersion};
		    $latestVersion = eval $code;
		    #print "\$latestVersionX=$latestVersion=<br>\n";
		}
		
		#Show buttom update
		$CONF->{OutputAjax}->{showUpdate} = 0;
		$CONF->{OutputAjax}->{msgAjax} = '-';
		if($CONF->{debug}){
			print "Latest=$latestVersion<br>\n";
			print "Current=" . $CONF->{resellerpf}->{$productName}->{version} . "<br>\n";
		}
		if(($latestVersion && $latestVersion ne '-') 
			&& defined $CONF->{resellerpf}->{$productName}->{version} && $CONF->{resellerpf}->{$productName}->{version} ne ''
		){	
			if( $latestVersion ne $CONF->{resellerpf}->{$productName}->{version} ){
				$CONF->{OutputAjax}->{showUpdate} = 1;
				$CONF->{OutputAjax}->{changelogUrl} = $CONF->{resellerpf}->{$productName}->{changelogUrl};
				$CONF->{OutputAjax}->{updateUrl} = $CONF->{resellerpf}->{$productName}->{updateUrl};
			}else{
				$CONF->{OutputAjax}->{showUpdate} = 0;
				$CONF->{OutputAjax}->{msgAjax} = 'Latest Version';
			}
		}
	}
	
}

sub ajax_getLicenseInfo {
    print "=====>Call ajax_getLicenseInfo();\n" if($CONF->{debug});
    my $programname  = (defined $PARAM->{product}) ? $PARAM->{product} : '';
    $CONF->{OutputAjax}->{programname} = $programname;
    $CONF->{OutputAjax}->{showsubproductbtn} = '1';
    if ($programname ne '') {
    	my @aAllIP = RvglobalsoftManager::Rvlibary::getAllIP();
    	my $rvcode = RvglobalsoftManager::Rvlibary::rvsLicenseEncode(@aAllIP);
    	$rvcode =~ s/\r|\t|\n//gi;
    	my $url = 'https://license2.rvglobalsoft.com/getinfo/'.$programname;
    	my %licenseData = RvglobalsoftManager::Rvlibary::getcontentbyurl($url,'POST',"rvcode=$rvcode");
    	
    	#if not get license info
		if (!%licenseData || $licenseData{'socketfail'}) {
		   $CONF->{OutputAjax}->{licensesocketfail} = 1;
		   $CONF->{OutputAjax}->{licenseerrorsocketfail} = $licenseData{'socketfail'};
		}
		
		#data to use after unserialize
		my %licenseinfo = RvglobalsoftManager::Rvlibary::UnSerializerData($licenseData{'page'});
		
		#if have an error
		if ($licenseinfo{'on error'}) {
			$CONF->{OutputAjax}->{licenseerror} = 1;
			$CONF->{OutputAjax}->{licenseerrorid} = $licenseinfo{'issue_id'};
			my $NATIP = 0;
			($CONF->{OutputAjax}->{licenseerrormsg},$NATIP)	= getlicenseerrormsg($programname,\@aAllIP,\%licenseinfo);
			if ($licenseinfo{'issue_id'} eq '001' || $licenseinfo{'issue_id'} eq '101' || $licenseinfo{'issue_id'} eq '402' ||$licenseinfo{'issue_id'} eq '002' ) {
				$CONF->{OutputAjax}->{btnmsg} = 'Contact Staff';
				if($programname eq 'rvskin'){
					$CONF->{OutputAjax}->{btnlink} = 'https://rvglobalsoft.com/tickets/new&deptId=6';
				}else{
					$CONF->{OutputAjax}->{btnlink} = 'https://rvglobalsoft.com/tickets/new&deptId=5';
				}
				$CONF->{OutputAjax}->{btncolor} = 'warning';
			} elsif ($licenseinfo{'issue_id'} eq '301' || $licenseinfo{'issue_id'} eq '302' || $licenseinfo{'issue_id'} eq '501' ) {
				$CONF->{OutputAjax}->{btnmsg} = 'Renew';
				$CONF->{OutputAjax}->{btnlink} = 'https://rvglobalsoft.com/clientarea';
				$CONF->{OutputAjax}->{btncolor} = 'warning';
			} elsif ($licenseinfo{'issue_id'} eq '401') {
				if($NATIP) {
					$CONF->{OutputAjax}->{btnmsg} = 'NAT IP Licensing';
					$CONF->{OutputAjax}->{btnlink} = 'https://rvglobalsoft.com/knowledgebase/article/306/order-and-change-ip-for-rv-product-licenses/';
					$CONF->{OutputAjax}->{btncolor} = 'warning';
				} else {
					$CONF->{OutputAjax}->{btnmsg} = $CONF->{product}->{$programname}->{price};
					$CONF->{OutputAjax}->{btnlink} = $CONF->{product}->{$programname}->{orderUrl};
					$CONF->{OutputAjax}->{btncolor} = 'info';
					$CONF->{OutputAjax}->{showsubproductbtn} = '0';
				}
			}
		}
		#if not have an error
		elsif ($licenseinfo{$programname}) {
			$CONF->{OutputAjax}->{licenseerror} = 0;
			$CONF->{OutputAjax}->{licenseinfostatus} = $licenseinfo{$programname}{'status'};
			$CONF->{OutputAjax}->{licenseinfoexpire} = $licenseinfo{$programname}{'expire-show'};
	    }
	    else {
	    	$CONF->{OutputAjax}->{licenseerror} = 0;
			$CONF->{OutputAjax}->{licenseinfostatus} = 'unknown';
			$CONF->{OutputAjax}->{licenseinfoexpire} = 'unknown';
	    }
	}    
}

sub getlicenseerrormsg {
	my $programname = $_[0];
	my @allip = @{$_[1]};
	my %licenseinfo = %{$_[2]};
	my $NATIP = 0;
    my $errorCode = $licenseinfo{'issue_id'};
    my $dateexpire = '';
    my @monthname = qw(January February March April May June July August September October November December);  
    foreach my $keys(keys %licenseinfo) {
        if ($keys =~ /tmp_/i) {
            my ($S,$M,$H,$d,$m,$Y) = localtime($licenseinfo{$keys});
            $Y += 1900;
            $dateexpire = sprintf("%s %02d, %04d - %02d:%02d (GMT+0)", $monthname[$m], $d, $Y, $H, $M);
            last;
        }
        
    }
    my $licenseerrortext = '';
    
    if ($errorCode eq '001') {
        $licenseerrortext = "Unknown software code." ;
    } 
    elsif ($errorCode eq '002') {
        $licenseerrortext = "There's no IP to be submitted from this server environment." ;
    }
    elsif ($errorCode eq '101') {
        $licenseerrortext = $programname." license for ".join(" ",@allip)." has not been activated yet." ;
    }
    elsif ($errorCode eq '301') {
    	$licenseerrortext = $programname." license for ".join(" ",@allip)." has been suspended. Last expiration date ".$dateexpire." . " ;
    	if (defined $licenseinfo{'license_type'} && ($licenseinfo{'license_type'} eq '0' || $licenseinfo{'license_type'} eq '1')) {
    		$licenseerrortext = $licenseerrortext."Please login to your account to renew the pending invoice at https://rvglobalsoft.com/clientarea -> “Account” -> “Invoices” and pay the pending invoice.";
    	}
    	if (defined $licenseinfo{'license_type'} && $licenseinfo{'license_type'} eq '2') {
            $licenseerrortext = $licenseerrortext."The license still can be used but can't update versions. Please login to your account at https://rvglobalsoft.com/clientarea, click “Service”  -> “Perpetual Licenses” -> check box infront of the license(s) you want to renew, click “Renew” botton.";
        }
    }
    elsif ($errorCode eq '302') {
        $licenseerrortext = $programname." license for ".join(" ",@allip)." has been forced to suspend. Please contact RVglobalsoft staff at https://rvglobalsoft.com/tickets/new&deptId=6." ;
    }
    elsif ($errorCode eq '401') {
         my $isPrivateIP = RvglobalsoftManager::Rvlibary::is_privateIP(@allip);
         if ($isPrivateIP) {
             my %yourPublicIP = RvglobalsoftManager::Rvlibary::getcontentbyurl('https://myip.cpanel.net/v1.0/');
             $licenseerrortext =  "Your server is running behind NAT/Firewall IP ".join(" ",@allip)." with public IP ".$yourPublicIP{'page'}." which has not been found in our license system. Please update the license IP related to this server by this guide. https://rvglobalsoft.com/knowledgebase/article/306/order-and-change-ip-for-rv-product-licenses/" ;
         	 $NATIP = 1;
         } else {
             $licenseerrortext =  "None of this server IPs ".join(" ",@allip)." was found in RVglobalsoft license system. Please validate license with RVglobalsoft https://rvglobalsoft.com/verifyrvlicense, or contact rvstaff at https://rvglobalsoft.com/tickets/new&deptId=6." ;        
         }
    }
    elsif ($errorCode eq '402') {
        $licenseerrortext = $programname." license for ".join(" ",@allip)." has more than 1 record. Please contact RVglobalsoft staff at https://rvglobalsoft.com/tickets/new&deptId=6." ;
    }
    elsif ($errorCode eq '501') {
        $licenseerrortext = $programname." license for ".join(" ",@allip)." has been expired. (".$dateexpire.") . " ;
        if (defined $licenseinfo{'license_type'} && ($licenseinfo{'license_type'} eq '0' || $licenseinfo{'license_type'} eq '1')) {
            $licenseerrortext = $licenseerrortext."To avoid license suspended, please login to your account to renew at https://rvglobalsoft.com/clientarea -> “Account” -> “Invoices” and pay the pending invoice.";
        }
        if (defined $licenseinfo{'license_type'} && $licenseinfo{'license_type'} eq '2') {
            $licenseerrortext = $licenseerrortext."The license still can be used but can't update versions. Please login to your account at https://rvglobalsoft.com/clientarea, click “Service”  -> “Perpetual Licenses” -> check box infront of the license(s) you want to renew, click “Renew” botton.";
        }
        
    }
    else {
        $licenseerrortext = "Unknown ERROR";
    }
    return $licenseerrortext,$NATIP ;
}

sub ajax_doSaveRestrictReseller {
	print "=====>Call ajax_doSaveRestrictReseller();\n" if($CONF->{debug});
	return 0 if(!$CONF->{CPHandle}->isRoot());
 	my $allow = (defined $PARAM->{res_accres})? $PARAM->{res_accres} : 0;
	if($CONF->{debug}){
		print "getRootDir=" . $CONF->{CPHandle}->getRootDir() . '/.rvglobalsoft/.allowreseller' . "\n";
		print "Allow Restrict Reseller = $allow\n";
	}
 	
	if ($allow) {
		open(FD, '>', $CONF->{CPHandle}->getRootDir() . '/.rvglobalsoft/.allowreseller');
		print FD 'allow';
		close(FD); 
	} else {
		if (-f $CONF->{CPHandle}->getRootDir() . '/.rvglobalsoft/.allowreseller') {
			unlink($CONF->{CPHandle}->getRootDir() . '/.rvglobalsoft/.allowreseller');
		}
	}
	return 1;
}

sub ajax_saveWhmUI {
    print "=====>Call ajax_saveWhmSidebar();\n" if($CONF->{debug});
    my $content  = (defined $PARAM->{content}) ? $PARAM->{content} : 0;
    my $filename = (defined $PARAM->{filename}) ? $PARAM->{filename} : 0;
    if ($content && $filename && defined $CONF->{product}->{'rvskin'}->{checkInstallComplete} && $CONF->{product}->{'rvskin'}->{checkInstallComplete}) {
    	#root
    	if ($ENV{'REMOTE_USER'} eq 'root') { 
	        open(FD, '>', (getpwnam('rvadmin'))[7] . '/.rvwhm'.$filename);
	        print FD $content;
	        close(FD);
	        system('chown','rvadmin:rvadmin',(getpwnam('rvadmin'))[7] . '/.rvwhm'.$filename);
    	} 
    	#reseller
    	else {
    		open(FD, '>', (getpwnam($ENV{'REMOTE_USER'}))[7] . '/rvadmin/.rvwhm'.$filename);
            print FD $content;
            close(FD);
            system('chown',$ENV{'REMOTE_USER'}.':'.$ENV{'REMOTE_USER'},(getpwnam($ENV{'REMOTE_USER'}))[7] . '/.rvwhm'.$filename); 
    	}
    } 
    return 1;
}


sub buildHtmlPageHeader {
	
	if(getRequestType() eq 'Browser') {
	    print "Content-Type: text/html; charset=utf-8\n\n" if(getRequestType() eq 'Browser');	  
		
		if (Cpanel::Version::Compare::compare(Cpanel::Version::getversionnumber(), '>', '11.62')) {
	                #defheader for cpanel 64 frame header
	                use Whostmgr::HTMLInterface ();
	                Whostmgr::HTMLInterface::defheader();
	    }   
	}
}

sub buildAjaxPageHeader {
	if(getRequestType() eq 'Ajax'){
		print "Expires: 0\n";
		print "Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0";
		print "Content-Type: application/json; charset=utf-8\n\n" ;
	}
}

sub function_exists {
    no strict 'refs';
    my $funcname = shift;
    return \&{$funcname} if defined &{$funcname};
    return;
}

sub buildData {
	foreach my $eachProduct (keys %{$CONF->{product}}){
		
		#========== My Software ==========
		if(validateMySoftware($eachProduct)){
			
			$CONF->{mySoftware}->{$eachProduct} = $CONF->{product}->{$eachProduct};
			#Show buttom update
			if(defined $CONF->{product}->{$eachProduct}->{latestVersion} && defined $CONF->{product}->{$eachProduct}->{version}
				&& $CONF->{product}->{$eachProduct}->{latestVersion} ne $CONF->{product}->{$eachProduct}->{version} 
			){
				#$CONF->{mySoftware}->{$eachProduct} = $CONF->{product}->{$eachProduct};
				if($CONF->{mySoftware}->{$eachProduct}){
					$CONF->{mySoftware}->{$eachProduct}->{showUpdate} = 1;
				}
			}
		}else{
		#Recommended
			$CONF->{recomSoftware}->{$eachProduct} = $CONF->{product}->{$eachProduct};
			#Check & Validate price / priceForVPS
			if(isVpsServer() && defined $CONF->{recomSoftware}->{$eachProduct}->{priceForVPS} && $CONF->{recomSoftware}->{$eachProduct}->{priceForVPS} ne ''){
				$CONF->{recomSoftware}->{$eachProduct}->{price} = $CONF->{recomSoftware}->{$eachProduct}->{priceForVPS}
			}
		
		}
		
		#Documentation
		#Show Document
		if(validateDocument($eachProduct)){
			#$CONF->{documentation}->{$eachProduct} = $CONF->{product}->{$eachProduct};
			#Manual
			if(defined $CONF->{product}->{$eachProduct}->{manualUrl} && $CONF->{product}->{$eachProduct}->{manualUrl} ne ''){
				$CONF->{documentation}->{$eachProduct}->{1}->{showMsgDisplay} = 'Manual';
				$CONF->{documentation}->{$eachProduct}->{1}->{showUrl} = $CONF->{product}->{$eachProduct}->{manualUrl};
			}
			#Tutorial
			if(defined $CONF->{product}->{$eachProduct}->{tutorialUrl} && $CONF->{product}->{$eachProduct}->{tutorialUrl} ne ''){
				$CONF->{documentation}->{$eachProduct}->{2}->{showMsgDisplay} = 'Tutorial';
				$CONF->{documentation}->{$eachProduct}->{2}->{showUrl} = $CONF->{product}->{$eachProduct}->{tutorialUrl};
			}
			#Send Support Ticket
			if(defined $CONF->{product}->{$eachProduct}->{supportUrl} && $CONF->{product}->{$eachProduct}->{supportUrl} ne ''){
				$CONF->{documentation}->{$eachProduct}->{3}->{showMsgDisplay} = 'Send Support Ticket';
				$CONF->{documentation}->{$eachProduct}->{3}->{showUrl} = $CONF->{product}->{$eachProduct}->{supportUrl};
			}
			#Changelog
			if(defined $CONF->{product}->{$eachProduct}->{changelogUrl} && $CONF->{product}->{$eachProduct}->{changelogUrl} ne ''){
				$CONF->{documentation}->{$eachProduct}->{4}->{showMsgDisplay} = 'Changelog';
				$CONF->{documentation}->{$eachProduct}->{4}->{showUrl} = $CONF->{product}->{$eachProduct}->{changelogUrl};
			}
			
		}
	}
	
	foreach my $eachProduct (keys %{$CONF->{resellerpf}}){
		
		#Documentation
		#Show Document
		if(validateDocument($eachProduct)){
			#$CONF->{documentation}->{$eachProduct} = $CONF->{resellerpf}->{$eachProduct};
			#Manual
			if(defined $CONF->{resellerpf}->{$eachProduct}->{manualUrl} && $CONF->{resellerpf}->{$eachProduct}->{manualUrl} ne ''){
				$CONF->{documentation}->{$eachProduct}->{1}->{showMsgDisplay} = 'Manual';
				$CONF->{documentation}->{$eachProduct}->{1}->{showUrl} = $CONF->{resellerpf}->{$eachProduct}->{manualUrl};
			}
			#Tutorial
			if(defined $CONF->{resellerpf}->{$eachProduct}->{tutorialUrl} && $CONF->{resellerpf}->{$eachProduct}->{tutorialUrl} ne ''){
				$CONF->{documentation}->{$eachProduct}->{2}->{showMsgDisplay} = 'Tutorial';
				$CONF->{documentation}->{$eachProduct}->{2}->{showUrl} = $CONF->{resellerpf}->{$eachProduct}->{tutorialUrl};
			}
			#Send Support Ticket
			if(defined $CONF->{resellerpf}->{$eachProduct}->{supportUrl} && $CONF->{resellerpf}->{$eachProduct}->{supportUrl} ne ''){
				$CONF->{documentation}->{$eachProduct}->{3}->{showMsgDisplay} = 'Send Support Ticket';
				$CONF->{documentation}->{$eachProduct}->{3}->{showUrl} = $CONF->{resellerpf}->{$eachProduct}->{supportUrl};
			}
			#Changelog
			if(defined $CONF->{resellerpf}->{$eachProduct}->{changelogUrl} && $CONF->{resellerpf}->{$eachProduct}->{changelogUrl} ne ''){
				$CONF->{documentation}->{$eachProduct}->{4}->{showMsgDisplay} = 'Changelog';
				$CONF->{documentation}->{$eachProduct}->{4}->{showUrl} = $CONF->{resellerpf}->{$eachProduct}->{changelogUrl};
			}
			
		}
	}
}

sub buildDataAjax {
	foreach my $eachProduct (keys %{$CONF->{productAjax}}){
		#========== Recommended Software ==========
		if(!defined $CONF->{product}->{$eachProduct}){
			$CONF->{recomSoftwareAjax}->{$eachProduct} = $CONF->{productAjax}->{$eachProduct};
		}
		
		#========== Show Document ==========
#		print "validateDocumentAjax($eachProduct)=" . validateDocumentAjax($eachProduct) . "\n" if($CONF->{debug});
#		if(validateDocumentAjax($eachProduct)){
#			$CONF->{documentationAjax}->{$eachProduct} = $CONF->{productAjax}->{$eachProduct};
#			if(defined $CONF->{productAjax}->{$eachProduct}->{manualUrl} && $CONF->{productAjax}->{$eachProduct}->{manualUrl} ne ''){
#				$CONF->{documentationAjax}->{$eachProduct}->{showMsgDisplay} = 'Manual';
#				$CONF->{documentationAjax}->{$eachProduct}->{showUrl} = $CONF->{documentationAjax}->{$eachProduct}->{manualUrl};
#			}elsif(defined $CONF->{productAjax}->{$eachProduct}->{tutorialUrl} && $CONF->{productAjax}->{$eachProduct}->{tutorialUrl} ne ''){
#				$CONF->{documentationAjax}->{$eachProduct}->{showMsgDisplay} = 'Tutorial';
#				$CONF->{documentationAjax}->{$eachProduct}->{showUrl} = $CONF->{documentationAjax}->{$eachProduct}->{tutorialUrl};
#			}elsif(defined $CONF->{productAjax}->{$eachProduct}->{supportUrl} && $CONF->{productAjax}->{$eachProduct}->{supportUrl} ne ''){
#				$CONF->{documentationAjax}->{$eachProduct}->{showMsgDisplay} = 'Support';
#				$CONF->{documentationAjax}->{$eachProduct}->{showUrl} = $CONF->{documentationAjax}->{$eachProduct}->{supportUrl};
#			}
#		}
	}
}

sub validateMySoftware {
	my $checkSoftware = $_[0];
	#Install Complete
	if(defined $CONF->{product}->{$checkSoftware}->{checkInstallComplete} 
		&& $CONF->{product}->{$checkSoftware}->{checkInstallComplete}){
		return 1;
	}
	
	#Validate License
	#TODO
	
	return 0;
}

sub validateDocument {
	my $checkSoftware = $_[0];
	if((defined $CONF->{product}->{$checkSoftware}->{tutorialUrl} && $CONF->{product}->{$checkSoftware}->{tutorialUrl} ne '')
		|| (defined $CONF->{product}->{$checkSoftware}->{manualUrl} && $CONF->{product}->{$checkSoftware}->{manualUrl} ne '')
		|| (defined $CONF->{product}->{$checkSoftware}->{supportUrl} && $CONF->{product}->{$checkSoftware}->{supportUrl} ne '')
		|| (defined $CONF->{product}->{$checkSoftware}->{changelogUrl} && $CONF->{product}->{$checkSoftware}->{changelogUrl} ne '')
	){
		return 1
	}
	
	if((defined $CONF->{resellerpf}->{$checkSoftware}->{tutorialUrl} && $CONF->{resellerpf}->{$checkSoftware}->{tutorialUrl} ne '')
		|| (defined $CONF->{resellerpf}->{$checkSoftware}->{manualUrl} && $CONF->{resellerpf}->{$checkSoftware}->{manualUrl} ne '')
		|| (defined $CONF->{resellerpf}->{$checkSoftware}->{supportUrl} && $CONF->{resellerpf}->{$checkSoftware}->{supportUrl} ne '')
		|| (defined $CONF->{resellerpf}->{$checkSoftware}->{changelogUrl} && $CONF->{resellerpf}->{$checkSoftware}->{changelogUrl} ne '')
	){
		return 1
	}
	return 0;
}

sub validateDocumentAjax {
	my $checkSoftware = $_[0];
	if((defined $CONF->{productAjax}->{$checkSoftware}->{tutorialUrl} && $CONF->{productAjax}->{$checkSoftware}->{tutorialUrl} ne '')
		|| (defined $CONF->{productAjax}->{$checkSoftware}->{manualUrl} && $CONF->{productAjax}->{$checkSoftware}->{manualUrl} ne '')
		|| (defined $CONF->{productAjax}->{$checkSoftware}->{supportUrl} && $CONF->{productAjax}->{$checkSoftware}->{supportUrl} ne '')
	){
		return 1
	}
	return 0;
}

sub buildBody {
	
}

sub buildFooter {
	
}

sub rvDumper {
	my $requestType = getRequestType();
	if($requestType eq 'Browser' || $requestType eq 'Ajax'){
		print "<pre>";
		print Dumper(@_);
		print "</pre>";
	}else{
		print Dumper(@_);
	}
}

sub isVpsServer {
	my $serverType = RvglobalsoftManager::ServerInfo::getServerType();
	if($serverType =~ /VPS Server/){
		return 1;
	}
	return 0;
}

sub validateDebugMod {
	my $fileDebug = dirname(realpath(__FILE__)) . '/debug';
	if(-e $fileDebug){
		$CONF->{debug} = 1;
	}elsif(defined $PARAM->{'debug'} && $PARAM->{'debug'}){
		$CONF->{debug} = 1;
	}elsif(defined $CONF->{debug}){
		#
	}else{
		$CONF->{debug} = 0;
	}
}

sub buildOutput {
    # some useful options (see below for full list)
    my $config = {
        INCLUDE_PATH => dirname(realpath(__FILE__)) . '/template',  # or list ref
        INTERPOLATE  => 1,               # expand "$var" in plain text
        POST_CHOMP   => 1,               # cleanup whitespace
        #PRE_PROCESS  => 'header',        # prefix each template
        EVAL_PERL    => 1,               # evaluate Perl code blocks
    };

    # create Template object
    my $template = Template->new($config);

    # define template variables for replacement
	my $vars = {
#        var1  => $value,
#        var2  => \%hash,
#        var3  => \@list,
#        var4  => \&code,
#        var5  => $object,
		'Config' => $CONF,
		
    };

    # specify input filename, or file handle, text reference, etc.
    my $input = 'index.tmpl';

    # process input template, substituting variables
    $template->process($input, $vars)
        || die $template->error();
}

sub buildAlaxOutput {
	#Validate Template
	my $input = (defined $CONF->{template} && $CONF->{template} ne '')?$CONF->{template}:'ajaxTemplate.tmpl';
	print "Template=" . $input . "\n" if($CONF->{debug});
    # some useful options (see below for full list)
    my $config = {
        INCLUDE_PATH => dirname(realpath(__FILE__)) . '/template',  # or list ref
        INTERPOLATE  => 1,               # expand "$var" in plain text
        POST_CHOMP   => 1,               # cleanup whitespace
        #PRE_PROCESS  => 'header',        # prefix each template
        EVAL_PERL    => 1,               # evaluate Perl code blocks
    };

    # create Template object
    my $template = Template->new($config);

    # define template variables for replacement
	my $vars = {
#        var1  => $value,
#        var2  => \%hash,
#        var3  => \@list,
#        var4  => \&code,
#        var5  => $object,
		'Config' => $CONF,
		
    };

    # specify input filename, or file handle, text reference, etc.
    #my $input = 'index.tmpl';

    # process input template, substituting variables
    $template->process($input, $vars)
        || die $template->error();
	
}

sub getParam {
	my $q = CGI->new();
	my @allParam = $q->param;
	$PARAM = {};
	foreach (@allParam){
		if(defined $q->param($_)){
			$PARAM->{$_} = $q->param($_);
		}
	}
	validateDebugMod();
	rvDumper($PARAM) if($CONF->{debug});
}

sub getRequestType {
	if (defined $ENV{'HTTP_X_REQUESTED_WITH'} && $ENV{'HTTP_X_REQUESTED_WITH'} eq 'XMLHttpRequest'){
		$CONF->{'requestType'} = 'Ajax';
		return 'Ajax';
	} elsif (defined $ENV{'HTTP_USER_AGENT'}) {
		$CONF->{'requestType'} = 'Browser';
		return 'Browser';
	} else {
		$CONF->{'requestType'} = 'Cli';
		return 'Cli';
	} 
}

sub setRequestType {
	if (defined $ENV{'HTTP_X_REQUESTED_WITH'} && $ENV{'HTTP_X_REQUESTED_WITH'} eq 'XMLHttpRequest'){
		$CONF->{'requestType'} = 'Ajax';
		return 'Ajax';
	} elsif (defined $ENV{'HTTP_USER_AGENT'}) {
		$CONF->{'requestType'} = 'Browser';
		return 'Browser';
	} else {
		$CONF->{'requestType'} = 'Cli';
		return 'Cli';
	} 
}

sub logmessage{
	my ($msg,$line) = @_;
    if (-w '/usr/local/rvglobalsoft/rvglobalsoftmanager/log') {
    	my ($date) = getDate();
        my ($user) = getpwuid($<);
        my ($id) = $ENV{RVS_SESSION};
        #$msg = $date . ': rvsitebuilder: ' . $user . '(' . $id . '): ' . $msg;
        if(defined $line && $line ne ''){
        	$msg = $date . ': ' . $user . ' [line ' . $line . '] : ' . $msg;
        }else{
        	$msg = $date . ': ' . $user . ' : ' . $msg;
        }
        open(my $fd, '>>', '/usr/local/rvglobalsoft/rvglobalsoftmanager/log');
        print  $fd "$msg\n";
        close($fd);
    }
}

sub getDate {
    my %dttime = ();
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

    $dttime{year }  = sprintf "%04d",($year + 1900);  ## four digits to specify the year
    $dttime{mon  }  = sprintf "%02d",($mon + 1);      ## zeropad months
    $dttime{mday }  = sprintf "%02d",$mday;           ## zeropad day of the month
    $dttime{wday }  = sprintf "%02d",$wday + 1;       ## zeropad day of week; sunday = 1;
    $dttime{yday }  = sprintf "%02d",$yday;           ## zeropad nth day of the year
    $dttime{hour }  = sprintf "%02d",$hour;           ## zeropad hour
    $dttime{min  }  = sprintf "%02d",$min;            ## zeropad minutes
    $dttime{sec  }  = sprintf "%02d",$sec;            ## zeropad seconds
    $dttime{isdst}  = $isdst;

    return "$dttime{year}-$dttime{mon}-$dttime{mday} $dttime{hour}:$dttime{min}:$dttime{sec}";
}
1;

Copyright 2K16 - 2K18 Indonesian Hacker Rulez