View source
<?php
function filebrowser_page_download($fid) {
$cleanup = NULL;
$files_fid = NULL;
if (strpos($fid, ':')) {
list($fid, $files_fid) = explode(':', $fid);
$files_fid = explode(',', $files_fid);
}
$content = _filebrowser_node_content_load($fid);
$node = NULL;
$target = NULL;
if ($content) {
$node = node_load($content['nid']);
$target = _filebrowser_convert_to_fs_encoding($node, _filebrowser_get_path($node) . $content['path']);
}
if (!$content || !$node || !_filebrowser_can_download_file($node) || !$target) {
drupal_access_denied();
exit;
}
if (is_dir($target)) {
if (!function_exists("zip_open")) {
drupal_set_message(t("No ZIP support found in PHP installation, please contact your administrator"));
return;
}
$zip = new ZipArchive();
$target = file_directory_temp() . "/filebrowser_" . _filebrowser_safe_basename($target) . ".zip";
$cleanup = $target;
if (!file_exists(_filebrowser_safe_dirname($target))) {
mkdir(_filebrowser_safe_dirname($target), 0777, TRUE);
}
if (file_exists($target)) {
unlink($target);
}
_filebrowser_load_files($node, $fid);
if ($zip
->open($target, ZIPARCHIVE::CREATE) === TRUE) {
foreach ($node->file_listing as $file_name => $file_data) {
if ($file_data['kind'] === 0 && (!$files_fid || in_array($file_data['fid'], $files_fid))) {
$fs_filename = realpath(_filebrowser_convert_to_fs_encoding($node, _filebrowser_get_path($node) . "/" . $file_data['relative-path']));
$zip
->addFile($fs_filename, $file_name);
}
}
$zip
->close();
}
else {
return t("Unable to create temporary zip file '@file'", array(
file => $target,
));
}
}
$decoded_file = _filebrowser_convert_from_fs_encoding($node, $target);
$result = _filebrowser_file_download($target, $decoded_file);
if ($result) {
if ($cleanup && file_exists($cleanup)) {
unlink($cleanup);
}
}
else {
drupal_access_denied();
}
exit;
}
function _filebrowser_file_download($file, $filename) {
header('Content-Description: File Transfer');
header("Cache-Control: public, must-revalidate, max-age=0");
header("Pragma: public");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Content-Type: " . file_get_mimetype($filename));
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($file));
if (eregi("MSIE", getenv("HTTP_USER_AGENT")) || eregi("Internet Explorer", getenv("HTTP_USER_AGENT"))) {
header('Content-Disposition: inline; filename="' . mb_convert_encoding(_filebrowser_safe_basename($filename), "ISO-8859-2", "UTF-8") . '";');
}
else {
header('Content-Disposition: inline; filename="' . _filebrowser_safe_basename($filename) . '";');
}
$block_size = 4096;
$buffer = '';
$handle = fopen($file, 'rb');
set_time_limit(0);
if ($handle !== FALSE) {
while (!feof($handle)) {
$buffer = fgets($handle, $block_size);
echo $buffer;
ob_flush();
flush();
}
fclose($handle);
}
return TRUE;
}
function _filebrowser_file_url($node, $record) {
static $cache = NULL;
if (is_null($cache)) {
$cache = module_invoke_all('filebrowser_download_manager_info');
}
$mode = $node->folder_rights->download_manager;
$mode = isset($cache[$mode]) ? $mode : 'private';
$callback = $cache[$mode]['url_callback'];
return $callback($node, $record);
}
function filebrowser_private_download_url($node, $record) {
return url('filebrowser/download/' . $record['fid'], array(
'absolute' => TRUE,
'language' => '',
));
}
function filebrowser_public_download_url($node, $record) {
$file = _filebrowser_convert_to_fs_encoding($node, _filebrowser_get_path($node) . $record['path']);
$web_root = getcwd();
if (strpos($web_root, $file) === 0) {
$target = substr($file, strlen($web_root));
}
else {
$target = $file;
}
return url($target, array(
'absolute' => TRUE,
'language' => '',
));
}