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