public static function LogLeech($url) { $log = new LeechLog(); $log->url = $url; $log->ip = LogController::getIP(); $log->user_agent = LogController::getUA(); $log->save(); }
public static function DownloadFile() { $id = intval(Input::get('file_id')); $fileInfo = File::whereId($id)->whereBanned(false)->first(); if ($fileInfo == null) { abort(404); } $config = Config::whereKey('file_dir')->whereApplied(true)->first(); $sourceFile = $config->value . $fileInfo->file_path; $outFile = $fileInfo->filename; if (!is_file($sourceFile)) { abort(404); } LogController::LogDownload($id); $len = filesize($sourceFile); //获取文件大小 $filename = basename($sourceFile); //获取文件名字 $ctype = FileDownloadController::GetFileExtension($outFile); //echo $outFile; header("Cache-Control: public"); header("Content-Type: {$ctype}"); header("Content-Disposition: attachment; filename=" . $outFile); header("Accept-Ranges: bytes"); $size = filesize($sourceFile); $range = 0; if (isset($_SERVER['HTTP_RANGE'])) { list($a, $range) = explode("=", $_SERVER['HTTP_RANGE']); str_replace($range, "-", $range); $size2 = $size - 1; //文件总字节数 $new_length = $size2 - $range; header("HTTP/1.1 206 Partial Content"); header("Content-Length: {$new_length}"); header("Content-Range: bytes {$range}{$size2}/{$size}"); } else { $size2 = $size - 1; header("Content-Range: bytes 0-{$size2}/{$size}"); header("Content-Length: " . $size); } $fp = fopen("{$sourceFile}", "rb"); fseek($fp, $range); while (!feof($fp)) { set_time_limit(0); print fread($fp, 1024 * 8); flush(); ob_flush(); } fclose($fp); exit; }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $domain = Config::whereKey('domain')->whereApplied(true)->first(); if ($domain == null) { return $next($request); } if ($request->header('Referer') == "") { LogController::LogLeech($request->fullUrl()); die("禁止盗链"); } $referer = parse_url($request->header('Referer')); if ($referer['host'] != $domain->value) { LogController::LogLeech($request->fullUrl()); die("禁止盗链"); } return $next($request); }