/[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.16 by sysadm, Fri May 30 00:58:02 2025 UTC Revision 1.19 by sysadm, Tue Jun 3 13:14:26 2025 UTC
# Line 20  Line 20 
20  #include "database.h"  #include "database.h"
21  #include "log.h"  #include "log.h"
22  #include <stdio.h>  #include <stdio.h>
 #include <mysql.h>  
23  #include <stdlib.h>  #include <stdlib.h>
24    #include <mysql/mysql.h>
25    
26  BBS_user_priv BBS_priv;  BBS_user_priv BBS_priv;
27    
28  int setpriv(BBS_user_priv *p_priv, int sid, int priv)  inline static int search_priv(BBS_user_priv *p_priv, int sid, int *p_offset)
29  {  {
30          int left = 0;          int left = 0;
31          int right = p_priv->s_count - 1;          int right = p_priv->s_count - 1;
32          int mid;          int mid = 0;
   
         if (sid == 0)  
         {  
                 p_priv->g_priv = priv;  
                 return 0;  
         }  
33    
34          while (left < right)          while (left < right)
35          {          {
# Line 51  int setpriv(BBS_user_priv *p_priv, int s Line 45  int setpriv(BBS_user_priv *p_priv, int s
45                  }                  }
46          }          }
47    
48          if (sid == p_priv->s_priv_list[left].sid) // found          *p_offset = left;
49    
50            return (left == right && sid == p_priv->s_priv_list[left].sid);
51    }
52    
53    int setpriv(BBS_user_priv *p_priv, int sid, int priv, int is_favor)
54    {
55            int offset;
56            int i;
57    
58            if (sid == 0)
59          {          {
60                  p_priv->s_priv_list[left].s_priv = priv;                  p_priv->g_priv = priv;
61                    return 0;
62            }
63    
64            if (search_priv(p_priv, sid, &offset)) //found
65            {
66                    p_priv->s_priv_list[offset].s_priv = priv;
67                    p_priv->s_priv_list[offset].is_favor = is_favor;
68                  return 0;                  return 0;
69          }          }
70    
# Line 64  int setpriv(BBS_user_priv *p_priv, int s Line 75  int setpriv(BBS_user_priv *p_priv, int s
75          }          }
76    
77          // move items at [left, p_priv->s_count - 1] to [left + 1, p_priv->s_count]          // move items at [left, p_priv->s_count - 1] to [left + 1, p_priv->s_count]
78          for (right = p_priv->s_count - 1; right >= left; right--)          for (i = p_priv->s_count - 1; i >= offset; i--)
79          {          {
80                  p_priv->s_priv_list[right + 1] = p_priv->s_priv_list[right];                  p_priv->s_priv_list[i + 1] = p_priv->s_priv_list[i];
81          }          }
82            p_priv->s_count++;
83    
84          // insert new item at offset left          // insert new item at offset left
85          p_priv->s_priv_list[left].s_priv = priv;          p_priv->s_priv_list[offset].sid = sid;
86            p_priv->s_priv_list[offset].s_priv = priv;
87            p_priv->s_priv_list[offset].is_favor = is_favor;
88    
89          return 0;          return 0;
90  }  }
91    
92  int getpriv(BBS_user_priv *p_priv, int sid)  int getpriv(BBS_user_priv *p_priv, int sid, int *p_is_favor)
93  {  {
94          int left = 0;          int offset;
         int right = p_priv->s_count - 1;  
         int mid;  
   
         while (left < right)  
         {  
                 mid = (left + right) / 2;  
   
                 if (sid <= p_priv->s_priv_list[mid].sid)  
                 {  
                         right = mid;  
                 }  
                 else  
                 {  
                         left = mid + 1;  
                 }  
         }  
95    
96          if (sid == p_priv->s_priv_list[left].sid) // found          if (search_priv(p_priv, sid, &offset)) //found
97          {          {
98                  return p_priv->s_priv_list[left].s_priv;                  *p_is_favor = p_priv->s_priv_list[offset].is_favor;
99                    return p_priv->s_priv_list[offset].s_priv;
100          }          }
101    
102            *p_is_favor = 0;
103          return (sid >= 0 ? p_priv->g_priv : S_NONE);          return (sid >= 0 ? p_priv->g_priv : S_NONE);
104  }  }
105    
# Line 107  int load_priv(MYSQL *db, BBS_user_priv * Line 108  int load_priv(MYSQL *db, BBS_user_priv *
108          MYSQL_RES *rs;          MYSQL_RES *rs;
109          MYSQL_ROW row;          MYSQL_ROW row;
110          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
111            int priv;
112            int is_favor;
113    
114          p_priv->uid = uid;          p_priv->uid = uid;
115          p_priv->level = (uid == 0 ? P_GUEST : P_USER);          p_priv->level = (uid == 0 ? P_GUEST : P_USER);
# Line 117  int load_priv(MYSQL *db, BBS_user_priv * Line 120  int load_priv(MYSQL *db, BBS_user_priv *
120                  return 1;                  return 1;
121    
122          // Permission          // Permission
123          snprintf(sql, sizeof(sql), "SELECT p_post, p_msg FROM user_list WHERE UID = %ld AND verified",          snprintf(sql, sizeof(sql),
124                             "SELECT p_post, p_msg FROM user_list WHERE UID = %ld AND verified",
125                           uid);                           uid);
126          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
127          {          {
# Line 137  int load_priv(MYSQL *db, BBS_user_priv * Line 141  int load_priv(MYSQL *db, BBS_user_priv *
141          mysql_free_result(rs);          mysql_free_result(rs);
142    
143          // Admin          // Admin
144          snprintf(sql, sizeof(sql), "SELECT major FROM admin_config WHERE UID = %ld "          snprintf(sql, sizeof(sql),
145                                                             "AND enable AND (NOW() BETWEEN begin_dt AND end_dt)",                           "SELECT major FROM admin_config WHERE UID = %ld "
146                             "AND enable AND (NOW() BETWEEN begin_dt AND end_dt)",
147                           uid);                           uid);
148          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
149          {          {
# Line 158  int load_priv(MYSQL *db, BBS_user_priv * Line 163  int load_priv(MYSQL *db, BBS_user_priv *
163          mysql_free_result(rs);          mysql_free_result(rs);
164    
165          // Section Master          // Section Master
166          snprintf(sql, sizeof(sql), "SELECT section_master.SID, major FROM section_master "          snprintf(sql, sizeof(sql),
167                                                             "INNER JOIN section_config ON section_master.SID = section_config.SID "                           "SELECT section_master.SID, major FROM section_master "
168                                                             "WHERE UID = %ld AND section_master.enable AND section_config.enable "                           "INNER JOIN section_config ON section_master.SID = section_config.SID "
169                                                             "AND (NOW() BETWEEN begin_dt AND end_dt)",                           "WHERE UID = %ld AND section_master.enable AND section_config.enable "
170                             "AND (NOW() BETWEEN begin_dt AND end_dt)",
171                           uid);                           uid);
172          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
173          {          {
# Line 176  int load_priv(MYSQL *db, BBS_user_priv * Line 182  int load_priv(MYSQL *db, BBS_user_priv *
182          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
183          {          {
184                  p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);                  p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);
185                  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));
186                    setpriv(p_priv, atoi(row[0]), priv, is_favor);
187          }          }
188          mysql_free_result(rs);          mysql_free_result(rs);
189    
190          // Section status          // Section status
191          snprintf(sql, sizeof(sql), "SELECT SID, exp_get, read_user_level, write_user_level FROM section_config "          snprintf(sql, sizeof(sql),
192                                                             "INNER JOIN section_class ON section_config.CID = section_class.CID "                           "SELECT SID, exp_get, read_user_level, write_user_level FROM section_config "
193                                                             "WHERE section_config.enable AND section_class.enable "                           "INNER JOIN section_class ON section_config.CID = section_class.CID "
194                                                             "ORDER BY SID");                           "WHERE section_config.enable AND section_class.enable "
195                             "ORDER BY SID");
196          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
197          {          {
198                  log_error("Query section_config error: %s\n", mysql_error(db));                  log_error("Query section_config error: %s\n", mysql_error(db));
# Line 197  int load_priv(MYSQL *db, BBS_user_priv * Line 205  int load_priv(MYSQL *db, BBS_user_priv *
205          }          }
206          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
207          {          {
208                  int priv = getpriv(p_priv, atoi(row[0]));                  int priv = getpriv(p_priv, atoi(row[0]), &is_favor);
209                  if (p_priv->level < atoi(row[2]))                  if (p_priv->level < atoi(row[2]))
210                  {                  {
211                          priv &= (~S_LIST);                          priv &= (~S_LIST);
# Line 210  int load_priv(MYSQL *db, BBS_user_priv * Line 218  int load_priv(MYSQL *db, BBS_user_priv *
218                  {                  {
219                          priv &= (~S_GETEXP);                          priv &= (~S_GETEXP);
220                  }                  }
221                  setpriv(p_priv, atoi(row[0]), priv);                  setpriv(p_priv, atoi(row[0]), priv, is_favor);
222          }          }
223          mysql_free_result(rs);          mysql_free_result(rs);
224    
225          // Section ban          // Section ban
226          snprintf(sql, sizeof(sql), "SELECT SID FROM ban_user_list WHERE UID = %ld AND enable "          snprintf(sql, sizeof(sql),
227                                                             "AND (NOW() BETWEEN ban_dt AND unban_dt)",                           "SELECT SID FROM ban_user_list WHERE UID = %ld AND enable "
228                             "AND (NOW() BETWEEN ban_dt AND unban_dt)",
229                           uid);                           uid);
230          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
231          {          {
# Line 230  int load_priv(MYSQL *db, BBS_user_priv * Line 239  int load_priv(MYSQL *db, BBS_user_priv *
239          }          }
240          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
241          {          {
242                  setpriv(p_priv, atoi(row[0]),                  priv = getpriv(p_priv, atoi(row[0]), &is_favor) & (~S_POST);
243                                  getpriv(p_priv, atoi(row[0])) & (~S_POST));                  setpriv(p_priv, atoi(row[0]), priv, is_favor);
244            }
245            mysql_free_result(rs);
246    
247            // User favor section
248            snprintf(sql, sizeof(sql),
249                             "SELECT SID FROM section_favorite WHERE UID = %ld",
250                             uid);
251            if (mysql_query(db, sql) != 0)
252            {
253                    log_error("Query section_favorite error: %s\n", mysql_error(db));
254                    return -1;
255            }
256            if ((rs = mysql_store_result(db)) == NULL)
257            {
258                    log_error("Get section_favorite data failed\n");
259                    return -1;
260            }
261            while ((row = mysql_fetch_row(rs)))
262            {
263                    priv = getpriv(p_priv, atoi(row[0]), &is_favor);
264                    if (!is_favor)
265                    {
266                            setpriv(p_priv, atoi(row[0]), priv, 1);
267                            priv = getpriv(p_priv, atoi(row[0]), &is_favor);
268                    }
269          }          }
270          mysql_free_result(rs);          mysql_free_result(rs);
271    


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

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