Develop and Download Open Source Software

Browse CVS Repository

Contents of /xoonips/AL/commonal.cc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.27 - (show annotations) (download) (as text)
Mon Dec 6 11:34:47 2004 UTC (19 years, 4 months ago) by aga
Branch: MAIN
Changes since 1.26: +24 -1 lines
File MIME type: text/x-c++src
・uninitializeDB()を追加.

1 /*
2 *
3 * $Revision: 1.26 $
4 * $Log: commonal.cc,v $
5 * Revision 1.26 2004/12/06 10:17:36 youi
6 * mysql_use_result????腟?????罧??????????冴??????鐚?
7 * while( row = mysql_fetch_row(result) );???水?ワ?
8 *
9 * Revision 1.25 2004/12/06 07:24:33 youi
10 * insertAccount, insertGroup:
11 * ?域?菴遵?????????宴???綽????????若??D鐚??違???若??ID??膃??綣??違???吾??莨若??.
12 *
13 * Revision 1.24 2004/12/06 01:45:50 aga
14 * ?祉?潟?<?潟???ゃ??.
15 * ?脂???上???篆??.
16 *
17 * Revision 1.23 2004/12/04 09:21:10 aga
18 * ??p_config ?? vpaccount_config.
19 *
20 * Revision 1.22 2004/12/03 07:17:21 youi
21 * isModerator?с?≪???祉?鴻???????若?帥???若?鴻???若????????篆??.
22 *
23 * Revision 1.21 2004/12/01 10:28:58 youi
24 * DB????????????眼??軸??篆??
25 * institute -> division
26 * organizaion -> company_name
27 *
28 * Revision 1.20 2004/12/01 04:37:04 youi
29 * freeResult: unsigned int* ?? int* ?娯信罩?
30 * ?潟?<?潟???????∽?違???????菴処?.
31 *
32 * Revision 1.19 2004/11/30 06:40:11 youi
33 * ?∽?違??菴遵??(gidExists, uidExists)
34 * getGroupCount??絎?臂?
35 * getGroupsByUid??絎?臂?
36 * isGroupAdmin??篆??.
37 * dumpGroupAdmins??絎?臂?
38 * deleteMember??絎?臂?
39 * insertMember??絎?臂?
40 * getMembers??絎?臂?
41 * isModerator??絎?臂?
42 *
43 * Revision 1.18 2004/11/30 05:46:32 aga
44 * ??ession????remoteHost??????
45 *
46 * Revision 1.17 2004/11/27 09:35:49 youi
47 * isActivated??絎?臂?
48 * activate??絎?臂?
49 * dumpUid??絎?臂?
50 *
51 * Revision 1.16 2004/11/27 06:29:29 youi
52 * getAccountCount??絎?臂?
53 * dumpGids??絎?臂?
54 * insertGroup, deleteGroup, updateGroup??絎?臂?
55 * getGroup, getGroups??絎?臂?
56 * ???若?帥???若?劫?????????弱?冴?????鐚?RES_DB_NOT_INITIALIZED??菴????????????∽?違??申??
57 * (???綽????∽?違??罧??c??????)
58 *
59 * Revision 1.15 2004/11/27 02:22:22 aga
60 * ??nitializeDB????ddSlashes()???若?吟???純?<??????篆??.
61 *
62 * Revision 1.14 2004/11/27 01:07:31 youi
63 * criteria2str: LIMIT??RDER BY?????????ャ???帥????.
64 *
65 * Revision 1.13 2004/11/27 00:35:39 youi
66 * getAccounts??絎?臂?
67 * getAccount??篆??鐚???????etAccounts???若?喝?冴??.
68 * criteria2str??絎?臂?
69 *
70 * Revision 1.12 2004/11/26 09:45:28 youi
71 * getAccount??絎?臂?
72 *
73 * Revision 1.11 2004/11/26 08:16:26 aga
74 * ??etSession, loginUser, createSession, freeResult(const session_t*) 篆??.
75 *
76 * Revision 1.10 2004/11/26 07:57:42 youi
77 * updateAccount, deleteAccount??絎?臂?
78 * mysql_query紊掩??????tderr?吾?????若?潟?若?????????若?<???祉?若?吾???阪??.
79 *
80 * Revision 1.9 2004/11/26 07:38:02 aga
81 * ??AILUE -> FAILURE.
82 *
83 * Revision 1.8 2004/11/26 06:36:55 aga
84 * ??reateSession, getSession ??篏???.
85 *
86 * Revision 1.7 2004/11/26 04:51:47 youi
87 * dbprefix??絎?臂?????.
88 * insertAccount??絎?臂?????.
89 * isValidSessionID??絎?臂?????.
90 *
91 * Revision 1.6 2004/11/26 04:35:55 aga
92 * ??oginUser(), logoutUser()??篏???.
93 *
94 * Revision 1.5 2004/11/26 01:08:25 aga
95 * ??ddSlashes()篏???.
96 *
97 * Revision 1.4 2004/11/25 12:14:29 youi
98 * getUid????????絎?臂?????.
99 *
100 * Revision 1.3 2004/11/25 11:13:59 youi
101 * getLastErrorString/setLastErrorString??絎?臂?
102 *
103 * Revision 1.2 2004/11/25 08:55:19 youi
104 * 綣??違??????篆??.
105 * freeResult??菴遵??.
106 *
107 * Revision 1.1 2004/11/25 05:14:58 youi
108 * initial version
109 *
110 *
111 */
112 #include <stdio.h>
113 #include <stdlib.h>
114 #include <string.h>
115 #include <mysql.h>
116 #include <errmsg.h>
117 #include <string>
118 #include <assert.h>
119 using namespace std;
120
121 #include "common.h"
122 #include "account.h"
123 #include "group.h"
124 #include "session.h"
125 #include "criteria.h"
126 #include "commonal.h"
127
128
129 static MYSQL* mysql = NULL; //!< MySQL????潟????/span>
130 static string dbprefix; //!< XOOPS???若?帥???若?鴻???若??????REFIX
131
132 /**
133 *
134 * php??ddslashes????????
135 *
136 * @param addslashes???鴻????絖?????NULL??絎?筝?????
137 * @return addslashes????????絖???
138 */
139 string addSlashes( const char *str )
140 {
141 if ( mysql == NULL ){
142 string s;
143 return s;
144 }
145 int len = strlen(str);
146 char *buf = new char[len*2+1];
147 int bufLen = mysql_real_escape_string( mysql, buf, str, len );
148 string s( buf, bufLen );
149 return s;
150 }
151
152 /**
153 *
154 * gid??????????с????
155 * DB???≪???祉?劫け????????false??????鐚?/span>
156 *
157 * @param gid ???с??????????GID
158 * @return true 絖???????
159 * @return false 絖?????????
160 *
161 */
162 static bool gidExists( groupid_t gid )
163 {
164 string sql;
165 MYSQL_RES* result;
166 MYSQL_ROW row;
167
168 sql = "SELECT * FROM " + dbprefix + "_vpaccount_groups ";
169 sql += "WHERE gid=" + string( unsignedIntToString( gid ) );
170 if( mysql_query( mysql, sql.c_str( ) ) ){
171 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
172 return false;
173 }
174 result = mysql_store_result( mysql ) ;
175 if( !mysql_fetch_row(result) ){
176 mysql_free_result( result );
177 return false;
178 }
179 mysql_free_result( result );
180 return true;
181 }
182
183
184 /**
185 *
186 * Platform???若?倶???宴??id??絖??????????????с????
187 * DB???≪???祉?劫け????????false??????鐚?/span>
188 *
189 * @param uid ???с??????????UID
190 * @return true 絖???????
191 * @return false 絖?????????
192 *
193 */
194 static bool uidExists( userid_t uid )
195 {
196 string sql;
197 MYSQL_RES* result;
198 MYSQL_ROW row;
199
200 sql = "SELECT * FROM " + dbprefix + "_vpaccount_users ";
201 sql += "WHERE uid=" + string( unsignedIntToString( uid ) );
202 if( mysql_query( mysql, sql.c_str( ) ) ){
203 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
204 return false;
205 }
206 result = mysql_store_result( mysql ) ;
207 if( !mysql_fetch_row(result) ){
208 mysql_free_result( result );
209 return false;
210 }
211 mysql_free_result( result );
212 return true;
213 }
214
215
216 /**
217 *
218 * criteria ?? SQL?????????
219 *
220 *
221 *
222 */
223 string criteria2str( criteria* cri )
224 {
225 string sql;
226
227 const orderby* odrby = cri -> headOrderBy( );
228 if( odrby != 0 ){
229 sql += " ORDER BY " + string( odrby -> getColumn( ) );
230 sql += ( odrby -> getOrder( ) == orderby::DESC ) ? " DESC" : " ";
231 while( ( odrby = cri -> nextOrderBy( ) ) != 0 ){
232 sql += ", " + string( odrby -> getColumn( ) );
233 sql += ( odrby -> getOrder( ) == orderby::DESC ) ? " DESC" : " ";
234 }
235 }
236
237 if( cri -> getLimitStart( ) != 0 || cri -> getLimitRows( ) != 0 ){
238 sql += " LIMIT " + string( intToString( cri -> getLimitStart( ) ) )
239 + ", " + string( intToString( cri -> getLimitRows( ) ) );
240 }
241 return sql;
242 }
243
244 /**
245 *
246 * ???若?帥???若?鴻????????
247 *
248 * @param host ???若?帥???若?鴻?????鴻????
249 * @param user ???若?帥???若?鴻???≪???祉?鴻???????若?九??
250 * @param password 筝?荐????若?吟?????鴻???若??
251 * @param dbname ???若?帥???若?鴻??????
252 * @param prefix XOOPS???若?帥???若?鴻???若??????REFIX
253 * @return result_t
254 * @refer result_t
255 */
256 result_t initializeDB( const char* host, const char* user, const char* password, const char* dbname, const char* prefix )
257 {
258 // ?≪??・膓?筝???????筝???????/span>
259 if ( mysql != NULL )
260 mysql_close( mysql );
261
262 dbprefix = prefix;
263
264
265 // ????????・膓?
266 mysql = mysql_init(NULL);
267 if ( mysql == NULL ){
268 return RES_DB_INITIALIZE_ERROR;
269 }
270 char *unix_socket = NULL; // ?
271 uint flag = 0; // ?
272 if ( NULL == mysql_real_connect( mysql, host, user, password, dbname, 3306, unix_socket, flag ) ){
273 return RES_DB_CONNECT_ERROR;
274 }
275
276 return RES_OK;
277 }
278
279 /**
280 *
281 * DB?ョ????????違???????????障??????
282 *
283 * @param ????
284 * @return RES_OK
285 */
286 result_t uninitializeDB()
287 {
288 if ( mysql != NULL ){
289 mysql_close( mysql );
290 mysql = NULL;
291 }
292
293 return RES_OK;
294 }
295
296
297 /**
298 *
299 * Platform???若?倶?粋??倶????緇?
300 *
301 * @param
302 * @return
303 *
304 */
305 bool isActivated( sessionid_t sid, userid_t uid )
306 {
307 if( mysql == NULL ) return false;
308 if( !isValidSessionID( sid ) ) return false;
309
310 string sql;
311 MYSQL_RES* result;
312 MYSQL_ROW row;
313
314 sql = "SELECT * FROM " + dbprefix + "_vpaccount_users ";
315 sql += "WHERE activate=1 and uid=" + string( unsignedIntToString( uid ) );
316 if( mysql_query( mysql, sql.c_str( ) ) ){
317 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
318 return false;
319 }
320 result = mysql_store_result( mysql ) ;
321 if( row = mysql_fetch_row(result) ){
322 mysql_free_result( result );
323 return true;
324 }
325 return false;
326 }
327
328 /**
329 *
330 * Platform???若?倶?粋??倶??紊???/span>
331 *
332 * @param
333 * @return
334 *
335 */
336 result_t activate( sessionid_t sid, userid_t uid, bool activate )
337 {
338 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
339 if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
340
341 string sql;
342 MYSQL_RES* result;
343 MYSQL_ROW row;
344
345 sql = "UPDATE " + dbprefix + "_vpaccount_users ";
346 sql += "SET activate=" + string( activate ? "1" : "0" );
347 sql += " WHERE uid=" + string( unsignedIntToString( uid ) );
348 if( mysql_query( mysql, sql.c_str( ) ) ){
349 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
350 return RES_DB_QUERY_ERROR;
351 }
352 if ( mysql_affected_rows(mysql) == 0 ){
353 // ?贋?医け??
354 return RES_NO_SUCH_USER;
355 }
356 return RES_OK;
357 }
358
359 /**
360 *
361 * ?≪?????潟????/span>
362 *
363 * @param
364 * @return
365 *
366 */
367 int getAccountCount( sessionid_t sid )
368 {
369 if( mysql == NULL ) return 0;
370 if( !isValidSessionID( sid ) ) return 0;
371
372 int ret = 0;
373 string sql;
374 MYSQL_RES* result;
375 MYSQL_ROW row;
376
377 //?≪?????潟???違??羆?????
378 sql = "SELECT COUNT(*) FROM " + dbprefix + "_vpaccount_users ";
379 if( mysql_query( mysql, sql.c_str( ) ) ){
380 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
381 return 0;
382 }
383 result = mysql_store_result( mysql ) ;
384 if( row = mysql_fetch_row(result) ){
385 ret = atoi( row[ 0 ] );
386 }else{
387 mysql_free_result( result );
388 return 0;
389 }
390 mysql_free_result( result );
391 return ret;
392 }
393
394 /**
395 *
396 * ?≪?????潟??????/span>
397 *
398 * @param
399 * @return RES_OK
400 * @return RES_NO_SUCH_SESSION
401 * @return RES_DB_QUERY_ERROR
402 *
403 */
404 result_t deleteAccount( sessionid_t sid, userid_t uid )
405 {
406 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
407 if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
408
409 string sql;
410 sql = "DELETE FROM " + dbprefix + "_users ";
411 sql += "WHERE uid = " + string( unsignedIntToString( uid ) );
412 if( mysql_query( mysql, sql.c_str( ) ) ){
413 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
414 return RES_DB_QUERY_ERROR;
415 }
416
417 sql = "DELETE FROM " + dbprefix + "_vpaccount_users ";
418 sql += "WHERE uid = " + string( unsignedIntToString( uid ) );
419 if( mysql_query( mysql, sql.c_str( ) ) ){
420 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
421 return RES_DB_QUERY_ERROR;
422 }
423
424 return RES_OK;
425 }
426
427 /**
428 *
429 * ?≪?????潟?????怨??
430 *
431 * @param sid ?祉???激?с??D
432 * @param uid ??緇??????????若?吟??ID
433 * @param acc ??緇??????≪?????潟??????account_t)?????ゃ?潟?帥???吾??莨若??綣???/span>
434 * @return RES_OK
435 * @return RES_NO_SUCH_USER
436 * @return RES_NO_SUCH_SESSION
437 * @return RES_DB_QUERY_ERROR
438 * @return RES_ERROR
439 *
440 */
441 result_t getAccount( sessionid_t sid, userid_t uid, const account_t** acc )
442 {
443 int len;
444 static criteria c;
445 result_t res = getAccounts( sid, &uid, 1, &c, acc, &len );
446 if( len == 0 ) return RES_NO_SUCH_USER;
447 return res;
448 }
449
450 /**
451 *
452 * ?≪?????潟?????怨??
453 *
454 * @param sid ?祉???激?с??D
455 * @param uids ??緇??????????若?吟??ID??????
456 * @param uidsLen uids???????膣???/span>
457 * @param cri 腟???????我??絎?鐚??純?若???>散??絎?
458 * @param accounts 罎?膣∝??????????????ゃ?潟?帥???吾??莨若??綣???/span>
459 * @param accountsLen 罎?膣∝???????????*accounts???膣???
460 * @return RES_OK| RES_DB_NOT_INITIALIZED| RES_NO_SUCH_SESSION
461 * @return RES_DB_QUERY_ERROR
462 *
463 */
464 result_t getAccounts( sessionid_t sid, const userid_t* uids, int uidsLen, criteria_t* cri, const account_t** accounts, int* accountsLen )
465 {
466 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
467 if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
468
469 string sql;
470 MYSQL_RES* result;
471 MYSQL_ROW row;
472 account_t* dst = new account_t[ uidsLen ];
473
474 sql += "SELECT u1.uid, u1.name, u1.uname, u1.email, u1.url, u1.user_avatar, u1.user_regdate, u1.user_icq, u1.user_from, u1.user_sig, u1.user_viewemail, u1.actkey, u1.user_aim, u1.user_yim, u1.user_msnm, u1.pass, u1.posts, u1.attachsig, u1.rank, u1.level, u1.theme, u1.timezone_offset, u1.last_login, u1.umode, u1.uorder, u1.notify_method, u1.notify_mode, u1.user_occ, u1.bio, u1.user_intrest, u1.user_mailok, u2.activate, u2.address, u2.division, u2.tel, u2.company_name, u2.country, u2.zipcode, u2.fax, u2.base_url, u2.notice_mail, u2.notice_mail_since ";
475 sql += "FROM " + dbprefix + "_users AS u1, " + dbprefix + "_vpaccount_users AS u2 ";
476 sql += "WHERE u1.uid = u2.uid AND ( u1.uid=" + string( unsignedIntToString( uids[ 0 ] ) );
477 for( int i = 1; i < uidsLen; i++ ){
478 sql += " OR u1.uid=" + string( unsignedIntToString( uids[ i ] ) );
479 }
480 sql += " ) ";
481 sql += criteria2str( cri );
482 if( mysql_query( mysql, sql.c_str( ) ) ){
483 fprintf( stderr, "%s\n", sql.c_str( ) );
484 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
485 return RES_DB_QUERY_ERROR;
486 }
487 result = mysql_use_result( mysql ) ;
488 *accountsLen=0;
489 for( int i = 0; ( row = mysql_fetch_row(result) ) != 0 && i < uidsLen ; i++ ){
490 dst[ i ].setUID( atoi( row[ 0 ] ) );
491 dst[ i ].setName( row[ 1 ] );
492 dst[ i ].setUname( row[ 2 ] );
493 dst[ i ].setEmail( row[ 3 ] );
494 dst[ i ].setURL( row[ 4 ] );
495 dst[ i ].setUserAvatar( row[ 5 ] );
496 dst[ i ].setUserRegdate( atoi( row[ 6 ] ) );
497 dst[ i ].setUserIcq( row[ 7 ] );
498 dst[ i ].setUserFrom( row[ 8 ] );
499 dst[ i ].setUserSig( row[ 9 ] );
500 dst[ i ].setUserViewemail( atoi( row[ 10 ] ) );
501 dst[ i ].setActkey( row[ 11 ] );
502 dst[ i ].setUserAim( row[ 12 ] );
503 dst[ i ].setUserYim( row[ 13 ] );
504 dst[ i ].setUserMsnm( row[ 14 ] );
505 dst[ i ].setPass( row[ 15 ] );
506 dst[ i ].setPosts( atoi( row[ 16 ] ) );
507 dst[ i ].setAttachsig( atoi( row[ 17 ] ) );
508 dst[ i ].setRank( atoi( row[ 18 ] ) );
509 dst[ i ].setLevel( atoi( row[ 19 ] ) );
510 dst[ i ].setTheme( row[ 20 ] );
511 dst[ i ].setTimezoneOffset( atof( row[ 21 ] ) );
512 dst[ i ].setLastLogin( atoi( row[ 22 ] ) );
513 dst[ i ].setUmode( row[ 23 ] );
514 dst[ i ].setUorder( atoi( row[ 24 ] ) );
515 dst[ i ].setNotifyMethod( atoi( row[ 25 ] ) );
516 dst[ i ].setNotifyMode( atoi( row[ 26 ] ) );
517 dst[ i ].setUserOcc( row[ 27 ] );
518 dst[ i ].setBio( row[ 28 ] );
519 dst[ i ].setUserIntrest( row[ 29 ] );
520 dst[ i ].setUserMailok( atoi( row[ 30 ] ) );
521 dst[ i ].setActivate( atoi( row[ 31 ] ) );
522 dst[ i ].setAddress( row[ 32 ] );
523 dst[ i ].setDivision( row[ 33 ] );
524 dst[ i ].setTel( row[ 34 ] );
525 dst[ i ].setCompanyName( row[ 35 ] );
526 dst[ i ].setCountry( row[ 36 ] );
527 dst[ i ].setZipcode( row[ 37 ] );
528 dst[ i ].setFax( row[ 38 ] );
529 dst[ i ].setBaseURL( row[ 39 ] );
530 dst[ i ].setNoticeMail( atoi( row[ 40 ] ) );
531 dst[ i ].setNoticeMailSince( atoi( row[ 41 ] ) );
532 ( *accountsLen )++;
533 }
534 while( row = mysql_fetch_row(result) );
535 mysql_free_result( result );
536 *accounts = dst;
537 return RES_OK;
538 }
539
540 /**
541 *
542 * ?≪?????潟???脂??
543 * ???若?倶???宴?????若?帥???若?鴻???脂?蚊???障??鐚?/span>
544 * ?脂?蚊???????若?倶???宴???綽????????若??D??uid???主????障??.
545 *
546 * @param sid ?祉???激?с??/span>
547 * @param account ?脂?蚊?????≪?????潟??????/span>
548 * @param uid ?脂?蚊?????≪?????潟?????宴???綽????????若??D
549 * @return RES_OK
550 * @return RES_NO_SUCH_SESSION
551 * @return RES_DB_QUERY_ERROR
552 * @return RES_ERROR
553 *
554 */
555 result_t insertAccount( sessionid_t sid, const account_t* account, userid_t* uid )
556 {
557 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
558 if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
559
560 string sql;
561 string tmp;
562 char buf[ 12 ];
563
564 //xoops?????若?吟???若???????吾??莨若??
565 sql = "INSERT INTO " + dbprefix + "_users (uname, name, email, url, user_avatar, user_regdate, user_icq, user_from, user_sig, user_viewemail, actkey, user_aim, user_yim, user_msnm, pass, posts, attachsig, rank, level, theme, timezone_offset, last_login, umode, uorder, notify_method, notify_mode, user_occ, bio, user_intrest, user_mailok) VALUES (";
566 sql += "'" + string( addSlashes( account -> getUname( ) ) ) + "', ";
567 sql += "'" + string( addSlashes( account -> getName( ) ) ) + "', ";
568 sql += "'" + string( addSlashes( account -> getEmail( ) ) ) + "', ";
569 sql += "'" + string( addSlashes( account -> getURL( ) ) ) + "', ";
570 sql += "'" + string( addSlashes( account -> getUserAvatar( ) ) ) + "', ";
571 snprintf( buf, 12, "%d", time( NULL ) );
572 sql += string( buf ) + ", ";
573 sql += "'" + string( addSlashes( account -> getUserIcq( ) ) ) + "', ";
574 sql += "'" + string( addSlashes( account -> getUserFrom( ) ) ) + "', ";
575 sql += "'" + string( addSlashes( account -> getUserSig( ) ) ) + "', ";
576 snprintf( buf, 12, "%d", account -> getUserViewemail( ) );
577 sql += string( buf ) + ", ";
578 sql += "'" + string( addSlashes( account -> getActkey( ) ) ) + "', ";
579 sql += "'" + string( addSlashes( account -> getUserAim( ) ) ) + "', ";
580 sql += "'" + string( addSlashes( account -> getUserYim( ) ) ) + "', ";
581 sql += "'" + string( addSlashes( account -> getUserMsnm( ) ) ) + "', ";
582 sql += "'" + string( addSlashes( account -> getPass( ) ) ) + "', ";
583 snprintf( buf, 12, "%d", account -> getPosts( ) );
584 sql += string( buf ) + ", ";
585 snprintf( buf, 12, "%d", account -> getAttachsig( ) );
586 sql += string( buf ) + ", ";
587 snprintf( buf, 12, "%d", account -> getRank( ) );
588 sql += string( buf ) + ", ";
589 snprintf( buf, 12, "%d", account -> getLevel( ) );
590 sql += string( buf ) + ", ";
591 sql += "'" + string( addSlashes( account -> getTheme( ) ) ) + "', ";
592 snprintf( buf, 12, "%lf", account -> getTimezoneOffset( ) );
593 sql += string( buf ) + ", ";
594 sql += "0, "; //last login
595 sql += "'" + string( addSlashes( account -> getUmode( ) ) ) + "', ";
596 snprintf( buf, 12, "%d", account -> getUorder( ) );
597 sql += string( buf ) + ", ";
598 snprintf( buf, 12, "%d", account -> getNotifyMethod( ) );
599 sql += string( buf ) + ", ";
600 snprintf( buf, 12, "%d", account -> getNotifyMode( ) );
601 sql += string( buf ) + ", ";
602 sql += "'" + string( addSlashes( account -> getUserOcc( ) ) ) + "', ";
603 sql += "'" + string( addSlashes( account -> getBio( ) ) ) + "', ";
604 sql += "'" + string( addSlashes( account -> getUserIntrest( ) ) ) + "', ";
605 snprintf( buf, 12, "%d", account -> getUserMailok( ) );
606 sql += string( buf ) + ")";
607 if( mysql_query( mysql, sql.c_str( ) ) ){
608 fprintf( stderr, "%s\n", sql.c_str( ) );
609 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
610 return RES_DB_QUERY_ERROR;
611 }
612
613 //???若??D????緇?????
614 *uid = mysql_insert_id( mysql );
615
616 //vpaccount?????若?吟???若??????????????宴???吾??莨若??
617 sql = "INSERT INTO " + dbprefix + "_vpaccount_users (uid, activate, address, division, tel, company_name, country, zipcode, fax, base_url, notice_mail, notice_mail_since) VALUES (";
618 snprintf( buf, 12, "%d", *uid );
619 sql += string( buf ) + ", ";
620 if( account -> getActivate() ){
621 sql += "1, ";
622 }else{
623 sql += "0, ";
624 }
625 sql += "'" + string( addSlashes( account -> getAddress() ) ) + "', ";
626 sql += "'" + string( addSlashes( account -> getDivision() ) ) + "', ";
627 sql += "'" + string( addSlashes( account -> getTel() ) ) + "', ";
628 sql += "'" + string( addSlashes( account -> getCompanyName() ) ) + "', ";
629 sql += "'" + string( addSlashes( account -> getCountry() ) ) + "', ";
630 sql += "'" + string( addSlashes( account -> getZipcode() ) ) + "', ";
631 sql += "'" + string( addSlashes( account -> getFax() ) ) + "', ";
632 sql += "'" + string( addSlashes( account -> getBaseURL() ) ) + "', ";
633 snprintf( buf, 12, "%d", account -> getNoticeMail( ) );
634 sql += string( buf ) + ", ";
635 snprintf( buf, 12, "%d", account -> getNoticeMailSince( ) );
636 sql += string( buf ) + ")";
637 if( mysql_query( mysql, sql.c_str( ) ) ){
638 fprintf( stderr, "%s\n", sql.c_str( ) );
639 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
640 //xoops_users??nsert???????潟?若???????ゃ????
641 sql = "DELETE FROM " + dbprefix + "_users where uid=";
642 snprintf( buf, 12, "%d", *uid );
643 sql += string( buf );
644 mysql_query( mysql, sql.c_str( ) );
645 return RES_DB_QUERY_ERROR;
646 }
647
648 return RES_OK;
649 }
650
651 /**
652 *
653 * ?≪?????潟?????宴??紊??眼????.
654 *
655 *
656 * @param sid ?祉???激?с??D
657 * @param account 紊??眼???????≪?????潟??????/span>
658 * @return RES_OK
659 * @return RES_NO_SUCH_SESSION
660 * @return RES_DB_QUERY_ERROR
661 * @return RES_NO_SUCH_USER
662 *
663 */
664 result_t updateAccount( sessionid_t sid, const account_t* account )
665 {
666 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
667 if( account == NULL ) return RES_ERROR;
668 if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
669 if( !uidExists( account -> getUID( ) ) ) return RES_NO_SUCH_USER;
670
671 string sql;
672 string tmp;
673 char buf[ 12 ];
674
675 //xoops?????若?吟???若???????吾??莨若??
676 sql = "UPDATE " + dbprefix + "_users SET ";
677 sql += "uname='" + string( addSlashes( account -> getUname( ) ) ) + "', ";
678 sql += "name='" + string( addSlashes( account -> getName( ) ) ) + "', ";
679 sql += "email='" + string( addSlashes( account -> getEmail( ) ) ) + "', ";
680 sql += "url='" + string( addSlashes( account -> getURL( ) ) ) + "', ";
681 sql += "user_avatar='" + string( addSlashes( account -> getUserAvatar( ) ) ) + "', ";
682 sql += "user_regdate=" + string( intToString( account -> getUserRegdate( ) ) ) + ", ";
683 sql += "user_icq='" + string( addSlashes( account -> getUserIcq( ) ) ) + "', ";
684 sql += "user_from='" + string( addSlashes( account -> getUserFrom( ) ) ) + "', ";
685 sql += "user_sig='" + string( addSlashes( account -> getUserSig( ) ) ) + "', ";
686 sql += "user_viewemail=" + string( intToString( account -> getUserViewemail( ) ) ) + ", ";
687 sql += "actkey='" + string( addSlashes( account -> getActkey( ) ) ) + "', ";
688 sql += "user_aim='" + string( addSlashes( account -> getUserAim( ) ) ) + "', ";
689 sql += "user_yim='" + string( addSlashes( account -> getUserYim( ) ) ) + "', ";
690 sql += "user_msnm='" + string( addSlashes( account -> getUserMsnm( ) ) ) + "', ";
691 sql += "pass='" + string( account -> getPass( ) ) + "', ";
692 sql += "posts=" + string( intToString( account -> getPosts( ) ) ) + ", ";
693 sql += "attachsig=" + string( intToString( account -> getAttachsig( ) ) ) + ", ";
694 sql += "rank=" + string( intToString( account -> getRank( ) ) ) + ", ";
695 sql += "level=" + string( intToString( account -> getLevel( ) ) ) + ", ";
696 sql += "theme='" + string( addSlashes( account -> getTheme( ) ) ) + "', ";
697 snprintf( buf, 12, "%.2f", account -> getTimezoneOffset( ) );
698 sql += "timezone_offset=" + string( buf ) + ", ";
699 sql += "last_login=" + string( unsignedIntToString( account -> getLastLogin( ) ) ) + ", ";
700 sql += "umode='" + string( account -> getUmode( ) ) + "', ";
701 sql += "uorder=" + string( intToString( account -> getUorder( ) ) ) + ", ";
702 sql += "notify_method=" + string( intToString( account -> getNotifyMethod( ) ) ) + ", ";
703 sql += "notify_mode=" + string( intToString( account -> getNotifyMode( ) ) ) + ", ";
704 sql += "user_occ='" + string( addSlashes( account -> getUserOcc( ) ) ) + "', ";
705 sql += "bio='" + string( addSlashes( account -> getBio( ) ) ) + "', ";
706 sql += "user_intrest='" + string( addSlashes( account -> getUserIntrest( ) ) ) + "', ";
707 sql += "user_mailok=" + string( intToString( account -> getUserMailok( ) ) );
708 sql += " WHERE uid = " + string( intToString( account -> getUID( ) ) );
709
710 if( mysql_query( mysql, sql.c_str( ) ) ){
711 fprintf( stderr, "%s\n", sql.c_str( ) );
712 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
713 return RES_DB_QUERY_ERROR;
714 }
715
716 //vpaccount?????若?吟???若??????????????宴??筝??吾??????
717 sql = "UPDATE " + dbprefix + "_vpaccount_users SET ";
718 sql += "activate=";
719 if( account -> getActivate() ){
720 sql += "1, ";
721 }else{
722 sql += "0, ";
723 }
724 sql += "address='" + string( addSlashes( account -> getAddress() ) ) + "', ";
725 sql += "division='" + string( addSlashes( account -> getDivision() ) ) + "', ";
726 sql += "tel='" + string( addSlashes( account -> getTel() ) ) + "', ";
727 sql += "company_name='" + string( addSlashes( account -> getCompanyName() ) ) + "', ";
728 sql += "country='" + string( addSlashes( account -> getCountry() ) ) + "', ";
729 sql += "zipcode='" + string( addSlashes( account -> getZipcode() ) ) + "', ";
730 sql += "fax='" + string( addSlashes( account -> getFax() ) ) + "', ";
731 sql += "base_url='" + string( addSlashes( account -> getBaseURL() ) ) + "', ";
732 sql += "notice_mail=" + string( intToString( account -> getNoticeMail( ) ) ) + ", ";
733 sql += "notice_mail_since=" + string( intToString( account -> getNoticeMailSince( ) ) );
734 sql += " WHERE uid = " + string( intToString( account -> getUID( ) ) );
735
736 if( mysql_query( mysql, sql.c_str( ) ) ){
737 fprintf( stderr, "%s\n", sql.c_str( ) );
738 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
739 return RES_DB_QUERY_ERROR;
740 }
741
742 return RES_OK;
743 }
744
745 /**
746 *
747 * criteria_t?ф??絎???????膀??蚊?????若??D??菴???.
748 * ???若??D????????腆坂????????????吾??莨若?随????????≪?????鴻??*uids???吾??莨若??.
749 * ???????泣?ゃ?冴??*uidsLen???吾??莨若??
750 *
751 * @param sid ?祉???激?с??D
752 * @param cri 腟???????我??絎?鐚??純?若???>散??絎?
753 * @param uids ???若?吟??ID??????
754 * @param uidsLen uids???????膣???/span>
755 * @return RES_OK| RES_DB_NOT_INITIALIZED| RES_NO_SUCH_SESSION
756 * @return RES_DB_QUERY_ERROR| RES_ERROR
757 *
758 */
759 result_t dumpUids( sessionid_t sid, criteria_t* cri, userid_t** uids, int* uidsLen )
760 {
761 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
762 if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
763
764 string sql;
765 MYSQL_RES* result;
766 MYSQL_ROW row;
767 userid_t* dst = 0;
768
769 sql = "SELECT uid FROM " + dbprefix + "_vpaccount_users ";
770 sql += criteria2str( cri );
771 if( mysql_query( mysql, sql.c_str( ) ) ){
772 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
773 return RES_DB_QUERY_ERROR;
774 }
775 result = mysql_store_result( mysql ) ;
776 if( result == NULL ){
777 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
778 return RES_DB_QUERY_ERROR;
779 }
780 *uidsLen = mysql_num_rows( result );
781 dst = new userid_t[ *uidsLen ];
782 for( int i = 0; ( row = mysql_fetch_row(result) ) != 0 && i < *uidsLen ; i++ ){
783 dst[ i ] = atoi( row[ 0 ] );
784 }
785 mysql_free_result( result );
786 *uids = dst;
787
788 return RES_OK;
789 }
790
791 /**
792 *
793 * ?違???若???違??菴???.
794 * 筝?罩c???祉???激?с??D????0??菴???.
795 *
796 * @param sid ?祉???激?с??D
797 * @return ?違???若????/span>
798 *
799 */
800 int getGroupCount( sessionid_t sid )
801 {
802 if( mysql == NULL ) return 0;
803 if( !isValidSessionID( sid ) ) return 0;
804
805 string sql;
806 MYSQL_RES* result;
807 MYSQL_ROW row;
808 int groupLen = 0;
809
810 //?違???若???違??羆?????
811 sql = "SELECT COUNT(*) FROM " + dbprefix + "_vpaccount_groups";
812 if( mysql_query( mysql, sql.c_str( ) ) ){
813 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
814 return RES_DB_QUERY_ERROR;
815 }
816 result = mysql_store_result( mysql ) ;
817 if( row = mysql_fetch_row(result) ){
818 groupLen = atoi( row[ 0 ] );
819 }
820 mysql_free_result( result );
821 return groupLen;
822 }
823
824 /**
825 *
826 * ??絮??違???若??筝?荀у??
827 *
828 * @param
829 * @return
830 *
831 */
832 result_t getGroupsByUid( sessionid_t sid, userid_t uid, criteria_t* cri, groupid_t** gids, int* gidsLen )
833 {
834 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
835 if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
836 if( !uidExists( uid ) ) return RES_NO_SUCH_USER; //uid??????????с????/span>
837
838 string sql;
839 MYSQL_RES* result;
840 MYSQL_ROW row;
841 groupid_t* dst = 0;
842 int len = 0;
843
844 //??絮??????違???若??????紊у???違??羆?????
845 sql = "SELECT count(*) FROM " + dbprefix + "_vpaccount_groups_users_link";
846 sql += " WHERE uid=" + string( unsignedIntToString( uid ) );
847 sql += criteria2str( cri );
848 if( mysql_query( mysql, sql.c_str( ) ) ){
849 fprintf( stderr, "%s\n", sql.c_str( ) );
850 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
851 return RES_DB_QUERY_ERROR;
852 }
853 result = mysql_store_result( mysql ) ;
854 if( row = mysql_fetch_row(result) ){
855 len = atoi( row[ 0 ] );
856 }
857 mysql_free_result( result );
858
859 sql = "SELECT gid, uid, is_admin FROM " + dbprefix + "_vpaccount_groups_users_link";
860 sql += " WHERE uid=" + string( unsignedIntToString( uid ) );
861 sql += criteria2str( cri );
862 if( mysql_query( mysql, sql.c_str( ) ) ){
863 fprintf( stderr, "%s\n", sql.c_str( ) );
864 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
865 return RES_DB_QUERY_ERROR;
866 }
867 *gidsLen = 0;
868 dst = new groupid_t[ len ];
869 result = mysql_store_result( mysql ) ;
870 for( int i = 0; ( row = mysql_fetch_row(result) ) != 0 && i < len ; i++ ){
871 dst[ i ] = atoi( row[ 0 ] );
872 ( *gidsLen )++;
873 }
874 mysql_free_result( result );
875 *gids = dst;
876
877 return RES_OK;
878 }
879
880 /**
881 *
882 * ?違???若??膊∞??罔????????
883 *
884 * @param
885 * @return
886 *
887 */
888 bool isGroupAdmin( sessionid_t sid, groupid_t gid, userid_t uid )
889 {
890 if( mysql == NULL ) return false;
891 if( !isValidSessionID( sid ) ) return false;
892 if( !uidExists( uid ) ) return false;
893 if( !gidExists( gid ) ) return false;
894
895 string sql;
896 MYSQL_RES* result;
897 MYSQL_ROW row;
898 groupid_t* dst = 0;
899 result_t res;
900
901 sql = "SELECT * FROM " + dbprefix + "_vpaccount_groups_users_link ";
902 sql += "WHERE gid=" + string( unsignedIntToString( gid ) );
903 sql += " AND uid=" + string( unsignedIntToString( uid ) );
904 sql += " AND is_admin=1";
905 if( mysql_query( mysql, sql.c_str( ) ) ){
906 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
907 return false;
908 }
909 result = mysql_store_result( mysql );
910 if( !mysql_fetch_row(result) ){
911 mysql_free_result( result );
912 return false;
913 }
914
915 mysql_free_result( result );
916 return true;;
917 }
918
919 /**
920 *
921 * ?違???若??ID筝?荀?/span>
922 *
923 * @param
924 * @return
925 *
926 */
927 result_t dumpGids( sessionid_t sid, criteria_t* cri, groupid_t** gids, int* gidsLen )
928 {
929 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
930 if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
931
932 string sql;
933 MYSQL_RES* result;
934 MYSQL_ROW row;
935 groupid_t* dst = 0;
936
937 sql = "SELECT gid FROM " + dbprefix + "_vpaccount_groups ";
938 sql += criteria2str( cri );
939 if( mysql_query( mysql, sql.c_str( ) ) ){
940 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
941 return RES_DB_QUERY_ERROR;
942 }
943 result = mysql_store_result( mysql ) ;
944 if( result == NULL ){
945 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
946 return RES_DB_QUERY_ERROR;
947 }
948 *gidsLen = mysql_num_rows( result );
949 dst = new groupid_t[ *gidsLen ];
950 for( int i = 0; ( row = mysql_fetch_row(result) ) != 0 && i < *gidsLen ; i++ ){
951 dst[ i ] = atoi( row[ 0 ] );
952 }
953 mysql_free_result( result );
954 *gids = dst;
955
956 return RES_OK;
957 /*
958 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
959 if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
960
961 string sql;
962 MYSQL_RES* result;
963 MYSQL_ROW row;
964 groupid_t* dst = 0;
965
966 //?違???若???違??羆?????
967 sql = "SELECT COUNT(*) FROM " + dbprefix + "_vpaccount_groups ";
968 sql += criteria2str( cri );
969 if( mysql_query( mysql, sql.c_str( ) ) ){
970 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
971 return RES_DB_QUERY_ERROR;
972 }
973 result = mysql_store_result( mysql ) ;
974 if( row = mysql_fetch_row(result) ){
975 *gidsLen = atoi( row[ 0 ] );
976 dst = new groupid_t[ *gidsLen ];
977 }else{
978 mysql_free_result( result );
979 return RES_ERROR;
980 }
981 mysql_free_result( result );
982
983 sql = "SELECT gid, gname, gdesc FROM " + dbprefix + "_vpaccount_groups ";
984 sql += criteria2str( cri );
985 if( mysql_query( mysql, sql.c_str( ) ) ){
986 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
987 return RES_DB_QUERY_ERROR;
988 }
989
990 result = mysql_use_result( mysql ) ;
991 for( int i = 0; ( row = mysql_fetch_row(result) ) != 0 && i < *gidsLen ; i++ ){
992 dst[ i ] = atoi( row[ 0 ] );
993 }
994 mysql_free_result( result );
995 *gids = dst;
996
997 return RES_OK;
998 */
999 }
1000
1001 /**
1002 *
1003 * ?違???若??膊∞????ID????緇?????
1004 *
1005 * @param sid ?祉???激?с??D
1006 * @param gid ???????????違???若????ID?ф??絎?????
1007 * @param cri 腟???????我??絎?鐚??純?若???>散??絎?
1008 * @param uids 膊∞??????ID???????????????????ゃ?潟??/span>
1009 * @param uidsLen uids???????膣???/span>
1010 * @return RES_OK
1011 * @return RES_DB_NOT_INITIALIZED
1012 * @return RES_NO_SUCH_SESSION
1013 * @return RES_DB_QUERY_ERROR
1014 * @return RES_ERROR
1015 *
1016 */
1017 result_t dumpGroupAdmins( sessionid_t sid, groupid_t gid, criteria_t* cri, userid_t** uids, int* uidsLen )
1018 {
1019 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
1020 if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
1021
1022 string sql;
1023 MYSQL_RES* result;
1024 MYSQL_ROW row;
1025 userid_t* dst = 0;
1026
1027 sql = "SELECT uid FROM " + dbprefix + "_vpaccount_groups_users_link ";
1028 sql += " WHERE is_admin=1";
1029 if( mysql_query( mysql, sql.c_str( ) ) ){
1030 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
1031 return RES_DB_QUERY_ERROR;
1032 }
1033
1034 result = mysql_store_result( mysql );
1035 if( result == NULL ){
1036 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
1037 return RES_DB_QUERY_ERROR;
1038 }
1039
1040 *uidsLen = mysql_num_rows( result );
1041 dst = new userid_t[ *uidsLen ];
1042 for( int i = 0; ( row = mysql_fetch_row(result) ) != 0 && i < *uidsLen ; i++ ){
1043 dst[ i ] = atoi( row[ 0 ] );
1044 }
1045 mysql_free_result( result );
1046 *uids = dst;
1047
1048 return RES_OK;
1049 }
1050
1051 /**
1052 *
1053 * ?違???若????絮????若?九????/span>
1054 *
1055 * @param
1056 * @return
1057 *
1058 */
1059 result_t deleteMember( sessionid_t sid, groupid_t gid, userid_t uid )
1060 {
1061 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
1062 if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
1063 if( !uidExists( uid ) ) return RES_NO_SUCH_USER; //uid??????????с????/span>
1064 if( !gidExists( gid ) ) return RES_NO_SUCH_GROUP; //gid??????????с????/span>
1065
1066 string sql;
1067
1068 sql = "DELETE FROM " + dbprefix + "_vpaccount_groups_users_link ";
1069 sql += "WHERE gid=" + string( unsignedIntToString( gid ) );
1070 sql += " AND uid=" + string( unsignedIntToString( uid ) );
1071 if( mysql_query( mysql, sql.c_str( ) ) ){
1072 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
1073 return RES_DB_QUERY_ERROR;
1074 }
1075 return RES_OK;
1076 }
1077
1078 /**
1079 *
1080 * ?違???若????絮????若?区申??
1081 *
1082 * @param sid ?祉???激?с??D
1083 * @param gid ??絮????違???若????D
1084 * @param uid ??絮??????????若?吟??D
1085 * @param admin 膊∞????罔?????筝?????????true
1086 * @return RES_OK
1087 * @return RES_DB_NOT_INITIALIZED
1088 * @return RES_NO_SUCH_SESSION
1089 * @return RES_DB_QUERY_ERROR
1090 * @return RES_ERROR
1091 *
1092 */
1093 result_t insertMember( sessionid_t sid, groupid_t gid, userid_t uid, bool admin )
1094 {
1095 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
1096 if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
1097 if( !uidExists( uid ) ) return RES_NO_SUCH_USER; //uid??????????с????/span>
1098 if( !gidExists( gid ) ) return RES_NO_SUCH_GROUP; //gid??????????с????/span>
1099
1100 string sql;
1101 MYSQL_RES* result;
1102 MYSQL_ROW row;
1103 groupid_t* dst = 0;
1104 result_t res;
1105
1106 //?<?潟???若??菴遵??
1107 sql = "INSERT INTO " + dbprefix + "_vpaccount_groups_users_link ( gid, uid, is_admin ) VALUES (";
1108 sql += string( unsignedIntToString( gid ) ) + ", ";
1109 sql += string( unsignedIntToString( uid ) ) + ", ";
1110 sql += string( admin ? "1" : "0" ) + ") ";
1111 if( mysql_query( mysql, sql.c_str( ) ) ){
1112 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
1113 return RES_DB_QUERY_ERROR;
1114 }
1115 return RES_OK;
1116 }
1117
1118 /**
1119 *
1120 * ?違???若????絮????若?九??
1121 *
1122 * @param
1123 * @return
1124 *
1125 */
1126 result_t getMembers( sessionid_t sid, groupid_t gid, criteria_t* cri, userid_t** uids, int* uidsLen )
1127 {
1128 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
1129 if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
1130 if( !gidExists( gid ) ) return RES_NO_SUCH_GROUP; //gid??????????с????/span>
1131
1132 string sql;
1133 MYSQL_RES* result;
1134 MYSQL_ROW row;
1135 userid_t* dst = 0;
1136 int len = 0;
1137
1138 //?<?潟???惹?違??羆??????<?≪??∈篆?
1139 sql = "SELECT COUNT(*) FROM " + dbprefix + "_vpaccount_groups_users_link ";
1140 sql += " WHERE gid=" + string( unsignedIntToString( gid ) );
1141 if( mysql_query( mysql, sql.c_str( ) ) ){
1142 fprintf( stderr, "%s\n", sql.c_str( ) );
1143 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
1144 return RES_DB_QUERY_ERROR;
1145 }
1146 result = mysql_store_result( mysql ) ;
1147 if( row = mysql_fetch_row(result) ){
1148 len = atoi( row[ 0 ] );
1149 dst = new userid_t[ len ];
1150 }else{
1151 mysql_free_result( result );
1152 return RES_ERROR;
1153 }
1154 mysql_free_result( result );
1155
1156 sql = "SELECT uid, gid, is_admin FROM " + dbprefix + "_vpaccount_groups_users_link ";
1157 sql += " WHERE gid=" + string( unsignedIntToString( gid ) );
1158 sql += " " + string( criteria2str( cri ) );
1159 if( mysql_query( mysql, sql.c_str( ) ) ){
1160 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
1161 delete[] dst;
1162 *uidsLen = 0;
1163 return RES_DB_QUERY_ERROR;
1164 }
1165 *uidsLen = 0;
1166 result = mysql_use_result( mysql ) ;
1167 for( int i = 0; ( row = mysql_fetch_row(result) ) != 0 && i < len ; i++ ){
1168 dst[ i ] = atoi( row[ 0 ] );
1169 ( *uidsLen )++;
1170 }
1171 while( row = mysql_fetch_row(result) );
1172 mysql_free_result( result );
1173 *uids = dst;
1174 return RES_OK;
1175 }
1176
1177 /**
1178 *
1179 * ?違???若????????/span>
1180 *
1181 * @param
1182 * @return RES_OK
1183 * @return RES_DB_QUERY_ERROR
1184 * @return RES_NO_SUCH_SESSION
1185 * @return RES_DB_NOT_INITIALIZED
1186 *
1187 */
1188 result_t deleteGroup( sessionid_t sid, groupid_t gid )
1189 {
1190 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
1191 if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
1192
1193 string sql;
1194 sql = "DELETE FROM " + dbprefix + "_vpaccount_groups ";
1195 sql += "WHERE gid = " + string( unsignedIntToString( gid ) );
1196 if( mysql_query( mysql, sql.c_str( ) ) ){
1197 fprintf( stderr, "%s\n", sql.c_str( ) );
1198 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
1199 return RES_DB_QUERY_ERROR;
1200 }
1201 return RES_OK;
1202 }
1203
1204 /**
1205 *
1206 * ?違???若???脂??
1207 * ?違???若???????宴?????若?帥???若?鴻????蚊???障??.
1208 * ?脂?蚊?????違???若?????綽??????違???若??ID??gid???主????障??.
1209 *
1210 * @param sid ?祉???激?с??D
1211 * @param group ?脂?蚊???????違???若????????/span>
1212 * @param gid ?脂?蚊?????違???若?????綽??????違???若??ID
1213 * @return RES_OK
1214 * @return RES_DB_NOT_INITIALIZED
1215 * @return RES_NO_SUCH_SESSION
1216 * @return RES_DB_QUERY_ERROR
1217 *
1218 */
1219 result_t insertGroup( sessionid_t sid, const group_t* group, groupid_t* gid )
1220 {
1221 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
1222 if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
1223
1224 string sql;
1225 sql = "INSERT INTO " + dbprefix + "_vpaccount_groups ( gname, gdesc ) VALUES (";
1226 sql += "'" + string( addSlashes( group -> getGname( ) ) ) + "', ";
1227 sql += "'" + string( addSlashes( group -> getDesc( ) ) ) + "' )";
1228 if( mysql_query( mysql, sql.c_str( ) ) ){
1229 fprintf( stderr, "%s\n", sql.c_str( ) );
1230 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
1231 return RES_DB_QUERY_ERROR;
1232 }
1233 *gid = mysql_insert_id( mysql );
1234 return RES_OK;
1235 }
1236
1237 /**
1238 *
1239 * ?違???若?????宴??紊??眼????
1240 *
1241 * @param sid ?祉???激?с??D
1242 * @param group ?違?????違???若??????/span>
1243 * @return RES_OK
1244 * @return RES_ERROR
1245 * @return RES_DB_NOT_INITIALIZED
1246 * @return RES_NO_SUCH_SESSION
1247 * @return RES_NO_SUCH_GROUP
1248 * @return RES_DB_QUERY_ERROR
1249 *
1250 */
1251 result_t updateGroup( sessionid_t sid, const group_t* group )
1252 {
1253 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
1254 if( group == NULL ) return RES_ERROR;
1255 if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
1256 if( !gidExists( group -> getGID( ) ) ) return RES_NO_SUCH_GROUP;
1257
1258 string sql;
1259 sql = "UPDATE " + dbprefix + "_vpaccount_groups SET ";
1260 sql += "gname='" + string( addSlashes( group -> getGname( ) ) ) + "', ";
1261 sql += "gdesc='" + string( addSlashes( group -> getDesc( ) ) ) + "' ";
1262 sql += "WHERE gid=" + string( unsignedIntToString( group -> getGID( ) ) );
1263 if( mysql_query( mysql, sql.c_str( ) ) ){
1264 fprintf( stderr, "%s\n", sql.c_str( ) );
1265 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
1266 return RES_DB_QUERY_ERROR;
1267 }
1268 return RES_OK;
1269 }
1270
1271 /**
1272 *
1273 * ?違???若?????怨??
1274 *
1275 * @param sid ?祉???激?с??D
1276 * @param gid ??緇????????違???若????ID
1277 * @param group ??緇??????違???若?????宴???≪?????鴻???吾??莨若?????ゃ?潟??/span>
1278 * @return RES_OK
1279 * @return RES_DB_NOT_INITIALIZED
1280 * @return RES_NO_SUCH_SESSION
1281 * @return RES_NO_SUCH_GROUP
1282 * @return RES_DB_QUERY_ERROR
1283 *
1284 */
1285 result_t getGroup( sessionid_t sid, groupid_t gid, const group_t** group )
1286 {
1287 int len;
1288 static criteria c;
1289 result_t res = getGroups( sid, &gid, 1, &c, group, &len );
1290 if( len == 0 ) return RES_NO_SUCH_GROUP;
1291 return res;
1292 }
1293
1294 /**
1295 *
1296 * 茲??違???違???若?????宴??筝?綺?????????.
1297 *
1298 * @param sid ?祉???激?с??D
1299 * @param gids ??緇????????違???若????ID??????
1300 * @param gidsLen gids???????膣???/span>
1301 * @param cri 腟???????膀??駕??純?若???>散????絎?
1302 * @param groups ??緇?腟??????違???若?????宴?????????≪?????鴻???吾??莨若?????ゃ?潟??/span>
1303 * @param groupsLen ??緇?腟????????????膣???/span>
1304 * @return RES_OK
1305 * @return RES_DB_NOT_INITIALIZED
1306 * @return RES_NO_SUCH_SESSION
1307 * @return RES_DB_QUERY_ERROR
1308 *
1309 */
1310 result_t getGroups( sessionid_t sid, groupid_t* gids, int gidsLen, criteria_t* cri, const group_t** groups, int* groupsLen )
1311 {
1312 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
1313 if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
1314
1315 string sql;
1316 MYSQL_RES* result;
1317 MYSQL_ROW row;
1318 group_t* dst = new group_t[ gidsLen ];
1319
1320 sql += "SELECT gid, gname, gdesc ";
1321 sql += "FROM " + dbprefix + "_vpaccount_groups ";
1322 sql += "WHERE gid=" + string( unsignedIntToString( gids[ 0 ] ) );
1323 for( int i = 1; i < gidsLen; i++ ){
1324 sql += " OR gid=" + string( unsignedIntToString( gids[ i ] ) );
1325 }
1326 sql += criteria2str( cri );
1327 if( mysql_query( mysql, sql.c_str( ) ) ){
1328 fprintf( stderr, "%s\n", sql.c_str( ) );
1329 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
1330 return RES_DB_QUERY_ERROR;
1331 }
1332 result = mysql_use_result( mysql ) ;
1333 *groupsLen=0;
1334 for( int i = 0; ( row = mysql_fetch_row(result) ) != 0 && i < gidsLen ; i++ ){
1335 dst[ i ].setGID( atoi( row[ 0 ] ) );
1336 dst[ i ].setGname( row[ 1 ] );
1337 dst[ i ].setDesc( row[ 2 ] );
1338 ( *groupsLen )++;
1339 }
1340 while( row = mysql_fetch_row(result) );
1341 mysql_free_result( result );
1342 *groups = dst;
1343 return RES_OK;
1344 }
1345
1346 /**
1347 *
1348 * ?≪?????若?炊┤???????<??菴???
1349 *
1350 * @param sid ?祉???激?с??D
1351 * @param uid ???????????????若?吟??ID
1352 * @return true 罔???????
1353 * @return false 罔???????
1354 *
1355 */
1356 bool isModerator( sessionid_t sid, userid_t uid )
1357 {
1358 if( mysql == NULL ) return false;
1359 if( !isValidSessionID( sid ) ) return false;
1360 //if( !uidExists( uid ) ) return false;
1361
1362 string sql;
1363 MYSQL_RES* result;
1364 MYSQL_ROW row;
1365 groupid_t* dst = 0;
1366 result_t res;
1367
1368 sql = "SELECT value FROM " + dbprefix + "_vpaccount_config";
1369 sql += " WHERE name='moderator_gid'";
1370 if( mysql_query( mysql, sql.c_str( ) ) ){
1371 fprintf( stderr, "%s\n", sql.c_str( ) );
1372 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
1373 return false;
1374 }
1375 result = mysql_store_result( mysql ) ;
1376 if( result == NULL ){
1377 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
1378 return false;
1379 }
1380 if( row = mysql_fetch_row(result) ){
1381 sql = "SELECT * from " + dbprefix + "_groups_users_link ";
1382 sql += "WHERE groupid=" + string( row[ 0 ] );
1383 sql += " AND uid=" + string( unsignedIntToString( uid ) );
1384 mysql_free_result( result );
1385
1386 if( mysql_query( mysql, sql.c_str( ) ) ){
1387 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
1388 return false;
1389 }
1390 result = mysql_store_result( mysql );
1391 if( result == NULL ){
1392 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
1393 return false;
1394 }
1395 if( !mysql_fetch_row(result) ){
1396 mysql_free_result( result );
1397 return false;
1398 }
1399 mysql_free_result( result );
1400 return true;
1401 }else{
1402 mysql_free_result( result );
1403 return false;
1404 }
1405 }
1406
1407 /**
1408 *
1409 * ???若??D??緇?
1410 *
1411 * @param uname ID????緇??????????若?吟?弱??
1412 * @param uid ID??篁e?ャ??????紊??違?????ゃ?潟??/span>
1413 * @return RES_ERROR
1414 * @return RES_OK
1415 * @refer result_t
1416 *
1417 */
1418 result_t getUid( const char* uname, userid_t* uid )
1419 {
1420 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
1421 if( uname == NULL ) return RES_ERROR;
1422
1423 string sql;
1424 string uname2 = addSlashes( uname );
1425 sql = "SELECT uid FROM " + dbprefix + "_users WHERE uname='" + uname2 + "';";
1426 if( mysql_query( mysql, sql.c_str( ) ) ){
1427 fprintf( stderr, "%d %s\n", mysql_errno(mysql), mysql_error(mysql) );
1428 return RES_DB_QUERY_ERROR;
1429 }
1430 MYSQL_RES* result = mysql_store_result( mysql ) ;
1431 MYSQL_ROW row = mysql_fetch_row(result);
1432 if( row ){
1433 *uid = atoi( row[0] );
1434 mysql_free_result( result );
1435 return RES_OK;
1436 }else{
1437 mysql_free_result( result );
1438 return RES_NO_SUCH_USER;
1439 }
1440 }
1441
1442 /**
1443 *
1444 * ?祉???激?с?潟??篏?????xoops_vpaccount_session???吾??莨若???? sessionid_t??session???????
1445 *
1446 * @param uid xoops_users??id
1447 * @param session sessionid_t???????????????????ゃ?潟?帥??
1448 * @return RES_OK ????
1449 * @return RES_DB_QUERY_ERROR DB????????筝?????????/span>
1450 */
1451 static result_t addSession( userid_t uid, sessionid_t* session )
1452 {
1453 result_t ret;
1454 string sql = "INSERT INTO " + dbprefix + "_vpaccount_session (uid) values (" +
1455 unsignedIntToString(uid) + ")";
1456 if ( 0 == mysql_query( mysql, sql.c_str() ) ){
1457 *session = (int)mysql_insert_id(mysql);
1458 ret = RES_OK;
1459 }
1460 else {
1461 ret = RES_DB_QUERY_ERROR;
1462 }
1463 return ret;
1464 }
1465
1466
1467 /**
1468 *
1469 * ???違?ゃ?活?荐若???祉???激?с?割???
1470 *
1471 * @param uname ???違?ゃ?喝??
1472 * @param passwd ???鴻???若??
1473 * @param session sessionid_t???????????????????ゃ?潟?帥??
1474 * @return RES_OK ???違?ゃ?恰??????session??essionid_t???吾??莨若??????<br>
1475 * @return RES_LOGIN_FAILURE uname?障????asswd???医幻<br>
1476 * @return RES_DB_QUERY_ERROR DB????????筝?????????/span>
1477 *
1478 */
1479 result_t loginUser(const char* uname, const char* passwd, sessionid_t* session )
1480 {
1481 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
1482
1483 result_t ret;
1484 string sql;
1485
1486 // uname, passwd -> uid
1487 string escUname = addSlashes( uname );
1488 string escPasswd = addSlashes( passwd );
1489 sql = "SELECT uid FROM " + dbprefix + "_users WHERE uname='" + escUname + "' and pass=md5('" + escPasswd + "')";
1490 if( 0 == mysql_query( mysql, sql.c_str( ) ) ){
1491 MYSQL_RES* result = mysql_store_result( mysql );
1492 if( result ){
1493 MYSQL_ROW row = mysql_fetch_row(result);
1494 if( row ){
1495 userid_t uid = atoi(row[0]);
1496 ret = addSession( uid, session );
1497 }else{
1498 ret = RES_LOGIN_FAILURE;
1499 }
1500 mysql_free_result( result );
1501 }else {
1502 ret = RES_DB_QUERY_ERROR;
1503 }
1504 }else{
1505 ret = RES_DB_QUERY_ERROR;
1506 }
1507
1508 return ret;
1509 }
1510
1511 /**
1512 *
1513 * ???違?≪???????祉???激?с?括?篋?
1514 *
1515 * @param sid session id
1516 * @return ????
1517 *
1518 */
1519 void logoutUser( sessionid_t sid )
1520 {
1521 if( mysql == NULL ) return; // RES_DB_NOT_INITIALIZED;
1522
1523 result_t ret;
1524 string sql;
1525
1526 sql = "DELETE FROM " + dbprefix + "_vpaccount_session WHERE sid=" + intToString((int)sid);
1527 if ( 0 == mysql_query( mysql, sql.c_str() ) ){
1528 if ( mysql_affected_rows(mysql) == 1 ){
1529 // ???違?≪????????
1530 }
1531 else {
1532 // sid???≦??/span>
1533 }
1534 }
1535 else {
1536 // DB????????筝?????????/span>
1537 }
1538 }
1539
1540 /**
1541 *
1542 * ?祉???激?с?潟?????.<br>
1543 * XOOPS??ession???若????????蚊??????sess_id?????ess_id??膈???????
1544 * ?逸?session_t??篏?????????
1545 * ???若?帥???若?鴻???祉???激?с?潟????違?????宴??荐??蚊????
1546 *
1547 * @param sessionid xoops??ession_id
1548 * @param uid xoops_users??id
1549 * @param session sessionid_t???????????????????ゃ?潟?帥??
1550 * @return RES_OK ??????<br>
1551 * @return RES_ERROR
1552 * @return RES_DB_QUERY_ERROR
1553 *
1554 */
1555 result_t createSession( const char* sess_id, userid_t uid, sessionid_t* session )
1556 {
1557 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
1558
1559 result_t ret;
1560 string sql;
1561
1562 string escSess_id = addSlashes(sess_id);
1563 sql = "SELECT sess_id from " + dbprefix + "_session where sess_id='" + escSess_id + "'";
1564 if( 0 == mysql_query( mysql, sql.c_str( ) ) ){
1565 MYSQL_RES* result = mysql_store_result( mysql );
1566 if( result ){
1567 MYSQL_ROW row = mysql_fetch_row(result);
1568 if( row ){
1569 ret = addSession( uid, session );
1570 }else{
1571 ret = RES_ERROR;
1572 }
1573 mysql_free_result( result );
1574 }else {
1575 ret = RES_DB_QUERY_ERROR;
1576 }
1577 }else{
1578 ret = RES_DB_QUERY_ERROR;
1579 }
1580
1581 return ret;
1582 }
1583
1584 /**
1585 *
1586 * ?祉???激?с?潟???膣違??session_t?у??????.<br>
1587 *
1588 * @param sid ?祉???激?с??D
1589 * @param ppsession sessionid_t*???????????????????ゃ?潟?帥??
1590 * @return RES_OK ??????freeResult(*ppsession)??綽?荀???<br>
1591 * @return RES_NO_SUCH_SESSION<br>
1592 * @return RES_DB_QUERY_ERROR<br>
1593 * @return RES_DB_NOT_INITIALIZED
1594 *
1595 */
1596 result_t getSession( sessionid_t sid, const session_t** ppsession )
1597 {
1598 if( mysql == NULL ) return RES_DB_NOT_INITIALIZED;
1599
1600 result_t ret;
1601 string sql;
1602
1603 sql = "SELECT sid, uid, unix_timestamp(timestamp) from " + dbprefix +
1604 "_vpaccount_session where sid=" + unsignedIntToString((unsigned int)sid);
1605 if( 0 == mysql_query( mysql, sql.c_str( ) ) ){
1606 MYSQL_RES* result = mysql_store_result( mysql );
1607 if( result ){
1608 MYSQL_ROW row = mysql_fetch_row(result);
1609 if( row ){
1610 session_t *p = new session[1];
1611 p->setSessionID(sid);
1612 p->setUserID(atoi(row[1]));
1613 p->setDate(atoi(row[2]));
1614 *ppsession = p;
1615 ret = RES_OK;
1616 }else{
1617 ret = RES_NO_SUCH_SESSION;
1618 }
1619 mysql_free_result( result );
1620 }else {
1621 ret = RES_DB_QUERY_ERROR;
1622 }
1623 }else{
1624 ret = RES_DB_QUERY_ERROR;
1625 }
1626
1627 return ret;
1628 }
1629
1630 /**
1631 *
1632 * ?祉???激?с?潟???綵??с?????с????????.
1633 *
1634 * @param sid ???с??????????session
1635 * @return true 罩e?
1636 * @return false 筝?罩?/span>
1637 *
1638 */
1639 bool isValidSessionID( sessionid_t sid )
1640 {
1641 if( mysql == NULL ) return false;
1642
1643 string sql;
1644 string sidstr;
1645 char buf[ 12 ];
1646 snprintf( buf, 12, "%d", sid );
1647
1648 sql = "SELECT * FROM " + dbprefix + "_vpaccount_session WHERE sid=" + string( buf );
1649 if( mysql_query( mysql, sql.c_str( ) ) ){
1650 return false;
1651 }
1652 MYSQL_RES* result = mysql_store_result( mysql ) ;
1653 MYSQL_ROW row = mysql_fetch_row(result);
1654 mysql_free_result( result );
1655 if( row ){
1656 return true;
1657 }else{
1658 return false;
1659 }
1660 }
1661
1662 void freeResult( const account_t* ptr ){ delete[] ( account_t* )ptr; }
1663 void freeResult( const group_t* ptr ){ delete[] ( group_t* )ptr; }
1664 void freeResult( const session_t* ptr ){ delete[] ( session_t* )ptr; }
1665 void freeResult( const int* ptr ){ delete[] ( int* )ptr; }
1666
1667 static string errstr;
1668 const char* getLastErrorString()
1669 {
1670 return errstr.c_str( );
1671 }
1672
1673 void setLastErrorString( const char* str )
1674 {
1675 errstr = str;
1676 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26