<?php
/*******************************************************************************
 *                    _ _ _ _
 *                   / _ _  /
 *            _ _   / /__/ /_ _   ____ _ _   _ _ _    __
 *           / _ ` /   _ _// _ ` / __// _ ` / _ \\ \ / /
 *          / _ _// /\ \  / _ _/ \_ \/ _ _// / \/ \ ` /
 *          \_ , /_/  \_\ \_ ,  /__ /\_ , /_/      \_/
 *
 *******************************************************************************
 *    Application:  eReserv
 *    Version:      6
 *    Author:       David Di Certo
 *    Website:      http://www.cogites.com
 *    Email:        contact@cogites.com
 *    Copyright:    2013 - David Di Certo
 *******************************************************************************
 *    == FR ==      La revente et la redistribution de l'application sont
 *                  strictement interdites.
 *
 *                  Vous êtes tenu d'avoir pris connaissance des
 *                  Conditions Générales de Vente disponibles sur Cogites.com
 *                  (y compris pour la version Free).
 *
 *    == EN ==      Resale and redistribution of the application are
 *                  Strictly prohibited.
 *
 *                  You are required to have read the Terms and Conditions
 *                  available on Cogites.com (including the Free version).
 *
 *******************************************************************************
*/

	require "../_container/container_top.php";

	$action = getPost('action');
	if (!empty($action)){
		if ($action == "validatekey"){
			echo "KEY validation...";
			// get the license from sent data
			$license = getPost('key');
			// the set key is the key validated
			$results = $padl->validate($license);
			echo print_r($results);
		}
		if ($action == "validatefile"){
			echo "FILE validation...";
			// get the license from uploaded file
			$license = file_get_contents($_FILES['keyfile']['tmp_name']);
			// the set key is the key validated
			$results = $padl->validate($license);
			echo print_r($results);
		}
		if ($action == "generatekey"){
		
// $padl = new Padl\License(true, true, true, true);

//For better security injecting a copy of $_SERVER global var
// $server_array = $_SERVER;
// $padl->setServerVars($server_array);

$date_expire = '12/31/2014';
list($month, $day, $year) = explode("/", $date_expire);
// Calculating the time offset (expireIn)
$now       = mktime(date('H'), date('i'), date('s'), date('m'), date('d') , date('Y'));
$dateLimit = mktime(23, 59, 59, $month, $day, $year);
$expireIn  = $dateLimit - $now;

// Generating a key with your server details
$license = $padl->generate('localhost', 0, $expireIn);
print_r($license);
// Save the license anywhere, database, filesystem, even remotely
		}
	}

?>

	<div>
		<ul class="breadcrumb">
			<li><a href="./"><?php echo $tr->translate("menuHome"); ?></a> <span class="divider">/</span></li>
			<li class="active"><?php echo $tr->translate("menuLicense"); ?></li>
		</ul>
	</div>
	<div class="sortable row-fluid">
		<a data-rel="tooltip" title="<?php echo $tr->translate('AddReserv'); ?>" class="well span2 top-block btn-action" href="#" name="newReservation" id="newReservation" _page="reservationDetail.php" _action="Add">
			<i class="icon-red icon-user"></i>
			<div><?php echo $tr->translate("AddReserv"); ?></div>
			<div></div>
		</a>
	</div>

	<div class="row-fluid sortable">
		<div class="box span12">
			<div class="box-header well" data-original-title>
				<h2><i class="icon-picture"></i> License</h2>
			</div>
			<div class="box-content">
			<form class="form-horizontal" method="post" name="licenseForm" id="licenseForm" action="license.php" enctype="">
				<input type="hidden" name="action" id="action" value="" />
					<div class="control-group" style="margin-left: 20px;">
						<label class="control-label" for="cfg_date_format">License key:</label>
						<div class="controls">
							<textarea class="xxlarge" id="key" name="key"></textarea>
						</div>
					</div>
					<div class="control-group" style="margin-left: 20px;">
						<label class="control-label" for="cfg_date_format">License FILE:</label>
						<div class="controls">
							<input class="input-file" id="keyfile" name="keyfile" type="file" title="<?php echo $tr->translate("ChooseFileTitle"); ?>">
						</div>
					</div>

					<div class="form-actions">
						<button type="button" name="validatekey" id="validatekey" class="btn btn-primary btn-action">Submit and Validate Key</button>
						<button type="button" name="validatefile" id="validatefile" class="btn btn-success btn-action">Submit and Validate File</button>
						<button type="button" name="generatekey" id="generatekey" class="btn btn-success">Generate License</button>
					</div>
			</form>
					
				<?php 
					// echo "License ID: " . $lic['ID'] . "<br>";
					// echo "Domain Name: " . $lic['SERVER']['DOMAIN'] . "<br>";
					// echo "MAC: " . $lic['SERVER']['MAC'] . "<br>";
					// echo "SERVER_ADDR: " . $lic['SERVER']['PATH']['SERVER_ADDR'] . "<br>";
					// echo "HTTP_HOST: " . $lic['SERVER']['PATH']['HTTP_HOST'] . "<br>";
					// echo "SERVER_NAME: " . $lic['SERVER']['PATH']['SERVER_NAME'] . "<br>";
					// $ips = $lic['SERVER']['IP'];
					// foreach ($ips as $ip)
						// echo "IP: " . $ip . "<br>";
					// echo "Start Name: " . $lic['DATE']['HUMAN']['START'] . "<br>";
					// echo "End Name: " . $lic['DATE']['HUMAN']['END'] . "<br><br><br>";
					// echo "Result: " . $lic['RESULT'] . "<br>";
					
					// $data="";
					// $ip="178.250.215.130";
					// $ip="192.168.1.100";
					// $res = $padl->registerInstall("cogites.com", date('Y-m-d H:i:s'), "NEVER", $data, $ip, "root", '80');
					// print_r($res);
				?>
				
			</div>
		</div><!--/span-->

	</div><!--/row-->
    
<?php require "../_container/container_bottom.php"; ?>
<script>
$('#validatekey').click(function(){
	$('#licenseForm').attr("enctype", "");
	$('#action').val("validatekey");
	licenseForm.submit();
});
$('#generatekey').click(function(){
	$('#licenseForm').attr("enctype", "");
	$('#action').val("generatekey");
	licenseForm.submit();
});
$('#validatefile').click(function(){
	$('#licenseForm').attr("enctype", "multipart/form-data");
	$('#action').val("validatefile");
	licenseForm.submit();
});
</script>