<?php

// Including external files
require_once dirname(__FILE__) . '/../config/config.php';
require_once dirname(__FILE__) . '/plugins/class.phpmailer.php';
require_once dirname(__FILE__) . '/plugins/class.smtp.php';
require_once dirname(__FILE__) . '/plugins/timeconvert.php';
require_once dirname(__FILE__) . '/plugins/Mobile_Detect.php';

// Keep script running even when user exits
ignore_user_abort(true);

// Set maximum execution time to unlimited
set_time_limit(0);

// Try to apply ini settings (when enabled)
if($set_ini_settings) {
    ini_set('max_execution_time', '0');
    ini_set('max_input_time', '-1');
    ini_set('post_max_size', $max_file_size . 'M');
    ini_set('upload_max_filesize', $max_file_size . 'M');
}

// Set all the variables
$max_file_size      = ($max_size * 1048576); //Get and count max file size
$upload_id          = mysqli_real_escape_string($mysqli, $_POST['id']); // Get Upload ID
$share              = mysqli_real_escape_string($mysqli, $_POST['share']); // Get share Type
$email_from         = mysqli_real_escape_string($mysqli, $_POST['email_from']); // Email from address
if(!empty($default_email_to)) {
    $temp_array = array($default_email_to);
    $email_to = $temp_array;
}
else
{
    $email_to = $_POST['email_to'];
}
$destruct_post      = mysqli_real_escape_string($mysqli, $_POST['destruct']); // Destruction type
$msg                = strip_tags(mysqli_real_escape_string($mysqli, $_POST['message']), '<br><br/>'); // The message
$password           = mysqli_real_escape_string($mysqli, $_POST['password']); // Get the password input

// Set the correct destruct type
if($destruct_post == 'yes') {
    $destruct = 'YES';
}
else
{
    $destruct = 'NO';
}

// Convert the password to a DB friendly version
if($password == null || $password == '') {
    $pwd = 'EMPTY';
}
else
{
	// Check if the user is allowed to set a password.
	if($pm_pass_enabled == 'false' || $password_enabled == 'false') {
		$pwd = 'EMPTY';
	}
	else
	{
    	$pwd = md5($password);
	}
}

// Checking if the S3 plugin has been installed.
if(isset($amazon_plugin) && $amazon_plugin == 'true') {
    include $amazon_dir . '/class.upload.php';

    // Initalize the upload class
    $upload = new Upload();

    if((!empty($email_from) && $share == 'mail') || ($share == 'link')) {
        // Process the upload and all its data.
        $upload_response = $upload->upload($_FILES['file'], $upload_id, $language_file, $share, $msg, $password, $pwd, $destruct, $email_to, $email_from);
    }
}
// Checking if the FTP plugin has been installed.
elseif(isset($ftp_plugin) && $ftp_plugin == 'true') {
    include $ftp_dir . '/class.upload.php';

    // Initalize the upload class
    $upload = new Upload();

    // Check if email_from input has been set.
    if((!empty($email_from) && $share == 'mail') || ($share == 'link')) {
        // Process the upload and all its data.
        $upload_response = $upload->upload($_FILES['file'], $upload_id, $language_file, $share, $msg, $password, $pwd, $destruct, $email_to, $email_from);
    }
}
else
{
    // Require the Droppy upload class
    require_once dirname(__FILE__) . '/../resources/class/upload.class.php';

    // Initalize the upload class
    $upload = new Upload();

	// Check if email_from input has been set.
    if((!empty($email_from) && $share == 'mail') || ($share == 'link')) {
        // Process the upload and all its data.
        $upload_response = $upload->upload($_FILES['file'], $upload_id, $language_file, $share, $msg, $password, $pwd, $destruct, $email_to, $email_from);
    }
}

// If the premium plugin has been installed and the user is logged in then enter the user id into the upload table.
$upload->setPremiumUser($upload_id);

// Return the upload response
echo json_encode($upload_response);