diff -r 5e1f1e916419 -r 98bbc533541c punbb/search.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/punbb/search.php Sun Apr 06 00:28:50 2008 -0400 @@ -0,0 +1,1250 @@ + $_ ) +{ + $$key =& $GLOBALS[$key]; +} + +($hook = get_hook('se_start')) ? eval($hook) : null; + +// Load the search.php language file +require PUN_ROOT.'lang/'.$pun_user['language'].'/search.php'; + +if ($pun_user['g_read_board'] == '0') + message($lang_common['No view']); +else if ($pun_user['g_search'] == '0') + message($lang_search['No search permission']); + + +// Figure out what to do :-) +if (isset($_GET['action']) || isset($_GET['search_id'])) +{ + // Grab and validate all the submitted data + $action = (isset($_GET['action'])) ? $_GET['action'] : null; + $value = (isset($_GET['value'])) ? intval($_GET['value']) : 86400; + $forum = (isset($_GET['forum'])) ? intval($_GET['forum']) : -1; + $sort_dir = (isset($_GET['sort_dir'])) ? (($_GET['sort_dir'] == 'DESC') ? 'DESC' : 'ASC') : 'DESC'; + if (isset($search_id)) unset($search_id); + + // If a search_id was supplied + if (isset($_GET['search_id'])) + { + $search_id = intval($_GET['search_id']); + if ($db_type == 'mysql' || $db_type == 'mysqli') + message($lang_common['Bad request']); + if ($search_id < 1) + message($lang_common['Bad request']); + } + + // A list of valid actions (extensions can add their own actions to the array) + $valid_actions = array('search', 'show_new', 'show_recent', 'show_user_posts', 'show_user_topics', 'show_subscriptions', 'show_unanswered'); + + ($hook = get_hook('se_search_selected')) ? eval($hook) : null; + + // Validate action + if ($action && !in_array($action, $valid_actions)) + message($lang_common['Bad request']); + + // If it's a regular search (keywords and/or author) + if ($action == 'search') + { + $keywords = (isset($_GET['keywords'])) ? strtolower(trim($_GET['keywords'])) : null; + $author = (isset($_GET['author'])) ? strtolower(trim($_GET['author'])) : null; + + if (preg_match('#^[\*%]+$#', $keywords) || pun_strlen(str_replace(array('*', '%'), '', $keywords)) < 3) + $keywords = ''; + + if (preg_match('#^[\*%]+$#', $author) || pun_strlen(str_replace(array('*', '%'), '', $author)) < 2) + $author = ''; + + if (!$keywords && !$author) + message($lang_search['No terms']); + + if ($author) + $author = str_replace('*', '%', $author); + + $show_as = (isset($_GET['show_as'])) ? $_GET['show_as'] : 'posts'; + $sort_by = (isset($_GET['sort_by'])) ? intval($_GET['sort_by']) : null; + $search_in = (!isset($_GET['search_in']) || $_GET['search_in'] == 'all') ? 0 : (($_GET['search_in'] == 'message') ? 1 : -1); + + } + // If it's a user search (by id), make sure we have a user_id + else if ($action == 'show_user_posts' || $action == 'show_user_topics') + { + $user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : 0; + if ($user_id < 2) + message($lang_common['Bad request']); + } + + // First of all lets find out if we need to cache the results, we only need to do this for detailed queries, not quicksearches or for mysql(i) + if ($db_type != 'mysql' && $db_type != 'mysqli' && (isset($keywords) || isset($author))) + { + // We need to grab results, insert them into the cache and reload with a search id before showing them + $keyword_results = $author_results = array(); + + // 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); + + // 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 ($word !== 'or' && ($num_chars < 3 || $num_chars > 20 || in_array($word, $stopwords))) + unset($keywords_array[$i]); + } + + $word_count = 0; + $match_type = 'and'; + $result_list = array(); + @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: + { + $cur_word = str_replace('*', '%', $cur_word); + + $query = array( + 'SELECT' => 'm.post_id', + 'FROM' => 'search_words AS w', + 'JOINS' => array( + array( + 'INNER JOIN' => 'search_matches AS m', + 'ON' => 'm.word_id=w.id' + ) + ), + 'WHERE' => 'w.word LIKE \''.$cur_word.'\'' + ); + + // Search in what? + if ($search_in) + $query['WHERE'] .= ($search_in > 0 ? ' AND m.subject_match=0' : ' AND m.subject_match=1'); + + ($hook = get_hook('se_qr_get_keyword_hits')) ? eval($hook) : null; + $result = $pun_db->query_build($query, true) or error(__FILE__, __LINE__); + + $row = array(); + while ($temp = $pun_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; + $pun_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 && strtolower($author) != 'guest' && strtolower($author) != strtolower($lang_common['Guest'])) + { + $query = array( + 'SELECT' => 'u.id', + 'FROM' => 'users AS u', + 'WHERE' => 'u.username '.($db_type == 'pgsql' ? 'ILIKE' : 'LIKE').' \''.$pun_db->escape($author).'\'' + ); + + ($hook = get_hook('se_qr_get_author')) ? eval($hook) : null; + $result = $pun_db->query_build($query) or error(__FILE__, __LINE__); + + if ($pun_db->num_rows($result)) + { + $user_ids = ''; + while ($row = $pun_db->fetch_row($result)) + $user_ids .= (($user_ids != '') ? ',' : '').$row[0]; + + $query = array( + 'SELECT' => 'p.id', + 'FROM' => 'posts AS p', + 'WHERE' => 'p.poster_id IN('.$user_ids.')' + ); + + ($hook = get_hook('se_qr_get_author_hits')) ? eval($hook) : null; + $result = $pun_db->query_build($query) or error(__FILE__, __LINE__); + + $search_ids = array(); + while ($row = $pun_db->fetch_row($result)) + $author_results[] = $row[0]; + + $pun_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; + + if (count($search_ids) == 0) + message($lang_search['No hits']); + + + // Setup the default show_as topics search + $query = array( + 'SELECT' => 't.id', + 'FROM' => 'posts AS p', + 'JOINS' => array( + array( + 'INNER JOIN' => 'topics AS t', + 'ON' => 't.id=p.topic_id' + ), + array( + 'LEFT JOIN' => '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.id IN('.implode(',', $search_ids).')', + 'GROUP BY' => 't.id' + ); + + // Search a specific forum? + if ($forum != -1 || ($forum == -1 && $pun_config['o_search_all_forums'] == '0' && !$pun_user['is_admmod'])) + $query['WHERE'] .= ' AND t.forum_id = '.$forum; + + // Adjust the query if show_as posts + if ($show_as == 'posts') + { + $query['SELECT'] = 'p.id'; + unset($query['GROUP BY']); + } + + ($hook = get_hook('se_qr_get_hits')) ? eval($hook) : null; + $result = $pun_db->query_build($query, true) or error(__FILE__, __LINE__); + + $search_ids = array(); + while ($row = $pun_db->fetch_row($result)) + $search_ids[] = $row[0]; + + + // Prune "old" search results + $query = array( + 'SELECT' => 'o.ident', + 'FROM' => 'online AS o' + ); + + ($hook = get_hook('se_qr_get_online_idents')) ? eval($hook) : null; + $result = $pun_db->query_build($query) or error(__FILE__, __LINE__); + if ($pun_db->num_rows($result)) + { + $online_idents = array(); + while ($row = $pun_db->fetch_row($result)) + $online_idents[] = '\''.$pun_db->escape($row[0]).'\''; + + $query = array( + 'DELETE' => 'search_cache', + 'WHERE' => 'ident NOT IN('.implode(',', $online_idents).')' + ); + + ($hook = get_hook('se_qr_delete_old_cached_searches')) ? eval($hook) : null; + $pun_db->query_build($query) or error(__FILE__, __LINE__); + } + + // Final search results + $search_results = implode(',', $search_ids); + + // Fill an array with our results and search properties + $temp['search_results'] = $search_results; + $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']; + + $query = array( + 'INSERT' => 'id, ident, search_data', + 'INTO' => 'search_cache', + 'VALUES' => $search_id.', \''.$pun_db->escape($ident).'\', \''.$pun_db->escape($temp).'\'' + ); + + ($hook = get_hook('se_qr_cache_search')) ? eval($hook) : null; + $pun_db->query_build($query) or error(__FILE__, __LINE__); + + $pun_db->end_transaction(); + $pun_db->close(); + + // Redirect the user to the cached result page + header('Location: '.str_replace('&', '&', pun_link($pun_url['search_results'], $search_id))); + exit; + } + + // If we're still running we don't need to cache results but we still need to get them, either from the cache or from their respective sources. + + // We're doing a fulltext search! + else if (($db_type == 'mysql' || $db_type == 'mysqli') && (isset($keywords) || isset($author))) + { + // Are we limiting the results to a specific forum? + if ($forum != -1 || ($forum == -1 && $pun_config['o_search_all_forums'] == '0' && !$pun_user['is_admmod'])) + $forum_where = ' AND f.id = '.$forum; + else + $forum_where = ''; + + // Sort out how to order the results + switch ($sort_by) + { + case 1: + $sort_by_sql = ($show_as == 'topics') ? 'poster' : 'p.poster'; + break; + + case 2: + $sort_by_sql = 'subject'; + break; + + case 3: + $sort_by_sql = 'forum_id'; + break; + + case 4: + if ($show_as == 'posts') + $sort_by_sql = 'MATCH(p.message) AGAINST(\''.$pun_db->escape($keywords).'\')'; + else + $sort_by_sql = 'total_relevance'; + break; + + default: + $sort_by_sql = ($show_as == 'topics') ? 'posted' : 'p.posted'; + break; + } + + // Generate the query to give us our results + if ($show_as == 'posts') + $query = ' + SELECT + p.id AS pid, p.poster AS pposter, p.posted AS pposted, p.poster_id, SUBSTRING(p.message, 1, 1000) AS message, t.id AS tid, t.poster, t.subject, t.first_post_id, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.forum_id, f.forum_name + FROM '.$pun_db->prefix.'posts AS p LEFT JOIN '.$pun_db->prefix.'topics AS t ON t.id=p.topic_id LEFT JOIN '.$pun_db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$pun_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) + '.($author ? 'AND p.poster LIKE \''.$pun_db->escape($author).'\'' : '').' + '.($keywords ? 'AND MATCH(p.message) AGAINST(\''.$pun_db->escape($keywords).'\' IN BOOLEAN MODE)' : '').' + '.$forum_where.' + ORDER BY '.$sort_by_sql.' '.$sort_dir; + else + { + $query = ' + SELECT + *, SUM(relevance) AS total_relevance FROM + ( + SELECT + 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.closed, t.forum_id, t.posted, f.forum_name, MATCH(t.subject) AGAINST(\''.$pun_db->escape($keywords).'\') AS relevance + FROM '.$pun_db->prefix.'topics AS t LEFT JOIN '.$pun_db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$pun_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) + '.($keywords ? 'AND MATCH(t.subject) AGAINST(\''.$pun_db->escape($keywords).'\' IN BOOLEAN MODE)' : '').' + '.$forum_where.' + UNION + SELECT + 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.closed, t.forum_id, t.posted, f.forum_name, MATCH(p.message) AGAINST(\''.$pun_db->escape($keywords).'\') AS relevance + FROM '.$pun_db->prefix.'posts AS p INNER JOIN '.$pun_db->prefix.'topics AS t ON p.topic_id = t.id LEFT JOIN '.$pun_db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$pun_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) + '.($author ? 'AND p.poster LIKE \''.$pun_db->escape($author).'\'' : '').' + '.($keywords ? 'AND MATCH(p.message) AGAINST(\''.$pun_db->escape($keywords).'\' IN BOOLEAN MODE)' : '').' + '.$forum_where.' + ) AS tmp + GROUP BY tid + ORDER BY '.$sort_by_sql.' '.$sort_dir; + } + + $url_type = $pun_url['search_resultft']; + + $search_id = array($keywords, $forum, $author, ($search_in == 0 ) ? 'all' : (($search_in == 1) ? 'message' : 'subject'), $sort_by, $sort_dir, $show_as); + } + // We aren't doing a fulltext but we are getting results, if a valid search_id was supplied we attempt to fetch the search results from the cache + else if (isset($search_id)) + { + $ident = ($pun_user['is_guest']) ? get_remote_address() : $pun_user['username']; + + $query = array( + 'SELECT' => 'sc.search_data', + 'FROM' => 'search_cache AS sc', + 'WHERE' => 'sc.id='.$search_id.' AND sc.ident=\''.$pun_db->escape($ident).'\'' + ); + + ($hook = get_hook('se_qr_get_cached_search_data')) ? eval($hook) : null; + $result = $pun_db->query_build($query) or error(__FILE__, __LINE__); + if ($row = $pun_db->fetch_assoc($result)) + { + $temp = unserialize($row['search_data']); + + $search_results = $temp['search_results']; + $sort_by = $temp['sort_by']; + $sort_dir = $temp['sort_dir']; + $show_as = $temp['show_as']; + + unset($temp); + } + else + message($lang_search['No hits']); + + 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.posted' : 'p.posted'; + break; + } + + if ($show_as == 'posts') + { + $query = array( + 'SELECT' => 'p.id AS pid, p.poster AS pposter, p.posted AS pposted, p.poster_id, '.(($db_type != 'sqlite') ? 'SUBSTRING' : 'SUBSTR').'(p.message, 1, 1000) AS message, t.id AS tid, t.poster, t.subject, t.first_post_id, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.forum_id, f.forum_name', + 'FROM' => 'posts AS p', + 'JOINS' => array( + array( + 'INNER JOIN' => 'topics AS t', + 'ON' => 't.id=p.topic_id' + ), + array( + 'INNER JOIN' => 'forums AS f', + 'ON' => 'f.id=t.forum_id' + ) + ), + 'WHERE' => 'p.id IN('.$search_results.')', + 'ORDER BY' => $sort_by_sql + ); + + ($hook = get_hook('se_qr_get_cached_hits_as_posts')) ? eval($hook) : null; + } + else + { + $query = array( + 'SELECT' => 't.id AS tid, t.poster, t.subject, t.first_post_id, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.closed, t.forum_id, f.forum_name', + 'FROM' => 'topics AS t', + 'JOINS' => array( + array( + 'INNER JOIN' => 'forums AS f', + 'ON' => 'f.id=t.forum_id' + ) + ), + 'WHERE' => 't.id IN('.$search_results.')', + 'ORDER BY' => 'p.id, '.$sort_by_sql + ); + + ($hook = get_hook('se_qr_get_cached_hits_as_topics')) ? eval($hook) : null; + } + + $url_type = $pun_url['search_results']; + } + else if (in_array($action, $valid_actions)) + { + $search_id = ''; + $show_as = 'topics'; + switch ($action) + { + case 'show_new': + if ($pun_user['is_guest']) + message($lang_common['No permission']); + + $query = array( + '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, f.forum_name', + 'FROM' => 'topics AS t', + 'JOINS' => array( + array( + 'INNER JOIN' => 'forums AS f', + 'ON' => 'f.id=t.forum_id' + ), + array( + 'LEFT JOIN' => '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'].' AND t.moved_to IS NULL', + 'ORDER BY' => 't.last_post DESC' + ); + + ($hook = get_hook('se_qr_get_new')) ? eval($hook) : null; + + $url_type = $pun_url['search_new']; + break; + + case 'show_recent': + $query = array( + '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, f.forum_name', + 'FROM' => 'topics AS t', + 'JOINS' => array( + array( + 'INNER JOIN' => 'posts AS p', + 'ON' => 'p.topic_id=t.id' + ), + array( + 'INNER JOIN' => 'forums AS f', + 'ON' => 'f.id=t.forum_id' + ), + array( + 'LEFT JOIN' => '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.posted>'.(time() - $value).' AND t.moved_to IS NULL', + 'GROUP BY' => 't.id', + 'ORDER BY' => 't.last_post DESC' + ); + + ($hook = get_hook('se_qr_get_recent')) ? eval($hook) : null; + + $url_type = $pun_url['search_24h']; + break; + + case 'show_user_posts': + $query = array( + 'SELECT' => 'p.id AS pid, p.poster AS pposter, p.posted AS pposted, p.poster_id, '.(($db_type != 'sqlite') ? 'SUBSTRING' : 'SUBSTR').'(p.message, 1, 1000) AS message, t.id AS tid, t.poster, t.subject, t.first_post_id, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.forum_id, f.forum_name', + 'FROM' => 'posts AS p', + 'JOINS' => array( + array( + 'INNER JOIN' => 'topics AS t', + 'ON' => 't.id=p.topic_id' + ), + array( + 'INNER JOIN' => 'forums AS f', + 'ON' => 'f.id=t.forum_id' + ), + array( + 'LEFT JOIN' => '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, + 'ORDER BY' => 'pposted DESC' + ); + + ($hook = get_hook('se_qr_get_user_posts')) ? eval($hook) : null; + + $url_type = $pun_url['search_user_posts']; + $search_id = $user_id; + $show_as = 'posts'; + break; + + case 'show_user_topics': + $query = array( + '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, f.forum_name', + 'FROM' => 'topics AS t', + 'JOINS' => array( + array( + 'INNER JOIN' => 'posts AS p', + 'ON' => 't.first_post_id=p.id' + ), + array( + 'INNER JOIN' => 'forums AS f', + 'ON' => 'f.id=t.forum_id' + ), + array( + 'LEFT JOIN' => '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, + 'ORDER BY' => 't.last_post DESC' + ); + + ($hook = get_hook('se_qr_get_user_topics')) ? eval($hook) : null; + + $url_type = $pun_url['search_user_topics']; + $search_id = $user_id; + break; + + case 'show_subscriptions': + if ($pun_user['is_guest']) + message($lang_common['Bad request']); + + $query = array( + '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, f.forum_name', + 'FROM' => 'topics AS t', + 'JOINS' => array( + array( + 'INNER JOIN' => 'subscriptions AS s', + 'ON' => '(t.id=s.topic_id AND s.user_id='.$pun_user['id'].')' + ), + array( + 'INNER JOIN' => 'forums AS f', + 'ON' => 'f.id=t.forum_id' + ), + array( + 'LEFT JOIN' => '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)', + 'ORDER BY' => 't.last_post DESC' + ); + + ($hook = get_hook('se_qr_get_subscriptions')) ? eval($hook) : null; + + $url_type = $pun_url['search_subscriptions']; + break; + + case 'show_unanswered': + $query = array( + '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, f.forum_name', + 'FROM' => 'topics AS t', + 'JOINS' => array( + array( + 'INNER JOIN' => 'posts AS p', + 'ON' => 't.id=p.topic_id' + ), + array( + 'INNER JOIN' => 'forums AS f', + 'ON' => 'f.id=t.forum_id' + ), + array( + 'LEFT JOIN' => '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', + 'GROUP BY' => 't.id', + 'ORDER BY' => 't.last_post DESC' + ); + + ($hook = get_hook('se_qr_get_unanswered')) ? eval($hook) : null; + + $url_type = $pun_url['search_unanswered']; + break; + + default: + // A good place for an extension to add a new search type (action must be added to $valid_actions first) + ($hook = get_hook('se_new_action')) ? eval($hook) : null; + break; + } + } + else + message($lang_common['Bad request']); + + // We now have a query that will give us our results in $query, lets get the data! + if (is_array($query)) + $result = $pun_db->query_build($query) or error(__FILE__, __LINE__); + else + $result = $pun_db->query($query) or error(__FILE__, __LINE__); + + // Make sure we actually have some results + $num_hits = $pun_db->num_rows($result); + if ($num_hits == 0) + { + $pun_page['search_again'] = ''.$lang_search['Perform new search'].''; + + switch ($action) + { + case 'show_new': + message($lang_search['No new posts'], $pun_page['search_again']); + + case 'show_recent': + message($lang_search['No recent posts'], $pun_page['search_again']); + + case 'show_user_posts': + message($lang_search['No user posts'], $pun_page['search_again']); + + case 'show_user_topics': + message($lang_search['No user topics'], $pun_page['search_again']); + + case 'show_subscriptions': + message($lang_search['No subscriptions'], $pun_page['search_again']); + + case 'show_unanswered': + message($lang_search['No unanswered'], $pun_page['search_again']); + + default: + ($hook = get_hook('se_new_action_no_hits')) ? eval($hook) : null; + message($lang_search['No hits'], $pun_page['search_again']); + } + } + + // Get topic/forum tracking data + if (!$pun_user['is_guest']) + $tracked_topics = get_tracked_topics(); + + // Determine the topic or post offset (based on $_GET['p']) + $pun_page['per_page'] = ($show_as == 'posts') ? $pun_user['disp_posts'] : $pun_user['disp_topics']; + $pun_page['num_pages'] = ceil($num_hits / $pun_page['per_page']); + + $pun_page['page'] = (!isset($_GET['p']) || $_GET['p'] <= 1 || $_GET['p'] > $pun_page['num_pages']) ? 1 : $_GET['p']; + $pun_page['start_from'] = $pun_page['per_page'] * ($pun_page['page'] - 1); + $pun_page['finish_at'] = min(($pun_page['start_from'] + $pun_page['per_page']), $num_hits); + + // Generate paging links + $pun_page['page_post'] = '

'.$lang_common['Pages'].' '.pun_paginate($pun_page['num_pages'], $pun_page['page'], $url_type, $search_id).'

'; + + + // Fill $search_set with out search hits + $search_set = array(); + $row_num = 0; + while ($row = $pun_db->fetch_assoc($result)) + { + if ($pun_page['start_from'] <= $row_num && $pun_page['finish_at'] > $row_num) + $search_set[] = $row; + ++$row_num; + } + + $pun_db->free_result($result); + + + // Navigation links for header and page numbering for title/meta description + if ($pun_page['page'] < $pun_page['num_pages']) + { + $pun_page['nav'][] = ''; + $pun_page['nav'][] = ''; + } + if ($pun_page['page'] > 1) + { + $pun_page['nav'][] = ''; + $pun_page['nav'][] = ''; + } + + // Setup breadcrumbs and results header and footer + $pun_page['main_foot_options'][] = ''.$lang_search['Perform new search'].''; + $pun_page['crumbs'][] = array($pun_config['o_board_title'], pun_link($pun_url['index'])); + + switch ($action) + { + case 'show_new': + $pun_page['crumbs'][] = $lang_common['New posts']; + $pun_page['main_info'] = (($pun_page['num_pages'] == 1) ? sprintf($lang_common['Page info'], $lang_search['Topics with new'], $num_hits) : ''.sprintf($lang_common['Page number'], $pun_page['page'], $pun_page['num_pages']).' '.sprintf($lang_common['Paged info'], $lang_search['Topics with new'], $pun_page['start_from'] + 1, $pun_page['finish_at'], $num_hits)); + $pun_page['main_foot_options'][] = ''.$lang_common['Mark all as read'].''; + break; + + case 'show_recent': + $pun_page['crumbs'][] = $lang_common['Recent posts']; + $pun_page['main_info'] = (($pun_page['num_pages'] == 1) ? sprintf($lang_common['Page info'], $lang_search['Topics with recent'], $num_hits) : ''.sprintf($lang_common['Page number'], $pun_page['page'], $pun_page['num_pages']).' '.sprintf($lang_common['Paged info'], $lang_search['Topics with recent'], $pun_page['start_from'] + 1, $pun_page['finish_at'], $num_hits)); + break; + + case 'show_unanswered': + $pun_page['crumbs'][] = $lang_common['Unanswered topics']; + $pun_page['main_info'] = (($pun_page['num_pages'] == 1) ? sprintf($lang_common['Page info'], $lang_common['Unanswered topics'], $num_hits) : ''.sprintf($lang_common['Page number'], $pun_page['page'], $pun_page['num_pages']).' '.sprintf($lang_common['Paged info'], $lang_common['Unanswered topics'], $pun_page['start_from'] + 1, $pun_page['finish_at'], $num_hits)); + break; + + case 'show_user_posts': + $pun_page['crumbs'][] = sprintf($lang_search['Posts by'], $search_set[0]['pposter']); + $pun_page['main_info'] = (($pun_page['num_pages'] == 1) ? sprintf($lang_common['Page info'], sprintf($lang_search['Posts by'], $search_set[0]['pposter']), $num_hits) : ''.sprintf($lang_common['Page number'], $pun_page['page'], $pun_page['num_pages']).' '.sprintf($lang_common['Paged info'], sprintf($lang_search['Posts by'], $search_set[0]['pposter']), $pun_page['start_from'] + 1, $pun_page['finish_at'], $num_hits)); + $pun_page['main_foot_options'][] = ''.sprintf($lang_search['Topics by'], $search_set[0]['pposter']).''; + break; + + case 'show_user_topics': + $pun_page['crumbs'][] = sprintf($lang_search['Topics by'], $search_set[0]['poster']); + $pun_page['main_info'] = (($pun_page['num_pages'] == 1) ? sprintf($lang_common['Page info'], sprintf($lang_search['Topics by'], $search_set[0]['poster']), $num_hits) : ''.sprintf($lang_common['Page number'], $pun_page['page'], $pun_page['num_pages']).' '.sprintf($lang_common['Paged info'], sprintf($lang_search['Topics by'], $search_set[0]['poster']), $pun_page['start_from'] + 1, $pun_page['finish_at'], $num_hits)); + $pun_page['main_foot_options'][] = ''.sprintf($lang_search['Posts by'], $search_set[0]['poster']).''; + break; + + case 'show_subscriptions': + $pun_page['crumbs'][] = $lang_common['Your subscriptions']; + $pun_page['main_info'] = (($pun_page['num_pages'] == 1) ? sprintf($lang_common['Page info'], $lang_common['Your subscriptions'], $num_hits) : ''.sprintf($lang_common['Page number'], $pun_page['page'], $pun_page['num_pages']).' '.sprintf($lang_common['Paged info'], $lang_common['Your subscriptions'], $pun_page['start_from'] + 1, $pun_page['finish_at'], $num_hits)); + break; + + default: + $pun_page['crumbs'][] = $lang_search['Search results']; + $pun_page['main_info'] = (($pun_page['num_pages'] == 1) ? sprintf($lang_common['Page info'], (($show_as=='topics') ? $lang_common['Topics'] : $lang_common['Posts']), $num_hits) : ''.sprintf($lang_common['Page number'], $pun_page['page'], $pun_page['num_pages']).' '.sprintf($lang_common['Paged info'], (($show_as=='topics') ? $lang_common['Topics'] : $lang_common['Posts']), $pun_page['start_from'] + 1, $pun_page['finish_at'], $num_hits)); + break; + } + + ($hook = get_hook('se_results_pre_header_load')) ? eval($hook) : null; + + define('PUN_PAGE', $show_as == 'topics' ? 'searchtopics' : 'searchposts'); + require PUN_ROOT.'header.php'; + + if ($show_as == 'topics') + { + // Load the forum.php language file + require PUN_ROOT.'lang/'.$pun_user['language'].'/forum.php'; + +?> +
+ +

+ +
+ +
+ +
+

+
+ +
+ + + + + + + + + + + +
+ +

+ +
+ +
+ +
+

+
+ +
+ ''.($pun_page['start_from'] + $pun_page['item_count']).'', + 'user' => ''.(($search_set[$i]['pid'] == $search_set[$i]['first_post_id']) ? sprintf($lang_topic['Topic by'], htmlspecialchars($search_set[$i]['pposter'])) : sprintf($lang_topic['Reply by'], htmlspecialchars($search_set[$i]['pposter']))).'', + 'date' => ''.format_time($search_set[$i]['pposted']).'' + ); + + $pun_page['item_head'] = ''; + + // Generate author identification + $pun_page['user_ident'] = (($search_set[$i]['poster_id'] > 1) ? ''.htmlspecialchars($search_set[$i]['pposter']).'' : ''.htmlspecialchars($search_set[$i]['pposter']).''); + + // Generate the post options links + $pun_page['post_options'] = array(); + $pun_page['post_options'][] = ''.$lang_search['Go to forum'].': '.htmlspecialchars($search_set[$i]['forum_name']).''; + + if ($search_set[$i]['pid'] != $search_set[$i]['first_post_id']) + $pun_page['post_options'][] = ''; + + $pun_page['post_options'][] = ''; + + // Generate the post title + $pun_page['item_subject'] = array(); + if ($search_set[$i]['pid'] == $search_set[$i]['first_post_id']) + $pun_page['item_subject'][] = ''.$lang_common['Topic'].': '.htmlspecialchars($search_set[$i]['subject']).''; + else + $pun_page['item_subject'][] = ''.$lang_common['Re'].' '.htmlspecialchars($search_set[$i]['subject']).''; + + $pun_page['item_subject'][] = sprintf($lang_search['Topic info'], htmlspecialchars($search_set[$i]['poster']), htmlspecialchars($search_set[$i]['forum_name']), $search_set[$i]['num_replies']); + + // Generate the post message + if ($pun_config['o_censoring'] == '1') + $search_set[$i]['message'] = censor_words($search_set[$i]['message']); + + $pun_page['message'] = str_replace("\n", '
', htmlspecialchars($search_set[$i]['message'])); + + if (pun_strlen($pun_page['message']) >= 1000) + $pun_page['message'] .= ' …'; + + // Give the post some class + $pun_page['item_status'] = array( + 'post', + (($pun_page['item_count'] % 2 == 0) ? 'odd' : 'even' ) + ); + + if ($pun_page['item_count'] == 1) + $pun_page['item_status'][] = 'firstpost'; + + if (($pun_page['start_from'] + $pun_page['item_count']) == $pun_page['finish_at']) + $pun_page['item_status'][] = 'lastpost'; + + if ($search_set[$i]['pid'] == $search_set[$i]['first_post_id']) + $pun_page['item_status'][] = 'topicpost'; + + ($hook = get_hook('se_results_posts_row_pre_display')) ? eval($hook) : null; + +?> +
+
+
+

+
+
+
+

+
+
+

+
+

+
+
+
+
+
+
+
+
+'.htmlspecialchars($search_set[$i]['subject']).''; + + $pun_page['item_pages'] = ceil(($search_set[$i]['num_replies'] + 1) / $pun_user['disp_posts']); + + if ($pun_page['item_pages'] > 1) + $pun_page['item_nav'][] = pun_paginate($pun_page['item_pages'], -1, $pun_url['topic'], $search_set[$i]['tid']); + + // Does this topic contains posts we haven't read? If so, tag it accordingly. + if (!$pun_user['is_guest'] && $search_set[$i]['last_post'] > $pun_user['last_visit'] && (!isset($tracked_topics['topics'][$search_set[$i]['tid']]) || $tracked_topics['topics'][$search_set[$i]['tid']] < $search_set[$i]['last_post']) && (!isset($tracked_topics['forums'][$search_set[$i]['forum_id']]) || $tracked_topics['forums'][$search_set[$i]['forum_id']] < $search_set[$i]['last_post'])) + { + $pun_page['item_nav'][] = ''.$lang_common['New posts'].''; + $pun_page['item_status'][] = 'new'; + } + + if (!empty($pun_page['item_nav'])) + $pun_page['item_subject'][] = '[ '.implode('  ', $pun_page['item_nav']).' ]'; + + $pun_page['item_subject'][] = ''.sprintf($lang_common['By user'], htmlspecialchars($search_set[$i]['poster'])).''; + $pun_page['item_last_post'][] = ''.format_time($search_set[$i]['last_post']).''; + $pun_page['item_last_post'][] = ''.sprintf($lang_common['By user'], htmlspecialchars($search_set[$i]['last_poster'])).''; + $pun_page['item_indicator'] = ''.$pun_page['item_alt_message'].''.$pun_page['item_indicator'].''; + + ($hook = get_hook('se_results_topics_row_pre_display')) ? eval($hook) : null; + +?> +
+ + + + + + + +
+ +
+ +
+

+

+
+ +
+ +
+ +
+ +
+

+
+'.$lang_search['Search info'].''); +if ($db_type == 'mysql' || $db_type == 'mysqli') +{ + $pun_page['frm-info'][] = '
  • '.$lang_search['Refine info fulltext'].'
  • '; + $pun_page['frm-info'][] = '
  • '.$lang_search['Wildcard info fulltext'].'
  • '; +} +else +{ + $pun_page['frm-info'][] = '
  • '.$lang_search['Refine info'].'
  • '; + $pun_page['frm-info'][] = '
  • '.$lang_search['Wildcard info'].'
  • '; +} + +// Setup predefined search (pds) links +$pun_page['pd_searches'] = array( + ''.$lang_common['Recent posts'].'', + ''.$lang_common['Unanswered topics'].'' +); + +if (!$pun_user['is_guest']) +{ + array_push( + $pun_page['pd_searches'], + ''.$lang_common['New posts'].'', + ''.$lang_common['Your posts'].'', + ''.$lang_common['Your topics'].'' + ); + + if ($pun_config['o_subscriptions'] == '1') + $pun_page['pd_searches'][] = ''.$lang_common['Your subscriptions'].''; +} + +// Setup breadcrumbs +$pun_page['crumbs'] = array( + array($pun_config['o_board_title'], pun_link($pun_url['index'])), + $lang_common['Search'] +); + +($hook = get_hook('se_pre_header_load')) ? eval($hook) : null; + +define('PUN_PAGE', 'search'); +require PUN_ROOT.'header.php'; + +?> +
    + +

    + +
    +

    +
    +
    +
    +

    +

    +

    +
      + +
    +
    +
    + + +
    + + +
    + +
    + +
    + +
    + +
    + +
    + +
    + +
    + + +
    + +
    + +
    + +
    +
    + +
    + +
    +
    + +
    + +
    + +
    +
    +
    + +
    +