<?php

if (isset($_REQUEST['name']) && strlen(trim($_REQUEST['name'])) > 0) {
    $name = stripslashes(strip_tags($_REQUEST['name']));
} else {
    $name = 'No name entered';
}
if (isset($_REQUEST['email']) && strlen(trim($_REQUEST['email'])) > 0) {
    $email = stripslashes(strip_tags($_REQUEST['email']));
} else {
    $email = 'No email entered';
}
if (isset($_REQUEST['prod_id']) && strlen(trim($_REQUEST['prod_id'])) > 0) {
    $prod_id = stripslashes(strip_tags($_REQUEST['prod_id']));
} else {
    $prod_id = 'No product id found';
}
if (isset($_REQUEST['ap_id']) && strlen(trim($_REQUEST['ap_id'])) > 0) {
    $ap_id = stripslashes(strip_tags($_REQUEST['ap_id']));
}
if (isset($_REQUEST['clientip']) && strlen(trim($_REQUEST['clientip'])) > 0) {
    $clientip = stripslashes(strip_tags($_REQUEST['clientip']));
}
if (!defined('ABSPATH')) {
    include_once '../../../wp-load.php';
}
include 'eStore_squeeze_form_functions.php';
eStore_process_squeeze_form_submission($name, $email, $prod_id, $ap_id, $clientip);
function eStore_free_download_form($id, $post_url = '', $success_msg = '', $button_text = '')
{
    global $wpdb;
    $products_table_name = WP_ESTORE_PRODUCTS_TABLE_NAME;
    $ret_product = $wpdb->get_row("SELECT * FROM {$products_table_name} WHERE id = '{$id}'", OBJECT);
    $button_image = $ret_product->button_image_url;
    if (empty($button_image)) {
        $button_image = WP_ESTORE_URL . "/images/download_icon.png";
    }
    if (is_numeric($ret_product->available_copies)) {
        if ($ret_product->available_copies < 1) {
            // No more copies left
            $output = WP_ESTORE_NO_COPIES_LEFT;
            return $output;
        }
    }
    $form_unique_identifier = "eStore_free_download" . $id;
    $form_unique_identifier2 = eStore_get_static_count();
    if ($form_unique_identifier2 === 0) {
        unset($_SESSION['eStore_squeeze_form_processed']);
    }
    $output = "";
    $email_sent = false;
    $use_recaptcha = false;
    $resp = "";
    if (get_option('wp_eStore_use_recaptcha')) {
        $use_recaptcha = true;
        $publickey = get_option('wp_eStore_captcha_public_key');
        $privatekey = get_option('wp_eStore_captcha_private_key');
    }
    if (isset($_POST[$form_unique_identifier])) {
        // Submit button was clicked...
        $process_form = true;
        if ($use_recaptcha) {
            if (!function_exists('_recaptcha_qsencode')) {
                require_once 'lib/recaptchalib.php';
            }
            $resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
            if (!$resp->is_valid) {
                $output .= '<p class="eStore_error_message">' . WP_ESTORE_IMAGE_VERIFICATION_FAILED . '</p>';
                $process_form = false;
            }
        }
        if ($process_form) {
            if (empty($success_msg)) {
                $success_msg = WP_ESTORE_EMAIL_SENT;
            }
            if (empty($_POST['cust_name']) || empty($_POST['cust_email'])) {
                $output .= WP_ESTORE_NAME_OR_EMAIL_MISSING;
            } else {
                if (!is_email($_POST['cust_email'])) {
                    $output .= '<p class="eStore_error_message">' . WP_ESTORE_EMAIL_INVALID . '</p>';
                } else {
                    if ($_SESSION['eStore_squeeze_form_processed'] !== "Processed") {
                        if (!empty($post_url)) {
                            $postURL = $post_url;
                        } else {
                            $postURL = WP_ESTORE_URL . "/ajax_process_download.php";
                        }
                        // prepare the data
                        $data = array();
                        $data['name'] = strip_tags($_POST['cust_name']);
                        $data['email'] = strip_tags($_POST['cust_email']);
                        $data['prod_id'] = strip_tags($_POST['free_download_product_id']);
                        $data['ap_id'] = strip_tags($_POST['free_download_ap_id']);
                        $data['clientip'] = strip_tags($_POST['free_download_clientip']);
                        //Process the squeeze form submission
                        if (empty($post_url)) {
                            //This is not a custom http post so process it internally
                            include_once 'eStore_squeeze_form_functions.php';
                            eStore_process_squeeze_form_submission($data['name'], $data['email'], $data['prod_id'], $data['ap_id'], $data['clientip']);
                            $retVal = "Success";
                        } else {
                            $retVal = eStore_post_data_using_wp_remote_post($postURL, $data);
                        }
                        if ($retVal == "Success") {
                            $output .= $success_msg;
                            //WP_ESTORE_EMAIL_SENT;
                        } else {
                            $output .= 'Could not POST the squeeze form processing request to the server!';
                        }
                        $email_sent = true;
                        $_SESSION['eStore_squeeze_form_processed'] = "Processed";
                    } else {
                        //Squeeze form for this product has already been processed on this page. No need to process multiple of them.
                        $output .= $success_msg;
                        //WP_ESTORE_EMAIL_SENT;
                        $email_sent = true;
                    }
                }
            }
            if ($email_sent && !empty($ret_product->return_url)) {
                //Post form submit redirect to page
                eStore_redirect_to_url($ret_product->return_url);
            }
        }
    }
    if (!$email_sent) {
        // E-mail has not yet been sent.  Output should be the input form.
        isset($_COOKIE['ap_id']) ? $cookie_value = $_COOKIE['ap_id'] : ($cookie_value = '');
        $output .= '<div class="free_download_form_old">';
        $output .= '<form method="post"  action=""  style="display:inline">';
        $output .= '<div class="eStore_sf_name_label eStore_sf_element">' . WP_ESTORE_NAME . ': </div>';
        $output .= '<div class="eStore_sf_name_field eStore_sf_element"><input name="cust_name" type="text" class="eStore_text_input" /></div>';
        $output .= '<div class="eStore_sf_email_label eStore_sf_element">' . ESTORE_EMAIL . ': </div>';
        $output .= '<div class="eStore_sf_email_field eStore_sf_element"><input name="cust_email" type="text" class="eStore_text_input" /></div>';
        $args = array('id' => $id);
        $output = apply_filters('eStore_squeeze_form_below_email_filter', $output, $args);
        if (get_option('eStore_show_t_c_for_squeeze_form')) {
            $output .= eStore_show_terms_and_cond();
        }
        $output .= '<input type="hidden" name="eStore_free_download" value="1" />';
        $output .= '<input type="hidden" name="' . $form_unique_identifier . '" value="1" />';
        $output .= '<input type="hidden" name="free_download_product_id" value="' . base64_encode($id) . '" />';
        $output .= '<input type="hidden" name="free_download_ap_id" id="free_download_ap_id" value="' . $cookie_value . '" />';
        $output .= '<input type="hidden" name="free_download_clientip" id="free_download_clientip" value="' . $_SERVER['REMOTE_ADDR'] . '" />';
        if ($use_recaptcha) {
            // Show the Re-Captcha challenge...
            if (!function_exists('_recaptcha_qsencode')) {
                require_once 'lib/recaptchalib.php';
            }
            $output .= recaptcha_get_html($publickey) . "<br />";
        }
        if (empty($button_text)) {
            //render the download button using image
            $output .= '<input type="image" name="submit" class="free_download_submit" alt="' . ESTORE_DOWNLOAD_TEXT . '" src="' . $button_image . '" />';
        } else {
            //render the download button using the specified text
            $output .= '<input type="submit" name="submit" class="free_download_submit" value="' . __($button_text) . '" />';
            //ESTORE_DOWNLOAD_TEXT
        }
        $output .= '</form>';
        $output .= '</div>';
        $output .= '<div class="eStore-clear-float"></div>';
    }
    return $output;
}