public static function downloadPublic($id)
{
$file = self::getDownloadFilePublic((int) $id);
$app = Factory::getApplication();
// Clears file status cache
clearstatcache();
$pathFile = PhocacartPath::getPath('publicfile');
$absOrRelFile = $pathFile['orig_abs_ds'] . $file->public_download_file;
if (!File::exists($absOrRelFile)) {
return false;
}
$fileWithoutPath = basename($absOrRelFile);
$fileSize = filesize($absOrRelFile);
if (function_exists('finfo_open') && function_exists('finfo_open') && function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$f = finfo_file($finfo, $absOrRelFile);
finfo_close($finfo);
$mimeType = $f;
} else {
if (function_exists('mime_content_type')) {
// we have mime magic
$mimeType = mime_content_type($absOrRelFile);
} else {
$mimeType = '';
}
}
if ($fileSize == 0) {
die(Text::_('COM_PHOCACART_FILE_SIZE_EMPTY'));
exit;
}
// Clean the output buffer
ob_end_clean();
// test for protocol and set the appropriate headers
jimport('joomla.environment.uri');
$_tmp_uri = Uri::getInstance(Uri::current());
$_tmp_protocol = $_tmp_uri->getScheme();
if ($_tmp_protocol == "https") {
// SSL Support
header('Cache-Control: private, max-age=0, must-revalidate, no-store');
} else {
header("Cache-Control: public, must-revalidate");
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header("Pragma: no-cache");
header("Expires: 0");
}
/* end if protocol https */
header("Content-Description: File Transfer");
header("Expires: Sat, 30 Dec 1990 07:07:07 GMT");
header("Accept-Ranges: bytes");
// Modified by Rene
// HTTP Range - see RFC2616 for more informations (http://www.ietf.org/rfc/rfc2616.txt)
$httpRange = 0;
$newFileSize = $fileSize - 1;
// Default values! Will be overridden if a valid range header field was detected!
$resultLength = (string) $fileSize;
$resultRange = "0-" . $newFileSize;
// We support requests for a single range only.
// So we check if we have a range field. If yes ensure that it is a valid one.
// If it is not valid we ignore it and sending the whole file.
if (isset($_SERVER['HTTP_RANGE']) && preg_match('%^bytes=\\d*\\-\\d*$%', $_SERVER['HTTP_RANGE'])) {
// Let's take the right side
list($a, $httpRange) = explode('=', $_SERVER['HTTP_RANGE']);
// and get the two values (as strings!)
$httpRange = explode('-', $httpRange);
// Check if we have values! If not we have nothing to do!
if (!empty($httpRange[0]) || !empty($httpRange[1])) {
// We need the new content length ...
$resultLength = $fileSize - $httpRange[0] - $httpRange[1];
// ... and we can add the 206 Status.
header("HTTP/1.1 206 Partial Content");
// Now we need the content-range, so we have to build it depending on the given range!
// ex.: -500 -> the last 500 bytes
if (empty($httpRange[0])) {
$resultRange = $resultLength . '-' . $newFileSize;
} elseif (empty($httpRange[1])) {
$resultRange = $httpRange[0] . '-' . $newFileSize;
} else {
$resultRange = $httpRange[0] . '-' . $httpRange[1];
}
//header("Content-Range: bytes ".$httpRange . $newFileSize .'/'. $fileSize);
}
}
header("Content-Length: " . $resultLength);
header("Content-Range: bytes " . $resultRange . '/' . $fileSize);
header("Content-Type: " . (string) $mimeType);
header('Content-Disposition: attachment; filename="' . $fileWithoutPath . '"');
header("Content-Transfer-Encoding: binary\n");
//@readfile($absOrRelFile);
// Try to deliver in chunks
@set_time_limit(0);
$fp = @fopen($absOrRelFile, 'rb');
if ($fp !== false) {
while (!feof($fp)) {
echo fread($fp, 8192);
}
fclose($fp);
} else {
@readfile($absOrRelFile);
}
flush();
exit;
}