<?php
session_start();
 
$message = '';

if (isset($_SESSION["user_id"]) and isset($_SESSION["password"])) {
	$dbhost=$_SESSION["dbhost"];
	$dbuser=$_SESSION["dbuser"];
	$dbpass=$_SESSION["dbpass"];

if (isset($_POST['receiver_id']) && isset($_POST['receiver_name']))
{
  if (isset($_FILES['encrypted_file']) && $_FILES['encrypted_file']['error'] === UPLOAD_ERR_OK)
  {
    // get details of the uploaded file
    $fileTmpPath = $_FILES['encrypted_file']['tmp_name'];
    $fileName = $_FILES['encrypted_file']['name'];
    $fileSize = $_FILES['encrypted_file']['size'];
    $fileType = $_FILES['encrypted_file']['type'];
    $fileNameCmps = explode(".", $fileName);
    $fileExtension = strtolower(end($fileNameCmps));
    $time_now = date("d-m-Y_H-i-sa",time());

    $newFileNameCmps = [];

    for ($x = 0; $x < count($fileNameCmps) - 1; $x++) {
      $newFileNameCmps[$x] = $fileNameCmps[$x];
    }

    $newFileNameRAW = join("_",$newFileNameCmps);
 
    // sanitize file-name
    //$newFileName = time() . '_' . $fileName . '.' . $fileExtension;

    //$newFileName = $time_now . '_'. $_SESSION["name"] . "_" . $_POST['receiver_name'] . "_" . $newFileNameRAW . "." . $fileExtension;
    $newFileName = $time_now . '_'. $_SESSION["name"] . "_" . $_POST['receiver_name'] . "_" . $fileName;

    $zipFileName = $time_now . '_'. $_SESSION["name"] . "_" . $_POST['receiver_name'] . "_" . $newFileNameRAW . "_" . $fileExtension . ".zip";
 
    // check if file has one of the following extensions
    $allowedfileExtensions = array('mx_encrypted');
    //$allowedfileExtensions = array('mx_encrypted');
 
    if (in_array($fileExtension, $allowedfileExtensions))
    {
      // directory in which the uploaded file will be moved
      $uploadFileDir = './[Downloads]/[Files]/';
      $dest_path = $uploadFileDir . $newFileName;
      //$message .= "Destination Path : ".$dest_path."<br>Receiver ID : ".$_POST['receiver_id']."<br>Receiver Name : ".$_POST['receiver_name']."<br>File Name Cmp : ".$fileNameCmps[0]."<br>";
 
      if(move_uploaded_file($fileTmpPath, $dest_path)) 
      {

      	$pdo=new PDO($dbhost, $dbuser, $dbpass);
      	// Put Into Transfer Log Database

                    $sql = "INSERT INTO transfer (sender_id, sender_name, sender_fullname, sender_public_key, receiver_id, receiver_name, receiver_fullname, file_path, file_name, transfer_time) VALUES (:sender_id, :sender_name, :sender_fullname, :sender_public_key, :receiver_id, :receiver_name, :receiver_fullname, :file_path, :file_name, :transfer_time)";
                    $stmt = $pdo->prepare($sql);
                    $stmt->execute(array(
                      ':sender_id' => $_SESSION["user_id"],
                      ':sender_name' => $_SESSION["name"],
                      ':sender_fullname' => $_SESSION["fullname"],
                      ':sender_public_key' => $_SESSION["host_public"],
                      ':receiver_id' => $_POST['receiver_id'],
                      ':receiver_name' => $_POST['receiver_name'],
                      ':receiver_fullname' => $_POST['receiver_fullname'],
                      ':file_path' => $dest_path,
                      ':file_name' => $fileName,
                      ':transfer_time' => $time_now,
                    ));
                    unset($_POST);

        $message .='File is successfully uploaded.';
      }
      else
      {
        $message .= 'There was some error moving the file to upload directory. Please make sure the upload directory is writable by web server.';
      }
    }
    else
    {
      $message .= 'Upload Failed!<br> Allowed File Types : "' . implode(',', $allowedfileExtensions) . '"';
    }
  }
  else
  {
    $message .= 'There\'s Some Error in the File Upload.<br>';

    if ($_FILES['encrypted_file']['error'] == 1) {
			$message .= 'Error : The uploaded file exceeds the upload_max_filesize directive in php.ini!';
    }
    elseif ($_FILES['encrypted_file']['error'] == 2) {
      $message .= 'Error : The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form!';
    }
    elseif ($_FILES['encrypted_file']['error'] == 3) {
      $message .= 'Error : The uploaded file was only partially uploaded!';
    }
    elseif ($_FILES['encrypted_file']['error'] == 4) {
      $message .= 'Error : No file was uploaded!';
    }
    elseif ($_FILES['encrypted_file']['error'] == 6) {
      $message .= 'Error : Missing a temporary folder!';
    }
    elseif ($_FILES['encrypted_file']['error'] == 7) {
      $message .= 'Error : Failed to write file to disk!';
    }
    elseif ($_FILES['encrypted_file']['error'] == 8) {
      $message .= 'Error : A PHP extension stopped the file upload! PHP does not provide a way to ascertain which extension caused the file upload to stop<br> Examining the list of loaded extensions with phpinfo() may help.';
    }
    else {
    	    $message .= 'Error : ' . $_FILES['encrypted_file']['error'];
    }

  }
}
}
$_SESSION['message'] = $message;
header("Location: sendfile.php");
?>