<?php
/*
PiAnoS (Picture Annotation System), A fingerprint examiner training tool
Copyright (C) 2011-2012 University of Lausanne, Switzerland
https://ips-labs.unil.ch/pianos

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see http://www.gnu.org/licenses/.
*/


function handleError($errno, $errstr, $errfile, $errline, $errcontext)
{
	throw new Exception("PHP error in ".$errfile."(".$errline."): ".$errstr." (errno=".$errno.")");
}
set_error_handler('handleError');

function var_dump_str($var)
{
	ob_start();
	var_dump($var);
	return ob_get_clean();
}

try
{	
	$res = array('error'=>false, 'ret'=>NULL);

	$data = file_get_contents("php://input");
	file_put_contents('report_rawdata.json', $data);
	$data = json_decode($data, true);
	file_put_contents('mark.svg', $data['xml']['mark']);
	if ($data['type'] == 'comparison')
		file_put_contents('print.svg', $data['xml']['print']);
	file_put_contents('../scripts/run.bat', "@echo off\nreport.py -m ../php/mark.svg -M ../".$_GET['markfile']." -p ../php/print.svg -P ../".$_GET['printfile']);
	
	header("Content-Type: text/plain; charset=utf-8");
	echo json_encode($res);
	exit;
	
	throw new Exception(var_dump_str($data));
	
	if (count($_POST))
	{
		if (isset($_POST['attachment']))
		{
			$answer = 'toto';
			$filename = isset($_POST['filename']) && strlen($_POST['filename']) ? $_POST['filename'] : 'report.pdf';
			$filename = preg_replace('/[^\.\-_\w]/i', '$', $filename); // keep only alnum, dots, dashes and underscores
			throw new Exception("damnit");
			
			if ($_POST['attachment'] === "1")
				header('Content-Disposition: attachment; filename="'.$filename.'"');
			header('Content-Type: application/pdf');
			header('Content-Length: '.strlen($answer));
			setcookie("pianos4_report_downloading", 1);
			
			echo $answer;
		}
		else
		{
			$data = file_get_contents("php://input");
			
			$res['ret'] = $data;
			
			header("Content-Type: text/plain; charset=utf-8");
			echo json_encode($res);
		}
	}
	else
	{
		header("Content-Type: text/html; charset=utf-8");?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>		
		<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
		<title>PiAnoS 4 - Downloading report</title>
		<script type="text/javascript" src="/cdn/jquery/js/jquery-1.7.1.min.js"></script>
		<script type="text/javascript" src="../js/pianos4_common.js"></script>
		<script type="text/javascript" src="../js/pianos4.js"></script>
		<script type="text/javascript" src="../js/pianos4_report.js"></script>
	</head>
	<body>
		<script type="text/javascript">
			$(document).ready(function() {
				log('Download iframe starting');
				
				$('form').submit();
			});
		</script>
		<div>Your report is ready and is now being downloaded</div>
		<form action="<?php $_SERVER['REQUEST_URI'] ?>" method="post">
			<input type="text" name="attachment" value="1" />
			<input type="text" name="filename" value="my_pianos_report.pdf" />
		</form>
	</body>
</html>
		<?php
	}
}
catch (Exception $e)
{
	header("Content-Type: text/plain; charset=utf-8");
	$message = 	$e->getMessage()."\n".'exception \''.get_class($e).'\' in '.$e->getFile().':'.$e->getLine()."\nStack trace:\n".$e->getTraceAsString();
	echo json_encode(array('error'=>true, 'message'=>$message));
}

?>
