forum_search.php

main 237 lines · 9.7 KB Raw
Alex Alex Commit Initial commit 01/10/2026 09:20
1<?php require_once 'engine/init.php'; theme_open(); protect_page();
2echo '<link rel="stylesheet" href="assets/forum.css?v=4.1.0">' . "\n";
3echo '<div class="znx-forum">';
4
5// Search CONFIG
6$searchResults = 30; // How many max search results
7
8/* Below HTML CODE = Plugin you can put anywhere in Znote AAC where you want search.
9<h1>'. t('fsearch.title'). '</h1>
10<form action="forum_search.php" method="get">
11 <select name="type">
12 <option value="1">Title</option>
13 <option value="2">Post</option>
14 <option value="3">'. t('fsearch.author_threads'). '</option>
15 <option value="4">'. t('fsearch.author_posts'). '</option>
16 <option value="5">'. t('fsearch.latest_posts2'). '</option>
17 <option value="6">'. t('fsearch.latest_threads2'). '</option>
18 </select>
19 <input type="text" name="text" placeholder="Search string">
20 <input type="submit" value="<?= t('common.search') ?>">
21</form>
22*/
23function stripBBCode($text_to_search) {
24 $pattern = '|[[\/\!]*?[^\[\]]*?]|si';
25 $replace = '';
26 return preg_replace($pattern, $replace, $text_to_search);
27}
28
29//data_dump($_GET, false, "Post data:");
30
31// Fetch and sanitize values:
32$type = getValue($_GET['type'] ?? null);
33if ($type !== false) $type = (int)$type;
34$text = getvalue($_GET['text'] ?? null);
35
36function forum_search_like_clause(string $column, array $words): array {
37 if (!$words) {
38 return ['1=1', []];
39 }
40 $parts = [];
41 $params = [];
42 foreach ($words as $word) {
43 $parts[] = "`{$column}` LIKE ?";
44 $params[] = '%' . $word . '%';
45 }
46 return [implode(' AND ', $parts), $params];
47}
48
49$textWords = ($text !== false) ? explode(' ', $text) : [];
50[$textTitleSql, $textTitleParams] = forum_search_like_clause('title', $textWords);
51[$textPostSql, $textPostParams] = forum_search_like_clause('text', $textWords);
52[$textAuthorSql, $textAuthorParams] = forum_search_like_clause('player_name', $textWords);
53if ($text !== false) {
54 $text = $textWords;
55}
56
57?>
58<h1><?= t('fsearch.title') ?></h1>
59<form method="" type="get">
60 <select name="type">
61 <option value="1" <?php if ($type == 1) echo "selected"; ?>><?= t('fsearch.option_title') ?></option>
62 <option value="2" <?php if ($type == 2) echo "selected"; ?>><?= t('fsearch.option_post') ?></option>
63 <option value="3" <?php if ($type == 3) echo "selected"; ?>><?= t('fsearch.author_threads') ?></option>
64 <option value="4" <?php if ($type == 4) echo "selected"; ?>><?= t('fsearch.author_posts') ?></option>
65 <option value="5" <?php if ($type == 5) echo "selected"; ?>><?= t('fsearch.latest_posts2') ?></option>
66 <option value="6" <?php if ($type == 6) echo "selected"; ?>><?= t('fsearch.latest_threads2') ?></option>
67 </select>
68 <input type="text" name="text" placeholder="<?= t('fsearch.search_placeholder') ?>" value="<?php if ($text !== false) echo implode(' ', $text); ?>">
69 <input type="submit" value="<?= t('common.search') ?>">
70</form>
71<?php
72
73if ($type !== false && $text !== false && $type <= 4 || $type > 4 && $type <= 6) {
74 $forums = db()->fetchAll("SELECT `id` FROM `znote_forum` WHERE `access`='1' AND `guild_id`='0';");
75 if (!is_array($forums)) $forums = array();
76 $allowedForums = array();
77 foreach($forums as $forum) $allowedForums[] = $forum['id'];
78
79 //data_dump($allowedForums, false, "Allowed forums to search");
80 // in_array(6, $allowedForums)
81
82 $results = false;
83 switch ($type) {
84 case 1: // Search titles
85 $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]);
86 if (!is_array($results)) $results = array();
87 // Filter out search results in custom access boards.
88 for ($i = 0; $i < count($results); $i++)
89 if (!in_array($results[$i]['forum_id'], $allowedForums))
90 $results[$i]['forum_id'] = false;
91 else {
92 $results[$i]['title'] = stripBBCode($results[$i]['title']);
93 $results[$i]['text'] = stripBBCode($results[$i]['text']);
94 }
95
96 //if ($results !== false) data_dump($results, false, "Search results");
97 //else echo "<br><b>'. t('fsearch.no_results'). '</b>";
98 break;
99
100 case 2: // Search posts
101 $results = db()->fetchAll("SELECT `thread_id`, `player_name`, `text` FROM `znote_forum_posts` WHERE {$textPostSql} ORDER BY `id` DESC LIMIT ?;", [...$textPostParams, (int)$searchResults]);
102 if (!is_array($results)) $results = array();
103 // Missing ['forum_id'], ['title'], lets get them
104 for ($i = 0; $i < count($results); $i++) {
105 // $results[$i]['asd']
106 $thread = db()->fetchOne("SELECT `forum_id`, `title` FROM `znote_forum_threads` WHERE `id` = ? LIMIT 1;", [$results[$i]['thread_id']]);
107 if ($thread !== false) {
108 $results[$i]['forum_id'] = $thread['forum_id'];
109 $results[$i]['title'] = $thread['title'];
110 if (!in_array($results[$i]['forum_id'], $allowedForums)) $results[$i]['forum_id'] = false;
111 else {
112 $results[$i]['title'] = stripBBCode($results[$i]['title']);
113 $results[$i]['text'] = stripBBCode($results[$i]['text']);
114 }
115 } else $results[$i]['forum_id'] = false;
116
117 } // DONE. :)
118 //data_dump(false, $results, "DATA");
119 break;
120
121 case 3: // Search authors last threads
122 $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]);
123 if (!is_array($results)) $results = array();
124 // Filter out search results in custom access boards.
125 for ($i = 0; $i < count($results); $i++)
126 if (!in_array($results[$i]['forum_id'], $allowedForums))
127 $results[$i]['forum_id'] = false;
128 else {
129 $results[$i]['title'] = stripBBCode($results[$i]['title']);
130 $results[$i]['text'] = stripBBCode($results[$i]['text']);
131 }
132
133 //if ($results !== false) data_dump($results, false, "Search results");
134 //else echo "<br><b>'. t('fsearch.no_results'). '</b>";
135 break;
136
137 case 4: // Search authors last posts
138 $results = db()->fetchAll("SELECT `thread_id`, `player_name`, `text` FROM `znote_forum_posts` WHERE {$textAuthorSql} ORDER BY `id` DESC LIMIT ?;", [...$textAuthorParams, (int)$searchResults]);
139 if (!is_array($results)) $results = array();
140 // Missing ['forum_id'], ['title'], lets get them
141 for ($i = 0; $i < count($results); $i++) {
142 // $results[$i]['asd']
143 $thread = db()->fetchOne("SELECT `forum_id`, `title` FROM `znote_forum_threads` WHERE `id` = ? LIMIT 1;", [$results[$i]['thread_id']]);
144 if ($thread !== false) {
145 $results[$i]['forum_id'] = $thread['forum_id'];
146 $results[$i]['title'] = $thread['title'];
147 if (!in_array($results[$i]['forum_id'], $allowedForums)) $results[$i]['forum_id'] = false;
148 else {
149 $results[$i]['title'] = stripBBCode($results[$i]['title']);
150 $results[$i]['text'] = stripBBCode($results[$i]['text']);
151 }
152 } else $results[$i]['forum_id'] = false;
153
154 } // DONE. :)
155 break;
156
157 case 5: // Search latest titles
158 $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]);
159 if (!is_array($results)) $results = array();
160 // Filter out search results in custom access boards.
161 for ($i = 0; $i < count($results); $i++)
162 if (!in_array($results[$i]['forum_id'], $allowedForums))
163 $results[$i]['forum_id'] = false;
164 else {
165 $results[$i]['title'] = stripBBCode($results[$i]['title']);
166 $results[$i]['text'] = stripBBCode($results[$i]['text']);
167 }
168
169 //if ($results !== false) data_dump($results, false, "Search results");
170 //else echo "<br><b>No results.</b>";
171 break;
172
173 case 6: // Search posts
174 $results = db()->fetchAll("SELECT `thread_id`, `player_name`, `text` FROM `znote_forum_posts` ORDER BY `id` DESC LIMIT ?;", [(int)$searchResults]);
175 if (!is_array($results)) $results = array();
176 // Missing ['forum_id'], ['title'], lets get them
177 for ($i = 0; $i < count($results); $i++) {
178 // $results[$i]['asd']
179 $thread = db()->fetchOne("SELECT `forum_id`, `title` FROM `znote_forum_threads` WHERE `id` = ? LIMIT 1;", [$results[$i]['thread_id']]);
180 if ($thread !== false) {
181 $results[$i]['forum_id'] = $thread['forum_id'];
182 $results[$i]['title'] = $thread['title'];
183 if (!in_array($results[$i]['forum_id'], $allowedForums)) $results[$i]['forum_id'] = false;
184 else {
185 $results[$i]['title'] = stripBBCode($results[$i]['title']);
186 $results[$i]['text'] = stripBBCode($results[$i]['text']);
187 }
188 } else $results[$i]['forum_id'] = false;
189
190 } // DONE. :)
191 //data_dump(false, $results, "DATA");
192 break;
193 default:
194 # code...
195 break;
196 }
197
198 // Create table and show stuff!
199 if ($results !== false) {
200 $count = 0;
201 foreach ($results as $r) if ($r['forum_id'] !== false) $count++;
202 if ($count > 0) {
203 ?>
204 <table class="znoteTable" id="forumThreadTable">
205 <tr class="yellow">
206 <th><?= t('fsearch.col_char') ?></th>
207 <th><?= t('fsearch.col_thread') ?></th>
208 <th><?= t('fsearch.col_post') ?></th>
209 </tr>
210 <?php
211 foreach ($results as $result) {
212 if ($result['forum_id'] !== false) {
213 // $result required fields = ['thread_id'], ['forum_id'], ['title'], ['text'], ['player_name']
214 ?>
215 <tr>
216 <td><a href="characterprofile.php?name=<?php echo $result['player_name']; ?>"><?php echo $result['player_name']; ?></a></td>
217 <td>
218 <a href="forum.php?thread=<?php echo $result['thread_id']; ?>&forum=Search&cat=<?php echo $result['forum_id']; ?>">
219 <?php echo $result['title']; ?>
220 </a>
221 </td>
222 <td><?php echo (strlen($result['text']) > 140) ? substr($result['text'],0,137).'...' : $result['text']; ?></td>
223 </tr>
224 <?php
225 }
226 }
227 ?>
228 </table>
229 <?php
230 } else echo t('fsearch.no_results');
231 } else echo t('fsearch.no_results');
232} else echo "<br><b>". t('fsearch.fill_all2'). "</b>";
233
234echo '</div>';
235theme_close();
236?>
237
Top