From 12ee85beafcb545fcb3a71eb1ad9cf90c329df33 Mon Sep 17 00:00:00 2001
From: fallenPineapple
Date: Sat, 10 Aug 2013 17:16:30 -0400
Subject: [PATCH 1/2] Adds "noko50" (View Last 50 Posts) pages. (Broken Reply)
---
inc/config.php | 7 ++-
inc/display.php | 9 +++-
inc/functions.php | 107 +++++++++++++++++++++++++++++++++++++++++++--
inc/mod/pages.php | 10 +++++
mod.php | 2 +
post.php | 14 ++++++
templates/post_thread.html | 11 ++++-
7 files changed, 152 insertions(+), 8 deletions(-)
diff --git a/inc/config.php b/inc/config.php
index ef2ba4ac..ecdfa4c4 100644
--- a/inc/config.php
+++ b/inc/config.php
@@ -589,7 +589,11 @@
// Display image identification links using regex.info/exif, TinEye and Google Images.
$config['image_identification'] = false;
-
+
+ // Number of posts in a "View Last X Posts" page
+ $config['noko50_count'] = 50;
+ // Number of posts a thread needs before it gets a "View Last X Posts" page
+ $config['noko50_min'] = 100;
/*
* ====================
* Board settings
@@ -877,6 +881,7 @@
// Location of files.
$config['file_index'] = 'index.html';
$config['file_page'] = '%d.html';
+ $config['file_page50'] = '%d+50.html';
$config['file_mod'] = 'mod.php';
$config['file_post'] = 'post.php';
$config['file_script'] = 'main.js';
diff --git a/inc/display.php b/inc/display.php
index 97b8c744..4d70e674 100644
--- a/inc/display.php
+++ b/inc/display.php
@@ -416,6 +416,9 @@ class Thread {
public function add(Post $post) {
$this->posts[] = $post;
}
+ public function postCount() {
+ return count($this->posts) + $this->omitted;
+ }
public function postControls() {
global $board, $config;
@@ -483,10 +486,12 @@ class Thread {
return fraction($this->filex, $this->filey, ':');
}
- public function build($index=false) {
+ public function build($index=false, $isnoko50=false) {
global $board, $config, $debug;
- $built = Element('post_thread.html', array('config' => $config, 'board' => $board, 'post' => &$this, 'index' => $index));
+ $hasnoko50 = $this->postCount() >= $config['noko50_min'];
+
+ $built = Element('post_thread.html', array('config' => $config, 'board' => $board, 'post' => &$this, 'index' => $index, 'hasnoko50' => $hasnoko50, 'isnoko50' => $isnoko50));
return $built;
}
diff --git a/inc/functions.php b/inc/functions.php
index 74ab96e9..f96afa1b 100644
--- a/inc/functions.php
+++ b/inc/functions.php
@@ -107,7 +107,10 @@ function loadConfig() {
'|' .
str_replace('%s', $config['board_regex'], preg_quote($config['board_path'], '/')) .
preg_quote($config['dir']['res'], '/') .
- str_replace('%d', '\d+', preg_quote($config['file_page'], '/')) .
+ '(' .
+ str_replace('%d', '\d+', preg_quote($config['file_page'], '/')) . '|' .
+ str_replace('%d', '\d+', preg_quote($config['file_page50'], '/')) .
+ ')' .
'|' .
preg_quote($config['file_mod'], '/') . '\?\/.+' .
')([#?](.+)?)?$/ui';
@@ -950,6 +953,7 @@ 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']));
$antispam_query = prepare('DELETE FROM ``antispam`` WHERE `board` = :board AND `thread` = :thread');
$antispam_query->bindValue(':board', $board['uri']);
@@ -1744,6 +1748,8 @@ function buildThread($id, $return = false, $mod = false) {
// Check if any posts were found
if (!isset($thread))
error($config['error']['nonexistant']);
+
+ $hasnoko50 = $thread->postCount() >= $config['noko50_min'];
$body = Element('thread.html', array(
'board' => $board,
@@ -1752,6 +1758,8 @@ function buildThread($id, $return = false, $mod = false) {
'config' => $config,
'id' => $id,
'mod' => $mod,
+ 'hasnoko50' => $hasnoko50,
+ 'isnoko50' => false,
'antibot' => $mod ? false : create_antibot($board['uri'], $id),
'boardlist' => createBoardlist($mod),
'return' => ($mod ? '?' . $board['url'] . $config['file_index'] : $config['root'] . $board['dir'] . $config['file_index'])
@@ -1760,10 +1768,103 @@ function buildThread($id, $return = false, $mod = false) {
if ($config['try_smarter'] && !$mod)
$build_pages[] = thread_find_page($id);
- if ($return)
+ if ($return) {
return $body;
+ } else {
+ $noko50fn = $board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $id);
+ if ($hasnoko50 || file_exists($noko50fn)) {
+ buildThread50($id, $return, $mod, $thread);
+ }
- 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), $body);
+ }
+}
+
+function buildThread50($id, $return = false, $mod = false, $thread = null) {
+ global $board, $config, $build_pages;
+ $id = round($id);
+
+ if (event('build-thread', $id))
+ return;
+
+ 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->bindValue(':limit', $config['noko50_count']+1, PDO::PARAM_INT);
+ $query->execute() or error(db_error($query));
+
+ $num_images = 0;
+ while ($post = $query->fetch(PDO::FETCH_ASSOC)) {
+ if (!isset($thread)) {
+ $thread = new Thread(
+ $post['id'], $post['subject'], $post['email'], $post['name'], $post['trip'], $post['capcode'], $post['body'], $post['time'],
+ $post['thumb'], $post['thumbwidth'], $post['thumbheight'], $post['file'], $post['filewidth'], $post['fileheight'], $post['filesize'],
+ $post['filename'], $post['ip'], $post['sticky'], $post['locked'], $post['sage'], $post['embed'], $mod ? '?/' : $config['root'], $mod
+ );
+ } else {
+ if ($post['file'])
+ $num_images++;
+
+ $thread->add(new Post(
+ $post['id'], $thread->id, $post['subject'], $post['email'], $post['name'], $post['trip'], $post['capcode'], $post['body'],
+ $post['time'], $post['thumb'], $post['thumbwidth'], $post['thumbheight'], $post['file'], $post['filewidth'], $post['fileheight'],
+ $post['filesize'], $post['filename'], $post['ip'], $post['embed'], $mod ? '?/' : $config['root'], $mod)
+ );
+ }
+ }
+
+ // Check if any posts were found
+ if (!isset($thread))
+ error($config['error']['nonexistant']);
+
+
+ if ($query->rowCount() == $config['noko50_count']+1) {
+ $count = prepare(sprintf("SELECT COUNT(`id`) as `num` FROM ``posts_%s`` WHERE `thread` = :thread UNION ALL SELECT COUNT(`id`) FROM ``posts_%s`` WHERE `file` IS NOT NULL AND `thread` = :thread", $board['uri'], $board['uri']));
+ $count->bindValue(':thread', $id, PDO::PARAM_INT);
+ $count->execute() or error(db_error($count));
+
+ $c = $count->fetch();
+ $thread->omitted = $c['num'] - $config['noko50_count'];
+
+ $c = $count->fetch();
+ $thread->omitted_images = $c['num'] - $num_images;
+ }
+
+ $thread->posts = array_reverse($thread->posts);
+ } else {
+ $allPosts = $thread->posts;
+
+ $thread->posts = array_slice($allPosts, -$config['noko50_count']);
+ $thread->omitted += count($allPosts) - count($thread->posts);
+ foreach ($allPosts as $index => $post) {
+ if ($index == count($allPosts)-count($thread->posts))
+ break;
+ if ($post->file)
+ $thread->omitted_images++;
+ }
+ }
+
+ $hasnoko50 = $thread->postCount() >= $config['noko50_min'];
+
+ $body = Element('thread.html', array(
+ 'board' => $board,
+ 'thread' => $thread,
+ 'body' => $thread->build(false, true),
+ 'config' => $config,
+ 'id' => $id,
+ 'mod' => $mod,
+ 'hasnoko50' => $hasnoko50,
+ 'isnoko50' => true,
+ 'antibot' => $mod ? false : create_antibot($board['uri'], $id),
+ 'boardlist' => createBoardlist($mod),
+ 'return' => ($mod ? '?' . $board['url'] . $config['file_index'] : $config['root'] . $board['dir'] . $config['file_index'])
+ ));
+
+ if ($return) {
+ return $body;
+ } else {
+ file_write($board['dir'] . $config['dir']['res'] . sprintf($config['file_page50'], $id), $body);
+ }
}
function rrmdir($dir) {
diff --git a/inc/mod/pages.php b/inc/mod/pages.php
index 706d747d..192f1537 100644
--- a/inc/mod/pages.php
+++ b/inc/mod/pages.php
@@ -705,6 +705,16 @@ function mod_view_thread($boardName, $thread) {
echo $page;
}
+function mod_view_thread50($boardName, $thread) {
+ global $config, $mod;
+
+ if (!openBoard($boardName))
+ error($config['error']['noboard']);
+
+ $page = buildThread50($thread, true, $mod);
+ echo $page;
+}
+
function mod_ip_remove_note($ip, $id) {
global $config, $mod;
diff --git a/mod.php b/mod.php
index be66db78..923f0709 100644
--- a/mod.php
+++ b/mod.php
@@ -91,6 +91,8 @@ $pages = array(
'/(\%b)/' . preg_quote($config['file_index'], '!') => 'view_board',
'/(\%b)/' . str_replace('%d', '(\d+)', preg_quote($config['file_page'], '!')) => 'view_board',
'/(\%b)/' . preg_quote($config['dir']['res'], '!') .
+ str_replace('%d', '(\d+)', preg_quote($config['file_page50'], '!')) => 'view_thread50',
+ '/(\%b)/' . preg_quote($config['dir']['res'], '!') .
str_replace('%d', '(\d+)', preg_quote($config['file_page'], '!')) => 'view_thread',
);
diff --git a/post.php b/post.php
index ab4775eb..a6997f45 100644
--- a/post.php
+++ b/post.php
@@ -656,6 +656,20 @@ if (isset($_POST['delete'])) {
if ($config['always_noko'] || $noko) {
$redirect = $root . $board['dir'] . $config['dir']['res'] .
sprintf($config['file_page'], $post['op'] ? $id:$post['thread']) . (!$post['op'] ? '#' . $id : '');
+
+ if (!$post['op'] && isset($_SERVER['HTTP_REFERER'])) {
+ $regex = array(
+ 'board' => str_replace('%s', '(\w{1,8})', preg_quote($config['board_path'], '/')),
+ 'page' => str_replace('%d', '(\d+)', preg_quote($config['file_page'], '/')),
+ 'page50' => str_replace('%d', '(\d+)', preg_quote($config['file_page50'], '/')),
+ 'res' => preg_quote($config['dir']['res'], '/'),
+ );
+
+ 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 : '');
+ }
+ }
} else {
$redirect = $root . $board['dir'] . $config['file_index'];
diff --git a/templates/post_thread.html b/templates/post_thread.html
index 21add308..5505d165 100644
--- a/templates/post_thread.html
+++ b/templates/post_thread.html
@@ -121,7 +121,14 @@
{% endif %}
{% endif %}
{% if index %}
- [{% trans %}Reply{% endtrans %}]
+ [{% trans %}View{% endtrans %}]
+ {% endif %}
+ {% if isnoko50 %}
+ [{% trans %}View All{% endtrans %}]
+ {% endif %}
+ {% if hasnoko50 and not isnoko50 %}
+ {% set lastcount = config.noko50_count %}
+ [{% trans %}Last {{ lastcount }} Posts{% endtrans %}]
{% endif %}
{{ post.postControls }}
@@ -146,7 +153,7 @@
{% plural post.omitted_images %}
{{ count }} image replies
{% endtrans %}
- {% endif %} {% trans %}omitted. Click reply to view.{% endtrans %}
+ {% endif %} {% trans %}omitted. Click View to see all.{% endtrans %}
{% endif %}
{% if not index %}
From a4e97c32ec9d413ea2c77fab61a46c5e6c2cb2d7 Mon Sep 17 00:00:00 2001
From: fallenPineapple
Date: Sun, 11 Aug 2013 06:29:30 -0400
Subject: [PATCH 2/2] noko50 reply fix
---
post.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/post.php b/post.php
index a6997f45..77c03439 100644
--- a/post.php
+++ b/post.php
@@ -170,7 +170,7 @@ if (isset($_POST['delete'])) {
error($config['error']['bot']);
// Check the referrer
- if (!isset($_SERVER['HTTP_REFERER']) || !preg_match($config['referer_match'], urldecode($_SERVER['HTTP_REFERER'])))
+ if (!isset($_SERVER['HTTP_REFERER']) || !preg_match($config['referer_match'], rawurldecode($_SERVER['HTTP_REFERER'])))
error($config['error']['referer']);
checkDNSBL();