/[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.7 by sysadm, Mon Apr 28 12:45:57 2025 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"  #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 checklevel(BBS_user_priv *p_priv, int level)  BBS_user_priv BBS_priv;
 {  
         return (((p_priv->level & level)) ^ level ? 0 : 1);  
 }  
27    
28  int setpriv(BBS_user_priv *p_priv, int sid, int priv)  int setpriv(BBS_user_priv *p_priv, int sid, int priv, int is_favor)
29  {  {
30          int i;          int left = 0;
31          if (sid > 0)          int right = p_priv->s_count - 1;
32            int mid = 0;
33    
34            if (sid == 0)
35          {          {
36                  for (i = 0; i < p_priv->s_count; i++)                  p_priv->g_priv = priv;
37                  {                  return 0;
38                          if (p_priv->s_priv_list[i].sid == sid)          }
39                          {  
40                                  p_priv->s_priv_list[i].s_priv = priv;          while (left < right)
41                                  return 0;          {
42                          }                  mid = (left + right) / 2;
43                  }  
44                  if (i < BBS_max_section)                  if (sid <= p_priv->s_priv_list[mid].sid)
45                  {                  {
46                          p_priv->s_priv_list[i].s_priv = priv;                          right = mid;
47                  }                  }
48                  else                  else
49                  {                  {
50                          return -1;                          left = mid + 1;
51                  }                  }
52          }          }
53          else  
54            if (left == right && sid == p_priv->s_priv_list[left].sid) // found
55          {          {
56                  p_priv->g_priv = priv;                  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;          return 0;
80  }  }
81    
82  int getpriv(BBS_user_priv *p_priv, int sid)  int getpriv(BBS_user_priv *p_priv, int sid, int *p_is_favor)
83  {  {
84          int i;          int left = 0;
85          for (i = 0; i < p_priv->s_count; i++)          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                  if (p_priv->s_priv_list[i].sid == sid)                  *p_is_favor = p_priv->s_priv_list[left].is_favor;
105                          return p_priv->s_priv_list[i].s_priv;                  return p_priv->s_priv_list[left].s_priv;
106          }          }
107    
108          return (sid >= 0 ? p_priv->g_priv : S_NONE);          if (sid != 0)
109  }          {
110                    *p_is_favor = 0;
111            }
112    
113  int checkpriv(BBS_user_priv *p_priv, int sid, int priv)          return (sid >= 0 ? p_priv->g_priv : S_NONE);
 {  
         return (((getpriv(p_priv, sid) & priv)) ^ priv ? 0 : 1);  
114  }  }
115    
116  int load_priv(MYSQL *db, BBS_user_priv *p_priv, long int uid)  int load_priv(MYSQL *db, BBS_user_priv *p_priv, long int uid)
117  {  {
118          MYSQL_RES *rs;          MYSQL_RES *rs;
119          MYSQL_ROW row;          MYSQL_ROW row;
120          char sql[1024];          char sql[SQL_BUFFER_LEN];
121          int i;          int priv;
122            int is_favor;
123    
124          p_priv->uid = uid;          p_priv->uid = uid;
125          p_priv->level = (uid == 0 ? P_GUEST : P_USER);          p_priv->level = (uid == 0 ? P_GUEST : P_USER);
126          p_priv->g_priv = S_DEFAULT;          p_priv->g_priv = S_DEFAULT;
127            p_priv->s_count = 0;
128    
129          if (db == NULL)          if (db == NULL)
130                  return 1;                  return 1;
131    
132          // Permission          // Permission
133          sprintf(sql, "SELECT p_post, p_msg FROM user_list WHERE UID = %ld AND verified",          snprintf(sql, sizeof(sql),
134                          uid);                           "SELECT p_post, p_msg FROM user_list WHERE UID = %ld AND verified",
135                             uid);
136          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
137          {          {
138                  log_error("Query user_list failed\n");                  log_error("Query user_list error: %s\n", mysql_error(db));
139                  return -1;                  return -1;
140          }          }
141          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 98  int load_priv(MYSQL *db, BBS_user_priv * Line 143  int load_priv(MYSQL *db, BBS_user_priv *
143                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
144                  return -1;                  return -1;
145          }          }
146          if (row = mysql_fetch_row(rs))          if ((row = mysql_fetch_row(rs)))
147          {          {
148                  p_priv->g_priv |= (atoi(row[0]) ? S_POST : 0);                  p_priv->g_priv |= (atoi(row[0]) ? S_POST : 0);
149                  p_priv->g_priv |= (atoi(row[1]) ? S_MSG : 0);                  p_priv->g_priv |= (atoi(row[1]) ? S_MSG : 0);
# Line 106  int load_priv(MYSQL *db, BBS_user_priv * Line 151  int load_priv(MYSQL *db, BBS_user_priv *
151          mysql_free_result(rs);          mysql_free_result(rs);
152    
153          // Admin          // Admin
154          sprintf(sql, "SELECT major FROM admin_config WHERE UID = %ld "          snprintf(sql, sizeof(sql),
155                                   "AND enable AND (NOW() BETWEEN begin_dt AND end_dt)",                           "SELECT major FROM admin_config WHERE UID = %ld "
156                          uid);                           "AND enable AND (NOW() BETWEEN begin_dt AND end_dt)",
157                             uid);
158          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
159          {          {
160                  log_error("Query admin_config failed\n");                  log_error("Query admin_config error: %s\n", mysql_error(db));
161                  return -1;                  return -1;
162          }          }
163          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 119  int load_priv(MYSQL *db, BBS_user_priv * Line 165  int load_priv(MYSQL *db, BBS_user_priv *
165                  log_error("Get admin_config data failed\n");                  log_error("Get admin_config data failed\n");
166                  return -1;                  return -1;
167          }          }
168          if (row = mysql_fetch_row(rs))          if ((row = mysql_fetch_row(rs)))
169          {          {
170                  p_priv->level |= (atoi(row[1]) ? P_ADMIN_M : P_ADMIN_S);                  p_priv->level |= (atoi(row[0]) ? P_ADMIN_M : P_ADMIN_S);
171                  p_priv->g_priv |= (atoi(row[1]) ? S_ALL : S_ADMIN);                  p_priv->g_priv |= (atoi(row[0]) ? S_ALL : S_ADMIN);
172          }          }
173          mysql_free_result(rs);          mysql_free_result(rs);
174    
175          // Section Master          // Section Master
176          sprintf(sql, "SELECT section_master.SID, major FROM section_master "          snprintf(sql, sizeof(sql),
177                                   "INNER JOIN section_config ON section_master.SID = section_config.SID "                           "SELECT section_master.SID, major FROM section_master "
178                                   "WHERE UID = %ld AND section_master.enable AND section_config.enable "                           "INNER JOIN section_config ON section_master.SID = section_config.SID "
179                                   "AND (NOW() BETWEEN begin_dt AND end_dt)",                           "WHERE UID = %ld AND section_master.enable AND section_config.enable "
180                          uid);                           "AND (NOW() BETWEEN begin_dt AND end_dt)",
181                             uid);
182          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
183          {          {
184                  log_error("Query section_master failed\n");                  log_error("Query section_master error: %s\n", mysql_error(db));
185                  return -1;                  return -1;
186          }          }
187          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 142  int load_priv(MYSQL *db, BBS_user_priv * Line 189  int load_priv(MYSQL *db, BBS_user_priv *
189                  log_error("Get section_master data failed\n");                  log_error("Get section_master data failed\n");
190                  return -1;                  return -1;
191          }          }
192          while (row = mysql_fetch_row(rs))          while ((row = mysql_fetch_row(rs)))
193          {          {
194                  p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);                  p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);
195                  setpriv(p_priv, atoi(row[0]), getpriv(p_priv, atoi(row[0])) | (atoi(row[1]) ? S_MAN_M : S_MAN_S));                  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);          mysql_free_result(rs);
199    
200          // Section status          // Section status
201          sprintf(sql, "SELECT SID, exp_get, read_user_level, write_user_level FROM section_config "          snprintf(sql, sizeof(sql),
202                                   "INNER JOIN section_class ON section_config.CID = section_class.CID "                           "SELECT SID, exp_get, read_user_level, write_user_level FROM section_config "
203                                   "WHERE section_config.enable AND section_class.enable "                           "INNER JOIN section_class ON section_config.CID = section_class.CID "
204                                   "ORDER BY SID");                           "WHERE section_config.enable AND section_class.enable "
205                             "ORDER BY SID");
206          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
207          {          {
208                  log_error("Query section_config failed\n");                  log_error("Query section_config error: %s\n", mysql_error(db));
209                  return -1;                  return -1;
210          }          }
211          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 164  int load_priv(MYSQL *db, BBS_user_priv * Line 213  int load_priv(MYSQL *db, BBS_user_priv *
213                  log_error("Get section_config data failed\n");                  log_error("Get section_config data failed\n");
214                  return -1;                  return -1;
215          }          }
216          while (row = mysql_fetch_row(rs))          while ((row = mysql_fetch_row(rs)))
217          {          {
218                  int priv = getpriv(p_priv, atoi(row[0]));                  int priv = getpriv(p_priv, atoi(row[0]), &is_favor);
219                  if (p_priv->level < atoi(row[2]))                  if (p_priv->level < atoi(row[2]))
220                  {                  {
221                          priv &= (~S_LIST);                          priv &= (~S_LIST);
# Line 179  int load_priv(MYSQL *db, BBS_user_priv * Line 228  int load_priv(MYSQL *db, BBS_user_priv *
228                  {                  {
229                          priv &= (~S_GETEXP);                          priv &= (~S_GETEXP);
230                  }                  }
231                  setpriv(p_priv, atoi(row[0]), priv);                  setpriv(p_priv, atoi(row[0]), priv, is_favor);
232          }          }
233          mysql_free_result(rs);          mysql_free_result(rs);
234    
235          // Section ban          // Section ban
236          sprintf(sql, "SELECT SID FROM ban_user_list WHERE UID = %ld AND enable "          snprintf(sql, sizeof(sql),
237                                   "AND (NOW() BETWEEN ban_dt AND unban_dt)",                           "SELECT SID FROM ban_user_list WHERE UID = %ld AND enable "
238                          uid);                           "AND (NOW() BETWEEN ban_dt AND unban_dt)",
239                             uid);
240          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
241          {          {
242                  log_error("Query ban_user_list failed\n");                  log_error("Query ban_user_list error: %s\n", mysql_error(db));
243                  return -1;                  return -1;
244          }          }
245          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 197  int load_priv(MYSQL *db, BBS_user_priv * Line 247  int load_priv(MYSQL *db, BBS_user_priv *
247                  log_error("Get ban_user_list data failed\n");                  log_error("Get ban_user_list data failed\n");
248                  return -1;                  return -1;
249          }          }
250          while (row = mysql_fetch_row(rs))          while ((row = mysql_fetch_row(rs)))
251          {          {
252                  setpriv(p_priv, atoi(row[0]),                  priv = getpriv(p_priv, atoi(row[0]), &is_favor) & (~S_POST);
253                                  getpriv(p_priv, atoi(row[0])) & (~S_POST));                  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);          mysql_free_result(rs);
281    


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

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