-
Notifications
You must be signed in to change notification settings - Fork 8
/
theme.php
421 lines (358 loc) · 16.5 KB
/
theme.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
require 'info.php';
require_once 'inc/anti-bot.php'; // DELETE ME THIS IS FOR print_err function only!
function catalog_build($action, $settings, $board) {
global $config;
print_err("catalog_build");
$b = new Catalog($settings);
$boards = explode(' ', $settings['boards']);
// Possible values for $action:
// - all (rebuild everything, initialization)
// - news (news has been updated)
// - boards (board list changed)
// - post (a reply has been made)
// - post-thread (a thread has been made)
if ($action === 'all') {
foreach ($boards as $board) {
$b = new Catalog($settings);
$action = generation_strategy("sb_catalog", array($board));
if ($action == 'delete') {
file_unlink($config['dir']['home'] . $board . '/catalog.html');
file_unlink($config['dir']['home'] . $board . '/index.rss');
}
elseif ($action == 'rebuild') {
print_err("catalog_build calling Catalog.build 1. board: $board");
$b->build($settings, $board);
}
}
} elseif ($action == 'post-thread' || ($settings['update_on_posts'] && $action == 'post') || ($settings['update_on_posts'] && $action == 'post-delete')
|| $action == 'sticky' || ($action == 'lock' && in_array($board, $boards))) {
$b = new Catalog($settings);
$action = generation_strategy("sb_catalog", array($board));
if ($action == 'delete') {
file_unlink($config['dir']['home'] . $board . '/catalog.html');
file_unlink($config['dir']['home'] . $board . '/index.rss');
}
elseif ($action == 'rebuild') {
print_err("catalog_build calling Catalog.build 2");
$b->build($settings, $board);
}
}
// FIXME: Check that Ukko is actually enabled
if ($settings['enable_ukko'] && (
$action === 'all' || $action === 'post' ||
$action === 'post-thread' || $action === 'post-delete' || $action === 'rebuild'))
{
$b->buildUkko();
}
// FIXME: Check that Ukko2 is actually enabled
if ($settings['enable_ukko2'] && (
$action === 'all' || $action === 'post' ||
$action === 'post-thread' || $action === 'post-delete' || $action === 'rebuild'))
{
$b->buildUkko2();
}
// FIXME: Check that Ukko3 is actually enabled
if ($settings['enable_ukko3'] && (
$action === 'all' || $action === 'post' ||
$action === 'post-thread' || $action === 'post-delete' || $action === 'rebuild'))
{
$b->buildUkko3();
}
// FIXME: Check that Ukko3 is actually enabled
if ($settings['enable_ukko4'] && (
$action === 'all' || $action === 'post' ||
$action === 'post-thread' || $action === 'post-delete' || $action === 'rebuild'))
{
$b->buildUkko4();
}
// FIXME: Check that Rand is actually enabled
if ($settings['enable_rand'] && (
$action === 'all' || $action === 'post' ||
$action === 'post-thread' || $action === 'post-delete' || $action === 'rebuild'))
{
$b->buildRand();
}
}
// Wrap functions in a class so they don't interfere with normal Tinyboard operations
class Catalog {
private $settings;
// Cache for threads from boards that have already been processed
private $threadsCache = array();
public function __construct($settings) {
$this->settings = $settings;
}
/**
* Build and save the HTML of the catalog for the Ukko theme
*/
public function buildUkko() {
global $config;
$ukkoSettings = themeSettings('ukko');
$queries = array();
$threads = array();
$exclusions = explode(' ', $ukkoSettings['exclude']);
$boards = array_diff(listBoards(true), $exclusions);
foreach ($boards as $b) {
if (array_key_exists($b, $this->threadsCache)) {
$threads = array_merge($threads, $this->threadsCache[$b]);
} else {
$queries[] = $this->buildThreadsQuery($b);
}
}
// Fetch threads from boards that haven't beenp processed yet
if (!empty($queries)) {
$sql = implode(' UNION ALL ', $queries);
$res = query($sql) or error(db_error());
$threads = array_merge($threads, $res->fetchAll(PDO::FETCH_ASSOC));
}
// Sort in bump order
usort($threads, function($a, $b) {
return strcmp($b['bump'], $a['bump']);
});
// Generate data for the template
$recent_posts = $this->generateRecentPosts($threads);
$this->saveForBoard($ukkoSettings['uri'], $recent_posts,
$config['root'] . $ukkoSettings['uri']);
}
/**
* Build and save the HTML of the catalog for the Ukko2 theme
*/
public function buildUkko2() {
global $config;
print_err("Catalog.buildUkko2");
$ukkoSettings = themeSettings('ukko2');
$queries = array();
$threads = array();
$inclusions = explode(' ', $ukkoSettings['include']);
$boards = array_intersect(listBoards(true), $inclusions);
foreach ($boards as $b) {
if (array_key_exists($b, $this->threadsCache)) {
$threads = array_merge($threads, $this->threadsCache[$b]);
} else {
$queries[] = $this->buildThreadsQuery($b);
}
}
// Fetch threads from boards that haven't beenp processed yet
if (!empty($queries)) {
$sql = implode(' UNION ALL ', $queries);
$res = query($sql) or error(db_error());
$threads = array_merge($threads, $res->fetchAll(PDO::FETCH_ASSOC));
}
// Sort in bump order
usort($threads, function($a, $b) {
return strcmp($b['bump'], $a['bump']);
});
// Generate data for the template
$recent_posts = $this->generateRecentPosts($threads);
$this->saveForBoard($ukkoSettings['uri'], $recent_posts,
$config['root'] . $ukkoSettings['uri']);
}
/**
* Build and save the HTML of the catalog for the Ukko3 theme
*/
public function buildUkko3() {
global $config;
print_err("Catalog.buildUkko3");
$ukkoSettings = themeSettings('ukko3');
$queries = array();
$threads = array();
$inclusions = explode(' ', $ukkoSettings['include']);
$boards = array_intersect(listBoards(true), $inclusions);
foreach ($boards as $b) {
if (array_key_exists($b, $this->threadsCache)) {
$threads = array_merge($threads, $this->threadsCache[$b]);
} else {
$queries[] = $this->buildThreadsQuery($b);
}
}
// Fetch threads from boards that haven't beenp processed yet
if (!empty($queries)) {
$sql = implode(' UNION ALL ', $queries);
$res = query($sql) or error(db_error());
$threads = array_merge($threads, $res->fetchAll(PDO::FETCH_ASSOC));
}
// Sort in bump order
usort($threads, function($a, $b) {
return strcmp($b['bump'], $a['bump']);
});
// Generate data for the template
$recent_posts = $this->generateRecentPosts($threads);
$this->saveForBoard($ukkoSettings['uri'], $recent_posts,
$config['root'] . $ukkoSettings['uri']);
}
/**
* Build and save the HTML of the catalog for the Ukko theme
*/
public function buildUkko4() {
global $config;
print_err("Catalog.buildUkko4");
$ukkoSettings = themeSettings('ukko4');
$queries = array();
$threads = array();
$exclusions = explode(' ', $ukkoSettings['exclude']);
$boards = array_diff(listBoards(true), $exclusions);
foreach ($boards as $b) {
if (array_key_exists($b, $this->threadsCache)) {
$threads = array_merge($threads, $this->threadsCache[$b]);
} else {
$queries[] = $this->buildThreadsQuery($b);
}
}
// Fetch threads from boards that haven't beenp processed yet
if (!empty($queries)) {
$sql = implode(' UNION ALL ', $queries);
$res = query($sql) or error(db_error());
$threads = array_merge($threads, $res->fetchAll(PDO::FETCH_ASSOC));
}
// Sort in bump order
usort($threads, function($a, $b) {
return strcmp($b['bump'], $a['bump']);
});
// Generate data for the template
$recent_posts = $this->generateRecentPosts($threads);
$this->saveForBoard($ukkoSettings['uri'], $recent_posts,
$config['root'] . $ukkoSettings['uri']);
}
/**
* Build and save the HTML of the catalog for the Rand theme
*/
public function buildRand() {
global $config;
print_err("Catalog.buildRand");
$randSettings = themeSettings('rand');
$queries = array();
$threads = array();
$exclusions = explode(' ', $randSettings['exclude']);
$boards = array_diff(listBoards(true), $exclusions);
foreach ($boards as $b) {
if (array_key_exists($b, $this->threadsCache)) {
$threads = array_merge($threads, $this->threadsCache[$b]);
} else {
$queries[] = $this->buildThreadsQuery($b);
}
}
// Fetch threads from boards that haven't beenp processed yet
if (!empty($queries)) {
$sql = implode(' UNION ALL ', $queries);
$res = query($sql) or error(db_error());
$threads = array_merge($threads, $res->fetchAll(PDO::FETCH_ASSOC));
}
// Sort in bump order
usort($threads, function($a, $b) {
return strcmp($b['bump'], $a['bump']);
});
// Generate data for the template
$recent_posts = $this->generateRecentPosts($threads);
$this->saveForBoard($randSettings['uri'], $recent_posts,
$config['root'] . $randSettings['uri']);
}
/**
* Build and save the HTML of the catalog for the given board
*/
public function build($settings, $board_name) {
global $config, $board;
print_err("Catalog.build");
if ($board['uri'] != $board_name) {
if (!openBoard($board_name)) {
error(sprintf(_("Board %s doesn't exist"), $board_name));
}
}
print_err("Catalog.build 1");
if (array_key_exists($board_name, $this->threadsCache)) {
$threads = $this->threadsCache[$board_name];
} else {
print_err("Catalog.build calling buildThreadsQuery. boardname: $board_name");
$sql = $this->buildThreadsQuery($board_name);
print_err("Catalog.build calling buildThreadsQuery ok");
$query = query($sql . ' ORDER BY `sticky` DESC,`bump` DESC') or error(db_error());
$threads = $query->fetchAll(PDO::FETCH_ASSOC);
print_err("Catalog.build has threads");
// Save for posterity
$this->threadsCache[$board_name] = $threads;
}
print_err("Catalog.build 2");
// Generate data for the template
$recent_posts = $this->generateRecentPosts($threads);
print_err("Catalog.build 3");
$this->saveForBoard($board_name, $recent_posts);
print_err("Catalog.build 4");
}
private function buildThreadsQuery($board) {
$sql = "SELECT *, `id` AS `thread_id`, " .
"(SELECT COUNT(`id`) FROM ``posts_$board`` WHERE `thread` = `thread_id`) AS `reply_count`, " .
"(SELECT SUM(`num_files`) FROM ``posts_$board`` WHERE `thread` = `thread_id` AND `num_files` IS NOT NULL) AS `image_count`, " .
"'$board' AS `board` FROM ``posts_$board`` WHERE `thread` IS NULL";
return $sql;
}
private function generateRecentPosts($threads) {
global $config, $board;
$posts = array();
foreach ($threads as $post) {
if ($board['uri'] !== $post['board']) {
openBoard($post['board']);
}
$post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . link_for($post);
$post['board_name'] = $board['name'];
if ($post['embed'] && preg_match('/^https?:\/\/(\w+\.)?(?:youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9\-_]{10,11})(&.+)?$/i', $post['embed'], $matches)) {
$post['youtube'] = $matches[2];
}
if (isset($post['files']) && $post['files']) {
$files = json_decode($post['files']);
if ($files[0]) {
if ($files[0]->file == 'deleted') {
if (count($files) > 1) {
foreach ($files as $file) {
if (($file == $files[0]) || ($file->file == 'deleted'))
continue;
$post['file'] = $config['uri_thumb'] . $file->thumb;
}
if (empty($post['file']))
$post['file'] = $config['root'] . $config['image_deleted'];
} else {
$post['file'] = $config['root'] . $config['image_deleted'];
}
} else if($files[0]->thumb == 'spoiler') {
$post['file'] = $config['root'] . $config['spoiler_image'];
} else {
$post['file'] = $config['uri_thumb'] . $files[0]->thumb;
}
}
} else {
$post['file'] = $config['root'] . $config['image_deleted'];
}
if (empty($post['image_count']))
$post['image_count'] = 0;
$post['pubdate'] = date('r', $post['time']);
$posts[] = $post;
}
return $posts;
}
private function saveForBoard($board_name, $recent_posts, $board_link = null) {
global $board, $config;
if ($board_link === null) {
$board_link = $config['root'] . $board['dir'];
}
$required_scripts = array('js/jquery.min.js', 'js/jquery.mixitup.min.js',
'js/catalog.js');
// Include scripts that haven't been yet included
foreach($required_scripts as $i => $s) {
if (!in_array($s, $config['additional_javascript']))
$config['additional_javascript'][] = $s;
}
file_write($config['dir']['home'] . $board_name . '/catalog.html', Element('themes/catalog/catalog.html', Array(
'settings' => $this->settings,
'config' => $config,
'boardlist' => createBoardlist(),
'recent_images' => array(),
'recent_posts' => $recent_posts,
'stats' => array(),
'board' => $board_name,
'link' => $board_link
)));
file_write($config['dir']['home'] . $board_name . '/index.rss', Element('themes/catalog/index.rss', Array(
'config' => $config,
'recent_posts' => $recent_posts,
'board' => $board
)));
}
}