From ef7556194cfeac6fd52a115a9162e611f322aaa4 Mon Sep 17 00:00:00 2001
From: 8chan <admin@8chan.co>
Date: Sat, 19 Jul 2014 18:41:48 +0000
Subject: [PATCH 1/3] Fix 55ch cancer; can now see next page of posts, ?/recent
 uses templating system

---
 inc/mod/pages.php               | 33 +++++++++++++--------------------
 templates/mod/recent_posts.html | 12 ++++++++++--
 2 files changed, 23 insertions(+), 22 deletions(-)

diff --git a/inc/mod/pages.php b/inc/mod/pages.php
index 801ad9eb..292d36a2 100644
--- a/inc/mod/pages.php
+++ b/inc/mod/pages.php
@@ -2236,6 +2236,7 @@ function mod_recent_posts($lim) {
 		error($config['error']['noaccess']);
 
 	$limit = (is_numeric($lim))? $lim : 25;
+	$last_time = (isset($_GET['last']) && is_numeric($_GET['last'])) ? $_GET['last'] : 0;
 
 	$mod_boards = array();
 	$boards = listBoards();
@@ -2256,37 +2257,29 @@ function mod_recent_posts($lim) {
 		$query .= sprintf('SELECT *, %s AS `board` FROM ``posts_%s`` UNION ALL ', $pdo->quote($board['uri']), $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 = query($query) or error(db_error());
+	$query = preg_replace('/UNION ALL $/', ') AS `all_posts` WHERE (`time` < :last_time OR NOT :last_time) ORDER BY `time` DESC LIMIT ' . $limit, $query);
+	$query = prepare($query);
+	$query->bindValue(':last_time', $last_time);
+	$query->execute() or error(db_error($query));
 	$posts = $query->fetchAll(PDO::FETCH_ASSOC);
 
-	$body = '<h4>Viewing last '.$limit.' posts</h4>
-	<p>View <a href="?/recent/25"> 25 </a>|<a href="?/recent/50"> 50 </a>|<a href="?/recent/100"> 100 </a></p>
-	<a href="javascript:void(0)" id="erase-local-data" style="float:right; clear:both">Erase local data</a></div>';
-	foreach ($posts as $post) {
+	foreach ($posts as &$post) {
 		openBoard($post['board']);
 		if (!$post['thread']) {
 			// Still need to fix this:
 			$po = new Thread($post, '?/', $mod, false);
-			$string = $po->build(true);
-			$string = '<div class="post-wrapper" data-board="'.$post['board'].'"><hr/><a class="eita-link" id="eita-'.$post['board'].'-'.$post['id'].'" href="?/'.$post['board'].'/res/'.$post['id'].'.html#'.$post['id'].'">/'.$post['board'].'/'.$post['id'].'</a><br>' . $string;
+			$post['built'] = $po->build(true);
 		} else {
 			$po = new Post($post, '?/', $mod);
-			$string = $po->build(true);
-			$string = '<div class="post-wrapper" data-board="'.$post['board'].'"><hr/><a class="eita-link" id="eita-'.$post['board'].'-'.$post['id'].'" href="?/'.$post['board'].'/res/'.$post['thread'].'.html#'.$post['id'].'">/'.$post['board'].'/'.$post['id'].'</a><br>' . $string; 
+			$post['built'] = $po->build(true);
 		}
-		$body .= $string . '</div>';
+		$last_time = $post['time'];
 	}
 
-	echo Element('page.html', array(
-		'config' => $config,
-		'mod' => $mod,
-		'hide_dashboard_link' => true,
-		'title' => _('Recent posts'),
-		'subtitle' => '',
-		'nojavascript' => false,
-		'is_recent_posts' => true,
-		'body' => $body
+	echo mod_page(_('Recent posts'), 'mod/recent_posts.html',  array(
+			'posts' => $posts,
+			'limit' => $limit,
+			'last_time' => $last_time
 		)
 	);
 
diff --git a/templates/mod/recent_posts.html b/templates/mod/recent_posts.html
index 4a2f9baa..7e38881b 100644
--- a/templates/mod/recent_posts.html
+++ b/templates/mod/recent_posts.html
@@ -1,6 +1,14 @@
 <script src="{{ config.additional_javascript_url }}js/mod/recent_posts.js"></script>
-{% if posts|count %}
+{% if not posts|count %}
 	<p style="text-align:center" class="unimportant">({% trans 'There are no active posts.' %})</p>
 {% else %}
-
+	<h4>Viewing last {{ limit|e }} posts</h4>
+	<p>View <a href="?/recent/25"> 25 </a>|<a href="?/recent/50"> 50 </a>|<a href="?/recent/100"> 100 </a></p>
+	<a href="javascript:void(0)" id="erase-local-data" style="float:right; clear:both">Erase local data</a></div>
+	{% for post in posts %}
+		<div class="post-wrapper" data-board="{{ post.board }}"><hr/><a class="eita-link" id="eita-{{ post.board }}-{{ post.id or post.thread }}" href="?/{{ post.board }}/res/{{ post.id or post.thread }}.html#{{ post.id or post.thread }}">/{{ post.board }}/{{ post.id }}</a><br>
+			{{ post.built }}
+		</div>
+	{% endfor %}
 {% endif %}
+<a href="/mod.php?/recent/{{ limit }}&amp;last={{ last_time }}">Next {{ limit }} posts</a>

From 18f158e2d57cb91f0675654975536b446afee64b Mon Sep 17 00:00:00 2001
From: Fredrick Brennan <admin@8chan.co>
Date: Sat, 19 Jul 2014 18:47:34 +0000
Subject: [PATCH 2/3] Forgot some |e

---
 templates/mod/recent_posts.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/templates/mod/recent_posts.html b/templates/mod/recent_posts.html
index 7e38881b..7ed80f21 100644
--- a/templates/mod/recent_posts.html
+++ b/templates/mod/recent_posts.html
@@ -11,4 +11,4 @@
 		</div>
 	{% endfor %}
 {% endif %}
-<a href="/mod.php?/recent/{{ limit }}&amp;last={{ last_time }}">Next {{ limit }} posts</a>
+<a href="/mod.php?/recent/{{ limit|e }}&amp;last={{ last_time|e }}">Next {{ limit }} posts</a>

From 48f99deaa56cd7a4af9eeac3b5c981e342816f95 Mon Sep 17 00:00:00 2001
From: 8chan <admin@8chan.co>
Date: Mon, 21 Jul 2014 16:54:51 +0000
Subject: [PATCH 3/3] Fix post linking bug in ?/recent

---
 templates/mod/recent_posts.html | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/templates/mod/recent_posts.html b/templates/mod/recent_posts.html
index 7ed80f21..b9c2c0cc 100644
--- a/templates/mod/recent_posts.html
+++ b/templates/mod/recent_posts.html
@@ -6,7 +6,12 @@
 	<p>View <a href="?/recent/25"> 25 </a>|<a href="?/recent/50"> 50 </a>|<a href="?/recent/100"> 100 </a></p>
 	<a href="javascript:void(0)" id="erase-local-data" style="float:right; clear:both">Erase local data</a></div>
 	{% for post in posts %}
-		<div class="post-wrapper" data-board="{{ post.board }}"><hr/><a class="eita-link" id="eita-{{ post.board }}-{{ post.id or post.thread }}" href="?/{{ post.board }}/res/{{ post.id or post.thread }}.html#{{ post.id or post.thread }}">/{{ post.board }}/{{ post.id }}</a><br>
+		{% if not post.thread %}
+			{% set thread = post.id %}
+		{% else %}
+			{% set thread = post.thread %}
+		{% endif %}
+		<div class="post-wrapper" data-board="{{ post.board }}"><hr/><a class="eita-link" id="eita-{{ post.board }}-{{ thread }}" href="?/{{ post.board }}/res/{{ thread }}.html#{{ post.id }}">/{{ post.board }}/{{ post.id }}</a><br>
 			{{ post.built }}
 		</div>
 	{% endfor %}