From dd0847b751081c2b998c3ffbabc44437d6e25f7a Mon Sep 17 00:00:00 2001
From: PupperWoff
Date: Sat, 24 Jun 2017 20:19:15 +0200
Subject: [PATCH] Added Archive Feature - Added feature allowing Users to vote
for threads they want Featured in the Archive
---
UPDATE_SCRIPT__ARCHIVE_VOTING.php | 63 +++++++++++++++++++++++++++++++
UPDATE_SQL__ARCHIVE_VOTING.sql | 14 +++++++
inc/archive.php | 58 ++++++++++++++++++++++++++--
inc/config.php | 1 +
post.php | 14 +++++++
templates/archive.sql | 1 +
templates/mod/archive_list.html | 25 ++++++++----
7 files changed, 166 insertions(+), 10 deletions(-)
create mode 100644 UPDATE_SCRIPT__ARCHIVE_VOTING.php
create mode 100644 UPDATE_SQL__ARCHIVE_VOTING.sql
diff --git a/UPDATE_SCRIPT__ARCHIVE_VOTING.php b/UPDATE_SCRIPT__ARCHIVE_VOTING.php
new file mode 100644
index 00000000..d78feea7
--- /dev/null
+++ b/UPDATE_SCRIPT__ARCHIVE_VOTING.php
@@ -0,0 +1,63 @@
+You are about to update the database to allow voting on archived threads.
';
+ $page['body'] .= 'Click here to update database entries.
';
+ break;
+ case 2:
+ $page['body'] = 'Database have been updated.
';
+
+ $sql_errors = "";
+ $file_errors = "";
+
+
+ // Update posts_* table to archive function
+ // Get list of boards
+ $boards = listBoards();
+ foreach ($boards as &$_board) {
+ $query = sprintf('ALTER TABLE `archive_%s` ADD `votes` INT UNSIGNED NOT NULL AFTER `mod_archived`', $_board['uri']);
+ query($query) or $sql_errors .= sprintf("Add Archive Voting column to DB for %s
", $_board['uri']) . db_error() . '';
+ }
+
+ if (!empty($sql_errors))
+ $page['body'] .= 'SQL errors
SQL errors were encountered when trying to update the database.
The errors encountered were:
';
+ if (!empty($file_errors))
+ $page['body'] .= 'File System errors
File System errors were encountered when trying to create folders.
The errors encountered were:
';
+
+ break;
+}
+
+
+echo Element('page.html', $page);
+
+?>
+
+
diff --git a/UPDATE_SQL__ARCHIVE_VOTING.sql b/UPDATE_SQL__ARCHIVE_VOTING.sql
new file mode 100644
index 00000000..8155f825
--- /dev/null
+++ b/UPDATE_SQL__ARCHIVE_VOTING.sql
@@ -0,0 +1,14 @@
+
+--
+-- Table structure for table `votes_archive`
+--
+
+CREATE TABLE `votes_archive` (
+ `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
+ `board` varchar(58) CHARACTER SET utf8mb4 NOT NULL,
+ `thread_id` int(10) NOT NULL,
+ `ip` varchar(61) CHARACTER SET ascii NOT NULL,
+ PRIMARY KEY (`id`),
+ UNIQUE KEY `id` (`id`),
+ KEY `ip` (`ip`, `board`, `thread_id`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
diff --git a/inc/archive.php b/inc/archive.php
index b57e38a4..e8f163dc 100644
--- a/inc/archive.php
+++ b/inc/archive.php
@@ -85,7 +85,7 @@ class Archive {
}
// Insert Archive Data in Database
- $query = prepare(sprintf("INSERT INTO ``archive_%s`` VALUES (:thread_id, :snippet, :lifetime, :files, 0, 0)", $board['uri']));
+ $query = prepare(sprintf("INSERT INTO ``archive_%s`` VALUES (:thread_id, :snippet, :lifetime, :files, 0, 0, 0)", $board['uri']));
$query->bindValue(':thread_id', $thread_id, PDO::PARAM_INT);
$query->bindValue(':snippet', $thread_data['snippet'], PDO::PARAM_STR);
$query->bindValue(':lifetime', time(), PDO::PARAM_INT);
@@ -128,6 +128,12 @@ class Archive {
// Delete Thread
@unlink($board['dir'] . $config['dir']['archive'] . $config['dir']['res'] . sprintf($config['file_page'], $thread['id']));
+
+ // Delete Vote Data
+ $del_query = prepare("DELETE FROM ``votes_archive`` WHERE `board` = :board AND `thread_id` = :thread_id");
+ $del_query->bindValue(':board', $board['uri']);
+ $del_query->bindValue(':thread_id', $thread['id'], PDO::PARAM_INT);
+ $del_query->execute() or error(db_error($del_query));
}
// Delete Archive Entries
@@ -187,6 +193,8 @@ class Archive {
// Rebuild Featured Index
self::buildFeaturedIndex();
+ // Rebuild Archive Index
+ self::buildArchiveIndex();
return true;
}
@@ -225,6 +233,8 @@ class Archive {
// Rebuild Featured Index
self::buildFeaturedIndex();
+ // Rebuild Archive Index
+ self::buildArchiveIndex();
}
@@ -267,10 +277,10 @@ class Archive {
'config' => $config,
'mod' => false,
'hide_dashboard_link' => true,
+ 'board' => $board,
'boardlist' => createBoardList(false),
'title' => $title,
'subtitle' => "",
- 'boardlist' => createBoardlist(),
'body' => Element("mod/archive_list.html", array(
'config' => $config,
'thread_count' => count($archive),
@@ -295,7 +305,7 @@ class Archive {
$query = query(sprintf("SELECT `id`, `snippet`, `featured`, `mod_archived` FROM ``archive_%s`` WHERE `mod_archived` = 1 ORDER BY `lifetime` DESC", $board['uri'])) or error(db_error());
$archive = $query->fetchAll(PDO::FETCH_ASSOC);
} else {
- $query = prepare(sprintf("SELECT `id`, `snippet`, `featured`, `mod_archived` FROM ``archive_%s`` WHERE `lifetime` > :lifetime ORDER BY `lifetime` DESC", $board['uri']));
+ $query = prepare(sprintf("SELECT `id`, `snippet`, `featured`, `mod_archived`, `votes` FROM ``archive_%s`` WHERE `lifetime` > :lifetime ORDER BY `lifetime` DESC", $board['uri']));
$query->bindValue(':lifetime', strtotime("-" . $config['archive']['lifetime']), PDO::PARAM_INT);
$query->execute() or error(db_error());
$archive = $query->fetchAll(PDO::FETCH_ASSOC);
@@ -337,6 +347,48 @@ class Archive {
file_write($config['dir']['home'] . $board['dir'] . $config['dir']['featured'] . $config['file_index'], $archive_page);
}
+
+
+
+
+
+ static public function addVote($board, $thread_id) {
+ global $config;
+
+ // Check if thread exist in archive
+ $query = prepare(sprintf("SELECT COUNT(*) FROM ``archive_%s`` WHERE `id` = %d", $board, $thread_id));
+ $query->execute() or error(db_error($query));
+ if ($query->fetchColumn(0) == 0)
+ error($config['error']['nonexistant']);
+
+ // Check if ip has voted
+ $query = prepare("SELECT COUNT(*) FROM ``votes_archive`` WHERE `board` = :board AND `thread_id` = :thread_id AND `ip` = :ip");
+ $query->bindValue(':board', $board, PDO::PARAM_STR);
+ $query->bindValue(':thread_id', $thread_id, PDO::PARAM_INT);
+ $query->bindValue(':ip', ($config['bcrypt_ip_addresses'] ? get_ip_hash($_SERVER['REMOTE_ADDR']) : $_SERVER['REMOTE_ADDR']), PDO::PARAM_STR);
+
+ // Error if already voted
+ $query->execute() or error(db_error($query));
+ if ($query->fetchColumn(0) != 0)
+ error($config['error']['already_voted']);
+
+ // Increase vote count for thread
+ query(sprintf("UPDATE ``archive_%s`` SET `votes` = `votes`+1 WHERE `id` = %d", $board, (int)$thread_id)) or error(db_error());
+
+ // Add ip to voted db
+ $query = prepare("INSERT INTO ``votes_archive`` VALUES (NULL, :board, :thread_id, :ip)");
+ $query->bindValue(':board', $board, PDO::PARAM_STR);
+ $query->bindValue(':thread_id', $thread_id, PDO::PARAM_INT);
+ $query->bindValue(':ip', ($config['bcrypt_ip_addresses'] ? get_ip_hash($_SERVER['REMOTE_ADDR']) : $_SERVER['REMOTE_ADDR']), PDO::PARAM_STR);
+ $query->execute() or error(db_error($query));
+
+ // Rebuild Archive Index
+ self::buildArchiveIndex();
+ }
+
+
+
+
}
?>
\ No newline at end of file
diff --git a/inc/config.php b/inc/config.php
index eb2e2229..47a05ef0 100644
--- a/inc/config.php
+++ b/inc/config.php
@@ -1240,6 +1240,7 @@
$config['error']['mime_exploit'] = _('MIME type detection XSS exploit (IE) detected; post discarded.');
$config['error']['invalid_embed'] = _('Couldn\'t make sense of the URL of the video you tried to embed.');
$config['error']['captcha'] = _('You seem to have mistyped the verification.');
+ $config['error']['already_voted'] = _('You have already voted for this thread to be featured.');
// Moderator errors
diff --git a/post.php b/post.php
index 21807d9b..fc6588f0 100644
--- a/post.php
+++ b/post.php
@@ -1395,6 +1395,20 @@ if (isset($_POST['delete'])) {
$query->execute() or error(db_error($query));
displayBan($ban);
+} elseif (isset($_POST['archive_vote'])) {
+ if (!isset($_POST['board'], $_POST['thread_id']))
+ error($config['error']['bot']);
+
+ // Check if board exists
+ if (!openBoard($_POST['board']))
+ error($config['error']['noboard']);
+
+ // Add Vote
+ Archive::addVote($_POST['board'], $_POST['thread_id']);
+
+ // Return user to archive
+ header('Location: ' . $config['root'] . sprintf($config['board_path'], $_POST['board']) . $config['dir']['archive'], true, $config['redirect_http']);
+
} else {
if (!file_exists($config['has_installed'])) {
header('Location: install.php', true, $config['redirect_http']);
diff --git a/templates/archive.sql b/templates/archive.sql
index 61944c14..427cd03c 100644
--- a/templates/archive.sql
+++ b/templates/archive.sql
@@ -5,6 +5,7 @@ CREATE TABLE IF NOT EXISTS ``archive_{{ board }}`` (
`files` mediumtext NOT NULL,
`featured` int(1) NOT NULL,
`mod_archived` int(1) NOT NULL,
+ `votes` INT UNSIGNED NOT NULL,
UNIQUE KEY `id` (`id`),
KEY `lifetime` (`lifetime`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
diff --git a/templates/mod/archive_list.html b/templates/mod/archive_list.html
index 9126314b..74476a5f 100644
--- a/templates/mod/archive_list.html
+++ b/templates/mod/archive_list.html
@@ -1,6 +1,3 @@
-
-
-
{% if config.mod_archive.threads and mod and mod|hasPermission(config.view_mod_archive) %}
[{% trans %}View Mod Archive{% endtrans %}]
@@ -21,6 +18,7 @@
Post # |
Snippet |
|
+ Votes |
{% if mod and mod|hasPermission(config.mod.feature_archived_threads, board.uri) %}
|
{% endif %}
@@ -35,6 +33,22 @@
{{ thread.id }} |
{{ thread.snippet }} |
[{% trans 'View' %}] |
+ {% if not mod %}
+ {% if not thread.featured %}
+ {{ thread.votes }}
+
+ {% else %}
+ Featured
+ {% endif %}
+ {% else %}
+ {{ thread.votes }}
+ {% endif %}
+ |
{% if mod and mod|hasPermission(config.mod.feature_archived_threads, board.uri) %}
{% if not thread.featured %}
@@ -66,7 +80,4 @@
{% endfor %}
-
-
-
-
+
\ No newline at end of file
|