query('SELECT search_data FROM '.$db->prefix.'search_cache WHERE id='.$search_id.' AND ident=\''.$db->escape($ident).'\'') or error('Unable to fetch search results', __FILE__, __LINE__, $db->error()); if ($row = $db->fetch_assoc($result)) { $temp = unserialize($row['search_data']); $search_ids = unserialize($temp['search_ids']); $num_hits = $temp['num_hits']; $sort_by = $temp['sort_by']; $sort_dir = $temp['sort_dir']; $show_as = $temp['show_as']; $search_type = $temp['search_type']; unset($temp); } else message($lang_search['No hits']); } else { $keyword_results = $author_results = array(); // Search a specific forum? $forum_sql = (!empty($forums) || (empty($forums) && $pun_config['o_search_all_forums'] == '0' && !$pun_user['is_admmod'])) ? ' AND t.forum_id IN ('.implode(',', $forums).')' : ''; if (!empty($author) || !empty($keywords)) { // Flood protection if ($pun_user['last_search'] && (time() - $pun_user['last_search']) < $pun_user['g_search_flood'] && (time() - $pun_user['last_search']) >= 0) message(sprintf($lang_search['Search flood'], $pun_user['g_search_flood'])); if (!$pun_user['is_guest']) $db->query('UPDATE '.$db->prefix.'users SET last_search='.time().' WHERE id='.$pun_user['id']) or error('Unable to update user', __FILE__, __LINE__, $db->error()); else $db->query('UPDATE '.$db->prefix.'online SET last_search='.time().' WHERE ident=\''.$db->escape(get_remote_address()).'\'' ) or error('Unable to update user', __FILE__, __LINE__, $db->error()); switch ($sort_by) { case 1: $sort_by_sql = ($show_as == 'topics') ? 't.poster' : 'p.poster'; $sort_type = SORT_STRING; break; case 2: $sort_by_sql = 't.subject'; $sort_type = SORT_STRING; break; case 3: $sort_by_sql = 't.forum_id'; $sort_type = SORT_NUMERIC; break; case 4: $sort_by_sql = 't.last_post'; $sort_type = SORT_NUMERIC; break; default: $sort_by_sql = ($show_as == 'topics') ? 't.last_post' : 'p.posted'; $sort_type = SORT_NUMERIC; break; } // If it's a search for keywords if ($keywords) { // split the keywords into words $keywords_array = split_words($keywords, false); if (empty($keywords_array)) message($lang_search['No hits']); // 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'; $sort_data = array(); foreach ($keywords_array as $cur_word) { switch ($cur_word) { case 'and': case 'or': case 'not': $match_type = $cur_word; break; default: { if (is_cjk($cur_word)) { $where_cond = str_replace('*', '%', $cur_word); $where_cond = ($search_in ? (($search_in > 0) ? 'p.message LIKE \'%'.$db->escape($where_cond).'%\'' : 't.subject LIKE \'%'.$db->escape($where_cond).'%\'') : 'p.message LIKE \'%'.$db->escape($where_cond).'%\' OR t.subject LIKE \'%'.$db->escape($where_cond).'%\''); $result = $db->query('SELECT p.id AS post_id, p.topic_id, '.$sort_by_sql.' AS sort_by FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_id AND fp.group_id='.$pun_user['g_id'].') WHERE ('.$where_cond.') AND (fp.read_forum IS NULL OR fp.read_forum=1)'.$forum_sql, true) or error('Unable to search for posts', __FILE__, __LINE__, $db->error()); } else $result = $db->query('SELECT m.post_id, p.topic_id, '.$sort_by_sql.' AS sort_by FROM '.$db->prefix.'search_words AS w INNER JOIN '.$db->prefix.'search_matches AS m ON m.word_id = w.id INNER JOIN '.$db->prefix.'posts AS p ON p.id=m.post_id INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_id AND fp.group_id='.$pun_user['g_id'].') WHERE w.word LIKE \''.$db->escape(str_replace('*', '%', $cur_word)).'\''.$search_in_cond.' AND (fp.read_forum IS NULL OR fp.read_forum=1)'.$forum_sql, true) or error('Unable to search for posts', __FILE__, __LINE__, $db->error()); $row = array(); while ($temp = $db->fetch_assoc($result)) { $row[$temp['post_id']] = $temp['topic_id']; if (!$word_count) { $keyword_results[$temp['post_id']] = $temp['topic_id']; $sort_data[$temp['post_id']] = $temp['sort_by']; } else if ($match_type == 'or') { $keyword_results[$temp['post_id']] = $temp['topic_id']; $sort_data[$temp['post_id']] = $temp['sort_by']; } else if ($match_type == 'not') { unset($keyword_results[$temp['post_id']]); unset($sort_data[$temp['post_id']]); } } if ($match_type == 'and' && $word_count) { foreach ($keyword_results as $post_id => $topic_id) { if (!isset($row[$post_id])) { unset($keyword_results[$post_id]); unset($sort_data[$post_id]); } } } ++$word_count; $db->free_result($result); break; } } } // Sort the results - annoyingly array_multisort re-indexes arrays with numeric keys, so we need to split the keys out into a seperate array then combine them again after $post_ids = array_keys($keyword_results); $topic_ids = array_values($keyword_results); array_multisort(array_values($sort_data), $sort_dir == 'DESC' ? SORT_DESC : SORT_ASC, $sort_type, $post_ids, $topic_ids); // combine the arrays back into a key=>value array (array_combine is PHP5 only unfortunately) $num_results = count($keyword_results); $keyword_results = array(); for ($i = 0;$i < $num_results;$i++) $keyword_results[$post_ids[$i]] = $topic_ids[$i]; unset($sort_data, $post_ids, $topic_ids); } // If it's a search for author name (and that author name isn't Guest) if ($author && $author != 'guest' && $author != utf8_strtolower($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('Unable to fetch users', __FILE__, __LINE__, $db->error()); break; default: $result = $db->query('SELECT id FROM '.$db->prefix.'users WHERE username LIKE \''.$db->escape($author).'\'') or error('Unable to fetch users', __FILE__, __LINE__, $db->error()); break; } if ($db->num_rows($result)) { $user_ids = array(); while ($row = $db->fetch_row($result)) $user_ids[] = $row[0]; $result = $db->query('SELECT p.id AS post_id, p.topic_id FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND p.poster_id IN('.implode(',', $user_ids).')'.$forum_sql.' ORDER BY '.$sort_by_sql.' '.$sort_dir) or error('Unable to fetch matched posts list', __FILE__, __LINE__, $db->error()); while ($temp = $db->fetch_assoc($result)) $author_results[$temp['post_id']] = $temp['topic_id']; $db->free_result($result); } } // If we searched for both keywords and author name we want the intersection between the results if ($author && $keywords) { $search_ids = array_intersect_assoc($keyword_results, $author_results); $search_type = array('both', array($keywords, pun_trim($_GET['author'])), implode(',', $forums), $search_in); } else if ($keywords) { $search_ids = $keyword_results; $search_type = array('keywords', $keywords, implode(',', $forums), $search_in); } else { $search_ids = $author_results; $search_type = array('author', pun_trim($_GET['author']), implode(',', $forums), $search_in); } unset($keyword_results, $author_results); if ($show_as == 'topics') $search_ids = array_values($search_ids); else $search_ids = array_keys($search_ids); $search_ids = array_unique($search_ids); $num_hits = count($search_ids); if (!$num_hits) message($lang_search['No hits']); } else if ($action == 'show_new' || $action == 'show_recent' || $action == 'show_replies' || $action == 'show_user_posts' || $action == 'show_user_topics' || $action == 'show_subscriptions' || $action == 'show_unanswered') { $search_type = array('action', $action); $show_as = 'topics'; // We want to sort things after last post $sort_by = 0; $sort_dir = 'DESC'; // If it's a search for new posts since last visit 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 LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_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'].' AND t.moved_to IS NULL'.(isset($_GET['fid']) ? ' AND t.forum_id='.intval($_GET['fid']) : '').' ORDER BY t.last_post DESC') or error('Unable to fetch topic list', __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 recent posts (in a certain time interval) else if ($action == 'show_recent') { $result = $db->query('SELECT t.id FROM '.$db->prefix.'topics AS t LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_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() - $interval).' AND t.moved_to IS NULL'.(isset($_GET['fid']) ? ' AND t.forum_id='.intval($_GET['fid']) : '').' ORDER BY t.last_post DESC') or error('Unable to fetch topic list', __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 topics in which the user has posted else if ($action == 'show_replies') { $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 LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND p.poster_id='.$pun_user['id'].' GROUP BY t.id'.($db_type == 'pgsql' ? ', t.last_post' : '').' ORDER BY t.last_post DESC') or error('Unable to fetch topic list', __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 posts by a specific user ID else if ($action == 'show_user_posts') { $show_as = 'posts'; $result = $db->query('SELECT p.id FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON p.topic_id=t.id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_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.' ORDER BY p.posted DESC') or error('Unable to fetch user posts', __FILE__, __LINE__, $db->error()); $num_hits = $db->num_rows($result); if (!$num_hits) message($lang_search['No user posts']); // Pass on the user ID so that we can later know whos posts we're searching for $search_type[2] = $user_id; } // If it's a search for topics by a specific user ID else if ($action == 'show_user_topics') { $result = $db->query('SELECT t.id FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'posts AS p ON t.first_post_id=p.id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_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.' ORDER BY t.last_post DESC') or error('Unable to fetch user topics', __FILE__, __LINE__, $db->error()); $num_hits = $db->num_rows($result); if (!$num_hits) message($lang_search['No user topics']); // Pass on the user ID so that we can later know whos topics we're searching for $search_type[2] = $user_id; } // 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.'topic_subscriptions AS s ON (t.id=s.topic_id AND s.user_id='.$user_id.') LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) ORDER BY t.last_post DESC') or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error()); $num_hits = $db->num_rows($result); if (!$num_hits) message($lang_search['No subscriptions']); // Pass on user ID so that we can later know whose subscriptions we're searching for $search_type[2] = $user_id; } // If it's a search for unanswered posts else { $result = $db->query('SELECT t.id FROM '.$db->prefix.'topics AS t LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_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 ORDER BY t.last_post DESC') or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error()); $num_hits = $db->num_rows($result); if (!$num_hits) message($lang_search['No unanswered']); } $search_ids = array(); while ($row = $db->fetch_row($result)) $search_ids[] = $row[0]; $db->free_result($result); } else message($lang_common['Bad request']); // Prune "old" search results $old_searches = array(); $result = $db->query('SELECT ident FROM '.$db->prefix.'online') or error('Unable to fetch online list', __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('Unable to delete search results', __FILE__, __LINE__, $db->error()); } // Fill an array with our results and search properties $temp = serialize(array( 'search_ids' => serialize($search_ids), 'num_hits' => $num_hits, 'sort_by' => $sort_by, 'sort_dir' => $sort_dir, 'show_as' => $show_as, 'search_type' => $search_type )); $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('Unable to insert search results', __FILE__, __LINE__, $db->error()); if ($search_type[0] != 'action') { $db->end_transaction(); $db->close(); // Redirect the user to the cached result page header('Location: search.php?search_id='.$search_id); exit; } } $forum_actions = array(); // If we're on the new posts search, display a "mark all as read" link if (!$pun_user['is_guest'] && $search_type[0] == 'action' && $search_type[1] == 'show_new') $forum_actions[] = ''.$lang_common['Mark all as read'].''; // Fetch results to display if (!empty($search_ids)) { 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; default: $sort_by_sql = ($show_as == 'topics') ? 't.last_post' : 'p.posted'; break; } // 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 : intval($_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); // throw away the first $start_from of $search_ids, only keep the top $per_page of $search_ids $search_ids = array_slice($search_ids, $start_from, $per_page); // Run the query and fetch the results if ($show_as == 'posts') $result = $db->query('SELECT p.id AS pid, p.poster AS pposter, p.posted AS pposted, p.poster_id, p.message, p.hide_smilies, t.id AS tid, t.poster, t.subject, t.first_post_id, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.forum_id, f.forum_name 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 WHERE p.id IN('.implode(',', $search_ids).') ORDER BY '.$sort_by_sql.' '.$sort_dir) or error('Unable to fetch search results', __FILE__, __LINE__, $db->error()); else $result = $db->query('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.sticky, t.forum_id, f.forum_name FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id WHERE t.id IN('.implode(',', $search_ids).') ORDER BY '.$sort_by_sql.' '.$sort_dir) or error('Unable to fetch search results', __FILE__, __LINE__, $db->error()); $search_set = array(); while ($row = $db->fetch_assoc($result)) $search_set[] = $row; $crumbs_text = array(); $crumbs_text['show_as'] = $lang_search['Search']; if ($search_type[0] == 'action') { if ($search_type[1] == 'show_user_topics') $crumbs_text['search_type'] = ''.sprintf($lang_search['Quick search show_user_topics'], pun_htmlspecialchars($search_set[0]['poster'])).''; else if ($search_type[1] == 'show_user_posts') $crumbs_text['search_type'] = ''.sprintf($lang_search['Quick search show_user_posts'], pun_htmlspecialchars($search_set[0]['pposter'])).''; else if ($search_type[1] == 'show_subscriptions') { // Fetch username of subscriber $subscriber_id = $search_type[2]; $result = $db->query('SELECT username FROM '.$db->prefix.'users WHERE id='.$subscriber_id) or error('Unable to fetch username of subscriber', __FILE__, __LINE__, $db->error()); if ($db->num_rows($result)) $subscriber_name = $db->result($result); else message($lang_common['Bad request']); $crumbs_text['search_type'] = ''.sprintf($lang_search['Quick search show_subscriptions'], pun_htmlspecialchars($subscriber_name)).''; } else $crumbs_text['search_type'] = ''.$lang_search['Quick search '.$search_type[1]].''; } else { $keywords = $author = ''; if ($search_type[0] == 'both') { list ($keywords, $author) = $search_type[1]; $crumbs_text['search_type'] = sprintf($lang_search['By both show as '.$show_as], pun_htmlspecialchars($keywords), pun_htmlspecialchars($author)); } else if ($search_type[0] == 'keywords') { $keywords = $search_type[1]; $crumbs_text['search_type'] = sprintf($lang_search['By keywords show as '.$show_as], pun_htmlspecialchars($keywords)); } else if ($search_type[0] == 'author') { $author = $search_type[1]; $crumbs_text['search_type'] = sprintf($lang_search['By user show as '.$show_as], pun_htmlspecialchars($author)); } $crumbs_text['search_type'] = ''.$crumbs_text['search_type'].''; } $page_title = array(pun_htmlspecialchars($pun_config['o_board_title']), $lang_search['Search results']); define('PUN_ACTIVE_PAGE', 'search'); require PUN_ROOT.'header.php'; ?>
|
'.format_time($cur_search['last_post']).' '.$lang_common['by'].' '.pun_htmlspecialchars($cur_search['last_poster']) ?> |