-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsearch.php
More file actions
206 lines (169 loc) · 6.62 KB
/
search.php
File metadata and controls
206 lines (169 loc) · 6.62 KB
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
<?php
require 'inc/bootstrap.php';
/**
* @var array $config
*/
if (!$config['search']['enable']) {
die(_("Post search is disabled"));
}
$queries_per_minutes = $config['search']['queries_per_minutes'];
$queries_per_minutes_all = $config['search']['queries_per_minutes_all'];
$search_limit = $config['search']['search_limit'];
if (isset($config['search']['boards'])) {
$boards = $config['search']['boards'];
} else {
$boards = listBoards(true);
}
$body = Element(
'search_form.html',
array(
'boards' => $boards,
'board' => isset($_GET['board']) ? $_GET['board'] : false,
'search' => isset($_GET['search']) ? str_replace('"', '"', utf8tohtml($_GET['search'])) : false
)
);
if (isset($_GET['search']) && !empty($_GET['search']) && isset($_GET['board']) && in_array($_GET['board'], $boards)) {
$phrase = $_GET['search'];
$_body = '';
$query = prepare("SELECT COUNT(*) FROM ``search_queries`` WHERE `ip` = :ip AND `time` > :time");
$query->bindValue(':ip', get_ip_hash($_SERVER['REMOTE_ADDR']));
$query->bindValue(':time', time() - ($queries_per_minutes[1] * 60));
$query->execute() or error(db_error($query));
if ($query->fetchColumn() > $queries_per_minutes[0]) {
error(_('Wait a while before searching again, please.'));
}
$query = prepare("SELECT COUNT(*) FROM ``search_queries`` WHERE `time` > :time");
$query->bindValue(':time', time() - ($queries_per_minutes_all[1] * 60));
$query->execute() or error(db_error($query));
if ($query->fetchColumn() > $queries_per_minutes_all[0]) {
error(_('Wait a while before searching again, please.'));
}
$query = prepare("INSERT INTO ``search_queries`` VALUES (:ip, :time, :query)");
$query->bindValue(':ip', get_ip_hash($_SERVER['REMOTE_ADDR']));
$query->bindValue(':time', time());
$query->bindValue(':query', $phrase);
$query->execute() or error(db_error($query));
_syslog(LOG_NOTICE, 'Searched /' . $_GET['board'] . '/ for "' . $phrase . '"');
// Cleanup search queries table
$query = prepare("DELETE FROM ``search_queries`` WHERE `time` <= :time");
$query->bindValue(':time', time() - ($queries_per_minutes_all[1] * 60));
$query->execute() or error(db_error($query));
openBoard($_GET['board']);
$filters = array();
function search_filters($m)
{
global $filters;
$name = $m[2];
$value = isset($m[4]) ? $m[4] : $m[3];
if (!in_array($name, array('id', 'thread', 'subject', 'name'))) {
// unknown filter
return $m[0];
}
$filters[$name] = $value;
return $m[1];
}
$phrase = trim(preg_replace_callback('/(^|\s)(\w+):("(.*)?"|[^\s]*)/', 'search_filters', $phrase));
if (!preg_match('/[^*^\s]/', $phrase) && empty($filters)) {
_syslog(LOG_WARNING, 'Query too broad.');
$body .= '<div class="post reply search-no-results">' . _('Query too broad. Be more specific.') . '</div>';
echo Element($config['file_page_template'], array(
'config' => $config,
'title' => _('Search'),
'body' => $body,
));
exit;
}
// Escape escape character
$phrase = str_replace('!', '!!', $phrase);
// Remove SQL wildcard
$phrase = str_replace('%', '!%', $phrase);
// Use asterisk as wildcard to suit convention
$phrase = str_replace('*', '%', $phrase);
// Remove `, it's used by table prefix magic
$phrase = str_replace('`', '!`', $phrase);
$like = '';
$match = array();
// Find exact phrases
if (preg_match_all('/"(.+?)"/', $phrase, $m)) {
foreach ($m[1] as &$quote) {
$phrase = str_replace("\"{$quote}\"", '', $phrase);
$match[] = $pdo->quote($quote);
}
}
$words = explode(' ', $phrase);
foreach ($words as &$word) {
if (empty($word)) {
continue;
}
$match[] = $pdo->quote($word);
}
$like = '';
foreach ($match as &$phrase) {
if (!empty($like)) {
$like .= ' AND ';
}
$phrase = preg_replace('/^\'(.+)\'$/', '\'%$1%\'', $phrase);
$like .= '`body` LIKE ' . $phrase . ' ESCAPE \'!\'';
}
foreach ($filters as $name => $value) {
if (!empty($like)) {
$like .= ' AND ';
}
$like .= '`' . $name . '` = ' . $pdo->quote($value);
}
$like = str_replace('%', '%%', $like);
$query = prepare(
sprintf("SELECT * FROM ``posts_%s`` WHERE " . $like . " ORDER BY `time` DESC LIMIT :limit", $board['uri'])
);
$query->bindValue(':limit', $search_limit, PDO::PARAM_INT);
$query->execute() or error(db_error($query));
if ($query->rowCount() == $search_limit) {
_syslog(LOG_WARNING, 'Query too broad.');
$body .= '<div class="post reply search-no-results">' . sprintf(_('Too many results (limit: %d). Try a more specific query.'), $search_limit) . '</div>';
echo Element($config['file_page_template'], array(
'config' => $config,
'title' => _('Search'),
'body' => $body,
));
exit;
}
$resultCount = $query->rowCount();
$temp = '';
while ($post = $query->fetch()) {
if (!$post['thread']) {
$po = new Thread($post);
} else {
$po = new Post($post);
}
$temp .= '<div class="post reply search-result-item">' . $po->build(true) . '</div>';
}
if (!empty($temp)) {
$_body .= '<div class="search-results">';
$_body .= '<div class="search-results-header-wrap">';
$_body .= '<div class="search-results-header">';
$_body .= sprintf(
ngettext('%d result in', '%d results in', $resultCount),
$resultCount
);
$_body .= ' <a href="/' . sprintf($config['board_path'], $board['uri']) . $config['file_index'] . '">';
$_body .= sprintf($config['board_abbreviation'], $board['uri']) . ' - ' . $board['title'];
$_body .= '</a>';
$_body .= '</div></div>';
$_body .= '<div class="search-results-body">';
$_body .= $temp;
$_body .= '</div>';
$_body .= '</div>';
}
if (!empty($_body)) {
$body .= $_body;
} else {
$body .= '<div class="post reply search-no-results">' . _('No results found.') . '</div>';
}
}
echo Element($config['file_page_template'], array(
'config' => $config,
'title' => _('Search'),
'subtitle' => _('Find your thread'),
'boardlist' => createBoardlist(),
'body' => '' . $body
));