File/embed duplication

This commit is contained in:
Juribiyan 2018-08-24 18:25:08 +05:00
parent d3f626b95f
commit 918f9bc3ac
8 changed files with 102 additions and 33 deletions

View File

@ -147,6 +147,7 @@ CREATE TABLE `PREFIX_boards` (
`balls` tinyint(1) NOT NULL DEFAULT '0',
`dice` tinyint(1) NOT NULL DEFAULT '0',
`useragent` tinyint(1) NOT NULL DEFAULT '0',
`duplication` tinyint(1) NOT NULL DEFAULT '0'
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=KU_DBCHARSET COLLATE=KU_COLLATION;

View File

@ -0,0 +1 @@
ALTER TABLE `boards` ADD `duplication` BOOLEAN NULL DEFAULT FALSE;

View File

@ -212,6 +212,9 @@
{t}A file or embed ID is required for a new thread.{/t}
{/if}
</li>
{if $board.duplication}
<li>{t}File end embed duplication is allowed{/t}.</li>
{/if}
</ul>
<script>
if (getCookie('ku_showblotter') != '1') {

View File

@ -811,6 +811,16 @@ class Board {
}
function EraseFileAndThumbs($file) {
global $tc_db;
$dups_exist = $tc_db->GetOne("SELECT COUNT(*) FROM `".KU_DBPREFIX."postembeds`
WHERE `file_md5`= ?
AND `boardid` = ?
AND `IS_DELETED` = 0
AND `file` != 'removed'", array($file['file_md5'], $this->board['id']));
if ($dups_exist)
return;
$files = GetFileAndThumbs($file);
$boardname = $this->board['name'];
foreach($files as $f) {
@ -835,8 +845,8 @@ class Board {
return array('error' => _gettext('Incorrect password.'));
}
clearPostCache($postfile['id'], $this->board['name']);
$this->EraseFileAndThumbs($postfile);
$tc_db->Execute("UPDATE `".KU_DBPREFIX."files` SET `file`='removed' WHERE `file_id`=".$tc_db->qstr($file_id));
$this->EraseFileAndThumbs($postfile);
if ($ismod) {
$parentid = $postfile['parentid']=='0' ? $postfile['id'] : $postfile['parentid'];
management_addlogentry(_gettext('Deleted file') .
@ -929,12 +939,9 @@ class Post extends Board {
@unlink(KU_BOARDSDIR.$boardname.'/res/'.$postid.'-100.html');
@unlink(KU_BOARDSDIR.$boardname.'/res/'.$postid.'+50.html');
// Physically delete all files
// Collect ID's
$file_ids = array(); $post_ids = array();
foreach($files as $file) {
if ($file['file'] != 'removed' && $file['file_size'] > 0)
$this->EraseFileAndThumbs($file);
// Do some extra stuff along the way
$post_id = "'".$file['id']."'";
if (!in_array($post_id, $post_ids)) {
$post_ids []= $post_id;
@ -960,6 +967,11 @@ class Post extends Board {
`boardid` = '" . $boardid . "'
AND
`id` IN (".implode($post_ids, ',').")");
// Physically delete all files
foreach($files as $file) {
if ($file['file'] != 'removed' && $file['file_size'] > 0)
$this->EraseFileAndThumbs($file);
}
// Clear reports
$tc_db->Execute("DELETE FROM `".KU_DBPREFIX."reports`
WHERE
@ -970,20 +982,10 @@ class Post extends Board {
return (count($post_ids)+1).' '; // huh?
}
else {
if ($this->post['embeds']) {
// Physically delete all files
foreach($this->post['embeds'] as $embed) {
$this->EraseFileAndThumbs($embed);
$file_ids []= "'".$embed['file_id']."'";
}
// Mark files as removed in db
$tc_db->Execute("UPDATE `".KU_DBPREFIX."files`
SET
`file`='removed'
WHERE
`boardid` = '" . $boardid . "'
AND
`file_id` IN (".implode($file_ids, ',').")");
// Collect ID's
$file_ids = array();
foreach($this->post['embeds'] as $embed) {
$file_ids []= "'".$embed['file_id']."'";
}
// Mark post as deleted
$tc_db->Execute("UPDATE `".KU_DBPREFIX."posts`
@ -994,6 +996,20 @@ class Post extends Board {
`boardid` = '" . $boardid . "'
AND
`id` = ".$tc_db->qstr($postid));
if ($this->post['embeds']) {
// Mark files as removed in db
$tc_db->Execute("UPDATE `".KU_DBPREFIX."files`
SET
`file`='removed'
WHERE
`boardid` = '" . $boardid . "'
AND
`file_id` IN (".implode($file_ids, ',').")");
// Physically delete all files
foreach($this->post['embeds'] as $embed) {
$this->EraseFileAndThumbs($embed);
}
}
// Un-bump threda
$tc_db->Execute('UPDATE
`'.KU_DBPREFIX.'posts` AS t1,
@ -1124,7 +1140,7 @@ class Post extends Board {
//file_size
($is_embed ? null : $attachment['file_size']),
//file_size_formatted
($is_embed ? $attachment['file_size_formatted'] : ConvertBytes($attachment['size'])),
(($is_embed || $attachment['is_duplicate']) ? $attachment['file_size_formatted'] : ConvertBytes($attachment['size'])),
//thumb_w
intval($attachment['imgWidth_thumb']),
//thumb_h

View File

@ -3051,7 +3051,8 @@ class Manage {
'locked',
'balls',
'dice',
'useragent'
'useragent',
'duplication'
) as $prop) {
$set []= "`$prop` = ".$tc_db->qstr(isset($_POST[$prop]) ? '1' : '0');
}
@ -3312,6 +3313,15 @@ class Manage {
$tpl_page .= ' />'. "\n" .
'<div class="desc">'. _gettext('Add ##Useragent## feature') .'</div><br />';
/* Duplication */
$tpl_page .= '<label for="duplication">'. _gettext('Duplication') .':</label>
<input type="checkbox" name="duplication"';
if ($lineboard['duplication'] == '1') {
$tpl_page .= ' checked';
}
$tpl_page .= ' />'. "\n" .
'<div class="desc">'. _gettext('Enable file and embed duplication') .'</div><br />';
/* Enable captcha */
$tpl_page .= '<label for="enablecaptcha">'. _gettext('Enable captcha') .':</label>
<input type="checkbox" name="enablecaptcha"';
@ -3476,7 +3486,8 @@ class Manage {
'locked',
'balls',
'dice',
'useragent'
'useragent',
'duplication'
) as $prop) {
$set []= "`$prop` = ".$tc_db->qstr(isset($_POST[$prop]) ? '1' : '0');
}
@ -3642,6 +3653,15 @@ class Manage {
$tpl_page .= ' />'. "\n" .
'<div class="desc">'. _gettext('Add ##Useragent## feature') .'</div><br />';
/* Duplication */
$tpl_page .= '<label for="duplication">'. _gettext('Duplication') .':</label>
<input type="checkbox" name="duplication"';
if ($lineboard['duplication'] == '1') {
$tpl_page .= ' checked';
}
$tpl_page .= ' />'. "\n" .
'<div class="desc">'. _gettext('Enable file and embed duplication') .'</div><br />';
/* Enable "no file" posting */
$tpl_page .= '<label for="enablenofile">'. _gettext('Enable \'no file\' posting') .':</label>
<input type="checkbox" name="enablenofile"';

View File

@ -115,7 +115,7 @@ class Upload {
// 2) Collect embeds
$embed_hashes = array();
if (is_array($_POST['embed']) || is_object($_POST['embed'])) {
foreach($_POST['embed'] as $i => $code) {
foreach($_POST['embed'] as $i => $code) {
if ($code != '') {
if (array_key_exists($_POST['embedtype'][$i], $board_class->board['embeds_allowed_assoc'])) {
$embed_filetype = $_POST['embedtype'][$i];
@ -137,10 +137,10 @@ class Upload {
}
else $this->exitWithUploadErrorPage(_gettext('Sorry, that filetype is not allowed on this board.'),
$atype, $i, $_POST['embedtype'][$i] . '/' . $code);
}
}
}
}
}
/*else { // Fancy embeds (not yet implemented) }*/
@ -174,11 +174,26 @@ class Upload {
? $attachment['file_original'].$attachment['file_type']
: $attachment['embedtype'] . '/' . $attachment['embed'];
// Check if attachment already posted somewhere else
$exists_thread = checkMd5($attachment['file_md5'], $board_class->board['name'], $board_class->board['id']);
if (is_array($exists_thread)) {
$exists_url = KU_BOARDSPATH . '/' . $board_class->board['name'] . '/res/' . $exists_thread[0] . '.html#' . $exists_thread[1];
$this->exitWithUploadErrorPage(_gettext('Duplicate file entry detected.') .
sprintf(_gettext('Already posted %shere%s.'),'<a target="_blank" href="' . $exists_url . '">','</a>'), $atype, $i, $filename);
$existing = checkMd5($attachment['file_md5'], $board_class->board['name'], $board_class->board['id']);
if ($existing) {
if ($board_class->board['duplication']) { // If file duplication is allowed on this board just copy all the properties from the prototype
$attachment[$attachment['attachmenttype']=='file' ? 'file_name' : 'embed'] = $existing['file'];
$attachment['imgWidth'] = $existing['image_w'];
$attachment['imgHeight'] = $existing['image_h'];
$attachment['file_size'] = $existing['file_size'];
$attachment['file_size_formatted'] = $existing['file_size_formatted'];
$attachment['imgWidth_thumb'] = $existing['thumb_w'];
$attachment['imgHeight_thumb'] = $existing['thumb_h'];
$attachment['is_duplicate'] = true;
if ($attachment['attachmenttype']=='embed')
$attachment['file_original'] = $existing['file_original'];
break;
}
else {
$exists_url = KU_BOARDSPATH . '/' . $board_class->board['name'] . '/res/' . $existing['parentid'] . '.html#' . $existing['id'];
$this->exitWithUploadErrorPage(_gettext('Duplicate file entry detected.') .
sprintf(_gettext('Already posted %shere%s.'),'<a target="_blank" href="' . $exists_url . '">','</a>'), $atype, $i, $filename);
}
}
// Handle File
if ($attachment['attachmenttype'] == 'file') {

View File

@ -5,14 +5,18 @@
*/
function checkMd5($md5, $board, $boardid) {
global $tc_db;
$matches = $tc_db->GetAll("SELECT `id`, `parentid`
$matches = $tc_db->GetAll("SELECT `id`, `parentid`, `file`, `file_size`, `file_size_formatted`, `image_w`, `image_h`, `thumb_w`, `thumb_h`, `file_original`
FROM `".KU_DBPREFIX."postembeds`
WHERE `boardid` = " . $boardid . "
AND `IS_DELETED` = 0
AND `IS_DELETED` = 0
AND `file` != 'removed'
AND `file_md5` = ".$tc_db->qstr($md5)." LIMIT 1");
if (count($matches) > 0) {
$r = $matches[0];
if ($r['parentid'] == 0)
$r['parentid'] = $r['id'];
$real_parentid = ($matches[0]['parentid'] == 0) ? $matches[0]['id'] : $matches[0]['parentid'];
return array($real_parentid, $matches[0]['id']);
return $r;
}
return false;

View File

@ -2102,4 +2102,13 @@ msgid "Add embed"
msgstr "Добавить вложение"
msgid "Hide filename"
msgstr "Скрыть имя файла"
msgstr "Скрыть имя файла"
msgid "Duplication"
msgstr "Дубликация файлов"
msgid "Enable file and embed duplication"
msgstr "Разрешить многократное переиспользование файлов и вложений"
msgid "File end embed duplication is allowed"
msgstr "Разрешено переиспользование файлов и вложений"