includes/mcp/mcp_topic.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/mcp/mcp_topic.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,642 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package mcp
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* View topic in MCP
    1.24 +*/
    1.25 +function mcp_topic_view($id, $mode, $action)
    1.26 +{
    1.27 +	global $phpEx, $phpbb_root_path, $config;
    1.28 +	global $template, $db, $user, $auth, $cache;
    1.29 +
    1.30 +	$url = append_sid("{$phpbb_root_path}mcp.$phpEx?" . extra_url());
    1.31 +
    1.32 +	$user->add_lang('viewtopic');
    1.33 +
    1.34 +	$topic_id = request_var('t', 0);
    1.35 +	$topic_info = get_topic_data(array($topic_id), false, true);
    1.36 +
    1.37 +	if (!sizeof($topic_info))
    1.38 +	{
    1.39 +		trigger_error('TOPIC_NOT_EXIST');
    1.40 +	}
    1.41 +
    1.42 +	$topic_info = $topic_info[$topic_id];
    1.43 +
    1.44 +	// Set up some vars
    1.45 +	$icon_id		= request_var('icon', 0);
    1.46 +	$subject		= utf8_normalize_nfc(request_var('subject', '', true));
    1.47 +	$start			= request_var('start', 0);
    1.48 +	$sort_days_old	= request_var('st_old', 0);
    1.49 +	$forum_id		= request_var('f', 0);
    1.50 +	$to_topic_id	= request_var('to_topic_id', 0);
    1.51 +	$to_forum_id	= request_var('to_forum_id', 0);
    1.52 +	$sort			= isset($_POST['sort']) ? true : false;
    1.53 +	$submitted_id_list	= request_var('post_ids', array(0));
    1.54 +	$checked_ids = $post_id_list = request_var('post_id_list', array(0));
    1.55 +
    1.56 +	// Split Topic?
    1.57 +	if ($action == 'split_all' || $action == 'split_beyond')
    1.58 +	{
    1.59 +		if (!$sort)
    1.60 +		{
    1.61 +			split_topic($action, $topic_id, $to_forum_id, $subject);
    1.62 +		}
    1.63 +		$action = 'split';
    1.64 +	}
    1.65 +
    1.66 +	// Merge Posts?
    1.67 +	if ($action == 'merge_posts')
    1.68 +	{
    1.69 +		if (!$sort)
    1.70 +		{
    1.71 +			merge_posts($topic_id, $to_topic_id);
    1.72 +		}
    1.73 +		$action = 'merge';
    1.74 +	}
    1.75 +
    1.76 +	if ($action == 'split' && !$subject)
    1.77 +	{
    1.78 +		$subject = $topic_info['topic_title'];
    1.79 +	}
    1.80 +
    1.81 +	// Approve posts?
    1.82 +	if ($action == 'approve' && $auth->acl_get('m_approve', $topic_info['forum_id']))
    1.83 +	{
    1.84 +		include($phpbb_root_path . 'includes/mcp/mcp_queue.' . $phpEx);
    1.85 +		include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
    1.86 +		include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
    1.87 +
    1.88 +		if (!sizeof($post_id_list))
    1.89 +		{
    1.90 +			trigger_error('NO_POST_SELECTED');
    1.91 +		}
    1.92 +
    1.93 +		if (!$sort)
    1.94 +		{
    1.95 +			approve_post($post_id_list, $id, $mode);
    1.96 +		}
    1.97 +	}
    1.98 +
    1.99 +	// Jumpbox, sort selects and that kind of things
   1.100 +	make_jumpbox($url . "&amp;i=$id&amp;mode=forum_view", $topic_info['forum_id'], false, 'm_', true);
   1.101 +	$where_sql = ($action == 'reports') ? 'WHERE post_reported = 1 AND ' : 'WHERE';
   1.102 +
   1.103 +	$sort_days = $total = 0;
   1.104 +	$sort_key = $sort_dir = '';
   1.105 +	$sort_by_sql = $sort_order_sql = array();
   1.106 +	mcp_sorting('viewtopic', $sort_days, $sort_key, $sort_dir, $sort_by_sql, $sort_order_sql, $total, $topic_info['forum_id'], $topic_id, $where_sql);
   1.107 +
   1.108 +	$limit_time_sql = ($sort_days) ? 'AND p.post_time >= ' . (time() - ($sort_days * 86400)) : '';
   1.109 +
   1.110 +	if ($total == -1)
   1.111 +	{
   1.112 +		if ($auth->acl_get('m_approve', $topic_info['forum_id']))
   1.113 +		{
   1.114 +			$total = $topic_info['topic_replies_real'] + 1;
   1.115 +		}
   1.116 +		else
   1.117 +		{
   1.118 +			$total = $topic_info['topic_replies'] + 1;
   1.119 +		}
   1.120 +	}
   1.121 +
   1.122 +	$posts_per_page = max(0, request_var('posts_per_page', intval($config['posts_per_page'])));
   1.123 +	if ($posts_per_page == 0)
   1.124 +	{
   1.125 +		$posts_per_page = $total;
   1.126 +	}
   1.127 +
   1.128 +	if ((!empty($sort_days_old) && $sort_days_old != $sort_days) || $total <= $posts_per_page)
   1.129 +	{
   1.130 +		$start = 0;
   1.131 +	}
   1.132 +
   1.133 +	// Make sure $start is set to the last page if it exceeds the amount
   1.134 +	if ($start < 0 || $start >= $total)
   1.135 +	{
   1.136 +		$start = ($start < 0) ? 0 : floor(($total - 1) / $posts_per_page) * $posts_per_page;
   1.137 +	}
   1.138 +
   1.139 +	$sql = 'SELECT u.username, u.username_clean, u.user_colour, p.*
   1.140 +		FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
   1.141 +		WHERE ' . (($action == 'reports') ? 'p.post_reported = 1 AND ' : '') . '
   1.142 +			p.topic_id = ' . $topic_id . ' ' .
   1.143 +			((!$auth->acl_get('m_approve', $topic_info['forum_id'])) ? ' AND p.post_approved = 1 ' : '') . '
   1.144 +			AND p.poster_id = u.user_id ' .
   1.145 +			$limit_time_sql . '
   1.146 +		ORDER BY ' . $sort_order_sql;
   1.147 +	$result = $db->sql_query_limit($sql, $posts_per_page, $start);
   1.148 +
   1.149 +	$rowset = $post_id_list = array();
   1.150 +	$bbcode_bitfield = '';
   1.151 +	while ($row = $db->sql_fetchrow($result))
   1.152 +	{
   1.153 +		$rowset[] = $row;
   1.154 +		$post_id_list[] = $row['post_id'];
   1.155 +		$bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
   1.156 +	}
   1.157 +	$db->sql_freeresult($result);
   1.158 +
   1.159 +	if ($bbcode_bitfield !== '')
   1.160 +	{
   1.161 +		include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
   1.162 +		$bbcode = new bbcode(base64_encode($bbcode_bitfield));
   1.163 +	}
   1.164 +
   1.165 +	$topic_tracking_info = array();
   1.166 +
   1.167 +	// Get topic tracking info
   1.168 +	if ($config['load_db_lastread'])
   1.169 +	{
   1.170 +		$tmp_topic_data = array($topic_id => $topic_info);
   1.171 +		$topic_tracking_info = get_topic_tracking($topic_info['forum_id'], $topic_id, $tmp_topic_data, array($topic_info['forum_id'] => $topic_info['forum_mark_time']));
   1.172 +		unset($tmp_topic_data);
   1.173 +	}
   1.174 +	else
   1.175 +	{
   1.176 +		$topic_tracking_info = get_complete_topic_tracking($topic_info['forum_id'], $topic_id);
   1.177 +	}
   1.178 +
   1.179 +	$has_unapproved_posts = false;
   1.180 +
   1.181 +	// Grab extensions
   1.182 +	$extensions = $attachments = array();
   1.183 +	if ($topic_info['topic_attachment'] && sizeof($post_id_list))
   1.184 +	{
   1.185 +		$extensions = $cache->obtain_attach_extensions($topic_info['forum_id']);
   1.186 +
   1.187 +		// Get attachments...
   1.188 +		if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $topic_info['forum_id']))
   1.189 +		{
   1.190 +			$sql = 'SELECT *
   1.191 +				FROM ' . ATTACHMENTS_TABLE . '
   1.192 +				WHERE ' . $db->sql_in_set('post_msg_id', $post_id_list) . '
   1.193 +					AND in_message = 0
   1.194 +				ORDER BY filetime DESC, post_msg_id ASC';
   1.195 +			$result = $db->sql_query($sql);
   1.196 +
   1.197 +			while ($row = $db->sql_fetchrow($result))
   1.198 +			{
   1.199 +				$attachments[$row['post_msg_id']][] = $row;
   1.200 +			}
   1.201 +			$db->sql_freeresult($result);
   1.202 +		}
   1.203 +	}
   1.204 +
   1.205 +	foreach ($rowset as $i => $row)
   1.206 +	{
   1.207 +		$message = $row['post_text'];
   1.208 +		$post_subject = ($row['post_subject'] != '') ? $row['post_subject'] : $topic_info['topic_title'];
   1.209 +
   1.210 +		if ($row['bbcode_bitfield'])
   1.211 +		{
   1.212 +			$bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
   1.213 +		}
   1.214 +
   1.215 +		$message = bbcode_nl2br($message);
   1.216 +		$message = smiley_text($message);
   1.217 +
   1.218 +		if (!empty($attachments[$row['post_id']]))
   1.219 +		{
   1.220 +			$update_count = array();
   1.221 +			parse_attachments($topic_info['forum_id'], $message, $attachments[$row['post_id']], $update_count);
   1.222 +		}
   1.223 +
   1.224 +		if (!$row['post_approved'])
   1.225 +		{
   1.226 +			$has_unapproved_posts = true;
   1.227 +		}
   1.228 +
   1.229 +		$post_unread = (isset($topic_tracking_info[$topic_id]) && $row['post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
   1.230 +
   1.231 +		$template->assign_block_vars('postrow', array(
   1.232 +			'POST_AUTHOR_FULL'		=> get_username_string('full', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
   1.233 +			'POST_AUTHOR_COLOUR'	=> get_username_string('colour', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
   1.234 +			'POST_AUTHOR'			=> get_username_string('username', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
   1.235 +			'U_POST_AUTHOR'			=> get_username_string('profile', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
   1.236 +
   1.237 +			'POST_DATE'		=> $user->format_date($row['post_time']),
   1.238 +			'POST_SUBJECT'	=> $post_subject,
   1.239 +			'MESSAGE'		=> $message,
   1.240 +			'POST_ID'		=> $row['post_id'],
   1.241 +			'RETURN_TOPIC'	=> sprintf($user->lang['RETURN_TOPIC'], '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", 't=' . $topic_id) . '">', '</a>'),
   1.242 +
   1.243 +			'MINI_POST_IMG'			=> ($post_unread) ? $user->img('icon_post_target_unread', 'UNREAD_POST') : $user->img('icon_post_target', 'POST'),
   1.244 +
   1.245 +			'S_POST_REPORTED'	=> ($row['post_reported'] && $auth->acl_get('m_report', $topic_info['forum_id'])),
   1.246 +			'S_POST_UNAPPROVED'	=> (!$row['post_approved'] && $auth->acl_get('m_approve', $topic_info['forum_id'])),
   1.247 +			'S_CHECKED'			=> (($submitted_id_list && !in_array(intval($row['post_id']), $submitted_id_list)) || in_array(intval($row['post_id']), $checked_ids)) ? true : false,
   1.248 +			'S_HAS_ATTACHMENTS'	=> (!empty($attachments[$row['post_id']])) ? true : false,
   1.249 +
   1.250 +			'U_POST_DETAILS'	=> "$url&amp;i=$id&amp;p={$row['post_id']}&amp;mode=post_details" . (($forum_id) ? "&amp;f=$forum_id" : ''),
   1.251 +			'U_MCP_APPROVE'		=> ($auth->acl_get('m_approve', $topic_info['forum_id'])) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&amp;mode=approve_details&amp;f=' . $topic_info['forum_id'] . '&amp;p=' . $row['post_id']) : '',
   1.252 +			'U_MCP_REPORT'		=> ($auth->acl_get('m_report', $topic_info['forum_id'])) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&amp;mode=report_details&amp;f=' . $topic_info['forum_id'] . '&amp;p=' . $row['post_id']) : '')
   1.253 +		);
   1.254 +
   1.255 +		// Display not already displayed Attachments for this post, we already parsed them. ;)
   1.256 +		if (!empty($attachments[$row['post_id']]))
   1.257 +		{
   1.258 +			foreach ($attachments[$row['post_id']] as $attachment)
   1.259 +			{
   1.260 +				$template->assign_block_vars('postrow.attachment', array(
   1.261 +					'DISPLAY_ATTACHMENT'	=> $attachment)
   1.262 +				);
   1.263 +			}
   1.264 +		}
   1.265 +
   1.266 +		unset($rowset[$i]);
   1.267 +	}
   1.268 +
   1.269 +	// Display topic icons for split topic
   1.270 +	$s_topic_icons = false;
   1.271 +
   1.272 +	if ($auth->acl_gets('m_split', 'm_merge', (int) $topic_info['forum_id']))
   1.273 +	{
   1.274 +		include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
   1.275 +		$s_topic_icons = posting_gen_topic_icons('', $icon_id);
   1.276 +
   1.277 +		// Has the user selected a topic for merge?
   1.278 +		if ($to_topic_id)
   1.279 +		{
   1.280 +			$to_topic_info = get_topic_data(array($to_topic_id), 'm_merge');
   1.281 +
   1.282 +			if (!sizeof($to_topic_info))
   1.283 +			{
   1.284 +				$to_topic_id = 0;
   1.285 +			}
   1.286 +			else
   1.287 +			{
   1.288 +				$to_topic_info = $to_topic_info[$to_topic_id];
   1.289 +
   1.290 +				if (!$to_topic_info['enable_icons'] || $auth->acl_get('!f_icons', $topic_info['forum_id']))
   1.291 +				{
   1.292 +					$s_topic_icons = false;
   1.293 +				}
   1.294 +			}
   1.295 +		}
   1.296 +	}
   1.297 +
   1.298 +	$s_hidden_fields = build_hidden_fields(array(
   1.299 +		'st_old'	=> $sort_days,
   1.300 +		'post_ids'	=> $post_id_list,
   1.301 +	));
   1.302 +
   1.303 +	$template->assign_vars(array(
   1.304 +		'TOPIC_TITLE'		=> $topic_info['topic_title'],
   1.305 +		'U_VIEW_TOPIC'		=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $topic_info['forum_id'] . '&amp;t=' . $topic_info['topic_id']),
   1.306 +
   1.307 +		'TO_TOPIC_ID'		=> $to_topic_id,
   1.308 +		'TO_TOPIC_INFO'		=> ($to_topic_id) ? sprintf($user->lang['YOU_SELECTED_TOPIC'], $to_topic_id, '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $to_topic_info['forum_id'] . '&amp;t=' . $to_topic_id) . '">' . $to_topic_info['topic_title'] . '</a>') : '',
   1.309 +
   1.310 +		'SPLIT_SUBJECT'		=> $subject,
   1.311 +		'POSTS_PER_PAGE'	=> $posts_per_page,
   1.312 +		'ACTION'			=> $action,
   1.313 +
   1.314 +		'REPORTED_IMG'		=> $user->img('icon_topic_reported', 'POST_REPORTED'),
   1.315 +		'UNAPPROVED_IMG'	=> $user->img('icon_topic_unapproved', 'POST_UNAPPROVED'),
   1.316 +		'INFO_IMG'			=> $user->img('icon_post_info', 'VIEW_INFO'),
   1.317 +
   1.318 +		'S_MCP_ACTION'		=> "$url&amp;i=$id&amp;mode=$mode&amp;action=$action&amp;start=$start",
   1.319 +		'S_FORUM_SELECT'	=> ($to_forum_id) ? make_forum_select($to_forum_id, false, false, true, true, true) : make_forum_select($topic_info['forum_id'], false, false, true, true, true),
   1.320 +		'S_CAN_SPLIT'		=> ($auth->acl_get('m_split', $topic_info['forum_id'])) ? true : false,
   1.321 +		'S_CAN_MERGE'		=> ($auth->acl_get('m_merge', $topic_info['forum_id'])) ? true : false,
   1.322 +		'S_CAN_DELETE'		=> ($auth->acl_get('m_delete', $topic_info['forum_id'])) ? true : false,
   1.323 +		'S_CAN_APPROVE'		=> ($has_unapproved_posts && $auth->acl_get('m_approve', $topic_info['forum_id'])) ? true : false,
   1.324 +		'S_CAN_LOCK'		=> ($auth->acl_get('m_lock', $topic_info['forum_id'])) ? true : false,
   1.325 +		'S_CAN_REPORT'		=> ($auth->acl_get('m_report', $topic_info['forum_id'])) ? true : false,
   1.326 +		'S_REPORT_VIEW'		=> ($action == 'reports') ? true : false,
   1.327 +		'S_MERGE_VIEW'		=> ($action == 'merge') ? true : false,
   1.328 +		'S_SPLIT_VIEW'		=> ($action == 'split') ? true : false,
   1.329 +
   1.330 +		'S_HIDDEN_FIELDS'	=> $s_hidden_fields,
   1.331 +
   1.332 +		'S_SHOW_TOPIC_ICONS'	=> $s_topic_icons,
   1.333 +		'S_TOPIC_ICON'			=> $icon_id,
   1.334 +
   1.335 +		'U_SELECT_TOPIC'	=> "$url&amp;i=$id&amp;mode=forum_view&amp;action=merge_select" . (($forum_id) ? "&amp;f=$forum_id" : ''),
   1.336 +
   1.337 +		'RETURN_TOPIC'		=> sprintf($user->lang['RETURN_TOPIC'], '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f={$topic_info['forum_id']}&amp;t={$topic_info['topic_id']}&amp;start=$start") . '">', '</a>'),
   1.338 +		'RETURN_FORUM'		=> sprintf($user->lang['RETURN_FORUM'], '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", "f={$topic_info['forum_id']}&amp;start=$start") . '">', '</a>'),
   1.339 +
   1.340 +		'PAGE_NUMBER'		=> on_page($total, $posts_per_page, $start),
   1.341 +		'PAGINATION'		=> (!$posts_per_page) ? '' : generate_pagination(append_sid("{$phpbb_root_path}mcp.$phpEx", "i=$id&amp;t={$topic_info['topic_id']}&amp;mode=$mode&amp;action=$action&amp;to_topic_id=$to_topic_id&amp;posts_per_page=$posts_per_page&amp;st=$sort_days&amp;sk=$sort_key&amp;sd=$sort_dir"), $total, $posts_per_page, $start),
   1.342 +		'TOTAL_POSTS'		=> ($total == 1) ? $user->lang['VIEW_TOPIC_POST'] : sprintf($user->lang['VIEW_TOPIC_POSTS'], $total),
   1.343 +	));
   1.344 +}
   1.345 +
   1.346 +/**
   1.347 +* Split topic
   1.348 +*/
   1.349 +function split_topic($action, $topic_id, $to_forum_id, $subject)
   1.350 +{
   1.351 +	global $db, $template, $user, $phpEx, $phpbb_root_path, $auth, $config;
   1.352 +
   1.353 +	$post_id_list	= request_var('post_id_list', array(0));
   1.354 +	$forum_id		= request_var('forum_id', 0);
   1.355 +	$start			= request_var('start', 0);
   1.356 +
   1.357 +	if (!sizeof($post_id_list))
   1.358 +	{
   1.359 +		$template->assign_var('MESSAGE', $user->lang['NO_POST_SELECTED']);
   1.360 +		return;
   1.361 +	}
   1.362 +
   1.363 +	if (!check_ids($post_id_list, POSTS_TABLE, 'post_id', array('m_split')))
   1.364 +	{
   1.365 +		return;
   1.366 +	}
   1.367 +
   1.368 +	$post_id = $post_id_list[0];
   1.369 +	$post_info = get_post_data(array($post_id));
   1.370 +
   1.371 +	if (!sizeof($post_info))
   1.372 +	{
   1.373 +		$template->assign_var('MESSAGE', $user->lang['NO_POST_SELECTED']);
   1.374 +		return;
   1.375 +	}
   1.376 +
   1.377 +	$post_info = $post_info[$post_id];
   1.378 +	$subject = trim($subject);
   1.379 +
   1.380 +	// Make some tests
   1.381 +	if (!$subject)
   1.382 +	{
   1.383 +		$template->assign_var('MESSAGE', $user->lang['EMPTY_SUBJECT']);
   1.384 +		return;
   1.385 +	}
   1.386 +
   1.387 +	if ($to_forum_id <= 0)
   1.388 +	{
   1.389 +		$template->assign_var('MESSAGE', $user->lang['NO_DESTINATION_FORUM']);
   1.390 +		return;
   1.391 +	}
   1.392 +
   1.393 +	$forum_info = get_forum_data(array($to_forum_id), 'f_post');
   1.394 +
   1.395 +	if (!sizeof($forum_info))
   1.396 +	{
   1.397 +		$template->assign_var('MESSAGE', $user->lang['USER_CANNOT_POST']);
   1.398 +		return;
   1.399 +	}
   1.400 +
   1.401 +	$forum_info = $forum_info[$to_forum_id];
   1.402 +
   1.403 +	if ($forum_info['forum_type'] != FORUM_POST)
   1.404 +	{
   1.405 +		$template->assign_var('MESSAGE', $user->lang['FORUM_NOT_POSTABLE']);
   1.406 +		return;
   1.407 +	}
   1.408 +
   1.409 +	$redirect = request_var('redirect', build_url(array('quickmod')));
   1.410 +
   1.411 +	$s_hidden_fields = build_hidden_fields(array(
   1.412 +		'i'				=> 'main',
   1.413 +		'post_id_list'	=> $post_id_list,
   1.414 +		'f'				=> $forum_id,
   1.415 +		'mode'			=> 'topic_view',
   1.416 +		'start'			=> $start,
   1.417 +		'action'		=> $action,
   1.418 +		't'				=> $topic_id,
   1.419 +		'redirect'		=> $redirect,
   1.420 +		'subject'		=> $subject,
   1.421 +		'to_forum_id'	=> $to_forum_id,
   1.422 +		'icon'			=> request_var('icon', 0))
   1.423 +	);
   1.424 +	$success_msg = $return_link = '';
   1.425 +
   1.426 +	if (confirm_box(true))
   1.427 +	{
   1.428 +		if ($action == 'split_beyond')
   1.429 +		{
   1.430 +			$sort_days = $total = 0;
   1.431 +			$sort_key = $sort_dir = '';
   1.432 +			$sort_by_sql = $sort_order_sql = array();
   1.433 +			mcp_sorting('viewtopic', $sort_days, $sort_key, $sort_dir, $sort_by_sql, $sort_order_sql, $total, $forum_id, $topic_id);
   1.434 +
   1.435 +			$limit_time_sql = ($sort_days) ? 'AND t.topic_last_post_time >= ' . (time() - ($sort_days * 86400)) : '';
   1.436 +
   1.437 +			if ($sort_order_sql[0] == 'u')
   1.438 +			{
   1.439 +				$sql = 'SELECT p.post_id, p.forum_id, p.post_approved
   1.440 +					FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . " u
   1.441 +					WHERE p.topic_id = $topic_id
   1.442 +						AND p.poster_id = u.user_id
   1.443 +						$limit_time_sql
   1.444 +					ORDER BY $sort_order_sql";
   1.445 +			}
   1.446 +			else
   1.447 +			{
   1.448 +				$sql = 'SELECT p.post_id, p.forum_id, p.post_approved
   1.449 +					FROM ' . POSTS_TABLE . " p
   1.450 +					WHERE p.topic_id = $topic_id
   1.451 +						$limit_time_sql
   1.452 +					ORDER BY $sort_order_sql";
   1.453 +			}
   1.454 +			$result = $db->sql_query_limit($sql, 0, $start);
   1.455 +
   1.456 +			$store = false;
   1.457 +			$post_id_list = array();
   1.458 +			while ($row = $db->sql_fetchrow($result))
   1.459 +			{
   1.460 +				// If split from selected post (split_beyond), we split the unapproved items too.
   1.461 +				if (!$row['post_approved'] && !$auth->acl_get('m_approve', $row['forum_id']))
   1.462 +				{
   1.463 +//					continue;
   1.464 +				}
   1.465 +
   1.466 +				// Start to store post_ids as soon as we see the first post that was selected
   1.467 +				if ($row['post_id'] == $post_id)
   1.468 +				{
   1.469 +					$store = true;
   1.470 +				}
   1.471 +
   1.472 +				if ($store)
   1.473 +				{
   1.474 +					$post_id_list[] = $row['post_id'];
   1.475 +				}
   1.476 +			}
   1.477 +			$db->sql_freeresult($result);
   1.478 +		}
   1.479 +
   1.480 +		if (!sizeof($post_id_list))
   1.481 +		{
   1.482 +			trigger_error('NO_POST_SELECTED');
   1.483 +		}
   1.484 +
   1.485 +		$icon_id = request_var('icon', 0);
   1.486 +
   1.487 +		$sql_ary = array(
   1.488 +			'forum_id'		=> $to_forum_id,
   1.489 +			'topic_title'	=> $subject,
   1.490 +			'icon_id'		=> $icon_id,
   1.491 +			'topic_approved'=> 1
   1.492 +		);
   1.493 +
   1.494 +		$sql = 'INSERT INTO ' . TOPICS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
   1.495 +		$db->sql_query($sql);
   1.496 +
   1.497 +		$to_topic_id = $db->sql_nextid();
   1.498 +		move_posts($post_id_list, $to_topic_id);
   1.499 +
   1.500 +		$topic_info = get_topic_data(array($topic_id));
   1.501 +		$topic_info = $topic_info[$topic_id];
   1.502 +
   1.503 +		add_log('mod', $to_forum_id, $to_topic_id, 'LOG_SPLIT_DESTINATION', $subject);
   1.504 +		add_log('mod', $forum_id, $topic_id, 'LOG_SPLIT_SOURCE', $topic_info['topic_title']);
   1.505 +
   1.506 +		// Change topic title of first post
   1.507 +		$sql = 'UPDATE ' . POSTS_TABLE . "
   1.508 +			SET post_subject = '" . $db->sql_escape($subject) . "'
   1.509 +			WHERE post_id = {$post_id_list[0]}";
   1.510 +		$db->sql_query($sql);
   1.511 +
   1.512 +		$success_msg = 'TOPIC_SPLIT_SUCCESS';
   1.513 +
   1.514 +		// Update forum statistics
   1.515 +		set_config_count('num_topics', 1, true);
   1.516 +
   1.517 +		// Link back to both topics
   1.518 +		$return_link = sprintf($user->lang['RETURN_TOPIC'], '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $post_info['forum_id'] . '&amp;t=' . $post_info['topic_id']) . '">', '</a>') . '<br /><br />' . sprintf($user->lang['RETURN_NEW_TOPIC'], '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $to_forum_id . '&amp;t=' . $to_topic_id) . '">', '</a>');
   1.519 +	}
   1.520 +	else
   1.521 +	{
   1.522 +		confirm_box(false, ($action == 'split_all') ? 'SPLIT_TOPIC_ALL' : 'SPLIT_TOPIC_BEYOND', $s_hidden_fields);
   1.523 +	}
   1.524 +
   1.525 +	$redirect = request_var('redirect', "index.$phpEx");
   1.526 +	$redirect = reapply_sid($redirect);
   1.527 +
   1.528 +	if (!$success_msg)
   1.529 +	{
   1.530 +		return;
   1.531 +	}
   1.532 +	else
   1.533 +	{
   1.534 +		meta_refresh(3, append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$to_forum_id&amp;t=$to_topic_id"));
   1.535 +		trigger_error($user->lang[$success_msg] . '<br /><br />' . $return_link);
   1.536 +	}
   1.537 +}
   1.538 +
   1.539 +/**
   1.540 +* Merge selected posts into selected topic
   1.541 +*/
   1.542 +function merge_posts($topic_id, $to_topic_id)
   1.543 +{
   1.544 +	global $db, $template, $user, $phpEx, $phpbb_root_path, $auth;
   1.545 +
   1.546 +	if (!$to_topic_id)
   1.547 +	{
   1.548 +		$template->assign_var('MESSAGE', $user->lang['NO_FINAL_TOPIC_SELECTED']);
   1.549 +		return;
   1.550 +	}
   1.551 +
   1.552 +	$topic_data = get_topic_data(array($to_topic_id), 'm_merge');
   1.553 +
   1.554 +	if (!sizeof($topic_data))
   1.555 +	{
   1.556 +		$template->assign_var('MESSAGE', $user->lang['NO_FINAL_TOPIC_SELECTED']);
   1.557 +		return;
   1.558 +	}
   1.559 +
   1.560 +	$topic_data = $topic_data[$to_topic_id];
   1.561 +
   1.562 +	$post_id_list	= request_var('post_id_list', array(0));
   1.563 +	$start			= request_var('start', 0);
   1.564 +
   1.565 +	if (!sizeof($post_id_list))
   1.566 +	{
   1.567 +		$template->assign_var('MESSAGE', $user->lang['NO_POST_SELECTED']);
   1.568 +		return;
   1.569 +	}
   1.570 +
   1.571 +	if (!check_ids($post_id_list, POSTS_TABLE, 'post_id', array('m_merge')))
   1.572 +	{
   1.573 +		return;
   1.574 +	}
   1.575 +
   1.576 +	$redirect = request_var('redirect', build_url(array('quickmod')));
   1.577 +
   1.578 +	$s_hidden_fields = build_hidden_fields(array(
   1.579 +		'i'				=> 'main',
   1.580 +		'post_id_list'	=> $post_id_list,
   1.581 +		'to_topic_id'	=> $to_topic_id,
   1.582 +		'mode'			=> 'topic_view',
   1.583 +		'action'		=> 'merge_posts',
   1.584 +		'start'			=> $start,
   1.585 +		'redirect'		=> $redirect,
   1.586 +		't'				=> $topic_id)
   1.587 +	);
   1.588 +	$success_msg = $return_link = '';
   1.589 +
   1.590 +	if (confirm_box(true))
   1.591 +	{
   1.592 +		$to_forum_id = $topic_data['forum_id'];
   1.593 +
   1.594 +		move_posts($post_id_list, $to_topic_id);
   1.595 +		add_log('mod', $to_forum_id, $to_topic_id, 'LOG_MERGE', $topic_data['topic_title']);
   1.596 +
   1.597 +		// Message and return links
   1.598 +		$success_msg = 'POSTS_MERGED_SUCCESS';
   1.599 +
   1.600 +		// Does the original topic still exist? If yes, link back to it
   1.601 +		$sql = 'SELECT forum_id
   1.602 +			FROM ' . TOPICS_TABLE . '
   1.603 +			WHERE topic_id = ' . $topic_id;
   1.604 +		$result = $db->sql_query_limit($sql, 1);
   1.605 +		$row = $db->sql_fetchrow($result);
   1.606 +		$db->sql_freeresult($result);
   1.607 +
   1.608 +		if ($row)
   1.609 +		{
   1.610 +			$return_link .= sprintf($user->lang['RETURN_TOPIC'], '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $row['forum_id'] . '&amp;t=' . $topic_id) . '">', '</a>');
   1.611 +		}
   1.612 +		else
   1.613 +		{
   1.614 +			// If the topic no longer exist, we will update the topic watch table.
   1.615 +			// To not let it error out on users watching both topics, we just return on an error...
   1.616 +			$db->sql_return_on_error(true);
   1.617 +			$db->sql_query('UPDATE ' . TOPICS_WATCH_TABLE . ' SET topic_id = ' . (int) $to_topic_id . ' WHERE topic_id = ' . (int) $topic_id);
   1.618 +			$db->sql_return_on_error(false);
   1.619 +
   1.620 +			$db->sql_query('DELETE FROM ' . TOPICS_WATCH_TABLE . ' WHERE topic_id = ' . (int) $topic_id);
   1.621 +		}
   1.622 +
   1.623 +		// Link to the new topic
   1.624 +		$return_link .= (($return_link) ? '<br /><br />' : '') . sprintf($user->lang['RETURN_NEW_TOPIC'], '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $to_forum_id . '&amp;t=' . $to_topic_id) . '">', '</a>');
   1.625 +	}
   1.626 +	else
   1.627 +	{
   1.628 +		confirm_box(false, 'MERGE_POSTS', $s_hidden_fields);
   1.629 +	}
   1.630 +
   1.631 +	$redirect = request_var('redirect', "index.$phpEx");
   1.632 +	$redirect = reapply_sid($redirect);
   1.633 +
   1.634 +	if (!$success_msg)
   1.635 +	{
   1.636 +		return;
   1.637 +	}
   1.638 +	else
   1.639 +	{
   1.640 +		meta_refresh(3, append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$to_forum_id&amp;t=$to_topic_id"));
   1.641 +		trigger_error($user->lang[$success_msg] . '<br /><br />' . $return_link);
   1.642 +	}
   1.643 +}
   1.644 +
   1.645 +?>
   1.646 \ No newline at end of file