From 830640d63c86859be669399b84169efee16ddfd3 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Thu, 25 Sep 2014 08:57:05 -0300 Subject: [PATCH 01/33] Adds the table posts to install.sql. --- install.sql | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/install.sql b/install.sql index 969107a22..0762f231b 100644 --- a/install.sql +++ b/install.sql @@ -77,6 +77,45 @@ INSERT INTO `boards` VALUES -- -------------------------------------------------------- +-- +-- Table structure for table `posts` +-- + +CREATE TABLE IF NOT EXISTS `posts` ( + `id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `board` varchar(58) NOT NULL, + `thread` int(11) DEFAULT NULL, + `id_for_board` int(11) unsigned NOT NULL, + `subject` varchar(100) DEFAULT NULL, + `email` varchar(30) DEFAULT NULL, + `name` varchar(35) DEFAULT NULL, + `trip` varchar(15) DEFAULT NULL, + `capcode` varchar(50) DEFAULT NULL, + `body` text NOT NULL, + `body_nomarkup` text, + `time` int(11) NOT NULL, + `bump` int(11) DEFAULT NULL, + `files` text, + `num_files` int(11) DEFAULT '0', + `filehash` text CHARACTER SET ascii, + `password` varchar(20) DEFAULT NULL, + `ip` varchar(39) CHARACTER SET ascii NOT NULL, + `sticky` int(1) NOT NULL, + `locked` int(1) NOT NULL, + `sage` int(1) NOT NULL, + `embed` text, + PRIMARY KEY (`id`), + UNIQUE KEY `id` (`id`), + UNIQUE KEY `board_id_for_board` (`board`,`id_for_board`), + KEY `thread_id` (`thread`,`id`), + KEY `filehash` (`filehash`(40)), + KEY `time` (`time`), + KEY `ip` (`ip`), + KEY `list_threads` (`thread`,`sticky`,`bump`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1; + +-- -------------------------------------------------------- + -- -- Table structure for table `cites` -- From 62f0a4a89db8299749784192d143ff3b9d8c33c2 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Thu, 25 Sep 2014 08:59:57 -0300 Subject: [PATCH 02/33] Changes the post function in inc/functions.php Makes the post function write to the posts table instead of the posts_%s table. --- inc/functions.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/inc/functions.php b/inc/functions.php index 72c2e974b..19c740409 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -882,7 +882,12 @@ 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)", $board['uri'])); + $query = prepare(sprintf("START TRANSACTION; +SELECT @id_for_board := COALESCE((SELECT MAX(`id_for_board`) FROM `posts` WHERE board='%s'),0); +SET @id_for_board = @id_for_board + 1; +INSERT INTO `posts` (`id_for_board`, `board`, `thread`, `subject`, `email`, `name`, `trip`, `capcode`, `body`, `body_nomarkup`, `time`, `bump`, `files`, `num_files`, `filehash`, `password`, `ip`, `sticky`, `locked`, `sage`, `embed`) VALUES(@id_for_board, :board, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, 0, :embed); COMMIT;", $board['uri'])); + + $query->bindValue(':board', $board['uri']); // Basic stuff if (!empty($post['subject'])) { From 05a4fdfaddc0040dcf2aa293e8791739d2a397b0 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Thu, 25 Sep 2014 11:54:08 -0300 Subject: [PATCH 03/33] Fixes the post function to return the right id. --- inc/functions.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index 19c740409..a136ff8fe 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -882,10 +882,8 @@ function insertFloodPost(array $post) { function post(array $post) { global $pdo, $board; - $query = prepare(sprintf("START TRANSACTION; -SELECT @id_for_board := COALESCE((SELECT MAX(`id_for_board`) FROM `posts` WHERE board='%s'),0); -SET @id_for_board = @id_for_board + 1; -INSERT INTO `posts` (`id_for_board`, `board`, `thread`, `subject`, `email`, `name`, `trip`, `capcode`, `body`, `body_nomarkup`, `time`, `bump`, `files`, `num_files`, `filehash`, `password`, `ip`, `sticky`, `locked`, `sage`, `embed`) VALUES(@id_for_board, :board, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, 0, :embed); COMMIT;", $board['uri'])); + + $query = prepare(sprintf("INSERT INTO `posts` (`id_for_board`, `board`, `thread`, `subject`, `email`, `name`, `trip`, `capcode`, `body`, `body_nomarkup`, `time`, `bump`, `files`, `num_files`, `filehash`, `password`, `ip`, `sticky`, `locked`, `sage`, `embed`) SELECT 1 + coalesce((SELECT max(`id_for_board`) FROM `posts` WHERE board='%s'),0), :board, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, 0, :embed", $board['uri'])); $query->bindValue(':board', $board['uri']); @@ -956,12 +954,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(); - return $pdo->lastInsertId(); + $query = prepare("SELECT `id_for_board` FROM `posts` WHERE `id` = :id"); + $query->bindValue(':id', $lastInsertId); + + if(!$query->execute()) { + undoImage($post); + error(db_error($query)); + } + $lastIdForBoard = $query->fetch(PDO::FETCH_COLUMN); + + $pdo->commit(); + + return $lastIdForBoard; } function bumpThread($id) { From 4818406585ce98c51ebde4fc9d2e245941d9945b Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Thu, 25 Sep 2014 11:54:42 -0300 Subject: [PATCH 04/33] Changes the templates to use id_for_board. --- inc/display.php | 4 ++-- templates/post/file_controls.html | 4 ++-- templates/post/post_controls.html | 28 +++++++++++++-------------- templates/post_reply.html | 12 ++++++------ templates/post_thread.html | 18 ++++++++--------- templates/themes/catalog/catalog.html | 2 +- 6 files changed, 34 insertions(+), 34 deletions(-) mode change 100644 => 100755 templates/post_reply.html mode change 100644 => 100755 templates/post_thread.html diff --git a/inc/display.php b/inc/display.php index 1a78e19b1..f7e697e59 100644 --- a/inc/display.php +++ b/inc/display.php @@ -383,7 +383,7 @@ public function __construct($post, $root=null, $mod=false) { public function link($pre = '', $page = false) { global $config, $board; - return $this->root . $board['dir'] . $config['dir']['res'] . sprintf(($page ? $page : $config['file_page']), $this->thread) . '#' . $pre . $this->id; + return $this->root . $board['dir'] . $config['dir']['res'] . sprintf(($page ? $page : $config['file_page']), $this->thread) . '#' . $pre . $this->id_for_board; } public function build($index=false) { @@ -438,7 +438,7 @@ public function __construct($post, $root = null, $mod = false, $hr = true) { public function link($pre = '', $page = false) { global $config, $board; - return $this->root . $board['dir'] . $config['dir']['res'] . sprintf(($page ? $page : $config['file_page']), $this->id) . '#' . $pre . $this->id; + return $this->root . $board['dir'] . $config['dir']['res'] . sprintf(($page ? $page : $config['file_page']), $this->id_for_board) . '#' . $pre . $this->id_for_board; } public function add(Post $post) { $this->posts[] = $post; diff --git a/templates/post/file_controls.html b/templates/post/file_controls.html index 6855a2931..679db613f 100644 --- a/templates/post/file_controls.html +++ b/templates/post/file_controls.html @@ -1,10 +1,10 @@ {% if post.files and mod %} {% if file.file != 'deleted' and mod|hasPermission(config.mod.deletefile, board.uri) %} -{{ secure_link_confirm(config.mod.link_deletefile, 'Delete file'|trans, 'Are you sure you want to delete this file?'|trans, board.dir ~ 'deletefile/' ~ post.id ~ '/' ~ loop.index0 ) }}  +{{ secure_link_confirm(config.mod.link_deletefile, 'Delete file'|trans, 'Are you sure you want to delete this file?'|trans, board.dir ~ 'deletefile/' ~ post.id_for_board ~ '/' ~ loop.index0 ) }}  {% endif %} {% if file.file and file.file != 'deleted' and file.thumb != 'spoiler' and mod|hasPermission(config.mod.spoilerimage, board.uri) %} -{{ secure_link_confirm(config.mod.link_spoilerimage, 'Spoiler file'|trans, 'Are you sure you want to spoiler this file?'|trans, board.dir ~ 'spoiler/' ~ post.id ~ '/' ~ loop.index0 ) }} +{{ secure_link_confirm(config.mod.link_spoilerimage, 'Spoiler file'|trans, 'Are you sure you want to spoiler this file?'|trans, board.dir ~ 'spoiler/' ~ post.id_for_board ~ '/' ~ loop.index0 ) }} {% endif %} {% endif %} diff --git a/templates/post/post_controls.html b/templates/post/post_controls.html index 99a8484c5..9cdb393cf 100644 --- a/templates/post/post_controls.html +++ b/templates/post/post_controls.html @@ -2,41 +2,41 @@ {% if mod|hasPermission(config.mod.delete, board.uri) %} - {{ secure_link_confirm(config.mod.link_delete, 'Delete'|trans, 'Are you sure you want to delete this?'|trans, board.dir ~ 'delete/' ~ post.id) }}  + {{ secure_link_confirm(config.mod.link_delete, 'Delete'|trans, 'Are you sure you want to delete this?'|trans, board.dir ~ 'delete/' ~ post.id_for_board) }}  {% endif %} {% if mod|hasPermission(config.mod.deletebyip, board.uri) %} - {{ secure_link_confirm(config.mod.link_deletebyip, 'Delete all posts by IP'|trans, 'Are you sure you want to delete all posts by this IP address?'|trans, board.dir ~ 'deletebyip/' ~ post.id) }}  + {{ secure_link_confirm(config.mod.link_deletebyip, 'Delete all posts by IP'|trans, 'Are you sure you want to delete all posts by this IP address?'|trans, board.dir ~ 'deletebyip/' ~ post.id_for_board) }}  {% endif %} {% if mod|hasPermission(config.mod.deletebyip_global, board.uri) %} - {{ secure_link_confirm(config.mod.link_deletebyip_global, 'Delete all posts by IP across all boards'|trans, 'Are you sure you want to delete all posts by this IP address, across all boards?'|trans, board.dir ~ 'deletebyip/' ~ post.id ~ '/global') }}  + {{ secure_link_confirm(config.mod.link_deletebyip_global, 'Delete all posts by IP across all boards'|trans, 'Are you sure you want to delete all posts by this IP address, across all boards?'|trans, board.dir ~ 'deletebyip/' ~ post.id_for_board ~ '/global') }}  {% endif %} {% if mod|hasPermission(config.mod.ban, board.uri) %} - {{ config.mod.link_ban }}  + {{ config.mod.link_ban }}  {% endif %} {% if mod|hasPermission(config.mod.bandelete, board.uri) %} - {{ config.mod.link_bandelete }}  + {{ config.mod.link_bandelete }}  {% endif %} {% if not post.thread %} {% if mod|hasPermission(config.mod.sticky, board.uri) %} {% if post.sticky %} - {{ config.mod.link_desticky }}  + {{ config.mod.link_desticky }}  {% else %} - {{ config.mod.link_sticky }}  + {{ config.mod.link_sticky }}  {% endif %} {% endif %} {% if mod|hasPermission(config.mod.bumplock, board.uri) %} {% if post.sage %} - {{ config.mod.link_bumpunlock }}  + {{ config.mod.link_bumpunlock }}  {% else %} - {{ config.mod.link_bumplock }}  + {{ config.mod.link_bumplock }}  {% endif %} {% endif %} {% if mod|hasPermission(config.mod.lock, board.uri) %} {% if post.locked %} - {{ config.mod.link_unlock }}  + {{ config.mod.link_unlock }}  {% else %} - {{ config.mod.link_lock }}  + {{ config.mod.link_lock }}  {% endif %} {% endif %} @@ -44,13 +44,13 @@ {% if mod|hasPermission(config.mod.move, board.uri) %} {% if not post.thread %} - {{ config.mod.link_move }}  + {{ config.mod.link_move }}  {% else %} - {{ config.mod.link_move }}  + {{ config.mod.link_move }}  {% endif %} {% endif %} {% if mod|hasPermission(config.mod.editpost, board.uri) %} - {{ config.mod.link_editpost }}  + {{ config.mod.link_editpost }}  {% endif %} diff --git a/templates/post_reply.html b/templates/post_reply.html old mode 100644 new mode 100755 index e73432308..71bbfccaa --- a/templates/post_reply.html +++ b/templates/post_reply.html @@ -1,10 +1,10 @@ {% filter remove_whitespace %} {# tabs and new lines will be ignored #} -
+

- {% if not index %}{% endif %} - -

{% include 'post/fileinfo.html' %} {% include 'post/post_controls.html' %} diff --git a/templates/post_thread.html b/templates/post_thread.html old mode 100644 new mode 100755 index aa0f1f78a..5b2f5772e --- a/templates/post_thread.html +++ b/templates/post_thread.html @@ -1,13 +1,13 @@ {% filter remove_whitespace %} {# tabs and new lines will be ignored #} -
-{% if not index %}{% endif %} +
+{% if not index %}{% endif %} {% include 'post/fileinfo.html' %}
1%}style='clear:both'{%endif%}>

- -

diff --git a/templates/themes/catalog/catalog.html b/templates/themes/catalog/catalog.html index be02a80e6..9daa7095b 100644 --- a/templates/themes/catalog/catalog.html +++ b/templates/themes/catalog/catalog.html @@ -52,7 +52,7 @@

{{ settings.title }} (/{{ board }}/)

{% else %} + id="img-{{ post.id_for_board }}" data-subject="{% if post.subject %}{{ post.subject }}{% endif %}" data-name="{{ post.name }}" data-muhdifference="{{ post.muhdifference }}" data-last-reply="{% if post.last_reply %}{{ post.last_reply }}{% endif %}" data-last-subject="{% if post.last_reply_subject %}{{ post.last_reply_subject }}{% endif %}" data-last-name="{% if post.last_reply %}{{ post.last_reply_name }}{% endif %}" data-last-difference="{% if post.last_reply %}{{ post.last_reply_difference }}{% endif %}" class="{{post.board}} thread-image" title="{{post.bump|date('%b %d %H:%M')}}">

From 01f52e582b517606b8f598dd23993f770d8e21b7 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Thu, 25 Sep 2014 11:55:23 -0300 Subject: [PATCH 05/33] Updates rebuild.php to work with posts table. --- tools/rebuild.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tools/rebuild.php b/tools/rebuild.php index 3501f8f68..8664a1e22 100755 --- a/tools/rebuild.php +++ b/tools/rebuild.php @@ -78,19 +78,19 @@ continue; // do no more if($options['full']) { - $query = query(sprintf("SELECT `id` FROM ``posts_%s``", $board['uri'])) or error(db_error()); + $query = query(sprintf("SELECT `id_for_board` FROM `posts` WHERE `board` = '%s'", $board['uri'])) or error(db_error()); while($post = $query->fetch()) { if(!$options['quiet']) - echo "Rebuilding #{$post['id']}...\n"; - rebuildPost($post['id']); + echo "Rebuilding #{$post['id_for_board']}...\n"; + rebuildPost($post['id_for_board']); } } - $query = query(sprintf("SELECT `id` FROM ``posts_%s`` WHERE `thread` IS NULL", $board['uri'])) or error(db_error()); + $query = query(sprintf("SELECT `id_for_board` FROM `posts` WHERE `thread` IS NULL AND `board` = '%s'", $board['uri'])) or error(db_error()); while($post = $query->fetch()) { if(!$options['quiet']) - echo "Rebuilding #{$post['id']}...\n"; - buildThread($post['id']); + echo "Rebuilding #{$post['id_for_board']}...\n"; + buildThread($post['id_for_board']); } } From 51b7a15286f5ff282ed69d8de25fcdcf9a5e3a99 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Thu, 25 Sep 2014 12:21:25 -0300 Subject: [PATCH 06/33] A minor typo in functions.php --- inc/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/functions.php b/inc/functions.php index a136ff8fe..05c3ce5b8 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -883,7 +883,7 @@ function insertFloodPost(array $post) { function post(array $post) { global $pdo, $board; - $query = prepare(sprintf("INSERT INTO `posts` (`id_for_board`, `board`, `thread`, `subject`, `email`, `name`, `trip`, `capcode`, `body`, `body_nomarkup`, `time`, `bump`, `files`, `num_files`, `filehash`, `password`, `ip`, `sticky`, `locked`, `sage`, `embed`) SELECT 1 + coalesce((SELECT max(`id_for_board`) FROM `posts` WHERE board='%s'),0), :board, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, 0, :embed", $board['uri'])); + $query = prepare(sprintf("INSERT INTO `posts` (`id_for_board`, `board`, `thread`, `subject`, `email`, `name`, `trip`, `capcode`, `body`, `body_nomarkup`, `time`, `bump`, `files`, `num_files`, `filehash`, `password`, `ip`, `sticky`, `locked`, `sage`, `embed`) SELECT 1 + coalesce((SELECT max(`id_for_board`) FROM `posts` WHERE `board`='%s'),0), :board, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, 0, :embed", $board['uri'])); $query->bindValue(':board', $board['uri']); From b36dda2795bfbf6cfe910c3671db72d3039c527a Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Sat, 27 Sep 2014 11:33:18 -0300 Subject: [PATCH 07/33] boards.php now uses the posts table --- boards.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/boards.php b/boards.php index 46bddb388..6d9c82936 100644 --- a/boards.php +++ b/boards.php @@ -38,15 +38,18 @@ $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 FROM ``posts_%s`` -", $board['uri'], $board['uri'], $board['uri'], $board['uri'])); + $query = prepare(" + SELECT + (SELECT coalesce((SELECT max(`id_for_board`) 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 + 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']; From 503d3ea6254128f3b469e268da5240db2e01f860 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Sat, 27 Sep 2014 11:35:17 -0300 Subject: [PATCH 08/33] rebuildPost() now uses the posts table --- inc/functions.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index 05c3ce5b8..c7cc42b57 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -1035,11 +1035,11 @@ function deleteFile($id, $remove_entirely_if_already=true, $file=null) { } // rebuild post (markup) -function rebuildPost($id) { +function rebuildPost($id_for_board) { global $board; - $query = prepare(sprintf("SELECT `body_nomarkup`, `thread` FROM ``posts_%s`` WHERE `id` = :id", $board['uri'])); - $query->bindValue(':id', $id, PDO::PARAM_INT); + $query = prepare(sprintf("SELECT `body_nomarkup`, `thread` FROM `posts` WHERE `id_for_board` = :id_for_board AND `board` = '%s'", $board['uri'])); + $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); $query->execute() or error(db_error($query)); if ((!$post = $query->fetch(PDO::FETCH_ASSOC)) || !$post['body_nomarkup']) @@ -1047,12 +1047,12 @@ function rebuildPost($id) { markup($body = &$post['body_nomarkup']); - $query = prepare(sprintf("UPDATE ``posts_%s`` SET `body` = :body WHERE `id` = :id", $board['uri'])); + $query = prepare(sprintf("UPDATE `posts` SET `body` = :body WHERE `id_for_board` = :id_for_board AND `board` = '%s'", $board['uri'])); $query->bindValue(':body', $body); - $query->bindValue(':id', $id, PDO::PARAM_INT); + $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); $query->execute() or error(db_error($query)); - buildThread($post['thread'] ? $post['thread'] : $id); + buildThread($post['thread'] ? $post['thread'] : $id_for_board); return true; } From e03140061c71225016ab6602cf68de5d56a48773 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Sat, 27 Sep 2014 12:28:15 -0300 Subject: [PATCH 09/33] Applies `` to the table name and removes sprintf. --- boards.php | 10 +++++----- inc/functions.php | 18 +++++++++++------- 2 files changed, 16 insertions(+), 12 deletions(-) mode change 100644 => 100755 boards.php mode change 100644 => 100755 inc/functions.php diff --git a/boards.php b/boards.php old mode 100644 new mode 100755 index 6d9c82936..ccc74bcbe --- a/boards.php +++ b/boards.php @@ -40,11 +40,11 @@ foreach ($boards as $i => $board) { $query = prepare(" SELECT - (SELECT coalesce((SELECT max(`id_for_board`) 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 - FROM `posts` + (SELECT coalesce((SELECT max(`id_for_board`) 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 + FROM ``posts`` WHERE `board` = :board"); $query->bindValue(':board', $board['uri']); $pdo->beginTransaction(); diff --git a/inc/functions.php b/inc/functions.php old mode 100644 new mode 100755 index c7cc42b57..f976c8398 --- a/inc/functions.php +++ b/inc/functions.php @@ -883,7 +883,7 @@ function insertFloodPost(array $post) { function post(array $post) { global $pdo, $board; - $query = prepare(sprintf("INSERT INTO `posts` (`id_for_board`, `board`, `thread`, `subject`, `email`, `name`, `trip`, `capcode`, `body`, `body_nomarkup`, `time`, `bump`, `files`, `num_files`, `filehash`, `password`, `ip`, `sticky`, `locked`, `sage`, `embed`) SELECT 1 + coalesce((SELECT max(`id_for_board`) FROM `posts` WHERE `board`='%s'),0), :board, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, 0, :embed", $board['uri'])); + $query = prepare("INSERT INTO ``posts`` (`id_for_board`, `board`, `thread`, `subject`, `email`, `name`, `trip`, `capcode`, `body`, `body_nomarkup`, `time`, `bump`, `files`, `num_files`, `filehash`, `password`, `ip`, `sticky`, `locked`, `sage`, `embed`) SELECT 1 + coalesce((SELECT max(`id_for_board`) FROM ``posts`` WHERE `board`=:board),0), :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']); @@ -961,7 +961,7 @@ function post(array $post) { } $lastInsertId = $pdo->lastInsertId(); - $query = prepare("SELECT `id_for_board` FROM `posts` WHERE `id` = :id"); + $query = prepare("SELECT `id_for_board` FROM ``posts`` WHERE `id` = :id"); $query->bindValue(':id', $lastInsertId); if(!$query->execute()) { @@ -994,8 +994,9 @@ function bumpThread($id) { 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->bindValue(':id', $id, PDO::PARAM_INT); + $query = prepare("SELECT `thread`, `files`, `num_files` FROM ``posts`` WHERE `id_for_board` = :id_for_board AND `board` = :board LIMIT 1"); + $query->bindValue(':id_for_board', $id_for_board, 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']); @@ -1038,8 +1039,9 @@ function deleteFile($id, $remove_entirely_if_already=true, $file=null) { function rebuildPost($id_for_board) { global $board; - $query = prepare(sprintf("SELECT `body_nomarkup`, `thread` FROM `posts` WHERE `id_for_board` = :id_for_board AND `board` = '%s'", $board['uri'])); + $query = prepare("SELECT `body_nomarkup`, `thread` FROM ``posts`` WHERE `id_for_board` = :id_for_board AND `board` = :board"); $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $query->bindValue(':board', $board); $query->execute() or error(db_error($query)); if ((!$post = $query->fetch(PDO::FETCH_ASSOC)) || !$post['body_nomarkup']) @@ -1047,9 +1049,10 @@ function rebuildPost($id_for_board) { markup($body = &$post['body_nomarkup']); - $query = prepare(sprintf("UPDATE `posts` SET `body` = :body WHERE `id_for_board` = :id_for_board AND `board` = '%s'", $board['uri'])); + $query = prepare("UPDATE ``posts`` SET `body` = :body WHERE `id_for_board` = :id_for_board AND `board` = '%s'")); $query->bindValue(':body', $body); $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $query->bindValue(':board', $board['uri']); $query->execute() or error(db_error($query)); buildThread($post['thread'] ? $post['thread'] : $id_for_board); @@ -1167,7 +1170,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)); From 76c0a9871bb69114cf19b6e1c7e936ae848cd587 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Sat, 27 Sep 2014 12:29:28 -0300 Subject: [PATCH 10/33] Missed a parenthesis. --- inc/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/functions.php b/inc/functions.php index f976c8398..75d385ac5 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -1049,7 +1049,7 @@ function rebuildPost($id_for_board) { markup($body = &$post['body_nomarkup']); - $query = prepare("UPDATE ``posts`` SET `body` = :body WHERE `id_for_board` = :id_for_board AND `board` = '%s'")); + $query = prepare("UPDATE ``posts`` SET `body` = :body WHERE `id_for_board` = :id_for_board AND `board` = '%s'"); $query->bindValue(':body', $body); $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); $query->bindValue(':board', $board['uri']); From 9913834ee60a3ada4f00270993811726465d622a Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Sat, 27 Sep 2014 13:34:20 -0300 Subject: [PATCH 11/33] bumpThread() now uses the posts table --- inc/functions.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index 75d385ac5..7490fd47f 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -975,18 +975,19 @@ function post(array $post) { return $lastIdForBoard; } -function bumpThread($id) { +function bumpThread($id_for_board) { global $config, $board, $build_pages; - if (event('bump', $id)) + if (event('bump', $id_for_board)) return true; if ($config['try_smarter']) - $build_pages[] = thread_find_page($id); + $build_pages[] = thread_find_page($id_for_board); - $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_for_board` = :id_for_board AND `thread` IS NULL AND `board` = :board"); $query->bindValue(':time', time(), PDO::PARAM_INT); - $query->bindValue(':id', $id, PDO::PARAM_INT); + $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $query->bindValue(':board', $board['uri']); $query->execute() or error(db_error($query)); } From beb142efc159018225f946775045886da4b68f34 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Sat, 27 Sep 2014 13:35:26 -0300 Subject: [PATCH 12/33] deleteFile() and numPosts() now use the posts table --- inc/functions.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index 7490fd47f..348755097 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -992,7 +992,7 @@ function bumpThread($id_for_board) { } // Remove file from post -function deleteFile($id, $remove_entirely_if_already=true, $file=null) { +function deleteFile($id_for_board, $remove_entirely_if_already=true, $file=null) { global $board, $config; $query = prepare("SELECT `thread`, `files`, `num_files` FROM ``posts`` WHERE `id_for_board` = :id_for_board AND `board` = :board LIMIT 1"); @@ -1007,7 +1007,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_for_board` = :id_for_board AND `board` = :board"); if (($file && $file_to_delete->file == 'deleted') && $remove_entirely_if_already) { // Already deleted; remove file fully $files[$file] = null; @@ -1027,13 +1027,14 @@ 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(':id_for_board', $id_for_board, PDO::PARAM_INT); + $query->bindValue(':board', $board['uri']); $query->execute() or error(db_error($query)); if ($post['thread']) buildThread($post['thread']); else - buildThread($id); + buildThread($id_for_board); } // rebuild post (markup) @@ -1378,10 +1379,11 @@ function checkRobot($body) { } // Returns an associative array with 'replies' and 'images' keys -function numPosts($id) { +function numPosts($id_for_board) { 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->bindValue(':thread', $id, PDO::PARAM_INT); + $query = prepare("SELECT COUNT(*) AS `replies`, SUM(`num_files`) AS `images` FROM ``posts`` WHERE `thread` = :thread AND `board` = :board"); + $query->bindValue(':thread', $id_for_board, PDO::PARAM_INT); + $query->bindValue(':board', $board['uri']); $query->execute() or error(db_error($query)); return $query->fetch(PDO::FETCH_ASSOC); From 405a49a143d57111fd1828bb8a7f2bb5c157b1a4 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Sat, 27 Sep 2014 13:36:19 -0300 Subject: [PATCH 13/33] deletePost now uses the posts table --- inc/functions.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index 348755097..fb1e3dea9 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -1063,12 +1063,13 @@ function rebuildPost($id_for_board) { } // Delete a post (reply or thread) -function deletePost($id, $error_if_doesnt_exist=true, $rebuild_after=true) { +function deletePost($id_for_board, $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->bindValue(':id', $id, PDO::PARAM_INT); + $query = prepare("SELECT `id_for_board`,`thread`,`files` FROM ``posts`` WHERE `board` = :board AND (`id_for_board` = :id_for_board OR `thread` = :id_for_board)"); + $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $query->bindValue(':board', $board['uri']); $query->execute() or error(db_error($query)); if ($query->rowCount() < 1) { @@ -1085,13 +1086,13 @@ function deletePost($id, $error_if_doesnt_exist=true, $rebuild_after=true) { if (!$post['thread']) { // Delete thread HTML page - file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['id'])); - file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $post['id'])); - file_unlink($board['dir'] . $config['dir']['res'] . sprintf('%d.json', $post['id'])); + file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['id_for_board'])); + file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $post['id_for_board'])); + file_unlink($board['dir'] . $config['dir']['res'] . sprintf('%d.json', $post['id_for_board'])); $antispam_query = prepare('DELETE FROM ``antispam`` WHERE `board` = :board AND `thread` = :thread'); $antispam_query->bindValue(':board', $board['uri']); - $antispam_query->bindValue(':thread', $post['id']); + $antispam_query->bindValue(':thread', $post['id_for_board']); $antispam_query->execute() or error(db_error($antispam_query)); } elseif ($query->rowCount() == 1) { // Rebuild thread @@ -1107,12 +1108,13 @@ function deletePost($id, $error_if_doesnt_exist=true, $rebuild_after=true) { } } - $ids[] = (int)$post['id']; + $ids[] = (int)$post['id_for_board']; } - $query = prepare(sprintf("DELETE FROM ``posts_%s`` WHERE `id` = :id OR `thread` = :id", $board['uri'])); - $query->bindValue(':id', $id, PDO::PARAM_INT); + $query = prepare("DELETE FROM ``posts`` WHERE `board` = :board AND (`id_for_board` = :id_for_board OR `thread` = :id_for_board)"); + $query->bindValue(':id_for_board', $id_for_board, 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`"); From 6bdb15a7a40dad76b090de0a05dcd238bc1b3265 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Sat, 27 Sep 2014 13:37:05 -0300 Subject: [PATCH 14/33] Couple of typos in rebuildPost. --- inc/functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index fb1e3dea9..c08d14ed8 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -1043,7 +1043,7 @@ function rebuildPost($id_for_board) { $query = prepare("SELECT `body_nomarkup`, `thread` FROM ``posts`` WHERE `id_for_board` = :id_for_board AND `board` = :board"); $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); - $query->bindValue(':board', $board); + $query->bindValue(':board', $board['uri']); $query->execute() or error(db_error($query)); if ((!$post = $query->fetch(PDO::FETCH_ASSOC)) || !$post['body_nomarkup']) @@ -1051,7 +1051,7 @@ function rebuildPost($id_for_board) { markup($body = &$post['body_nomarkup']); - $query = prepare("UPDATE ``posts`` SET `body` = :body WHERE `id_for_board` = :id_for_board AND `board` = '%s'"); + $query = prepare("UPDATE ``posts`` SET `body` = :body WHERE `id_for_board` = :id_for_board AND `board` = :board"); $query->bindValue(':body', $body); $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); $query->bindValue(':board', $board['uri']); From a21b0bed54fdccc195431f9c782d71a01b63b4bf Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Sat, 27 Sep 2014 13:38:09 -0300 Subject: [PATCH 15/33] More functions now use posts table. index(), thread_find_page() and getPages() --- inc/functions.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index c08d14ed8..bad8a440f 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -1161,7 +1161,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; @@ -1192,7 +1192,7 @@ function index($page, $mod=false) { $thread = new Thread($th, $mod ? '?/' : $config['root'], $mod); if ($config['cache']['enabled']) { - $cached = cache::get("thread_index_{$board['uri']}_{$th['id']}"); + $cached = cache::get("thread_index_{$board['uri']}_{$th['id_for_board']}"); if (isset($cached['replies'], $cached['omitted'])) { $replies = $cached['replies']; $omitted = $cached['omitted']; @@ -1201,22 +1201,23 @@ 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->bindValue(':id', $th['id']); + $posts = prepare("SELECT * FROM ``posts`` WHERE `thread` = :id_for_board AND `board` = :board ORDER BY `id_for_board` DESC LIMIT :limit"); + $posts->bindValue(':id_for_board', $th['id_for_board']); + $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)); $replies = array_reverse($posts->fetchAll(PDO::FETCH_ASSOC)); if (count($replies) == ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview'])) { - $count = numPosts($th['id']); + $count = numPosts($th['id_for_board']); $omitted = array('post_count' => $count['replies'], 'image_count' => $count['images']); } else { $omitted = false; } if ($config['cache']['enabled']) - cache::set("thread_index_{$board['uri']}_{$th['id']}", array( + cache::set("thread_index_{$board['uri']}_{$th['id_for_board']}", array( 'replies' => $replies, 'omitted' => $omitted, )); @@ -1306,7 +1307,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()); $count = $query->fetchColumn(); } $count = floor(($config['threads_per_page'] + $count - 1) / $config['threads_per_page']); From cfb5066f88cc75c7ac5d77f40938956c87768809 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Sat, 27 Sep 2014 13:38:56 -0300 Subject: [PATCH 16/33] buildThread() now uses the posts table --- inc/functions.php | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index bad8a440f..140314f71 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -1995,21 +1995,22 @@ function strip_combining_chars($str) { return $str; } -function buildThread($id, $return = false, $mod = false) { +function buildThread($id_for_board, $return = false, $mod = false) { global $board, $config, $build_pages; - $id = round($id); + $id_for_board = round($id_for_board); - if (event('build-thread', $id)) + if (event('build-thread', $id_for_board)) return; if ($config['cache']['enabled'] && !$mod) { // Clear cache - cache::delete("thread_index_{$board['uri']}_{$id}"); - cache::delete("thread_{$board['uri']}_{$id}"); + cache::delete("thread_index_{$board['uri']}_{$id_for_board}"); + cache::delete("thread_{$board['uri']}_{$id_for_board}"); } - $query = prepare(sprintf("SELECT * FROM ``posts_%s`` WHERE (`thread` IS NULL AND `id` = :id) OR `thread` = :id ORDER BY `thread`,`id`", $board['uri'])); - $query->bindValue(':id', $id, PDO::PARAM_INT); + $query = prepare("SELECT * FROM ``posts`` WHERE `board` = :board AND ((`thread` IS NULL AND `id_for_board` = :id_for_board) OR `thread` = :id_for_board) ORDER BY `thread`,`id_for_board`"); + $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $query->bindValue(':board', $board['uri']); $query->execute() or error(db_error($query)); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { @@ -2025,14 +2026,14 @@ function buildThread($id, $return = false, $mod = false) { error($config['error']['nonexistant']); $hasnoko50 = $thread->postCount() >= $config['noko50_min']; - $antibot = $mod || $return ? false : create_antibot($board['uri'], $id); + $antibot = $mod || $return ? false : create_antibot($board['uri'], $id_for_board); $body = Element('thread.html', array( 'board' => $board, 'thread' => $thread, 'body' => $thread->build(), 'config' => $config, - 'id' => $id, + 'id' => $id_for_board, 'mod' => $mod, 'hasnoko50' => $hasnoko50, 'isnoko50' => false, @@ -2042,25 +2043,25 @@ function buildThread($id, $return = false, $mod = false) { )); if ($config['try_smarter'] && !$mod) - $build_pages[] = thread_find_page($id); + $build_pages[] = thread_find_page($id_for_board); // json api if ($config['api']['enabled']) { $api = new Api(); $json = json_encode($api->translateThread($thread)); - $jsonFilename = $board['dir'] . $config['dir']['res'] . $id . '.json'; + $jsonFilename = $board['dir'] . $config['dir']['res'] . $id_for_board . '.json'; file_write($jsonFilename, $json); } if ($return) { return $body; } else { - $noko50fn = $board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $id); + $noko50fn = $board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $id_for_board); if ($hasnoko50 || file_exists($noko50fn)) { - buildThread50($id, $return, $mod, $thread, $antibot); + buildThread50($id_for_board, $return, $mod, $thread, $antibot); } - file_write($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $id), $body); + file_write($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $id_for_board), $body); } } From 2ad9062e05bd38e08853c06a3c9092f220029e3b Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Sat, 27 Sep 2014 13:40:10 -0300 Subject: [PATCH 17/33] Fixes the table prefix syntax in tools/rebuild.php --- tools/rebuild.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/rebuild.php b/tools/rebuild.php index 8664a1e22..be9ec0f6f 100755 --- a/tools/rebuild.php +++ b/tools/rebuild.php @@ -78,7 +78,7 @@ continue; // do no more if($options['full']) { - $query = query(sprintf("SELECT `id_for_board` FROM `posts` WHERE `board` = '%s'", $board['uri'])) or error(db_error()); + $query = query(sprintf("SELECT `id_for_board` FROM ``posts`` WHERE `board` = '%s'", $board['uri'])) or error(db_error()); while($post = $query->fetch()) { if(!$options['quiet']) echo "Rebuilding #{$post['id_for_board']}...\n"; @@ -86,7 +86,7 @@ } } - $query = query(sprintf("SELECT `id_for_board` FROM `posts` WHERE `thread` IS NULL AND `board` = '%s'", $board['uri'])) or error(db_error()); + $query = query(sprintf("SELECT `id_for_board` FROM ``posts`` WHERE `thread` IS NULL AND `board` = '%s'", $board['uri'])) or error(db_error()); while($post = $query->fetch()) { if(!$options['quiet']) echo "Rebuilding #{$post['id_for_board']}...\n"; From f1973644326508e5c6b4a3eb1c17f23ec16cfe1f Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Sat, 27 Sep 2014 13:41:35 -0300 Subject: [PATCH 18/33] Deletion in post.php now uses the posts table --- post.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) mode change 100644 => 100755 post.php diff --git a/post.php b/post.php old mode 100644 new mode 100755 index 881895721..67cabe950 --- a/post.php +++ b/post.php @@ -50,16 +50,18 @@ function strip_array($var) { if (empty($delete)) error($config['error']['nodelete']); - foreach ($delete as &$id) { - $query = prepare(sprintf("SELECT `thread`, `time`,`password` FROM ``posts_%s`` WHERE `id` = :id", $board['uri'])); - $query->bindValue(':id', $id, PDO::PARAM_INT); + foreach ($delete as &$id_for_board) { + $query = prepare("SELECT `thread`, `time`,`password` FROM ``posts`` WHERE `id_for_board` = :id_for_board AND `board` = :board"); + $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $query->bindValue(':board', $board['uri']); $query->execute() or error(db_error($query)); if ($post = $query->fetch(PDO::FETCH_ASSOC)) { $thread = false; if ($config['user_moderation'] && $post['thread']) { - $thread_query = prepare(sprintf("SELECT `time`,`password` FROM ``posts_%s`` WHERE `id` = :id", $board['uri'])); - $thread_query->bindValue(':id', $post['thread'], PDO::PARAM_INT); + $thread_query = prepare("SELECT `time`,`password` FROM ``posts`` WHERE `id_for_board` = :id_for_board AND `board` = :board"); + $thread_query->bindValue(':id_for_board', $post['thread'], PDO::PARAM_INT); + $thread_query->bindValue(':board', $board['uri']); $thread_query->execute() or error(db_error($query)); $thread = $thread_query->fetch(PDO::FETCH_ASSOC); @@ -74,14 +76,14 @@ function strip_array($var) { if (isset($_POST['file'])) { // Delete just the file - deleteFile($id); + deleteFile($id_for_board); } else { // Delete entire post - deletePost($id); + deletePost($id_for_board); } _syslog(LOG_INFO, 'Deleted post: ' . - '/' . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['thread'] ? $post['thread'] : $id) . ($post['thread'] ? '#' . $id : '') + '/' . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['thread'] ? $post['thread'] : $id_for_board) . ($post['thread'] ? '#' . $id_for_board : '') ); } } From f81f10a440af4a342104ed51ce7b292e20959a2b Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Sat, 27 Sep 2014 13:42:55 -0300 Subject: [PATCH 19/33] More changes in post.php --- post.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/post.php b/post.php index 67cabe950..506f0789a 100755 --- a/post.php +++ b/post.php @@ -253,8 +253,9 @@ function strip_array($var) { //Check if thread exists if (!$post['op']) { - $query = prepare(sprintf("SELECT `sticky`,`locked`,`sage` FROM ``posts_%s`` WHERE `id` = :id AND `thread` IS NULL LIMIT 1", $board['uri'])); - $query->bindValue(':id', $post['thread'], PDO::PARAM_INT); + $query = prepare("SELECT `sticky`,`locked`,`sage` FROM ``posts`` WHERE `id_for_board` = :id_for_board AND `thread` IS NULL AND `board` = :board LIMIT 1"); + $query->bindValue(':id_for_board', $post['thread'], PDO::PARAM_INT); + $query->bindValue(':board', $board['uri']); $query->execute() or error(db_error()); if (!$thread = $query->fetch(PDO::FETCH_ASSOC)) { @@ -794,7 +795,7 @@ function ipv4to6($ip) { $post['files'] = $post['files']; $post['num_files'] = sizeof($post['files']); - $post['id'] = $id = post($post); + $post['id_for_board'] = $id_for_board = post($post); insertFloodPost($post); @@ -806,7 +807,7 @@ function ipv4to6($ip) { $insert_rows = array(); foreach ($post['tracked_cites'] as $cite) { $insert_rows[] = '(' . - $pdo->quote($board['uri']) . ', ' . (int)$id . ', ' . + $pdo->quote($board['uri']) . ', ' . (int)$id_for_board . ', ' . $pdo->quote($cite[0]) . ', ' . (int)$cite[1] . ')'; } query('INSERT INTO ``cites`` VALUES ' . implode(', ', $insert_rows)) or error(db_error()); @@ -816,7 +817,7 @@ function ipv4to6($ip) { bumpThread($post['thread']); } - buildThread($post['op'] ? $id : $post['thread']); + buildThread($post['op'] ? $id_for_board : $post['thread']); if ($config['try_smarter'] && $post['op']) $build_pages = range(1, $config['max_pages']); @@ -844,7 +845,7 @@ function ipv4to6($ip) { if ($noko) { $redirect = $root . $board['dir'] . $config['dir']['res'] . - sprintf($config['file_page'], $post['op'] ? $id:$post['thread']) . (!$post['op'] ? '#' . $id : ''); + sprintf($config['file_page'], $post['op'] ? $id_for_board:$post['thread']) . (!$post['op'] ? '#' . $id_for_board : ''); if (!$post['op'] && isset($_SERVER['HTTP_REFERER'])) { $regex = array( @@ -856,7 +857,7 @@ function ipv4to6($ip) { if (preg_match('/\/' . $regex['board'] . $regex['res'] . $regex['page50'] . '([?&].*)?$/', $_SERVER['HTTP_REFERER'])) { $redirect = $root . $board['dir'] . $config['dir']['res'] . - sprintf($config['file_page50'], $post['op'] ? $id:$post['thread']) . (!$post['op'] ? '#' . $id : ''); + sprintf($config['file_page50'], $post['op'] ? $id_for_board:$post['thread']) . (!$post['op'] ? '#' . $id_for_board : ''); } } } else { @@ -866,7 +867,7 @@ function ipv4to6($ip) { if ($config['syslog']) _syslog(LOG_INFO, 'New post: /' . $board['dir'] . $config['dir']['res'] . - sprintf($config['file_page'], $post['op'] ? $id : $post['thread']) . (!$post['op'] ? '#' . $id : '')); + sprintf($config['file_page'], $post['op'] ? $id_for_board : $post['thread']) . (!$post['op'] ? '#' . $id_for_board : '')); if (!$post['mod']) header('X-Associated-Content: "' . $redirect . '"'); @@ -882,7 +883,7 @@ function ipv4to6($ip) { echo json_encode(array( 'redirect' => $redirect, 'noko' => $noko, - 'id' => $id + 'id' => $id_for_board )); } } elseif (isset($_POST['appeal'])) { From e2fbedd878878ab4a7e41d99eeda050bb7b247e9 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Mon, 29 Sep 2014 07:51:41 -0300 Subject: [PATCH 20/33] tools/stats.php now uses the posts table --- tools/stats.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/stats.php b/tools/stats.php index cec8d2ab2..00280f7ba 100755 --- a/tools/stats.php +++ b/tools/stats.php @@ -20,7 +20,7 @@ printf("%10s || ", $f['uri']); foreach ($variants as $iter) { list($term, $time) = $iter; - $qq = query(sprintf("SELECT COUNT(*) as count FROM ``posts_%s`` WHERE time > %d", $f['uri'], time()-$time)); + $qq = query(sprintf("SELECT COUNT(*) as count FROM ``posts`` WHERE `board` = '%s' AND time > %d", $f['uri'], time()-$time)); $c = $qq->fetch()['count']; printf("%8d | ", $c); From f69c8563b13f5e7dca330f58a737f962070e9ee8 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Mon, 29 Sep 2014 08:45:18 -0300 Subject: [PATCH 21/33] Catalog now uses the posts table. --- templates/themes/catalog/theme.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/templates/themes/catalog/theme.php b/templates/themes/catalog/theme.php index 5b9401679..c1ad42ad3 100644 --- a/templates/themes/catalog/theme.php +++ b/templates/themes/catalog/theme.php @@ -39,14 +39,14 @@ public function build($settings, $board_name) { $recent_posts = array(); $stats = array(); - $query = query(sprintf("SELECT *, `id` AS `thread_id`, - (SELECT COUNT(`id`) FROM ``posts_%s`` WHERE `thread` = `thread_id`) AS `reply_count`, - (SELECT SUM(`num_files`) FROM ``posts_%s`` WHERE `thread` = `thread_id` AND `num_files` IS NOT NULL) AS `image_count`, - '%s' AS `board` FROM ``posts_%s`` WHERE `thread` IS NULL ORDER BY `bump` DESC", + $query = query(sprintf("SELECT *, `id_for_board` AS `thread_id`, + (SELECT COUNT(`id_for_board`) FROM ``posts`` WHERE `board` = '%s' AND `thread` = `thread_id`) AS `reply_count`, + (SELECT SUM(`num_files`) FROM ``posts`` WHERE `board` = '%s' AND `thread` = `thread_id` AND `num_files` IS NOT NULL) AS `image_count` + FROM ``posts`` WHERE `board` = '%s' AND `thread` IS NULL ORDER BY `bump` DESC", $board_name, $board_name, $board_name, $board_name, $board_name)) or error(db_error()); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { - $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], ($post['thread'] ? $post['thread'] : $post['id'])); + $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], ($post['thread'] ? $post['thread'] : $post['id_for_board'])); $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)) { From d4984d398c2906c03bfd9d10ef8ba1b176d10482 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Mon, 29 Sep 2014 08:56:17 -0300 Subject: [PATCH 22/33] Recent theme now uses the posts table. --- templates/themes/recent/.theme.php.swp | Bin 0 -> 16384 bytes templates/themes/recent/theme.php | 14 +++++++------- 2 files changed, 7 insertions(+), 7 deletions(-) create mode 100644 templates/themes/recent/.theme.php.swp diff --git a/templates/themes/recent/.theme.php.swp b/templates/themes/recent/.theme.php.swp new file mode 100644 index 0000000000000000000000000000000000000000..b94ee4935e68b36ebac89fe64c936df0abc7e95e GIT binary patch literal 16384 zcmeHOO>87b6>fG}U?5APfCND(s+}FuJ(lfp*eE}X?Pb<>SV!ye#vW&hM%GMEcg;*E z{WIzAvE$tk5CM?`hg@S_g=lKSKZs5xo>$z+%bKdfzJ(w@xycF`j0++%=q?6!*Fab zqT;vv*s;@lI~l5flsc@qa;tKuQ%Pme_w7_Bm9!@Ve8tj{VXCgC`@R0P^q9-!4CD-4 z69a>^Gj->20(_AQLAZH+FAZH+FAZH+FAZH+FAZOsc!9dzS zVtg4xIiiR2p#FZ}*!NL=J)yt-v3h-(f8`A14CD;t4CD;t4CD;t4CD;t4CD;t4CD;t z3>-oRY}YV80=-_OgdflUv;6<9>kZ@gz{|kXz&C)4z$xGc;PsCh#!rCffbRm20bcxDI$72Q04v0yql1fCHK5fv*D%;6C6k;5J|exEXi@HY2-T$qs9je-#Ra zP83Vq=?SaUj_lY?gq?_6N=?2fi^~Q?N)HWWyd&m>xxRFMX`wFGH&(1x8ueRcarfHl zim=SjkzA!B>QQR@q8}w`LM4PR(Vs#|y|dBes>UWdE74DG$6^J(ZkT-IW@rb}WGTLG zn5saDXQD~y_4_hqx#Fx0=8IxlG_9f)M#71L0Pl#Di)EA*XDd{zT1!8XtkQnn^n<4E z)f(#K?wFgC;?zu8wf8#UigYJksZN}j6UAa#4O3P4UihF=--*JG*KL?_6p^CSLebnX zT`yMGnFJR=P$pcS^kXkfJ60*H?|8m!_U$fsnH*}6_Fz314bCV&8|JX&bXMZIO&D2O z)Z8o+A?8VwR8h+Atsk*S)R*B|a*EyY;sRZ;%yth7X+_AK>?Bk39ho{k>%!vd>C<;F z)fdh+tLy8l3%KiLB8V__CvIV>%d&`KA>%lTEw|lNKg#MxL-~FzyUkeQT_8tVW@V$c zyjm00^XEmWVonNkb!~BJO`N@7v{EmSEwQ+?z97youPoOEjn)F4w!}Ptt zOO-zoqgo9fns!;`_S17Bvqe$+|Acb;R*MKsS}k$$+|t^TXtA%hXrg>3F~zQaMRc$D zZBTC#16a;X3yLc<7%e8LC{uSP2QR`W;Unhxlj$TKyyzyLKYW> z9kN(YdflFy(4{nNCf+64xOG!E&Th?;+n$^Dlm?mL%(&oHIwt)HYQ;?+t+^|%d%$o$ z-<5D4;sUqcoqJ;qbtEWoC>R^5n1lJXys5o@M3722QV6(Y=2t@!|F3ggTk^k+RXiGm zes+G5%`dSXg=Q++QrKIz=i6<>3NI9Rflp+w^9dU5q(~ysm7$FBf}bE=5)nq*+^HeP zK4W}Sd#2l!zD%VHx_2iO_fROYbzs;Bk|=Dt(urJ{Rk<)%4d)_Hz69!zSC^H&4nr`c zF{$aO0wmVD1)FIec)rWzuJNmp7bq^#bpV3f(y<4acespU4gl*TTMHuiB?ZT9=}s4Q z#ATlKq7QG*+?a0vjs{~j^WK?gT&*pN7Mr{!me)mXwJvHKSUV1Hl+HF}bC?!M$_sH~ zsnElgNKzX{o)cAGFZ5bDy3IPEPO&^zNj-M`sEys5jz(B=*hwh*_6Kd>b3|tlI;j_h zq89~{wj$PWJynqoVc;ygVkoy0K@w<;f68fz{pNP;r6ZA5?|P_1wQytsq+#~bz-M1j zE8jl%RAXjuwUuHNd~M%1#Uqc1(nuN2_Oy_-&zN5$$oS9pUowfGwGf zI~10-y|jn86bEp?x)<)K)dWEo>jFFu*@VFimfBPE2dYH+m)bB3+;3DXHEU2=sT8I- zJJDtrmJM}i3oUxcd)iWlBCuUa79Yxz5c$smB07>bv&vdZWj{(0^;=hD$&%JlpYC#HEYm?8iX|VJO_(C- z+Vh`BmB!PYWKQZFniMCfu{KOfAh%?b4wcY@I&B<1=3a>WK@cLln5b|wePkl|`+s+^ zT^7}Kq0X9{NE2F=YVs{quBQ@9eK51nX|+1_zv{@uyN|gjOynXnPK&z3Rj^8e;*#Zdi{5k zMX$2w!sexcluded)) continue; - $query .= sprintf("SELECT *, '%s' AS `board` FROM ``posts_%s`` WHERE `files` IS NOT NULL UNION ALL ", $_board['uri'], $_board['uri']); + $query .= sprintf("SELECT * FROM ``posts`` WHERE `board` = '%s' AND `files` IS NOT NULL UNION ALL ", $_board['uri']); } $query = preg_replace('/UNION ALL $/', 'ORDER BY `time` DESC LIMIT ' . (int)$settings['limit_images'], $query); @@ -62,7 +62,7 @@ public function homepage($settings) { // board settings won't be available in the template file, so generate links now $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] - . sprintf($config['file_page'], ($post['thread'] ? $post['thread'] : $post['id'])) . '#' . $post['id']; + . sprintf($config['file_page'], ($post['thread'] ? $post['thread'] : $post['id_for_board'])) . '#' . $post['id_for_board']; if ($files) { if ($files[0]->thumb == 'spoiler') { @@ -84,7 +84,7 @@ public function homepage($settings) { foreach ($boards as &$_board) { if (in_array($_board['uri'], $this->excluded)) continue; - $query .= sprintf("SELECT *, '%s' AS `board` FROM ``posts_%s`` UNION ALL ", $_board['uri'], $_board['uri']); + $query .= sprintf("SELECT * FROM ``posts`` WHERE `board` = '%s' UNION ALL ", $_board['uri']); } $query = preg_replace('/UNION ALL $/', 'ORDER BY `time` DESC LIMIT ' . (int)$settings['limit_posts'], $query); $query = query($query) or error(db_error()); @@ -92,7 +92,7 @@ public function homepage($settings) { while ($post = $query->fetch(PDO::FETCH_ASSOC)) { openBoard($post['board']); - $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], ($post['thread'] ? $post['thread'] : $post['id'])) . '#' . $post['id']; + $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], ($post['thread'] ? $post['thread'] : $post['id_for_board'])) . '#' . $post['id_for_board']; if ($post['body'] != "") $post['snippet'] = pm_snippet($post['body'], 30); else @@ -107,7 +107,7 @@ public function homepage($settings) { foreach ($boards as &$_board) { if (in_array($_board['uri'], $this->excluded)) continue; - $query .= sprintf("SELECT MAX(`id`) AS `top` FROM ``posts_%s`` UNION ALL ", $_board['uri']); + $query .= sprintf("SELECT MAX(`id_for_board`) AS `top` FROM ``posts`` WHERE `board` = '%s' UNION ALL ", $_board['uri']); } $query = preg_replace('/UNION ALL $/', ') AS `posts_all`', $query); $query = query($query) or error(db_error()); @@ -118,7 +118,7 @@ public function homepage($settings) { foreach ($boards as &$_board) { if (in_array($_board['uri'], $this->excluded)) continue; - $query .= sprintf("SELECT `ip` FROM ``posts_%s`` UNION ALL ", $_board['uri']); + $query .= sprintf("SELECT `ip` FROM ``posts`` WHERE `board` = '%s' UNION ALL ", $_board['uri']); } $query = preg_replace('/UNION ALL $/', ') AS `posts_all`', $query); $query = query($query) or error(db_error()); @@ -129,7 +129,7 @@ public function homepage($settings) { foreach ($boards as &$_board) { if (in_array($_board['uri'], $this->excluded)) continue; - $query .= sprintf("SELECT `filesize` FROM ``posts_%s`` UNION ALL ", $_board['uri']); + $query .= sprintf("SELECT `filesize` FROM ``posts`` WHERE `board` = '%s' UNION ALL ", $_board['uri']); } $query = preg_replace('/UNION ALL $/', ') AS `posts_all`', $query); $query = query($query) or error(db_error()); From a054116e1699d6f06c56e5f9f07b40cfcb8bc43b Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Mon, 29 Sep 2014 09:21:44 -0300 Subject: [PATCH 23/33] Sitemap now uses the posts table. --- templates/themes/sitemap/theme.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/themes/sitemap/theme.php b/templates/themes/sitemap/theme.php index 3e048dd5a..0fc69ea4a 100644 --- a/templates/themes/sitemap/theme.php +++ b/templates/themes/sitemap/theme.php @@ -26,7 +26,7 @@ function sitemap_build($action, $settings, $board) { $threads = array(); foreach ($boards as $board) { - $query = query(sprintf("SELECT `id` AS `thread_id`, (SELECT `time` FROM ``posts_%s`` WHERE `thread` = `thread_id` OR `id` = `thread_id` ORDER BY `time` DESC LIMIT 1) AS `lastmod` FROM ``posts_%s`` WHERE `thread` IS NULL", $board, $board)) or error(db_error()); + $query = query(sprintf("SELECT `id_for_board` AS `thread_id`, (SELECT `time` FROM ``posts`` WHERE `board` = '%s' AND`thread` = `thread_id` OR `id_for_board` = `thread_id` ORDER BY `time` DESC LIMIT 1) AS `lastmod` FROM ``posts`` WHERE `board` = '%s' AND`thread` IS NULL", $board, $board)) or error(db_error()); $threads[$board] = $query->fetchAll(PDO::FETCH_ASSOC); } From e2adc88c6f3671158f4f0ae2b0581c04ef94690d Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Mon, 29 Sep 2014 10:54:40 -0300 Subject: [PATCH 24/33] post.php and functions.php now use posts table --- inc/functions.php | 74 ++++++++++++++++++++++++++--------------------- post.php | 19 ++++++------ 2 files changed, 51 insertions(+), 42 deletions(-) mode change 100755 => 100644 inc/functions.php diff --git a/inc/functions.php b/inc/functions.php old mode 100755 new mode 100644 index 140314f71..927bb2414 --- a/inc/functions.php +++ b/inc/functions.php @@ -722,8 +722,8 @@ 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` = " . - (int)$ban['post']['id'], $board['uri'])); + $query = query(sprintf("SELECT `files` FROM ``posts`` WHERE `board` = '%s' AND `id` = " . + (int)$ban['post']['id_for_board'], $board['uri'])); if ($_post = $query->fetch(PDO::FETCH_ASSOC)) { $ban['post'] = array_merge($ban['post'], $_post); } @@ -814,14 +814,15 @@ function checkBan($board = false) { cache::set('purged_bans_last', time()); } -function threadLocked($id) { +function threadLocked($id_for_board) { global $board; - if (event('check-locked', $id)) + if (event('check-locked', $id_for_board)) return true; - $query = prepare(sprintf("SELECT `locked` FROM ``posts_%s`` WHERE `id` = :id AND `thread` IS NULL LIMIT 1", $board['uri'])); - $query->bindValue(':id', $id, PDO::PARAM_INT); + $query = prepare("SELECT `locked` FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board AND `thread` IS NULL LIMIT 1"); + $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $query->bindValue(':board', $board['uri']); $query->execute() or error(db_error()); if (($locked = $query->fetchColumn()) === false) { @@ -832,14 +833,15 @@ function threadLocked($id) { return (bool)$locked; } -function threadSageLocked($id) { +function threadSageLocked($id_for_board) { global $board; - if (event('check-sage-locked', $id)) + if (event('check-sage-locked', $id_for_board)) return true; - $query = prepare(sprintf("SELECT `sage` FROM ``posts_%s`` WHERE `id` = :id AND `thread` IS NULL LIMIT 1", $board['uri'])); - $query->bindValue(':id', $id, PDO::PARAM_INT); + $query = prepare("SELECT `sage` FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board AND `thread` IS NULL LIMIT 1"); + $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $query->bindValue(':board', $board['uri'], PDO::PARAM_INT); $query->execute() or error(db_error()); if (($sagelocked = $query->fetchColumn()) === false) { @@ -850,11 +852,12 @@ function threadSageLocked($id) { return (bool)$sagelocked; } -function threadExists($id) { +function threadExists($id_for_board) { global $board; - $query = prepare(sprintf("SELECT 1 FROM ``posts_%s`` WHERE `id` = :id AND `thread` IS NULL LIMIT 1", $board['uri'])); - $query->bindValue(':id', $id, PDO::PARAM_INT); + $query = prepare("SELECT 1 FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board AND `thread` IS NULL LIMIT 1"); + $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $query->bindValue(':board', $board['uri']); $query->execute() or error(db_error()); if ($query->rowCount()) { @@ -1149,12 +1152,13 @@ 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_for_board` 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)); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { - deletePost($post['id'], false, false); + deletePost($post['id_for_board'], false, false); } } @@ -1770,12 +1774,12 @@ 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_for_board` FROM ``posts`` WHERE `board` = "%s" ' . implode(' OR ', $search_cites), $board['uri'])) or error(db_error()); $cited_posts = array(); while ($cited = $query->fetch(PDO::FETCH_ASSOC)) { - $cited_posts[$cited['id']] = $cited['thread'] ? $cited['thread'] : false; + $cited_posts[$cited['id_for_board']] = $cited['thread'] ? $cited['thread'] : false; } foreach ($cites as $matches) { @@ -1843,7 +1847,7 @@ function markup(&$body, $track_cites = false) { foreach ($search_cites as $cite) { if (!$cite || isset($cited_posts[$_board][$cite])) continue; - $clauses[] = '`id` = ' . $cite; + $clauses[] = '`id_for_board` = ' . $cite; } $clauses = array_unique($clauses); @@ -1855,12 +1859,12 @@ 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_for_board` FROM ``posts`` WHERE `board` = "%s"' . implode(' OR ', $clauses), $board['uri'])) or error(db_error()); while ($cite = $query->fetch(PDO::FETCH_ASSOC)) { - $cited_posts[$_board][$cite['id']] = $config['root'] . $board['dir'] . $config['dir']['res'] . - ($cite['thread'] ? $cite['thread'] : $cite['id']) . '.html#' . $cite['id']; + $cited_posts[$_board][$cite['id_for_board']] = $config['root'] . $board['dir'] . $config['dir']['res'] . + ($cite['thread'] ? $cite['thread'] : $cite['id_for_board']) . '.html#' . $cite['id_for_board']; } } @@ -2065,16 +2069,17 @@ function buildThread($id_for_board, $return = false, $mod = false) { } } -function buildThread50($id, $return = false, $mod = false, $thread = null, $antibot = false) { +function buildThread50($id_for_board, $return = false, $mod = false, $thread = null, $antibot = false) { global $board, $config, $build_pages; - $id = round($id); + $id_for_board = round($id_for_board); if ($antibot) $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->bindValue(':id', $id, PDO::PARAM_INT); + $query = prepare("SELECT * FROM ``posts`` WHERE `board` = :board AND (`thread` IS NULL AND `id_for_board` = :id_for_board) OR `thread` = :id_for_board ORDER BY `thread`,`id_for_board` DESC LIMIT :limit"); + $query->bindValue(':id_for_board', $id_for_board, 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)); @@ -2096,9 +2101,10 @@ 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->bindValue(':thread', $id, PDO::PARAM_INT); + $count = prepare("SELECT COUNT(`id_for_board`) 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_for_board, PDO::PARAM_INT); $count->execute() or error(db_error($count)); $c = $count->fetch(); @@ -2129,11 +2135,11 @@ function buildThread50($id, $return = false, $mod = false, $thread = null, $anti 'thread' => $thread, 'body' => $thread->build(false, true), 'config' => $config, - 'id' => $id, + 'id' => $id_for_board, 'mod' => $mod, 'hasnoko50' => $hasnoko50, 'isnoko50' => true, - 'antibot' => $mod ? false : ($antibot ? $antibot : create_antibot($board['uri'], $id)), + 'antibot' => $mod ? false : ($antibot ? $antibot : create_antibot($board['uri'], $id_for_board)), 'boardlist' => createBoardlist($mod), 'return' => ($mod ? '?' . $board['url'] . $config['file_index'] : $config['root'] . $board['dir'] . $config['file_index']) )); @@ -2141,7 +2147,7 @@ function buildThread50($id, $return = false, $mod = false, $thread = null, $anti if ($return) { return $body; } else { - file_write($board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $id), $body); + file_write($board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $id_for_board), $body); } } @@ -2238,7 +2244,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_for_board`,`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)); @@ -2251,7 +2258,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_for_board`,`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)); diff --git a/post.php b/post.php index 506f0789a..37ca9cc9b 100755 --- a/post.php +++ b/post.php @@ -131,23 +131,24 @@ function strip_array($var) { $reason = escape_markup_modifiers($_POST['reason']); markup($reason); - foreach ($report as &$id) { - $query = prepare(sprintf("SELECT `thread` FROM ``posts_%s`` WHERE `id` = :id", $board['uri'])); - $query->bindValue(':id', $id, PDO::PARAM_INT); + foreach ($report as &$id_for_board) { + $query = prepare("SELECT `thread` FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board"); + $query->bindValue(':board', $board['uri']); + $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); $query->execute() or error(db_error($query)); $thread = $query->fetchColumn(); if ($config['syslog']) _syslog(LOG_INFO, 'Reported post: ' . - '/' . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $thread ? $thread : $id) . ($thread ? '#' . $id : '') . + '/' . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $thread ? $thread : $id_for_board) . ($thread ? '#' . $id_for_board : '') . ' for "' . $reason . '"' ); $query = prepare("INSERT INTO ``reports`` VALUES (NULL, :time, :ip, :board, :post, :reason, :global)"); $query->bindValue(':time', time(), PDO::PARAM_INT); $query->bindValue(':ip', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR); $query->bindValue(':board', $board['uri'], PDO::PARAM_INT); - $query->bindValue(':post', $id, PDO::PARAM_INT); + $query->bindValue(':post', $id_for_board, PDO::PARAM_INT); $query->bindValue(':reason', $reason, PDO::PARAM_STR); $query->bindValue(':global', isset($_POST['global']), PDO::PARAM_BOOL); $query->execute() or error(db_error($query)); @@ -738,9 +739,9 @@ function ipv4to6($ip) { ($post['mod'] ? $config['root'] . $config['file_mod'] . '?/' : $config['root']) . ($board['dir'] . $config['dir']['res'] . ($p['thread'] ? - $p['thread'] . '.html#' . $p['id'] + $p['thread'] . '.html#' . $p['id_for_board'] : - $p['id'] . '.html' + $p['id_for_board'] . '.html' )) )); } @@ -751,9 +752,9 @@ function ipv4to6($ip) { ($post['mod'] ? $config['root'] . $config['file_mod'] . '?/' : $config['root']) . ($board['dir'] . $config['dir']['res'] . ($p['thread'] ? - $p['thread'] . '.html#' . $p['id'] + $p['thread'] . '.html#' . $p['id_for_board'] : - $p['id'] . '.html' + $p['id_for_board'] . '.html' )) )); } From 38cd9a7e1217b19a46e684a4337c2f8436b2ae66 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Tue, 30 Sep 2014 16:44:17 -0300 Subject: [PATCH 25/33] Last files moved to posts table. May Allah (peace be upon him) have mercy on my soul. --- expire.php | 12 ++- inc/instance-config.php | 4 +- inc/mod/pages.php | 132 +++++++++++++++++--------------- search.php | 3 +- templates/themes/ukko/theme.php | 14 ++-- tools/delete-stray-images.php | 2 +- tools/recount-bumps.php | 23 +++--- 7 files changed, 105 insertions(+), 85 deletions(-) mode change 100644 => 100755 expire.php mode change 100644 => 100755 inc/mod/pages.php mode change 100644 => 100755 search.php diff --git a/expire.php b/expire.php old mode 100644 new mode 100755 index 8e8228529..10f325e5f --- a/expire.php +++ b/expire.php @@ -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_for_board) AS count FROM posts WHERE `board` = :board", $board); + $query->bindValue(':board', $board); $query->execute(); $count = $query->fetch(); @@ -78,8 +80,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'); diff --git a/inc/instance-config.php b/inc/instance-config.php index 2f014fa97..3191ba403 100644 --- a/inc/instance-config.php +++ b/inc/instance-config.php @@ -334,9 +334,9 @@ 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_for_board` FROM ``posts`` WHERE `board` = '%s' AND `thread` IS NULL", $b)) or error(db_error()); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { - buildThread($post['id']); + buildThread($post['id_for_board']); } modLog('Edited board settings', $b); } diff --git a/inc/mod/pages.php b/inc/mod/pages.php old mode 100644 new mode 100755 index f7ccf1a6c..6b04f1a42 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -283,7 +283,7 @@ function mod_search($type, $search_query_escaped, $page_no = 1) { if (!empty($query)) $query .= ' UNION ALL '; - $query .= sprintf("SELECT *, '%s' AS `board` FROM ``posts_%s`` WHERE %s", $board['uri'], $board['uri'], $sql_like); + $query .= sprintf("SELECT * FROM ``posts`` WHERE `board` = '%s' AND %s", $board['uri'], $sql_like); } // You weren't allowed to search any boards @@ -378,8 +378,10 @@ function mod_edit_board($boardName) { modLog('Deleted board: ' . sprintf($config['board_abbreviation'], $board['uri']), false); - // 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'); @@ -504,13 +506,6 @@ function mod_new_board() { if (!openBoard($_POST['uri'])) error(_("Couldn't open board after creation.")); - $query = Element('posts.sql', array('board' => $board['uri'])); - - if (mysql_version() < 50503) - $query = preg_replace('/(CHARSET=|CHARACTER SET )utf8mb4/', '$1utf8', $query); - - query($query) or error(db_error()); - if ($config['cache']['enabled']) cache::delete('all_boards'); @@ -816,7 +811,8 @@ function mod_page_ip($ip) { openBoard($board['uri']); if (!hasPermission($config['mod']['show_ip'], $board['uri'])) continue; - $query = prepare(sprintf('SELECT * FROM ``posts_%s`` WHERE `ip` = :ip ORDER BY `sticky` DESC, `id` DESC LIMIT :limit', $board['uri'])); + $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `ip` = :ip ORDER BY `sticky` DESC, `id_for_board` DESC LIMIT :limit'); + $query->bindValue(':board', $board['uri']); $query->bindValue(':ip', $ip); $query->bindValue(':limit', $config['mod']['ip_recentposts'], PDO::PARAM_INT); $query->execute() or error(db_error($query)); @@ -976,10 +972,10 @@ function mod_ban_appeals() { $ban['post'] = json_decode($ban['post'], true); $ban['mask'] = Bans::range_to_string(array($ban['ipstart'], $ban['ipend'])); - if ($ban['post'] && isset($ban['post']['board'], $ban['post']['id'])) { + if ($ban['post'] && isset($ban['post']['board'], $ban['post']['id_for_board'])) { if (openBoard($ban['post']['board'])) { - $query = query(sprintf("SELECT `num_files`, `files` FROM ``posts_%s`` WHERE `id` = " . - (int)$ban['post']['id'], $board['uri'])); + $query = query(sprintf("SELECT `num_files`, `files` FROM ``posts`` WHERE `board` = '%s' AND `id_for_board` = " . + (int)$ban['post']['id_for_board'], $board['uri'])); if ($_post = $query->fetch(PDO::FETCH_ASSOC)) { $_post['files'] = $_post['files'] ? json_decode($_post['files']) : array(); $ban['post'] = array_merge($ban['post'], $_post); @@ -1019,8 +1015,9 @@ function mod_lock($board, $unlock, $post) { if (!hasPermission($config['mod']['lock'], $board)) error($config['error']['noaccess']); - $query = prepare(sprintf('UPDATE ``posts_%s`` SET `locked` = :locked WHERE `id` = :id AND `thread` IS NULL', $board)); - $query->bindValue(':id', $post); + $query = prepare('UPDATE ``posts`` SET `locked` = :locked WHERE `id_for_board` = :id_for_board AND `board` = :board AND `thread` IS NULL'); + $query->bindValue(':board', $board); + $query->bindValue(':id_for_board', $post); $query->bindValue(':locked', $unlock ? 0 : 1); $query->execute() or error(db_error($query)); if ($query->rowCount()) { @@ -1053,8 +1050,9 @@ function mod_sticky($board, $unsticky, $post) { if (!hasPermission($config['mod']['sticky'], $board)) error($config['error']['noaccess']); - $query = prepare(sprintf('UPDATE ``posts_%s`` SET `sticky` = :sticky WHERE `id` = :id AND `thread` IS NULL', $board)); - $query->bindValue(':id', $post); + $query = prepare('UPDATE ``posts`` SET `sticky` = :sticky WHERE `board` = :board AND `id_for_board` = :id_for_board AND `thread` IS NULL'); + $query->bindValue(':board', $board); + $query->bindValue(':id_for_board', $post); $query->bindValue(':sticky', $unsticky ? 0 : 1); $query->execute() or error(db_error($query)); if ($query->rowCount()) { @@ -1075,8 +1073,9 @@ function mod_bumplock($board, $unbumplock, $post) { if (!hasPermission($config['mod']['bumplock'], $board)) error($config['error']['noaccess']); - $query = prepare(sprintf('UPDATE ``posts_%s`` SET `sage` = :bumplock WHERE `id` = :id AND `thread` IS NULL', $board)); - $query->bindValue(':id', $post); + $query = prepare('UPDATE ``posts`` SET `sage` = :bumplock WHERE `board` = :board AND `id_for_board` = :id_for_board AND `thread` IS NULL'); + $query->bindValue(':board', $board); + $query->bindValue(':id_for_board', $post); $query->bindValue(':bumplock', $unbumplock ? 0 : 1); $query->execute() or error(db_error($query)); if ($query->rowCount()) { @@ -1097,8 +1096,9 @@ function mod_move_reply($originBoard, $postID) { if (!hasPermission($config['mod']['move'], $originBoard)) error($config['error']['noaccess']); - $query = prepare(sprintf('SELECT * FROM ``posts_%s`` WHERE `id` = :id', $originBoard)); - $query->bindValue(':id', $postID); + $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query->bindValue(':board', $originBoard); + $query->bindValue(':id_for_board', $postID); $query->execute() or error(db_error($query)); if (!$post = $query->fetch(PDO::FETCH_ASSOC)) error($config['error']['404']); @@ -1107,8 +1107,9 @@ function mod_move_reply($originBoard, $postID) { $targetBoard = $_POST['board']; if ($_POST['target_thread']) { - $query = prepare(sprintf('SELECT * FROM ``posts_%s`` WHERE `id` = :id', $targetBoard)); - $query->bindValue(':id', $_POST['target_thread']); + $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query->bindValue(':board', $targetBoard); + $query->bindValue(':id_for_board', $_POST['target_thread']); $query->execute() or error(db_error($query)); // If it fails, thread probably does not exist $post['op'] = false; $post['thread'] = $_POST['target_thread']; @@ -1168,8 +1169,9 @@ function mod_move_reply($originBoard, $postID) { openBoard($targetBoard); // Find new thread on our target board - $query = prepare(sprintf('SELECT thread FROM ``posts_%s`` WHERE `id` = :id', $targetBoard)); - $query->bindValue(':id', $newID); + $query = prepare('SELECT thread FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query->bindValue(':board', $targetBoard); + $query->bindValue(':id_for_board', $newID); $query->execute() or error(db_error($query)); $post = $query->fetch(PDO::FETCH_ASSOC); @@ -1197,8 +1199,9 @@ function mod_move($originBoard, $postID) { if (!hasPermission($config['mod']['move'], $originBoard)) error($config['error']['noaccess']); - $query = prepare(sprintf('SELECT * FROM ``posts_%s`` WHERE `id` = :id AND `thread` IS NULL', $originBoard)); - $query->bindValue(':id', $postID); + $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board AND `thread` IS NULL'); + $query->bindValue(':board', $originBoard); + $query->bindValue(':id_for_board', $postID); $query->execute() or error(db_error($query)); if (!$post = $query->fetch(PDO::FETCH_ASSOC)) error($config['error']['404']); @@ -1251,8 +1254,9 @@ function mod_move($originBoard, $postID) { // go back to the original board to fetch replies openBoard($originBoard); - $query = prepare(sprintf('SELECT * FROM ``posts_%s`` WHERE `thread` = :id ORDER BY `id`', $originBoard)); - $query->bindValue(':id', $postID, PDO::PARAM_INT); + $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `thread` = :id_for_board ORDER BY `id_for_board`'); + $query->bindValue(':board', $originBoard); + $query->bindValue(':id_for_board', $postID, PDO::PARAM_INT); $query->execute() or error(db_error($query)); $replies = array(); @@ -1282,7 +1286,7 @@ function mod_move($originBoard, $postID) { foreach ($replies as &$post) { $query = prepare('SELECT `target` FROM ``cites`` WHERE `target_board` = :board AND `board` = :board AND `post` = :post'); $query->bindValue(':board', $originBoard); - $query->bindValue(':post', $post['id'], PDO::PARAM_INT); + $query->bindValue(':post', $post['id_for_board'], PDO::PARAM_INT); $query->execute() or error(db_error($qurey)); // correct >>X links @@ -1310,7 +1314,7 @@ function mod_move($originBoard, $postID) { } } // insert reply - $newIDs[$post['id']] = $newPostID = post($post); + $newIDs[$post['id_for_board']] = $newPostID = post($post); if (!empty($post['tracked_cites'])) { @@ -1340,8 +1344,9 @@ function mod_move($originBoard, $postID) { if ($shadow) { // lock old thread - $query = prepare(sprintf('UPDATE ``posts_%s`` SET `locked` = 1 WHERE `id` = :id', $originBoard)); - $query->bindValue(':id', $postID, PDO::PARAM_INT); + $query = prepare('UPDATE ``posts`` SET `locked` = 1 WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query->bindValue(':board', $originBoard); + $query->bindValue(':id_for_board', $postID, PDO::PARAM_INT); $query->execute() or error(db_error($query)); // leave a reply, linking to the new thread @@ -1399,9 +1404,10 @@ function mod_ban_post($board, $delete, $post, $token = false) { $security_token = make_secure_link_token($board . '/ban/' . $post); - $query = prepare(sprintf('SELECT ' . ($config['ban_show_post'] ? '*' : '`ip`, `thread`') . - ' FROM ``posts_%s`` WHERE `id` = :id', $board)); - $query->bindValue(':id', $post); + $query = prepare('SELECT ' . ($config['ban_show_post'] ? '*' : '`ip`, `thread`') . + ' FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query->bindValue(':board', $board); + $query->bindValue(':id_for_board', $post); $query->execute() or error(db_error($query)); if (!$_post = $query->fetch(PDO::FETCH_ASSOC)) error($config['error']['404']); @@ -1424,8 +1430,9 @@ function mod_ban_post($board, $delete, $post, $token = false) { $_POST['message'] = preg_replace('/[\r\n]/', '', $_POST['message']); $_POST['message'] = str_replace('%length%', $length_english, $_POST['message']); $_POST['message'] = str_replace('%LENGTH%', strtoupper($length_english), $_POST['message']); - $query = prepare(sprintf('UPDATE ``posts_%s`` SET `body_nomarkup` = CONCAT(`body_nomarkup`, :body_nomarkup) WHERE `id` = :id', $board)); - $query->bindValue(':id', $post); + $query = prepare('UPDATE ``posts`` SET `body_nomarkup` = CONCAT(`body_nomarkup`, :body_nomarkup) WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query->bindValue(':board', $board); + $query->bindValue(':id_for_board', $post); $query->bindValue(':body_nomarkup', sprintf("\n%s", utf8tohtml($_POST['message']))); $query->execute() or error(db_error($query)); rebuildPost($post); @@ -1473,8 +1480,9 @@ function mod_edit_post($board, $edit_raw_html, $postID) { $security_token = make_secure_link_token($board . '/edit' . ($edit_raw_html ? '_raw' : '') . '/' . $postID); - $query = prepare(sprintf('SELECT * FROM ``posts_%s`` WHERE `id` = :id', $board)); - $query->bindValue(':id', $postID); + $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query->bindValue(':board', $board); + $query->bindValue(':id_for_board', $postID); $query->execute() or error(db_error($query)); if (!$post = $query->fetch(PDO::FETCH_ASSOC)) @@ -1482,10 +1490,11 @@ function mod_edit_post($board, $edit_raw_html, $postID) { if (isset($_POST['name'], $_POST['email'], $_POST['subject'], $_POST['body'])) { if ($edit_raw_html) - $query = prepare(sprintf('UPDATE ``posts_%s`` SET `name` = :name, `email` = :email, `subject` = :subject, `body` = :body, `body_nomarkup` = :body_nomarkup WHERE `id` = :id', $board)); + $query = prepare('UPDATE ``posts`` SET `name` = :name, `email` = :email, `subject` = :subject, `body` = :body, `body_nomarkup` = :body_nomarkup WHERE `board` = :board AND `id_for_board` = :id_for_board'); else - $query = prepare(sprintf('UPDATE ``posts_%s`` SET `name` = :name, `email` = :email, `subject` = :subject, `body_nomarkup` = :body WHERE `id` = :id', $board)); - $query->bindValue(':id', $postID); + $query = prepare('UPDATE ``posts`` SET `name` = :name, `email` = :email, `subject` = :subject, `body_nomarkup` = :body WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query->bindValue(':board', $board); + $query->bindValue(':id_for_board', $postID); $query->bindValue('name', $_POST['name']); $query->bindValue(':email', $_POST['email']); $query->bindValue(':subject', $_POST['subject']); @@ -1576,8 +1585,9 @@ function mod_spoiler_image($board, $post, $file) { error($config['error']['noaccess']); // Delete file thumbnail - $query = prepare(sprintf("SELECT `files`, `thread` FROM ``posts_%s`` WHERE id = :id", $board)); - $query->bindValue(':id', $post, PDO::PARAM_INT); + $query = prepare("SELECT `files`, `thread` FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board"); + $query->bindValue(':board', $board); + $query->bindValue(':id_for_board', $post, PDO::PARAM_INT); $query->execute() or error(db_error($query)); $result = $query->fetch(PDO::FETCH_ASSOC); $files = json_decode($result['files']); @@ -1588,9 +1598,10 @@ function mod_spoiler_image($board, $post, $file) { $files[$file]->thumbwidth = 128; // Make thumbnail spoiler - $query = prepare(sprintf("UPDATE ``posts_%s`` SET `files` = :files WHERE `id` = :id", $board)); + $query = prepare("UPDATE ``posts`` SET `files` = :files WHERE `board` = :board AND `id_for_board` = :id_for_board"); $query->bindValue(':files', json_encode($files)); - $query->bindValue(':id', $post, PDO::PARAM_INT); + $query->bindValue(':board', $board); + $query->bindValue(':id_for_board', $post, PDO::PARAM_INT); $query->execute() or error(db_error($query)); // Record the action @@ -1624,8 +1635,9 @@ function mod_deletebyip($boardName, $post, $global = false) { error($config['error']['noaccess']); // Find IP address - $query = prepare(sprintf('SELECT `ip` FROM ``posts_%s`` WHERE `id` = :id', $boardName)); - $query->bindValue(':id', $post); + $query = prepare('SELECT `ip` FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query->bindValue(':board', $boardName); + $query->bindValue(':id_for_board', $post); $query->execute() or error(db_error($query)); if (!$ip = $query->fetchColumn()) error($config['error']['invalidpost']); @@ -1634,7 +1646,7 @@ function mod_deletebyip($boardName, $post, $global = false) { $query = ''; foreach ($boards as $_board) { - $query .= sprintf("SELECT `thread`, `id`, '%s' AS `board` FROM ``posts_%s`` WHERE `ip` = :ip UNION ALL ", $_board['uri'], $_board['uri']); + $query .= sprintf("SELECT `thread`, `id_for_board` FROM ``posts`` WHERE `board` = '%s' AND `ip` = :ip UNION ALL ", $_board['uri']); } $query = preg_replace('/UNION ALL $/', '', $query); @@ -1652,14 +1664,14 @@ function mod_deletebyip($boardName, $post, $global = false) { while ($post = $query->fetch(PDO::FETCH_ASSOC)) { openBoard($post['board']); - deletePost($post['id'], false, false); + deletePost($post['id_for_board'], false, false); rebuildThemes('post-delete', $board['uri']); if ($post['thread']) $threads_to_rebuild[$post['board']][$post['thread']] = true; else - $threads_deleted[$post['board']][$post['id']] = true; + $threads_deleted[$post['board']][$post['id_for_board']] = true; } foreach ($threads_to_rebuild as $_board => $_threads) { @@ -2115,10 +2127,10 @@ function mod_rebuild() { } if (isset($_POST['rebuild_thread'])) { - $query = query(sprintf("SELECT `id` FROM ``posts_%s`` WHERE `thread` IS NULL", $board['uri'])) or error(db_error()); + $query = query(sprintf("SELECT `id_for_board` FROM ``posts`` WHERE `board` = '%s' AND `thread` IS NULL", $board['uri'])) or error(db_error()); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { - $log[] = '' . sprintf($config['board_abbreviation'], $board['uri']) . ': Rebuilding thread #' . $post['id']; - buildThread($post['id']); + $log[] = '' . sprintf($config['board_abbreviation'], $board['uri']) . ': Rebuilding thread #' . $post['id_for_board']; + buildThread($post['id_for_board']); } } } @@ -2167,9 +2179,9 @@ function mod_reports($global = false) { foreach ($report_queries as $board => $posts) { $report_posts[$board] = array(); - $query = query(sprintf('SELECT * FROM ``posts_%s`` WHERE `id` = ' . implode(' OR `id` = ', $posts), $board)) or error(db_error()); + $query = query(sprintf('SELECT * FROM ``posts`` WHERE `board` = "%s" AND `id_for_board` = ' . implode(' OR `id_for_board` = ', $posts), $board)) or error(db_error()); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { - $report_posts[$board][$post['id']] = $post; + $report_posts[$board][$post['id_for_board']] = $post; } } @@ -2289,7 +2301,7 @@ function mod_recent_posts($lim) { // Manually build an SQL query $query = 'SELECT * FROM ('; foreach ($mod_boards as $board) { - $query .= sprintf('SELECT *, %s AS `board` FROM ``posts_%s`` UNION ALL ', $pdo->quote($board['uri']), $board['uri']); + $query .= sprintf('SELECT * FROM ``posts`` WHERE `board` = "%s" UNION ALL ', $board['uri']); } // Remove the last "UNION ALL" seperator and complete the query $query = preg_replace('/UNION ALL $/', ') AS `all_posts` WHERE (`time` < :last_time OR NOT :last_time) ORDER BY `time` DESC LIMIT ' . $limit, $query); @@ -2651,7 +2663,7 @@ function mod_debug_recent_posts() { // Manually build an SQL query $query = 'SELECT * FROM ('; foreach ($boards as $board) { - $query .= sprintf('SELECT *, %s AS `board` FROM ``posts_%s`` UNION ALL ', $pdo->quote($board['uri']), $board['uri']); + $query .= sprintf('SELECT * FROM ``posts`` WHERE `board` = %s UNION ALL ', $pdo->quote($board['uri'])); } // Remove the last "UNION ALL" seperator and complete the query $query = preg_replace('/UNION ALL $/', ') AS `all_posts` ORDER BY `time` DESC LIMIT ' . $limit, $query); diff --git a/search.php b/search.php old mode 100644 new mode 100755 index 394aa3ffc..accda7201 --- a/search.php +++ b/search.php @@ -126,7 +126,8 @@ function search_filters($m) { $like = str_replace('%', '%%', $like); - $query = prepare(sprintf("SELECT * FROM ``posts_%s`` WHERE " . $like . " ORDER BY `time` DESC LIMIT :limit", $board['uri'])); + $query = prepare("SELECT * FROM ``posts`` WHERE `board` = :board AND " . $like . " ORDER BY `time` DESC LIMIT :limit"); + $query->bindValue(':board', $board['uri']); $query->bindValue(':limit', $search_limit, PDO::PARAM_INT); $query->execute() or error(db_error($query)); diff --git a/templates/themes/ukko/theme.php b/templates/themes/ukko/theme.php index 35c7c7b80..52850c682 100644 --- a/templates/themes/ukko/theme.php +++ b/templates/themes/ukko/theme.php @@ -27,7 +27,7 @@ public function build($mod = false) { foreach($boards as &$_board) { if(in_array($_board['uri'], explode(' ', $this->settings['exclude']))) continue; - $query .= sprintf("SELECT *, '%s' AS `board` FROM ``posts_%s`` WHERE `thread` IS NULL UNION ALL ", $_board['uri'], $_board['uri']); + $query .= sprintf("SELECT * FROM ``posts`` WHERE `board` = '%s' AND `thread` IS NULL UNION ALL ", $_board['uri']); } $query = preg_replace('/UNION ALL $/', 'ORDER BY `bump` DESC', $query); $query = query($query) or error(db_error()); @@ -48,8 +48,9 @@ public function build($mod = false) { $board['dir'] = $post['board'].'/'; $thread = new Thread($post, $mod ? '?/' : $config['root'], $mod); - $posts = prepare(sprintf("SELECT * FROM ``posts_%s`` WHERE `thread` = :id ORDER BY `id` DESC LIMIT :limit", $post['board'])); - $posts->bindValue(':id', $post['id']); + $posts = prepare("SELECT * FROM ``posts`` WHERE `board` = :board AND `thread` = :id_for_board ORDER BY `id_for_board` DESC LIMIT :limit"); + $posts->bindValue(':id_for_board', $post['id_for_board']); + $posts->bindValue(':board', $post['board']); $posts->bindValue(':limit', ($post['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']), PDO::PARAM_INT); $posts->execute() or error(db_error($posts)); @@ -65,8 +66,9 @@ public function build($mod = false) { } if ($posts->rowCount() == ($post['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview'])) { - $ct = prepare(sprintf("SELECT COUNT(`id`) as `num` FROM ``posts_%s`` WHERE `thread` = :thread UNION ALL SELECT COUNT(`id`) FROM ``posts_%s`` WHERE `files` IS NOT NULL AND `thread` = :thread", $post['board'], $post['board'])); - $ct->bindValue(':thread', $post['id'], PDO::PARAM_INT); + $ct = prepare("SELECT COUNT(`id_for_board`) as `num` FROM ``posts`` WHERE `board` = :board AND `thread` = :thread UNION ALL SELECT COUNT(`id_for_board`) FROM ``posts`` WHERE `board` = :board AND `files` IS NOT NULL AND `thread` = :thread"); + $ct->bindValue(':thread', $post['id_for_board'], PDO::PARAM_INT); + $ct->bindValue(':board', $post['board']); $ct->execute() or error(db_error($count)); $c = $ct->fetch(); @@ -85,7 +87,7 @@ public function build($mod = false) { if(floor($threads[$post['board']] / $config['threads_per_page']) > 0) { $page = floor($threads[$post['board']] / $config['threads_per_page']) + 1; } - $overflow[] = array('id' => $post['id'], 'board' => $post['board'], 'page' => $page . '.html'); + $overflow[] = array('id' => $post['id_for_board'], 'board' => $post['board'], 'page' => $page . '.html'); } $count += 1; diff --git a/tools/delete-stray-images.php b/tools/delete-stray-images.php index cf94dfe52..1c2dbb883 100755 --- a/tools/delete-stray-images.php +++ b/tools/delete-stray-images.php @@ -18,7 +18,7 @@ openBoard($board['uri']); - $query = query(sprintf("SELECT `file`, `thumb` FROM ``posts_%s`` WHERE `file` IS NOT NULL", $board['uri'])); + $query = query(sprintf("SELECT `file`, `thumb` FROM ``posts`` WHERE `board` = '%s' AND `file` IS NOT NULL", $board['uri'])); $valid_src = array(); $valid_thumb = array(); diff --git a/tools/recount-bumps.php b/tools/recount-bumps.php index 06e8ca9f9..bf6c8dc8c 100644 --- a/tools/recount-bumps.php +++ b/tools/recount-bumps.php @@ -9,26 +9,27 @@ } $board = $argv[1]; -$q = query(sprintf("SELECT `id`, `bump`, `time` FROM ``posts_%s`` - WHERE `thread` IS NULL", $board)); +$q = query(sprintf("SELECT `id_for_board`, `bump`, `time` FROM ``posts`` + WHERE `board` = '%s' AND `thread` IS NULL", $board)); while ($val = $q->fetch()) { - $lc = prepare(sprintf('SELECT MAX(`time`) AS `aq` FROM ``posts_%s`` - WHERE ((`thread` = :thread and - `email` != "sage" ) OR `id` = :thread', $board)); + $lc = prepare(sprintf('SELECT MAX(`time`) AS `aq` FROM ``posts`` + WHERE `board` = "%s" AND ((`thread` = :thread and + `email` != "sage" ) OR `id_for_board` = :thread', $board)); - $lc->bindValue(":thread", $val['id']); + $lc->bindValue(":thread", $val['id_for_board']); $lc->execute(); $f = $lc->fetch(); if ($val['bump'] != $f['aq']) { - $query = prepare(sprintf("UPDATE ``posts_%s`` SET `bump`=:bump - WHERE `id`=:id", $board)); + $query = prepare("UPDATE ``posts`` SET `bump`=:bump + WHERE `board` = :board AND `id_for_board`=:id_for_board"); $query->bindValue(":bump", $f['aq']); - $query->bindValue(":id", $val['id']); - echo("Thread $val[id] - to be $val[bump] -> $f[aq]\n"); + $query->bindValue(":board", $board); + $query->bindValue(":id_for_board", $val['id_for_board']); + echo("Thread $val[id_for_board] - to be $val[bump] -> $f[aq]\n"); } else { - echo("Thread $val[id] ok\n"); + echo("Thread $val[id_for_board] ok\n"); } } From ca9c37cdd731222d76bebe6c743a65d8b3dfedab Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Wed, 1 Oct 2014 07:33:09 -0300 Subject: [PATCH 26/33] Removes posts.sql. --- templates/posts.sql | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 templates/posts.sql diff --git a/templates/posts.sql b/templates/posts.sql deleted file mode 100644 index 9a47c6ad2..000000000 --- a/templates/posts.sql +++ /dev/null @@ -1,29 +0,0 @@ -CREATE TABLE IF NOT EXISTS ``posts_{{ board }}`` ( - `id` int(11) unsigned NOT NULL AUTO_INCREMENT, - `thread` int(11) DEFAULT NULL, - `subject` varchar(100) DEFAULT NULL, - `email` varchar(30) DEFAULT NULL, - `name` varchar(35) DEFAULT NULL, - `trip` varchar(15) DEFAULT NULL, - `capcode` varchar(50) DEFAULT NULL, - `body` text NOT NULL, - `body_nomarkup` text, - `time` int(11) NOT NULL, - `bump` int(11) DEFAULT NULL, - `files` text DEFAULT NULL, - `num_files` int(11) DEFAULT 0, - `filehash` text CHARACTER SET ascii, - `password` varchar(20) DEFAULT NULL, - `ip` varchar(39) CHARACTER SET ascii NOT NULL, - `sticky` int(1) NOT NULL, - `locked` int(1) NOT NULL, - `sage` int(1) NOT NULL, - `embed` text, - UNIQUE KEY `id` (`id`), - KEY `thread_id` (`thread`,`id`), - KEY `filehash` (`filehash`(40)), - KEY `time` (`time`), - KEY `ip` (`ip`), - KEY `list_threads` (`thread`, `sticky`, `bump`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1 ; - From 4e6c22392f8dbf1ccd13f75ba6f7fcf91a2a6e6f Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Wed, 8 Oct 2014 20:39:17 -0300 Subject: [PATCH 27/33] Renaming id_for_board to just id, duh --- boards.php | 2 +- expire.php | 2 +- inc/display.php | 4 +- inc/functions.php | 162 +++++++++++++------------- inc/mod/pages.php | 94 +++++++-------- install.sql | 8 +- post.php | 52 ++++----- templates/post/file_controls.html | 4 +- templates/post/post_controls.html | 28 ++--- templates/post_reply.html | 12 +- templates/post_thread.html | 18 +-- templates/themes/catalog/catalog.html | 2 +- templates/themes/catalog/theme.php | 6 +- templates/themes/recent/theme.php | 6 +- templates/themes/sitemap/theme.php | 2 +- templates/themes/ukko/theme.php | 10 +- tools/rebuild.php | 12 +- tools/recount-bumps.php | 14 +-- 18 files changed, 218 insertions(+), 220 deletions(-) mode change 100644 => 100755 install.sql diff --git a/boards.php b/boards.php index ccc74bcbe..0ccd86371 100755 --- a/boards.php +++ b/boards.php @@ -40,7 +40,7 @@ foreach ($boards as $i => $board) { $query = prepare(" SELECT - (SELECT coalesce((SELECT max(`id_for_board`) FROM ``posts`` WHERE `board` = :board),0)) max, + (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 diff --git a/expire.php b/expire.php index 10f325e5f..68fd8c2a4 100755 --- a/expire.php +++ b/expire.php @@ -25,7 +25,7 @@ $row = $query->fetch(); //count posts - $query = prepare("SELECT COUNT(id_for_board) AS count FROM posts WHERE `board` = :board", $board); + $query = prepare("SELECT COUNT(id) AS count FROM posts WHERE `board` = :board", $board); $query->bindValue(':board', $board); $query->execute(); $count = $query->fetch(); diff --git a/inc/display.php b/inc/display.php index f7e697e59..1a78e19b1 100644 --- a/inc/display.php +++ b/inc/display.php @@ -383,7 +383,7 @@ public function __construct($post, $root=null, $mod=false) { public function link($pre = '', $page = false) { global $config, $board; - return $this->root . $board['dir'] . $config['dir']['res'] . sprintf(($page ? $page : $config['file_page']), $this->thread) . '#' . $pre . $this->id_for_board; + return $this->root . $board['dir'] . $config['dir']['res'] . sprintf(($page ? $page : $config['file_page']), $this->thread) . '#' . $pre . $this->id; } public function build($index=false) { @@ -438,7 +438,7 @@ public function __construct($post, $root = null, $mod = false, $hr = true) { public function link($pre = '', $page = false) { global $config, $board; - return $this->root . $board['dir'] . $config['dir']['res'] . sprintf(($page ? $page : $config['file_page']), $this->id_for_board) . '#' . $pre . $this->id_for_board; + return $this->root . $board['dir'] . $config['dir']['res'] . sprintf(($page ? $page : $config['file_page']), $this->id) . '#' . $pre . $this->id; } public function add(Post $post) { $this->posts[] = $post; diff --git a/inc/functions.php b/inc/functions.php index 927bb2414..6e9af192a 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -723,7 +723,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`` WHERE `board` = '%s' AND `id` = " . - (int)$ban['post']['id_for_board'], $board['uri'])); + (int)$ban['post']['id'], $board['uri'])); if ($_post = $query->fetch(PDO::FETCH_ASSOC)) { $ban['post'] = array_merge($ban['post'], $_post); } @@ -814,14 +814,14 @@ function checkBan($board = false) { cache::set('purged_bans_last', time()); } -function threadLocked($id_for_board) { +function threadLocked($id) { global $board; - if (event('check-locked', $id_for_board)) + if (event('check-locked', $id)) return true; - $query = prepare("SELECT `locked` FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board AND `thread` IS NULL LIMIT 1"); - $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $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()); @@ -833,14 +833,14 @@ function threadLocked($id_for_board) { return (bool)$locked; } -function threadSageLocked($id_for_board) { +function threadSageLocked($id) { global $board; - if (event('check-sage-locked', $id_for_board)) + if (event('check-sage-locked', $id)) return true; - $query = prepare("SELECT `sage` FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board AND `thread` IS NULL LIMIT 1"); - $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $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()); @@ -852,11 +852,11 @@ function threadSageLocked($id_for_board) { return (bool)$sagelocked; } -function threadExists($id_for_board) { +function threadExists($id) { global $board; - $query = prepare("SELECT 1 FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board AND `thread` IS NULL LIMIT 1"); - $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $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()); @@ -886,7 +886,7 @@ function insertFloodPost(array $post) { function post(array $post) { global $pdo, $board; - $query = prepare("INSERT INTO ``posts`` (`id_for_board`, `board`, `thread`, `subject`, `email`, `name`, `trip`, `capcode`, `body`, `body_nomarkup`, `time`, `bump`, `files`, `num_files`, `filehash`, `password`, `ip`, `sticky`, `locked`, `sage`, `embed`) SELECT 1 + coalesce((SELECT max(`id_for_board`) FROM ``posts`` WHERE `board`=:board),0), :board, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, 0, :embed"); + $query = prepare("INSERT INTO ``posts`` (`id`, `board`, `thread`, `subject`, `email`, `name`, `trip`, `capcode`, `body`, `body_nomarkup`, `time`, `bump`, `files`, `num_files`, `filehash`, `password`, `ip`, `sticky`, `locked`, `sage`, `embed`) SELECT 1 + coalesce((SELECT max(`id`) FROM ``posts`` WHERE `board`=:board),0), :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']); @@ -964,7 +964,7 @@ function post(array $post) { } $lastInsertId = $pdo->lastInsertId(); - $query = prepare("SELECT `id_for_board` FROM ``posts`` WHERE `id` = :id"); + $query = prepare("SELECT `id` FROM ``posts`` WHERE `id` = :id"); $query->bindValue(':id', $lastInsertId); if(!$query->execute()) { @@ -978,28 +978,28 @@ function post(array $post) { return $lastIdForBoard; } -function bumpThread($id_for_board) { +function bumpThread($id) { global $config, $board, $build_pages; - if (event('bump', $id_for_board)) + if (event('bump', $id)) return true; if ($config['try_smarter']) - $build_pages[] = thread_find_page($id_for_board); + $build_pages[] = thread_find_page($id); - $query = prepare("UPDATE ``posts`` SET `bump` = :time WHERE `id_for_board` = :id_for_board AND `thread` IS NULL AND `board` = :board"); + $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_for_board', $id_for_board, 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_for_board, $remove_entirely_if_already=true, $file=null) { +function deleteFile($id, $remove_entirely_if_already=true, $file=null) { global $board, $config; - $query = prepare("SELECT `thread`, `files`, `num_files` FROM ``posts`` WHERE `id_for_board` = :id_for_board AND `board` = :board LIMIT 1"); - $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $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)) @@ -1010,7 +1010,7 @@ function deleteFile($id_for_board, $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("UPDATE ``posts`` SET `files` = :file WHERE `id_for_board` = :id_for_board AND `board` = :board"); + $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; @@ -1030,22 +1030,22 @@ function deleteFile($id_for_board, $remove_entirely_if_already=true, $file=null) $query->bindValue(':file', json_encode($files), PDO::PARAM_STR); - $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $query->bindValue(':id', $id, PDO::PARAM_INT); $query->bindValue(':board', $board['uri']); $query->execute() or error(db_error($query)); if ($post['thread']) buildThread($post['thread']); else - buildThread($id_for_board); + buildThread($id); } // rebuild post (markup) -function rebuildPost($id_for_board) { +function rebuildPost($id) { global $board; - $query = prepare("SELECT `body_nomarkup`, `thread` FROM ``posts`` WHERE `id_for_board` = :id_for_board AND `board` = :board"); - $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $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)); @@ -1054,24 +1054,24 @@ function rebuildPost($id_for_board) { markup($body = &$post['body_nomarkup']); - $query = prepare("UPDATE ``posts`` SET `body` = :body WHERE `id_for_board` = :id_for_board AND `board` = :board"); + $query = prepare("UPDATE ``posts`` SET `body` = :body WHERE `id` = :id AND `board` = :board"); $query->bindValue(':body', $body); - $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $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_for_board); + buildThread($post['thread'] ? $post['thread'] : $id); return true; } // Delete a post (reply or thread) -function deletePost($id_for_board, $error_if_doesnt_exist=true, $rebuild_after=true) { +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("SELECT `id_for_board`,`thread`,`files` FROM ``posts`` WHERE `board` = :board AND (`id_for_board` = :id_for_board OR `thread` = :id_for_board)"); - $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $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)); @@ -1089,13 +1089,13 @@ function deletePost($id_for_board, $error_if_doesnt_exist=true, $rebuild_after=t if (!$post['thread']) { // Delete thread HTML page - file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['id_for_board'])); - file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $post['id_for_board'])); - file_unlink($board['dir'] . $config['dir']['res'] . sprintf('%d.json', $post['id_for_board'])); + file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['id'])); + file_unlink($board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $post['id'])); + file_unlink($board['dir'] . $config['dir']['res'] . sprintf('%d.json', $post['id'])); $antispam_query = prepare('DELETE FROM ``antispam`` WHERE `board` = :board AND `thread` = :thread'); $antispam_query->bindValue(':board', $board['uri']); - $antispam_query->bindValue(':thread', $post['id_for_board']); + $antispam_query->bindValue(':thread', $post['id']); $antispam_query->execute() or error(db_error($antispam_query)); } elseif ($query->rowCount() == 1) { // Rebuild thread @@ -1111,12 +1111,12 @@ function deletePost($id_for_board, $error_if_doesnt_exist=true, $rebuild_after=t } } - $ids[] = (int)$post['id_for_board']; + $ids[] = (int)$post['id']; } - $query = prepare("DELETE FROM ``posts`` WHERE `board` = :board AND (`id_for_board` = :id_for_board OR `thread` = :id_for_board)"); - $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $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)); @@ -1152,13 +1152,13 @@ function clean() { $offset = round($config['max_pages']*$config['threads_per_page']); // I too wish there was an easier way of doing this... - $query = prepare("SELECT `id_for_board` FROM ``posts`` WHERE `board` = :board AND `thread` IS NULL ORDER BY `sticky` DESC, `bump` DESC LIMIT :offset, 9001"); + $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)); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { - deletePost($post['id_for_board'], false, false); + deletePost($post['id'], false, false); } } @@ -1196,7 +1196,7 @@ function index($page, $mod=false) { $thread = new Thread($th, $mod ? '?/' : $config['root'], $mod); if ($config['cache']['enabled']) { - $cached = cache::get("thread_index_{$board['uri']}_{$th['id_for_board']}"); + $cached = cache::get("thread_index_{$board['uri']}_{$th['id']}"); if (isset($cached['replies'], $cached['omitted'])) { $replies = $cached['replies']; $omitted = $cached['omitted']; @@ -1205,8 +1205,8 @@ function index($page, $mod=false) { } } if (!isset($cached)) { - $posts = prepare("SELECT * FROM ``posts`` WHERE `thread` = :id_for_board AND `board` = :board ORDER BY `id_for_board` DESC LIMIT :limit"); - $posts->bindValue(':id_for_board', $th['id_for_board']); + $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)); @@ -1214,14 +1214,14 @@ function index($page, $mod=false) { $replies = array_reverse($posts->fetchAll(PDO::FETCH_ASSOC)); if (count($replies) == ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview'])) { - $count = numPosts($th['id_for_board']); + $count = numPosts($th['id']); $omitted = array('post_count' => $count['replies'], 'image_count' => $count['images']); } else { $omitted = false; } if ($config['cache']['enabled']) - cache::set("thread_index_{$board['uri']}_{$th['id_for_board']}", array( + cache::set("thread_index_{$board['uri']}_{$th['id']}", array( 'replies' => $replies, 'omitted' => $omitted, )); @@ -1386,10 +1386,10 @@ function checkRobot($body) { } // Returns an associative array with 'replies' and 'images' keys -function numPosts($id_for_board) { +function numPosts($id) { global $board; $query = prepare("SELECT COUNT(*) AS `replies`, SUM(`num_files`) AS `images` FROM ``posts`` WHERE `thread` = :thread AND `board` = :board"); - $query->bindValue(':thread', $id_for_board, PDO::PARAM_INT); + $query->bindValue(':thread', $id, PDO::PARAM_INT); $query->bindValue(':board', $board['uri']); $query->execute() or error(db_error($query)); @@ -1774,12 +1774,12 @@ function markup(&$body, $track_cites = false) { } $search_cites = array_unique($search_cites); - $query = query(sprintf('SELECT `thread`, `id_for_board` FROM ``posts`` WHERE `board` = "%s" ' . + $query = query(sprintf('SELECT `thread`, `id` FROM ``posts`` WHERE `board` = "%s" ' . implode(' OR ', $search_cites), $board['uri'])) or error(db_error()); $cited_posts = array(); while ($cited = $query->fetch(PDO::FETCH_ASSOC)) { - $cited_posts[$cited['id_for_board']] = $cited['thread'] ? $cited['thread'] : false; + $cited_posts[$cited['id']] = $cited['thread'] ? $cited['thread'] : false; } foreach ($cites as $matches) { @@ -1847,7 +1847,7 @@ function markup(&$body, $track_cites = false) { foreach ($search_cites as $cite) { if (!$cite || isset($cited_posts[$_board][$cite])) continue; - $clauses[] = '`id_for_board` = ' . $cite; + $clauses[] = '`id` = ' . $cite; } $clauses = array_unique($clauses); @@ -1859,12 +1859,12 @@ function markup(&$body, $track_cites = false) { if (!empty($clauses)) { $cited_posts[$_board] = array(); - $query = query(sprintf('SELECT `thread`, `id_for_board` FROM ``posts`` WHERE `board` = "%s"' . + $query = query(sprintf('SELECT `thread`, `id` FROM ``posts`` WHERE `board` = "%s"' . implode(' OR ', $clauses), $board['uri'])) or error(db_error()); while ($cite = $query->fetch(PDO::FETCH_ASSOC)) { - $cited_posts[$_board][$cite['id_for_board']] = $config['root'] . $board['dir'] . $config['dir']['res'] . - ($cite['thread'] ? $cite['thread'] : $cite['id_for_board']) . '.html#' . $cite['id_for_board']; + $cited_posts[$_board][$cite['id']] = $config['root'] . $board['dir'] . $config['dir']['res'] . + ($cite['thread'] ? $cite['thread'] : $cite['id']) . '.html#' . $cite['id']; } } @@ -1999,21 +1999,21 @@ function strip_combining_chars($str) { return $str; } -function buildThread($id_for_board, $return = false, $mod = false) { +function buildThread($id, $return = false, $mod = false) { global $board, $config, $build_pages; - $id_for_board = round($id_for_board); + $id = round($id); - if (event('build-thread', $id_for_board)) + if (event('build-thread', $id)) return; if ($config['cache']['enabled'] && !$mod) { // Clear cache - cache::delete("thread_index_{$board['uri']}_{$id_for_board}"); - cache::delete("thread_{$board['uri']}_{$id_for_board}"); + cache::delete("thread_index_{$board['uri']}_{$id}"); + cache::delete("thread_{$board['uri']}_{$id}"); } - $query = prepare("SELECT * FROM ``posts`` WHERE `board` = :board AND ((`thread` IS NULL AND `id_for_board` = :id_for_board) OR `thread` = :id_for_board) ORDER BY `thread`,`id_for_board`"); - $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $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)); @@ -2030,14 +2030,14 @@ function buildThread($id_for_board, $return = false, $mod = false) { error($config['error']['nonexistant']); $hasnoko50 = $thread->postCount() >= $config['noko50_min']; - $antibot = $mod || $return ? false : create_antibot($board['uri'], $id_for_board); + $antibot = $mod || $return ? false : create_antibot($board['uri'], $id); $body = Element('thread.html', array( 'board' => $board, 'thread' => $thread, 'body' => $thread->build(), 'config' => $config, - 'id' => $id_for_board, + 'id' => $id, 'mod' => $mod, 'hasnoko50' => $hasnoko50, 'isnoko50' => false, @@ -2047,38 +2047,38 @@ function buildThread($id_for_board, $return = false, $mod = false) { )); if ($config['try_smarter'] && !$mod) - $build_pages[] = thread_find_page($id_for_board); + $build_pages[] = thread_find_page($id); // json api if ($config['api']['enabled']) { $api = new Api(); $json = json_encode($api->translateThread($thread)); - $jsonFilename = $board['dir'] . $config['dir']['res'] . $id_for_board . '.json'; + $jsonFilename = $board['dir'] . $config['dir']['res'] . $id . '.json'; file_write($jsonFilename, $json); } if ($return) { return $body; } else { - $noko50fn = $board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $id_for_board); + $noko50fn = $board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $id); if ($hasnoko50 || file_exists($noko50fn)) { - buildThread50($id_for_board, $return, $mod, $thread, $antibot); + buildThread50($id, $return, $mod, $thread, $antibot); } - file_write($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $id_for_board), $body); + file_write($board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $id), $body); } } -function buildThread50($id_for_board, $return = false, $mod = false, $thread = null, $antibot = false) { +function buildThread50($id, $return = false, $mod = false, $thread = null, $antibot = false) { global $board, $config, $build_pages; - $id_for_board = round($id_for_board); + $id = round($id); if ($antibot) $antibot->reset(); if (!$thread) { - $query = prepare("SELECT * FROM ``posts`` WHERE `board` = :board AND (`thread` IS NULL AND `id_for_board` = :id_for_board) OR `thread` = :id_for_board ORDER BY `thread`,`id_for_board` DESC LIMIT :limit"); - $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $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)); @@ -2101,10 +2101,10 @@ function buildThread50($id_for_board, $return = false, $mod = false, $thread = n if ($query->rowCount() == $config['noko50_count']+1) { - $count = prepare("SELECT COUNT(`id_for_board`) as `num` FROM ``posts`` WHERE `board` = :board AND `thread` = :thread UNION ALL + $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_for_board, PDO::PARAM_INT); + $count->bindValue(':thread', $id, PDO::PARAM_INT); $count->execute() or error(db_error($count)); $c = $count->fetch(); @@ -2135,11 +2135,11 @@ function buildThread50($id_for_board, $return = false, $mod = false, $thread = n 'thread' => $thread, 'body' => $thread->build(false, true), 'config' => $config, - 'id' => $id_for_board, + 'id' => $id, 'mod' => $mod, 'hasnoko50' => $hasnoko50, 'isnoko50' => true, - 'antibot' => $mod ? false : ($antibot ? $antibot : create_antibot($board['uri'], $id_for_board)), + 'antibot' => $mod ? false : ($antibot ? $antibot : create_antibot($board['uri'], $id)), 'boardlist' => createBoardlist($mod), 'return' => ($mod ? '?' . $board['url'] . $config['file_index'] : $config['root'] . $board['dir'] . $config['file_index']) )); @@ -2147,7 +2147,7 @@ function buildThread50($id_for_board, $return = false, $mod = false, $thread = n if ($return) { return $body; } else { - file_write($board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $id_for_board), $body); + file_write($board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $id), $body); } } @@ -2244,7 +2244,7 @@ function fraction($numerator, $denominator, $sep) { function getPostByHash($hash) { global $board; - $query = prepare("SELECT `id_for_board`,`thread` FROM ``posts`` WHERE `board` = :board AND `filehash` = :hash"); + $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)); @@ -2258,7 +2258,7 @@ function getPostByHash($hash) { function getPostByHashInThread($hash, $thread) { global $board; - $query = prepare("SELECT `id_for_board`,`thread` FROM ``posts`` WHERE `board` = :board AND `filehash` = :hash AND ( `thread` = :thread OR `id` = :thread )"); + $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); diff --git a/inc/mod/pages.php b/inc/mod/pages.php index 6b04f1a42..1c4c54d98 100755 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -811,7 +811,7 @@ function mod_page_ip($ip) { openBoard($board['uri']); if (!hasPermission($config['mod']['show_ip'], $board['uri'])) continue; - $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `ip` = :ip ORDER BY `sticky` DESC, `id_for_board` DESC LIMIT :limit'); + $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `ip` = :ip ORDER BY `sticky` DESC, `id` DESC LIMIT :limit'); $query->bindValue(':board', $board['uri']); $query->bindValue(':ip', $ip); $query->bindValue(':limit', $config['mod']['ip_recentposts'], PDO::PARAM_INT); @@ -972,10 +972,10 @@ function mod_ban_appeals() { $ban['post'] = json_decode($ban['post'], true); $ban['mask'] = Bans::range_to_string(array($ban['ipstart'], $ban['ipend'])); - if ($ban['post'] && isset($ban['post']['board'], $ban['post']['id_for_board'])) { + if ($ban['post'] && isset($ban['post']['board'], $ban['post']['id'])) { if (openBoard($ban['post']['board'])) { - $query = query(sprintf("SELECT `num_files`, `files` FROM ``posts`` WHERE `board` = '%s' AND `id_for_board` = " . - (int)$ban['post']['id_for_board'], $board['uri'])); + $query = query(sprintf("SELECT `num_files`, `files` FROM ``posts`` WHERE `board` = '%s' AND `id` = " . + (int)$ban['post']['id'], $board['uri'])); if ($_post = $query->fetch(PDO::FETCH_ASSOC)) { $_post['files'] = $_post['files'] ? json_decode($_post['files']) : array(); $ban['post'] = array_merge($ban['post'], $_post); @@ -1015,9 +1015,9 @@ function mod_lock($board, $unlock, $post) { if (!hasPermission($config['mod']['lock'], $board)) error($config['error']['noaccess']); - $query = prepare('UPDATE ``posts`` SET `locked` = :locked WHERE `id_for_board` = :id_for_board AND `board` = :board AND `thread` IS NULL'); + $query = prepare('UPDATE ``posts`` SET `locked` = :locked WHERE `id` = :id AND `board` = :board AND `thread` IS NULL'); $query->bindValue(':board', $board); - $query->bindValue(':id_for_board', $post); + $query->bindValue(':id', $post); $query->bindValue(':locked', $unlock ? 0 : 1); $query->execute() or error(db_error($query)); if ($query->rowCount()) { @@ -1050,9 +1050,9 @@ function mod_sticky($board, $unsticky, $post) { if (!hasPermission($config['mod']['sticky'], $board)) error($config['error']['noaccess']); - $query = prepare('UPDATE ``posts`` SET `sticky` = :sticky WHERE `board` = :board AND `id_for_board` = :id_for_board AND `thread` IS NULL'); + $query = prepare('UPDATE ``posts`` SET `sticky` = :sticky WHERE `board` = :board AND `id` = :id AND `thread` IS NULL'); $query->bindValue(':board', $board); - $query->bindValue(':id_for_board', $post); + $query->bindValue(':id', $post); $query->bindValue(':sticky', $unsticky ? 0 : 1); $query->execute() or error(db_error($query)); if ($query->rowCount()) { @@ -1073,9 +1073,9 @@ function mod_bumplock($board, $unbumplock, $post) { if (!hasPermission($config['mod']['bumplock'], $board)) error($config['error']['noaccess']); - $query = prepare('UPDATE ``posts`` SET `sage` = :bumplock WHERE `board` = :board AND `id_for_board` = :id_for_board AND `thread` IS NULL'); + $query = prepare('UPDATE ``posts`` SET `sage` = :bumplock WHERE `board` = :board AND `id` = :id AND `thread` IS NULL'); $query->bindValue(':board', $board); - $query->bindValue(':id_for_board', $post); + $query->bindValue(':id', $post); $query->bindValue(':bumplock', $unbumplock ? 0 : 1); $query->execute() or error(db_error($query)); if ($query->rowCount()) { @@ -1096,9 +1096,9 @@ function mod_move_reply($originBoard, $postID) { if (!hasPermission($config['mod']['move'], $originBoard)) error($config['error']['noaccess']); - $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `id` = :id'); $query->bindValue(':board', $originBoard); - $query->bindValue(':id_for_board', $postID); + $query->bindValue(':id', $postID); $query->execute() or error(db_error($query)); if (!$post = $query->fetch(PDO::FETCH_ASSOC)) error($config['error']['404']); @@ -1107,9 +1107,9 @@ function mod_move_reply($originBoard, $postID) { $targetBoard = $_POST['board']; if ($_POST['target_thread']) { - $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `id` = :id'); $query->bindValue(':board', $targetBoard); - $query->bindValue(':id_for_board', $_POST['target_thread']); + $query->bindValue(':id', $_POST['target_thread']); $query->execute() or error(db_error($query)); // If it fails, thread probably does not exist $post['op'] = false; $post['thread'] = $_POST['target_thread']; @@ -1169,9 +1169,9 @@ function mod_move_reply($originBoard, $postID) { openBoard($targetBoard); // Find new thread on our target board - $query = prepare('SELECT thread FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query = prepare('SELECT thread FROM ``posts`` WHERE `board` = :board AND `id` = :id'); $query->bindValue(':board', $targetBoard); - $query->bindValue(':id_for_board', $newID); + $query->bindValue(':id', $newID); $query->execute() or error(db_error($query)); $post = $query->fetch(PDO::FETCH_ASSOC); @@ -1199,9 +1199,9 @@ function mod_move($originBoard, $postID) { if (!hasPermission($config['mod']['move'], $originBoard)) error($config['error']['noaccess']); - $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board AND `thread` IS NULL'); + $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `id` = :id AND `thread` IS NULL'); $query->bindValue(':board', $originBoard); - $query->bindValue(':id_for_board', $postID); + $query->bindValue(':id', $postID); $query->execute() or error(db_error($query)); if (!$post = $query->fetch(PDO::FETCH_ASSOC)) error($config['error']['404']); @@ -1254,9 +1254,9 @@ function mod_move($originBoard, $postID) { // go back to the original board to fetch replies openBoard($originBoard); - $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `thread` = :id_for_board ORDER BY `id_for_board`'); + $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `thread` = :id ORDER BY `id`'); $query->bindValue(':board', $originBoard); - $query->bindValue(':id_for_board', $postID, PDO::PARAM_INT); + $query->bindValue(':id', $postID, PDO::PARAM_INT); $query->execute() or error(db_error($query)); $replies = array(); @@ -1286,7 +1286,7 @@ function mod_move($originBoard, $postID) { foreach ($replies as &$post) { $query = prepare('SELECT `target` FROM ``cites`` WHERE `target_board` = :board AND `board` = :board AND `post` = :post'); $query->bindValue(':board', $originBoard); - $query->bindValue(':post', $post['id_for_board'], PDO::PARAM_INT); + $query->bindValue(':post', $post['id'], PDO::PARAM_INT); $query->execute() or error(db_error($qurey)); // correct >>X links @@ -1314,7 +1314,7 @@ function mod_move($originBoard, $postID) { } } // insert reply - $newIDs[$post['id_for_board']] = $newPostID = post($post); + $newIDs[$post['id']] = $newPostID = post($post); if (!empty($post['tracked_cites'])) { @@ -1344,9 +1344,9 @@ function mod_move($originBoard, $postID) { if ($shadow) { // lock old thread - $query = prepare('UPDATE ``posts`` SET `locked` = 1 WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query = prepare('UPDATE ``posts`` SET `locked` = 1 WHERE `board` = :board AND `id` = :id'); $query->bindValue(':board', $originBoard); - $query->bindValue(':id_for_board', $postID, PDO::PARAM_INT); + $query->bindValue(':id', $postID, PDO::PARAM_INT); $query->execute() or error(db_error($query)); // leave a reply, linking to the new thread @@ -1405,9 +1405,9 @@ function mod_ban_post($board, $delete, $post, $token = false) { $security_token = make_secure_link_token($board . '/ban/' . $post); $query = prepare('SELECT ' . ($config['ban_show_post'] ? '*' : '`ip`, `thread`') . - ' FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board'); + ' FROM ``posts`` WHERE `board` = :board AND `id` = :id'); $query->bindValue(':board', $board); - $query->bindValue(':id_for_board', $post); + $query->bindValue(':id', $post); $query->execute() or error(db_error($query)); if (!$_post = $query->fetch(PDO::FETCH_ASSOC)) error($config['error']['404']); @@ -1430,9 +1430,9 @@ function mod_ban_post($board, $delete, $post, $token = false) { $_POST['message'] = preg_replace('/[\r\n]/', '', $_POST['message']); $_POST['message'] = str_replace('%length%', $length_english, $_POST['message']); $_POST['message'] = str_replace('%LENGTH%', strtoupper($length_english), $_POST['message']); - $query = prepare('UPDATE ``posts`` SET `body_nomarkup` = CONCAT(`body_nomarkup`, :body_nomarkup) WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query = prepare('UPDATE ``posts`` SET `body_nomarkup` = CONCAT(`body_nomarkup`, :body_nomarkup) WHERE `board` = :board AND `id` = :id'); $query->bindValue(':board', $board); - $query->bindValue(':id_for_board', $post); + $query->bindValue(':id', $post); $query->bindValue(':body_nomarkup', sprintf("\n%s", utf8tohtml($_POST['message']))); $query->execute() or error(db_error($query)); rebuildPost($post); @@ -1480,9 +1480,9 @@ function mod_edit_post($board, $edit_raw_html, $postID) { $security_token = make_secure_link_token($board . '/edit' . ($edit_raw_html ? '_raw' : '') . '/' . $postID); - $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `id` = :id'); $query->bindValue(':board', $board); - $query->bindValue(':id_for_board', $postID); + $query->bindValue(':id', $postID); $query->execute() or error(db_error($query)); if (!$post = $query->fetch(PDO::FETCH_ASSOC)) @@ -1490,11 +1490,11 @@ function mod_edit_post($board, $edit_raw_html, $postID) { if (isset($_POST['name'], $_POST['email'], $_POST['subject'], $_POST['body'])) { if ($edit_raw_html) - $query = prepare('UPDATE ``posts`` SET `name` = :name, `email` = :email, `subject` = :subject, `body` = :body, `body_nomarkup` = :body_nomarkup WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query = prepare('UPDATE ``posts`` SET `name` = :name, `email` = :email, `subject` = :subject, `body` = :body, `body_nomarkup` = :body_nomarkup WHERE `board` = :board AND `id` = :id'); else - $query = prepare('UPDATE ``posts`` SET `name` = :name, `email` = :email, `subject` = :subject, `body_nomarkup` = :body WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query = prepare('UPDATE ``posts`` SET `name` = :name, `email` = :email, `subject` = :subject, `body_nomarkup` = :body WHERE `board` = :board AND `id` = :id'); $query->bindValue(':board', $board); - $query->bindValue(':id_for_board', $postID); + $query->bindValue(':id', $postID); $query->bindValue('name', $_POST['name']); $query->bindValue(':email', $_POST['email']); $query->bindValue(':subject', $_POST['subject']); @@ -1585,9 +1585,9 @@ function mod_spoiler_image($board, $post, $file) { error($config['error']['noaccess']); // Delete file thumbnail - $query = prepare("SELECT `files`, `thread` FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board"); + $query = prepare("SELECT `files`, `thread` FROM ``posts`` WHERE `board` = :board AND `id` = :id"); $query->bindValue(':board', $board); - $query->bindValue(':id_for_board', $post, PDO::PARAM_INT); + $query->bindValue(':id', $post, PDO::PARAM_INT); $query->execute() or error(db_error($query)); $result = $query->fetch(PDO::FETCH_ASSOC); $files = json_decode($result['files']); @@ -1598,10 +1598,10 @@ function mod_spoiler_image($board, $post, $file) { $files[$file]->thumbwidth = 128; // Make thumbnail spoiler - $query = prepare("UPDATE ``posts`` SET `files` = :files WHERE `board` = :board AND `id_for_board` = :id_for_board"); + $query = prepare("UPDATE ``posts`` SET `files` = :files WHERE `board` = :board AND `id` = :id"); $query->bindValue(':files', json_encode($files)); $query->bindValue(':board', $board); - $query->bindValue(':id_for_board', $post, PDO::PARAM_INT); + $query->bindValue(':id', $post, PDO::PARAM_INT); $query->execute() or error(db_error($query)); // Record the action @@ -1635,9 +1635,9 @@ function mod_deletebyip($boardName, $post, $global = false) { error($config['error']['noaccess']); // Find IP address - $query = prepare('SELECT `ip` FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board'); + $query = prepare('SELECT `ip` FROM ``posts`` WHERE `board` = :board AND `id` = :id'); $query->bindValue(':board', $boardName); - $query->bindValue(':id_for_board', $post); + $query->bindValue(':id', $post); $query->execute() or error(db_error($query)); if (!$ip = $query->fetchColumn()) error($config['error']['invalidpost']); @@ -1646,7 +1646,7 @@ function mod_deletebyip($boardName, $post, $global = false) { $query = ''; foreach ($boards as $_board) { - $query .= sprintf("SELECT `thread`, `id_for_board` FROM ``posts`` WHERE `board` = '%s' AND `ip` = :ip UNION ALL ", $_board['uri']); + $query .= sprintf("SELECT `thread`, `id` FROM ``posts`` WHERE `board` = '%s' AND `ip` = :ip UNION ALL ", $_board['uri']); } $query = preg_replace('/UNION ALL $/', '', $query); @@ -1664,14 +1664,14 @@ function mod_deletebyip($boardName, $post, $global = false) { while ($post = $query->fetch(PDO::FETCH_ASSOC)) { openBoard($post['board']); - deletePost($post['id_for_board'], false, false); + deletePost($post['id'], false, false); rebuildThemes('post-delete', $board['uri']); if ($post['thread']) $threads_to_rebuild[$post['board']][$post['thread']] = true; else - $threads_deleted[$post['board']][$post['id_for_board']] = true; + $threads_deleted[$post['board']][$post['id']] = true; } foreach ($threads_to_rebuild as $_board => $_threads) { @@ -2127,10 +2127,10 @@ function mod_rebuild() { } if (isset($_POST['rebuild_thread'])) { - $query = query(sprintf("SELECT `id_for_board` FROM ``posts`` WHERE `board` = '%s' AND `thread` IS NULL", $board['uri'])) or error(db_error()); + $query = query(sprintf("SELECT `id` FROM ``posts`` WHERE `board` = '%s' AND `thread` IS NULL", $board['uri'])) or error(db_error()); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { - $log[] = '' . sprintf($config['board_abbreviation'], $board['uri']) . ': Rebuilding thread #' . $post['id_for_board']; - buildThread($post['id_for_board']); + $log[] = '' . sprintf($config['board_abbreviation'], $board['uri']) . ': Rebuilding thread #' . $post['id']; + buildThread($post['id']); } } } @@ -2179,9 +2179,9 @@ function mod_reports($global = false) { foreach ($report_queries as $board => $posts) { $report_posts[$board] = array(); - $query = query(sprintf('SELECT * FROM ``posts`` WHERE `board` = "%s" AND `id_for_board` = ' . implode(' OR `id_for_board` = ', $posts), $board)) or error(db_error()); + $query = query(sprintf('SELECT * FROM ``posts`` WHERE `board` = "%s" AND `id` = ' . implode(' OR `id` = ', $posts), $board)) or error(db_error()); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { - $report_posts[$board][$post['id_for_board']] = $post; + $report_posts[$board][$post['id']] = $post; } } diff --git a/install.sql b/install.sql old mode 100644 new mode 100755 index 0762f231b..49c599071 --- a/install.sql +++ b/install.sql @@ -85,7 +85,6 @@ CREATE TABLE IF NOT EXISTS `posts` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `board` varchar(58) NOT NULL, `thread` int(11) DEFAULT NULL, - `id_for_board` int(11) unsigned NOT NULL, `subject` varchar(100) DEFAULT NULL, `email` varchar(30) DEFAULT NULL, `name` varchar(35) DEFAULT NULL, @@ -104,15 +103,14 @@ CREATE TABLE IF NOT EXISTS `posts` ( `locked` int(1) NOT NULL, `sage` int(1) NOT NULL, `embed` text, - PRIMARY KEY (`id`), - UNIQUE KEY `id` (`id`), - UNIQUE KEY `board_id_for_board` (`board`,`id_for_board`), + PRIMARY KEY (`board`,`id`), + UNIQUE KEY `board_id` (`board`,`id`), KEY `thread_id` (`thread`,`id`), KEY `filehash` (`filehash`(40)), KEY `time` (`time`), KEY `ip` (`ip`), KEY `list_threads` (`thread`,`sticky`,`bump`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1; +) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1; -- -------------------------------------------------------- diff --git a/post.php b/post.php index 37ca9cc9b..dc096c8b3 100755 --- a/post.php +++ b/post.php @@ -50,17 +50,17 @@ function strip_array($var) { if (empty($delete)) error($config['error']['nodelete']); - foreach ($delete as &$id_for_board) { - $query = prepare("SELECT `thread`, `time`,`password` FROM ``posts`` WHERE `id_for_board` = :id_for_board AND `board` = :board"); - $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + foreach ($delete as &$id) { + $query = prepare("SELECT `thread`, `time`,`password` 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)) { $thread = false; if ($config['user_moderation'] && $post['thread']) { - $thread_query = prepare("SELECT `time`,`password` FROM ``posts`` WHERE `id_for_board` = :id_for_board AND `board` = :board"); - $thread_query->bindValue(':id_for_board', $post['thread'], PDO::PARAM_INT); + $thread_query = prepare("SELECT `time`,`password` FROM ``posts`` WHERE `id` = :id AND `board` = :board"); + $thread_query->bindValue(':id', $post['thread'], PDO::PARAM_INT); $thread_query->bindValue(':board', $board['uri']); $thread_query->execute() or error(db_error($query)); @@ -76,14 +76,14 @@ function strip_array($var) { if (isset($_POST['file'])) { // Delete just the file - deleteFile($id_for_board); + deleteFile($id); } else { // Delete entire post - deletePost($id_for_board); + deletePost($id); } _syslog(LOG_INFO, 'Deleted post: ' . - '/' . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['thread'] ? $post['thread'] : $id_for_board) . ($post['thread'] ? '#' . $id_for_board : '') + '/' . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $post['thread'] ? $post['thread'] : $id) . ($post['thread'] ? '#' . $id : '') ); } } @@ -131,24 +131,24 @@ function strip_array($var) { $reason = escape_markup_modifiers($_POST['reason']); markup($reason); - foreach ($report as &$id_for_board) { - $query = prepare("SELECT `thread` FROM ``posts`` WHERE `board` = :board AND `id_for_board` = :id_for_board"); + foreach ($report as &$id) { + $query = prepare("SELECT `thread` FROM ``posts`` WHERE `board` = :board AND `id` = :id"); $query->bindValue(':board', $board['uri']); - $query->bindValue(':id_for_board', $id_for_board, PDO::PARAM_INT); + $query->bindValue(':id', $id, PDO::PARAM_INT); $query->execute() or error(db_error($query)); $thread = $query->fetchColumn(); if ($config['syslog']) _syslog(LOG_INFO, 'Reported post: ' . - '/' . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $thread ? $thread : $id_for_board) . ($thread ? '#' . $id_for_board : '') . + '/' . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], $thread ? $thread : $id) . ($thread ? '#' . $id : '') . ' for "' . $reason . '"' ); $query = prepare("INSERT INTO ``reports`` VALUES (NULL, :time, :ip, :board, :post, :reason, :global)"); $query->bindValue(':time', time(), PDO::PARAM_INT); $query->bindValue(':ip', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR); $query->bindValue(':board', $board['uri'], PDO::PARAM_INT); - $query->bindValue(':post', $id_for_board, PDO::PARAM_INT); + $query->bindValue(':post', $id, PDO::PARAM_INT); $query->bindValue(':reason', $reason, PDO::PARAM_STR); $query->bindValue(':global', isset($_POST['global']), PDO::PARAM_BOOL); $query->execute() or error(db_error($query)); @@ -254,8 +254,8 @@ function strip_array($var) { //Check if thread exists if (!$post['op']) { - $query = prepare("SELECT `sticky`,`locked`,`sage` FROM ``posts`` WHERE `id_for_board` = :id_for_board AND `thread` IS NULL AND `board` = :board LIMIT 1"); - $query->bindValue(':id_for_board', $post['thread'], PDO::PARAM_INT); + $query = prepare("SELECT `sticky`,`locked`,`sage` FROM ``posts`` WHERE `id` = :id AND `thread` IS NULL AND `board` = :board LIMIT 1"); + $query->bindValue(':id', $post['thread'], PDO::PARAM_INT); $query->bindValue(':board', $board['uri']); $query->execute() or error(db_error()); @@ -739,9 +739,9 @@ function ipv4to6($ip) { ($post['mod'] ? $config['root'] . $config['file_mod'] . '?/' : $config['root']) . ($board['dir'] . $config['dir']['res'] . ($p['thread'] ? - $p['thread'] . '.html#' . $p['id_for_board'] + $p['thread'] . '.html#' . $p['id'] : - $p['id_for_board'] . '.html' + $p['id'] . '.html' )) )); } @@ -752,9 +752,9 @@ function ipv4to6($ip) { ($post['mod'] ? $config['root'] . $config['file_mod'] . '?/' : $config['root']) . ($board['dir'] . $config['dir']['res'] . ($p['thread'] ? - $p['thread'] . '.html#' . $p['id_for_board'] + $p['thread'] . '.html#' . $p['id'] : - $p['id_for_board'] . '.html' + $p['id'] . '.html' )) )); } @@ -796,7 +796,7 @@ function ipv4to6($ip) { $post['files'] = $post['files']; $post['num_files'] = sizeof($post['files']); - $post['id_for_board'] = $id_for_board = post($post); + $post['id'] = $id = post($post); insertFloodPost($post); @@ -808,7 +808,7 @@ function ipv4to6($ip) { $insert_rows = array(); foreach ($post['tracked_cites'] as $cite) { $insert_rows[] = '(' . - $pdo->quote($board['uri']) . ', ' . (int)$id_for_board . ', ' . + $pdo->quote($board['uri']) . ', ' . (int)$id . ', ' . $pdo->quote($cite[0]) . ', ' . (int)$cite[1] . ')'; } query('INSERT INTO ``cites`` VALUES ' . implode(', ', $insert_rows)) or error(db_error()); @@ -818,7 +818,7 @@ function ipv4to6($ip) { bumpThread($post['thread']); } - buildThread($post['op'] ? $id_for_board : $post['thread']); + buildThread($post['op'] ? $id : $post['thread']); if ($config['try_smarter'] && $post['op']) $build_pages = range(1, $config['max_pages']); @@ -846,7 +846,7 @@ function ipv4to6($ip) { if ($noko) { $redirect = $root . $board['dir'] . $config['dir']['res'] . - sprintf($config['file_page'], $post['op'] ? $id_for_board:$post['thread']) . (!$post['op'] ? '#' . $id_for_board : ''); + sprintf($config['file_page'], $post['op'] ? $id:$post['thread']) . (!$post['op'] ? '#' . $id : ''); if (!$post['op'] && isset($_SERVER['HTTP_REFERER'])) { $regex = array( @@ -858,7 +858,7 @@ function ipv4to6($ip) { if (preg_match('/\/' . $regex['board'] . $regex['res'] . $regex['page50'] . '([?&].*)?$/', $_SERVER['HTTP_REFERER'])) { $redirect = $root . $board['dir'] . $config['dir']['res'] . - sprintf($config['file_page50'], $post['op'] ? $id_for_board:$post['thread']) . (!$post['op'] ? '#' . $id_for_board : ''); + sprintf($config['file_page50'], $post['op'] ? $id:$post['thread']) . (!$post['op'] ? '#' . $id : ''); } } } else { @@ -868,7 +868,7 @@ function ipv4to6($ip) { if ($config['syslog']) _syslog(LOG_INFO, 'New post: /' . $board['dir'] . $config['dir']['res'] . - sprintf($config['file_page'], $post['op'] ? $id_for_board : $post['thread']) . (!$post['op'] ? '#' . $id_for_board : '')); + sprintf($config['file_page'], $post['op'] ? $id : $post['thread']) . (!$post['op'] ? '#' . $id : '')); if (!$post['mod']) header('X-Associated-Content: "' . $redirect . '"'); @@ -884,7 +884,7 @@ function ipv4to6($ip) { echo json_encode(array( 'redirect' => $redirect, 'noko' => $noko, - 'id' => $id_for_board + 'id' => $id )); } } elseif (isset($_POST['appeal'])) { diff --git a/templates/post/file_controls.html b/templates/post/file_controls.html index 679db613f..6855a2931 100644 --- a/templates/post/file_controls.html +++ b/templates/post/file_controls.html @@ -1,10 +1,10 @@ {% if post.files and mod %} {% if file.file != 'deleted' and mod|hasPermission(config.mod.deletefile, board.uri) %} -{{ secure_link_confirm(config.mod.link_deletefile, 'Delete file'|trans, 'Are you sure you want to delete this file?'|trans, board.dir ~ 'deletefile/' ~ post.id_for_board ~ '/' ~ loop.index0 ) }}  +{{ secure_link_confirm(config.mod.link_deletefile, 'Delete file'|trans, 'Are you sure you want to delete this file?'|trans, board.dir ~ 'deletefile/' ~ post.id ~ '/' ~ loop.index0 ) }}  {% endif %} {% if file.file and file.file != 'deleted' and file.thumb != 'spoiler' and mod|hasPermission(config.mod.spoilerimage, board.uri) %} -{{ secure_link_confirm(config.mod.link_spoilerimage, 'Spoiler file'|trans, 'Are you sure you want to spoiler this file?'|trans, board.dir ~ 'spoiler/' ~ post.id_for_board ~ '/' ~ loop.index0 ) }} +{{ secure_link_confirm(config.mod.link_spoilerimage, 'Spoiler file'|trans, 'Are you sure you want to spoiler this file?'|trans, board.dir ~ 'spoiler/' ~ post.id ~ '/' ~ loop.index0 ) }} {% endif %} {% endif %} diff --git a/templates/post/post_controls.html b/templates/post/post_controls.html index 9cdb393cf..99a8484c5 100644 --- a/templates/post/post_controls.html +++ b/templates/post/post_controls.html @@ -2,41 +2,41 @@ {% if mod|hasPermission(config.mod.delete, board.uri) %} - {{ secure_link_confirm(config.mod.link_delete, 'Delete'|trans, 'Are you sure you want to delete this?'|trans, board.dir ~ 'delete/' ~ post.id_for_board) }}  + {{ secure_link_confirm(config.mod.link_delete, 'Delete'|trans, 'Are you sure you want to delete this?'|trans, board.dir ~ 'delete/' ~ post.id) }}  {% endif %} {% if mod|hasPermission(config.mod.deletebyip, board.uri) %} - {{ secure_link_confirm(config.mod.link_deletebyip, 'Delete all posts by IP'|trans, 'Are you sure you want to delete all posts by this IP address?'|trans, board.dir ~ 'deletebyip/' ~ post.id_for_board) }}  + {{ secure_link_confirm(config.mod.link_deletebyip, 'Delete all posts by IP'|trans, 'Are you sure you want to delete all posts by this IP address?'|trans, board.dir ~ 'deletebyip/' ~ post.id) }}  {% endif %} {% if mod|hasPermission(config.mod.deletebyip_global, board.uri) %} - {{ secure_link_confirm(config.mod.link_deletebyip_global, 'Delete all posts by IP across all boards'|trans, 'Are you sure you want to delete all posts by this IP address, across all boards?'|trans, board.dir ~ 'deletebyip/' ~ post.id_for_board ~ '/global') }}  + {{ secure_link_confirm(config.mod.link_deletebyip_global, 'Delete all posts by IP across all boards'|trans, 'Are you sure you want to delete all posts by this IP address, across all boards?'|trans, board.dir ~ 'deletebyip/' ~ post.id ~ '/global') }}  {% endif %} {% if mod|hasPermission(config.mod.ban, board.uri) %} - {{ config.mod.link_ban }}  + {{ config.mod.link_ban }}  {% endif %} {% if mod|hasPermission(config.mod.bandelete, board.uri) %} - {{ config.mod.link_bandelete }}  + {{ config.mod.link_bandelete }}  {% endif %} {% if not post.thread %} {% if mod|hasPermission(config.mod.sticky, board.uri) %} {% if post.sticky %} - {{ config.mod.link_desticky }}  + {{ config.mod.link_desticky }}  {% else %} - {{ config.mod.link_sticky }}  + {{ config.mod.link_sticky }}  {% endif %} {% endif %} {% if mod|hasPermission(config.mod.bumplock, board.uri) %} {% if post.sage %} - {{ config.mod.link_bumpunlock }}  + {{ config.mod.link_bumpunlock }}  {% else %} - {{ config.mod.link_bumplock }}  + {{ config.mod.link_bumplock }}  {% endif %} {% endif %} {% if mod|hasPermission(config.mod.lock, board.uri) %} {% if post.locked %} - {{ config.mod.link_unlock }}  + {{ config.mod.link_unlock }}  {% else %} - {{ config.mod.link_lock }}  + {{ config.mod.link_lock }}  {% endif %} {% endif %} @@ -44,13 +44,13 @@ {% if mod|hasPermission(config.mod.move, board.uri) %} {% if not post.thread %} - {{ config.mod.link_move }}  + {{ config.mod.link_move }}  {% else %} - {{ config.mod.link_move }}  + {{ config.mod.link_move }}  {% endif %} {% endif %} {% if mod|hasPermission(config.mod.editpost, board.uri) %} - {{ config.mod.link_editpost }}  + {{ config.mod.link_editpost }}  {% endif %} diff --git a/templates/post_reply.html b/templates/post_reply.html index 71bbfccaa..e73432308 100755 --- a/templates/post_reply.html +++ b/templates/post_reply.html @@ -1,10 +1,10 @@ {% filter remove_whitespace %} {# tabs and new lines will be ignored #} -

+

- {% if not index %}{% endif %} - -

{% include 'post/fileinfo.html' %} {% include 'post/post_controls.html' %} diff --git a/templates/post_thread.html b/templates/post_thread.html index 5b2f5772e..6c3d6af5c 100755 --- a/templates/post_thread.html +++ b/templates/post_thread.html @@ -1,13 +1,13 @@ {% filter remove_whitespace %} {# tabs and new lines will be ignored #} -
-{% if not index %}{% endif %} +
+{% if not index %}{% endif %} {% include 'post/fileinfo.html' %}
1%}style='clear:both'{%endif%}>

- -

diff --git a/templates/themes/catalog/catalog.html b/templates/themes/catalog/catalog.html index 9daa7095b..be02a80e6 100644 --- a/templates/themes/catalog/catalog.html +++ b/templates/themes/catalog/catalog.html @@ -52,7 +52,7 @@

{{ settings.title }} (/{{ board }}/)

{% else %} + id="img-{{ post.id }}" data-subject="{% if post.subject %}{{ post.subject }}{% endif %}" data-name="{{ post.name }}" data-muhdifference="{{ post.muhdifference }}" data-last-reply="{% if post.last_reply %}{{ post.last_reply }}{% endif %}" data-last-subject="{% if post.last_reply_subject %}{{ post.last_reply_subject }}{% endif %}" data-last-name="{% if post.last_reply %}{{ post.last_reply_name }}{% endif %}" data-last-difference="{% if post.last_reply %}{{ post.last_reply_difference }}{% endif %}" class="{{post.board}} thread-image" title="{{post.bump|date('%b %d %H:%M')}}">

diff --git a/templates/themes/catalog/theme.php b/templates/themes/catalog/theme.php index c1ad42ad3..3d17f3ec7 100644 --- a/templates/themes/catalog/theme.php +++ b/templates/themes/catalog/theme.php @@ -39,14 +39,14 @@ public function build($settings, $board_name) { $recent_posts = array(); $stats = array(); - $query = query(sprintf("SELECT *, `id_for_board` AS `thread_id`, - (SELECT COUNT(`id_for_board`) FROM ``posts`` WHERE `board` = '%s' AND `thread` = `thread_id`) AS `reply_count`, + $query = query(sprintf("SELECT *, `id` AS `thread_id`, + (SELECT COUNT(`id`) FROM ``posts`` WHERE `board` = '%s' AND `thread` = `thread_id`) AS `reply_count`, (SELECT SUM(`num_files`) FROM ``posts`` WHERE `board` = '%s' AND `thread` = `thread_id` AND `num_files` IS NOT NULL) AS `image_count` FROM ``posts`` WHERE `board` = '%s' AND `thread` IS NULL ORDER BY `bump` DESC", $board_name, $board_name, $board_name, $board_name, $board_name)) or error(db_error()); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { - $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], ($post['thread'] ? $post['thread'] : $post['id_for_board'])); + $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], ($post['thread'] ? $post['thread'] : $post['id'])); $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)) { diff --git a/templates/themes/recent/theme.php b/templates/themes/recent/theme.php index 0d250a254..d8b7dc1fb 100644 --- a/templates/themes/recent/theme.php +++ b/templates/themes/recent/theme.php @@ -62,7 +62,7 @@ public function homepage($settings) { // board settings won't be available in the template file, so generate links now $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] - . sprintf($config['file_page'], ($post['thread'] ? $post['thread'] : $post['id_for_board'])) . '#' . $post['id_for_board']; + . sprintf($config['file_page'], ($post['thread'] ? $post['thread'] : $post['id'])) . '#' . $post['id']; if ($files) { if ($files[0]->thumb == 'spoiler') { @@ -92,7 +92,7 @@ public function homepage($settings) { while ($post = $query->fetch(PDO::FETCH_ASSOC)) { openBoard($post['board']); - $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], ($post['thread'] ? $post['thread'] : $post['id_for_board'])) . '#' . $post['id_for_board']; + $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], ($post['thread'] ? $post['thread'] : $post['id'])) . '#' . $post['id']; if ($post['body'] != "") $post['snippet'] = pm_snippet($post['body'], 30); else @@ -107,7 +107,7 @@ public function homepage($settings) { foreach ($boards as &$_board) { if (in_array($_board['uri'], $this->excluded)) continue; - $query .= sprintf("SELECT MAX(`id_for_board`) AS `top` FROM ``posts`` WHERE `board` = '%s' UNION ALL ", $_board['uri']); + $query .= sprintf("SELECT MAX(`id`) AS `top` FROM ``posts`` WHERE `board` = '%s' UNION ALL ", $_board['uri']); } $query = preg_replace('/UNION ALL $/', ') AS `posts_all`', $query); $query = query($query) or error(db_error()); diff --git a/templates/themes/sitemap/theme.php b/templates/themes/sitemap/theme.php index 0fc69ea4a..a4f87c59d 100644 --- a/templates/themes/sitemap/theme.php +++ b/templates/themes/sitemap/theme.php @@ -26,7 +26,7 @@ function sitemap_build($action, $settings, $board) { $threads = array(); foreach ($boards as $board) { - $query = query(sprintf("SELECT `id_for_board` AS `thread_id`, (SELECT `time` FROM ``posts`` WHERE `board` = '%s' AND`thread` = `thread_id` OR `id_for_board` = `thread_id` ORDER BY `time` DESC LIMIT 1) AS `lastmod` FROM ``posts`` WHERE `board` = '%s' AND`thread` IS NULL", $board, $board)) or error(db_error()); + $query = query(sprintf("SELECT `id` AS `thread_id`, (SELECT `time` FROM ``posts`` WHERE `board` = '%s' AND`thread` = `thread_id` OR `id` = `thread_id` ORDER BY `time` DESC LIMIT 1) AS `lastmod` FROM ``posts`` WHERE `board` = '%s' AND`thread` IS NULL", $board, $board)) or error(db_error()); $threads[$board] = $query->fetchAll(PDO::FETCH_ASSOC); } diff --git a/templates/themes/ukko/theme.php b/templates/themes/ukko/theme.php index 52850c682..927b9524e 100644 --- a/templates/themes/ukko/theme.php +++ b/templates/themes/ukko/theme.php @@ -48,8 +48,8 @@ public function build($mod = false) { $board['dir'] = $post['board'].'/'; $thread = new Thread($post, $mod ? '?/' : $config['root'], $mod); - $posts = prepare("SELECT * FROM ``posts`` WHERE `board` = :board AND `thread` = :id_for_board ORDER BY `id_for_board` DESC LIMIT :limit"); - $posts->bindValue(':id_for_board', $post['id_for_board']); + $posts = prepare("SELECT * FROM ``posts`` WHERE `board` = :board AND `thread` = :id ORDER BY `id` DESC LIMIT :limit"); + $posts->bindValue(':id', $post['id']); $posts->bindValue(':board', $post['board']); $posts->bindValue(':limit', ($post['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']), PDO::PARAM_INT); $posts->execute() or error(db_error($posts)); @@ -66,8 +66,8 @@ public function build($mod = false) { } if ($posts->rowCount() == ($post['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview'])) { - $ct = prepare("SELECT COUNT(`id_for_board`) as `num` FROM ``posts`` WHERE `board` = :board AND `thread` = :thread UNION ALL SELECT COUNT(`id_for_board`) FROM ``posts`` WHERE `board` = :board AND `files` IS NOT NULL AND `thread` = :thread"); - $ct->bindValue(':thread', $post['id_for_board'], PDO::PARAM_INT); + $ct = prepare("SELECT COUNT(`id`) as `num` FROM ``posts`` WHERE `board` = :board AND `thread` = :thread UNION ALL SELECT COUNT(`id`) FROM ``posts`` WHERE `board` = :board AND `files` IS NOT NULL AND `thread` = :thread"); + $ct->bindValue(':thread', $post['id'], PDO::PARAM_INT); $ct->bindValue(':board', $post['board']); $ct->execute() or error(db_error($count)); @@ -87,7 +87,7 @@ public function build($mod = false) { if(floor($threads[$post['board']] / $config['threads_per_page']) > 0) { $page = floor($threads[$post['board']] / $config['threads_per_page']) + 1; } - $overflow[] = array('id' => $post['id_for_board'], 'board' => $post['board'], 'page' => $page . '.html'); + $overflow[] = array('id' => $post['id'], 'board' => $post['board'], 'page' => $page . '.html'); } $count += 1; diff --git a/tools/rebuild.php b/tools/rebuild.php index be9ec0f6f..aac4e070b 100755 --- a/tools/rebuild.php +++ b/tools/rebuild.php @@ -78,19 +78,19 @@ continue; // do no more if($options['full']) { - $query = query(sprintf("SELECT `id_for_board` FROM ``posts`` WHERE `board` = '%s'", $board['uri'])) or error(db_error()); + $query = query(sprintf("SELECT `id` FROM ``posts`` WHERE `board` = '%s'", $board['uri'])) or error(db_error()); while($post = $query->fetch()) { if(!$options['quiet']) - echo "Rebuilding #{$post['id_for_board']}...\n"; - rebuildPost($post['id_for_board']); + echo "Rebuilding #{$post['id']}...\n"; + rebuildPost($post['id']); } } - $query = query(sprintf("SELECT `id_for_board` FROM ``posts`` WHERE `thread` IS NULL AND `board` = '%s'", $board['uri'])) or error(db_error()); + $query = query(sprintf("SELECT `id` FROM ``posts`` WHERE `thread` IS NULL AND `board` = '%s'", $board['uri'])) or error(db_error()); while($post = $query->fetch()) { if(!$options['quiet']) - echo "Rebuilding #{$post['id_for_board']}...\n"; - buildThread($post['id_for_board']); + echo "Rebuilding #{$post['id']}...\n"; + buildThread($post['id']); } } diff --git a/tools/recount-bumps.php b/tools/recount-bumps.php index bf6c8dc8c..b90144d9a 100644 --- a/tools/recount-bumps.php +++ b/tools/recount-bumps.php @@ -9,27 +9,27 @@ } $board = $argv[1]; -$q = query(sprintf("SELECT `id_for_board`, `bump`, `time` FROM ``posts`` +$q = query(sprintf("SELECT `id`, `bump`, `time` FROM ``posts`` WHERE `board` = '%s' AND `thread` IS NULL", $board)); while ($val = $q->fetch()) { $lc = prepare(sprintf('SELECT MAX(`time`) AS `aq` FROM ``posts`` WHERE `board` = "%s" AND ((`thread` = :thread and - `email` != "sage" ) OR `id_for_board` = :thread', $board)); + `email` != "sage" ) OR `id` = :thread', $board)); - $lc->bindValue(":thread", $val['id_for_board']); + $lc->bindValue(":thread", $val['id']); $lc->execute(); $f = $lc->fetch(); if ($val['bump'] != $f['aq']) { $query = prepare("UPDATE ``posts`` SET `bump`=:bump - WHERE `board` = :board AND `id_for_board`=:id_for_board"); + WHERE `board` = :board AND `id`=:id"); $query->bindValue(":bump", $f['aq']); $query->bindValue(":board", $board); - $query->bindValue(":id_for_board", $val['id_for_board']); - echo("Thread $val[id_for_board] - to be $val[bump] -> $f[aq]\n"); + $query->bindValue(":id", $val['id']); + echo("Thread $val[id] - to be $val[bump] -> $f[aq]\n"); } else { - echo("Thread $val[id_for_board] ok\n"); + echo("Thread $val[id] ok\n"); } } From cc09792175b50ecfedbd7cc8d8a7caf529e13687 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Thu, 9 Oct 2014 16:50:01 -0300 Subject: [PATCH 28/33] Migration script. --- install.php | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/install.php b/install.php index 8b3815a24..388855f9f 100644 --- a/install.php +++ b/install.php @@ -1,7 +1,7 @@ execute() or error(db_error($query)); + while($post = $query->fetch(PDO::FETCH_ASSOC)) { + $insert = prepare("INSERT INTO ``posts`` (`id`, `board`, `thread`, `subject`, `email`, `name`, `trip`, `capcode`, `body`, `body_nomarkup`, `time`, `bump`, `files`, `num_files`, `filehash`, `password`, `ip`, `sticky`, `locked`, `sage`, `embed`) VALUES (:id, :board, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, :sage, :embed)"); + $insert->bindValue(':id', $post['id']); + $insert->bindValue(':board', $board['uri']); + $insert->bindValue(':subject', $post['subject']); + $insert->bindValue(':email', $post['email']); + $insert->bindValue(':trip', $post['trip']); + $insert->bindValue(':name', $post['name']); + $insert->bindValue(':body', $post['body']); + $insert->bindValue(':body_nomarkup', $post['body_nomarkup']); + $insert->bindValue(':time', $post['time'], PDO::PARAM_INT); + $insert->bindValue(':password', $post['password']); + $insert->bindValue(':ip', $post['ip']); + $insert->bindValue(':sticky', $post['sticky'], PDO::PARAM_INT); + $insert->bindValue(':locked', $post['locked'], PDO::PARAM_INT); + $insert->bindValue(':capcode', $post['capcode'], PDO::PARAM_INT); + $insert->bindValue(':thread', $post['thread'], PDO::PARAM_INT); + $insert->bindValue(':files', $post['files']); + $insert->bindValue(':num_files', $post['num_files']); + $insert->bindValue(':filehash', $post['filehash']); + $insert->bindValue(':embed', $post['embed']); + $insert->bindValue(':sage', $post['sage']); + $insert->bindValue(':embed', $post['embed']); + $insert->execute() or error(db_error($insert)); + } + query(sprintf("DROP TABLE ``posts_%s``", $board['uri'])); + } + file_write($config['has_installed'], '5.0.0'); + break; case false: // TODO: enhance Tinyboard -> vichan upgrade path. query("CREATE TABLE IF NOT EXISTS ``search_queries`` ( `ip` varchar(39) NOT NULL, `time` int(11) NOT NULL, `query` text NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8;") or error(db_error()); From 257f01f057c49879ad0581bff503d784a171fca4 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Thu, 9 Oct 2014 17:25:21 -0300 Subject: [PATCH 29/33] A little fix in functions.php. --- inc/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/functions.php b/inc/functions.php index 6e9af192a..1ce49b0e0 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -886,7 +886,7 @@ function insertFloodPost(array $post) { function post(array $post) { global $pdo, $board; - $query = prepare("INSERT INTO ``posts`` (`id`, `board`, `thread`, `subject`, `email`, `name`, `trip`, `capcode`, `body`, `body_nomarkup`, `time`, `bump`, `files`, `num_files`, `filehash`, `password`, `ip`, `sticky`, `locked`, `sage`, `embed`) SELECT 1 + coalesce((SELECT max(`id`) FROM ``posts`` WHERE `board`=:board),0), :board, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, 0, :embed"); + $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']); From 9d5f85a44242eaaf8fac1f844d117e2ce8e450de Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Thu, 9 Oct 2014 17:26:30 -0300 Subject: [PATCH 30/33] Forgot instance-config.php. --- inc/instance-config.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/instance-config.php b/inc/instance-config.php index 3191ba403..cd7ef50ff 100644 --- a/inc/instance-config.php +++ b/inc/instance-config.php @@ -334,9 +334,9 @@ function purify($s){ openBoard($b); buildIndex(); buildJavascript(); - $query = query(sprintf("SELECT `id_for_board` FROM ``posts`` WHERE `board` = '%s' AND `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()); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { - buildThread($post['id_for_board']); + buildThread($post['id']); } modLog('Edited board settings', $b); } From 1ef61610ae904a4b1a914e5a19a0c01226248763 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Fri, 10 Oct 2014 09:08:39 -0300 Subject: [PATCH 31/33] Changes the insert query because performance. --- install.php | 65 ++++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/install.php b/install.php index 46720a750..3c6dd71d2 100644 --- a/install.php +++ b/install.php @@ -548,38 +548,43 @@ function __query($sql) { case '4.9.90': case '4.9.91': case '4.9.92': - query("CREATE TABLE IF NOT EXISTS `posts` (`id` int(11) unsigned NOT NULL AUTO_INCREMENT, `board` varchar(58) NOT NULL, `thread` int(11) DEFAULT NULL, `subject` varchar(100) DEFAULT NULL, `email` varchar(30) DEFAULT NULL, `name` varchar(35) DEFAULT NULL, `trip` varchar(15) DEFAULT NULL, `capcode` varchar(50) DEFAULT NULL, `body` text NOT NULL, `body_nomarkup` text, `time` int(11) NOT NULL, `bump` int(11) DEFAULT NULL, `files` text, `num_files` int(11) DEFAULT '0', `filehash` text CHARACTER SET ascii, `password` varchar(20) DEFAULT NULL, `ip` varchar(39) CHARACTER SET ascii NOT NULL, `sticky` int(1) NOT NULL, `locked` int(1) NOT NULL, `sage` int(1) NOT NULL, `embed` text, `edited_at` DATETIME NULL, PRIMARY KEY (`board`,`id`), UNIQUE KEY `board_id` (`board`,`id`), KEY `thread_id` (`thread`,`id`), KEY `filehash` (`filehash`(40)), KEY `time` (`time`), KEY `ip` (`ip`), KEY `list_threads` (`thread`,`sticky`,`bump`)) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1;") or error(db_error()); + query("CREATE TABLE IF NOT EXISTS ``posts`` + (`id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `board` varchar(58) NOT NULL, + `thread` int(11) DEFAULT NULL, + `subject` varchar(100) DEFAULT NULL, + `email` varchar(30) DEFAULT NULL, + `name` varchar(35) DEFAULT NULL, + `trip` varchar(15) DEFAULT NULL, + `capcode` varchar(50) DEFAULT NULL, + `body` text NOT NULL, + `body_nomarkup` text, + `time` int(11) NOT NULL, + `bump` int(11) DEFAULT NULL, + `files` text, + `num_files` int(11) DEFAULT '0', + `filehash` text CHARACTER SET ascii, + `password` varchar(20) DEFAULT NULL, + `ip` varchar(39) CHARACTER SET ascii NOT NULL, + `sticky` int(1) NOT NULL, + `locked` int(1) NOT NULL, + `sage` int(1) NOT NULL, + `embed` text, + `edited_at` DATETIME NULL, + PRIMARY KEY (`board`,`id`), + UNIQUE KEY `board_id` (`board`,`id`), + KEY `thread_id` (`thread`,`id`), + KEY `filehash` (`filehash`(40)), + KEY `time` (`time`), + KEY `ip` (`ip`), + KEY `list_threads` (`thread`,`sticky`,`bump`)) + ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1;") or error(db_error()); + foreach ($boards as &$board) { - $query = prepare(sprintf('SELECT * FROM ``posts_%s``', $board["uri"])); - $query->execute() or error(db_error($query)); - while($post = $query->fetch(PDO::FETCH_ASSOC)) { - $insert = prepare("INSERT INTO ``posts`` (`id`, `board`, `thread`, `subject`, `email`, `name`, `trip`, `capcode`, `body`, `body_nomarkup`, `time`, `bump`, `files`, `num_files`, `filehash`, `password`, `ip`, `sticky`, `locked`, `sage`, `embed`, `edited_at`) VALUES (:id, :board, :thread, :subject, :email, :name, :trip, :capcode, :body, :body_nomarkup, :time, :time, :files, :num_files, :filehash, :password, :ip, :sticky, :locked, :sage, :embed, :edited_at)"); - $insert->bindValue(':id', $post['id']); - $insert->bindValue(':board', $board['uri']); - $insert->bindValue(':subject', $post['subject']); - $insert->bindValue(':email', $post['email']); - $insert->bindValue(':trip', $post['trip']); - $insert->bindValue(':name', $post['name']); - $insert->bindValue(':body', $post['body']); - $insert->bindValue(':body_nomarkup', $post['body_nomarkup']); - $insert->bindValue(':time', $post['time'], PDO::PARAM_INT); - $insert->bindValue(':password', $post['password']); - $insert->bindValue(':ip', $post['ip']); - $insert->bindValue(':sticky', $post['sticky'], PDO::PARAM_INT); - $insert->bindValue(':locked', $post['locked'], PDO::PARAM_INT); - $insert->bindValue(':capcode', $post['capcode'], PDO::PARAM_INT); - $insert->bindValue(':thread', $post['thread'], PDO::PARAM_INT); - $insert->bindValue(':files', $post['files']); - $insert->bindValue(':num_files', $post['num_files']); - $insert->bindValue(':filehash', $post['filehash']); - $insert->bindValue(':embed', $post['embed']); - $insert->bindValue(':sage', $post['sage']); - $insert->bindValue(':embed', $post['embed']); - $insert->bindValue(':edited_at', $post['edited_at']); - $insert->execute() or error(db_error($insert)); - } - query(sprintf("DROP TABLE ``posts_%s``", $board['uri'])); + query(sprintf("INSERT INTO ``posts`` (`id`, `board`, `thread`, `subject`, `email`, `name`, `trip`, `capcode`, `body`, `body_nomarkup`, `time`, `bump`, `files`, `num_files`, `filehash`, `password`, `ip`, `sticky`, `locked`, `sage`, `embed`, `edited_at`) SELECT `id`, '%s', `thread`, `subject`, `email`, `name`, `trip`, `capcode`, `body`, `body_nomarkup`, `time`, `bump`, `files`, `num_files`, `filehash`, `password`, `ip`, `sticky`, `locked`, `sage`, `embed`, `edited_at` FROM ``posts_%s``", $board['uri'], $board['uri'])) or error(db_error()); + query(sprintf("DROP TABLE ``posts_%s``", $board['uri'])) or error(db_error()); } + file_write($config['has_installed'], '5.0.0'); break; case false: From e8ff2d2012dd11dd53744767c1c49ce633172b0a Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Tue, 14 Oct 2014 21:28:55 -0300 Subject: [PATCH 32/33] Changes sprintf to prepare. Also, other changes requested by czaks. --- inc/functions.php | 22 +++++--- inc/mod/pages.php | 88 ++++++++++++++++++++---------- install.php | 1 - install.sql | 1 - templates/themes/catalog/theme.php | 11 ++-- templates/themes/recent/theme.php | 32 ++++++++--- templates/themes/sitemap/theme.php | 4 +- templates/themes/ukko/theme.php | 25 +++++++-- tools/delete-stray-images.php | 4 +- tools/rebuild.php | 8 ++- tools/recount-bumps.php | 13 +++-- tools/stats.php | 4 +- 12 files changed, 145 insertions(+), 68 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index 84235fd19..780f17bbc 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -744,8 +744,10 @@ 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`` WHERE `board` = '%s' AND `id` = " . - (int)$ban['post']['id'], $board['uri'])); + $query = prepare("SELECT `files` FROM ``posts`` WHERE `board` = :board AND `id` = :id"); + $query->bindValue(':board', $board['uri']); + $query->bindValue(':id', (int)$ban['post']['id'], PDO::PARAM_INT); + $query->execute() or error(db_error()); if ($_post = $query->fetch(PDO::FETCH_ASSOC)) { $ban['post'] = array_merge($ban['post'], $_post); } @@ -1333,7 +1335,9 @@ function getPages($mod=false) { $count = $board['thread_count']; } else { // Count threads - $query = query(sprintf("SELECT COUNT(*) FROM ``posts`` WHERE `thread` IS NULL AND `board` = '%s'", $board['uri'])) or error(db_error()); + $query = prepare("SELECT COUNT(*) FROM ``posts`` WHERE `thread` IS NULL AND `board` = :board"); + $query->bindValue(':board', $board['uri']); + $query->execute() or error(db_error()); $count = $query->fetchColumn(); } $count = floor(($config['threads_per_page'] + $count - 1) / $config['threads_per_page']); @@ -1796,8 +1800,10 @@ function markup(&$body, $track_cites = false) { } $search_cites = array_unique($search_cites); - $query = query(sprintf('SELECT `thread`, `id` FROM ``posts`` WHERE `board` = "%s" ' . - implode(' OR ', $search_cites), $board['uri'])) or error(db_error()); + $query = prepare('SELECT `thread`, `id` FROM ``posts`` WHERE `board` = :board ' . + implode(' OR ', $search_cites)); + $query->bindValue(':board', $board['uri']); + $query->execute() or error(db_error()); $cited_posts = array(); while ($cited = $query->fetch(PDO::FETCH_ASSOC)) { @@ -1881,8 +1887,10 @@ function markup(&$body, $track_cites = false) { if (!empty($clauses)) { $cited_posts[$_board] = array(); - $query = query(sprintf('SELECT `thread`, `id` FROM ``posts`` WHERE `board` = "%s"' . - implode(' OR ', $clauses), $board['uri'])) or error(db_error()); + $query = prepare('SELECT `thread`, `id` FROM ``posts`` WHERE `board` = :board ' . + implode(' OR ', $clauses)); + $query->bindValue(':board', $board['uri']); + $query->execute() or error(db_error()); while ($cite = $query->fetch(PDO::FETCH_ASSOC)) { $cited_posts[$_board][$cite['id']] = $config['root'] . $board['dir'] . $config['dir']['res'] . diff --git a/inc/mod/pages.php b/inc/mod/pages.php index 3c4bbc12e..dcfa430b6 100755 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -271,26 +271,35 @@ function mod_search($type, $search_query_escaped, $page_no = 1) { // Compile SQL query if ($type == 'posts') { - $query = ''; + $query = 'SELECT * FROM ``posts`` WHERE '; $boards = listBoards(); if (empty($boards)) error(_('There are no boards to search!')); + $sql_boards = '('; + $allowed_boards = false; + $first = true; foreach ($boards as $board) { openBoard($board['uri']); - if (!hasPermission($config['mod']['search_posts'], $board['uri'])) + if (!hasPermission($config['mod']['search_posts'], $board['uri'])) { continue; - - if (!empty($query)) - $query .= ' UNION ALL '; - $query .= sprintf("SELECT * FROM ``posts`` WHERE `board` = '%s' AND %s", $board['uri'], $sql_like); + } + + if($first) { + $sql_boards .= sprintf(" `board` = '%s' ", $board['uri']); + } else { + $sql_boards .= sprintf(" OR `board` = '%s' ", $board['uri']); + } + $first = false; + $allowed_boards = true; } + $sql_boards .= ") AND "; // You weren't allowed to search any boards - if (empty($query)) + if ($allowed_boards == false) error($config['error']['noaccess']); - $query .= ' ORDER BY `sticky` DESC, `id` DESC'; + $query .= $sql_boards . $sql_like . ' ORDER BY `sticky` DESC, `id` DESC'; } if ($type == 'IP_notes') { @@ -498,7 +507,7 @@ function mod_new_board() { error(sprintf($config['error']['boardexists'], $board['url'])); } - $query = prepare('INSERT INTO ``boards`` VALUES (:uri, :title, :subtitle)'); + $query = prepare('INSERT INTO ``boards`` (`uri`, `title`, `subtitle`) VALUES (:uri, :title, :subtitle)'); $query->bindValue(':uri', $_POST['uri']); $query->bindValue(':title', $_POST['title']); $query->bindValue(':subtitle', $_POST['subtitle']); @@ -1081,8 +1090,10 @@ function mod_ban_appeals() { if ($ban['post'] && isset($ban['post']['board'], $ban['post']['id'])) { if (openBoard($ban['post']['board'])) { - $query = query(sprintf("SELECT `num_files`, `files` FROM ``posts`` WHERE `board` = '%s' AND `id` = " . - (int)$ban['post']['id'], $board['uri'])); + $query = prepare("SELECT `num_files`, `files` FROM ``posts`` WHERE `board` = :board AND `id` = :id"); + $query->bindValue(':board', $board['uri']); + $query->bindValue(':id', (int)$ban['post']['id']); + $query->execute() or error(db_error()); if ($_post = $query->fetch(PDO::FETCH_ASSOC)) { $_post['files'] = $_post['files'] ? json_decode($_post['files']) : array(); $ban['post'] = array_merge($ban['post'], $_post); @@ -1755,12 +1766,25 @@ function mod_deletebyip($boardName, $post, $global = false) { $boards = $global ? listBoards() : array(array('uri' => $boardName)); - $query = ''; - foreach ($boards as $_board) { - $query .= sprintf("SELECT `thread`, `id` FROM ``posts`` WHERE `board` = '%s' AND `ip` = :ip UNION ALL ", $_board['uri']); + $query = 'SELECT `thread`, `id` FROM ``posts`` WHERE '; + + if($global) { + $sql_boards = ''; + } else { + $sql_boards = '('; + $first = true; + foreach ($boards as $_board) { + if($first) { + $sql_boards .= sprintf(" `board` = '%s' ", $_board['uri']); + } else { + $sql_boards .= sprintf(" OR `board` = '%s' ", $_board['uri']); + } + $first = false; + } + $sql_boards .= ") AND "; } - $query = preg_replace('/UNION ALL $/', '', $query); - + + $query .= $sql_boards . " `ip` = :ip"; $query = prepare($query); $query->bindValue(':ip', $ip); $query->execute() or error(db_error($query)); @@ -2251,7 +2275,9 @@ function mod_rebuild() { } if (isset($_POST['rebuild_thread'])) { - $query = query(sprintf("SELECT `id` FROM ``posts`` WHERE `board` = '%s' AND `thread` IS NULL", $board['uri'])) or error(db_error()); + $query = prepare("SELECT `id` FROM ``posts`` WHERE `board` = :board AND `thread` IS NULL"); + $query->bindValue(':board', $board['uri']); + $query->execute() or error(db_error()); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { $log[] = '' . sprintf($config['board_abbreviation'], $board['uri']) . ': Rebuilding thread #' . $post['id']; buildThread($post['id']); @@ -2303,7 +2329,9 @@ function mod_reports($global = false) { foreach ($report_queries as $board => $posts) { $report_posts[$board] = array(); - $query = query(sprintf('SELECT * FROM ``posts`` WHERE `board` = "%s" AND `id` = ' . implode(' OR `id` = ', $posts), $board)) or error(db_error()); + $query = prepare('SELECT * FROM ``posts`` WHERE `board` = :board AND `id` = ' . implode(' OR `id` = ', $posts)); + $query->bindValue(':board', $board); + $query->execute() or error(db_error()); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { $report_posts[$board][$post['id']] = $post; } @@ -2418,17 +2446,24 @@ function mod_recent_posts($lim) { if (in_array($board['uri'], $mod['boards'])) $mod_boards[] = $board; } + $allboards = false; } else { $mod_boards = $boards; + $all_boards = true; } // Manually build an SQL query - $query = 'SELECT * FROM ('; - foreach ($mod_boards as $board) { - $query .= sprintf('SELECT * FROM ``posts`` WHERE `board` = "%s" UNION ALL ', $board['uri']); + $query = 'SELECT * FROM ``posts`` WHERE '; + if(!$all_boards) { + $boards_uris = array(); + foreach($mod_boards as $_board) { + $boards_uris[] = "'" . $_board['uri'] . "'"; + } + $query .= ' (`board` = '; + $query .= implode(' OR `board` = ', $boards_uris); + $query .= ') AND '; } - // Remove the last "UNION ALL" seperator and complete the query - $query = preg_replace('/UNION ALL $/', ') AS `all_posts` WHERE (`time` < :last_time OR NOT :last_time) ORDER BY `time` DESC LIMIT ' . $limit, $query); + $query .= ' (`time` < :last_time OR NOT :last_time) ORDER BY `time` DESC LIMIT ' . $limit; $query = prepare($query); $query->bindValue(':last_time', $last_time); $query->execute() or error(db_error($query)); @@ -2785,12 +2820,7 @@ function mod_debug_recent_posts() { $boards = listBoards(); // Manually build an SQL query - $query = 'SELECT * FROM ('; - foreach ($boards as $board) { - $query .= sprintf('SELECT * FROM ``posts`` WHERE `board` = %s UNION ALL ', $pdo->quote($board['uri'])); - } - // Remove the last "UNION ALL" seperator and complete the query - $query = preg_replace('/UNION ALL $/', ') AS `all_posts` ORDER BY `time` DESC LIMIT ' . $limit, $query); + $query = 'SELECT * FROM ``posts`` ORDER BY `time` DESC LIMIT ' . $limit; $query = query($query) or error(db_error()); $posts = $query->fetchAll(PDO::FETCH_ASSOC); diff --git a/install.php b/install.php index 3c6dd71d2..6e7cf2e0b 100644 --- a/install.php +++ b/install.php @@ -572,7 +572,6 @@ function __query($sql) { `embed` text, `edited_at` DATETIME NULL, PRIMARY KEY (`board`,`id`), - UNIQUE KEY `board_id` (`board`,`id`), KEY `thread_id` (`thread`,`id`), KEY `filehash` (`filehash`(40)), KEY `time` (`time`), diff --git a/install.sql b/install.sql index 184794c06..a13f90ba0 100755 --- a/install.sql +++ b/install.sql @@ -112,7 +112,6 @@ CREATE TABLE IF NOT EXISTS `posts` ( `embed` text, `edited_at` DATETIME NULL, PRIMARY KEY (`board`,`id`), - UNIQUE KEY `board_id` (`board`,`id`), KEY `thread_id` (`thread`,`id`), KEY `filehash` (`filehash`(40)), KEY `time` (`time`), diff --git a/templates/themes/catalog/theme.php b/templates/themes/catalog/theme.php index 3d17f3ec7..8c194f74d 100644 --- a/templates/themes/catalog/theme.php +++ b/templates/themes/catalog/theme.php @@ -39,11 +39,12 @@ public function build($settings, $board_name) { $recent_posts = array(); $stats = array(); - $query = query(sprintf("SELECT *, `id` AS `thread_id`, - (SELECT COUNT(`id`) FROM ``posts`` WHERE `board` = '%s' AND `thread` = `thread_id`) AS `reply_count`, - (SELECT SUM(`num_files`) FROM ``posts`` WHERE `board` = '%s' AND `thread` = `thread_id` AND `num_files` IS NOT NULL) AS `image_count` - FROM ``posts`` WHERE `board` = '%s' AND `thread` IS NULL ORDER BY `bump` DESC", - $board_name, $board_name, $board_name, $board_name, $board_name)) or error(db_error()); + $query = prepare("SELECT *, `id` AS `thread_id`, + (SELECT COUNT(`id`) FROM ``posts`` WHERE `board` = :board AND `thread` = `thread_id`) AS `reply_count`, + (SELECT SUM(`num_files`) FROM ``posts`` WHERE `board` = :board AND `thread` = `thread_id` AND `num_files` IS NOT NULL) AS `image_count` + FROM ``posts`` WHERE `board` = :board AND `thread` IS NULL ORDER BY `bump` DESC"); + $query->bindValue(':board', $board_name); + $query->execute() or error(db_error()); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { $post['link'] = $config['root'] . $board['dir'] . $config['dir']['res'] . sprintf($config['file_page'], ($post['thread'] ? $post['thread'] : $post['id'])); diff --git a/templates/themes/recent/theme.php b/templates/themes/recent/theme.php index d8b7dc1fb..f44d21365 100644 --- a/templates/themes/recent/theme.php +++ b/templates/themes/recent/theme.php @@ -38,15 +38,23 @@ public function homepage($settings) { $boards = listBoards(); - $query = ''; + $no_boards = true; + $first = true; + $query = "SELECT * FROM ``posts`` WHERE ("; foreach ($boards as &$_board) { if (in_array($_board['uri'], $this->excluded)) continue; - $query .= sprintf("SELECT * FROM ``posts`` WHERE `board` = '%s' AND `files` IS NOT NULL UNION ALL ", $_board['uri']); + if($first == false) { + $query .= ' OR '; + } + $first = false; + $query .= sprintf(" `board` = '%s' ", $_board['uri']); + + $no_boards = false; } - $query = preg_replace('/UNION ALL $/', 'ORDER BY `time` DESC LIMIT ' . (int)$settings['limit_images'], $query); - - if ($query == '') { + $query .= ') AND `files` IS NOT NULL ORDER BY `time` DESC LIMIT ' . (int)$settings['limit_images']; + + if ($no_boards) { error(_("Can't build the RecentPosts theme, because there are no boards to be fetched.")); } @@ -79,14 +87,20 @@ public function homepage($settings) { $recent_images[] = $post; } - - $query = ''; + $query = "SELECT * FROM ``posts`` WHERE ("; + $first = true; foreach ($boards as &$_board) { if (in_array($_board['uri'], $this->excluded)) continue; - $query .= sprintf("SELECT * FROM ``posts`` WHERE `board` = '%s' UNION ALL ", $_board['uri']); + + if($first == false) { + $query .= ' OR '; + } + $first = false; + $query .= sprintf(" `board` = '%s' ", $_board['uri']); } - $query = preg_replace('/UNION ALL $/', 'ORDER BY `time` DESC LIMIT ' . (int)$settings['limit_posts'], $query); + $query .= ') ORDER BY `time` DESC LIMIT ' . (int)$settings['limit_posts']; + $query = query($query) or error(db_error()); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { diff --git a/templates/themes/sitemap/theme.php b/templates/themes/sitemap/theme.php index a4f87c59d..db0736bfd 100644 --- a/templates/themes/sitemap/theme.php +++ b/templates/themes/sitemap/theme.php @@ -26,7 +26,9 @@ function sitemap_build($action, $settings, $board) { $threads = array(); foreach ($boards as $board) { - $query = query(sprintf("SELECT `id` AS `thread_id`, (SELECT `time` FROM ``posts`` WHERE `board` = '%s' AND`thread` = `thread_id` OR `id` = `thread_id` ORDER BY `time` DESC LIMIT 1) AS `lastmod` FROM ``posts`` WHERE `board` = '%s' AND`thread` IS NULL", $board, $board)) or error(db_error()); + $query = prepare("SELECT `id` AS `thread_id`, (SELECT `time` FROM ``posts`` WHERE `board` = :board AND`thread` = `thread_id` OR `id` = `thread_id` ORDER BY `time` DESC LIMIT 1) AS `lastmod` FROM ``posts`` WHERE `board` = :board AND`thread` IS NULL"); + $query->bindValue(':board', $board); + $query->execute() or error(db_error()); $threads[$board] = $query->fetchAll(PDO::FETCH_ASSOC); } diff --git a/templates/themes/ukko/theme.php b/templates/themes/ukko/theme.php index 77732f44a..e93d50902 100644 --- a/templates/themes/ukko/theme.php +++ b/templates/themes/ukko/theme.php @@ -27,13 +27,26 @@ public function build($mod = false) { 'title' => sprintf($this->settings['subtitle'], $this->settings['thread_limit']) ); - $query = ''; - foreach($boards as &$_board) { - if(in_array($_board['uri'], explode(' ', $this->settings['exclude']))) - continue; - $query .= sprintf("SELECT * FROM ``posts`` WHERE `board` = '%s' AND `thread` IS NULL UNION ALL ", $_board['uri']); + $query = 'SELECT * FROM ``posts`` WHERE '; + if($this->settings['exclude'] != '') { + $query .= '( '; } - $query = preg_replace('/UNION ALL $/', 'ORDER BY `bump` DESC', $query); + $first = true; + # It uses `board` != '%s' instead of `board` = '%s' because I'm assuming we + # don't want a query with >3000 comparisons + foreach ($boards as &$_board) { + if (in_array($_board['uri'], explode(' ', $this->settings['exclude']))) { + if($first == false) { + $query .= ' OR '; + } + $first = false; + $query .= sprintf(" `board` != '%s' ", $_board['uri']); + } + } + if($this->settings['exclude'] != '') { + $query .= ') AND '; + } + $query .= ' `thread` IS NULL ORDER BY `bump` DESC'; $query = query($query) or error(db_error()); $count = 0; diff --git a/tools/delete-stray-images.php b/tools/delete-stray-images.php index 1c2dbb883..b5b50379f 100755 --- a/tools/delete-stray-images.php +++ b/tools/delete-stray-images.php @@ -18,7 +18,9 @@ openBoard($board['uri']); - $query = query(sprintf("SELECT `file`, `thumb` FROM ``posts`` WHERE `board` = '%s' AND `file` IS NOT NULL", $board['uri'])); + $query = prepare("SELECT `file`, `thumb` FROM ``posts`` WHERE `board` = :board AND `file` IS NOT NULL"); + $query->bindValue(':board', $board['uri']); + $query->execute(); $valid_src = array(); $valid_thumb = array(); diff --git a/tools/rebuild.php b/tools/rebuild.php index d4268ace2..adeb8ed63 100755 --- a/tools/rebuild.php +++ b/tools/rebuild.php @@ -81,7 +81,9 @@ continue; // do no more if($options['full']) { - $query = query(sprintf("SELECT `id` FROM ``posts`` WHERE `board` = '%s'", $board['uri'])) or error(db_error()); + $query = prepare("SELECT `id` FROM ``posts`` WHERE `board` = :board"); + $query->bindValue(':board', $board['uri']); + $query->execute() or error(db_error()); while($post = $query->fetch()) { if(!$options['quiet']) echo "Rebuilding #{$post['id']}...\n"; @@ -89,7 +91,9 @@ } } - $query = query(sprintf("SELECT `id` FROM ``posts`` WHERE `thread` IS NULL AND `board` = '%s'", $board['uri'])) or error(db_error()); + $query = prepare("SELECT `id` FROM ``posts`` WHERE `thread` IS NULL AND `board` = :board"); + $query->bindValue(':board', $board['uri']); + $query->execute() or error(db_error()); while($post = $query->fetch()) { if(!$options['quiet']) echo "Rebuilding #{$post['id']}...\n"; diff --git a/tools/recount-bumps.php b/tools/recount-bumps.php index b90144d9a..c3b8c2501 100644 --- a/tools/recount-bumps.php +++ b/tools/recount-bumps.php @@ -9,12 +9,15 @@ } $board = $argv[1]; -$q = query(sprintf("SELECT `id`, `bump`, `time` FROM ``posts`` - WHERE `board` = '%s' AND `thread` IS NULL", $board)); +$q = prepare("SELECT `id`, `bump`, `time` FROM ``posts`` + WHERE `board` = :board AND `thread` IS NULL"); +$q->bindValue(':board', $board); +$q->execute(); while ($val = $q->fetch()) { - $lc = prepare(sprintf('SELECT MAX(`time`) AS `aq` FROM ``posts`` - WHERE `board` = "%s" AND ((`thread` = :thread and - `email` != "sage" ) OR `id` = :thread', $board)); + $lc = prepare('SELECT MAX(`time`) AS `aq` FROM ``posts`` + WHERE `board` = :board AND ((`thread` = :thread and + `email` != "sage" ) OR `id` = :thread'); + $lc->bindValue(':board', $board); $lc->bindValue(":thread", $val['id']); $lc->execute(); diff --git a/tools/stats.php b/tools/stats.php index 00280f7ba..be339aff3 100755 --- a/tools/stats.php +++ b/tools/stats.php @@ -20,7 +20,9 @@ printf("%10s || ", $f['uri']); foreach ($variants as $iter) { list($term, $time) = $iter; - $qq = query(sprintf("SELECT COUNT(*) as count FROM ``posts`` WHERE `board` = '%s' AND time > %d", $f['uri'], time()-$time)); + $qq = prepare("SELECT COUNT(*) as count FROM ``posts`` WHERE `board` = :board AND time > :time"); + $qq->bindValue(':board', $f['uri']); + $qq->bindValue(':time', time()-$time); $c = $qq->fetch()['count']; printf("%8d | ", $c); From e070b9d2907e88d3e82789609854132a12def668 Mon Sep 17 00:00:00 2001 From: fourfivesix Date: Tue, 14 Oct 2014 21:30:53 -0300 Subject: [PATCH 33/33] Sprintf->prepare in inc/instance-config.php. --- inc/instance-config.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/inc/instance-config.php b/inc/instance-config.php index 7e370c383..6a4ef8293 100644 --- a/inc/instance-config.php +++ b/inc/instance-config.php @@ -353,7 +353,9 @@ function purify($s){ openBoard($b); buildIndex(); buildJavascript(); - $query = query(sprintf("SELECT `id` FROM ``posts`` WHERE `board` = '%s' AND `thread` IS NULL", $b)) or error(db_error()); + $query = prepare("SELECT `id` FROM ``posts`` WHERE `board` = :board AND `thread` IS NULL"); + $query->bindValue(':board', $b); + $query->execute() or error(db_error()); while ($post = $query->fetch(PDO::FETCH_ASSOC)) { buildThread($post['id']); }