Chris@76: $user_info['id'], Chris@76: 'board_list' => $boards, Chris@76: ) Chris@76: ); Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}log_boards Chris@76: WHERE id_board IN ({array_int:board_list}) Chris@76: AND id_member = {int:current_member}', Chris@76: array( Chris@76: 'current_member' => $user_info['id'], Chris@76: 'board_list' => $boards, Chris@76: ) Chris@76: ); Chris@76: } Chris@76: // Otherwise mark the board as read. Chris@76: else Chris@76: { Chris@76: $markRead = array(); Chris@76: foreach ($boards as $board) Chris@76: $markRead[] = array($modSettings['maxMsgID'], $user_info['id'], $board); Chris@76: Chris@76: // Update log_mark_read and log_boards. Chris@76: $smcFunc['db_insert']('replace', Chris@76: '{db_prefix}log_mark_read', Chris@76: array('id_msg' => 'int', 'id_member' => 'int', 'id_board' => 'int'), Chris@76: $markRead, Chris@76: array('id_board', 'id_member') Chris@76: ); Chris@76: Chris@76: $smcFunc['db_insert']('replace', Chris@76: '{db_prefix}log_boards', Chris@76: array('id_msg' => 'int', 'id_member' => 'int', 'id_board' => 'int'), Chris@76: $markRead, Chris@76: array('id_board', 'id_member') Chris@76: ); Chris@76: } Chris@76: Chris@76: // Get rid of useless log_topics data, because log_mark_read is better for it - even if marking unread - I think so... Chris@76: $result = $smcFunc['db_query']('', ' Chris@76: SELECT MIN(id_topic) Chris@76: FROM {db_prefix}log_topics Chris@76: WHERE id_member = {int:current_member}', Chris@76: array( Chris@76: 'current_member' => $user_info['id'], Chris@76: ) Chris@76: ); Chris@76: list ($lowest_topic) = $smcFunc['db_fetch_row']($result); Chris@76: $smcFunc['db_free_result']($result); Chris@76: Chris@76: if (empty($lowest_topic)) Chris@76: return; Chris@76: Chris@76: // !!!SLOW This query seems to eat it sometimes. Chris@76: $result = $smcFunc['db_query']('', ' Chris@76: SELECT lt.id_topic Chris@76: FROM {db_prefix}log_topics AS lt Chris@76: INNER JOIN {db_prefix}topics AS t /*!40000 USE INDEX (PRIMARY) */ ON (t.id_topic = lt.id_topic Chris@76: AND t.id_board IN ({array_int:board_list})) Chris@76: WHERE lt.id_member = {int:current_member} Chris@76: AND lt.id_topic >= {int:lowest_topic}', Chris@76: array( Chris@76: 'current_member' => $user_info['id'], Chris@76: 'board_list' => $boards, Chris@76: 'lowest_topic' => $lowest_topic, Chris@76: ) Chris@76: ); Chris@76: $topics = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($result)) Chris@76: $topics[] = $row['id_topic']; Chris@76: $smcFunc['db_free_result']($result); Chris@76: Chris@76: if (!empty($topics)) Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}log_topics Chris@76: WHERE id_member = {int:current_member} Chris@76: AND id_topic IN ({array_int:topic_list})', Chris@76: array( Chris@76: 'current_member' => $user_info['id'], Chris@76: 'topic_list' => $topics, Chris@76: ) Chris@76: ); Chris@76: } Chris@76: Chris@76: // Mark one or more boards as read. Chris@76: function MarkRead() Chris@76: { Chris@76: global $board, $topic, $user_info, $board_info, $modSettings, $smcFunc; Chris@76: Chris@76: // No Guests allowed! Chris@76: is_not_guest(); Chris@76: Chris@76: checkSession('get'); Chris@76: Chris@76: if (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'all') Chris@76: { Chris@76: // Find all the boards this user can see. Chris@76: $result = $smcFunc['db_query']('', ' Chris@76: SELECT b.id_board Chris@76: FROM {db_prefix}boards AS b Chris@76: WHERE {query_see_board}', Chris@76: array( Chris@76: ) Chris@76: ); Chris@76: $boards = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($result)) Chris@76: $boards[] = $row['id_board']; Chris@76: $smcFunc['db_free_result']($result); Chris@76: Chris@76: if (!empty($boards)) Chris@76: markBoardsRead($boards, isset($_REQUEST['unread'])); Chris@76: Chris@76: $_SESSION['id_msg_last_visit'] = $modSettings['maxMsgID']; Chris@76: if (!empty($_SESSION['old_url']) && strpos($_SESSION['old_url'], 'action=unread') !== false) Chris@76: redirectexit('action=unread'); Chris@76: Chris@76: if (isset($_SESSION['topicseen_cache'])) Chris@76: $_SESSION['topicseen_cache'] = array(); Chris@76: Chris@76: redirectexit(); Chris@76: } Chris@76: elseif (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'unreadreplies') Chris@76: { Chris@76: // Make sure all the boards are integers! Chris@76: $topics = explode('-', $_REQUEST['topics']); Chris@76: Chris@76: $markRead = array(); Chris@76: foreach ($topics as $id_topic) Chris@76: $markRead[] = array($modSettings['maxMsgID'], $user_info['id'], (int) $id_topic); Chris@76: Chris@76: $smcFunc['db_insert']('replace', Chris@76: '{db_prefix}log_topics', Chris@76: array('id_msg' => 'int', 'id_member' => 'int', 'id_topic' => 'int'), Chris@76: $markRead, Chris@76: array('id_member', 'id_topic') Chris@76: ); Chris@76: Chris@76: if (isset($_SESSION['topicseen_cache'])) Chris@76: $_SESSION['topicseen_cache'] = array(); Chris@76: Chris@76: redirectexit('action=unreadreplies'); Chris@76: } Chris@76: Chris@76: // Special case: mark a topic unread! Chris@76: elseif (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'topic') Chris@76: { Chris@76: // First, let's figure out what the latest message is. Chris@76: $result = $smcFunc['db_query']('', ' Chris@76: SELECT id_first_msg, id_last_msg Chris@76: FROM {db_prefix}topics Chris@76: WHERE id_topic = {int:current_topic}', Chris@76: array( Chris@76: 'current_topic' => $topic, Chris@76: ) Chris@76: ); Chris@76: $topicinfo = $smcFunc['db_fetch_assoc']($result); Chris@76: $smcFunc['db_free_result']($result); Chris@76: Chris@76: if (!empty($_GET['t'])) Chris@76: { Chris@76: // If they read the whole topic, go back to the beginning. Chris@76: if ($_GET['t'] >= $topicinfo['id_last_msg']) Chris@76: $earlyMsg = 0; Chris@76: // If they want to mark the whole thing read, same. Chris@76: elseif ($_GET['t'] <= $topicinfo['id_first_msg']) Chris@76: $earlyMsg = 0; Chris@76: // Otherwise, get the latest message before the named one. Chris@76: else Chris@76: { Chris@76: $result = $smcFunc['db_query']('', ' Chris@76: SELECT MAX(id_msg) Chris@76: FROM {db_prefix}messages Chris@76: WHERE id_topic = {int:current_topic} Chris@76: AND id_msg >= {int:id_first_msg} Chris@76: AND id_msg < {int:topic_msg_id}', Chris@76: array( Chris@76: 'current_topic' => $topic, Chris@76: 'topic_msg_id' => (int) $_GET['t'], Chris@76: 'id_first_msg' => $topicinfo['id_first_msg'], Chris@76: ) Chris@76: ); Chris@76: list ($earlyMsg) = $smcFunc['db_fetch_row']($result); Chris@76: $smcFunc['db_free_result']($result); Chris@76: } Chris@76: } Chris@76: // Marking read from first page? That's the whole topic. Chris@76: elseif ($_REQUEST['start'] == 0) Chris@76: $earlyMsg = 0; Chris@76: else Chris@76: { Chris@76: $result = $smcFunc['db_query']('', ' Chris@76: SELECT id_msg Chris@76: FROM {db_prefix}messages Chris@76: WHERE id_topic = {int:current_topic} Chris@76: ORDER BY id_msg Chris@76: LIMIT ' . (int) $_REQUEST['start'] . ', 1', Chris@76: array( Chris@76: 'current_topic' => $topic, Chris@76: ) Chris@76: ); Chris@76: list ($earlyMsg) = $smcFunc['db_fetch_row']($result); Chris@76: $smcFunc['db_free_result']($result); Chris@76: Chris@76: $earlyMsg--; Chris@76: } Chris@76: Chris@76: // Blam, unread! Chris@76: $smcFunc['db_insert']('replace', Chris@76: '{db_prefix}log_topics', Chris@76: array('id_msg' => 'int', 'id_member' => 'int', 'id_topic' => 'int'), Chris@76: array($earlyMsg, $user_info['id'], $topic), Chris@76: array('id_member', 'id_topic') Chris@76: ); Chris@76: Chris@76: redirectexit('board=' . $board . '.0'); Chris@76: } Chris@76: else Chris@76: { Chris@76: $categories = array(); Chris@76: $boards = array(); Chris@76: Chris@76: if (isset($_REQUEST['c'])) Chris@76: { Chris@76: $_REQUEST['c'] = explode(',', $_REQUEST['c']); Chris@76: foreach ($_REQUEST['c'] as $c) Chris@76: $categories[] = (int) $c; Chris@76: } Chris@76: if (isset($_REQUEST['boards'])) Chris@76: { Chris@76: $_REQUEST['boards'] = explode(',', $_REQUEST['boards']); Chris@76: foreach ($_REQUEST['boards'] as $b) Chris@76: $boards[] = (int) $b; Chris@76: } Chris@76: if (!empty($board)) Chris@76: $boards[] = (int) $board; Chris@76: Chris@76: if (isset($_REQUEST['children']) && !empty($boards)) Chris@76: { Chris@76: // They want to mark the entire tree starting with the boards specified Chris@76: // The easist thing is to just get all the boards they can see, but since we've specified the top of tree we ignore some of them Chris@76: Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT b.id_board, b.id_parent Chris@76: FROM {db_prefix}boards AS b Chris@76: WHERE {query_see_board} Chris@76: AND b.child_level > {int:no_parents} Chris@76: AND b.id_board NOT IN ({array_int:board_list}) Chris@76: ORDER BY child_level ASC Chris@76: ', Chris@76: array( Chris@76: 'no_parents' => 0, Chris@76: 'board_list' => $boards, Chris@76: ) Chris@76: ); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: if (in_array($row['id_parent'], $boards)) Chris@76: $boards[] = $row['id_board']; Chris@76: $smcFunc['db_free_result']($request); Chris@76: } Chris@76: Chris@76: $clauses = array(); Chris@76: $clauseParameters = array(); Chris@76: if (!empty($categories)) Chris@76: { Chris@76: $clauses[] = 'id_cat IN ({array_int:category_list})'; Chris@76: $clauseParameters['category_list'] = $categories; Chris@76: } Chris@76: if (!empty($boards)) Chris@76: { Chris@76: $clauses[] = 'id_board IN ({array_int:board_list})'; Chris@76: $clauseParameters['board_list'] = $boards; Chris@76: } Chris@76: Chris@76: if (empty($clauses)) Chris@76: redirectexit(); Chris@76: Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT b.id_board Chris@76: FROM {db_prefix}boards AS b Chris@76: WHERE {query_see_board} Chris@76: AND b.' . implode(' OR b.', $clauses), Chris@76: array_merge($clauseParameters, array( Chris@76: )) Chris@76: ); Chris@76: $boards = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: $boards[] = $row['id_board']; Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: if (empty($boards)) Chris@76: redirectexit(); Chris@76: Chris@76: markBoardsRead($boards, isset($_REQUEST['unread'])); Chris@76: Chris@76: foreach ($boards as $b) Chris@76: { Chris@76: if (isset($_SESSION['topicseen_cache'][$b])) Chris@76: $_SESSION['topicseen_cache'][$b] = array(); Chris@76: } Chris@76: Chris@76: if (!isset($_REQUEST['unread'])) Chris@76: { Chris@76: // Find all the boards this user can see. Chris@76: $result = $smcFunc['db_query']('', ' Chris@76: SELECT b.id_board Chris@76: FROM {db_prefix}boards AS b Chris@76: WHERE b.id_parent IN ({array_int:parent_list}) Chris@76: AND {query_see_board}', Chris@76: array( Chris@76: 'parent_list' => $boards, Chris@76: ) Chris@76: ); Chris@76: if ($smcFunc['db_num_rows']($result) > 0) Chris@76: { Chris@76: $logBoardInserts = ''; Chris@76: while ($row = $smcFunc['db_fetch_assoc']($result)) Chris@76: $logBoardInserts[] = array($modSettings['maxMsgID'], $user_info['id'], $row['id_board']); Chris@76: Chris@76: $smcFunc['db_insert']('replace', Chris@76: '{db_prefix}log_boards', Chris@76: array('id_msg' => 'int', 'id_member' => 'int', 'id_board' => 'int'), Chris@76: $logBoardInserts, Chris@76: array('id_member', 'id_board') Chris@76: ); Chris@76: } Chris@76: $smcFunc['db_free_result']($result); Chris@76: Chris@76: if (empty($board)) Chris@76: redirectexit(); Chris@76: else Chris@76: redirectexit('board=' . $board . '.0'); Chris@76: } Chris@76: else Chris@76: { Chris@76: if (empty($board_info['parent'])) Chris@76: redirectexit(); Chris@76: else Chris@76: redirectexit('board=' . $board_info['parent'] . '.0'); Chris@76: } Chris@76: } Chris@76: } Chris@76: Chris@76: // Get the id_member associated with the specified message. Chris@76: function getMsgMemberID($messageID) Chris@76: { Chris@76: global $smcFunc; Chris@76: Chris@76: // Find the topic and make sure the member still exists. Chris@76: $result = $smcFunc['db_query']('', ' Chris@76: SELECT IFNULL(mem.id_member, 0) Chris@76: FROM {db_prefix}messages AS m Chris@76: LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member) Chris@76: WHERE m.id_msg = {int:selected_message} Chris@76: LIMIT 1', Chris@76: array( Chris@76: 'selected_message' => (int) $messageID, Chris@76: ) Chris@76: ); Chris@76: if ($smcFunc['db_num_rows']($result) > 0) Chris@76: list ($memberID) = $smcFunc['db_fetch_row']($result); Chris@76: // The message doesn't even exist. Chris@76: else Chris@76: $memberID = 0; Chris@76: $smcFunc['db_free_result']($result); Chris@76: Chris@76: return (int) $memberID; Chris@76: } Chris@76: Chris@76: // Modify the settings and position of a board. Chris@76: function modifyBoard($board_id, &$boardOptions) Chris@76: { Chris@76: global $sourcedir, $cat_tree, $boards, $boardList, $modSettings, $smcFunc; Chris@76: Chris@76: // Get some basic information about all boards and categories. Chris@76: getBoardTree(); Chris@76: Chris@76: // Make sure given boards and categories exist. Chris@76: if (!isset($boards[$board_id]) || (isset($boardOptions['target_board']) && !isset($boards[$boardOptions['target_board']])) || (isset($boardOptions['target_category']) && !isset($cat_tree[$boardOptions['target_category']]))) Chris@76: fatal_lang_error('no_board'); Chris@76: Chris@76: // All things that will be updated in the database will be in $boardUpdates. Chris@76: $boardUpdates = array(); Chris@76: $boardUpdateParameters = array(); Chris@76: Chris@76: // In case the board has to be moved Chris@76: if (isset($boardOptions['move_to'])) Chris@76: { Chris@76: // Move the board to the top of a given category. Chris@76: if ($boardOptions['move_to'] == 'top') Chris@76: { Chris@76: $id_cat = $boardOptions['target_category']; Chris@76: $child_level = 0; Chris@76: $id_parent = 0; Chris@76: $after = $cat_tree[$id_cat]['last_board_order']; Chris@76: } Chris@76: Chris@76: // Move the board to the bottom of a given category. Chris@76: elseif ($boardOptions['move_to'] == 'bottom') Chris@76: { Chris@76: $id_cat = $boardOptions['target_category']; Chris@76: $child_level = 0; Chris@76: $id_parent = 0; Chris@76: $after = 0; Chris@76: foreach ($cat_tree[$id_cat]['children'] as $id_board => $dummy) Chris@76: $after = max($after, $boards[$id_board]['order']); Chris@76: } Chris@76: Chris@76: // Make the board a child of a given board. Chris@76: elseif ($boardOptions['move_to'] == 'child') Chris@76: { Chris@76: $id_cat = $boards[$boardOptions['target_board']]['category']; Chris@76: $child_level = $boards[$boardOptions['target_board']]['level'] + 1; Chris@76: $id_parent = $boardOptions['target_board']; Chris@76: Chris@76: // People can be creative, in many ways... Chris@76: if (isChildOf($id_parent, $board_id)) Chris@76: fatal_lang_error('mboards_parent_own_child_error', false); Chris@76: elseif ($id_parent == $board_id) Chris@76: fatal_lang_error('mboards_board_own_child_error', false); Chris@76: Chris@76: $after = $boards[$boardOptions['target_board']]['order']; Chris@76: Chris@76: // Check if there are already children and (if so) get the max board order. Chris@76: if (!empty($boards[$id_parent]['tree']['children']) && empty($boardOptions['move_first_child'])) Chris@76: foreach ($boards[$id_parent]['tree']['children'] as $childBoard_id => $dummy) Chris@76: $after = max($after, $boards[$childBoard_id]['order']); Chris@76: } Chris@76: Chris@76: // Place a board before or after another board, on the same child level. Chris@76: elseif (in_array($boardOptions['move_to'], array('before', 'after'))) Chris@76: { Chris@76: $id_cat = $boards[$boardOptions['target_board']]['category']; Chris@76: $child_level = $boards[$boardOptions['target_board']]['level']; Chris@76: $id_parent = $boards[$boardOptions['target_board']]['parent']; Chris@76: $after = $boards[$boardOptions['target_board']]['order'] - ($boardOptions['move_to'] == 'before' ? 1 : 0); Chris@76: } Chris@76: Chris@76: // Oops...? Chris@76: else Chris@76: trigger_error('modifyBoard(): The move_to value \'' . $boardOptions['move_to'] . '\' is incorrect', E_USER_ERROR); Chris@76: Chris@76: // Get a list of children of this board. Chris@76: $childList = array(); Chris@76: recursiveBoards($childList, $boards[$board_id]['tree']); Chris@76: Chris@76: // See if there are changes that affect children. Chris@76: $childUpdates = array(); Chris@76: $levelDiff = $child_level - $boards[$board_id]['level']; Chris@76: if ($levelDiff != 0) Chris@76: $childUpdates[] = 'child_level = child_level ' . ($levelDiff > 0 ? '+ ' : '') . '{int:level_diff}'; Chris@76: if ($id_cat != $boards[$board_id]['category']) Chris@76: $childUpdates[] = 'id_cat = {int:category}'; Chris@76: Chris@76: // Fix the children of this board. Chris@76: if (!empty($childList) && !empty($childUpdates)) Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}boards Chris@76: SET ' . implode(', Chris@76: ', $childUpdates) . ' Chris@76: WHERE id_board IN ({array_int:board_list})', Chris@76: array( Chris@76: 'board_list' => $childList, Chris@76: 'category' => $id_cat, Chris@76: 'level_diff' => $levelDiff, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Make some room for this spot. Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}boards Chris@76: SET board_order = board_order + {int:new_order} Chris@76: WHERE board_order > {int:insert_after} Chris@76: AND id_board != {int:selected_board}', Chris@76: array( Chris@76: 'insert_after' => $after, Chris@76: 'selected_board' => $board_id, Chris@76: 'new_order' => 1 + count($childList), Chris@76: ) Chris@76: ); Chris@76: Chris@76: $boardUpdates[] = 'id_cat = {int:id_cat}'; Chris@76: $boardUpdates[] = 'id_parent = {int:id_parent}'; Chris@76: $boardUpdates[] = 'child_level = {int:child_level}'; Chris@76: $boardUpdates[] = 'board_order = {int:board_order}'; Chris@76: $boardUpdateParameters += array( Chris@76: 'id_cat' => $id_cat, Chris@76: 'id_parent' => $id_parent, Chris@76: 'child_level' => $child_level, Chris@76: 'board_order' => $after + 1, Chris@76: ); Chris@76: } Chris@76: Chris@76: // This setting is a little twisted in the database... Chris@76: if (isset($boardOptions['posts_count'])) Chris@76: { Chris@76: $boardUpdates[] = 'count_posts = {int:count_posts}'; Chris@76: $boardUpdateParameters['count_posts'] = $boardOptions['posts_count'] ? 0 : 1; Chris@76: } Chris@76: Chris@76: // Set the theme for this board. Chris@76: if (isset($boardOptions['board_theme'])) Chris@76: { Chris@76: $boardUpdates[] = 'id_theme = {int:id_theme}'; Chris@76: $boardUpdateParameters['id_theme'] = (int) $boardOptions['board_theme']; Chris@76: } Chris@76: Chris@76: // Should the board theme override the user preferred theme? Chris@76: if (isset($boardOptions['override_theme'])) Chris@76: { Chris@76: $boardUpdates[] = 'override_theme = {int:override_theme}'; Chris@76: $boardUpdateParameters['override_theme'] = $boardOptions['override_theme'] ? 1 : 0; Chris@76: } Chris@76: Chris@76: // Who's allowed to access this board. Chris@76: if (isset($boardOptions['access_groups'])) Chris@76: { Chris@76: $boardUpdates[] = 'member_groups = {string:member_groups}'; Chris@76: $boardUpdateParameters['member_groups'] = implode(',', $boardOptions['access_groups']); Chris@76: } Chris@76: Chris@76: if (isset($boardOptions['board_name'])) Chris@76: { Chris@76: $boardUpdates[] = 'name = {string:board_name}'; Chris@76: $boardUpdateParameters['board_name'] = $boardOptions['board_name']; Chris@76: } Chris@76: Chris@76: if (isset($boardOptions['board_description'])) Chris@76: { Chris@76: $boardUpdates[] = 'description = {string:board_description}'; Chris@76: $boardUpdateParameters['board_description'] = $boardOptions['board_description']; Chris@76: } Chris@76: Chris@76: if (isset($boardOptions['profile'])) Chris@76: { Chris@76: $boardUpdates[] = 'id_profile = {int:profile}'; Chris@76: $boardUpdateParameters['profile'] = (int) $boardOptions['profile']; Chris@76: } Chris@76: Chris@76: if (isset($boardOptions['redirect'])) Chris@76: { Chris@76: $boardUpdates[] = 'redirect = {string:redirect}'; Chris@76: $boardUpdateParameters['redirect'] = $boardOptions['redirect']; Chris@76: } Chris@76: Chris@76: if (isset($boardOptions['num_posts'])) Chris@76: { Chris@76: $boardUpdates[] = 'num_posts = {int:num_posts}'; Chris@76: $boardUpdateParameters['num_posts'] = (int) $boardOptions['num_posts']; Chris@76: } Chris@76: Chris@76: // Do the updates (if any). Chris@76: if (!empty($boardUpdates)) Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}boards Chris@76: SET Chris@76: ' . implode(', Chris@76: ', $boardUpdates) . ' Chris@76: WHERE id_board = {int:selected_board}', Chris@76: array_merge($boardUpdateParameters, array( Chris@76: 'selected_board' => $board_id, Chris@76: )) Chris@76: ); Chris@76: Chris@76: // Set moderators of this board. Chris@76: if (isset($boardOptions['moderators']) || isset($boardOptions['moderator_string'])) Chris@76: { Chris@76: // Reset current moderators for this board - if there are any! Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}moderators Chris@76: WHERE id_board = {int:board_list}', Chris@76: array( Chris@76: 'board_list' => $board_id, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Validate and get the IDs of the new moderators. Chris@76: if (isset($boardOptions['moderator_string']) && trim($boardOptions['moderator_string']) != '') Chris@76: { Chris@76: // Divvy out the usernames, remove extra space. Chris@76: $moderator_string = strtr($smcFunc['htmlspecialchars']($boardOptions['moderator_string'], ENT_QUOTES), array('"' => '"')); Chris@76: preg_match_all('~"([^"]+)"~', $moderator_string, $matches); Chris@76: $moderators = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $moderator_string))); Chris@76: for ($k = 0, $n = count($moderators); $k < $n; $k++) Chris@76: { Chris@76: $moderators[$k] = trim($moderators[$k]); Chris@76: Chris@76: if (strlen($moderators[$k]) == 0) Chris@76: unset($moderators[$k]); Chris@76: } Chris@76: Chris@76: // Find all the id_member's for the member_name's in the list. Chris@76: if (empty($boardOptions['moderators'])) Chris@76: $boardOptions['moderators'] = array(); Chris@76: if (!empty($moderators)) Chris@76: { Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_member Chris@76: FROM {db_prefix}members Chris@76: WHERE member_name IN ({array_string:moderator_list}) OR real_name IN ({array_string:moderator_list}) Chris@76: LIMIT ' . count($moderators), Chris@76: array( Chris@76: 'moderator_list' => $moderators, Chris@76: ) Chris@76: ); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: $boardOptions['moderators'][] = $row['id_member']; Chris@76: $smcFunc['db_free_result']($request); Chris@76: } Chris@76: } Chris@76: Chris@76: // Add the moderators to the board. Chris@76: if (!empty($boardOptions['moderators'])) Chris@76: { Chris@76: $inserts = array(); Chris@76: foreach ($boardOptions['moderators'] as $moderator) Chris@76: $inserts[] = array($board_id, $moderator); Chris@76: Chris@76: $smcFunc['db_insert']('insert', Chris@76: '{db_prefix}moderators', Chris@76: array('id_board' => 'int', 'id_member' => 'int'), Chris@76: $inserts, Chris@76: array('id_board', 'id_member') Chris@76: ); Chris@76: } Chris@76: Chris@76: // Note that caches can now be wrong! Chris@76: updateSettings(array('settings_updated' => time())); Chris@76: } Chris@76: Chris@76: if (isset($boardOptions['move_to'])) Chris@76: reorderBoards(); Chris@76: Chris@76: clean_cache('data'); Chris@76: Chris@76: if (empty($boardOptions['dont_log'])) Chris@76: logAction('edit_board', array('board' => $board_id), 'admin'); Chris@76: } Chris@76: Chris@76: // Create a new board and set its properties and position. Chris@76: function createBoard($boardOptions) Chris@76: { Chris@76: global $boards, $modSettings, $smcFunc; Chris@76: Chris@76: // Trigger an error if one of the required values is not set. Chris@76: if (!isset($boardOptions['board_name']) || trim($boardOptions['board_name']) == '' || !isset($boardOptions['move_to']) || !isset($boardOptions['target_category'])) Chris@76: trigger_error('createBoard(): One or more of the required options is not set', E_USER_ERROR); Chris@76: Chris@76: if (in_array($boardOptions['move_to'], array('child', 'before', 'after')) && !isset($boardOptions['target_board'])) Chris@76: trigger_error('createBoard(): Target board is not set', E_USER_ERROR); Chris@76: Chris@76: // Set every optional value to its default value. Chris@76: $boardOptions += array( Chris@76: 'posts_count' => true, Chris@76: 'override_theme' => false, Chris@76: 'board_theme' => 0, Chris@76: 'access_groups' => array(), Chris@76: 'board_description' => '', Chris@76: 'profile' => 1, Chris@76: 'moderators' => '', Chris@76: 'inherit_permissions' => true, Chris@76: 'dont_log' => true, Chris@76: ); Chris@76: Chris@76: // Insert a board, the settings are dealt with later. Chris@76: $smcFunc['db_insert']('', Chris@76: '{db_prefix}boards', Chris@76: array( Chris@76: 'id_cat' => 'int', 'name' => 'string-255', 'description' => 'string', 'board_order' => 'int', Chris@76: 'member_groups' => 'string', 'redirect' => 'string', Chris@76: ), Chris@76: array( Chris@76: $boardOptions['target_category'], $boardOptions['board_name'] , '', 0, Chris@76: '-1,0', '', Chris@76: ), Chris@76: array('id_board') Chris@76: ); Chris@76: $board_id = $smcFunc['db_insert_id']('{db_prefix}boards', 'id_board'); Chris@76: Chris@76: if (empty($board_id)) Chris@76: return 0; Chris@76: Chris@76: // Change the board according to the given specifications. Chris@76: modifyBoard($board_id, $boardOptions); Chris@76: Chris@76: // Do we want the parent permissions to be inherited? Chris@76: if ($boardOptions['inherit_permissions']) Chris@76: { Chris@76: getBoardTree(); Chris@76: Chris@76: if (!empty($boards[$board_id]['parent'])) Chris@76: { Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_profile Chris@76: FROM {db_prefix}boards Chris@76: WHERE id_board = {int:board_parent} Chris@76: LIMIT 1', Chris@76: array( Chris@76: 'board_parent' => (int) $boards[$board_id]['parent'], Chris@76: ) Chris@76: ); Chris@76: list ($boardOptions['profile']) = $smcFunc['db_fetch_row']($request); Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}boards Chris@76: SET id_profile = {int:new_profile} Chris@76: WHERE id_board = {int:current_board}', Chris@76: array( Chris@76: 'new_profile' => $boardOptions['profile'], Chris@76: 'current_board' => $board_id, Chris@76: ) Chris@76: ); Chris@76: } Chris@76: } Chris@76: Chris@76: // Clean the data cache. Chris@76: clean_cache('data'); Chris@76: Chris@76: // Created it. Chris@76: logAction('add_board', array('board' => $board_id), 'admin'); Chris@76: Chris@76: // Here you are, a new board, ready to be spammed. Chris@76: return $board_id; Chris@76: } Chris@76: Chris@76: // Remove one or more boards. Chris@76: function deleteBoards($boards_to_remove, $moveChildrenTo = null) Chris@76: { Chris@76: global $sourcedir, $boards, $smcFunc; Chris@76: Chris@76: // No boards to delete? Return! Chris@76: if (empty($boards_to_remove)) Chris@76: return; Chris@76: Chris@76: getBoardTree(); Chris@76: Chris@76: // If $moveChildrenTo is set to null, include the children in the removal. Chris@76: if ($moveChildrenTo === null) Chris@76: { Chris@76: // Get a list of the child boards that will also be removed. Chris@76: $child_boards_to_remove = array(); Chris@76: foreach ($boards_to_remove as $board_to_remove) Chris@76: recursiveBoards($child_boards_to_remove, $boards[$board_to_remove]['tree']); Chris@76: Chris@76: // Merge the children with their parents. Chris@76: if (!empty($child_boards_to_remove)) Chris@76: $boards_to_remove = array_unique(array_merge($boards_to_remove, $child_boards_to_remove)); Chris@76: } Chris@76: // Move the children to a safe home. Chris@76: else Chris@76: { Chris@76: foreach ($boards_to_remove as $id_board) Chris@76: { Chris@76: // !!! Separate category? Chris@76: if ($moveChildrenTo === 0) Chris@76: fixChildren($id_board, 0, 0); Chris@76: else Chris@76: fixChildren($id_board, $boards[$moveChildrenTo]['level'] + 1, $moveChildrenTo); Chris@76: } Chris@76: } Chris@76: Chris@76: // Delete ALL topics in the selected boards (done first so topics can't be marooned.) Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_topic Chris@76: FROM {db_prefix}topics Chris@76: WHERE id_board IN ({array_int:boards_to_remove})', Chris@76: array( Chris@76: 'boards_to_remove' => $boards_to_remove, Chris@76: ) Chris@76: ); Chris@76: $topics = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: $topics[] = $row['id_topic']; Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: require_once($sourcedir . '/RemoveTopic.php'); Chris@76: removeTopics($topics, false); Chris@76: Chris@76: // Delete the board's logs. Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}log_mark_read Chris@76: WHERE id_board IN ({array_int:boards_to_remove})', Chris@76: array( Chris@76: 'boards_to_remove' => $boards_to_remove, Chris@76: ) Chris@76: ); Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}log_boards Chris@76: WHERE id_board IN ({array_int:boards_to_remove})', Chris@76: array( Chris@76: 'boards_to_remove' => $boards_to_remove, Chris@76: ) Chris@76: ); Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}log_notify Chris@76: WHERE id_board IN ({array_int:boards_to_remove})', Chris@76: array( Chris@76: 'boards_to_remove' => $boards_to_remove, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Delete this board's moderators. Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}moderators Chris@76: WHERE id_board IN ({array_int:boards_to_remove})', Chris@76: array( Chris@76: 'boards_to_remove' => $boards_to_remove, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Delete any extra events in the calendar. Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}calendar Chris@76: WHERE id_board IN ({array_int:boards_to_remove})', Chris@76: array( Chris@76: 'boards_to_remove' => $boards_to_remove, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Delete any message icons that only appear on these boards. Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}message_icons Chris@76: WHERE id_board IN ({array_int:boards_to_remove})', Chris@76: array( Chris@76: 'boards_to_remove' => $boards_to_remove, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Delete the boards. Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}boards Chris@76: WHERE id_board IN ({array_int:boards_to_remove})', Chris@76: array( Chris@76: 'boards_to_remove' => $boards_to_remove, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Latest message/topic might not be there anymore. Chris@76: updateStats('message'); Chris@76: updateStats('topic'); Chris@76: updateSettings(array( Chris@76: 'calendar_updated' => time(), Chris@76: )); Chris@76: Chris@76: // Plus reset the cache to stop people getting odd results. Chris@76: updateSettings(array('settings_updated' => time())); Chris@76: Chris@76: // Clean the cache as well. Chris@76: clean_cache('data'); Chris@76: Chris@76: // Let's do some serious logging. Chris@76: foreach ($boards_to_remove as $id_board) Chris@76: logAction('delete_board', array('boardname' => $boards[$id_board]['name']), 'admin'); Chris@76: Chris@76: reorderBoards(); Chris@76: } Chris@76: Chris@76: // Put all boards in the right order. Chris@76: function reorderBoards() Chris@76: { Chris@76: global $cat_tree, $boardList, $boards, $smcFunc; Chris@76: Chris@76: getBoardTree(); Chris@76: Chris@76: // Set the board order for each category. Chris@76: $board_order = 0; Chris@76: foreach ($cat_tree as $catID => $dummy) Chris@76: { Chris@76: foreach ($boardList[$catID] as $boardID) Chris@76: if ($boards[$boardID]['order'] != ++$board_order) Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}boards Chris@76: SET board_order = {int:new_order} Chris@76: WHERE id_board = {int:selected_board}', Chris@76: array( Chris@76: 'new_order' => $board_order, Chris@76: 'selected_board' => $boardID, Chris@76: ) Chris@76: ); Chris@76: } Chris@76: Chris@76: // Sort the records of the boards table on the board_order value. Chris@76: $smcFunc['db_query']('alter_table_boards', ' Chris@76: ALTER TABLE {db_prefix}boards Chris@76: ORDER BY board_order', Chris@76: array( Chris@76: 'db_error_skip' => true, Chris@76: ) Chris@76: ); Chris@76: } Chris@76: Chris@76: // Fixes the children of a board by setting their child_levels to new values. Chris@76: function fixChildren($parent, $newLevel, $newParent) Chris@76: { Chris@76: global $smcFunc; Chris@76: Chris@76: // Grab all children of $parent... Chris@76: $result = $smcFunc['db_query']('', ' Chris@76: SELECT id_board Chris@76: FROM {db_prefix}boards Chris@76: WHERE id_parent = {int:parent_board}', Chris@76: array( Chris@76: 'parent_board' => $parent, Chris@76: ) Chris@76: ); Chris@76: $children = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($result)) Chris@76: $children[] = $row['id_board']; Chris@76: $smcFunc['db_free_result']($result); Chris@76: Chris@76: // ...and set it to a new parent and child_level. Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}boards Chris@76: SET id_parent = {int:new_parent}, child_level = {int:new_child_level} Chris@76: WHERE id_parent = {int:parent_board}', Chris@76: array( Chris@76: 'new_parent' => $newParent, Chris@76: 'new_child_level' => $newLevel, Chris@76: 'parent_board' => $parent, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Recursively fix the children of the children. Chris@76: foreach ($children as $child) Chris@76: fixChildren($child, $newLevel + 1, $child); Chris@76: } Chris@76: Chris@76: // Load a lot of useful information regarding the boards and categories. Chris@76: function getBoardTree() Chris@76: { Chris@76: global $cat_tree, $boards, $boardList, $txt, $modSettings, $smcFunc; Chris@76: Chris@76: // Getting all the board and category information you'd ever wanted. Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT Chris@76: IFNULL(b.id_board, 0) AS id_board, b.id_parent, b.name AS board_name, b.description, b.child_level, Chris@76: b.board_order, b.count_posts, b.member_groups, b.id_theme, b.override_theme, b.id_profile, b.redirect, Chris@76: b.num_posts, b.num_topics, c.id_cat, c.name AS cat_name, c.cat_order, c.can_collapse Chris@76: FROM {db_prefix}categories AS c Chris@76: LEFT JOIN {db_prefix}boards AS b ON (b.id_cat = c.id_cat) Chris@76: ORDER BY c.cat_order, b.child_level, b.board_order', Chris@76: array( Chris@76: ) Chris@76: ); Chris@76: $cat_tree = array(); Chris@76: $boards = array(); Chris@76: $last_board_order = 0; Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: if (!isset($cat_tree[$row['id_cat']])) Chris@76: { Chris@76: $cat_tree[$row['id_cat']] = array( Chris@76: 'node' => array( Chris@76: 'id' => $row['id_cat'], Chris@76: 'name' => $row['cat_name'], Chris@76: 'order' => $row['cat_order'], Chris@76: 'can_collapse' => $row['can_collapse'] Chris@76: ), Chris@76: 'is_first' => empty($cat_tree), Chris@76: 'last_board_order' => $last_board_order, Chris@76: 'children' => array() Chris@76: ); Chris@76: $prevBoard = 0; Chris@76: $curLevel = 0; Chris@76: } Chris@76: Chris@76: if (!empty($row['id_board'])) Chris@76: { Chris@76: if ($row['child_level'] != $curLevel) Chris@76: $prevBoard = 0; Chris@76: Chris@76: $boards[$row['id_board']] = array( Chris@76: 'id' => $row['id_board'], Chris@76: 'category' => $row['id_cat'], Chris@76: 'parent' => $row['id_parent'], Chris@76: 'level' => $row['child_level'], Chris@76: 'order' => $row['board_order'], Chris@76: 'name' => $row['board_name'], Chris@76: 'member_groups' => explode(',', $row['member_groups']), Chris@76: 'description' => $row['description'], Chris@76: 'count_posts' => empty($row['count_posts']), Chris@76: 'posts' => $row['num_posts'], Chris@76: 'topics' => $row['num_topics'], Chris@76: 'theme' => $row['id_theme'], Chris@76: 'override_theme' => $row['override_theme'], Chris@76: 'profile' => $row['id_profile'], Chris@76: 'redirect' => $row['redirect'], Chris@76: 'prev_board' => $prevBoard Chris@76: ); Chris@76: $prevBoard = $row['id_board']; Chris@76: $last_board_order = $row['board_order']; Chris@76: Chris@76: if (empty($row['child_level'])) Chris@76: { Chris@76: $cat_tree[$row['id_cat']]['children'][$row['id_board']] = array( Chris@76: 'node' => &$boards[$row['id_board']], Chris@76: 'is_first' => empty($cat_tree[$row['id_cat']]['children']), Chris@76: 'children' => array() Chris@76: ); Chris@76: $boards[$row['id_board']]['tree'] = &$cat_tree[$row['id_cat']]['children'][$row['id_board']]; Chris@76: } Chris@76: else Chris@76: { Chris@76: // Parent doesn't exist! Chris@76: if (!isset($boards[$row['id_parent']]['tree'])) Chris@76: fatal_lang_error('no_valid_parent', false, array($row['board_name'])); Chris@76: Chris@76: // Wrong childlevel...we can silently fix this... Chris@76: if ($boards[$row['id_parent']]['tree']['node']['level'] != $row['child_level'] - 1) Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}boards Chris@76: SET child_level = {int:new_child_level} Chris@76: WHERE id_board = {int:selected_board}', Chris@76: array( Chris@76: 'new_child_level' => $boards[$row['id_parent']]['tree']['node']['level'] + 1, Chris@76: 'selected_board' => $row['id_board'], Chris@76: ) Chris@76: ); Chris@76: Chris@76: $boards[$row['id_parent']]['tree']['children'][$row['id_board']] = array( Chris@76: 'node' => &$boards[$row['id_board']], Chris@76: 'is_first' => empty($boards[$row['id_parent']]['tree']['children']), Chris@76: 'children' => array() Chris@76: ); Chris@76: $boards[$row['id_board']]['tree'] = &$boards[$row['id_parent']]['tree']['children'][$row['id_board']]; Chris@76: } Chris@76: } Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: // Get a list of all the boards in each category (using recursion). Chris@76: $boardList = array(); Chris@76: foreach ($cat_tree as $catID => $node) Chris@76: { Chris@76: $boardList[$catID] = array(); Chris@76: recursiveBoards($boardList[$catID], $node); Chris@76: } Chris@76: } Chris@76: Chris@76: // Recursively get a list of boards. Chris@76: function recursiveBoards(&$_boardList, &$_tree) Chris@76: { Chris@76: if (empty($_tree['children'])) Chris@76: return; Chris@76: Chris@76: foreach ($_tree['children'] as $id => $node) Chris@76: { Chris@76: $_boardList[] = $id; Chris@76: recursiveBoards($_boardList, $node); Chris@76: } Chris@76: } Chris@76: Chris@76: // Returns whether the child board id is actually a child of the parent (recursive). Chris@76: function isChildOf($child, $parent) Chris@76: { Chris@76: global $boards; Chris@76: Chris@76: if (empty($boards[$child]['parent'])) Chris@76: return false; Chris@76: Chris@76: if ($boards[$child]['parent'] == $parent) Chris@76: return true; Chris@76: Chris@76: return isChildOf($boards[$child]['parent'], $parent); Chris@76: } Chris@76: Chris@76: ?>