2 /***************************************************************************
5 * begin : Wed Sep 05 2001
6 * copyright : (C) 2002 The phpBB Group
7 * email : support@phpbb.com
9 * $Id: functions_search.php,v 1.8.2.20 2005/09/14 18:14:30 acydburn Exp $
11 ****************************************************************************/
13 /***************************************************************************
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 ***************************************************************************/
22 function clean_words($mode, &$entry, &$stopword_list, &$synonym_list)
24 static $drop_char_match = array('^', '$', '&', '(', ')', '<', '>', '`', '\'', '"', '|', ',', '@', '_', '?', '%', '-', '~', '+', '.', '[', ']', '{', '}', ':', '\\', '/', '=', '#', '\'', ';', '!');
25 static $drop_char_replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', '', '', ' ', ' ', ' ', ' ', '', ' ', ' ', '', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' , ' ', ' ', ' ', ' ', ' ', ' ');
27 $entry = ' ' . strip_tags(strtolower($entry)) . ' ';
29 if ( $mode == 'post' )
31 // Replace line endings by a space
32 $entry = preg_replace('/[\n\r]/is', ' ', $entry);
33 // HTML entities like
34 $entry = preg_replace('/\b&[a-z]+;\b/', ' ', $entry);
36 $entry = preg_replace('/\b[a-z0-9]+:\/\/[a-z0-9\.\-]+(\/[a-z0-9\?\.%_\-\+=&\/]+)?/', ' ', $entry);
37 // Quickly remove BBcode.
38 $entry = preg_replace('/\[img:[a-z0-9]{10,}\].*?\[\/img:[a-z0-9]{10,}\]/', ' ', $entry);
39 $entry = preg_replace('/\[\/?url(=.*?)?\]/', ' ', $entry);
40 $entry = preg_replace('/\[\/?[a-z\*=\+\-]+(\:?[0-9a-z]+)?:[a-z0-9]{10,}(\:[a-z0-9]+)?=?.*?\]/', ' ', $entry);
42 else if ( $mode == 'search' )
44 $entry = str_replace(' +', ' and ', $entry);
45 $entry = str_replace(' -', ' not ', $entry);
49 // Filter out strange characters like ^, $, &, change "it's" to "its"
51 for($i = 0; $i < count($drop_char_match); $i++)
53 $entry = str_replace($drop_char_match[$i], $drop_char_replace[$i], $entry);
56 if ( $mode == 'post' )
58 $entry = str_replace('*', ' ', $entry);
60 // 'words' that consist of <3 or >20 characters are removed.
61 $entry = preg_replace('/[ ]([\S]{1,2}|[\S]{21,})[ ]/',' ', $entry);
64 if ( !empty($stopword_list) )
66 for ($j = 0; $j < count($stopword_list); $j++)
68 $stopword = trim($stopword_list[$j]);
70 if ( $mode == 'post' || ( $stopword != 'not' && $stopword != 'and' && $stopword != 'or' ) )
72 $entry = str_replace(' ' . trim($stopword) . ' ', ' ', $entry);
77 if ( !empty($synonym_list) )
79 for ($j = 0; $j < count($synonym_list); $j++)
81 list($replace_synonym, $match_synonym) = split(' ', trim(strtolower($synonym_list[$j])));
82 if ( $mode == 'post' || ( $match_synonym != 'not' && $match_synonym != 'and' && $match_synonym != 'or' ) )
84 $entry = str_replace(' ' . trim($match_synonym) . ' ', ' ' . trim($replace_synonym) . ' ', $entry);
92 function split_words($entry, $mode = 'post')
94 // If you experience problems with the new method, uncomment this block.
96 $rex = ( $mode == 'post' ) ? "/\b([\w±µ-ÿ][\w±µ-ÿ']*[\w±µ-ÿ]+|[\w±µ-ÿ]+?)\b/" : '/(\*?[a-z0-9±µ-ÿ]+\*?)|\b([a-z0-9±µ-ÿ]+)\b/';
97 preg_match_all($rex, $entry, $split_entries);
99 return $split_entries[1];
101 // Trim 1+ spaces to one space and split this trimmed string into words.
102 return explode(' ', trim(preg_replace('#\s+#', ' ', $entry)));
105 function add_search_words($mode, $post_id, $post_text, $post_title = '')
107 global $db, $phpbb_root_path, $board_config, $lang;
109 $stopword_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . "/search_stopwords.txt");
110 $synonym_array = @file($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . "/search_synonyms.txt");
112 $search_raw_words = array();
113 $search_raw_words['text'] = split_words(clean_words('post', $post_text, $stopword_array, $synonym_array));
114 $search_raw_words['title'] = split_words(clean_words('post', $post_title, $stopword_array, $synonym_array));
119 $word_insert_sql = array();
120 while ( list($word_in, $search_matches) = @each($search_raw_words) )
122 $word_insert_sql[$word_in] = '';
123 if ( !empty($search_matches) )
125 for ($i = 0; $i < count($search_matches); $i++)
127 $search_matches[$i] = trim($search_matches[$i]);
129 if( $search_matches[$i] != '' )
131 $word[] = $search_matches[$i];
132 if ( !strstr($word_insert_sql[$word_in], "'" . $search_matches[$i] . "'") )
134 $word_insert_sql[$word_in] .= ( $word_insert_sql[$word_in] != "" ) ? ", '" . $search_matches[$i] . "'" : "'" . $search_matches[$i] . "'";
147 $temp_word = array();
148 for($i = 0; $i < count($word); $i++)
150 if ( $word[$i] != $prev_word )
152 $temp_word[] = $word[$i];
153 $word_text_sql .= ( ( $word_text_sql != '' ) ? ', ' : '' ) . "'" . $word[$i] . "'";
155 $prev_word = $word[$i];
159 $check_words = array();
167 $sql = "SELECT word_id, word_text
168 FROM " . SEARCH_WORD_TABLE . "
169 WHERE word_text IN ($word_text_sql)";
170 if ( !($result = $db->sql_query($sql)) )
172 message_die(GENERAL_ERROR, 'Could not select words', '', __LINE__, __FILE__, $sql);
175 while ( $row = $db->sql_fetchrow($result) )
177 $check_words[$row['word_text']] = $row['word_id'];
183 $match_word = array();
184 for ($i = 0; $i < count($word); $i++)
187 if ( isset($check_words[$word[$i]]) )
198 $value_sql .= ( ( $value_sql != '' ) ? ', ' : '' ) . '(\'' . $word[$i] . '\', 0)';
202 $value_sql .= ( ( $value_sql != '' ) ? ' UNION ALL ' : '' ) . "SELECT '" . $word[$i] . "', 0";
205 $sql = "INSERT INTO " . SEARCH_WORD_TABLE . " (word_text, word_common)
206 VALUES ('" . $word[$i] . "', 0)";
207 if( !$db->sql_query($sql) )
209 message_die(GENERAL_ERROR, 'Could not insert new word', '', __LINE__, __FILE__, $sql);
216 if ( $value_sql != '' )
222 $sql = "INSERT IGNORE INTO " . SEARCH_WORD_TABLE . " (word_text, word_common)
227 $sql = "INSERT INTO " . SEARCH_WORD_TABLE . " (word_text, word_common)
232 if ( !$db->sql_query($sql) )
234 message_die(GENERAL_ERROR, 'Could not insert new word', '', __LINE__, __FILE__, $sql);
239 while( list($word_in, $match_sql) = @each($word_insert_sql) )
241 $title_match = ( $word_in == 'title' ) ? 1 : 0;
243 if ( $match_sql != '' )
245 $sql = "INSERT INTO " . SEARCH_MATCH_TABLE . " (post_id, word_id, title_match)
246 SELECT $post_id, word_id, $title_match
247 FROM " . SEARCH_WORD_TABLE . "
248 WHERE word_text IN ($match_sql)";
249 if ( !$db->sql_query($sql) )
251 message_die(GENERAL_ERROR, 'Could not insert new word matches', '', __LINE__, __FILE__, $sql);
256 if ($mode == 'single')
258 remove_common('single', 4/10, $word);
265 // Check if specified words are too common now
267 function remove_common($mode, $fraction, $word_id_list = array())
271 $sql = "SELECT COUNT(post_id) AS total_posts
272 FROM " . POSTS_TABLE;
273 if ( !($result = $db->sql_query($sql)) )
275 message_die(GENERAL_ERROR, 'Could not obtain post count', '', __LINE__, __FILE__, $sql);
278 $row = $db->sql_fetchrow($result);
280 if ( $row['total_posts'] >= 100 )
282 $common_threshold = floor($row['total_posts'] * $fraction);
284 if ( $mode == 'single' && count($word_id_list) )
287 for($i = 0; $i < count($word_id_list); $i++)
289 $word_id_sql .= ( ( $word_id_sql != '' ) ? ', ' : '' ) . "'" . $word_id_list[$i] . "'";
292 $sql = "SELECT m.word_id
293 FROM " . SEARCH_MATCH_TABLE . " m, " . SEARCH_WORD_TABLE . " w
294 WHERE w.word_text IN ($word_id_sql)
295 AND m.word_id = w.word_id
297 HAVING COUNT(m.word_id) > $common_threshold";
301 $sql = "SELECT word_id
302 FROM " . SEARCH_MATCH_TABLE . "
304 HAVING COUNT(word_id) > $common_threshold";
307 if ( !($result = $db->sql_query($sql)) )
309 message_die(GENERAL_ERROR, 'Could not obtain common word list', '', __LINE__, __FILE__, $sql);
312 $common_word_id = '';
313 while ( $row = $db->sql_fetchrow($result) )
315 $common_word_id .= ( ( $common_word_id != '' ) ? ', ' : '' ) . $row['word_id'];
317 $db->sql_freeresult($result);
319 if ( $common_word_id != '' )
321 $sql = "UPDATE " . SEARCH_WORD_TABLE . "
322 SET word_common = " . TRUE . "
323 WHERE word_id IN ($common_word_id)";
324 if ( !$db->sql_query($sql) )
326 message_die(GENERAL_ERROR, 'Could not delete word list entry', '', __LINE__, __FILE__, $sql);
329 $sql = "DELETE FROM " . SEARCH_MATCH_TABLE . "
330 WHERE word_id IN ($common_word_id)";
331 if ( !$db->sql_query($sql) )
333 message_die(GENERAL_ERROR, 'Could not delete word match entry', '', __LINE__, __FILE__, $sql);
341 function remove_search_post($post_id_sql)
345 $words_removed = false;
351 $sql = "SELECT word_id
352 FROM " . SEARCH_MATCH_TABLE . "
353 WHERE post_id IN ($post_id_sql)
355 if ( $result = $db->sql_query($sql) )
358 while ( $row = $db->sql_fetchrow($result) )
360 $word_id_sql .= ( $word_id_sql != '' ) ? ', ' . $row['word_id'] : $row['word_id'];
363 $sql = "SELECT word_id
364 FROM " . SEARCH_MATCH_TABLE . "
365 WHERE word_id IN ($word_id_sql)
367 HAVING COUNT(word_id) = 1";
368 if ( $result = $db->sql_query($sql) )
371 while ( $row = $db->sql_fetchrow($result) )
373 $word_id_sql .= ( $word_id_sql != '' ) ? ', ' . $row['word_id'] : $row['word_id'];
376 if ( $word_id_sql != '' )
378 $sql = "DELETE FROM " . SEARCH_WORD_TABLE . "
379 WHERE word_id IN ($word_id_sql)";
380 if ( !$db->sql_query($sql) )
382 message_die(GENERAL_ERROR, 'Could not delete word list entry', '', __LINE__, __FILE__, $sql);
385 $words_removed = $db->sql_affectedrows();
392 $sql = "DELETE FROM " . SEARCH_WORD_TABLE . "
395 FROM " . SEARCH_MATCH_TABLE . "
398 FROM " . SEARCH_MATCH_TABLE . "
399 WHERE post_id IN ($post_id_sql)
403 HAVING COUNT(word_id) = 1
405 if ( !$db->sql_query($sql) )
407 message_die(GENERAL_ERROR, 'Could not delete old words from word table', '', __LINE__, __FILE__, $sql);
410 $words_removed = $db->sql_affectedrows();
415 $sql = "DELETE FROM " . SEARCH_MATCH_TABLE . "
416 WHERE post_id IN ($post_id_sql)";
417 if ( !$db->sql_query($sql) )
419 message_die(GENERAL_ERROR, 'Error in deleting post', '', __LINE__, __FILE__, $sql);
422 return $words_removed;
428 function username_search($search_match)
430 global $db, $board_config, $template, $lang, $images, $theme, $phpEx, $phpbb_root_path;
431 global $starttime, $gen_simple_header;
433 $gen_simple_header = TRUE;
436 if ( !empty($search_match) )
438 $username_search = preg_replace('/\*/', '%', phpbb_clean_username($search_match));
440 $sql = "SELECT username
441 FROM " . USERS_TABLE . "
442 WHERE username LIKE '" . str_replace("\'", "''", $username_search) . "' AND user_id <> " . ANONYMOUS . "
444 if ( !($result = $db->sql_query($sql)) )
446 message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
449 if ( $row = $db->sql_fetchrow($result) )
453 $username_list .= '<option value="' . $row['username'] . '">' . $row['username'] . '</option>';
455 while ( $row = $db->sql_fetchrow($result) );
459 $username_list .= '<option>' . $lang['No_match']. '</option>';
461 $db->sql_freeresult($result);
464 $page_title = $lang['Search'];
465 include($phpbb_root_path . 'includes/page_header.'.$phpEx);
467 $template->set_filenames(array(
468 'search_user_body' => 'search_username.tpl')
471 $template->assign_vars(array(
472 'USERNAME' => (!empty($search_match)) ? phpbb_clean_username($search_match) : '',
474 'L_CLOSE_WINDOW' => $lang['Close_window'],
475 'L_SEARCH_USERNAME' => $lang['Find_username'],
476 'L_UPDATE_USERNAME' => $lang['Select_username'],
477 'L_SELECT' => $lang['Select'],
478 'L_SEARCH' => $lang['Search'],
479 'L_SEARCH_EXPLAIN' => $lang['Search_author_explain'],
480 'L_CLOSE_WINDOW' => $lang['Close_window'],
482 'S_USERNAME_OPTIONS' => $username_list,
483 'S_SEARCH_ACTION' => append_sid("search.$phpEx?mode=searchuser"))
486 if ( $username_list != '' )
488 $template->assign_block_vars('switch_select_name', array());
491 $template->pparse('search_user_body');
493 include($phpbb_root_path . 'includes/page_tail.'.$phpEx);