1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-05-08 00:25:19 +02:00

[feature/attach-dl] Downloading all attachments fully implemented

Added a function to list all available archiving methods and integrated
it with the prosilver style.

Heavy modifications to download/file.php to support archiving and
downloading of multiple files at once.

PHPBB3-11042
This commit is contained in:
Fyorl 2012-08-04 13:18:20 +01:00
parent 5bffd9883d
commit ee7d9614c0
5 changed files with 307 additions and 74 deletions

View File

@ -132,8 +132,12 @@ if (isset($_GET['avatar']))
// implicit else: we are not in avatar mode // implicit else: we are not in avatar mode
include($phpbb_root_path . 'common.' . $phpEx); include($phpbb_root_path . 'common.' . $phpEx);
require($phpbb_root_path . 'includes/functions_download' . '.' . $phpEx); require($phpbb_root_path . 'includes/functions_download' . '.' . $phpEx);
require($phpbb_root_path . 'includes/functions_compress.' . $phpEx);
$download_id = request_var('id', 0); $download_id = request_var('id', 0);
$topic_id = $request->variable('topic_id', 0);
$post_id = $request->variable('post_id', 0);
$archive = $request->variable('archive', '.tar');
$mode = request_var('mode', ''); $mode = request_var('mode', '');
$thumbnail = request_var('t', false); $thumbnail = request_var('t', false);
@ -142,7 +146,7 @@ $user->session_begin(false);
$auth->acl($user->data); $auth->acl($user->data);
$user->setup('viewtopic'); $user->setup('viewtopic');
if (!$download_id) if (!$download_id && !$post_id && !$topic_id)
{ {
send_status_line(404, 'Not Found'); send_status_line(404, 'Not Found');
trigger_error('NO_ATTACHMENT_SELECTED'); trigger_error('NO_ATTACHMENT_SELECTED');
@ -154,20 +158,62 @@ if (!$config['allow_attachments'] && !$config['allow_pm_attach'])
trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED'); trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
} }
$sql = 'SELECT attach_id, in_message, post_msg_id, extension, is_orphan, poster_id, filetime $attachment = false;
FROM ' . ATTACHMENTS_TABLE . " $attachments = false;
WHERE attach_id = $download_id";
$result = $db->sql_query_limit($sql, 1);
$attachment = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
if (!$attachment) if ($download_id)
{
$sql = 'SELECT attach_id, in_message, post_msg_id, extension, is_orphan, poster_id, filetime
FROM ' . ATTACHMENTS_TABLE . "
WHERE attach_id = $download_id";
$result = $db->sql_query_limit($sql, 1);
$attachment = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
}
if ($topic_id)
{
$sql = "
SELECT attach_id, in_message, post_msg_id, extension, is_orphan, a.poster_id, filetime
FROM " . POSTS_TABLE . " p, " . ATTACHMENTS_TABLE . " a
WHERE p.topic_id = $topic_id
AND p.post_attachment = 1
AND a.post_msg_id = p.post_id
";
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$attachments[] = $row;
}
$db->sql_freeresult($result);
}
if ($post_id)
{
$sql = "
SELECT attach_id, in_message, post_msg_id, extension, is_orphan, poster_id, filetime
FROM " . ATTACHMENTS_TABLE . "
WHERE post_msg_id = $post_id
";
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$attachments[] = $row;
}
$db->sql_freeresult($result);
}
if (!$attachment && !$attachments)
{ {
send_status_line(404, 'Not Found'); send_status_line(404, 'Not Found');
trigger_error('ERROR_NO_ATTACHMENT'); trigger_error('ERROR_NO_ATTACHMENT');
} }
if ((!$attachment['in_message'] && !$config['allow_attachments']) || ($attachment['in_message'] && !$config['allow_pm_attach'])) if ($attachment && ((!$attachment['in_message'] && !$config['allow_attachments']) || ($attachment['in_message'] && !$config['allow_pm_attach'])))
{ {
send_status_line(404, 'Not Found'); send_status_line(404, 'Not Found');
trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED'); trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
@ -175,7 +221,7 @@ if ((!$attachment['in_message'] && !$config['allow_attachments']) || ($attachmen
$row = array(); $row = array();
if ($attachment['is_orphan']) if ($attachment && $attachment['is_orphan'])
{ {
// We allow admins having attachment permissions to see orphan attachments... // We allow admins having attachment permissions to see orphan attachments...
$own_attachment = ($auth->acl_get('a_attach') || $attachment['poster_id'] == $user->data['user_id']) ? true : false; $own_attachment = ($auth->acl_get('a_attach') || $attachment['poster_id'] == $user->data['user_id']) ? true : false;
@ -191,13 +237,26 @@ if ($attachment['is_orphan'])
} }
else else
{ {
if (!$attachment['in_message']) if ($attachments || ($attachment && !$attachment['in_message']))
{ {
// if ($download_id || $post_id)
$sql = 'SELECT p.forum_id, f.forum_password, f.parent_id {
FROM ' . POSTS_TABLE . ' p, ' . FORUMS_TABLE . ' f $sql = 'SELECT p.forum_id, f.forum_password, f.parent_id
WHERE p.post_id = ' . $attachment['post_msg_id'] . ' FROM ' . POSTS_TABLE . ' p, ' . FORUMS_TABLE . ' f
AND p.forum_id = f.forum_id'; WHERE p.post_id = ' . (($attachment) ? $attachment['post_msg_id'] : $post_id) . '
AND p.forum_id = f.forum_id';
}
if ($topic_id)
{
$sql = "
SELECT t.forum_id, f.forum_password, f.parent_id
FROM " . TOPICS_TABLE . " t, " . FORUMS_TABLE . " f
WHERE t.topic_id = $topic_id
AND t.forum_id = f.forum_id
";
}
$result = $db->sql_query_limit($sql, 1); $result = $db->sql_query_limit($sql, 1);
$row = $db->sql_fetchrow($result); $row = $db->sql_fetchrow($result);
$db->sql_freeresult($result); $db->sql_freeresult($result);
@ -252,8 +311,24 @@ else
} }
// disallowed? // disallowed?
$extensions = array(); $extensions = $cache->obtain_attach_extensions($row['forum_id']);
if (!extension_allowed($row['forum_id'], $attachment['extension'], $extensions))
if ($attachments)
{
// Remove attachments with disallowed extensions
$new_ary = array();
foreach ($attachments as $attach)
{
if (isset($extensions['_allowed_'][$attach['extension']]))
{
$new_ary[] = $attach;
}
}
$attachments = $new_ary;
}
if (($attachments && empty($attachments)) || ($attachment && !isset($extensions['_allowed_'][$attachment['extension']])))
{ {
send_status_line(404, 'Forbidden'); send_status_line(404, 'Forbidden');
trigger_error(sprintf($user->lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension'])); trigger_error(sprintf($user->lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension']));
@ -266,71 +341,147 @@ if (!download_allowed())
trigger_error($user->lang['LINKAGE_FORBIDDEN']); trigger_error($user->lang['LINKAGE_FORBIDDEN']);
} }
$download_mode = (int) $extensions[$attachment['extension']]['download_mode']; if ($attachments && sizeof($attachments) < 2)
{
$attachments = false;
$attachment = $attachments[0];
}
if ($attachment)
{
$download_mode = (int) $extensions[$attachment['extension']]['download_mode'];
}
// Fetching filename here to prevent sniffing of filename // Fetching filename here to prevent sniffing of filename
$sql = 'SELECT attach_id, is_orphan, in_message, post_msg_id, extension, physical_filename, real_filename, mimetype, filesize, filetime if ($attachment)
FROM ' . ATTACHMENTS_TABLE . " {
WHERE attach_id = $download_id"; $sql = 'SELECT attach_id, is_orphan, in_message, post_msg_id, extension, physical_filename, real_filename, mimetype, filesize, filetime
$result = $db->sql_query_limit($sql, 1); FROM ' . ATTACHMENTS_TABLE . "
$attachment = $db->sql_fetchrow($result); WHERE attach_id = $download_id";
$db->sql_freeresult($result); $result = $db->sql_query_limit($sql, 1);
$attachment = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
}
if (!$attachment) if ($attachments)
{
$attach_ids = array();
foreach ($attachments as $attach)
{
$attach_ids[] = $attach['attach_id'];
}
$attach_ids = implode(',', $attach_ids);
$sql = "
SELECT attach_id, is_orphan, in_message, post_msg_id, extension, physical_filename, real_filename, mimetype, filesize, filetime
FROM " . ATTACHMENTS_TABLE . "
WHERE attach_id IN ($attach_ids)
";
$result = $db->sql_query($sql);
$attachments = array();
while ($row = $db->sql_fetchrow($result))
{
$attachments[] = $row;
}
$db->sql_freeresult($result);
}
if (!$attachment && empty($attachments))
{ {
send_status_line(404, 'Not Found'); send_status_line(404, 'Not Found');
trigger_error('ERROR_NO_ATTACHMENT'); trigger_error('ERROR_NO_ATTACHMENT');
} }
$attachment['physical_filename'] = utf8_basename($attachment['physical_filename']); if ($attachment)
$display_cat = $extensions[$attachment['extension']]['display_cat']; {
$attachment['physical_filename'] = utf8_basename($attachment['physical_filename']);
$display_cat = $extensions[$attachment['extension']]['display_cat'];
if (($display_cat == ATTACHMENT_CATEGORY_IMAGE || $display_cat == ATTACHMENT_CATEGORY_THUMB) && !$user->optionget('viewimg')) if (($display_cat == ATTACHMENT_CATEGORY_IMAGE || $display_cat == ATTACHMENT_CATEGORY_THUMB) && !$user->optionget('viewimg'))
{
$display_cat = ATTACHMENT_CATEGORY_NONE;
}
if ($display_cat == ATTACHMENT_CATEGORY_FLASH && !$user->optionget('viewflash'))
{
$display_cat = ATTACHMENT_CATEGORY_NONE;
}
if ($thumbnail)
{
$attachment['physical_filename'] = 'thumb_' . $attachment['physical_filename'];
}
else if (($display_cat == ATTACHMENT_CATEGORY_NONE/* || $display_cat == ATTACHMENT_CATEGORY_IMAGE*/) && !$attachment['is_orphan'] && !phpbb_http_byte_range($attachment['filesize']))
{
// Update download count
$sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
SET download_count = download_count + 1
WHERE attach_id = ' . $attachment['attach_id'];
$db->sql_query($sql);
}
if ($display_cat == ATTACHMENT_CATEGORY_IMAGE && $mode === 'view' && (strpos($attachment['mimetype'], 'image') === 0) && ((strpos(strtolower($user->browser), 'msie') !== false) && (strpos(strtolower($user->browser), 'msie 8.0') === false)))
{
wrap_img_in_html(append_sid($phpbb_root_path . 'download/file.' . $phpEx, 'id=' . $attachment['attach_id']), $attachment['real_filename']);
file_gc();
}
else
{
// Determine the 'presenting'-method
if ($download_mode == PHYSICAL_LINK)
{ {
// This presenting method should no longer be used $display_cat = ATTACHMENT_CATEGORY_NONE;
if (!@is_dir($phpbb_root_path . $config['upload_path'])) }
{
send_status_line(500, 'Internal Server Error');
trigger_error($user->lang['PHYSICAL_DOWNLOAD_NOT_POSSIBLE']);
}
redirect($phpbb_root_path . $config['upload_path'] . '/' . $attachment['physical_filename']); if ($display_cat == ATTACHMENT_CATEGORY_FLASH && !$user->optionget('viewflash'))
{
$display_cat = ATTACHMENT_CATEGORY_NONE;
}
if ($thumbnail)
{
$attachment['physical_filename'] = 'thumb_' . $attachment['physical_filename'];
}
else if (($display_cat == ATTACHMENT_CATEGORY_NONE/* || $display_cat == ATTACHMENT_CATEGORY_IMAGE*/) && !$attachment['is_orphan'] && !phpbb_http_byte_range($attachment['filesize']))
{
// Update download count
$sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
SET download_count = download_count + 1
WHERE attach_id = ' . $attachment['attach_id'];
$db->sql_query($sql);
}
if ($display_cat == ATTACHMENT_CATEGORY_IMAGE && $mode === 'view' && (strpos($attachment['mimetype'], 'image') === 0) && ((strpos(strtolower($user->browser), 'msie') !== false) && (strpos(strtolower($user->browser), 'msie 8.0') === false)))
{
wrap_img_in_html(append_sid($phpbb_root_path . 'download/file.' . $phpEx, 'id=' . $attachment['attach_id']), $attachment['real_filename']);
file_gc(); file_gc();
} }
else else
{ {
send_file_to_browser($attachment, $config['upload_path'], $display_cat); // Determine the 'presenting'-method
file_gc(); if ($download_mode == PHYSICAL_LINK)
{
// This presenting method should no longer be used
if (!@is_dir($phpbb_root_path . $config['upload_path']))
{
send_status_line(500, 'Internal Server Error');
trigger_error($user->lang['PHYSICAL_DOWNLOAD_NOT_POSSIBLE']);
}
redirect($phpbb_root_path . $config['upload_path'] . '/' . $attachment['physical_filename']);
file_gc();
}
else
{
send_file_to_browser($attachment, $config['upload_path'], $display_cat);
file_gc();
}
} }
} }
if ($attachments)
{
$sql = "
UPDATE " . ATTACHMENTS_TABLE . "
SET download_count = download_count + 1
WHERE attach_id IN ($attach_ids)
";
$db->sql_query($sql);
if (!in_array($archive, compress::methods()))
{
$archive = '.tar';
}
$store_name = 'att_' . time() . '_' . unique_id();
$archive_name = 'attachments';
if ($archive === '.zip')
{
$compress = new compress_zip('w', "{$phpbb_root_path}store/{$store_name}{$archive}");
}
else
{
$compress = new compress_tar('w', "{$phpbb_root_path}store/{$store_name}{$archive}", $archive);
}
foreach ($attachments as $attach)
{
$compress->add_custom_file("{$phpbb_root_path}files/{$attach['physical_filename']}", $attach['real_filename']);
}
$compress->close();
$compress->download($store_name, $archive_name);
file_gc();
}

View File

@ -1285,3 +1285,33 @@ function get_user_avatar($avatar, $avatar_type, $avatar_width, $avatar_height, $
$avatar_img .= $avatar; $avatar_img .= $avatar;
return '<img src="' . (str_replace(' ', '%20', $avatar_img)) . '" width="' . $avatar_width . '" height="' . $avatar_height . '" alt="' . ((!empty($user->lang[$alt])) ? $user->lang[$alt] : $alt) . '" />'; return '<img src="' . (str_replace(' ', '%20', $avatar_img)) . '" width="' . $avatar_width . '" height="' . $avatar_height . '" alt="' . ((!empty($user->lang[$alt])) ? $user->lang[$alt] : $alt) . '" />';
} }
/**
* Generate a list of archive types available for compressing attachments
*
* @param string $param_key Either topic_id or post_id
* @param string $param_val The value of the topic or post id
* @param string $phpbb_root_path The root path of the phpBB installation
* @param string $phpEx The PHP extension
*
* @return array Array containing the link and the type of compression
*/
function gen_download_links($param_key, $param_val, $phpbb_root_path, $phpEx)
{
$methods = compress::methods();
$links = array();
foreach ($methods as $method)
{
$type = array_pop(explode('.', $method));
$params = array('archive' => $method);
$params[$param_key] = $param_val;
$links[] = array(
'LINK' => append_sid("{$phpbb_root_path}download/file.$phpEx", $params),
'TYPE' => $type,
);
}
return $links;
}

View File

@ -46,7 +46,14 @@
<!-- ENDIF --> <!-- ENDIF -->
<!-- IF S_HAS_ATTACHMENTS --> <!-- IF S_HAS_ATTACHMENTS -->
<span>[ <a href="{U_DL_ALL_LINK}">{L_DOWNLOAD_ALL_ATTACH}</a> ]</span> <div class="dl_links">
<strong>{L_DOWNLOAD_ALL_ATTACH}:</strong>
<ul>
<!-- BEGIN dl_method -->
<li>[ <a href="{dl_method.LINK}">{dl_method.TYPE}</a> ]</li>
<!-- END dl_method -->
</ul>
</div>
<!-- ENDIF --> <!-- ENDIF -->
<!-- IF .pagination or TOTAL_POSTS --> <!-- IF .pagination or TOTAL_POSTS -->
@ -161,7 +168,19 @@
<!-- IF postrow.S_HAS_ATTACHMENTS --> <!-- IF postrow.S_HAS_ATTACHMENTS -->
<dl class="attachbox"> <dl class="attachbox">
<dt>{L_ATTACHMENTS}<!-- IF postrow.S_MULTIPLE_ATTACH --> [ <a href="{postrow.U_DL_ALL_LINK}">{L_DOWNLOAD_ALL}</a> ]<!-- ENDIF --></dt> <dt>
{L_ATTACHMENTS}
<!-- IF postrow.S_MULTIPLE_ATTACH -->
<div class="dl_links">
<strong>{L_DOWNLOAD_ALL}:</strong>
<ul>
<!-- BEGIN dl_method -->
<li>[ <a href="{postrow.dl_method.LINK}">{postrow.dl_method.TYPE}</a> ]</li>
<!-- END dl_method -->
</ul>
</div>
<!-- ENDIF -->
</dt>
<!-- BEGIN attachment --> <!-- BEGIN attachment -->
<dd>{postrow.attachment.DISPLAY_ATTACHMENT}</dd> <dd>{postrow.attachment.DISPLAY_ATTACHMENT}</dd>
<!-- END attachment --> <!-- END attachment -->

View File

@ -702,3 +702,26 @@ dl.pmlist dd {
margin-left: 61% !important; margin-left: 61% !important;
margin-bottom: 2px; margin-bottom: 2px;
} }
.topic-actions div.dl_links {
padding: 10px 0 0 10px;
}
div.dl_links {
display: inline-block;
text-transform: none;
}
.dl_links strong {
font-weight: bold;
}
.dl_links ul {
list-style-type: none;
margin: 0;
display: inline-block;
}
.dl_links li {
display: inline-block;
}

View File

@ -15,6 +15,7 @@ $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1); $phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx); include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx); include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include($phpbb_root_path . 'includes/functions_compress.' . $phpEx);
include($phpbb_root_path . 'includes/bbcode.' . $phpEx); include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
// Start session management // Start session management
@ -1316,9 +1317,14 @@ if (sizeof($attach_list))
$template->assign_vars(array( $template->assign_vars(array(
'S_HAS_ATTACHMENTS' => !empty($attachments), 'S_HAS_ATTACHMENTS' => !empty($attachments),
'U_DL_ALL_LINK' => append_sid("{$phpbb_root_path}download/file.$phpEx", "topic_id=$topic_id"),
)); ));
$methods = gen_download_links('topic_id', $topic_id, $phpbb_root_path, $phpEx);
foreach ($methods as $method)
{
$template->assign_block_vars('dl_method', $method);
}
// Instantiate BBCode if need be // Instantiate BBCode if need be
if ($bbcode_bitfield !== '') if ($bbcode_bitfield !== '')
{ {
@ -1573,8 +1579,6 @@ for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
'S_IGNORE_POST' => ($row['hide_post']) ? true : false, 'S_IGNORE_POST' => ($row['hide_post']) ? true : false,
'L_IGNORE_POST' => ($row['hide_post']) ? sprintf($user->lang['POST_BY_FOE'], get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']), '<a href="' . $viewtopic_url . "&amp;p={$row['post_id']}&amp;view=show#p{$row['post_id']}" . '">', '</a>') : '', 'L_IGNORE_POST' => ($row['hide_post']) ? sprintf($user->lang['POST_BY_FOE'], get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']), '<a href="' . $viewtopic_url . "&amp;p={$row['post_id']}&amp;view=show#p{$row['post_id']}" . '">', '</a>') : '',
'U_DL_ALL_LINK' => append_sid("{$phpbb_root_path}download/file.$phpEx", 'post_id=' . $row['post_id']),
); );
if (isset($cp_row['row']) && sizeof($cp_row['row'])) if (isset($cp_row['row']) && sizeof($cp_row['row']))
@ -1602,6 +1606,12 @@ for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
'DISPLAY_ATTACHMENT' => $attachment) 'DISPLAY_ATTACHMENT' => $attachment)
); );
} }
$methods = gen_download_links('post_id', $row['post_id'], $phpbb_root_path, $phpEx);
foreach ($methods as $method)
{
$template->assign_block_vars('postrow.dl_method', $method);
}
} }
$prev_post_id = $row['post_id']; $prev_post_id = $row['post_id'];