/** * Notifies members who have requested notification for new topics posted on a board of said posts. * * receives data on the topics to send out notifications to by the passed in array. * only sends notifications to those who can *currently* see the topic (it doesn't matter if they could when they requested notification.) * loads the Post language file multiple times for each language if the userLanguage setting is set. * * @param mixed[] $topicData */ function sendBoardNotifications(&$topicData) { global $scripturl, $language, $user_info, $modSettings, $webmaster_email; $db = database(); require_once SUBSDIR . '/Mail.subs.php'; require_once SUBSDIR . '/Emailpost.subs.php'; // Do we have one or lots of topics? if (isset($topicData['body'])) { $topicData = array($topicData); } // Using the post to email functions? $maillist = !empty($modSettings['maillist_enabled']) && !empty($modSettings['pbe_post_enabled']); // Find out what boards we have... and clear out any rubbish! $boards = array(); foreach ($topicData as $key => $topic) { if (!empty($topic['board'])) { $boards[$topic['board']][] = $key; } else { unset($topic[$key]); continue; } // Convert to markdown markup e.g. styled plain text, while doing the censoring pbe_prepare_text($topicData[$key]['body'], $topicData[$key]['subject'], $topicData[$key]['signature']); } // Just the board numbers. $board_index = array_unique(array_keys($boards)); if (empty($board_index)) { return; } // Load the actual board names require_once SUBSDIR . '/Boards.subs.php'; $board_names = fetchBoardsInfo(array('boards' => $board_index, 'override_permissions' => true)); // Yea, we need to add this to the digest queue. $digest_insert = array(); foreach ($topicData as $id => $data) { $digest_insert[] = array($data['topic'], $data['msg'], 'topic', $user_info['id']); } $db->insert('', '{db_prefix}log_digest', array('id_topic' => 'int', 'id_msg' => 'int', 'note_type' => 'string', 'exclude' => 'int'), $digest_insert, array()); // Find the members with notification on for these boards. $members = $db->query('', ' SELECT mem.id_member, mem.email_address, mem.notify_regularity, mem.notify_send_body, mem.lngfile, mem.warning, ln.sent, ln.id_board, mem.id_group, mem.additional_groups, b.member_groups, b.id_profile, mem.id_post_group FROM {db_prefix}log_notify AS ln INNER JOIN {db_prefix}boards AS b ON (b.id_board = ln.id_board) INNER JOIN {db_prefix}members AS mem ON (mem.id_member = ln.id_member) WHERE ln.id_board IN ({array_int:board_list}) AND mem.id_member != {int:current_member} AND mem.is_activated = {int:is_activated} AND mem.notify_types != {int:notify_types} AND mem.notify_regularity < {int:notify_regularity} ORDER BY mem.lngfile', array('current_member' => $user_info['id'], 'board_list' => $board_index, 'is_activated' => 1, 'notify_types' => 4, 'notify_regularity' => 2)); // While we have members with board notifications while ($rowmember = $db->fetch_assoc($members)) { $email_perm = true; if (validateNotificationAccess($rowmember, $maillist, $email_perm) === false) { continue; } $langloaded = loadLanguage('index', empty($rowmember['lngfile']) || empty($modSettings['userLanguage']) ? $language : $rowmember['lngfile'], false); // Now loop through all the notifications to send for this board. if (empty($boards[$rowmember['id_board']])) { continue; } $sentOnceAlready = 0; // For each message we need to send (from this board to this member) foreach ($boards[$rowmember['id_board']] as $key) { // Don't notify the guy who started the topic! // @todo In this case actually send them a "it's approved hooray" email :P if ($topicData[$key]['poster'] == $rowmember['id_member']) { continue; } // Setup the string for adding the body to the message, if a user wants it. $send_body = $maillist || empty($modSettings['disallow_sendBody']) && !empty($rowmember['notify_send_body']); $replacements = array('TOPICSUBJECT' => $topicData[$key]['subject'], 'POSTERNAME' => un_htmlspecialchars($topicData[$key]['name']), 'TOPICLINK' => $scripturl . '?topic=' . $topicData[$key]['topic'] . '.new#new', 'TOPICLINKNEW' => $scripturl . '?topic=' . $topicData[$key]['topic'] . '.new#new', 'MESSAGE' => $send_body ? $topicData[$key]['body'] : '', 'UNSUBSCRIBELINK' => $scripturl . '?action=notifyboard;board=' . $topicData[$key]['board'] . '.0', 'SIGNATURE' => !empty($topicData[$key]['signature']) ? $topicData[$key]['signature'] : '', 'BOARDNAME' => $board_names[$topicData[$key]['board']]['name']); // Figure out which email to send $emailtype = ''; // Send only if once is off or it's on and it hasn't been sent. if (!empty($rowmember['notify_regularity']) && !$sentOnceAlready && empty($rowmember['sent'])) { $emailtype = 'notify_boards_once'; } elseif (empty($rowmember['notify_regularity'])) { $emailtype = 'notify_boards'; } if (!empty($emailtype)) { $emailtype .= $send_body ? '_body' : ''; $emaildata = loadEmailTemplate(($maillist && $email_perm && $send_body ? 'pbe_' : '') . $emailtype, $replacements, $langloaded); $emailname = !empty($topicData[$key]['name']) ? un_htmlspecialchars($topicData[$key]['name']) : null; // Maillist style? if ($maillist && $email_perm && $send_body) { // Add in the from wrapper and trigger sendmail to add in a security key $from_wrapper = !empty($modSettings['maillist_mail_from']) ? $modSettings['maillist_mail_from'] : (empty($modSettings['maillist_sitename_address']) ? $webmaster_email : $modSettings['maillist_sitename_address']); sendmail($rowmember['email_address'], $emaildata['subject'], $emaildata['body'], $emailname, 't' . $topicData[$key]['topic'], false, 3, null, false, $from_wrapper, $topicData[$key]['topic']); } else { sendmail($rowmember['email_address'], $emaildata['subject'], $emaildata['body'], null, null, false, 3); } } $sentOnceAlready = 1; } } $db->free_result($members); loadLanguage('index', $user_info['language']); // Sent! $db->query('', ' UPDATE {db_prefix}log_notify SET sent = {int:is_sent} WHERE id_board IN ({array_int:board_list}) AND id_member != {int:current_member}', array('current_member' => $user_info['id'], 'board_list' => $board_index, 'is_sent' => 1)); }
/** * Gather the results and show them. * * What it does: * - checks user input and searches the messages table for messages matching the query. * - requires the search_posts permission. * - uses the results sub template of the Search template. * - uses the Search language file. * - stores the results into the search cache. * - show the results of the search query. */ public function action_results() { global $scripturl, $modSettings, $txt; global $user_info, $context, $options, $messages_request, $boards_can; global $excludedWords, $participants; // We shouldn't be working with the db, but we do :P $db = database(); $db_search = db_search(); // No, no, no... this is a bit hard on the server, so don't you go prefetching it! if (isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch') { @ob_end_clean(); header('HTTP/1.1 403 Forbidden'); die; } $this->_setup_weight_factors(); // These vars don't require an interface, they're just here for tweaking. $recentPercentage = 0.3; $humungousTopicPosts = 200; $maxMembersToSearch = 500; $maxMessageResults = empty($modSettings['search_max_results']) ? 0 : $modSettings['search_max_results'] * 5; // Start with no errors. $context['search_errors'] = array(); // Number of pages hard maximum - normally not set at all. $modSettings['search_max_results'] = empty($modSettings['search_max_results']) ? 200 * $modSettings['search_results_per_page'] : (int) $modSettings['search_max_results']; // Maximum length of the string. $context['search_string_limit'] = 100; loadLanguage('Search'); if (!isset($_REQUEST['xml'])) { loadTemplate('Search'); } else { $context['sub_template'] = 'results'; } // Are you allowed? isAllowedTo('search_posts'); require_once CONTROLLERDIR . '/Display.controller.php'; require_once SUBSDIR . '/Package.subs.php'; require_once SUBSDIR . '/Search.subs.php'; // Load up the search API we are going to use. $searchAPI = findSearchAPI(); // $search_params will carry all settings that differ from the default search parameters. // That way, the URLs involved in a search page will be kept as short as possible. $search_params = array(); if (isset($_REQUEST['params'])) { // Due to IE's 2083 character limit, we have to compress long search strings $temp_params = base64_decode(str_replace(array('-', '_', '.'), array('+', '/', '='), $_REQUEST['params'])); // Test for gzuncompress failing $temp_params2 = @gzuncompress($temp_params); $temp_params = explode('|"|', !empty($temp_params2) ? $temp_params2 : $temp_params); foreach ($temp_params as $i => $data) { @(list($k, $v) = explode('|\'|', $data)); $search_params[$k] = $v; } if (isset($search_params['brd'])) { $search_params['brd'] = empty($search_params['brd']) ? array() : explode(',', $search_params['brd']); } } // Store whether simple search was used (needed if the user wants to do another query). if (!isset($search_params['advanced'])) { $search_params['advanced'] = empty($_REQUEST['advanced']) ? 0 : 1; } // 1 => 'allwords' (default, don't set as param) / 2 => 'anywords'. if (!empty($search_params['searchtype']) || !empty($_REQUEST['searchtype']) && $_REQUEST['searchtype'] == 2) { $search_params['searchtype'] = 2; } // Minimum age of messages. Default to zero (don't set param in that case). if (!empty($search_params['minage']) || !empty($_REQUEST['minage']) && $_REQUEST['minage'] > 0) { $search_params['minage'] = !empty($search_params['minage']) ? (int) $search_params['minage'] : (int) $_REQUEST['minage']; } // Maximum age of messages. Default to infinite (9999 days: param not set). if (!empty($search_params['maxage']) || !empty($_REQUEST['maxage']) && $_REQUEST['maxage'] < 9999) { $search_params['maxage'] = !empty($search_params['maxage']) ? (int) $search_params['maxage'] : (int) $_REQUEST['maxage']; } // Searching a specific topic? if (!empty($_REQUEST['topic']) || !empty($_REQUEST['search_selection']) && $_REQUEST['search_selection'] == 'topic') { $search_params['topic'] = empty($_REQUEST['search_selection']) ? (int) $_REQUEST['topic'] : (isset($_REQUEST['sd_topic']) ? (int) $_REQUEST['sd_topic'] : ''); $search_params['show_complete'] = true; } elseif (!empty($search_params['topic'])) { $search_params['topic'] = (int) $search_params['topic']; } if (!empty($search_params['minage']) || !empty($search_params['maxage'])) { $request = $db->query('', ' SELECT ' . (empty($search_params['maxage']) ? '0, ' : 'IFNULL(MIN(id_msg), -1), ') . (empty($search_params['minage']) ? '0' : 'IFNULL(MAX(id_msg), -1)') . ' FROM {db_prefix}messages WHERE 1=1' . ($modSettings['postmod_active'] ? ' AND approved = {int:is_approved_true}' : '') . (empty($search_params['minage']) ? '' : ' AND poster_time <= {int:timestamp_minimum_age}') . (empty($search_params['maxage']) ? '' : ' AND poster_time >= {int:timestamp_maximum_age}'), array('timestamp_minimum_age' => empty($search_params['minage']) ? 0 : time() - 86400 * $search_params['minage'], 'timestamp_maximum_age' => empty($search_params['maxage']) ? 0 : time() - 86400 * $search_params['maxage'], 'is_approved_true' => 1)); list($minMsgID, $maxMsgID) = $db->fetch_row($request); if ($minMsgID < 0 || $maxMsgID < 0) { $context['search_errors']['no_messages_in_time_frame'] = true; } $db->free_result($request); } // Default the user name to a wildcard matching every user (*). if (!empty($search_params['userspec']) || !empty($_REQUEST['userspec']) && $_REQUEST['userspec'] != '*') { $search_params['userspec'] = isset($search_params['userspec']) ? $search_params['userspec'] : $_REQUEST['userspec']; } // If there's no specific user, then don't mention it in the main query. if (empty($search_params['userspec'])) { $userQuery = ''; } else { $userString = strtr(Util::htmlspecialchars($search_params['userspec'], ENT_QUOTES), array('"' => '"')); $userString = strtr($userString, array('%' => '\\%', '_' => '\\_', '*' => '%', '?' => '_')); preg_match_all('~"([^"]+)"~', $userString, $matches); $possible_users = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $userString))); for ($k = 0, $n = count($possible_users); $k < $n; $k++) { $possible_users[$k] = trim($possible_users[$k]); if (strlen($possible_users[$k]) == 0) { unset($possible_users[$k]); } } // Create a list of database-escaped search names. $realNameMatches = array(); foreach ($possible_users as $possible_user) { $realNameMatches[] = $db->quote('{string:possible_user}', array('possible_user' => $possible_user)); } // Retrieve a list of possible members. $request = $db->query('', ' SELECT id_member FROM {db_prefix}members WHERE {raw:match_possible_users}', array('match_possible_users' => 'real_name LIKE ' . implode(' OR real_name LIKE ', $realNameMatches))); // Simply do nothing if there're too many members matching the criteria. if ($db->num_rows($request) > $maxMembersToSearch) { $userQuery = ''; } elseif ($db->num_rows($request) == 0) { $userQuery = $db->quote('m.id_member = {int:id_member_guest} AND ({raw:match_possible_guest_names})', array('id_member_guest' => 0, 'match_possible_guest_names' => 'm.poster_name LIKE ' . implode(' OR m.poster_name LIKE ', $realNameMatches))); } else { $memberlist = array(); while ($row = $db->fetch_assoc($request)) { $memberlist[] = $row['id_member']; } $userQuery = $db->quote('(m.id_member IN ({array_int:matched_members}) OR (m.id_member = {int:id_member_guest} AND ({raw:match_possible_guest_names})))', array('matched_members' => $memberlist, 'id_member_guest' => 0, 'match_possible_guest_names' => 'm.poster_name LIKE ' . implode(' OR m.poster_name LIKE ', $realNameMatches))); } $db->free_result($request); } // Ensure that boards are an array of integers (or nothing). if (!empty($search_params['brd']) && is_array($search_params['brd'])) { $query_boards = array_map('intval', $search_params['brd']); } elseif (!empty($_REQUEST['brd']) && is_array($_REQUEST['brd'])) { $query_boards = array_map('intval', $_REQUEST['brd']); } elseif (!empty($_REQUEST['brd'])) { $query_boards = array_map('intval', explode(',', $_REQUEST['brd'])); } elseif (!empty($_REQUEST['sd_brd']) && is_array($_REQUEST['sd_brd'])) { $query_boards = array_map('intval', $_REQUEST['sd_brd']); } elseif (isset($_REQUEST['sd_brd']) && (int) $_REQUEST['sd_brd'] !== 0) { $query_boards = array((int) $_REQUEST['sd_brd']); } else { $query_boards = array(); } // Special case for boards: searching just one topic? if (!empty($search_params['topic'])) { $request = $db->query('', ' SELECT b.id_board FROM {db_prefix}topics AS t INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board) WHERE t.id_topic = {int:search_topic_id} AND {query_see_board}' . ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved_true}' : '') . ' LIMIT 1', array('search_topic_id' => $search_params['topic'], 'is_approved_true' => 1)); if ($db->num_rows($request) == 0) { fatal_lang_error('topic_gone', false); } $search_params['brd'] = array(); list($search_params['brd'][0]) = $db->fetch_row($request); $db->free_result($request); } elseif ($user_info['is_admin'] && (!empty($search_params['advanced']) || !empty($query_boards))) { $search_params['brd'] = $query_boards; } else { require_once SUBSDIR . '/Boards.subs.php'; $search_params['brd'] = array_keys(fetchBoardsInfo(array('boards' => $query_boards), array('include_recycle' => false, 'include_redirects' => false, 'wanna_see_board' => empty($search_params['advanced'])))); // This error should pro'bly only happen for hackers. if (empty($search_params['brd'])) { $context['search_errors']['no_boards_selected'] = true; } } if (count($search_params['brd']) != 0) { foreach ($search_params['brd'] as $k => $v) { $search_params['brd'][$k] = (int) $v; } // If we've selected all boards, this parameter can be left empty. require_once SUBSDIR . '/Boards.subs.php'; $num_boards = countBoards(); if (count($search_params['brd']) == $num_boards) { $boardQuery = ''; } elseif (count($search_params['brd']) == $num_boards - 1 && !empty($modSettings['recycle_board']) && !in_array($modSettings['recycle_board'], $search_params['brd'])) { $boardQuery = '!= ' . $modSettings['recycle_board']; } else { $boardQuery = 'IN (' . implode(', ', $search_params['brd']) . ')'; } } else { $boardQuery = ''; } $search_params['show_complete'] = !empty($search_params['show_complete']) || !empty($_REQUEST['show_complete']); $search_params['subject_only'] = !empty($search_params['subject_only']) || !empty($_REQUEST['subject_only']); $context['compact'] = !$search_params['show_complete']; // Get the sorting parameters right. Default to sort by relevance descending. $sort_columns = array('relevance', 'num_replies', 'id_msg'); call_integration_hook('integrate_search_sort_columns', array(&$sort_columns)); if (empty($search_params['sort']) && !empty($_REQUEST['sort'])) { list($search_params['sort'], $search_params['sort_dir']) = array_pad(explode('|', $_REQUEST['sort']), 2, ''); } $search_params['sort'] = !empty($search_params['sort']) && in_array($search_params['sort'], $sort_columns) ? $search_params['sort'] : 'relevance'; if (!empty($search_params['topic']) && $search_params['sort'] === 'num_replies') { $search_params['sort'] = 'id_msg'; } // Sorting direction: descending unless stated otherwise. $search_params['sort_dir'] = !empty($search_params['sort_dir']) && $search_params['sort_dir'] == 'asc' ? 'asc' : 'desc'; // Determine some values needed to calculate the relevance. $minMsg = (int) ((1 - $recentPercentage) * $modSettings['maxMsgID']); $recentMsg = $modSettings['maxMsgID'] - $minMsg; // *** Parse the search query call_integration_hook('integrate_search_params', array(&$search_params)); // Unfortunately, searching for words like this is going to be slow, so we're blacklisting them. // @todo Setting to add more here? // @todo Maybe only blacklist if they are the only word, or "any" is used? $blacklisted_words = array('img', 'url', 'quote', 'www', 'http', 'the', 'is', 'it', 'are', 'if'); call_integration_hook('integrate_search_blacklisted_words', array(&$blacklisted_words)); // What are we searching for? if (empty($search_params['search'])) { if (isset($_GET['search'])) { $search_params['search'] = un_htmlspecialchars($_GET['search']); } elseif (isset($_POST['search'])) { $search_params['search'] = $_POST['search']; } else { $search_params['search'] = ''; } } // Nothing?? if (!isset($search_params['search']) || $search_params['search'] == '') { $context['search_errors']['invalid_search_string'] = true; } elseif (Util::strlen($search_params['search']) > $context['search_string_limit']) { $context['search_errors']['string_too_long'] = true; } // Change non-word characters into spaces. $stripped_query = preg_replace('~(?:[\\x0B\\0\\x{A0}\\t\\r\\s\\n(){}\\[\\]<>!@$%^*.,:+=`\\~\\?/\\\\]+|&(?:amp|lt|gt|quot);)+~u', ' ', $search_params['search']); // Make the query lower case. It's gonna be case insensitive anyway. $stripped_query = un_htmlspecialchars(Util::strtolower($stripped_query)); // This (hidden) setting will do fulltext searching in the most basic way. if (!empty($modSettings['search_simple_fulltext'])) { $stripped_query = strtr($stripped_query, array('"' => '')); } $no_regexp = preg_match('~&#(?:\\d{1,7}|x[0-9a-fA-F]{1,6});~', $stripped_query) === 1; // Extract phrase parts first (e.g. some words "this is a phrase" some more words.) preg_match_all('/(?:^|\\s)([-]?)"([^"]+)"(?:$|\\s)/', $stripped_query, $matches, PREG_PATTERN_ORDER); $phraseArray = $matches[2]; // Remove the phrase parts and extract the words. $wordArray = preg_replace('~(?:^|\\s)(?:[-]?)"(?:[^"]+)"(?:$|\\s)~u', ' ', $search_params['search']); $wordArray = explode(' ', Util::htmlspecialchars(un_htmlspecialchars($wordArray), ENT_QUOTES)); // A minus sign in front of a word excludes the word.... so... $excludedWords = array(); $excludedIndexWords = array(); $excludedSubjectWords = array(); $excludedPhrases = array(); // .. first, we check for things like -"some words", but not "-some words". foreach ($matches[1] as $index => $word) { if ($word === '-') { if (($word = trim($phraseArray[$index], '-_\' ')) !== '' && !in_array($word, $blacklisted_words)) { $excludedWords[] = $word; } unset($phraseArray[$index]); } } // Now we look for -test, etc.... normaller. foreach ($wordArray as $index => $word) { if (strpos(trim($word), '-') === 0) { if (($word = trim($word, '-_\' ')) !== '' && !in_array($word, $blacklisted_words)) { $excludedWords[] = $word; } unset($wordArray[$index]); } } // The remaining words and phrases are all included. $searchArray = array_merge($phraseArray, $wordArray); // This is used to remember words that will be ignored (because too short usually) $context['search_ignored'] = array(); // Trim everything and make sure there are no words that are the same. foreach ($searchArray as $index => $value) { // Skip anything practically empty. if (($searchArray[$index] = trim($value, '-_\' ')) === '') { unset($searchArray[$index]); } elseif (in_array($searchArray[$index], $blacklisted_words)) { $foundBlackListedWords = true; unset($searchArray[$index]); } elseif (Util::strlen($value) < 2) { $context['search_ignored'][] = $value; unset($searchArray[$index]); } else { $searchArray[$index] = $searchArray[$index]; } } $searchArray = array_slice(array_unique($searchArray), 0, 10); // Create an array of replacements for highlighting. $context['mark'] = array(); foreach ($searchArray as $word) { $context['mark'][$word] = '<strong class="highlight">' . $word . '</strong>'; } // Initialize two arrays storing the words that have to be searched for. $orParts = array(); $searchWords = array(); // Make sure at least one word is being searched for. if (empty($searchArray)) { if (!empty($context['search_ignored'])) { $context['search_errors']['search_string_small_words'] = true; } else { $context['search_errors']['invalid_search_string' . (!empty($foundBlackListedWords) ? '_blacklist' : '')] = true; } } elseif (empty($search_params['searchtype'])) { $orParts[0] = $searchArray; } else { foreach ($searchArray as $index => $value) { $orParts[$index] = array($value); } } // Don't allow duplicate error messages if one string is too short. if (isset($context['search_errors']['search_string_small_words'], $context['search_errors']['invalid_search_string'])) { unset($context['search_errors']['invalid_search_string']); } // Make sure the excluded words are in all or-branches. foreach ($orParts as $orIndex => $andParts) { foreach ($excludedWords as $word) { $orParts[$orIndex][] = $word; } } // Determine the or-branches and the fulltext search words. foreach ($orParts as $orIndex => $andParts) { $searchWords[$orIndex] = array('indexed_words' => array(), 'words' => array(), 'subject_words' => array(), 'all_words' => array(), 'complex_words' => array()); // Sort the indexed words (large words -> small words -> excluded words). if ($searchAPI->supportsMethod('searchSort')) { usort($orParts[$orIndex], 'searchSort'); } foreach ($orParts[$orIndex] as $word) { $is_excluded = in_array($word, $excludedWords); $searchWords[$orIndex]['all_words'][] = $word; $subjectWords = text2words($word); if (!$is_excluded || count($subjectWords) === 1) { $searchWords[$orIndex]['subject_words'] = array_merge($searchWords[$orIndex]['subject_words'], $subjectWords); if ($is_excluded) { $excludedSubjectWords = array_merge($excludedSubjectWords, $subjectWords); } } else { $excludedPhrases[] = $word; } // Have we got indexes to prepare? if ($searchAPI->supportsMethod('prepareIndexes')) { $searchAPI->prepareIndexes($word, $searchWords[$orIndex], $excludedIndexWords, $is_excluded); } } // Search_force_index requires all AND parts to have at least one fulltext word. if (!empty($modSettings['search_force_index']) && empty($searchWords[$orIndex]['indexed_words'])) { $context['search_errors']['query_not_specific_enough'] = true; break; } elseif ($search_params['subject_only'] && empty($searchWords[$orIndex]['subject_words']) && empty($excludedSubjectWords)) { $context['search_errors']['query_not_specific_enough'] = true; break; } else { $searchWords[$orIndex]['indexed_words'] = array_slice($searchWords[$orIndex]['indexed_words'], 0, 7); $searchWords[$orIndex]['subject_words'] = array_slice($searchWords[$orIndex]['subject_words'], 0, 7); $searchWords[$orIndex]['words'] = array_slice($searchWords[$orIndex]['words'], 0, 4); } } // *** Spell checking? if (!empty($modSettings['enableSpellChecking']) && function_exists('pspell_new')) { $this->_load_suggestions($search_params, $searchArray); } // Let the user adjust the search query, should they wish? $context['search_params'] = $search_params; if (isset($context['search_params']['search'])) { $context['search_params']['search'] = Util::htmlspecialchars($context['search_params']['search']); } if (isset($context['search_params']['userspec'])) { $context['search_params']['userspec'] = Util::htmlspecialchars($context['search_params']['userspec']); } if (empty($context['search_params']['minage'])) { $context['search_params']['minage'] = 0; } if (empty($context['search_params']['maxage'])) { $context['search_params']['maxage'] = 9999; } $context['search_params'] = $this->_fill_default_search_params($context['search_params']); // Do we have captcha enabled? if ($user_info['is_guest'] && !empty($modSettings['search_enable_captcha']) && empty($_SESSION['ss_vv_passed']) && (empty($_SESSION['last_ss']) || $_SESSION['last_ss'] != $search_params['search'])) { // If we come from another search box tone down the error... if (!isset($_REQUEST['search_vv'])) { $context['search_errors']['need_verification_code'] = true; } else { require_once SUBSDIR . '/VerificationControls.class.php'; $verificationOptions = array('id' => 'search'); $context['require_verification'] = create_control_verification($verificationOptions, true); if (is_array($context['require_verification'])) { foreach ($context['require_verification'] as $error) { $context['search_errors'][$error] = true; } } else { $_SESSION['ss_vv_passed'] = true; } } } // *** Encode all search params // All search params have been checked, let's compile them to a single string... made less simple by PHP 4.3.9 and below. $temp_params = $search_params; if (isset($temp_params['brd'])) { $temp_params['brd'] = implode(',', $temp_params['brd']); } $context['params'] = array(); foreach ($temp_params as $k => $v) { $context['params'][] = $k . '|\'|' . $v; } if (!empty($context['params'])) { // Due to old IE's 2083 character limit, we have to compress long search strings $params = @gzcompress(implode('|"|', $context['params'])); // Gzcompress failed, use try non-gz if (empty($params)) { $params = implode('|"|', $context['params']); } // Base64 encode, then replace +/= with uri safe ones that can be reverted $context['params'] = str_replace(array('+', '/', '='), array('-', '_', '.'), base64_encode($params)); } // ... and add the links to the link tree. $context['linktree'][] = array('url' => $scripturl . '?action=search;params=' . $context['params'], 'name' => $txt['search']); $context['linktree'][] = array('url' => $scripturl . '?action=search;sa=results;params=' . $context['params'], 'name' => $txt['search_results']); // Start guest off collapsed if ($context['user']['is_guest'] && !isset($context['minmax_preferences']['asearch'])) { $context['minmax_preferences']['asearch'] = 1; } // *** A last error check call_integration_hook('integrate_search_errors'); // One or more search errors? Go back to the first search screen. if (!empty($context['search_errors'])) { $_REQUEST['params'] = $context['params']; return $this->action_search(); } // Spam me not, Spam-a-lot? if (empty($_SESSION['last_ss']) || $_SESSION['last_ss'] != $search_params['search']) { spamProtection('search'); } // Store the last search string to allow pages of results to be browsed. $_SESSION['last_ss'] = $search_params['search']; // *** Reserve an ID for caching the search results. $query_params = array_merge($search_params, array('min_msg_id' => isset($minMsgID) ? (int) $minMsgID : 0, 'max_msg_id' => isset($maxMsgID) ? (int) $maxMsgID : 0, 'memberlist' => !empty($memberlist) ? $memberlist : array())); // Can this search rely on the API given the parameters? if ($searchAPI->supportsMethod('searchQuery', $query_params)) { $participants = array(); $searchArray = array(); $num_results = $searchAPI->searchQuery($query_params, $searchWords, $excludedIndexWords, $participants, $searchArray); } else { $update_cache = empty($_SESSION['search_cache']) || $_SESSION['search_cache']['params'] != $context['params']; if ($update_cache) { // Increase the pointer... $modSettings['search_pointer'] = empty($modSettings['search_pointer']) ? 0 : (int) $modSettings['search_pointer']; // ...and store it right off. updateSettings(array('search_pointer' => $modSettings['search_pointer'] >= 255 ? 0 : $modSettings['search_pointer'] + 1)); // As long as you don't change the parameters, the cache result is yours. $_SESSION['search_cache'] = array('id_search' => $modSettings['search_pointer'], 'num_results' => -1, 'params' => $context['params']); // Clear the previous cache of the final results cache. $db_search->search_query('delete_log_search_results', ' DELETE FROM {db_prefix}log_search_results WHERE id_search = {int:search_id}', array('search_id' => $_SESSION['search_cache']['id_search'])); if ($search_params['subject_only']) { // We do this to try and avoid duplicate keys on databases not supporting INSERT IGNORE. $inserts = array(); foreach ($searchWords as $orIndex => $words) { $subject_query_params = array(); $subject_query = array('from' => '{db_prefix}topics AS t', 'inner_join' => array(), 'left_join' => array(), 'where' => array()); if ($modSettings['postmod_active']) { $subject_query['where'][] = 't.approved = {int:is_approved}'; } $numTables = 0; $prev_join = 0; $numSubjectResults = 0; foreach ($words['subject_words'] as $subjectWord) { $numTables++; if (in_array($subjectWord, $excludedSubjectWords)) { $subject_query['left_join'][] = '{db_prefix}log_search_subjects AS subj' . $numTables . ' ON (subj' . $numTables . '.word ' . (empty($modSettings['search_match_words']) ? 'LIKE {string:subject_words_' . $numTables . '_wild}' : '= {string:subject_words_' . $numTables . '}') . ' AND subj' . $numTables . '.id_topic = t.id_topic)'; $subject_query['where'][] = '(subj' . $numTables . '.word IS NULL)'; } else { $subject_query['inner_join'][] = '{db_prefix}log_search_subjects AS subj' . $numTables . ' ON (subj' . $numTables . '.id_topic = ' . ($prev_join === 0 ? 't' : 'subj' . $prev_join) . '.id_topic)'; $subject_query['where'][] = 'subj' . $numTables . '.word ' . (empty($modSettings['search_match_words']) ? 'LIKE {string:subject_words_' . $numTables . '_wild}' : '= {string:subject_words_' . $numTables . '}'); $prev_join = $numTables; } $subject_query_params['subject_words_' . $numTables] = $subjectWord; $subject_query_params['subject_words_' . $numTables . '_wild'] = '%' . $subjectWord . '%'; } if (!empty($userQuery)) { if ($subject_query['from'] != '{db_prefix}messages AS m') { $subject_query['inner_join'][] = '{db_prefix}messages AS m ON (m.id_topic = t.id_topic)'; } $subject_query['where'][] = $userQuery; } if (!empty($search_params['topic'])) { $subject_query['where'][] = 't.id_topic = ' . $search_params['topic']; } if (!empty($minMsgID)) { $subject_query['where'][] = 't.id_first_msg >= ' . $minMsgID; } if (!empty($maxMsgID)) { $subject_query['where'][] = 't.id_last_msg <= ' . $maxMsgID; } if (!empty($boardQuery)) { $subject_query['where'][] = 't.id_board ' . $boardQuery; } if (!empty($excludedPhrases)) { if ($subject_query['from'] != '{db_prefix}messages AS m') { $subject_query['inner_join'][] = '{db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)'; } $count = 0; foreach ($excludedPhrases as $phrase) { $subject_query['where'][] = 'm.subject NOT ' . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:excluded_phrases_' . $count . '}'; $subject_query_params['excluded_phrases_' . $count++] = empty($modSettings['search_match_words']) || $no_regexp ? '%' . strtr($phrase, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\\[\\]$.+*?|{}()])/'), array('[$1]'), $phrase), '\\\'') . '[[:>:]]'; } } call_integration_hook('integrate_subject_only_search_query', array(&$subject_query, &$subject_query_params)); // Build the search relevance query $relevance = '1000 * ('; foreach ($this->_weight_factors as $type => $value) { if (isset($value['results'])) { $relevance .= $this->_weight[$type]; if (!empty($value['results'])) { $relevance .= ' * ' . $value['results']; } $relevance .= ' + '; } } $relevance = substr($relevance, 0, -3) . ') / ' . $this->_weight_total . ' AS relevance'; $ignoreRequest = $db_search->search_query('insert_log_search_results_subject', ($db->support_ignore() ? ' INSERT IGNORE INTO {db_prefix}log_search_results (id_search, id_topic, relevance, id_msg, num_matches)' : '') . ' SELECT {int:id_search}, t.id_topic, ' . $relevance . ', ' . (empty($userQuery) ? 't.id_first_msg' : 'm.id_msg') . ', 1 FROM ' . $subject_query['from'] . (empty($subject_query['inner_join']) ? '' : ' INNER JOIN ' . implode(' INNER JOIN ', $subject_query['inner_join'])) . (empty($subject_query['left_join']) ? '' : ' LEFT JOIN ' . implode(' LEFT JOIN ', $subject_query['left_join'])) . ' WHERE ' . implode(' AND ', $subject_query['where']) . (empty($modSettings['search_max_results']) ? '' : ' LIMIT ' . ($modSettings['search_max_results'] - $numSubjectResults)), array_merge($subject_query_params, array('id_search' => $_SESSION['search_cache']['id_search'], 'min_msg' => $minMsg, 'recent_message' => $recentMsg, 'huge_topic_posts' => $humungousTopicPosts, 'is_approved' => 1))); // If the database doesn't support IGNORE to make this fast we need to do some tracking. if (!$db->support_ignore()) { while ($row = $db->fetch_row($ignoreRequest)) { // No duplicates! if (isset($inserts[$row[1]])) { continue; } foreach ($row as $key => $value) { $inserts[$row[1]][] = (int) $row[$key]; } } $db->free_result($ignoreRequest); $numSubjectResults = count($inserts); } else { $numSubjectResults += $db->affected_rows(); } if (!empty($modSettings['search_max_results']) && $numSubjectResults >= $modSettings['search_max_results']) { break; } } // If there's data to be inserted for non-IGNORE databases do it here! if (!empty($inserts)) { $db->insert('', '{db_prefix}log_search_results', array('id_search' => 'int', 'id_topic' => 'int', 'relevance' => 'int', 'id_msg' => 'int', 'num_matches' => 'int'), $inserts, array('id_search', 'id_topic')); } $_SESSION['search_cache']['num_results'] = $numSubjectResults; } else { $main_query = array('select' => array('id_search' => $_SESSION['search_cache']['id_search'], 'relevance' => '0'), 'weights' => array(), 'from' => '{db_prefix}topics AS t', 'inner_join' => array('{db_prefix}messages AS m ON (m.id_topic = t.id_topic)'), 'left_join' => array(), 'where' => array(), 'group_by' => array(), 'parameters' => array('min_msg' => $minMsg, 'recent_message' => $recentMsg, 'huge_topic_posts' => $humungousTopicPosts, 'is_approved' => 1)); if (empty($search_params['topic']) && empty($search_params['show_complete'])) { $main_query['select']['id_topic'] = 't.id_topic'; $main_query['select']['id_msg'] = 'MAX(m.id_msg) AS id_msg'; $main_query['select']['num_matches'] = 'COUNT(*) AS num_matches'; $main_query['weights'] = $this->_weight_factors; $main_query['group_by'][] = 't.id_topic'; } else { // This is outrageous! $main_query['select']['id_topic'] = 'm.id_msg AS id_topic'; $main_query['select']['id_msg'] = 'm.id_msg'; $main_query['select']['num_matches'] = '1 AS num_matches'; $main_query['weights'] = array('age' => array('search' => '((m.id_msg - t.id_first_msg) / CASE WHEN t.id_last_msg = t.id_first_msg THEN 1 ELSE t.id_last_msg - t.id_first_msg END)'), 'first_message' => array('search' => 'CASE WHEN m.id_msg = t.id_first_msg THEN 1 ELSE 0 END')); if (!empty($search_params['topic'])) { $main_query['where'][] = 't.id_topic = {int:topic}'; $main_query['parameters']['topic'] = $search_params['topic']; } if (!empty($search_params['show_complete'])) { $main_query['group_by'][] = 'm.id_msg, t.id_first_msg, t.id_last_msg'; } } // *** Get the subject results. $numSubjectResults = 0; if (empty($search_params['topic'])) { $inserts = array(); // Create a temporary table to store some preliminary results in. $db_search->search_query('drop_tmp_log_search_topics', ' DROP TABLE IF EXISTS {db_prefix}tmp_log_search_topics', array('db_error_skip' => true)); $createTemporary = $db_search->search_query('create_tmp_log_search_topics', ' CREATE TEMPORARY TABLE {db_prefix}tmp_log_search_topics ( id_topic mediumint(8) unsigned NOT NULL default {string:string_zero}, PRIMARY KEY (id_topic) ) ENGINE=MEMORY', array('string_zero' => '0', 'db_error_skip' => true)) !== false; // Clean up some previous cache. if (!$createTemporary) { $db_search->search_query('delete_log_search_topics', ' DELETE FROM {db_prefix}log_search_topics WHERE id_search = {int:search_id}', array('search_id' => $_SESSION['search_cache']['id_search'])); } foreach ($searchWords as $orIndex => $words) { $subject_query = array('from' => '{db_prefix}topics AS t', 'inner_join' => array(), 'left_join' => array(), 'where' => array(), 'params' => array()); $numTables = 0; $prev_join = 0; $count = 0; $excluded = false; foreach ($words['subject_words'] as $subjectWord) { $numTables++; if (in_array($subjectWord, $excludedSubjectWords)) { if ($subject_query['from'] != '{db_prefix}messages AS m' && !$excluded) { $subject_query['inner_join'][] = '{db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)'; $excluded = true; } $subject_query['left_join'][] = '{db_prefix}log_search_subjects AS subj' . $numTables . ' ON (subj' . $numTables . '.word ' . (empty($modSettings['search_match_words']) ? 'LIKE {string:subject_not_' . $count . '}' : '= {string:subject_not_' . $count . '}') . ' AND subj' . $numTables . '.id_topic = t.id_topic)'; $subject_query['params']['subject_not_' . $count] = empty($modSettings['search_match_words']) ? '%' . $subjectWord . '%' : $subjectWord; $subject_query['where'][] = '(subj' . $numTables . '.word IS NULL)'; $subject_query['where'][] = 'm.body NOT ' . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:body_not_' . $count . '}'; $subject_query['params']['body_not_' . $count++] = empty($modSettings['search_match_words']) || $no_regexp ? '%' . strtr($subjectWord, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\\[\\]$.+*?|{}()])/'), array('[$1]'), $subjectWord), '\\\'') . '[[:>:]]'; } else { $subject_query['inner_join'][] = '{db_prefix}log_search_subjects AS subj' . $numTables . ' ON (subj' . $numTables . '.id_topic = ' . ($prev_join === 0 ? 't' : 'subj' . $prev_join) . '.id_topic)'; $subject_query['where'][] = 'subj' . $numTables . '.word LIKE {string:subject_like_' . $count . '}'; $subject_query['params']['subject_like_' . $count++] = empty($modSettings['search_match_words']) ? '%' . $subjectWord . '%' : $subjectWord; $prev_join = $numTables; } } if (!empty($userQuery)) { if ($subject_query['from'] != '{db_prefix}messages AS m') { $subject_query['inner_join'][] = '{db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)'; } $subject_query['where'][] = '{raw:user_query}'; $subject_query['params']['user_query'] = $userQuery; } if (!empty($search_params['topic'])) { $subject_query['where'][] = 't.id_topic = {int:topic}'; $subject_query['params']['topic'] = $search_params['topic']; } if (!empty($minMsgID)) { $subject_query['where'][] = 't.id_first_msg >= {int:min_msg_id}'; $subject_query['params']['min_msg_id'] = $minMsgID; } if (!empty($maxMsgID)) { $subject_query['where'][] = 't.id_last_msg <= {int:max_msg_id}'; $subject_query['params']['max_msg_id'] = $maxMsgID; } if (!empty($boardQuery)) { $subject_query['where'][] = 't.id_board {raw:board_query}'; $subject_query['params']['board_query'] = $boardQuery; } if (!empty($excludedPhrases)) { if ($subject_query['from'] != '{db_prefix}messages AS m') { $subject_query['inner_join'][] = '{db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)'; } $count = 0; foreach ($excludedPhrases as $phrase) { $subject_query['where'][] = 'm.subject NOT ' . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:exclude_phrase_' . $count . '}'; $subject_query['where'][] = 'm.body NOT ' . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:exclude_phrase_' . $count . '}'; $subject_query['params']['exclude_phrase_' . $count++] = empty($modSettings['search_match_words']) || $no_regexp ? '%' . strtr($phrase, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\\[\\]$.+*?|{}()])/'), array('[$1]'), $phrase), '\\\'') . '[[:>:]]'; } } call_integration_hook('integrate_subject_search_query', array(&$subject_query)); // Nothing to search for? if (empty($subject_query['where'])) { continue; } $ignoreRequest = $db_search->search_query('insert_log_search_topics', ($db->support_ignore() ? ' INSERT IGNORE INTO {db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_topics (' . ($createTemporary ? '' : 'id_search, ') . 'id_topic)' : '') . ' SELECT ' . ($createTemporary ? '' : $_SESSION['search_cache']['id_search'] . ', ') . 't.id_topic FROM ' . $subject_query['from'] . (empty($subject_query['inner_join']) ? '' : ' INNER JOIN ' . implode(' INNER JOIN ', $subject_query['inner_join'])) . (empty($subject_query['left_join']) ? '' : ' LEFT JOIN ' . implode(' LEFT JOIN ', $subject_query['left_join'])) . ' WHERE ' . implode(' AND ', $subject_query['where']) . (empty($modSettings['search_max_results']) ? '' : ' LIMIT ' . ($modSettings['search_max_results'] - $numSubjectResults)), $subject_query['params']); // Don't do INSERT IGNORE? Manually fix this up! if (!$db->support_ignore()) { while ($row = $db->fetch_row($ignoreRequest)) { $ind = $createTemporary ? 0 : 1; // No duplicates! if (isset($inserts[$row[$ind]])) { continue; } $inserts[$row[$ind]] = $row; } $db->free_result($ignoreRequest); $numSubjectResults = count($inserts); } else { $numSubjectResults += $db->affected_rows(); } if (!empty($modSettings['search_max_results']) && $numSubjectResults >= $modSettings['search_max_results']) { break; } } // Got some non-MySQL data to plonk in? if (!empty($inserts)) { $db->insert('', '{db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_topics', $createTemporary ? array('id_topic' => 'int') : array('id_search' => 'int', 'id_topic' => 'int'), $inserts, $createTemporary ? array('id_topic') : array('id_search', 'id_topic')); } if ($numSubjectResults !== 0) { $main_query['weights']['subject']['search'] = 'CASE WHEN MAX(lst.id_topic) IS NULL THEN 0 ELSE 1 END'; $main_query['left_join'][] = '{db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_topics AS lst ON (' . ($createTemporary ? '' : 'lst.id_search = {int:id_search} AND ') . 'lst.id_topic = t.id_topic)'; if (!$createTemporary) { $main_query['parameters']['id_search'] = $_SESSION['search_cache']['id_search']; } } } $indexedResults = 0; // We building an index? if ($searchAPI->supportsMethod('indexedWordQuery', $query_params)) { $inserts = array(); $db_search->search_query('drop_tmp_log_search_messages', ' DROP TABLE IF EXISTS {db_prefix}tmp_log_search_messages', array('db_error_skip' => true)); $createTemporary = $db_search->search_query('create_tmp_log_search_messages', ' CREATE TEMPORARY TABLE {db_prefix}tmp_log_search_messages ( id_msg int(10) unsigned NOT NULL default {string:string_zero}, PRIMARY KEY (id_msg) ) ENGINE=MEMORY', array('string_zero' => '0', 'db_error_skip' => true)) !== false; // Clear, all clear! if (!$createTemporary) { $db_search->search_query('delete_log_search_messages', ' DELETE FROM {db_prefix}log_search_messages WHERE id_search = {int:id_search}', array('id_search' => $_SESSION['search_cache']['id_search'])); } foreach ($searchWords as $orIndex => $words) { // Search for this word, assuming we have some words! if (!empty($words['indexed_words'])) { // Variables required for the search. $search_data = array('insert_into' => ($createTemporary ? 'tmp_' : '') . 'log_search_messages', 'no_regexp' => $no_regexp, 'max_results' => $maxMessageResults, 'indexed_results' => $indexedResults, 'params' => array('id_search' => !$createTemporary ? $_SESSION['search_cache']['id_search'] : 0, 'excluded_words' => $excludedWords, 'user_query' => !empty($userQuery) ? $userQuery : '', 'board_query' => !empty($boardQuery) ? $boardQuery : '', 'topic' => !empty($search_params['topic']) ? $search_params['topic'] : 0, 'min_msg_id' => !empty($minMsgID) ? $minMsgID : 0, 'max_msg_id' => !empty($maxMsgID) ? $maxMsgID : 0, 'excluded_phrases' => !empty($excludedPhrases) ? $excludedPhrases : array(), 'excluded_index_words' => !empty($excludedIndexWords) ? $excludedIndexWords : array(), 'excluded_subject_words' => !empty($excludedSubjectWords) ? $excludedSubjectWords : array())); $ignoreRequest = $searchAPI->indexedWordQuery($words, $search_data); if (!$db->support_ignore()) { while ($row = $db->fetch_row($ignoreRequest)) { // No duplicates! if (isset($inserts[$row[0]])) { continue; } $inserts[$row[0]] = $row; } $db->free_result($ignoreRequest); $indexedResults = count($inserts); } else { $indexedResults += $db->affected_rows(); } if (!empty($maxMessageResults) && $indexedResults >= $maxMessageResults) { break; } } } // More non-MySQL stuff needed? if (!empty($inserts)) { $db->insert('', '{db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_messages', $createTemporary ? array('id_msg' => 'int') : array('id_msg' => 'int', 'id_search' => 'int'), $inserts, $createTemporary ? array('id_msg') : array('id_msg', 'id_search')); } if (empty($indexedResults) && empty($numSubjectResults) && !empty($modSettings['search_force_index'])) { $context['search_errors']['query_not_specific_enough'] = true; $_REQUEST['params'] = $context['params']; return $this->action_search(); } elseif (!empty($indexedResults)) { $main_query['inner_join'][] = '{db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_messages AS lsm ON (lsm.id_msg = m.id_msg)'; if (!$createTemporary) { $main_query['where'][] = 'lsm.id_search = {int:id_search}'; $main_query['parameters']['id_search'] = $_SESSION['search_cache']['id_search']; } } } else { $orWhere = array(); $count = 0; foreach ($searchWords as $orIndex => $words) { $where = array(); foreach ($words['all_words'] as $regularWord) { $where[] = 'm.body' . (in_array($regularWord, $excludedWords) ? ' NOT' : '') . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:all_word_body_' . $count . '}'; if (in_array($regularWord, $excludedWords)) { $where[] = 'm.subject NOT' . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:all_word_body_' . $count . '}'; } $main_query['parameters']['all_word_body_' . $count++] = empty($modSettings['search_match_words']) || $no_regexp ? '%' . strtr($regularWord, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\\[\\]$.+*?|{}()])/'), array('[$1]'), $regularWord), '\\\'') . '[[:>:]]'; } if (!empty($where)) { $orWhere[] = count($where) > 1 ? '(' . implode(' AND ', $where) . ')' : $where[0]; } } if (!empty($orWhere)) { $main_query['where'][] = count($orWhere) > 1 ? '(' . implode(' OR ', $orWhere) . ')' : $orWhere[0]; } if (!empty($userQuery)) { $main_query['where'][] = '{raw:user_query}'; $main_query['parameters']['user_query'] = $userQuery; } if (!empty($search_params['topic'])) { $main_query['where'][] = 'm.id_topic = {int:topic}'; $main_query['parameters']['topic'] = $search_params['topic']; } if (!empty($minMsgID)) { $main_query['where'][] = 'm.id_msg >= {int:min_msg_id}'; $main_query['parameters']['min_msg_id'] = $minMsgID; } if (!empty($maxMsgID)) { $main_query['where'][] = 'm.id_msg <= {int:max_msg_id}'; $main_query['parameters']['max_msg_id'] = $maxMsgID; } if (!empty($boardQuery)) { $main_query['where'][] = 'm.id_board {raw:board_query}'; $main_query['parameters']['board_query'] = $boardQuery; } } call_integration_hook('integrate_main_search_query', array(&$main_query)); // Did we either get some indexed results, or otherwise did not do an indexed query? if (!empty($indexedResults) || !$searchAPI->supportsMethod('indexedWordQuery', $query_params)) { $relevance = '1000 * ('; $new_weight_total = 0; foreach ($main_query['weights'] as $type => $value) { $relevance .= $this->_weight[$type]; if (!empty($value['search'])) { $relevance .= ' * ' . $value['search']; } $relevance .= ' + '; $new_weight_total += $this->_weight[$type]; } $main_query['select']['relevance'] = substr($relevance, 0, -3) . ') / ' . $new_weight_total . ' AS relevance'; $ignoreRequest = $db_search->search_query('insert_log_search_results_no_index', ($db->support_ignore() ? ' INSERT IGNORE INTO ' . '{db_prefix}log_search_results (' . implode(', ', array_keys($main_query['select'])) . ')' : '') . ' SELECT ' . implode(', ', $main_query['select']) . ' FROM ' . $main_query['from'] . (empty($main_query['inner_join']) ? '' : ' INNER JOIN ' . implode(' INNER JOIN ', $main_query['inner_join'])) . (empty($main_query['left_join']) ? '' : ' LEFT JOIN ' . implode(' LEFT JOIN ', $main_query['left_join'])) . (!empty($main_query['where']) ? ' WHERE ' : '') . implode(' AND ', $main_query['where']) . (empty($main_query['group_by']) ? '' : ' GROUP BY ' . implode(', ', $main_query['group_by'])) . (empty($modSettings['search_max_results']) ? '' : ' LIMIT ' . $modSettings['search_max_results']), $main_query['parameters']); // We love to handle non-good databases that don't support our ignore! if (!$db->support_ignore()) { $inserts = array(); while ($row = $db->fetch_row($ignoreRequest)) { // No duplicates! if (isset($inserts[$row[2]])) { continue; } foreach ($row as $key => $value) { $inserts[$row[2]][] = (int) $row[$key]; } } $db->free_result($ignoreRequest); // Now put them in! if (!empty($inserts)) { $query_columns = array(); foreach ($main_query['select'] as $k => $v) { $query_columns[$k] = 'int'; } $db->insert('', '{db_prefix}log_search_results', $query_columns, $inserts, array('id_search', 'id_topic')); } $_SESSION['search_cache']['num_results'] += count($inserts); } else { $_SESSION['search_cache']['num_results'] = $db->affected_rows(); } } // Insert subject-only matches. if ($_SESSION['search_cache']['num_results'] < $modSettings['search_max_results'] && $numSubjectResults !== 0) { $relevance = '1000 * ('; foreach ($this->_weight_factors as $type => $value) { if (isset($value['results'])) { $relevance .= $this->_weight[$type]; if (!empty($value['results'])) { $relevance .= ' * ' . $value['results']; } $relevance .= ' + '; } } $relevance = substr($relevance, 0, -3) . ') / ' . $this->_weight_total . ' AS relevance'; $usedIDs = array_flip(empty($inserts) ? array() : array_keys($inserts)); $ignoreRequest = $db_search->search_query('insert_log_search_results_sub_only', ($db->support_ignore() ? ' INSERT IGNORE INTO {db_prefix}log_search_results (id_search, id_topic, relevance, id_msg, num_matches)' : '') . ' SELECT {int:id_search}, t.id_topic, ' . $relevance . ', t.id_first_msg, 1 FROM {db_prefix}topics AS t INNER JOIN {db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_topics AS lst ON (lst.id_topic = t.id_topic)' . ($createTemporary ? '' : ' WHERE lst.id_search = {int:id_search}') . (empty($modSettings['search_max_results']) ? '' : ' LIMIT ' . ($modSettings['search_max_results'] - $_SESSION['search_cache']['num_results'])), array('id_search' => $_SESSION['search_cache']['id_search'], 'min_msg' => $minMsg, 'recent_message' => $recentMsg, 'huge_topic_posts' => $humungousTopicPosts)); // Once again need to do the inserts if the database don't support ignore! if (!$db->support_ignore()) { $inserts = array(); while ($row = $db->fetch_row($ignoreRequest)) { // No duplicates! if (isset($usedIDs[$row[1]])) { continue; } $usedIDs[$row[1]] = true; $inserts[] = $row; } $db->free_result($ignoreRequest); // Now put them in! if (!empty($inserts)) { $db->insert('', '{db_prefix}log_search_results', array('id_search' => 'int', 'id_topic' => 'int', 'relevance' => 'float', 'id_msg' => 'int', 'num_matches' => 'int'), $inserts, array('id_search', 'id_topic')); } $_SESSION['search_cache']['num_results'] += count($inserts); } else { $_SESSION['search_cache']['num_results'] += $db->affected_rows(); } } elseif ($_SESSION['search_cache']['num_results'] == -1) { $_SESSION['search_cache']['num_results'] = 0; } } } // *** Retrieve the results to be shown on the page $participants = array(); $request = $db_search->search_query('', ' SELECT ' . (empty($search_params['topic']) ? 'lsr.id_topic' : $search_params['topic'] . ' AS id_topic') . ', lsr.id_msg, lsr.relevance, lsr.num_matches FROM {db_prefix}log_search_results AS lsr' . ($search_params['sort'] == 'num_replies' ? ' INNER JOIN {db_prefix}topics AS t ON (t.id_topic = lsr.id_topic)' : '') . ' WHERE lsr.id_search = {int:id_search} ORDER BY ' . $search_params['sort'] . ' ' . $search_params['sort_dir'] . ' LIMIT ' . (int) $_REQUEST['start'] . ', ' . $modSettings['search_results_per_page'], array('id_search' => $_SESSION['search_cache']['id_search'])); while ($row = $db->fetch_assoc($request)) { $context['topics'][$row['id_msg']] = array('relevance' => round($row['relevance'] / 10, 1) . '%', 'num_matches' => $row['num_matches'], 'matches' => array()); // By default they didn't participate in the topic! $participants[$row['id_topic']] = false; } $db->free_result($request); $num_results = $_SESSION['search_cache']['num_results']; } if (!empty($context['topics'])) { // Create an array for the permissions. $boards_can = boardsAllowedTo(array('post_reply_own', 'post_reply_any', 'mark_any_notify'), true, false); // How's about some quick moderation? if (!empty($options['display_quick_mod'])) { $boards_can = array_merge($boards_can, boardsAllowedTo(array('lock_any', 'lock_own', 'make_sticky', 'move_any', 'move_own', 'remove_any', 'remove_own', 'merge_any'), true, false)); $context['can_lock'] = in_array(0, $boards_can['lock_any']); $context['can_sticky'] = in_array(0, $boards_can['make_sticky']) && !empty($modSettings['enableStickyTopics']); $context['can_move'] = in_array(0, $boards_can['move_any']); $context['can_remove'] = in_array(0, $boards_can['remove_any']); $context['can_merge'] = in_array(0, $boards_can['merge_any']); } // What messages are we using? $msg_list = array_keys($context['topics']); // Load the posters... $request = $db->query('', ' SELECT id_member FROM {db_prefix}messages WHERE id_member != {int:no_member} AND id_msg IN ({array_int:message_list}) LIMIT ' . count($context['topics']), array('message_list' => $msg_list, 'no_member' => 0)); $posters = array(); while ($row = $db->fetch_assoc($request)) { $posters[] = $row['id_member']; } $db->free_result($request); call_integration_hook('integrate_search_message_list', array(&$msg_list, &$posters)); if (!empty($posters)) { loadMemberData(array_unique($posters)); } // Get the messages out for the callback - select enough that it can be made to look just like Display. $messages_request = $db->query('', ' SELECT m.id_msg, m.subject, m.poster_name, m.poster_email, m.poster_time, m.id_member, m.icon, m.poster_ip, m.body, m.smileys_enabled, m.modified_time, m.modified_name, first_m.id_msg AS first_msg, first_m.subject AS first_subject, first_m.icon AS first_icon, first_m.poster_time AS first_poster_time, first_mem.id_member AS first_member_id, IFNULL(first_mem.real_name, first_m.poster_name) AS first_member_name, last_m.id_msg AS last_msg, last_m.poster_time AS last_poster_time, last_mem.id_member AS last_member_id, IFNULL(last_mem.real_name, last_m.poster_name) AS last_member_name, last_m.icon AS last_icon, last_m.subject AS last_subject, t.id_topic, t.is_sticky, t.locked, t.id_poll, t.num_replies, t.num_views, t.num_likes, b.id_board, b.name AS board_name, c.id_cat, c.name AS cat_name FROM {db_prefix}messages AS m INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic) INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board) INNER JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat) INNER JOIN {db_prefix}messages AS first_m ON (first_m.id_msg = t.id_first_msg) INNER JOIN {db_prefix}messages AS last_m ON (last_m.id_msg = t.id_last_msg) LEFT JOIN {db_prefix}members AS first_mem ON (first_mem.id_member = first_m.id_member) LEFT JOIN {db_prefix}members AS last_mem ON (last_mem.id_member = first_m.id_member) WHERE m.id_msg IN ({array_int:message_list})' . ($modSettings['postmod_active'] ? ' AND m.approved = {int:is_approved}' : '') . ' ORDER BY FIND_IN_SET(m.id_msg, {string:message_list_in_set}) LIMIT {int:limit}', array('message_list' => $msg_list, 'is_approved' => 1, 'message_list_in_set' => implode(',', $msg_list), 'limit' => count($context['topics']))); // If there are no results that means the things in the cache got deleted, so pretend we have no topics anymore. if ($db->num_rows($messages_request) == 0) { $context['topics'] = array(); } // If we want to know who participated in what then load this now. if (!empty($modSettings['enableParticipation']) && !$user_info['is_guest']) { require_once SUBSDIR . '/MessageIndex.subs.php'; $topics_participated_in = topicsParticipation($user_info['id'], array_keys($participants)); foreach ($topics_participated_in as $topic) { $participants[$topic['id_topic']] = true; } } } // Now that we know how many results to expect we can start calculating the page numbers. $context['page_index'] = constructPageIndex($scripturl . '?action=search;sa=results;params=' . $context['params'], $_REQUEST['start'], $num_results, $modSettings['search_results_per_page'], false); // Consider the search complete! if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 2) { cache_put_data('search_start:' . ($user_info['is_guest'] ? $user_info['ip'] : $user_info['id']), null, 90); } $context['key_words'] =& $searchArray; // Setup the default topic icons... for checking they exist and the like! require_once SUBSDIR . '/MessageIndex.subs.php'; $context['icon_sources'] = MessageTopicIcons(); $context['sub_template'] = 'results'; $context['page_title'] = $txt['search_results']; $context['get_topics'] = array($this, 'prepareSearchContext_callback'); $context['jump_to'] = array('label' => addslashes(un_htmlspecialchars($txt['jump_to'])), 'board_name' => addslashes(un_htmlspecialchars($txt['select_destination']))); }
/** * Remove one or more categories. * general function to delete one or more categories. * allows to move all boards in the categories to a different category before deleting them. * if moveBoardsTo is set to null, all boards inside the given categories will be deleted. * deletes all information that's associated with the given categories. * updates the statistics to reflect the new situation. * * @param int[] $categories * @param integer|null $moveBoardsTo = null */ function deleteCategories($categories, $moveBoardsTo = null) { global $cat_tree; $db = database(); require_once SUBSDIR . '/Boards.subs.php'; getBoardTree(); call_integration_hook('integrate_delete_category', array($categories, &$moveBoardsTo)); // With no category set to move the boards to, delete them all. if ($moveBoardsTo === null) { $boards_inside = array_keys(fetchBoardsInfo(array('categories' => $categories))); if (!empty($boards_inside)) { deleteBoards($boards_inside, null); } } elseif (in_array($moveBoardsTo, $categories)) { trigger_error('deleteCategories(): You cannot move the boards to a category that\'s being deleted', E_USER_ERROR); } else { $db->query('', ' UPDATE {db_prefix}boards SET id_cat = {int:new_parent_cat} WHERE id_cat IN ({array_int:category_list})', array('category_list' => $categories, 'new_parent_cat' => $moveBoardsTo)); } // No one will ever be able to collapse these categories anymore. $db->query('', ' DELETE FROM {db_prefix}collapsed_categories WHERE id_cat IN ({array_int:category_list})', array('category_list' => $categories)); // Do the deletion of the category itself $db->query('', ' DELETE FROM {db_prefix}categories WHERE id_cat IN ({array_int:category_list})', array('category_list' => $categories)); // Log what we've done. foreach ($categories as $category) { logAction('delete_cat', array('catname' => $cat_tree[$category]['node']['name']), 'admin'); } // Get all boards back into the right order. reorderBoards(); }
/** * Send out a daily email of all subscribed topics, to members. * * - It sends notifications about replies or new topics, * and moderation actions. */ public function daily_digest() { global $is_weekly, $txt, $mbname, $scripturl, $modSettings, $boardurl; $db = database(); // We'll want this... require_once SUBSDIR . '/Mail.subs.php'; loadEssentialThemeData(); // If the maillist function is on then so is the enhanced digest $maillist = !empty($modSettings['maillist_enabled']) && !empty($modSettings['maillist_digest_enabled']); if ($maillist) { require_once SUBSDIR . '/Emailpost.subs.php'; } $is_weekly = !empty($is_weekly) ? 1 : 0; // Right - get all the notification data FIRST. $request = $db->query('', ' SELECT ln.id_topic, COALESCE(t.id_board, ln.id_board) AS id_board, mem.email_address, mem.member_name, mem.real_name, mem.notify_types, mem.lngfile, mem.id_member FROM {db_prefix}log_notify AS ln INNER JOIN {db_prefix}members AS mem ON (mem.id_member = ln.id_member) LEFT JOIN {db_prefix}topics AS t ON (ln.id_topic != {int:empty_topic} AND t.id_topic = ln.id_topic) WHERE mem.notify_regularity = {int:notify_regularity} AND mem.is_activated = {int:is_activated}', array('empty_topic' => 0, 'notify_regularity' => $is_weekly ? '3' : '2', 'is_activated' => 1)); $members = array(); $langs = array(); $notify = array(); $boards = array(); while ($row = $db->fetch_assoc($request)) { if (!isset($members[$row['id_member']])) { $members[$row['id_member']] = array('email' => $row['email_address'], 'name' => $row['real_name'] == '' ? $row['member_name'] : un_htmlspecialchars($row['real_name']), 'id' => $row['id_member'], 'notifyMod' => $row['notify_types'] < 3 ? true : false, 'lang' => $row['lngfile']); $langs[$row['lngfile']] = $row['lngfile']; } // Store this useful data! $boards[$row['id_board']] = $row['id_board']; if ($row['id_topic']) { $notify['topics'][$row['id_topic']][] = $row['id_member']; } else { $notify['boards'][$row['id_board']][] = $row['id_member']; } } $db->free_result($request); if (empty($boards)) { return true; } // Just get the board names. require_once SUBSDIR . '/Boards.subs.php'; $boards = fetchBoardsInfo(array('boards' => $boards), array('override_permissions' => true)); if (empty($boards)) { return true; } // Get the actual topics... $request = $db->query('', ' SELECT ld.note_type, t.id_topic, t.id_board, t.id_member_started, m.id_msg, m.subject, m.body, ld.id_msg AS last_reply, b.name AS board_name, ml.body as last_body FROM {db_prefix}log_digest AS ld INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ld.id_topic AND t.id_board IN ({array_int:board_list})) INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg) INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = ld.id_msg) INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board) WHERE ' . ($is_weekly ? 'ld.daily != {int:daily_value}' : 'ld.daily IN (0, 2)'), array('board_list' => array_keys($boards), 'daily_value' => 2)); $types = array(); while ($row = $db->fetch_assoc($request)) { if (!isset($types[$row['note_type']][$row['id_board']])) { $types[$row['note_type']][$row['id_board']] = array('lines' => array(), 'name' => un_htmlspecialchars($row['board_name']), 'id' => $row['id_board']); } // A reply has been made if ($row['note_type'] === 'reply') { // More than one reply to this topic? if (isset($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']])) { $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['count']++; // keep track of the highest numbered reply and body text for this topic ... if ($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['body_id'] < $row['last_reply']) { $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['body_id'] = $row['last_reply']; $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['body_text'] = $row['last_body']; } } else { // First time we have seen a reply to this topic, so load our array $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']] = array('id' => $row['id_topic'], 'subject' => un_htmlspecialchars($row['subject']), 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.new;topicseen#new', 'count' => 1, 'body_id' => $row['last_reply'], 'body_text' => $row['last_body']); } } elseif ($row['note_type'] === 'topic') { if ($maillist) { // Convert to markdown markup e.g. text ;) pbe_prepare_text($row['body']); $row['body'] = Util::shorten_text($row['body'], !empty($modSettings['digest_preview_length']) ? $modSettings['digest_preview_length'] : 375, true); $row['body'] = preg_replace("~\n~s", "\n ", $row['body']); } // Topics are simple since we are only concerned with the first post if (!isset($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']])) { $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']] = array('id' => $row['id_topic'], 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.new;topicseen#new', 'subject' => un_htmlspecialchars($row['subject']), 'body' => $row['body']); } } elseif ($maillist && empty($modSettings['pbe_no_mod_notices'])) { if (!isset($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']])) { $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']] = array('id' => $row['id_topic'], 'subject' => un_htmlspecialchars($row['subject']), 'starter' => $row['id_member_started']); } } $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['members'] = array(); if (!empty($notify['topics'][$row['id_topic']])) { $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['members'] = array_merge($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['members'], $notify['topics'][$row['id_topic']]); } if (!empty($notify['boards'][$row['id_board']])) { $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['members'] = array_merge($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['members'], $notify['boards'][$row['id_board']]); } } $db->free_result($request); if (empty($types)) { return true; } // Fix the last reply message so its suitable for previewing if ($maillist) { foreach ($types['reply'] as $id => $board) { foreach ($board['lines'] as $topic) { // Replace the body array with the appropriate preview message $body = $types['reply'][$id]['lines'][$topic['id']]['body_text']; pbe_prepare_text($body); $body = Util::shorten_text($body, !empty($modSettings['digest_preview_length']) ? $modSettings['digest_preview_length'] : 375, true); $body = preg_replace("~\n~s", "\n ", $body); $types['reply'][$id]['lines'][$topic['id']]['body'] = $body; unset($types['reply'][$id]['lines'][$topic['id']]['body_text'], $body); } } } // Let's load all the languages into a cache thingy. $langtxt = array(); foreach ($langs as $lang) { loadLanguage('Post', $lang); loadLanguage('index', $lang); loadLanguage('Maillist', $lang); loadLanguage('EmailTemplates', $lang); $langtxt[$lang] = array('subject' => $txt['digest_subject_' . ($is_weekly ? 'weekly' : 'daily')], 'char_set' => 'UTF-8', 'intro' => sprintf($txt['digest_intro_' . ($is_weekly ? 'weekly' : 'daily')], $mbname), 'new_topics' => $txt['digest_new_topics'], 'topic_lines' => $txt['digest_new_topics_line'], 'new_replies' => $txt['digest_new_replies'], 'mod_actions' => $txt['digest_mod_actions'], 'replies_one' => $txt['digest_new_replies_one'], 'replies_many' => $txt['digest_new_replies_many'], 'sticky' => $txt['digest_mod_act_sticky'], 'lock' => $txt['digest_mod_act_lock'], 'unlock' => $txt['digest_mod_act_unlock'], 'remove' => $txt['digest_mod_act_remove'], 'move' => $txt['digest_mod_act_move'], 'merge' => $txt['digest_mod_act_merge'], 'split' => $txt['digest_mod_act_split'], 'bye' => (!empty($modSettings['maillist_sitename_regards']) ? $modSettings['maillist_sitename_regards'] : '') . "\n" . $boardurl, 'preview' => $txt['digest_preview'], 'see_full' => $txt['digest_see_full'], 'reply_preview' => $txt['digest_reply_preview'], 'unread_reply_link' => $txt['digest_unread_reply_link']); } // Right - send out the silly things - this will take quite some space! foreach ($members as $mid => $member) { // Do the start stuff! $email = array('subject' => $mbname . ' - ' . $langtxt[$lang]['subject'], 'body' => $member['name'] . ',' . "\n\n" . $langtxt[$lang]['intro'] . "\n" . $scripturl . '?action=profile;area=notification;u=' . $member['id'] . "\n", 'email' => $member['email']); // All the new topics if (isset($types['topic'])) { $titled = false; // Each type contains a board ID and then a topic number foreach ($types['topic'] as $id => $board) { foreach ($board['lines'] as $topic) { // They have requested notification for new topics in this board if (in_array($mid, $topic['members'])) { // Start of the new topics with a heading bar if (!$titled) { $email['body'] .= "\n" . $langtxt[$lang]['new_topics'] . ':' . "\n" . str_repeat('-', 78); $titled = true; } $email['body'] .= "\n" . sprintf($langtxt[$lang]['topic_lines'], $topic['subject'], $board['name']); if ($maillist) { $email['body'] .= $langtxt[$lang]['preview'] . $topic['body'] . $langtxt[$lang]['see_full'] . $topic['link'] . "\n"; } } } } if ($titled) { $email['body'] .= "\n"; } } // What about replies? if (isset($types['reply'])) { $titled = false; // Each reply will have a board id and then a topic ID foreach ($types['reply'] as $id => $board) { foreach ($board['lines'] as $topic) { // This member wants notices on replys to this topic if (in_array($mid, $topic['members'])) { // First one in the section gets a nice heading if (!$titled) { $email['body'] .= "\n" . $langtxt[$lang]['new_replies'] . ':' . "\n" . str_repeat('-', 78); $titled = true; } $email['body'] .= "\n" . ($topic['count'] === 1 ? sprintf($langtxt[$lang]['replies_one'], $topic['subject']) : sprintf($langtxt[$lang]['replies_many'], $topic['count'], $topic['subject'])); if ($maillist) { $email['body'] .= $langtxt[$lang]['reply_preview'] . $topic['body'] . $langtxt[$lang]['unread_reply_link'] . $topic['link'] . "\n"; } } } } if ($titled) { $email['body'] .= "\n"; } } // Finally, moderation actions! $titled = false; foreach ($types as $note_type => $type) { if ($note_type === 'topic' || $note_type === 'reply') { continue; } foreach ($type as $id => $board) { foreach ($board['lines'] as $topic) { if (in_array($mid, $topic['members'])) { if (!$titled) { $email['body'] .= "\n" . $langtxt[$lang]['mod_actions'] . ':' . "\n" . str_repeat('-', 47); $titled = true; } $email['body'] .= "\n" . sprintf($langtxt[$lang][$note_type], $topic['subject']); } } } } if ($titled) { $email['body'] .= "\n"; } // Then just say our goodbyes! $email['body'] .= "\n\n" . $langtxt[$lang]['bye']; // Send it - low priority! sendmail($email['email'], $email['subject'], $email['body'], null, null, false, 4); } // Using the queue, do a final flush before we say thats all folks if (!empty($modSettings['mail_queue'])) { AddMailQueue(true); } // Clean up... if ($is_weekly) { $db->query('', ' DELETE FROM {db_prefix}log_digest WHERE daily != {int:not_daily}', array('not_daily' => 0)); $db->query('', ' UPDATE {db_prefix}log_digest SET daily = {int:daily_value} WHERE daily = {int:not_daily}', array('daily_value' => 2, 'not_daily' => 0)); } else { // Clear any only weekly ones, and stop us from sending daily again. $db->query('', ' DELETE FROM {db_prefix}log_digest WHERE daily = {int:daily_value}', array('daily_value' => 2)); $db->query('', ' UPDATE {db_prefix}log_digest SET daily = {int:both_value} WHERE daily = {int:no_value}', array('both_value' => 1, 'no_value' => 0)); } // Just in case the member changes their settings mark this as sent. $members = array_keys($members); $db->query('', ' UPDATE {db_prefix}log_notify SET sent = {int:is_sent} WHERE id_member IN ({array_int:member_list})', array('member_list' => $members, 'is_sent' => 1)); // Log we've done it... return true; }
/** * Create a new board and set its properties and position. * * - Allows (almost) the same options as the modifyBoard() function. * - With the option inherit_permissions set, the parent board permissions * will be inherited. * * @package Boards * @param mixed[] $boardOptions * @return int The new board id */ function createBoard($boardOptions) { global $boards; $db = database(); // Trigger an error if one of the required values is not set. if (!isset($boardOptions['board_name']) || trim($boardOptions['board_name']) == '' || !isset($boardOptions['move_to']) || !isset($boardOptions['target_category'])) { trigger_error('createBoard(): One or more of the required options is not set', E_USER_ERROR); } if (in_array($boardOptions['move_to'], array('child', 'before', 'after')) && !isset($boardOptions['target_board'])) { trigger_error('createBoard(): Target board is not set', E_USER_ERROR); } // Set every optional value to its default value. $boardOptions += array('posts_count' => true, 'override_theme' => false, 'board_theme' => 0, 'access_groups' => array(), 'board_description' => '', 'profile' => 1, 'moderators' => '', 'inherit_permissions' => true, 'dont_log' => true); $board_columns = array('id_cat' => 'int', 'name' => 'string-255', 'description' => 'string', 'board_order' => 'int', 'member_groups' => 'string', 'redirect' => 'string'); $board_parameters = array($boardOptions['target_category'], $boardOptions['board_name'], '', 0, '-1,0', ''); // Insert a board, the settings are dealt with later. $db->insert('', '{db_prefix}boards', $board_columns, $board_parameters, array('id_board')); $board_id = $db->insert_id('{db_prefix}boards', 'id_board'); if (empty($board_id)) { return 0; } // Change the board according to the given specifications. modifyBoard($board_id, $boardOptions); // Do we want the parent permissions to be inherited? if ($boardOptions['inherit_permissions']) { getBoardTree(); if (!empty($boards[$board_id]['parent'])) { $board_data = fetchBoardsInfo(array('boards' => $boards[$board_id]['parent']), array('selects' => 'permissions')); $db->query('', ' UPDATE {db_prefix}boards SET id_profile = {int:new_profile} WHERE id_board = {int:current_board}', array('new_profile' => $board_data[$boards[$board_id]['parent']]['id_profile'], 'current_board' => $board_id)); } } // Clean the data cache. clean_cache('data'); // Created it. logAction('add_board', array('board' => $board_id), 'admin'); // Here you are, a new board, ready to be spammed. return $board_id; }
/** * Retrieves a list of membergroups that are allowed to do the given * permission. (on the given board) * * - If board_id is not null, a board permission is assumed. * - The function takes different permission settings into account. * * @package Members * @param string $permission * @param integer|null $board_id = null * @return an array containing an array for the allowed membergroup ID's * and an array for the denied membergroup ID's. */ function groupsAllowedTo($permission, $board_id = null) { global $board_info; $db = database(); // Admins are allowed to do anything. $member_groups = array('allowed' => array(1), 'denied' => array()); // Assume we're dealing with regular permissions (like profile_view_own). if ($board_id === null) { $request = $db->query('', ' SELECT id_group, add_deny FROM {db_prefix}permissions WHERE permission = {string:permission}', array('permission' => $permission)); while ($row = $db->fetch_assoc($request)) { $member_groups[$row['add_deny'] === '1' ? 'allowed' : 'denied'][] = $row['id_group']; } $db->free_result($request); } else { // First get the profile of the given board. if (isset($board_info['id']) && $board_info['id'] == $board_id) { $profile_id = $board_info['profile']; } elseif ($board_id !== 0) { require_once SUBSDIR . '/Boards.subs.php'; $board_data = fetchBoardsInfo(array('boards' => $board_id), array('selects' => 'permissions')); if (empty($board_data)) { fatal_lang_error('no_board'); } $profile_id = $board_data[$board_id]['id_profile']; } else { $profile_id = 1; } $request = $db->query('', ' SELECT bp.id_group, bp.add_deny FROM {db_prefix}board_permissions AS bp WHERE bp.permission = {string:permission} AND bp.id_profile = {int:profile_id}', array('profile_id' => $profile_id, 'permission' => $permission)); while ($row = $db->fetch_assoc($request)) { $member_groups[$row['add_deny'] === '1' ? 'allowed' : 'denied'][] = $row['id_group']; } $db->free_result($request); } // Denied is never allowed. $member_groups['allowed'] = array_diff($member_groups['allowed'], $member_groups['denied']); return $member_groups; }
/** * Outputs xml data representing recent information or a profile. * * What it does: * - Can be passed 4 subactions which decide what is output: * 'recent' for recent posts, * 'news' for news topics, * 'members' for recently registered members, * 'profile' for a member's profile. * - To display a member's profile, a user id has to be given. (;u=1) e.g. ?action=.xml;sa=profile;u=1;type=atom * - Outputs an rss feed instead of a proprietary one if the 'type' $_GET * parameter is 'rss' or 'rss2'. * - Accessed via ?action=.xml * - Does not use any templates, sub templates, or template layers. * * @uses Stats language file. */ public function action_showfeed() { global $board, $board_info, $context, $scripturl, $boardurl, $txt, $modSettings, $user_info; global $forum_version, $cdata_override, $settings; // If it's not enabled, die. if (empty($modSettings['xmlnews_enable'])) { obExit(false); } loadLanguage('Stats'); $txt['xml_rss_desc'] = replaceBasicActionUrl($txt['xml_rss_desc']); // Default to latest 5. No more than whats defined in the ACP or 255 $limit = empty($modSettings['xmlnews_limit']) ? 5 : min($modSettings['xmlnews_limit'], 255); $this->_limit = empty($_GET['limit']) || (int) $_GET['limit'] < 1 ? $limit : min((int) $_GET['limit'], $limit); // Handle the cases where a board, boards, or category is asked for. $this->_query_this_board = '1=1'; $context['optimize_msg'] = array('highest' => 'm.id_msg <= b.id_last_msg'); if (!empty($_REQUEST['c']) && empty($board)) { $categories = array_map('intval', explode(',', $_REQUEST['c'])); if (count($categories) == 1) { require_once SUBSDIR . '/Categories.subs.php'; $feed_title = categoryName($categories[0]); $feed_title = ' - ' . strip_tags($feed_title); } require_once SUBSDIR . '/Boards.subs.php'; $boards_posts = boardsPosts(array(), $categories); $total_cat_posts = array_sum($boards_posts); $boards = array_keys($boards_posts); if (!empty($boards)) { $this->_query_this_board = 'b.id_board IN (' . implode(', ', $boards) . ')'; } // Try to limit the number of messages we look through. if ($total_cat_posts > 100 && $total_cat_posts > $modSettings['totalMessages'] / 15) { $context['optimize_msg']['lowest'] = 'm.id_msg >= ' . max(0, $modSettings['maxMsgID'] - 400 - $this->_limit * 5); } } elseif (!empty($_REQUEST['boards'])) { require_once SUBSDIR . '/Boards.subs.php'; $query_boards = array_map('intval', explode(',', $_REQUEST['boards'])); $boards_data = fetchBoardsInfo(array('boards' => $query_boards), array('selects' => 'detailed')); // Either the board specified doesn't exist or you have no access. $num_boards = count($boards_data); if ($num_boards == 0) { fatal_lang_error('no_board'); } $total_posts = 0; $boards = array_keys($boards_data); foreach ($boards_data as $row) { if ($num_boards == 1) { $feed_title = ' - ' . strip_tags($row['name']); } $total_posts += $row['num_posts']; } $this->_query_this_board = 'b.id_board IN (' . implode(', ', $boards) . ')'; // The more boards, the more we're going to look through... if ($total_posts > 100 && $total_posts > $modSettings['totalMessages'] / 12) { $context['optimize_msg']['lowest'] = 'm.id_msg >= ' . max(0, $modSettings['maxMsgID'] - 500 - $this->_limit * 5); } } elseif (!empty($board)) { require_once SUBSDIR . '/Boards.subs.php'; $boards_data = fetchBoardsInfo(array('boards' => $board), array('selects' => 'posts')); $feed_title = ' - ' . strip_tags($board_info['name']); $this->_query_this_board = 'b.id_board = ' . $board; // Try to look through just a few messages, if at all possible. if ($boards_data[$board]['num_posts'] > 80 && $boards_data[$board]['num_posts'] > $modSettings['totalMessages'] / 10) { $context['optimize_msg']['lowest'] = 'm.id_msg >= ' . max(0, $modSettings['maxMsgID'] - 600 - $this->_limit * 5); } } else { $this->_query_this_board = '{query_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? ' AND b.id_board != ' . $modSettings['recycle_board'] : ''); $context['optimize_msg']['lowest'] = 'm.id_msg >= ' . max(0, $modSettings['maxMsgID'] - 100 - $this->_limit * 5); } // If format isn't set, rss2 is default $xml_format = isset($_GET['type']) && in_array($_GET['type'], array('rss', 'rss2', 'atom', 'rdf', 'webslice')) ? $_GET['type'] : 'rss2'; // List all the different types of data they can pull. $subActions = array('recent' => array('action_xmlrecent'), 'news' => array('action_xmlnews'), 'members' => array('action_xmlmembers'), 'profile' => array('action_xmlprofile')); // Easy adding of sub actions call_integration_hook('integrate_xmlfeeds', array(&$subActions)); $subAction = isset($_GET['sa']) && isset($subActions[$_GET['sa']]) ? $_GET['sa'] : 'recent'; // Webslices doesn't do everything (yet? ever?) so for now only recent posts is allowed in that format if ($xml_format == 'webslice' && $subAction != 'recent') { $xml_format = 'rss2'; } elseif ($xml_format == 'webslice') { $context['user'] += $user_info; $cdata_override = true; loadTemplate('Xml'); } // We only want some information, not all of it. $cachekey = array($xml_format, $_GET['action'], $this->_limit, $subAction); foreach (array('board', 'boards', 'c') as $var) { if (isset($_REQUEST[$var])) { $cachekey[] = $_REQUEST[$var]; } } $cachekey = md5(serialize($cachekey) . (!empty($this->_query_this_board) ? $this->_query_this_board : '')); $cache_t = microtime(true); // Get the associative array representing the xml. if (!empty($modSettings['cache_enable']) && (!$user_info['is_guest'] || $modSettings['cache_enable'] >= 3)) { $xml = cache_get_data('xmlfeed-' . $xml_format . ':' . ($user_info['is_guest'] ? '' : $user_info['id'] . '-') . $cachekey, 240); } if (empty($xml)) { $xml = $this->{$subActions[$subAction][0]}($xml_format); if (!empty($modSettings['cache_enable']) && ($user_info['is_guest'] && $modSettings['cache_enable'] >= 3 || !$user_info['is_guest'] && microtime(true) - $cache_t > 0.2)) { cache_put_data('xmlfeed-' . $xml_format . ':' . ($user_info['is_guest'] ? '' : $user_info['id'] . '-') . $cachekey, $xml, 240); } } $feed_title = encode_special(strip_tags(un_htmlspecialchars($context['forum_name']) . (isset($feed_title) ? $feed_title : ''))); // This is an xml file.... @ob_end_clean(); if (!empty($modSettings['enableCompressedOutput'])) { ob_start('ob_gzhandler'); } else { ob_start(); } if (isset($_REQUEST['debug'])) { header('Content-Type: text/xml; charset=UTF-8'); } elseif ($xml_format == 'rss' || $xml_format == 'rss2' || $xml_format == 'webslice') { header('Content-Type: application/rss+xml; charset=UTF-8'); } elseif ($xml_format == 'atom') { header('Content-Type: application/atom+xml; charset=UTF-8'); } elseif ($xml_format == 'rdf') { header('Content-Type: ' . (isBrowser('ie') ? 'text/xml' : 'application/rdf+xml') . '; charset=UTF-8'); } // First, output the xml header. echo '<?xml version="1.0" encoding="UTF-8"?' . '>'; // Are we outputting an rss feed or one with more information? if ($xml_format == 'rss' || $xml_format == 'rss2') { // Start with an RSS 2.0 header. echo ' <rss version=', $xml_format == 'rss2' ? '"2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"' : '"0.92"', ' xml:lang="', strtr($txt['lang_locale'], '_', '-'), '"> <channel> <title>', $feed_title, '</title> <link>', $scripturl, '</link> <description><![CDATA[', un_htmlspecialchars(strip_tags($txt['xml_rss_desc'])), ']]></description> <generator>ElkArte</generator> <ttl>30</ttl> <image> <url>', $settings['default_theme_url'], '/images/logo.png</url> <title>', $feed_title, '</title> <link>', $scripturl, '</link> </image>'; // Output all of the associative array, start indenting with 2 tabs, and name everything "item". dumpTags($xml, 2, 'item', $xml_format); // Output the footer of the xml. echo ' </channel> </rss>'; } elseif ($xml_format == 'webslice') { // Format specification http://msdn.microsoft.com/en-us/library/cc304073%28VS.85%29.aspx // Known browsers to support webslices: IE8, IE9, Firefox with Webchunks addon. // It uses RSS 2. // We send a feed with recent posts, and alerts for PMs for logged in users $context['recent_posts_data'] = $xml; $context['can_pm_read'] = allowedTo('pm_read'); // This always has RSS 2 echo ' <rss version="2.0" xmlns:mon="http://www.microsoft.com/schemas/rss/monitoring/2007" xml:lang="', strtr($txt['lang_locale'], '_', '-'), '"> <channel> <title>', $feed_title, ' - ', $txt['recent_posts'], '</title> <link>', $scripturl, '?action=recent</link> <description><![CDATA[', strip_tags($txt['xml_rss_desc']), ']]></description> <item> <title>', $feed_title, ' - ', $txt['recent_posts'], '</title> <link>', $scripturl, '?action=recent</link> <description><![CDATA[ ', template_webslice_header_above(), ' ', template_webslice_recent_posts(), ' ]]></description> </item> </channel> </rss>'; } elseif ($xml_format == 'atom') { $url_parts = array(); foreach (array('board', 'boards', 'c') as $var) { if (isset($_REQUEST[$var])) { $url_parts[] = $var . '=' . (is_array($_REQUEST[$var]) ? implode(',', $_REQUEST[$var]) : $_REQUEST[$var]); } } echo ' <feed xmlns="http://www.w3.org/2005/Atom"> <title>', $feed_title, '</title> <link rel="alternate" type="text/html" href="', $scripturl, '" /> <link rel="self" type="application/rss+xml" href="', $scripturl, '?type=atom;action=.xml', !empty($url_parts) ? ';' . implode(';', $url_parts) : '', '" /> <id>', $scripturl, '</id> <icon>', $boardurl, '/favicon.ico</icon> <updated>', gmstrftime('%Y-%m-%dT%H:%M:%SZ'), '</updated> <subtitle><![CDATA[', strip_tags(un_htmlspecialchars($txt['xml_rss_desc'])), ']]></subtitle> <generator uri="http://www.elkarte.net" version="', strtr($forum_version, array('ElkArte' => '')), '">ElkArte</generator> <author> <name>', strip_tags(un_htmlspecialchars($context['forum_name'])), '</name> </author>'; dumpTags($xml, 2, 'entry', $xml_format); echo ' </feed>'; } else { echo ' <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns="http://purl.org/rss/1.0/"> <channel rdf:about="', $scripturl, '"> <title>', $feed_title, '</title> <link>', $scripturl, '</link> <description><![CDATA[', strip_tags($txt['xml_rss_desc']), ']]></description> <items> <rdf:Seq>'; foreach ($xml as $item) { echo ' <rdf:li rdf:resource="', $item['link'], '" />'; } echo ' </rdf:Seq> </items> </channel> '; dumpTags($xml, 1, 'item', $xml_format); echo ' </rdf:RDF>'; } obExit(false); }
/** * Report for showing all the forum staff members - quite a feat! * functions ending with "Report" are responsible for generating data * for reporting. * they are all called from action_index. * never access the context directly, but use the data handling * functions to do so. */ public function action_staff() { global $txt; require_once SUBSDIR . '/Members.subs.php'; require_once SUBSDIR . '/Boards.subs.php'; require_once SUBSDIR . '/Membergroups.subs.php'; // Fetch all the board names. $boards = fetchBoardsInfo('all'); $moderators = allBoardModerators(true); $boards_moderated = array(); foreach ($moderators as $id_member => $rows) { foreach ($rows as $row) { $boards_moderated[$id_member][] = $row['id_board']; } } // Get a list of global moderators (i.e. members with moderation powers). $global_mods = array_intersect(membersAllowedTo('moderate_board', 0), membersAllowedTo('approve_posts', 0), membersAllowedTo('remove_any', 0), membersAllowedTo('modify_any', 0)); // How about anyone else who is special? $allStaff = array_merge(membersAllowedTo('admin_forum'), membersAllowedTo('manage_membergroups'), membersAllowedTo('manage_permissions'), array_keys($moderators), $global_mods); // Make sure everyone is there once - no admin less important than any other! $allStaff = array_unique($allStaff); // This is a bit of a cop out - but we're protecting their forum, really! if (count($allStaff) > 300) { fatal_lang_error('report_error_too_many_staff'); } // Get all the possible membergroups! $all_groups = getBasicMembergroupData(array('all'), array(), null, false); $groups = array(0 => $txt['full_member']); foreach ($all_groups as $row) { $groups[$row['id']] = empty($row['online_color']) ? $row['name'] : '<span style="color: ' . $row['online_color'] . '">' . $row['name'] . '</span>'; } // All the fields we'll show. $staffSettings = array('position' => $txt['report_staff_position'], 'moderates' => $txt['report_staff_moderates'], 'posts' => $txt['report_staff_posts'], 'last_login' => $txt['report_staff_last_login']); // Do it in columns, it's just easier. setKeys('cols'); // Get the latest activated member's display name. $result = getBasicMemberData($allStaff, array('moderation' => true, 'sort' => 'real_name')); foreach ($result as $row) { // Each member gets their own table!. newTable($row['real_name'], '', 'left', 'auto', 'left', 200, 'center'); // First off, add in the side key. addData($staffSettings); // Create the main data array. $staffData = array('position' => isset($groups[$row['id_group']]) ? $groups[$row['id_group']] : $groups[0], 'posts' => $row['posts'], 'last_login' => standardTime($row['last_login']), 'moderates' => array()); // What do they moderate? if (in_array($row['id_member'], $global_mods)) { $staffData['moderates'] = '<em>' . $txt['report_staff_all_boards'] . '</em>'; } elseif (isset($boards_moderated[$row['id_member']])) { // Get the names foreach ($boards_moderated[$row['id_member']] as $board) { if (isset($boards[$board])) { $staffData['moderates'][] = $boards[$board]['name']; } } $staffData['moderates'] = implode(', ', $staffData['moderates']); } else { $staffData['moderates'] = '<em>' . $txt['report_staff_no_boards'] . '</em>'; } // Next add the main data. addData($staffData); } }
/** * Allows for moderation from the message index. * @todo refactor this... */ public function action_quickmod() { global $board, $user_info, $modSettings, $context; $db = database(); // Check the session = get or post. checkSession('request'); // Lets go straight to the restore area. if (isset($_REQUEST['qaction']) && $_REQUEST['qaction'] == 'restore' && !empty($_REQUEST['topics'])) { redirectexit('action=restoretopic;topics=' . implode(',', $_REQUEST['topics']) . ';' . $context['session_var'] . '=' . $context['session_id']); } if (isset($_SESSION['topicseen_cache'])) { $_SESSION['topicseen_cache'] = array(); } // This is going to be needed to send off the notifications and for updateLastMessages(). require_once SUBSDIR . '/Post.subs.php'; require_once SUBSDIR . '/Notification.subs.php'; // Process process process data. require_once SUBSDIR . '/Topic.subs.php'; // Remember the last board they moved things to. if (isset($_REQUEST['move_to'])) { $_SESSION['move_to_topic'] = array('move_to' => $_REQUEST['move_to'], 'redirect_topic' => !empty($_REQUEST['redirect_topic']) ? (int) $_REQUEST['redirect_topic'] : 0, 'redirect_expires' => !empty($_REQUEST['redirect_expires']) ? (int) $_REQUEST['redirect_expires'] : 0); } // Only a few possible actions. $possibleActions = array(); if (!empty($board)) { $boards_can = array('make_sticky' => allowedTo('make_sticky') ? array($board) : array(), 'move_any' => allowedTo('move_any') ? array($board) : array(), 'move_own' => allowedTo('move_own') ? array($board) : array(), 'remove_any' => allowedTo('remove_any') ? array($board) : array(), 'remove_own' => allowedTo('remove_own') ? array($board) : array(), 'lock_any' => allowedTo('lock_any') ? array($board) : array(), 'lock_own' => allowedTo('lock_own') ? array($board) : array(), 'merge_any' => allowedTo('merge_any') ? array($board) : array(), 'approve_posts' => allowedTo('approve_posts') ? array($board) : array()); $redirect_url = 'board=' . $board . '.' . $_REQUEST['start']; } else { $boards_can = boardsAllowedTo(array('make_sticky', 'move_any', 'move_own', 'remove_any', 'remove_own', 'lock_any', 'lock_own', 'merge_any', 'approve_posts'), true, false); $redirect_url = isset($_POST['redirect_url']) ? $_POST['redirect_url'] : (isset($_SESSION['old_url']) ? $_SESSION['old_url'] : ''); } if (!$user_info['is_guest']) { $possibleActions[] = 'markread'; } if (!empty($boards_can['make_sticky']) && !empty($modSettings['enableStickyTopics'])) { $possibleActions[] = 'sticky'; } if (!empty($boards_can['move_any']) || !empty($boards_can['move_own'])) { $possibleActions[] = 'move'; } if (!empty($boards_can['remove_any']) || !empty($boards_can['remove_own'])) { $possibleActions[] = 'remove'; } if (!empty($boards_can['lock_any']) || !empty($boards_can['lock_own'])) { $possibleActions[] = 'lock'; } if (!empty($boards_can['merge_any'])) { $possibleActions[] = 'merge'; } if (!empty($boards_can['approve_posts'])) { $possibleActions[] = 'approve'; } // Two methods: $_REQUEST['actions'] (id_topic => action), and $_REQUEST['topics'] and $_REQUEST['qaction']. // (if action is 'move', $_REQUEST['move_to'] or $_REQUEST['move_tos'][$topic] is used.) if (!empty($_REQUEST['topics'])) { // If the action isn't valid, just quit now. if (empty($_REQUEST['qaction']) || !in_array($_REQUEST['qaction'], $possibleActions)) { redirectexit($redirect_url); } // Merge requires all topics as one parameter and can be done at once. if ($_REQUEST['qaction'] == 'merge') { // Merge requires at least two topics. if (empty($_REQUEST['topics']) || count($_REQUEST['topics']) < 2) { redirectexit($redirect_url); } require_once CONTROLLERDIR . '/MergeTopics.controller.php'; $controller = new MergeTopics_Controller(); return $controller->action_mergeExecute($_REQUEST['topics']); } // Just convert to the other method, to make it easier. foreach ($_REQUEST['topics'] as $topic) { $_REQUEST['actions'][(int) $topic] = $_REQUEST['qaction']; } } // Weird... how'd you get here? if (empty($_REQUEST['actions'])) { redirectexit($redirect_url); } // Validate each action. $temp = array(); foreach ($_REQUEST['actions'] as $topic => $action) { if (in_array($action, $possibleActions)) { $temp[(int) $topic] = $action; } } $_REQUEST['actions'] = $temp; if (!empty($_REQUEST['actions'])) { // Find all topics... $topics_info = topicsDetails(array_keys($_REQUEST['actions'])); foreach ($topics_info as $row) { if (!empty($board)) { if ($row['id_board'] != $board || $modSettings['postmod_active'] && !$row['approved'] && !allowedTo('approve_posts')) { unset($_REQUEST['actions'][$row['id_topic']]); } } else { // Don't allow them to act on unapproved posts they can't see... if ($modSettings['postmod_active'] && !$row['approved'] && !in_array(0, $boards_can['approve_posts']) && !in_array($row['id_board'], $boards_can['approve_posts'])) { unset($_REQUEST['actions'][$row['id_topic']]); } elseif ($_REQUEST['actions'][$row['id_topic']] == 'sticky' && !in_array(0, $boards_can['make_sticky']) && !in_array($row['id_board'], $boards_can['make_sticky'])) { unset($_REQUEST['actions'][$row['id_topic']]); } elseif ($_REQUEST['actions'][$row['id_topic']] == 'move' && !in_array(0, $boards_can['move_any']) && !in_array($row['id_board'], $boards_can['move_any']) && ($row['id_member_started'] != $user_info['id'] || !in_array(0, $boards_can['move_own']) && !in_array($row['id_board'], $boards_can['move_own']))) { unset($_REQUEST['actions'][$row['id_topic']]); } elseif ($_REQUEST['actions'][$row['id_topic']] == 'remove' && !in_array(0, $boards_can['remove_any']) && !in_array($row['id_board'], $boards_can['remove_any']) && ($row['id_member_started'] != $user_info['id'] || !in_array(0, $boards_can['remove_own']) && !in_array($row['id_board'], $boards_can['remove_own']))) { unset($_REQUEST['actions'][$row['id_topic']]); } elseif ($_REQUEST['actions'][$row['id_topic']] == 'lock' && !in_array(0, $boards_can['lock_any']) && !in_array($row['id_board'], $boards_can['lock_any']) && ($row['id_member_started'] != $user_info['id'] || $row['locked'] == 1 || !in_array(0, $boards_can['lock_own']) && !in_array($row['id_board'], $boards_can['lock_own']))) { unset($_REQUEST['actions'][$row['id_topic']]); } elseif ($_REQUEST['actions'][$row['id_topic']] == 'approve' && (!$row['unapproved_posts'] || !in_array(0, $boards_can['approve_posts']) && !in_array($row['id_board'], $boards_can['approve_posts']))) { unset($_REQUEST['actions'][$row['id_topic']]); } } } } $stickyCache = array(); $moveCache = array(0 => array(), 1 => array()); $removeCache = array(); $lockCache = array(); $markCache = array(); $approveCache = array(); // Separate the actions. foreach ($_REQUEST['actions'] as $topic => $action) { $topic = (int) $topic; if ($action == 'markread') { $markCache[] = $topic; } elseif ($action == 'sticky') { $stickyCache[] = $topic; } elseif ($action == 'move') { moveTopicConcurrence(); // $moveCache[0] is the topic, $moveCache[1] is the board to move to. $moveCache[1][$topic] = (int) (isset($_REQUEST['move_tos'][$topic]) ? $_REQUEST['move_tos'][$topic] : $_REQUEST['move_to']); if (empty($moveCache[1][$topic])) { continue; } $moveCache[0][] = $topic; } elseif ($action == 'remove') { $removeCache[] = $topic; } elseif ($action == 'lock') { $lockCache[] = $topic; } elseif ($action == 'approve') { $approveCache[] = $topic; } } if (empty($board)) { $affectedBoards = array(); } else { $affectedBoards = array($board => array(0, 0)); } // Do all the stickies... if (!empty($stickyCache)) { toggleTopicSticky($stickyCache); // Get the board IDs and Sticky status $request = $db->query('', ' SELECT id_topic, id_board, is_sticky FROM {db_prefix}topics WHERE id_topic IN ({array_int:sticky_topic_ids}) LIMIT ' . count($stickyCache), array('sticky_topic_ids' => $stickyCache)); $stickyCacheBoards = array(); $stickyCacheStatus = array(); while ($row = $db->fetch_assoc($request)) { $stickyCacheBoards[$row['id_topic']] = $row['id_board']; $stickyCacheStatus[$row['id_topic']] = empty($row['is_sticky']); } $db->free_result($request); } // Move sucka! (this is, by the by, probably the most complicated part....) if (!empty($moveCache[0])) { // I know - I just KNOW you're trying to beat the system. Too bad for you... we CHECK :P. $request = $db->query('', ' SELECT t.id_topic, t.id_board, b.count_posts FROM {db_prefix}topics AS t LEFT JOIN {db_prefix}boards AS b ON (t.id_board = b.id_board) WHERE t.id_topic IN ({array_int:move_topic_ids})' . (!empty($board) && !allowedTo('move_any') ? ' AND t.id_member_started = {int:current_member}' : '') . ' LIMIT ' . count($moveCache[0]), array('current_member' => $user_info['id'], 'move_topic_ids' => $moveCache[0])); $moveTos = array(); $moveCache2 = array(); $countPosts = array(); while ($row = $db->fetch_assoc($request)) { $to = $moveCache[1][$row['id_topic']]; if (empty($to)) { continue; } // Does this topic's board count the posts or not? $countPosts[$row['id_topic']] = empty($row['count_posts']); if (!isset($moveTos[$to])) { $moveTos[$to] = array(); } $moveTos[$to][] = $row['id_topic']; // For reporting... $moveCache2[] = array($row['id_topic'], $row['id_board'], $to); } $db->free_result($request); $moveCache = $moveCache2; // Do the actual moves... foreach ($moveTos as $to => $topics) { moveTopics($topics, $to); } // Does the post counts need to be updated? if (!empty($moveTos)) { require_once SUBSDIR . '/Boards.subs.php'; $topicRecounts = array(); $boards_info = fetchBoardsInfo(array('boards' => array_keys($moveTos)), array('selects' => 'posts')); foreach ($boards_info as $row) { $cp = empty($row['count_posts']); // Go through all the topics that are being moved to this board. foreach ($moveTos[$row['id_board']] as $topic) { // If both boards have the same value for post counting then no adjustment needs to be made. if ($countPosts[$topic] != $cp) { // If the board being moved to does count the posts then the other one doesn't so add to their post count. $topicRecounts[$topic] = $cp ? 1 : -1; } } } if (!empty($topicRecounts)) { // Get all the members who have posted in the moved topics. $posters = topicsPosters(array_keys($topicRecounts)); foreach ($posters as $id_member => $topics) { $post_adj = 0; foreach ($topics as $id_topic) { $post_adj += $topicRecounts[$id_topic]; } // And now update that member's post counts if (!empty($post_adj)) { updateMemberData($id_member, array('posts' => 'posts + ' . $post_adj)); } } } } } // Now delete the topics... if (!empty($removeCache)) { // They can only delete their own topics. (we wouldn't be here if they couldn't do that..) $result = $db->query('', ' SELECT id_topic, id_board FROM {db_prefix}topics WHERE id_topic IN ({array_int:removed_topic_ids})' . (!empty($board) && !allowedTo('remove_any') ? ' AND id_member_started = {int:current_member}' : '') . ' LIMIT ' . count($removeCache), array('current_member' => $user_info['id'], 'removed_topic_ids' => $removeCache)); $removeCache = array(); $removeCacheBoards = array(); while ($row = $db->fetch_assoc($result)) { $removeCache[] = $row['id_topic']; $removeCacheBoards[$row['id_topic']] = $row['id_board']; } $db->free_result($result); // Maybe *none* were their own topics. if (!empty($removeCache)) { // Gotta send the notifications *first*! foreach ($removeCache as $topic) { // Only log the topic ID if it's not in the recycle board. logAction('remove', array(empty($modSettings['recycle_enable']) || $modSettings['recycle_board'] != $removeCacheBoards[$topic] ? 'topic' : 'old_topic_id' => $topic, 'board' => $removeCacheBoards[$topic])); sendNotifications($topic, 'remove'); } removeTopics($removeCache); } } // Approve the topics... if (!empty($approveCache)) { // We need unapproved topic ids and their authors! $request = $db->query('', ' SELECT id_topic, id_member_started FROM {db_prefix}topics WHERE id_topic IN ({array_int:approve_topic_ids}) AND approved = {int:not_approved} LIMIT ' . count($approveCache), array('approve_topic_ids' => $approveCache, 'not_approved' => 0)); $approveCache = array(); $approveCacheMembers = array(); while ($row = $db->fetch_assoc($request)) { $approveCache[] = $row['id_topic']; $approveCacheMembers[$row['id_topic']] = $row['id_member_started']; } $db->free_result($request); // Any topics to approve? if (!empty($approveCache)) { // Handle the approval part... approveTopics($approveCache); // Time for some logging! foreach ($approveCache as $topic) { logAction('approve_topic', array('topic' => $topic, 'member' => $approveCacheMembers[$topic])); } } } // And (almost) lastly, lock the topics... if (!empty($lockCache)) { $lockStatus = array(); // Gotta make sure they CAN lock/unlock these topics... if (!empty($board) && !allowedTo('lock_any')) { // Make sure they started the topic AND it isn't already locked by someone with higher priv's. $result = $db->query('', ' SELECT id_topic, locked, id_board FROM {db_prefix}topics WHERE id_topic IN ({array_int:locked_topic_ids}) AND id_member_started = {int:current_member} AND locked IN (2, 0) LIMIT ' . count($lockCache), array('current_member' => $user_info['id'], 'locked_topic_ids' => $lockCache)); $lockCache = array(); $lockCacheBoards = array(); while ($row = $db->fetch_assoc($result)) { $lockCache[] = $row['id_topic']; $lockCacheBoards[$row['id_topic']] = $row['id_board']; $lockStatus[$row['id_topic']] = empty($row['locked']); } $db->free_result($result); } else { $result = $db->query('', ' SELECT id_topic, locked, id_board FROM {db_prefix}topics WHERE id_topic IN ({array_int:locked_topic_ids}) LIMIT ' . count($lockCache), array('locked_topic_ids' => $lockCache)); $lockCacheBoards = array(); while ($row = $db->fetch_assoc($result)) { $lockStatus[$row['id_topic']] = empty($row['locked']); $lockCacheBoards[$row['id_topic']] = $row['id_board']; } $db->free_result($result); } // It could just be that *none* were their own topics... if (!empty($lockCache)) { // Alternate the locked value. $db->query('', ' UPDATE {db_prefix}topics SET locked = CASE WHEN locked = {int:is_locked} THEN ' . (allowedTo('lock_any') ? '1' : '2') . ' ELSE 0 END WHERE id_topic IN ({array_int:locked_topic_ids})', array('locked_topic_ids' => $lockCache, 'is_locked' => 0)); } } if (!empty($markCache)) { $logged_topics = getLoggedTopics($user_info['id'], $markCache); $markArray = array(); foreach ($markCache as $topic) { $markArray[] = array($user_info['id'], $topic, $modSettings['maxMsgID'], (int) (!empty($logged_topics[$topic]))); } markTopicsRead($markArray, true); } foreach ($moveCache as $topic) { // Didn't actually move anything! if (!isset($topic[0])) { break; } logAction('move', array('topic' => $topic[0], 'board_from' => $topic[1], 'board_to' => $topic[2])); sendNotifications($topic[0], 'move'); } foreach ($lockCache as $topic) { logAction($lockStatus[$topic] ? 'lock' : 'unlock', array('topic' => $topic, 'board' => $lockCacheBoards[$topic])); sendNotifications($topic, $lockStatus[$topic] ? 'lock' : 'unlock'); } foreach ($stickyCache as $topic) { logAction($stickyCacheStatus[$topic] ? 'unsticky' : 'sticky', array('topic' => $topic, 'board' => $stickyCacheBoards[$topic])); sendNotifications($topic, 'sticky'); } updateStats('topic'); updateStats('message'); updateSettings(array('calendar_updated' => time())); if (!empty($affectedBoards)) { updateLastMessages(array_keys($affectedBoards)); } redirectexit($redirect_url); }
/** * Find the ten most recent posts. * Accessed by action=recent. */ public function action_recent() { global $txt, $scripturl, $user_info, $context, $modSettings, $board; $db = database(); loadTemplate('Recent'); $context['page_title'] = $txt['recent_posts']; $context['sub_template'] = 'recent'; require_once SUBSDIR . '/Recent.subs.php'; if (isset($_REQUEST['start']) && $_REQUEST['start'] > 95) { $_REQUEST['start'] = 95; } $query_parameters = array(); // Recent posts by category id's if (!empty($_REQUEST['c']) && empty($board)) { $categories = array_map('intval', explode(',', $_REQUEST['c'])); if (count($categories) === 1) { require_once SUBSDIR . '/Categories.subs.php'; $name = categoryName($categories[0]); if (empty($name)) { fatal_lang_error('no_access', false); } $context['linktree'][] = array('url' => $scripturl . '#c' . $categories[0], 'name' => $name); } // Find the number of posts in these categorys, exclude the recycle board. require_once SUBSDIR . '/Boards.subs.php'; $boards_posts = boardsPosts(array(), $categories, false, false); $total_cat_posts = array_sum($boards_posts); $boards = array_keys($boards_posts); if (empty($boards)) { fatal_lang_error('error_no_boards_selected'); } // The query for getting the messages $query_this_board = 'b.id_board IN ({array_int:boards})'; $query_parameters['boards'] = $boards; // If this category has a significant number of posts in it... if ($total_cat_posts > 100 && $total_cat_posts > $modSettings['totalMessages'] / 15) { $query_this_board .= ' AND m.id_msg >= {int:max_id_msg}'; $query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 400 - $_REQUEST['start'] * 7); } $context['page_index'] = constructPageIndex($scripturl . '?action=recent;c=' . implode(',', $categories), $_REQUEST['start'], min(100, $total_cat_posts), 10, false); } elseif (!empty($_REQUEST['boards'])) { $_REQUEST['boards'] = explode(',', $_REQUEST['boards']); foreach ($_REQUEST['boards'] as $i => $b) { $_REQUEST['boards'][$i] = (int) $b; } // Fetch the number of posts for the supplied board IDs require_once SUBSDIR . '/Boards.subs.php'; $boards_posts = boardsPosts($_REQUEST['boards'], array()); $total_posts = array_sum($boards_posts); $boards = array_keys($boards_posts); if (empty($boards)) { fatal_lang_error('error_no_boards_selected'); } // Build the query for finding the messages $query_this_board = 'b.id_board IN ({array_int:boards})'; $query_parameters['boards'] = $boards; // If these boards have a significant number of posts in them... if ($total_posts > 100 && $total_posts > $modSettings['totalMessages'] / 12) { $query_this_board .= ' AND m.id_msg >= {int:max_id_msg}'; $query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 500 - $_REQUEST['start'] * 9); } $context['page_index'] = constructPageIndex($scripturl . '?action=recent;boards=' . implode(',', $_REQUEST['boards']), $_REQUEST['start'], min(100, $total_posts), 10, false); } elseif (!empty($board)) { require_once SUBSDIR . '/Boards.subs.php'; $board_data = fetchBoardsInfo(array('boards' => $board), array('selects' => 'posts')); $query_this_board = 'b.id_board = {int:board}'; $query_parameters['board'] = $board; // If this board has a significant number of posts in it... if ($board_data[$board]['num_posts'] > 80 && $board_data[$board]['num_posts'] > $modSettings['totalMessages'] / 10) { $query_this_board .= ' AND m.id_msg >= {int:max_id_msg}'; $query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 600 - $_REQUEST['start'] * 10); } $context['page_index'] = constructPageIndex($scripturl . '?action=recent;board=' . $board . '.%1$d', $_REQUEST['start'], min(100, $board_data[$board]['num_posts']), 10, true); } else { $query_this_board = '{query_wanna_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? ' AND b.id_board != {int:recycle_board}' : '') . ' AND m.id_msg >= {int:max_id_msg}'; $query_parameters['max_id_msg'] = max(0, $modSettings['maxMsgID'] - 100 - $_REQUEST['start'] * 6); $query_parameters['recycle_board'] = $modSettings['recycle_board']; // Set up the pageindex require_once SUBSDIR . '/Boards.subs.php'; $context['page_index'] = constructPageIndex($scripturl . '?action=recent', $_REQUEST['start'], min(100, sumRecentPosts()), 10, false); } $context['linktree'][] = array('url' => $scripturl . '?action=recent' . (empty($board) ? empty($categories) ? '' : ';c=' . implode(',', $categories) : ';board=' . $board . '.0'), 'name' => $context['page_title']); $key = 'recent-' . $user_info['id'] . '-' . md5(serialize(array_diff_key($query_parameters, array('max_id_msg' => 0)))) . '-' . (int) $_REQUEST['start']; if (empty($modSettings['cache_enable']) || ($messages = cache_get_data($key, 120)) == null) { $done = false; while (!$done) { // Find the 10 most recent messages they can *view*. // @todo SLOW This query is really slow still, probably? $request = $db->query('', ' SELECT m.id_msg FROM {db_prefix}messages AS m INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board) WHERE ' . $query_this_board . ' AND m.approved = {int:is_approved} ORDER BY m.id_msg DESC LIMIT {int:offset}, {int:limit}', array_merge($query_parameters, array('is_approved' => 1, 'offset' => $_REQUEST['start'], 'limit' => 10))); // If we don't have 10 results, try again with an unoptimized version covering all rows, and cache the result. if (isset($query_parameters['max_id_msg']) && $db->num_rows($request) < 10) { $db->free_result($request); $query_this_board = str_replace('AND m.id_msg >= {int:max_id_msg}', '', $query_this_board); $cache_results = true; unset($query_parameters['max_id_msg']); } else { $done = true; } } $messages = array(); while ($row = $db->fetch_assoc($request)) { $messages[] = $row['id_msg']; } $db->free_result($request); if (!empty($cache_results)) { cache_put_data($key, $messages, 120); } } // Nothing here... Or at least, nothing you can see... if (empty($messages)) { $context['posts'] = array(); return; } list($context['posts'], $board_ids) = getRecentPosts($messages, $_REQUEST['start']); // There might be - and are - different permissions between any and own. $permissions = array('own' => array('post_reply_own' => 'can_reply', 'delete_own' => 'can_delete'), 'any' => array('post_reply_any' => 'can_reply', 'mark_any_notify' => 'can_mark_notify', 'delete_any' => 'can_delete')); // Provide an easy way for integration to interact with the recent display items call_integration_hook('integrate_recent_message_list', array($messages, &$permissions)); // Now go through all the permissions, looking for boards they can do it on. foreach ($permissions as $type => $list) { foreach ($list as $permission => $allowed) { // They can do it on these boards... $boards = boardsAllowedTo($permission); // If 0 is the only thing in the array, they can do it everywhere! if (!empty($boards) && $boards[0] == 0) { $boards = array_keys($board_ids[$type]); } // Go through the boards, and look for posts they can do this on. foreach ($boards as $board_id) { // Hmm, they have permission, but there are no topics from that board on this page. if (!isset($board_ids[$type][$board_id])) { continue; } // Okay, looks like they can do it for these posts. foreach ($board_ids[$type][$board_id] as $counter) { if ($type == 'any' || $context['posts'][$counter]['poster']['id'] == $user_info['id']) { $context['posts'][$counter]['tests'][$allowed] = true; } } } } } $quote_enabled = empty($modSettings['disabledBBC']) || !in_array('quote', explode(',', $modSettings['disabledBBC'])); foreach ($context['posts'] as $counter => $post) { // Some posts - the first posts - can't just be deleted. $context['posts'][$counter]['tests']['can_delete'] &= $context['posts'][$counter]['delete_possible']; // And some cannot be quoted... $context['posts'][$counter]['tests']['can_quote'] = $context['posts'][$counter]['tests']['can_reply'] && $quote_enabled; // Let's add some buttons here! $context['posts'][$counter]['buttons'] = array('remove' => array('href' => $scripturl . '?action=deletemsg;msg=' . $post['id'] . ';topic=' . $post['topic'] . ';recent;' . $context['session_var'] . '=' . $context['session_id'], 'text' => $txt['remove'], 'test' => 'can_delete', 'custom' => 'onclick="return confirm(' . JavaScriptEscape($txt['remove_message'] . '?') . ');"'), 'notify' => array('href' => $scripturl . '?action=notify;topic=' . $post['topic'] . '.' . $post['start'], 'text' => $txt['notify'], 'test' => 'can_mark_notify'), 'reply' => array('href' => $scripturl . '?action=post;topic=' . $post['topic'] . '.' . $post['start'], 'text' => $txt['reply'], 'test' => 'can_reply'), 'quote' => array('href' => $scripturl . '?action=post;topic=' . $post['topic'] . '.' . $post['start'] . ';quote=' . $post['id'], 'text' => $txt['quote'], 'test' => 'can_quote')); } }
/** * Set merge options and do the actual merge of two or more topics. * * the merge options screen: * * shows topics to be merged and allows to set some merge options. * * is accessed by ?action=mergetopics;sa=options.and can also internally be called by action_quickmod(). * * uses 'merge_extra_options' sub template of the MergeTopics template. * * the actual merge: * * is accessed with ?action=mergetopics;sa=execute. * * updates the statistics to reflect the merge. * * logs the action in the moderation log. * * sends a notification is sent to all users monitoring this topic. * * redirects to ?action=mergetopics;sa=done. * * @param int[] $topics = array() of topic ids */ public function action_mergeExecute($topics = array()) { global $user_info, $txt, $context, $scripturl, $modSettings; $db = database(); // Check the session. checkSession('request'); require_once SUBSDIR . '/Topic.subs.php'; require_once SUBSDIR . '/Post.subs.php'; // Handle URLs from action_mergeIndex. if (!empty($_GET['from']) && !empty($_GET['to'])) { $topics = array((int) $_GET['from'], (int) $_GET['to']); } // If we came from a form, the topic IDs came by post. if (!empty($_POST['topics']) && is_array($_POST['topics'])) { $topics = $_POST['topics']; } // There's nothing to merge with just one topic... if (empty($topics) || !is_array($topics) || count($topics) == 1) { fatal_lang_error('merge_need_more_topics'); } // Make sure every topic is numeric, or some nasty things could be done with the DB. foreach ($topics as $id => $topic) { $topics[$id] = (int) $topic; } // Joy of all joys, make sure they're not pi**ing about with unapproved topics they can't see :P if ($modSettings['postmod_active']) { $can_approve_boards = !empty($user_info['mod_cache']['ap']) ? $user_info['mod_cache']['ap'] : boardsAllowedTo('approve_posts'); } // Get info about the topics and polls that will be merged. $request = $db->query('', ' SELECT t.id_topic, t.id_board, t.id_poll, t.num_views, t.is_sticky, t.approved, t.num_replies, t.unapproved_posts, m1.subject, m1.poster_time AS time_started, IFNULL(mem1.id_member, 0) AS id_member_started, IFNULL(mem1.real_name, m1.poster_name) AS name_started, m2.poster_time AS time_updated, IFNULL(mem2.id_member, 0) AS id_member_updated, IFNULL(mem2.real_name, m2.poster_name) AS name_updated FROM {db_prefix}topics AS t INNER JOIN {db_prefix}messages AS m1 ON (m1.id_msg = t.id_first_msg) INNER JOIN {db_prefix}messages AS m2 ON (m2.id_msg = t.id_last_msg) LEFT JOIN {db_prefix}members AS mem1 ON (mem1.id_member = m1.id_member) LEFT JOIN {db_prefix}members AS mem2 ON (mem2.id_member = m2.id_member) WHERE t.id_topic IN ({array_int:topic_list}) ORDER BY t.id_first_msg LIMIT ' . count($topics), array('topic_list' => $topics)); if ($db->num_rows($request) < 2) { fatal_lang_error('no_topic_id'); } $num_views = 0; $is_sticky = 0; $boardTotals = array(); $topic_data = array(); $boards = array(); $polls = array(); $firstTopic = 0; while ($row = $db->fetch_assoc($request)) { // Make a note for the board counts... if (!isset($boardTotals[$row['id_board']])) { $boardTotals[$row['id_board']] = array('num_posts' => 0, 'num_topics' => 0, 'unapproved_posts' => 0, 'unapproved_topics' => 0); } // We can't see unapproved topics here? if ($modSettings['postmod_active'] && !$row['approved'] && $can_approve_boards != array(0) && in_array($row['id_board'], $can_approve_boards)) { continue; } elseif (!$row['approved']) { $boardTotals[$row['id_board']]['unapproved_topics']++; } else { $boardTotals[$row['id_board']]['num_topics']++; } $boardTotals[$row['id_board']]['unapproved_posts'] += $row['unapproved_posts']; $boardTotals[$row['id_board']]['num_posts'] += $row['num_replies'] + ($row['approved'] ? 1 : 0); $topic_data[$row['id_topic']] = array('id' => $row['id_topic'], 'board' => $row['id_board'], 'poll' => $row['id_poll'], 'num_views' => $row['num_views'], 'subject' => $row['subject'], 'started' => array('time' => standardTime($row['time_started']), 'html_time' => htmlTime($row['time_started']), 'timestamp' => forum_time(true, $row['time_started']), 'href' => empty($row['id_member_started']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member_started'], 'link' => empty($row['id_member_started']) ? $row['name_started'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_started'] . '">' . $row['name_started'] . '</a>'), 'updated' => array('time' => standardTime($row['time_updated']), 'html_time' => htmlTime($row['time_updated']), 'timestamp' => forum_time(true, $row['time_updated']), 'href' => empty($row['id_member_updated']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member_updated'], 'link' => empty($row['id_member_updated']) ? $row['name_updated'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_updated'] . '">' . $row['name_updated'] . '</a>')); $num_views += $row['num_views']; $boards[] = $row['id_board']; // If there's no poll, id_poll == 0... if ($row['id_poll'] > 0) { $polls[] = $row['id_poll']; } // Store the id_topic with the lowest id_first_msg. if (empty($firstTopic)) { $firstTopic = $row['id_topic']; } $is_sticky = max($is_sticky, $row['is_sticky']); } $db->free_result($request); // If we didn't get any topics then they've been messing with unapproved stuff. if (empty($topic_data)) { fatal_lang_error('no_topic_id'); } $boards = array_values(array_unique($boards)); // The parameters of action_mergeExecute were set, so this must've been an internal call. if (!empty($topics)) { isAllowedTo('merge_any', $boards); loadTemplate('MergeTopics'); } // Get the boards a user is allowed to merge in. $merge_boards = boardsAllowedTo('merge_any'); if (empty($merge_boards)) { fatal_lang_error('cannot_merge_any', 'user'); } require_once SUBSDIR . '/Boards.subs.php'; // Make sure they can see all boards.... $query_boards = array('boards' => $boards); if (!in_array(0, $merge_boards)) { $query_boards['boards'] = array_merge($query_boards['boards'], $merge_boards); } // Saved in a variable to (potentially) save a query later $boards_info = fetchBoardsInfo($query_boards); // This happens when a member is moderator of a board he cannot see foreach ($boards as $board) { if (!isset($boards_info[$board])) { fatal_lang_error('no_board'); } } if (empty($_REQUEST['sa']) || $_REQUEST['sa'] == 'options') { if (count($polls) > 1) { $request = $db->query('', ' SELECT t.id_topic, t.id_poll, m.subject, p.question FROM {db_prefix}polls AS p INNER JOIN {db_prefix}topics AS t ON (t.id_poll = p.id_poll) INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg) WHERE p.id_poll IN ({array_int:polls}) LIMIT ' . count($polls), array('polls' => $polls)); while ($row = $db->fetch_assoc($request)) { $context['polls'][] = array('id' => $row['id_poll'], 'topic' => array('id' => $row['id_topic'], 'subject' => $row['subject']), 'question' => $row['question'], 'selected' => $row['id_topic'] == $firstTopic); } $db->free_result($request); } if (count($boards) > 1) { foreach ($boards_info as $row) { $context['boards'][] = array('id' => $row['id_board'], 'name' => $row['name'], 'selected' => $row['id_board'] == $topic_data[$firstTopic]['board']); } } $context['topics'] = $topic_data; foreach ($topic_data as $id => $topic) { $context['topics'][$id]['selected'] = $topic['id'] == $firstTopic; } $context['page_title'] = $txt['merge']; $context['sub_template'] = 'merge_extra_options'; return; } // Determine target board. $target_board = count($boards) > 1 ? (int) $_REQUEST['board'] : $boards[0]; if (!in_array($target_board, $boards)) { fatal_lang_error('no_board'); } // Determine which poll will survive and which polls won't. $target_poll = count($polls) > 1 ? (int) $_POST['poll'] : (count($polls) == 1 ? $polls[0] : 0); if ($target_poll > 0 && !in_array($target_poll, $polls)) { fatal_lang_error('no_access', false); } $deleted_polls = empty($target_poll) ? $polls : array_diff($polls, array($target_poll)); // Determine the subject of the newly merged topic - was a custom subject specified? if (empty($_POST['subject']) && isset($_POST['custom_subject']) && $_POST['custom_subject'] != '') { $target_subject = strtr(Util::htmltrim(Util::htmlspecialchars($_POST['custom_subject'])), array("\r" => '', "\n" => '', "\t" => '')); // Keep checking the length. if (Util::strlen($target_subject) > 100) { $target_subject = Util::substr($target_subject, 0, 100); } // Nothing left - odd but pick the first topics subject. if ($target_subject == '') { $target_subject = $topic_data[$firstTopic]['subject']; } } elseif (!empty($topic_data[(int) $_POST['subject']]['subject'])) { $target_subject = $topic_data[(int) $_POST['subject']]['subject']; } else { $target_subject = $topic_data[$firstTopic]['subject']; } // Get the first and last message and the number of messages.... $request = $db->query('', ' SELECT approved, MIN(id_msg) AS first_msg, MAX(id_msg) AS last_msg, COUNT(*) AS message_count FROM {db_prefix}messages WHERE id_topic IN ({array_int:topics}) GROUP BY approved ORDER BY approved DESC', array('topics' => $topics)); $topic_approved = 1; $first_msg = 0; while ($row = $db->fetch_assoc($request)) { // If this is approved, or is fully unapproved. if ($row['approved'] || !isset($first_msg)) { $first_msg = $row['first_msg']; $last_msg = $row['last_msg']; if ($row['approved']) { $num_replies = $row['message_count'] - 1; $num_unapproved = 0; } else { $topic_approved = 0; $num_replies = 0; $num_unapproved = $row['message_count']; } } else { // If this has a lower first_msg then the first post is not approved and hence the number of replies was wrong! if ($first_msg > $row['first_msg']) { $first_msg = $row['first_msg']; $num_replies++; $topic_approved = 0; } $num_unapproved = $row['message_count']; } } $db->free_result($request); // Ensure we have a board stat for the target board. if (!isset($boardTotals[$target_board])) { $boardTotals[$target_board] = array('num_posts' => 0, 'num_topics' => 0, 'unapproved_posts' => 0, 'unapproved_topics' => 0); } // Fix the topic count stuff depending on what the new one counts as. if ($topic_approved) { $boardTotals[$target_board]['num_topics']--; } else { $boardTotals[$target_board]['unapproved_topics']--; } $boardTotals[$target_board]['unapproved_posts'] -= $num_unapproved; $boardTotals[$target_board]['num_posts'] -= $topic_approved ? $num_replies + 1 : $num_replies; // Get the member ID of the first and last message. $request = $db->query('', ' SELECT id_member FROM {db_prefix}messages WHERE id_msg IN ({int:first_msg}, {int:last_msg}) ORDER BY id_msg LIMIT 2', array('first_msg' => $first_msg, 'last_msg' => $last_msg)); list($member_started) = $db->fetch_row($request); list($member_updated) = $db->fetch_row($request); // First and last message are the same, so only row was returned. if ($member_updated === null) { $member_updated = $member_started; } $db->free_result($request); // Obtain all the message ids we are going to affect. $affected_msgs = messagesInTopics($topics); // Assign the first topic ID to be the merged topic. $id_topic = min($topics); // Grab the response prefix (like 'Re: ') in the default forum language. $context['response_prefix'] = response_prefix(); $enforce_subject = isset($_POST['enforce_subject']) ? Util::htmlspecialchars(trim($_POST['enforce_subject'])) : ''; // Merge topic notifications. $notifications = isset($_POST['notifications']) && is_array($_POST['notifications']) ? array_intersect($topics, $_POST['notifications']) : array(); fixMergedTopics($first_msg, $topics, $id_topic, $target_board, $target_subject, $enforce_subject, $notifications); // Asssign the properties of the newly merged topic. $db->query('', ' UPDATE {db_prefix}topics SET id_board = {int:id_board}, id_member_started = {int:id_member_started}, id_member_updated = {int:id_member_updated}, id_first_msg = {int:id_first_msg}, id_last_msg = {int:id_last_msg}, id_poll = {int:id_poll}, num_replies = {int:num_replies}, unapproved_posts = {int:unapproved_posts}, num_views = {int:num_views}, is_sticky = {int:is_sticky}, approved = {int:approved} WHERE id_topic = {int:id_topic}', array('id_board' => $target_board, 'is_sticky' => $is_sticky, 'approved' => $topic_approved, 'id_topic' => $id_topic, 'id_member_started' => $member_started, 'id_member_updated' => $member_updated, 'id_first_msg' => $first_msg, 'id_last_msg' => $last_msg, 'id_poll' => $target_poll, 'num_replies' => $num_replies, 'unapproved_posts' => $num_unapproved, 'num_views' => $num_views)); // Get rid of the redundant polls. if (!empty($deleted_polls)) { require_once SUBSDIR . '/Poll.subs.php'; removePoll($deleted_polls); } // Cycle through each board... foreach ($boardTotals as $id_board => $stats) { decrementBoard($id_board, $stats); } // Determine the board the final topic resides in $topic_info = getTopicInfo($id_topic); $id_board = $topic_info['id_board']; // Update all the statistics. updateStats('topic'); updateStats('subject', $id_topic, $target_subject); updateLastMessages($boards); logAction('merge', array('topic' => $id_topic, 'board' => $id_board)); // Notify people that these topics have been merged? require_once SUBSDIR . '/Notification.subs.php'; sendNotifications($id_topic, 'merge'); // If there's a search index that needs updating, update it... require_once SUBSDIR . '/Search.subs.php'; $searchAPI = findSearchAPI(); if (is_callable(array($searchAPI, 'topicMerge'))) { $searchAPI->topicMerge($id_topic, $topics, $affected_msgs, empty($enforce_subject) ? null : array($context['response_prefix'], $target_subject)); } // Send them to the all done page. redirectexit('action=mergetopics;sa=done;to=' . $id_topic . ';targetboard=' . $target_board); }
/** * Gets the moderation log entries that match the specified parameters. * Callback for createList() in Modlog::action_log(). * * @param int $start * @param int $items_per_page * @param string $sort * @param string|null $query_string * @param mixed[] $query_params * @param int $log_type */ function list_getModLogEntries($start, $items_per_page, $sort, $query_string = '', $query_params = array(), $log_type = 1) { global $context, $scripturl, $txt, $user_info; $db = database(); $modlog_query = allowedTo('admin_forum') || $user_info['mod_cache']['bq'] == '1=1' ? '1=1' : ($user_info['mod_cache']['bq'] == '0=1' ? 'lm.id_board = 0 AND lm.id_topic = 0' : strtr($user_info['mod_cache']['bq'], array('id_board' => 'b.id_board')) . ' AND ' . strtr($user_info['mod_cache']['bq'], array('id_board' => 't.id_board'))); // Do a little bit of self protection. if (!isset($context['hoursdisable'])) { $context['hoursdisable'] = 24; } // Can they see the IP address? $seeIP = allowedTo('moderate_forum'); // Here we have the query getting the log details. $result = $db->query('', ' SELECT lm.id_action, lm.id_member, lm.ip, lm.log_time, lm.action, lm.id_board, lm.id_topic, lm.id_msg, lm.extra, mem.real_name, mg.group_name FROM {db_prefix}log_actions AS lm LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lm.id_member) LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = CASE WHEN mem.id_group = {int:reg_group_id} THEN mem.id_post_group ELSE mem.id_group END) LEFT JOIN {db_prefix}boards AS b ON (b.id_board = lm.id_board) LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = lm.id_topic) WHERE id_log = {int:log_type} AND {raw:modlog_query}' . (!empty($query_string) ? ' AND ' . $query_string : '') . ' ORDER BY ' . $sort . ' LIMIT ' . $start . ', ' . $items_per_page, array_merge($query_params, array('reg_group_id' => 0, 'log_type' => $log_type, 'modlog_query' => $modlog_query))); // Arrays for decoding objects into. $topics = array(); $boards = array(); $members = array(); $messages = array(); $entries = array(); while ($row = $db->fetch_assoc($result)) { $row['extra'] = @unserialize($row['extra']); // Corrupt? $row['extra'] = is_array($row['extra']) ? $row['extra'] : array(); // Add on some of the column stuff info if (!empty($row['id_board'])) { if ($row['action'] == 'move') { $row['extra']['board_to'] = $row['id_board']; } else { $row['extra']['board'] = $row['id_board']; } } if (!empty($row['id_topic'])) { $row['extra']['topic'] = $row['id_topic']; } if (!empty($row['id_msg'])) { $row['extra']['message'] = $row['id_msg']; } // Is this associated with a topic? if (isset($row['extra']['topic'])) { $topics[(int) $row['extra']['topic']][] = $row['id_action']; } if (isset($row['extra']['new_topic'])) { $topics[(int) $row['extra']['new_topic']][] = $row['id_action']; } // How about a member? if (isset($row['extra']['member'])) { // Guests don't have names! if (empty($row['extra']['member'])) { $row['extra']['member'] = $txt['modlog_parameter_guest']; } else { // Try to find it... $members[(int) $row['extra']['member']][] = $row['id_action']; } } // Associated with a board? if (isset($row['extra']['board_to'])) { $boards[(int) $row['extra']['board_to']][] = $row['id_action']; } if (isset($row['extra']['board_from'])) { $boards[(int) $row['extra']['board_from']][] = $row['id_action']; } if (isset($row['extra']['board'])) { $boards[(int) $row['extra']['board']][] = $row['id_action']; } // A message? if (isset($row['extra']['message'])) { $messages[(int) $row['extra']['message']][] = $row['id_action']; } // IP Info? if (isset($row['extra']['ip_range'])) { if ($seeIP) { $row['extra']['ip_range'] = '<a href="' . $scripturl . '?action=trackip;searchip=' . $row['extra']['ip_range'] . '">' . $row['extra']['ip_range'] . '</a>'; } else { $row['extra']['ip_range'] = $txt['logged']; } } // Email? if (isset($row['extra']['email'])) { $row['extra']['email'] = '<a href="mailto:' . $row['extra']['email'] . '">' . $row['extra']['email'] . '</a>'; } // Bans are complex. if ($row['action'] == 'ban') { if (!isset($row['extra']['new']) || $row['extra']['new'] == 1) { $row['action_text'] = $txt['modlog_ac_ban']; } elseif ($row['extra']['new'] == 0) { $row['action_text'] = $txt['modlog_ac_ban_update']; } else { $row['action_text'] = $txt['modlog_ac_ban_remove']; } foreach (array('member', 'email', 'ip_range', 'hostname') as $type) { if (isset($row['extra'][$type])) { $row['action_text'] .= $txt['modlog_ac_ban_trigger_' . $type]; } } } // The array to go to the template. Note here that action is set to a "default" value of the action doesn't match anything in the descriptions. Allows easy adding of logging events with basic details. $entries[$row['id_action']] = array('id' => $row['id_action'], 'ip' => $seeIP ? $row['ip'] : $txt['logged'], 'position' => empty($row['real_name']) && empty($row['group_name']) ? $txt['guest'] : $row['group_name'], 'moderator_link' => $row['id_member'] ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>' : (empty($row['real_name']) ? $txt['guest'] . (!empty($row['extra']['member_acted']) ? ' (' . $row['extra']['member_acted'] . ')' : '') : $row['real_name']), 'time' => standardTime($row['log_time']), 'html_time' => htmlTime($row['log_time']), 'timestamp' => forum_time(true, $row['log_time']), 'editable' => time() > $row['log_time'] + $context['hoursdisable'] * 3600, 'extra' => $row['extra'], 'action' => $row['action'], 'action_text' => isset($row['action_text']) ? $row['action_text'] : ''); } $db->free_result($result); if (!empty($boards)) { require_once SUBSDIR . '/Boards.subs.php'; $boards_info = fetchBoardsInfo(array('boards' => array_keys($boards))); foreach ($boards_info as $row) { foreach ($boards[$row['id_board']] as $action) { // Make the board number into a link - dealing with moving too. if (isset($entries[$action]['extra']['board_to']) && $entries[$action]['extra']['board_to'] == $row['id_board']) { $entries[$action]['extra']['board_to'] = '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>'; } elseif (isset($entries[$action]['extra']['board_from']) && $entries[$action]['extra']['board_from'] == $row['id_board']) { $entries[$action]['extra']['board_from'] = '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>'; } elseif (isset($entries[$action]['extra']['board']) && $entries[$action]['extra']['board'] == $row['id_board']) { $entries[$action]['extra']['board'] = '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>'; } } } } if (!empty($topics)) { $request = $db->query('', ' SELECT ms.subject, t.id_topic FROM {db_prefix}topics AS t INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg) WHERE t.id_topic IN ({array_int:topic_list}) LIMIT ' . count(array_keys($topics)), array('topic_list' => array_keys($topics))); while ($row = $db->fetch_assoc($request)) { foreach ($topics[$row['id_topic']] as $action) { $this_action =& $entries[$action]; // This isn't used in the current theme. $this_action['topic'] = array('id' => $row['id_topic'], 'subject' => $row['subject'], 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0', 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>'); // Make the topic number into a link - dealing with splitting too. if (isset($this_action['extra']['topic']) && $this_action['extra']['topic'] == $row['id_topic']) { $this_action['extra']['topic'] = '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.' . (isset($this_action['extra']['message']) ? 'msg' . $this_action['extra']['message'] . '#msg' . $this_action['extra']['message'] : '0') . '">' . $row['subject'] . '</a>'; } elseif (isset($this_action['extra']['new_topic']) && $this_action['extra']['new_topic'] == $row['id_topic']) { $this_action['extra']['new_topic'] = '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.' . (isset($this_action['extra']['message']) ? 'msg' . $this_action['extra']['message'] . '#msg' . $this_action['extra']['message'] : '0') . '">' . $row['subject'] . '</a>'; } } } $db->free_result($request); } if (!empty($messages)) { $request = $db->query('', ' SELECT id_msg, subject FROM {db_prefix}messages WHERE id_msg IN ({array_int:message_list}) LIMIT ' . count(array_keys($messages)), array('message_list' => array_keys($messages))); while ($row = $db->fetch_assoc($request)) { foreach ($messages[$row['id_msg']] as $action) { $this_action =& $entries[$action]; // This isn't used in the current theme. $this_action['message'] = array('id' => $row['id_msg'], 'subject' => $row['subject'], 'href' => $scripturl . '?msg=' . $row['id_msg'], 'link' => '<a href="' . $scripturl . '?msg=' . $row['id_msg'] . '">' . $row['subject'] . '</a>'); // Make the message number into a link. if (isset($this_action['extra']['message']) && $this_action['extra']['message'] == $row['id_msg']) { $this_action['extra']['message'] = '<a href="' . $scripturl . '?msg=' . $row['id_msg'] . '">' . $row['subject'] . '</a>'; } } } $db->free_result($request); } if (!empty($members)) { require_once SUBSDIR . '/Members.subs.php'; // Get the latest activated member's display name. $result = getBasicMemberData(array_keys($members)); foreach ($result as $row) { foreach ($members[$row['id_member']] as $action) { // Not used currently. $entries[$action]['member'] = array('id' => $row['id_member'], 'name' => $row['real_name'], 'href' => $scripturl . '?action=profile;u=' . $row['id_member'], 'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>'); // Make the member number into a name. $entries[$action]['extra']['member'] = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>'; } } } // Do some formatting of the action string. $callback = new ModLogEntriesReplacement(); $callback->entries = $entries; foreach ($entries as $k => $entry) { // Make any message info links so its easier to go find that message. if (isset($entry['extra']['message']) && (empty($entry['message']) || empty($entry['message']['id']))) { $entries[$k]['extra']['message'] = '<a href="' . $scripturl . '?msg=' . $entry['extra']['message'] . '">' . $entry['extra']['message'] . '</a>'; } // Mark up any deleted members, topics and boards. foreach (array('board', 'board_from', 'board_to', 'member', 'topic', 'new_topic') as $type) { if (!empty($entry['extra'][$type]) && is_numeric($entry['extra'][$type])) { $entries[$k]['extra'][$type] = sprintf($txt['modlog_id'], $entry['extra'][$type]); } } if (empty($entries[$k]['action_text'])) { $entries[$k]['action_text'] = isset($txt['modlog_ac_' . $entry['action']]) ? $txt['modlog_ac_' . $entry['action']] : $entry['action']; } $callback->key = $k; $entries[$k]['action_text'] = preg_replace_callback('~\\{([A-Za-z\\d_]+)\\}~i', array($callback, 'callback'), $entries[$k]['action_text']); } // Back we go! return $entries; }