ZwiiCMS/core/vendor/filemanager/force_download.php

128 lines
3.4 KiB
PHP
Raw Normal View History

2018-04-02 08:29:19 +02:00
<?php
$config = include 'config/config.php';
include 'include/utils.php';
2019-01-11 09:59:01 +01:00
include 'include/mime_type_lib.php';
2018-04-02 08:29:19 +02:00
2019-01-11 09:59:01 +01:00
if ($_SESSION['RF']["verify"] != "RESPONSIVEfilemanager") {
response(trans('forbidden') . AddErrorLocation(), 403)->send();
2019-01-11 09:59:01 +01:00
exit;
2018-04-02 08:29:19 +02:00
}
if (!checkRelativePath($_POST['path']) || strpos($_POST['path'], '/') === 0) {
response(trans('wrong path') . AddErrorLocation(), 400)->send();
2019-01-11 09:59:01 +01:00
exit;
2018-04-02 08:29:19 +02:00
}
2019-01-11 09:59:01 +01:00
if (strpos($_POST['name'], '/') !== false) {
response(trans('wrong path') . AddErrorLocation(), 400)->send();
2019-01-11 09:59:01 +01:00
exit;
2018-04-02 08:29:19 +02:00
}
2019-01-11 09:59:01 +01:00
$ftp = ftp_con($config);
2019-01-11 09:59:01 +01:00
if ($ftp) {
$path = $config['ftp_base_url'] . $config['upload_dir'] . $_POST['path'];
} else {
$path = $config['current_path'] . $_POST['path'];
2018-04-02 08:29:19 +02:00
}
$name = $_POST['name'];
$info = pathinfo($name);
2019-01-11 09:59:01 +01:00
if (!check_extension($info['extension'], $config)) {
response(trans('wrong extension') . AddErrorLocation(), 400)->send();
2019-01-11 09:59:01 +01:00
exit;
2018-04-02 08:29:19 +02:00
}
$file_name = $info['basename'];
$file_ext = $info['extension'];
$file_path = $path . $name;
2018-04-02 08:29:19 +02:00
2019-01-11 09:59:01 +01:00
2018-04-02 08:29:19 +02:00
// make sure the file exists
2019-01-11 09:59:01 +01:00
if ($ftp) {
2018-04-02 08:29:19 +02:00
header('Content-Type: application/octet-stream');
2019-01-11 09:59:01 +01:00
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . $file_name . "\"");
2018-04-02 08:29:19 +02:00
readfile($file_path);
2019-01-11 09:59:01 +01:00
} elseif (is_file($file_path) && is_readable($file_path)) {
if (!file_exists($path . $name)) {
response(trans('File_Not_Found') . AddErrorLocation(), 404)->send();
2018-04-02 08:29:19 +02:00
exit;
}
$size = filesize($file_path);
$file_name = rawurldecode($file_name);
2019-01-11 09:59:01 +01:00
if (function_exists('mime_content_type')) {
2018-04-02 08:29:19 +02:00
$mime_type = mime_content_type($file_path);
2019-01-11 09:59:01 +01:00
} elseif (function_exists('finfo_open')) {
2018-04-02 08:29:19 +02:00
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $file_path);
2019-01-11 09:59:01 +01:00
} else {
2018-04-02 08:29:19 +02:00
$mime_type = get_file_mime_type($file_path);
}
2019-01-11 09:59:01 +01:00
2018-04-02 08:29:19 +02:00
@ob_end_clean();
2019-01-11 09:59:01 +01:00
if (ini_get('zlib.output_compression')) {
2018-04-02 08:29:19 +02:00
ini_set('zlib.output_compression', 'Off');
}
header('Content-Type: ' . $mime_type);
2019-01-11 09:59:01 +01:00
header('Content-Disposition: attachment; filename="' . $file_name . '"');
2018-04-02 08:29:19 +02:00
header("Content-Transfer-Encoding: binary");
header('Accept-Ranges: bytes');
2019-01-11 09:59:01 +01:00
if (isset($_SERVER['HTTP_RANGE'])) {
list($a, $range) = explode("=", $_SERVER['HTTP_RANGE'], 2);
list($range) = explode(",", $range, 2);
2018-04-02 08:29:19 +02:00
list($range, $range_end) = explode("-", $range);
2019-01-11 09:59:01 +01:00
$range = intval($range);
if (!$range_end) {
$range_end = $size - 1;
2018-04-02 08:29:19 +02:00
} else {
2019-01-11 09:59:01 +01:00
$range_end = intval($range_end);
2018-04-02 08:29:19 +02:00
}
2019-01-11 09:59:01 +01:00
$new_length = $range_end - $range + 1;
2018-04-02 08:29:19 +02:00
header("HTTP/1.1 206 Partial Content");
header("Content-Length: $new_length");
header("Content-Range: bytes $range-$range_end/$size");
} else {
2019-01-11 09:59:01 +01:00
$new_length = $size;
header("Content-Length: " . $size);
2018-04-02 08:29:19 +02:00
}
2019-01-11 09:59:01 +01:00
$chunksize = 1 * (1024 * 1024);
2018-04-02 08:29:19 +02:00
$bytes_send = 0;
2019-01-11 09:59:01 +01:00
if ($file = fopen($file_path, 'r')) {
if (isset($_SERVER['HTTP_RANGE'])) {
fseek($file, $range);
}
2018-04-02 08:29:19 +02:00
2019-01-11 09:59:01 +01:00
while (!feof($file) &&
2018-04-02 08:29:19 +02:00
(!connection_aborted()) &&
2019-01-11 09:59:01 +01:00
($bytes_send < $new_length)
) {
2018-04-02 08:29:19 +02:00
$buffer = fread($file, $chunksize);
2019-01-11 09:59:01 +01:00
echo $buffer;
2018-04-02 08:29:19 +02:00
flush();
$bytes_send += strlen($buffer);
}
fclose($file);
} else {
die('Error - can not open file.');
}
die();
2019-01-11 09:59:01 +01:00
} else {
// file does not exist
header("HTTP/1.0 404 Not Found");
2018-04-02 08:29:19 +02:00
}
2019-01-11 09:59:01 +01:00
exit;