From ee7d9614c07c3961d62edbd0e8bf5ef1e2d75ff8 Mon Sep 17 00:00:00 2001
From: Fyorl <gaelreth@gmail.com>
Date: Sat, 4 Aug 2012 13:18:20 +0100
Subject: [PATCH] [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
---
 phpBB/download/file.php                       | 289 +++++++++++++-----
 phpBB/includes/functions_display.php          |  30 ++
 .../prosilver/template/viewtopic_body.html    |  23 +-
 phpBB/styles/prosilver/theme/content.css      |  23 ++
 phpBB/viewtopic.php                           |  16 +-
 5 files changed, 307 insertions(+), 74 deletions(-)

diff --git a/phpBB/download/file.php b/phpBB/download/file.php
index c01b0789de..243d276607 100644
--- a/phpBB/download/file.php
+++ b/phpBB/download/file.php
@@ -132,8 +132,12 @@ if (isset($_GET['avatar']))
 // implicit else: we are not in avatar mode
 include($phpbb_root_path . 'common.' . $phpEx);
 require($phpbb_root_path . 'includes/functions_download' . '.' . $phpEx);
+require($phpbb_root_path . 'includes/functions_compress.' . $phpEx);
 
 $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', '');
 $thumbnail = request_var('t', false);
 
@@ -142,7 +146,7 @@ $user->session_begin(false);
 $auth->acl($user->data);
 $user->setup('viewtopic');
 
-if (!$download_id)
+if (!$download_id && !$post_id && !$topic_id)
 {
 	send_status_line(404, 'Not Found');
 	trigger_error('NO_ATTACHMENT_SELECTED');
@@ -154,20 +158,62 @@ if (!$config['allow_attachments'] && !$config['allow_pm_attach'])
 	trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
 }
 
-$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);
+$attachment = false;
+$attachments = false;
 
-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');
 	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');
 	trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
@@ -175,7 +221,7 @@ if ((!$attachment['in_message'] && !$config['allow_attachments']) || ($attachmen
 
 $row = array();
 
-if ($attachment['is_orphan'])
+if ($attachment && $attachment['is_orphan'])
 {
 	// 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;
@@ -191,13 +237,26 @@ if ($attachment['is_orphan'])
 }
 else
 {
-	if (!$attachment['in_message'])
+	if ($attachments || ($attachment && !$attachment['in_message']))
 	{
-		//
-		$sql = 'SELECT p.forum_id, f.forum_password, f.parent_id
-			FROM ' . POSTS_TABLE . ' p, ' . FORUMS_TABLE . ' f
-			WHERE p.post_id = ' . $attachment['post_msg_id'] . '
-				AND p.forum_id = f.forum_id';
+		if ($download_id || $post_id)
+		{
+			$sql = 'SELECT p.forum_id, f.forum_password, f.parent_id
+				FROM ' . POSTS_TABLE . ' p, ' . FORUMS_TABLE . ' f
+				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);
 		$row = $db->sql_fetchrow($result);
 		$db->sql_freeresult($result);
@@ -252,8 +311,24 @@ else
 	}
 
 	// disallowed?
-	$extensions = array();
-	if (!extension_allowed($row['forum_id'], $attachment['extension'], $extensions))
+	$extensions = $cache->obtain_attach_extensions($row['forum_id']);
+
+	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');
 		trigger_error(sprintf($user->lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension']));
@@ -266,71 +341,147 @@ if (!download_allowed())
 	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
-$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 = $download_id";
-$result = $db->sql_query_limit($sql, 1);
-$attachment = $db->sql_fetchrow($result);
-$db->sql_freeresult($result);
+if ($attachment)
+{
+	$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 = $download_id";
+	$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');
 	trigger_error('ERROR_NO_ATTACHMENT');
 }
 
-$attachment['physical_filename'] = utf8_basename($attachment['physical_filename']);
-$display_cat = $extensions[$attachment['extension']]['display_cat'];
+if ($attachment)
+{
+	$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'))
-{
-	$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)
+	if (($display_cat == ATTACHMENT_CATEGORY_IMAGE || $display_cat == ATTACHMENT_CATEGORY_THUMB) && !$user->optionget('viewimg'))
 	{
-		// 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']);
-		}
+		$display_cat = ATTACHMENT_CATEGORY_NONE;
+	}
 
-		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();
 	}
 	else
 	{
-		send_file_to_browser($attachment, $config['upload_path'], $display_cat);
-		file_gc();
+		// Determine the 'presenting'-method
+		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();
+}
diff --git a/phpBB/includes/functions_display.php b/phpBB/includes/functions_display.php
index a1ff7a1f99..a99d2353ed 100644
--- a/phpBB/includes/functions_display.php
+++ b/phpBB/includes/functions_display.php
@@ -1285,3 +1285,33 @@ function get_user_avatar($avatar, $avatar_type, $avatar_width, $avatar_height, $
 	$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) . '" />';
 }
+
+/**
+* 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;
+}
diff --git a/phpBB/styles/prosilver/template/viewtopic_body.html b/phpBB/styles/prosilver/template/viewtopic_body.html
index d27d653907..57afa55a36 100644
--- a/phpBB/styles/prosilver/template/viewtopic_body.html
+++ b/phpBB/styles/prosilver/template/viewtopic_body.html
@@ -46,7 +46,14 @@
 	<!-- ENDIF -->
 
 	<!-- 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 -->
 
 	<!-- IF .pagination or TOTAL_POSTS -->
@@ -161,7 +168,19 @@
 
 			<!-- IF postrow.S_HAS_ATTACHMENTS -->
 				<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 -->
 						<dd>{postrow.attachment.DISPLAY_ATTACHMENT}</dd>
 					<!-- END attachment -->
diff --git a/phpBB/styles/prosilver/theme/content.css b/phpBB/styles/prosilver/theme/content.css
index 60903911dd..b6012f8a63 100644
--- a/phpBB/styles/prosilver/theme/content.css
+++ b/phpBB/styles/prosilver/theme/content.css
@@ -702,3 +702,26 @@ dl.pmlist dd {
 	margin-left: 61% !important;
 	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;
+}
diff --git a/phpBB/viewtopic.php b/phpBB/viewtopic.php
index fce1be7784..1199fc4d14 100644
--- a/phpBB/viewtopic.php
+++ b/phpBB/viewtopic.php
@@ -15,6 +15,7 @@ $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
 $phpEx = substr(strrchr(__FILE__, '.'), 1);
 include($phpbb_root_path . 'common.' . $phpEx);
 include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
+include($phpbb_root_path . 'includes/functions_compress.' . $phpEx);
 include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
 
 // Start session management
@@ -1316,9 +1317,14 @@ if (sizeof($attach_list))
 
 $template->assign_vars(array(
 	'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
 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,
 		'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']))
@@ -1602,6 +1606,12 @@ for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
 				'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'];