-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathforum_search.php
224 lines (202 loc) · 8.68 KB
/
forum_search.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php require_once 'engine/init.php'; include 'layout/overall/header.php'; protect_page();
// Search CONFIG
$searchResults = 30; // How many max search results
/* Below HTML CODE = Plugin you can put anywhere in Znote AAC where you want search.
<h1>Search forum</h1>
<form action="forum_search.php" method="get">
<select name="type">
<option value="1">Title</option>
<option value="2">Post</option>
<option value="3">Author (threads)</option>
<option value="4">Author (posts)</option>
<option value="5">Latest Posts</option>
<option value="6">Latest Threads</option>
</select>
<input type="text" name="text" placeholder="Search string">
<input type="submit" value="Search">
</form>
*/
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']);
if ($type !== false) $type = (int)$type;
$text = getvalue($_GET['text']);
$textTitleSql = "";
$textPostSql = "";
$textAuthorSql = "";
if ($text !== false) {
$text = explode(' ', $text);
for ($i = 0; $i < count($text); $i++) {
if ($i != count($text) -1) {
$textTitleSql .= "`title` LIKE '%". $text[$i] ."%' AND ";
$textPostSql .= "`text` LIKE '%". $text[$i] ."%' AND ";
$textAuthorSql .= "`player_name` LIKE '%". $text[$i] ."%' AND ";
} else {
$textTitleSql .= "`title` LIKE '%". $text[$i] ."%'";
$textPostSql .= "`text` LIKE '%". $text[$i] ."%'";
$textAuthorSql .= "`player_name` LIKE '%". $text[$i] ."%'";
}
}
//data_dump($text, array($textTitleSql, $textPostSql, $textAuthorSql), "search");
}
?>
<h1>Search forum</h1>
<form method="" type="get">
<select name="type">
<option value="1" <?php if ($type == 1) echo "selected"; ?>>Title</option>
<option value="2" <?php if ($type == 2) echo "selected"; ?>>Post</option>
<option value="3" <?php if ($type == 3) echo "selected"; ?>>Author (threads)</option>
<option value="4" <?php if ($type == 4) echo "selected"; ?>>Author (posts)</option>
<option value="5" <?php if ($type == 5) echo "selected"; ?>>Latest Posts</option>
<option value="6" <?php if ($type == 6) echo "selected"; ?>>Latest Threads</option>
</select>
<input type="text" name="text" value="<?php if ($text !== false) echo implode(' ', $text); ?>">
<input type="submit" value="Search">
</form>
<?php
if ($type !== false && $text !== false && $type <= 4 || $type > 4 && $type <= 6) {
$forums = mysql_select_multi("SELECT `id` FROM `znote_forum` WHERE `access`='1' AND `guild_id`='0';");
$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 = mysql_select_multi("SELECT `id` AS `thread_id`, `forum_id`, `title`, `text`, `player_name` FROM `znote_forum_threads` WHERE $textTitleSql ORDER BY `id` DESC LIMIT $searchResults;");
// 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 "<br><b>No results.</b>";
break;
case 2: // Search posts
$results = mysql_select_multi("SELECT `thread_id`, `player_name`, `text` FROM `znote_forum_posts` WHERE $textPostSql ORDER BY `id` DESC LIMIT $searchResults;");
// Missing ['forum_id'], ['title'], lets get them
for ($i = 0; $i < count($results); $i++) {
// $results[$i]['asd']
$thread = mysql_select_single("SELECT `forum_id`, `title` FROM `znote_forum_threads` WHERE `id`='".$results[$i]['thread_id']."' LIMIT 1;");
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 = mysql_select_multi("SELECT `id` AS `thread_id`, `forum_id`, `title`, `text`, `player_name` FROM `znote_forum_threads` WHERE $textAuthorSql ORDER BY `id` DESC LIMIT $searchResults;");
// 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 "<br><b>No results.</b>";
break;
case 4: // Search authors last posts
$results = mysql_select_multi("SELECT `thread_id`, `player_name`, `text` FROM `znote_forum_posts` WHERE $textAuthorSql ORDER BY `id` DESC LIMIT $searchResults;");
// Missing ['forum_id'], ['title'], lets get them
for ($i = 0; $i < count($results); $i++) {
// $results[$i]['asd']
$thread = mysql_select_single("SELECT `forum_id`, `title` FROM `znote_forum_threads` WHERE `id`='".$results[$i]['thread_id']."' LIMIT 1;");
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 = mysql_select_multi("SELECT `id` AS `thread_id`, `forum_id`, `title`, `text`, `player_name` FROM `znote_forum_threads` ORDER BY `id` DESC LIMIT $searchResults;");
// 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 "<br><b>No results.</b>";
break;
case 6: // Search posts
$results = mysql_select_multi("SELECT `thread_id`, `player_name`, `text` FROM `znote_forum_posts` ORDER BY `id` DESC LIMIT $searchResults;");
// Missing ['forum_id'], ['title'], lets get them
for ($i = 0; $i < count($results); $i++) {
// $results[$i]['asd']
$thread = mysql_select_single("SELECT `forum_id`, `title` FROM `znote_forum_threads` WHERE `id`='".$results[$i]['thread_id']."' LIMIT 1;");
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) {
?>
<table>
<tr>
<th>Char</th>
<th>Thread</th>
<th>Post</th>
</tr>
<?php
foreach ($results as $result) {
if ($result['forum_id'] !== false) {
// $result required fields = ['thread_id'], ['forum_id'], ['title'], ['text'], ['player_name']
?>
<tr>
<td><a href="characterprofile.php?name=<?php echo $result['player_name']; ?>"><?php echo $result['player_name']; ?></a></td>
<td>
<a href="forum.php?thread=<?php echo $result['thread_id']; ?>&forum=Search&cat=<?php echo $result['forum_id']; ?>">
<?php echo $result['title']; ?>
</a>
</td>
<td><?php echo (strlen($result['text']) > 140) ? substr($result['text'],0,137).'...' : $result['text']; ?></td>
</tr>
<?php
}
}
?>
</table>
<?php
} else echo "No results.";
} else echo "No results.";
} else echo "<br><b>You must fill in all fields!</b>";
include 'layout/overall/footer.php';
?>