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.26 - (show annotations) (download) (as text)
Mon Dec 6 10:17:36 2004 UTC (19 years, 4 months ago) by youi
Branch: MAIN
Changes since 1.25: +8 -1 lines
File MIME type: text/x-c++src
mysql_use_resultの取得結果を残さず取り出すため,
while( row = mysql_fetch_row(result) );を挿入.

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

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