punbb/search.php
changeset 2 a8a21e1c7afa
parent 0 f9ffdbd96607
equal deleted inserted replaced
1:8f6143115bf5 2:a8a21e1c7afa
    25 
    25 
    26 // The contents of this file are very much inspired by the file search.php
    26 // The contents of this file are very much inspired by the file search.php
    27 // from the phpBB Group forum software phpBB2 (http://www.phpbb.com).
    27 // from the phpBB Group forum software phpBB2 (http://www.phpbb.com).
    28 
    28 
    29 
    29 
    30 define('PUN_ROOT', './');
    30 //define('PUN_ROOT', './');
    31 require PUN_ROOT.'include/common.php';
    31 //require PUN_ROOT.'include/common.php';
       
    32 
       
    33 global $pun_db, $pun_user, $pun_config, $lang_common;
       
    34 
    32 
    35 
    33 
    36 
    34 // Load the search.php language file
    37 // Load the search.php language file
    35 require PUN_ROOT.'lang/'.$pun_user['language'].'/search.php';
    38 require PUN_ROOT.'lang/'.$pun_user['language'].'/search.php';
    36 
    39 
    99 	// If a valid search_id was supplied we attempt to fetch the search results from the db
   102 	// If a valid search_id was supplied we attempt to fetch the search results from the db
   100 	if (isset($search_id))
   103 	if (isset($search_id))
   101 	{
   104 	{
   102 		$ident = ($pun_user['is_guest']) ? get_remote_address() : $pun_user['username'];
   105 		$ident = ($pun_user['is_guest']) ? get_remote_address() : $pun_user['username'];
   103 
   106 
   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());
   107 		$result = $pun_db->query('SELECT search_data FROM '.$pun_db->prefix.'search_cache WHERE id='.$search_id.' AND ident=\''.$pun_db->escape($ident).'\'') or error('Unable to fetch search results', __FILE__, __LINE__, $pun_db->error());
   105 		if ($row = $db->fetch_assoc($result))
   108 		if ($row = $pun_db->fetch_assoc($result))
   106 		{
   109 		{
   107 			$temp = unserialize($row['search_data']);
   110 			$temp = unserialize($row['search_data']);
   108 
   111 
   109 			$search_results = $temp['search_results'];
   112 			$search_results = $temp['search_results'];
   110 			$num_hits = $temp['num_hits'];
   113 			$num_hits = $temp['num_hits'];
   141 					$keywords_array = explode(' ', $keywords);
   144 					$keywords_array = explode(' ', $keywords);
   142 				}
   145 				}
   143 				else
   146 				else
   144 				{
   147 				{
   145 					// Filter out non-alphabetical chars
   148 					// Filter out non-alphabetical chars
   146 					$noise_match = array('^', '$', '&', '(', ')', '<', '>', '`', '\'', '"', '|', ',', '@', '_', '?', '%', '~', '[', ']', '{', '}', ':', '\\', '/', '=', '#', '\'', ';', '!', '¤');
   149 					$noise_match = array('^', '$', '&', '(', ')', '<', '>', '`', '\'', '"', '|', ',', '@', '_', '?', '%', '~', '[', ']', '{', '}', ':', '\\', '/', '=', '#', '\'', ';', '!', '�');
   147 					$noise_replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', '',  '',   ' ', ' ', ' ', ' ', '',  ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '' ,  ' ', ' ', ' ', ' ',  ' ', ' ', ' ');
   150 					$noise_replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', '',  '',   ' ', ' ', ' ', ' ', '',  ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '' ,  ' ', ' ', ' ', ' ',  ' ', ' ', ' ');
   148 					$keywords = str_replace($noise_match, $noise_replace, $keywords);
   151 					$keywords = str_replace($noise_match, $noise_replace, $keywords);
   149 
   152 
   150 					// Strip out excessive whitespace
   153 					// Strip out excessive whitespace
   151 					$keywords = trim(preg_replace('#\s+#', ' ', $keywords));
   154 					$keywords = trim(preg_replace('#\s+#', ' ', $keywords));
   185 						default:
   188 						default:
   186 						{
   189 						{
   187 							// Are we searching for multibyte charset text?
   190 							// Are we searching for multibyte charset text?
   188 							if ($multibyte)
   191 							if ($multibyte)
   189 							{
   192 							{
   190 								$cur_word = $db->escape('%'.str_replace('*', '', $cur_word).'%');
   193 								$cur_word = $pun_db->escape('%'.str_replace('*', '', $cur_word).'%');
   191 								$cur_word_like = ($db_type == 'pgsql') ? 'ILIKE \''.$cur_word.'\'' : 'LIKE \''.$cur_word.'\'';
   194 								$cur_word_like = ($db_type == 'pgsql') ? 'ILIKE \''.$cur_word.'\'' : 'LIKE \''.$cur_word.'\'';
   192 
   195 
   193 								if ($search_in > 0)
   196 								if ($search_in > 0)
   194 									$sql = 'SELECT id FROM '.$db->prefix.'posts WHERE message '.$cur_word_like;
   197 									$sql = 'SELECT id FROM '.$pun_db->prefix.'posts WHERE message '.$cur_word_like;
   195 								else if ($search_in < 0)
   198 								else if ($search_in < 0)
   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';
   199 									$sql = 'SELECT p.id FROM '.$pun_db->prefix.'posts AS p INNER JOIN '.$pun_db->prefix.'topics AS t ON t.id=p.topic_id WHERE t.subject '.$cur_word_like.' GROUP BY p.id, t.id';
   197 								else
   200 								else
   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';
   201 									$sql = 'SELECT p.id FROM '.$pun_db->prefix.'posts AS p INNER JOIN '.$pun_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';
   199 							}
   202 							}
   200 							else
   203 							else
   201 							{
   204 							{
   202 								$cur_word = str_replace('*', '%', $cur_word);
   205 								$cur_word = str_replace('*', '%', $cur_word);
   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;
   206 								$sql = 'SELECT m.post_id FROM '.$pun_db->prefix.'search_words AS w INNER JOIN '.$pun_db->prefix.'search_matches AS m ON m.word_id = w.id WHERE w.word LIKE \''.$cur_word.'\''.$search_in_cond;
   204 							}
   207 							}
   205 
   208 
   206 							$result = $db->query($sql, true) or error('Unable to search for posts', __FILE__, __LINE__, $db->error());
   209 							$result = $pun_db->query($sql, true) or error('Unable to search for posts', __FILE__, __LINE__, $pun_db->error());
   207 
   210 
   208 							$row = array();
   211 							$row = array();
   209 							while ($temp = $db->fetch_row($result))
   212 							while ($temp = $pun_db->fetch_row($result))
   210 							{
   213 							{
   211 								$row[$temp[0]] = 1;
   214 								$row[$temp[0]] = 1;
   212 
   215 
   213 								if (!$word_count)
   216 								if (!$word_count)
   214 									$result_list[$temp[0]] = 1;
   217 									$result_list[$temp[0]] = 1;
   227 										$result_list[$post_id] = 0;
   230 										$result_list[$post_id] = 0;
   228 								}
   231 								}
   229 							}
   232 							}
   230 
   233 
   231 							++$word_count;
   234 							++$word_count;
   232 							$db->free_result($result);
   235 							$pun_db->free_result($result);
   233 
   236 
   234 							break;
   237 							break;
   235 						}
   238 						}
   236 					}
   239 					}
   237 				}
   240 				}
   250 			if ($author && strcasecmp($author, 'Guest') && strcasecmp($author, $lang_common['Guest']))
   253 			if ($author && strcasecmp($author, 'Guest') && strcasecmp($author, $lang_common['Guest']))
   251 			{
   254 			{
   252 				switch ($db_type)
   255 				switch ($db_type)
   253 				{
   256 				{
   254 					case 'pgsql':
   257 					case 'pgsql':
   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());
   258 						$result = $pun_db->query('SELECT id FROM '.$pun_db->prefix.'users WHERE username ILIKE \''.$pun_db->escape($author).'\'') or error('Unable to fetch users', __FILE__, __LINE__, $pun_db->error());
   256 						break;
   259 						break;
   257 
   260 
   258 					default:
   261 					default:
   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());
   262 						$result = $pun_db->query('SELECT id FROM '.$pun_db->prefix.'users WHERE username LIKE \''.$pun_db->escape($author).'\'') or error('Unable to fetch users', __FILE__, __LINE__, $pun_db->error());
   260 						break;
   263 						break;
   261 				}
   264 				}
   262 
   265 
   263 				if ($db->num_rows($result))
   266 				if ($pun_db->num_rows($result))
   264 				{
   267 				{
   265 					$user_ids = '';
   268 					$user_ids = '';
   266 					while ($row = $db->fetch_row($result))
   269 					while ($row = $pun_db->fetch_row($result))
   267 						$user_ids .= (($user_ids != '') ? ',' : '').$row[0];
   270 						$user_ids .= (($user_ids != '') ? ',' : '').$row[0];
   268 
   271 
   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());
   272 					$result = $pun_db->query('SELECT id FROM '.$pun_db->prefix.'posts WHERE poster_id IN('.$user_ids.')') or error('Unable to fetch matched posts list', __FILE__, __LINE__, $pun_db->error());
   270 
   273 
   271 					$search_ids = array();
   274 					$search_ids = array();
   272 					while ($row = $db->fetch_row($result))
   275 					while ($row = $pun_db->fetch_row($result))
   273 						$author_results[] = $row[0];
   276 						$author_results[] = $row[0];
   274 
   277 
   275 					$db->free_result($result);
   278 					$pun_db->free_result($result);
   276 				}
   279 				}
   277 			}
   280 			}
   278 
   281 
   279 
   282 
   280 			if ($author && $keywords)
   283 			if ($author && $keywords)
   293 				message($lang_search['No hits']);
   296 				message($lang_search['No hits']);
   294 
   297 
   295 
   298 
   296 			if ($show_as == 'topics')
   299 			if ($show_as == 'topics')
   297 			{
   300 			{
   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());
   301 				$result = $pun_db->query('SELECT t.id FROM '.$pun_db->prefix.'posts AS p INNER JOIN '.$pun_db->prefix.'topics AS t ON t.id=p.topic_id INNER 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) AND p.id IN('.implode(',', $search_ids).')'.$forum_sql.' GROUP BY t.id', true) or error('Unable to fetch topic list', __FILE__, __LINE__, $pun_db->error());
   299 
   302 
   300 				$search_ids = array();
   303 				$search_ids = array();
   301 				while ($row = $db->fetch_row($result))
   304 				while ($row = $pun_db->fetch_row($result))
   302 					$search_ids[] = $row[0];
   305 					$search_ids[] = $row[0];
   303 
   306 
   304 				$db->free_result($result);
   307 				$pun_db->free_result($result);
   305 
   308 
   306 				$num_hits = count($search_ids);
   309 				$num_hits = count($search_ids);
   307 			}
   310 			}
   308 			else
   311 			else
   309 			{
   312 			{
   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());
   313 				$result = $pun_db->query('SELECT p.id FROM '.$pun_db->prefix.'posts AS p INNER JOIN '.$pun_db->prefix.'topics AS t ON t.id=p.topic_id INNER 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) AND p.id IN('.implode(',', $search_ids).')'.$forum_sql, true) or error('Unable to fetch topic list', __FILE__, __LINE__, $pun_db->error());
   311 
   314 
   312 				$search_ids = array();
   315 				$search_ids = array();
   313 				while ($row = $db->fetch_row($result))
   316 				while ($row = $pun_db->fetch_row($result))
   314 					$search_ids[] = $row[0];
   317 					$search_ids[] = $row[0];
   315 
   318 
   316 				$db->free_result($result);
   319 				$pun_db->free_result($result);
   317 
   320 
   318 				$num_hits = count($search_ids);
   321 				$num_hits = count($search_ids);
   319 			}
   322 			}
   320 		}
   323 		}
   321 		else if ($action == 'show_new' || $action == 'show_24h' || $action == 'show_user' || $action == 'show_subscriptions' || $action == 'show_unanswered')
   324 		else if ($action == 'show_new' || $action == 'show_24h' || $action == 'show_user' || $action == 'show_subscriptions' || $action == 'show_unanswered')
   324 			if ($action == 'show_new')
   327 			if ($action == 'show_new')
   325 			{
   328 			{
   326 				if ($pun_user['is_guest'])
   329 				if ($pun_user['is_guest'])
   327 					message($lang_common['No permission']);
   330 					message($lang_common['No permission']);
   328 
   331 
   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());
   332 				$result = $pun_db->query('SELECT t.id FROM '.$pun_db->prefix.'topics AS t INNER 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) AND t.last_post>'.$pun_user['last_visit'].' AND t.moved_to IS NULL') or error('Unable to fetch topic list', __FILE__, __LINE__, $pun_db->error());
   330 				$num_hits = $db->num_rows($result);
   333 				$num_hits = $pun_db->num_rows($result);
   331 
   334 
   332 				if (!$num_hits)
   335 				if (!$num_hits)
   333 					message($lang_search['No new posts']);
   336 					message($lang_search['No new posts']);
   334 			}
   337 			}
   335 			// If it's a search for todays posts
   338 			// If it's a search for todays posts
   336 			else if ($action == 'show_24h')
   339 			else if ($action == 'show_24h')
   337 			{
   340 			{
   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());
   341 				$result = $pun_db->query('SELECT t.id FROM '.$pun_db->prefix.'topics AS t INNER 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) AND t.last_post>'.(time() - 86400).' AND t.moved_to IS NULL') or error('Unable to fetch topic list', __FILE__, __LINE__, $pun_db->error());
   339 				$num_hits = $db->num_rows($result);
   342 				$num_hits = $pun_db->num_rows($result);
   340 
   343 
   341 				if (!$num_hits)
   344 				if (!$num_hits)
   342 					message($lang_search['No recent posts']);
   345 					message($lang_search['No recent posts']);
   343 			}
   346 			}
   344 			// If it's a search for posts by a specific user ID
   347 			// If it's a search for posts by a specific user ID
   345 			else if ($action == 'show_user')
   348 			else if ($action == 'show_user')
   346 			{
   349 			{
   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());
   350 				$result = $pun_db->query('SELECT t.id FROM '.$pun_db->prefix.'topics AS t INNER JOIN '.$pun_db->prefix.'posts AS p ON t.id=p.topic_id INNER 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) AND p.poster_id='.$user_id.' GROUP BY t.id') or error('Unable to fetch topic list', __FILE__, __LINE__, $pun_db->error());
   348 				$num_hits = $db->num_rows($result);
   351 				$num_hits = $pun_db->num_rows($result);
   349 
   352 
   350 				if (!$num_hits)
   353 				if (!$num_hits)
   351 					message($lang_search['No user posts']);
   354 					message($lang_search['No user posts']);
   352 			}
   355 			}
   353 			// If it's a search for subscribed topics
   356 			// If it's a search for subscribed topics
   354 			else if ($action == 'show_subscriptions')
   357 			else if ($action == 'show_subscriptions')
   355 			{
   358 			{
   356 				if ($pun_user['is_guest'])
   359 				if ($pun_user['is_guest'])
   357 					message($lang_common['Bad request']);
   360 					message($lang_common['Bad request']);
   358 
   361 
   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());
   362 				$result = $pun_db->query('SELECT t.id FROM '.$pun_db->prefix.'topics AS t INNER JOIN '.$pun_db->prefix.'subscriptions AS s ON (t.id=s.topic_id AND s.user_id='.$pun_user['id'].') INNER 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)') or error('Unable to fetch topic list', __FILE__, __LINE__, $pun_db->error());
   360 				$num_hits = $db->num_rows($result);
   363 				$num_hits = $pun_db->num_rows($result);
   361 
   364 
   362 				if (!$num_hits)
   365 				if (!$num_hits)
   363 					message($lang_search['No subscriptions']);
   366 					message($lang_search['No subscriptions']);
   364 			}
   367 			}
   365 			// If it's a search for unanswered posts
   368 			// If it's a search for unanswered posts
   366 			else
   369 			else
   367 			{
   370 			{
   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());
   371 				$result = $pun_db->query('SELECT t.id FROM '.$pun_db->prefix.'topics AS t INNER 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) AND t.num_replies=0 AND t.moved_to IS NULL') or error('Unable to fetch topic list', __FILE__, __LINE__, $pun_db->error());
   369 				$num_hits = $db->num_rows($result);
   372 				$num_hits = $pun_db->num_rows($result);
   370 
   373 
   371 				if (!$num_hits)
   374 				if (!$num_hits)
   372 					message($lang_search['No unanswered']);
   375 					message($lang_search['No unanswered']);
   373 			}
   376 			}
   374 
   377 
   375 			// We want to sort things after last post
   378 			// We want to sort things after last post
   376 			$sort_by = 4;
   379 			$sort_by = 4;
   377 
   380 
   378 			$search_ids = array();
   381 			$search_ids = array();
   379 			while ($row = $db->fetch_row($result))
   382 			while ($row = $pun_db->fetch_row($result))
   380 				$search_ids[] = $row[0];
   383 				$search_ids[] = $row[0];
   381 
   384 
   382 			$db->free_result($result);
   385 			$pun_db->free_result($result);
   383 
   386 
   384 			$show_as = 'topics';
   387 			$show_as = 'topics';
   385 		}
   388 		}
   386 		else
   389 		else
   387 			message($lang_common['Bad request']);
   390 			message($lang_common['Bad request']);
   388 
   391 
   389 
   392 
   390 		// Prune "old" search results
   393 		// Prune "old" search results
   391 		$old_searches = array();
   394 		$old_searches = array();
   392 		$result = $db->query('SELECT ident FROM '.$db->prefix.'online') or error('Unable to fetch online list', __FILE__, __LINE__, $db->error());
   395 		$result = $pun_db->query('SELECT ident FROM '.$pun_db->prefix.'online') or error('Unable to fetch online list', __FILE__, __LINE__, $pun_db->error());
   393 
   396 
   394 		if ($db->num_rows($result))
   397 		if ($pun_db->num_rows($result))
   395 		{
   398 		{
   396 			while ($row = $db->fetch_row($result))
   399 			while ($row = $pun_db->fetch_row($result))
   397 				$old_searches[] = '\''.$db->escape($row[0]).'\'';
   400 				$old_searches[] = '\''.$pun_db->escape($row[0]).'\'';
   398 
   401 
   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());
   402 			$pun_db->query('DELETE FROM '.$pun_db->prefix.'search_cache WHERE ident NOT IN('.implode(',', $old_searches).')') or error('Unable to delete search results', __FILE__, __LINE__, $pun_db->error());
   400 		}
   403 		}
   401 
   404 
   402 		// Final search results
   405 		// Final search results
   403 		$search_results = implode(',', $search_ids);
   406 		$search_results = implode(',', $search_ids);
   404 
   407 
   411 		$temp = serialize($temp);
   414 		$temp = serialize($temp);
   412 		$search_id = mt_rand(1, 2147483647);
   415 		$search_id = mt_rand(1, 2147483647);
   413 
   416 
   414 		$ident = ($pun_user['is_guest']) ? get_remote_address() : $pun_user['username'];
   417 		$ident = ($pun_user['is_guest']) ? get_remote_address() : $pun_user['username'];
   415 
   418 
   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());
   419 		$pun_db->query('INSERT INTO '.$pun_db->prefix.'search_cache (id, ident, search_data) VALUES('.$search_id.', \''.$pun_db->escape($ident).'\', \''.$pun_db->escape($temp).'\')') or error('Unable to insert search results', __FILE__, __LINE__, $pun_db->error());
   417 
   420 
   418 		if ($action != 'show_new' && $action != 'show_24h')
   421 		if ($action != 'show_new' && $action != 'show_24h')
   419 		{
   422 		{
   420 			$db->end_transaction();
   423 			$pun_db->end_transaction();
   421 			$db->close();
   424 			$pun_db->close();
   422 
   425 
   423 			// Redirect the user to the cached result page
   426 			// Redirect the user to the cached result page
   424 			header('Location: search.php?search_id='.$search_id);
   427 			header('Location: search.php?search_id='.$search_id);
   425 			exit;
   428 			exit;
   426 		}
   429 		}
   454 		}
   457 		}
   455 
   458 
   456 		if ($show_as == 'posts')
   459 		if ($show_as == 'posts')
   457 		{
   460 		{
   458 			$substr_sql = ($db_type != 'sqlite') ? 'SUBSTRING' : 'SUBSTR';
   461 			$substr_sql = ($db_type != 'sqlite') ? 'SUBSTRING' : 'SUBSTR';
   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;
   462 			$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 '.$pun_db->prefix.'posts AS p INNER JOIN '.$pun_db->prefix.'topics AS t ON t.id=p.topic_id WHERE p.id IN('.$search_results.') ORDER BY '.$sort_by_sql;
   460 		}
   463 		}
   461 		else
   464 		else
   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;
   465 			$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 '.$pun_db->prefix.'topics AS t WHERE t.id IN('.$search_results.') ORDER BY '.$sort_by_sql;
   463 
   466 
   464 
   467 
   465 		// Determine the topic or post offset (based on $_GET['p'])
   468 		// Determine the topic or post offset (based on $_GET['p'])
   466 		$per_page = ($show_as == 'posts') ? $pun_user['disp_posts'] : $pun_user['disp_topics'];
   469 		$per_page = ($show_as == 'posts') ? $pun_user['disp_posts'] : $pun_user['disp_topics'];
   467 		$num_pages = ceil($num_hits / $per_page);
   470 		$num_pages = ceil($num_hits / $per_page);
   468 
   471 
   469 		$p = (!isset($_GET['p']) || $_GET['p'] <= 1 || $_GET['p'] > $num_pages) ? 1 : $_GET['p'];
   472 		$p = (!isset($_GET['p']) || $_GET['p'] <= 1 || $_GET['p'] > $num_pages) ? 1 : $_GET['p'];
   470 		$start_from = $per_page * ($p - 1);
   473 		$start_from = $per_page * ($p - 1);
   471 
   474 
   472 		// Generate paging links
   475 		// Generate paging links
   473 		$paging_links = $lang_common['Pages'].': '.paginate($num_pages, $p, 'search.php?search_id='.$search_id);
   476 		$paging_links = $lang_common['Pages'].': '.pun_paginate($num_pages, $p, 'search.php?search_id='.$search_id);
   474 
   477 
   475 
   478 
   476 		$sql .= ' '.$sort_dir.' LIMIT '.$start_from.', '.$per_page;
   479 		$sql .= ' '.$sort_dir.' LIMIT '.$start_from.', '.$per_page;
   477 
   480 
   478 		$result = $db->query($sql) or error('Unable to fetch search results', __FILE__, __LINE__, $db->error());
   481 		$result = $pun_db->query($sql) or error('Unable to fetch search results', __FILE__, __LINE__, $pun_db->error());
   479 
   482 
   480 		$search_set = array();
   483 		$search_set = array();
   481 		while ($row = $db->fetch_assoc($result))
   484 		while ($row = $pun_db->fetch_assoc($result))
   482 			$search_set[] = $row;
   485 			$search_set[] = $row;
   483 
   486 
   484 		$db->free_result($result);
   487 		$pun_db->free_result($result);
   485 
   488 
   486 		$page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / '.$lang_search['Search results'];
   489 		$page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / '.$lang_search['Search results'];
   487 		require PUN_ROOT.'header.php';
   490 		require PUN_ROOT.'header.php';
   488 
   491 
   489 
   492 
   520 <?php
   523 <?php
   521 
   524 
   522 		}
   525 		}
   523 
   526 
   524 		// Fetch the list of forums
   527 		// Fetch the list of forums
   525 		$result = $db->query('SELECT id, forum_name FROM '.$db->prefix.'forums') or error('Unable to fetch forum list', __FILE__, __LINE__, $db->error());
   528 		$result = $pun_db->query('SELECT id, forum_name FROM '.$pun_db->prefix.'forums') or error('Unable to fetch forum list', __FILE__, __LINE__, $pun_db->error());
   526 
   529 
   527 		$forum_list = array();
   530 		$forum_list = array();
   528 		while ($forum_list[] = $db->fetch_row($result))
   531 		while ($forum_list[] = $pun_db->fetch_row($result))
   529 			;
   532 			;
   530 
   533 
   531 		// Finally, lets loop through the results and output them
   534 		// Finally, lets loop through the results and output them
   532 		for ($i = 0; $i < count($search_set); ++$i)
   535 		for ($i = 0; $i < count($search_set); ++$i)
   533 		{
   536 		{
   624 					$subject_new_posts = null;
   627 					$subject_new_posts = null;
   625 
   628 
   626 				$num_pages_topic = ceil(($search_set[$i]['num_replies'] + 1) / $pun_user['disp_posts']);
   629 				$num_pages_topic = ceil(($search_set[$i]['num_replies'] + 1) / $pun_user['disp_posts']);
   627 
   630 
   628 				if ($num_pages_topic > 1)
   631 				if ($num_pages_topic > 1)
   629 					$subject_multipage = '[ '.paginate($num_pages_topic, -1, 'viewtopic.php?id='.$search_set[$i]['tid']).' ]';
   632 					$subject_multipage = '[ '.pun_paginate($num_pages_topic, -1, 'viewtopic.php?id='.$search_set[$i]['tid']).' ]';
   630 				else
   633 				else
   631 					$subject_multipage = null;
   634 					$subject_multipage = null;
   632 
   635 
   633 				// Should we show the "New posts" and/or the multipage links?
   636 				// Should we show the "New posts" and/or the multipage links?
   634 				if (!empty($subject_new_posts) || !empty($subject_multipage))
   637 				if (!empty($subject_new_posts) || !empty($subject_multipage))
   701 					<div class="infldset">
   704 					<div class="infldset">
   702 						<label class="conl"><?php echo $lang_search['Forum search'] ?>
   705 						<label class="conl"><?php echo $lang_search['Forum search'] ?>
   703 						<br /><select id="forum" name="forum">
   706 						<br /><select id="forum" name="forum">
   704 <?php
   707 <?php
   705 
   708 
   706 if ($pun_config['o_search_all_forums'] == '1' || $pun_user['g_id'] < PUN_GUEST)
   709 if ($pun_config['o_search_all_forums'] == '1' || $pun_user['g_id'] >= USER_LEVEL_MEMBER)
   707 	echo "\t\t\t\t\t\t\t".'<option value="-1">'.$lang_search['All forums'].'</option>'."\n";
   710 	echo "\t\t\t\t\t\t\t".'<option value="-1">'.$lang_search['All forums'].'</option>'."\n";
   708 
   711 
   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());
   712 $result = $pun_db->query('SELECT c.id AS cid, c.cat_name, f.id AS fid, f.forum_name, f.redirect_url FROM '.$pun_db->prefix.'categories AS c INNER JOIN '.$pun_db->prefix.'forums AS f ON c.id=f.cat_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) 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__, $pun_db->error());
   710 
   713 
   711 $cur_category = 0;
   714 $cur_category = 0;
   712 while ($cur_forum = $db->fetch_assoc($result))
   715 while ($cur_forum = $pun_db->fetch_assoc($result))
   713 {
   716 {
   714 	if ($cur_forum['cid'] != $cur_category)	// A new category since last iteration?
   717 	if ($cur_forum['cid'] != $cur_category)	// A new category since last iteration?
   715 	{
   718 	{
   716 		if ($cur_category)
   719 		if ($cur_category)
   717 			echo "\t\t\t\t\t\t\t".'</optgroup>'."\n";
   720 			echo "\t\t\t\t\t\t\t".'</optgroup>'."\n";