punbb/search.php
author Dan
Wed, 11 Jul 2007 21:01:48 -0400
changeset 0 f9ffdbd96607
child 2 a8a21e1c7afa
permissions -rw-r--r--
Initial population
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     1
<?php
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     2
/***********************************************************************
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     3
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     4
  Copyright (C) 2002-2005  Rickard Andersson (rickard@punbb.org)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     5
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     6
  This file is part of PunBB.
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     7
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     8
  PunBB is free software; you can redistribute it and/or modify it
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     9
  under the terms of the GNU General Public License as published
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    10
  by the Free Software Foundation; either version 2 of the License,
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    11
  or (at your option) any later version.
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    12
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    13
  PunBB is distributed in the hope that it will be useful, but
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    14
  WITHOUT ANY WARRANTY; without even the implied warranty of
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    15
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    16
  GNU General Public License for more details.
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    17
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    18
  You should have received a copy of the GNU General Public License
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    19
  along with this program; if not, write to the Free Software
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    20
  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    21
  MA  02111-1307  USA
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    22
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    23
************************************************************************/
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    24
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    25
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    26
// The contents of this file are very much inspired by the file search.php
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    27
// from the phpBB Group forum software phpBB2 (http://www.phpbb.com).
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    28
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    29
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    30
define('PUN_ROOT', './');
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    31
require PUN_ROOT.'include/common.php';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    32
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    33
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    34
// Load the search.php language file
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    35
require PUN_ROOT.'lang/'.$pun_user['language'].'/search.php';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    36
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    37
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    38
if ($pun_user['g_read_board'] == '0')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    39
	message($lang_common['No view']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    40
else if ($pun_user['g_search'] == '0')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    41
	message($lang_search['No search permission']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    42
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    43
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    44
// Detect two byte character sets
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    45
$multibyte = (isset($lang_common['lang_multibyte']) && $lang_common['lang_multibyte']) ? true : false;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    46
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    47
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    48
// Figure out what to do :-)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    49
if (isset($_GET['action']) || isset($_GET['search_id']))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    50
{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    51
	$action = (isset($_GET['action'])) ? $_GET['action'] : null;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    52
	$forum = (isset($_GET['forum'])) ? intval($_GET['forum']) : -1;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    53
	$sort_dir = (isset($_GET['sort_dir'])) ? (($_GET['sort_dir'] == 'DESC') ? 'DESC' : 'ASC') : 'DESC';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    54
	if (isset($search_id)) unset($search_id);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    55
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    56
	// If a search_id was supplied
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    57
	if (isset($_GET['search_id']))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    58
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    59
		$search_id = intval($_GET['search_id']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    60
		if ($search_id < 1)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    61
			message($lang_common['Bad request']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    62
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    63
	// If it's a regular search (keywords and/or author)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    64
	else if ($action == 'search')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    65
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    66
		$keywords = (isset($_GET['keywords'])) ? strtolower(trim($_GET['keywords'])) : null;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    67
		$author = (isset($_GET['author'])) ? strtolower(trim($_GET['author'])) : null;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    68
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    69
		if (preg_match('#^[\*%]+$#', $keywords) || strlen(str_replace(array('*', '%'), '', $keywords)) < 3)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    70
			$keywords = '';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    71
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    72
		if (preg_match('#^[\*%]+$#', $author) || strlen(str_replace(array('*', '%'), '', $author)) < 3)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    73
			$author = '';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    74
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    75
		if (!$keywords && !$author)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    76
			message($lang_search['No terms']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    77
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    78
		if ($author)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    79
			$author = str_replace('*', '%', $author);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    80
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    81
		$show_as = (isset($_GET['show_as'])) ? $_GET['show_as'] : 'posts';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    82
		$sort_by = (isset($_GET['sort_by'])) ? intval($_GET['sort_by']) : null;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    83
		$search_in = (!isset($_GET['search_in']) || $_GET['search_in'] == 'all') ? 0 : (($_GET['search_in'] == 'message') ? 1 : -1);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    84
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    85
	// If it's a user search (by id)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    86
	else if ($action == 'show_user')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    87
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    88
		$user_id = intval($_GET['user_id']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    89
		if ($user_id < 2)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    90
			message($lang_common['Bad request']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    91
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    92
	else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    93
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    94
		if ($action != 'show_new' && $action != 'show_24h' && $action != 'show_unanswered' && $action != 'show_subscriptions')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    95
			message($lang_common['Bad request']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    96
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    97
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    98
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    99
	// If a valid search_id was supplied we attempt to fetch the search results from the db
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   100
	if (isset($search_id))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   101
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   102
		$ident = ($pun_user['is_guest']) ? get_remote_address() : $pun_user['username'];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   103
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   104
		$result = $db->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());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   105
		if ($row = $db->fetch_assoc($result))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   106
		{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   107
			$temp = unserialize($row['search_data']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   108
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   109
			$search_results = $temp['search_results'];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   110
			$num_hits = $temp['num_hits'];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   111
			$sort_by = $temp['sort_by'];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   112
			$sort_dir = $temp['sort_dir'];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   113
			$show_as = $temp['show_as'];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   114
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   115
			unset($temp);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   116
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   117
		else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   118
			message($lang_search['No hits']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   119
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   120
	else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   121
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   122
		$keyword_results = $author_results = array();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   123
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   124
		// Search a specific forum?
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   125
		$forum_sql = ($forum != -1 || ($forum == -1 && $pun_config['o_search_all_forums'] == '0')) ? ' AND t.forum_id = '.$forum : '';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   126
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   127
		if (!empty($author) || !empty($keywords))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   128
		{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   129
			// If it's a search for keywords
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   130
			if ($keywords)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   131
			{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   132
				$stopwords = (array)@file(PUN_ROOT.'lang/'.$pun_user['language'].'/stopwords.txt');
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   133
				$stopwords = array_map('trim', $stopwords);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   134
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   135
				// Are we searching for multibyte charset text?
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   136
				if ($multibyte)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   137
				{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   138
					// Strip out excessive whitespace
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   139
					$keywords = trim(preg_replace('#\s+#', ' ', $keywords));
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   140
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   141
					$keywords_array = explode(' ', $keywords);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   142
				}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   143
				else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   144
				{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   145
					// Filter out non-alphabetical chars
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   146
					$noise_match = array('^', '$', '&', '(', ')', '<', '>', '`', '\'', '"', '|', ',', '@', '_', '?', '%', '~', '[', ']', '{', '}', ':', '\\', '/', '=', '#', '\'', ';', '!', '¤');
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   147
					$noise_replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', '',  '',   ' ', ' ', ' ', ' ', '',  ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '' ,  ' ', ' ', ' ', ' ',  ' ', ' ', ' ');
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   148
					$keywords = str_replace($noise_match, $noise_replace, $keywords);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   149
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   150
					// Strip out excessive whitespace
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   151
					$keywords = trim(preg_replace('#\s+#', ' ', $keywords));
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   152
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   153
					// Fill an array with all the words
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   154
					$keywords_array = explode(' ', $keywords);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   155
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   156
					if (empty($keywords_array))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   157
						message($lang_search['No hits']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   158
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   159
					while (list($i, $word) = @each($keywords_array))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   160
					{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   161
						$num_chars = pun_strlen($word);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   162
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   163
						if ($num_chars < 3 || $num_chars > 20 || in_array($word, $stopwords))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   164
							unset($keywords_array[$i]);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   165
					}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   166
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   167
					// Should we search in message body or topic subject specifically?
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   168
					$search_in_cond = ($search_in) ? (($search_in > 0) ? ' AND m.subject_match = 0' : ' AND m.subject_match = 1') : '';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   169
				}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   170
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   171
				$word_count = 0;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   172
				$match_type = 'and';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   173
				$result_list = array();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   174
				@reset($keywords_array);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   175
				while (list(, $cur_word) = @each($keywords_array))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   176
				{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   177
					switch ($cur_word)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   178
					{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   179
						case 'and':
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   180
						case 'or':
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   181
						case 'not':
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   182
							$match_type = $cur_word;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   183
							break;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   184
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   185
						default:
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   186
						{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   187
							// Are we searching for multibyte charset text?
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   188
							if ($multibyte)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   189
							{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   190
								$cur_word = $db->escape('%'.str_replace('*', '', $cur_word).'%');
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   191
								$cur_word_like = ($db_type == 'pgsql') ? 'ILIKE \''.$cur_word.'\'' : 'LIKE \''.$cur_word.'\'';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   192
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   193
								if ($search_in > 0)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   194
									$sql = 'SELECT id FROM '.$db->prefix.'posts WHERE message '.$cur_word_like;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   195
								else if ($search_in < 0)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   196
									$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';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   197
								else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   198
									$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';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   199
							}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   200
							else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   201
							{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   202
								$cur_word = str_replace('*', '%', $cur_word);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   203
								$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;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   204
							}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   205
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   206
							$result = $db->query($sql, true) or error('Unable to search for posts', __FILE__, __LINE__, $db->error());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   207
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   208
							$row = array();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   209
							while ($temp = $db->fetch_row($result))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   210
							{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   211
								$row[$temp[0]] = 1;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   212
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   213
								if (!$word_count)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   214
									$result_list[$temp[0]] = 1;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   215
								else if ($match_type == 'or')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   216
									$result_list[$temp[0]] = 1;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   217
								else if ($match_type == 'not')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   218
									$result_list[$temp[0]] = 0;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   219
							}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   220
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   221
							if ($match_type == 'and' && $word_count)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   222
							{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   223
								@reset($result_list);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   224
								while (list($post_id,) = @each($result_list))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   225
								{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   226
									if (!isset($row[$post_id]))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   227
										$result_list[$post_id] = 0;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   228
								}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   229
							}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   230
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   231
							++$word_count;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   232
							$db->free_result($result);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   233
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   234
							break;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   235
						}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   236
					}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   237
				}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   238
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   239
				@reset($result_list);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   240
				while (list($post_id, $matches) = @each($result_list))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   241
				{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   242
					if ($matches)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   243
						$keyword_results[] = $post_id;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   244
				}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   245
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   246
				unset($result_list);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   247
			}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   248
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   249
			// If it's a search for author name (and that author name isn't Guest)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   250
			if ($author && strcasecmp($author, 'Guest') && strcasecmp($author, $lang_common['Guest']))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   251
			{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   252
				switch ($db_type)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   253
				{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   254
					case 'pgsql':
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   255
						$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());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   256
						break;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   257
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   258
					default:
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   259
						$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());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   260
						break;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   261
				}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   262
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   263
				if ($db->num_rows($result))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   264
				{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   265
					$user_ids = '';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   266
					while ($row = $db->fetch_row($result))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   267
						$user_ids .= (($user_ids != '') ? ',' : '').$row[0];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   268
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   269
					$result = $db->query('SELECT id FROM '.$db->prefix.'posts WHERE poster_id IN('.$user_ids.')') or error('Unable to fetch matched posts list', __FILE__, __LINE__, $db->error());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   270
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   271
					$search_ids = array();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   272
					while ($row = $db->fetch_row($result))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   273
						$author_results[] = $row[0];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   274
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   275
					$db->free_result($result);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   276
				}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   277
			}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   278
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   279
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   280
			if ($author && $keywords)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   281
			{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   282
				// If we searched for both keywords and author name we want the intersection between the results
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   283
				$search_ids = array_intersect($keyword_results, $author_results);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   284
				unset($keyword_results, $author_results);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   285
			}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   286
			else if ($keywords)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   287
				$search_ids = $keyword_results;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   288
			else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   289
				$search_ids = $author_results;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   290
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   291
			$num_hits = count($search_ids);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   292
			if (!$num_hits)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   293
				message($lang_search['No hits']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   294
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   295
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   296
			if ($show_as == 'topics')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   297
			{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   298
				$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('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   299
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   300
				$search_ids = array();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   301
				while ($row = $db->fetch_row($result))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   302
					$search_ids[] = $row[0];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   303
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   304
				$db->free_result($result);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   305
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   306
				$num_hits = count($search_ids);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   307
			}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   308
			else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   309
			{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   310
				$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('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   311
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   312
				$search_ids = array();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   313
				while ($row = $db->fetch_row($result))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   314
					$search_ids[] = $row[0];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   315
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   316
				$db->free_result($result);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   317
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   318
				$num_hits = count($search_ids);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   319
			}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   320
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   321
		else if ($action == 'show_new' || $action == 'show_24h' || $action == 'show_user' || $action == 'show_subscriptions' || $action == 'show_unanswered')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   322
		{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   323
			// If it's a search for new posts
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   324
			if ($action == 'show_new')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   325
			{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   326
				if ($pun_user['is_guest'])
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   327
					message($lang_common['No permission']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   328
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   329
				$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'].' AND t.moved_to IS NULL') or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   330
				$num_hits = $db->num_rows($result);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   331
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   332
				if (!$num_hits)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   333
					message($lang_search['No new posts']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   334
			}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   335
			// If it's a search for todays posts
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   336
			else if ($action == 'show_24h')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   337
			{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   338
				$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).' AND t.moved_to IS NULL') or error('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   339
				$num_hits = $db->num_rows($result);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   340
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   341
				if (!$num_hits)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   342
					message($lang_search['No recent posts']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   343
			}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   344
			// If it's a search for posts by a specific user ID
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   345
			else if ($action == 'show_user')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   346
			{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   347
				$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('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   348
				$num_hits = $db->num_rows($result);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   349
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   350
				if (!$num_hits)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   351
					message($lang_search['No user posts']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   352
			}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   353
			// If it's a search for subscribed topics
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   354
			else if ($action == 'show_subscriptions')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   355
			{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   356
				if ($pun_user['is_guest'])
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   357
					message($lang_common['Bad request']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   358
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   359
				$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('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   360
				$num_hits = $db->num_rows($result);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   361
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   362
				if (!$num_hits)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   363
					message($lang_search['No subscriptions']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   364
			}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   365
			// If it's a search for unanswered posts
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   366
			else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   367
			{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   368
				$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('Unable to fetch topic list', __FILE__, __LINE__, $db->error());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   369
				$num_hits = $db->num_rows($result);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   370
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   371
				if (!$num_hits)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   372
					message($lang_search['No unanswered']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   373
			}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   374
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   375
			// We want to sort things after last post
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   376
			$sort_by = 4;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   377
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   378
			$search_ids = array();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   379
			while ($row = $db->fetch_row($result))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   380
				$search_ids[] = $row[0];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   381
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   382
			$db->free_result($result);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   383
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   384
			$show_as = 'topics';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   385
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   386
		else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   387
			message($lang_common['Bad request']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   388
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   389
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   390
		// Prune "old" search results
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   391
		$old_searches = array();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   392
		$result = $db->query('SELECT ident FROM '.$db->prefix.'online') or error('Unable to fetch online list', __FILE__, __LINE__, $db->error());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   393
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   394
		if ($db->num_rows($result))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   395
		{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   396
			while ($row = $db->fetch_row($result))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   397
				$old_searches[] = '\''.$db->escape($row[0]).'\'';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   398
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   399
			$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());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   400
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   401
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   402
		// Final search results
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   403
		$search_results = implode(',', $search_ids);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   404
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   405
		// Fill an array with our results and search properties
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   406
		$temp['search_results'] = $search_results;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   407
		$temp['num_hits'] = $num_hits;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   408
		$temp['sort_by'] = $sort_by;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   409
		$temp['sort_dir'] = $sort_dir;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   410
		$temp['show_as'] = $show_as;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   411
		$temp = serialize($temp);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   412
		$search_id = mt_rand(1, 2147483647);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   413
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   414
		$ident = ($pun_user['is_guest']) ? get_remote_address() : $pun_user['username'];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   415
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   416
		$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());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   417
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   418
		if ($action != 'show_new' && $action != 'show_24h')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   419
		{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   420
			$db->end_transaction();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   421
			$db->close();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   422
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   423
			// Redirect the user to the cached result page
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   424
			header('Location: search.php?search_id='.$search_id);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   425
			exit;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   426
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   427
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   428
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   429
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   430
	// Fetch results to display
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   431
	if ($search_results != '')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   432
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   433
		switch ($sort_by)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   434
		{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   435
			case 1:
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   436
				$sort_by_sql = ($show_as == 'topics') ? 't.poster' : 'p.poster';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   437
				break;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   438
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   439
			case 2:
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   440
				$sort_by_sql = 't.subject';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   441
				break;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   442
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   443
			case 3:
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   444
				$sort_by_sql = 't.forum_id';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   445
				break;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   446
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   447
			case 4:
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   448
				$sort_by_sql = 't.last_post';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   449
				break;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   450
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   451
			default:
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   452
				$sort_by_sql = ($show_as == 'topics') ? 't.posted' : 'p.posted';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   453
				break;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   454
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   455
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   456
		if ($show_as == 'posts')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   457
		{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   458
			$substr_sql = ($db_type != 'sqlite') ? 'SUBSTRING' : 'SUBSTR';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   459
			$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;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   460
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   461
		else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   462
			$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.'topics AS t WHERE t.id IN('.$search_results.') ORDER BY '.$sort_by_sql;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   463
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   464
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   465
		// Determine the topic or post offset (based on $_GET['p'])
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   466
		$per_page = ($show_as == 'posts') ? $pun_user['disp_posts'] : $pun_user['disp_topics'];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   467
		$num_pages = ceil($num_hits / $per_page);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   468
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   469
		$p = (!isset($_GET['p']) || $_GET['p'] <= 1 || $_GET['p'] > $num_pages) ? 1 : $_GET['p'];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   470
		$start_from = $per_page * ($p - 1);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   471
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   472
		// Generate paging links
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   473
		$paging_links = $lang_common['Pages'].': '.paginate($num_pages, $p, 'search.php?search_id='.$search_id);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   474
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   475
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   476
		$sql .= ' '.$sort_dir.' LIMIT '.$start_from.', '.$per_page;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   477
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   478
		$result = $db->query($sql) or error('Unable to fetch search results', __FILE__, __LINE__, $db->error());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   479
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   480
		$search_set = array();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   481
		while ($row = $db->fetch_assoc($result))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   482
			$search_set[] = $row;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   483
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   484
		$db->free_result($result);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   485
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   486
		$page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / '.$lang_search['Search results'];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   487
		require PUN_ROOT.'header.php';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   488
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   489
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   490
?>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   491
<div class="linkst">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   492
	<div class="inbox">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   493
		<p class="pagelink"><?php echo $paging_links ?></p>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   494
	</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   495
</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   496
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   497
<?php
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   498
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   499
		//Set background switching on for show as posts
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   500
		$bg_switch = true;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   501
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   502
		if ($show_as == 'topics')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   503
		{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   504
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   505
?>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   506
<div id="vf" class="blocktable">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   507
	<h2><span><?php echo $lang_search['Search results']; ?></span></h2>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   508
	<div class="box">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   509
		<div class="inbox">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   510
			<table cellspacing="0">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   511
			<thead>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   512
				<tr>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   513
					<th class="tcl" scope="col"><?php echo $lang_common['Topic']; ?></th>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   514
					<th class="tc2" scope="col"><?php echo $lang_common['Forum'] ?></th>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   515
					<th class="tc3" scope="col"><?php echo $lang_common['Replies'] ?></th>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   516
					<th class="tcr" scope="col"><?php echo $lang_common['Last post'] ?></th>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   517
				</tr>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   518
			</thead>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   519
			<tbody>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   520
<?php
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   521
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   522
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   523
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   524
		// Fetch the list of forums
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   525
		$result = $db->query('SELECT id, forum_name FROM '.$db->prefix.'forums') or error('Unable to fetch forum list', __FILE__, __LINE__, $db->error());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   526
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   527
		$forum_list = array();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   528
		while ($forum_list[] = $db->fetch_row($result))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   529
			;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   530
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   531
		// Finally, lets loop through the results and output them
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   532
		for ($i = 0; $i < count($search_set); ++$i)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   533
		{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   534
			@reset($forum_list);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   535
			while (list(, $temp) = @each($forum_list))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   536
			{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   537
				if ($temp[0] == $search_set[$i]['forum_id'])
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   538
					$forum = '<a href="viewforum.php?id='.$temp[0].'">'.pun_htmlspecialchars($temp[1]).'</a>';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   539
			}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   540
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   541
			if ($pun_config['o_censoring'] == '1')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   542
				$search_set[$i]['subject'] = censor_words($search_set[$i]['subject']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   543
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   544
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   545
			if ($show_as == 'posts')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   546
			{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   547
				$icon = '<div class="icon"><div class="nosize">'.$lang_common['Normal icon'].'</div></div>'."\n";
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   548
				$subject = '<a href="viewtopic.php?id='.$search_set[$i]['tid'].'">'.pun_htmlspecialchars($search_set[$i]['subject']).'</a>';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   549
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   550
				if (!$pun_user['is_guest'] && $search_set[$i]['last_post'] > $pun_user['last_visit'])
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   551
					$icon = '<div class="icon inew"><div class="nosize">'.$lang_common['New icon'].'</div></div>'."\n";
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   552
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   553
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   554
				if ($pun_config['o_censoring'] == '1')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   555
					$search_set[$i]['message'] = censor_words($search_set[$i]['message']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   556
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   557
				$message = str_replace("\n", '<br />', pun_htmlspecialchars($search_set[$i]['message']));
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   558
				$pposter = pun_htmlspecialchars($search_set[$i]['pposter']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   559
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   560
				if ($search_set[$i]['poster_id'] > 1)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   561
					$pposter = '<strong><a href="profile.php?id='.$search_set[$i]['poster_id'].'">'.$pposter.'</a></strong>';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   562
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   563
				if (pun_strlen($message) >= 1000)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   564
					$message .= ' &hellip;';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   565
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   566
				$vtpost1 = ($i == 0) ? ' vtp1' : '';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   567
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   568
				// Switch the background color for every message.
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   569
				$bg_switch = ($bg_switch) ? $bg_switch = false : $bg_switch = true;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   570
				$vtbg = ($bg_switch) ? ' rowodd' : ' roweven';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   571
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   572
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   573
?>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   574
<div class="blockpost searchposts<?php echo $vtbg ?>">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   575
	<h2><?php echo $forum ?>&nbsp;&raquo;&nbsp;<?php echo $subject ?>&nbsp;&raquo;&nbsp;<a href="viewtopic.php?pid=<?php echo $search_set[$i]['pid'].'#p'.$search_set[$i]['pid'] ?>"><?php echo format_time($search_set[$i]['pposted']) ?></a></h2>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   576
	<div class="box">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   577
		<div class="inbox">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   578
			<div class="postleft">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   579
				<dl>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   580
					<dt><?php echo $pposter ?></dt>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   581
					<dd>Replies: <?php echo $search_set[$i]['num_replies'] ?></dd>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   582
					<dd><?php echo $icon; ?></dd>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   583
					<dd><p class="clearb"><a href="viewtopic.php?pid=<?php echo $search_set[$i]['pid'].'#p'.$search_set[$i]['pid'] ?>"><?php echo $lang_search['Go to post'] ?></a></p></dd>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   584
				</dl>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   585
			</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   586
			<div class="postright">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   587
				<div class="postmsg">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   588
					<p><?php echo $message ?></p>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   589
				</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   590
			</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   591
			<div class="clearer"></div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   592
		</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   593
	</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   594
</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   595
<?php
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   596
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   597
			}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   598
			else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   599
			{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   600
				$icon = '<div class="icon"><div class="nosize">'.$lang_common['Normal icon'].'</div></div>'."\n";
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   601
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   602
				$icon_text = $lang_common['Normal icon'];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   603
				$item_status = '';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   604
				$icon_type = 'icon';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   605
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   606
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   607
				$subject = '<a href="viewtopic.php?id='.$search_set[$i]['tid'].'">'.pun_htmlspecialchars($search_set[$i]['subject']).'</a> <span class="byuser">'.$lang_common['by'].'&nbsp;'.pun_htmlspecialchars($search_set[$i]['poster']).'</span>';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   608
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   609
				if ($search_set[$i]['closed'] != '0')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   610
				{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   611
					$icon_text = $lang_common['Closed icon'];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   612
					$item_status = 'iclosed';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   613
				}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   614
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   615
				if (!$pun_user['is_guest'] && $search_set[$i]['last_post'] > $pun_user['last_visit'])
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   616
				{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   617
					$icon_text .= ' '.$lang_common['New icon'];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   618
					$item_status .= ' inew';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   619
					$icon_type = 'icon inew';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   620
					$subject = '<strong>'.$subject.'</strong>';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   621
					$subject_new_posts = '<span class="newtext">[&nbsp;<a href="viewtopic.php?id='.$search_set[$i]['tid'].'&amp;action=new" title="'.$lang_common['New posts info'].'">'.$lang_common['New posts'].'</a>&nbsp;]</span>';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   622
				}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   623
				else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   624
					$subject_new_posts = null;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   625
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   626
				$num_pages_topic = ceil(($search_set[$i]['num_replies'] + 1) / $pun_user['disp_posts']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   627
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   628
				if ($num_pages_topic > 1)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   629
					$subject_multipage = '[ '.paginate($num_pages_topic, -1, 'viewtopic.php?id='.$search_set[$i]['tid']).' ]';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   630
				else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   631
					$subject_multipage = null;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   632
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   633
				// Should we show the "New posts" and/or the multipage links?
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   634
				if (!empty($subject_new_posts) || !empty($subject_multipage))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   635
				{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   636
					$subject .= '&nbsp; '.(!empty($subject_new_posts) ? $subject_new_posts : '');
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   637
					$subject .= !empty($subject_multipage) ? ' '.$subject_multipage : '';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   638
				}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   639
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   640
?>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   641
				<tr<?php if ($item_status != '') echo ' class="'.trim($item_status).'"'; ?>>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   642
					<td class="tcl">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   643
						<div class="intd">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   644
							<div class="<?php echo $icon_type ?>"><div class="nosize"><?php echo trim($icon_text) ?></div></div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   645
							<div class="tclcon">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   646
								<?php echo $subject."\n" ?>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   647
							</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   648
						</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   649
					</td>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   650
					<td class="tc2"><?php echo $forum ?></td>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   651
					<td class="tc3"><?php echo $search_set[$i]['num_replies'] ?></td>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   652
					<td class="tcr"><?php echo '<a href="viewtopic.php?pid='.$search_set[$i]['last_post_id'].'#p'.$search_set[$i]['last_post_id'].'">'.format_time($search_set[$i]['last_post']).'</a> '.$lang_common['by'].'&nbsp;'.pun_htmlspecialchars($search_set[$i]['last_poster']) ?></td>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   653
				</tr>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   654
<?php
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   655
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   656
			}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   657
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   658
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   659
		if ($show_as == 'topics')
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   660
			echo "\t\t\t".'</tbody>'."\n\t\t\t".'</table>'."\n\t\t".'</div>'."\n\t".'</div>'."\n".'</div>'."\n\n";
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   661
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   662
?>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   663
<div class="<?php echo ($show_as == 'topics') ? 'linksb' : 'postlinksb'; ?>">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   664
	<div class="inbox">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   665
		<p class="pagelink"><?php echo $paging_links ?></p>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   666
	</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   667
</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   668
<?php
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   669
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   670
		$footer_style = 'search';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   671
		require PUN_ROOT.'footer.php';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   672
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   673
	else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   674
		message($lang_search['No hits']);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   675
}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   676
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   677
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   678
$page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / '.$lang_search['Search'];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   679
$focus_element = array('search', 'keywords');
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   680
require PUN_ROOT.'header.php';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   681
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   682
?>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   683
<div id="searchform" class="blockform">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   684
	<h2><span><?php echo $lang_search['Search'] ?></span></h2>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   685
	<div class="box">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   686
		<form id="search" method="get" action="search.php">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   687
			<div class="inform">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   688
				<fieldset>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   689
					<legend><?php echo $lang_search['Search criteria legend'] ?></legend>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   690
					<div class="infldset">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   691
						<input type="hidden" name="action" value="search" />
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   692
						<label class="conl"><?php echo $lang_search['Keyword search'] ?><br /><input type="text" name="keywords" size="40" maxlength="100" /><br /></label>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   693
						<label class="conl"><?php echo $lang_search['Author search'] ?><br /><input id="author" type="text" name="author" size="25" maxlength="25" /><br /></label>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   694
						<p class="clearb"><?php echo $lang_search['Search info'] ?></p>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   695
					</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   696
				</fieldset>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   697
			</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   698
			<div class="inform">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   699
				<fieldset>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   700
					<legend><?php echo $lang_search['Search in legend'] ?></legend>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   701
					<div class="infldset">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   702
						<label class="conl"><?php echo $lang_search['Forum search'] ?>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   703
						<br /><select id="forum" name="forum">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   704
<?php
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   705
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   706
if ($pun_config['o_search_all_forums'] == '1' || $pun_user['g_id'] < PUN_GUEST)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   707
	echo "\t\t\t\t\t\t\t".'<option value="-1">'.$lang_search['All forums'].'</option>'."\n";
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   708
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   709
$result = $db->query('SELECT c.id AS cid, c.cat_name, f.id AS fid, f.forum_name, f.redirect_url FROM '.$db->prefix.'categories AS c INNER JOIN '.$db->prefix.'forums AS f ON c.id=f.cat_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 f.redirect_url IS NULL ORDER BY c.disp_position, c.id, f.disp_position', true) or error('Unable to fetch category/forum list', __FILE__, __LINE__, $db->error());
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   710
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   711
$cur_category = 0;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   712
while ($cur_forum = $db->fetch_assoc($result))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   713
{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   714
	if ($cur_forum['cid'] != $cur_category)	// A new category since last iteration?
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   715
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   716
		if ($cur_category)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   717
			echo "\t\t\t\t\t\t\t".'</optgroup>'."\n";
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   718
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   719
		echo "\t\t\t\t\t\t\t".'<optgroup label="'.pun_htmlspecialchars($cur_forum['cat_name']).'">'."\n";
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   720
		$cur_category = $cur_forum['cid'];
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   721
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   722
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   723
	echo "\t\t\t\t\t\t\t\t".'<option value="'.$cur_forum['fid'].'">'.pun_htmlspecialchars($cur_forum['forum_name']).'</option>'."\n";
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   724
}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   725
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   726
?>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   727
							</optgroup>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   728
						</select>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   729
						<br /></label>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   730
						<label class="conl"><?php echo $lang_search['Search in'] ?>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   731
						<br /><select id="search_in" name="search_in">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   732
							<option value="all"><?php echo $lang_search['Message and subject'] ?></option>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   733
							<option value="message"><?php echo $lang_search['Message only'] ?></option>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   734
							<option value="topic"><?php echo $lang_search['Topic only'] ?></option>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   735
						</select>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   736
						<br /></label>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   737
						<p class="clearb"><?php echo $lang_search['Search in info'] ?></p>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   738
					</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   739
				</fieldset>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   740
			</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   741
			<div class="inform">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   742
				<fieldset>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   743
					<legend><?php echo $lang_search['Search results legend'] ?></legend>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   744
					<div class="infldset">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   745
						<label class="conl"><?php echo $lang_search['Sort by'] ?>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   746
						<br /><select name="sort_by">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   747
							<option value="0"><?php echo $lang_search['Sort by post time'] ?></option>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   748
							<option value="1"><?php echo $lang_search['Sort by author'] ?></option>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   749
							<option value="2"><?php echo $lang_search['Sort by subject'] ?></option>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   750
							<option value="3"><?php echo $lang_search['Sort by forum'] ?></option>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   751
						</select>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   752
						<br /></label>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   753
						<label class="conl"><?php echo $lang_search['Sort order'] ?>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   754
						<br /><select name="sort_dir">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   755
							<option value="DESC"><?php echo $lang_search['Descending'] ?></option>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   756
							<option value="ASC"><?php echo $lang_search['Ascending'] ?></option>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   757
						</select>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   758
						<br /></label>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   759
						<label class="conl"><?php echo $lang_search['Show as'] ?>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   760
						<br /><select name="show_as">
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   761
							<option value="topics"><?php echo $lang_search['Show as topics'] ?></option>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   762
							<option value="posts"><?php echo $lang_search['Show as posts'] ?></option>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   763
						</select>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   764
						<br /></label>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   765
						<p class="clearb"><?php echo $lang_search['Search results info'] ?></p>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   766
					</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   767
				</fieldset>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   768
			</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   769
			<p><input type="submit" name="search" value="<?php echo $lang_common['Submit'] ?>" accesskey="s" /></p>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   770
		</form>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   771
	</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   772
</div>
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   773
<?php
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   774
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   775
require PUN_ROOT.'footer.php';