/[LeafOK_CVS]/lbbs/src/article_del.c
ViewVC logotype

Annotation of /lbbs/src/article_del.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations)
Wed Jul 2 04:17:33 2025 UTC (8 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.5: +5 -5 lines
Content type: text/x-csrc
Support UTF8 instead of GBK

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

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1