/**
  * create_log function.
  *
  * @access public
  * @return void
  */
 public function create_log($type, $status, $message, $download, $version)
 {
     global $wpdb;
     $wpdb->hide_errors();
     $wpdb->insert($wpdb->download_log, array('type' => $type, 'user_id' => absint(get_current_user_id()), 'user_ip' => DLM_Utils::get_visitor_ip(), 'user_agent' => DLM_Utils::get_visitor_ua(), 'download_id' => absint($download->id), 'version_id' => absint($version->id), 'version' => $version->version, 'download_date' => current_time('mysql'), 'download_status' => $status, 'download_status_message' => $message), array('%s', '%d', '%s', '%s', '%d', '%d', '%s', '%s', '%s'));
     return $wpdb->insert_id;
 }
 /**
  * Load data from DB. Not the ideal way to do this but we can't break BC.
  *
  * @param int $version_id
  * @param int $download_id
  */
 private function setup($version_id, $download_id)
 {
     $this->id = absint($version_id);
     $this->download_id = absint($download_id);
     // Get Version Data
     $this->version = strtolower(get_post_meta($this->id, '_version', true));
     $this->download_count = get_post_meta($this->id, '_download_count', true);
     $this->filesize = get_post_meta($this->id, '_filesize', true);
     $this->md5 = get_post_meta($this->id, '_md5', true);
     $this->sha1 = get_post_meta($this->id, '_sha1', true);
     $this->crc32 = get_post_meta($this->id, '_crc32', true);
     $this->mirrors = get_post_meta($this->id, '_files', true);
     // Get URLS
     if (is_string($this->mirrors)) {
         $this->mirrors = array_filter((array) json_decode($this->mirrors));
     } elseif (is_array($this->mirrors)) {
         $this->mirrors = array_filter($this->mirrors);
     } else {
         $this->mirrors = array();
     }
     $this->url = current($this->mirrors);
     $this->filename = current(explode('?', DLM_Utils::basename($this->url)));
     $this->filetype = strtolower(substr(strrchr($this->filename, "."), 1));
     // If we don't have a filesize, lets get it now
     if ($this->filesize === "") {
         $this->filesize = $this->get_filesize($this->url);
     }
 }
 /**
  * Output download headers
  */
 private function download_headers($file_path, $download, $version)
 {
     global $is_IE;
     // Get Mime Type
     $mime_type = "application/octet-stream";
     foreach (get_allowed_mime_types() as $mime => $type) {
         $mimes = explode('|', $mime);
         if (in_array($version->filetype, $mimes)) {
             $mime_type = $type;
             break;
         }
     }
     // Get file name
     $file_name = urldecode(DLM_Utils::basename($file_path));
     if (strstr($file_name, '?')) {
         $file_name = current(explode('?', $file_name));
     }
     // Environment + headers
     if (!ini_get('safe_mode')) {
         @set_time_limit(0);
     }
     if (function_exists('get_magic_quotes_runtime') && get_magic_quotes_runtime()) {
         @set_magic_quotes_runtime(0);
     }
     if (function_exists('apache_setenv')) {
         @apache_setenv('no-gzip', 1);
     }
     @session_write_close();
     @ini_set('zlib.output_compression', 'Off');
     @error_reporting(0);
     /**
      * Prevents errors, for example: transfer closed with 3 bytes remaining to read
      */
     @ob_end_clean();
     // Clear the output buffer
     // Zip corruption fix
     while (ob_get_level() > 0) {
         @ob_end_clean();
     }
     $headers = array();
     if ($is_IE && is_ssl()) {
         // IE bug prevents download via SSL when Cache Control and Pragma no-cache headers set.
         $headers['Expires'] = 'Wed, 11 Jan 1984 05:00:00 GMT';
         $headers['Cache-Control'] = 'private';
     } else {
         nocache_headers();
     }
     $headers['X-Robots-Tag'] = 'noindex, nofollow';
     $headers['Content-Type'] = $mime_type;
     $headers['Content-Description'] = 'File Transfer';
     $headers['Content-Disposition'] = "attachment; filename=\"{$file_name}\";";
     $headers['Content-Transfer-Encoding'] = 'binary';
     if ($version->filesize) {
         $headers['Content-Length'] = $version->filesize;
         $headers['Accept-Ranges'] = 'bytes';
     }
     $headers = apply_filters('dlm_download_headers', $headers, $file_path, $download, $version);
     foreach ($headers as $key => $value) {
         header($key . ': ' . $value);
     }
 }
 /**
  * Check if visitor has downloaded version in the past 24 hours
  *
  * @param DLM_Download_Version $version
  *
  * @return bool
  */
 private function has_ip_downloaded_version($version)
 {
     global $wpdb;
     return absint($wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM {$wpdb->download_log} WHERE type = 'download' AND `version_id` = %d AND `user_ip` = %s", $version->id, DLM_Utils::get_visitor_ip()))) > 0;
 }