<?php
require_once('../wu_init.php');
if (!USER_LOGGED) { exit('Вы не авторизованы'); }

$id = intval($_GET['id']);
$ok = mysqli_query($connect_db, "SELECT id FROM ".DB_PREFIX."_es_b WHERE usr='$u_id' AND m='$id'");
$okey = mysqli_num_rows($ok);
if ($okey > 0) {
$sec = mysqli_fetch_assoc(mysqli_query($connect_db, "SELECT id,ty,fi,sec FROM ".DB_PREFIX."_es WHERE id='$id'"));
if ($sec['ty'] == 1) { exit('Неверный тип товара'); }
$fsize=filesize('../../eshop/items/'.$sec['sec']);
$fdown='../../eshop/items/'.$sec['sec'];
if (getenv('HTTP_RANGE')=="") {
$f=fopen($fdown, 'r');
if (ob_get_level()) {
ob_end_clean();
}
header("HTTP/1.1 200 OK");
header("Connection: close");
header("Content-Type: application/rar");
header("Accept-Ranges: bytes");
header("Content-Disposition: Attachment; filename=".$sec['fi']);
header("Content-Length: ".$fsize); 
while (!feof($f)) {
if (connection_aborted()) {
fclose($f);
break;
}
echo fread($f, 1000000);
sleep(1);
}
fclose($f);
}
else {
preg_match ("/bytes=(\d+)-/", getenv('HTTP_RANGE'), $m);
$csize=$fsize-$m[1]; // Размер фрагмента
$p1=$fsize-$csize; // Позиция, с которой начинать чтение файла
$p2=$fsize-1; // Конец фрагмента
$f=fopen($fdown, 'r');
if (ob_get_level()) {
ob_end_clean();
}
header("HTTP/1.1 206 Partial Content");
header("Connection: close");
header("Content-Type: application/rar");
header("Accept-Ranges: bytes");
header("Content-Disposition: Attachment; filename=".$sec['fi']);
header("Content-Range: bytes ".$p1."-".$p2."/".$fsize);
header("Content-Length: ".$csize);
fseek ($f, $p1);
while (!feof($f)) {
if (connection_aborted()) {
fclose($f);
break;
}
echo fread($f, 1000000);
sleep(1);
}
fclose($f);
}
} else {
echo 'Вы не покупали данный товар';
}
?>