<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if (isset($_GET['command'])) 
{
	switch ($_GET['command']) 
	{
		case 'list':
		
			if (isset($_GET['older'])) 
			{
				$fp = fopen("hr_version_statistic.csv", 'a');
				$chaine = date('Y-m-d H:i:s') . ";UPDATE LIST;" . $_GET['older'] . ";" . $_GET['id'] . "\n";
				fputs($fp, $chaine);
				fclose($fp);
			}
		
			$versionsList = getList();
			foreach ($versionsList as $version) 
			{
				echo $version . "\n";
			}
			
			break;
			
		case 'hotnews':
			if (isset($_GET['older'])) 
			{
				$fp = fopen("hr_version_statistic.csv", 'a');
				$chaine = date('Y-m-d H:i:s') . ";CHECK NEWS;" . $_GET['older'] . ";" . $_GET['id'] . "\n";
				fputs($fp, $chaine);
				fclose($fp);
			}
			
			// Recuperer le texte
			$contenu = iconv('utf-8','ASCII//IGNORE//TRANSLIT', file_get_contents( "releases/private/Releases.txt" ));
			
			// Couper a la derniere release
			$contenu = substr($contenu, 0, stripos($contenu, "--------------------------------"));
			
			// Et l'afficher
			echo "$contenu";
			break;
			
		case 'news':
			if (isset($_GET['older'])) 
			{
				$fp = fopen("version_statistic.csv", 'a');
				$chaine = date('Y-m-d H:i:s') . ";CHECK NEWS;" . $_GET['older'] . ";" . $_GET['id'] . "\n";
				fputs($fp, $chaine);
				fclose($fp);
			}
			
			// Recuperer le texte
			$contenu = iconv('utf-8','ASCII//IGNORE//TRANSLIT', file_get_contents( "releases/public/Releases.txt" ));
			
			// Couper a la derniere release
			$contenu = substr($contenu, 0, stripos($contenu, "--------------------------------"));
			
			// Et l'afficher
			echo "$contenu";
			break;

		case 'update':
			if (isset($_GET['older'])) 
			{
				$fp = fopen("version_statistic.csv", 'a');
				$chaine = date('Y-m-d H:i:s') . ";CHECK UPDATE;" . $_GET['older'] . ";" . $_GET['id'] . "\n";
				fputs($fp, $chaine);
				fclose($fp);
			}
			
			$version = isset($_GET['releases/public']) ? $_GET['releases/public'] : null;
			$v = getVersions($version,false);
			echo getDepositUrl(false) . $v;
			break;

		case 'hotupdate':
			if (isset($_GET['older'])) 
			{
				$fp = fopen("hr_version_statistic.csv", 'a');
				$chaine = date('Y-m-d H:i:s') . ";CHECK UPDATE;" . $_GET['older'] . ";" . $_GET['id'] . "\n";
				fputs($fp, $chaine);
				fclose($fp);
			}
			
			$version = isset($_GET['releases/private']) ? $_GET['releases/private'] : null;
			$v = getVersions($version,true);
			echo getDepositUrl(true) . $v;
			break;
	}
} 
else 
{
	echo "DEPOSIT SERVER<br/><br/>";

	echo "[FR] Vous devez renseignez une commande GET parmi les commandes suivantes : 'list' et 'update' et vous pouvez spécifier en second paramètre si vous choissieez update, le paramètre version avec le numéro de version désiré.<br/>";
	echo "[EN] You must use 'GET' command of the following command : 'list' or 'update' and you can optionnaly add the version number.<br/><br/>";

	echo "eg : update.php?command=update<br/>";

}

function getList($update = false,$hotrelease)
{
	if($hotrelease == true)
		$versionsHtmlContent = file_get_contents('releases/hot_updates_list.txt');
	else
		$versionsHtmlContent = file_get_contents('releases/updates_list.txt');
	
	$versions = explode(',', $versionsHtmlContent);
	$v = [];
	
	foreach ($versions as $version) 
	{
		$version = explode(':', $version);
		$v[intval($version[0])] = $update ? $version[1] : str_replace('.ZIP', '', $version[1]);
	}
	return $v;
}


function getVersions($param_version = null, $hotrelease)
{
	$v = getList(true, $hotrelease);
	if (!is_null($param_version)) 
		return $v[intval($param_version)]; // test : deposit.cpcdos.net/update.php?command=update&version=2019072521
	else 
		return end($v); // test : deposit.cpcdos.net/update.php?command=update
}

function getDepositUrl($hotrelease)
{
	if (isset($_SERVER['HTTPS'])) {
		$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
	} else {
		$protocol = 'http';
	}
	if($hotrelease == false)
		return $protocol . "://" . $_SERVER['HTTP_HOST'] . '/releases/public/';
	else
		return $protocol . "://" . $_SERVER['HTTP_HOST'] . '/releases/private/';
}




