Example #1
0
function sdm_check_pass_ajax_call()
{
    global $wpdb;
    $button_id = $_POST['button_id'];
    // Get button cpt id
    $pass_val = $_POST['pass_val'];
    // Get password attempt
    $success = '';
    $download_link = '';
    // Get post object
    $post_object = get_post($button_id);
    // Get post password
    $post_pass = $post_object->post_password;
    // Check if password is a match
    if ($post_pass != $pass_val) {
        // Match is a failure
        $success = 'no';
        // Pass back to ajax
    } else {
        // Match is a success
        $success = 'yes';
        // Pass back to ajax
        $download_id = $button_id;
        $download_title = get_the_title($download_id);
        $download_link = get_post_meta($download_id, 'sdm_upload', true);
        $ipaddress = $_SERVER["REMOTE_ADDR"];
        $date_time = current_time('mysql');
        $visitor_country = sdm_ip_info('Visitor', 'Country');
        if (is_user_logged_in()) {
            // Get user name (if logged in)
            global $current_user;
            get_currentuserinfo();
            $visitor_name = $current_user->user_login;
        } else {
            $visitor_name = __('Not Logged In', 'simple-download-monitor');
        }
        // Get option for global disabling of download logging
        $main_option = get_option('sdm_downloads_options');
        $no_logs = isset($main_option['admin_no_logs']);
        // Get optoin for logging only unique IPs
        $unique_ips = isset($main_option['admin_log_unique']);
        // Get post meta for individual disabling of download logging
        $get_meta = get_post_meta($download_id, 'sdm_item_no_log', true);
        $item_logging_checked = isset($get_meta) && $get_meta === 'on' ? 'on' : 'off';
        $dl_logging_needed = true;
        // Check if download logs have been disabled (globally or per download item)
        if ($no_logs === true || $item_logging_checked === 'on') {
            $dl_logging_needed = false;
        }
        // Check if we are only logging unique ips
        if ($unique_ips === true) {
            $check_ip = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . 'sdm_downloads WHERE post_id="' . $download_id . '" AND visitor_ip = "' . $ipaddress . '"');
            //This IP is already logged for this download item. No need to log it again.
            if ($check_ip) {
                $dl_logging_needed = false;
            }
        }
        if ($dl_logging_needed) {
            $table = $wpdb->prefix . 'sdm_downloads';
            $data = array('post_id' => $download_id, 'post_title' => $download_title, 'file_url' => $download_link, 'visitor_ip' => $ipaddress, 'date_time' => $date_time, 'visitor_country' => $visitor_country, 'visitor_name' => $visitor_name);
            $insert_table = $wpdb->insert($table, $data);
        }
    }
    // Generate ajax response
    $response = json_encode(array('success' => $success, 'url' => $download_link));
    header('Content-Type: application/json');
    echo $response;
    exit;
}
Example #2
0
function handle_sdm_download_via_direct_post()
{
    if (isset($_REQUEST['smd_process_download']) && $_REQUEST['smd_process_download'] == '1') {
        global $wpdb;
        $download_id = strip_tags($_REQUEST['download_id']);
        $download_title = get_the_title($download_id);
        $download_link = get_post_meta($download_id, 'sdm_upload', true);
        //Do some validation checks
        if (empty($download_id)) {
            wp_die(__('Error! Incorrect download item id.', 'simple-download-monitor'));
        }
        if (empty($download_link)) {
            wp_die(__('Error! This download item (' . $download_id . ') does not have any download link. Edit this item and specify a downloadable file URL for it.', 'simple-download-monitor'));
        }
        //Check download password (if applicable for this download)
        $post_object = get_post($download_id);
        // Get post object
        $post_pass = $post_object->post_password;
        // Get post password
        if (!empty($post_pass)) {
            //This download item has a password. So validate the password.
            $pass_val = $_REQUEST['pass_text'];
            if (empty($pass_val)) {
                //No password was submitted with the downoad request.
                wp_die(__('Error! This download requires a password.', 'simple-download-monitor'));
            }
            if ($post_pass != $pass_val) {
                //Incorrect password submitted.
                wp_die(__('Error! Incorrect password. This download requires a valid password.', 'simple-download-monitor'));
            } else {
                //Password is valid. Go ahead with the download
            }
        }
        //End of password check
        $ipaddress = $_SERVER["REMOTE_ADDR"];
        $date_time = current_time('mysql');
        $visitor_country = sdm_ip_info('Visitor', 'Country');
        if (is_user_logged_in()) {
            // Get user name (if logged in)
            global $current_user;
            get_currentuserinfo();
            $visitor_name = $current_user->user_login;
        } else {
            $visitor_name = __('Not Logged In', 'simple-download-monitor');
        }
        // Get option for global disabling of download logging
        $main_option = get_option('sdm_downloads_options');
        $no_logs = isset($main_option['admin_no_logs']);
        // Get optoin for logging only unique IPs
        $unique_ips = isset($main_option['admin_log_unique']);
        // Get post meta for individual disabling of download logging
        $get_meta = get_post_meta($download_id, 'sdm_item_no_log', true);
        $item_logging_checked = isset($get_meta) && $get_meta === 'on' ? 'on' : 'off';
        $dl_logging_needed = true;
        // Check if download logs have been disabled (globally or per download item)
        if ($no_logs === true || $item_logging_checked === 'on') {
            $dl_logging_needed = false;
        }
        // Check if we are only logging unique ips
        if ($unique_ips === true) {
            $check_ip = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . 'sdm_downloads WHERE post_id="' . $download_id . '" AND visitor_ip = "' . $ipaddress . '"');
            //This IP is already logged for this download item. No need to log it again.
            if ($check_ip) {
                $dl_logging_needed = false;
            }
        }
        if ($dl_logging_needed) {
            // We need to log this download.
            $table = $wpdb->prefix . 'sdm_downloads';
            $data = array('post_id' => $download_id, 'post_title' => $download_title, 'file_url' => $download_link, 'visitor_ip' => $ipaddress, 'date_time' => $date_time, 'visitor_country' => $visitor_country, 'visitor_name' => $visitor_name);
            $data = array_filter($data);
            //Remove any null values.
            $insert_table = $wpdb->insert($table, $data);
            if ($insert_table) {
                //Download request was logged successfully
            } else {
                //Failed to log the download request
                wp_die(__('Error! Failed to log the download request in the database table', 'simple-download-monitor'));
            }
        }
        //Downoad the item
        sdm_redirect_to_url($download_link);
        exit;
    }
}