forked from ctrlcctrlv/infinity
-
Notifications
You must be signed in to change notification settings - Fork 3
/
board-search.php
executable file
·329 lines (268 loc) · 9.16 KB
/
board-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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
// We want to return a value if we're included.
// Otherwise, we will be printing a JSON object-array.
$Included = defined("TINYBOARD");
if (!$Included) {
include "inc/functions.php";
}
$CanViewUnindexed = isset($mod["type"]) && $mod["type"] <= GlobalVolunteer;
/* The expected output of this page is JSON. */
$response = array();
/* Determine search parameters from $_GET */
$search = array(
'lang' => false,
'nsfw' => true,
'page' => 0,
'tags' => false,
'time' => ( (int)( time() / 3600 ) * 3600 ) - 3600,
'title' => false,
'index' => count( $_GET ) == 0,
);
// Include NSFW boards?
if (isset( $_GET['sfw'] ) && $_GET['sfw'] != "") {
$search['nsfw'] = !$_GET['sfw'];
}
// Bringing up more results
if (isset( $_GET['page'] ) && $_GET['page'] != "") {
$search['page'] = (int) $_GET['page'];
if ($search['page'] < 0) {
$search['page'] = 0;
}
}
// Include what language (if the language is not blank and we recognize it)?
if (isset( $_GET['lang'] ) && $_GET['lang'] != "" && isset($config['languages'][$_GET['lang']])) {
$search['lang'] = $_GET['lang'];
}
// Include what tag?
if (isset( $_GET['tags'] ) && $_GET['tags'] != "") {
if (!is_array($_GET['tags'])) {
$search['tags'] = explode( " ", (string) $_GET['tags'] );
}
else {
$search['tags'] = $_GET['tags'];
}
$search['tags'] = array_splice( $search['tags'], 0, 5 );
foreach ($search['tags'] as &$tag)
{
$tag = strtolower( $tag );
}
}
// What time range?
if (isset( $_GET['time'] ) && is_numeric( $_GET['time'] ) ) {
$search['time'] = ( (int)( $_GET['time'] / 3600 ) * 3600 );
}
// Include what in the uri / title / subtitle?
if (isset( $_GET['title'] ) && $_GET['title'] != "") {
$search['title'] = strtolower( $_GET['title'] );
}
/* Search boards */
$boards = listBoards();
$response['boards'] = array();
// Loop through our available boards and filter out inapplicable ones based on standard filtering.
foreach ($boards as $board) {
// Checks we can do without looking at config.
if (
// Indexed, or we are staff,
( $CanViewUnindexed !== true && !$board['indexed'] )
// Not filtering NSFW, or board is SFW.
|| ( $search['nsfw'] !== true && $board['sfw'] != 1 )
) {
continue;
}
// Are we searching by title?
if ($search['title'] !== false) {
// This checks each component of the board's identity against our search terms.
// The weight determines order.
// "left" would match /leftypol/ and /nkvd/ which has /leftypol/ in the title.
// /leftypol/ would always appear above it but it would match both.
if (strpos("/{$board['uri']}/", $search['title']) !== false) {
$board['weight'] = 30;
}
else if (strpos(strtolower($board['title']), $search['title']) !== false) {
$board['weight'] = 20;
}
else if (strpos(strtolower($board['subtitle']), $search['title']) !== false) {
$board['weight'] = 10;
}
else {
continue;
}
unset( $boardTitleString );
}
else {
$board['weight'] = 0;
}
// Load board config.
$boardConfig = loadBoardConfig( $board['uri'] );
// Determine language/locale and tags.
$boardLang = strtolower( array_slice( explode( "_", $boardConfig['locale'] ?: "" ), 0 )[0] ); // en_US -> en OR en -> en
// Check against our config search options.
if ($search['lang'] !== false && $search['lang'] != $boardLang) {
continue;
}
if (isset($config['languages'][$boardLang])) {
$board['locale'] = $config['languages'][$boardLang];
}
else {
$board['locale'] = $boardLang;
}
$response['boards'][ $board['uri'] ] = $board;
}
unset( $boards );
/* Tag Fetching */
// (We have do this even if we're not filtering by tags so that we know what each board's tags are)
// Fetch all board tags for our boards.
$boardTags = fetchBoardTags( array_keys( $response['boards'] ) );
// Loop through each board and determine if there are tag matches.
foreach ($response['boards'] as $boardUri => &$board) {
// If we are filtering by tag and there is no match, remove from the response.
if ( $search['tags'] !== false && ( !isset( $boardTags[ $boardUri ] ) || count(array_intersect($search['tags'], $boardTags[ $boardUri ])) !== count($search['tags']) ) ) {
unset( $response['boards'][$boardUri] );
continue;
}
// If we aren't filtering / there is a match AND we have tags, set the tags.
else if ( isset( $boardTags[ $boardUri ] ) && $boardTags[ $boardUri ] ) {
$board['tags'] = $boardTags[ $boardUri ];
}
// Othrwise, just declare our tag array blank.
else {
$board['tags'] = array();
}
// Legacy support for API readers.
$board['max'] = &$board['posts_total'];
}
unset( $boardTags );
/* Activity Fetching */
$boardActivity = fetchBoardActivity( array_keys( $response['boards'] ), $search['time'], true );
// Loop through each board and record activity to it.
// We will also be weighing and building a tag list.
foreach ($response['boards'] as $boardUri => &$board) {
$board['active'] = 0;
$board['pph'] = 0;
$board['ppd'] = 0;
if (isset($boardActivity['active'][ $boardUri ])) {
$board['active'] = (int) $boardActivity['active'][ $boardUri ];
}
if (isset($boardActivity['average'][ $boardUri ])) {
$precision = 0;
if ($boardActivity['average'][ $boardUri ] > 0 && $boardActivity['average'][ $boardUri ] < 10) {
$precision = 1;
}
$board['pph_average'] = round( $boardActivity['average'][ $boardUri ], $precision );
$board['pph'] = (int) $boardActivity['last'][ $boardUri ];
$board['ppd'] = round( $boardActivity['today'][ $boardUri ], $precision );
unset( $precision );
}
}
// Sort boards by their popularity, then by their total posts.
$boardActivityValues = array();
$boardTotalPostsValues = array();
$boardWeightValues = array();
foreach ($response['boards'] as $boardUri => &$board) {
$boardActivityValues[$boardUri] = (int) $board['active'];
$boardTotalPostsValues[$boardUri] = (int) $board['posts_total'];
$boardWeightValues[$boardUri] = (int) $board['weight'];
}
array_multisort(
$boardWeightValues, SORT_DESC, SORT_NUMERIC, // Sort by weight
$boardActivityValues, SORT_DESC, SORT_NUMERIC, // Sort by number of active posters
$boardTotalPostsValues, SORT_DESC, SORT_NUMERIC, // Then, sort by total number of posts
$response['boards']
);
if (php_sapi_name() == 'cli') {
$response['boardsFull'] = $response['boards'];
}
$boardLimit = $search['index'] ? 50 : 100;
$response['omitted'] = count( $response['boards'] ) - $boardLimit;
$response['omitted'] = $response['omitted'] < 0 ? 0 : $response['omitted'];
$response['boards'] = array_splice( $response['boards'], $search['page'], $boardLimit );
$response['order'] = array_keys( $response['boards'] );
// Loop through the truncated array to compile tags.
$response['tags'] = array();
$tagUsage = array( 'boards' => array(), 'users' => array() );
foreach ($response['boards'] as $boardUri => &$board) {
if (isset($board['tags']) && count($board['tags']) > 0) {
foreach ($board['tags'] as $tag) {
if (!isset($tagUsage['boards'][$tag])) {
$tagUsage['boards'][$tag] = 0;
}
if (!isset($tagUsage['users'][$tag])) {
$tagUsage['users'][$tag] = 0;
}
$response['tags'][$tag] = true;
++$tagUsage['boards'][$tag];
$tagUsage['users'][$tag] += $board['active'];
}
}
}
// Get the top most popular tags.
if (count($response['tags']) > 0) {
arsort( $tagUsage['boards'] );
arsort( $tagUsage['users'] );
array_multisort(
$tagUsage['boards'], SORT_DESC, SORT_NUMERIC,
$tagUsage['users'], SORT_DESC, SORT_NUMERIC,
$response['tags']
);
// Get the first n most active tags.
$response['tags'] = array_splice( $response['tags'], 0, 100 );
$response['tagOrder'] = array_keys( $response['tags'] );
$response['tagWeight'] = array();
$tagsMostUsers = max( $tagUsage['users'] );
$tagsLeastUsers = min( $tagUsage['users'] );
$tagsAvgUsers = array_sum( $tagUsage['users'] ) / count( $tagUsage['users'] );
$weightDepartureFurthest = 0;
foreach ($tagUsage['users'] as $tagUsers) {
$weightDeparture = abs( $tagUsers - $tagsAvgUsers );
if( $weightDeparture > $weightDepartureFurthest ) {
$weightDepartureFurthest = $weightDeparture;
}
}
foreach ($tagUsage['users'] as $tagName => $tagUsers) {
if ($weightDepartureFurthest != 0) {
$weightDeparture = abs( $tagUsers - $tagsAvgUsers );
$response['tagWeight'][$tagName] = 75 + round( 100 * ( $weightDeparture / $weightDepartureFurthest ), 0);
}
else {
$response['tagWeight'][$tagName] = 100;
}
}
}
/* Include our interpreted search terms. */
$response['search'] = $search;
/* (Please) Respond */
if (!$Included) {
$json = json_encode( $response );
// Error Handling
switch (json_last_error()) {
case JSON_ERROR_NONE:
$jsonError = false;
break;
case JSON_ERROR_DEPTH:
$jsonError = 'Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$jsonError = 'Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$jsonError = 'Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$jsonError = 'Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$jsonError = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
$jsonError = 'Unknown error';
break;
}
if ($jsonError) {
$json = "{\"error\":\"{$jsonError}\"}";
}
// Successful output
echo $json;
}
else {
return $response;
}