-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.php
239 lines (216 loc) · 6.17 KB
/
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
$noAutoHeader = TRUE;
include("lib/common.php");
if(isset($_POST['google']))
{
$full = GetFullURL();
$here = substr($full, 0, strrpos($full, "/"));
header("Location: http://www.google.com/search?q=".urlencode($_POST['google']." site:".$here));
}
include("lib/header.php");
AssertForbidden("search");
write("
<div style=\"float: left; width: 70%;\">
<form action=\"search.php\" method=\"post\">
<div class=\"outline PoRT margin width25\" style=\"margin: 16px; width: 100%; float: none;\">
<div class=\"errort\">
<strong>".__("Google search")."</strong>
</div>
<div class=\"errorc left cell0\" style=\"padding: 8px; font-size: 150%\">
<input type=\"text\" maxlength=\"1024\" name=\"google\" style=\"width: 80%;\">
<input type=\"submit\" value=\"".__("Search")."\">
</div>
</div>
</form>
");
if($loguser['powerlevel'] < 1) die("</div>");
write("
<form action=\"search.php\" method=\"get\">
<div class=\"outline PoRT margin\" style=\"margin: 16px; width: 100%; float: none;\">
<div class=\"errort\">
<strong>".__("Internal search")."</strong>
</div>
<div class=\"errorc left cell0\" style=\"padding: 8px; font-size: 150%\">
<input type=\"text\" maxlength=\"1024\" name=\"q\" style=\"width: 80%;\" value=\"".htmlspecialchars($_GET['q'])."\">
<input type=\"submit\" value=\"".__("Search")."\">
</div>
</div>
</form>
</div>
<div class=\"PoRT margin width25\">
<div class=\"errort\"><strong>".__("Search help")."</strong></div>
<div class=\"errorc left cell0\" style=\"padding: 8px 8px;\">
".__("Internal search checks both thread titles and post text, returning results from both.")."
<dl>
<dt><samp>foo bar</samp></dt>
<dd>".__("Find entries with either term")."</dd>
<dt><samp>\"foo bar\"</samp></dt>
<dd>".__("Find entries with full phrase")."</dd>
<dt><samp>+foo -bar</samp></dt>
<dd>".__("Find entries with <var>foo</var> but not <var>bar</var>")."</dd>
</dl>
</div>
</div>
<hr style=\"clear: both; visibility: hidden;\" />
");
if(isset($_GET['q']))
{
$totalResults = 0;
$bool = htmlspecialchars($_GET['q']);
$t = explode(" ", $bool);
$terms = array();
foreach($t as $term)
{
if($term[0] == "-")
continue;
if($term[0] == "+" || $term[0] == "\"")
$terms[] = substr($term, 1);
else if($term[strlen($term)-1] == "*" || $term[strlen($term)-1] == "\"")
$terms[] = substr($term, 0, strlen($term) - 1);
else if($term != "")
$terms[] = $term;
}
$final = "";
$search = Query("SELECT `threads`.`title`, `threads`.`user`, `name`, `displayname`, `sex`, `powerlevel` FROM `threads` LEFT JOIN `users` ON `users`.`id`=`threads`.`user` WHERE MATCH(`threads`.`title`) AGAINST('".$bool."' IN BOOLEAN MODE) ORDER BY `threads`.`lastpostdate` DESC LIMIT 0,100");
if(NumRows($search))
{
$results = "";
while($result = Fetch($search))
{
$snippet = MakeSnippet(htmlspecialchars($result['title']), $terms, true);
if($snippet != "")
$results .= Format(
"
<tr class=\"cell0\">
<td class=\"smallFonts\">
{2}
</td>
<td>
<a href=\"./?tid={1}\">{0}</a>
</td>
</tr>
", $snippet, $result['id'], UserLink($result, "user"), $tags);
}
if($results != "")
{
$final .= Format(
"
<table class=\"outline margin\">
<tr class=\"header0\">
<th colspan=\"4\">Thread title results</th>
</tr>
<tr class=\"header1\">
<th>User</th>
<th>Thread</th>
</tr>
{0}
</table>
", $results);
$totalResults += NumRows($search);
}
}
$search = Query("SELECT `text`, `pid`, `threads`.`title`, `thread`, `posts`.`user`, `name`, `displayname`, `sex`, `powerlevel` FROM `posts_text` LEFT JOIN `posts` ON `posts_text`.`pid`=`posts`.`id` LEFT JOIN `threads` ON `threads`.`id`=`posts`.`thread` LEFT JOIN `users` ON `users`.`id`=`posts`.`user` WHERE `posts_text`.`revision`=`posts`.`currentrevision` AND MATCH(`text`) AGAINST('".$bool."' IN BOOLEAN MODE) ORDER BY `posts`.`date` DESC LIMIT 0,100");
if(NumRows($search))
{
$results = "";
while($result = Fetch($search))
{
$result['text'] = str_replace("<!--", "~#~", str_replace("-->", "~#~", $result['text']));
$snippet = MakeSnippet(htmlspecialchars($result['text']), $terms);
if($snippet != "")
$results .= Format(
"
<tr class=\"cell0\">
<td class=\"smallFonts\">
{3}
</td>
<td>
{0}
</td>
<td class=\"smallFonts\">
<a href=\"./?tid={4}\">{2}</a>
</td>
<td class=\"smallFonts\">
» <a href=\"./?pid={1}\">{1}</a>
</td>
</tr>
", $snippet, $result['pid'], $result['title'], UserLink($result, "user"), $result['thread']);
}
if($results != "")
{
$final .= Format(
"
<table class=\"outline margin\">
<tr class=\"header0\">
<th colspan=\"4\">Text results</th>
</tr>
<tr class=\"header1\">
<th>User</th>
<th>Text</th>
<th>Thread</th>
<th>ID</th>
</tr>
{0}
</table>
", $results);
$totalResults += NumRows($search);
}
}
if($totalResults == 0)
Alert(Format("No results for \"{0}\".", htmlspecialchars($_GET['q'])), "Search");
else
Write("
<div class=\"outline header2 cell2 margin\" style=\"text-align: center; font-size: 130%;\">
{0}
</div>
{1}
", Plural($totalResults, "result"), $final);
}
function MakeSnippet($text, $terms, $title = false)
{
$text = strip_tags($text);
if(!$title)
$text = preg_replace("/(\[\/?)(\w+)([^\]]*\])/i", "", $text);
$lines = explode("\n", $text);
$terms = implode("|", $terms);
$contextlines = 3;
$max = 50;
$pat1 = "/(.*)(".$terms.")(.{0,".$max."})/i";
$lineno = 0;
$extract = "";
foreach($lines as $line)
{
if($contextlines == 0)
break;
$lineno++;
if($title)
$line = htmlspecialchars($line);
else
{
$m = array();
if(!preg_match($pat1, $line, $m))
continue;
$contextlines--;
$pre = substr($m[1], -$max);
if(count($m) < 3)
$post = "";
else
$post = $m[3];
$found = $m[2];
$line = htmlspecialchars($pre.$found.$post);
}
$line = trim($line);
if($line == "")
continue;
$pat2 = "/(".$terms.")/i";
$line = preg_replace($pat2, "<strong style=\"text-shadow: 0px 0px 2px white\">\\1</strong>", $line);
$line = preg_replace("/\~#\~(.*?)\~#\~/", "<span style=\"color: #6f6;\"><!--\\1--></span>", $line);
if(!$title)
$extract .= "• ".$line."<br />";
else
$extract .= $line;
}
return $extract;
}