Yoshi2889 /
SMF2.1
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Handle all of the searching from here. |
||
| 5 | * |
||
| 6 | * Simple Machines Forum (SMF) |
||
| 7 | * |
||
| 8 | * @package SMF |
||
| 9 | * @author Simple Machines http://www.simplemachines.org |
||
| 10 | * @copyright 2019 Simple Machines and individual contributors |
||
| 11 | * @license http://www.simplemachines.org/about/smf/license.php BSD |
||
| 12 | * |
||
| 13 | * @version 2.1 RC2 |
||
| 14 | */ |
||
| 15 | |||
| 16 | if (!defined('SMF')) |
||
| 17 | die('No direct access...'); |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Ask the user what they want to search for. |
||
| 21 | * What it does: |
||
| 22 | * - shows the screen to search forum posts (action=search) |
||
| 23 | * - uses the main sub template of the Search template. |
||
| 24 | * - uses the Search language file. |
||
| 25 | * - requires the search_posts permission. |
||
| 26 | * - decodes and loads search parameters given in the URL (if any). |
||
| 27 | * - the form redirects to index.php?action=search2. |
||
| 28 | */ |
||
| 29 | function PlushSearch1() |
||
| 30 | { |
||
| 31 | global $txt, $scripturl, $modSettings, $user_info, $context, $smcFunc, $sourcedir; |
||
| 32 | |||
| 33 | // Is the load average too high to allow searching just now? |
||
| 34 | if (!empty($context['load_average']) && !empty($modSettings['loadavg_search']) && $context['load_average'] >= $modSettings['loadavg_search']) |
||
| 35 | fatal_lang_error('loadavg_search_disabled', false); |
||
| 36 | |||
| 37 | loadLanguage('Search'); |
||
| 38 | // Don't load this in XML mode. |
||
| 39 | if (!isset($_REQUEST['xml'])) |
||
| 40 | { |
||
| 41 | loadTemplate('Search'); |
||
| 42 | loadJavaScriptFile('suggest.js', array('defer' => false, 'minimize' => true), 'smf_suggest'); |
||
| 43 | } |
||
| 44 | |||
| 45 | // Check the user's permissions. |
||
| 46 | isAllowedTo('search_posts'); |
||
| 47 | |||
| 48 | // Link tree.... |
||
| 49 | $context['linktree'][] = array( |
||
| 50 | 'url' => $scripturl . '?action=search', |
||
| 51 | 'name' => $txt['search'] |
||
| 52 | ); |
||
| 53 | |||
| 54 | // This is hard coded maximum string length. |
||
| 55 | $context['search_string_limit'] = 100; |
||
| 56 | |||
| 57 | $context['require_verification'] = $user_info['is_guest'] && !empty($modSettings['search_enable_captcha']) && empty($_SESSION['ss_vv_passed']); |
||
| 58 | if ($context['require_verification']) |
||
| 59 | { |
||
| 60 | require_once($sourcedir . '/Subs-Editor.php'); |
||
| 61 | $verificationOptions = array( |
||
| 62 | 'id' => 'search', |
||
| 63 | ); |
||
| 64 | $context['require_verification'] = create_control_verification($verificationOptions); |
||
| 65 | $context['visual_verification_id'] = $verificationOptions['id']; |
||
| 66 | } |
||
| 67 | |||
| 68 | // If you got back from search2 by using the linktree, you get your original search parameters back. |
||
| 69 | if (isset($_REQUEST['params'])) |
||
| 70 | { |
||
| 71 | // Due to IE's 2083 character limit, we have to compress long search strings |
||
| 72 | $temp_params = base64_decode(str_replace(array('-', '_', '.'), array('+', '/', '='), $_REQUEST['params'])); |
||
| 73 | // Test for gzuncompress failing |
||
| 74 | $temp_params2 = @gzuncompress($temp_params); |
||
| 75 | $temp_params = explode('|"|', !empty($temp_params2) ? $temp_params2 : $temp_params); |
||
| 76 | |||
| 77 | $context['search_params'] = array(); |
||
| 78 | foreach ($temp_params as $i => $data) |
||
| 79 | { |
||
| 80 | @list ($k, $v) = explode('|\'|', $data); |
||
| 81 | $context['search_params'][$k] = $v; |
||
| 82 | } |
||
| 83 | if (isset($context['search_params']['brd'])) |
||
| 84 | $context['search_params']['brd'] = $context['search_params']['brd'] == '' ? array() : explode(',', $context['search_params']['brd']); |
||
| 85 | } |
||
| 86 | |||
| 87 | if (isset($_REQUEST['search'])) |
||
| 88 | $context['search_params']['search'] = un_htmlspecialchars($_REQUEST['search']); |
||
| 89 | |||
| 90 | if (isset($context['search_params']['search'])) |
||
| 91 | $context['search_params']['search'] = $smcFunc['htmlspecialchars']($context['search_params']['search']); |
||
| 92 | if (isset($context['search_params']['userspec'])) |
||
| 93 | $context['search_params']['userspec'] = $smcFunc['htmlspecialchars']($context['search_params']['userspec']); |
||
| 94 | if (!empty($context['search_params']['searchtype'])) |
||
| 95 | $context['search_params']['searchtype'] = 2; |
||
| 96 | if (!empty($context['search_params']['minage'])) |
||
| 97 | $context['search_params']['minage'] = (int) $context['search_params']['minage']; |
||
| 98 | if (!empty($context['search_params']['maxage'])) |
||
| 99 | $context['search_params']['maxage'] = (int) $context['search_params']['maxage']; |
||
| 100 | |||
| 101 | $context['search_params']['show_complete'] = !empty($context['search_params']['show_complete']); |
||
| 102 | $context['search_params']['subject_only'] = !empty($context['search_params']['subject_only']); |
||
| 103 | |||
| 104 | // Load the error text strings if there were errors in the search. |
||
| 105 | if (!empty($context['search_errors'])) |
||
| 106 | { |
||
| 107 | loadLanguage('Errors'); |
||
| 108 | $context['search_errors']['messages'] = array(); |
||
| 109 | foreach ($context['search_errors'] as $search_error => $dummy) |
||
| 110 | { |
||
| 111 | if ($search_error === 'messages') |
||
| 112 | continue; |
||
| 113 | |||
| 114 | if ($search_error == 'string_too_long') |
||
| 115 | $txt['error_string_too_long'] = sprintf($txt['error_string_too_long'], $context['search_string_limit']); |
||
| 116 | |||
| 117 | $context['search_errors']['messages'][] = $txt['error_' . $search_error]; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | // Find all the boards this user is allowed to see. |
||
| 122 | $request = $smcFunc['db_query']('order_by_board_order', ' |
||
| 123 | SELECT b.id_cat, c.name AS cat_name, b.id_board, b.name, b.child_level |
||
| 124 | FROM {db_prefix}boards AS b |
||
| 125 | LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat) |
||
| 126 | WHERE {query_see_board} |
||
| 127 | AND redirect = {string:empty_string}', |
||
| 128 | array( |
||
| 129 | 'empty_string' => '', |
||
| 130 | ) |
||
| 131 | ); |
||
| 132 | $context['num_boards'] = $smcFunc['db_num_rows']($request); |
||
| 133 | $context['boards_check_all'] = true; |
||
| 134 | $context['categories'] = array(); |
||
| 135 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 136 | { |
||
| 137 | // This category hasn't been set up yet.. |
||
| 138 | if (!isset($context['categories'][$row['id_cat']])) |
||
| 139 | $context['categories'][$row['id_cat']] = array( |
||
| 140 | 'id' => $row['id_cat'], |
||
| 141 | 'name' => $row['cat_name'], |
||
| 142 | 'boards' => array() |
||
| 143 | ); |
||
| 144 | |||
| 145 | // Set this board up, and let the template know when it's a child. (indent them..) |
||
| 146 | $context['categories'][$row['id_cat']]['boards'][$row['id_board']] = array( |
||
| 147 | 'id' => $row['id_board'], |
||
| 148 | 'name' => $row['name'], |
||
| 149 | 'child_level' => $row['child_level'], |
||
| 150 | 'selected' => (empty($context['search_params']['brd']) && (empty($modSettings['recycle_enable']) || $row['id_board'] != $modSettings['recycle_board']) && !in_array($row['id_board'], $user_info['ignoreboards'])) || (!empty($context['search_params']['brd']) && in_array($row['id_board'], $context['search_params']['brd'])) |
||
| 151 | ); |
||
| 152 | |||
| 153 | // If a board wasn't checked that probably should have been ensure the board selection is selected, yo! |
||
| 154 | if (!$context['categories'][$row['id_cat']]['boards'][$row['id_board']]['selected'] && (empty($modSettings['recycle_enable']) || $row['id_board'] != $modSettings['recycle_board'])) |
||
| 155 | $context['boards_check_all'] = false; |
||
| 156 | } |
||
| 157 | $smcFunc['db_free_result']($request); |
||
| 158 | |||
| 159 | require_once($sourcedir . '/Subs-Boards.php'); |
||
| 160 | sortCategories($context['categories']); |
||
| 161 | |||
| 162 | // Now, let's sort the list of categories into the boards for templates that like that. |
||
| 163 | $temp_boards = array(); |
||
| 164 | foreach ($context['categories'] as $category) |
||
| 165 | { |
||
| 166 | $temp_boards[] = array( |
||
| 167 | 'name' => $category['name'], |
||
| 168 | 'child_ids' => array_keys($category['boards']) |
||
| 169 | ); |
||
| 170 | $temp_boards = array_merge($temp_boards, array_values($category['boards'])); |
||
| 171 | |||
| 172 | // Include a list of boards per category for easy toggling. |
||
| 173 | $context['categories'][$category['id']]['child_ids'] = array_keys($category['boards']); |
||
| 174 | } |
||
| 175 | |||
| 176 | $max_boards = ceil(count($temp_boards) / 2); |
||
| 177 | if ($max_boards == 1) |
||
| 178 | $max_boards = 2; |
||
| 179 | |||
| 180 | // Now, alternate them so they can be shown left and right ;). |
||
| 181 | $context['board_columns'] = array(); |
||
| 182 | for ($i = 0; $i < $max_boards; $i++) |
||
| 183 | { |
||
| 184 | $context['board_columns'][] = $temp_boards[$i]; |
||
| 185 | if (isset($temp_boards[$i + $max_boards])) |
||
| 186 | $context['board_columns'][] = $temp_boards[$i + $max_boards]; |
||
| 187 | else |
||
| 188 | $context['board_columns'][] = array(); |
||
| 189 | } |
||
| 190 | |||
| 191 | if (!empty($_REQUEST['topic'])) |
||
| 192 | { |
||
| 193 | $context['search_params']['topic'] = (int) $_REQUEST['topic']; |
||
| 194 | $context['search_params']['show_complete'] = true; |
||
| 195 | } |
||
| 196 | if (!empty($context['search_params']['topic'])) |
||
| 197 | { |
||
| 198 | $context['search_params']['topic'] = (int) $context['search_params']['topic']; |
||
| 199 | |||
| 200 | $context['search_topic'] = array( |
||
| 201 | 'id' => $context['search_params']['topic'], |
||
| 202 | 'href' => $scripturl . '?topic=' . $context['search_params']['topic'] . '.0', |
||
| 203 | ); |
||
| 204 | |||
| 205 | $request = $smcFunc['db_query']('', ' |
||
| 206 | SELECT subject |
||
| 207 | FROM {db_prefix}topics AS t |
||
| 208 | INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg) |
||
| 209 | WHERE t.id_topic = {int:search_topic_id} |
||
| 210 | AND {query_see_message_board} ' . ($modSettings['postmod_active'] ? ' |
||
| 211 | AND t.approved = {int:is_approved_true}' : '') . ' |
||
| 212 | LIMIT 1', |
||
| 213 | array( |
||
| 214 | 'is_approved_true' => 1, |
||
| 215 | 'search_topic_id' => $context['search_params']['topic'], |
||
| 216 | ) |
||
| 217 | ); |
||
| 218 | |||
| 219 | if ($smcFunc['db_num_rows']($request) == 0) |
||
| 220 | fatal_lang_error('topic_gone', false); |
||
| 221 | |||
| 222 | list ($context['search_topic']['subject']) = $smcFunc['db_fetch_row']($request); |
||
| 223 | $smcFunc['db_free_result']($request); |
||
| 224 | |||
| 225 | $context['search_topic']['link'] = '<a href="' . $context['search_topic']['href'] . '">' . $context['search_topic']['subject'] . '</a>'; |
||
| 226 | } |
||
| 227 | |||
| 228 | $context['page_title'] = $txt['set_parameters']; |
||
| 229 | |||
| 230 | call_integration_hook('integrate_search'); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Gather the results and show them. |
||
| 235 | * What it does: |
||
| 236 | * - checks user input and searches the messages table for messages matching the query. |
||
| 237 | * - requires the search_posts permission. |
||
| 238 | * - uses the results sub template of the Search template. |
||
| 239 | * - uses the Search language file. |
||
| 240 | * - stores the results into the search cache. |
||
| 241 | * - show the results of the search query. |
||
| 242 | */ |
||
| 243 | function PlushSearch2() |
||
| 244 | { |
||
| 245 | global $scripturl, $modSettings, $sourcedir, $txt; |
||
| 246 | global $user_info, $context, $options, $messages_request, $boards_can; |
||
| 247 | global $excludedWords, $participants, $smcFunc, $cache_enable; |
||
| 248 | |||
| 249 | // if comming from the quick search box, and we want to search on members, well we need to do that ;) |
||
| 250 | if (isset($_REQUEST['search_selection']) && $_REQUEST['search_selection'] === 'members') |
||
| 251 | redirectexit($scripturl . '?action=mlist;sa=search;fields=name,email;search=' . urlencode($_REQUEST['search'])); |
||
| 252 | |||
| 253 | if (!empty($context['load_average']) && !empty($modSettings['loadavg_search']) && $context['load_average'] >= $modSettings['loadavg_search']) |
||
| 254 | fatal_lang_error('loadavg_search_disabled', false); |
||
| 255 | |||
| 256 | // No, no, no... this is a bit hard on the server, so don't you go prefetching it! |
||
| 257 | if (isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch') |
||
| 258 | { |
||
| 259 | ob_end_clean(); |
||
| 260 | send_http_status(403); |
||
| 261 | die; |
||
|
0 ignored issues
–
show
|
|||
| 262 | } |
||
| 263 | |||
| 264 | $weight_factors = array( |
||
| 265 | 'frequency' => array( |
||
| 266 | 'search' => 'COUNT(*) / (MAX(t.num_replies) + 1)', |
||
| 267 | 'results' => '(t.num_replies + 1)', |
||
| 268 | ), |
||
| 269 | 'age' => array( |
||
| 270 | 'search' => 'CASE WHEN MAX(m.id_msg) < {int:min_msg} THEN 0 ELSE (MAX(m.id_msg) - {int:min_msg}) / {int:recent_message} END', |
||
| 271 | 'results' => 'CASE WHEN t.id_first_msg < {int:min_msg} THEN 0 ELSE (t.id_first_msg - {int:min_msg}) / {int:recent_message} END', |
||
| 272 | ), |
||
| 273 | 'length' => array( |
||
| 274 | 'search' => 'CASE WHEN MAX(t.num_replies) < {int:huge_topic_posts} THEN MAX(t.num_replies) / {int:huge_topic_posts} ELSE 1 END', |
||
| 275 | 'results' => 'CASE WHEN t.num_replies < {int:huge_topic_posts} THEN t.num_replies / {int:huge_topic_posts} ELSE 1 END', |
||
| 276 | ), |
||
| 277 | 'subject' => array( |
||
| 278 | 'search' => 0, |
||
| 279 | 'results' => 0, |
||
| 280 | ), |
||
| 281 | 'first_message' => array( |
||
| 282 | 'search' => 'CASE WHEN MIN(m.id_msg) = MAX(t.id_first_msg) THEN 1 ELSE 0 END', |
||
| 283 | ), |
||
| 284 | 'sticky' => array( |
||
| 285 | 'search' => 'MAX(t.is_sticky)', |
||
| 286 | 'results' => 't.is_sticky', |
||
| 287 | ), |
||
| 288 | ); |
||
| 289 | |||
| 290 | call_integration_hook('integrate_search_weights', array(&$weight_factors)); |
||
| 291 | |||
| 292 | $weight = array(); |
||
| 293 | $weight_total = 0; |
||
| 294 | foreach ($weight_factors as $weight_factor => $value) |
||
| 295 | { |
||
| 296 | $weight[$weight_factor] = empty($modSettings['search_weight_' . $weight_factor]) ? 0 : (int) $modSettings['search_weight_' . $weight_factor]; |
||
| 297 | $weight_total += $weight[$weight_factor]; |
||
| 298 | } |
||
| 299 | |||
| 300 | // Zero weight. Weightless :P. |
||
| 301 | if (empty($weight_total)) |
||
| 302 | fatal_lang_error('search_invalid_weights'); |
||
| 303 | |||
| 304 | // These vars don't require an interface, they're just here for tweaking. |
||
| 305 | $recentPercentage = 0.30; |
||
| 306 | $humungousTopicPosts = 200; |
||
| 307 | $maxMembersToSearch = 500; |
||
| 308 | $maxMessageResults = empty($modSettings['search_max_results']) ? 0 : $modSettings['search_max_results'] * 5; |
||
| 309 | |||
| 310 | // Start with no errors. |
||
| 311 | $context['search_errors'] = array(); |
||
| 312 | |||
| 313 | // Number of pages hard maximum - normally not set at all. |
||
| 314 | $modSettings['search_max_results'] = empty($modSettings['search_max_results']) ? 200 * $modSettings['search_results_per_page'] : (int) $modSettings['search_max_results']; |
||
| 315 | |||
| 316 | // Maximum length of the string. |
||
| 317 | $context['search_string_limit'] = 100; |
||
| 318 | |||
| 319 | loadLanguage('Search'); |
||
| 320 | if (!isset($_REQUEST['xml'])) |
||
| 321 | loadTemplate('Search'); |
||
| 322 | //If we're doing XML we need to use the results template regardless really. |
||
| 323 | else |
||
| 324 | $context['sub_template'] = 'results'; |
||
| 325 | |||
| 326 | // Are you allowed? |
||
| 327 | isAllowedTo('search_posts'); |
||
| 328 | |||
| 329 | require_once($sourcedir . '/Display.php'); |
||
| 330 | require_once($sourcedir . '/Subs-Package.php'); |
||
| 331 | |||
| 332 | // Search has a special database set. |
||
| 333 | db_extend('search'); |
||
| 334 | |||
| 335 | // Load up the search API we are going to use. |
||
| 336 | $searchAPI = findSearchAPI(); |
||
| 337 | |||
| 338 | // $search_params will carry all settings that differ from the default search parameters. |
||
| 339 | // That way, the URLs involved in a search page will be kept as short as possible. |
||
| 340 | $search_params = array(); |
||
| 341 | |||
| 342 | if (isset($_REQUEST['params'])) |
||
| 343 | { |
||
| 344 | // Due to IE's 2083 character limit, we have to compress long search strings |
||
| 345 | $temp_params = base64_decode(str_replace(array('-', '_', '.'), array('+', '/', '='), $_REQUEST['params'])); |
||
| 346 | |||
| 347 | // Test for gzuncompress failing |
||
| 348 | $temp_params2 = @gzuncompress($temp_params); |
||
| 349 | $temp_params = explode('|"|', (!empty($temp_params2) ? $temp_params2 : $temp_params)); |
||
| 350 | |||
| 351 | foreach ($temp_params as $i => $data) |
||
| 352 | { |
||
| 353 | @list($k, $v) = explode('|\'|', $data); |
||
| 354 | $search_params[$k] = $v; |
||
| 355 | } |
||
| 356 | |||
| 357 | if (isset($search_params['brd'])) |
||
| 358 | $search_params['brd'] = empty($search_params['brd']) ? array() : explode(',', $search_params['brd']); |
||
| 359 | } |
||
| 360 | |||
| 361 | // Store whether simple search was used (needed if the user wants to do another query). |
||
| 362 | if (!isset($search_params['advanced'])) |
||
| 363 | $search_params['advanced'] = empty($_REQUEST['advanced']) ? 0 : 1; |
||
| 364 | |||
| 365 | // 1 => 'allwords' (default, don't set as param) / 2 => 'anywords'. |
||
| 366 | if (!empty($search_params['searchtype']) || (!empty($_REQUEST['searchtype']) && $_REQUEST['searchtype'] == 2)) |
||
| 367 | $search_params['searchtype'] = 2; |
||
| 368 | |||
| 369 | // Minimum age of messages. Default to zero (don't set param in that case). |
||
| 370 | if (!empty($search_params['minage']) || (!empty($_REQUEST['minage']) && $_REQUEST['minage'] > 0)) |
||
| 371 | $search_params['minage'] = !empty($search_params['minage']) ? (int) $search_params['minage'] : (int) $_REQUEST['minage']; |
||
| 372 | |||
| 373 | // Maximum age of messages. Default to infinite (9999 days: param not set). |
||
| 374 | if (!empty($search_params['maxage']) || (!empty($_REQUEST['maxage']) && $_REQUEST['maxage'] < 9999)) |
||
| 375 | $search_params['maxage'] = !empty($search_params['maxage']) ? (int) $search_params['maxage'] : (int) $_REQUEST['maxage']; |
||
| 376 | |||
| 377 | // Searching a specific topic? |
||
| 378 | if (!empty($_REQUEST['topic']) || (!empty($_REQUEST['search_selection']) && $_REQUEST['search_selection'] == 'topic')) |
||
| 379 | { |
||
| 380 | $search_params['topic'] = empty($_REQUEST['search_selection']) ? (int) $_REQUEST['topic'] : (isset($_REQUEST['sd_topic']) ? (int) $_REQUEST['sd_topic'] : ''); |
||
| 381 | $search_params['show_complete'] = true; |
||
| 382 | } |
||
| 383 | elseif (!empty($search_params['topic'])) |
||
| 384 | $search_params['topic'] = (int) $search_params['topic']; |
||
| 385 | |||
| 386 | if (!empty($search_params['minage']) || !empty($search_params['maxage'])) |
||
| 387 | { |
||
| 388 | $request = $smcFunc['db_query']('', ' |
||
| 389 | SELECT ' . (empty($search_params['maxage']) ? '0, ' : 'COALESCE(MIN(id_msg), -1), ') . (empty($search_params['minage']) ? '0' : 'COALESCE(MAX(id_msg), -1)') . ' |
||
| 390 | FROM {db_prefix}messages |
||
| 391 | WHERE 1=1' . ($modSettings['postmod_active'] ? ' |
||
| 392 | AND approved = {int:is_approved_true}' : '') . (empty($search_params['minage']) ? '' : ' |
||
| 393 | AND poster_time <= {int:timestamp_minimum_age}') . (empty($search_params['maxage']) ? '' : ' |
||
| 394 | AND poster_time >= {int:timestamp_maximum_age}'), |
||
| 395 | array( |
||
| 396 | 'timestamp_minimum_age' => empty($search_params['minage']) ? 0 : time() - 86400 * $search_params['minage'], |
||
| 397 | 'timestamp_maximum_age' => empty($search_params['maxage']) ? 0 : time() - 86400 * $search_params['maxage'], |
||
| 398 | 'is_approved_true' => 1, |
||
| 399 | ) |
||
| 400 | ); |
||
| 401 | list ($minMsgID, $maxMsgID) = $smcFunc['db_fetch_row']($request); |
||
| 402 | if ($minMsgID < 0 || $maxMsgID < 0) |
||
| 403 | $context['search_errors']['no_messages_in_time_frame'] = true; |
||
| 404 | $smcFunc['db_free_result']($request); |
||
| 405 | } |
||
| 406 | |||
| 407 | // Default the user name to a wildcard matching every user (*). |
||
| 408 | if (!empty($search_params['userspec']) || (!empty($_REQUEST['userspec']) && $_REQUEST['userspec'] != '*')) |
||
| 409 | $search_params['userspec'] = isset($search_params['userspec']) ? $search_params['userspec'] : $_REQUEST['userspec']; |
||
| 410 | |||
| 411 | // If there's no specific user, then don't mention it in the main query. |
||
| 412 | if (empty($search_params['userspec'])) |
||
| 413 | $userQuery = ''; |
||
| 414 | else |
||
| 415 | { |
||
| 416 | $userString = strtr($smcFunc['htmlspecialchars']($search_params['userspec'], ENT_QUOTES), array('"' => '"')); |
||
| 417 | $userString = strtr($userString, array('%' => '\%', '_' => '\_', '*' => '%', '?' => '_')); |
||
| 418 | |||
| 419 | preg_match_all('~"([^"]+)"~', $userString, $matches); |
||
| 420 | $possible_users = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $userString))); |
||
| 421 | |||
| 422 | for ($k = 0, $n = count($possible_users); $k < $n; $k++) |
||
| 423 | { |
||
| 424 | $possible_users[$k] = trim($possible_users[$k]); |
||
| 425 | |||
| 426 | if (strlen($possible_users[$k]) == 0) |
||
| 427 | unset($possible_users[$k]); |
||
| 428 | } |
||
| 429 | |||
| 430 | // Create a list of database-escaped search names. |
||
| 431 | $realNameMatches = array(); |
||
| 432 | foreach ($possible_users as $possible_user) |
||
| 433 | $realNameMatches[] = $smcFunc['db_quote']( |
||
| 434 | '{string:possible_user}', |
||
| 435 | array( |
||
| 436 | 'possible_user' => $possible_user |
||
| 437 | ) |
||
| 438 | ); |
||
| 439 | |||
| 440 | // Retrieve a list of possible members. |
||
| 441 | $request = $smcFunc['db_query']('', ' |
||
| 442 | SELECT id_member |
||
| 443 | FROM {db_prefix}members |
||
| 444 | WHERE {raw:match_possible_users}', |
||
| 445 | array( |
||
| 446 | 'match_possible_users' => 'real_name LIKE ' . implode(' OR real_name LIKE ', $realNameMatches), |
||
| 447 | ) |
||
| 448 | ); |
||
| 449 | // Simply do nothing if there're too many members matching the criteria. |
||
| 450 | if ($smcFunc['db_num_rows']($request) > $maxMembersToSearch) |
||
| 451 | $userQuery = ''; |
||
| 452 | elseif ($smcFunc['db_num_rows']($request) == 0) |
||
| 453 | { |
||
| 454 | $userQuery = $smcFunc['db_quote']( |
||
| 455 | 'm.id_member = {int:id_member_guest} AND ({raw:match_possible_guest_names})', |
||
| 456 | array( |
||
| 457 | 'id_member_guest' => 0, |
||
| 458 | 'match_possible_guest_names' => 'm.poster_name LIKE ' . implode(' OR m.poster_name LIKE ', $realNameMatches), |
||
| 459 | ) |
||
| 460 | ); |
||
| 461 | } |
||
| 462 | else |
||
| 463 | { |
||
| 464 | $memberlist = array(); |
||
| 465 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 466 | $memberlist[] = $row['id_member']; |
||
| 467 | $userQuery = $smcFunc['db_quote']( |
||
| 468 | '(m.id_member IN ({array_int:matched_members}) OR (m.id_member = {int:id_member_guest} AND ({raw:match_possible_guest_names})))', |
||
| 469 | array( |
||
| 470 | 'matched_members' => $memberlist, |
||
| 471 | 'id_member_guest' => 0, |
||
| 472 | 'match_possible_guest_names' => 'm.poster_name LIKE ' . implode(' OR m.poster_name LIKE ', $realNameMatches), |
||
| 473 | ) |
||
| 474 | ); |
||
| 475 | } |
||
| 476 | $smcFunc['db_free_result']($request); |
||
| 477 | } |
||
| 478 | |||
| 479 | // If the boards were passed by URL (params=), temporarily put them back in $_REQUEST. |
||
| 480 | if (!empty($search_params['brd']) && is_array($search_params['brd'])) |
||
| 481 | $_REQUEST['brd'] = $search_params['brd']; |
||
| 482 | |||
| 483 | // Ensure that brd is an array. |
||
| 484 | if ((!empty($_REQUEST['brd']) && !is_array($_REQUEST['brd'])) || (!empty($_REQUEST['search_selection']) && $_REQUEST['search_selection'] == 'board')) |
||
| 485 | { |
||
| 486 | if (!empty($_REQUEST['brd'])) |
||
| 487 | $_REQUEST['brd'] = strpos($_REQUEST['brd'], ',') !== false ? explode(',', $_REQUEST['brd']) : array($_REQUEST['brd']); |
||
| 488 | else |
||
| 489 | $_REQUEST['brd'] = isset($_REQUEST['sd_brd']) ? array($_REQUEST['sd_brd']) : array(); |
||
| 490 | } |
||
| 491 | |||
| 492 | // Make sure all boards are integers. |
||
| 493 | if (!empty($_REQUEST['brd'])) |
||
| 494 | foreach ($_REQUEST['brd'] as $id => $brd) |
||
| 495 | $_REQUEST['brd'][$id] = (int) $brd; |
||
| 496 | |||
| 497 | // Special case for boards: searching just one topic? |
||
| 498 | if (!empty($search_params['topic'])) |
||
| 499 | { |
||
| 500 | $request = $smcFunc['db_query']('', ' |
||
| 501 | SELECT t.id_board |
||
| 502 | FROM {db_prefix}topics AS t |
||
| 503 | WHERE t.id_topic = {int:search_topic_id} |
||
| 504 | AND {query_see_topic_board}' . ($modSettings['postmod_active'] ? ' |
||
| 505 | AND t.approved = {int:is_approved_true}' : '') . ' |
||
| 506 | LIMIT 1', |
||
| 507 | array( |
||
| 508 | 'search_topic_id' => $search_params['topic'], |
||
| 509 | 'is_approved_true' => 1, |
||
| 510 | ) |
||
| 511 | ); |
||
| 512 | |||
| 513 | if ($smcFunc['db_num_rows']($request) == 0) |
||
| 514 | fatal_lang_error('topic_gone', false); |
||
| 515 | |||
| 516 | $search_params['brd'] = array(); |
||
| 517 | list ($search_params['brd'][0]) = $smcFunc['db_fetch_row']($request); |
||
| 518 | $smcFunc['db_free_result']($request); |
||
| 519 | } |
||
| 520 | // Select all boards you've selected AND are allowed to see. |
||
| 521 | elseif ($user_info['is_admin'] && (!empty($search_params['advanced']) || !empty($_REQUEST['brd']))) |
||
| 522 | $search_params['brd'] = empty($_REQUEST['brd']) ? array() : $_REQUEST['brd']; |
||
| 523 | else |
||
| 524 | { |
||
| 525 | $see_board = empty($search_params['advanced']) ? 'query_wanna_see_board' : 'query_see_board'; |
||
| 526 | $request = $smcFunc['db_query']('', ' |
||
| 527 | SELECT b.id_board |
||
| 528 | FROM {db_prefix}boards AS b |
||
| 529 | WHERE {raw:boards_allowed_to_see} |
||
| 530 | AND redirect = {string:empty_string}' . (empty($_REQUEST['brd']) ? (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? ' |
||
| 531 | AND b.id_board != {int:recycle_board_id}' : '') : ' |
||
| 532 | AND b.id_board IN ({array_int:selected_search_boards})'), |
||
| 533 | array( |
||
| 534 | 'boards_allowed_to_see' => $user_info[$see_board], |
||
| 535 | 'empty_string' => '', |
||
| 536 | 'selected_search_boards' => empty($_REQUEST['brd']) ? array() : $_REQUEST['brd'], |
||
| 537 | 'recycle_board_id' => $modSettings['recycle_board'], |
||
| 538 | ) |
||
| 539 | ); |
||
| 540 | $search_params['brd'] = array(); |
||
| 541 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 542 | $search_params['brd'][] = $row['id_board']; |
||
| 543 | $smcFunc['db_free_result']($request); |
||
| 544 | |||
| 545 | // This error should pro'bly only happen for hackers. |
||
| 546 | if (empty($search_params['brd'])) |
||
| 547 | $context['search_errors']['no_boards_selected'] = true; |
||
| 548 | } |
||
| 549 | |||
| 550 | if (count($search_params['brd']) != 0) |
||
| 551 | { |
||
| 552 | foreach ($search_params['brd'] as $k => $v) |
||
| 553 | $search_params['brd'][$k] = (int) $v; |
||
| 554 | |||
| 555 | // If we've selected all boards, this parameter can be left empty. |
||
| 556 | $request = $smcFunc['db_query']('', ' |
||
| 557 | SELECT COUNT(*) |
||
| 558 | FROM {db_prefix}boards |
||
| 559 | WHERE redirect = {string:empty_string}', |
||
| 560 | array( |
||
| 561 | 'empty_string' => '', |
||
| 562 | ) |
||
| 563 | ); |
||
| 564 | list ($num_boards) = $smcFunc['db_fetch_row']($request); |
||
| 565 | $smcFunc['db_free_result']($request); |
||
| 566 | |||
| 567 | if (count($search_params['brd']) == $num_boards) |
||
| 568 | $boardQuery = ''; |
||
| 569 | elseif (count($search_params['brd']) == $num_boards - 1 && !empty($modSettings['recycle_board']) && !in_array($modSettings['recycle_board'], $search_params['brd'])) |
||
| 570 | $boardQuery = '!= ' . $modSettings['recycle_board']; |
||
| 571 | else |
||
| 572 | $boardQuery = 'IN (' . implode(', ', $search_params['brd']) . ')'; |
||
| 573 | } |
||
| 574 | else |
||
| 575 | $boardQuery = ''; |
||
| 576 | |||
| 577 | $search_params['show_complete'] = !empty($search_params['show_complete']) || !empty($_REQUEST['show_complete']); |
||
| 578 | $search_params['subject_only'] = !empty($search_params['subject_only']) || !empty($_REQUEST['subject_only']); |
||
| 579 | |||
| 580 | $context['compact'] = !$search_params['show_complete']; |
||
| 581 | |||
| 582 | // Get the sorting parameters right. Default to sort by relevance descending. |
||
| 583 | $sort_columns = array( |
||
| 584 | 'relevance', |
||
| 585 | 'num_replies', |
||
| 586 | 'id_msg', |
||
| 587 | ); |
||
| 588 | call_integration_hook('integrate_search_sort_columns', array(&$sort_columns)); |
||
| 589 | if (empty($search_params['sort']) && !empty($_REQUEST['sort'])) |
||
| 590 | list ($search_params['sort'], $search_params['sort_dir']) = array_pad(explode('|', $_REQUEST['sort']), 2, ''); |
||
| 591 | $search_params['sort'] = !empty($search_params['sort']) && in_array($search_params['sort'], $sort_columns) ? $search_params['sort'] : 'relevance'; |
||
| 592 | if (!empty($search_params['topic']) && $search_params['sort'] === 'num_replies') |
||
| 593 | $search_params['sort'] = 'id_msg'; |
||
| 594 | |||
| 595 | // Sorting direction: descending unless stated otherwise. |
||
| 596 | $search_params['sort_dir'] = !empty($search_params['sort_dir']) && $search_params['sort_dir'] == 'asc' ? 'asc' : 'desc'; |
||
| 597 | |||
| 598 | // Determine some values needed to calculate the relevance. |
||
| 599 | $minMsg = (int) ((1 - $recentPercentage) * $modSettings['maxMsgID']); |
||
| 600 | $recentMsg = $modSettings['maxMsgID'] - $minMsg; |
||
| 601 | |||
| 602 | // *** Parse the search query |
||
| 603 | call_integration_hook('integrate_search_params', array(&$search_params)); |
||
| 604 | |||
| 605 | /* |
||
| 606 | * Unfortunately, searching for words like this is going to be slow, so we're blacklisting them. |
||
| 607 | * |
||
| 608 | * @todo Setting to add more here? |
||
| 609 | * @todo Maybe only blacklist if they are the only word, or "any" is used? |
||
| 610 | */ |
||
| 611 | $blacklisted_words = array('img', 'url', 'quote', 'www', 'http', 'the', 'is', 'it', 'are', 'if'); |
||
| 612 | call_integration_hook('integrate_search_blacklisted_words', array(&$blacklisted_words)); |
||
| 613 | |||
| 614 | // What are we searching for? |
||
| 615 | if (empty($search_params['search'])) |
||
| 616 | { |
||
| 617 | if (isset($_GET['search'])) |
||
| 618 | $search_params['search'] = un_htmlspecialchars($_GET['search']); |
||
| 619 | elseif (isset($_POST['search'])) |
||
| 620 | $search_params['search'] = $_POST['search']; |
||
| 621 | else |
||
| 622 | $search_params['search'] = ''; |
||
| 623 | } |
||
| 624 | |||
| 625 | // Nothing?? |
||
| 626 | if (!isset($search_params['search']) || $search_params['search'] == '') |
||
| 627 | $context['search_errors']['invalid_search_string'] = true; |
||
| 628 | // Too long? |
||
| 629 | elseif ($smcFunc['strlen']($search_params['search']) > $context['search_string_limit']) |
||
| 630 | { |
||
| 631 | $context['search_errors']['string_too_long'] = true; |
||
| 632 | } |
||
| 633 | |||
| 634 | // Change non-word characters into spaces. |
||
| 635 | $stripped_query = preg_replace('~(?:[\x0B\0' . ($context['utf8'] ? '\x{A0}' : '\xA0') . '\t\r\s\n(){}\\[\\]<>!@$%^*.,:+=`\~\?/\\\\]+|&(?:amp|lt|gt|quot);)+~' . ($context['utf8'] ? 'u' : ''), ' ', $search_params['search']); |
||
| 636 | |||
| 637 | // Make the query lower case. It's gonna be case insensitive anyway. |
||
| 638 | $stripped_query = un_htmlspecialchars($smcFunc['strtolower']($stripped_query)); |
||
| 639 | |||
| 640 | // This (hidden) setting will do fulltext searching in the most basic way. |
||
| 641 | if (!empty($modSettings['search_simple_fulltext'])) |
||
| 642 | $stripped_query = strtr($stripped_query, array('"' => '')); |
||
| 643 | |||
| 644 | $no_regexp = preg_match('~&#(?:\d{1,7}|x[0-9a-fA-F]{1,6});~', $stripped_query) === 1; |
||
| 645 | |||
| 646 | // Extract phrase parts first (e.g. some words "this is a phrase" some more words.) |
||
| 647 | preg_match_all('/(?:^|\s)([-]?)"([^"]+)"(?:$|\s)/', $stripped_query, $matches, PREG_PATTERN_ORDER); |
||
| 648 | $phraseArray = $matches[2]; |
||
| 649 | |||
| 650 | // Remove the phrase parts and extract the words. |
||
| 651 | $wordArray = preg_replace('~(?:^|\s)(?:[-]?)"(?:[^"]+)"(?:$|\s)~' . ($context['utf8'] ? 'u' : ''), ' ', $search_params['search']); |
||
| 652 | $wordArray = explode(' ', $smcFunc['htmlspecialchars'](un_htmlspecialchars($wordArray), ENT_QUOTES)); |
||
| 653 | |||
| 654 | // A minus sign in front of a word excludes the word.... so... |
||
| 655 | $excludedWords = array(); |
||
| 656 | $excludedIndexWords = array(); |
||
| 657 | $excludedSubjectWords = array(); |
||
| 658 | $excludedPhrases = array(); |
||
| 659 | |||
| 660 | // .. first, we check for things like -"some words", but not "-some words". |
||
| 661 | foreach ($matches[1] as $index => $word) |
||
| 662 | { |
||
| 663 | if ($word === '-') |
||
| 664 | { |
||
| 665 | if (($word = trim($phraseArray[$index], '-_\' ')) !== '' && !in_array($word, $blacklisted_words)) |
||
| 666 | $excludedWords[] = $word; |
||
| 667 | unset($phraseArray[$index]); |
||
| 668 | } |
||
| 669 | } |
||
| 670 | |||
| 671 | // Now we look for -test, etc.... normaller. |
||
| 672 | foreach ($wordArray as $index => $word) |
||
| 673 | { |
||
| 674 | if (strpos(trim($word), '-') === 0) |
||
| 675 | { |
||
| 676 | if (($word = trim($word, '-_\' ')) !== '' && !in_array($word, $blacklisted_words)) |
||
| 677 | $excludedWords[] = $word; |
||
| 678 | unset($wordArray[$index]); |
||
| 679 | } |
||
| 680 | } |
||
| 681 | |||
| 682 | // The remaining words and phrases are all included. |
||
| 683 | $searchArray = array_merge($phraseArray, $wordArray); |
||
| 684 | |||
| 685 | $context['search_ignored'] = array(); |
||
| 686 | // Trim everything and make sure there are no words that are the same. |
||
| 687 | foreach ($searchArray as $index => $value) |
||
| 688 | { |
||
| 689 | // Skip anything practically empty. |
||
| 690 | if (($searchArray[$index] = trim($value, '-_\' ')) === '') |
||
| 691 | unset($searchArray[$index]); |
||
| 692 | // Skip blacklisted words. Make sure to note we skipped them in case we end up with nothing. |
||
| 693 | elseif (in_array($searchArray[$index], $blacklisted_words)) |
||
| 694 | { |
||
| 695 | $foundBlackListedWords = true; |
||
| 696 | unset($searchArray[$index]); |
||
| 697 | } |
||
| 698 | // Don't allow very, very short words. |
||
| 699 | elseif ($smcFunc['strlen']($value) < 2) |
||
| 700 | { |
||
| 701 | $context['search_ignored'][] = $value; |
||
| 702 | unset($searchArray[$index]); |
||
| 703 | } |
||
| 704 | } |
||
| 705 | $searchArray = array_slice(array_unique($searchArray), 0, 10); |
||
| 706 | |||
| 707 | // Create an array of replacements for highlighting. |
||
| 708 | $context['mark'] = array(); |
||
| 709 | foreach ($searchArray as $word) |
||
| 710 | $context['mark'][$word] = '<strong class="highlight">' . $word . '</strong>'; |
||
| 711 | |||
| 712 | // Initialize two arrays storing the words that have to be searched for. |
||
| 713 | $orParts = array(); |
||
| 714 | $searchWords = array(); |
||
| 715 | |||
| 716 | // Make sure at least one word is being searched for. |
||
| 717 | if (empty($searchArray)) |
||
| 718 | $context['search_errors']['invalid_search_string' . (!empty($foundBlackListedWords) ? '_blacklist' : '')] = true; |
||
| 719 | // All words/sentences must match. |
||
| 720 | elseif (empty($search_params['searchtype'])) |
||
| 721 | $orParts[0] = $searchArray; |
||
| 722 | // Any word/sentence must match. |
||
| 723 | else |
||
| 724 | foreach ($searchArray as $index => $value) |
||
| 725 | $orParts[$index] = array($value); |
||
| 726 | |||
| 727 | // Don't allow duplicate error messages if one string is too short. |
||
| 728 | if (isset($context['search_errors']['search_string_small_words'], $context['search_errors']['invalid_search_string'])) |
||
| 729 | unset($context['search_errors']['invalid_search_string']); |
||
| 730 | // Make sure the excluded words are in all or-branches. |
||
| 731 | foreach ($orParts as $orIndex => $andParts) |
||
| 732 | foreach ($excludedWords as $word) |
||
| 733 | $orParts[$orIndex][] = $word; |
||
| 734 | |||
| 735 | // Determine the or-branches and the fulltext search words. |
||
| 736 | foreach ($orParts as $orIndex => $andParts) |
||
| 737 | { |
||
| 738 | $searchWords[$orIndex] = array( |
||
| 739 | 'indexed_words' => array(), |
||
| 740 | 'words' => array(), |
||
| 741 | 'subject_words' => array(), |
||
| 742 | 'all_words' => array(), |
||
| 743 | 'complex_words' => array(), |
||
| 744 | ); |
||
| 745 | |||
| 746 | // Sort the indexed words (large words -> small words -> excluded words). |
||
| 747 | if ($searchAPI->supportsMethod('searchSort')) |
||
| 748 | usort($orParts[$orIndex], 'searchSort'); |
||
| 749 | |||
| 750 | foreach ($orParts[$orIndex] as $word) |
||
| 751 | { |
||
| 752 | $is_excluded = in_array($word, $excludedWords); |
||
| 753 | |||
| 754 | $searchWords[$orIndex]['all_words'][] = $word; |
||
| 755 | |||
| 756 | $subjectWords = text2words($word); |
||
| 757 | if (!$is_excluded || count($subjectWords) === 1) |
||
| 758 | { |
||
| 759 | $searchWords[$orIndex]['subject_words'] = array_merge($searchWords[$orIndex]['subject_words'], $subjectWords); |
||
| 760 | if ($is_excluded) |
||
| 761 | $excludedSubjectWords = array_merge($excludedSubjectWords, $subjectWords); |
||
| 762 | } |
||
| 763 | else |
||
| 764 | $excludedPhrases[] = $word; |
||
| 765 | |||
| 766 | // Have we got indexes to prepare? |
||
| 767 | if ($searchAPI->supportsMethod('prepareIndexes')) |
||
| 768 | $searchAPI->prepareIndexes($word, $searchWords[$orIndex], $excludedIndexWords, $is_excluded); |
||
| 769 | } |
||
| 770 | |||
| 771 | // Search_force_index requires all AND parts to have at least one fulltext word. |
||
| 772 | if (!empty($modSettings['search_force_index']) && empty($searchWords[$orIndex]['indexed_words'])) |
||
| 773 | { |
||
| 774 | $context['search_errors']['query_not_specific_enough'] = true; |
||
| 775 | break; |
||
| 776 | } |
||
| 777 | elseif ($search_params['subject_only'] && empty($searchWords[$orIndex]['subject_words']) && empty($excludedSubjectWords)) |
||
| 778 | { |
||
| 779 | $context['search_errors']['query_not_specific_enough'] = true; |
||
| 780 | break; |
||
| 781 | } |
||
| 782 | |||
| 783 | // Make sure we aren't searching for too many indexed words. |
||
| 784 | else |
||
| 785 | { |
||
| 786 | $searchWords[$orIndex]['indexed_words'] = array_slice($searchWords[$orIndex]['indexed_words'], 0, 7); |
||
| 787 | $searchWords[$orIndex]['subject_words'] = array_slice($searchWords[$orIndex]['subject_words'], 0, 7); |
||
| 788 | $searchWords[$orIndex]['words'] = array_slice($searchWords[$orIndex]['words'], 0, 4); |
||
| 789 | } |
||
| 790 | } |
||
| 791 | |||
| 792 | // *** Spell checking |
||
| 793 | $context['show_spellchecking'] = !empty($modSettings['enableSpellChecking']) && (function_exists('pspell_new') || (function_exists('enchant_broker_init') && ($txt['lang_character_set'] == 'UTF-8' || function_exists('iconv')))); |
||
| 794 | if ($context['show_spellchecking']) |
||
| 795 | { |
||
| 796 | require_once($sourcedir . '/Subs-Post.php'); |
||
| 797 | |||
| 798 | // Don't hardcode spellchecking functions! |
||
| 799 | $link = spell_init(); |
||
| 800 | |||
| 801 | $did_you_mean = array('search' => array(), 'display' => array()); |
||
| 802 | $found_misspelling = false; |
||
| 803 | foreach ($searchArray as $word) |
||
| 804 | { |
||
| 805 | if (empty($link)) |
||
| 806 | continue; |
||
| 807 | |||
| 808 | // Don't check phrases. |
||
| 809 | if (preg_match('~^\w+$~', $word) === 0) |
||
| 810 | { |
||
| 811 | $did_you_mean['search'][] = '"' . $word . '"'; |
||
| 812 | $did_you_mean['display'][] = '"' . $smcFunc['htmlspecialchars']($word) . '"'; |
||
| 813 | continue; |
||
| 814 | } |
||
| 815 | // For some strange reason spell check can crash PHP on decimals. |
||
| 816 | elseif (preg_match('~\d~', $word) === 1) |
||
| 817 | { |
||
| 818 | $did_you_mean['search'][] = $word; |
||
| 819 | $did_you_mean['display'][] = $smcFunc['htmlspecialchars']($word); |
||
| 820 | continue; |
||
| 821 | } |
||
| 822 | elseif (spell_check($link, $word)) |
||
| 823 | { |
||
| 824 | $did_you_mean['search'][] = $word; |
||
| 825 | $did_you_mean['display'][] = $smcFunc['htmlspecialchars']($word); |
||
| 826 | continue; |
||
| 827 | } |
||
| 828 | |||
| 829 | $suggestions = spell_suggest($link, $word); |
||
| 830 | foreach ($suggestions as $i => $s) |
||
| 831 | { |
||
| 832 | // Search is case insensitive. |
||
| 833 | if ($smcFunc['strtolower']($s) == $smcFunc['strtolower']($word)) |
||
| 834 | unset($suggestions[$i]); |
||
| 835 | // Plus, don't suggest something the user thinks is rude! |
||
| 836 | elseif ($suggestions[$i] != censorText($s)) |
||
| 837 | unset($suggestions[$i]); |
||
| 838 | } |
||
| 839 | |||
| 840 | // Anything found? If so, correct it! |
||
| 841 | if (!empty($suggestions)) |
||
| 842 | { |
||
| 843 | $suggestions = array_values($suggestions); |
||
| 844 | $did_you_mean['search'][] = $suggestions[0]; |
||
| 845 | $did_you_mean['display'][] = '<em><strong>' . $smcFunc['htmlspecialchars']($suggestions[0]) . '</strong></em>'; |
||
| 846 | $found_misspelling = true; |
||
| 847 | } |
||
| 848 | else |
||
| 849 | { |
||
| 850 | $did_you_mean['search'][] = $word; |
||
| 851 | $did_you_mean['display'][] = $smcFunc['htmlspecialchars']($word); |
||
| 852 | } |
||
| 853 | } |
||
| 854 | |||
| 855 | if ($found_misspelling) |
||
| 856 | { |
||
| 857 | // Don't spell check excluded words, but add them still... |
||
| 858 | $temp_excluded = array('search' => array(), 'display' => array()); |
||
| 859 | foreach ($excludedWords as $word) |
||
| 860 | { |
||
| 861 | if (preg_match('~^\w+$~', $word) == 0) |
||
| 862 | { |
||
| 863 | $temp_excluded['search'][] = '-"' . $word . '"'; |
||
| 864 | $temp_excluded['display'][] = '-"' . $smcFunc['htmlspecialchars']($word) . '"'; |
||
| 865 | } |
||
| 866 | else |
||
| 867 | { |
||
| 868 | $temp_excluded['search'][] = '-' . $word; |
||
| 869 | $temp_excluded['display'][] = '-' . $smcFunc['htmlspecialchars']($word); |
||
| 870 | } |
||
| 871 | } |
||
| 872 | |||
| 873 | $did_you_mean['search'] = array_merge($did_you_mean['search'], $temp_excluded['search']); |
||
| 874 | $did_you_mean['display'] = array_merge($did_you_mean['display'], $temp_excluded['display']); |
||
| 875 | |||
| 876 | $temp_params = $search_params; |
||
| 877 | $temp_params['search'] = implode(' ', $did_you_mean['search']); |
||
| 878 | if (isset($temp_params['brd'])) |
||
| 879 | $temp_params['brd'] = implode(',', $temp_params['brd']); |
||
| 880 | $context['params'] = array(); |
||
| 881 | foreach ($temp_params as $k => $v) |
||
| 882 | $context['did_you_mean_params'][] = $k . '|\'|' . $v; |
||
| 883 | $context['did_you_mean_params'] = base64_encode(implode('|"|', $context['did_you_mean_params'])); |
||
| 884 | $context['did_you_mean'] = implode(' ', $did_you_mean['display']); |
||
| 885 | } |
||
| 886 | } |
||
| 887 | |||
| 888 | // Let the user adjust the search query, should they wish? |
||
| 889 | $context['search_params'] = $search_params; |
||
| 890 | if (isset($context['search_params']['search'])) |
||
| 891 | $context['search_params']['search'] = $smcFunc['htmlspecialchars']($context['search_params']['search']); |
||
| 892 | if (isset($context['search_params']['userspec'])) |
||
| 893 | $context['search_params']['userspec'] = $smcFunc['htmlspecialchars']($context['search_params']['userspec']); |
||
| 894 | |||
| 895 | // Do we have captcha enabled? |
||
| 896 | 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'])) |
||
| 897 | { |
||
| 898 | require_once($sourcedir . '/Subs-Editor.php'); |
||
| 899 | $verificationOptions = array( |
||
| 900 | 'id' => 'search', |
||
| 901 | ); |
||
| 902 | $context['require_verification'] = create_control_verification($verificationOptions, true); |
||
| 903 | |||
| 904 | if (is_array($context['require_verification'])) |
||
| 905 | { |
||
| 906 | foreach ($context['require_verification'] as $error) |
||
| 907 | $context['search_errors'][$error] = true; |
||
| 908 | } |
||
| 909 | // Don't keep asking for it - they've proven themselves worthy. |
||
| 910 | else |
||
| 911 | $_SESSION['ss_vv_passed'] = true; |
||
| 912 | } |
||
| 913 | |||
| 914 | // *** Encode all search params |
||
| 915 | |||
| 916 | // All search params have been checked, let's compile them to a single string... made less simple by PHP 4.3.9 and below. |
||
| 917 | $temp_params = $search_params; |
||
| 918 | if (isset($temp_params['brd'])) |
||
| 919 | $temp_params['brd'] = implode(',', $temp_params['brd']); |
||
| 920 | $context['params'] = array(); |
||
| 921 | foreach ($temp_params as $k => $v) |
||
| 922 | $context['params'][] = $k . '|\'|' . $v; |
||
| 923 | |||
| 924 | if (!empty($context['params'])) |
||
| 925 | { |
||
| 926 | // Due to old IE's 2083 character limit, we have to compress long search strings |
||
| 927 | $params = @gzcompress(implode('|"|', $context['params'])); |
||
| 928 | // Gzcompress failed, use try non-gz |
||
| 929 | if (empty($params)) |
||
| 930 | $params = implode('|"|', $context['params']); |
||
| 931 | // Base64 encode, then replace +/= with uri safe ones that can be reverted |
||
| 932 | $context['params'] = str_replace(array('+', '/', '='), array('-', '_', '.'), base64_encode($params)); |
||
| 933 | } |
||
| 934 | |||
| 935 | // ... and add the links to the link tree. |
||
| 936 | $context['linktree'][] = array( |
||
| 937 | 'url' => $scripturl . '?action=search;params=' . $context['params'], |
||
| 938 | 'name' => $txt['search'] |
||
| 939 | ); |
||
| 940 | $context['linktree'][] = array( |
||
| 941 | 'url' => $scripturl . '?action=search2;params=' . $context['params'], |
||
| 942 | 'name' => $txt['search_results'] |
||
| 943 | ); |
||
| 944 | |||
| 945 | // *** A last error check |
||
| 946 | call_integration_hook('integrate_search_errors'); |
||
| 947 | |||
| 948 | // One or more search errors? Go back to the first search screen. |
||
| 949 | if (!empty($context['search_errors'])) |
||
| 950 | { |
||
| 951 | $_REQUEST['params'] = $context['params']; |
||
| 952 | return PlushSearch1(); |
||
| 953 | } |
||
| 954 | |||
| 955 | // Spam me not, Spam-a-lot? |
||
| 956 | if (empty($_SESSION['last_ss']) || $_SESSION['last_ss'] != $search_params['search']) |
||
| 957 | spamProtection('search'); |
||
| 958 | // Store the last search string to allow pages of results to be browsed. |
||
| 959 | $_SESSION['last_ss'] = $search_params['search']; |
||
| 960 | |||
| 961 | // *** Reserve an ID for caching the search results. |
||
| 962 | $query_params = array_merge($search_params, array( |
||
| 963 | 'min_msg_id' => isset($minMsgID) ? (int) $minMsgID : 0, |
||
| 964 | 'max_msg_id' => isset($maxMsgID) ? (int) $maxMsgID : 0, |
||
| 965 | 'memberlist' => !empty($memberlist) ? $memberlist : array(), |
||
| 966 | )); |
||
| 967 | |||
| 968 | // Can this search rely on the API given the parameters? |
||
| 969 | if ($searchAPI->supportsMethod('searchQuery', $query_params)) |
||
| 970 | { |
||
| 971 | $participants = array(); |
||
| 972 | $searchArray = array(); |
||
| 973 | |||
| 974 | $searchAPI->searchQuery($query_params, $searchWords, $excludedIndexWords, $participants, $searchArray); |
||
| 975 | } |
||
| 976 | |||
| 977 | // Update the cache if the current search term is not yet cached. |
||
| 978 | else |
||
| 979 | { |
||
| 980 | $update_cache = empty($_SESSION['search_cache']) || ($_SESSION['search_cache']['params'] != $context['params']); |
||
| 981 | // Are the result fresh? |
||
| 982 | if (!$update_cache && !empty($_SESSION['search_cache']['id_search'])) |
||
| 983 | { |
||
| 984 | $request = $smcFunc['db_query']('', ' |
||
| 985 | SELECT id_search |
||
| 986 | FROM {db_prefix}log_search_results |
||
| 987 | WHERE id_search = {int:search_id} |
||
| 988 | LIMIT 1', |
||
| 989 | array( |
||
| 990 | 'search_id' => $_SESSION['search_cache']['id_search'], |
||
| 991 | ) |
||
| 992 | ); |
||
| 993 | |||
| 994 | if ($smcFunc['db_num_rows']($request) === 0) |
||
| 995 | $update_cache = true; |
||
| 996 | } |
||
| 997 | |||
| 998 | if ($update_cache) |
||
| 999 | { |
||
| 1000 | // Increase the pointer... |
||
| 1001 | $modSettings['search_pointer'] = empty($modSettings['search_pointer']) ? 0 : (int) $modSettings['search_pointer']; |
||
| 1002 | // ...and store it right off. |
||
| 1003 | updateSettings(array('search_pointer' => $modSettings['search_pointer'] >= 255 ? 0 : $modSettings['search_pointer'] + 1)); |
||
| 1004 | // As long as you don't change the parameters, the cache result is yours. |
||
| 1005 | $_SESSION['search_cache'] = array( |
||
| 1006 | 'id_search' => $modSettings['search_pointer'], |
||
| 1007 | 'num_results' => -1, |
||
| 1008 | 'params' => $context['params'], |
||
| 1009 | ); |
||
| 1010 | |||
| 1011 | // Clear the previous cache of the final results cache. |
||
| 1012 | $smcFunc['db_search_query']('delete_log_search_results', ' |
||
| 1013 | DELETE FROM {db_prefix}log_search_results |
||
| 1014 | WHERE id_search = {int:search_id}', |
||
| 1015 | array( |
||
| 1016 | 'search_id' => $_SESSION['search_cache']['id_search'], |
||
| 1017 | ) |
||
| 1018 | ); |
||
| 1019 | |||
| 1020 | if ($search_params['subject_only']) |
||
| 1021 | { |
||
| 1022 | // We do this to try and avoid duplicate keys on databases not supporting INSERT IGNORE. |
||
| 1023 | $inserts = array(); |
||
| 1024 | foreach ($searchWords as $orIndex => $words) |
||
| 1025 | { |
||
| 1026 | $subject_query_params = array(); |
||
| 1027 | $subject_query = array( |
||
| 1028 | 'from' => '{db_prefix}topics AS t', |
||
| 1029 | 'inner_join' => array(), |
||
| 1030 | 'left_join' => array(), |
||
| 1031 | 'where' => array(), |
||
| 1032 | ); |
||
| 1033 | |||
| 1034 | if ($modSettings['postmod_active']) |
||
| 1035 | $subject_query['where'][] = 't.approved = {int:is_approved}'; |
||
| 1036 | |||
| 1037 | $numTables = 0; |
||
| 1038 | $prev_join = 0; |
||
| 1039 | $numSubjectResults = 0; |
||
| 1040 | foreach ($words['subject_words'] as $subjectWord) |
||
| 1041 | { |
||
| 1042 | $numTables++; |
||
| 1043 | if (in_array($subjectWord, $excludedSubjectWords)) |
||
| 1044 | { |
||
| 1045 | $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)'; |
||
| 1046 | $subject_query['where'][] = '(subj' . $numTables . '.word IS NULL)'; |
||
| 1047 | } |
||
| 1048 | else |
||
| 1049 | { |
||
| 1050 | $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)'; |
||
| 1051 | $subject_query['where'][] = 'subj' . $numTables . '.word ' . (empty($modSettings['search_match_words']) ? 'LIKE {string:subject_words_' . $numTables . '_wild}' : '= {string:subject_words_' . $numTables . '}'); |
||
| 1052 | $prev_join = $numTables; |
||
| 1053 | } |
||
| 1054 | $subject_query_params['subject_words_' . $numTables] = $subjectWord; |
||
| 1055 | $subject_query_params['subject_words_' . $numTables . '_wild'] = '%' . $subjectWord . '%'; |
||
| 1056 | } |
||
| 1057 | |||
| 1058 | if (!empty($userQuery)) |
||
| 1059 | { |
||
| 1060 | if ($subject_query['from'] != '{db_prefix}messages AS m') |
||
| 1061 | { |
||
| 1062 | $subject_query['inner_join'][] = '{db_prefix}messages AS m ON (m.id_topic = t.id_topic)'; |
||
| 1063 | } |
||
| 1064 | $subject_query['where'][] = $userQuery; |
||
| 1065 | } |
||
| 1066 | if (!empty($search_params['topic'])) |
||
| 1067 | $subject_query['where'][] = 't.id_topic = ' . $search_params['topic']; |
||
| 1068 | if (!empty($minMsgID)) |
||
| 1069 | $subject_query['where'][] = 't.id_first_msg >= ' . $minMsgID; |
||
| 1070 | if (!empty($maxMsgID)) |
||
| 1071 | $subject_query['where'][] = 't.id_last_msg <= ' . $maxMsgID; |
||
| 1072 | if (!empty($boardQuery)) |
||
| 1073 | $subject_query['where'][] = 't.id_board ' . $boardQuery; |
||
| 1074 | if (!empty($excludedPhrases)) |
||
| 1075 | { |
||
| 1076 | if ($subject_query['from'] != '{db_prefix}messages AS m') |
||
| 1077 | { |
||
| 1078 | $subject_query['inner_join'][] = '{db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)'; |
||
| 1079 | } |
||
| 1080 | $count = 0; |
||
| 1081 | foreach ($excludedPhrases as $phrase) |
||
| 1082 | { |
||
| 1083 | $subject_query['where'][] = 'm.subject NOT ' . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:excluded_phrases_' . $count . '}'; |
||
| 1084 | $subject_query_params['excluded_phrases_' . $count++] = empty($modSettings['search_match_words']) || $no_regexp ? '%' . strtr($phrase, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $phrase), '\\\'') . '[[:>:]]'; |
||
| 1085 | } |
||
| 1086 | } |
||
| 1087 | call_integration_hook('integrate_subject_only_search_query', array(&$subject_query, &$subject_query_params)); |
||
| 1088 | |||
| 1089 | $relevance = '1000 * ('; |
||
| 1090 | foreach ($weight_factors as $type => $value) |
||
| 1091 | { |
||
| 1092 | $relevance .= $weight[$type]; |
||
| 1093 | if (!empty($value['results'])) |
||
| 1094 | $relevance .= ' * ' . $value['results']; |
||
| 1095 | $relevance .= ' + '; |
||
| 1096 | } |
||
| 1097 | $relevance = substr($relevance, 0, -3) . ') / ' . $weight_total . ' AS relevance'; |
||
| 1098 | |||
| 1099 | $ignoreRequest = $smcFunc['db_search_query']('insert_log_search_results_subject', |
||
| 1100 | ($smcFunc['db_support_ignore'] ? ' |
||
| 1101 | INSERT IGNORE INTO {db_prefix}log_search_results |
||
| 1102 | (id_search, id_topic, relevance, id_msg, num_matches)' : '') . ' |
||
| 1103 | SELECT |
||
| 1104 | {int:id_search}, |
||
| 1105 | t.id_topic, |
||
| 1106 | ' . $relevance . ', |
||
| 1107 | ' . (empty($userQuery) ? 't.id_first_msg' : 'm.id_msg') . ', |
||
| 1108 | 1 |
||
| 1109 | FROM ' . $subject_query['from'] . (empty($subject_query['inner_join']) ? '' : ' |
||
| 1110 | INNER JOIN ' . implode(' |
||
| 1111 | INNER JOIN ', $subject_query['inner_join'])) . (empty($subject_query['left_join']) ? '' : ' |
||
| 1112 | LEFT JOIN ' . implode(' |
||
| 1113 | LEFT JOIN ', $subject_query['left_join'])) . ' |
||
| 1114 | WHERE ' . implode(' |
||
| 1115 | AND ', $subject_query['where']) . (empty($modSettings['search_max_results']) ? '' : ' |
||
| 1116 | LIMIT ' . ($modSettings['search_max_results'] - $numSubjectResults)), |
||
| 1117 | array_merge($subject_query_params, array( |
||
| 1118 | 'id_search' => $_SESSION['search_cache']['id_search'], |
||
| 1119 | 'min_msg' => $minMsg, |
||
| 1120 | 'recent_message' => $recentMsg, |
||
| 1121 | 'huge_topic_posts' => $humungousTopicPosts, |
||
| 1122 | 'is_approved' => 1, |
||
| 1123 | )) |
||
| 1124 | ); |
||
| 1125 | |||
| 1126 | // If the database doesn't support IGNORE to make this fast we need to do some tracking. |
||
| 1127 | if (!$smcFunc['db_support_ignore']) |
||
| 1128 | { |
||
| 1129 | while ($row = $smcFunc['db_fetch_row']($ignoreRequest)) |
||
| 1130 | { |
||
| 1131 | // No duplicates! |
||
| 1132 | if (isset($inserts[$row[1]])) |
||
| 1133 | continue; |
||
| 1134 | |||
| 1135 | foreach ($row as $key => $value) |
||
| 1136 | $inserts[$row[1]][] = (int) $row[$key]; |
||
| 1137 | } |
||
| 1138 | $smcFunc['db_free_result']($ignoreRequest); |
||
| 1139 | $numSubjectResults = count($inserts); |
||
| 1140 | } |
||
| 1141 | else |
||
| 1142 | $numSubjectResults += $smcFunc['db_affected_rows'](); |
||
| 1143 | |||
| 1144 | if (!empty($modSettings['search_max_results']) && $numSubjectResults >= $modSettings['search_max_results']) |
||
| 1145 | break; |
||
| 1146 | } |
||
| 1147 | |||
| 1148 | // If there's data to be inserted for non-IGNORE databases do it here! |
||
| 1149 | if (!empty($inserts)) |
||
| 1150 | { |
||
| 1151 | $smcFunc['db_insert']('', |
||
| 1152 | '{db_prefix}log_search_results', |
||
| 1153 | array('id_search' => 'int', 'id_topic' => 'int', 'relevance' => 'int', 'id_msg' => 'int', 'num_matches' => 'int'), |
||
| 1154 | $inserts, |
||
| 1155 | array('id_search', 'id_topic') |
||
| 1156 | ); |
||
| 1157 | } |
||
| 1158 | |||
| 1159 | $_SESSION['search_cache']['num_results'] = $numSubjectResults; |
||
| 1160 | } |
||
| 1161 | else |
||
| 1162 | { |
||
| 1163 | $main_query = array( |
||
| 1164 | 'select' => array( |
||
| 1165 | 'id_search' => $_SESSION['search_cache']['id_search'], |
||
| 1166 | 'relevance' => '0', |
||
| 1167 | ), |
||
| 1168 | 'weights' => array(), |
||
| 1169 | 'from' => '{db_prefix}topics AS t', |
||
| 1170 | 'inner_join' => array( |
||
| 1171 | '{db_prefix}messages AS m ON (m.id_topic = t.id_topic)' |
||
| 1172 | ), |
||
| 1173 | 'left_join' => array(), |
||
| 1174 | 'where' => array(), |
||
| 1175 | 'group_by' => array(), |
||
| 1176 | 'parameters' => array( |
||
| 1177 | 'min_msg' => $minMsg, |
||
| 1178 | 'recent_message' => $recentMsg, |
||
| 1179 | 'huge_topic_posts' => $humungousTopicPosts, |
||
| 1180 | 'is_approved' => 1, |
||
| 1181 | ), |
||
| 1182 | ); |
||
| 1183 | |||
| 1184 | if (empty($search_params['topic']) && empty($search_params['show_complete'])) |
||
| 1185 | { |
||
| 1186 | $main_query['select']['id_topic'] = 't.id_topic'; |
||
| 1187 | $main_query['select']['id_msg'] = 'MAX(m.id_msg) AS id_msg'; |
||
| 1188 | $main_query['select']['num_matches'] = 'COUNT(*) AS num_matches'; |
||
| 1189 | |||
| 1190 | $main_query['weights'] = $weight_factors; |
||
| 1191 | |||
| 1192 | $main_query['group_by'][] = 't.id_topic'; |
||
| 1193 | } |
||
| 1194 | else |
||
| 1195 | { |
||
| 1196 | // This is outrageous! |
||
| 1197 | $main_query['select']['id_topic'] = 'm.id_msg AS id_topic'; |
||
| 1198 | $main_query['select']['id_msg'] = 'm.id_msg'; |
||
| 1199 | $main_query['select']['num_matches'] = '1 AS num_matches'; |
||
| 1200 | |||
| 1201 | $main_query['weights'] = array( |
||
| 1202 | 'age' => array( |
||
| 1203 | '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)', |
||
| 1204 | ), |
||
| 1205 | 'first_message' => array( |
||
| 1206 | 'search' => 'CASE WHEN m.id_msg = t.id_first_msg THEN 1 ELSE 0 END', |
||
| 1207 | ), |
||
| 1208 | ); |
||
| 1209 | |||
| 1210 | if (!empty($search_params['topic'])) |
||
| 1211 | { |
||
| 1212 | $main_query['where'][] = 't.id_topic = {int:topic}'; |
||
| 1213 | $main_query['parameters']['topic'] = $search_params['topic']; |
||
| 1214 | } |
||
| 1215 | if (!empty($search_params['show_complete'])) |
||
| 1216 | $main_query['group_by'][] = 'm.id_msg, t.id_first_msg, t.id_last_msg'; |
||
| 1217 | } |
||
| 1218 | |||
| 1219 | // *** Get the subject results. |
||
| 1220 | $numSubjectResults = 0; |
||
| 1221 | if (empty($search_params['topic'])) |
||
| 1222 | { |
||
| 1223 | $inserts = array(); |
||
| 1224 | // Create a temporary table to store some preliminary results in. |
||
| 1225 | $smcFunc['db_search_query']('drop_tmp_log_search_topics', ' |
||
| 1226 | DROP TABLE IF EXISTS {db_prefix}tmp_log_search_topics', |
||
| 1227 | array( |
||
| 1228 | ) |
||
| 1229 | ); |
||
| 1230 | $createTemporary = $smcFunc['db_search_query']('create_tmp_log_search_topics', ' |
||
| 1231 | CREATE TEMPORARY TABLE {db_prefix}tmp_log_search_topics ( |
||
| 1232 | id_topic int NOT NULL default {string:string_zero}, |
||
| 1233 | PRIMARY KEY (id_topic) |
||
| 1234 | ) ENGINE=MEMORY', |
||
| 1235 | array( |
||
| 1236 | 'string_zero' => '0', |
||
| 1237 | ) |
||
| 1238 | ) !== false; |
||
| 1239 | |||
| 1240 | // Clean up some previous cache. |
||
| 1241 | if (!$createTemporary) |
||
| 1242 | $smcFunc['db_search_query']('delete_log_search_topics', ' |
||
| 1243 | DELETE FROM {db_prefix}log_search_topics |
||
| 1244 | WHERE id_search = {int:search_id}', |
||
| 1245 | array( |
||
| 1246 | 'search_id' => $_SESSION['search_cache']['id_search'], |
||
| 1247 | ) |
||
| 1248 | ); |
||
| 1249 | |||
| 1250 | foreach ($searchWords as $orIndex => $words) |
||
| 1251 | { |
||
| 1252 | $subject_query = array( |
||
| 1253 | 'from' => '{db_prefix}topics AS t', |
||
| 1254 | 'inner_join' => array(), |
||
| 1255 | 'left_join' => array(), |
||
| 1256 | 'where' => array(), |
||
| 1257 | 'params' => array(), |
||
| 1258 | ); |
||
| 1259 | |||
| 1260 | $numTables = 0; |
||
| 1261 | $prev_join = 0; |
||
| 1262 | $count = 0; |
||
| 1263 | $excluded = false; |
||
| 1264 | foreach ($words['subject_words'] as $subjectWord) |
||
| 1265 | { |
||
| 1266 | $numTables++; |
||
| 1267 | if (in_array($subjectWord, $excludedSubjectWords)) |
||
| 1268 | { |
||
| 1269 | if (($subject_query['from'] != '{db_prefix}messages AS m') && !$excluded) |
||
| 1270 | { |
||
| 1271 | $subject_query['inner_join'][] = '{db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)'; |
||
| 1272 | $excluded = true; |
||
| 1273 | } |
||
| 1274 | $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)'; |
||
| 1275 | $subject_query['params']['subject_not_' . $count] = empty($modSettings['search_match_words']) ? '%' . $subjectWord . '%' : $subjectWord; |
||
| 1276 | |||
| 1277 | $subject_query['where'][] = '(subj' . $numTables . '.word IS NULL)'; |
||
| 1278 | $subject_query['where'][] = 'm.body NOT ' . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:body_not_' . $count . '}'; |
||
| 1279 | $subject_query['params']['body_not_' . $count++] = empty($modSettings['search_match_words']) || $no_regexp ? '%' . strtr($subjectWord, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $subjectWord), '\\\'') . '[[:>:]]'; |
||
| 1280 | } |
||
| 1281 | else |
||
| 1282 | { |
||
| 1283 | $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)'; |
||
| 1284 | $subject_query['where'][] = 'subj' . $numTables . '.word LIKE {string:subject_like_' . $count . '}'; |
||
| 1285 | $subject_query['params']['subject_like_' . $count++] = empty($modSettings['search_match_words']) ? '%' . $subjectWord . '%' : $subjectWord; |
||
| 1286 | $prev_join = $numTables; |
||
| 1287 | } |
||
| 1288 | } |
||
| 1289 | |||
| 1290 | if (!empty($userQuery)) |
||
| 1291 | { |
||
| 1292 | if ($subject_query['from'] != '{db_prefix}messages AS m') |
||
| 1293 | { |
||
| 1294 | $subject_query['inner_join'][] = '{db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)'; |
||
| 1295 | } |
||
| 1296 | $subject_query['where'][] = '{raw:user_query}'; |
||
| 1297 | $subject_query['params']['user_query'] = $userQuery; |
||
| 1298 | } |
||
| 1299 | if (!empty($search_params['topic'])) |
||
| 1300 | { |
||
| 1301 | $subject_query['where'][] = 't.id_topic = {int:topic}'; |
||
| 1302 | $subject_query['params']['topic'] = $search_params['topic']; |
||
| 1303 | } |
||
| 1304 | if (!empty($minMsgID)) |
||
| 1305 | { |
||
| 1306 | $subject_query['where'][] = 't.id_first_msg >= {int:min_msg_id}'; |
||
| 1307 | $subject_query['params']['min_msg_id'] = $minMsgID; |
||
| 1308 | } |
||
| 1309 | if (!empty($maxMsgID)) |
||
| 1310 | { |
||
| 1311 | $subject_query['where'][] = 't.id_last_msg <= {int:max_msg_id}'; |
||
| 1312 | $subject_query['params']['max_msg_id'] = $maxMsgID; |
||
| 1313 | } |
||
| 1314 | if (!empty($boardQuery)) |
||
| 1315 | { |
||
| 1316 | $subject_query['where'][] = 't.id_board {raw:board_query}'; |
||
| 1317 | $subject_query['params']['board_query'] = $boardQuery; |
||
| 1318 | } |
||
| 1319 | if (!empty($excludedPhrases)) |
||
| 1320 | { |
||
| 1321 | if ($subject_query['from'] != '{db_prefix}messages AS m') |
||
| 1322 | { |
||
| 1323 | $subject_query['inner_join'][] = '{db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)'; |
||
| 1324 | } |
||
| 1325 | $count = 0; |
||
| 1326 | foreach ($excludedPhrases as $phrase) |
||
| 1327 | { |
||
| 1328 | $subject_query['where'][] = 'm.subject NOT ' . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:exclude_phrase_' . $count . '}'; |
||
| 1329 | $subject_query['where'][] = 'm.body NOT ' . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:exclude_phrase_' . $count . '}'; |
||
| 1330 | $subject_query['params']['exclude_phrase_' . $count++] = empty($modSettings['search_match_words']) || $no_regexp ? '%' . strtr($phrase, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $phrase), '\\\'') . '[[:>:]]'; |
||
| 1331 | } |
||
| 1332 | } |
||
| 1333 | call_integration_hook('integrate_subject_search_query', array(&$subject_query)); |
||
| 1334 | |||
| 1335 | // Nothing to search for? |
||
| 1336 | if (empty($subject_query['where'])) |
||
| 1337 | continue; |
||
| 1338 | |||
| 1339 | $ignoreRequest = $smcFunc['db_search_query']('insert_log_search_topics', ($smcFunc['db_support_ignore'] ? (' |
||
| 1340 | INSERT IGNORE INTO {db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_topics |
||
| 1341 | (' . ($createTemporary ? '' : 'id_search, ') . 'id_topic)') : '') . ' |
||
| 1342 | SELECT ' . ($createTemporary ? '' : $_SESSION['search_cache']['id_search'] . ', ') . 't.id_topic |
||
| 1343 | FROM ' . $subject_query['from'] . (empty($subject_query['inner_join']) ? '' : ' |
||
| 1344 | INNER JOIN ' . implode(' |
||
| 1345 | INNER JOIN ', $subject_query['inner_join'])) . (empty($subject_query['left_join']) ? '' : ' |
||
| 1346 | LEFT JOIN ' . implode(' |
||
| 1347 | LEFT JOIN ', $subject_query['left_join'])) . ' |
||
| 1348 | WHERE ' . implode(' |
||
| 1349 | AND ', $subject_query['where']) . (empty($modSettings['search_max_results']) ? '' : ' |
||
| 1350 | LIMIT ' . ($modSettings['search_max_results'] - $numSubjectResults)), |
||
| 1351 | $subject_query['params'] |
||
| 1352 | ); |
||
| 1353 | // Don't do INSERT IGNORE? Manually fix this up! |
||
| 1354 | if (!$smcFunc['db_support_ignore']) |
||
| 1355 | { |
||
| 1356 | while ($row = $smcFunc['db_fetch_row']($ignoreRequest)) |
||
| 1357 | { |
||
| 1358 | $ind = $createTemporary ? 0 : 1; |
||
| 1359 | // No duplicates! |
||
| 1360 | if (isset($inserts[$row[$ind]])) |
||
| 1361 | continue; |
||
| 1362 | |||
| 1363 | $inserts[$row[$ind]] = $row; |
||
| 1364 | } |
||
| 1365 | $smcFunc['db_free_result']($ignoreRequest); |
||
| 1366 | $numSubjectResults = count($inserts); |
||
| 1367 | } |
||
| 1368 | else |
||
| 1369 | $numSubjectResults += $smcFunc['db_affected_rows'](); |
||
| 1370 | |||
| 1371 | if (!empty($modSettings['search_max_results']) && $numSubjectResults >= $modSettings['search_max_results']) |
||
| 1372 | break; |
||
| 1373 | } |
||
| 1374 | |||
| 1375 | // Got some non-MySQL data to plonk in? |
||
| 1376 | if (!empty($inserts)) |
||
| 1377 | { |
||
| 1378 | $smcFunc['db_insert']('', |
||
| 1379 | ('{db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_topics'), |
||
| 1380 | $createTemporary ? array('id_topic' => 'int') : array('id_search' => 'int', 'id_topic' => 'int'), |
||
| 1381 | $inserts, |
||
| 1382 | $createTemporary ? array('id_topic') : array('id_search', 'id_topic') |
||
| 1383 | ); |
||
| 1384 | } |
||
| 1385 | |||
| 1386 | if ($numSubjectResults !== 0) |
||
| 1387 | { |
||
| 1388 | $main_query['weights']['subject']['search'] = 'CASE WHEN MAX(lst.id_topic) IS NULL THEN 0 ELSE 1 END'; |
||
| 1389 | $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)'; |
||
| 1390 | if (!$createTemporary) |
||
| 1391 | $main_query['parameters']['id_search'] = $_SESSION['search_cache']['id_search']; |
||
| 1392 | } |
||
| 1393 | } |
||
| 1394 | |||
| 1395 | $indexedResults = 0; |
||
| 1396 | // We building an index? |
||
| 1397 | if ($searchAPI->supportsMethod('indexedWordQuery', $query_params)) |
||
| 1398 | { |
||
| 1399 | $inserts = array(); |
||
| 1400 | $smcFunc['db_search_query']('drop_tmp_log_search_messages', ' |
||
| 1401 | DROP TABLE IF EXISTS {db_prefix}tmp_log_search_messages', |
||
| 1402 | array( |
||
| 1403 | ) |
||
| 1404 | ); |
||
| 1405 | |||
| 1406 | $createTemporary = $smcFunc['db_search_query']('create_tmp_log_search_messages', ' |
||
| 1407 | CREATE TEMPORARY TABLE {db_prefix}tmp_log_search_messages ( |
||
| 1408 | id_msg int NOT NULL default {string:string_zero}, |
||
| 1409 | PRIMARY KEY (id_msg) |
||
| 1410 | ) ENGINE=MEMORY', |
||
| 1411 | array( |
||
| 1412 | 'string_zero' => '0', |
||
| 1413 | ) |
||
| 1414 | ) !== false; |
||
| 1415 | |||
| 1416 | // Clear, all clear! |
||
| 1417 | if (!$createTemporary) |
||
| 1418 | $smcFunc['db_search_query']('delete_log_search_messages', ' |
||
| 1419 | DELETE FROM {db_prefix}log_search_messages |
||
| 1420 | WHERE id_search = {int:id_search}', |
||
| 1421 | array( |
||
| 1422 | 'id_search' => $_SESSION['search_cache']['id_search'], |
||
| 1423 | ) |
||
| 1424 | ); |
||
| 1425 | |||
| 1426 | foreach ($searchWords as $orIndex => $words) |
||
| 1427 | { |
||
| 1428 | // Search for this word, assuming we have some words! |
||
| 1429 | if (!empty($words['indexed_words'])) |
||
| 1430 | { |
||
| 1431 | // Variables required for the search. |
||
| 1432 | $search_data = array( |
||
| 1433 | 'insert_into' => ($createTemporary ? 'tmp_' : '') . 'log_search_messages', |
||
| 1434 | 'no_regexp' => $no_regexp, |
||
| 1435 | 'max_results' => $maxMessageResults, |
||
| 1436 | 'indexed_results' => $indexedResults, |
||
| 1437 | 'params' => array( |
||
| 1438 | 'id_search' => !$createTemporary ? $_SESSION['search_cache']['id_search'] : 0, |
||
| 1439 | 'excluded_words' => $excludedWords, |
||
| 1440 | 'user_query' => !empty($userQuery) ? $userQuery : '', |
||
| 1441 | 'board_query' => !empty($boardQuery) ? $boardQuery : '', |
||
| 1442 | 'topic' => !empty($search_params['topic']) ? $search_params['topic'] : 0, |
||
| 1443 | 'min_msg_id' => !empty($minMsgID) ? $minMsgID : 0, |
||
| 1444 | 'max_msg_id' => !empty($maxMsgID) ? $maxMsgID : 0, |
||
| 1445 | 'excluded_phrases' => !empty($excludedPhrases) ? $excludedPhrases : array(), |
||
| 1446 | 'excluded_index_words' => !empty($excludedIndexWords) ? $excludedIndexWords : array(), |
||
| 1447 | 'excluded_subject_words' => !empty($excludedSubjectWords) ? $excludedSubjectWords : array(), |
||
| 1448 | ), |
||
| 1449 | ); |
||
| 1450 | |||
| 1451 | $ignoreRequest = $searchAPI->indexedWordQuery($words, $search_data); |
||
| 1452 | |||
| 1453 | if (!$smcFunc['db_support_ignore']) |
||
| 1454 | { |
||
| 1455 | while ($row = $smcFunc['db_fetch_row']($ignoreRequest)) |
||
| 1456 | { |
||
| 1457 | // No duplicates! |
||
| 1458 | if (isset($inserts[$row[0]])) |
||
| 1459 | continue; |
||
| 1460 | |||
| 1461 | $inserts[$row[0]] = $row; |
||
| 1462 | } |
||
| 1463 | $smcFunc['db_free_result']($ignoreRequest); |
||
| 1464 | $indexedResults = count($inserts); |
||
| 1465 | } |
||
| 1466 | else |
||
| 1467 | $indexedResults += $smcFunc['db_affected_rows'](); |
||
| 1468 | |||
| 1469 | if (!empty($maxMessageResults) && $indexedResults >= $maxMessageResults) |
||
| 1470 | break; |
||
| 1471 | } |
||
| 1472 | } |
||
| 1473 | |||
| 1474 | // More non-MySQL stuff needed? |
||
| 1475 | if (!empty($inserts)) |
||
| 1476 | { |
||
| 1477 | $smcFunc['db_insert']('', |
||
| 1478 | '{db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_messages', |
||
| 1479 | $createTemporary ? array('id_msg' => 'int') : array('id_msg' => 'int', 'id_search' => 'int'), |
||
| 1480 | $inserts, |
||
| 1481 | $createTemporary ? array('id_msg') : array('id_msg', 'id_search') |
||
| 1482 | ); |
||
| 1483 | } |
||
| 1484 | |||
| 1485 | if (empty($indexedResults) && empty($numSubjectResults) && !empty($modSettings['search_force_index'])) |
||
| 1486 | { |
||
| 1487 | $context['search_errors']['query_not_specific_enough'] = true; |
||
| 1488 | $_REQUEST['params'] = $context['params']; |
||
| 1489 | return PlushSearch1(); |
||
| 1490 | } |
||
| 1491 | elseif (!empty($indexedResults)) |
||
| 1492 | { |
||
| 1493 | $main_query['inner_join'][] = '{db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_messages AS lsm ON (lsm.id_msg = m.id_msg)'; |
||
| 1494 | if (!$createTemporary) |
||
| 1495 | { |
||
| 1496 | $main_query['where'][] = 'lsm.id_search = {int:id_search}'; |
||
| 1497 | $main_query['parameters']['id_search'] = $_SESSION['search_cache']['id_search']; |
||
| 1498 | } |
||
| 1499 | } |
||
| 1500 | } |
||
| 1501 | |||
| 1502 | // Not using an index? All conditions have to be carried over. |
||
| 1503 | else |
||
| 1504 | { |
||
| 1505 | $orWhere = array(); |
||
| 1506 | $count = 0; |
||
| 1507 | foreach ($searchWords as $orIndex => $words) |
||
| 1508 | { |
||
| 1509 | $where = array(); |
||
| 1510 | foreach ($words['all_words'] as $regularWord) |
||
| 1511 | { |
||
| 1512 | $where[] = 'm.body' . (in_array($regularWord, $excludedWords) ? ' NOT' : '') . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:all_word_body_' . $count . '}'; |
||
| 1513 | if (in_array($regularWord, $excludedWords)) |
||
| 1514 | $where[] = 'm.subject NOT' . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:all_word_body_' . $count . '}'; |
||
| 1515 | $main_query['parameters']['all_word_body_' . $count++] = empty($modSettings['search_match_words']) || $no_regexp ? '%' . strtr($regularWord, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $regularWord), '\\\'') . '[[:>:]]'; |
||
| 1516 | } |
||
| 1517 | if (!empty($where)) |
||
| 1518 | $orWhere[] = count($where) > 1 ? '(' . implode(' AND ', $where) . ')' : $where[0]; |
||
| 1519 | } |
||
| 1520 | if (!empty($orWhere)) |
||
| 1521 | $main_query['where'][] = count($orWhere) > 1 ? '(' . implode(' OR ', $orWhere) . ')' : $orWhere[0]; |
||
| 1522 | |||
| 1523 | if (!empty($userQuery)) |
||
| 1524 | { |
||
| 1525 | $main_query['where'][] = '{raw:user_query}'; |
||
| 1526 | $main_query['parameters']['user_query'] = $userQuery; |
||
| 1527 | } |
||
| 1528 | if (!empty($search_params['topic'])) |
||
| 1529 | { |
||
| 1530 | $main_query['where'][] = 'm.id_topic = {int:topic}'; |
||
| 1531 | $main_query['parameters']['topic'] = $search_params['topic']; |
||
| 1532 | } |
||
| 1533 | if (!empty($minMsgID)) |
||
| 1534 | { |
||
| 1535 | $main_query['where'][] = 'm.id_msg >= {int:min_msg_id}'; |
||
| 1536 | $main_query['parameters']['min_msg_id'] = $minMsgID; |
||
| 1537 | } |
||
| 1538 | if (!empty($maxMsgID)) |
||
| 1539 | { |
||
| 1540 | $main_query['where'][] = 'm.id_msg <= {int:max_msg_id}'; |
||
| 1541 | $main_query['parameters']['max_msg_id'] = $maxMsgID; |
||
| 1542 | } |
||
| 1543 | if (!empty($boardQuery)) |
||
| 1544 | { |
||
| 1545 | $main_query['where'][] = 'm.id_board {raw:board_query}'; |
||
| 1546 | $main_query['parameters']['board_query'] = $boardQuery; |
||
| 1547 | } |
||
| 1548 | } |
||
| 1549 | call_integration_hook('integrate_main_search_query', array(&$main_query)); |
||
| 1550 | |||
| 1551 | // Did we either get some indexed results, or otherwise did not do an indexed query? |
||
| 1552 | if (!empty($indexedResults) || !$searchAPI->supportsMethod('indexedWordQuery', $query_params)) |
||
| 1553 | { |
||
| 1554 | $relevance = '1000 * ('; |
||
| 1555 | $new_weight_total = 0; |
||
| 1556 | foreach ($main_query['weights'] as $type => $value) |
||
| 1557 | { |
||
| 1558 | $relevance .= $weight[$type]; |
||
| 1559 | if (!empty($value['search'])) |
||
| 1560 | $relevance .= ' * ' . $value['search']; |
||
| 1561 | $relevance .= ' + '; |
||
| 1562 | $new_weight_total += $weight[$type]; |
||
| 1563 | } |
||
| 1564 | $main_query['select']['relevance'] = substr($relevance, 0, -3) . ') / ' . $new_weight_total . ' AS relevance'; |
||
| 1565 | |||
| 1566 | $ignoreRequest = $smcFunc['db_search_query']('insert_log_search_results_no_index', ($smcFunc['db_support_ignore'] ? (' |
||
| 1567 | INSERT IGNORE INTO ' . '{db_prefix}log_search_results |
||
| 1568 | (' . implode(', ', array_keys($main_query['select'])) . ')') : '') . ' |
||
| 1569 | SELECT |
||
| 1570 | ' . implode(', |
||
| 1571 | ', $main_query['select']) . ' |
||
| 1572 | FROM ' . $main_query['from'] . (empty($main_query['inner_join']) ? '' : ' |
||
| 1573 | INNER JOIN ' . implode(' |
||
| 1574 | INNER JOIN ', $main_query['inner_join'])) . (empty($main_query['left_join']) ? '' : ' |
||
| 1575 | LEFT JOIN ' . implode(' |
||
| 1576 | LEFT JOIN ', $main_query['left_join'])) . (!empty($main_query['where']) ? ' |
||
| 1577 | WHERE ' : '') . implode(' |
||
| 1578 | AND ', $main_query['where']) . (empty($main_query['group_by']) ? '' : ' |
||
| 1579 | GROUP BY ' . implode(', ', $main_query['group_by'])) . (empty($modSettings['search_max_results']) ? '' : ' |
||
| 1580 | LIMIT ' . $modSettings['search_max_results']), |
||
| 1581 | $main_query['parameters'] |
||
| 1582 | ); |
||
| 1583 | |||
| 1584 | // We love to handle non-good databases that don't support our ignore! |
||
| 1585 | if (!$smcFunc['db_support_ignore']) |
||
| 1586 | { |
||
| 1587 | $inserts = array(); |
||
| 1588 | while ($row = $smcFunc['db_fetch_row']($ignoreRequest)) |
||
| 1589 | { |
||
| 1590 | // No duplicates! |
||
| 1591 | if (isset($inserts[$row[2]])) |
||
| 1592 | continue; |
||
| 1593 | |||
| 1594 | foreach ($row as $key => $value) |
||
| 1595 | $inserts[$row[2]][] = (int) $row[$key]; |
||
| 1596 | } |
||
| 1597 | $smcFunc['db_free_result']($ignoreRequest); |
||
| 1598 | |||
| 1599 | // Now put them in! |
||
| 1600 | if (!empty($inserts)) |
||
| 1601 | { |
||
| 1602 | $query_columns = array(); |
||
| 1603 | foreach ($main_query['select'] as $k => $v) |
||
| 1604 | $query_columns[$k] = 'int'; |
||
| 1605 | |||
| 1606 | $smcFunc['db_insert']('', |
||
| 1607 | '{db_prefix}log_search_results', |
||
| 1608 | $query_columns, |
||
| 1609 | $inserts, |
||
| 1610 | array('id_search', 'id_topic') |
||
| 1611 | ); |
||
| 1612 | } |
||
| 1613 | $_SESSION['search_cache']['num_results'] += count($inserts); |
||
| 1614 | } |
||
| 1615 | else |
||
| 1616 | $_SESSION['search_cache']['num_results'] = $smcFunc['db_affected_rows'](); |
||
| 1617 | } |
||
| 1618 | |||
| 1619 | // Insert subject-only matches. |
||
| 1620 | if ($_SESSION['search_cache']['num_results'] < $modSettings['search_max_results'] && $numSubjectResults !== 0) |
||
| 1621 | { |
||
| 1622 | $relevance = '1000 * ('; |
||
| 1623 | foreach ($weight_factors as $type => $value) |
||
| 1624 | if (isset($value['results'])) |
||
| 1625 | { |
||
| 1626 | $relevance .= $weight[$type]; |
||
| 1627 | if (!empty($value['results'])) |
||
| 1628 | $relevance .= ' * ' . $value['results']; |
||
| 1629 | $relevance .= ' + '; |
||
| 1630 | } |
||
| 1631 | $relevance = substr($relevance, 0, -3) . ') / ' . $weight_total . ' AS relevance'; |
||
| 1632 | |||
| 1633 | $usedIDs = array_flip(empty($inserts) ? array() : array_keys($inserts)); |
||
| 1634 | $ignoreRequest = $smcFunc['db_search_query']('insert_log_search_results_sub_only', ($smcFunc['db_support_ignore'] ? (' |
||
| 1635 | INSERT IGNORE INTO {db_prefix}log_search_results |
||
| 1636 | (id_search, id_topic, relevance, id_msg, num_matches)') : '') . ' |
||
| 1637 | SELECT |
||
| 1638 | {int:id_search}, |
||
| 1639 | t.id_topic, |
||
| 1640 | ' . $relevance . ', |
||
| 1641 | t.id_first_msg, |
||
| 1642 | 1 |
||
| 1643 | FROM {db_prefix}topics AS t |
||
| 1644 | INNER JOIN {db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_topics AS lst ON (lst.id_topic = t.id_topic)' |
||
| 1645 | . ($createTemporary ? '' : ' WHERE lst.id_search = {int:id_search}') |
||
| 1646 | . (empty($modSettings['search_max_results']) ? '' : ' |
||
| 1647 | LIMIT ' . ($modSettings['search_max_results'] - $_SESSION['search_cache']['num_results'])), |
||
| 1648 | array( |
||
| 1649 | 'id_search' => $_SESSION['search_cache']['id_search'], |
||
| 1650 | 'min_msg' => $minMsg, |
||
| 1651 | 'recent_message' => $recentMsg, |
||
| 1652 | 'huge_topic_posts' => $humungousTopicPosts, |
||
| 1653 | ) |
||
| 1654 | ); |
||
| 1655 | // Once again need to do the inserts if the database don't support ignore! |
||
| 1656 | if (!$smcFunc['db_support_ignore']) |
||
| 1657 | { |
||
| 1658 | $inserts = array(); |
||
| 1659 | while ($row = $smcFunc['db_fetch_row']($ignoreRequest)) |
||
| 1660 | { |
||
| 1661 | // No duplicates! |
||
| 1662 | if (isset($usedIDs[$row[1]])) |
||
| 1663 | continue; |
||
| 1664 | |||
| 1665 | $usedIDs[$row[1]] = true; |
||
| 1666 | $inserts[] = $row; |
||
| 1667 | } |
||
| 1668 | $smcFunc['db_free_result']($ignoreRequest); |
||
| 1669 | |||
| 1670 | // Now put them in! |
||
| 1671 | if (!empty($inserts)) |
||
| 1672 | { |
||
| 1673 | $smcFunc['db_insert']('', |
||
| 1674 | '{db_prefix}log_search_results', |
||
| 1675 | array('id_search' => 'int', 'id_topic' => 'int', 'relevance' => 'float', 'id_msg' => 'int', 'num_matches' => 'int'), |
||
| 1676 | $inserts, |
||
| 1677 | array('id_search', 'id_topic') |
||
| 1678 | ); |
||
| 1679 | } |
||
| 1680 | $_SESSION['search_cache']['num_results'] += count($inserts); |
||
| 1681 | } |
||
| 1682 | else |
||
| 1683 | $_SESSION['search_cache']['num_results'] += $smcFunc['db_affected_rows'](); |
||
| 1684 | } |
||
| 1685 | elseif ($_SESSION['search_cache']['num_results'] == -1) |
||
| 1686 | $_SESSION['search_cache']['num_results'] = 0; |
||
| 1687 | } |
||
| 1688 | } |
||
| 1689 | |||
| 1690 | if (!empty($modSettings['postmod_active'])) |
||
| 1691 | { |
||
| 1692 | $approve_boards = boardsAllowedTo('approve_posts'); |
||
| 1693 | |||
| 1694 | // Can approve everywhere, so search all topics. |
||
| 1695 | if ($approve_boards === array(0)) |
||
| 1696 | $approve_query = ''; |
||
| 1697 | |||
| 1698 | // Can't approve anywhere, so search ony their own topics and approved topics. |
||
| 1699 | elseif (empty($approve_boards)) |
||
| 1700 | $approve_query = ' |
||
| 1701 | AND (t.approved = {int:is_approved} OR t.id_member_started = {int:current_member})'; |
||
| 1702 | |||
| 1703 | // Can approve in some boards, so search own, approved, and approvable topics. |
||
| 1704 | else |
||
| 1705 | $approve_query = ' |
||
| 1706 | AND (t.approved = {int:is_approved} OR t.id_member_started = {int:current_member} OR t.id_board IN ({array_int:approve_boards}))'; |
||
| 1707 | } |
||
| 1708 | |||
| 1709 | // *** Retrieve the results to be shown on the page |
||
| 1710 | $participants = array(); |
||
| 1711 | $request = $smcFunc['db_search_query']('', ' |
||
| 1712 | SELECT ' . (empty($search_params['topic']) ? 'lsr.id_topic' : $search_params['topic'] . ' AS id_topic') . ', lsr.id_msg, lsr.relevance, lsr.num_matches |
||
| 1713 | FROM {db_prefix}log_search_results AS lsr' . ($search_params['sort'] == 'num_replies' || !empty($approve_query) ? ' |
||
| 1714 | INNER JOIN {db_prefix}topics AS t ON (t.id_topic = lsr.id_topic)' : '') . ' |
||
| 1715 | WHERE lsr.id_search = {int:id_search}' . $approve_query . ' |
||
| 1716 | ORDER BY {raw:sort} {raw:sort_dir} |
||
| 1717 | LIMIT {int:start}, {int:max}', |
||
| 1718 | array( |
||
| 1719 | 'id_search' => $_SESSION['search_cache']['id_search'], |
||
| 1720 | 'sort' => $search_params['sort'], |
||
| 1721 | 'sort_dir' => $search_params['sort_dir'], |
||
| 1722 | 'start' => $_REQUEST['start'], |
||
| 1723 | 'max' => $modSettings['search_results_per_page'], |
||
| 1724 | 'is_approved' => 1, |
||
| 1725 | 'current_member' => $user_info['id'], |
||
| 1726 | 'approve_boards' => !empty($approve_boards) ? $approve_boards : array(0), |
||
| 1727 | ) |
||
| 1728 | ); |
||
| 1729 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 1730 | { |
||
| 1731 | $context['topics'][$row['id_msg']] = array( |
||
| 1732 | 'relevance' => round($row['relevance'] / 10, 1) . '%', |
||
| 1733 | 'num_matches' => $row['num_matches'], |
||
| 1734 | 'matches' => array(), |
||
| 1735 | ); |
||
| 1736 | // By default they didn't participate in the topic! |
||
| 1737 | $participants[$row['id_topic']] = false; |
||
| 1738 | } |
||
| 1739 | $smcFunc['db_free_result']($request); |
||
| 1740 | } |
||
| 1741 | |||
| 1742 | $num_results = 0; |
||
| 1743 | if (!empty($context['topics'])) |
||
| 1744 | { |
||
| 1745 | // Create an array for the permissions. |
||
| 1746 | $perms = array('post_reply_own', 'post_reply_any'); |
||
| 1747 | |||
| 1748 | if (!empty($options['display_quick_mod'])) |
||
| 1749 | $perms = array_merge($perms, array('lock_any', 'lock_own', 'make_sticky', 'move_any', 'move_own', 'remove_any', 'remove_own', 'merge_any')); |
||
| 1750 | |||
| 1751 | if (!empty($modSettings['postmod_active']) && !isset($approve_boards)) |
||
| 1752 | $perms[] = 'approve_posts'; |
||
| 1753 | |||
| 1754 | $boards_can = boardsAllowedTo($perms, true, false); |
||
| 1755 | |||
| 1756 | // How's about some quick moderation? |
||
| 1757 | if (!empty($options['display_quick_mod'])) |
||
| 1758 | { |
||
| 1759 | $context['can_lock'] = in_array(0, $boards_can['lock_any']); |
||
| 1760 | $context['can_sticky'] = in_array(0, $boards_can['make_sticky']); |
||
| 1761 | $context['can_move'] = in_array(0, $boards_can['move_any']); |
||
| 1762 | $context['can_remove'] = in_array(0, $boards_can['remove_any']); |
||
| 1763 | $context['can_merge'] = in_array(0, $boards_can['merge_any']); |
||
| 1764 | } |
||
| 1765 | |||
| 1766 | if (!empty($modSettings['postmod_active'])) |
||
| 1767 | { |
||
| 1768 | if (!isset($approve_boards)) |
||
| 1769 | $approve_boards = $boards_can['approve_posts']; |
||
| 1770 | |||
| 1771 | if ($approve_boards === array(0)) |
||
| 1772 | $approve_query = ''; |
||
| 1773 | |||
| 1774 | elseif (empty($approve_boards)) |
||
| 1775 | $approve_query = ' |
||
| 1776 | AND (m.approved = {int:is_approved} OR m.id_member = {int:current_member})'; |
||
| 1777 | |||
| 1778 | else |
||
| 1779 | $approve_query = ' |
||
| 1780 | AND (m.approved = {int:is_approved} OR m.id_member = {int:current_member} OR m.id_board IN ({array_int:approve_boards}))'; |
||
| 1781 | } |
||
| 1782 | |||
| 1783 | // What messages are we using? |
||
| 1784 | $msg_list = array_keys($context['topics']); |
||
| 1785 | |||
| 1786 | // Load the posters... |
||
| 1787 | $request = $smcFunc['db_query']('', ' |
||
| 1788 | SELECT id_member |
||
| 1789 | FROM {db_prefix}messages |
||
| 1790 | WHERE id_member != {int:no_member} |
||
| 1791 | AND id_msg IN ({array_int:message_list}) |
||
| 1792 | LIMIT {int:limit}', |
||
| 1793 | array( |
||
| 1794 | 'message_list' => $msg_list, |
||
| 1795 | 'no_member' => 0, |
||
| 1796 | 'limit' => count($context['topics']), |
||
| 1797 | ) |
||
| 1798 | ); |
||
| 1799 | $posters = array(); |
||
| 1800 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 1801 | $posters[] = $row['id_member']; |
||
| 1802 | $smcFunc['db_free_result']($request); |
||
| 1803 | |||
| 1804 | call_integration_hook('integrate_search_message_list', array(&$msg_list, &$posters)); |
||
| 1805 | |||
| 1806 | if (!empty($posters)) |
||
| 1807 | loadMemberData(array_unique($posters)); |
||
| 1808 | |||
| 1809 | // Get the messages out for the callback - select enough that it can be made to look just like Display. |
||
| 1810 | $messages_request = $smcFunc['db_query']('', ' |
||
| 1811 | SELECT |
||
| 1812 | m.id_msg, m.subject, m.poster_name, m.poster_email, m.poster_time, m.id_member, |
||
| 1813 | m.icon, m.poster_ip, m.body, m.smileys_enabled, m.modified_time, m.modified_name, |
||
| 1814 | 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, |
||
| 1815 | first_mem.id_member AS first_member_id, COALESCE(first_mem.real_name, first_m.poster_name) AS first_member_name, |
||
| 1816 | last_m.id_msg AS last_msg, last_m.poster_time AS last_poster_time, last_mem.id_member AS last_member_id, |
||
| 1817 | COALESCE(last_mem.real_name, last_m.poster_name) AS last_member_name, last_m.icon AS last_icon, last_m.subject AS last_subject, |
||
| 1818 | t.id_topic, t.is_sticky, t.locked, t.id_poll, t.num_replies, t.num_views, |
||
| 1819 | b.id_board, b.name AS board_name, c.id_cat, c.name AS cat_name |
||
| 1820 | FROM {db_prefix}messages AS m |
||
| 1821 | INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic) |
||
| 1822 | INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board) |
||
| 1823 | INNER JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat) |
||
| 1824 | INNER JOIN {db_prefix}messages AS first_m ON (first_m.id_msg = t.id_first_msg) |
||
| 1825 | INNER JOIN {db_prefix}messages AS last_m ON (last_m.id_msg = t.id_last_msg) |
||
| 1826 | LEFT JOIN {db_prefix}members AS first_mem ON (first_mem.id_member = first_m.id_member) |
||
| 1827 | LEFT JOIN {db_prefix}members AS last_mem ON (last_mem.id_member = first_m.id_member) |
||
| 1828 | WHERE m.id_msg IN ({array_int:message_list})' . $approve_query . ' |
||
| 1829 | ORDER BY ' . $smcFunc['db_custom_order']('m.id_msg', $msg_list) . ' |
||
| 1830 | LIMIT {int:limit}', |
||
| 1831 | array( |
||
| 1832 | 'message_list' => $msg_list, |
||
| 1833 | 'is_approved' => 1, |
||
| 1834 | 'current_member' => $user_info['id'], |
||
| 1835 | 'approve_boards' => !empty($approve_boards) ? $approve_boards : array(0), |
||
| 1836 | 'limit' => count($context['topics']), |
||
| 1837 | ) |
||
| 1838 | ); |
||
| 1839 | |||
| 1840 | // How many results will the user be able to see? |
||
| 1841 | $num_results = $smcFunc['db_num_rows']($messages_request); |
||
| 1842 | |||
| 1843 | // If there are no results that means the things in the cache got deleted, so pretend we have no topics anymore. |
||
| 1844 | if ($num_results == 0) |
||
| 1845 | $context['topics'] = array(); |
||
| 1846 | |||
| 1847 | // If we want to know who participated in what then load this now. |
||
| 1848 | if (!empty($modSettings['enableParticipation']) && !$user_info['is_guest']) |
||
| 1849 | { |
||
| 1850 | $result = $smcFunc['db_query']('', ' |
||
| 1851 | SELECT id_topic |
||
| 1852 | FROM {db_prefix}messages |
||
| 1853 | WHERE id_topic IN ({array_int:topic_list}) |
||
| 1854 | AND id_member = {int:current_member} |
||
| 1855 | GROUP BY id_topic |
||
| 1856 | LIMIT {int:limit}', |
||
| 1857 | array( |
||
| 1858 | 'current_member' => $user_info['id'], |
||
| 1859 | 'topic_list' => array_keys($participants), |
||
| 1860 | 'limit' => count($participants), |
||
| 1861 | ) |
||
| 1862 | ); |
||
| 1863 | while ($row = $smcFunc['db_fetch_assoc']($result)) |
||
| 1864 | $participants[$row['id_topic']] = true; |
||
| 1865 | |||
| 1866 | $smcFunc['db_free_result']($result); |
||
| 1867 | } |
||
| 1868 | } |
||
| 1869 | |||
| 1870 | // Now that we know how many results to expect we can start calculating the page numbers. |
||
| 1871 | $context['page_index'] = constructPageIndex($scripturl . '?action=search2;params=' . $context['params'], $_REQUEST['start'], $num_results, $modSettings['search_results_per_page'], false); |
||
| 1872 | |||
| 1873 | // Consider the search complete! |
||
| 1874 | if (!empty($cache_enable) && $cache_enable >= 2) |
||
| 1875 | cache_put_data('search_start:' . ($user_info['is_guest'] ? $user_info['ip'] : $user_info['id']), null, 90); |
||
| 1876 | |||
| 1877 | $context['key_words'] = &$searchArray; |
||
| 1878 | |||
| 1879 | // Setup the default topic icons... for checking they exist and the like! |
||
| 1880 | $context['icon_sources'] = array(); |
||
| 1881 | foreach ($context['stable_icons'] as $icon) |
||
| 1882 | $context['icon_sources'][$icon] = 'images_url'; |
||
| 1883 | |||
| 1884 | $context['sub_template'] = 'results'; |
||
| 1885 | $context['page_title'] = $txt['search_results']; |
||
| 1886 | $context['get_topics'] = 'prepareSearchContext'; |
||
| 1887 | $context['can_restore_perm'] = allowedTo('move_any') && !empty($modSettings['recycle_enable']); |
||
| 1888 | $context['can_restore'] = false; // We won't know until we handle the context later whether we can actually restore... |
||
| 1889 | |||
| 1890 | $context['jump_to'] = array( |
||
| 1891 | 'label' => addslashes(un_htmlspecialchars($txt['jump_to'])), |
||
| 1892 | 'board_name' => addslashes(un_htmlspecialchars($txt['select_destination'])), |
||
| 1893 | ); |
||
| 1894 | } |
||
| 1895 | |||
| 1896 | /** |
||
| 1897 | * Callback to return messages - saves memory. |
||
| 1898 | * |
||
| 1899 | * What it does: |
||
| 1900 | * - callback function for the results sub template. |
||
| 1901 | * - loads the necessary contextual data to show a search result. |
||
| 1902 | * |
||
| 1903 | * @param bool $reset Whether to reset the counter |
||
| 1904 | * @return array An array of contextual info related to this search |
||
| 1905 | */ |
||
| 1906 | function prepareSearchContext($reset = false) |
||
| 1907 | { |
||
| 1908 | global $txt, $modSettings, $scripturl, $user_info; |
||
| 1909 | global $memberContext, $context, $settings, $options, $messages_request; |
||
| 1910 | global $boards_can, $participants, $smcFunc; |
||
| 1911 | static $recycle_board = null; |
||
| 1912 | |||
| 1913 | if ($recycle_board === null) |
||
| 1914 | $recycle_board = !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) ? (int) $modSettings['recycle_board'] : 0; |
||
| 1915 | |||
| 1916 | // Remember which message this is. (ie. reply #83) |
||
| 1917 | static $counter = null; |
||
| 1918 | if ($counter == null || $reset) |
||
| 1919 | $counter = $_REQUEST['start'] + 1; |
||
| 1920 | |||
| 1921 | // If the query returned false, bail. |
||
| 1922 | if ($messages_request == false) |
||
| 1923 | return false; |
||
| 1924 | |||
| 1925 | // Start from the beginning... |
||
| 1926 | if ($reset) |
||
| 1927 | return @$smcFunc['db_data_seek']($messages_request, 0); |
||
| 1928 | |||
| 1929 | // Attempt to get the next message. |
||
| 1930 | $message = $smcFunc['db_fetch_assoc']($messages_request); |
||
| 1931 | if (!$message) |
||
| 1932 | return false; |
||
| 1933 | |||
| 1934 | // Can't have an empty subject can we? |
||
| 1935 | $message['subject'] = $message['subject'] != '' ? $message['subject'] : $txt['no_subject']; |
||
| 1936 | |||
| 1937 | $message['first_subject'] = $message['first_subject'] != '' ? $message['first_subject'] : $txt['no_subject']; |
||
| 1938 | $message['last_subject'] = $message['last_subject'] != '' ? $message['last_subject'] : $txt['no_subject']; |
||
| 1939 | |||
| 1940 | // If it couldn't load, or the user was a guest.... someday may be done with a guest table. |
||
| 1941 | if (!loadMemberContext($message['id_member'])) |
||
| 1942 | { |
||
| 1943 | // Notice this information isn't used anywhere else.... *cough guest table cough*. |
||
| 1944 | $memberContext[$message['id_member']]['name'] = $message['poster_name']; |
||
| 1945 | $memberContext[$message['id_member']]['id'] = 0; |
||
| 1946 | $memberContext[$message['id_member']]['group'] = $txt['guest_title']; |
||
| 1947 | $memberContext[$message['id_member']]['link'] = $message['poster_name']; |
||
| 1948 | $memberContext[$message['id_member']]['email'] = $message['poster_email']; |
||
| 1949 | } |
||
| 1950 | $memberContext[$message['id_member']]['ip'] = inet_dtop($message['poster_ip']); |
||
| 1951 | |||
| 1952 | // Do the censor thang... |
||
| 1953 | censorText($message['body']); |
||
| 1954 | censorText($message['subject']); |
||
| 1955 | |||
| 1956 | censorText($message['first_subject']); |
||
| 1957 | censorText($message['last_subject']); |
||
| 1958 | |||
| 1959 | // Shorten this message if necessary. |
||
| 1960 | if ($context['compact']) |
||
| 1961 | { |
||
| 1962 | // Set the number of characters before and after the searched keyword. |
||
| 1963 | $charLimit = 50; |
||
| 1964 | |||
| 1965 | $message['body'] = strtr($message['body'], array("\n" => ' ', '<br>' => "\n")); |
||
| 1966 | $message['body'] = parse_bbc($message['body'], $message['smileys_enabled'], $message['id_msg']); |
||
| 1967 | $message['body'] = strip_tags(strtr($message['body'], array('</div>' => '<br>', '</li>' => '<br>')), '<br>'); |
||
| 1968 | |||
| 1969 | if ($smcFunc['strlen']($message['body']) > $charLimit) |
||
| 1970 | { |
||
| 1971 | if (empty($context['key_words'])) |
||
| 1972 | $message['body'] = $smcFunc['substr']($message['body'], 0, $charLimit) . '<strong>...</strong>'; |
||
| 1973 | else |
||
| 1974 | { |
||
| 1975 | $matchString = ''; |
||
| 1976 | $force_partial_word = false; |
||
| 1977 | foreach ($context['key_words'] as $keyword) |
||
| 1978 | { |
||
| 1979 | $keyword = un_htmlspecialchars($keyword); |
||
| 1980 | $keyword = preg_replace_callback('~(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)~', 'entity_fix__callback', strtr($keyword, array('\\\'' => '\'', '&' => '&'))); |
||
| 1981 | |||
| 1982 | if (preg_match('~[\'\.,/@%&;:(){}\[\]_\-+\\\\]$~', $keyword) != 0 || preg_match('~^[\'\.,/@%&;:(){}\[\]_\-+\\\\]~', $keyword) != 0) |
||
| 1983 | $force_partial_word = true; |
||
| 1984 | $matchString .= strtr(preg_quote($keyword, '/'), array('\*' => '.+?')) . '|'; |
||
| 1985 | } |
||
| 1986 | $matchString = un_htmlspecialchars(substr($matchString, 0, -1)); |
||
| 1987 | |||
| 1988 | $message['body'] = un_htmlspecialchars(strtr($message['body'], array(' ' => ' ', '<br>' => "\n", '[' => '[', ']' => ']', ':' => ':', '@' => '@'))); |
||
| 1989 | |||
| 1990 | if (empty($modSettings['search_method']) || $force_partial_word) |
||
| 1991 | preg_match_all('/([^\s\W]{' . $charLimit . '}[\s\W]|[\s\W].{0,' . $charLimit . '}?|^)(' . $matchString . ')(.{0,' . $charLimit . '}[\s\W]|[^\s\W]{0,' . $charLimit . '})/is' . ($context['utf8'] ? 'u' : ''), $message['body'], $matches); |
||
| 1992 | else |
||
| 1993 | preg_match_all('/([^\s\W]{' . $charLimit . '}[\s\W]|[\s\W].{0,' . $charLimit . '}?[\s\W]|^)(' . $matchString . ')([\s\W].{0,' . $charLimit . '}[\s\W]|[\s\W][^\s\W]{0,' . $charLimit . '})/is' . ($context['utf8'] ? 'u' : ''), $message['body'], $matches); |
||
| 1994 | |||
| 1995 | $message['body'] = ''; |
||
| 1996 | foreach ($matches[0] as $index => $match) |
||
| 1997 | { |
||
| 1998 | $match = strtr($smcFunc['htmlspecialchars']($match, ENT_QUOTES), array("\n" => ' ')); |
||
| 1999 | $message['body'] .= '<strong>......</strong> ' . $match . ' <strong>......</strong>'; |
||
| 2000 | } |
||
| 2001 | } |
||
| 2002 | |||
| 2003 | // Re-fix the international characters. |
||
| 2004 | $message['body'] = preg_replace_callback('~(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)~', 'entity_fix__callback', $message['body']); |
||
| 2005 | } |
||
| 2006 | $message['subject_highlighted'] = highlight($message['subject'], $context['key_words']); |
||
| 2007 | $message['body_highlighted'] = highlight($message['body'], $context['key_words']); |
||
| 2008 | } |
||
| 2009 | else |
||
| 2010 | { |
||
| 2011 | $message['subject_highlighted'] = highlight($message['subject'], $context['key_words']); |
||
| 2012 | $message['body_highlighted'] = highlight($message['body'], $context['key_words']); |
||
| 2013 | |||
| 2014 | // Run BBC interpreter on the message. |
||
| 2015 | $message['body'] = parse_bbc($message['body'], $message['smileys_enabled'], $message['id_msg']); |
||
| 2016 | } |
||
| 2017 | |||
| 2018 | // Make sure we don't end up with a practically empty message body. |
||
| 2019 | $message['body'] = preg_replace('~^(?: )+$~', '', $message['body']); |
||
| 2020 | |||
| 2021 | if (!empty($recycle_board) && $message['id_board'] == $recycle_board) |
||
| 2022 | { |
||
| 2023 | $message['first_icon'] = 'recycled'; |
||
| 2024 | $message['last_icon'] = 'recycled'; |
||
| 2025 | $message['icon'] = 'recycled'; |
||
| 2026 | } |
||
| 2027 | |||
| 2028 | // Sadly, we need to check the icon ain't broke. |
||
| 2029 | if (!empty($modSettings['messageIconChecks_enable'])) |
||
| 2030 | { |
||
| 2031 | if (!isset($context['icon_sources'][$message['first_icon']])) |
||
| 2032 | $context['icon_sources'][$message['first_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $message['first_icon'] . '.png') ? 'images_url' : 'default_images_url'; |
||
| 2033 | if (!isset($context['icon_sources'][$message['last_icon']])) |
||
| 2034 | $context['icon_sources'][$message['last_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $message['last_icon'] . '.png') ? 'images_url' : 'default_images_url'; |
||
| 2035 | if (!isset($context['icon_sources'][$message['icon']])) |
||
| 2036 | $context['icon_sources'][$message['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $message['icon'] . '.png') ? 'images_url' : 'default_images_url'; |
||
| 2037 | } |
||
| 2038 | else |
||
| 2039 | { |
||
| 2040 | if (!isset($context['icon_sources'][$message['first_icon']])) |
||
| 2041 | $context['icon_sources'][$message['first_icon']] = 'images_url'; |
||
| 2042 | if (!isset($context['icon_sources'][$message['last_icon']])) |
||
| 2043 | $context['icon_sources'][$message['last_icon']] = 'images_url'; |
||
| 2044 | if (!isset($context['icon_sources'][$message['icon']])) |
||
| 2045 | $context['icon_sources'][$message['icon']] = 'images_url'; |
||
| 2046 | } |
||
| 2047 | |||
| 2048 | // Do we have quote tag enabled? |
||
| 2049 | $quote_enabled = empty($modSettings['disabledBBC']) || !in_array('quote', explode(',', $modSettings['disabledBBC'])); |
||
| 2050 | |||
| 2051 | // Reference the main color class. |
||
| 2052 | $colorClass = 'windowbg'; |
||
| 2053 | |||
| 2054 | // Sticky topics should get a different color, too. |
||
| 2055 | if ($message['is_sticky']) |
||
| 2056 | $colorClass .= ' sticky'; |
||
| 2057 | |||
| 2058 | // Locked topics get special treatment as well. |
||
| 2059 | if ($message['locked']) |
||
| 2060 | $colorClass .= ' locked'; |
||
| 2061 | |||
| 2062 | $output = array_merge($context['topics'][$message['id_msg']], array( |
||
| 2063 | 'id' => $message['id_topic'], |
||
| 2064 | 'is_sticky' => !empty($message['is_sticky']), |
||
| 2065 | 'is_locked' => !empty($message['locked']), |
||
| 2066 | 'css_class' => $colorClass, |
||
| 2067 | 'is_poll' => $modSettings['pollMode'] == '1' && $message['id_poll'] > 0, |
||
| 2068 | 'posted_in' => !empty($participants[$message['id_topic']]), |
||
| 2069 | 'views' => $message['num_views'], |
||
| 2070 | 'replies' => $message['num_replies'], |
||
| 2071 | 'can_reply' => in_array($message['id_board'], $boards_can['post_reply_any']) || in_array(0, $boards_can['post_reply_any']), |
||
| 2072 | 'can_quote' => (in_array($message['id_board'], $boards_can['post_reply_any']) || in_array(0, $boards_can['post_reply_any'])) && $quote_enabled, |
||
| 2073 | 'first_post' => array( |
||
| 2074 | 'id' => $message['first_msg'], |
||
| 2075 | 'time' => timeformat($message['first_poster_time']), |
||
| 2076 | 'timestamp' => forum_time(true, $message['first_poster_time']), |
||
| 2077 | 'subject' => $message['first_subject'], |
||
| 2078 | 'href' => $scripturl . '?topic=' . $message['id_topic'] . '.0', |
||
| 2079 | 'link' => '<a href="' . $scripturl . '?topic=' . $message['id_topic'] . '.0">' . $message['first_subject'] . '</a>', |
||
| 2080 | 'icon' => $message['first_icon'], |
||
| 2081 | 'icon_url' => $settings[$context['icon_sources'][$message['first_icon']]] . '/post/' . $message['first_icon'] . '.png', |
||
| 2082 | 'member' => array( |
||
| 2083 | 'id' => $message['first_member_id'], |
||
| 2084 | 'name' => $message['first_member_name'], |
||
| 2085 | 'href' => !empty($message['first_member_id']) ? $scripturl . '?action=profile;u=' . $message['first_member_id'] : '', |
||
| 2086 | 'link' => !empty($message['first_member_id']) ? '<a href="' . $scripturl . '?action=profile;u=' . $message['first_member_id'] . '" title="' . $txt['profile_of'] . ' ' . $message['first_member_name'] . '">' . $message['first_member_name'] . '</a>' : $message['first_member_name'] |
||
| 2087 | ) |
||
| 2088 | ), |
||
| 2089 | 'last_post' => array( |
||
| 2090 | 'id' => $message['last_msg'], |
||
| 2091 | 'time' => timeformat($message['last_poster_time']), |
||
| 2092 | 'timestamp' => forum_time(true, $message['last_poster_time']), |
||
| 2093 | 'subject' => $message['last_subject'], |
||
| 2094 | 'href' => $scripturl . '?topic=' . $message['id_topic'] . ($message['num_replies'] == 0 ? '.0' : '.msg' . $message['last_msg']) . '#msg' . $message['last_msg'], |
||
| 2095 | 'link' => '<a href="' . $scripturl . '?topic=' . $message['id_topic'] . ($message['num_replies'] == 0 ? '.0' : '.msg' . $message['last_msg']) . '#msg' . $message['last_msg'] . '">' . $message['last_subject'] . '</a>', |
||
| 2096 | 'icon' => $message['last_icon'], |
||
| 2097 | 'icon_url' => $settings[$context['icon_sources'][$message['last_icon']]] . '/post/' . $message['last_icon'] . '.png', |
||
| 2098 | 'member' => array( |
||
| 2099 | 'id' => $message['last_member_id'], |
||
| 2100 | 'name' => $message['last_member_name'], |
||
| 2101 | 'href' => !empty($message['last_member_id']) ? $scripturl . '?action=profile;u=' . $message['last_member_id'] : '', |
||
| 2102 | 'link' => !empty($message['last_member_id']) ? '<a href="' . $scripturl . '?action=profile;u=' . $message['last_member_id'] . '" title="' . $txt['profile_of'] . ' ' . $message['last_member_name'] . '">' . $message['last_member_name'] . '</a>' : $message['last_member_name'] |
||
| 2103 | ) |
||
| 2104 | ), |
||
| 2105 | 'board' => array( |
||
| 2106 | 'id' => $message['id_board'], |
||
| 2107 | 'name' => $message['board_name'], |
||
| 2108 | 'href' => $scripturl . '?board=' . $message['id_board'] . '.0', |
||
| 2109 | 'link' => '<a href="' . $scripturl . '?board=' . $message['id_board'] . '.0">' . $message['board_name'] . '</a>' |
||
| 2110 | ), |
||
| 2111 | 'category' => array( |
||
| 2112 | 'id' => $message['id_cat'], |
||
| 2113 | 'name' => $message['cat_name'], |
||
| 2114 | 'href' => $scripturl . '#c' . $message['id_cat'], |
||
| 2115 | 'link' => '<a href="' . $scripturl . '#c' . $message['id_cat'] . '">' . $message['cat_name'] . '</a>' |
||
| 2116 | ) |
||
| 2117 | )); |
||
| 2118 | |||
| 2119 | if (!empty($options['display_quick_mod'])) |
||
| 2120 | { |
||
| 2121 | $started = $output['first_post']['member']['id'] == $user_info['id']; |
||
| 2122 | |||
| 2123 | $output['quick_mod'] = array( |
||
| 2124 | 'lock' => in_array(0, $boards_can['lock_any']) || in_array($output['board']['id'], $boards_can['lock_any']) || ($started && (in_array(0, $boards_can['lock_own']) || in_array($output['board']['id'], $boards_can['lock_own']))), |
||
| 2125 | 'sticky' => (in_array(0, $boards_can['make_sticky']) || in_array($output['board']['id'], $boards_can['make_sticky'])), |
||
| 2126 | 'move' => in_array(0, $boards_can['move_any']) || in_array($output['board']['id'], $boards_can['move_any']) || ($started && (in_array(0, $boards_can['move_own']) || in_array($output['board']['id'], $boards_can['move_own']))), |
||
| 2127 | 'remove' => in_array(0, $boards_can['remove_any']) || in_array($output['board']['id'], $boards_can['remove_any']) || ($started && (in_array(0, $boards_can['remove_own']) || in_array($output['board']['id'], $boards_can['remove_own']))), |
||
| 2128 | 'restore' => $context['can_restore_perm'] && ($modSettings['recycle_board'] == $output['board']['id']), |
||
| 2129 | ); |
||
| 2130 | |||
| 2131 | $context['can_lock'] |= $output['quick_mod']['lock']; |
||
| 2132 | $context['can_sticky'] |= $output['quick_mod']['sticky']; |
||
| 2133 | $context['can_move'] |= $output['quick_mod']['move']; |
||
| 2134 | $context['can_remove'] |= $output['quick_mod']['remove']; |
||
| 2135 | $context['can_merge'] |= in_array($output['board']['id'], $boards_can['merge_any']); |
||
| 2136 | $context['can_restore'] |= $output['quick_mod']['restore']; |
||
| 2137 | $context['can_markread'] = $context['user']['is_logged']; |
||
| 2138 | |||
| 2139 | $context['qmod_actions'] = array('remove', 'lock', 'sticky', 'move', 'merge', 'restore', 'markread'); |
||
| 2140 | call_integration_hook('integrate_quick_mod_actions_search'); |
||
| 2141 | } |
||
| 2142 | |||
| 2143 | $output['matches'][] = array( |
||
| 2144 | 'id' => $message['id_msg'], |
||
| 2145 | 'attachment' => array(), |
||
| 2146 | 'member' => &$memberContext[$message['id_member']], |
||
| 2147 | 'icon' => $message['icon'], |
||
| 2148 | 'icon_url' => $settings[$context['icon_sources'][$message['icon']]] . '/post/' . $message['icon'] . '.png', |
||
| 2149 | 'subject' => $message['subject'], |
||
| 2150 | 'subject_highlighted' => $message['subject_highlighted'], |
||
| 2151 | 'time' => timeformat($message['poster_time']), |
||
| 2152 | 'timestamp' => forum_time(true, $message['poster_time']), |
||
| 2153 | 'counter' => $counter, |
||
| 2154 | 'modified' => array( |
||
| 2155 | 'time' => timeformat($message['modified_time']), |
||
| 2156 | 'timestamp' => forum_time(true, $message['modified_time']), |
||
| 2157 | 'name' => $message['modified_name'] |
||
| 2158 | ), |
||
| 2159 | 'body' => $message['body'], |
||
| 2160 | 'body_highlighted' => $message['body_highlighted'], |
||
| 2161 | 'start' => 'msg' . $message['id_msg'] |
||
| 2162 | ); |
||
| 2163 | $counter++; |
||
| 2164 | |||
| 2165 | call_integration_hook('integrate_search_message_context', array(&$output, &$message, $counter)); |
||
| 2166 | |||
| 2167 | return $output; |
||
| 2168 | } |
||
| 2169 | |||
| 2170 | /** |
||
| 2171 | * Creates a search API and returns the object. |
||
| 2172 | * |
||
| 2173 | * @return search_api_interface An instance of the search API interface |
||
| 2174 | */ |
||
| 2175 | function findSearchAPI() |
||
| 2176 | { |
||
| 2177 | global $sourcedir, $modSettings, $searchAPI, $txt; |
||
| 2178 | |||
| 2179 | require_once($sourcedir . '/Subs-Package.php'); |
||
| 2180 | require_once($sourcedir . '/Class-SearchAPI.php'); |
||
| 2181 | |||
| 2182 | // Search has a special database set. |
||
| 2183 | db_extend('search'); |
||
| 2184 | |||
| 2185 | // Load up the search API we are going to use. |
||
| 2186 | $modSettings['search_index'] = empty($modSettings['search_index']) ? 'standard' : $modSettings['search_index']; |
||
| 2187 | if (!file_exists($sourcedir . '/SearchAPI-' . ucwords($modSettings['search_index']) . '.php')) |
||
| 2188 | fatal_lang_error('search_api_missing'); |
||
| 2189 | require_once($sourcedir . '/SearchAPI-' . ucwords($modSettings['search_index']) . '.php'); |
||
| 2190 | |||
| 2191 | // Create an instance of the search API and check it is valid for this version of SMF. |
||
| 2192 | $search_class_name = $modSettings['search_index'] . '_search'; |
||
| 2193 | $searchAPI = new $search_class_name(); |
||
| 2194 | |||
| 2195 | // An invalid Search API. |
||
| 2196 | if (!$searchAPI || !($searchAPI instanceof search_api_interface) || ($searchAPI->supportsMethod('isValid') && !$searchAPI->isValid()) || !matchPackageVersion(SMF_VERSION, $searchAPI->min_smf_version . '-' . $searchAPI->version_compatible)) |
||
|
0 ignored issues
–
show
|
|||
| 2197 | { |
||
| 2198 | // Log the error. |
||
| 2199 | loadLanguage('Errors'); |
||
| 2200 | log_error(sprintf($txt['search_api_not_compatible'], 'SearchAPI-' . ucwords($modSettings['search_index']) . '.php'), 'critical'); |
||
| 2201 | |||
| 2202 | require_once($sourcedir . '/SearchAPI-Standard.php'); |
||
| 2203 | $searchAPI = new standard_search(); |
||
| 2204 | } |
||
| 2205 | |||
| 2206 | return $searchAPI; |
||
| 2207 | } |
||
| 2208 | |||
| 2209 | /** |
||
| 2210 | * This function compares the length of two strings plus a little. |
||
| 2211 | * What it does: |
||
| 2212 | * - callback function for usort used to sort the fulltext results. |
||
| 2213 | * - passes sorting duty to the current API. |
||
| 2214 | * |
||
| 2215 | * @param string $a |
||
| 2216 | * @param string $b |
||
| 2217 | * @return int |
||
| 2218 | */ |
||
| 2219 | function searchSort($a, $b) |
||
| 2220 | { |
||
| 2221 | global $searchAPI; |
||
| 2222 | |||
| 2223 | return $searchAPI->searchSort($a, $b); |
||
| 2224 | } |
||
| 2225 | |||
| 2226 | /** |
||
| 2227 | * Highlighting matching string |
||
| 2228 | * |
||
| 2229 | * @param string $text Text to search through |
||
| 2230 | * @param array $words List of keywords to search |
||
| 2231 | * |
||
| 2232 | * @return string Text with highlighted keywords |
||
| 2233 | */ |
||
| 2234 | function highlight($text, array $words) |
||
| 2235 | { |
||
| 2236 | $words = implode('|', array_map('preg_quote', $words)); |
||
| 2237 | $highlighted = preg_filter('/' . $words . '/i', '<span class="highlight">$0</span>', $text); |
||
| 2238 | |||
| 2239 | if (!empty($highlighted)) |
||
| 2240 | $text = $highlighted; |
||
| 2241 | |||
| 2242 | return $text; |
||
| 2243 | } |
||
| 2244 | |||
| 2245 | ?> |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.