예제 #1
0
if ($ebd_item->download_id) {
    if (!$is_new_dm) {
        $dl = $wpdb->get_row("SELECT * FROM {$wp_dlm_db}  WHERE id = " . esc_sql($ebd_item->download_id) . ";");
        //another way of getting downloads from download monitor
        $downloads = get_downloads('include=' . $ebd_item->download_id . '');
        $d = new downloadable_file($dl);
        $file = $downloads[0]->url;
        //if the link is masked use the real path of the DM file
        if ($is_masked && function_exists('curl_init')) {
            $file = $d->filename;
        }
    } else {
        //$file = do_shortcode('[download_data id="'.$ebd_item->download_id.'" data="download_link"]');
        if ($is_masked && function_exists('curl_init')) {
            $dld = new DLM_Download($ebd_item->download_id);
            $file = $dld->get_file_version()->url;
            if (!isset($_SERVER['HTTP_RANGE'])) {
                $dld->get_file_version()->increase_download_count();
            }
        } else {
            $file = do_shortcode('[download_data id="' . $ebd_item->download_id . '" data="download_link"]');
        }
    }
}
$wpdb->update($table_link, array("is_downloaded" => 1), array("uid" => esc_sql($dId)));
//Check if the cUrl functions are available and the url hide option is enabled.
//If not, just rederect to real file url.
if ($is_masked && function_exists('curl_init')) {
    $filesize = 0;
    if ($dld == null) {
        $curl = curl_init();
 /**
  * custom_columns function.
  *
  * @access public
  * @param mixed $column
  * @return void
  */
 public function custom_columns($column)
 {
     global $post, $download_monitor;
     $download = new DLM_Download($post->ID);
     $file = $download->get_file_version();
     switch ($column) {
         case "thumb":
             echo $download->get_the_image();
             break;
         case "download_id":
             echo $post->ID;
             break;
         case "download_cat":
             if (!($terms = get_the_term_list($post->ID, 'dlm_download_category', '', ', ', ''))) {
                 echo '<span class="na">&ndash;</span>';
             } else {
                 echo $terms;
             }
             break;
         case "download_tag":
             if (!($terms = get_the_term_list($post->ID, 'dlm_download_tag', '', ', ', ''))) {
                 echo '<span class="na">&ndash;</span>';
             } else {
                 echo $terms;
             }
             break;
         case "featured":
             if ($download->is_featured()) {
                 echo '<span class="yes">' . __('Yes', 'download-monitor') . '</span>';
             } else {
                 echo '<span class="na">&ndash;</span>';
             }
             break;
         case "members_only":
             if ($download->is_members_only()) {
                 echo '<span class="yes">' . __('Yes', 'download-monitor') . '</span>';
             } else {
                 echo '<span class="na">&ndash;</span>';
             }
             break;
         case "redirect_only":
             if ($download->redirect_only()) {
                 echo '<span class="yes">' . __('Yes', 'download-monitor') . '</span>';
             } else {
                 echo '<span class="na">&ndash;</span>';
             }
             break;
         case "file":
             if ($file) {
                 echo '<a href="' . $download->get_the_download_link() . '"><code>' . $file->filename;
                 if ($size = $download->get_the_filesize()) {
                     echo ' &ndash; ' . $size;
                 }
                 echo '</code></a>';
             } else {
                 echo '<span class="na">&ndash;</span>';
             }
             break;
         case "version":
             if ($file && $file->version) {
                 echo $file->version;
             } else {
                 echo '<span class="na">&ndash;</span>';
             }
             break;
         case "download_count":
             echo number_format($download->get_the_download_count(), 0, '.', ',');
             break;
         case "featured":
             if ($download->is_featured()) {
                 echo '<img src="' . $download_monitor->plugin_url() . '/assets/images/on.png" alt="yes" />';
             } else {
                 echo '<span class="na">&ndash;</span>';
             }
             break;
     }
 }
function ebd_process_email_form($cf7)
{
    if (isset($_POST['_wpcf7_download_id'])) {
        global $wpdb, $wp_dlm_root, $wp_dlm_db, $wp_dlm_db_taxonomies, $def_format, $dlm_url, $downloadurl, $downloadtype, $wp_dlm_db_meta;
        //compatibility check for the Contact Form 7 plugin
        $is_new_cf7 = true;
        if (isset($cf7->posted_data)) {
            $is_new_cf7 = false;
        } else {
            $cf7->posted_data = $_POST;
        }
        $is_new_dm = false;
        $old_rep = error_reporting(E_ERROR | E_PARSE);
        $pd =& get_file_data(WP_PLUGIN_DIR . "/download-monitor/download-monitor.php", array("Version" => "Version"), 'plugin');
        if (!$pd['Version']) {
        } else {
            $is_new_dm = true;
        }
        $new = error_reporting($old_rep);
        //table names
        $table_item = $wpdb->prefix . "ebd_item";
        $table_link = $wpdb->prefix . "ebd_link";
        $table_posted_data = $wpdb->prefix . "ebd_posted_data";
        $delivered_as = get_option('email_before_download_send_email');
        $emailFrom = get_option('email_before_download_email_from');
        if (isset($_POST['email_from'])) {
            $emailFrom = htmlspecialchars_decode(urldecode($_POST['email_from']));
        }
        if (strlen($emailFrom) > 0) {
            $emailFrom = 'From: ' . $emailFrom . "\r\n";
        }
        $use_attachments = get_option('email_before_download_attachment');
        if (isset($_POST['delivered_as'])) {
            $delivered_as = $_POST['delivered_as'];
        }
        if (isset($_POST['attachment'])) {
            $use_attachments = trim($_POST['attachment']) == 'yes';
        }
        //check if email is allowed
        $email = $cf7->posted_data['your-email'];
        //compare email againts not allowed domains.
        $forbidden_domains = get_option('email_before_download_forbidden_domains');
        $domains = explode(',', $forbidden_domains);
        if (check_domains($email, $domains)) {
            $id = (int) $_POST['_wpcf7'];
            $unit_tag = $_POST['_wpcf7_unit_tag'];
            $items = array('mailSent' => false, 'into' => '#' . $unit_tag, 'captcha' => null);
            //error message
            $items['message'] = "The email that you provided is not allowed. Please provide another one.";
            $on_sent_ok = $cf7->additional_setting('on_sent_ok', false);
            $items['onSentOk'] = $on_sent_ok;
            $echo = json_encode($items);
            @header('Content-Type: application/json; charset=' . get_option('blog_charset'));
            echo $echo;
            die;
        }
        //get selected downloads
        $dIds = $_POST['ebd_downloads'];
        $dl_items = array();
        $multipleLinks = '';
        $message_mult = '';
        $attachments = array();
        $time_requested = time();
        $target = '_blank';
        //get all download monitor objects
        if ($dIds) {
            foreach ($dIds as $id) {
                if (!$is_new_dm) {
                    $dl_it = $wpdb->get_row("SELECT * FROM {$wp_dlm_db}  WHERE id = '" . esc_sql($id) . "';");
                    $dl_items[] = new downloadable_file($dl_it);
                } else {
                    $dl_it = new stdClass();
                    //$dl_it->title = do_shortcode('[download_data id="'.$id.'" data="title"]');
                    //$d->url  = do_shortcode('[download_data id="'.$id.'" data="filename"]');
                    $dl_tmp = new DLM_Download($id);
                    $dl_it->title = $dl_tmp->get_the_title();
                    $dl_it->filename = $dl_tmp->get_file_version()->url;
                    $dl_it->id = $id;
                    $dl_items[] = $dl_it;
                }
            }
        }
        //get edb items: it's common for all
        $dId = $_POST['_wpcf7_download_id'];
        $ebd_item = $wpdb->get_row("SELECT * FROM {$table_item}  WHERE id = '" . esc_sql($dId) . "';");
        $d = null;
        $dl = null;
        //get single download, multible are comma separated so the $dl for this will be NULL
        if (!$is_new_dm) {
            $dl = $wpdb->get_row("SELECT * FROM {$wp_dlm_db}  WHERE id = '" . esc_sql($ebd_item->download_id) . "';");
            $d = new downloadable_file($dl);
        } else {
            $d = new stdClass();
            $dl_tmp = new DLM_Download($ebd_item->download_id);
            $d->title = $dl_tmp->get_the_title();
            $d->filename = $dl_tmp->get_file_version()->url;
        }
        //variable for the title it wll be used only for the single downloads and the email subject
        $title = '';
        $title = $ebd_item->title;
        if ($title == NULL || $title == '') {
            if ($ebd_item->file) {
                $title = basename($ebd_item->file);
            } else {
                $title = $d->title;
            }
        }
        $url = '';
        //titles and urls for multiple
        $titles = array();
        $urls = array();
        $innerHtml = '';
        //if checkboxes were selected
        if (count($dl_items) > 0) {
            foreach ($dl_items as $dl_item) {
                //generate unique id for the file (link)
                $uid = md5(uniqid(rand(), true));
                //expiration date if needed if it's 0 or NULL the link will never expire
                $expireAt = 0;
                if (get_option('email_before_download_expire_time') != NULL && get_option('email_before_download_expire_time') != "0") {
                    $expireAt = strtotime(get_option('email_before_download_expire_time'));
                }
                $link_data = array();
                $link_data['uid'] = $uid;
                $link_data['selected_id'] = $dl_item->id;
                $link_data['expire_time'] = $expireAt;
                $link_data['time_requested'] = $time_requested;
                $link_data['email'] = $cf7->posted_data['your-email'];
                $link_data['item_id'] = $_POST['_wpcf7_download_id'];
                $link_data['delivered_as'] = $delivered_as;
                if (isset($_POST['masked'])) {
                    $link_data['is_masked'] = $_POST['masked'];
                }
                $wpdb->insert($table_link, $link_data);
                //
                $url = WP_PLUGIN_URL . "/email-before-download/download.php?dl=" . $uid;
                $titles[] = $dl_item->title;
                $title = implode($titles, '|');
                if (isset($_POST['format'])) {
                    $link = do_shortcode('[download id="' . $dl_item->id . '" format="' . $_POST['format'] . '"]');
                    $innerHtml .= $link . '<br />';
                } else {
                    $innerHtml .= '<a class="icon-button download-icon" target="' . $target . '" href="' . $url . '"><span class="et-icon"><span>' . addslashes($dl_item->title) . '</span></span></a><br />';
                }
                //       if(get_option('email_before_download_send_email') == 'Send Email' || get_option('email_before_download_send_email') == 'Both'){
                //       }
                if ($use_attachments) {
                    $dirs = wp_upload_dir();
                    $uploadpath = trailingslashit($dirs['baseurl']);
                    $absuploadpath = trailingslashit($dirs['basedir']);
                    $attachment = NULL;
                    if ($uploadpath && (strstr($dl_item->filename, $uploadpath) || strstr($dl_item->filename, $absuploadpath))) {
                        $file = str_replace($uploadpath, "", $dl_item->filename);
                        if (is_file($absuploadpath . $file)) {
                            $attachment = $absuploadpath . $file;
                        }
                    }
                    $attachments[] = $attachment;
                }
            }
        } else {
            if (!empty($d) || !empty($ebd_item->file)) {
                //generate unique id for the file (link)
                $uid = md5(uniqid(rand(), true));
                //expiration date if needed if it's 0 or NULL the link will never expire
                $expireAt = 0;
                if (get_option('email_before_download_expire_time') != NULL && get_option('email_before_download_expire_time') != "0") {
                    $expireAt = strtotime(get_option('email_before_download_expire_time'));
                }
                $link_data = array();
                $link_data['uid'] = $uid;
                $link_data['expire_time'] = $expireAt;
                $link_data['time_requested'] = $time_requested;
                $link_data['email'] = $cf7->posted_data['your-email'];
                $link_data['item_id'] = $_POST['_wpcf7_download_id'];
                $link_data['delivered_as'] = $delivered_as;
                $link_data['selected_id'] = 0;
                if (isset($_POST['masked'])) {
                    $link_data['is_masked'] = $_POST['masked'];
                }
                if (isset($_POST['force_download'])) {
                    $link_data['is_force_download'] = $_POST['force_download'];
                }
                $wpdb->insert($table_link, $link_data);
                if (isset($_POST['format']) && $ebd_item->download_id != NULL) {
                    $link = do_shortcode('[download id="' . $ebd_item->download_id . '" format="' . $_POST['format'] . '"]');
                    $innerHtml .= $link . '<br />';
                } else {
                    $url = WP_PLUGIN_URL . "/email-before-download/download.php?dl=" . $uid;
                    $innerHtml = '<a class="icon-button download-icon" target="' . $target . '" href="' . $url . '"><span class="et-icon"><span>' . addslashes($title) . '</span></span></a><br />';
                }
            } else {
                //we don't sent an email and throw an error
                $cf7->skip_mail = true;
                //this message doesn't seem to appear but we leave it for now
                if ($is_new_cf7) {
                    $additional_settings = $cf7->prop('additional_settings');
                    $additional_settings .= "on_sent_ok: \"document.getElementById('wpm_download_{$dId}').style.display = 'inline'; document.getElementById('wpm_download_{$dId}').innerHTML='You should select the files to dowload.'; \"";
                    $cf7->set_properties(array('additional_settings' => $additional_settings));
                } else {
                    $cf7->additional_settings .= "on_sent_ok: \"document.getElementById('wpm_download_{$dId}').style.display = 'inline'; document.getElementById('wpm_download_{$dId}').innerHTML='You should select the files to dowload.'; \"";
                }
                $id = (int) $_POST['_wpcf7'];
                $unit_tag = $_POST['_wpcf7_unit_tag'];
                $items = array('mailSent' => false, 'into' => '#' . $unit_tag, 'captcha' => null);
                //error message
                $items['message'] = "Please select at least one of the documents";
                $on_sent_ok = $cf7->additional_setting('on_sent_ok', false);
                $items['onSentOk'] = $on_sent_ok;
                $echo = json_encode($items);
                @header('Content-Type: application/json; charset=' . get_option('blog_charset'));
                echo $echo;
                die;
            }
        }
        $cf7->posted_data['your-message'] = 'The downloaded file name(s): ' . $title;
        if ($is_new_cf7) {
            $mail = $cf7->prop('mail');
            if (strpos($mail['body'], "[your-message]") === false) {
                $mail['body'] = $mail['body'] . "\nThe downloaded file name: {$title};";
                $cf7->posted_data['your-message'] = $title;
            } else {
                $mbody = $mail['body'];
                $mail['body'] = str_replace("[your-message]", 'The downloaded file name(s): ' . $title, $mbody);
            }
            $cf7->set_properties(array('mail' => $mail));
        } else {
            if (strpos($cf7->mail['body'], "[your-message]") === false) {
                $cf7->posted_data['your-message'] = $title;
                $cf7->mail['body'] = $cf7->mail['body'] . "\nThe downloaded file name: [your-message]";
            }
        }
        $target = get_option('email_before_download_link_target');
        $html_before = get_option('email_before_download_html_before_link');
        $html_after = get_option('email_before_download_html_after_link');
        //if multiple files are downloaded ???
        $message = '';
        if (count($dl_items) > 0) {
            $email_template = get_option('email_before_download_email_template_mult');
            if (strlen(trim($email_template)) > 0) {
                $message = str_replace(array('[requesting_name]', '[file_urls]'), array($cf7->posted_data['your-name'], $innerHtml), trim($email_template));
            } else {
                $message = $innerHtml;
            }
        } else {
            $email_template = get_option('email_before_download_email_template');
            if (strlen(trim($email_template)) > 0) {
                if (isset($_POST['format']) && $ebd_item->download_id != NULL) {
                    $message = 'You requested: ' . $innerHtml;
                } else {
                    $message = str_replace(array('[requesting_name]', '[file_url]', '[file_name]'), array($cf7->posted_data['your-name'], $url, $title), trim($email_template));
                }
            } else {
                if (isset($_POST['format']) && $ebd_item->download_id != NULL) {
                    $message = 'You requested: ' . $innerHtml;
                } else {
                    $message = '<a class="icon-button download-icon" target="' . $target . '" href="' . $url . '">' . $title . '</a>';
                }
            }
        }
        $innerHtml = $html_before . $innerHtml . $html_after;
        if ($delivered_as == 'Send Email') {
            // $attachments = NULL;
            if ($use_attachments && count($dl_items) == 0) {
                $dirs = wp_upload_dir();
                $uploadpath = trailingslashit($dirs['baseurl']);
                $absuploadpath = trailingslashit($dirs['basedir']);
                $attachment = NULL;
                if ($uploadpath && (strstr($d->filename, $uploadpath) || strstr($d->filename, $absuploadpath))) {
                    $file = str_replace($uploadpath, "", $d->filename);
                    if (is_file($absuploadpath . $file)) {
                        $attachment = $absuploadpath . $file;
                    }
                }
                $attachments = array($attachment);
            }
            if (count($attachments) == 0) {
                $attachments = NULL;
            }
            $email_subject = get_option('email_before_download_subject');
            if (strlen(trim($email_subject)) > 0) {
                $email_subject = str_replace('[files]', $title, $email_subject);
            } else {
                $email_subject = 'Requested URL for the file(s): ' . $title;
            }
            //email_before_download_subject
            @wp_mail($cf7->posted_data['your-email'], $email_subject, stripslashes($message), $emailFrom . "Content-Type: text/html\n", $attachments);
            if ($is_new_cf7) {
                $additional_settings = $cf7->prop('additional_settings');
                $additional_settings .= "\n" . "on_sent_ok: \"document.getElementById('wpm_download_{$dId}').style.display = 'inline'; document.getElementById('wpm_download_{$dId}').innerHTML='The link to the file(s) has been emailed to you.'; \"";
                $cf7->set_properties(array('additional_settings' => $additional_settings));
            } else {
                $cf7->additional_settings .= "\n" . "on_sent_ok: \"document.getElementById('wpm_download_{$dId}').style.display = 'inline'; document.getElementById('wpm_download_{$dId}').innerHTML='The link to the file(s) has been emailed to you.'; \"";
            }
        } else {
            if ($delivered_as == 'Both') {
                //$attachments = NULL;
                if ($use_attachments && count($dl_items) == 0) {
                    $dirs = wp_upload_dir();
                    $uploadpath = trailingslashit($dirs['baseurl']);
                    $absuploadpath = trailingslashit($dirs['basedir']);
                    $attachment = NULL;
                    if ($uploadpath && (strstr($d->filename, $uploadpath) || strstr($d->filename, $absuploadpath))) {
                        $file = str_replace($uploadpath, "", $d->filename);
                        if (is_file($absuploadpath . $file)) {
                            $attachment = $absuploadpath . $file;
                        }
                    }
                    $attachments = array($attachment);
                }
                if (count($attachments) == 0) {
                    $attachments = NULL;
                }
                $email_subject = get_option('email_before_download_subject');
                if (strlen(trim($email_subject)) > 0) {
                    $email_subject = str_replace('[files]', $title, $email_subject);
                } else {
                    $email_subject = 'Requested URL for the file(s): ' . $title;
                }
                @wp_mail($cf7->posted_data['your-email'], $email_subject, $message, $emailFrom . "Content-Type: text/html\n", $attachments);
                if ($is_new_cf7) {
                    $additional_settings = $cf7->prop('additional_settings');
                    $additional_settings .= "\n" . "on_sent_ok: \"document.getElementById('wpm_download_{$dId}').style.display = 'inline'; document.getElementById('wpm_download_{$dId}').innerHTML='{$innerHtml}'; \"";
                    $cf7->set_properties(array('additional_settings' => $additional_settings));
                } else {
                    $cf7->additional_settings .= "\n" . "on_sent_ok: \"document.getElementById('wpm_download_{$dId}').style.display = 'inline'; document.getElementById('wpm_download_{$dId}').innerHTML='{$innerHtml}'; \"";
                }
            } else {
                if ($is_new_cf7) {
                    $additional_settings = $cf7->prop('additional_settings');
                    $additional_settings .= "\n" . "on_sent_ok: \"document.getElementById('wpm_download_{$dId}').style.display = 'inline'; document.getElementById('wpm_download_{$dId}').innerHTML='{$innerHtml}'; \"";
                    $cf7->set_properties(array('additional_settings' => $additional_settings));
                } else {
                    $cf7->additional_settings .= "\n" . "on_sent_ok: \"document.getElementById('wpm_download_{$dId}').style.display = 'inline'; document.getElementById('wpm_download_{$dId}').innerHTML='{$innerHtml}'; \"";
                }
            }
        }
        // save the extra form information into the xml
        $xml = new SimpleXMLElement('<posted_data></posted_data>');
        foreach ($cf7->posted_data as $key => $value) {
            if (is_array($value)) {
                $value = implode(',', $value);
            }
            $xml->addChild($key, htmlentities($value, ENT_QUOTES, 'utf-8'));
            //encode some chars like '&'
        }
        $posted_data = array();
        $posted_data['time_requested'] = $time_requested;
        $posted_data['posted_data'] = $xml->asXML();
        $posted_data['email'] = $cf7->posted_data['your-email'];
        $posted_data['user_name'] = $cf7->posted_data['your-name'];
        $wpdb->insert($table_posted_data, $posted_data);
    }
    return $cf7;
}