<?php
include('../init.php');
include('../libs/functions.php');

if (checklogin($userID, "../index.php")) {

	if (isset($_REQUEST['data_id']) && is_numeric($_REQUEST['data_id'])) { 
		$data_id = scrub_sql(scrub_white_list($_REQUEST['data_id'], 'NUMBERONLY'), 9);
		$message = 'Downloaded data file with id: ' . $data_id;

		$sql = 'Select file_name, file_contents from tb_response_file Where id = ';
		$sql .= "'$data_id' and user_id = '$userID'";
		
		ini_set ("odbc.defaultlrl", "5242880");
		$rs = $db->Execute($sql) or die ("<br>Select file failed");

		$file_name = $rs->fields('file_name');
		$contents = scrub_white_list($rs->fields('file_contents'),'DATA');
		
		header('Content-type: application/X-csv');
		header("Content-Disposition: attachment; filename=$file_name");
		header("Pragma: public");

		echo $contents;
		audit_log($userID, $message);

	} else {
		//error
		echo 'wrong data id';
	}
} else {
	//error	
	echo 'No permission';
}

?>



