<?php

include("config.inc.php");
include("FunctionsLicense.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
$connremote = mysql_connect($config['dbServerRemote'],$config['dbUserRemote'],$config['dbPassRemote']) or die("Could not connect to DB");

$actionmode = $_GET['actionmode'];
$license_companyname = $_POST['companyname'];
$license_firstname = $_POST['firstname'];
$license_fathername = $_POST['fathername'];
$license_familyname = $_POST['familyname'];
$license_email = $_POST['email'];
$license_telephoneno = $_POST['telephoneno'];
$license_mobile = $_POST['mobile'];
$license_country = $_POST['country'];
$license_address = $_POST['address'];
$license_licenseno = $_POST['licenseno'];

$EmailFilter = "/([\w\-]+\@[\w\-]+\.[\w\-]+)/";
$SetupVersion = $config['Version'];

if ($actionmode == 'request') {
	$ErrorExists = 0;
	
	mysql_select_db($config['dbName'], $connection);
	
	if (!preg_match($EmailFilter,$license_email)) {
		$error = "Invalid email format."; 
		$ErrorExists = 1;
	}
	
	if ($ErrorExists == 0) {
		$counter = count($_POST['moduleslist']);
		
		$sub_query="select licensemodules_index from licensemodules order by licensemodules_index ";
		$sub_result=mysql_query($sub_query,$connection)	or die( mysql_error());
		while ($sub_row=mysql_fetch_row($sub_result)){
			$index = $sub_row[0];
			$indexchecked = 0;
			
			for ($i=0;$i<=$counter;$i++) {
				if ($_POST['moduleslist'][$i] == $index) {
					$indexchecked = 1;
					break;
				}	
			}
			
			$binarymodules .= $indexchecked;
		
		}

		//echo $binarymodules; exit;
		
		$LicenseNo = GenerateLicenseSerials('1', $binarymodules, '', '', '', $connection);
		$LicenseNo = EncryptData($LicenseNo, $EncryptKey);
		$Modules = EncryptData($binarymodules, $EncryptKey);
		
		
		$query = " insert into license (license_no, license_companyname, license_firstname, license_fathername, license_family, license_email, license_telephoneno, license_mobileno, license_country, license_address, license_modules) ".
				 " values (".GetSQLValueString($LicenseNo,'text').", ".GetSQLValueString($license_companyname,'text').", ".GetSQLValueString($license_firstname,'text').", ".
				 " 		   ".GetSQLValueString($license_fathername,'text').", ".GetSQLValueString($license_familyname,'text').", ".GetSQLValueString($license_email,'text').", ".
				 "	       ".GetSQLValueString($license_telephoneno,'text').", ".GetSQLValueString($license_mobile,'text').", ".GetSQLValueString($license_country,'text').", ".
				 "		   ".GetSQLValueString($license_address,'text').", ".GetSQLValueString($Modules,'text').")" ;

		$result = mysql_query($query,$connection);
		if ($result) {
			$error = "Request is sent successfully.<br>The License Number will be send to the following email address (".$license_email.").";
		} else {
			$error = "Problem in sending request. Please try again.";
		}	
	}	
	
    $error = ereg_replace(" ", "%20", $error);
	header("Location:RequestLicense.php?error=".$error);

}

else if ($actionmode == 'activate') {
	$LicenseExists = 0;
	$ErrorExists = 0;
	
	mysql_select_db($config['dbNameRemote'], $connremote);
	$sub_query  = " select count(*) from license where upper(license_no) = upper(".GetSQLValueString(EncryptData($license_licenseno, $EncryptKey),'text').")";
	$sub_result = mysql_query($sub_query,$connremote);
	$sub_row    = mysql_fetch_row($sub_result);
	$LicenseExists = $sub_row[0];
	
	if ($LicenseExists == 0) {
		$error = "License (".$license_licenseno.") doesn't exist.";
		$ErrorExists = 1;
	}
	
	if ($ErrorExists == 0) {
		$sub_query  = " select count(*) from license where upper(license_no) = upper(".GetSQLValueString(EncryptData($license_licenseno, $EncryptKey),'text').") and ifnull(license_activate,0) = 1 ";
		$sub_result = mysql_query($sub_query,$connremote);
		$sub_row    = mysql_fetch_row($sub_result);
		$LicenseActivated = $sub_row[0];
		
		if ($LicenseActivated >= 1) {
			$error = "License (".$license_licenseno.") is already activated.";
			$ErrorExists = 1;
		}
	}
	
	if ($ErrorExists == 0) {
		if (!preg_match($EmailFilter,$license_email)) {
			$error = "Invalid email format."; 
			$ErrorExists = 1;
		}
	
		if ($ErrorExists == 0) {
			$SerialNo = GenerateLicenseSerials('2', '', strtoupper($SetupVersion), strtoupper($license_licenseno), '', $connremote);
			$SerialNoEnc = EncryptData($SerialNo, $EncryptKey);
			
			$query = " update license set ".
					 " license_companyname = ".GetSQLValueString($license_companyname,'text').",".
					 " license_firstname = ".GetSQLValueString($license_firstname,'text').",".
					 " license_fathername = ".GetSQLValueString($license_fathername,'text').",".
					 " license_family = ".GetSQLValueString($license_familyname,'text').",".
					 " license_email = ".GetSQLValueString($license_email,'text').",".
					 " license_telephoneno = ".GetSQLValueString($license_telephoneno,'text').",".
					 " license_mobileno = ".GetSQLValueString($license_mobile,'text').",".
					 " license_country = ".GetSQLValueString($license_country,'text').",".
					 " license_address = ".GetSQLValueString($license_address,'text').", ".
					 " license_clientserialno = ".GetSQLValueString($SerialNoEnc,'text').", ".
					 " license_activate = 1 ".
					 " where upper(license_no) = upper(".GetSQLValueString(EncryptData($license_licenseno, $EncryptKey),'text').")";
			//echo $query; exit;
			$result = mysql_query($query,$connremote);
			$error .= mysql_error($connremote);
			
			if ($result) {
				$error = "License (".$license_licenseno.") is activated.";
				
				mysql_select_db($config['dbName'], $connection);
				$query1 = " update setup set Setup_LicenseNo = upper(".GetSQLValueString(EncryptData($license_licenseno, $EncryptKey),'text')."), ".
						  " Setup_ClientSerialNo = ".GetSQLValueString($SerialNoEnc,'text')." ";
				$result1 = mysql_query($query1,$connection);
				if (!$result1) $error .= mysql_error($connection);
			}
		} 	
	} 
	
	$error = ereg_replace(" ", "%20", $error);
	//header("Location:ActivateLicense.php?error=".$error);
	header("Location:../cashregister_index.php?LicenseError=".$error);
}

else if ($actionmode == 'generate') {
	$ErrorExists = 0;
	
	mysql_select_db($config['dbName'], $connection);
	
	if ($ErrorExists == 0) {
		$moduleslist = $_GET['moduleslist'];
		$moduleslist = explode(',', $moduleslist);
		$counter = count($moduleslist);
		
		$sub_query="select licensemodules_index from licensemodules order by licensemodules_index ";
		$sub_result=mysql_query($sub_query,$connection)	or die( mysql_error());
		while ($sub_row=mysql_fetch_row($sub_result)){
			$index = $sub_row[0];
			$indexchecked = 0;
			
			for ($i=0;$i<=$counter;$i++) {
				if ($moduleslist[$i] == $index) {
					$indexchecked = 1;
					break;
				}	
			}
			
			$binarymodules .= $indexchecked;
		
		}

		//echo $binarymodules; exit;
		
		for ($i=1; $i <= $_GET['licensecount']; $i++) {
		
			$LicenseNo = GenerateLicenseSerials('1', $binarymodules, '', '', '', $connection);
			$LicensesList .= $LicenseNo."<br>";
			
			$LicenseNo = EncryptData($LicenseNo, $EncryptKey);
			$Modules = EncryptData($binarymodules, $EncryptKey);
			

			$query = " insert into license (license_no, license_modules) ".
					 " values (".GetSQLValueString($LicenseNo,'text').", ".GetSQLValueString($Modules,'text').")" ;

			$result = mysql_query($query,$connection);

		}	
	}	
	
    $error = ereg_replace(" ", "%20", $error);
	//header("Location:GenerateLicense.php?error=".$error);
	
	$return_arr = array();

	$row_array['error'] = $error;
	$row_array['LicensesList'] = $LicensesList;
			
	array_push($return_arr,$row_array);
	echo json_encode($return_arr);

}


mysql_close($connection);


?>