Browse Source

Added Archive Feature - Added feature allowing Users to vote for threads they want Featured in the Archive

main
PupperWoff 7 years ago
committed by discomrade
parent
commit
dd0847b751
  1. 63
      UPDATE_SCRIPT__ARCHIVE_VOTING.php
  2. 14
      UPDATE_SQL__ARCHIVE_VOTING.sql
  3. 58
      inc/archive.php
  4. 1
      inc/config.php
  5. 14
      post.php
  6. 1
      templates/archive.sql
  7. 25
      templates/mod/archive_list.html

63
UPDATE_SCRIPT__ARCHIVE_VOTING.php

@ -0,0 +1,63 @@
<?php
// require 'inc/config.php';
// require 'inc/config_instance.php';
require 'inc/functions.php';
global $config;
// Check so only ADMIN can run script
check_login(true);
if (!$mod || $mod['type'] != ADMIN)
die("You need to be logged in as admin");
// Set timelimit to what it is for rebuild
@set_time_limit($config['mod']['rebuild_timelimit']);
$page['title'] = 'Updating Database - Voting on Archived Threads';
$step = isset($_GET['step']) ? round($_GET['step']) : 0;
switch($step)
{
default:
case 0:
$page['body'] = '<p style="text-align:center">You are about to update the database to allow voting on archived threads.</p>';
$page['body'] .= '<p style="text-align:center"><a href="?step=2">Click here to update database entries.</a></p>';
break;
case 2:
$page['body'] = '<p style="text-align:center">Database have been updated.</p>';
$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("<li>Add Archive Voting column to DB for %s<br/>", $_board['uri']) . db_error() . '</li>';
}
if (!empty($sql_errors))
$page['body'] .= '<div class="ban"><h2>SQL errors</h2><p>SQL errors were encountered when trying to update the database.</p><p>The errors encountered were:</p><ul>' . $sql_errors . '</ul></div>';
if (!empty($file_errors))
$page['body'] .= '<div class="ban"><h2>File System errors</h2><p>File System errors were encountered when trying to create folders.</p><p>The errors encountered were:</p><ul>' . $file_errors . '</ul></div>';
break;
}
echo Element('page.html', $page);
?>
<!-- There is probably a much better way to do this, but eh. -->
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />

14
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;

58
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();
}
}
?>

1
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

14
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']);

1
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;

25
templates/mod/archive_list.html

@ -1,6 +1,3 @@
{% if config.mod_archive.threads and mod and mod|hasPermission(config.view_mod_archive) %}
<p style="text-align: center">
<a id="unimportant" href="{% if mod %}{{ config.file_mod }}?{% endif %}{{ config.root }}{{ board.dir }}{{ config.dir.mod_archive }}">[{% trans %}View Mod Archive{% endtrans %}]</a>
@ -21,6 +18,7 @@
<th width='80px'>Post #</th>
<th>Snippet</th>
<th width='50px'>&nbsp;</th>
<th width='50px'>Votes</th>
{% if mod and mod|hasPermission(config.mod.feature_archived_threads, board.uri) %}
<th width='80px'>&nbsp;</th>
{% endif %}
@ -35,6 +33,22 @@
<td>{{ thread.id }}</td>
<td>{{ thread.snippet }}</td>
<td><a href="{{ thread.archived_url }}">[{% trans 'View' %}]</a></td>
<td>{% if not mod %}
{% if not thread.featured %}
{{ thread.votes }}
<form action="{{ config.post_url }}" method="post" class="archiveForm" style="float: right">
<input type="hidden" name="board" value="{{ board.uri }}" />
<input type="hidden" name="thread_id" value="{{ thread.id }}">
<input type="hidden" name="archive_vote" value="1">
<a href="#" onclick="confirm('Press OK to vote for this thread to be added to featured archive.')?this.parentNode.submit():false;">[+]</a>
</form>
{% else %}
<strong>Featured</strong>
{% endif %}
{% else %}
{{ thread.votes }}
{% endif %}
</td>
{% if mod and mod|hasPermission(config.mod.feature_archived_threads, board.uri) %}
<td>
{% if not thread.featured %}
@ -66,7 +80,4 @@
</tr>
{% endfor %}
</tbody>
</table>
</table>
Loading…
Cancel
Save