/[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.11 - (show annotations)
Mon May 5 11:46:04 2025 UTC (10 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.10: +5 -5 lines
Content type: text/x-csrc
Refine

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

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