/** * Create a log if logging is enabled */ private function log($type = '', $status = '', $message = '', $download, $version) { if (function_exists('dlm_create_log')) { dlm_create_log($type, $status, $message, $download, $version); } }
/** * trigger function. * * @access private * @param mixed $download * @return void */ private function trigger($download) { global $is_IE; $version = $download->get_file_version(); $file_paths = $version->mirrors; if (empty($file_paths)) { wp_die(__('No file paths defined.', 'download_monitor') . ' <a href="' . home_url() . '">' . __('Go to homepage →', 'download_monitor') . '</a>', __('Download Error', 'download_monitor')); } $file_path = $file_paths[array_rand($file_paths)]; if (!$file_path) { wp_die(__('No file paths defined.', 'download_monitor') . ' <a href="' . home_url() . '">' . __('Go to homepage →', 'download_monitor') . '</a>', __('Download Error', 'download_monitor')); } // Check Access if (!apply_filters('dlm_can_download', true, $download, $version)) { if ($redirect = apply_filters('dlm_access_denied_redirect', false)) { wp_redirect($redirect); } else { wp_die(__('You do not have permission to access this download.', 'download_monitor') . ' <a href="' . home_url() . '">' . __('Go to homepage →', 'download_monitor') . '</a>', __('Download Error', 'download_monitor'), array('response' => 200)); } exit; } if (empty($_COOKIE['wp_dlm_downloading']) || $download->id != $_COOKIE['wp_dlm_downloading']) { // Increase download count $version->increase_download_count(); // Trigger Download Action do_action('dlm_downloading', $download, $version, $file_path); // Set cookie to prevent double logging setcookie('wp_dlm_downloading', $download->id, time() + 60, COOKIEPATH, COOKIE_DOMAIN, false); // Logging $this->log = function_exists('dlm_create_log'); } else { $this->log = false; } // Redirect to the file... if ($download->redirect_only() || apply_filters('dlm_do_not_force', false, $download, $version)) { if ($this->log) { dlm_create_log('download', 'redirected', __('Redirected to file', 'download_monitor'), $download, $version); } $file_path = str_replace(ABSPATH, site_url('/', 'http'), $file_path); header('Location: ' . $file_path); exit; } // ...or serve it if (!is_multisite()) { $file_path = str_replace(site_url('/', 'https'), ABSPATH, $file_path); $file_path = str_replace(site_url('/', 'http'), ABSPATH, $file_path); } else { // Try to replace network url $file_path = str_replace(network_admin_url('/', 'https'), ABSPATH, $file_path); $file_path = str_replace(network_admin_url('/', 'http'), ABSPATH, $file_path); // Try to replace upload URL $upload_dir = wp_upload_dir(); $file_path = str_replace($upload_dir['baseurl'], $upload_dir['basedir'], $file_path); } // See if its local or remote if (strstr($file_path, 'http:') || strstr($file_path, 'https:') || strstr($file_path, 'ftp:')) { $remote_file = true; } else { $remote_file = false; $real_file_path = realpath(current(explode('?', $file_path))); if (!empty($real_file_path)) { $file_path = $real_file_path; } // See if we need to add abspath if this is a relative URL if (!file_exists($file_path) && file_exists(ABSPATH . $file_path)) { $file_path = ABSPATH . $file_path; } } // 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 = 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); @ob_clean(); // Clear the output buffer if ($is_IE && is_ssl()) { // IE bug prevents download via SSL when Cache Control and Pragma no-cache headers set. header('Expires: Wed, 11 Jan 1984 05:00:00 GMT'); header('Cache-Control: private'); } else { nocache_headers(); } header("X-Robots-Tag: noindex, nofollow", true); header("Content-Type: " . $mime_type); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=\"" . $file_name . "\";"); header("Content-Transfer-Encoding: binary"); if ($version->filesize) { header("Content-Length: " . $version->filesize); header("Accept-Ranges: bytes"); } if (get_option('dlm_xsendfile_enabled')) { if (getcwd()) { $file_path = trim(preg_replace('`^' . str_replace('\\', '/', getcwd()) . '`', '', $file_path), '/'); } if (function_exists('apache_get_modules') && in_array('mod_xsendfile', apache_get_modules())) { if ($this->log) { dlm_create_log('download', 'redirected', __('Redirected to file', 'download_monitor'), $download, $version); } header("X-Sendfile: {$file_path}"); exit; } elseif (stristr(getenv('SERVER_SOFTWARE'), 'lighttpd')) { if ($this->log) { dlm_create_log('download', 'redirected', __('Redirected to file', 'download_monitor'), $download, $version); } header("X-LIGHTTPD-send-file: {$file_path}"); exit; } elseif (stristr(getenv('SERVER_SOFTWARE'), 'nginx') || stristr(getenv('SERVER_SOFTWARE'), 'cherokee')) { if ($this->log) { dlm_create_log('download', 'redirected', __('Redirected to file', 'download_monitor'), $download, $version); } header("X-Accel-Redirect: /{$file_path}"); exit; } } // multipart-download and download resuming support - http://www.phpgang.com/force-to-download-a-file-in-php_112.html if (isset($_SERVER['HTTP_RANGE']) && $version->filesize) { list($a, $range) = explode("=", $_SERVER['HTTP_RANGE'], 2); list($range) = explode(",", $range, 2); list($range, $range_end) = explode("-", $range); $range = intval($range); if (!$range_end) { $range_end = $version->filesize - 1; } else { $range_end = intval($range_end); } $new_length = $range_end - $range + 1; header("HTTP/1.1 206 Partial Content"); header("Content-Length: {$new_length}"); header("Content-Range: bytes {$range}-{$range_end}/{$version->filesize}"); } else { $range = false; } if ($this->readfile_chunked($file_path, $range)) { // Complete! if ($this->log) { dlm_create_log('download', 'completed', '', $download, $version); } } elseif ($remote_file) { // Redirect - we can't track if this completes or not if ($this->log) { dlm_create_log('download', 'redirected', __('Redirected to remote file.', 'download_monitor'), $download, $version); } header('Location: ' . $file_path); } else { if ($this->log) { dlm_create_log('download', 'failed', __('File not found', 'download_monitor'), $download, $version); } wp_die(__('File not found.', 'download_monitor') . ' <a href="' . home_url() . '">' . __('Go to homepage →', 'download_monitor') . '</a>', __('Download Error', 'download_monitor'), array('response' => 404)); } exit; }