<?php
session_start();

if (isset($_SESSION["user_id"]) and isset($_SESSION["password"])) {
	?>
	<!DOCTYPE html>
	<html lang="zxx">
	<head>
		<title>SuperCryptEX | Inbox</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)">
	<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>Inbox</h2>
						<h3>This is Your File Inbox Page</h3>
						<p>What is This Page?<br>
							<strong>File Inbox</strong><br>
						On this page, you can see who has sent the file to you. You can download the file and to open it, you only need to decrypt the file with our decryption program and your private key.</b></i></p>



						<div style="color: white;">
							<a onclick="toggle('#card_parent','#sendfile_recipient', '#divider')" class="site-btn" style="background-color:#FFC107">View</a>
						</div>
					</div>
				</div>

				<div class="col-lg-6">

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





					<!--- DECRYPT TUTORIAL --->
					<div id="sendfile_download">
						<ul class="sub-list" id="card_border">

							<div class="contact-form">
								<div class="sub-text">
									<h3>[ # ] How to Open the Encrypted Files :</h3>
								</div>
								<!--- Download Encryptor --->
								<li><h5 style="color: white;margin-bottom: 15px;"><div class="fa fa-download" style="margin-right: 15px;"></div>1. Download Our Encryption Program (Skip if You Already Have It)</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 Client Private Key</h5>
										<div class="form-group">
											<button class="btn btn-warning text-white" onclick="DownloadKey('<?php echo $_SESSION["name"] ?>_ClientPrivateKey.mx_cpriv', '<?php echo $_SESSION["client_private"] ?>')">Download Private Key [Client]</button>
										</div></li>

										<!--- Download Recipient Public Key --->
										<li><h5 style="color: white;margin-bottom: 15px;"><div class="fa fa-download" style="margin-right: 15px;"></div>3. Download the File that You want to Open with its Sender's Public Key on the Inbox List</h5>
											<div class="form-group">

											</div></li>

											<!--- Download Recipient Public Key --->
											<li><h5 style="color: white;margin-bottom: 15px;"><div class="fa fa-download" style="margin-right: 15px;"></div>4. Decrypt the File Using Our Program and All of the the Required Keys</h5>
												<div class="form-group">

												</div></li>

											</div>


										</ul>
									</div>
									<!--- End DECRYPT TUTORIAL --->





								</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 --->

						</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 To-Do Data --->
						<script type="text/javascript">
							$(document).ready(function(){

										//AJAX to Get To-Do Data
										$.ajax({
											'url':'./fetch_inbox.php',
											'success': function(data){
												
												for (var i in data.objectz) {
													$("#card_parent").append(

														// View List Inbox Card
														'<div class="card text-white cardz" style="display:block;margin: 5px;background-color: #28a745;" id="card_view_'+i+'">'+

														'<div id="transfer_id_'+i+'" hidden>'+data.objectz[i].transfer_id+'</div>'+
														'<div id="sender_id_'+i+'" hidden>'+data.objectz[i].sender_id+'</div>'+

														'<div class="card-header">'+
														'<h5 class="cardz-title">[ '+parseInt(parseInt(i)+parseInt(1))+' ] '+data.objectz[i].file_name+'</h5>'+
														'</div>'+

														'<div class="card-body">'+
														'<h5 class="card-title">Sent by : '+data.objectz[i].sender_fullname+'</h5>'+
														'<p class="card-text" style="white-space: pre-wrap;">Time Stamp : '+data.objectz[i].transfer_time+'</p>'+

														'<button class="btn btn-primary text-white" onclick="DownloadFile(\''+data.objectz[i].file_path+'\')">Download File</button>'+

														'<button class="btn btn-warning text-white" onclick="DownloadKey(\''+data.objectz[i].sender_name+'_HostPublicKey.mx_hpub\', \''+data.objectz[i].sender_public_key+'\')">Download '+data.objectz[i].sender_fullname+'\'s Public Key</button>'+

														'</div>'+

														'</div><br>'
														);
												}
											}
										});



										//AJAX to Get File Recipient List Data
										$.ajax({
											'url':'./fetch_file_recipients.php',
											'success': function(data){
												
												for (var i in data.objectz) {
													$("#file_recipient").append(
														'<option value="'+data.objectz[i].client_public+'">'+data.objectz[i].fullname+'</option>');
												}
											}
										});

									});
								</script>
								<!--- End Fetch To-Do 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;


// 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(){
	var recipient = document.getElementById("file_recipient");
	var getRecipientPublic = recipient.value;
	var getRecipientName = recipient.options[recipient.selectedIndex].text;

	if (getRecipientPublic != "" && getRecipientPublic != null){
		GLOBAL_RECIPIENT_PUBLIC_KEY = getRecipientPublic;
		GLOBAL_RECIPIENT_NAME = getRecipientName;
		toggle('#sendfile_download','#sendfile_recipient');
	}
	else{
		alert("Please Select a 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");
}
?>