Example #1
0
/**
 * return download link after verifying password
 * data format: json
 */
function wpdm_getlink()
{
    global $wpdb;
    if (!isset($_POST['id'])) {
        return;
    }
    $id = (int) $_POST['id'];
    $password = isset($_POST['password']) ? addslashes($_POST['password']) : '';
    $file = get_post($id, ARRAY_A);
    $file['ID'] = $file['ID'];
    $file = wpdm_setup_package_data($file);
    $key = uniqid();
    $file1 = $file;
    // and( password='******' or password like '%[$password]%')
    $plock = isset($file['password_lock']) ? $file['password_lock'] : 0;
    $data = array('error' => '', 'downloadurl' => '');
    if (isset($_POST['verify']) && $_POST['verify'] == 'email' && $file['email_lock'] == 1) {
        if (wpdm_verify_email($_POST['email'])) {
            $subject = "Your Download Link";
            $site = get_option('blogname');
            add_post_meta($file['ID'], "__wpdmkey_" . $key, 3);
            //file_put_contents(WPDM_CACHE_DIR.'wpdm_'.$key,"3");
            $download_url = wpdm_download_url($file, "_wpdmkey={$key}");
            $cff = isset($_POST['custom_form_field']) ? $_POST['custom_form_field'] : array();
            $wpdb->insert("{$wpdb->prefix}ahm_emails", array('email' => $_POST['email'], 'pid' => $file['ID'], 'date' => time(), 'custom_data' => serialize($cff)));
            $eml = get_option('_wpdm_etpl');
            $eml['fromname'] = isset($eml['fromname']) ? $eml['fromname'] : get_bloginfo('name');
            $eml['frommail'] = isset($eml['frommail']) ? $eml['frommail'] : get_bloginfo('admin_email');
            $eml['subject'] = isset($eml['subject']) ? $eml['subject'] : 'Download ' . $file['post_title'];
            $headers = 'From: ' . $eml['fromname'] . ' <' . $eml['frommail'] . '>' . "\r\nContent-type: text/html\r\n";
            $file = wpdm_setup_package_data($file);
            $file['download_url'] = $download_url;
            //Custom Download URL for email lock
            $keys = array();
            foreach ($file as $key => $value) {
                $_key = "[{$key}]";
                $tdata[$_key] = $value;
            }
            $tdata["[site_url]"] = home_url('/');
            $tdata["[site_name]"] = get_bloginfo('sitename');
            $tdata["[download_url]"] = $download_url;
            $tdata["unsaved:///"] = "";
            $tdata["[date]"] = date(get_option('date_format'), time());
            $message = $eml['body'];
            foreach ($tdata as $skey => $svalue) {
                if (!is_array($svalue)) {
                    $message = str_replace(strval($skey), strval($svalue), $message);
                    $eml['subject'] = str_replace(strval($skey), strval($svalue), $eml['subject']);
                }
            }
            //do something before sending download link
            do_action("wpdm_before_email_download_link", $_POST, $file);
            $message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>' . __("Welcome Message", "wpdmpro") . '</title></head><body>' . stripslashes($message) . '</body></html>';
            wp_mail($_POST['email'], stripcslashes($eml['subject']), stripcslashes($message), $headers);
            $idl = isset($file['email_lock_idl']) ? $file['email_lock_idl'] : 0;
            if ($idl != 1) {
                $data['downloadurl'] = "";
                $data['error'] = '<div class="alert alert-success">' . __('Download link sent to your email!', 'wpdmpro') . '</div>';
            } else {
                $data['downloadurl'] = $file['download_url'];
                $data['error'] = '';
                $data['msg'] = '<div class="alert alert-success">' . __('Download link also sent to your email!', 'wpdmpro') . '</div>';
            }
            header('HTTP/1.0 200 OK');
            header("Content-type: application/json");
            echo json_encode($data);
            die;
        } else {
            $data['downloadurl'] = "";
            $data['error'] = '<div class="alert alert-danger">' . __('Invalid Email Address!', 'wpdmpro') . '</i>';
            header("Content-type: application/json");
            echo json_encode($data);
            die;
        }
    }
    if (isset($_POST['force']) && $_POST['force'] != '') {
        $vr = explode('|', base64_decode($_POST['force']));
        if ($vr[0] == 'unlocked') {
            $social = array('f' => 'wpdm_fb_likes', 'g' => 'wpdm_gplus1s', 't' => 'wpdm_tweets', 'l' => 'wpdm_lishare');
            if ($_POST['social'] && isset($social[$_POST['social']])) {
                update_option($social[$_POST['social']], (int) get_option($social[$_POST['social']]) + 1);
            }
            add_post_meta($file['ID'], "__wpdmkey_" . $key, 3);
            $data['downloadurl'] = wpdm_download_url($file, "_wpdmkey={$key}");
            $adata = apply_filters("wpdmgetlink", $data, $file);
            $data = is_array($adata) ? $adata : $data;
            header("Content-type: application/json");
            die(json_encode($data));
        }
    }
    if ($plock == 1 && $password != $file['password'] && !strpos("__" . $file['password'], "[{$password}]")) {
        $data['error'] = 'Wrong Password!';
        $file = array();
    }
    if ($plock == 1 && $password == '') {
        $data['error'] = 'Wrong Password!';
        $file = array();
    }
    $ux = "";
    if ($plock == 1) {
        add_post_meta($file['ID'], "__wpdmkey_" . $key, 3);
    }
    if ($file['ID'] != '') {
        $pu = isset($file['password_usage']) && is_array($file['password_usage']) ? $file['password_usage'] : array();
        $pul = $file['password_usage_limit'];
        if (is_array($pu) && isset($pu[$password]) && $pu[$password] >= $pul && $pul > 0) {
            $data['error'] = __msg('PASSWORD_LIMIT_EXCEED');
        } else {
            if (!is_array($pu)) {
                $pu = array();
            }
            $pu[$password] = isset($pu[$password]) ? $pu[$password] + 1 : 1;
            update_post_meta($file['ID'], '__wpdm_password_usage', $pu);
        }
    }
    if (isset($_COOKIE['unlocked_' . $file['ID']]) && $_COOKIE['unlocked_' . $file['ID']] == 1) {
        $data['error'] = '';
        $file = $file1;
    }
    if ($data['error'] == '') {
        $data['downloadurl'] = wpdm_download_url($file, "_wpdmkey={$key}");
    }
    // home_url('/?downloadkey='.md5($file['files']).'&file='.$id.$ux);
    $adata = apply_filters("wpdmgetlink", $data, $file);
    $data = is_array($adata) ? $adata : $data;
    header("Content-type: application/json");
    die(json_encode($data));
}
/**
 * return download link after verifying password
 * data format: json
 */
function wpdm_getlink()
{
    global $wpdb;
    if (!isset($_POST['id'])) {
        return;
    }
    $id = (int) $_POST['id'];
    $password = isset($_POST['password']) ? addslashes($_POST['password']) : '';
    $file = get_post($id, ARRAY_A);
    //$file['ID'] = $file['ID'];
    $file = wpdm_setup_package_data($file);
    $key = uniqid();
    $file1 = $file;
    // and( password='******' or password like '%[$password]%')
    $plock = isset($file['password_lock']) ? $file['password_lock'] : 0;
    $data = array('error' => '', 'downloadurl' => '');
    if (isset($_POST['reCaptchaVerify'])) {
        $ret = remote_post('https://www.google.com/recaptcha/api/siteverify', array('secret' => get_option('_wpdm_recaptcha_secret_key'), 'response' => $_POST['reCaptchaVerify'], 'remoteip' => $_SERVER['REMOTE_ADDR']));
        $ret = json_decode($ret);
        if ($ret->success == 1) {
            $_SESSION['_wpdm_unlocked_' . $file['ID']] = 1;
            update_post_meta($file['ID'], "__wpdmkey_" . $key, 3);
            $download_url = wpdm_download_url($file, "_wpdmkey={$key}");
            $data['downloadurl'] = $download_url;
        } else {
            $data['error'] = __("Captcha Verification Failed!", "wpmdpro");
        }
        header("Content-type: application/json");
        echo json_encode($data);
        die;
    }
    if (isset($_POST['verify']) && $_POST['verify'] == 'email' && $file['email_lock'] == 1) {
        if (wpdm_verify_email($_POST['email'])) {
            $subject = "Your Download Link";
            $site = get_option('blogname');
            update_post_meta($file['ID'], "__wpdmkey_" . $key, 3);
            //file_put_contents(WPDM_CACHE_DIR.'wpdm_'.$key,"3");
            $_SESSION['_wpdm_unlocked_' . $file['ID']] = 1;
            $download_url = wpdm_download_url($file, "_wpdmkey={$key}");
            $cff = isset($_POST['custom_form_field']) ? $_POST['custom_form_field'] : array();
            $wpdb->insert("{$wpdb->prefix}ahm_emails", array('email' => $_POST['email'], 'pid' => $file['ID'], 'date' => time(), 'custom_data' => serialize($cff)));
            $eml = get_option('_wpdm_etpl');
            $eml['fromname'] = isset($eml['fromname']) ? $eml['fromname'] : get_bloginfo('name');
            $eml['frommail'] = isset($eml['frommail']) ? $eml['frommail'] : get_bloginfo('admin_email');
            $eml['subject'] = isset($eml['subject']) ? $eml['subject'] : 'Download ' . $file['post_title'];
            $headers = 'From: ' . $eml['fromname'] . ' <' . $eml['frommail'] . '>' . "\r\nContent-type: text/html\r\n";
            $file = wpdm_setup_package_data($file);
            $file['download_url'] = $download_url;
            //Custom Download URL for email lock
            $keys = array();
            foreach ($file as $fkey => $value) {
                $_key = "[{$fkey}]";
                $tdata[$_key] = $value;
            }
            $tdata["[site_url]"] = home_url('/');
            $tdata["[site_name]"] = get_bloginfo('sitename');
            $tdata["[download_url]"] = $download_url;
            $tdata["unsaved:///"] = "";
            $tdata["[date]"] = date(get_option('date_format'), time());
            $message = $eml['body'];
            foreach ($tdata as $skey => $svalue) {
                if (!is_array($svalue)) {
                    $message = str_replace(strval($skey), strval($svalue), $message);
                    $eml['subject'] = str_replace(strval($skey), strval($svalue), $eml['subject']);
                }
            }
            //do something before sending download link
            do_action("wpdm_before_email_download_link", $_POST, $file);
            $message = str_replace('[#message#]', stripslashes($message), file_get_contents(wpdm_tpl_path('html-frame.html', WPDM_BASE_DIR . 'email-templates/')));
            wp_mail($_POST['email'], stripcslashes($eml['subject']), stripcslashes($message), $headers);
            $idl = isset($file['email_lock_idl']) ? $file['email_lock_idl'] : 0;
            if ($idl != 1) {
                $data['downloadurl'] = "";
                $data['error'] = '<div class="alert alert-success">' . __('Download link sent to your email!', 'wpdmpro') . '</div>';
            } else {
                $data['downloadurl'] = $file['download_url'];
                $data['error'] = '';
                $data['msg'] = '<div class="alert alert-success">' . __('Download link also sent to your email!', 'wpdmpro') . '</div>';
            }
            if (!wpdm_is_ajax()) {
                @setcookie("wpdm_getlink_data_" . $key, json_encode($data));
                if (isset($data['downloadurl']) && $data['downloadurl'] != '') {
                    header("location: " . $data['downloadurl']);
                    die;
                }
                header("location: " . $_SERVER['HTTP_REFERER'] . "#nojs_popup|ckid:" . $key);
                die;
            }
            header('HTTP/1.0 200 OK');
            header("Content-type: application/json");
            echo json_encode($data);
            die;
        } else {
            $data['downloadurl'] = "";
            $data['error'] = '<div class="alert alert-danger">' . __('Invalid Email Address!', 'wpdmpro') . '</i>';
            if (!wpdm_is_ajax()) {
                @setcookie("wpdm_getlink_data_" . $key, json_encode($data));
                if (isset($data['downloadurl']) && $data['downloadurl'] != '') {
                    header("location: " . $data['downloadurl']);
                    die;
                }
                header("location: " . $_SERVER['HTTP_REFERER'] . "#nojs_popup|ckid:" . $key);
                die;
            }
            header("Content-type: application/json");
            echo json_encode($data);
            die;
        }
    }
    if (isset($_POST['force']) && $_POST['force'] != '') {
        $vr = explode('|', base64_decode($_POST['force']));
        if ($vr[0] == 'unlocked') {
            $social = array('f' => 'wpdm_fb_likes', 'g' => 'wpdm_gplus1s', 't' => 'wpdm_tweets', 'l' => 'wpdm_lishare');
            if ($_POST['social'] && isset($social[$_POST['social']])) {
                update_option($social[$_POST['social']], (int) get_option($social[$_POST['social']]) + 1);
            }
            update_post_meta($file['ID'], "__wpdmkey_" . $key, 3);
            $_SESSION['_wpdm_unlocked_' . $file['ID']] = 1;
            $data['downloadurl'] = wpdm_download_url($file, "_wpdmkey={$key}");
            $adata = apply_filters("wpdmgetlink", $data, $file);
            $data = is_array($adata) ? $adata : $data;
            if (!wpdm_is_ajax()) {
                @setcookie("wpdm_getlink_data_" . $key, json_encode($data));
                if (isset($data['downloadurl']) && $data['downloadurl'] != '') {
                    header("location: " . $data['downloadurl']);
                    die;
                }
                header("location: " . $_SERVER['HTTP_REFERER'] . "#nojs_popup|ckid:" . $key);
                die;
            }
            header("Content-type: application/json");
            die(json_encode($data));
        }
    }
    if ($plock == 1 && $password != $file['password'] && !strpos("__" . $file['password'], "[{$password}]")) {
        $data['error'] = __('Wrong Password!', 'wpdmpro');
        $file = array();
    }
    if ($plock == 1 && $password == '') {
        $data['error'] = __('Wrong Password!', 'wpdmpro');
        $file = array();
    }
    $ux = "";
    if ($plock == 1 && isset($file['ID'])) {
        update_post_meta($file['ID'], "__wpdmkey_" . $key, 3);
    }
    if (isset($file['ID']) && $file['ID'] != '') {
        $pu = isset($file['password_usage']) && is_array($file['password_usage']) ? $file['password_usage'] : array();
        $pul = $file['password_usage_limit'];
        if (is_array($pu) && isset($pu[$password]) && $pu[$password] >= $pul && $pul > 0) {
            $data['error'] = __('Password usages limit exceeded', 'wpdmpro');
        } else {
            if (!is_array($pu)) {
                $pu = array();
            }
            $pu[$password] = isset($pu[$password]) ? $pu[$password] + 1 : 1;
            update_post_meta($file['ID'], '__wpdm_password_usage', $pu);
        }
    }
    if (isset($_COOKIE['unlocked_' . $file1['ID']]) && $_COOKIE['unlocked_' . $file1['ID']] == 1) {
        $data['error'] = '';
        $file = $file1;
    }
    if ($data['error'] == '') {
        $_SESSION['_wpdm_unlocked_' . $file['ID']] = 1;
        $data['downloadurl'] = wpdm_download_url($file, "_wpdmkey={$key}");
    }
    // home_url('/?downloadkey='.md5($file['files']).'&file='.$id.$ux);
    $adata = apply_filters("wpdmgetlink", $data, $file);
    $data = is_array($adata) ? $adata : $data;
    if (!wpdm_is_ajax()) {
        @setcookie("wpdm_getlink_data_" . $key, json_encode($data));
        if (isset($data['downloadurl']) && $data['downloadurl'] != '') {
            header("location: " . $data['downloadurl']);
            die;
        }
        header("location: " . $_SERVER['HTTP_REFERER'] . "#nojs_popup|ckid:" . $key);
        die;
    }
    header("Content-type: application/json");
    die(json_encode($data));
}