includes/mcp/mcp_main.php
changeset 0 ceef6d64c21d
equal deleted inserted replaced
-1:000000000000 0:ceef6d64c21d
       
     1 <?php
       
     2 /**
       
     3 *
       
     4 * @package mcp
       
     5 * @version $Id$
       
     6 * @copyright (c) 2005 phpBB Group
       
     7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
       
     8 *
       
     9 */
       
    10 
       
    11 /**
       
    12 * @ignore
       
    13 */
       
    14 if (!defined('IN_PHPBB'))
       
    15 {
       
    16 	exit;
       
    17 }
       
    18 
       
    19 /**
       
    20 * mcp_main
       
    21 * Handling mcp actions
       
    22 * @package mcp
       
    23 */
       
    24 class mcp_main
       
    25 {
       
    26 	var $p_master;
       
    27 	var $u_action;
       
    28 
       
    29 	function mcp_main(&$p_master)
       
    30 	{
       
    31 		$this->p_master = &$p_master;
       
    32 	}
       
    33 
       
    34 	function main($id, $mode)
       
    35 	{
       
    36 		global $auth, $db, $user, $template, $action;
       
    37 		global $config, $phpbb_root_path, $phpEx;
       
    38 
       
    39 		$quickmod = ($mode == 'quickmod') ? true : false;
       
    40 
       
    41 		switch ($action)
       
    42 		{
       
    43 			case 'lock':
       
    44 			case 'unlock':
       
    45 				$topic_ids = (!$quickmod) ? request_var('topic_id_list', array(0)) : array(request_var('t', 0));
       
    46 
       
    47 				if (!sizeof($topic_ids))
       
    48 				{
       
    49 					trigger_error('NO_TOPIC_SELECTED');
       
    50 				}
       
    51 
       
    52 				lock_unlock($action, $topic_ids);
       
    53 			break;
       
    54 
       
    55 			case 'lock_post':
       
    56 			case 'unlock_post':
       
    57 
       
    58 				$post_ids = (!$quickmod) ? request_var('post_id_list', array(0)) : array(request_var('p', 0));
       
    59 
       
    60 				if (!sizeof($post_ids))
       
    61 				{
       
    62 					trigger_error('NO_POST_SELECTED');
       
    63 				}
       
    64 
       
    65 				lock_unlock($action, $post_ids);
       
    66 			break;
       
    67 
       
    68 			case 'make_announce':
       
    69 			case 'make_sticky':
       
    70 			case 'make_global':
       
    71 			case 'make_normal':
       
    72 
       
    73 				$topic_ids = (!$quickmod) ? request_var('topic_id_list', array(0)) : array(request_var('t', 0));
       
    74 
       
    75 				if (!sizeof($topic_ids))
       
    76 				{
       
    77 					trigger_error('NO_TOPIC_SELECTED');
       
    78 				}
       
    79 
       
    80 				change_topic_type($action, $topic_ids);
       
    81 			break;
       
    82 
       
    83 			case 'move':
       
    84 				$user->add_lang('viewtopic');
       
    85 
       
    86 				$topic_ids = (!$quickmod) ? request_var('topic_id_list', array(0)) : array(request_var('t', 0));
       
    87 
       
    88 				if (!sizeof($topic_ids))
       
    89 				{
       
    90 					trigger_error('NO_TOPIC_SELECTED');
       
    91 				}
       
    92 
       
    93 				mcp_move_topic($topic_ids);
       
    94 			break;
       
    95 
       
    96 			case 'fork':
       
    97 				$user->add_lang('viewtopic');
       
    98 
       
    99 				$topic_ids = (!$quickmod) ? request_var('topic_id_list', array(0)) : array(request_var('t', 0));
       
   100 
       
   101 				if (!sizeof($topic_ids))
       
   102 				{
       
   103 					trigger_error('NO_TOPIC_SELECTED');
       
   104 				}
       
   105 
       
   106 				mcp_fork_topic($topic_ids);
       
   107 			break;
       
   108 
       
   109 			case 'delete_topic':
       
   110 				$user->add_lang('viewtopic');
       
   111 
       
   112 				$topic_ids = (!$quickmod) ? request_var('topic_id_list', array(0)) : array(request_var('t', 0));
       
   113 
       
   114 				if (!sizeof($topic_ids))
       
   115 				{
       
   116 					trigger_error('NO_TOPIC_SELECTED');
       
   117 				}
       
   118 
       
   119 				mcp_delete_topic($topic_ids);
       
   120 			break;
       
   121 
       
   122 			case 'delete_post':
       
   123 				$user->add_lang('posting');
       
   124 
       
   125 				$post_ids = (!$quickmod) ? request_var('post_id_list', array(0)) : array(request_var('p', 0));
       
   126 
       
   127 				if (!sizeof($post_ids))
       
   128 				{
       
   129 					trigger_error('NO_POST_SELECTED');
       
   130 				}
       
   131 
       
   132 				mcp_delete_post($post_ids);
       
   133 			break;
       
   134 		}
       
   135 
       
   136 		switch ($mode)
       
   137 		{
       
   138 			case 'front':
       
   139 				include($phpbb_root_path . 'includes/mcp/mcp_front.' . $phpEx);
       
   140 
       
   141 				$user->add_lang('acp/common');
       
   142 
       
   143 				mcp_front_view($id, $mode, $action);
       
   144 
       
   145 				$this->tpl_name = 'mcp_front';
       
   146 				$this->page_title = 'MCP_MAIN';
       
   147 			break;
       
   148 
       
   149 			case 'forum_view':
       
   150 				include($phpbb_root_path . 'includes/mcp/mcp_forum.' . $phpEx);
       
   151 
       
   152 				$user->add_lang('viewforum');
       
   153 
       
   154 				$forum_id = request_var('f', 0);
       
   155 
       
   156 				$forum_info = get_forum_data($forum_id, 'm_', true);
       
   157 
       
   158 				if (!sizeof($forum_info))
       
   159 				{
       
   160 					$this->main('main', 'front');
       
   161 					return;
       
   162 				}
       
   163 
       
   164 				$forum_info = $forum_info[$forum_id];
       
   165 
       
   166 				mcp_forum_view($id, $mode, $action, $forum_info);
       
   167 
       
   168 				$this->tpl_name = 'mcp_forum';
       
   169 				$this->page_title = 'MCP_MAIN_FORUM_VIEW';
       
   170 			break;
       
   171 
       
   172 			case 'topic_view':
       
   173 				include($phpbb_root_path . 'includes/mcp/mcp_topic.' . $phpEx);
       
   174 
       
   175 				mcp_topic_view($id, $mode, $action);
       
   176 
       
   177 				$this->tpl_name = 'mcp_topic';
       
   178 				$this->page_title = 'MCP_MAIN_TOPIC_VIEW';
       
   179 			break;
       
   180 
       
   181 			case 'post_details':
       
   182 				include($phpbb_root_path . 'includes/mcp/mcp_post.' . $phpEx);
       
   183 
       
   184 				mcp_post_details($id, $mode, $action);
       
   185 
       
   186 				$this->tpl_name = ($action == 'whois') ? 'mcp_whois' : 'mcp_post';
       
   187 				$this->page_title = 'MCP_MAIN_POST_DETAILS';
       
   188 			break;
       
   189 
       
   190 			default:
       
   191 				trigger_error('NO_MODE', E_USER_ERROR);
       
   192 			break;
       
   193 		}
       
   194 	}
       
   195 }
       
   196 
       
   197 /**
       
   198 * Lock/Unlock Topic/Post
       
   199 */
       
   200 function lock_unlock($action, $ids)
       
   201 {
       
   202 	global $auth, $user, $db, $phpEx, $phpbb_root_path;
       
   203 
       
   204 	if ($action == 'lock' || $action == 'unlock')
       
   205 	{
       
   206 		$table = TOPICS_TABLE;
       
   207 		$sql_id = 'topic_id';
       
   208 		$set_id = 'topic_status';
       
   209 		$l_prefix = 'TOPIC';
       
   210 	}
       
   211 	else
       
   212 	{
       
   213 		$table = POSTS_TABLE;
       
   214 		$sql_id = 'post_id';
       
   215 		$set_id = 'post_edit_locked';
       
   216 		$l_prefix = 'POST';
       
   217 	}
       
   218 
       
   219 	$orig_ids = $ids;
       
   220 
       
   221 	if (!check_ids($ids, $table, $sql_id, array('m_lock')))
       
   222 	{
       
   223 		// Make sure that for f_user_lock only the lock action is triggered.
       
   224 		if ($action != 'lock')
       
   225 		{
       
   226 			return;
       
   227 		}
       
   228 
       
   229 		$ids = $orig_ids;
       
   230 
       
   231 		if (!check_ids($ids, $table, $sql_id, array('f_user_lock')))
       
   232 		{
       
   233 			return;
       
   234 		}
       
   235 	}
       
   236 	unset($orig_ids);
       
   237 
       
   238 	$redirect = request_var('redirect', build_url(array('action', 'quickmod')));
       
   239 
       
   240 	$s_hidden_fields = build_hidden_fields(array(
       
   241 		$sql_id . '_list'	=> $ids,
       
   242 		'action'			=> $action,
       
   243 		'redirect'			=> $redirect)
       
   244 	);
       
   245 	$success_msg = '';
       
   246 
       
   247 	if (confirm_box(true))
       
   248 	{
       
   249 		$sql = "UPDATE $table
       
   250 			SET $set_id = " . (($action == 'lock' || $action == 'lock_post') ? ITEM_LOCKED : ITEM_UNLOCKED) . '
       
   251 			WHERE ' . $db->sql_in_set($sql_id, $ids);
       
   252 		$db->sql_query($sql);
       
   253 
       
   254 		$data = ($action == 'lock' || $action == 'unlock') ? get_topic_data($ids) : get_post_data($ids);
       
   255 
       
   256 		foreach ($data as $id => $row)
       
   257 		{
       
   258 			add_log('mod', $row['forum_id'], $row['topic_id'], 'LOG_' . strtoupper($action), $row['topic_title']);
       
   259 		}
       
   260 
       
   261 		$success_msg = $l_prefix . ((sizeof($ids) == 1) ? '' : 'S') . '_' . (($action == 'lock' || $action == 'lock_post') ? 'LOCKED' : 'UNLOCKED') . '_SUCCESS';
       
   262 	}
       
   263 	else
       
   264 	{
       
   265 		confirm_box(false, strtoupper($action) . '_' . $l_prefix . ((sizeof($ids) == 1) ? '' : 'S'), $s_hidden_fields);
       
   266 	}
       
   267 
       
   268 	$redirect = request_var('redirect', "index.$phpEx");
       
   269 	$redirect = reapply_sid($redirect);
       
   270 
       
   271 	if (!$success_msg)
       
   272 	{
       
   273 		redirect($redirect);
       
   274 	}
       
   275 	else
       
   276 	{
       
   277 		meta_refresh(2, $redirect);
       
   278 		trigger_error($user->lang[$success_msg] . '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $redirect . '">', '</a>'));
       
   279 	}
       
   280 }
       
   281 
       
   282 /**
       
   283 * Change Topic Type
       
   284 */
       
   285 function change_topic_type($action, $topic_ids)
       
   286 {
       
   287 	global $auth, $user, $db, $phpEx, $phpbb_root_path;
       
   288 
       
   289 	switch ($action)
       
   290 	{
       
   291 		case 'make_announce':
       
   292 			$new_topic_type = POST_ANNOUNCE;
       
   293 			$check_acl = 'f_announce';
       
   294 			$l_new_type = (sizeof($topic_ids) == 1) ? 'MCP_MAKE_ANNOUNCEMENT' : 'MCP_MAKE_ANNOUNCEMENTS';
       
   295 		break;
       
   296 
       
   297 		case 'make_global':
       
   298 			$new_topic_type = POST_GLOBAL;
       
   299 			$check_acl = 'f_announce';
       
   300 			$l_new_type = (sizeof($topic_ids) == 1) ? 'MCP_MAKE_GLOBAL' : 'MCP_MAKE_GLOBALS';
       
   301 		break;
       
   302 
       
   303 		case 'make_sticky':
       
   304 			$new_topic_type = POST_STICKY;
       
   305 			$check_acl = 'f_sticky';
       
   306 			$l_new_type = (sizeof($topic_ids) == 1) ? 'MCP_MAKE_STICKY' : 'MCP_MAKE_STICKIES';
       
   307 		break;
       
   308 
       
   309 		default:
       
   310 			$new_topic_type = POST_NORMAL;
       
   311 			$check_acl = false;
       
   312 			$l_new_type = (sizeof($topic_ids) == 1) ? 'MCP_MAKE_NORMAL' : 'MCP_MAKE_NORMALS';
       
   313 		break;
       
   314 	}
       
   315 
       
   316 	$forum_id = check_ids($topic_ids, TOPICS_TABLE, 'topic_id', $check_acl, true);
       
   317 
       
   318 	if ($forum_id === false)
       
   319 	{
       
   320 		return;
       
   321 	}
       
   322 
       
   323 	$redirect = request_var('redirect', build_url(array('action', 'quickmod')));
       
   324 
       
   325 	$s_hidden_fields = array(
       
   326 		'topic_id_list'	=> $topic_ids,
       
   327 		'f'				=> $forum_id,
       
   328 		'action'		=> $action,
       
   329 		'redirect'		=> $redirect,
       
   330 	);
       
   331 	$success_msg = '';
       
   332 
       
   333 	if (confirm_box(true))
       
   334 	{
       
   335 		if ($new_topic_type != POST_GLOBAL)
       
   336 		{
       
   337 			$sql = 'UPDATE ' . TOPICS_TABLE . "
       
   338 				SET topic_type = $new_topic_type
       
   339 				WHERE " . $db->sql_in_set('topic_id', $topic_ids) . '
       
   340 					AND forum_id <> 0';
       
   341 			$db->sql_query($sql);
       
   342 
       
   343 			// Reset forum id if a global topic is within the array
       
   344 			$to_forum_id = request_var('to_forum_id', 0);
       
   345 
       
   346 			if ($to_forum_id)
       
   347 			{
       
   348 				$sql = 'UPDATE ' . TOPICS_TABLE . "
       
   349 					SET topic_type = $new_topic_type, forum_id = $to_forum_id
       
   350 					WHERE " . $db->sql_in_set('topic_id', $topic_ids) . '
       
   351 						AND forum_id = 0';
       
   352 				$db->sql_query($sql);
       
   353 
       
   354 				// Update forum_ids for all posts
       
   355 				$sql = 'UPDATE ' . POSTS_TABLE . "
       
   356 					SET forum_id = $to_forum_id
       
   357 					WHERE " . $db->sql_in_set('topic_id', $topic_ids) . '
       
   358 						AND forum_id = 0';
       
   359 				$db->sql_query($sql);
       
   360 
       
   361 				// Do a little forum sync stuff
       
   362 				$sql = 'SELECT SUM(t.topic_replies + t.topic_approved) as topic_posts, COUNT(t.topic_approved) as topics_authed
       
   363 					FROM ' . TOPICS_TABLE . ' t
       
   364 					WHERE ' . $db->sql_in_set('t.topic_id', $topic_ids);
       
   365 				$result = $db->sql_query($sql);
       
   366 				$row_data = $db->sql_fetchrow($result);
       
   367 				$db->sql_freeresult($result);
       
   368 
       
   369 				$sync_sql = array();
       
   370 
       
   371 				if ($row_data['topic_posts'])
       
   372 				{
       
   373 					$sync_sql[$to_forum_id][]	= 'forum_posts = forum_posts + ' . (int) $row_data['topic_posts'];
       
   374 				}
       
   375 
       
   376 				if ($row_data['topics_authed'])
       
   377 				{
       
   378 					$sync_sql[$to_forum_id][]	= 'forum_topics = forum_topics + ' . (int) $row_data['topics_authed'];
       
   379 				}
       
   380 
       
   381 				$sync_sql[$to_forum_id][]	= 'forum_topics_real = forum_topics_real + ' . (int) sizeof($topic_ids);
       
   382 
       
   383 				foreach ($sync_sql as $forum_id_key => $array)
       
   384 				{
       
   385 					$sql = 'UPDATE ' . FORUMS_TABLE . '
       
   386 						SET ' . implode(', ', $array) . '
       
   387 						WHERE forum_id = ' . $forum_id_key;
       
   388 					$db->sql_query($sql);
       
   389 				}
       
   390 
       
   391 				sync('forum', 'forum_id', $to_forum_id);
       
   392 			}
       
   393 		}
       
   394 		else
       
   395 		{
       
   396 			// Get away with those topics already being a global announcement by re-calculating $topic_ids
       
   397 			$sql = 'SELECT topic_id
       
   398 				FROM ' . TOPICS_TABLE . '
       
   399 				WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
       
   400 					AND forum_id <> 0';
       
   401 			$result = $db->sql_query($sql);
       
   402 
       
   403 			$topic_ids = array();
       
   404 			while ($row = $db->sql_fetchrow($result))
       
   405 			{
       
   406 				$topic_ids[] = $row['topic_id'];
       
   407 			}
       
   408 			$db->sql_freeresult($result);
       
   409 
       
   410 			if (sizeof($topic_ids))
       
   411 			{
       
   412 				// Delete topic shadows for global announcements
       
   413 				$sql = 'DELETE FROM ' . TOPICS_TABLE . '
       
   414 					WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids);
       
   415 				$db->sql_query($sql);
       
   416 
       
   417 				$sql = 'UPDATE ' . TOPICS_TABLE . "
       
   418 					SET topic_type = $new_topic_type, forum_id = 0
       
   419 						WHERE " . $db->sql_in_set('topic_id', $topic_ids);
       
   420 				$db->sql_query($sql);
       
   421 
       
   422 				// Update forum_ids for all posts
       
   423 				$sql = 'UPDATE ' . POSTS_TABLE . '
       
   424 					SET forum_id = 0
       
   425 					WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
       
   426 				$db->sql_query($sql);
       
   427 
       
   428 				// Do a little forum sync stuff
       
   429 				$sql = 'SELECT SUM(t.topic_replies + t.topic_approved) as topic_posts, COUNT(t.topic_approved) as topics_authed
       
   430 					FROM ' . TOPICS_TABLE . ' t
       
   431 					WHERE ' . $db->sql_in_set('t.topic_id', $topic_ids);
       
   432 				$result = $db->sql_query($sql);
       
   433 				$row_data = $db->sql_fetchrow($result);
       
   434 				$db->sql_freeresult($result);
       
   435 
       
   436 				$sync_sql = array();
       
   437 
       
   438 				if ($row_data['topic_posts'])
       
   439 				{
       
   440 					$sync_sql[$forum_id][]	= 'forum_posts = forum_posts - ' . (int) $row_data['topic_posts'];
       
   441 				}
       
   442 
       
   443 				if ($row_data['topics_authed'])
       
   444 				{
       
   445 					$sync_sql[$forum_id][]	= 'forum_topics = forum_topics - ' . (int) $row_data['topics_authed'];
       
   446 				}
       
   447 
       
   448 				$sync_sql[$forum_id][]	= 'forum_topics_real = forum_topics_real - ' . (int) sizeof($topic_ids);
       
   449 
       
   450 				foreach ($sync_sql as $forum_id_key => $array)
       
   451 				{
       
   452 					$sql = 'UPDATE ' . FORUMS_TABLE . '
       
   453 						SET ' . implode(', ', $array) . '
       
   454 						WHERE forum_id = ' . $forum_id_key;
       
   455 					$db->sql_query($sql);
       
   456 				}
       
   457 
       
   458 				sync('forum', 'forum_id', $forum_id);
       
   459 			}
       
   460 		}
       
   461 
       
   462 		$success_msg = (sizeof($topic_ids) == 1) ? 'TOPIC_TYPE_CHANGED' : 'TOPICS_TYPE_CHANGED';
       
   463 
       
   464 		if (sizeof($topic_ids))
       
   465 		{
       
   466 			$data = get_topic_data($topic_ids);
       
   467 
       
   468 			foreach ($data as $topic_id => $row)
       
   469 			{
       
   470 				add_log('mod', $forum_id, $topic_id, 'LOG_TOPIC_TYPE_CHANGED', $row['topic_title']);
       
   471 			}
       
   472 		}
       
   473 	}
       
   474 	else
       
   475 	{
       
   476 		// Global topic involved?
       
   477 		$global_involved = false;
       
   478 
       
   479 		if ($new_topic_type != POST_GLOBAL)
       
   480 		{
       
   481 			$sql = 'SELECT forum_id
       
   482 				FROM ' . TOPICS_TABLE . '
       
   483 				WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
       
   484 					AND forum_id = 0';
       
   485 			$result = $db->sql_query($sql);
       
   486 			$row = $db->sql_fetchrow($result);
       
   487 			$db->sql_freeresult($result);
       
   488 
       
   489 			if ($row)
       
   490 			{
       
   491 				$global_involved = true;
       
   492 			}
       
   493 		}
       
   494 
       
   495 		if ($global_involved)
       
   496 		{
       
   497 			global $template;
       
   498 
       
   499 			$template->assign_vars(array(
       
   500 				'S_FORUM_SELECT'		=> make_forum_select(request_var('f', $forum_id), false, false, true, true),
       
   501 				'S_CAN_LEAVE_SHADOW'	=> false,
       
   502 				'ADDITIONAL_MSG'		=> (sizeof($topic_ids) == 1) ? $user->lang['SELECT_FORUM_GLOBAL_ANNOUNCEMENT'] : $user->lang['SELECT_FORUM_GLOBAL_ANNOUNCEMENTS'])
       
   503 			);
       
   504 
       
   505 			confirm_box(false, $l_new_type, build_hidden_fields($s_hidden_fields), 'mcp_move.html');
       
   506 		}
       
   507 		else
       
   508 		{
       
   509 			confirm_box(false, $l_new_type, build_hidden_fields($s_hidden_fields));
       
   510 		}
       
   511 	}
       
   512 
       
   513 	$redirect = request_var('redirect', "index.$phpEx");
       
   514 	$redirect = reapply_sid($redirect);
       
   515 
       
   516 	if (!$success_msg)
       
   517 	{
       
   518 		redirect($redirect);
       
   519 	}
       
   520 	else
       
   521 	{
       
   522 		meta_refresh(2, $redirect);
       
   523 		trigger_error($user->lang[$success_msg] . '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $redirect . '">', '</a>'));
       
   524 	}
       
   525 }
       
   526 
       
   527 /**
       
   528 * Move Topic
       
   529 */
       
   530 function mcp_move_topic($topic_ids)
       
   531 {
       
   532 	global $auth, $user, $db, $template;
       
   533 	global $phpEx, $phpbb_root_path;
       
   534 
       
   535 	// Here we limit the operation to one forum only
       
   536 	$forum_id = check_ids($topic_ids, TOPICS_TABLE, 'topic_id', array('m_move'), true);
       
   537 
       
   538 	if ($forum_id === false)
       
   539 	{
       
   540 		return;
       
   541 	}
       
   542 
       
   543 	$to_forum_id = request_var('to_forum_id', 0);
       
   544 	$redirect = request_var('redirect', build_url(array('action', 'quickmod')));
       
   545 	$additional_msg = $success_msg = '';
       
   546 
       
   547 	$s_hidden_fields = build_hidden_fields(array(
       
   548 		'topic_id_list'	=> $topic_ids,
       
   549 		'f'				=> $forum_id,
       
   550 		'action'		=> 'move',
       
   551 		'redirect'		=> $redirect)
       
   552 	);
       
   553 
       
   554 	if ($to_forum_id)
       
   555 	{
       
   556 		$forum_data = get_forum_data($to_forum_id, 'f_post');
       
   557 
       
   558 		if (!sizeof($forum_data))
       
   559 		{
       
   560 			$additional_msg = $user->lang['FORUM_NOT_EXIST'];
       
   561 		}
       
   562 		else
       
   563 		{
       
   564 			$forum_data = $forum_data[$to_forum_id];
       
   565 
       
   566 			if ($forum_data['forum_type'] != FORUM_POST)
       
   567 			{
       
   568 				$additional_msg = $user->lang['FORUM_NOT_POSTABLE'];
       
   569 			}
       
   570 			else if (!$auth->acl_get('f_post', $to_forum_id) || (!$auth->acl_get('m_approve', $to_forum_id) && !$auth->acl_get('f_noapprove', $to_forum_id)))
       
   571 			{
       
   572 				$additional_msg = $user->lang['USER_CANNOT_POST'];
       
   573 			}
       
   574 			else if ($forum_id == $to_forum_id)
       
   575 			{
       
   576 				$additional_msg = $user->lang['CANNOT_MOVE_SAME_FORUM'];
       
   577 			}
       
   578 		}
       
   579 	}
       
   580 	else if (isset($_POST['confirm']))
       
   581 	{
       
   582 		$additional_msg = $user->lang['FORUM_NOT_EXIST'];
       
   583 	}
       
   584 
       
   585 	if (!$to_forum_id || $additional_msg)
       
   586 	{
       
   587 		unset($_POST['confirm']);
       
   588 		unset($_REQUEST['confirm_key']);
       
   589 	}
       
   590 
       
   591 	if (confirm_box(true))
       
   592 	{
       
   593 		$topic_data = get_topic_data($topic_ids);
       
   594 		$leave_shadow = (isset($_POST['move_leave_shadow'])) ? true : false;
       
   595 
       
   596 		$forum_sync_data = array();
       
   597 
       
   598 		$forum_sync_data[$forum_id] = current($topic_data);
       
   599 		$forum_sync_data[$to_forum_id] = $forum_data;
       
   600 
       
   601 		// Real topics added to target forum
       
   602 		$topics_moved = sizeof($topic_data);
       
   603 
       
   604 		// Approved topics added to target forum
       
   605 		$topics_authed_moved = 0;
       
   606 
       
   607 		// Posts (topic replies + topic post if approved) added to target forum
       
   608 		$topic_posts_added = 0;
       
   609 
       
   610 		// Posts (topic replies + topic post if approved and not global announcement) removed from source forum
       
   611 		$topic_posts_removed = 0;
       
   612 
       
   613 		// Real topics removed from source forum (all topics without global announcements)
       
   614 		$topics_removed = 0;
       
   615 
       
   616 		// Approved topics removed from source forum (except global announcements)
       
   617 		$topics_authed_removed = 0;
       
   618 
       
   619 		foreach ($topic_data as $topic_id => $topic_info)
       
   620 		{
       
   621 			if ($topic_info['topic_approved'])
       
   622 			{
       
   623 				$topics_authed_moved++;
       
   624 				$topic_posts_added++;
       
   625 			}
       
   626 
       
   627 			$topic_posts_added += $topic_info['topic_replies'];
       
   628 
       
   629 			if ($topic_info['topic_type'] != POST_GLOBAL)
       
   630 			{
       
   631 				$topics_removed++;
       
   632 				$topic_posts_removed += $topic_info['topic_replies'];
       
   633 
       
   634 				if ($topic_info['topic_approved'])
       
   635 				{
       
   636 					$topics_authed_removed++;
       
   637 					$topic_posts_removed++;
       
   638 				}
       
   639 			}
       
   640 		}
       
   641 
       
   642 		$db->sql_transaction('begin');
       
   643 
       
   644 		$sync_sql = array();
       
   645 
       
   646 		if ($topic_posts_added)
       
   647 		{
       
   648 			$sync_sql[$to_forum_id][] = 'forum_posts = forum_posts + ' . $topic_posts_added;
       
   649 		}
       
   650 
       
   651 		if ($topics_authed_moved)
       
   652 		{
       
   653 			$sync_sql[$to_forum_id][] = 'forum_topics = forum_topics + ' . (int) $topics_authed_moved;
       
   654 		}
       
   655 
       
   656 		$sync_sql[$to_forum_id][] = 'forum_topics_real = forum_topics_real + ' . (int) $topics_moved;
       
   657 
       
   658 		// Move topics, but do not resync yet
       
   659 		move_topics($topic_ids, $to_forum_id, false);
       
   660 
       
   661 		$forum_ids = array($to_forum_id);
       
   662 		foreach ($topic_data as $topic_id => $row)
       
   663 		{
       
   664 			// Get the list of forums to resync, add a log entry
       
   665 			$forum_ids[] = $row['forum_id'];
       
   666 			add_log('mod', $to_forum_id, $topic_id, 'LOG_MOVE', $row['forum_name'], $forum_data['forum_name']);
       
   667 
       
   668 			// If we have moved a global announcement, we need to correct the topic type
       
   669 			if ($row['topic_type'] == POST_GLOBAL)
       
   670 			{
       
   671 				$sql = 'UPDATE ' . TOPICS_TABLE . '
       
   672 					SET topic_type = ' . POST_ANNOUNCE . '
       
   673 					WHERE topic_id = ' . (int) $row['topic_id'];
       
   674 				$db->sql_query($sql);
       
   675 			}
       
   676 
       
   677 			// Leave a redirection if required and only if the topic is visible to users
       
   678 			if ($leave_shadow && $row['topic_approved'] && $row['topic_type'] != POST_GLOBAL)
       
   679 			{
       
   680 				$shadow = array(
       
   681 					'forum_id'				=>	(int) $row['forum_id'],
       
   682 					'icon_id'				=>	(int) $row['icon_id'],
       
   683 					'topic_attachment'		=>	(int) $row['topic_attachment'],
       
   684 					'topic_approved'		=>	1, // a shadow topic is always approved
       
   685 					'topic_reported'		=>	0, // a shadow topic is never reported
       
   686 					'topic_title'			=>	(string) $row['topic_title'],
       
   687 					'topic_poster'			=>	(int) $row['topic_poster'],
       
   688 					'topic_time'			=>	(int) $row['topic_time'],
       
   689 					'topic_time_limit'		=>	(int) $row['topic_time_limit'],
       
   690 					'topic_views'			=>	(int) $row['topic_views'],
       
   691 					'topic_replies'			=>	(int) $row['topic_replies'],
       
   692 					'topic_replies_real'	=>	(int) $row['topic_replies_real'],
       
   693 					'topic_status'			=>	ITEM_MOVED,
       
   694 					'topic_type'			=>	POST_NORMAL,
       
   695 					'topic_first_post_id'	=>	(int) $row['topic_first_post_id'],
       
   696 					'topic_first_poster_colour'=>(string) $row['topic_first_poster_colour'],
       
   697 					'topic_first_poster_name'=>	(string) $row['topic_first_poster_name'],
       
   698 					'topic_last_post_id'	=>	(int) $row['topic_last_post_id'],
       
   699 					'topic_last_poster_id'	=>	(int) $row['topic_last_poster_id'],
       
   700 					'topic_last_poster_colour'=>(string) $row['topic_last_poster_colour'],
       
   701 					'topic_last_poster_name'=>	(string) $row['topic_last_poster_name'],
       
   702 					'topic_last_post_subject'=>	(string)  $row['topic_last_post_subject'],
       
   703 					'topic_last_post_time'	=>	(int) $row['topic_last_post_time'],
       
   704 					'topic_last_view_time'	=>	(int) $row['topic_last_view_time'],
       
   705 					'topic_moved_id'		=>	(int) $row['topic_id'],
       
   706 					'topic_bumped'			=>	(int) $row['topic_bumped'],
       
   707 					'topic_bumper'			=>	(int) $row['topic_bumper'],
       
   708 					'poll_title'			=>	(string) $row['poll_title'],
       
   709 					'poll_start'			=>	(int) $row['poll_start'],
       
   710 					'poll_length'			=>	(int) $row['poll_length'],
       
   711 					'poll_max_options'		=>	(int) $row['poll_max_options'],
       
   712 					'poll_last_vote'		=>	(int) $row['poll_last_vote']
       
   713 				);
       
   714 
       
   715 				$db->sql_query('INSERT INTO ' . TOPICS_TABLE . $db->sql_build_array('INSERT', $shadow));
       
   716 
       
   717 				// Shadow topics only count on new "topics" and not posts... a shadow topic alone has 0 posts
       
   718 				$topics_removed--;
       
   719 				$topics_authed_removed--;
       
   720 			}
       
   721 		}
       
   722 		unset($topic_data);
       
   723 
       
   724 		if ($topic_posts_removed)
       
   725 		{
       
   726 			$sync_sql[$forum_id][] = 'forum_posts = forum_posts - ' . $topic_posts_removed;
       
   727 		}
       
   728 
       
   729 		if ($topics_removed)
       
   730 		{
       
   731 			$sync_sql[$forum_id][]	= 'forum_topics_real = forum_topics_real - ' . (int) $topics_removed;
       
   732 		}
       
   733 
       
   734 		if ($topics_authed_removed)
       
   735 		{
       
   736 			$sync_sql[$forum_id][]	= 'forum_topics = forum_topics - ' . (int) $topics_authed_removed;
       
   737 		}
       
   738 
       
   739 		$success_msg = (sizeof($topic_ids) == 1) ? 'TOPIC_MOVED_SUCCESS' : 'TOPICS_MOVED_SUCCESS';
       
   740 
       
   741 		foreach ($sync_sql as $forum_id_key => $array)
       
   742 		{
       
   743 			$sql = 'UPDATE ' . FORUMS_TABLE . '
       
   744 				SET ' . implode(', ', $array) . '
       
   745 				WHERE forum_id = ' . $forum_id_key;
       
   746 			$db->sql_query($sql);
       
   747 		}
       
   748 
       
   749 		$db->sql_transaction('commit');
       
   750 
       
   751 		sync('forum', 'forum_id', array($forum_id, $to_forum_id));
       
   752 	}
       
   753 	else
       
   754 	{
       
   755 		$template->assign_vars(array(
       
   756 			'S_FORUM_SELECT'		=> make_forum_select($to_forum_id, $forum_id, false, true, true, true),
       
   757 			'S_CAN_LEAVE_SHADOW'	=> true,
       
   758 			'ADDITIONAL_MSG'		=> $additional_msg)
       
   759 		);
       
   760 
       
   761 		confirm_box(false, 'MOVE_TOPIC' . ((sizeof($topic_ids) == 1) ? '' : 'S'), $s_hidden_fields, 'mcp_move.html');
       
   762 	}
       
   763 
       
   764 	$redirect = request_var('redirect', "index.$phpEx");
       
   765 	$redirect = reapply_sid($redirect);
       
   766 
       
   767 	if (!$success_msg)
       
   768 	{
       
   769 		redirect($redirect);
       
   770 	}
       
   771 	else
       
   772 	{
       
   773 		meta_refresh(3, $redirect);
       
   774 
       
   775 		$message = $user->lang[$success_msg];
       
   776 		$message .= '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $redirect . '">', '</a>');
       
   777 		$message .= '<br /><br />' . sprintf($user->lang['RETURN_FORUM'], '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id") . '">', '</a>');
       
   778 		$message .= '<br /><br />' . sprintf($user->lang['RETURN_NEW_FORUM'], '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$to_forum_id") . '">', '</a>');
       
   779 
       
   780 		trigger_error($message);
       
   781 	}
       
   782 }
       
   783 
       
   784 /**
       
   785 * Delete Topics
       
   786 */
       
   787 function mcp_delete_topic($topic_ids)
       
   788 {
       
   789 	global $auth, $user, $db, $phpEx, $phpbb_root_path;
       
   790 
       
   791 	if (!check_ids($topic_ids, TOPICS_TABLE, 'topic_id', array('m_delete')))
       
   792 	{
       
   793 		return;
       
   794 	}
       
   795 
       
   796 	$redirect = request_var('redirect', build_url(array('action', 'quickmod')));
       
   797 	$forum_id = request_var('f', 0);
       
   798 
       
   799 	$s_hidden_fields = build_hidden_fields(array(
       
   800 		'topic_id_list'	=> $topic_ids,
       
   801 		'f'				=> $forum_id,
       
   802 		'action'		=> 'delete_topic',
       
   803 		'redirect'		=> $redirect)
       
   804 	);
       
   805 	$success_msg = '';
       
   806 
       
   807 	if (confirm_box(true))
       
   808 	{
       
   809 		$success_msg = (sizeof($topic_ids) == 1) ? 'TOPIC_DELETED_SUCCESS' : 'TOPICS_DELETED_SUCCESS';
       
   810 
       
   811 		$data = get_topic_data($topic_ids);
       
   812 
       
   813 		foreach ($data as $topic_id => $row)
       
   814 		{
       
   815 			if ($row['topic_moved_id'])
       
   816 			{
       
   817 				add_log('mod', $row['forum_id'], $topic_id, 'LOG_DELETE_SHADOW_TOPIC', $row['topic_title']);
       
   818 			}
       
   819 			else
       
   820 			{
       
   821 				add_log('mod', $row['forum_id'], $topic_id, 'LOG_DELETE_TOPIC', $row['topic_title'], $row['topic_first_poster_name']);
       
   822 			}
       
   823 		}
       
   824 
       
   825 		$return = delete_topics('topic_id', $topic_ids);
       
   826 	}
       
   827 	else
       
   828 	{
       
   829 		confirm_box(false, (sizeof($topic_ids) == 1) ? 'DELETE_TOPIC' : 'DELETE_TOPICS', $s_hidden_fields);
       
   830 	}
       
   831 
       
   832 	if (!isset($_REQUEST['quickmod']))
       
   833 	{
       
   834 		$redirect = request_var('redirect', "index.$phpEx");
       
   835 		$redirect = reapply_sid($redirect);
       
   836 		$redirect_message = 'PAGE';
       
   837 	}
       
   838 	else
       
   839 	{
       
   840 		$redirect = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id);
       
   841 		$redirect_message = 'FORUM';
       
   842 	}
       
   843 
       
   844 	if (!$success_msg)
       
   845 	{
       
   846 		redirect($redirect);
       
   847 	}
       
   848 	else
       
   849 	{
       
   850 		meta_refresh(3, $redirect);
       
   851 		trigger_error($user->lang[$success_msg] . '<br /><br />' . sprintf($user->lang['RETURN_' . $redirect_message], '<a href="' . $redirect . '">', '</a>'));
       
   852 	}
       
   853 }
       
   854 
       
   855 /**
       
   856 * Delete Posts
       
   857 */
       
   858 function mcp_delete_post($post_ids)
       
   859 {
       
   860 	global $auth, $user, $db, $phpEx, $phpbb_root_path;
       
   861 
       
   862 	if (!check_ids($post_ids, POSTS_TABLE, 'post_id', array('m_delete')))
       
   863 	{
       
   864 		return;
       
   865 	}
       
   866 
       
   867 	$redirect = request_var('redirect', build_url(array('action', 'quickmod')));
       
   868 	$forum_id = request_var('f', 0);
       
   869 
       
   870 	$s_hidden_fields = build_hidden_fields(array(
       
   871 		'post_id_list'	=> $post_ids,
       
   872 		'f'				=> $forum_id,
       
   873 		'action'		=> 'delete_post',
       
   874 		'redirect'		=> $redirect)
       
   875 	);
       
   876 	$success_msg = '';
       
   877 
       
   878 	if (confirm_box(true))
       
   879 	{
       
   880 		if (!function_exists('delete_posts'))
       
   881 		{
       
   882 			include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
       
   883 		}
       
   884 
       
   885 		// Count the number of topics that are affected
       
   886 		// I did not use COUNT(DISTINCT ...) because I remember having problems
       
   887 		// with it on older versions of MySQL -- Ashe
       
   888 
       
   889 		$sql = 'SELECT DISTINCT topic_id
       
   890 			FROM ' . POSTS_TABLE . '
       
   891 			WHERE ' . $db->sql_in_set('post_id', $post_ids);
       
   892 		$result = $db->sql_query($sql);
       
   893 
       
   894 		$topic_id_list = array();
       
   895 		while ($row = $db->sql_fetchrow($result))
       
   896 		{
       
   897 			$topic_id_list[] = $row['topic_id'];
       
   898 		}
       
   899 		$affected_topics = sizeof($topic_id_list);
       
   900 		$db->sql_freeresult($result);
       
   901 
       
   902 		$post_data = get_post_data($post_ids);
       
   903 
       
   904 		foreach ($post_data as $id => $row)
       
   905 		{
       
   906 			$post_username = ($row['poster_id'] == ANONYMOUS && !empty($row['post_username'])) ? $row['post_username'] : $row['username'];
       
   907 			add_log('mod', $row['forum_id'], $row['topic_id'], 'LOG_DELETE_POST', $row['post_subject'], $post_username);
       
   908 		}
       
   909 
       
   910 		// Now delete the posts, topics and forums are automatically resync'ed
       
   911 		delete_posts('post_id', $post_ids);
       
   912 
       
   913 		$sql = 'SELECT COUNT(topic_id) AS topics_left
       
   914 			FROM ' . TOPICS_TABLE . '
       
   915 			WHERE ' . $db->sql_in_set('topic_id', $topic_id_list);
       
   916 		$result = $db->sql_query_limit($sql, 1);
       
   917 
       
   918 		$deleted_topics = ($row = $db->sql_fetchrow($result)) ? ($affected_topics - $row['topics_left']) : $affected_topics;
       
   919 		$db->sql_freeresult($result);
       
   920 
       
   921 		$topic_id = request_var('t', 0);
       
   922 
       
   923 		// Return links
       
   924 		$return_link = array();
       
   925 		if ($affected_topics == 1 && !$deleted_topics && $topic_id)
       
   926 		{
       
   927 			$return_link[] = sprintf($user->lang['RETURN_TOPIC'], '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id") . '">', '</a>');
       
   928 		}
       
   929 		$return_link[] = sprintf($user->lang['RETURN_FORUM'], '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id) . '">', '</a>');
       
   930 
       
   931 		if (sizeof($post_ids) == 1)
       
   932 		{
       
   933 			if ($deleted_topics)
       
   934 			{
       
   935 				// We deleted the only post of a topic, which in turn has
       
   936 				// been removed from the database
       
   937 				$success_msg = $user->lang['TOPIC_DELETED_SUCCESS'];
       
   938 			}
       
   939 			else
       
   940 			{
       
   941 				$success_msg = $user->lang['POST_DELETED_SUCCESS'];
       
   942 			}
       
   943 		}
       
   944 		else
       
   945 		{
       
   946 			if ($deleted_topics)
       
   947 			{
       
   948 				// Some of topics disappeared
       
   949 				$success_msg = $user->lang['POSTS_DELETED_SUCCESS'] . '<br /><br />' . $user->lang['EMPTY_TOPICS_REMOVED_WARNING'];
       
   950 			}
       
   951 			else
       
   952 			{
       
   953 				$success_msg = $user->lang['POSTS_DELETED_SUCCESS'];
       
   954 			}
       
   955 		}
       
   956 	}
       
   957 	else
       
   958 	{
       
   959 		confirm_box(false, (sizeof($post_ids) == 1) ? 'DELETE_POST' : 'DELETE_POSTS', $s_hidden_fields);
       
   960 	}
       
   961 
       
   962 	$redirect = request_var('redirect', "index.$phpEx");
       
   963 	$redirect = reapply_sid($redirect);
       
   964 
       
   965 	if (!$success_msg)
       
   966 	{
       
   967 		redirect($redirect);
       
   968 	}
       
   969 	else
       
   970 	{
       
   971 		if ($affected_topics != 1 || $deleted_topics || !$topic_id)
       
   972 		{
       
   973 			$redirect = append_sid("{$phpbb_root_path}mcp.$phpEx", "f=$forum_id&i=main&mode=forum_view", false);
       
   974 		}
       
   975 
       
   976 		meta_refresh(3, $redirect);
       
   977 		trigger_error($success_msg . '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $redirect . '">', '</a>') . '<br /><br />' . implode('<br /><br />', $return_link));
       
   978 	}
       
   979 }
       
   980 
       
   981 /**
       
   982 * Fork Topic
       
   983 */
       
   984 function mcp_fork_topic($topic_ids)
       
   985 {
       
   986 	global $auth, $user, $db, $template, $config;
       
   987 	global $phpEx, $phpbb_root_path;
       
   988 
       
   989 	if (!check_ids($topic_ids, TOPICS_TABLE, 'topic_id', array('m_')))
       
   990 	{
       
   991 		return;
       
   992 	}
       
   993 
       
   994 	$to_forum_id = request_var('to_forum_id', 0);
       
   995 	$forum_id = request_var('f', 0);
       
   996 	$redirect = request_var('redirect', build_url(array('action', 'quickmod')));
       
   997 	$additional_msg = $success_msg = '';
       
   998 
       
   999 	$s_hidden_fields = build_hidden_fields(array(
       
  1000 		'topic_id_list'	=> $topic_ids,
       
  1001 		'f'				=> $forum_id,
       
  1002 		'action'		=> 'fork',
       
  1003 		'redirect'		=> $redirect)
       
  1004 	);
       
  1005 
       
  1006 	if ($to_forum_id)
       
  1007 	{
       
  1008 		$forum_data = get_forum_data($to_forum_id, 'f_post');
       
  1009 
       
  1010 		if (!sizeof($topic_ids))
       
  1011 		{
       
  1012 			$additional_msg = $user->lang['NO_TOPIC_SELECTED'];
       
  1013 		}
       
  1014 		else if (!sizeof($forum_data))
       
  1015 		{
       
  1016 			$additional_msg = $user->lang['FORUM_NOT_EXIST'];
       
  1017 		}
       
  1018 		else
       
  1019 		{
       
  1020 			$forum_data = $forum_data[$to_forum_id];
       
  1021 
       
  1022 			if ($forum_data['forum_type'] != FORUM_POST)
       
  1023 			{
       
  1024 				$additional_msg = $user->lang['FORUM_NOT_POSTABLE'];
       
  1025 			}
       
  1026 			else if (!$auth->acl_get('f_post', $to_forum_id))
       
  1027 			{
       
  1028 				$additional_msg = $user->lang['USER_CANNOT_POST'];
       
  1029 			}
       
  1030 		}
       
  1031 	}
       
  1032 	else if (isset($_POST['confirm']))
       
  1033 	{
       
  1034 		$additional_msg = $user->lang['FORUM_NOT_EXIST'];
       
  1035 	}
       
  1036 
       
  1037 	if ($additional_msg)
       
  1038 	{
       
  1039 		unset($_POST['confirm']);
       
  1040 		unset($_REQUEST['confirm_key']);
       
  1041 	}
       
  1042 
       
  1043 	if (confirm_box(true))
       
  1044 	{
       
  1045 		$topic_data = get_topic_data($topic_ids, 'f_post');
       
  1046 
       
  1047 		$total_posts = 0;
       
  1048 		$new_topic_id_list = array();
       
  1049 
       
  1050 
       
  1051 		foreach ($topic_data as $topic_id => $topic_row)
       
  1052 		{
       
  1053 			if (!isset($search_type) && $topic_row['enable_indexing'])
       
  1054 			{
       
  1055 				// Select the search method and do some additional checks to ensure it can actually be utilised
       
  1056 				$search_type = basename($config['search_type']);
       
  1057 
       
  1058 				if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
       
  1059 				{
       
  1060 					trigger_error('NO_SUCH_SEARCH_MODULE');
       
  1061 				}
       
  1062 
       
  1063 				if (!class_exists($search_type))
       
  1064 				{
       
  1065 					include("{$phpbb_root_path}includes/search/$search_type.$phpEx");
       
  1066 				}
       
  1067 
       
  1068 				$error = false;
       
  1069 				$search = new $search_type($error);
       
  1070 				$search_mode = 'post';
       
  1071 
       
  1072 				if ($error)
       
  1073 				{
       
  1074 					trigger_error($error);
       
  1075 				}
       
  1076 			}
       
  1077 			else if (!isset($search_type) && !$topic_row['enable_indexing'])
       
  1078 			{
       
  1079 				$search_type = false;
       
  1080 			}
       
  1081 
       
  1082 			$sql_ary = array(
       
  1083 				'forum_id'					=> (int) $to_forum_id,
       
  1084 				'icon_id'					=> (int) $topic_row['icon_id'],
       
  1085 				'topic_attachment'			=> (int) $topic_row['topic_attachment'],
       
  1086 				'topic_approved'			=> 1,
       
  1087 				'topic_reported'			=> 0,
       
  1088 				'topic_title'				=> (string) $topic_row['topic_title'],
       
  1089 				'topic_poster'				=> (int) $topic_row['topic_poster'],
       
  1090 				'topic_time'				=> (int) $topic_row['topic_time'],
       
  1091 				'topic_replies'				=> (int) $topic_row['topic_replies_real'],
       
  1092 				'topic_replies_real'		=> (int) $topic_row['topic_replies_real'],
       
  1093 				'topic_status'				=> (int) $topic_row['topic_status'],
       
  1094 				'topic_type'				=> (int) $topic_row['topic_type'],
       
  1095 				'topic_first_poster_name'	=> (string) $topic_row['topic_first_poster_name'],
       
  1096 				'topic_last_poster_id'		=> (int) $topic_row['topic_last_poster_id'],
       
  1097 				'topic_last_poster_name'	=> (string) $topic_row['topic_last_poster_name'],
       
  1098 				'topic_last_post_time'		=> (int) $topic_row['topic_last_post_time'],
       
  1099 				'topic_last_view_time'		=> (int) $topic_row['topic_last_view_time'],
       
  1100 				'topic_bumped'				=> (int) $topic_row['topic_bumped'],
       
  1101 				'topic_bumper'				=> (int) $topic_row['topic_bumper'],
       
  1102 				'poll_title'				=> (string) $topic_row['poll_title'],
       
  1103 				'poll_start'				=> (int) $topic_row['poll_start'],
       
  1104 				'poll_length'				=> (int) $topic_row['poll_length'],
       
  1105 				'poll_max_options'			=> (int) $topic_row['poll_max_options'],
       
  1106 				'poll_vote_change'			=> (int) $topic_row['poll_vote_change'],
       
  1107 			);
       
  1108 
       
  1109 			$db->sql_query('INSERT INTO ' . TOPICS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
       
  1110 			$new_topic_id = $db->sql_nextid();
       
  1111 			$new_topic_id_list[$topic_id] = $new_topic_id;
       
  1112 
       
  1113 			if ($topic_row['poll_start'])
       
  1114 			{
       
  1115 				$poll_rows = array();
       
  1116 
       
  1117 				$sql = 'SELECT *
       
  1118 					FROM ' . POLL_OPTIONS_TABLE . "
       
  1119 					WHERE topic_id = $topic_id";
       
  1120 				$result = $db->sql_query($sql);
       
  1121 
       
  1122 				while ($row = $db->sql_fetchrow($result))
       
  1123 				{
       
  1124 					$sql_ary = array(
       
  1125 						'poll_option_id'	=> (int) $row['poll_option_id'],
       
  1126 						'topic_id'			=> (int) $new_topic_id,
       
  1127 						'poll_option_text'	=> (string) $row['poll_option_text'],
       
  1128 						'poll_option_total'	=> 0
       
  1129 					);
       
  1130 
       
  1131 					$db->sql_query('INSERT INTO ' . POLL_OPTIONS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
       
  1132 				}
       
  1133 			}
       
  1134 
       
  1135 			$sql = 'SELECT *
       
  1136 				FROM ' . POSTS_TABLE . "
       
  1137 				WHERE topic_id = $topic_id
       
  1138 				ORDER BY post_time ASC";
       
  1139 			$result = $db->sql_query($sql);
       
  1140 
       
  1141 			$post_rows = array();
       
  1142 			while ($row = $db->sql_fetchrow($result))
       
  1143 			{
       
  1144 				$post_rows[] = $row;
       
  1145 			}
       
  1146 			$db->sql_freeresult($result);
       
  1147 
       
  1148 			if (!sizeof($post_rows))
       
  1149 			{
       
  1150 				continue;
       
  1151 			}
       
  1152 
       
  1153 			$total_posts += sizeof($post_rows);
       
  1154 			foreach ($post_rows as $row)
       
  1155 			{
       
  1156 				$sql_ary = array(
       
  1157 					'topic_id'			=> (int) $new_topic_id,
       
  1158 					'forum_id'			=> (int) $to_forum_id,
       
  1159 					'poster_id'			=> (int) $row['poster_id'],
       
  1160 					'icon_id'			=> (int) $row['icon_id'],
       
  1161 					'poster_ip'			=> (string) $row['poster_ip'],
       
  1162 					'post_time'			=> (int) $row['post_time'],
       
  1163 					'post_approved'		=> 1,
       
  1164 					'post_reported'		=> 0,
       
  1165 					'enable_bbcode'		=> (int) $row['enable_bbcode'],
       
  1166 					'enable_smilies'	=> (int) $row['enable_smilies'],
       
  1167 					'enable_magic_url'	=> (int) $row['enable_magic_url'],
       
  1168 					'enable_sig'		=> (int) $row['enable_sig'],
       
  1169 					'post_username'		=> (string) $row['post_username'],
       
  1170 					'post_subject'		=> (string) $row['post_subject'],
       
  1171 					'post_text'			=> (string) $row['post_text'],
       
  1172 					'post_edit_reason'	=> (string) $row['post_edit_reason'],
       
  1173 					'post_edit_user'	=> (int) $row['post_edit_user'],
       
  1174 					'post_checksum'		=> (string) $row['post_checksum'],
       
  1175 					'post_attachment'	=> (int) $row['post_attachment'],
       
  1176 					'bbcode_bitfield'	=> $row['bbcode_bitfield'],
       
  1177 					'bbcode_uid'		=> (string) $row['bbcode_uid'],
       
  1178 					'post_edit_time'	=> (int) $row['post_edit_time'],
       
  1179 					'post_edit_count'	=> (int) $row['post_edit_count'],
       
  1180 					'post_edit_locked'	=> (int) $row['post_edit_locked'],
       
  1181 					'post_postcount'	=> 0,
       
  1182 				);
       
  1183 
       
  1184 				$db->sql_query('INSERT INTO ' . POSTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
       
  1185 				$new_post_id = $db->sql_nextid();
       
  1186 
       
  1187 				// Copy whether the topic is dotted
       
  1188 				markread('post', $to_forum_id, $new_topic_id, 0, $row['poster_id']);
       
  1189 
       
  1190 				if (!empty($search_type))
       
  1191 				{
       
  1192 					$search->index($search_mode, $new_post_id, $sql_ary['post_text'], $sql_ary['post_subject'], $sql_ary['poster_id'], ($topic_row['topic_type'] == POST_GLOBAL) ? 0 : $to_forum_id);
       
  1193 					$search_mode = 'reply'; // After one we index replies
       
  1194 				}
       
  1195 
       
  1196 				// Copy Attachments
       
  1197 				if ($row['post_attachment'])
       
  1198 				{
       
  1199 					$sql = 'SELECT * FROM ' . ATTACHMENTS_TABLE . "
       
  1200 						WHERE post_msg_id = {$row['post_id']}
       
  1201 							AND topic_id = $topic_id
       
  1202 							AND in_message = 0";
       
  1203 					$result = $db->sql_query($sql);
       
  1204 
       
  1205 					$sql_ary = array();
       
  1206 					while ($attach_row = $db->sql_fetchrow($result))
       
  1207 					{
       
  1208 						$sql_ary[] = array(
       
  1209 							'post_msg_id'		=> (int) $new_post_id,
       
  1210 							'topic_id'			=> (int) $new_topic_id,
       
  1211 							'in_message'		=> 0,
       
  1212 							'is_orphan'			=> (int) $attach_row['is_orphan'],
       
  1213 							'poster_id'			=> (int) $attach_row['poster_id'],
       
  1214 							'physical_filename'	=> (string) utf8_basename($attach_row['physical_filename']),
       
  1215 							'real_filename'		=> (string) utf8_basename($attach_row['real_filename']),
       
  1216 							'download_count'	=> (int) $attach_row['download_count'],
       
  1217 							'attach_comment'	=> (string) $attach_row['attach_comment'],
       
  1218 							'extension'			=> (string) $attach_row['extension'],
       
  1219 							'mimetype'			=> (string) $attach_row['mimetype'],
       
  1220 							'filesize'			=> (int) $attach_row['filesize'],
       
  1221 							'filetime'			=> (int) $attach_row['filetime'],
       
  1222 							'thumbnail'			=> (int) $attach_row['thumbnail']
       
  1223 						);
       
  1224 					}
       
  1225 					$db->sql_freeresult($result);
       
  1226 
       
  1227 					if (sizeof($sql_ary))
       
  1228 					{
       
  1229 						$db->sql_multi_insert(ATTACHMENTS_TABLE, $sql_ary);
       
  1230 					}
       
  1231 				}
       
  1232 			}
       
  1233 
       
  1234 			$sql = 'SELECT user_id, notify_status
       
  1235 				FROM ' . TOPICS_WATCH_TABLE . '
       
  1236 				WHERE topic_id = ' . $topic_id;
       
  1237 			$result = $db->sql_query($sql);
       
  1238 
       
  1239 			$sql_ary = array();
       
  1240 			while ($row = $db->sql_fetchrow($result))
       
  1241 			{
       
  1242 				$sql_ary[] = array(
       
  1243 					'topic_id'		=> (int) $new_topic_id,
       
  1244 					'user_id'		=> (int) $row['user_id'],
       
  1245 					'notify_status'	=> (int) $row['notify_status'],
       
  1246 				);
       
  1247 			}
       
  1248 			$db->sql_freeresult($result);
       
  1249 
       
  1250 			if (sizeof($sql_ary))
       
  1251 			{
       
  1252 				$db->sql_multi_insert(TOPICS_WATCH_TABLE, $sql_ary);
       
  1253 			}
       
  1254 		}
       
  1255 
       
  1256 		// Sync new topics, parent forums and board stats
       
  1257 		sync('topic', 'topic_id', $new_topic_id_list);
       
  1258 
       
  1259 		$sync_sql = array();
       
  1260 
       
  1261 		$sync_sql[$to_forum_id][]	= 'forum_posts = forum_posts + ' . $total_posts;
       
  1262 		$sync_sql[$to_forum_id][]	= 'forum_topics = forum_topics + ' . sizeof($new_topic_id_list);
       
  1263 		$sync_sql[$to_forum_id][]	= 'forum_topics_real = forum_topics_real + ' . sizeof($new_topic_id_list);
       
  1264 
       
  1265 		foreach ($sync_sql as $forum_id_key => $array)
       
  1266 		{
       
  1267 			$sql = 'UPDATE ' . FORUMS_TABLE . '
       
  1268 				SET ' . implode(', ', $array) . '
       
  1269 				WHERE forum_id = ' . $forum_id_key;
       
  1270 			$db->sql_query($sql);
       
  1271 		}
       
  1272 
       
  1273 		sync('forum', 'forum_id', $to_forum_id);
       
  1274 		set_config_count('num_topics', sizeof($new_topic_id_list), true);
       
  1275 		set_config_count('num_posts', $total_posts, true);
       
  1276 
       
  1277 		foreach ($new_topic_id_list as $topic_id => $new_topic_id)
       
  1278 		{
       
  1279 			add_log('mod', $to_forum_id, $new_topic_id, 'LOG_FORK', $topic_row['forum_name']);
       
  1280 		}
       
  1281 
       
  1282 		$success_msg = (sizeof($topic_ids) == 1) ? 'TOPIC_FORKED_SUCCESS' : 'TOPICS_FORKED_SUCCESS';
       
  1283 	}
       
  1284 	else
       
  1285 	{
       
  1286 		$template->assign_vars(array(
       
  1287 			'S_FORUM_SELECT'		=> make_forum_select($to_forum_id, false, false, true, true, true),
       
  1288 			'S_CAN_LEAVE_SHADOW'	=> false,
       
  1289 			'ADDITIONAL_MSG'		=> $additional_msg)
       
  1290 		);
       
  1291 
       
  1292 		confirm_box(false, 'FORK_TOPIC' . ((sizeof($topic_ids) == 1) ? '' : 'S'), $s_hidden_fields, 'mcp_move.html');
       
  1293 	}
       
  1294 
       
  1295 	$redirect = request_var('redirect', "index.$phpEx");
       
  1296 	$redirect = reapply_sid($redirect);
       
  1297 
       
  1298 	if (!$success_msg)
       
  1299 	{
       
  1300 		redirect($redirect);
       
  1301 	}
       
  1302 	else
       
  1303 	{
       
  1304 		$redirect_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id);
       
  1305 		meta_refresh(3, $redirect_url);
       
  1306 		$return_link = sprintf($user->lang['RETURN_FORUM'], '<a href="' . $redirect_url . '">', '</a>');
       
  1307 
       
  1308 		if ($forum_id != $to_forum_id)
       
  1309 		{
       
  1310 			$return_link .= '<br /><br />' . sprintf($user->lang['RETURN_NEW_FORUM'], '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $to_forum_id) . '">', '</a>');
       
  1311 		}
       
  1312 
       
  1313 		trigger_error($user->lang[$success_msg] . '<br /><br />' . $return_link);
       
  1314 	}
       
  1315 }
       
  1316 
       
  1317 ?>