<?php
session_start();

if (isset($_SESSION["user_id"]) and isset($_SESSION["password"])) {
	?>
	<!DOCTYPE html>
	<html lang="zxx">
	<head>
		<title>SuperCryptEX | Send File</title>
		<meta charset="UTF-8">
		<meta name="description" content="SolMusic HTML Template">
		<meta name="keywords" content="music, html">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">

		<!-- Favicon -->
		<link href="img/favicon.ico" rel="shortcut icon"/>

		<!-- Google font -->
		<link href="https://fonts.googleapis.com/css?family=Montserrat:300,300i,400,400i,500,500i,600,600i,700,700i&display=swap" rel="stylesheet">

		<!-- Stylesheets -->
		<link rel="stylesheet" href="css/bootstrap.min.css"/>
		<link rel="stylesheet" href="css/font-awesome.min.css"/>
		<link rel="stylesheet" href="css/owl.carousel.min.css"/>
		<link rel="stylesheet" href="css/slicknav.min.css"/>

		<!-- Main Stylesheets -->
		<link rel="stylesheet" href="css/style.css"/>

	<!--[if lt IE 9]>
		<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
		<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
	<![endif]-->

	<!--- Smooth Scrolling --->
	<style>html { scroll-behavior: smooth; }</style>
</head>
<body onload="checkSessionParams(false)">
	<?php
	if (isset($_SESSION['message']) && $_SESSION['message'])
	{
      //echo '<p class="notification">'.$_SESSION['message'].'</p>';
		echo '<p class="notification">'.$_SESSION['message'].'</p>';
		echo '<script>toggle("#sendfile_encrypted","#sendfile_download");window.location.href = "#sendfile_encrypted";</script>';
		unset($_SESSION['message']);
	}
	?>
	<iframe id="download_iframe" style="display:none;"></iframe>
	<!-- Page Preloder -->
	<div id="preloder">
		<div class="loader"></div>
	</div>

	<!-- Header section -->
	<header class="header-section clearfix" id="home">
		<a href="#" class="site-logo">
			<h4 style="color: #fc0254;">SuperCrypt <span style="color: white;">EX</span></h4>
			<!---<img src="img/logo.png" alt="">--->
		</a>
		<div class="header-right">
			<a href="#" class="hr-btn">Help</a>
			<span>|</span>
			<div class="user-panel">
				<a href="./destroy_session.php" class="login">Logout</a>
				<a href="" class="register"><?= $_SESSION["fullname"]; ?></a>
			</div> 
		</div>
		<ul class="main-menu">
			<li><a href="./main.php#home">Home</a></li>
			<li><a href="./todo.php#">To-Do</a></li>
			<li><a href="./sendfile.php#">File Transfer</a></li>
			<li><a href="./inbox.php#">Inbox</a></li>
			<li><a href="./contact.php#">Contact Us</a></li>
		</ul>
	</header>
	<!-- Header section end -->

	<!-- Subscription section -->
	<section class="subscription-section spad" id="main">
		<div class="container">
			<div class="row">
				<div class="col-lg-6">
					<div class="sub-text">
						<h2>Send a File</h2>
						<h3>Securely Send Your File to Another Person</h3>
						<p>What is File Encryption?<br>
							<strong>File Encryption</strong><br>
							File encryption protects individual files or file systems by encrypting them with a specific key, making them accessible only to the keyholder. <br><br>The goal is to prevent malicious or unauthorized parties from accessing files that are stored on the disk. A decryption key allows access to the sensitive files. File encryption is helpful if a user needs to send individual files securely over the internet or store them on a removable device such as a USB stick. - <i><b>Webopedia, 2021</b></i></p>
							<div style="color: white;">
								<a onclick="toggle('#sendfile_recipient','#card_parent', '#divider')" class="site-btn" style="background-color:#17A2B8;margin-right: 10px;">Send File</a>
							</div>
						</div>
					</div>
					<div class="col-lg-6">

						<img src="img/todo/todo.png" style="background-size: fill;">

					</div>
				</div>

				<hr style="border-color: white;" id="divider">

				<div class="row justify-content-md-center">
					<div id="show_hide_container">
						<button class="btn pull-right cardz" onclick="hs_toggle()" id="show_hide" style="background-color: #343A40;color: white;"><span class="fa fa-arrow-circle-down"></span></button>
					</div>
				</div>
				<br>

				<!--- Parent Card --->
				<div class="row" id="card_parent" style="display: none;">

				</div>
				<!--- End ParentCard --->

				<!--- [1 OF 3] FILE SEND - SELECT RECIPIENT --->
				<div id="sendfile_recipient" style="display: none;">
					<ul class="sub-list" id="card_border">

						<div class="contact-form">
							<div class="sub-text">
								<h3>[ 1 ] Select Who will Receive the File :</h3>
							</div>
							<!--- Select Recipient --->
							<li><h5 style="color: white;"><div class="fa fa-address-card" style="margin-right: 15px;"></div>Send To</h5>
								<div class="form-group">

								</div></li>

								<div class="contact-from justify-content-center">
									<div class="row">
										<div class="col-md-8">
											<input type="text" placeholder="Please Input the Username" id="input_username">
										</div>
										<div class="col text-right">
											<button class="site-btn" id="search_username" onclick="getUser()">Search Username</button>
										</div>
									</div>
								</div>

								<div id="found_user" class="my-5"></div>

							</div>


						</ul>
					</div>
					<!--- End [1 OF 3] FILE SEND - SELECT RECIPIENT --->

									<!--- [2 OF 3] FILE SEND - DOWNLOAD ENCRYPTOR --->
									<div id="sendfile_download" style="display: none;">
										<ul class="sub-list" id="card_border">

											<div class="contact-form">
												<div class="sub-text">
													<h3>[ 2 ] Download These Required Files :</h3>
												</div>
												<!--- Download Encryptor --->
												<li><h5 style="color: white;"><div class="fa fa-download" style="margin-right: 15px;margin-bottom: 15px;"></div>1. Download Our Encryption Program</h5>
													<div class="form-group">
														<button class="btn btn-warning text-white" onclick="DownloadFile('./[Downloads]/[Program]/SuperCryptEX.zip')">Download Encryptor</button>
													</div></li>

													<!--- Download Your Private Key --->
													<li><h5 style="color: white;"><div class="fa fa-download" style="margin-right: 15px;margin-bottom: 15px;"></div>2. Download Your Host Private Key</h5>
														<div class="form-group">
															<button class="btn btn-warning text-white" onclick="DownloadKey('<?php echo $_SESSION["name"] ?>_HostPrivateKey.mx_hpriv', '<?php echo $_SESSION["host_private"] ?>')">Download Private Key [Host]</button>
														</div></li>

														<!--- Download Recipient Public Key --->
														<li><h5 style="color: white;"><div class="fa fa-download" style="margin-right: 15px;margin-bottom: 15px;"></div>3. Download Receiver's Client Public Key</h5>
															<div class="form-group">
																<button class="btn btn-warning text-white" onclick="DownloadKey(GLOBAL_RECIPIENT_NAME + '_ClientPublicKey.mx_cpub',GLOBAL_RECIPIENT_PUBLIC_KEY)">Download Public Key [Client]</button>
															</div></li>

															<div id="reg_form" class="text-right">
																<button class="site-btn" onclick="toggle('#sendfile_recipient','#sendfile_download')" style="background-color: #dc3545;">Back</button>
																<button class="site-btn" onclick="toggle('#sendfile_encrypted','#sendfile_download')" style="background-color: #28a745;">Next</button>
															</div>

														</div>


													</ul>
												</div>
												<!--- End [2 OF 3] FILE SEND - DOWNLOAD ENCRYPTOR --->

												<!--- [3 OF 3] FILE SEND - SEND ENCRYPTED FILE --->
												<div id="sendfile_encrypted" style="display: none;">
													<ul class="sub-list" id="card_border">

														<form class="contact-form" method="POST" action="upload.php" enctype="multipart/form-data">
															<div class="sub-text">
																<h3>[ 3 ] Send Your Encrypted File :</h3>
															</div>
															<!--- Upload Encrypted File --->
															<li><h5 style="color: white;"><div class="fa fa-download" style="margin-right: 15px;margin-bottom: 15px;"></div>1. Upload Your Encrypted File</h5>
																<div class="form-group">

																	<!-- Hidden Variables to POST : -->
																	<input type="hidden" class="form-control form-control-user" name="receiver_id" id="receiver_id" required>
																	<input type="hidden" class="form-control form-control-user" name="receiver_name" id="receiver_name" required>
																	<input type="hidden" class="form-control form-control-user" name="receiver_fullname" id="receiver_fullname" required>

																	<input type="file" class="form-control form-control-user" name="encrypted_file" id="encrypted_file" placeholder="Encrypted File" accept=".mx_encrypted">
																	<!-- <input type="file" class="form-control form-control-user" name="encrypted_file" id="encrypted_file" placeholder="Encrypted File"> -->
																	<br><br>
																	<!-- <button class="btn btn-warning text-white" onclick="UploadEncryptedFile()">Upload File</button> -->
																</div></li>

																<div id="reg_form" class="text-right">
																	<button class="site-btn" onclick="toggle('#sendfile_download','#sendfile_encrypted')" style="background-color: #dc3545;">Back</button>
																	<button type="submit" class="site-btn" onclick="" style="background-color: #28a745;">Send!</button>
																</div>

															</form>


														</ul>
													</div>
													<!--- End [3 OF 3] FILE SEND - SEND ENCRYPTED FILE --->

												</div>

											</section>
											<!-- Subscription section end -->


											<!-- Footer section -->
											<footer class="footer-section">
												<div class="container">
													<div class="row">
														<div class="col-xl-6 col-lg-7 order-lg-2">
															<div class="row">
																<div class="col-sm-4">
																	<div class="footer-widget">
																		<h2>Services</h2>
																		<ul>
																			<li><a href="./todo.php#">To-Do List</a></li>
																			<li><a href="./sendfile.php#">File Transfer</a></li>
																			<li><a href="./inbox.php#">Inbox</a></li>
																		</ul>
																	</div>
																</div>
																<div class="col-sm-4">
																	<div class="footer-widget">
																		<h2>About us</h2>
																		<ul>
																			<li><a href="./admins.php#">IPFlare OP's</a></li>
																			<li><a href="./contact.php#">Support</a></li>
																		</ul>
																	</div>
																</div>

															</div>
														</div>
														<div class="col-xl-6 col-lg-5 order-lg-1">
															<a href="#" class="">
																<h4 style="color: #fc0254;">SuperCrypt <span style="color: white;">EX</span></h4>
																<!---<img src="img/logo.png" alt="">--->
															</a>
															<div class="copyright">
																&copy;<script>document.write(new Date().getFullYear());</script> | SuperCryptEX - Secure Server
															</div>

															<div class="social-links">
																<a href="https://www.instagram.com/"><i class="fa fa-instagram"></i></a>
																<a href="https://www.pinterest.com/"><i class="fa fa-pinterest"></i></a>
																<a href="https://www.facebook.com/"><i class="fa fa-facebook"></i></a>
																<a href="https://twitter.com/"><i class="fa fa-twitter"></i></a>
																<a href="https://www.youtube.com/"><i class="fa fa-youtube"></i></a>
															</div>

															<div class="copyright"><strong>Design by <br>[</strong>
																<!-- Link back to Colorlib can't be removed. Template is licensed under CC BY 3.0. -->
																Copyright &copy;<script>document.write(new Date().getFullYear());</script> All rights reserved | This template is made with <i class="fa fa-heart-o" aria-hidden="true"></i> by <a href="https://colorlib.com" target="_blank">Colorlib</a>
																<!-- Link back to Colorlib can't be removed. Template is licensed under CC BY 3.0. -->
																<strong> ]</strong></div>

															</div>
														</div>
													</div>
												</footer>
												<!-- Footer section end -->

												<!-- MODAL SECTION -->
												<!-- Modal -->
												<div class="modal fade" id="dotnetModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
													<div class="modal-dialog" role="document">
														<div class="modal-content">
															<div class="modal-header">
																<h5 class="modal-title" id="exampleModalLabel">Attention</h5>
																<button type="button" class="close" data-dismiss="modal" aria-label="Close">
																	<span aria-hidden="true">&times;</span>
																</button>
															</div>
															<div class="modal-body">
																In order to run <b style="color: #fc0254;">SuperCryptEX</b> program correctly, you need to install <b>"Microsoft .NET Core 3.1 Desktop Runtime"</b>. <br><br>Click Here to download the <a href="https://download.visualstudio.microsoft.com/download/pr/7cea63ad-1e76-41f0-a54a-eacb48fec749/87c339835cd7647c0fee3f14820cd909/windowsdesktop-runtime-3.1.16-win-x64.exe
																">64-bit</a> | <a href="https://download.visualstudio.microsoft.com/download/pr/f703f604-a973-4ab9-abe4-b4b2ec786e66/af8cea0988953ef074157ea99d30879a/windowsdesktop-runtime-3.1.16-win-x86.exe
																">32-bit</a> version.
															</div>
															<div class="modal-footer">
																<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
															</div>
														</div>
													</div>
												</div>

												<!-- MODAL SECTION -->

												<!--====== Javascripts & Jquery ======-->
												<script src="js/jquery-3.2.1.min.js"></script>
												<script src="js/bootstrap.min.js"></script>
												<script src="js/jquery.slicknav.min.js"></script>
												<script src="js/owl.carousel.min.js"></script>
												<script src="js/mixitup.min.js"></script>
												<script src="js/main.js"></script>

												<!--- Fetch Search Data --->
					<script type="text/javascript">
						function getUser(){
							if ($('#input_username').val() == "") {
								$("#found_user").empty().append(
									'<div id="" style="display: block;">'+
									'<ul class="sub-list" id="" style="background-color: #E81123; padding: 25px">'+
									'<div class="contact-form">'+
									'<h5 class="text-white text-center"><i>No Username Matches the Keyword!</i></h5>'+
									'</div>'+
									'</ul>'+
									'</div>'
									);
							} else {
								var formData = {
                'name': $('#input_username').val()
            };
												//AJAX to Get Search Data
												$.ajax({
													type: 'POST',
													url:'./fetch_search_user.php',
													data: formData,
													success: function(data){
														$("#found_user").empty();
														if(data.objectz[0].name == "NO DATA") {
															$("#found_user").append(
																'<div id="" style="display: block;">'+
																'<ul class="sub-list" id="" style="background-color: #E81123; padding: 25px">'+
																'<div class="contact-form">'+
																'<h5 class="text-white text-center"><i>No Username Matches the Keyword!</i></h5>'+
																'</div>'+
																'</ul>'+
																'</div>'
																);
														} else {
															for (var i in data.objectz) {
																$("#found_user").append(
																	'<div class="my-3" id="" style="display: block;">'+
																	'<ul class="sub-list" id="" style="background-color: #17A2B8; padding: 25px">'+
																	'<div class="contact-form">'+
																	'<div class="row justify-content-center">'+
																	'<div class="col">'+
																	'<div class="row">'+
																	'<div class="col-sm-auto"><h2><div class="fa fa-user text-white text-center"></div></h2></div>'+
																	'<div class="col">'+
																	'<h3 class="mb-2 text-white">'+data.objectz[i].fullname+'</h3>'+
																	'<h5 class="text-white"><i>Username : '+data.objectz[i].name+'</i></h5>'+
																	'</div>'+
																	'</div>'+
																	'</div>'+
																	'<div id="" class="col text-right">'+
																	'<button class="site-btn" style="background-color: #FFC107;" onclick="getRecipient(\''+data.objectz[i].user_id+'\',\''+data.objectz[i].name+'\',\''+data.objectz[i].fullname+'\',\''+data.objectz[i].client_public+'\')">Choose</button>'+
																	'</div>'+
																	'</div>'+
																	'</div>'+
																	'</ul>'+
																	'</div>'
																	);
															}
														}
													},
													error: function(data){
														$("#found_user").empty().append(
															'<div id="" style="display: block;">'+
															'<ul class="sub-list" id="" style="background-color: #17A2B8; padding: 25px">'+
															'<div class="contact-form">'+
															'<div class="row justify-content-center">'+
															'<div class="col">'+
															'<div class="row">'+
															'<div class="col-sm-auto"><h2><div class="fa fa-user text-white text-center"></div></h2></div>'+
															'<div class="col">'+
															'<h3 class="mb-2 text-white">ERRORRRRRRRRRR</h3>'+
															'<h5 class="text-white"><i>UID : ERRORRRRRRRRRR</i></h5>'+
															'</div>'+
															'</div>'+
															'</div>'+
															'<div id="" class="col text-right">'+
															'<button class="site-btn" style="background-color: #FFC107;" onclick="getRecipient()">Choose</button>'+
															'</div>'+
															'</div>'+
															'</div>'+
															'</ul>'+
															'</div>'
															);
													}
												});
											}
										};
									</script>
								<!--- End Fetch Search Data --->

								<!--- Toggle --->
								<script type="text/javascript">
									var card_color="#1c294a";
									var show_state=false;

									function hs_toggle(){
										$("#todo_create").hide();
										if(show_state==false){
											show_state=true;
											$("#card_parent").show();
											$("#show_hide").remove();
											$("#show_hide_container").append('<button class="btn pull-right cardz" onclick="hs_toggle()" id="show_hide" style="background-color: #343A40;color: white;"><span class="fa fa-arrow-circle-up"></span></button>');
											window.location.href = "#show_hide_container";
										}
										else{
											show_state=false;
											$("#card_parent").hide();
											$("#show_hide").remove();
											$("#show_hide_container").append('<button class="btn pull-right cardz" onclick="hs_toggle()" id="show_hide" style="background-color: #343A40;color: white;"><span class="fa fa-arrow-circle-down"></span></button>');
											window.location.href = "#main";
										}
									};
									function toggle(show, hide, hrefz='#'){
										if (show_state==false) {
											hs_toggle();
										}

										$(hide).hide();
										$(show).show();
										
										if (hrefz!='#') {
											window.location.href = hrefz;
										}
									}
									function todo_edit(x, title, desc, todo){
										var todo_id=document.getElementById(x).innerHTML;
										var e_title=document.getElementById(title).value;
										var e_desc=document.getElementById(desc).value;
										var e_todo=document.getElementById(todo).value;

										var conf = confirm("[Edit Data] Are You Sure?");
										if (conf == true) {
											$.ajax({
												'url':'./todo_edit.php',
												'method':'POST',
												'data' : {
													'todo_id':todo_id,
													'e_title':e_title,
													'e_desc':e_desc,
													'e_todo':e_todo
												},
												'success':function(data){
													//alert("Data Updated!");
													document.location.reload(true);
												}
											});
										}
									};
									function todo_delete(x){
										var todo_id=document.getElementById(x).innerHTML;
										var conf = confirm("[Delete Data] Are You Sure?");
										if (conf == true) {
											$.ajax({
												'url':'./todo_delete.php',
												'method':'POST',
												'data' : {'todo_id':todo_id},
												'success':function(data){
													//alert("Data Deleted!");
													document.location.reload(true);
												}
											});
										}
									};
									function todo_color(x){
										if (x=="danger") { card_color="#DC3545"; }
										if (x=="warning") { card_color="#FFC107"; }
										if (x=="success") { card_color="#28A745"; }
										if (x=="info") { card_color="#17A2B8"; }
										if (x=="primary") { card_color="#007BFF"; }
										if (x=="secondary") { card_color="#868E96"; }
										if (x=="dark") { card_color="#343A40"; }
										if (x=="default") { card_color="#1C294A"; }
										document.getElementById("card_border").style.backgroundColor = card_color;
										document.getElementById("todo_color").value = card_color;
									}
								</script>
								<!--- End Toggle --->


								<!--- ALL SEND FILE FUNCTIONS --->
								<script type="text/javascript">
									var GLOBAL_RECIPIENT_PUBLIC_KEY;
									var GLOBAL_RECIPIENT_NAME;
									var GLOBAL_RECIPIENT_FULLNAME;
									var GLOBAL_RECIPIENT_ARRAY = [];
									var GLOBAL_RECIPIENT_USERNAME_ARRAY = [];


// Check Params
function checkSessionParams(isEnabled){
	if (isEnabled == true) {
		alert(
			"Session Host Private : <?php echo $_SESSION["host_private"] ?>\n" +
			"Session Host Public : <?php echo $_SESSION["host_public"] ?>\n" +
			"Session Client Private : <?php echo $_SESSION["client_private"] ?>\n" +
			"Session Client Public : <?php echo $_SESSION["client_public"] ?>\n"
			);
	}

	// Add Modal Function
	$('#dotnetModal').modal('show');
}

// Get Recipient Public Key
function getRecipient(r_id, r_uname, r_name, r_public){

	GLOBAL_RECIPIENT_PUBLIC_KEY = r_public;
	GLOBAL_RECIPIENT_FULLNAME = r_name;
	GLOBAL_RECIPIENT_NAME = r_uname; 

	document.getElementById("receiver_id").value = r_id;
	document.getElementById("receiver_name").value = GLOBAL_RECIPIENT_NAME;
	document.getElementById("receiver_fullname").value = GLOBAL_RECIPIENT_FULLNAME;

	toggle('#sendfile_download','#sendfile_recipient');

}
// Upload Function
async function UploadEncryptedFile() {
	let formData = new FormData();
	formData.append("file", encrypted_file.files[0]);
	await fetch('./sendfile.php', {method: "POST", body: formData});
	alert("File Uploaded Successfully!");
}


// Download Function
function DownloadKey(filename, text) {
	var element = document.createElement('a');
	element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
	element.setAttribute('download', filename);

	element.style.display = 'none';
	document.body.appendChild(element);

	element.click();

	document.body.removeChild(element);
}

function DownloadFile(url) {
	document.getElementById('download_iframe').src = url;
};


</script>
<?php
if (isset($_FILES['file']['name'])) {
// Get the Name of Uploaded File
	$filename = $_SESSION["name"]."_".$_FILES['file']['name'];

// Choose Where to Save the Uploaded File
	$location = "./[Downloads]/[Files]/".$filename;

// Save the Uploaded File to Server
	if ( move_uploaded_file($_FILES['file']['tmp_name'], $location)) {
		echo '<script>alert("Success!");</script>';
	}
	else {
		echo '<script>alert("Failed!")</script>';
	}

	unset($_POST);
}
?>


</body>
</html>
<?php
}
else {
	header("Location: ./login.php");
}
?>