function __construct() { wpfb_loadclass('Download', 'Admin'); $dir = WPFB_Core::UploadDir() . '/.tmp/'; WPFB_Admin::Mkdir($dir); $test_files = array('banner.png' => 'https://wpfilebase.com/wp-content/blogs.dir/2/files/2015/03/banner_023.png', 'small.txt' => 'https://wpfilebase.com/robots.txt'); $this->local_files = array(); foreach ($test_files as $f => $u) { $fn = $dir . $f; $this->local_files[$f] = $fn; if (file_exists($fn)) { continue; } echo "Downloading test file {$u}\n"; WPFB_Download::SideloadFile($u, $fn); } }
static function SendFile($file_path, $args = array()) { global $wpdb; $defaults = array('bandwidth' => 0, 'etag' => null, 'force_download' => WPFB_Core::$settings->force_download, '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!', 'wp-filebase')); } list($begin, $end) = self::ParseRangeHeader($size); if ($begin > 0 || $end < $size - 1) { header('HTTP/1.0 206 Partial Content'); header("Content-Range: bytes {$begin}-{$end}/{$size}"); } else { header('HTTP/1.0 200 OK'); } $length = $end - $begin + 1; WPFB_Download::AddTraffic($length); if (self::ShouldSendRangeHeader($file_path, $file_type)) { header("Accept-Ranges: bytes"); } $request_file_name = basename(urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))); $filename_set = !empty($filename); if (!$filename_set) { $filename = basename($file_path); } // content headers if ($force_download) { header("Content-Disposition: attachment; filename=\"{$filename}\""); header("Content-Description: File Transfer"); } elseif ($filename != $request_file_name) { header("Content-Disposition: inline; filename=\"{$filename}\""); } header("Content-Length: " . $length); // clean up things that are not needed for download @session_write_close(); // disable blocking of multiple downloads at the same time // close db connection if (method_exists($wpdb, 'close')) { $wpdb->close(); } elseif (function_exists('mysql_close')) { if (!empty($wpdb->dbh) && is_resource($wpdb->dbh)) { @mysql_close($wpdb->dbh); } else { @mysql_close(); } } @ob_flush(); @flush(); // 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 / 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; }
public static function SideloadFile($url, $dest_file = null, $size_for_progress = 0) { //WARNING: The file is not automatically deleted, The script must unlink() the file. @ini_set('max_execution_time', '0'); @set_time_limit(0); require_once ABSPATH . 'wp-admin/includes/file.php'; if (!$url) { return array('error' => __('Invalid URL Provided.')); } if (empty($dest_file)) { // if no dest file set, create temp file $fi = self::GetRemoteFileInfo($url); if (empty($fi)) { return array('error' => sprintf(__('Could not get file information from %s!', WPFB), $url)); } if (!($dest_file = self::GetTmpFile($fi['name']))) { return array('error' => __('Could not create Temporary file.')); } } if ($size_for_progress >= self::$MIN_SIZE_FOR_PROGRESSBAR) { if (!class_exists('progressbar')) { include_once WPFB_PLUGIN_ROOT . 'extras/progressbar.class.php'; } $progress_bar = new progressbar(0, $size_for_progress, 300, 30, '#aaa'); echo "<p><code>" . esc_html($url) . "</code> ...</p>"; $progress_bar->print_code(); } else { $progress_bar = null; } wpfb_loadclass('Download'); $result = WPFB_Download::SideloadFile($url, $dest_file, $progress_bar); if (is_array($result) && !empty($result['error'])) { return $result; } return array('error' => false, 'file' => $dest_file); }
function Download() { global $wpdb, $current_user, $user_ID; @error_reporting(0); wpfb_loadclass('Category', 'Download'); $downloader_ip = preg_replace('/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR']); get_currentuserinfo(); $logged_in = !empty($user_ID); $user_role = $logged_in ? reset($current_user->roles) : null; // get user's highest role (like in user-eidt.php) $is_admin = current_user_can('manage_options'); // check user level if (!$this->CurUserCanAccess()) { $this->DownloadDenied('inaccessible_msg'); } // check offline if ($this->file_offline && !$is_admin) { wp_die(WPFB_Core::$settings->file_offline_msg); } // check referrer if ($this->file_direct_linking != 1) { // if referer check failed, redirect to the file post if (!WPFB_Download::RefererCheck()) { $url = WPFB_Core::GetPostUrl($this->file_post_id); if (empty($url)) { $url = home_url(); } wp_redirect($url); exit; } } // check traffic if ($this->IsLocal() && !WPFB_Download::CheckTraffic($this->file_size)) { header('HTTP/1.x 503 Service Unavailable'); wp_die(WPFB_Core::$settings->traffic_exceeded_msg); } // check daily user limit if (!$is_admin && WPFB_Core::$settings->daily_user_limits) { if (!$logged_in) { $this->DownloadDenied('inaccessible_msg'); } $today = intval(date('z')); $usr_dls_today = intval(get_user_option(WPFB_OPT_NAME . '_dls_today')); $usr_last_dl_day = intval(date('z', intval(get_user_option(WPFB_OPT_NAME . '_last_dl')))); if ($today != $usr_last_dl_day) { $usr_dls_today = 0; } // check for limit $dl_limit = intval(WPFB_Core::GetOpt('daily_limit_' . $user_role)); if ($dl_limit > 0 && $usr_dls_today >= $dl_limit) { $this->DownloadDenied(sprintf(WPFB_Core::$settings->daily_limit_exceeded_msg, $dl_limit)); } $usr_dls_today++; update_user_option($user_ID, WPFB_OPT_NAME . '_dls_today', $usr_dls_today); update_user_option($user_ID, WPFB_OPT_NAME . '_last_dl', time()); } // count download if (!$is_admin || !WPFB_Core::$settings->ignore_admin_dls) { $last_dl_time = mysql2date('U', $this->file_last_dl_time, false); if (empty($this->file_last_dl_ip) || $this->file_last_dl_ip != $downloader_ip || time() - $last_dl_time > 86400) { $wpdb->query("UPDATE " . $wpdb->wpfilebase_files . " SET file_hits = file_hits + 1, file_last_dl_ip = '" . $downloader_ip . "', file_last_dl_time = '" . current_time('mysql') . "' WHERE file_id = " . (int) $this->file_id); } } // external hooks do_action('wpfilebase_file_downloaded', $this->file_id); $url = $this->GetRemoteUri(); $is_local_remote = !empty($url) && parse_url($url, PHP_URL_SCHEME) === 'file' && is_readable($url); // download or redirect if ($this->IsLocal() || $is_local_remote) { $bw = 'bitrate_' . ($logged_in ? 'registered' : 'unregistered'); WPFB_Download::SendFile($is_local_remote ? $url : $this->GetLocalPath(), array('bandwidth' => WPFB_Core::$settings->{$bw}, 'etag' => $this->file_hash, 'md5_hash' => WPFB_Core::$settings->fake_md5 ? null : $this->file_hash, 'force_download' => WPFB_Core::$settings->force_download || $this->file_force_download, 'cache_max_age' => 10, 'filename' => empty($this->file_name_original) ? $this->file_name : $this->file_name_original)); } else { //header('HTTP/1.1 301 Moved Permanently'); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Expires: Thu, 01 Jan 1970 00:00:00 GMT'); header('Location: ' . $url); } exit; }
<?php define('FASTLOAD', true); require_once 'wpfb-load.php'; wpfb_loadclass('Core', 'File', 'Category', 'Download'); $item = null; if (isset($_GET['fid'])) { $fid = intval($_GET['fid']); if ($fid == 0) { $img_path = ABSPATH . WPINC . '/images/'; if (file_exists($img = $img_path . 'crystal/default.png') || file_exists($img = $img_path . 'default.png') || file_exists($img = $img_path . 'blank.gif')) { WPFB_Download::SendFile($img, array('cache_max_age' => -1)); } //was 3600 * 12 exit; } $item = WPFB_File::GetFile($fid); } elseif (isset($_GET['cid'])) { $item = WPFB_Category::GetCat(intval($_GET['cid'])); } if ($item == null || !$item->CurUserCanAccess(true)) { exit; } // if no thumbnail, redirect if (empty($item->file_thumbnail) && empty($item->cat_icon)) { header('Location: ' . $item->GetIconUrl()); exit; } // send thumbnail WPFB_Download::SendFile($item->GetThumbPath(), array('cache_max_age' => -1)); // was 3600 * 12