' . "\n";
echo '
';
// Search CONFIG
$searchResults = 30; // How many max search results
/* Below HTML CODE = Plugin you can put anywhere in Znote AAC where you want search.
'. t('fsearch.title'). '
*/
function stripBBCode($text_to_search) {
$pattern = '|[[\/\!]*?[^\[\]]*?]|si';
$replace = '';
return preg_replace($pattern, $replace, $text_to_search);
}
//data_dump($_GET, false, "Post data:");
// Fetch and sanitize values:
$type = getValue($_GET['type'] ?? null);
if ($type !== false) $type = (int)$type;
$text = getvalue($_GET['text'] ?? null);
function forum_search_like_clause(string $column, array $words): array {
if (!$words) {
return ['1=1', []];
}
$parts = [];
$params = [];
foreach ($words as $word) {
$parts[] = "`{$column}` LIKE ?";
$params[] = '%' . $word . '%';
}
return [implode(' AND ', $parts), $params];
}
$textWords = ($text !== false) ? explode(' ', $text) : [];
[$textTitleSql, $textTitleParams] = forum_search_like_clause('title', $textWords);
[$textPostSql, $textPostParams] = forum_search_like_clause('text', $textWords);
[$textAuthorSql, $textAuthorParams] = forum_search_like_clause('player_name', $textWords);
if ($text !== false) {
$text = $textWords;
}
?>
= t('fsearch.title') ?>
4 && $type <= 6) {
$forums = db()->fetchAll("SELECT `id` FROM `znote_forum` WHERE `access`='1' AND `guild_id`='0';");
if (!is_array($forums)) $forums = array();
$allowedForums = array();
foreach($forums as $forum) $allowedForums[] = $forum['id'];
//data_dump($allowedForums, false, "Allowed forums to search");
// in_array(6, $allowedForums)
$results = false;
switch ($type) {
case 1: // Search titles
$results = db()->fetchAll("SELECT `id` AS `thread_id`, `forum_id`, `title`, `text`, `player_name` FROM `znote_forum_threads` WHERE {$textTitleSql} ORDER BY `id` DESC LIMIT ?;", [...$textTitleParams, (int)$searchResults]);
if (!is_array($results)) $results = array();
// Filter out search results in custom access boards.
for ($i = 0; $i < count($results); $i++)
if (!in_array($results[$i]['forum_id'], $allowedForums))
$results[$i]['forum_id'] = false;
else {
$results[$i]['title'] = stripBBCode($results[$i]['title']);
$results[$i]['text'] = stripBBCode($results[$i]['text']);
}
//if ($results !== false) data_dump($results, false, "Search results");
//else echo "
'. t('fsearch.no_results'). '";
break;
case 2: // Search posts
$results = db()->fetchAll("SELECT `thread_id`, `player_name`, `text` FROM `znote_forum_posts` WHERE {$textPostSql} ORDER BY `id` DESC LIMIT ?;", [...$textPostParams, (int)$searchResults]);
if (!is_array($results)) $results = array();
// Missing ['forum_id'], ['title'], lets get them
for ($i = 0; $i < count($results); $i++) {
// $results[$i]['asd']
$thread = db()->fetchOne("SELECT `forum_id`, `title` FROM `znote_forum_threads` WHERE `id` = ? LIMIT 1;", [$results[$i]['thread_id']]);
if ($thread !== false) {
$results[$i]['forum_id'] = $thread['forum_id'];
$results[$i]['title'] = $thread['title'];
if (!in_array($results[$i]['forum_id'], $allowedForums)) $results[$i]['forum_id'] = false;
else {
$results[$i]['title'] = stripBBCode($results[$i]['title']);
$results[$i]['text'] = stripBBCode($results[$i]['text']);
}
} else $results[$i]['forum_id'] = false;
} // DONE. :)
//data_dump(false, $results, "DATA");
break;
case 3: // Search authors last threads
$results = db()->fetchAll("SELECT `id` AS `thread_id`, `forum_id`, `title`, `text`, `player_name` FROM `znote_forum_threads` WHERE {$textAuthorSql} ORDER BY `id` DESC LIMIT ?;", [...$textAuthorParams, (int)$searchResults]);
if (!is_array($results)) $results = array();
// Filter out search results in custom access boards.
for ($i = 0; $i < count($results); $i++)
if (!in_array($results[$i]['forum_id'], $allowedForums))
$results[$i]['forum_id'] = false;
else {
$results[$i]['title'] = stripBBCode($results[$i]['title']);
$results[$i]['text'] = stripBBCode($results[$i]['text']);
}
//if ($results !== false) data_dump($results, false, "Search results");
//else echo "
'. t('fsearch.no_results'). '";
break;
case 4: // Search authors last posts
$results = db()->fetchAll("SELECT `thread_id`, `player_name`, `text` FROM `znote_forum_posts` WHERE {$textAuthorSql} ORDER BY `id` DESC LIMIT ?;", [...$textAuthorParams, (int)$searchResults]);
if (!is_array($results)) $results = array();
// Missing ['forum_id'], ['title'], lets get them
for ($i = 0; $i < count($results); $i++) {
// $results[$i]['asd']
$thread = db()->fetchOne("SELECT `forum_id`, `title` FROM `znote_forum_threads` WHERE `id` = ? LIMIT 1;", [$results[$i]['thread_id']]);
if ($thread !== false) {
$results[$i]['forum_id'] = $thread['forum_id'];
$results[$i]['title'] = $thread['title'];
if (!in_array($results[$i]['forum_id'], $allowedForums)) $results[$i]['forum_id'] = false;
else {
$results[$i]['title'] = stripBBCode($results[$i]['title']);
$results[$i]['text'] = stripBBCode($results[$i]['text']);
}
} else $results[$i]['forum_id'] = false;
} // DONE. :)
break;
case 5: // Search latest titles
$results = db()->fetchAll("SELECT `id` AS `thread_id`, `forum_id`, `title`, `text`, `player_name` FROM `znote_forum_threads` ORDER BY `id` DESC LIMIT ?;", [(int)$searchResults]);
if (!is_array($results)) $results = array();
// Filter out search results in custom access boards.
for ($i = 0; $i < count($results); $i++)
if (!in_array($results[$i]['forum_id'], $allowedForums))
$results[$i]['forum_id'] = false;
else {
$results[$i]['title'] = stripBBCode($results[$i]['title']);
$results[$i]['text'] = stripBBCode($results[$i]['text']);
}
//if ($results !== false) data_dump($results, false, "Search results");
//else echo "
No results.";
break;
case 6: // Search posts
$results = db()->fetchAll("SELECT `thread_id`, `player_name`, `text` FROM `znote_forum_posts` ORDER BY `id` DESC LIMIT ?;", [(int)$searchResults]);
if (!is_array($results)) $results = array();
// Missing ['forum_id'], ['title'], lets get them
for ($i = 0; $i < count($results); $i++) {
// $results[$i]['asd']
$thread = db()->fetchOne("SELECT `forum_id`, `title` FROM `znote_forum_threads` WHERE `id` = ? LIMIT 1;", [$results[$i]['thread_id']]);
if ($thread !== false) {
$results[$i]['forum_id'] = $thread['forum_id'];
$results[$i]['title'] = $thread['title'];
if (!in_array($results[$i]['forum_id'], $allowedForums)) $results[$i]['forum_id'] = false;
else {
$results[$i]['title'] = stripBBCode($results[$i]['title']);
$results[$i]['text'] = stripBBCode($results[$i]['text']);
}
} else $results[$i]['forum_id'] = false;
} // DONE. :)
//data_dump(false, $results, "DATA");
break;
default:
# code...
break;
}
// Create table and show stuff!
if ($results !== false) {
$count = 0;
foreach ($results as $r) if ($r['forum_id'] !== false) $count++;
if ($count > 0) {
?>
| = t('fsearch.col_char') ?> |
= t('fsearch.col_thread') ?> |
= t('fsearch.col_post') ?> |
|
|
140) ? substr($result['text'],0,137).'...' : $result['text']; ?> |
". t('fsearch.fill_all2'). "";
echo '
';
theme_close();
?>