| 1 |
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
| 2 |
/*
|
| 3 |
* article_del
|
| 4 |
* - user interactive feature to delete article
|
| 5 |
*
|
| 6 |
* Copyright (C) 2004-2026 Leaflet <leaflet@leafok.com>
|
| 7 |
*/
|
| 8 |
|
| 9 |
#ifdef HAVE_CONFIG_H
|
| 10 |
#include "config.h"
|
| 11 |
#endif
|
| 12 |
|
| 13 |
#include "article_del.h"
|
| 14 |
#include "database.h"
|
| 15 |
#include "io.h"
|
| 16 |
#include "log.h"
|
| 17 |
#include "screen.h"
|
| 18 |
#include "user_priv.h"
|
| 19 |
#include <ctype.h>
|
| 20 |
#include <stdlib.h>
|
| 21 |
|
| 22 |
int article_del(const SECTION_LIST *p_section, const ARTICLE *p_article)
|
| 23 |
{
|
| 24 |
MYSQL *db = NULL;
|
| 25 |
MYSQL_RES *rs = NULL;
|
| 26 |
MYSQL_ROW row;
|
| 27 |
char sql[SQL_BUFFER_LEN];
|
| 28 |
int ret = 0;
|
| 29 |
int ch;
|
| 30 |
int article_visible = 0;
|
| 31 |
int article_excerption = 0;
|
| 32 |
int exp_change = 0;
|
| 33 |
|
| 34 |
if (p_section == NULL || p_article == NULL)
|
| 35 |
{
|
| 36 |
log_error("NULL pointer error");
|
| 37 |
}
|
| 38 |
|
| 39 |
if (p_article->excerption) // Delete is not allowed
|
| 40 |
{
|
| 41 |
clearscr();
|
| 42 |
moveto(1, 1);
|
| 43 |
prints("该文章无法被删除,请联系版主。");
|
| 44 |
press_any_key();
|
| 45 |
|
| 46 |
return 0;
|
| 47 |
}
|
| 48 |
|
| 49 |
clearscr();
|
| 50 |
moveto(1, 1);
|
| 51 |
prints("真的要删除文章?(Y)是, (N)否 [N]: ");
|
| 52 |
iflush();
|
| 53 |
|
| 54 |
for (ch = 0; !SYS_server_exit; ch = igetch_t(BBS_max_user_idle_time))
|
| 55 |
{
|
| 56 |
switch (toupper(ch))
|
| 57 |
{
|
| 58 |
case KEY_NULL:
|
| 59 |
case KEY_TIMEOUT:
|
| 60 |
goto cleanup;
|
| 61 |
case CR:
|
| 62 |
case KEY_ESC:
|
| 63 |
case 'N':
|
| 64 |
return 0;
|
| 65 |
case 'Y':
|
| 66 |
break;
|
| 67 |
default: // Invalid selection
|
| 68 |
continue;
|
| 69 |
}
|
| 70 |
|
| 71 |
break;
|
| 72 |
}
|
| 73 |
|
| 74 |
if (SYS_server_exit) // Do not save data on shutdown
|
| 75 |
{
|
| 76 |
goto cleanup;
|
| 77 |
}
|
| 78 |
|
| 79 |
db = db_open();
|
| 80 |
if (db == NULL)
|
| 81 |
{
|
| 82 |
log_error("db_open() error: %s", mysql_error(db));
|
| 83 |
ret = -1;
|
| 84 |
goto cleanup;
|
| 85 |
}
|
| 86 |
|
| 87 |
// Begin transaction
|
| 88 |
if (mysql_query(db, "SET autocommit=0") != 0)
|
| 89 |
{
|
| 90 |
log_error("SET autocommit=0 error: %s", mysql_error(db));
|
| 91 |
ret = -1;
|
| 92 |
goto cleanup;
|
| 93 |
}
|
| 94 |
|
| 95 |
if (mysql_query(db, "BEGIN") != 0)
|
| 96 |
{
|
| 97 |
log_error("Begin transaction error: %s", mysql_error(db));
|
| 98 |
ret = -1;
|
| 99 |
goto cleanup;
|
| 100 |
}
|
| 101 |
|
| 102 |
snprintf(sql, sizeof(sql),
|
| 103 |
"SELECT visible, excerption FROM bbs WHERE AID = %d FOR UPDATE",
|
| 104 |
p_article->aid);
|
| 105 |
|
| 106 |
if (mysql_query(db, sql) != 0)
|
| 107 |
{
|
| 108 |
log_error("Query article status error: %s", mysql_error(db));
|
| 109 |
ret = -1;
|
| 110 |
goto cleanup;
|
| 111 |
}
|
| 112 |
if ((rs = mysql_use_result(db)) == NULL)
|
| 113 |
{
|
| 114 |
log_error("Get article status data failed");
|
| 115 |
ret = -1;
|
| 116 |
goto cleanup;
|
| 117 |
}
|
| 118 |
|
| 119 |
if ((row = mysql_fetch_row(rs)))
|
| 120 |
{
|
| 121 |
article_visible = atoi(row[0]);
|
| 122 |
article_excerption = atoi(row[1]);
|
| 123 |
}
|
| 124 |
mysql_free_result(rs);
|
| 125 |
rs = NULL;
|
| 126 |
|
| 127 |
if (!article_visible) // Already deleted
|
| 128 |
{
|
| 129 |
mysql_close(db);
|
| 130 |
db = NULL;
|
| 131 |
|
| 132 |
clearscr();
|
| 133 |
moveto(1, 1);
|
| 134 |
prints("该文章已被删除,请稍后刷新列表。");
|
| 135 |
press_any_key();
|
| 136 |
|
| 137 |
goto cleanup;
|
| 138 |
}
|
| 139 |
|
| 140 |
if (article_excerption) // Delete is not allowed
|
| 141 |
{
|
| 142 |
clearscr();
|
| 143 |
moveto(1, 1);
|
| 144 |
prints("该文章无法被删除,请联系版主。");
|
| 145 |
press_any_key();
|
| 146 |
|
| 147 |
goto cleanup;
|
| 148 |
}
|
| 149 |
|
| 150 |
// Update article(s)
|
| 151 |
snprintf(sql, sizeof(sql),
|
| 152 |
"UPDATE bbs SET visible = 0, reply_count = 0, m_del = %d "
|
| 153 |
"WHERE (AID = %d OR TID = %d) AND visible",
|
| 154 |
(p_article->uid == BBS_priv.uid ? 0 : 1),
|
| 155 |
p_article->aid, p_article->aid);
|
| 156 |
|
| 157 |
if (mysql_query(db, sql) != 0)
|
| 158 |
{
|
| 159 |
log_error("Update article status error: %s", mysql_error(db));
|
| 160 |
ret = -1;
|
| 161 |
goto cleanup;
|
| 162 |
}
|
| 163 |
|
| 164 |
// Update exp
|
| 165 |
if (p_article->uid == BBS_priv.uid)
|
| 166 |
{
|
| 167 |
exp_change = (p_article->tid == 0 ? -20 : -5);
|
| 168 |
}
|
| 169 |
else
|
| 170 |
{
|
| 171 |
exp_change = (p_article->tid == 0 ? -50 : -15);
|
| 172 |
}
|
| 173 |
|
| 174 |
snprintf(sql, sizeof(sql),
|
| 175 |
"UPDATE user_pubinfo SET exp = exp + %d WHERE UID = %d",
|
| 176 |
exp_change, BBS_priv.uid);
|
| 177 |
|
| 178 |
if (mysql_query(db, sql) != 0)
|
| 179 |
{
|
| 180 |
log_error("Update exp error: %s", mysql_error(db));
|
| 181 |
ret = -1;
|
| 182 |
goto cleanup;
|
| 183 |
}
|
| 184 |
|
| 185 |
// Add log
|
| 186 |
snprintf(sql, sizeof(sql),
|
| 187 |
"INSERT INTO bbs_article_op(AID, UID, type, op_dt, op_ip)"
|
| 188 |
"VALUES(%d, %d, '%c', NOW(), '%s')",
|
| 189 |
p_article->aid, BBS_priv.uid,
|
| 190 |
(p_article->uid == BBS_priv.uid ? 'D' : 'X'),
|
| 191 |
hostaddr_client);
|
| 192 |
|
| 193 |
if (mysql_query(db, sql) != 0)
|
| 194 |
{
|
| 195 |
log_error("Add log error: %s", mysql_error(db));
|
| 196 |
ret = -1;
|
| 197 |
goto cleanup;
|
| 198 |
}
|
| 199 |
|
| 200 |
// Set reply count
|
| 201 |
if (p_article->tid != 0)
|
| 202 |
{
|
| 203 |
snprintf(sql, sizeof(sql),
|
| 204 |
"UPDATE bbs SET reply_count = reply_count - 1 WHERE AID = %d",
|
| 205 |
p_article->tid);
|
| 206 |
|
| 207 |
if (mysql_query(db, sql) != 0)
|
| 208 |
{
|
| 209 |
log_error("Update article error: %s", mysql_error(db));
|
| 210 |
ret = -1;
|
| 211 |
goto cleanup;
|
| 212 |
}
|
| 213 |
}
|
| 214 |
|
| 215 |
// Commit transaction
|
| 216 |
if (mysql_query(db, "COMMIT") != 0)
|
| 217 |
{
|
| 218 |
log_error("Commit transaction error: %s", mysql_error(db));
|
| 219 |
ret = -1;
|
| 220 |
goto cleanup;
|
| 221 |
}
|
| 222 |
|
| 223 |
mysql_close(db);
|
| 224 |
db = NULL;
|
| 225 |
|
| 226 |
clearscr();
|
| 227 |
moveto(1, 1);
|
| 228 |
prints("删除成功,请在%d秒后刷新列表。", BBS_section_list_load_interval);
|
| 229 |
press_any_key();
|
| 230 |
ret = 1; // Success
|
| 231 |
|
| 232 |
cleanup:
|
| 233 |
mysql_free_result(rs);
|
| 234 |
mysql_close(db);
|
| 235 |
|
| 236 |
return ret;
|
| 237 |
}
|