PHP Code:
if ($_POST['do'] == 'domovethreadsbin')
{
$vbulletin->input->clean_array_gpc('p', array(
'destforumid' => TYPE_UINT,
'redirect' => TYPE_STR,
'frame' => TYPE_STR,
'period' => TYPE_UINT,
));
// check whether dest can contain posts
$destforumid = verify_id('forum', '168');
$destforuminfo = fetch_foruminfo($destforumid);
if (!$destforuminfo['cancontainthreads'] OR $destforuminfo['link'])
{
eval(standard_error(fetch_error('moveillegalforum')));
}
// check destination forum permissions
$forumperms = fetch_permissions($destforuminfo['forumid']);
if (!($forumperms & $vbulletin->bf_ugp_forumpermissions['canview']))
{
print_no_permission();
}
if ($vbulletin->GPC['redirect'] == 'none')
{
$method = 'move';
}
else
{
$method = 'movered';
}
$countingthreads = array();
$redirectids = array();
// Validate threads
$threads = $db->query_read_slave("
SELECT threadid, visible, open, pollid, title, prefixid, postuserid, forumid
" . ($method == 'movered' ? ", lastpost, replycount, postusername, lastposter, dateline, views, iconid" : "") . "
FROM " . TABLE_PREFIX . "thread
WHERE threadid IN(" . implode(',', $threadids) . ")
");
while ($thread = $db->fetch_array($threads))
{
$forumperms = fetch_permissions($thread['forumid']);
if (
!($forumperms & $vbulletin->bf_ugp_forumpermissions['canview'])
OR
!($forumperms & $vbulletin->bf_ugp_forumpermissions['canviewthreads'])
OR
(!($forumperms & $vbulletin->bf_ugp_forumpermissions['canviewothers']) AND $thread['postuserid'] != $vbulletin->userinfo['userid'])
)
{
print_no_permission();
}
$thread['prefix_plain_html'] = ($thread['prefixid'] ? htmlspecialchars_uni($vbphrase["prefix_$thread[prefixid]_title_plain"]) . ' ' : '');
if (!can_moderate($thread['forumid'], 'canmanagethreads'))
{
eval(standard_error(fetch_error('you_do_not_have_permission_to_manage_threads_and_posts', $vbphrase['n_a'], $thread['prefix_plain_html'] . $thread['title'], $vbulletin->forumcache["$thread[forumid]"]['title'])));
}
else if (!$thread['visible'] AND !can_moderate($thread['forumid'], 'canmoderateposts'))
{
eval(standard_error(fetch_error('you_do_not_have_permission_to_manage_moderated_threads_and_posts')));
}
else if ($thread['visible'] == 2 AND !can_moderate($thread['forumid'], 'candeleteposts'))
{
eval(standard_error(fetch_error('you_do_not_have_permission_to_manage_deleted_threads_and_posts', $vbphrase['n_a'], $thread['prefix_plain_html'] . $thread['title'], $vbulletin->forumcache["$thread[forumid]"]['title'])));
}
if ($thread['visible'] == 2 AND !can_moderate($destforuminfo['forumid'], 'candeleteposts'))
{
eval(standard_error(fetch_error('you_do_not_have_permission_to_manage_deleted_threads_and_posts_in_destination_forum')));
}
else if (!$thread['visible'] AND !can_moderate($destforuminfo['forumid'], 'canmoderateposts'))
{
eval(standard_error(fetch_error('you_do_not_have_permission_to_manage_moderated_threads_and_posts_in_destination_forum')));
}
// Ignore all threads that are already in the destination forum
if ($thread['forumid'] == $destforuminfo['forumid'])
{
$sameforum = true;
continue;
}
$threadarray["$thread[threadid]"] = $thread;
$forumlist["$thread[forumid]"] = true;
if ($thread['open'] == 10)
{
$redirectids["$thread[pollid]"][] = $thread['threadid'];
}
else if ($thread['visible'])
{
$countingthreads[] = $thread['threadid'];
}
}
if (empty($threadarray))
{
if ($sameforum)
{
eval(standard_error(fetch_error('thread_is_already_in_the_forum')));
}
else
{
eval(standard_error(fetch_error('you_did_not_select_any_valid_threads')));
}
}
// check to see if these threads are being returned to a forum they've already been in
// if redirects exist in the destination forum, remove them
$checkprevious = $db->query_read_slave("
SELECT threadid
FROM " . TABLE_PREFIX . "thread
WHERE forumid = $destforuminfo[forumid]
AND open = 10
AND pollid IN(" . implode(',', array_keys($threadarray)) . ")
");
while ($check = $db->fetch_array($checkprevious))
{
$old_redirect =& datamanager_init('Thread', $vbulletin, ERRTYPE_SILENT, 'threadpost');
$old_redirect->set_existing($check);
$old_redirect->delete(false, true, NULL, false);
unset($old_redirect);
}
// check to see if a redirect is being moved to a forum where its destination thread already exists
// if so delete the redirect
if (!empty($redirectids))
{
$checkprevious = $db->query_read_slave("
SELECT threadid
FROM " . TABLE_PREFIX . "thread
WHERE forumid = $destforuminfo[forumid]
AND threadid IN(" . implode(',', array_keys($redirectids)) . ")
");
while ($check = $db->fetch_array($checkprevious))
{
if (!empty($redirectids["$check[threadid]"]))
{
foreach($redirectids["$check[threadid]"] AS $threadid)
{
$old_redirect =& datamanager_init('Thread', $vbulletin, ERRTYPE_SILENT, 'threadpost');
$old_redirect->set_existing($threadarray["$threadid"]);
$old_redirect->delete(false, true, NULL, false);
unset($old_redirect);
# Remove redirect threadids from $threadarray so no log entry is entered below or new redirect is added
unset($threadarray["$threadid"]);
}
}
}
}
if (!empty($threadarray))
{
// Move threads
// If mod can not manage threads in destination forum then unstick all moved threads
$db->query_write("
UPDATE " . TABLE_PREFIX . "thread
SET forumid = $destforuminfo[forumid]
" . (!can_moderate($destforuminfo['forumid'], 'canmanagethreads') ? ", sticky = 0" : "") . "
WHERE threadid IN(" . implode(',', array_keys($threadarray)) . ")
");
require_once(DIR . '/includes/functions_prefix.php');
remove_invalid_prefixes(array_keys($threadarray), $destforuminfo['forumid']);
// update canview status of thread subscriptions
update_subscriptions(array('threadids' => array_keys($threadarray)));
// kill the post cache for these threads
delete_post_cache_threads(array_keys($threadarray));
$movelog = array();
// Insert Redirects FUN FUN FUN
if ($method == 'movered')
{
$redirectsql = array();
if ($vbulletin->GPC['redirect'] == 'expires')
{
switch($vbulletin->GPC['frame'])
{
case 'h':
$expires = mktime(date('H') + $vbulletin->GPC['period'], date('i'), date('s'), date('m'), date('d'), date('y'));
break;
case 'd':
$expires = mktime(date('H'), date('i'), date('s'), date('m'), date('d') + $vbulletin->GPC['period'], date('y'));
break;
case 'w':
$expires = $vbulletin->GPC['period'] * 60 * 60 * 24 * 7 + TIMENOW;
break;
case 'y':
$expires = mktime(date('H'), date('i'), date('s'), date('m'), date('d'), date('y') + $vbulletin->GPC['period']);
break;
case 'm':
default:
$expires = mktime(date('H'), date('i'), date('s'), date('m') + $vbulletin->GPC['period'], date('d'), date('y'));
}
}
foreach($threadarray AS $threadid => $thread)
{
if ($thread['visible'] == 1)
{
$thread['open'] = 10;
$thread['pollid'] = $threadid;
unset($thread['threadid']);
$redir =& datamanager_init('Thread', $vbulletin, ERRTYPE_SILENT, 'threadpost');
foreach (array_keys($thread) AS $field)
{
// bypassing the verify_* calls; this data should be valid as is
if (isset($redir->validfields["$field"]))
{
$redir->setr($field, $thread["$field"], true, false);
}
}
$redirthreadid = $redir->save();
if ($vbulletin->GPC['redirect'] == 'expires')
{
$redirectsql[] = "$redirthreadid, $expires";
}
unset($redir);
}
else
{
// else this is a moderated or deleted thread so leave no redirect behind
// insert modlog entry of just "move", not "moved with redirect"
// unset threadarray[threadid] so thread_moved_with_redirect log entry is not entered below.
unset($threadarray["$threadid"]);
$movelog = array(
'userid' =>& $vbulletin->userinfo['userid'],
'forumid' =>& $thread['forumid'],
'threadid' => $threadid,
);
}
}
if (!empty($redirectsql))
{
$db->query_write("
INSERT INTO " . TABLE_PREFIX . "threadredirect
(threadid, expires)
VALUES
(" . implode("), (", $redirectsql) . ")
");
}
}
if (!empty($movelog))
{
log_moderator_action($movelog, 'thread_moved_to_x', $destforuminfo['title']);
}
if (!empty($threadarray))
{
foreach ($threadarray AS $threadid => $thread)
{
$modlog[] = array(
'userid' =>& $vbulletin->userinfo['userid'],
'forumid' =>& $thread['forumid'],
'threadid' => $threadid,
);
}
log_moderator_action($modlog, ($method == 'move') ? 'thread_moved_to_x' : 'thread_moved_with_redirect_to_a', $destforuminfo['title']);
if (!empty($countingthreads))
{
$posts = $db->query_read_slave("
SELECT userid, threadid
FROM " . TABLE_PREFIX . "post
WHERE threadid IN(" . implode(',', $countingthreads) . ")
AND visible = 1
AND userid > 0
");
$userbyuserid = array();
while ($post = $db->fetch_array($posts))
{
$foruminfo = fetch_foruminfo($threadarray["$post[threadid]"]['forumid']);
if ($foruminfo['countposts'] AND !$destforuminfo['countposts'])
{ // Take away a post
if (!isset($userbyuserid["$post[userid]"]))
{
$userbyuserid["$post[userid]"] = -1;
}
else
{
$userbyuserid["$post[userid]"]--;
}
}
else if (!$foruminfo['countposts'] AND $destforuminfo['countposts'])
{ // Add a post
if (!isset($userbyuserid["$post[userid]"]))
{
$userbyuserid["$post[userid]"] = 1;
}
else
{
$userbyuserid["$post[userid]"]++;
}
}
}
if (!empty($userbyuserid))
{
$userbypostcount = array();
$alluserids = '';
foreach ($userbyuserid AS $postuserid => $postcount)
{
$alluserids .= ",$postuserid";
$userbypostcount["$postcount"] .= ",$postuserid";
}
foreach ($userbypostcount AS $postcount => $userids)
{
$casesql .= " WHEN userid IN (0$userids) THEN $postcount";
}
$db->query_write("
UPDATE " . TABLE_PREFIX . "user
SET posts = CAST(posts AS SIGNED) +
CASE
$casesql
ELSE 0
END
WHERE userid IN (0$alluserids)
");
}
}
}
}
foreach(array_keys($forumlist) AS $forumid)
{
build_forum_counters($forumid);
}
build_forum_counters($destforuminfo['forumid']);
// empty cookie
setcookie('vbulletin_inlinethread', '', TIMENOW - 3600, '/');
($hook = vBulletinHook::fetch_hook('inlinemod_domovethread')) ? eval($hook) : false;
eval(print_standard_redirect('redirect_inline_moved', true, $forceredirect));
}