Zaczynamy od wygenerowania odpowiedniego linku w odtwarzaczu:
Zmienna $UCODE przyjmuje kod md5 z funkcji date("U"); Dodatkowo dodaje 2 cyfrową zmienną. To jest najprostsza forma utrudnienia hashu do rozpoznania - dowolnośc utrudnienia. Pamietać trzeba o odkodowaniu poprawnie czasu.
$m - nazwa pliku unikalna na film
$f - format pliku
następnie tworzymi plik ajax.php z funkcjami:
rangeDownload- funkcja buffurująca plik pobierany
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
private function rangeDownload($file) { $fp = @fopen($file, 'rb'); $size = filesize($file); // File size $length = $size; // Content length $start = 0; // Start byte $end = $size - 1; // End byte header("Accept-Ranges: 0-$length"); if (isset($_SERVER['HTTP_RANGE'])) { $c_start = $start; $c_end = $end; list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2); if (strpos($range, ',') !== false) { header('HTTP/1.1 416 Requested Range Not Satisfiable'); header("Content-Range: bytes $start-$end/$size"); exit; } if ($range0 == '-') { // The n-number of the last bytes is requested $c_start = $size - substr($range, 1); } else { $range = explode('-', $range); $c_start = $range[0]; $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size; } /* Check the range and make sure it's treated according to the specs. * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html */ $c_end = ($c_end > $end) ? $end : $c_end; if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) { header('HTTP/1.1 416 Requested Range Not Satisfiable'); header("Content-Range: bytes $start-$end/$size"); exit; } $start = $c_start; $end = $c_end; $length = $end - $start + 1; // Calculate new content length fseek($fp, $start); header('HTTP/1.1 206 Partial Content'); } header("Content-Range: bytes $start-$end/$size"); header("Content-Length: $length"); // Start buffered download $buffer = 1024 * 8; while(!feof($fp) && ($p = ftell($fp)) <= $end) { if ($p + $buffer > $end) { $buffer = $end - $p + 1; } set_time_limit(0); echo fread($fp, $buffer); flush(); // Free up memory. Otherwise large files will trigger PHP's memory limit. } fclose($fp); } |
Plik wykonujący i pozwalający na pobranie pliku:
ajax.php - wykonanie | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
$project = array( 'movie1'=>DIR_ROOT.'video/movie1', ); $view=false; $czas_hashu_roznicy = 5; // udzielasz czasu 5 sekund time-out if(isset($_GET['hash'])) for($i=-2; $i<$czas_hashu_roznicy; $i++){ if(md5(time()+$i) == substr($_GET['hash'], 0, -2)) $view=true; } if(!isset($_GET['m']) || !isset($_GET['f']) || empty($project[$_GET['m']]) || !$view){ header('HTTP/1.0 404 Not Found'); exit; } if($view){ $mine = mime_content_type($project[$_GET['m']].'.'.$_GET['f']); header("Content-Type: ".$mine); if (isset($_SERVER['HTTP_RANGE'])){ rangeDownload($project[$_GET['m']].'.'.$_GET['f']); }else{ $size = filesize($project[$_GET['m']].'.'.$_GET['f']); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".$size); header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); readfile($project[$_GET['m']].'.'.$_GET['f']); } } |