/** * Completely rebuild the counters for a particular forum (useful if they become out of sync) * * @param int The forum ID */ function rebuild_forum_counters($fid) { global $db; // Fetch the number of threads and replies in this forum (Approved only) $query = $db->simple_select('threads', 'COUNT(tid) AS threads, SUM(replies) AS replies, SUM(unapprovedposts) AS unapprovedposts, SUM(deletedposts) AS deletedposts', "fid='{$fid}' AND visible='1'"); $count = $db->fetch_array($query); $count['posts'] = $count['threads'] + $count['replies']; // Fetch the number of threads and replies in this forum (Unapproved only) $query = $db->simple_select('threads', 'COUNT(tid) AS threads, SUM(replies)+SUM(unapprovedposts)+SUM(deletedposts) AS impliedunapproved', "fid='{$fid}' AND visible='0'"); $count2 = $db->fetch_array($query); $count['unapprovedthreads'] = $count2['threads']; $count['unapprovedposts'] += $count2['impliedunapproved'] + $count2['threads']; // Fetch the number of threads and replies in this forum (Soft deleted only) $query = $db->simple_select('threads', 'COUNT(tid) AS threads, SUM(replies)+SUM(unapprovedposts)+SUM(deletedposts) AS implieddeleted', "fid='{$fid}' AND visible='-1'"); $count3 = $db->fetch_array($query); $count['deletedthreads'] = $count3['threads']; $count['deletedposts'] += $count3['implieddeleted'] + $count3['threads']; update_forum_counters($fid, $count); update_forum_lastpost($fid); }
function upgrade5_forumlastposts() { global $db, $output; $output->print_header("Przebudowywanie ostatnich postów na forum"); echo "<p>Trwa przebudowywanie informacji o ostatnich postach na forum...</p>"; $query = $db->simple_select("forums", "fid"); while ($forum = $db->fetch_array($query)) { update_forum_lastpost($forum['fid']); } echo "<p>Zakończono"; echo "<p>Naciśnij przycisk Dalej, aby kontynuować proces aktualizacji.</p>"; $output->print_footer("5_indexes"); }
/** * Updates a post that is already in the database. * */ function update_post() { global $db, $mybb, $plugins; // Yes, validating is required. if ($this->get_validated() != true) { die("The post needs to be validated before inserting it into the DB."); } if (count($this->get_errors()) > 0) { die("The post is not valid."); } $post =& $this->data; $post['pid'] = (int) $post['pid']; $existing_post = get_post($post['pid']); $post['tid'] = $existing_post['tid']; $post['fid'] = $existing_post['fid']; $forum = get_forum($post['fid']); $forumpermissions = forum_permissions($post['fid'], $post['uid']); // Check if this is the first post in a thread. $options = array("order_by" => "dateline", "order_dir" => "asc", "limit_start" => 0, "limit" => 1); $query = $db->simple_select("posts", "pid", "tid='" . (int) $post['tid'] . "'", $options); $first_post_check = $db->fetch_array($query); if ($first_post_check['pid'] == $post['pid']) { $first_post = true; } else { $first_post = false; } // Decide on the visibility of this post. $ismod = is_moderator($post['fid'], "", $post['uid']); // Keep visibility for unapproved and deleted posts if ($existing_post['visible'] == 0) { $visible = 0; } elseif ($existing_post['visible'] == -1) { $visible = -1; } elseif ($forumpermissions['mod_edit_posts'] == 1 && !$ismod) { $visible = 0; require_once MYBB_ROOT . "inc/class_moderation.php"; $moderation = new Moderation(); $moderation->unapprove_posts(array($post['pid'])); } else { $visible = 1; } // Update the thread details that might have been changed first. if ($first_post) { $this->tid = $post['tid']; if (isset($post['prefix'])) { $this->thread_update_data['prefix'] = (int) $post['prefix']; } if (isset($post['subject'])) { $this->thread_update_data['subject'] = $db->escape_string($post['subject']); } if (isset($post['icon'])) { $this->thread_update_data['icon'] = (int) $post['icon']; } if (count($this->thread_update_data) > 0) { $plugins->run_hooks("datahandler_post_update_thread", $this); $db->update_query("threads", $this->thread_update_data, "tid='" . (int) $post['tid'] . "'"); } } // Prepare array for post updating. $this->pid = $post['pid']; if (isset($post['subject'])) { $this->post_update_data['subject'] = $db->escape_string($post['subject']); } if (isset($post['message'])) { $this->post_update_data['message'] = $db->escape_string($post['message']); } if (isset($post['editreason']) && trim($post['editreason']) != '') { $this->post_update_data['editreason'] = $db->escape_string(trim($post['editreason'])); } if (isset($post['icon'])) { $this->post_update_data['icon'] = (int) $post['icon']; } if (isset($post['options'])) { if (isset($post['options']['disablesmilies'])) { $this->post_update_data['smilieoff'] = $db->escape_string($post['options']['disablesmilies']); } if (isset($post['options']['signature'])) { $this->post_update_data['includesig'] = $db->escape_string($post['options']['signature']); } } // If we need to show the edited by, let's do so. if ($mybb->settings['showeditedby'] == 1 && !is_moderator($post['fid'], "caneditposts", $post['edit_uid']) || $mybb->settings['showeditedbyadmin'] == 1 && is_moderator($post['fid'], "caneditposts", $post['edit_uid'])) { $this->post_update_data['edituid'] = (int) $post['edit_uid']; $this->post_update_data['edittime'] = TIME_NOW; } $plugins->run_hooks("datahandler_post_update", $this); $db->update_query("posts", $this->post_update_data, "pid='" . (int) $post['pid'] . "'"); // Automatic subscription to the thread if ($post['options']['subscriptionmethod'] != "" && $post['uid'] > 0) { switch ($post['options']['subscriptionmethod']) { case "pm": $notification = 2; break; case "email": $notification = 1; break; default: $notification = 0; } require_once MYBB_ROOT . "inc/functions_user.php"; add_subscribed_thread($post['tid'], $notification, $post['uid']); } else { $db->delete_query("threadsubscriptions", "uid='" . (int) $post['uid'] . "' AND tid='" . (int) $post['tid'] . "'"); } update_forum_lastpost($post['fid']); update_last_post($post['tid']); // Return the thread's first post id and whether or not it is visible. $this->return_values = array('visible' => $visible, 'first_post' => $first_post); $plugins->run_hooks("datahandler_post_update_end", $this); return $this->return_values; }
function upgrade5_forumlastposts() { global $db, $output; $output->print_header("Rebuilding Forum Last Posts"); echo "<p>Rebuilding last post information for forums..</p>"; $query = $db->simple_select("forums", "fid"); while ($forum = $db->fetch_array($query)) { update_forum_lastpost($forum['fid']); } echo "<p>Done"; echo "<p>Click next to continue with the upgrade process.</p>"; $output->print_footer("5_indexes"); }
/** * Updates the forum counters with a specific value (or addition/subtraction of the previous value) * * @param int The forum ID * @param array Array of items being updated (threads, posts, unapprovedthreads, unapprovedposts) and their value (ex, 1, +1, -1) */ function update_forum_counters($fid, $changes = array()) { global $db, $cache; $update_query = array(); $counters = array('threads', 'unapprovedthreads', 'posts', 'unapprovedposts'); // Fetch above counters for this forum $query = $db->simple_select("forums", implode(",", $counters), "fid='{$fid}'"); $forum = $db->fetch_array($query); foreach ($counters as $counter) { if (array_key_exists($counter, $changes)) { // Adding or subtracting from previous value? if (substr($changes[$counter], 0, 1) == "+" || substr($changes[$counter], 0, 1) == "-") { $update_query[$counter] = $forum[$counter] + $changes[$counter]; } else { $update_query[$counter] = $changes[$counter]; } // Less than 0? That's bad if ($update_query[$counter] < 0) { $update_query[$counter] = 0; } } } // Only update if we're actually doing something if (count($update_query) > 0) { $db->update_query("forums", $update_query, "fid='" . intval($fid) . "'"); } // Guess we should update the statistics too? $new_stats = array(); if (array_key_exists('threads', $update_query)) { $threads_diff = $update_query['threads'] - $forum['threads']; if ($threads_diff > -1) { $new_stats['numthreads'] = "+{$threads_diff}"; } else { $new_stats['numthreads'] = "{$threads_diff}"; } } if (array_key_exists('unapprovedthreads', $update_query)) { $unapprovedthreads_diff = $update_query['unapprovedthreads'] - $forum['unapprovedthreads']; if ($unapprovedthreads_diff > -1) { $new_stats['numunapprovedthreads'] = "+{$unapprovedthreads_diff}"; } else { $new_stats['numunapprovedthreads'] = "{$unapprovedthreads_diff}"; } } if (array_key_exists('posts', $update_query)) { $posts_diff = $update_query['posts'] - $forum['posts']; if ($posts_diff > -1) { $new_stats['numposts'] = "+{$posts_diff}"; } else { $new_stats['numposts'] = "{$posts_diff}"; } } if (array_key_exists('unapprovedposts', $update_query)) { $unapprovedposts_diff = $update_query['unapprovedposts'] - $forum['unapprovedposts']; if ($unapprovedposts_diff > -1) { $new_stats['numunapprovedposts'] = "+{$unapprovedposts_diff}"; } else { $new_stats['numunapprovedposts'] = "{$unapprovedposts_diff}"; } } if (!empty($new_stats)) { update_stats($new_stats); } // Update last post info update_forum_lastpost($fid); $cache->update_forums(); }
/** Create new forums for an IC group based on the given attributes */ public function create_groupforums($gid, $settings) { $idarray = array(); // Configure Prefix & Region $prefixquery = $this->db->simple_select('threadprefixes', '*', 'pid = ' . $settings['prefix']); $prefix = $this->db->fetch_array($prefixquery); $forumarray = explode(',', $prefix['forums']); $forums = ''; if (!empty($prefix['forums'])) { foreach ($forumarray as $forum) { if ($forum != $settings['region']) { $forums .= $forum . ','; } } } // Create the IC Forum for the group $forum = Creation::IC_FORUM; $forum['name'] = $settings['title']; $forum['description'] = 'This pack claims the territory of <strong>' . $prefix['prefix'] . '</strong>. If you aren\'\'t a member of this pack, posting in this forum means you\'\'re trespassing!'; $forum['pid'] = $settings['region']; $this->db->insert_query('forums', $forum); $fid = $this->db->insert_id(); $idarray['fid'] = $fid; $this->db->update_query('forums', array('parentlist' => make_parent_list($fid)), 'fid = ' . $fid); // Update the prefix with the new forum $this->db->update_query('threadprefixes', array('forums' => $forums . $fid), 'pid = ' . $settings['prefix']); //Move existing prefixed threads to new board $threadquery = $this->db->simple_select('threads', '*', 'fid = ' . $forum['pid'] . ' AND prefix = ' . $settings['prefix']); while ($thread = $this->db->fetch_array($threadquery)) { $threadstring .= $thread['tid']; } if (!empty($threadstring)) { $this->db->update_query('threads', array('fid' => $fid), 'tid IN (' . $threadstring . ')'); $this->db->update_query('posts', array('fid' => $fid), 'tid IN (' . $threadstring . ')'); update_forum_lastpost($fid); } $moid = 0; // Create the Members only subforum $moforum = Creation::OOC_FORUM; $moforum['name'] = $settings['title'] . ' Members Only'; $moforum['description'] = ''; $moforum['pid'] = $fid; $this->db->insert_query('forums', $moforum); $mofid = $this->db->insert_id(); $idarray['mofid'] = $mofid; $this->db->update_query('forums', array('parentlist' => make_parent_list($fid) . ',' . $mofid), 'fid = ' . $mofid); // Set permissions for other groups to noread // Get array of all current groups $othergroups = array(); $groupquery = $this->db->simple_select('usergroups g left join ' . TABLE_PREFIX . 'icgroups i on g.gid = i.gid', '*, g.gid', 'g.gid NOT IN (' . $gid . ',' . Groups::ADMIN . ')'); while ($og = $this->db->fetch_array($groupquery)) { $othergroups[] = $og; } $mopermissions = Creation::FORUM_PERM_NOREAD; if (!empty($othergroups)) { foreach ($othergroups as $othergroup) { if (!empty($mofid)) { $mopermissions['fid'] = $mofid; $mopermissions['gid'] = $othergroup['gid']; $this->db->insert_query('forumpermissions', $mopermissions); } } } // Set permissions to read $momemberpermissions = Creation::FORUM_PERM_READWRITE; $momemberpermissions['fid'] = $mofid; $momemberpermissions['gid'] = $gid; $this->db->insert_query('forumpermissions', $momemberpermissions); $momemberpermissions['gid'] = Groups::ADMIN; $this->db->insert_query('forumpermissions', $momemberpermissions); $this->cache->update_forums(); $this->cache->update_forumpermissions(); $this->cache->update_threadprefixes(); return $idarray; }
/** * Soft delete one or more threads * * @param array|int Thread ID(s) * @return boolean */ function soft_delete_threads($tids) { global $db, $cache, $plugins; if (!is_array($tids)) { $tids = array($tids); } if (empty($tids)) { return false; } // Make sure we only have valid values $tids = array_map('intval', $tids); $tid_list = implode(',', $tids); $tid_moved_list = ""; $comma = ""; foreach ($tids as $tid) { $tid_moved_list .= "{$comma}'moved|{$tid}'"; $comma = ","; } $forum_counters = $user_counters = $posts_to_delete = array(); foreach ($tids as $tid) { $thread = get_thread($tid); $forum = get_forum($thread['fid']); if ($thread['visible'] == 1 || $thread['visible'] == 0) { if (!isset($forum_counters[$forum['fid']])) { $forum_counters[$forum['fid']] = array('num_posts' => 0, 'num_threads' => 0, 'num_deleted_threads' => 0, 'num_deleted_posts' => 0, 'unapproved_threads' => 0, 'unapproved_posts' => 0); } if (!isset($user_counters[$thread['uid']])) { $user_counters[$thread['uid']] = array('num_posts' => 0, 'num_threads' => 0); } ++$forum_counters[$forum['fid']]['num_deleted_threads']; $forum_counters[$forum['fid']]['num_deleted_posts'] += $thread['replies'] + $thread['unapprovedposts'] + 1; if ($thread['visible'] == 1) { ++$forum_counters[$forum['fid']]['num_threads']; $forum_counters[$forum['fid']]['num_posts'] += $thread['replies'] + 1; // Add implied invisible to count $forum_counters[$forum['fid']]['unapproved_posts'] += $thread['unapprovedposts']; } else { ++$forum_counters[$forum['fid']]['unapproved_threads']; $forum_counters[$forum['fid']]['unapproved_posts'] += $thread['replies'] + $thread['deletedposts'] + $thread['unapprovedposts'] + 1; // Add implied invisible to count $forum_counters[$forum['fid']]['num_deleted_posts'] += $thread['deletedposts']; } // On unapproving thread update user post counts if ($thread['visible'] == 1 && $forum['usepostcounts'] != 0) { $query = $db->simple_select("posts", "COUNT(pid) AS posts, uid", "tid='{$tid}' AND (visible='1' OR pid='{$thread['firstpost']}') AND uid > 0 GROUP BY uid"); while ($counter = $db->fetch_array($query)) { if (!isset($user_counters[$counter['uid']]['num_posts'])) { $user_counters[$counter['uid']]['num_posts'] = 0; } $user_counters[$counter['uid']]['num_posts'] += $counter['posts']; } } if ($thread['visible'] == 1 && $forum['usethreadcounts'] != 0 && substr($thread['closed'], 0, 6) != 'moved|') { ++$user_counters[$thread['uid']]['num_threads']; } } $posts_to_delete[] = $thread['firstpost']; } $update = array("visible" => -1); $db->update_query("threads", $update, "tid IN ({$tid_list})"); // Soft delete redirects, too $redirect_tids = array(); $query = $db->simple_select('threads', 'tid', "closed IN ({$tid_moved_list})"); mark_reports($tids, "threads"); while ($redirect_tid = $db->fetch_field($query, 'tid')) { $redirect_tids[] = $redirect_tid; } if (!empty($redirect_tids)) { $this->soft_delete_threads($redirect_tids); } if (!empty($posts_to_delete)) { $db->update_query("posts", $update, "pid IN (" . implode(',', $posts_to_delete) . ")"); } $plugins->run_hooks("class_moderation_soft_delete_threads", $tids); if (is_array($forum_counters)) { foreach ($forum_counters as $fid => $counters) { // Update stats $update_array = array("threads" => "-{$counters['num_threads']}", "unapprovedthreads" => "-{$counters['unapproved_threads']}", "posts" => "-{$counters['num_posts']}", "unapprovedposts" => "-{$counters['unapproved_posts']}", "deletedposts" => "+{$counters['num_deleted_posts']}", "deletedthreads" => "+{$counters['num_deleted_threads']}"); update_forum_counters($fid, $update_array); update_forum_lastpost($fid); } } if (!empty($user_counters)) { foreach ($user_counters as $uid => $counters) { $update_array = array("postnum" => "-{$counters['num_posts']}", "threadnum" => "-{$counters['num_threads']}"); update_user_counters($uid, $update_array); } } return true; }
/** Remove the group's forums from the game */ public function remove_forums() { if ($this->info['fid']) { $forumquery = $this->db->simple_select('forums', '*', 'fid = ' . $this->info['fid']); while ($forum = $this->db->fetch_array($forumquery)) { //Move prefix to parent board $prefixquery = $this->db->simple_select('threadprefixes', '*', 'CONCAT(\',\',forums,\',\') LIKE \'%,' . $this->info['fid'] . ',%\''); while ($prefix = $this->db->fetch_array($prefixquery)) { $forums = explode(',', $prefix['forums']); foreach ($forums as $f) { $forumstring .= $f !== $this->info['fid'] ? $f . ',' : $forum['pid'] . ','; } $this->db->update_query('threadprefixes', array('forums' => trim($forumstring, ',')), 'pid = ' . $prefix['pid']); } //Move threads to parent board $threadquery = $this->db->simple_select('threads', '*', 'fid = ' . $this->info['fid']); while ($thread = $this->db->fetch_array($threadquery)) { $threadstring .= $thread['tid']; } if (!empty($threadstring)) { $this->db->update_query('threads', array('fid' => $forum['pid']), 'tid IN (' . $threadstring . ')'); $this->db->update_query('posts', array('fid' => $forum['pid']), 'tid IN (' . $threadstring . ')'); update_forum_lastpost($forum['pid']); } } //Delete MO forum $this->db->query('DELETE FROM ' . TABLE_PREFIX . 'forums WHERE fid = ' . $this->info['mo_fid']); $threadquery = $this->db->simple_select('threads', '*', 'fid = ' . $this->info['mo_fid']); while ($thread = $this->db->fetch_array($threadquery)) { delete_thread($thread['tid']); } //Delete all permissions $this->db->query('DELETE FROM ' . TABLE_PREFIX . 'forumpermissions WHERE fid = ' . $this->info['mo_fid']); $this->db->query('DELETE FROM ' . TABLE_PREFIX . 'moderators WHERE fid = ' . $this->info['mo_fid']); $this->db->query('DELETE FROM ' . TABLE_PREFIX . 'moderators WHERE fid = ' . $this->info['fid']); //Delete forum $this->db->query('DELETE FROM ' . TABLE_PREFIX . 'forums WHERE fid = ' . $this->info['fid']); } $this->cache->update_forums(); $this->cache->update_moderators(); $this->cache->update_forumpermissions(); $this->cache->update_threadprefixes(); }
die; } require_once 'inc/include_mybb.php'; $subject = $_POST['subject']; $message = $_POST['message']; $tid = $_POST['tid']; $uid = $_POST['uid']; $ip = $_POST['ip']; $thread = $MyBBI->getThread($tid); $fid = $thread['fid']; $user = $MyBBI->getUser($uid); $username = $user['username']; $data = array('tid' => $tid, 'fid' => $fid, 'uid' => $uid, 'username' => $username, 'ipaddress' => $ip, 'subject' => $subject, 'message' => $message, 'savedraft' => false, 'options' => array('signature' => true, 'disablesmilies' => false, 'subscriptionmethod' => false)); $create = $MyBBI->createPost($data, false); $thread = $MyBBI->getThread($tid); update_forum_lastpost($fid); exit(serialize($create)); break; /* case 'create_mybb_user': { require_once('inc/include_mybb.php'); $username=$_POST['username']; $password=$_POST['password']; $email=trim($_POST['email']); $usergroup = 2; $user = array( "username" => $username,
/** * Merge one thread into another * * @param int Thread that will be merged into destination * @param int Destination thread * @param string New thread subject * @return boolean true */ function merge_threads($mergetid, $tid, $subject) { global $db, $mybb, $mergethread, $thread, $plugins; $mergetid = intval($mergetid); $tid = intval($tid); if (!isset($mergethread['tid']) || $mergethread['tid'] != $mergetid) { $query = $db->simple_select("threads", "*", "tid='{$mergetid}'"); $mergethread = $db->fetch_array($query); } if (!isset($thread['tid']) || $thread['tid'] != $tid) { $query = $db->simple_select("threads", "*", "tid='{$tid}'"); $thread = $db->fetch_array($query); } $pollsql = ''; if ($mergethread['poll']) { $pollsql['poll'] = $mergethread['poll']; $sqlarray = array("tid" => $tid); $db->update_query("polls", $sqlarray, "tid='" . intval($mergethread['tid']) . "'"); } else { $query = $db->simple_select("threads", "*", "poll='{$mergethread['poll']}' AND tid != '{$mergetid}'"); $pollcheck = $db->fetch_array($query); if (!$pollcheck['poll']) { $db->delete_query("polls", "pid='{$mergethread['poll']}'"); $db->delete_query("pollvotes", "pid='{$mergethread['poll']}'"); } } $subject = $db->escape_string($subject); $sqlarray = array("tid" => $tid, "fid" => $thread['fid'], "replyto" => 0); $db->update_query("posts", $sqlarray, "tid='{$mergetid}'"); $pollsql['subject'] = $subject; $db->update_query("threads", $pollsql, "tid='{$tid}'"); $sqlarray = array("closed" => "moved|{$tid}"); $db->update_query("threads", $sqlarray, "closed='moved|{$mergetid}'"); $sqlarray = array("tid" => $tid); // Update the thread ratings $new_numrating = $thread['numratings'] + $mergethread['numratings']; $new_threadrating = $thread['totalratings'] + $mergethread['totalratings']; $sqlarray = array("numratings" => $new_numrating, "totalratings" => $new_threadrating); $db->update_query("threads", $sqlarray, "tid = '{$tid}'"); // Check if we have a thread subscription already for our new thread $subscriptions = array($tid => array(), $mergetid => array()); $query = $db->simple_select("threadsubscriptions", "tid, uid", "tid='{$mergetid}' OR tid='{$tid}'"); while ($subscription = $db->fetch_array($query)) { $subscriptions[$subscription['tid']][] = $subscription['uid']; } // Update any subscriptions for the merged thread if (is_array($subscriptions[$mergetid])) { $update_users = array(); foreach ($subscriptions[$mergetid] as $user) { if (!in_array($user, $subscriptions[$tid])) { // User doesn't have a $tid subscription $update_users[] = $user; } } if (!empty($update_users)) { $update_array = array("tid" => $tid); $update_users = implode(",", $update_users); $db->update_query("threadsubscriptions", $update_array, "tid = '{$mergetid}' AND uid IN ({$update_users})"); } } // Remove source thread subscriptions $db->delete_query("threadsubscriptions", "tid = '{$mergetid}'"); update_first_post($tid); $arguments = array("mergetid" => $mergetid, "tid" => $tid, "subject" => $subject); $plugins->run_hooks("class_moderation_merge_threads", $arguments); $this->delete_thread($mergetid); // In some cases the thread we may be merging with may cause us to have a new firstpost if it is an older thread // Therefore resync the visible field to make sure they're the same if they're not $query = $db->simple_select("posts", "pid, visible", "tid='{$tid}'", array('order_by' => 'dateline', 'order_dir' => 'asc', 'limit' => 1)); $new_firstpost = $db->fetch_array($query); if ($thread['visible'] != $new_firstpost['visible']) { $db->update_query("posts", array('visible' => $thread['visible']), "pid='{$new_firstpost['pid']}'"); $mergethread['visible'] = $thread['visible']; } $updated_stats = array("replies" => '+' . ($mergethread['replies'] + 1), "attachmentcount" => "+{$mergethread['attachmentcount']}", "unapprovedposts" => "+{$mergethread['unapprovedposts']}"); update_thread_counters($tid, $updated_stats); // Thread is not in current forum if ($mergethread['fid'] != $thread['fid']) { // If new thread is unapproved, implied counter comes in to effect if ($thread['visible'] == 0 || $mergethread['visible'] == 0) { $updated_stats = array("unapprovedposts" => '+' . ($mergethread['replies'] + 1 + $mergethread['unapprovedposts'])); } else { $updated_stats = array("posts" => '+' . ($mergethread['replies'] + 1), "unapprovedposts" => "+{$mergethread['unapprovedposts']}"); } update_forum_counters($thread['fid'], $updated_stats); // If old thread is unapproved, implied counter comes in to effect if ($mergethread['visible'] == 0) { $updated_stats = array("unapprovedposts" => '-' . ($mergethread['replies'] + 1 + $mergethread['unapprovedposts'])); } else { $updated_stats = array("posts" => '-' . ($mergethread['replies'] + 1), "unapprovedposts" => "-{$mergethread['unapprovedposts']}"); } update_forum_counters($mergethread['fid'], $updated_stats); } else { update_forum_lastpost($thread['fid']); } return true; }
/** * Updates a post that is already in the database. * */ function update_post() { global $db, $mybb, $plugins; // Yes, validating is required. if ($this->get_validated() != true) { die("The post needs to be validated before inserting it into the DB."); } if (count($this->get_errors()) > 0) { die("The post is not valid."); } $post =& $this->data; $post['pid'] = intval($post['pid']); $existing_post = get_post($post['pid']); $post['tid'] = $existing_post['tid']; $post['fid'] = $existing_post['fid']; $forum = get_forum($post['fid']); // Decide on the visibility of this post. if (isset($post['visible']) && $post['visible'] != $existing_post['visible']) { if ($forum['mod_edit_posts'] == 1 && !is_moderator($post['fid'], "", $post['uid'])) { if ($existing_post['visible'] == 1) { update_thread_data($existing_post['tid']); update_thread_counters($existing_post['tid'], array('replies' => '-1', 'unapprovedposts' => '+1')); update_forum_counters($existing_post['fid'], array('unapprovedthreads' => '+1', 'unapprovedposts' => '+1')); // Subtract from the users post count // Update the post count if this forum allows post counts to be tracked if ($forum['usepostcounts'] != 0) { $db->write_query("UPDATE " . TABLE_PREFIX . "users SET postnum=postnum-1 WHERE uid='{$existing_post['uid']}'"); } } $visible = 0; } else { if ($existing_post['visible'] == 0) { update_thread_data($existing_post['tid']); update_thread_counters($existing_post['tid'], array('replies' => '+1', 'unapprovedposts' => '-1')); update_forum_counters($existing_post['fid'], array('unapprovedthreads' => '-1', 'unapprovedposts' => '-1')); // Update the post count if this forum allows post counts to be tracked if ($forum['usepostcounts'] != 0) { $db->write_query("UPDATE " . TABLE_PREFIX . "users SET postnum=postnum+1 WHERE uid='{$existing_post['uid']}'"); } } $visible = 1; } } else { $visible = 0; if ($forum['mod_edit_posts'] != 1 || is_moderator($post['fid'], "", $post['uid'])) { $visible = 1; } } // Check if this is the first post in a thread. $options = array("order_by" => "dateline", "order_dir" => "asc", "limit_start" => 0, "limit" => 1); $query = $db->simple_select("posts", "pid", "tid='" . intval($post['tid']) . "'", $options); $first_post_check = $db->fetch_array($query); if ($first_post_check['pid'] == $post['pid']) { $first_post = true; } else { $first_post = false; } if ($existing_post['visible'] == 0) { $visible = 0; } // Update the thread details that might have been changed first. if ($first_post) { $this->tid = $post['tid']; $this->thread_update_data['visible'] = $visible; if (isset($post['prefix'])) { $this->thread_update_data['prefix'] = intval($post['prefix']); } if (isset($post['subject'])) { $this->thread_update_data['subject'] = $db->escape_string($post['subject']); } if (isset($post['icon'])) { $this->thread_update_data['icon'] = intval($post['icon']); } if (count($this->thread_update_data) > 0) { $plugins->run_hooks("datahandler_post_update_thread", $this); $db->update_query("threads", $this->thread_update_data, "tid='" . intval($post['tid']) . "'"); } } // Prepare array for post updating. $this->pid = $post['pid']; if (isset($post['subject'])) { $this->post_update_data['subject'] = $db->escape_string($post['subject']); } if (isset($post['message'])) { $this->post_update_data['message'] = $db->escape_string($post['message']); } if (isset($post['icon'])) { $this->post_update_data['icon'] = intval($post['icon']); } if (isset($post['options'])) { if (isset($post['options']['disablesmilies'])) { $this->post_update_data['smilieoff'] = $db->escape_string($post['options']['disablesmilies']); } if (isset($post['options']['signature'])) { $this->post_update_data['includesig'] = $db->escape_string($post['options']['signature']); } } // If we need to show the edited by, let's do so. if ($mybb->settings['showeditedby'] == 1 && !is_moderator($post['fid'], "caneditposts", $post['edit_uid']) || $mybb->settings['showeditedbyadmin'] == 1 && is_moderator($post['fid'], "caneditposts", $post['edit_uid'])) { $this->post_update_data['edituid'] = intval($post['edit_uid']); $this->post_update_data['edittime'] = TIME_NOW; } $this->post_update_data['visible'] = $visible; $plugins->run_hooks("datahandler_post_update", $this); $db->update_query("posts", $this->post_update_data, "pid='" . intval($post['pid']) . "'"); // Automatic subscription to the thread if ($post['options']['subscriptionmethod'] != "" && $post['uid'] > 0) { switch ($post['options']['subscriptionmethod']) { case "instant": $notification = 1; break; default: $notification = 0; } require_once MYBB_ROOT . "inc/functions_user.php"; add_subscribed_thread($post['tid'], $notification, $post['uid']); } else { $db->delete_query("threadsubscriptions", "uid='" . intval($post['uid']) . "' AND tid='" . intval($post['tid']) . "'"); } update_forum_lastpost($post['fid']); return array('visible' => $visible, 'first_post' => $first_post); }
/** * Merge posts within thread * * @param array Post IDs to be merged * @param int Thread ID (Set to 0 if posts from multiple threads are * selected) * @return boolean true */ function merge_posts($pids, $tid = 0, $sep = "new_line") { global $db, $plugins; // Make sure we only have valid values $pids = array_map('intval', $pids); $tid = intval($tid); $pidin = implode(',', $pids); $attachment_count = 0; $first = 1; // Get the messages to be merged $query = $db->query("\n\t\t\tSELECT p.pid, p.uid, p.fid, p.tid, p.visible, p.message, f.usepostcounts, t.visible AS threadvisible, t.replies AS threadreplies, t.firstpost AS threadfirstpost, t.unapprovedposts AS threadunapprovedposts\n\t\t\tFROM " . TABLE_PREFIX . "posts p\n\t\t\tLEFT JOIN " . TABLE_PREFIX . "threads t ON (t.tid=p.tid)\n\t\t\tLEFT JOIN " . TABLE_PREFIX . "forums f ON (f.fid=p.fid)\n\t\t\tWHERE p.pid IN({$pidin})\n\t\t\tORDER BY p.dateline ASC\n\t\t"); $num_unapproved_posts = $num_approved_posts = 0; $message = ''; while ($post = $db->fetch_array($query)) { if ($first == 1) { // all posts will be merged into this one $masterpid = $post['pid']; $message = $post['message']; $fid = $post['fid']; $mastertid = $post['tid']; $first = 0; } else { // these are the selected posts if ($sep == "new_line") { $message .= "\n\n {$post['message']}"; } else { $message .= "[hr]{$post['message']}"; } if ($post['visible'] == 1 && $post['threadvisible'] == 1) { // Subtract 1 approved post from post's thread if (!$thread_counters[$post['tid']]['replies']) { $thread_counters[$post['tid']]['replies'] = $post['threadreplies']; } --$thread_counters[$post['tid']]['replies']; // Subtract 1 approved post from post's forum --$forum_counters[$post['fid']]['num_posts']; // Subtract 1 from user's post count if ($post['usepostcounts'] != 0) { // Update post count of the user of the merged posts $db->write_query("UPDATE " . TABLE_PREFIX . "users SET postnum=postnum-1 WHERE uid='{$post['uid']}'"); } } elseif ($post['visible'] == 0) { // Subtract 1 unapproved post from post's thread if (!$thread_counters[$post['tid']]['unapprovedposts']) { $thread_counters[$post['tid']]['unapprovedposts'] = $post['threadunapprovedposts']; } --$thread_counters[$post['tid']]['unapprovedposts']; // Subtract 1 unapproved post from post's forum --$forum_counters[$post['fid']]['unapprovedposts']; } } } // Get lastpost pid to check if we're merging a post that is on the lastpost info $query = $db->simple_select("posts", "pid", "tid = '{$post['tid']}'", array('order_by' => 'dateline', 'order_dir' => 'desc', 'limit' => '1')); $lastpostpid = $db->fetch_field($query, 'pid'); $query2 = $db->simple_select("attachments", "COUNT(aid) as count", "pid IN({$pidin}) AND visible='1'"); $attachment_count = $db->fetch_field($query2, "count"); $db->update_query("threads", array("attachmentcount" => $attachment_count), "tid = '{$mastertid}'"); // Update the message $mergepost = array("message" => $db->escape_string($message)); $db->update_query("posts", $mergepost, "pid = '{$masterpid}'"); // Delete the extra posts $db->delete_query("posts", "pid IN({$pidin}) AND pid != '{$masterpid}'"); // Update pid for attachments $mergepost2 = array("pid" => $masterpid); $db->update_query("attachments", $mergepost2, "pid IN({$pidin})"); $arguments = array("pids" => $pids, "tid" => $tid); $plugins->run_hooks("class_moderation_merge_posts", $arguments); if (is_array($thread_counters)) { foreach ($thread_counters as $tid => $counters) { $db->update_query("threads", $counters, "tid='{$tid}'"); update_thread_data($tid); } } update_thread_data($mastertid); update_forum_lastpost($fid); if (is_array($forum_counters)) { foreach ($forum_counters as $fid => $counters) { $updated_forum_stats = array("posts" => "+{$counters['num_posts']}", "unapprovedposts" => "-{$counters['num_posts']}", "threads" => "+{$counters['num_threads']}", "unapprovedthreads" => "-{$counters['num_threads']}"); update_forum_counters($fid, $updated_forum_stats); } } return true; }
/** * Changes the author of the post. * * */ function accountswitcher_author_change() { global $mybb, $db, $eas; // Change action if ($mybb->input['action'] == "do_author" && $mybb->request_method == "post" && ($mybb->settings['aj_changeauthor'] == 1 || $mybb->settings['aj_admin_changeauthor'] == 1)) { // Verify incoming POST request verify_post_check($mybb->get_input('my_post_key')); // Get the current author of the post $pid = $mybb->get_input('pid', MyBB::INPUT_INT); $post = get_post($pid); $tid = (int) $post['tid']; $forum = get_forum($post['fid']); // Get the new user if (is_numeric($mybb->input['authorswitch'])) { // Input is uid from change author $newuid = $mybb->get_input('authorswitch', MyBB::INPUT_INT); $newauthor = get_user($newuid); } else { // Input is username from author moderation $newname = htmlspecialchars_uni($mybb->get_input('authorswitch')); $newauthor = get_user_by_username($newname); $newauthor = get_user((int) $newauthor['uid']); } // New user doesn't exist? Redirect back to the post without changes if ($newauthor['uid'] == 0) { redirect(htmlentities($_POST['p_link'])); return; } // Subtract from the users post count // Update the post count if this forum allows post counts to be tracked if ($forum['usepostcounts'] != 0) { $db->write_query("UPDATE " . TABLE_PREFIX . "users SET postnum=postnum-1 WHERE uid='" . (int) $post['uid'] . "'"); $db->write_query("UPDATE " . TABLE_PREFIX . "users SET postnum=postnum+1 WHERE uid='" . (int) $newauthor['uid'] . "'"); } $updated_record = array("uid" => (int) $newauthor['uid'], "username" => $db->escape_string($newauthor['username'])); if ($db->update_query("posts", $updated_record, "pid='" . (int) $post['pid'] . "'")) { global $lang; if (!isset($lang->aj_author_change_log)) { $lang->load("accountswitcher"); } // Update first/last post info, log moderator action, redirect back to the post update_thread_data($tid); update_forum_lastpost((int) $post['fid']); $lang->aj_author_change_log = $lang->sprintf($lang->aj_author_change_log, (int) $post['pid'], htmlspecialchars_uni($post['username']), htmlspecialchars_uni($newauthor['username'])); log_moderator_action(array("pid" => $post['pid']), $lang->aj_author_change_log); // Send pm to old and new author after moderation if ($post['uid'] != $mybb->user['uid'] && $mybb->settings['aj_admin_changeauthor'] == 1) { if ($mybb->settings['aj_authorpm'] == 1) { // Send PM require_once MYBB_ROOT . "inc/datahandlers/pm.php"; $pmhandler = new PMDataHandler(); $lang->aj_author_change_pm_body = $lang->sprintf($lang->aj_author_change_pm_body, htmlspecialchars_uni($mybb->user['username']), $mybb->settings['bburl'] . '/' . htmlentities($_POST['p_link']), htmlspecialchars_uni($post['subject']), htmlspecialchars_uni($post['username']), htmlspecialchars_uni($newauthor['username'])); $subject = $lang->aj_author_change_pm_subject; $body = $lang->aj_author_change_pm_body; $pm = array('subject' => $subject, 'message' => $body, 'icon' => '', 'toid' => array($post['uid'], $newauthor['uid']), 'fromid' => $mybb->user['uid'], "do" => '', "pmid" => ''); $pm['options'] = array('signature' => '0', 'savecopy' => '0', 'disablesmilies' => '0', 'readreceipt' => '0'); $pmhandler->set_data($pm); $valid_pm = $pmhandler->validate_pm(); if ($valid_pm) { $pmhandler->insert_pm(); } } // Show alert if ($mybb->settings['aj_myalerts'] == 1 && isset($mybb->user['myalerts_disabled_alert_types'])) { $alertType = MybbStuff_MyAlerts_AlertTypeManager::getInstance()->getByCode('accountswitcher_author'); $alerts = array(); $subject = htmlspecialchars_uni($post['subject']); $alert_old = new MybbStuff_MyAlerts_Entity_Alert((int) $post['uid'], $alertType, $tid); $alert_old->setExtraDetails(array('thread_title' => $subject, 'pid' => $pid, 'tid' => $tid, 'olduser' => htmlspecialchars_uni($post['username']), 'newuser' => htmlspecialchars_uni($newauthor['username']))); $alerts[] = $alert_old; $alert_new = new MybbStuff_MyAlerts_Entity_Alert((int) $newauthor['uid'], $alertType, $tid); $alert_new->setExtraDetails(array('thread_title' => $subject, 'pid' => $pid, 'tid' => $tid, 'olduser' => htmlspecialchars_uni($post['username']), 'newuser' => htmlspecialchars_uni($newauthor['username']))); $alerts[] = $alert_new; if (!empty($alerts)) { MybbStuff_MyAlerts_AlertManager::getInstance()->addAlerts($alerts); } } } $eas->update_accountswitcher_cache(); redirect(htmlentities($_POST['p_link'])); } } else { return; } }