<?php

require_once(__DIR__ . "/../config/connect.php");
//require_once(__DIR__ . "/../global_functions.php");
ini_set('memory_limit', '-1');
set_time_limit(6000);
$debug_log=true;

function run_mysqldump_command()
{
	global $debug_log;
	ini_set('max_execution_time', 0);
	if ($debug_log) log_to_file("Backup wird gestartet", false, true);
	$filename = __dir__ . "\\uploads\\backup_".SQL_DB."_".date('Ymd_His').".sql";
	$command = "C:\\xampp\\mysql\\bin\\mysqldump -u " . SQL_USER . " -p" . SQL_PW . " " . SQL_DB . " --add-drop-database >" . $filename;
	$output = array();
	exec($command, $output, $error_code);
	if ($error_code) {
		log_to_file("Error Code - " . $error_code . ", beim Backup ist ein Fehler Aufgetreten !", false, true);
		exit;		
	} else 
	{
		echo "$filename - Backup wurde ausgefuehrt";
		if ($debug_log) log_to_file("Backup wurde erfolgreich beendet: $filename",false, true);
		return($filename);
	}
	return(false);
}

function sendFile_sync($files, $token) {
	global $debug_log;

	try
	{
		log_to_file("------> SYNC DATA TO WaWi : sendFile_sync");
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, FILIALSYSTEM_URL_ZENTRALSYSTEM . "/synchronize/sync_mysqldump.php");
		//curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch,CURLOPT_HEADER,false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		
		$files['kassa'] = FILIALSYSTEM_KASSA_NAME;
		$files['token'] = $token;
		$files['kassa_token'] = FILIALSYSTEM_TOKEN;
		
		
		log_to_file("kassa: ".FILIALSYSTEM_KASSA_NAME);
		log_to_file("token: ".$token);
		log_to_file("kassa_token: ".FILIALSYSTEM_TOKEN);
		
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_FAILONERROR, true);
		
		
		log_to_file("curl_exec");
		$result = curl_exec($ch);
		var_dump($result);
		log_to_file("ENDE curl_exec");

		if ($result===false) 
		{
			log_to_file("CURL Fehler :". curl_error($ch)."<br>");
			echo "CURL Fehler :". curl_error($ch)."<br>";
		}
		else
		{

			$resultStr = substr($result, strpos($result,"{"));
			//log_to_file("vorher -->".$resultStr."<--");
			var_dump($resultStr);
			exit;

			$result_arr = json_decode($resultStr,true);
			//log_to_file("-->".$result."<--");
			if ( empty($resultStr) )
			{
				echo "Error: Return von Server ist falsch :".$result."\n";
				log_to_file("Error: ".$result);
				exit;
			}
			else
			{
				if ($debug_log) log_to_file("-->".$resultStr."<--");
			}
			$table_ausgabe = null;
		}
	}
	catch(Exception $e)
	{
		log_to_file("Exeption: ".$e);
		echo "Exeption: ".$e;
	}
	finally
	{
		log_to_file("<-------- SYNC DATA TO WaWi : end sendFile_sync");
		curl_close($ch);
		return(true);
	}
}


function upload_file($filename)
{
	$rand = substr(md5(microtime()),rand(0,26),5);
	$token = FILIALSYSTEM_KASSA_NAME . "_" . $rand . "_" . date(DATE_ATOM);

	$files['file[0]'] = new CurlFile(realpath($filename));
	$rc=sendFile_sync($files, $token);
	if ($rc===false) $anzahl_fehler=$anzahl_fehler+1;
	//delete_files($filenames);
}

/*
echo "run_mysqldump_command";
$filename=run_mysqldump_command();
echo $filename;
//$filename = __dir__ . "\\uploads\\backup_sitpos_20230930_223012.sql";
upload_file($filename);
exit;

run_backup_command();
exit;
*/

?>
