<?php
include("functions.inc.php");
checkLogin();

if(intval($_GET['id']) > 0){

	dbConnect();
	$att_id = intval($_GET['id']);
	
	$result = $mysqli->query($q="
	SELECT * FROM attachments 
	LEFT JOIN anagrafiche ON attachments.anagr_id = anagrafiche.anagr_id
	LEFT JOIN attachments_name ON attachments.attach_id = attachments_name.attach_id
	WHERE
	attachments.att_id = ".$att_id." AND (
		anagrafiche.user_id = ".$_SESSION['user']['id']." OR
		( anagrafiche.ente_id = ".$_SESSION['user']['ente_id']." AND 'adm' = '".$_SESSION['user']['grp']."')
	)");
	
	if($result->num_rows == 1){
		
		$row = $result->fetch_assoc();
		$fname = ROOT.'/userfiles/allegati/'.$row['att_fname'];
		$ext = getExt($row['att_fname']);
		$show_name = $row['attach_shortname'].'-'.$row['attach_id'].'.'.$ext;
	}
}

if($_GET['module']){
	if(is_fname($_GET['module'])){
		$fname = ROOT.'/userfiles/moduli/'.$_GET['module'];
		$show_name = $_GET['module'];
	}
}





if(is_file($fname)){
	header("Content-disposition: attachment; filename=".$show_name);
	header("Content-type: application/octet-stream");
	readfile($fname);
	die();
}

$p = new Page('Home');
$p->addMenu('logged');
$p->printHeader();
?>
<h3 class="p-title">File non trovato</h3>
<div class="panel panel-default">
	<div class="panel-body">
		<?php printMsg("Il file cercato non esiste o &egrave; stato eliminato","warning",false); ?>
        <a class="btn btn-default" href="<?php echo $_SERVER['HTTP_REFERER'];?>">Torna indietro</a>
    </div>
</div>
<?php
$p->printFooter();
?>