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

Diff of /lbbs/src/user_priv.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.1 by sysadm, Fri Oct 22 15:21:28 2004 UTC Revision 1.17 by sysadm, Fri May 30 02:56:02 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                            user_priv.c  -  description                                                    user_priv.c  -  description
3                               -------------------                                                           -------------------
4      begin                : Mon Oct 22 2004          Copyright            : (C) 2004-2025 by Leaflet
5      copyright            : (C) 2004 by Leaflet          Email                : leaflet@leafok.com
     email                : leaflet@leafok.com  
6   ***************************************************************************/   ***************************************************************************/
7    
8  /***************************************************************************  /***************************************************************************
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   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  *   *   it under the terms of the GNU General Public License as published by  *
12   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 3 of the License, or     *
13   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #include "user_priv.h"
18  #include "bbs.h"  #include "bbs.h"
19    #include "common.h"
20    #include "database.h"
21    #include "log.h"
22    #include <stdio.h>
23  #include <mysql.h>  #include <mysql.h>
24    #include <stdlib.h>
25    
26  int  BBS_user_priv BBS_priv;
27  load_priv (MYSQL * db, BBS_user_priv * p_priv, int uid,  
28    int auth_uid, int priv_excluse)  int setpriv(BBS_user_priv *p_priv, int sid, int priv, int is_favor)
29    {
30            int left = 0;
31            int right = p_priv->s_count - 1;
32            int mid = 0;
33    
34            if (sid == 0)
35            {
36                    p_priv->g_priv = priv;
37                    return 0;
38            }
39    
40            while (left < right)
41            {
42                    mid = (left + right) / 2;
43    
44                    if (sid <= p_priv->s_priv_list[mid].sid)
45                    {
46                            right = mid;
47                    }
48                    else
49                    {
50                            left = mid + 1;
51                    }
52            }
53    
54            if (left == right && sid == p_priv->s_priv_list[left].sid) // found
55            {
56                    p_priv->s_priv_list[left].s_priv = priv;
57                    p_priv->s_priv_list[left].is_favor = is_favor;
58                    return 0;
59            }
60    
61            // not found
62            if (p_priv->s_count >= BBS_max_section)
63            {
64                    return -1;
65            }
66    
67            // move items at [left, p_priv->s_count - 1] to [left + 1, p_priv->s_count]
68            for (right = p_priv->s_count - 1; right >= left; right--)
69            {
70                    p_priv->s_priv_list[right + 1] = p_priv->s_priv_list[right];
71            }
72            p_priv->s_count++;
73    
74            // insert new item at offset left
75            p_priv->s_priv_list[left].sid = sid;
76            p_priv->s_priv_list[left].s_priv = priv;
77            p_priv->s_priv_list[left].is_favor = is_favor;
78    
79            return 0;
80    }
81    
82    int getpriv(BBS_user_priv *p_priv, int sid, int *p_is_favor)
83    {
84            int left = 0;
85            int right = p_priv->s_count - 1;
86            int mid = 0;
87    
88            while (left < right)
89            {
90                    mid = (left + right) / 2;
91    
92                    if (sid <= p_priv->s_priv_list[mid].sid)
93                    {
94                            right = mid;
95                    }
96                    else
97                    {
98                            left = mid + 1;
99                    }
100            }
101    
102            if (left == right && sid == p_priv->s_priv_list[left].sid) // found
103            {
104                    *p_is_favor = p_priv->s_priv_list[left].is_favor;
105                    return p_priv->s_priv_list[left].s_priv;
106            }
107    
108            if (sid != 0)
109            {
110                    *p_is_favor = 0;
111            }
112    
113            return (sid >= 0 ? p_priv->g_priv : S_NONE);
114    }
115    
116    int load_priv(MYSQL *db, BBS_user_priv *p_priv, long int uid)
117  {  {
118            MYSQL_RES *rs;
119            MYSQL_ROW row;
120            char sql[SQL_BUFFER_LEN];
121            int priv;
122            int is_favor;
123    
124            p_priv->uid = uid;
125            p_priv->level = (uid == 0 ? P_GUEST : P_USER);
126            p_priv->g_priv = S_DEFAULT;
127            p_priv->s_count = 0;
128    
129            if (db == NULL)
130                    return 1;
131    
132            // Permission
133            snprintf(sql, sizeof(sql),
134                             "SELECT p_post, p_msg FROM user_list WHERE UID = %ld AND verified",
135                             uid);
136            if (mysql_query(db, sql) != 0)
137            {
138                    log_error("Query user_list error: %s\n", mysql_error(db));
139                    return -1;
140            }
141            if ((rs = mysql_store_result(db)) == NULL)
142            {
143                    log_error("Get user_list data failed\n");
144                    return -1;
145            }
146            if ((row = mysql_fetch_row(rs)))
147            {
148                    p_priv->g_priv |= (atoi(row[0]) ? S_POST : 0);
149                    p_priv->g_priv |= (atoi(row[1]) ? S_MSG : 0);
150            }
151            mysql_free_result(rs);
152    
153            // Admin
154            snprintf(sql, sizeof(sql),
155                             "SELECT major FROM admin_config WHERE UID = %ld "
156                             "AND enable AND (NOW() BETWEEN begin_dt AND end_dt)",
157                             uid);
158            if (mysql_query(db, sql) != 0)
159            {
160                    log_error("Query admin_config error: %s\n", mysql_error(db));
161                    return -1;
162            }
163            if ((rs = mysql_store_result(db)) == NULL)
164            {
165                    log_error("Get admin_config data failed\n");
166                    return -1;
167            }
168            if ((row = mysql_fetch_row(rs)))
169            {
170                    p_priv->level |= (atoi(row[0]) ? P_ADMIN_M : P_ADMIN_S);
171                    p_priv->g_priv |= (atoi(row[0]) ? S_ALL : S_ADMIN);
172            }
173            mysql_free_result(rs);
174    
175            // Section Master
176            snprintf(sql, sizeof(sql),
177                             "SELECT section_master.SID, major FROM section_master "
178                             "INNER JOIN section_config ON section_master.SID = section_config.SID "
179                             "WHERE UID = %ld AND section_master.enable AND section_config.enable "
180                             "AND (NOW() BETWEEN begin_dt AND end_dt)",
181                             uid);
182            if (mysql_query(db, sql) != 0)
183            {
184                    log_error("Query section_master error: %s\n", mysql_error(db));
185                    return -1;
186            }
187            if ((rs = mysql_store_result(db)) == NULL)
188            {
189                    log_error("Get section_master data failed\n");
190                    return -1;
191            }
192            while ((row = mysql_fetch_row(rs)))
193            {
194                    p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);
195                    priv = (getpriv(p_priv, atoi(row[0]), &is_favor) | (atoi(row[1]) ? S_MAN_M : S_MAN_S));
196                    setpriv(p_priv, atoi(row[0]), priv, is_favor);
197            }
198            mysql_free_result(rs);
199    
200            // Section status
201            snprintf(sql, sizeof(sql),
202                             "SELECT SID, exp_get, read_user_level, write_user_level FROM section_config "
203                             "INNER JOIN section_class ON section_config.CID = section_class.CID "
204                             "WHERE section_config.enable AND section_class.enable "
205                             "ORDER BY SID");
206            if (mysql_query(db, sql) != 0)
207            {
208                    log_error("Query section_config error: %s\n", mysql_error(db));
209                    return -1;
210            }
211            if ((rs = mysql_store_result(db)) == NULL)
212            {
213                    log_error("Get section_config data failed\n");
214                    return -1;
215            }
216            while ((row = mysql_fetch_row(rs)))
217            {
218                    int priv = getpriv(p_priv, atoi(row[0]), &is_favor);
219                    if (p_priv->level < atoi(row[2]))
220                    {
221                            priv &= (~S_LIST);
222                    }
223                    if (p_priv->level < atoi(row[3]))
224                    {
225                            priv &= (~S_POST);
226                    }
227                    if (!atoi(row[1]))
228                    {
229                            priv &= (~S_GETEXP);
230                    }
231                    setpriv(p_priv, atoi(row[0]), priv, is_favor);
232            }
233            mysql_free_result(rs);
234    
235            // Section ban
236            snprintf(sql, sizeof(sql),
237                             "SELECT SID FROM ban_user_list WHERE UID = %ld AND enable "
238                             "AND (NOW() BETWEEN ban_dt AND unban_dt)",
239                             uid);
240            if (mysql_query(db, sql) != 0)
241            {
242                    log_error("Query ban_user_list error: %s\n", mysql_error(db));
243                    return -1;
244            }
245            if ((rs = mysql_store_result(db)) == NULL)
246            {
247                    log_error("Get ban_user_list data failed\n");
248                    return -1;
249            }
250            while ((row = mysql_fetch_row(rs)))
251            {
252                    priv = getpriv(p_priv, atoi(row[0]), &is_favor) & (~S_POST);
253                    setpriv(p_priv, atoi(row[0]), priv, is_favor);
254            }
255            mysql_free_result(rs);
256    
257            // User favor section
258            snprintf(sql, sizeof(sql),
259                             "SELECT SID FROM section_favorite WHERE UID = %ld",
260                             uid);
261            if (mysql_query(db, sql) != 0)
262            {
263                    log_error("Query section_favorite error: %s\n", mysql_error(db));
264                    return -1;
265            }
266            if ((rs = mysql_store_result(db)) == NULL)
267            {
268                    log_error("Get section_favorite data failed\n");
269                    return -1;
270            }
271            while ((row = mysql_fetch_row(rs)))
272            {
273                    priv = getpriv(p_priv, atoi(row[0]), &is_favor);
274                    if (!is_favor)
275                    {
276                            setpriv(p_priv, atoi(row[0]), priv, 1);
277                            priv = getpriv(p_priv, atoi(row[0]), &is_favor);
278                    }
279            }
280            mysql_free_result(rs);
281    
282    return 0;          return 0;
283  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

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