//TODO: Clean up this fucking mess
/*
Forums search result page
*/
include(SERVER_ROOT.'/classes/class_text.php');
$Text = new TEXT;
list($Page,$Limit) = Format::page_limit(POSTS_PER_PAGE);
if($LoggedUser['CustomForums']) {
unset($LoggedUser['CustomForums']['']);
$RestrictedForums = implode("','", array_keys($LoggedUser['CustomForums'], 0));
$PermittedForums = implode("','", array_keys($LoggedUser['CustomForums'], 1));
}
if((isset($_GET['type']) && $_GET['type'] == 'body')) {
$Type = 'body';
} else {
$Type='title';
}
// What are we looking for? Let's make sure it isn't dangerous.
if(isset($_GET['search'])) {
$Search = trim($_GET['search']);
} else {
$Search = '';
}
// Searching for posts by a specific user
if(!empty($_GET['user'])) {
$User = $_GET['user'];
$DB->query("SELECT ID FROM users_main WHERE Username='".db_string($User)."'");
list($AuthorID) = $DB->next_record();
} else {
$User = '';
}
// Are we looking in individual forums?
if(isset($_GET['forums']) && is_array($_GET['forums'])) {
$ForumArray = array();
foreach($_GET['forums'] as $Forum) {
if(is_number($Forum)) {
$ForumArray[]=$Forum;
}
}
if(count($ForumArray)>0) {
$SearchForums = implode(', ',$ForumArray);
}
}
// Searching for posts in a specific thread
if(!empty($_GET['threadid'])) {
$ThreadID = db_string($_GET['threadid']);
$Type='body';
$SQL = "SELECT Title FROM forums_topics AS t
JOIN forums AS f ON f.ID=t.ForumID
WHERE f.MinClassRead <= '$LoggedUser[Class]'
AND t.ID=$ThreadID";
if(!empty($RestrictedForums)) {
$SQL .= " AND f.ID NOT IN ('".$RestrictedForums."')";
}
$DB->query($SQL);
if (list($Title) = $DB->next_record()) {
$Title = " > $Title";
} else {
$Title = '';
$ThreadID = '';
}
} else {
$ThreadID = '';
}
// Let's hope we got some results - start printing out the content.
View::show_header('Forums'.' > '.'Search', 'bbcode');
?>
Forums > Search=$Title?>
// Break search string down into individual words
$Words = explode(' ', db_string($Search));
if($Type == 'body') {
$sql = "SELECT SQL_CALC_FOUND_ROWS
t.ID,
".(!empty($ThreadID) ? "SUBSTRING_INDEX(p.Body, ' ', 40)":"t.Title").",
t.ForumID,
f.Name,
p.AddedTime,
p.ID,
p.Body
FROM forums_posts AS p
JOIN forums_topics AS t ON t.ID=p.TopicID
JOIN forums AS f ON f.ID=t.ForumID
WHERE
((f.MinClassRead<='$LoggedUser[Class]'";
if(!empty($RestrictedForums)) {
$sql.=" AND f.ID NOT IN ('".$RestrictedForums."')";
}
$sql .= ')';
if(!empty($PermittedForums)) {
$sql.=' OR f.ID IN (\''.$PermittedForums.'\')';
}
$sql .= ') AND ';
//In tests, this is significantly faster than LOCATE
$sql .= "p.Body LIKE '%";
$sql .= implode("%' AND p.Body LIKE '%", $Words);
$sql .= "%' ";
//$sql .= "LOCATE('";
//$sql .= implode("', p.Body) AND LOCATE('", $Words);
//$sql .= "', p.Body) ";
if(isset($SearchForums)) {
$sql.=" AND f.ID IN ($SearchForums)";
}
if(isset($AuthorID)) {
$sql.=" AND p.AuthorID='$AuthorID' ";
}
if(!empty($ThreadID)) {
$sql.=" AND t.ID='$ThreadID' ";
}
$sql .= "ORDER BY p.AddedTime DESC LIMIT $Limit";
} else {
$sql = "SELECT SQL_CALC_FOUND_ROWS
t.ID,
t.Title,
t.ForumID,
f.Name,
t.LastPostTime,
'',
''
FROM forums_topics AS t
JOIN forums AS f ON f.ID=t.ForumID
WHERE
((f.MinClassRead<='$LoggedUser[Class]'";
if(!empty($RestrictedForums)) {
$sql.=" AND f.ID NOT IN ('".$RestrictedForums."')";
}
$sql .= ')';
if(!empty($PermittedForums)) {
$sql.=' OR f.ID IN (\''.$PermittedForums.'\')';
}
$sql .= ') AND ';
$sql .= "t.Title LIKE '%";
$sql .= implode("%' AND t.Title LIKE '%", $Words);
$sql .= "%' ";
if(isset($SearchForums)) {
$sql.=" AND f.ID IN ($SearchForums)";
}
if(isset($AuthorID)) {
$sql.=" AND t.AuthorID='$AuthorID' ";
}
$sql .= "ORDER BY t.LastPostTime DESC LIMIT $Limit";
}
// Perform the query
$Records = $DB->query($sql);
$DB->query('SELECT FOUND_ROWS()');
list($Results) = $DB->next_record();
$DB->set_query_id($Records);
$Pages=Format::get_pages($Page,$Results,POSTS_PER_PAGE,9);
echo $Pages;
?>
Forum |
=(!empty($ThreadID))?'Post Begins':'Topic'?> |
Time |
if($DB->record_count() == 0) { ?>
Nothing found! |
}
$Row = 'a'; // For the pretty colours
while(list($ID, $Title, $ForumID, $ForumName, $LastTime, $PostID, $Body) = $DB->next_record()) {
$Row = ($Row == 'a') ? 'b' : 'a';
// Print results
?>
=$ForumName?>
|
if(empty($ThreadID)) { ?>
=Format::cut_string($Title, 80); ?>
} else { ?>
=Format::cut_string($Title, 80); ?>
}
if ($Type == 'body') { ?>
(show)
} ?>
|
=time_diff($LastTime)?>
|
if($Type == 'body') { ?>
=$Text->full_format($Body)?> |
}
}
?>
=$Pages?>
View::show_footer(); ?>