<?php
/*
Droppy cron job file

Should run around every 1 to 5 minutes
*/

//Including 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';

$current_time = time();

// Auto remove a file that is processing longer than 1 day (86400 seconds)
$remove_processing_files = true;

if(isset($amazon_plugin) && $amazon_plugin == 'true') {
	include $amazon_dir . '/cron_droppy.php';
}
elseif(isset($ftp_plugin) && $ftp_plugin == 'true') {
	include $ftp_dir . '/cron_droppy.php';
}
else
{
	$get_uploads = $mysqli->query("SELECT * FROM $table_uploads WHERE status='ready'");
	while($uploads = $get_uploads->fetch_assoc()) {
		if($expire != 0) {
			if(($current_time == $uploads['time_expire'] || $current_time > $uploads['time_expire'])) {
				$upload_id = $uploads['upload_id'];

				$email_list = '<ul>';
				$get_receivers = $mysqli->query("SELECT * FROM $table_receivers WHERE upload_id='$upload_id'");
				while($row = $get_receivers->fetch_assoc()) {
					$email_list .= '<li>' . $row['email'] . '</li>';
				}
				$email_list .= '</ul>';

				$file_list = '<ul>';
				$get_files = $mysqli->query("SELECT * FROM $table_files WHERE upload_id='$upload_id'");
				while($row = $get_files->fetch_assoc()) {
					$file_list .= '<li>' . $row['file'] . '</li>';
				}
				$file_list .= '</ul>';

				if($uploads['count'] > 1) {
					unlink(dirname(__FILE__) . '/../' . $upload_dir . $name_on_file . '-' . $uploads['upload_id'] . '.zip');
				}
				else
				{
					$get_files = $mysqli->query("SELECT * FROM $table_files WHERE upload_id='$upload_id'");
					while($files = $get_files->fetch_assoc()) {
						unlink(dirname(__FILE__) . '/../' . $upload_dir . $files['secret_code'] . '-' . $files['file']);
						$mysqli->query("DELETE FROM $table_files WHERE upload_id='$upload_id'");
					}
				}
				$mysqli->query("UPDATE $table_uploads SET status='destroyed' WHERE upload_id='$upload_id'");

				if(empty($uploads['lang'])) {
					$upload_language = $language;
				}
				else
				{
					$upload_language = $uploads['lang'];
				}

				$tokens = array(
				    'email_from' => $uploads['email_from'],
				    'size' => round($rows['size'] / 1048576, 2),
				    'site_name' => $site_name,
				    'upload_id' => $uploads['upload_id'],
				    'message' => $uploads['message'],
				    'total_files' => $uploads['count'],
				    'download_btn' => '<a href="' . $site_url . $upload_id . '&p=' . $private_id . '" style="text-decoration: none;color: #FFF;background-color: #348eda;border: solid #348eda;border-width: 10px 20px;line-height: 2;font-weight: bold;margin-right: 10px;text-align: center;cursor: pointer;display: inline-block;border-radius: 25px;">' . $text['download'] . '</a>',
		            'download_url' => $site_url . $upload_id . '&p=' . $private_id,
		            'file_list' => $file_list,
		            'email_list' => $email_list,
		            'expire_time' => secs_to_h($expire),
				);

				$pattern = '{%s}';

				$map = array();
				foreach($tokens as $var => $value)
				{
				    $map[sprintf($pattern, $var)] = $value;
				}

				$message_destruct = strtr(requestTemplate('destroyed',$upload_language), $map);
				$destroyed_subject = strtr(requestTemplate('destroyed_subject',$upload_language), $map);

				//Email user
				emailSender($message_destruct,$destroyed_subject,$uploads['email_from']);
			}
		}
	}
}

//Remove processing files that are processing longer than 1 day. (86400 seconds)
$get_processing_uploads = $mysqli->query("SELECT * FROM $table_uploads WHERE status='processing'");
while($uploads = $get_processing_uploads->fetch_assoc()) {
    if($uploads['status'] == 'processing' && $remove_processing_files == true && $current_time >= ($uploads['time'] + 86400)) {
        $mysqli->query("DELETE FROM $table_uploads WHERE id='".$uploads['id']."'");
    }
}

//This will deletes uploads from the database that are older than 1 month to spare some storage on your server and reduce slowness in your database set it to NO in the config file to disable it
if($delete_old_data == 'YES') {
    $get_old_data = $mysqli->query("SELECT * FROM $table_uploads WHERE status='destroyed'");
    while($row = $get_old_data->fetch_assoc()) {
        if($current_time >= $row['time'] + 2628000) {
            $mysqli->query("DELETE FROM $table_uploads WHERE upload_id='".$row['upload_id']."'");
            $mysqli->query("DELETE FROM $table_downloads WHERE download_id='".$row['upload_id']."'");
            $mysqli->query("DELETE FROM $table_files WHERE upload_id='".$row['upload_id']."'");
            $mysqli->query("DELETE FROM $table_receivers WHERE upload_id='".$row['upload_id']."'");
        }
    }
}
?>