<?php
// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Verify nonce
if( ! empty( $_GET['action'] ) && ( 'deletelog' == $_GET['action'] ) ) {
    if ( empty( $_REQUEST['_wpnonce'] ) ) {
        return false;
    } else {
    }
} elseif( defined( 'YTAM_FREEMIUS_UNINSTALL' ) ) {
} else {
    return false;
}

// Start the session
session_start();


// Get upload folder
$upload_dir 		= wp_upload_dir();
$upload_base_dir 	= $upload_dir['basedir'];
$dir_logs			= $upload_base_dir . '/youtube-automate/logs/';

// Check the file
$files  = @scandir( $dir_logs );
$result = '';

if ( ! empty( $files ) ) {
    foreach ( $files as $key => $value ) {
        if ( ! in_array( $value, array( '.', '..' ), true ) ) {
            if ( ! is_dir( $value ) && strstr( $value, '.log' ) ) {
                $result = $value;
                $filename = $dir_logs . $result;
            }
        }
    }
}


// Exit if no log file
if( empty( $result ) ) {
    return false;
}



if ( !is_file($filename) || !is_readable( $filename ) ) {
    header("HTTP/1.1 404 Not Found");
    exit;
}
$size = filesize($filename);

// Close the session
session_write_close();

// Delete logs file
wp_delete_file( $filename );


// Redirect without delete arguments
if( ! defined( 'YTAM_FREEMIUS_UNINSTALL' ) ) {
	wp_redirect( remove_query_arg( array( 'action', '_wpnonce' ) ) );
	exit;
}

?>
