Skip to content
This repository was archived by the owner on Jun 13, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
830640d
Adds the table posts to install.sql.
Sep 25, 2014
62f0a4a
Changes the post function in inc/functions.php
Sep 25, 2014
05a4fdf
Fixes the post function to return the right id.
Sep 25, 2014
4818406
Changes the templates to use id_for_board.
Sep 25, 2014
01f52e5
Updates rebuild.php to work with posts table.
Sep 25, 2014
51b7a15
A minor typo in functions.php
Sep 25, 2014
b36dda2
boards.php now uses the posts table
Sep 27, 2014
503d3ea
rebuildPost() now uses the posts table
Sep 27, 2014
e031400
Applies `` to the table name and removes sprintf.
Sep 27, 2014
76c0a98
Missed a parenthesis.
Sep 27, 2014
9913834
bumpThread() now uses the posts table
Sep 27, 2014
beb142e
deleteFile() and numPosts() now use the posts table
Sep 27, 2014
405a49a
deletePost now uses the posts table
Sep 27, 2014
6bdb15a
Couple of typos in rebuildPost.
Sep 27, 2014
a21b0be
More functions now use posts table.
Sep 27, 2014
cfb5066
buildThread() now uses the posts table
Sep 27, 2014
2ad9062
Fixes the table prefix syntax in tools/rebuild.php
Sep 27, 2014
f197364
Deletion in post.php now uses the posts table
Sep 27, 2014
f81f10a
More changes in post.php
Sep 27, 2014
e2fbedd
tools/stats.php now uses the posts table
Sep 29, 2014
f69c856
Catalog now uses the posts table.
Sep 29, 2014
d4984d3
Recent theme now uses the posts table.
Sep 29, 2014
a054116
Sitemap now uses the posts table.
Sep 29, 2014
e2adc88
post.php and functions.php now use posts table
Sep 29, 2014
38cd9a7
Last files moved to posts table.
Sep 30, 2014
ca9c37c
Removes posts.sql.
Oct 1, 2014
4e6c223
Renaming id_for_board to just id, duh
Oct 8, 2014
cc09792
Migration script.
Oct 9, 2014
257f01f
A little fix in functions.php.
Oct 9, 2014
9d5f85a
Forgot instance-config.php.
Oct 9, 2014
7e1f4be
Merge branch 'master' of github.com:ctrlcctrlv/8chan into posts-table
Oct 9, 2014
1ef6161
Changes the insert query because performance.
Oct 10, 2014
4ceab4f
Merge branch 'master' of github.com:ctrlcctrlv/8chan into posts-table
Oct 10, 2014
e8ff2d2
Changes sprintf to prepare.
Oct 15, 2014
e070b9d
Sprintf->prepare in inc/instance-config.php.
Oct 15, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions boards.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@
$total_posts = 0;

foreach ($boards as $i => $board) {

//$query = prepare(sprintf("SELECT (SELECT MAX(id) from ``posts_%s``) AS max, (SELECT MAX(id) FROM ``posts_%s`` WHERE FROM_UNIXTIME(time) < DATE_SUB(NOW(), INTERVAL 1 HOUR)) AS oldmax, (SELECT MAX(id) from ``posts_%s``) AS max_d, (SELECT MAX(id) FROM ``posts_%s`` WHERE FROM_UNIXTIME(time) < DATE_SUB(NOW(), INTERVAL 1 DAY)) AS oldmax_d, (SELECT count(id) FROM ``posts_%s``) AS count;", $board['uri'], $board['uri'], $board['uri'], $board['uri'], $board['uri']));

$query = prepare(sprintf("
SELECT MAX(id) max, (SELECT COUNT(*) FROM ``posts_%s`` WHERE FROM_UNIXTIME(time) > DATE_SUB(NOW(), INTERVAL 1 DAY)) ppd,
(SELECT COUNT(*) FROM ``posts_%s`` WHERE FROM_UNIXTIME(time) > DATE_SUB(NOW(), INTERVAL 1 HOUR)) pph,
(SELECT count(id) FROM ``posts_%s``) count,
(SELECT COUNT(DISTINCT ip) FROM ``posts_%s`` WHERE FROM_UNIXTIME(time) > DATE_SUB(NOW(), INTERVAL 3 DAY)) uniq_ip
FROM ``posts_%s``
", $board['uri'], $board['uri'], $board['uri'], $board['uri'], $board['uri']));
$query = prepare("
SELECT
(SELECT coalesce((SELECT max(`id`) FROM ``posts`` WHERE `board` = :board),0)) max,
(SELECT COUNT(*) FROM ``posts`` WHERE `board` = :board AND FROM_UNIXTIME(time) > DATE_SUB(NOW(), INTERVAL 1 DAY)) ppd,
(SELECT COUNT(*) FROM ``posts`` WHERE `board` = :board AND FROM_UNIXTIME(time) > DATE_SUB(NOW(), INTERVAL 1 HOUR)) pph,
(SELECT count(id) FROM ``posts`` WHERE `board` = :board) count,
(SELECT COUNT(DISTINCT ip) FROM ``posts`` WHERE `board` = :board AND FROM_UNIXTIME(time) > DATE_SUB(NOW(), INTERVAL 3 DAY)) uniq_ip
FROM ``posts``
WHERE `board` = :board");
$query->bindValue(':board', $board['uri']);
$pdo->beginTransaction();
$query->execute() or error(db_error($query));
$pdo->commit();
$r = $query->fetch(PDO::FETCH_ASSOC);

$pph = $r['pph'];
Expand Down
3 changes: 0 additions & 3 deletions create.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@
$query->bindValue(':subtitle', $_POST['subtitle']);
$query->execute() or error(db_error($query));

$query = Element('posts.sql', array('board' => $uri));
query($query) or error(db_error());

if (!openBoard($_POST['uri']))
error(_("Couldn't open board after creation."));
if ($config['cache']['enabled'])
Expand Down
12 changes: 8 additions & 4 deletions expire.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
}

// last post
$query = prepare(sprintf("SELECT MAX(time) AS time FROM posts_%s", $board));
$query = prepare("SELECT MAX(time) AS time FROM posts WHERE `board` = :board");
$query->bindValue(':board', $board);
$query->execute();
$row = $query->fetch();

//count posts
$query = prepare(sprintf("SELECT COUNT(id) AS count FROM posts_%s", $board));
$query = prepare("SELECT COUNT(id) AS count FROM posts WHERE `board` = :board", $board);
$query->bindValue(':board', $board);
$query->execute();
$count = $query->fetch();

Expand Down Expand Up @@ -79,8 +81,10 @@
cache::delete('all_boards');
}

// Delete posting table
$query = query(sprintf('DROP TABLE IF EXISTS ``posts_%s``', $board['uri'])) or error(db_error());
// Delete posts
$query = prepare('DELETE FROM ``posts`` WHERE `board` = :board');
$query->bindValue(':board', $board['uri']);
$query->execute() or error(db_error($query));

// Clear reports
$query = prepare('DELETE FROM ``reports`` WHERE `board` = :id');
Expand Down
89 changes: 62 additions & 27 deletions inc/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ function displayBan($ban) {

if ($ban['post'] && isset($ban['post']['board'], $ban['post']['id'])) {
if (openBoard($ban['post']['board'])) {
$query = query(sprintf("SELECT `files` FROM ``posts_%s`` WHERE `id` = " .
$query = query(sprintf("SELECT `files` FROM ``posts`` WHERE `board` = '%s' AND `id` = " .

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

query -> prepare

(int)$ban['post']['id'], $board['uri']));
if ($_post = $query->fetch(PDO::FETCH_ASSOC)) {
$ban['post'] = array_merge($ban['post'], $_post);
Expand Down Expand Up @@ -842,8 +842,9 @@ function threadLocked($id) {
if (event('check-locked', $id))
return true;

$query = prepare(sprintf("SELECT `locked` FROM ``posts_%s`` WHERE `id` = :id AND `thread` IS NULL LIMIT 1", $board['uri']));
$query = prepare("SELECT `locked` FROM ``posts`` WHERE `board` = :board AND `id` = :id AND `thread` IS NULL LIMIT 1");
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->bindValue(':board', $board['uri']);
$query->execute() or error(db_error());

if (($locked = $query->fetchColumn()) === false) {
Expand All @@ -860,8 +861,9 @@ function threadSageLocked($id) {
if (event('check-sage-locked', $id))
return true;

$query = prepare(sprintf("SELECT `sage` FROM ``posts_%s`` WHERE `id` = :id AND `thread` IS NULL LIMIT 1", $board['uri']));
$query = prepare("SELECT `sage` FROM ``posts`` WHERE `board` = :board AND `id` = :id AND `thread` IS NULL LIMIT 1");
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->bindValue(':board', $board['uri'], PDO::PARAM_INT);
$query->execute() or error(db_error());

if (($sagelocked = $query->fetchColumn()) === false) {
Expand All @@ -875,8 +877,9 @@ function threadSageLocked($id) {
function threadExists($id) {
global $board;

$query = prepare(sprintf("SELECT 1 FROM ``posts_%s`` WHERE `id` = :id AND `thread` IS NULL LIMIT 1", $board['uri']));
$query = prepare("SELECT 1 FROM ``posts`` WHERE `board` = :board AND `id` = :id AND `thread` IS NULL LIMIT 1");
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->bindValue(':board', $board['uri']);
$query->execute() or error(db_error());

if ($query->rowCount()) {
Expand Down Expand Up @@ -904,7 +907,10 @@ function insertFloodPost(array $post) {

function post(array $post) {
global $pdo, $board;
$query = prepare(sprintf("INSERT INTO ``posts_%s`` VALUES ( NULL, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, 0, :embed, NULL)", $board['uri']));

$query = prepare("INSERT INTO ``posts`` (`board`, `thread`, `subject`, `email`, `name`, `trip`, `capcode`, `body`, `body_nomarkup`, `time`, `bump`, `files`, `num_files`, `filehash`, `password`, `ip`, `sticky`, `locked`, `sage`, `embed`) VALUES (:board, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, 0, :embed)");

$query->bindValue(':board', $board['uri']);

// Basic stuff
if (!empty($post['subject'])) {
Expand Down Expand Up @@ -973,12 +979,25 @@ function post(array $post) {
$query->bindValue(':filehash', null, PDO::PARAM_NULL);
}

$pdo->beginTransaction();
if (!$query->execute()) {
undoImage($post);
error(db_error($query));
}
$lastInsertId = $pdo->lastInsertId();

$query = prepare("SELECT `id` FROM ``posts`` WHERE `id` = :id");
$query->bindValue(':id', $lastInsertId);

return $pdo->lastInsertId();
if(!$query->execute()) {
undoImage($post);
error(db_error($query));
}
$lastIdForBoard = $query->fetch(PDO::FETCH_COLUMN);

$pdo->commit();

return $lastIdForBoard;
}

function bumpThread($id) {
Expand All @@ -990,18 +1009,20 @@ function bumpThread($id) {
if ($config['try_smarter'])
$build_pages[] = thread_find_page($id);

$query = prepare(sprintf("UPDATE ``posts_%s`` SET `bump` = :time WHERE `id` = :id AND `thread` IS NULL", $board['uri']));
$query = prepare("UPDATE ``posts`` SET `bump` = :time WHERE `id` = :id AND `thread` IS NULL AND `board` = :board");
$query->bindValue(':time', time(), PDO::PARAM_INT);
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->bindValue(':board', $board['uri']);
$query->execute() or error(db_error($query));
}

// Remove file from post
function deleteFile($id, $remove_entirely_if_already=true, $file=null) {
global $board, $config;

$query = prepare(sprintf("SELECT `thread`, `files`, `num_files` FROM ``posts_%s`` WHERE `id` = :id LIMIT 1", $board['uri']));
$query = prepare("SELECT `thread`, `files`, `num_files` FROM ``posts`` WHERE `id` = :id AND `board` = :board LIMIT 1");
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->bindValue(':board', $board['uri']);
$query->execute() or error(db_error($query));
if (!$post = $query->fetch(PDO::FETCH_ASSOC))
error($config['error']['invalidpost']);
Expand All @@ -1011,7 +1032,7 @@ function deleteFile($id, $remove_entirely_if_already=true, $file=null) {
if ($files[0]->file == 'deleted' && $post['num_files'] == 1 && !$post['thread'])
return; // Can't delete OP's image completely.

$query = prepare(sprintf("UPDATE ``posts_%s`` SET `files` = :file WHERE `id` = :id", $board['uri']));
$query = prepare("UPDATE ``posts`` SET `files` = :file WHERE `id` = :id AND `board` = :board");
if (($file && $file_to_delete->file == 'deleted') && $remove_entirely_if_already) {
// Already deleted; remove file fully
$files[$file] = null;
Expand All @@ -1032,6 +1053,7 @@ function deleteFile($id, $remove_entirely_if_already=true, $file=null) {
$query->bindValue(':file', json_encode($files), PDO::PARAM_STR);

$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->bindValue(':board', $board['uri']);
$query->execute() or error(db_error($query));

if ($post['thread'])
Expand All @@ -1044,18 +1066,20 @@ function deleteFile($id, $remove_entirely_if_already=true, $file=null) {
function rebuildPost($id) {
global $board;

$query = prepare(sprintf("SELECT `body_nomarkup`, `thread` FROM ``posts_%s`` WHERE `id` = :id", $board['uri']));
$query = prepare("SELECT `body_nomarkup`, `thread` FROM ``posts`` WHERE `id` = :id AND `board` = :board");
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->bindValue(':board', $board['uri']);
$query->execute() or error(db_error($query));

if ((!$post = $query->fetch(PDO::FETCH_ASSOC)) || !$post['body_nomarkup'])
return false;

markup($body = &$post['body_nomarkup']);

$query = prepare(sprintf("UPDATE ``posts_%s`` SET `body` = :body WHERE `id` = :id", $board['uri']));
$query = prepare("UPDATE ``posts`` SET `body` = :body WHERE `id` = :id AND `board` = :board");
$query->bindValue(':body', $body);
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->bindValue(':board', $board['uri']);
$query->execute() or error(db_error($query));

buildThread($post['thread'] ? $post['thread'] : $id);
Expand All @@ -1068,8 +1092,9 @@ function deletePost($id, $error_if_doesnt_exist=true, $rebuild_after=true) {
global $board, $config;

// Select post and replies (if thread) in one query
$query = prepare(sprintf("SELECT `id`,`thread`,`files` FROM ``posts_%s`` WHERE `id` = :id OR `thread` = :id", $board['uri']));
$query = prepare("SELECT `id`,`thread`,`files` FROM ``posts`` WHERE `board` = :board AND (`id` = :id OR `thread` = :id)");
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->bindValue(':board', $board['uri']);
$query->execute() or error(db_error($query));

if ($query->rowCount() < 1) {
Expand Down Expand Up @@ -1112,8 +1137,9 @@ function deletePost($id, $error_if_doesnt_exist=true, $rebuild_after=true) {

}

$query = prepare(sprintf("DELETE FROM ``posts_%s`` WHERE `id` = :id OR `thread` = :id", $board['uri']));
$query = prepare("DELETE FROM ``posts`` WHERE `board` = :board AND (`id` = :id OR `thread` = :id)");
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->bindValue(':board', $board['uri']);
$query->execute() or error(db_error($query));

$query = prepare("SELECT `board`, `post` FROM ``cites`` WHERE `target_board` = :board AND (`target` = " . implode(' OR `target` = ', $ids) . ") ORDER BY `board`");
Expand Down Expand Up @@ -1148,7 +1174,8 @@ function clean() {
$offset = round($config['max_pages']*$config['threads_per_page']);

// I too wish there was an easier way of doing this...
$query = prepare(sprintf("SELECT `id` FROM ``posts_%s`` WHERE `thread` IS NULL ORDER BY `sticky` DESC, `bump` DESC LIMIT :offset, 9001", $board['uri']));
$query = prepare("SELECT `id` FROM ``posts`` WHERE `board` = :board AND `thread` IS NULL ORDER BY `sticky` DESC, `bump` DESC LIMIT :offset, 9001");
$query->bindValue(':board', $board['uri']);
$query->bindValue(':offset', $offset, PDO::PARAM_INT);

$query->execute() or error(db_error($query));
Expand All @@ -1160,7 +1187,7 @@ function clean() {
function thread_find_page($thread) {
global $config, $board;

$query = query(sprintf("SELECT `id` FROM ``posts_%s`` WHERE `thread` IS NULL ORDER BY `sticky` DESC, `bump` DESC", $board['uri'])) or error(db_error($query));
$query = query(sprintf("SELECT `id` FROM ``posts`` WHERE `thread` IS NULL AND `board` = '%s' ORDER BY `sticky` DESC, `bump` DESC", $board['uri'])) or error(db_error($query));
$threads = $query->fetchAll(PDO::FETCH_COLUMN);
if (($index = array_search($thread, $threads)) === false)
return false;
Expand All @@ -1173,7 +1200,8 @@ function index($page, $mod=false) {
$body = '';
$offset = round($page*$config['threads_per_page']-$config['threads_per_page']);

$query = prepare(sprintf("SELECT * FROM ``posts_%s`` WHERE `thread` IS NULL ORDER BY `sticky` DESC, `bump` DESC LIMIT :offset,:threads_per_page", $board['uri']));
$query = prepare("SELECT * FROM ``posts`` WHERE `thread` IS NULL AND `board` = :board ORDER BY `sticky` DESC, `bump` DESC LIMIT :offset,:threads_per_page");
$query->bindValue(':board', $board['uri']);
$query->bindValue(':offset', $offset, PDO::PARAM_INT);
$query->bindValue(':threads_per_page', $config['threads_per_page'], PDO::PARAM_INT);
$query->execute() or error(db_error($query));
Expand All @@ -1199,8 +1227,9 @@ function index($page, $mod=false) {
}
}
if (!isset($cached)) {
$posts = prepare(sprintf("SELECT * FROM ``posts_%s`` WHERE `thread` = :id ORDER BY `id` DESC LIMIT :limit", $board['uri']));
$posts = prepare("SELECT * FROM ``posts`` WHERE `thread` = :id AND `board` = :board ORDER BY `id` DESC LIMIT :limit");
$posts->bindValue(':id', $th['id']);
$posts->bindValue(':board', $board['uri']);
$posts->bindValue(':limit', ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']), PDO::PARAM_INT);
$posts->execute() or error(db_error($posts));

Expand Down Expand Up @@ -1304,7 +1333,7 @@ function getPages($mod=false) {
$count = $board['thread_count'];
} else {
// Count threads
$query = query(sprintf("SELECT COUNT(*) FROM ``posts_%s`` WHERE `thread` IS NULL", $board['uri'])) or error(db_error());
$query = query(sprintf("SELECT COUNT(*) FROM ``posts`` WHERE `thread` IS NULL AND `board` = '%s'", $board['uri'])) or error(db_error());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sprintf query -> prepare

$count = $query->fetchColumn();
}
$count = floor(($config['threads_per_page'] + $count - 1) / $config['threads_per_page']);
Expand Down Expand Up @@ -1381,8 +1410,9 @@ function checkRobot($body) {
// Returns an associative array with 'replies' and 'images' keys
function numPosts($id) {
global $board;
$query = prepare(sprintf("SELECT COUNT(*) AS `replies`, SUM(`num_files`) AS `images` FROM ``posts_%s`` WHERE `thread` = :thread", $board['uri'], $board['uri']));
$query = prepare("SELECT COUNT(*) AS `replies`, SUM(`num_files`) AS `images` FROM ``posts`` WHERE `thread` = :thread AND `board` = :board");
$query->bindValue(':thread', $id, PDO::PARAM_INT);
$query->bindValue(':board', $board['uri']);
$query->execute() or error(db_error($query));

return $query->fetch(PDO::FETCH_ASSOC);
Expand Down Expand Up @@ -1766,7 +1796,7 @@ function markup(&$body, $track_cites = false) {
}
$search_cites = array_unique($search_cites);

$query = query(sprintf('SELECT `thread`, `id` FROM ``posts_%s`` WHERE ' .
$query = query(sprintf('SELECT `thread`, `id` FROM ``posts`` WHERE `board` = "%s" ' .

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sprintf -> prepare

implode(' OR ', $search_cites), $board['uri'])) or error(db_error());

$cited_posts = array();
Expand Down Expand Up @@ -1851,7 +1881,7 @@ function markup(&$body, $track_cites = false) {
if (!empty($clauses)) {
$cited_posts[$_board] = array();

$query = query(sprintf('SELECT `thread`, `id` FROM ``posts_%s`` WHERE ' .
$query = query(sprintf('SELECT `thread`, `id` FROM ``posts`` WHERE `board` = "%s"' .

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sprintf -> prepare

implode(' OR ', $clauses), $board['uri'])) or error(db_error());

while ($cite = $query->fetch(PDO::FETCH_ASSOC)) {
Expand Down Expand Up @@ -1996,8 +2026,9 @@ function buildThread($id, $return = false, $mod = false) {
cache::delete("thread_{$board['uri']}_{$id}");
}

$query = prepare(sprintf("SELECT * FROM ``posts_%s`` WHERE (`thread` IS NULL AND `id` = :id) OR `thread` = :id ORDER BY `thread`,`id`", $board['uri']));
$query = prepare("SELECT * FROM ``posts`` WHERE `board` = :board AND ((`thread` IS NULL AND `id` = :id) OR `thread` = :id) ORDER BY `thread`,`id`");
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->bindValue(':board', $board['uri']);
$query->execute() or error(db_error($query));

while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
Expand Down Expand Up @@ -2060,8 +2091,9 @@ function buildThread50($id, $return = false, $mod = false, $thread = null, $anti
$antibot->reset();

if (!$thread) {
$query = prepare(sprintf("SELECT * FROM ``posts_%s`` WHERE (`thread` IS NULL AND `id` = :id) OR `thread` = :id ORDER BY `thread`,`id` DESC LIMIT :limit", $board['uri']));
$query = prepare("SELECT * FROM ``posts`` WHERE `board` = :board AND (`thread` IS NULL AND `id` = :id) OR `thread` = :id ORDER BY `thread`,`id` DESC LIMIT :limit");
$query->bindValue(':id', $id, PDO::PARAM_INT);
$query->bindValue(':board', $board['uri']);
$query->bindValue(':limit', $config['noko50_count']+1, PDO::PARAM_INT);
$query->execute() or error(db_error($query));

Expand All @@ -2083,8 +2115,9 @@ function buildThread50($id, $return = false, $mod = false, $thread = null, $anti


if ($query->rowCount() == $config['noko50_count']+1) {
$count = prepare(sprintf("SELECT COUNT(`id`) as `num` FROM ``posts_%s`` WHERE `thread` = :thread UNION ALL
SELECT SUM(`num_files`) FROM ``posts_%s`` WHERE `files` IS NOT NULL AND `thread` = :thread", $board['uri'], $board['uri']));
$count = prepare("SELECT COUNT(`id`) as `num` FROM ``posts`` WHERE `board` = :board AND `thread` = :thread UNION ALL
SELECT SUM(`num_files`) FROM ``posts`` WHERE `board` = :board AND `files` IS NOT NULL AND `thread` = :thread");
$count->bindValue(':board', $board['uri']);
$count->bindValue(':thread', $id, PDO::PARAM_INT);
$count->execute() or error(db_error($count));

Expand Down Expand Up @@ -2225,7 +2258,8 @@ function fraction($numerator, $denominator, $sep) {

function getPostByHash($hash) {
global $board;
$query = prepare(sprintf("SELECT `id`,`thread` FROM ``posts_%s`` WHERE `filehash` = :hash", $board['uri']));
$query = prepare("SELECT `id`,`thread` FROM ``posts`` WHERE `board` = :board AND `filehash` = :hash");
$query->bindValue(':board', $board['uri']);
$query->bindValue(':hash', $hash, PDO::PARAM_STR);
$query->execute() or error(db_error($query));

Expand All @@ -2238,7 +2272,8 @@ function getPostByHash($hash) {

function getPostByHashInThread($hash, $thread) {
global $board;
$query = prepare(sprintf("SELECT `id`,`thread` FROM ``posts_%s`` WHERE `filehash` = :hash AND ( `thread` = :thread OR `id` = :thread )", $board['uri']));
$query = prepare("SELECT `id`,`thread` FROM ``posts`` WHERE `board` = :board AND `filehash` = :hash AND ( `thread` = :thread OR `id` = :thread )");
$query->bindValue(':board', $board['uri']);
$query->bindValue(':hash', $hash, PDO::PARAM_STR);
$query->bindValue(':thread', $thread, PDO::PARAM_INT);
$query->execute() or error(db_error($query));
Expand Down
2 changes: 1 addition & 1 deletion inc/instance-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ function purify($s){
openBoard($b);
buildIndex();
buildJavascript();
$query = query(sprintf("SELECT `id` FROM ``posts_%s`` WHERE `thread` IS NULL", $b)) or error(db_error());
$query = query(sprintf("SELECT `id` FROM ``posts`` WHERE `board` = '%s' AND `thread` IS NULL", $b)) or error(db_error());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sprintf -> prepare

while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
buildThread($post['id']);
}
Expand Down
Loading