formatting: retab some files

This commit is contained in:
towards-a-new-leftypol 2023-08-03 17:43:10 -04:00
parent 151c64110d
commit 70847aeff3
2 changed files with 446 additions and 446 deletions

View File

@ -5,314 +5,314 @@ require_once 'vendor/autoload.php';
use Lifo\IP\CIDR; use Lifo\IP\CIDR;
class Bans { class Bans {
static public function range_to_string($mask) { static public function range_to_string($mask) {
list($ipstart, $ipend) = $mask; list($ipstart, $ipend) = $mask;
if (!isset($ipend) || $ipend === false) { if (!isset($ipend) || $ipend === false) {
// Not a range. Single IP address. // Not a range. Single IP address.
$ipstr = inet_ntop($ipstart); $ipstr = inet_ntop($ipstart);
return $ipstr; return $ipstr;
} }
if (strlen($ipstart) != strlen($ipend)) if (strlen($ipstart) != strlen($ipend))
return '???'; // What the fuck are you doing, son? return '???'; // What the fuck are you doing, son?
$range = CIDR::range_to_cidr(inet_ntop($ipstart), inet_ntop($ipend)); $range = CIDR::range_to_cidr(inet_ntop($ipstart), inet_ntop($ipend));
if ($range !== false) if ($range !== false)
return $range; return $range;
return '???'; return '???';
} }
private static function calc_cidr($mask) { private static function calc_cidr($mask) {
$cidr = new CIDR($mask); $cidr = new CIDR($mask);
$range = $cidr->getRange(); $range = $cidr->getRange();
return array(inet_pton($range[0]), inet_pton($range[1])); return array(inet_pton($range[0]), inet_pton($range[1]));
} }
public static function parse_time($str) { public static function parse_time($str) {
if (empty($str)) if (empty($str))
return false; return false;
if (($time = @strtotime($str)) !== false) if (($time = @strtotime($str)) !== false)
return $time; return $time;
if (!preg_match('/^((\d+)\s?ye?a?r?s?)?\s?+((\d+)\s?mon?t?h?s?)?\s?+((\d+)\s?we?e?k?s?)?\s?+((\d+)\s?da?y?s?)?((\d+)\s?ho?u?r?s?)?\s?+((\d+)\s?mi?n?u?t?e?s?)?\s?+((\d+)\s?se?c?o?n?d?s?)?$/', $str, $matches)) if (!preg_match('/^((\d+)\s?ye?a?r?s?)?\s?+((\d+)\s?mon?t?h?s?)?\s?+((\d+)\s?we?e?k?s?)?\s?+((\d+)\s?da?y?s?)?((\d+)\s?ho?u?r?s?)?\s?+((\d+)\s?mi?n?u?t?e?s?)?\s?+((\d+)\s?se?c?o?n?d?s?)?$/', $str, $matches))
return false; return false;
$expire = 0; $expire = 0;
if (isset($matches[2])) { if (isset($matches[2])) {
// Years // Years
$expire += (int)$matches[2]*60*60*24*365; $expire += (int)$matches[2]*60*60*24*365;
} }
if (isset($matches[4])) { if (isset($matches[4])) {
// Months // Months
$expire += (int)$matches[4]*60*60*24*30; $expire += (int)$matches[4]*60*60*24*30;
} }
if (isset($matches[6])) { if (isset($matches[6])) {
// Weeks // Weeks
$expire += (int)$matches[6]*60*60*24*7; $expire += (int)$matches[6]*60*60*24*7;
} }
if (isset($matches[8])) { if (isset($matches[8])) {
// Days // Days
$expire += (int)$matches[8]*60*60*24; $expire += (int)$matches[8]*60*60*24;
} }
if (isset($matches[10])) { if (isset($matches[10])) {
// Hours // Hours
$expire += (int)$matches[10]*60*60; $expire += (int)$matches[10]*60*60;
} }
if (isset($matches[12])) { if (isset($matches[12])) {
// Minutes // Minutes
$expire += (int)$matches[12]*60; $expire += (int)$matches[12]*60;
} }
if (isset($matches[14])) { if (isset($matches[14])) {
// Seconds // Seconds
$expire += (int)$matches[14]; $expire += (int)$matches[14];
} }
return time() + $expire; return time() + $expire;
} }
static public function parse_range($mask) { static public function parse_range($mask) {
$ipstart = false; $ipstart = false;
$ipend = false; $ipend = false;
if (preg_match('@^(\d{1,3}\.){1,3}([\d*]{1,3})?$@', $mask) && substr_count($mask, '*') == 1) { if (preg_match('@^(\d{1,3}\.){1,3}([\d*]{1,3})?$@', $mask) && substr_count($mask, '*') == 1) {
// IPv4 wildcard mask // IPv4 wildcard mask
$parts = explode('.', $mask); $parts = explode('.', $mask);
$ipv4 = ''; $ipv4 = '';
foreach ($parts as $part) { foreach ($parts as $part) {
if ($part == '*') { if ($part == '*') {
$ipstart = inet_pton($ipv4 . '0' . str_repeat('.0', 3 - substr_count($ipv4, '.'))); $ipstart = inet_pton($ipv4 . '0' . str_repeat('.0', 3 - substr_count($ipv4, '.')));
$ipend = inet_pton($ipv4 . '255' . str_repeat('.255', 3 - substr_count($ipv4, '.'))); $ipend = inet_pton($ipv4 . '255' . str_repeat('.255', 3 - substr_count($ipv4, '.')));
break; break;
} elseif(($wc = strpos($part, '*')) !== false) { } elseif(($wc = strpos($part, '*')) !== false) {
$ipstart = inet_pton($ipv4 . substr($part, 0, $wc) . '0' . str_repeat('.0', 3 - substr_count($ipv4, '.'))); $ipstart = inet_pton($ipv4 . substr($part, 0, $wc) . '0' . str_repeat('.0', 3 - substr_count($ipv4, '.')));
$ipend = inet_pton($ipv4 . substr($part, 0, $wc) . '9' . str_repeat('.255', 3 - substr_count($ipv4, '.'))); $ipend = inet_pton($ipv4 . substr($part, 0, $wc) . '9' . str_repeat('.255', 3 - substr_count($ipv4, '.')));
break; break;
} }
$ipv4 .= "$part."; $ipv4 .= "$part.";
} }
} elseif (preg_match('@^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d+$@', $mask)) { } elseif (preg_match('@^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d+$@', $mask)) {
list($ipv4, $bits) = explode('/', $mask); list($ipv4, $bits) = explode('/', $mask);
if ($bits > 32) if ($bits > 32)
return false; return false;
list($ipstart, $ipend) = self::calc_cidr($mask); list($ipstart, $ipend) = self::calc_cidr($mask);
} elseif (preg_match('@^[:a-z\d]+/\d+$@i', $mask)) { } elseif (preg_match('@^[:a-z\d]+/\d+$@i', $mask)) {
list($ipv6, $bits) = explode('/', $mask); list($ipv6, $bits) = explode('/', $mask);
if ($bits > 128) if ($bits > 128)
return false; return false;
list($ipstart, $ipend) = self::calc_cidr($mask); list($ipstart, $ipend) = self::calc_cidr($mask);
} else { } else {
if (($ipstart = @inet_pton($mask)) === false) if (($ipstart = @inet_pton($mask)) === false)
return false; return false;
} }
return array($ipstart, $ipend); return array($ipstart, $ipend);
} }
static public function find($ip, $board = false, $get_mod_info = false) { static public function find($ip, $board = false, $get_mod_info = false) {
global $config; global $config;
$query = prepare('SELECT ``bans``.*' . ($get_mod_info ? ', `username`' : '') . ' FROM ``bans`` $query = prepare('SELECT ``bans``.*' . ($get_mod_info ? ', `username`' : '') . ' FROM ``bans``
' . ($get_mod_info ? 'LEFT JOIN ``mods`` ON ``mods``.`id` = `creator`' : '') . ' ' . ($get_mod_info ? 'LEFT JOIN ``mods`` ON ``mods``.`id` = `creator`' : '') . '
WHERE WHERE
(' . ($board !== false ? '(`board` IS NULL OR `board` = :board) AND' : '') . ' (' . ($board !== false ? '(`board` IS NULL OR `board` = :board) AND' : '') . '
(`ipstart` = :ip OR (:ip >= `ipstart` AND :ip <= `ipend`))) (`ipstart` = :ip OR (:ip >= `ipstart` AND :ip <= `ipend`)))
ORDER BY `expires` IS NULL, `expires` DESC'); ORDER BY `expires` IS NULL, `expires` DESC');
if ($board !== false) if ($board !== false)
$query->bindValue(':board', $board, PDO::PARAM_STR); $query->bindValue(':board', $board, PDO::PARAM_STR);
$query->bindValue(':ip', inet_pton($ip)); $query->bindValue(':ip', inet_pton($ip));
$query->execute() or error(db_error($query)); $query->execute() or error(db_error($query));
$ban_list = array(); $ban_list = array();
while ($ban = $query->fetch(PDO::FETCH_ASSOC)) { while ($ban = $query->fetch(PDO::FETCH_ASSOC)) {
if ($ban['expires'] && ($ban['seen'] || !$config['require_ban_view']) && $ban['expires'] < time()) { if ($ban['expires'] && ($ban['seen'] || !$config['require_ban_view']) && $ban['expires'] < time()) {
self::delete($ban['id']); self::delete($ban['id']);
} else { } else {
if ($ban['post']) if ($ban['post'])
$ban['post'] = json_decode($ban['post'], true); $ban['post'] = json_decode($ban['post'], true);
$ban['mask'] = self::range_to_string(array($ban['ipstart'], $ban['ipend'])); $ban['mask'] = self::range_to_string(array($ban['ipstart'], $ban['ipend']));
$ban_list[] = $ban; $ban_list[] = $ban;
} }
} }
return $ban_list; return $ban_list;
} }
static public function stream_json($out = false, $filter_ips = false, $filter_staff = false, $board_access = false, $hide_regexes = []) { static public function stream_json($out = false, $filter_ips = false, $filter_staff = false, $board_access = false, $hide_regexes = []) {
$query = query("SELECT ``bans``.*, `username` FROM ``bans`` $query = query("SELECT ``bans``.*, `username` FROM ``bans``
LEFT JOIN ``mods`` ON ``mods``.`id` = `creator` LEFT JOIN ``mods`` ON ``mods``.`id` = `creator`
ORDER BY `created` DESC") or error(db_error()); ORDER BY `created` DESC") or error(db_error());
$bans = $query->fetchAll(PDO::FETCH_ASSOC); $bans = $query->fetchAll(PDO::FETCH_ASSOC);
if ($board_access && $board_access[0] == '*') $board_access = false; if ($board_access && $board_access[0] == '*') $board_access = false;
$out ? fputs($out, "[") : print("["); $out ? fputs($out, "[") : print("[");
$end = end($bans); $end = end($bans);
foreach ($bans as &$ban) { foreach ($bans as &$ban) {
$ban['mask'] = self::range_to_string(array($ban['ipstart'], $ban['ipend'])); $ban['mask'] = self::range_to_string(array($ban['ipstart'], $ban['ipend']));
$hide_message = false; $hide_message = false;
foreach ($hide_regexes as $regex) { foreach ($hide_regexes as $regex) {
if(preg_match($regex, $ban['reason'])) { if(preg_match($regex, $ban['reason'])) {
$hide_message = true; $hide_message = true;
break; break;
} }
} }
if ($ban['post'] && !$hide_message) { if ($ban['post'] && !$hide_message) {
$post = json_decode($ban['post']); $post = json_decode($ban['post']);
$ban['message'] = isset($post->body) ? $post->body : 0; $ban['message'] = isset($post->body) ? $post->body : 0;
} }
unset($ban['ipstart'], $ban['ipend'], $ban['post'], $ban['creator']); unset($ban['ipstart'], $ban['ipend'], $ban['post'], $ban['creator']);
if ($board_access === false || in_array ($ban['board'], $board_access)) { if ($board_access === false || in_array ($ban['board'], $board_access)) {
$ban['access'] = true; $ban['access'] = true;
} }
if (filter_var($ban['mask'], FILTER_VALIDATE_IP) !== false) { if (filter_var($ban['mask'], FILTER_VALIDATE_IP) !== false) {
$ban['single_addr'] = true; $ban['single_addr'] = true;
} }
if ($filter_staff || ($board_access !== false && !in_array($ban['board'], $board_access))) { if ($filter_staff || ($board_access !== false && !in_array($ban['board'], $board_access))) {
$ban['username'] = '?'; $ban['username'] = '?';
} }
if ($filter_ips || ($board_access !== false && !in_array($ban['board'], $board_access))) { if ($filter_ips || ($board_access !== false && !in_array($ban['board'], $board_access))) {
@list($ban['mask'], $subnet) = explode("/", $ban['mask']); @list($ban['mask'], $subnet) = explode("/", $ban['mask']);
$ban['mask'] = preg_split("/[\.:]/", $ban['mask']); $ban['mask'] = preg_split("/[\.:]/", $ban['mask']);
$ban['mask'] = array_slice($ban['mask'], 0, 2); $ban['mask'] = array_slice($ban['mask'], 0, 2);
$ban['mask'] = implode(".", $ban['mask']); $ban['mask'] = implode(".", $ban['mask']);
$ban['mask'] .= ".x.x"; $ban['mask'] .= ".x.x";
if (isset ($subnet)) { if (isset ($subnet)) {
$ban['mask'] .= "/$subnet"; $ban['mask'] .= "/$subnet";
} }
$ban['masked'] = true; $ban['masked'] = true;
} }
$json = json_encode($ban); $json = json_encode($ban);
$out ? fputs($out, $json) : print($json); $out ? fputs($out, $json) : print($json);
if ($ban['id'] != $end['id']) { if ($ban['id'] != $end['id']) {
$out ? fputs($out, ",") : print(","); $out ? fputs($out, ",") : print(",");
} }
} }
$out ? fputs($out, "]") : print("]"); $out ? fputs($out, "]") : print("]");
} }
static public function seen($ban_id) { static public function seen($ban_id) {
$query = query("UPDATE ``bans`` SET `seen` = 1 WHERE `id` = " . (int)$ban_id) or error(db_error()); $query = query("UPDATE ``bans`` SET `seen` = 1 WHERE `id` = " . (int)$ban_id) or error(db_error());
rebuildThemes('bans'); rebuildThemes('bans');
} }
static public function purge() { static public function purge() {
$query = query("DELETE FROM ``bans`` WHERE `expires` IS NOT NULL AND `expires` < " . time() . " AND `seen` = 1") or error(db_error()); $query = query("DELETE FROM ``bans`` WHERE `expires` IS NOT NULL AND `expires` < " . time() . " AND `seen` = 1") or error(db_error());
rebuildThemes('bans'); rebuildThemes('bans');
} }
static public function delete($ban_id, $modlog = false, $boards = false, $dont_rebuild = false) { static public function delete($ban_id, $modlog = false, $boards = false, $dont_rebuild = false) {
global $config; global $config;
if ($boards && $boards[0] == '*') $boards = false; if ($boards && $boards[0] == '*') $boards = false;
if ($modlog) { if ($modlog) {
$query = query("SELECT `ipstart`, `ipend`, `board` FROM ``bans`` WHERE `id` = " . (int)$ban_id) or error(db_error()); $query = query("SELECT `ipstart`, `ipend`, `board` FROM ``bans`` WHERE `id` = " . (int)$ban_id) or error(db_error());
if (!$ban = $query->fetch(PDO::FETCH_ASSOC)) { if (!$ban = $query->fetch(PDO::FETCH_ASSOC)) {
// Ban doesn't exist // Ban doesn't exist
return false; return false;
} }
if ($boards !== false && !in_array($ban['board'], $boards)) if ($boards !== false && !in_array($ban['board'], $boards))
error($config['error']['noaccess']); error($config['error']['noaccess']);
$mask = self::range_to_string(array($ban['ipstart'], $ban['ipend'])); $mask = self::range_to_string(array($ban['ipstart'], $ban['ipend']));
modLog("Removed ban #{$ban_id} for " . modLog("Removed ban #{$ban_id} for " .
(filter_var($mask, FILTER_VALIDATE_IP) !== false ? "<a href=\"?/IP/$mask\">$mask</a>" : $mask)); (filter_var($mask, FILTER_VALIDATE_IP) !== false ? "<a href=\"?/IP/$mask\">$mask</a>" : $mask));
} }
query("DELETE FROM ``bans`` WHERE `id` = " . (int)$ban_id) or error(db_error()); query("DELETE FROM ``bans`` WHERE `id` = " . (int)$ban_id) or error(db_error());
if (!$dont_rebuild) rebuildThemes('bans'); if (!$dont_rebuild) rebuildThemes('bans');
return true; return true;
} }
static public function new_ban($mask, $reason, $length = false, $ban_board = false, $mod_id = false, $post = false) { static public function new_ban($mask, $reason, $length = false, $ban_board = false, $mod_id = false, $post = false) {
global $mod, $pdo, $board; global $mod, $pdo, $board;
if ($mod_id === false) { if ($mod_id === false) {
$mod_id = isset($mod['id']) ? $mod['id'] : -1; $mod_id = isset($mod['id']) ? $mod['id'] : -1;
} }
$range = self::parse_range($mask); $range = self::parse_range($mask);
$mask = self::range_to_string($range); $mask = self::range_to_string($range);
$query = prepare("INSERT INTO ``bans`` VALUES (NULL, :ipstart, :ipend, :time, :expires, :board, :mod, :reason, 0, :post)"); $query = prepare("INSERT INTO ``bans`` VALUES (NULL, :ipstart, :ipend, :time, :expires, :board, :mod, :reason, 0, :post)");
$query->bindValue(':ipstart', $range[0]); $query->bindValue(':ipstart', $range[0]);
if ($range[1] !== false && $range[1] != $range[0]) if ($range[1] !== false && $range[1] != $range[0])
$query->bindValue(':ipend', $range[1]); $query->bindValue(':ipend', $range[1]);
else else
$query->bindValue(':ipend', null, PDO::PARAM_NULL); $query->bindValue(':ipend', null, PDO::PARAM_NULL);
$query->bindValue(':mod', $mod_id); $query->bindValue(':mod', $mod_id);
$query->bindValue(':time', time()); $query->bindValue(':time', time());
if ($reason !== '') { if ($reason !== '') {
$reason = escape_markup_modifiers($reason); $reason = escape_markup_modifiers($reason);
markup($reason); markup($reason);
$query->bindValue(':reason', $reason); $query->bindValue(':reason', $reason);
} else } else
$query->bindValue(':reason', null, PDO::PARAM_NULL); $query->bindValue(':reason', null, PDO::PARAM_NULL);
if ($length) { if ($length) {
if (is_int($length) || ctype_digit($length)) { if (is_int($length) || ctype_digit($length)) {
$length = time() + $length; $length = time() + $length;
} else { } else {
$length = self::parse_time($length); $length = self::parse_time($length);
} }
$query->bindValue(':expires', $length); $query->bindValue(':expires', $length);
} else { } else {
$query->bindValue(':expires', null, PDO::PARAM_NULL); $query->bindValue(':expires', null, PDO::PARAM_NULL);
} }
if ($ban_board) if ($ban_board)
$query->bindValue(':board', $ban_board); $query->bindValue(':board', $ban_board);
else else
$query->bindValue(':board', null, PDO::PARAM_NULL); $query->bindValue(':board', null, PDO::PARAM_NULL);
if ($post) { if ($post) {
$post['board'] = $board['uri']; $post['board'] = $board['uri'];
$query->bindValue(':post', json_encode($post)); $query->bindValue(':post', json_encode($post));
} else } else
$query->bindValue(':post', null, PDO::PARAM_NULL); $query->bindValue(':post', null, PDO::PARAM_NULL);
$query->execute() or error(db_error($query)); $query->execute() or error(db_error($query));
if (isset($mod['id']) && $mod['id'] == $mod_id) { if (isset($mod['id']) && $mod['id'] == $mod_id) {
modLog('Created a new ' . modLog('Created a new ' .
($length > 0 ? preg_replace('/^(\d+) (\w+?)s?$/', '$1-$2', until($length)) : 'permanent') . ($length > 0 ? preg_replace('/^(\d+) (\w+?)s?$/', '$1-$2', until($length)) : 'permanent') .
' ban on ' . ' ban on ' .
($ban_board ? '/' . $ban_board . '/' : 'all boards') . ($ban_board ? '/' . $ban_board . '/' : 'all boards') .
' for ' . ' for ' .
(filter_var($mask, FILTER_VALIDATE_IP) !== false ? "<a href=\"?/IP/$mask\">$mask</a>" : $mask) . (filter_var($mask, FILTER_VALIDATE_IP) !== false ? "<a href=\"?/IP/$mask\">$mask</a>" : $mask) .
' (<small>#' . $pdo->lastInsertId() . '</small>)' . ' (<small>#' . $pdo->lastInsertId() . '</small>)' .
' with ' . ($reason ? 'reason: ' . utf8tohtml($reason) . '' : 'no reason')); ' with ' . ($reason ? 'reason: ' . utf8tohtml($reason) . '' : 'no reason'));
} }
rebuildThemes('bans'); rebuildThemes('bans');
return $pdo->lastInsertId(); return $pdo->lastInsertId();
} }
} }

View File

@ -1,168 +1,168 @@
<?php <?php
require 'info.php'; require 'info.php';
function recentposts_build($action, $settings, $board) { function recentposts_build($action, $settings, $board) {
// 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 post has been made) // - post (a post has been made)
// - post-thread (a thread has been made) // - post-thread (a thread has been made)
$b = new RecentPosts(); $b = new RecentPosts();
$b->build($action, $settings); $b->build($action, $settings);
} }
// Wrap functions in a class so they don't interfere with normal Tinyboard operations // Wrap functions in a class so they don't interfere with normal Tinyboard operations
class RecentPosts { class RecentPosts {
public function build($action, $settings) { public function build($action, $settings) {
global $config, $_theme; global $config, $_theme;
if ($action == 'all') { if ($action == 'all') {
copy('templates/themes/recent/' . $settings['basecss'], $config['dir']['home'] . "stylesheets/" . $settings['css']); copy('templates/themes/recent/' . $settings['basecss'], $config['dir']['home'] . "stylesheets/" . $settings['css']);
} }
$this->excluded = explode(' ', $settings['exclude']); $this->excluded = explode(' ', $settings['exclude']);
if ($action == 'all' || $action == 'post' || $action == 'post-thread' || $action == 'post-delete') { if ($action == 'all' || $action == 'post' || $action == 'post-thread' || $action == 'post-delete') {
$action = generation_strategy('sb_recent', array()); $action = generation_strategy('sb_recent', array());
if ($action == 'delete') { if ($action == 'delete') {
file_unlink($config['dir']['home'] . $settings['html']); file_unlink($config['dir']['home'] . $settings['html']);
} }
elseif ($action == 'rebuild') { elseif ($action == 'rebuild') {
file_write($config['dir']['home'] . $settings['html'], $this->homepage($settings)); file_write($config['dir']['home'] . $settings['html'], $this->homepage($settings));
} }
} }
} }
// Build news page // Build news page
public function homepage($settings) { public function homepage($settings) {
global $config, $board; global $config, $board;
$recent_images = Array(); $recent_images = Array();
$recent_posts = Array(); $recent_posts = Array();
$stats = Array(); $stats = Array();
$boards = listBoards(); $boards = listBoards();
$query = ''; $query = '';
foreach ($boards as &$_board) { foreach ($boards as &$_board) {
if (in_array($_board['uri'], $this->excluded)) if (in_array($_board['uri'], $this->excluded))
continue; continue;
$query .= sprintf("SELECT *, '%s' AS `board` FROM ``posts_%s`` WHERE `files` IS NOT NULL UNION ALL ", $_board['uri'], $_board['uri']); $query .= sprintf("SELECT *, '%s' AS `board` FROM ``posts_%s`` WHERE `files` IS NOT NULL UNION ALL ", $_board['uri'], $_board['uri']);
} }
$query = preg_replace('/UNION ALL $/', 'ORDER BY `time` DESC LIMIT ' . (int)$settings['limit_images'], $query); $query = preg_replace('/UNION ALL $/', 'ORDER BY `time` DESC LIMIT ' . (int)$settings['limit_images'], $query);
if ($query == '') { if ($query == '') {
error(_("Can't build the RecentPosts theme, because there are no boards to be fetched.")); error(_("Can't build the RecentPosts theme, because there are no boards to be fetched."));
} }
$query = query($query) or error(db_error()); $query = query($query) or error(db_error());
while ($post = $query->fetch(PDO::FETCH_ASSOC)) { while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
openBoard($post['board']); openBoard($post['board']);
if (isset($post['files'])) if (isset($post['files']))
$files = json_decode($post['files']); $files = json_decode($post['files']);
if ($files[0]->file == 'deleted' || $files[0]->thumb == 'file') continue; if ($files[0]->file == 'deleted' || $files[0]->thumb == 'file') continue;
// board settings won't be available in the template file, so generate links now // board settings won't be available in the template file, so generate links now
$post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res']
. link_for($post) . '#' . $post['id']; . link_for($post) . '#' . $post['id'];
if ($files) { if ($files) {
if ($files[0]->thumb == 'spoiler') { if ($files[0]->thumb == 'spoiler') {
$tn_size = @getimagesize($config['spoiler_image']); $tn_size = @getimagesize($config['spoiler_image']);
$post['src'] = $config['spoiler_image']; $post['src'] = $config['spoiler_image'];
$post['thumbwidth'] = $tn_size[0]; $post['thumbwidth'] = $tn_size[0];
$post['thumbheight'] = $tn_size[1]; $post['thumbheight'] = $tn_size[1];
} }
else { else {
$post['src'] = $config['uri_thumb'] . $files[0]->thumb; $post['src'] = $config['uri_thumb'] . $files[0]->thumb;
$post['thumbwidth'] = $files[0]->thumbwidth; $post['thumbwidth'] = $files[0]->thumbwidth;
$post['thumbheight'] = $files[0]->thumbheight; $post['thumbheight'] = $files[0]->thumbheight;
} }
} }
$recent_images[] = $post; $recent_images[] = $post;
} }
$query = ''; $query = '';
foreach ($boards as &$_board) { foreach ($boards as &$_board) {
if (in_array($_board['uri'], $this->excluded)) if (in_array($_board['uri'], $this->excluded))
continue; continue;
$query .= sprintf("SELECT *, '%s' AS `board` FROM ``posts_%s`` UNION ALL ", $_board['uri'], $_board['uri']); $query .= sprintf("SELECT *, '%s' AS `board` FROM ``posts_%s`` UNION ALL ", $_board['uri'], $_board['uri']);
} }
$query = preg_replace('/UNION ALL $/', 'ORDER BY `time` DESC LIMIT ' . (int)$settings['limit_posts'], $query); $query = preg_replace('/UNION ALL $/', 'ORDER BY `time` DESC LIMIT ' . (int)$settings['limit_posts'], $query);
$query = query($query) or error(db_error()); $query = query($query) or error(db_error());
while ($post = $query->fetch(PDO::FETCH_ASSOC)) { while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
openBoard($post['board']); openBoard($post['board']);
$post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . link_for($post) . '#' . $post['id']; $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . link_for($post) . '#' . $post['id'];
if ($post['body'] != "") if ($post['body'] != "")
$post['snippet'] = pm_snippet($post['body'], 30); $post['snippet'] = pm_snippet($post['body'], 30);
else else
$post['snippet'] = "<em>" . _("(no comment)") . "</em>"; $post['snippet'] = "<em>" . _("(no comment)") . "</em>";
$post['board_name'] = $board['name']; $post['board_name'] = $board['name'];
$recent_posts[] = $post; $recent_posts[] = $post;
} }
// Total posts // Total posts
$query = 'SELECT SUM(`top`) FROM ('; $query = 'SELECT SUM(`top`) FROM (';
foreach ($boards as &$_board) { foreach ($boards as &$_board) {
if (in_array($_board['uri'], $this->excluded)) if (in_array($_board['uri'], $this->excluded))
continue; continue;
$query .= sprintf("SELECT MAX(`id`) AS `top` FROM ``posts_%s`` UNION ALL ", $_board['uri']); $query .= sprintf("SELECT MAX(`id`) AS `top` FROM ``posts_%s`` UNION ALL ", $_board['uri']);
} }
$query = preg_replace('/UNION ALL $/', ') AS `posts_all`', $query); $query = preg_replace('/UNION ALL $/', ') AS `posts_all`', $query);
$query = query($query) or error(db_error()); $query = query($query) or error(db_error());
$stats['total_posts'] = number_format($query->fetchColumn()); $stats['total_posts'] = number_format($query->fetchColumn());
// Unique IPs // Unique IPs
$query = 'SELECT COUNT(DISTINCT(`ip`)) FROM ('; $query = 'SELECT COUNT(DISTINCT(`ip`)) FROM (';
foreach ($boards as &$_board) { foreach ($boards as &$_board) {
if (in_array($_board['uri'], $this->excluded)) if (in_array($_board['uri'], $this->excluded))
continue; continue;
$query .= sprintf("SELECT `ip` FROM ``posts_%s`` UNION ALL ", $_board['uri']); $query .= sprintf("SELECT `ip` FROM ``posts_%s`` UNION ALL ", $_board['uri']);
} }
$query = preg_replace('/UNION ALL $/', ') AS `posts_all`', $query); $query = preg_replace('/UNION ALL $/', ') AS `posts_all`', $query);
$query = query($query) or error(db_error()); $query = query($query) or error(db_error());
$stats['unique_posters'] = number_format($query->fetchColumn()); $stats['unique_posters'] = number_format($query->fetchColumn());
// Active content // Active content
$query = 'SELECT DISTINCT(`files`) FROM ('; $query = 'SELECT DISTINCT(`files`) FROM (';
foreach ($boards as &$_board) { foreach ($boards as &$_board) {
if (in_array($_board['uri'], $this->excluded)) if (in_array($_board['uri'], $this->excluded))
continue; continue;
$query .= sprintf("SELECT `files` FROM `posts_%s` WHERE `num_files` > 0 UNION ALL ", $_board['uri']); $query .= sprintf("SELECT `files` FROM `posts_%s` WHERE `num_files` > 0 UNION ALL ", $_board['uri']);
} }
$query = preg_replace('/UNION ALL $/', ') AS `posts_all`', $query); $query = preg_replace('/UNION ALL $/', ') AS `posts_all`', $query);
$query = query($query) or error(db_error()); $query = query($query) or error(db_error());
$files = $query->fetchAll(); $files = $query->fetchAll();
$stats['active_content'] = 0; $stats['active_content'] = 0;
foreach ($files as &$file) { foreach ($files as &$file) {
preg_match_all('/"size":([0-9]*)/', $file[0], $matches); preg_match_all('/"size":([0-9]*)/', $file[0], $matches);
$stats['active_content'] += array_sum($matches[1]); $stats['active_content'] += array_sum($matches[1]);
} }
$query = query("SELECT * FROM ``news`` ORDER BY `time` DESC" . ($settings['limit_news'] ? ' LIMIT ' . $settings['limit_news'] : '')) or error(db_error()); $query = query("SELECT * FROM ``news`` ORDER BY `time` DESC" . ($settings['limit_news'] ? ' LIMIT ' . $settings['limit_news'] : '')) or error(db_error());
$recent_news = $query->fetchAll(PDO::FETCH_ASSOC); $recent_news = $query->fetchAll(PDO::FETCH_ASSOC);
return Element('themes/recent/recent.html', Array( return Element('themes/recent/recent.html', Array(
'settings' => $settings, 'settings' => $settings,
'config' => $config, 'config' => $config,
'boardlist' => createBoardlist(), 'boardlist' => createBoardlist(),
'recent_images' => $recent_images, 'recent_images' => $recent_images,
'recent_posts' => $recent_posts, 'recent_posts' => $recent_posts,
'stats' => $stats, 'stats' => $stats,
'recent_news' => $recent_news, 'recent_news' => $recent_news,
)); ));
} }
}; };
?> ?>