Gazelle/sections/forums/search.php

383 lines
10 KiB
PHP
Raw Normal View History

2011-03-28 14:21:28 +00:00
<?
//TODO: Clean up this fucking mess
/*
Forums search result page
*/
2013-05-27 08:00:58 +00:00
include(SERVER_ROOT.'/classes/text.class.php');
2012-06-23 08:00:13 +00:00
$Text = new TEXT;
2011-03-28 14:21:28 +00:00
2013-05-21 08:01:09 +00:00
list($Page, $Limit) = Format::page_limit(POSTS_PER_PAGE);
2011-03-28 14:21:28 +00:00
2013-04-19 08:00:55 +00:00
if ((isset($_GET['type']) && $_GET['type'] == 'body')) {
2011-03-28 14:21:28 +00:00
$Type = 'body';
} else {
$Type='title';
}
// What are we looking for? Let's make sure it isn't dangerous.
2013-04-19 08:00:55 +00:00
if (isset($_GET['search'])) {
2011-03-28 14:21:28 +00:00
$Search = trim($_GET['search']);
} else {
$Search = '';
}
2013-09-15 08:00:53 +00:00
$ThreadAfterDate = db_string($_GET['thread_created_after']);
$ThreadBeforeDate = db_string($_GET['thread_created_before']);
$ThreadAfterDateDisplay = "";
$ThreadBeforeDateDisplay = "";
if (!empty($ThreadAfterDate) && !is_date($ThreadAfterDate)) {
error('Incorrect topic after date format');
} elseif (!empty($ThreadAfterDate)) {
$ThreadAfterDateDisplay = "value='" . date('Y-m-d', strtotime($ThreadAfterDate)) . "'";
}
if (!empty($ThreadBeforeDate) && !is_date($ThreadBeforeDate)) {
error('Incorrect topic before date format');
} elseif (!empty($ThreadBeforeDate)) {
$ThreadBeforeDateDisplay = "value='" . date('Y-m-d', strtotime($ThreadBeforeDate)) . "'";
}
$PostAfterDate = db_string($_GET['post_created_after']);
$PostBeforeDate = db_string($_GET['post_created_before']);
$PostAfterDateDisplay = "";
$PostBeforeDateDisplay = "";
if (!empty($PostAfterDate) && !is_date($PostAfterDate)) {
error('Incorrect post after date format');
} elseif (!empty($PostAfterDate)) {
$PostAfterDateDisplay = "value='" . date('Y-m-d', strtotime($PostAfterDate)) . "'";
}
if (!empty($PostBeforeDate) && !is_date($PostBeforeDate)) {
error('Incorrect post before date format');
} elseif (!empty($PostBeforeDate)) {
$PostBeforeDateDisplay = "value='" . date('Y-m-d', strtotime($PostBeforeDate)) . "'";
}
2012-11-13 08:00:17 +00:00
2011-03-28 14:21:28 +00:00
// Searching for posts by a specific user
2013-04-19 08:00:55 +00:00
if (!empty($_GET['user'])) {
2013-02-18 08:00:22 +00:00
$User = trim($_GET['user']);
2013-07-10 00:08:53 +00:00
$DB->query("
SELECT ID
FROM users_main
WHERE Username = '".db_string($User)."'");
2011-03-28 14:21:28 +00:00
list($AuthorID) = $DB->next_record();
2013-04-19 08:00:55 +00:00
if ($AuthorID === null) {
2013-02-18 08:00:22 +00:00
$AuthorID = 0;
//this will cause the search to return 0 results.
//workaround in line 276 to display that the username was wrong.
}
2011-03-28 14:21:28 +00:00
} else {
$User = '';
}
// Are we looking in individual forums?
2013-04-19 08:00:55 +00:00
if (isset($_GET['forums']) && is_array($_GET['forums'])) {
2011-03-28 14:21:28 +00:00
$ForumArray = array();
2013-04-19 08:00:55 +00:00
foreach ($_GET['forums'] as $Forum) {
if (is_number($Forum)) {
2011-03-28 14:21:28 +00:00
$ForumArray[]=$Forum;
}
}
2013-04-19 08:00:55 +00:00
if (count($ForumArray) > 0) {
2011-03-28 14:21:28 +00:00
$SearchForums = implode(', ',$ForumArray);
}
}
// Searching for posts in a specific thread
2013-02-16 08:00:57 +00:00
if (!empty($_GET['threadid']) && is_number($_GET['threadid'])) {
$ThreadID = $_GET['threadid'];
$Type = 'body';
2013-04-19 08:00:55 +00:00
$SQL = "
SELECT
Title
FROM forums_topics AS t
JOIN forums AS f ON f.ID=t.ForumID
WHERE t.ID=$ThreadID
2013-08-28 23:08:41 +00:00
AND " . Forums::user_forums_sql();
$DB->query($SQL);
if (list($Title) = $DB->next_record()) {
2012-09-09 08:00:26 +00:00
$Title = " &gt; <a href=\"forums.php?action=viewthread&amp;threadid=$ThreadID\">$Title</a>";
} else {
2013-02-16 08:00:57 +00:00
error(404);
}
} else {
$ThreadID = '';
}
2011-03-28 14:21:28 +00:00
// Let's hope we got some results - start printing out the content.
2013-06-27 08:01:06 +00:00
View::show_header('Forums &gt; Search', 'bbcode,forum_search');
2011-03-28 14:21:28 +00:00
?>
<div class="thin">
2013-05-21 08:01:09 +00:00
<div class="header">
<h2><a href="forums.php">Forums</a> &gt; Search<?=$Title?></h2>
</div>
2012-09-15 08:00:25 +00:00
<form class="search_form" name="forums" action="" method="get">
2011-03-28 14:21:28 +00:00
<input type="hidden" name="action" value="search" />
2012-09-01 08:00:24 +00:00
<table cellpadding="6" cellspacing="1" border="0" class="layout border" width="100%">
2011-03-28 14:21:28 +00:00
<tr>
<td><strong>Search for:</strong></td>
<td>
<input type="text" name="search" size="70" value="<?=display_str($Search)?>" />
</td>
</tr>
2013-09-15 08:00:53 +00:00
<tr>
<td><strong>Username:</strong></td>
<td>
<input type="text" name="user" size="70" value="<?=display_str($User)?>" />
</td>
</tr>
<tr>
<td><strong>Topic created:</strong></td>
<td>
After:
<input type="date" name="thread_created_after" id="thread_created_after" <?=$ThreadAfterDateDisplay?>/>
Before:
<input type="date" name="thread_created_before" id="thread_created_before" <?=$ThreadBeforeDateDisplay?> />
</td>
</tr>
<?
2013-02-22 08:00:24 +00:00
if (empty($ThreadID)) { ?>
2011-03-28 14:21:28 +00:00
<tr>
<td><strong>Search in:</strong></td>
<td>
2013-05-15 08:00:54 +00:00
<input type="radio" name="type" id="type_title" value="title" <? if ($Type == 'title') { echo 'checked="checked" '; } ?>/>
2011-03-28 14:21:28 +00:00
<label for="type_title">Titles</label>
2013-05-15 08:00:54 +00:00
<input type="radio" name="type" id="type_body" value="body" <? if ($Type == 'body') { echo 'checked="checked" '; } ?>/>
2011-03-28 14:21:28 +00:00
<label for="type_body">Post bodies</label>
</td>
</tr>
2013-09-15 08:00:53 +00:00
<tr id="post_created_row" <? if ($Type == 'title') { echo "class='hidden'"; } ?>>
<td><strong>Post created:</strong></td>
<td>
After:
<input type="date" name="post_created_after" id="post_created_after" <?=$PostAfterDateDisplay?>/>
Before:
<input type="date" name="post_created_before" id="post_created_before" <?=$PostBeforeDateDisplay?> />
</td>
</tr>
2011-03-28 14:21:28 +00:00
<tr>
<td><strong>Forums:</strong></td>
<td>
2012-09-01 08:00:24 +00:00
<table class="cat_list layout">
2013-02-22 08:00:24 +00:00
2013-05-21 08:01:09 +00:00
<? // List of forums
$Open = false;
$LastCategoryID = -1;
$Columns = 0;
2013-05-21 08:01:09 +00:00
$i = 0;
2013-04-19 08:00:55 +00:00
foreach ($Forums as $Forum) {
2013-08-28 23:08:41 +00:00
if (!Forums::check_forumperm($Forum['ID'])) {
continue;
}
2013-02-22 08:00:24 +00:00
$Columns++;
2013-02-22 08:00:24 +00:00
if ($Forum['CategoryID'] != $LastCategoryID) {
$LastCategoryID = $Forum['CategoryID'];
2013-04-19 08:00:55 +00:00
if ($Open) {
if ($Columns % 5) { ?>
<td colspan="<?=(5 - ($Columns % 5))?>"></td>
2013-02-22 08:00:24 +00:00
<?
}
2011-03-28 14:21:28 +00:00
?>
</tr>
2013-02-22 08:00:24 +00:00
<?
}
$Columns = 0;
$Open = true;
2013-05-21 08:01:09 +00:00
$i++;
2011-03-28 14:21:28 +00:00
?>
<tr>
2013-09-04 08:00:43 +00:00
<td colspan="5" class="forum_cat">
2013-05-21 08:01:09 +00:00
<strong><?=$ForumCats[$Forum['CategoryID']]?></strong>
<a href="#" class="brackets forum_category" id="forum_category_<?=$i?>">Check all</a>
</td>
2011-03-28 14:21:28 +00:00
</tr>
<tr>
2013-04-19 08:00:55 +00:00
<? } elseif ($Columns % 5 == 0) { ?>
2011-03-28 14:21:28 +00:00
</tr>
<tr>
<? } ?>
2011-03-28 14:21:28 +00:00
<td>
2013-05-21 08:01:09 +00:00
<input type="checkbox" name="forums[]" value="<?=$Forum['ID']?>" data-category="forum_category_<?=$i?>" id="forum_<?=$Forum['ID']?>"<? if (isset($_GET['forums']) && in_array($Forum['ID'], $_GET['forums'])) { echo ' checked="checked"';} ?> />
2013-01-16 08:00:31 +00:00
<label for="forum_<?=$Forum['ID']?>"><?=htmlspecialchars($Forum['Name'])?></label>
2011-03-28 14:21:28 +00:00
</td>
2013-01-16 08:00:31 +00:00
<? }
2013-04-19 08:00:55 +00:00
if ($Columns % 5) { ?>
<td colspan="<?=(5 - ($Columns % 5))?>"></td>
<? } ?>
2011-03-28 14:21:28 +00:00
</tr>
</table>
<? } else { ?>
2013-01-16 08:00:31 +00:00
<input type="hidden" name="threadid" value="<?=$ThreadID?>" />
<? } ?>
2013-01-16 08:00:31 +00:00
</td>
</tr>
2011-03-28 14:21:28 +00:00
<tr>
<td colspan="2" class="center">
<input type="submit" value="Search" />
</td>
</tr>
2013-02-22 08:00:24 +00:00
</table>
2011-03-28 14:21:28 +00:00
</form>
<div class="linkbox">
<?
// Break search string down into individual words
2013-04-19 08:00:55 +00:00
$Words = explode(' ', db_string($Search));
2011-03-28 14:21:28 +00:00
2013-04-19 08:00:55 +00:00
if ($Type == 'body') {
2013-02-22 08:00:24 +00:00
2013-09-15 08:00:53 +00:00
$SQL = "
2013-04-19 08:00:55 +00:00
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
2011-03-28 14:21:28 +00:00
FROM forums_posts AS p
2013-04-19 08:00:55 +00:00
JOIN forums_topics AS t ON t.ID=p.TopicID
JOIN forums AS f ON f.ID=t.ForumID
2013-08-28 23:08:41 +00:00
WHERE " . Forums::user_forums_sql() . ' AND ';
2011-03-28 14:21:28 +00:00
//In tests, this is significantly faster than LOCATE
2013-09-15 08:00:53 +00:00
$SQL .= "p.Body LIKE '%";
$SQL .= implode("%' AND p.Body LIKE '%", $Words);
$SQL .= "%' ";
2011-03-28 14:21:28 +00:00
2013-09-15 08:00:53 +00:00
//$SQL .= "LOCATE('";
//$SQL .= implode("', p.Body) AND LOCATE('", $Words);
//$SQL .= "', p.Body) ";
2011-03-28 14:21:28 +00:00
2013-04-19 08:00:55 +00:00
if (isset($SearchForums)) {
2013-09-15 08:00:53 +00:00
$SQL.=" AND f.ID IN ($SearchForums)";
2011-03-28 14:21:28 +00:00
}
2013-04-19 08:00:55 +00:00
if (isset($AuthorID)) {
2013-09-15 08:00:53 +00:00
$SQL.=" AND p.AuthorID='$AuthorID' ";
2011-03-28 14:21:28 +00:00
}
2013-04-19 08:00:55 +00:00
if (!empty($ThreadID)) {
2013-09-15 08:00:53 +00:00
$SQL.=" AND t.ID='$ThreadID' ";
}
if (!empty($ThreadAfterDate)) {
$SQL .= " AND t.CreatedTime >= '$ThreadAfterDate'";
}
if (!empty($ThreadBeforeDate)) {
$SQL .= " AND t.CreatedTime <= '$ThreadBeforeDate'";
}
if (!empty($PostAfterDate)) {
$SQL .= " AND p.AddedTime >= '$PostAfterDate'";
}
if (!empty($PostBeforeDate)) {
$SQL .= " AND p.AddedTime <= '$PostBeforeDate'";
}
2013-02-22 08:00:24 +00:00
2013-09-15 08:00:53 +00:00
$SQL .= "
2013-06-04 08:00:34 +00:00
ORDER BY p.AddedTime DESC
LIMIT $Limit";
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
} else {
2013-09-15 08:00:53 +00:00
$SQL = "
2013-04-19 08:00:55 +00:00
SELECT
SQL_CALC_FOUND_ROWS
t.ID,
t.Title,
t.ForumID,
f.Name,
t.LastPostTime,
'',
2013-09-15 08:00:53 +00:00
'',
t.CreatedTime
2013-02-22 08:00:24 +00:00
FROM forums_topics AS t
2013-04-19 08:00:55 +00:00
JOIN forums AS f ON f.ID=t.ForumID
2013-08-28 23:08:41 +00:00
WHERE " . Forums::user_forums_sql() . ' AND ';
2013-09-15 08:00:53 +00:00
$SQL .= "t.Title LIKE '%";
$SQL .= implode("%' AND t.Title LIKE '%", $Words);
$SQL .= "%' ";
2013-04-19 08:00:55 +00:00
if (isset($SearchForums)) {
2013-09-15 08:00:53 +00:00
$SQL .= " AND f.ID IN ($SearchForums)";
2011-03-28 14:21:28 +00:00
}
2013-04-19 08:00:55 +00:00
if (isset($AuthorID)) {
2013-09-15 08:00:53 +00:00
$SQL .= " AND t.AuthorID = '$AuthorID' ";
2011-03-28 14:21:28 +00:00
}
2013-09-15 08:00:53 +00:00
if (!empty($ThreadAfterDate)) {
$SQL .= " AND t.CreatedTime >= '$ThreadAfterDate'";
}
if (!empty($ThreadBeforeDate)) {
$SQL .= " AND t.CreatedTime <= '$ThreadBeforeDate'";
}
$SQL .= "
2013-06-04 08:00:34 +00:00
ORDER BY t.LastPostTime DESC
LIMIT $Limit";
2011-03-28 14:21:28 +00:00
}
// Perform the query
2013-09-15 08:00:53 +00:00
$Records = $DB->query($SQL);
2011-03-28 14:21:28 +00:00
$DB->query('SELECT FOUND_ROWS()');
list($Results) = $DB->next_record();
$DB->set_query_id($Records);
2013-04-30 18:18:07 +00:00
$Pages = Format::get_pages($Page, $Results, POSTS_PER_PAGE, 9);
2011-03-28 14:21:28 +00:00
echo $Pages;
?>
</div>
2012-09-01 08:00:24 +00:00
<table cellpadding="6" cellspacing="1" border="0" class="forum_list border" width="100%">
2011-03-28 14:21:28 +00:00
<tr class="colhead">
<td>Forum</td>
2013-05-21 08:01:09 +00:00
<td><?=((!empty($ThreadID)) ? 'Post begins' : 'Topic')?></td>
2013-09-15 08:00:53 +00:00
<td>Topic creation time</td>
<td>Last post time</td>
2011-03-28 14:21:28 +00:00
</tr>
2013-07-10 00:08:53 +00:00
<? if (!$DB->has_results()) { ?>
2013-10-05 08:01:00 +00:00
<tr><td colspan="4">Nothing found<?=((isset($AuthorID) && $AuthorID == 0) ? ' (unknown username)' : '')?>!</td></tr>
2011-03-28 14:21:28 +00:00
<? }
$Row = 'a'; // For the pretty colours
2013-09-15 08:00:53 +00:00
while (list($ID, $Title, $ForumID, $ForumName, $LastTime, $PostID, $Body, $ThreadCreatedTime) = $DB->next_record()) {
2013-08-28 23:08:41 +00:00
$Row = $Row === 'a' ? 'b' : 'a';
2011-03-28 14:21:28 +00:00
// Print results
?>
<tr class="row<?=$Row?>">
<td>
<a href="forums.php?action=viewforum&amp;forumid=<?=$ForumID?>"><?=$ForumName?></a>
</td>
<td>
2013-04-19 08:00:55 +00:00
<? if (empty($ThreadID)) { ?>
2012-10-11 08:00:15 +00:00
<a href="forums.php?action=viewthread&amp;threadid=<?=$ID?>"><?=Format::cut_string($Title, 80); ?></a>
2013-04-19 08:00:55 +00:00
<? } else { ?>
2012-10-11 08:00:15 +00:00
<?=Format::cut_string($Title, 80); ?>
2013-08-28 23:08:41 +00:00
<?
}
2013-04-19 08:00:55 +00:00
if ($Type == 'body') { ?>
2013-08-28 23:08:41 +00:00
<a href="#" onclick="$('#post_<?=$PostID?>_text').gtoggle(); return false;">(Show)</a> <span style="float: right;" class="tooltip last_read" title="Jump to post"><a href="forums.php?action=viewthread&amp;threadid=<?=$ID?><? if (!empty($PostID)) { echo "&amp;postid=$PostID#post$PostID"; } ?>"></a></span>
2013-04-19 08:00:55 +00:00
<? } ?>
2011-03-28 14:21:28 +00:00
</td>
2013-09-15 08:00:53 +00:00
<td>
<?=time_diff($ThreadCreatedTime)?>
</td>
2011-03-28 14:21:28 +00:00
<td>
<?=time_diff($LastTime)?>
</td>
</tr>
2013-08-28 23:08:41 +00:00
<? if ($Type == 'body') { ?>
2012-06-23 08:00:13 +00:00
<tr class="row<?=$Row?> hidden" id="post_<?=$PostID?>_text">
2013-10-05 08:01:00 +00:00
<td colspan="4"><?=$Text->full_format($Body)?></td>
2012-06-23 08:00:13 +00:00
</tr>
<? }
2011-03-28 14:21:28 +00:00
}
?>
</table>
<div class="linkbox">
<?=$Pages?>
</div>
</div>
2012-10-11 08:00:15 +00:00
<? View::show_footer(); ?>