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

Contents of /lbbs/src/user_priv.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (show annotations)
Tue May 6 05:31:26 2025 UTC (10 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.11: +3 -4 lines
Content type: text/x-csrc
Update copyright and license information

1 /***************************************************************************
2 user_priv.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 "user_priv.h"
18 #include "bbs.h"
19 #include "common.h"
20 #include "database.h"
21 #include "log.h"
22 #include <stdio.h>
23 #include <mysql.h>
24
25 BBS_user_priv BBS_priv;
26
27 int checklevel(BBS_user_priv *p_priv, int level)
28 {
29 if (level == P_GUEST)
30 {
31 return 1;
32 }
33
34 return ((p_priv->level & level) ? 1 : 0);
35 }
36
37 int setpriv(BBS_user_priv *p_priv, int sid, int priv)
38 {
39 int i;
40 if (sid > 0)
41 {
42 for (i = 0; i < p_priv->s_count; i++)
43 {
44 if (p_priv->s_priv_list[i].sid == sid)
45 {
46 p_priv->s_priv_list[i].s_priv = priv;
47 return 0;
48 }
49 }
50 if (i < BBS_max_section)
51 {
52 p_priv->s_priv_list[i].s_priv = priv;
53 }
54 else
55 {
56 return -1;
57 }
58 }
59 else
60 {
61 p_priv->g_priv = priv;
62 }
63
64 return 0;
65 }
66
67 int getpriv(BBS_user_priv *p_priv, int sid)
68 {
69 int i;
70 for (i = 0; i < p_priv->s_count; i++)
71 {
72 if (p_priv->s_priv_list[i].sid == sid)
73 return p_priv->s_priv_list[i].s_priv;
74 }
75
76 return (sid >= 0 ? p_priv->g_priv : S_NONE);
77 }
78
79 int checkpriv(BBS_user_priv *p_priv, int sid, int priv)
80 {
81 return (((getpriv(p_priv, sid) & priv)) == priv ? 1 : 0);
82 }
83
84 int load_priv(MYSQL *db, BBS_user_priv *p_priv, long int uid)
85 {
86 MYSQL_RES *rs;
87 MYSQL_ROW row;
88 char sql[SQL_BUFFER_LEN];
89
90 p_priv->uid = uid;
91 p_priv->level = (uid == 0 ? P_GUEST : P_USER);
92 p_priv->g_priv = S_DEFAULT;
93
94 if (db == NULL)
95 return 1;
96
97 // Permission
98 snprintf(sql, sizeof(sql), "SELECT p_post, p_msg FROM user_list WHERE UID = %ld AND verified",
99 uid);
100 if (mysql_query(db, sql) != 0)
101 {
102 log_error("Query user_list failed\n");
103 return -1;
104 }
105 if ((rs = mysql_store_result(db)) == NULL)
106 {
107 log_error("Get user_list data failed\n");
108 return -1;
109 }
110 if ((row = mysql_fetch_row(rs)))
111 {
112 p_priv->g_priv |= (atoi(row[0]) ? S_POST : 0);
113 p_priv->g_priv |= (atoi(row[1]) ? S_MSG : 0);
114 }
115 mysql_free_result(rs);
116
117 // Admin
118 snprintf(sql, sizeof(sql), "SELECT major FROM admin_config WHERE UID = %ld "
119 "AND enable AND (NOW() BETWEEN begin_dt AND end_dt)",
120 uid);
121 if (mysql_query(db, sql) != 0)
122 {
123 log_error("Query admin_config failed\n");
124 return -1;
125 }
126 if ((rs = mysql_store_result(db)) == NULL)
127 {
128 log_error("Get admin_config data failed\n");
129 return -1;
130 }
131 if ((row = mysql_fetch_row(rs)))
132 {
133 p_priv->level |= (atoi(row[0]) ? P_ADMIN_M : P_ADMIN_S);
134 p_priv->g_priv |= (atoi(row[0]) ? S_ALL : S_ADMIN);
135 }
136 mysql_free_result(rs);
137
138 // Section Master
139 snprintf(sql, sizeof(sql), "SELECT section_master.SID, major FROM section_master "
140 "INNER JOIN section_config ON section_master.SID = section_config.SID "
141 "WHERE UID = %ld AND section_master.enable AND section_config.enable "
142 "AND (NOW() BETWEEN begin_dt AND end_dt)",
143 uid);
144 if (mysql_query(db, sql) != 0)
145 {
146 log_error("Query section_master failed\n");
147 return -1;
148 }
149 if ((rs = mysql_store_result(db)) == NULL)
150 {
151 log_error("Get section_master data failed\n");
152 return -1;
153 }
154 while ((row = mysql_fetch_row(rs)))
155 {
156 p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);
157 setpriv(p_priv, atoi(row[0]), getpriv(p_priv, atoi(row[0])) | (atoi(row[1]) ? S_MAN_M : S_MAN_S));
158 }
159 mysql_free_result(rs);
160
161 // Section status
162 snprintf(sql, sizeof(sql), "SELECT SID, exp_get, read_user_level, write_user_level FROM section_config "
163 "INNER JOIN section_class ON section_config.CID = section_class.CID "
164 "WHERE section_config.enable AND section_class.enable "
165 "ORDER BY SID");
166 if (mysql_query(db, sql) != 0)
167 {
168 log_error("Query section_config failed\n");
169 return -1;
170 }
171 if ((rs = mysql_store_result(db)) == NULL)
172 {
173 log_error("Get section_config data failed\n");
174 return -1;
175 }
176 while ((row = mysql_fetch_row(rs)))
177 {
178 int priv = getpriv(p_priv, atoi(row[0]));
179 if (p_priv->level < atoi(row[2]))
180 {
181 priv &= (~S_LIST);
182 }
183 if (p_priv->level < atoi(row[3]))
184 {
185 priv &= (~S_POST);
186 }
187 if (!atoi(row[1]))
188 {
189 priv &= (~S_GETEXP);
190 }
191 setpriv(p_priv, atoi(row[0]), priv);
192 }
193 mysql_free_result(rs);
194
195 // Section ban
196 snprintf(sql, sizeof(sql), "SELECT SID FROM ban_user_list WHERE UID = %ld AND enable "
197 "AND (NOW() BETWEEN ban_dt AND unban_dt)",
198 uid);
199 if (mysql_query(db, sql) != 0)
200 {
201 log_error("Query ban_user_list failed\n");
202 return -1;
203 }
204 if ((rs = mysql_store_result(db)) == NULL)
205 {
206 log_error("Get ban_user_list data failed\n");
207 return -1;
208 }
209 while ((row = mysql_fetch_row(rs)))
210 {
211 setpriv(p_priv, atoi(row[0]),
212 getpriv(p_priv, atoi(row[0])) & (~S_POST));
213 }
214 mysql_free_result(rs);
215
216 return 0;
217 }

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