<?php

/*
#
# Author: Sebastian Enger, M.Sc.
# Date: 4/17/2016
# Website: www.OneTIPP.com
# Email: Sebastian.Enger@gmail.com
# Topic: Webschnittstelle -> gibt ankommenden Verarbeitungsclients Aufgaben - und Trainingspakete zum verarbeiten
# Version: 0.1.8 -> Added ForceUPDATE FileHash
#
Achtung: Wenn ein Task abgeholt wurde, muss der status noch auf "1"=assigned gesetzt werden
*/
ini_set("allow_url_fopen", true);
require_once("/home/www/tech.onetipp.net/library/Config.inc.php");
require_once("/home/www/tech.onetipp.net/library/Functions.inc.php");
require_once("/home/www/tech.onetipp.net/library/Connection.inc.php");
  
$actionTask	= $_REQUEST['action'];
$actionID 	= $_REQUEST['aid'];

$func	 	= new Functions();
$config	 	= new Config();
$conn 		= new Connection();

header("Content-Type: text/xml; charset=UTF-8");
header("Cache-Control: no-cache, must-revalidate"); //HTTP 1.1
header("Pragma: no-cache"); //HTTP 1.0
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

if ( $actionTask == 'statusTask' ){
//	echo "GetStatusTaskContent()";
	echo $func->GetStatusTaskContent($actionID);
	exit(0);
}

if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['aid_filename'])){
	$ua 			= $_SERVER['HTTP_USER_AGENT'];
	$HashActionID 	= $_POST['aid_hash'];// {HASHID}
	$Filename 		= $_POST['aid_filename']; // {HASHID}-{UNIXTIMESTAMP}.tar.bz2
	$FileHash 		= $_POST['aid_filehash']; // MD5_OF({HASHID}-{UNIXTIMESTAMP}.tar.bz2)
	//$suchmuster 	= '/(\w)-(\w)\.tar\.(bz2|gz)/i';
	
	$data 			= file_get_contents($_FILES['upload']['tmp_name']);
	
	// only if given input filehash is same with uploaded file md5
	if ($FileHash == md5($data)){
		$store_path 	= $config->data_storepath();
		$store_path_aid	= $store_path."/".$HashActionID;
		$store_file		= $store_path."/".$HashActionID."/".$Filename;
		
		if (!file_exists($store_path)) {
			mkdir($store_path, 0750, true);
		}
		if (!file_exists($store_path_aid)) {
			mkdir($store_path_aid, 0750, true);
		}
						
		file_put_contents("/var/tmp/debug.txt", "UA: $ua / PATH: $store_path / STORE_AID: $store_path_aid / FILE: $store_file / FILEHASH: $FileHash\n", FILE_APPEND);	
		file_put_contents($store_file, $data, LOCK_EX);
	
		// nach dem empfangen der gesendeten daten, den dateinamen (HASH-TIME().tar.bz2)
		// und den PFAD und die Server IP immer in der MySQL und MongoDB speichern
		
		$pdo 	= $conn->prepareQuery();
		$table 	= $config->sql_tablename();
		
		$stmt 	= $pdo->prepare("UPDATE `$table`   
			SET `local_resultfilename` 		= :local_resultfilename,
			   `local_resultpath` 			= :local_resultpath,
			   `local_resultfilename_md5` 	= :local_resultfilename_md5,
			   `status` 					= :status,
			   `done` 						= :done
			WHERE `hash` = :aid_hash"); 
	 
		$md5_file = md5_file($store_file);
		$stmt->bindValue(':local_resultfilename', $Filename, PDO::PARAM_STR);
		$stmt->bindValue(':local_resultpath', $store_file, PDO::PARAM_STR);
		$stmt->bindValue(':local_resultfilename_md5',$md5_file , PDO::PARAM_STR);
		$stmt->bindValue(':aid_hash', $HashActionID, PDO::PARAM_STR);
		$stmt->bindValue(':status', 1, PDO::PARAM_INT);
		$stmt->bindValue(':done', 1, PDO::PARAM_INT);
		$stmt->execute();
		
		file_put_contents("/tmp/pdo_mysql.txt", $stmt->errorInfo(), LOCK_EX);
		
	}
	exit(0);
} // if ($_SERVER['REQUEST_METHOD'] == 'POST'){

if (!isset($actionTask) || empty($actionTask)){
//	echo "GetIndexPageXML()";
	echo $func->GetIndexPageXML(array(),'IndexPage');
	exit(0);
}

if ($actionTask == 'getTask'){
//	echo "GetNewTaskContent()";
	echo $func->GetNewTaskContent();
	exit(0);
}
exit(0);