This commit is contained in:
towards_a_new_leftypol 2020-10-19 23:00:18 -04:00 committed by towards-a-new-leftypol
parent 1596ab3421
commit 740abd82d7
1 changed files with 351 additions and 345 deletions

View File

@ -1,406 +1,412 @@
<?php <?php
require 'info.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) { function catalog_build($action, $settings, $board) {
global $config; global $config;
print_err("catalog_build");
$b = new Catalog($settings); $b = new Catalog($settings);
$boards = explode(' ', $settings['boards']); $boards = explode(' ', $settings['boards']);
// Possible values for $action: // Possible values for $action:
// - all (rebuild everything, initialization) // - all (rebuild everything, initialization)
// - news (news has been updated) // - news (news has been updated)
// - boards (board list changed) // - boards (board list changed)
// - post (a reply has been made) // - post (a reply has been made)
// - post-thread (a thread has been made) // - post-thread (a thread has been made)
if ($action === 'all') { if ($action === 'all') {
foreach ($boards as $board) { foreach ($boards as $board) {
$b = new Catalog($settings); $b = new Catalog($settings);
$action = generation_strategy("sb_catalog", array($board)); $action = generation_strategy("sb_catalog", array($board));
if ($action == 'delete') { if ($action == 'delete') {
file_unlink($config['dir']['home'] . $board . '/catalog.html'); file_unlink($config['dir']['home'] . $board . '/catalog.html');
file_unlink($config['dir']['home'] . $board . '/index.rss'); file_unlink($config['dir']['home'] . $board . '/index.rss');
} }
elseif ($action == 'rebuild') { elseif ($action == 'rebuild') {
$b->build($settings, $board); $b->build($settings, $board);
} }
} }
} elseif ($action == 'post-thread' || ($settings['update_on_posts'] && $action == 'post') || ($settings['update_on_posts'] && $action == 'post-delete') } 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))) { || $action == 'sticky' || ($action == 'lock' && in_array($board, $boards))) {
$b = new Catalog($settings); $b = new Catalog($settings);
$action = generation_strategy("sb_catalog", array($board)); $action = generation_strategy("sb_catalog", array($board));
if ($action == 'delete') { if ($action == 'delete') {
file_unlink($config['dir']['home'] . $board . '/catalog.html'); file_unlink($config['dir']['home'] . $board . '/catalog.html');
file_unlink($config['dir']['home'] . $board . '/index.rss'); file_unlink($config['dir']['home'] . $board . '/index.rss');
} }
elseif ($action == 'rebuild') { elseif ($action == 'rebuild') {
$b->build($settings, $board); $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']);
} }
// FIXME: Check that Ukko is actually enabled /**
if ($settings['enable_ukko'] && ( * Build and save the HTML of the catalog for the Ukko2 theme
$action === 'all' || $action === 'post' || */
$action === 'post-thread' || $action === 'post-delete' || $action === 'rebuild')) public function buildUkko2() {
{ global $config;
$b->buildUkko(); print_err("Catalog.buildUkko2");
} $ukkoSettings = themeSettings('ukko2');
$queries = array();
$threads = array();
// FIXME: Check that Ukko2 is actually enabled $inclusions = explode(' ', $ukkoSettings['include']);
if ($settings['enable_ukko2'] && ( $boards = array_intersect(listBoards(true), $inclusions);
$action === 'all' || $action === 'post' ||
$action === 'post-thread' || $action === 'post-delete' || $action === 'rebuild'))
{
$b->buildUkko2();
}
// FIXME: Check that Ukko3 is actually enabled foreach ($boards as $b) {
if ($settings['enable_ukko3'] && ( if (array_key_exists($b, $this->threadsCache)) {
$action === 'all' || $action === 'post' || $threads = array_merge($threads, $this->threadsCache[$b]);
$action === 'post-thread' || $action === 'post-delete' || $action === 'rebuild')) } else {
{ $queries[] = $this->buildThreadsQuery($b);
$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 // Fetch threads from boards that haven't beenp processed yet
class Catalog { if (!empty($queries)) {
private $settings; $sql = implode(' UNION ALL ', $queries);
// Cache for threads from boards that have already been processed $res = query($sql) or error(db_error());
private $threadsCache = array(); $threads = array_merge($threads, $res->fetchAll(PDO::FETCH_ASSOC));
}
public function __construct($settings) { // Sort in bump order
$this->settings = $settings; 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,
* Build and save the HTML of the catalog for the Ukko theme $config['root'] . $ukkoSettings['uri']);
*/ }
public function buildUkko() {
global $config;
$ukkoSettings = themeSettings('ukko'); /**
$queries = array(); * Build and save the HTML of the catalog for the Ukko3 theme
$threads = array(); */
public function buildUkko3() {
global $config;
print_err("Catalog.buildUkko3");
$exclusions = explode(' ', $ukkoSettings['exclude']); $ukkoSettings = themeSettings('ukko3');
$boards = array_diff(listBoards(true), $exclusions); $queries = array();
$threads = array();
foreach ($boards as $b) { $inclusions = explode(' ', $ukkoSettings['include']);
if (array_key_exists($b, $this->threadsCache)) { $boards = array_intersect(listBoards(true), $inclusions);
$threads = array_merge($threads, $this->threadsCache[$b]);
} else {
$queries[] = $this->buildThreadsQuery($b);
}
}
// Fetch threads from boards that haven't beenp processed yet foreach ($boards as $b) {
if (!empty($queries)) { if (array_key_exists($b, $this->threadsCache)) {
$sql = implode(' UNION ALL ', $queries); $threads = array_merge($threads, $this->threadsCache[$b]);
$res = query($sql) or error(db_error()); } else {
$threads = array_merge($threads, $res->fetchAll(PDO::FETCH_ASSOC)); $queries[] = $this->buildThreadsQuery($b);
} }
}
// Sort in bump order // Fetch threads from boards that haven't beenp processed yet
usort($threads, function($a, $b) { if (!empty($queries)) {
return strcmp($b['bump'], $a['bump']); $sql = implode(' UNION ALL ', $queries);
}); $res = query($sql) or error(db_error());
// Generate data for the template $threads = array_merge($threads, $res->fetchAll(PDO::FETCH_ASSOC));
$recent_posts = $this->generateRecentPosts($threads); }
$this->saveForBoard($ukkoSettings['uri'], $recent_posts, // Sort in bump order
$config['root'] . $ukkoSettings['uri']); usort($threads, function($a, $b) {
} return strcmp($b['bump'], $a['bump']);
/** });
* Build and save the HTML of the catalog for the Ukko2 theme // Generate data for the template
*/ $recent_posts = $this->generateRecentPosts($threads);
public function buildUkko2() {
global $config;
$ukkoSettings = themeSettings('ukko2');
$queries = array();
$threads = array();
$inclusions = explode(' ', $ukkoSettings['include']); $this->saveForBoard($ukkoSettings['uri'], $recent_posts,
$boards = array_intersect(listBoards(true), $inclusions); $config['root'] . $ukkoSettings['uri']);
}
foreach ($boards as $b) { /**
if (array_key_exists($b, $this->threadsCache)) { * Build and save the HTML of the catalog for the Ukko theme
$threads = array_merge($threads, $this->threadsCache[$b]); */
} else { public function buildUkko4() {
$queries[] = $this->buildThreadsQuery($b); global $config;
} print_err("Catalog.buildUkko4");
}
// Fetch threads from boards that haven't beenp processed yet $ukkoSettings = themeSettings('ukko4');
if (!empty($queries)) { $queries = array();
$sql = implode(' UNION ALL ', $queries); $threads = array();
$res = query($sql) or error(db_error());
$threads = array_merge($threads, $res->fetchAll(PDO::FETCH_ASSOC));
}
// Sort in bump order $exclusions = explode(' ', $ukkoSettings['exclude']);
usort($threads, function($a, $b) { $boards = array_diff(listBoards(true), $exclusions);
return strcmp($b['bump'], $a['bump']);
});
// Generate data for the template
$recent_posts = $this->generateRecentPosts($threads);
$this->saveForBoard($ukkoSettings['uri'], $recent_posts, foreach ($boards as $b) {
$config['root'] . $ukkoSettings['uri']); 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
* Build and save the HTML of the catalog for the Ukko3 theme if (!empty($queries)) {
*/ $sql = implode(' UNION ALL ', $queries);
public function buildUkko3() { $res = query($sql) or error(db_error());
global $config; $threads = array_merge($threads, $res->fetchAll(PDO::FETCH_ASSOC));
}
$ukkoSettings = themeSettings('ukko3'); // Sort in bump order
$queries = array(); usort($threads, function($a, $b) {
$threads = array(); return strcmp($b['bump'], $a['bump']);
});
// Generate data for the template
$recent_posts = $this->generateRecentPosts($threads);
$inclusions = explode(' ', $ukkoSettings['include']); $this->saveForBoard($ukkoSettings['uri'], $recent_posts,
$boards = array_intersect(listBoards(true), $inclusions); $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");
foreach ($boards as $b) { $randSettings = themeSettings('rand');
if (array_key_exists($b, $this->threadsCache)) { $queries = array();
$threads = array_merge($threads, $this->threadsCache[$b]); $threads = array();
} else {
$queries[] = $this->buildThreadsQuery($b);
}
}
// Fetch threads from boards that haven't beenp processed yet $exclusions = explode(' ', $randSettings['exclude']);
if (!empty($queries)) { $boards = array_diff(listBoards(true), $exclusions);
$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 foreach ($boards as $b) {
usort($threads, function($a, $b) { if (array_key_exists($b, $this->threadsCache)) {
return strcmp($b['bump'], $a['bump']); $threads = array_merge($threads, $this->threadsCache[$b]);
}); } else {
// Generate data for the template $queries[] = $this->buildThreadsQuery($b);
$recent_posts = $this->generateRecentPosts($threads); }
}
$this->saveForBoard($ukkoSettings['uri'], $recent_posts, // Fetch threads from boards that haven't beenp processed yet
$config['root'] . $ukkoSettings['uri']); 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
* Build and save the HTML of the catalog for the Ukko theme usort($threads, function($a, $b) {
*/ return strcmp($b['bump'], $a['bump']);
public function buildUkko4() { });
global $config; // Generate data for the template
$recent_posts = $this->generateRecentPosts($threads);
$ukkoSettings = themeSettings('ukko4'); $this->saveForBoard($randSettings['uri'], $recent_posts,
$queries = array(); $config['root'] . $randSettings['uri']);
$threads = array(); }
$exclusions = explode(' ', $ukkoSettings['exclude']); /**
$boards = array_diff(listBoards(true), $exclusions); * 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));
}
}
foreach ($boards as $b) { if (array_key_exists($board_name, $this->threadsCache)) {
if (array_key_exists($b, $this->threadsCache)) { $threads = $this->threadsCache[$board_name];
$threads = array_merge($threads, $this->threadsCache[$b]); } else {
} else { $sql = $this->buildThreadsQuery($board_name);
$queries[] = $this->buildThreadsQuery($b); $query = query($sql . ' ORDER BY `bump` DESC') or error(db_error());
} $threads = $query->fetchAll(PDO::FETCH_ASSOC);
} // Save for posterity
$this->threadsCache[$board_name] = $threads;
}
// Fetch threads from boards that haven't beenp processed yet // Generate data for the template
if (!empty($queries)) { $recent_posts = $this->generateRecentPosts($threads);
$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 $this->saveForBoard($board_name, $recent_posts);
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, private function buildThreadsQuery($board) {
$config['root'] . $ukkoSettings['uri']); $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`, " .
* Build and save the HTML of the catalog for the Rand theme "'$board' AS `board` FROM ``posts_$board`` WHERE `thread` IS NULL";
*/
public function buildRand() {
global $config;
$randSettings = themeSettings('rand'); return $sql;
$queries = array(); }
$threads = array();
$exclusions = explode(' ', $randSettings['exclude']); private function generateRecentPosts($threads) {
$boards = array_diff(listBoards(true), $exclusions); global $config, $board;
foreach ($boards as $b) { $posts = array();
if (array_key_exists($b, $this->threadsCache)) { foreach ($threads as $post) {
$threads = array_merge($threads, $this->threadsCache[$b]); if ($board['uri'] !== $post['board']) {
} else { openBoard($post['board']);
$queries[] = $this->buildThreadsQuery($b); }
}
}
// Fetch threads from boards that haven't beenp processed yet $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . link_for($post);
if (!empty($queries)) { $post['board_name'] = $board['name'];
$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 if ($post['embed'] && preg_match('/^https?:\/\/(\w+\.)?(?:youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9\-_]{10,11})(&.+)?$/i', $post['embed'], $matches)) {
usort($threads, function($a, $b) { $post['youtube'] = $matches[2];
return strcmp($b['bump'], $a['bump']); }
});
// Generate data for the template
$recent_posts = $this->generateRecentPosts($threads);
$this->saveForBoard($randSettings['uri'], $recent_posts, if (isset($post['files']) && $post['files']) {
$config['root'] . $randSettings['uri']); $files = json_decode($post['files']);
}
/** if ($files[0]) {
* Build and save the HTML of the catalog for the given board if ($files[0]->file == 'deleted') {
*/ if (count($files) > 1) {
public function build($settings, $board_name) { foreach ($files as $file) {
global $config, $board; if (($file == $files[0]) || ($file->file == 'deleted'))
if ($board['uri'] != $board_name) { continue;
if (!openBoard($board_name)) { $post['file'] = $config['uri_thumb'] . $file->thumb;
error(sprintf(_("Board %s doesn't exist"), $board_name)); }
}
}
if (array_key_exists($board_name, $this->threadsCache)) { if (empty($post['file']))
$threads = $this->threadsCache[$board_name]; $post['file'] = $config['image_deleted'];
} else { } else {
$sql = $this->buildThreadsQuery($board_name); $post['file'] = $config['image_deleted'];
$query = query($sql . ' ORDER BY `bump` DESC') or error(db_error()); }
$threads = $query->fetchAll(PDO::FETCH_ASSOC); } else if($files[0]->thumb == 'spoiler') {
// Save for posterity $post['file'] = '/' . $config['spoiler_image'];
$this->threadsCache[$board_name] = $threads; } else {
} $post['file'] = $config['uri_thumb'] . $files[0]->thumb;
}
}
} else {
$post['file'] = $config['root'] . $config['image_deleted'];
}
// Generate data for the template if (empty($post['image_count']))
$recent_posts = $this->generateRecentPosts($threads); $post['image_count'] = 0;
$post['pubdate'] = date('r', $post['time']);
$this->saveForBoard($board_name, $recent_posts); $posts[] = $post;
} }
private function buildThreadsQuery($board) { return $posts;
$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 saveForBoard($board_name, $recent_posts, $board_link = null) {
} global $board, $config;
private function generateRecentPosts($threads) { if ($board_link === null) {
global $config, $board; $board_link = $config['root'] . $board['dir'];
}
$posts = array(); $required_scripts = array('js/jquery.min.js', 'js/jquery.mixitup.min.js',
foreach ($threads as $post) { 'js/catalog.js');
if ($board['uri'] !== $post['board']) {
openBoard($post['board']);
}
$post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . link_for($post); // Include scripts that haven't been yet included
$post['board_name'] = $board['name']; foreach($required_scripts as $i => $s) {
if (!in_array($s, $config['additional_javascript']))
$config['additional_javascript'][] = $s;
}
if ($post['embed'] && preg_match('/^https?:\/\/(\w+\.)?(?:youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9\-_]{10,11})(&.+)?$/i', $post['embed'], $matches)) { file_write($config['dir']['home'] . $board_name . '/catalog.html', Element('themes/catalog/catalog.html', Array(
$post['youtube'] = $matches[2]; 'settings' => $this->settings,
} 'config' => $config,
'boardlist' => createBoardlist(),
'recent_images' => array(),
'recent_posts' => $recent_posts,
'stats' => array(),
'board' => $board_name,
'link' => $board_link
)));
if (isset($post['files']) && $post['files']) { file_write($config['dir']['home'] . $board_name . '/index.rss', Element('themes/catalog/index.rss', Array(
$files = json_decode($post['files']); 'config' => $config,
'recent_posts' => $recent_posts,
if ($files[0]) { 'board' => $board
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['image_deleted'];
} else {
$post['file'] = $config['image_deleted'];
}
} else if($files[0]->thumb == 'spoiler') {
$post['file'] = '/' . $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
)));
}
}