<?php

global $EncryptKey;
$EncryptKey = 'A1B2C3D4E5F6zG7yK8xL9vM0uNtsOrqPpoQnmRlkSjiThgUfeVdcWbaXYZHwIJ!@#$%^&*?)(+';


if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType) 
{
  $theValue = trim($theValue);	
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Null";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "Null";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "Null";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Null";
      break;
  }
  return $theValue;
}
}

function GenerateLicenseSerials($PIndex, $PBinSerial, $PVersion, $PLicense, $PBoardSerial, $connection) {
	
	$vSerial = '';
	
	//Generate License
	if ($PIndex == '1') {
		//Convert Binary To Hexadecimal
		//$sub_query = " Select CONV('".$PBinSerial."', 2, 16) ";
		$sub_query = " Select UPPER(CAST(MD5('".$PBinSerial."') as CHAR(12))) ";
		$sub_result = mysql_query($sub_query, $connection);
		$sub_row    = mysql_fetch_row($sub_result);
		$HexSerial = $sub_row[0];
		
		//Get A Random String Of 4 Chars
		$sub_query = " Select UPPER(CAST(MD5(RAND()) as CHAR(4))) ";
		$sub_result = mysql_query($sub_query, $connection);
		$sub_row    = mysql_fetch_row($sub_result);
		$RandSerial = $sub_row[0];
	
		$vSerial = $HexSerial . $RandSerial;
	}
	//Generate Serial Combination
	else if ($PIndex == '2') {
		//Get A Random String Of 4 Chars
		$sub_query = " Select UPPER(CAST(MD5(RAND()) as CHAR(4))) ";
		$sub_result = mysql_query($sub_query, $connection);
		$sub_row    = mysql_fetch_row($sub_result);
		$RandSerial = $sub_row[0];
		
		//Get Current Year/Month
		$sub_query = " Select Date_Format(now(), '%y%m') ";
		$sub_result = mysql_query($sub_query, $connection);
		$sub_row    = mysql_fetch_row($sub_result);
		$DateSerial = $sub_row[0];
	
		//GET Server Baseboard Serial Number
		$BaseBoardSerial = GetBaseBoardSerialNo();
		
		$vSerial = $PVersion ."-". $PLicense ."-". $BaseBoardSerial ."-". $DateSerial ."-". $RandSerial;
	}

	return $vSerial;
}

function GetBaseBoardSerialNo() {
	//GET Server Baseboard Serial Number
	$output = array();
	exec('%SystemRoot%\\System32\\Wbem\\wmic BASEBOARD GET SerialNumber 2>&1', $output);
	$BaseBoardSerial = $output[1];

	return $BaseBoardSerial;
}

function CheckSoftwareLicense($connection, $EncryptKey) {
	
	$error = "";
	
	$BaseBoardSerial = GetBaseBoardSerialNo();
	
	//$sub_query  = " select count(*) from license where ifnull(license_activate,0) = 1 ";
	$sub_query  = " select Setup_LicenseNo from setup limit 1 ";
	$sub_result = mysql_query($sub_query,$connection);
	$sub_row    = mysql_fetch_row($sub_result);
	$LicenseExists = 0;
	if ($sub_row[0] != "") $LicenseExists = 1;
	
	if ($LicenseExists == 0) {
		$error = "Unable to run the Software. License is not activated.<br>Please activate license and try login again.";
		$ErrorExists = 1;
	}
	
	if ($ErrorExists == 0) {
		$sub_query  = " select Setup_ClientSerialNo from setup limit 1 ";
		$sub_result = mysql_query($sub_query,$connection);
		$sub_row    = mysql_fetch_row($sub_result);
		$ClientSerialNo  = $sub_row[0];
		
		$ClientSerialNo = DecryptData($ClientSerialNo, $EncryptKey);
		
		if (strpos($ClientSerialNo,$BaseBoardSerial) === false) { 
			$error = "Unable to run the Software. Incorrect license.";
			$ErrorExists = 1;
		}	
	}
	
	return $error;
}

function GetLicenseModules($connection, $dbLabel, $EncryptKey) {
	
	$ModulesList = array();
	
	$sub_query  = " select license_modules from ".$dbLabel.".license where ifnull(license_activate,0) = 1 limit 1 ";
	$sub_result = mysql_query($sub_query,$connection);
	$sub_row    = mysql_fetch_row($sub_result);
	$LicenseModules = $sub_row[0];
	
	$LicenseModules = DecryptData($LicenseModules, $EncryptKey);
	
	for ($i=1; $i <= strlen($LicenseModules); $i++) {
		
		$Index = 'B' . str_pad($i,2,'0',STR_PAD_LEFT);
		$ModulesList[$Index] = substr($LicenseModules, $i-1, 1);
	}
	
	
	return $ModulesList;
}

function EncryptData($text, $key)
{
    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}

function DecryptData($text, $key)
{
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
}



?>