Example #1
0
function thread_merge($tida, $tidb, $merge_type, &$error_str)
{
    if (!($db = db::get())) {
        return false;
    }
    if (!is_numeric($tida)) {
        return false;
    }
    if (!is_numeric($tidb)) {
        return false;
    }
    if (!in_array($merge_type, array(THREAD_MERGE_BY_CREATED, THREAD_MERGE_START, THREAD_MERGE_END))) {
        return false;
    }
    if (!($table_prefix = get_table_prefix())) {
        return thread_merge_error(THREAD_MERGE_FORUM_ERROR, $error_str);
    }
    if (!($forum_fid = get_forum_fid())) {
        return thread_merge_error(THREAD_MERGE_FORUM_ERROR, $error_str);
    }
    // Get Thread A data
    if (!($threada = thread_get($tida))) {
        return thread_merge_error(THREAD_MERGE_THREAD_ERROR, $error_str);
    }
    // Get Thread B data
    if (!($threadb = thread_get($tidb))) {
        return thread_merge_error(THREAD_MERGE_THREAD_ERROR, $error_str);
    }
    // Check the threads aren't polls.
    if ($threada['POLL_FLAG'] == 'Y' || $threadb['POLL_FLAG'] == 'Y') {
        return thread_merge_error(THREAD_MERGE_POLL_ERROR, $error_str);
    }
    // Check thread A permissions
    if (!session::check_perm(USER_PERM_FOLDER_MODERATE, $threada['FID'])) {
        return thread_merge_error(THREAD_MERGE_PERMS_ERROR, $error_str);
    }
    // Check thread B permissions
    if (!session::check_perm(USER_PERM_FOLDER_MODERATE, $threada['FID'])) {
        return thread_merge_error(THREAD_MERGE_PERMS_ERROR, $error_str);
    }
    // Close thread A
    thread_set_closed($tida, true);
    // Close thread B
    thread_set_closed($tidb, true);
    // Create new thread. Mark it as deleted so user's cannot see it.
    if (!($new_tid = post_create_thread($threada['FID'], $threada['BY_UID'], $threada['TITLE'], 'N', 'N', true, true))) {
        // Unlock the threads if they weren't originally locked.
        thread_set_closed($tida, $threada['CLOSED'] > 0);
        thread_set_closed($tidb, $threadb['CLOSED'] > 0);
        // Return error message.
        return thread_merge_error(THREAD_MERGE_CREATE_ERROR, $error_str);
    }
    // Construct query to correctly sort the posts in the new thread.
    switch ($merge_type) {
        case THREAD_MERGE_BY_CREATED:
            $sql = "INSERT INTO `{$table_prefix}POST` (TID, REPLY_TO_PID, ";
            $sql .= "FROM_UID, TO_UID, VIEWED, CREATED, STATUS, APPROVED, APPROVED_BY, ";
            $sql .= "EDITED, EDITED_BY, IPADDRESS, MOVED_TID, MOVED_PID) ";
            $sql .= "SELECT '{$new_tid}', REPLY_TO_PID, FROM_UID, TO_UID, NULL, NOW(), ";
            $sql .= "STATUS, APPROVED, APPROVED_BY, EDITED, EDITED_BY, IPADDRESS, TID, ";
            $sql .= "PID FROM `{$table_prefix}POST` WHERE TID IN ('{$tida}', '{$tidb}') ";
            $sql .= "ORDER BY CREATED";
            break;
        case THREAD_MERGE_START:
            $sql = "INSERT INTO `{$table_prefix}POST` (TID, REPLY_TO_PID, ";
            $sql .= "FROM_UID, TO_UID, VIEWED, CREATED, STATUS, APPROVED, APPROVED_BY, ";
            $sql .= "EDITED, EDITED_BY, IPADDRESS, MOVED_TID, MOVED_PID) ";
            $sql .= "SELECT '{$new_tid}', REPLY_TO_PID, FROM_UID, TO_UID, NULL, NOW(), ";
            $sql .= "STATUS, APPROVED, APPROVED_BY, EDITED, EDITED_BY, IPADDRESS, TID, ";
            $sql .= "PID FROM `{$table_prefix}POST` WHERE TID IN ('{$tida}', '{$tidb}') ";
            $sql .= "ORDER BY TID = '{$tidb}', CREATED";
            break;
        case THREAD_MERGE_END:
            $sql = "INSERT INTO `{$table_prefix}POST` (TID, REPLY_TO_PID, ";
            $sql .= "FROM_UID, TO_UID, VIEWED, CREATED, STATUS, APPROVED, APPROVED_BY, ";
            $sql .= "EDITED, EDITED_BY, IPADDRESS, MOVED_TID, MOVED_PID) ";
            $sql .= "SELECT '{$new_tid}', REPLY_TO_PID, FROM_UID, TO_UID, NULL, NOW(), ";
            $sql .= "STATUS, APPROVED, APPROVED_BY, EDITED, EDITED_BY, IPADDRESS, TID, ";
            $sql .= "PID FROM `{$table_prefix}POST` WHERE TID IN ('{$tida}', '{$tidb}') ";
            $sql .= "ORDER BY TID = '{$tida}', CREATED";
            break;
    }
    // Execute the query to copy the posts.
    if (!$db->query($sql)) {
        // Unlock the threads if they weren't originally locked.
        thread_set_closed($tida, $threada['CLOSED'] > 0);
        thread_set_closed($tidb, $threadb['CLOSED'] > 0);
        // Return error message.
        return thread_merge_error(THREAD_MERGE_QUERY_ERROR, $error_str);
    }
    // Copy the post contents to the new thread
    $sql = "INSERT INTO `{$table_prefix}POST_CONTENT` (TID, PID, CONTENT) ";
    $sql .= "SELECT POST.TID, POST.PID, POST_CONTENT.CONTENT FROM `{$table_prefix}POST` POST ";
    $sql .= "LEFT JOIN `{$table_prefix}POST_CONTENT` POST_CONTENT ";
    $sql .= "ON (POST_CONTENT.TID = POST.MOVED_TID AND POST_CONTENT.PID = MOVED_PID) ";
    $sql .= "WHERE POST.TID = '{$new_tid}'";
    if (!$db->query($sql)) {
        // Unlock the threads if they weren't originally locked.
        thread_set_closed($tida, $threada['CLOSED'] > 0);
        thread_set_closed($tidb, $threadb['CLOSED'] > 0);
        // Return error message.
        return thread_merge_error(THREAD_MERGE_QUERY_ERROR, $error_str);
    }
    // Insert the new Sphinx Search IDs.
    $sql = "INSERT INTO `{$table_prefix}POST_SEARCH_ID` (TID, PID) ";
    $sql .= "SELECT {$new_tid}, POST.PID FROM `{$table_prefix}POST` POST ";
    $sql .= "WHERE POST.TID = '{$new_tid}'";
    if (!$db->query($sql)) {
        // Unlock the threads if they weren't originally locked.
        thread_set_closed($tida, $threada['CLOSED'] > 0);
        thread_set_closed($tidb, $threadb['CLOSED'] > 0);
        // Return error message.
        return thread_merge_error(THREAD_MERGE_QUERY_ERROR, $error_str);
    }
    // Update the REPLY_TO_PIDs in the new thread
    $sql = "INSERT INTO `{$table_prefix}POST` (TID, PID, REPLY_TO_PID) ";
    $sql .= "SELECT TARGET_POST.TID, TARGET_POST.PID, SOURCE_POST.PID ";
    $sql .= "FROM `{$table_prefix}POST` TARGET_POST ";
    $sql .= "INNER JOIN `{$table_prefix}POST` SOURCE_POST ";
    $sql .= "ON (SOURCE_POST.MOVED_TID = TARGET_POST.MOVED_TID ";
    $sql .= "AND TARGET_POST.REPLY_TO_PID = SOURCE_POST.MOVED_PID) ";
    $sql .= "WHERE TARGET_POST.TID = '{$new_tid}' ";
    $sql .= "ON DUPLICATE KEY UPDATE REPLY_TO_PID = VALUES(REPLY_TO_PID) ";
    if (!$db->query($sql)) {
        // Unlock the threads if they weren't originally locked.
        thread_set_closed($tida, $threada['CLOSED'] > 0);
        thread_set_closed($tidb, $threadb['CLOSED'] > 0);
        // Return error message.
        return thread_merge_error(THREAD_MERGE_QUERY_ERROR, $error_str);
    }
    // Link the attachments to the new thread.
    $sql = "INSERT INTO POST_ATTACHMENT_IDS (FID, TID, PID, AID) ";
    $sql .= "SELECT {$forum_fid}, TARGET_POST.TID, TARGET_POST.PID, ";
    $sql .= "SOURCE_POST_ATTACHMENT_IDS.AID ";
    $sql .= "FROM `{$table_prefix}POST` TARGET_POST ";
    $sql .= "INNER JOIN `{$table_prefix}POST` SOURCE_POST ";
    $sql .= "ON (SOURCE_POST.MOVED_TID = TARGET_POST.MOVED_TID ";
    $sql .= "AND TARGET_POST.REPLY_TO_PID = SOURCE_POST.MOVED_PID) ";
    $sql .= "INNER JOIN POST_ATTACHMENT_IDS SOURCE_POST_ATTACHMENT_IDS ";
    $sql .= "ON (SOURCE_POST_ATTACHMENT_IDS.TID = SOURCE_POST.TID ";
    $sql .= "AND SOURCE_POST_ATTACHMENT_IDS.PID = SOURCE_POST.PID) ";
    $sql .= "WHERE TARGET_POST.TID = '{$new_tid}'";
    if (!$db->query($sql)) {
        // Unlock the threads if they weren't originally locked.
        thread_set_closed($tida, $threada['CLOSED'] > 0);
        thread_set_closed($tidb, $threadb['CLOSED'] > 0);
        // Return error message.
        return thread_merge_error(THREAD_MERGE_QUERY_ERROR, $error_str);
    }
    // Now we unset the MOVED_TID and MOVED_PIDs for the new thread
    // so the posts appear in the new thread.
    $sql = "UPDATE `{$table_prefix}POST` SET MOVED_TID = NULL, ";
    $sql .= "MOVED_PID = NULL WHERE TID = '{$new_tid}'";
    if (!$db->query($sql)) {
        // Unlock the threads if they weren't originally locked.
        thread_set_closed($tida, $threada['CLOSED'] > 0);
        thread_set_closed($tidb, $threadb['CLOSED'] > 0);
        // Return error message.
        return thread_merge_error(THREAD_MERGE_QUERY_ERROR, $error_str);
    }
    // Update the new thread length
    thread_set_length($new_tid, $threada['LENGTH'] + $threadb['LENGTH']);
    // Set the original threads as moved
    thread_set_moved($tida, $new_tid);
    thread_set_moved($tidb, $new_tid);
    // Update the new thread so it's closed if either
    // of it's source threads were originally closed.
    thread_set_closed($new_tid, $threada['CLOSED'] > 0 | $threadb['CLOSED'] > 0);
    // Undelete the thread.
    thread_undelete($new_tid);
    // Return the admin log data.
    return array($tida, $threada['TITLE'], $tidb, $threadb['TITLE'], $new_tid, $threada['TITLE']);
}
             if (thread_delete($tid, $delete_thread)) {
                 post_add_edit_text($tid, 1);
                 admin_add_log_entry(DELETE_THREAD, array($tid, $thread_data['TITLE']));
                 html_draw_top(sprintf('title=%s', gettext("Delete Thread")), 'class=window_title');
                 html_display_msg(gettext("Delete Thread"), gettext("Thread was successfully deleted"), 'discussion.php', 'get', array('continue' => gettext("Continue")), false, html_get_frame_name('main'), 'center');
                 html_draw_bottom();
                 exit;
             } else {
                 $error_msg_array[] = gettext("Failed to delete thread.");
                 $valid = false;
             }
         }
     }
     if (isset($_POST['undelete_thread']) && $_POST['undelete_thread'] == "Y") {
         if (isset($_POST['undelete_thread_confirm']) && $_POST['undelete_thread_confirm'] == "Y") {
             if (thread_undelete($tid)) {
                 post_add_edit_text($tid, 1);
                 admin_add_log_entry(UNDELETE_THREAD, array($tid, $thread_data['TITLE']));
                 html_draw_top(sprintf('title=%s', gettext("Undelete Thread")), 'class=window_title');
                 html_display_msg(gettext("Undelete Thread"), gettext("Thread was successfully undeleted"), 'thread_options.php', 'get', array('back' => gettext("Back")), array('msg' => $msg), '_self', 'center');
                 html_draw_bottom();
                 exit;
             } else {
                 $error_msg_array[] = gettext("Failed to un-delete thread");
                 $valid = false;
             }
         }
     }
 }
 if ($valid) {
     header_redirect("thread_options.php?webtag={$webtag}&msg={$msg}&updated=true");