<?php
date_default_timezone_set('Europe/Rome');

//include($_SERVER['DOCUMENT_ROOT'] . "/library/php/webapp_language/language.php");
include_once(__DIR__ . '/licenseHandler/licenseHandler.php');
include_once(__DIR__ . '/licenseHandler/licenseUpdater.php');
include(__DIR__ . "/webapp_language/language.php");



function LicenseMessage($license_status){

	$output = "";

        $licenseKey = generateBitonliveKey(0, file_get_contents(__DIR__ . "/../../version.txt"));

				$licenseStatusText= "";

        if($license_status === 'missing_license') $licenseStatusText = 'Missing License';
        if($license_status === 'invalid_license') $licenseStatusText = 'Invalid License';
        if($license_status === 'license_expired') $licenseStatusText = 'Expired License';

        $output.="<div id='corruptmessage' style='width: 100%; z-index: 9999999; position: fixed; top: 0; left : 0; height: 100%; display: block; box-sizing: border-box; padding: 5vw;'>";

        $output.="<div style='width: 100%; font-size: 3vw; float: left; color: black; font-weight: bold; text-align: center '>{$licenseStatusText}</div>";
        $output.="<div style='width: 100%; float: left; color: black; font-size: 1vw; text-align: center'>".'Warning, you need a valid and active license in order to continue using this software'."</div>";
        $output.="<div style='width: 100%; float: left; height: 40%; box-sizing:border-box; padding: 2vw;'>
          <div style='width: 50%; height: 100%; float: left;'>
            <div style='width: 100%; height: 3vw; font-size: 1vw; color: black; text-align: center'>".'Your software Key'."</div>
            <textarea spellcheck='false' style='width: 100%; height: calc(100% - 3vw); font-size: 0.7vw; float: left; resize: none; color: black; background: none; box-sizing: border-box; padding: 1vw;'>".$licenseKey."</textarea>
          </div>
          <div style='width: 50%; height: 100%; float: left; display: flex; align-items: center; justify-content: center;  flex-direction: column'>
            <div style='width: 100%; height: 2vw; font-size: 1vw; color: black; text-align: center'>".'Download your Key'."</div>
            <button onclick='downloadLicenseKey()' style='width: 20vw; height: 2.5vw; cursor: pointer; font-size: 1vw; border-radius: 10vw; background: #a2a2a2; border: none; color: black; font-weight: bold; margin-top: 1vw;'>"."Download"."</button>
          </div>

        </div>

        <div style='width: 100%; height: 40%; float: left; box-sizing: border-box; display: flex; justify-content: center; align-items: center; flex-direction: column'>
          <div style='width: 100%; height: 2vw; font-size: 2vw; color: black; text-align: center; font-weight: bold'>".'Do you have your license?'."</div>
          <button onclick='$(\"#license-input\").trigger(\"click\")' style='width: 20vw; cursor: pointer; height: 2.5vw; border-radius: 10vw; background: #64d81b; border: none; color: black; font-weight: bold; font-size: 1vw; margin-top: 2vw;'>"."Upload File"."</button>
          <input type='file' accept='.bitonlive' style='display: none' onchange='submitLicenseFile(this)' id='license-input' />
    	</div>

    ";

    $output.="</div>";
	$output.=include(__DIR__ . '/../../libapp.php');
	$output.="<script>

	function submitLicenseFile(obj){

		let formdata = new FormData();
		formdata.append('license' , obj.files[0]);

		$.ajax({
		  type : 'post',
		  url:'webcore/get_content.php?registerLicense',
		  contentType: false,
		  processData: false,
		  data : formdata,

			success: function(data){

				if(data == 'invalid_license' || data == 'blocked_license') alert('Error: License is INVALID. Please, try again and select a valid License...');
				if(data == 'license_expired') alert('Error: License is EXPIRED. Please, try again and select a valid License...');
				if(data == 'valid_license') alert('Valid License Installed. Accept to restarting the software...');

			  location.reload();

			}

		  });


	  }

	  function downloadLicenseKey(){
		window.open('webcore/get_content.php?get2LicenseKey');
	  }


	</script>";

	return $output;

}



function setDataOraIT()
{
	setlocale(LC_TIME, 'it_IT.ISO_8859-1');
	$data = date("d-m-Y H:i:s");
	return ($data);
}

function setDataEstesa()
{
	setlocale(LC_TIME, 'it_IT.ISO_8859-1');
	$data = date("dmY");
	return ($data);
}

function setDataIT()
{
	setlocale(LC_TIME, 'it_IT.ISO_8859-1');
	$data = date("d-m-Y");
	return ($data);
}

function datediff($tipo, $partenza, $fine)
{
	//echo $tipo . " " . $partenza . " " . $fine;
	switch ($tipo) {
		case "A":
			$tipo = 365;
			break;
		case "M":
			$tipo = (365 / 12);
			break;
		case "S":
			$tipo = (365 / 52);
			break;
		case "G":
			$tipo = 1;
			break;
	}
	$arr_partenza = explode("-", $partenza);
	$partenza_gg = $arr_partenza[0];
	$partenza_mm = $arr_partenza[1];
	$partenza_aa = $arr_partenza[2];
	$arr_fine = explode("-", $fine);
	$fine_gg = $arr_fine[0];
	$fine_mm = $arr_fine[1];
	$fine_aa = $arr_fine[2];
	$date_diff = gmmktime(12, 0, 0, $fine_mm, $fine_gg, $fine_aa) - gmmktime(12, 0, 0, $partenza_mm, $partenza_gg, $partenza_aa);
	//$date_diff = mktime(1, 0, 0, $fine_mm, $fine_gg, $fine_aa) - mktime(1, 0, 0, $partenza_mm, $partenza_gg, $partenza_aa);
	$date_diff  = floor(($date_diff / 60 / 60 / 24) / $tipo);
	return $date_diff;
}

function formattaData($val)
{
	if (stripos($val, "-") > 0 || stripos($val, "/") > 0) {
		$g = strtok($val, "-/");
		$m = strtok("-/");
		$a = strtok("-/");
		$data = $a . $m . $g;
	} else {
		$g = substr($val, 0, 2);
		$m = substr($val, 2, 2);
		$a = substr($val, 4, 4);
		$data = $a . $m . $g;
	}
	return ($data);
}

function formattaData1($val)
{
	$a = substr($val, 0, 4);
	$m = substr($val, 4, 2);
	$g = substr($val, 6, 2);

	$data = $g . '-' . $m . '-' . $a;
	return ($data);
}

function formattaDataPerDB($val)
{
	if (stripos($val, "-") > 0 || stripos($val, "/") > 0) {
		$g = strtok($val, "-/");
		$m = strtok("-/");
		$a = strtok("-/");
		$data = $a . $m . $g;
	} else {
		$g = substr($val, 0, 2);
		$m = substr($val, 2, 2);
		$a = substr($val, 4, 4);
		$data = $a . '-' . $m . '-' . $g;
	}
	return ($data);
}

function formattaData2($val)
{
	$a = substr($val, 0, 4);
	$m = substr($val, 4, 2);
	$g = substr($val, 6, 2);

	$data = $a . '-' . $m . '-' . $g;
	return ($data);
}

function formattaDataOra($val)
{
	$data = '';
	if ($val != '') {
		$a = substr($val, 0, 4);
		$m = substr($val, 4, 2);
		$g = substr($val, 6, 2);

		$hh = substr($val, 9, 2);
		$mm = substr($val, 11, 2);
		$ss = substr($val, 13, 2);

		$data = $a . '-' . $m . '-' . $g . '*' . $hh . ':' . $mm . ':' . $ss;
		return ($data);
	}
}


function generaSpazi($quanti)
{
	$i = 0;
	$stringa = '';
	for ($i = 0; $i < $quanti; $i++) {
		$stringa .= ' ';
	}
	return ($stringa);
}


function formattaDurata($val)
{
	$hh = 0;
	$mm = 0;
	$ss = 0;

	if ($val <= 240000 && $val >= 100000) {
		$hh = substr($val, (strlen($val) - 6), strlen($val) - 4);
		$mm = substr($val, (strlen($val) - 4), (strlen($val) - 4));
		$ss = substr($val, (strlen($val) - 2), strlen($val));
	} else if ($val < 100000 && $val > 5900) {
		$hh = substr($val, (strlen($val) - 5), strlen($val) - 4);
		$mm = substr($val, (strlen($val) - 4), (strlen($val) - 3));
		$ss = substr($val, (strlen($val) - 2), strlen($val));
	} else if ($val <= 5900 && $val > 900) {
		$mm = substr($val, (strlen($val) - 4), (strlen($val) - 2));
		$ss = substr($val, (strlen($val) - 2), strlen($val));
	} else if ($val <= 900 && $val > 99) {
		$mm = substr($val, (strlen($val) - 3), (strlen($val) - 2));
		$ss = substr($val, (strlen($val) - 2), strlen($val));
	} else if ($val <= 59 && $val >= 10) {
		$ss = substr($val, (strlen($val) - 2), strlen($val));
	} else if ($val < 10 && $val > 0) {
		$ss = $val;
	}

	$hh = $hh * 60 * 60;
	$mm = $mm * 60;
	$durata = $hh + $mm + $ss;
	return ($durata);
}


function formatOraDaDB($val)
{
	$ss = '00';
	$mm = '00';
	$hh = '00';

	if ($val <= 240000 && $val >= 100000) {
		$hh = substr($val, (strlen($val) - 6), (strlen($val) - 4));
		$mm = substr($val, (strlen($val) - 4), (strlen($val) - 4));
		$ss = substr($val, (strlen($val) - 2), strlen($val));
	} else if ($val < 100000 && $val > 5900) {
		$hh = '0' . substr($val, (strlen($val) - 5), (strlen($val) - 4));
		$mm = substr($val, (strlen($val) - 4), (strlen($val) - 3));
		$ss = substr($val, (strlen($val) - 2), strlen($val));
	} else if ($val <= 5900 && $val > 900) {
		$hh = '00';
		$mm = substr($val, (strlen($val) - 4), (strlen($val) - 2));
		$ss = substr($val, (strlen($val) - 2), strlen($val));
	} else if ($val <= 900 && $val > 59) {
		$hh = '00';
		$mm = '0' . substr($val, (strlen($val) - 3), (strlen($val) - 2));
		$ss = substr($val, (strlen($val) - 2), strlen($val));
	} else if ($val <= 59 && $val >= 10) {
		$hh = '00';
		$mm = '00';
		$ss = substr($val, (strlen($val) - 2), strlen($val));
	} else if ($val < 10 && $val > 0) {
		$hh = '00';
		$mm = '00';
		$ss = '0' + $val;
	}
	$orario = $hh . ':' . $mm . ':' . $ss;
	return ($orario);
}

function formatTimeToDb($val)
{
	$time = 0;
	if ($val != '' && $val != '0') {
		$tmp = explode(":", $val);
		if (isset($tmp[2])) {
			$time = (($tmp[0] * 3600) + ($tmp[1] * 60) + $tmp[2]) * 1000;
		} else {
			$time = (($tmp[0] * 3600) + ($tmp[1] * 60)) * 1000;
		}
	} else {
		$time = 0;
	}
	return ($time);
}


function formatOra($val)
{
	$hh = substr($val, 0, 2);
	$mm = substr($val, 2, 2);
	$ss = substr($val, 4, 2);
	return ($hh . ':' . $mm . ':' . $ss);
}

function formatOraNoZeros($val)
{
	$hh = substr($val, 0, 2);
	$mm = substr($val, 2, 2);
	$ss = substr($val, 4, 2);

	$h = $m = '';
	$s = $ss;

	if ($hh < 10) $h = str_replace(0, '', $hh);
	else $h = $hh;


	if ($mm < 10 && $hh == 0) $m = str_replace(0, '', $mm);
	else $m = $mm;


	if ($mm == 0 && $hh == 0 && $ss < 10) $s = str_replace(0, '', $s);

	if ($h != '') $h = $h . ':';
	if ($m != '') $m = $m . ':';

	return $h . $m . $s;
}

function convertToUtf8($string)
{
	// Detect encoding using a list of possible encodings
	$encoding = mb_detect_encoding($string, ['UTF-8', 'ISO-8859-1', 'Windows-1252'], true);

	// Convert to UTF-8 if encoding is detected
	if ($encoding) {
		$string = mb_convert_encoding($string, 'UTF-8', $encoding);
	} else {
		// Fallback if detection fails
		$string = mb_convert_encoding($string, 'UTF-8', 'ISO-8859-1');
	}

	// Clean up the string using checkStringa function
	$string = checkStringa($string);

	// Use iconv to remove any non-UTF-8 characters
	$string = iconv('UTF-8', 'UTF-8//IGNORE', $string);

	// Replace any remaining non-printable characters
	$string = preg_replace('/[^\P{C}\n]+/u', '', $string);

	return $string;

}

function checkStringa($stringa)
{
	$stringa = str_replace("'", "`", $stringa);
	$stringa = str_replace("<", "&lt;", $stringa);
	$stringa = str_replace(">", "&gt;", $stringa);
	$stringa = str_replace("�", "", $stringa);
	$stringa = str_replace("à", "a", $stringa);
	$stringa = str_replace("á", "a", $stringa);
	$stringa = str_replace("â", "a", $stringa);
	$stringa = str_replace("ä", "a", $stringa);
	$stringa = str_replace("ã", "a", $stringa);
	$stringa = str_replace("å", "a", $stringa);
	$stringa = str_replace("ā", "a", $stringa);
	$stringa = str_replace("è", "e", $stringa);
	$stringa = str_replace("é", "e", $stringa);
	$stringa = str_replace("ê", "e", $stringa);
	$stringa = str_replace("ë", "e", $stringa);
	$stringa = str_replace("ē", "e", $stringa);
	$stringa = str_replace("ė", "e", $stringa);
	$stringa = str_replace("ę", "e", $stringa);
	$stringa = str_replace("ì", "i", $stringa);
	$stringa = str_replace("í", "i", $stringa);
	$stringa = str_replace("î", "i", $stringa);
	$stringa = str_replace("ï", "i", $stringa);
	$stringa = str_replace("ī", "i", $stringa);
	$stringa = str_replace("ò", "o", $stringa);
	$stringa = str_replace("ó", "o", $stringa);
	$stringa = str_replace("ô", "o", $stringa);
	$stringa = str_replace("ö", "o", $stringa);
	$stringa = str_replace("õ", "o", $stringa);
	$stringa = str_replace("ø", "o", $stringa);
	$stringa = str_replace("ō", "o", $stringa);
	$stringa = str_replace("ù", "u", $stringa);
	$stringa = str_replace("ú", "u", $stringa);
	$stringa = str_replace("û", "u", $stringa);
	$stringa = str_replace("ü", "u", $stringa);
	$stringa = str_replace("ū", "u", $stringa);
	$stringa = str_replace("ý", "y", $stringa);
	$stringa = str_replace("ÿ", "y", $stringa);
	$stringa = str_replace("ñ", "n", $stringa);
	$stringa = str_replace("ç", "c", $stringa);
	$stringa = str_replace("ß", "ss", $stringa);
	$stringa = str_replace("œ", "oe", $stringa);
	$stringa = str_replace("æ", "ae", $stringa);
	$stringa = str_replace("°", "", $stringa);
	$stringa = str_replace("™", "", $stringa);
	$stringa = str_replace("©", "", $stringa);
	$stringa = str_replace("®", "", $stringa);
	/*
	$stringa=str_replace("�","A",$stringa);
	$stringa=str_replace("�","E",$stringa);
	$stringa=str_replace("�","I",$stringa);
	$stringa=str_replace("�","O",$stringa);
	$stringa=str_replace("�","U",$stringa);
	$stringa=str_replace("�","A",$stringa);
	$stringa=str_replace("�","E",$stringa);
	$stringa=str_replace("�","I",$stringa);
	$stringa=str_replace("�","O",$stringa);
	$stringa=str_replace("�","U",$stringa);
	$stringa=str_replace("�","a",$stringa);
	$stringa=str_replace("�","e",$stringa);
	$stringa=str_replace("�","i",$stringa);
	$stringa=str_replace("�","o",$stringa);
	$stringa=str_replace("�","u",$stringa);
	$stringa=str_replace("�","a",$stringa);
	$stringa=str_replace("�","e",$stringa);
	$stringa=str_replace("�","i",$stringa);
	$stringa=str_replace("�","o",$stringa);
	$stringa=str_replace("�","u",$stringa);
	$stringa=str_replace("�","e",$stringa);
	$stringa=str_replace("�","O",$stringa);
	*/
	return ($stringa);
}


function formatMMSS($durata)
{
	$tmp = $durata / 60;

	if (strpos($tmp, ".") > 0) $minuti	 = substr($tmp, 0, strpos($tmp, "."));
	else $minuti = $tmp;
	if ($minuti < 10) $minuti = '0' . $minuti;

	$tmp = $durata  - ($minuti * 60);
	if (strpos($tmp, ".") > 0) $secondi = substr($tmp, 0, strpos($tmp, "."));
	else $secondi = $tmp;

	if ($secondi < 10) $durata = "00:" . $minuti . ":0" . $secondi;
	else  $durata = "00:" . $minuti . ":" . $secondi;

	return ($durata);
}


function formatHHMMSS($durata)
{
	$tmp = $durata / 3600;
	if (strpos($tmp, ".") > 0) $ore	 = substr($tmp, 0, strpos($tmp, "."));
	else $ore = $tmp;
	if ($ore < 10) $ore = '0' . $ore;

	$tmp = (($durata - ($ore * 3600)) / 60);
	if (strpos($tmp, ".") > 0) $minuti	 = substr($tmp, 0, strpos($tmp, "."));
	else $minuti = $tmp;
	if ($minuti < 10) $minuti = '0' . $minuti;

	$tmp = $durata - ($ore * 3600) - ($minuti * 60);
	if (strpos($tmp, ".") > 0) $secondi = substr($tmp, 0, strpos($tmp, "."));
	else $secondi = $tmp;

	if ($secondi < 10) $durata = $ore . ':' . $minuti . ':0' . $secondi;
	else  $durata = $ore . ':' . $minuti . ':' . $secondi;

	return ($durata);
}


function formatHHMMSSNoZeros($durata)
{


	$tmp = $durata / 3600;

	if (strpos($tmp, ".") > 0) $ore = substr($tmp, 0, strpos($tmp, "."));
	else $ore = $tmp;
	if ($ore < 10) $ore = '0' . $ore;

	$tmp = (($durata - ($ore * 3600)) / 60);

	if (strpos($tmp, ".") > 0) $minuti     = substr($tmp, 0, strpos($tmp, "."));
	else $minuti = $tmp;
	if ($minuti < 10) $minuti = '0' . $minuti;

	$tmp = $durata - ($ore * 3600) - ($minuti * 60);

	if (strpos($tmp, ".") > 0) $secondi = substr($tmp, 0, strpos($tmp, "."));
	else $secondi = $tmp;
	if ($secondi < 10) $secondi = '0' . $secondi;

	$hh = "";
	$mm = "";
	$ss = $secondi;

	/*if ($ore > 0) {
		if ($ore < 10) $hh =  str_replace(0, "", $ore) . ":";
		else $hh = $ore . ":";
	}

	if ($minuti > 0) {
		if ($minuti < 10 && $ore <= 0) $mm =  str_replace(0, "", $minuti) . ":";
		else  $mm = $minuti . ":";

		if ($secondi < 10) $ss = '0' . $secondi;
		else  $ss = $secondi;
	} else{
		if($ore > 0 ) {
			$mm = $minuti . ':';

			if ($secondi < 10) $ss = '0' . $secondi;
			else  $ss = $secondi;
		}
	}*/

	if ($ore < 10) $hh = str_replace(0, '', $ore);
	else $hh = $ore;

	if ($minuti < 10 && $ore == 0) $mm = str_replace(0, '', $minuti);
	else $mm = $minuti;

	if ($minuti == 0 && $ore == 0) {
		if($secondi < 10) $ss = str_replace(0, '', $secondi);
		else if($secondi == '00') $ss = 0;
	}

	if ($hh != "") $hh = $hh . ':';
	if ($mm != "") $mm = $mm . ':';

	$durata = $hh . $mm  . $ss;

	return ($durata);
}


function formatMsecond($val)
{
	$tmp = explode(":", $val);
	$time = (((($tmp[0] * 60) * 60) + ($tmp[1] * 60) + ($tmp[2])) * 1000);
	return ($time);
}

function formatFromHHMMSS($val)
{
	$ora = 0;
	switch (strlen($val)) {
		case 1:
			$ora = $val;
			break;
		case 2:
			$ora = $val;
			break;
		case 3:
			$mm = substr($val, 0, 1);
			$ss = substr($val, 1, 2);
			$ora = ($mm * 60) + $ss;

			break;
		case 4:
			$mm = substr($val, 0, 2);
			$ss = substr($val, 2, 2);
			$ora = ($mm * 60) + $ss;
			break;
		case 5:
			$hh = substr($val, 0, 1);
			$mm = substr($val, 1, 2);
			$ss = substr($val, 3, 2);
			$ora = ($hh * 3600) + ($mm * 60) + $ss;
			break;
		case 6:
			$hh = substr($val, 0, 2);
			$mm = substr($val, 2, 2);
			$ss = substr($val, 4, 2);
			$ora = ($hh * 3600) + ($mm * 60) + $ss;
			break;
	}
	return ($ora);
}

function recursive_remove_directory($directory, $empty = FALSE)
{
	if (substr($directory, -1) == '/') {
		$directory = substr($directory, 0, -1);
	}
	if (!file_exists($directory) || !is_dir($directory)) {
		return FALSE;
	} elseif (is_readable($directory)) {
		$handle = opendir($directory);
		while (FALSE !== ($item = readdir($handle))) {
			if ($item != '.' && $item != '..') {
				$path = $directory . '/' . $item;
				if (is_dir($path)) {
					recursive_remove_directory($path);
				} else {
					unlink($path);
				}
			}
		}
		closedir($handle);
		if ($empty == FALSE) {
			if (!rmdir($directory)) {
				return FALSE;
			}
		}
	}
	return TRUE;
}


function cropImg($img, $filename)
{
	$d = 120;
	$image = imagecreatefromjpeg($img);

	$width = imagesx($image);
	$height = imagesy($image);

	if ($width <= $height && $width > $d) {
		$n_height = ($d * $height) / $width;
		$n_width = $d;
	} else {
		$n_width = ($d * $width) / $height;
		$n_height = $d;
	}

	$thumb = imagecreatetruecolor($d, $d);
	imagecopyresampled(
		$thumb,
		$image,
		0 - ($n_width - $d) / 2, // Center the image horizontally
		//0 - ($n_height - $d) / 2, // Center the image vertically
		0,
		0,
		0,
		$n_width,
		$n_height,
		$width,
		$height
	);
	imagejpeg($thumb, $filename, 80);
}

/*
function add_column_if_not_exist($db, $column, $column_attr)
{
	$exists = false;
	$columns = mysqli_query("show columns from $db");
	while ($c = mysqli_fetch_assoc($columns)) {
		if ($c['Field'] == $column) {
			$exists = true;
			break;
		}
	}
	if (!$exists) {
		mysqli_query("ALTER TABLE `$db` ADD `$column`  $column_attr");
	}
}
*/
//
function getPeriodo($type)
{
	$today = getdate();
	$periodo = array();

	if ($type == 1) {
		$d = 31;
		$y = $today['year'];

		if ($today['mon'] > 1) {
			$m = $today['mon'] - 1;
		} else {
			$m = 1;
			$y = $y - 1;
		}

		if ($m == 2) {
			if (checkdate(2, 29, $y)) $d = 29;
			else $d = 28;
		} else if ($m == 4 || $m == 6 || $m == 9 || $m == 11) {
			$d = 30;
		}

		if ($m < 10) $m = '0' . $m;

		$periodo[0] = '01-' . $m . '-' . $y;
		$periodo[1] = $d . '-' . $m . '-' . $y;
	} else if ($type == 2) {
		$periodo[0] = date("d-m-Y", mktime(0, 0, 0, date("m"), date("d") - 1, date("Y")));
		$periodo[1] = date("d-m-Y", mktime(0, 0, 0, date("m"), date("d") - 1, date("Y")));
	} else if ($type == 0) {
		$periodo[0] = date("d-m-Y");
		$periodo[1] = date("d-m-Y");
	}
	return $periodo;
}

function checkPath($path)
{
	$path = str_replace("\\", "/", $path);
	if (substr($path, (strlen($path) - 1)) == "/") $path = substr($path, 0, (strlen($path) - 1));
	$path = str_replace("////", "//", $path);
	return $path;
}



function infoFilesDateTime($filename)
{
	$dataora = '';
	if (file_exists($filename)) {
		$dataora = date("Y-m-d H:i:s", filemtime($filename));
	}
	return $dataora;
}

function infoFilesSize($filename)
{
	$size = 0;
	if (file_exists($filename)) {
		$size = filesize($filename); // in bytes
	}
	return $size;
}


function resizeimage($inputimg, $format, $w, $h, $outimg) {

	if ($format == "jpg" || $format == "jpeg") {
		//JPEG
		$resource_org = imagecreatefromjpeg($inputimg);
		$scaled = imagescale($resource_org, $w, $h);
		imagejpeg($scaled, $outimg);
	} elseif ($format == "png") {
		//PNG
		$resource_org = imagecreatefrompng($inputimg);
		$scaled = imagescale($resource_org, $w, $h);
		imagepng($scaled, $outimg);
	} elseif ($format == "gif") {
		//GIF
		$resource_org = imagecreatefromgif($inputimg);
		$scaled = imagescale($resource_org, $w, $h);
		imagegif($scaled, $outimg);
	} elseif ($format == "wbmp") {
		//WBMP
		$resource_org = imagecreatefromwbmp($inputimg);
		$scaled = imagescale($resource_org, $w, $h);
		imagewbmp($scaled, $outimg);
	} else {
		//DEFAULT
		echo "Not Supported File";
		exit();
	}
}


// sum time
function AddTime($times)
{
	$all_seconds = 0;
	// loop throught all the times
	foreach ($times as $time) {

		if ($time == "") continue;

		list($hour, $minute, $second) = explode(':', $time);
		$all_seconds += $hour * 3600;
		$all_seconds += $minute * 60;
		$all_seconds += $second;
	}

	$total_minutes = floor($all_seconds / 60);
	$seconds = $all_seconds % 60;
	$hours = floor($total_minutes / 60);
	$minutes = $total_minutes % 60;

	// returns the time already formatted
	return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}


// differenza tra due orari
function diffTime($time1, $time2)
{
	$d1 = new DateTime($time1);
	$d2 = new DateTime($time2);
	$interval = $d1->diff($d2);

	$hours = $interval->format('%h');
	$minutes = $interval->format('%i');
	$seconds = $interval->format('%s');

	return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}


// generazione log
function logThis($folder, $string)
{

	$logpath = __DIR__ . "/../../Log/";

	if (!file_exists($logpath . $folder  . "/" . date('Y'))) // verifico se esiste la cartella dell'anno corrente
		mkdir($logpath . $folder  . "/" . date('Y'), 0777, true);

	if (!file_exists($logpath . $folder  . "/" . date('Y') . "/" . date('Ymd') . ".txt")) // verifico se esiste già il file di log del giorno altrimenti lo creo vuoto
		file_put_contents($logpath . $folder  . "/" . date('Y') . "/" . date('Ymd') . ".txt", "");

	$log = fopen($logpath . $folder  . "/" . date('Y') . "/" . date('Ymd') . ".txt", "a");
	@stream_set_timeout($log, 0, 5000);
	if(!empty($string)) @fwrite($log, PHP_EOL . "[" . date('H:i:s') . "] : " . $string);
	else  @fwrite($log, PHP_EOL);
	@fclose($log);

}