static function SendFile($file_path, $args = array()) { $defaults = array('bandwidth' => 0, 'etag' => null, 'force_download' => false, 'cache_max_age' => 0, 'md5_hash' => null, 'filename' => null); extract(wp_parse_args($args, $defaults), EXTR_SKIP); @ini_set('max_execution_time', '0'); @set_time_limit(0); @error_reporting(0); while (@ob_end_clean()) { } $no_cache = WPFB_Core::$settings->http_nocache && $cache_max_age != 0; @ini_set("zlib.output_compression", "Off"); // remove some headers if (function_exists('header_remove')) { header_remove(); } else { header("Expires: "); header("X-Pingback: "); } if (!@file_exists($file_path) || !is_file($file_path)) { header('HTTP/1.x 404 Not Found'); wp_die('File ' . basename($file_path) . ' not found!'); } wpfb_loadclass('FileUtils'); $size = WPFB_FileUtils::GetFileSize($file_path); $time = filemtime($file_path); $file_type = WPFB_Download::GetFileType($file_path); if (empty($etag)) { $etag = md5("{$size}|{$time}|{$file_type}"); } else { $etag = trim($etag, '"'); } // set basic headers if ($no_cache) { header("Cache-Control: no-cache, must-revalidate, max-age=0"); header("Pragma: no-cache"); header("Expires: Wed, 11 Jan 1984 05:00:00 GMT"); } elseif ($cache_max_age > 0) { header("Cache-Control: must-revalidate, max-age={$cache_max_age}"); } elseif ($cache_max_age == -1) { header("Cache-Control: public"); } //header("Connection: close"); //header("Keep-Alive: timeout=5, max=100"); //header("Connection: Keep-Alive"); header("Content-Type: " . $file_type . (strpos($file_type, 'text/') !== false ? '; charset=' : '')); // charset fix header("Last-Modified: " . gmdate("D, d M Y H:i:s", $no_cache ? time() : $time) . " GMT"); if (!empty($md5_hash) && $md5_hash[0] != '#') { // check if fake md5 $pmd5 = @pack('H32', $md5_hash); if (!empty($pmd5)) { header("Content-MD5: " . @base64_encode($pmd5)); } } if (!$no_cache) { header("ETag: \"{$etag}\""); $if_mod_since = !empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false; $if_none_match = !empty($_SERVER['HTTP_IF_NONE_MATCH']) ? $etag == trim($_SERVER['HTTP_IF_NONE_MATCH'], '"') : false; if ($if_mod_since || $if_none_match) { $not_modified = true; if ($not_modified && $if_mod_since) { $not_modified = @strtotime($if_mod_since) >= $time; } if ($not_modified && $if_none_match) { $not_modified = $if_none_match == $etag; } if ($not_modified) { header("Content-Length: " . $size); header("HTTP/1.x 304 Not Modified"); exit; } } } if (!($fh = @fopen($file_path, 'rb'))) { wp_die(__('Could not read file!', WPFB)); } $begin = 0; $end = $size - 1; $http_range = isset($_SERVER['HTTP_RANGE']) ? $_SERVER['HTTP_RANGE'] : ''; if (!empty($http_range) && strpos($http_range, 'bytes=') !== false && strpos($http_range, ',') === false) { $range = array_map('trim', explode('-', trim(substr($http_range, 6)))); if (is_numeric($range[0])) { $begin = 0 + $range[0]; if (is_numeric($range[1])) { $end = 0 + $range[1]; } } else { $begin = $size - $range[1]; // format "-x": last x bytes } } else { $http_range = ''; } if ($begin > 0 || $end < $size - 1) { header('HTTP/1.0 206 Partial Content'); } else { header('HTTP/1.0 200 OK'); } $length = $end - $begin + 1; WPFB_Download::AddTraffic($length); if (WPFB_Download::ShouldSendRangeHeader($file_path, $file_type)) { header("Accept-Ranges: bytes"); } // content headers if (!empty($force_download) || WPFB_Download::ShouldSendDLHeader($file_path, $file_type) || !empty($filename)) { header("Content-Disposition: attachment; filename=\"" . (empty($filename) ? basename($file_path) : $filename) . "\""); header("Content-Description: File Transfer"); } header("Content-Length: " . $length); if (!empty($http_range)) { header("Content-Range: bytes {$begin}-{$end}/{$size}"); } // clean up things that are not needed for download @session_write_close(); // disable blocking of multiple downloads at the same time global $wpdb; if (!empty($wpdb->dbh) && is_resource($wpdb->dbh)) { @mysql_close($wpdb->dbh); } else { @mysql_close(); } @ob_flush(); @flush(); //if(WPFB_Core::$settings->dl_destroy_session) // @session_destroy(); // ready to send the file! if ($begin > 0) { fseek($fh, $begin, 0); } if (WPFB_Core::$settings->use_fpassthru) { fpassthru($fh); } else { $bandwidth = empty($bandwidth) ? 0 : (double) $bandwidth; if ($bandwidth <= 0) { $bandwidth = 1024 * 1024; } $buffer_size = (int) (1024 * min($bandwidth, 64)); // convert kib/s => bytes/ms $bandwidth *= 1024; $bandwidth /= 1000; $cur = $begin; while (!@feof($fh) && $cur <= $end && @connection_status() == 0) { $nbytes = min($buffer_size, $end - $cur + 1); $ts = microtime(true); print @fread($fh, $nbytes); @ob_flush(); @flush(); $dt = (microtime(true) - $ts) * 1000; // dt = time delta in ms $st = $nbytes / $bandwidth - $dt; if ($st > 0) { usleep($st * 1000); } $cur += $nbytes; } } @fclose($fh); return true; }