<?php 
if (!isset($_SESSION['FirstName'])) {
	session_start();
}

$userid = $_SESSION['UserKey'];
if( isset($_SESSION['adminkey']) && $_SESSION['adminkey']=="security"){
 	$path = $_SERVER['DOCUMENT_ROOT'].'/custom/users/'.$_GET['id'].'/'.$_GET['file'];
	if( file_exists($path)) {
		header( 'Cache-Control: public' );
      	header( 'Content-Description: File Transfer' );
      	header( "Content-Disposition: attachment; filename={$_GET['file']}" );
      	header( 'Content-Transfer-Encoding: binary' );
      	readfile( $_GET['file'] );
      	exit;
	} else {
		die('Sorry this file no longer exists!');
	}
} else {
	if( isset($userid) && !is_null($userid) ){
		if( $userid == $_GET['id'] ){	
			$path = $_SERVER['DOCUMENT_ROOT'].'/custom/users/'.$_GET['id'].'/'.$_GET['file'];
			if( file_exists($path)) {
				header( 'Cache-Control: public' );
		      	header( 'Content-Description: File Transfer' );
		      	header( "Content-Disposition: attachment; filename={$_GET['file']}" );
		      	header( 'Content-Transfer-Encoding: binary' );
		      	readfile( $_GET['file'] );
		      	exit;
			} else {
				die('Sorry this file no longer exists!');
			}
		} else{
			die('Permission Denied');
		}
	} else {
		header('Location:my-account');
	}
}
?>