query('SELECT search_data FROM '.$db->prefix.'search_cache WHERE id='.$search_id.' AND ident=\''.$db->escape($ident).'\'') or error('Impossible de retrouver les résultats de la recherche', __FILE__, __LINE__, $db->error()); if ($row = $db->fetch_assoc($result)) { $temp = unserialize($row['search_data']); $search_results = $temp['search_results']; $num_hits = $temp['num_hits']; $sort_by = $temp['sort_by']; $sort_dir = $temp['sort_dir']; $show_as = $temp['show_as']; unset($temp); } else message($lang_search['No hits']); } else { $keyword_results = $author_results = array(); // Search a specific forum? $forum_sql = ($forum != -1) ? ' AND t.forum_id = '.$forum : ''; if (!empty($author) || !empty($keywords)) { // If it's a search for keywords if ($keywords) { $stopwords = (array)@file(PUN_ROOT.'lang/'.$pun_user['language'].'/stopwords.txt'); $stopwords = array_map('trim', $stopwords); // Are we searching for multibyte charset text? if ($multibyte) { // Strip out excessive whitespace $keywords = trim(preg_replace('#\s+#', ' ', $keywords)); $keywords_array = explode(' ', $keywords); } else { // Filter out non-alphabetical chars $noise_match = array('^', '$', '&', '(', ')', '<', '>', '`', '\'', '"', '|', ',', '@', '_', '?', '%', '~', '[', ']', '{', '}', ':', '\\', '/', '=', '#', '\'', ';', '!', '¤'); $noise_replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', '', '', ' ', ' ', ' ', ' ', '', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '' , ' ', ' ', ' ', ' ', ' ', ' ', ' '); $keywords = str_replace($noise_match, $noise_replace, $keywords); // Strip out excessive whitespace $keywords = trim(preg_replace('#\s+#', ' ', $keywords)); // Fill an array with all the words $keywords_array = explode(' ', $keywords); if (empty($keywords_array)) message($lang_search['No hits']); while (list($i, $word) = @each($keywords_array)) { $num_chars = pun_strlen($word); if ($num_chars < 3 || $num_chars > 20 || in_array($word, $stopwords)) unset($keywords_array[$i]); } // Should we search in message body or topic subject specifically? $search_in_cond = ($search_in) ? (($search_in > 0) ? ' AND m.subject_match = 0' : ' AND m.subject_match = 1') : ''; } $word_count = 0; $match_type = 'and'; @reset($keywords_array); while (list(, $cur_word) = @each($keywords_array)) { switch ($cur_word) { case 'and': case 'or': case 'not': $match_type = $cur_word; break; default: { // Are we searching for multibyte charset text? if ($multibyte) { $cur_word = $db->escape('%'.str_replace('*', '', $cur_word).'%'); $cur_word_like = ($db_type == 'pgsql') ? 'ILIKE \''.$cur_word.'\'' : 'LIKE \''.$cur_word.'\''; if ($search_in > 0) $sql = 'SELECT id FROM '.$db->prefix.'posts WHERE message '.$cur_word_like; else if ($search_in < 0) $sql = 'SELECT p.id FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id WHERE t.subject '.$cur_word_like.' GROUP BY p.id, t.id'; else $sql = 'SELECT p.id FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id WHERE p.message '.$cur_word_like.' OR t.subject '.$cur_word_like.' GROUP BY p.id, t.id'; } else { $cur_word = str_replace('*', '%', $cur_word); $sql = 'SELECT m.post_id FROM '.$db->prefix.'search_words AS w INNER JOIN '.$db->prefix.'search_matches AS m ON m.word_id = w.id WHERE w.word LIKE \''.$cur_word.'\''.$search_in_cond; } $result = $db->query($sql, true) or error('Impossible de rechercher les messages', __FILE__, __LINE__, $db->error()); $row = array(); while ($temp = $db->fetch_row($result)) { $row[$temp[0]] = 1; if (!$word_count) $result_list[$temp[0]] = 1; else if ($match_type == 'or') $result_list[$temp[0]] = 1; else if ($match_type == 'not') $result_list[$temp[0]] = 0; } if ($match_type == 'and' && $word_count) { @reset($result_list); while (list($post_id,) = @each($result_list)) { if (!isset($row[$post_id])) $result_list[$post_id] = 0; } } ++$word_count; $db->free_result($result); break; } } } @reset($result_list); while (list($post_id, $matches) = @each($result_list)) { if ($matches) $keyword_results[] = $post_id; } unset($result_list); } // If it's a search for author name (and that author name isn't Guest) if ($author && strcasecmp($author, 'Guest') && strcasecmp($author, $lang_common['Guest'])) { switch ($db_type) { case 'pgsql': $result = $db->query('SELECT id FROM '.$db->prefix.'users WHERE username ILIKE \''.$db->escape($author).'\'') or error('Impossible de retrouver les membres', __FILE__, __LINE__, $db->error()); break; default: $result = $db->query('SELECT id FROM '.$db->prefix.'users WHERE username LIKE \''.$db->escape($author).'\'') or error('Impossible de retrouver les membres', __FILE__, __LINE__, $db->error()); break; } if ($db->num_rows($result)) { $user_ids = ''; while ($row = $db->fetch_row($result)) $user_ids .= (($user_ids != '') ? ',' : '').$row[0]; $result = $db->query('SELECT id FROM '.$db->prefix.'posts WHERE poster_id IN('.$user_ids.')') or error('Impossible de retrouver la liste des messages correspondants', __FILE__, __LINE__, $db->error()); $search_ids = array(); while ($row = $db->fetch_row($result)) $author_results[] = $row[0]; $db->free_result($result); } } if ($author && $keywords) { // If we searched for both keywords and author name we want the intersection between the results $search_ids = array_intersect($keyword_results, $author_results); unset($keyword_results, $author_results); } else if ($keywords) $search_ids = $keyword_results; else $search_ids = $author_results; $num_hits = count($search_ids); if (!$num_hits) message($lang_search['No hits']); if ($show_as == 'topics') { $result = $db->query('SELECT t.id FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND p.id IN('.implode(',', $search_ids).')'.$forum_sql.' GROUP BY t.id', true) or error('Impossible de retrouver la liste des discussions', __FILE__, __LINE__, $db->error()); $search_ids = array(); while ($row = $db->fetch_row($result)) $search_ids[] = $row[0]; $db->free_result($result); $num_hits = count($search_ids); } else { $result = $db->query('SELECT p.id FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND p.id IN('.implode(',', $search_ids).')'.$forum_sql, true) or error('Impossible de retrouver la liste des discussions', __FILE__, __LINE__, $db->error()); $search_ids = array(); while ($row = $db->fetch_row($result)) $search_ids[] = $row[0]; $db->free_result($result); $num_hits = count($search_ids); } } else if ($action == 'show_new' || $action == 'show_24h' || $action == 'show_user' || $action == 'show_subscriptions' || $action == 'show_unanswered') { // If it's a search for new posts if ($action == 'show_new') { if ($pun_user['is_guest']) message($lang_common['No permission']); $result = $db->query('SELECT t.id FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.last_post>'.$pun_user['last_visit']) or error('Impossible de retrouver la liste des discussions', __FILE__, __LINE__, $db->error()); $num_hits = $db->num_rows($result); if (!$num_hits) message($lang_search['No new posts']); } // If it's a search for todays posts else if ($action == 'show_24h') { $result = $db->query('SELECT t.id FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.last_post>'.(time() - 86400)) or error('Impossible de retrouver la liste des discussions', __FILE__, __LINE__, $db->error()); $num_hits = $db->num_rows($result); if (!$num_hits) message($lang_search['No recent posts']); } // If it's a search for posts by a specific user ID else if ($action == 'show_user') { $result = $db->query('SELECT t.id FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'posts AS p ON t.id=p.topic_id INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND p.poster_id='.$user_id.' GROUP BY t.id') or error('Impossible de retrouver la liste des discussions', __FILE__, __LINE__, $db->error()); $num_hits = $db->num_rows($result); if (!$num_hits) message($lang_search['No user posts']); } // If it's a search for subscribed topics else if ($action == 'show_subscriptions') { if ($pun_user['is_guest']) message($lang_common['Bad request']); $result = $db->query('SELECT t.id FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'subscriptions AS s ON (t.id=s.topic_id AND s.user_id='.$pun_user['id'].') INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1)') or error('Impossible de retrouver la liste des discussions', __FILE__, __LINE__, $db->error()); $num_hits = $db->num_rows($result); if (!$num_hits) message($lang_search['No subscriptions']); } // If it's a search for unanswered posts else { $result = $db->query('SELECT t.id FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.num_replies=0 AND t.moved_to IS NULL') or error('Impossible de retrouver la liste des discussions', __FILE__, __LINE__, $db->error()); $num_hits = $db->num_rows($result); if (!$num_hits) message($lang_search['No unanswered']); } // We want to sort things after last post $sort_by = 4; $search_ids = array(); while ($row = $db->fetch_row($result)) $search_ids[] = $row[0]; $db->free_result($result); $show_as = 'topics'; } else message($lang_common['Bad request']); // Prune "old" search results $old_searches = array(); $result = $db->query('SELECT ident FROM '.$db->prefix.'online') or error('Impossible de retrouver la liste de utilisateurs en ligne', __FILE__, __LINE__, $db->error()); if ($db->num_rows($result)) { while ($row = $db->fetch_row($result)) $old_searches[] = '\''.$db->escape($row[0]).'\''; $db->query('DELETE FROM '.$db->prefix.'search_cache WHERE ident NOT IN('.implode(',', $old_searches).')') or error('Impossible de supprimer les résultats de recherches', __FILE__, __LINE__, $db->error()); } // Final search results $search_results = implode(',', $search_ids); // Fill an array with our results and search properties $temp['search_results'] = $search_results; $temp['num_hits'] = $num_hits; $temp['sort_by'] = $sort_by; $temp['sort_dir'] = $sort_dir; $temp['show_as'] = $show_as; $temp = serialize($temp); $search_id = mt_rand(1, 2147483647); $ident = ($pun_user['is_guest']) ? get_remote_address() : $pun_user['username']; $db->query('INSERT INTO '.$db->prefix.'search_cache (id, ident, search_data) VALUES('.$search_id.', \''.$db->escape($ident).'\', \''.$db->escape($temp).'\')') or error('Impossible d\'insérer les résultats de recherche', __FILE__, __LINE__, $db->error()); if ($action != 'show_new' && $action != 'show_24h') { $db->end_transaction(); $db->close(); // Redirect the user to the cached result page header('Location: search.php?search_id='.$search_id); exit; } } // Fetch results to display if ($search_results != '') { $group_by_sql = ''; switch ($sort_by) { case 1: $sort_by_sql = ($show_as == 'topics') ? 't.poster' : 'p.poster'; break; case 2: $sort_by_sql = 't.subject'; break; case 3: $sort_by_sql = 't.forum_id'; break; case 4: $sort_by_sql = 't.last_post'; break; default: { $sort_by_sql = ($show_as == 'topics') ? 't.posted' : 'p.posted'; if ($show_as == 'topics') $group_by_sql = ', t.posted'; break; } } if ($show_as == 'posts') { $substr_sql = ($db_type != 'sqlite') ? 'SUBSTRING' : 'SUBSTR'; $sql = 'SELECT p.id AS pid, p.poster AS pposter, p.posted AS pposted, p.poster_id, '.$substr_sql.'(p.message, 1, 1000) AS message, t.id AS tid, t.poster, t.subject, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.forum_id FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id WHERE p.id IN('.$search_results.') ORDER BY '.$sort_by_sql; } else $sql = 'SELECT t.id AS tid, t.poster, t.subject, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.closed, t.forum_id FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id WHERE t.id IN('.$search_results.') GROUP BY t.id, t.poster, t.subject, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.closed, t.forum_id'.$group_by_sql.' ORDER BY '.$sort_by_sql; // Determine the topic or post offset (based on $_GET['p']) $per_page = ($show_as == 'posts') ? $pun_user['disp_posts'] : $pun_user['disp_topics']; $num_pages = ceil($num_hits / $per_page); $p = (!isset($_GET['p']) || $_GET['p'] <= 1 || $_GET['p'] > $num_pages) ? 1 : $_GET['p']; $start_from = $per_page * ($p - 1); // Generate paging links $paging_links = $lang_common['Pages'].': '.paginate($num_pages, $p, 'search.php?search_id='.$search_id); $sql .= ' '.$sort_dir.' LIMIT '.$start_from.', '.$per_page; $result = $db->query($sql) or error('Impossible de retrouver les résultats de recherches', __FILE__, __LINE__, $db->error()); $search_set = array(); while ($row = $db->fetch_assoc($result)) $search_set[] = $row; $db->free_result($result); $page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / '.$lang_search['Search results']; require PUN_ROOT.'header.php'; ?>
|
'.format_time($search_set[$i]['last_post']).' '.$lang_common['by'].' '.pun_htmlspecialchars($search_set[$i]['last_poster']) ?> |