Develop and Download Open Source Software

Browse CVS Repository

Diff of /xoonips/AL/commonal.cc

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

revision 1.43 by aga, Wed Jan 19 01:42:15 2005 UTC revision 1.44 by youi, Wed Jan 19 06:01:59 2005 UTC
# Line 2  Line 2 
2   *   *
3   * $Revision$   * $Revision$
4   * $Log$   * $Log$
5     * Revision 1.44  2005/01/19 06:01:59  youi
6     * 関数追加
7     *  getConfigValue
8     *  setConfigValue
9     *  getChangeLogs
10     *  insertChangeLog
11     *  getItemIDByBinderID
12     *  registerBinderItem
13     *  unregisterBinderItem
14     *  getIndexPermission
15     *  freeString
16     *  freeChangeLog
17     * 引数にcriteria_tを追加
18     *  getItemIDByIndexID
19     * 引数の順序を変更
20     *  getCertifyState
21     *  setCertifyState
22     *  getCertifyPermission
23     * sessionID2UID,querySimple,queryGetUnsignedIntの利用
24     * insertItemの内部でPrivateIndexへ登録を行なう
25     * getItems: getItemPermissionを使った読み込み権限チェック.
26     *
27   * Revision 1.43  2005/01/19 01:42:15  aga   * Revision 1.43  2005/01/19 01:42:15  aga
28   * ・x_xnpaccount_indexのgid,uidが0にならないよう修正.   * ・x_xnpaccount_indexのgid,uidが0にならないよう修正.
29   *   *
# Line 203  using namespace std; Line 225  using namespace std;
225  #include "commonal.h"  #include "commonal.h"
226  #include "item.h"  #include "item.h"
227  #include "index.h"  #include "index.h"
228    #include "changelog.h"
229    
230  static string dbprefix; //!< XOOPSデータベーステーブルのPREFIX  static string dbprefix; //!< XOOPSデータベーステーブルのPREFIX
231    
# Line 238  static result_t countResultRows( const c Line 261  static result_t countResultRows( const c
261      return ret;      return ret;
262  }  }
263    
264    
265  /**  /**
266   *   *
267   * セッションに対応付けられたユーザIDを得る   * インデックス登録,アイテム更新を行なったときの承認状態の初期値を,
268   *   * システム設定にしたがって返します.
269   * @param sid セッションID   *
  * @param uid ユーザIDを受け取るポインタ引数  
  * @return RES_OK  
  * @return RES_ERROR  
  *  
270   */   */
271  static result_t getUIDFromSession( sessionid_t sid, userid_t* uid )  static certify_t getInitialCertifyStateFromConfig( )
272  {  {
273      const session_t* session;      char* certify_item_val;
274      if( getSession( sid, &session ) == RES_OK ){      certify_t ret = index::NOT_CERTIFIED;
275          *uid = session ->  getUID( );      
276          freeSession( session );      if( getConfigValue( XNP_CONFIG_CERTIFY_ITEM_KEY, &certify_item_val ) == RES_OK ){
277          return RES_OK;          if( strcmp( certify_item_val, XNP_CONFIG_CERTIFY_ITEM_AUTO ) == 0 ){
278                //certify automatic
279                ret = index::CERTIFIED;
280            }else if( strcmp( certify_item_val, XNP_CONFIG_CERTIFY_ITEM_ON ) == 0 ){
281                //certify by moderator or group admin
282                ret = index::CERTIFY_REQUIRED;
283            }
284            freeString( certify_item_val );
285      }      }
286      return RES_ERROR;      return ret;
287  }  }
288    
289  /**  /**
# Line 342  static result_t queryGetUnsignedInt( con Line 369  static result_t queryGetUnsignedInt( con
369      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
370          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), strlen( sql.c_str() ) ) ) == SQL_SUCCESS ){          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), strlen( sql.c_str() ) ) ) == SQL_SUCCESS ){
371              SQLUINTEGER sInt = 0;              SQLUINTEGER sInt = 0;
372              SQLINTEGER len;              SQLINTEGER len = 0;
373              SQLBindCol( hstmt, 1, SQL_C_ULONG, &sInt, 0, &len );              SQLBindCol( hstmt, 1, SQL_C_ULONG, &sInt, 0, &len );
374              if( ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS ){              if( ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS ){
375                  if ( len == SQL_NULL_DATA )                  if ( len == SQL_NULL_DATA )
# Line 352  static result_t queryGetUnsignedInt( con Line 379  static result_t queryGetUnsignedInt( con
379              }else{              }else{
380                  string s( "SQLFetch in queryGetUnsignedInt " );                  string s( "SQLFetch in queryGetUnsignedInt " );
381                  s += functionName;                  s += functionName;
382                    s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );
383                  setLastErrorString( s.c_str( ) );                  setLastErrorString( s.c_str( ) );
384                  ret = RES_ERROR;                  ret = RES_ERROR;
385              }              }
# Line 399  static char* strncpy2( char* dst, const Line 427  static char* strncpy2( char* dst, const
427  {  {
428      strncpy( dst, src, len );      strncpy( dst, src, len );
429      dst[ len ] = '\0';      dst[ len ] = '\0';
430  //    fprintf( stderr, "strncpy2 '%s'\n", dst );      //fprintf( stderr, "\nstrncpy2 '%s'\n", dst );
431      return dst;      return dst;
432  }  }
433    
# Line 822  int getAccountCount( sessionid_t sid ) Line 850  int getAccountCount( sessionid_t sid )
850      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
851          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
852              SQLUINTEGER count = 0;              SQLUINTEGER count = 0;
853              SQLINTEGER len;              SQLINTEGER len = 0;
854              SQLBindCol( hstmt, 1, SQL_C_ULONG, &count, 0, &len );              SQLBindCol( hstmt, 1, SQL_C_ULONG, &count, 0, &len );
855              if( ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS ){              if( ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS ){
856                  ret = count;                  ret = count;
# Line 1033  result_t getAccounts( sessionid_t sid, c Line 1061  result_t getAccounts( sessionid_t sid, c
1061            
1062      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
1063          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
1064              SQLINTEGER len;              SQLINTEGER len = 0;
1065              userid_t uid = 0;              userid_t uid = 0;
1066              SQLBindCol( hstmt, 1, SQL_C_ULONG, &uid, 0, &len );              SQLBindCol( hstmt, 1, SQL_C_ULONG, &uid, 0, &len );
1067              *accountsLen=0;              *accountsLen=0;
# Line 1162  result_t insertAccount( sessionid_t sid, Line 1190  result_t insertAccount( sessionid_t sid,
1190    
1191      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
1192          string sql;          string sql;
1193          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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";          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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, MD5(?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
1194          sqlcode = SQLPrepare(hstmt, (SQLCHAR*)sql.c_str(), SQL_NTS);          sqlcode = SQLPrepare(hstmt, (SQLCHAR*)sql.c_str(), SQL_NTS);
1195          if( sqlcode == SQL_SUCCESS || sqlcode == SQL_SUCCESS_WITH_INFO ){          if( sqlcode == SQL_SUCCESS || sqlcode == SQL_SUCCESS_WITH_INFO ){
1196              SQLBindParameter(hstmt,  1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, ACCOUNT_UNAME_LEN, 0, uname, 0, &cbUname );              SQLBindParameter(hstmt,  1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, ACCOUNT_UNAME_LEN, 0, uname, 0, &cbUname );
# Line 1231  result_t insertAccount( sessionid_t sid, Line 1259  result_t insertAccount( sessionid_t sid,
1259                  SQLINTEGER count = 0;                  SQLINTEGER count = 0;
1260                  if( ( sqlcode = SQLRowCount( hstmt, &count ) ) == SQL_SUCCESS && count > 0 ){                  if( ( sqlcode = SQLRowCount( hstmt, &count ) ) == SQL_SUCCESS && count > 0 ){
1261                      //ユーザIDを取得する                      //ユーザIDを取得する
1262                      SQLHANDLE hstmt2 = NULL;                      sql = "SELECT LAST_INSERT_ID()";
1263                      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt2 ) ) == SQL_SUCCESS ) {                      ret = queryGetUnsignedInt( "insertAccount", sql, (unsigned int*)uid );
                         sql = "SELECT LAST_INSERT_ID()"; // MySQL  
                         // sql = "SELECT LAST_INSERT_ROWID()"; // SQLite  
                         if( ( sqlcode = SQLExecDirect( hstmt2, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){  
                             SQLUINTEGER last_insert_id;  
                             SQLINTEGER len;  
                             SQLBindCol( hstmt2, 1, SQL_C_ULONG, &last_insert_id, 0, &len );  
                             if( ( sqlcode = SQLFetch( hstmt2 ) ) == SQL_SUCCESS ){  
                                 *uid = last_insert_id;  
                                 ret = RES_OK;  
                             }else{  
                                 ret = RES_DB_QUERY_ERROR;  
                                 string s( "SQLFetch in insertAccount " );  
                                 s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );  
                                 setLastErrorString( s.c_str( ) );  
                             }  
                         }else{  
                             string s( "SQLExecDirect in insertAccount " );  
                             s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );  
                             setLastErrorString( s.c_str( ) );  
                             ret = RES_DB_QUERY_ERROR;  
                         }  
                         SQLFreeHandle( SQL_HANDLE_STMT, hstmt2 );  
                     }  
1264                  }else{                  }else{
1265                      string s( "SQLRowCount in insertAccount sql=" );                      string s( "SQLRowCount in insertAccount sql=" );
1266                      s += string( sql );                      s += string( sql );
# Line 1708  result_t dumpUids( sessionid_t sid, crit Line 1713  result_t dumpUids( sessionid_t sid, crit
1713      }      }
1714      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
1715          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
1716              userid_t uid;              userid_t uid = 0;
1717              SQLINTEGER len = 0;              SQLINTEGER len = 0;
1718              SQLBindCol( hstmt, 1, SQL_C_ULONG, &uid, 0, &len );              SQLBindCol( hstmt, 1, SQL_C_ULONG, &uid, 0, &len );
1719    
# Line 1739  result_t dumpUids( sessionid_t sid, crit Line 1744  result_t dumpUids( sessionid_t sid, crit
1744  #ifdef USE_SYSLOG  #ifdef USE_SYSLOG
1745          syslog( LOG_DEBUG, "dumpUids %s", getLastErrorString( ) );          syslog( LOG_DEBUG, "dumpUids %s", getLastErrorString( ) );
1746  #endif  #endif
1747          ret = RES_DB_QUERY_ERROR;          ret = RES_ERROR;
1748      }      }
1749      return ret;      return ret;
1750  }  }
# Line 1768  int getGroupCount( sessionid_t sid ) Line 1773  int getGroupCount( sessionid_t sid )
1773      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
1774          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
1775              SQLUINTEGER count = 0;              SQLUINTEGER count = 0;
1776              SQLINTEGER len;              SQLINTEGER len = 0;
1777              SQLBindCol( hstmt, 1, SQL_C_ULONG, &count, 0, &len );              SQLBindCol( hstmt, 1, SQL_C_ULONG, &count, 0, &len );
1778              if( ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS ){              if( ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS ){
1779                  ret = count;                  ret = count;
# Line 1842  result_t getGroupsByUid( sessionid_t sid Line 1847  result_t getGroupsByUid( sessionid_t sid
1847                      sql += " WHERE uid=" + string( unsignedIntToString( uid ) );                      sql += " WHERE uid=" + string( unsignedIntToString( uid ) );
1848                      sql += criteria2str( cri );                      sql += criteria2str( cri );
1849                      if( ( sqlcode = SQLExecDirect( hstmt2, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){                      if( ( sqlcode = SQLExecDirect( hstmt2, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
1850                          SQLINTEGER len;                          SQLINTEGER len = 0;
1851                          groupid_t gid = 0;                          groupid_t gid = 0;
1852                          SQLBindCol( hstmt, 1, SQL_C_ULONG, &gid, 0, &len );                          SQLBindCol( hstmt, 1, SQL_C_ULONG, &gid, 0, &len );
1853                          for( int i = 0; ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS && i < *gidsLen ; i++ ){                          for( int i = 0; ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS && i < *gidsLen ; i++ ){
# Line 1858  result_t getGroupsByUid( sessionid_t sid Line 1863  result_t getGroupsByUid( sessionid_t sid
1863                  }else{                  }else{
1864                      string s( "SQLAllocHandle in getGroupsByUid " );                      string s( "SQLAllocHandle in getGroupsByUid " );
1865                      s += odbcDiagString( SQL_HANDLE_DBC, hdbc, sqlcode );                      s += odbcDiagString( SQL_HANDLE_DBC, hdbc, sqlcode );
1866                      ret = RES_DB_QUERY_ERROR;                      ret = RES_ERROR;
1867                  }                  }
1868              }else{              }else{
1869                  ret = RES_OK;                  ret = RES_OK;
# Line 1872  result_t getGroupsByUid( sessionid_t sid Line 1877  result_t getGroupsByUid( sessionid_t sid
1877      }else{      }else{
1878          string s( "SQLAllocHandle in getGroupsByUid " );          string s( "SQLAllocHandle in getGroupsByUid " );
1879          setLastErrorString( s.c_str( ) );          setLastErrorString( s.c_str( ) );
1880          ret = RES_DB_QUERY_ERROR;          ret = RES_ERROR;
1881      }      }
1882      return ret;      return ret;
1883  }  }
# Line 1956  result_t dumpGids( sessionid_t sid, crit Line 1961  result_t dumpGids( sessionid_t sid, crit
1961            
1962      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
1963          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
1964              groupid_t gid;              groupid_t gid = 0;
1965              SQLINTEGER len = 0;              SQLINTEGER len = 0;
1966              SQLBindCol( hstmt, 1, SQL_C_ULONG, &gid, 0, &len );              SQLBindCol( hstmt, 1, SQL_C_ULONG, &gid, 0, &len );
1967              for( int i = 0; ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS && i < *gidsLen ; i++ ){              for( int i = 0; ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS && i < *gidsLen ; i++ ){
# Line 2175  result_t insertMember( sessionid_t sid, Line 2180  result_t insertMember( sessionid_t sid,
2180          SQLFreeHandle( SQL_HANDLE_STMT, hstmt );          SQLFreeHandle( SQL_HANDLE_STMT, hstmt );
2181      }else{      }else{
2182          setLastErrorString( "SQLAllocHandle(SQL_HANDLE_STMT,...) in insertMember" );          setLastErrorString( "SQLAllocHandle(SQL_HANDLE_STMT,...) in insertMember" );
2183          ret = RES_DB_QUERY_ERROR;          ret = RES_ERROR;
2184      }      }
2185      return ret;      return ret;
2186  }  }
# Line 2216  result_t getMembers( sessionid_t sid, gr Line 2221  result_t getMembers( sessionid_t sid, gr
2221      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
2222          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
2223              SQLUINTEGER count = 0;              SQLUINTEGER count = 0;
2224              SQLINTEGER len;              SQLINTEGER len = 0;
2225              SQLBindCol( hstmt, 1, SQL_C_ULONG, &count, 0, &len );              SQLBindCol( hstmt, 1, SQL_C_ULONG, &count, 0, &len );
2226              if( ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS ){              if( ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS ){
2227                  dst = new userid_t[ count ];                  dst = new userid_t[ count ];
2228                  *uids = dst;                  *uids = dst;
2229                  *uidsLen = count;                  *uidsLen = count;
2230                  if( count > 0 ){                  if( count > 0 ){
2231                      //retrive member's IDs                      //retrieve member's IDs
2232                      SQLHANDLE hstmt2 = NULL;                          SQLHANDLE hstmt2 = NULL;    
2233                     if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt2 ) ) == SQL_SUCCESS ) {                     if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt2 ) ) == SQL_SUCCESS ) {
2234                          sql = "SELECT uid, gid, is_admin FROM " + dbprefix + "_xnpaccount_groups_users_link ";                          sql = "SELECT uid, gid, is_admin FROM " + dbprefix + "_xnpaccount_groups_users_link ";
# Line 2231  result_t getMembers( sessionid_t sid, gr Line 2236  result_t getMembers( sessionid_t sid, gr
2236                          sql += " " + string( criteria2str( cri ) );                          sql += " " + string( criteria2str( cri ) );
2237                          if( ( sqlcode = SQLExecDirect( hstmt2, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){                          if( ( sqlcode = SQLExecDirect( hstmt2, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
2238                              SQLUINTEGER uid = 0;                              SQLUINTEGER uid = 0;
2239                              SQLINTEGER len;                              SQLINTEGER len = 0;
2240                              SQLBindCol( hstmt2, 1, SQL_C_ULONG, &uid, 0, &len );                              SQLBindCol( hstmt2, 1, SQL_C_ULONG, &uid, 0, &len );
2241                              for( int i = 0; ( sqlcode = SQLFetch( hstmt2 ) ) == SQL_SUCCESS && i < count ; i++ ){                              for( int i = 0; ( sqlcode = SQLFetch( hstmt2 ) ) == SQL_SUCCESS && i < count ; i++ ){
2242                                  dst[ i ] = uid;                                  dst[ i ] = uid;
# Line 2244  result_t getMembers( sessionid_t sid, gr Line 2249  result_t getMembers( sessionid_t sid, gr
2249                          SQLFreeHandle( SQL_HANDLE_STMT, hstmt2 );                          SQLFreeHandle( SQL_HANDLE_STMT, hstmt2 );
2250                      }else{                      }else{
2251                          setLastErrorString( "SQLAllocHandle(SQL_HANDLE_STMT,...) in getMembers" );                          setLastErrorString( "SQLAllocHandle(SQL_HANDLE_STMT,...) in getMembers" );
2252                          ret = RES_DB_QUERY_ERROR;                          ret = RES_ERROR;
2253                      }                      }
2254                  }else{                  }else{
2255                      ret = RES_OK;                      ret = RES_OK;
# Line 2262  result_t getMembers( sessionid_t sid, gr Line 2267  result_t getMembers( sessionid_t sid, gr
2267          SQLFreeHandle( SQL_HANDLE_STMT, hstmt );          SQLFreeHandle( SQL_HANDLE_STMT, hstmt );
2268      }else{      }else{
2269          setLastErrorString( "SQLAllocHandle(SQL_HANDLE_STMT,...) in getMembers" );          setLastErrorString( "SQLAllocHandle(SQL_HANDLE_STMT,...) in getMembers" );
2270          ret = RES_DB_QUERY_ERROR;          ret = RES_ERROR;
2271      }      }
2272      return ret;      return ret;
2273  }  }
# Line 2358  result_t insertGroup( sessionid_t sid, c Line 2363  result_t insertGroup( sessionid_t sid, c
2363                  SQLINTEGER count = 0;                  SQLINTEGER count = 0;
2364                  if( ( sqlcode = SQLRowCount( hstmt, &count ) ) == SQL_SUCCESS && count > 0 ){                  if( ( sqlcode = SQLRowCount( hstmt, &count ) ) == SQL_SUCCESS && count > 0 ){
2365                      //グループIDを取得する                      //グループIDを取得する
2366                      SQLHANDLE hstmt2 = NULL;                      sql = "SELECT LAST_INSERT_ID()";
2367                      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt2 ) ) == SQL_SUCCESS ) {                      ret = queryGetUnsignedInt( "insertGroup", sql, (unsigned int*)gid );
                         sql = "SELECT LAST_INSERT_ID()"; // MySQL  
                         // sql = "SELECT LAST_INSERT_ROWID()"; // SQLite  
                         if( ( sqlcode = SQLExecDirect( hstmt2, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){  
                             SQLUINTEGER last_insert_id;  
                             SQLINTEGER len;  
                             SQLBindCol( hstmt2, 1, SQL_C_ULONG, &last_insert_id, 0, &len );  
                             if( ( sqlcode = SQLFetch( hstmt2 ) ) == SQL_SUCCESS ){  
                                 *gid = last_insert_id;  
                                 ret = RES_OK;  
                             }else{  
                                 ret = RES_DB_QUERY_ERROR;  
                                 string s( "SQLFetch in insertGroup " );  
                                 s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );  
                                 setLastErrorString( s.c_str( ) );  
                             }  
                         }else{  
                             string s( "SQLExecDirect in insertGroup " );  
                             s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );  
                             setLastErrorString( s.c_str( ) );  
                             ret = RES_DB_QUERY_ERROR;  
                         }  
                         SQLFreeHandle( SQL_HANDLE_STMT, hstmt2 );  
                     }  
2368                  }else{                  }else{
2369                      string s( "SQLRowCount in insertGroup sql=" );                      string s( "SQLRowCount in insertGroup sql=" );
2370                      s += string( sql );                      s += string( sql );
# Line 2576  result_t getGroups( sessionid_t sid, gro Line 2558  result_t getGroups( sessionid_t sid, gro
2558            
2559      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
2560          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
2561              SQLINTEGER len;              SQLINTEGER len = 0;
2562              groupid_t gid = 0;              groupid_t gid = 0;
2563              SQLBindCol( hstmt, 1, SQL_C_ULONG, &gid, 0, &len );              SQLBindCol( hstmt, 1, SQL_C_ULONG, &gid, 0, &len );
2564              *groupsLen=0;              *groupsLen=0;
# Line 2610  result_t getGroups( sessionid_t sid, gro Line 2592  result_t getGroups( sessionid_t sid, gro
2592  #ifdef USE_SYSLOG  #ifdef USE_SYSLOG
2593          syslog( LOG_DEBUG, "getGroups %s", getLastErrorString( ) );          syslog( LOG_DEBUG, "getGroups %s", getLastErrorString( ) );
2594  #endif  #endif
2595          ret = RES_DB_QUERY_ERROR;          ret = RES_ERROR;
2596      }      }
2597  #ifdef USE_SYSLOG  #ifdef USE_SYSLOG
2598      closelog( );      closelog( );
# Line 2699  result_t getUid( const char* uname, user Line 2681  result_t getUid( const char* uname, user
2681      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
2682          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
2683              userid_t _uid = 0;              userid_t _uid = 0;
2684              SQLINTEGER len;              SQLINTEGER len = 0;
2685              SQLBindCol( hstmt, 1, SQL_C_ULONG, &_uid, 0, &len );              SQLBindCol( hstmt, 1, SQL_C_ULONG, &_uid, 0, &len );
2686              sqlcode = SQLFetch( hstmt );              sqlcode = SQLFetch( hstmt );
2687              if( sqlcode == SQL_SUCCESS ){              if( sqlcode == SQL_SUCCESS ){
# Line 2718  result_t getUid( const char* uname, user Line 2700  result_t getUid( const char* uname, user
2700          SQLFreeHandle( SQL_HANDLE_STMT, hstmt );          SQLFreeHandle( SQL_HANDLE_STMT, hstmt );
2701      }else{      }else{
2702          setLastErrorString( "SQLAllocHandle(SQL_HANDLE_STMT,...) in getUid" );          setLastErrorString( "SQLAllocHandle(SQL_HANDLE_STMT,...) in getUid" );
2703          ret = RES_DB_QUERY_ERROR;          ret = RES_ERROR;
2704      }      }
2705            
2706      return ret;      return ret;
# Line 2775  result_t loginUser(const char* uname, co Line 2757  result_t loginUser(const char* uname, co
2757      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
2758          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
2759              userid_t uid = 0;              userid_t uid = 0;
2760              SQLINTEGER len;              SQLINTEGER len = 0;
2761              SQLBindCol( hstmt, 1, SQL_C_ULONG, &uid, 0, &len );              SQLBindCol( hstmt, 1, SQL_C_ULONG, &uid, 0, &len );
2762              sqlcode = SQLFetch( hstmt );              sqlcode = SQLFetch( hstmt );
2763              if( sqlcode == SQL_SUCCESS ){              if( sqlcode == SQL_SUCCESS ){
# Line 2792  result_t loginUser(const char* uname, co Line 2774  result_t loginUser(const char* uname, co
2774          SQLFreeHandle( SQL_HANDLE_STMT, hstmt );          SQLFreeHandle( SQL_HANDLE_STMT, hstmt );
2775      }else{      }else{
2776          setLastErrorString( "SQLAllocHandle(SQL_HANDLE_STMT,...) in loginUser" );          setLastErrorString( "SQLAllocHandle(SQL_HANDLE_STMT,...) in loginUser" );
2777          ret = RES_DB_QUERY_ERROR;          ret = RES_ERROR;
2778      }      }
2779            
2780      return ret;      return ret;
# Line 2880  result_t createSession( const char* sess Line 2862  result_t createSession( const char* sess
2862              }              }
2863          }          }
2864      }else{      }else{
2865          ret = RES_DB_QUERY_ERROR;          ret = RES_ERROR;
2866      }      }
2867      return ret;      return ret;
2868  }  }
# Line 2942  result_t getSession( sessionid_t sid, co Line 2924  result_t getSession( sessionid_t sid, co
2924          string s( "SQLAllocHandle(SQL_HANDLE_STMT,...) in getSession" );          string s( "SQLAllocHandle(SQL_HANDLE_STMT,...) in getSession" );
2925          s += odbcDiagString( SQL_HANDLE_DBC, hdbc, sqlcode );          s += odbcDiagString( SQL_HANDLE_DBC, hdbc, sqlcode );
2926          setLastErrorString( s.c_str( ) );          setLastErrorString( s.c_str( ) );
2927          ret = RES_DB_QUERY_ERROR;          ret = RES_ERROR;
2928      }      }
2929      return ret;      return ret;
2930            
# Line 3000  result_t insertItem( sessionid_t sid, co Line 2982  result_t insertItem( sessionid_t sid, co
2982      if( !isActivatedBySession( sid ) ) return RES_NO_WRITE_ACCESS_RIGHT;      if( !isActivatedBySession( sid ) ) return RES_NO_WRITE_ACCESS_RIGHT;
2983            
2984      string sql;      string sql;
2985        SQLHANDLE hstmt = NULL, hstmt2 = NULL;
2986      SQLRETURN sqlcode;      SQLRETURN sqlcode;
2987      result_t ret = RES_ERROR;      result_t ret = RES_ERROR;
2988      SQLINTEGER uid, item_type_id, creation_date, last_update_date;      SQLINTEGER uid, item_type_id, creation_date, last_update_date;
# Line 3012  result_t insertItem( sessionid_t sid, co Line 2995  result_t insertItem( sessionid_t sid, co
2995          cbKeywords = SQL_NTS,          cbKeywords = SQL_NTS,
2996          cbDescription = SQL_NTS,          cbDescription = SQL_NTS,
2997          cbDOI = SQL_NTS;          cbDOI = SQL_NTS;
2998        indexid_t private_xid;
2999            
3000      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
3001          string sql;          string sql;
# Line 3040  result_t insertItem( sessionid_t sid, co Line 3024  result_t insertItem( sessionid_t sid, co
3024                  SQLINTEGER count = 0;                  SQLINTEGER count = 0;
3025                  if( ( sqlcode = SQLRowCount( hstmt, &count ) ) == SQL_SUCCESS && count > 0 ){                  if( ( sqlcode = SQLRowCount( hstmt, &count ) ) == SQL_SUCCESS && count > 0 ){
3026                      //アイテムIDを取得する                      //アイテムIDを取得する
3027                      SQLHANDLE hstmt2 = NULL;                      sql = "SELECT LAST_INSERT_ID()";
3028                      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt2 ) ) == SQL_SUCCESS ) {                      if( ( ret = queryGetUnsignedInt( "insertItem", sql, (unsigned int*)itemid ) ) == RES_OK ){
3029                          sql = "SELECT LAST_INSERT_ID()"; // MySQL                          if( item -> getItemTypeID( ) == item::ITID_BINDER ){
3030                          // sql = "SELECT LAST_INSERT_ROWID()"; // SQLite                              //insert into binder index
3031                          if( ( sqlcode = SQLExecDirect( hstmt2, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){                              if( ( ret = registerItem( sid, item::IID_BINDERS, *itemid ) ) == RES_OK ){
3032                              SQLUINTEGER last_insert_id;                                  ret = setCertifyState( sid, item::IID_BINDERS, *itemid, index::CERTIFIED );
                             SQLINTEGER len;  
                             SQLBindCol( hstmt2, 1, SQL_C_ULONG, &last_insert_id, 0, &len );  
                             if( ( sqlcode = SQLFetch( hstmt2 ) ) == SQL_SUCCESS ){  
                                 *itemid = last_insert_id;  
                                 ret = RES_OK;  
                             }else{  
                                 ret = RES_DB_QUERY_ERROR;  
                                 string s( "SQLFetch in insertItem " );  
                                 s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );  
                                 setLastErrorString( s.c_str( ) );  
3033                              }                              }
3034                          }else{                          }else{
3035                              string s( "SQLExecDirect in insertItem " );                              //insert into private index
3036                              s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );                              sql = "SELECT private_index_id FROM " + dbprefix + "_xnpaccount_users ";
3037                              setLastErrorString( s.c_str( ) );                              sql += " WHERE uid=" + unsignedIntToString( uid );
3038                              ret = RES_DB_QUERY_ERROR;                              //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
3039                                if( ( ret = queryGetUnsignedInt( "insertItem", sql, (unsigned int*)&private_xid ) ) == RES_OK ){
3040                                    ret = registerItem( sid, private_xid, *itemid );
3041                                }
3042                          }                          }
3043                          SQLFreeHandle( SQL_HANDLE_STMT, hstmt2 );                          ret = RES_OK;
3044                      }                      }
                     //TODO insert into private index  
3045                  }else{                  }else{
3046                      string s( "SQLRowCount in insertItem sql=" );                      string s( "SQLRowCount in insertItem sql=" );
3047                      s += string( sql );                      s += string( sql );
# Line 3154  result_t getItems( sessionid_t sid, cons Line 3130  result_t getItems( sessionid_t sid, cons
3130      SQLRETURN sqlcode;      SQLRETURN sqlcode;
3131      SQLHANDLE hstmt = NULL;          SQLHANDLE hstmt = NULL;    
3132      string sql;      string sql;
3133        string where;
3134      item_t* dst = new item_t[ iidsLen ];      item_t* dst = new item_t[ iidsLen ];
3135                
3136      sql = "SELECT item_id, item_type_id, title, keywords, description, doi, uid, creation_date, last_update_date ";      sql = "SELECT item_id, item_type_id, title, keywords, description, doi, uid, creation_date, last_update_date ";
3137      sql += "FROM " + dbprefix + "_xnpaccount_item_basic ";      sql += "FROM " + dbprefix + "_xnpaccount_item_basic ";
3138      sql += "WHERE ";      if( iidsLen > 0 ){
3139            int i;
3140            for( i = 0; i < iidsLen; i++ ){
3141                if( getItemPermission( sid, iids[ i ], item::OP_READ ) )
3142                    where = string( unsignedIntToString( iids[ i ] ) );
3143                break;
3144            }
3145            for( ; i < iidsLen; i++ ){
3146                if( getItemPermission( sid, iids[ i ], item::OP_READ ) )
3147                    where = string( unsignedIntToString( iids[ i ] ) ) + ", " + where;
3148            }
3149            if( where.length() > 0 )
3150                sql += "WHERE item_id IN (" + where + " ) ";
3151            else{
3152                //no items can be retrieve.
3153                *items = new item_t[ 0 ];
3154                *itemsLen = 0;
3155                return RES_OK;
3156            }
3157        }
3158    /*
3159      if( !isModeratorBySession( sid ) ){      if( !isModeratorBySession( sid ) ){
3160          userid_t uid;          userid_t uid;
3161          if( getUIDFromSession( sid, &uid ) == RES_OK ){          if( sessionID2UID( sid, &uid ) == RES_OK ){
3162              sql += "uid=" + unsignedIntToString( uid );              sql += "uid=" + unsignedIntToString( uid );
3163              sql += " AND";              sql += " AND";
3164          }else{          }else{
# Line 3175  result_t getItems( sessionid_t sid, cons Line 3172  result_t getItems( sessionid_t sid, cons
3172          }          }
3173          sql += " ) ";          sql += " ) ";
3174      }      }
3175    */
3176      sql += criteria2str( cri );      sql += criteria2str( cri );
3177            
3178        //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
3179      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
3180          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
3181              userid_t uid = 0;              userid_t uid = 0;
# Line 3218  result_t getItems( sessionid_t sid, cons Line 3217  result_t getItems( sessionid_t sid, cons
3217          string s( "SQLAllocHandle(SQL_HANDLE_STMT,...) in getItems" );          string s( "SQLAllocHandle(SQL_HANDLE_STMT,...) in getItems" );
3218          s += odbcDiagString( SQL_HANDLE_DBC, hdbc, sqlcode );          s += odbcDiagString( SQL_HANDLE_DBC, hdbc, sqlcode );
3219          setLastErrorString( s.c_str( ) );          setLastErrorString( s.c_str( ) );
3220          ret = RES_DB_QUERY_ERROR;          ret = RES_ERROR;
3221      }      }
3222      return ret;      return ret;
3223  }  }
# Line 3251  result_t dumpItemID(sessionid_t sid, cri Line 3250  result_t dumpItemID(sessionid_t sid, cri
3250      string sql;      string sql;
3251      itemid_t* dst = 0;      itemid_t* dst = 0;
3252            
3253      sql = "SELECT item_id ";      sql = "SELECT item_id";
3254      sql += "FROM " + dbprefix + "_xnpaccount_item_basic ";      sql += " FROM " + dbprefix + "_xnpaccount_item_basic";
3255        sql += " WHERE item_type_id!=" + unsignedIntToString( item::ITID_INDEX );
3256      if( !isModeratorBySession( sid ) ){      if( !isModeratorBySession( sid ) ){
         sql += "WHERE ";  
3257          userid_t uid;          userid_t uid;
3258          if( getUIDFromSession( sid, &uid ) == RES_OK ){          if( sessionID2UID( sid, &uid ) == RES_OK ){
3259              sql += "uid=" + unsignedIntToString( uid );              sql += " AND uid=" + unsignedIntToString( uid );
3260          }else{          }else{
3261              return RES_ERROR;              return RES_ERROR;
3262          }          }
3263      }      }
3264      sql += criteria2str( c );      sql += criteria2str( c );
       
3265      if( countResultRows( sql.c_str(), &count ) == RES_OK ){      if( countResultRows( sql.c_str(), &count ) == RES_OK ){
3266          dst = new itemid_t[ count ];          dst = new itemid_t[ count ];
3267          *iids = dst;          *iids = dst;
# Line 3273  result_t dumpItemID(sessionid_t sid, cri Line 3271  result_t dumpItemID(sessionid_t sid, cri
3271            
3272      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
3273          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
3274              SQLINTEGER len;              SQLINTEGER len = 0;
3275              userid_t itemid = 0;              SQLUINTEGER itemid = 0;
3276              SQLBindCol( hstmt, 1, SQL_C_ULONG, &itemid, 0, &len );              SQLBindCol( hstmt, 1, SQL_C_ULONG, &itemid, 0, &len );
3277              *iidsLen = 0;              *iidsLen = 0;
3278              for( int i = 0; ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS && i < count ; i++ ){              for( int i = 0; ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS && i < count ; i++ ){
3279                  dst[ i ] = itemid;                  dst[ i ] = itemid;
3280                  *iidsLen++;                  (*iidsLen)++;
3281              }              }
3282              ret = RES_OK;              ret = RES_OK;
3283          }else{          }else{
# Line 3294  result_t dumpItemID(sessionid_t sid, cri Line 3292  result_t dumpItemID(sessionid_t sid, cri
3292          string s( "SQLAllocHandle(SQL_HANDLE_STMT,...) in dumpItemID" );          string s( "SQLAllocHandle(SQL_HANDLE_STMT,...) in dumpItemID" );
3293          s += odbcDiagString( SQL_HANDLE_DBC, hdbc, sqlcode );          s += odbcDiagString( SQL_HANDLE_DBC, hdbc, sqlcode );
3294          setLastErrorString( s.c_str( ) );          setLastErrorString( s.c_str( ) );
3295          ret = RES_DB_QUERY_ERROR;          ret = RES_ERROR;
3296      }      }
3297      return ret;      return ret;
3298  }  }
# Line 3312  result_t dumpItemID(sessionid_t sid, cri Line 3310  result_t dumpItemID(sessionid_t sid, cri
3310   * @return RES_NO_SUCH_SESSION   * @return RES_NO_SUCH_SESSION
3311   * @return RES_DB_QUERY_ERROR   * @return RES_DB_QUERY_ERROR
3312   * @return RES_DB_NOT_INITIALIZED   * @return RES_DB_NOT_INITIALIZED
3313   *   * @return RES_NO_WRITE_ACCESS_RIGHT
3314   */   */
3315  result_t updateItem( sessionid_t sid, const item_t* item )  result_t updateItem( sessionid_t sid, const item_t* item )
3316  {  {
# Line 3325  result_t updateItem( sessionid_t sid, co Line 3323  result_t updateItem( sessionid_t sid, co
3323      result_t ret = RES_ERROR;      result_t ret = RES_ERROR;
3324            
3325      userid_t sess_uid;      userid_t sess_uid;
3326      if( getUIDFromSession( sid, &sess_uid ) == RES_OK ){      if( sessionID2UID( sid, &sess_uid ) == RES_OK ){
3327          if( sess_uid != item -> getContributorUID( ) && !isModeratorBySession( sid ) )          if( sess_uid != item -> getContributorUID( ) && !isModeratorBySession( sid ) )
3328              return RES_NO_WRITE_ACCESS_RIGHT;//no permissions to delete this item              return RES_NO_WRITE_ACCESS_RIGHT;//no permissions to delete this item
3329      }else{      }else{
# Line 3380  result_t updateItem( sessionid_t sid, co Line 3378  result_t updateItem( sessionid_t sid, co
3378              if( ( sqlcode = SQLExecute( hstmt ) ) == SQL_SUCCESS ){              if( ( sqlcode = SQLExecute( hstmt ) ) == SQL_SUCCESS ){
3379                  SQLINTEGER count = 0;                  SQLINTEGER count = 0;
3380                  if( ( sqlcode = SQLRowCount( hstmt, &count ) ) == SQL_SUCCESS && count > 0 ){                  if( ( sqlcode = SQLRowCount( hstmt, &count ) ) == SQL_SUCCESS && count > 0 ){
3381                      ret = RES_OK;                      char* certify_item_val;
3382                      //TODO update certify state                      if( getConfigValue( XNP_CONFIG_CERTIFY_ITEM_KEY, &certify_item_val ) == RES_OK ){
3383                            SQLHANDLE hstmt2 = NULL;
3384                            certify_t cerity_state = index::NOT_CERTIFIED;
3385                            if( strcmp( certify_item_val, XNP_CONFIG_CERTIFY_ITEM_AUTO ) == 0 ){
3386                                cerity_state = index::CERTIFIED;
3387                            }else if( strcmp( certify_item_val, XNP_CONFIG_CERTIFY_ITEM_ON ) == 0 ){
3388                                cerity_state = index::CERTIFY_REQUIRED;
3389                            }
3390                            //update certify state
3391                            if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt2 ) ) == SQL_SUCCESS ) {
3392                                string sql;
3393                                sql = "SELECT index_item_link_id ";
3394                                sql += " FROM xtest_xnpaccount_index_item_link as tlink ";
3395                                sql += " LEFT JOIN xtest_xnpaccount_index as tx ON tlink.index_id=tx.index_id ";
3396                                sql += " LEFT JOIN xtest_xnpaccount_item_basic as ti ON tlink.item_id=ti.item_id ";
3397                                sql += " WHERE (tx.open_level=" + unsignedIntToString( index::OL_PUBLIC );
3398                                sql += " OR tx.open_level=" + unsignedIntToString( index::OL_GROUP_ONLY );
3399                                sql += ") AND ti.item_type_id!=" + unsignedIntToString( item::ITID_BINDER );
3400                                sql += " AND tlink.item_id=" + unsignedIntToString( item_id );
3401                                //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
3402                                if( ( sqlcode = SQLExecDirect( hstmt2, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
3403                                    SQLINTEGER len = 0;
3404                                    SQLUINTEGER registered_index_id = 0;
3405                                    SQLBindCol( hstmt2,1, SQL_C_ULONG, &registered_index_id, 0, &len );
3406                                    certify_t certify_state = getInitialCertifyStateFromConfig( );
3407                                    while ( ( sqlcode = SQLFetch( hstmt2 ) ) == SQL_SUCCESS ){
3408                                        string sql = "UPDATE " + dbprefix + "_xnpaccount_index_item_link";
3409                                        sql += " SET certify_state=" + unsignedIntToString( certify_state );
3410                                        sql += " WHERE index_item_link_id=" + unsignedIntToString( registered_index_id );
3411                                        //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
3412                                        querySimple( "updateItem", sql );
3413                                    }
3414                                    ret = RES_OK;
3415                                }else{
3416                                    string s( "SQLExecDirect in updateItem" );
3417                                    s += odbcDiagString( SQL_HANDLE_STMT, hstmt2, sqlcode );
3418                                    s += string( ", sql=" ) + string( sql );
3419                                    setLastErrorString( s.c_str( ) );
3420                                    ret = RES_DB_QUERY_ERROR;
3421                                }
3422                            }else{
3423                                string s( "SQLAllocHandle(SQL_HANDLE_STMT,...) in updateItem" );
3424                                s += odbcDiagString( SQL_HANDLE_DBC, hdbc, sqlcode );
3425                                setLastErrorString( s.c_str( ) );
3426                                ret = RES_ERROR;
3427                            }
3428                            freeString( certify_item_val );
3429                        }
3430                  }else{                  }else{
3431                      string s( "SQLRowCount in updateItem sql=" );                      string s( "SQLRowCount in updateItem sql=" );
3432                      s += string( sql );                      s += string( sql );
# Line 3409  result_t updateItem( sessionid_t sid, co Line 3454  result_t updateItem( sessionid_t sid, co
3454      return ret;      return ret;
3455  }  }
3456    
3457    /**
3458     *
3459     * アイテム(Basic Information)を削除する.
3460     *
3461     * @param sid セッションID
3462     * @param item 削除したいアイテムの情報
3463     * @return RES_OK
3464     * @return RES_ERROR
3465     * @return RES_NO_SUCH_SESSION
3466     * @return RES_DB_QUERY_ERROR
3467     * @return RES_DB_NOT_INITIALIZED
3468     * @return RES_NO_WRITE_ACCESS_RIGHT
3469     */
3470  result_t deleteItem( sessionid_t sid, itemid_t itemid )  result_t deleteItem( sessionid_t sid, itemid_t itemid )
3471  {  {
3472      if( hdbc == NULL ) return RES_DB_NOT_INITIALIZED;      if( hdbc == NULL ) return RES_DB_NOT_INITIALIZED;
# Line 3426  result_t deleteItem( sessionid_t sid, it Line 3484  result_t deleteItem( sessionid_t sid, it
3484      userid_t item_uid;      userid_t item_uid;
3485      const session_t* psess;      const session_t* psess;
3486            
3487      // retrive an item id      // retrieve an item's owner uid
3488      if( getItem( sid, itemid, &pitem ) != RES_OK )      if( getItem( sid, itemid, &pitem ) != RES_OK )
3489          return RES_NO_WRITE_ACCESS_RIGHT;//no permissions to delete this item          return RES_NO_WRITE_ACCESS_RIGHT;//no permissions to delete this item
3490      item_uid = pitem -> getContributorUID();      item_uid = pitem -> getContributorUID();
# Line 3474  result_t deleteItem( sessionid_t sid, it Line 3532  result_t deleteItem( sessionid_t sid, it
3532      return ret;      return ret;
3533  }  }
3534    
3535  result_t getItemIDByIndexID( sessionid_t sid, indexid_t xid, itemid_t** iids, int* iidsLen ){ return RES_ERROR; }  /**
3536     *
3537     * インデックスに登録されたアイテムのIDを取得します.
3538     * @param sid セッションID
3539     * @param binderid 対象インデックスのID
3540     * @param cri 結果の範囲指定,ソート条件指定
3541     * @param iids 登録されたアイテムIDを受け取る引数
3542     * @param iidsLen iidsに格納された個数を受け取る引数
3543     * @return RES_OK
3544     * @return RES_DB_NOT_INITIALIZED
3545     * @return RES_NO_SUCH_SESSION
3546     * @return RES_DB_QUERY_ERROR
3547     * @return RES_ERROR
3548     *
3549     */
3550    result_t getItemIDByIndexID( sessionid_t sid, indexid_t xid, criteria_t* cri, itemid_t** iids, int* iidsLen )
3551    {
3552        if( hdbc == NULL ) return RES_DB_NOT_INITIALIZED;
3553        if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
3554        
3555        result_t ret = RES_ERROR;
3556        itemid_t* dst = 0;
3557        string sql;
3558        SQLRETURN sqlcode;
3559        SQLINTEGER count = 0;
3560        
3561        sql = "SELECT t1.item_id FROM " + dbprefix + "_xnpaccount_item_basic as t1, "
3562            + dbprefix + "_xnpaccount_index_item_link as t2 ";
3563        sql += " WHERE t1.item_id = t2.item_id";
3564        sql += " AND t2.index_id=" + unsignedIntToString( xid );
3565        sql += criteria2str( cri );
3566        if( countResultRows( sql.c_str(), &count ) == RES_OK ){
3567            dst = new itemid_t[ count ];
3568            *iids = dst;
3569        }else{
3570            return RES_ERROR;
3571        }
3572        
3573        if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
3574            if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
3575                itemid_t iid;
3576                SQLINTEGER len = 0;
3577                SQLBindCol( hstmt, 1, SQL_C_ULONG, &iid, 0, &len );
3578                *iidsLen = 0;
3579                for( int i = 0; ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS && i < count ; i++ ){
3580                    dst[ i ] = iid;
3581                    (*iidsLen)++;
3582                }
3583                ret = RES_OK;
3584            }else{
3585                string s( "SQLExecDirect in getItemIDByIndexID " );
3586                s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );
3587                s += "sql=";
3588                s += string( sql );
3589                setLastErrorString( s.c_str( ) );
3590                ret = RES_DB_QUERY_ERROR;
3591            }
3592            SQLFreeHandle( SQL_HANDLE_STMT, hstmt );
3593        }else{
3594            string s( "SQLAllocHandle(SQL_HANDLE_STMT,...) in getItemIDByIndexID" );
3595            s += odbcDiagString( SQL_HANDLE_DBC, hdbc, sqlcode );
3596            setLastErrorString( s.c_str( ) );
3597            ret = RES_ERROR;
3598        }
3599        return ret;
3600    }
3601    
3602  result_t getCertifyState( sessionid_t sid, itemid_t iid, indexid_t xid, certify_t* certify ){ return RES_ERROR; }  /**
3603     *
3604     * バインダーに登録されたアイテムのIDを取得します.
3605     * @param sid セッションID
3606     * @param binderid 対象バインダーのID
3607     * @param cri 結果の範囲指定,ソート条件指定
3608     * @param iids 登録されたアイテムIDを受け取る引数
3609     * @param iidsLen iidsに格納された個数を受け取る引数
3610     * @return RES_OK
3611     * @return RES_DB_NOT_INITIALIZED
3612     * @return RES_NO_SUCH_SESSION
3613     * @return RES_DB_QUERY_ERROR
3614     * @return RES_ERROR
3615     *
3616     */
3617    result_t getItemIDByBinderID( sessionid_t sid, itemid_t binderid, criteria_t* cri, itemid_t** iids, int* iidsLen )
3618    {
3619        if( hdbc == NULL ) return RES_DB_NOT_INITIALIZED;
3620        if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
3621        
3622        result_t ret = RES_ERROR;
3623        itemid_t* dst = 0;
3624        string sql;
3625        SQLRETURN sqlcode;
3626        SQLINTEGER count = 0;
3627        
3628        sql = "SELECT t1.item_id FROM " + dbprefix + "_xnpaccount_item_basic as t1, "
3629            + dbprefix + "_xnpaccount_binder_item_link as t2 ";
3630        sql += " WHERE t1.item_id = t2.item_id";
3631        sql += " AND t2.binder_id=" + unsignedIntToString( binderid );
3632        sql += criteria2str( cri );
3633        if( countResultRows( sql.c_str(), &count ) == RES_OK ){
3634            dst = new itemid_t[ count ];
3635            *iids = dst;
3636        }else{
3637            return RES_ERROR;
3638        }
3639        
3640        if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
3641            if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
3642                itemid_t iid;
3643                SQLINTEGER len = 0;
3644                SQLBindCol( hstmt, 1, SQL_C_ULONG, &iid, 0, &len );
3645                *iidsLen = 0;
3646                for( int i = 0; ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS && i < count ; i++ ){
3647                    dst[ i ] = iid;
3648                    (*iidsLen)++;
3649                }
3650                ret = RES_OK;
3651            }else{
3652                string s( "SQLExecDirect in getItemIDByBinderID " );
3653                s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );
3654                s += "sql=";
3655                s += string( sql );
3656                setLastErrorString( s.c_str( ) );
3657                ret = RES_DB_QUERY_ERROR;
3658            }
3659            SQLFreeHandle( SQL_HANDLE_STMT, hstmt );
3660        }else{
3661            string s( "SQLAllocHandle(SQL_HANDLE_STMT,...) in getItemIDByBinderID" );
3662            s += odbcDiagString( SQL_HANDLE_DBC, hdbc, sqlcode );
3663            setLastErrorString( s.c_str( ) );
3664            ret = RES_ERROR;
3665        }
3666        return ret;
3667    }
3668    
3669  result_t setCertifyState( sessionid_t sid, itemid_t iid, indexid_t xid, certify_t state ){ return RES_ERROR; }  /**
3670     *
3671     * アイテムの承認状態を取得します.
3672     * @refer certify_t
3673     * @param sid セッションID
3674     * @param xid 対象アイテムが登録されているインデックスのID
3675     * @param iid 対象アイテムのID
3676     * @param state 承認状態を受け取る引数
3677     * @return RES_OK
3678     * @return RES_NO_WRITE_ACCESS_RIGHT
3679     *
3680     */
3681    result_t getCertifyState( sessionid_t sid, indexid_t xid, itemid_t iid, certify_t* certify )
3682    {
3683        if( hdbc == NULL ) return RES_DB_NOT_INITIALIZED;
3684        
3685        string sql;
3686        
3687        sql = "SELECT certify_state";
3688        sql += " FROM " + dbprefix + "_xnpaccount_index_item_link";
3689        sql += " WHERE item_id = " + unsignedIntToString( iid );
3690        sql += " AND index_id = " + unsignedIntToString( xid );
3691        //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
3692        return queryGetUnsignedInt( "getCertifyState", sql, (unsigned int*)certify );
3693    }
3694    
3695    /**
3696     *
3697     * アイテムの承認状態を変更します.
3698     * @refer certify_t
3699     * @param sid セッションID
3700     * @param xid 変更対象アイテムが登録されているインデックスのID
3701     * @param iid 変更対象アイテムのID
3702     * @param state 新しく変更したい承認状態
3703     * @return RES_OK
3704     * @return RES_NO_WRITE_ACCESS_RIGHT
3705     *
3706     */
3707    result_t setCertifyState( sessionid_t sid, indexid_t xid, itemid_t iid, certify_t state )
3708    {
3709        if( !getCertifyPermission( sid, xid, iid, state ) ) return RES_NO_WRITE_ACCESS_RIGHT;
3710        
3711        string sql = "UPDATE " + dbprefix + "_xnpaccount_index_item_link SET certify_state="
3712            + unsignedIntToString( state )
3713            + " WHERE index_id=" + unsignedIntToString( xid )
3714            + " AND item_id=" + unsignedIntToString( iid );
3715        return querySimple( "setCertifyState", sql );
3716    }
3717    
3718  bool getCertifyPermission( sessionid_t sid, itemid_t iid, indexid_t xid, certify_t state ){ return RES_ERROR; }  /**
3719     *
3720     * アイテムの承認状態を変更する権限の有無を調べます.
3721     * @refer certify_t
3722     * @param sid セッションID
3723     * @param xid 変更対象アイテムが登録されているインデックスのID
3724     * @param iid 変更対象アイテムのID
3725     * @param state 新しく変更したい承認状態
3726     * @return true 権限あり
3727     * @return false 権限なし
3728     *
3729     */
3730    bool getCertifyPermission( sessionid_t sid, indexid_t xid, itemid_t iid, certify_t state )
3731    {
3732        //available if moderator
3733        if( isModeratorBySession( sid ) ) return true;
3734        
3735        const index_t* index;
3736        if( getIndex( sid, xid, &index ) == RES_OK ){
3737            //available for all users if index is binders
3738            if( index -> getIndexID() == index::IID_BINDERS ){
3739                freeIndex( index );
3740                return true;
3741            }
3742            //
3743            indexid_t index_uid = index -> getOwnerUID( );
3744            freeIndex( index );
3745            userid_t sess_uid;
3746            if( sessionID2UID( sid, &sess_uid ) == RES_OK ){
3747                const item_t* item;
3748                if( getItem( sid, iid, &item ) == RES_OK ){
3749                    itemid_t item_uid = item -> getContributorUID( );
3750                    freeItem( item );
3751                    if( sess_uid == item_uid && state == index::NOT_CERTIFIED )
3752                        return true;
3753                    else if( index -> getOpenLevel( ) == index::OL_GROUP_ONLY
3754                             && isGroupAdmin( sid, index -> getOwnerGID( ), sess_uid ) )
3755                        return true;
3756                    else if( index -> getOpenLevel( ) == index::OL_PUBLIC
3757                             && isModeratorBySession( sid ) )
3758                        return true;
3759                }
3760            }
3761        }
3762        return false;
3763    }
3764    
3765  result_t getOverlappedItems( sessionid_t sid, indexid_t xid, item_t* item, itemid_t** iids, int* iidsLen ){ return RES_ERROR; }  result_t getOverlappedItems( sessionid_t sid, indexid_t xid, item_t* item, itemid_t** iids, int* iidsLen ){ return RES_ERROR; }
3766    
3767  result_t registerItem( sessionid_t sid, indexid_t xid, itemid_t iid ){ return RES_ERROR; }  /**
3768     *
3769  result_t unregisterItem( sessionid_t sid, indexid_t xid, itemid_t iid ){ return RES_ERROR; }   * インデックスにアイテムを追加する
3770     *
3771     * @param sid セッションID
3772     * @param xid 処理対象のインデックスのID
3773     * @param iid インデックスに追加したいアイテムのID
3774     * @return RES_OK
3775     * @return RES_DB_QUERY_ERROR
3776     * @return RES_NO_SUCH_SESSION
3777     * @return RES_NO_WRITE_ACCESS_RIGHT
3778     * @return RES_ERROR
3779     *
3780     */
3781    result_t registerItem( sessionid_t sid, indexid_t xid, itemid_t iid )
3782    {
3783        if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
3784        
3785        userid_t uid;
3786        const index_t* index;
3787        unsigned int tmp;
3788        char* certify_item_val;
3789        string sql;
3790        
3791        if( !getIndexPermission( sid, xid, index::OP_REGISTER ) ) return RES_NO_WRITE_ACCESS_RIGHT;
3792        
3793        // register the item.
3794        sql = "INSERT INTO " + dbprefix + "_xnpaccount_index_item_link (index_id, item_id, certify_state) values ("
3795            + unsignedIntToString(xid) + ","
3796            + unsignedIntToString(iid) + ","
3797            + unsignedIntToString( getInitialCertifyStateFromConfig( ) ) + ")";
3798        //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
3799        return querySimple( "registerItem", sql );
3800    }
3801    
3802  bool getItemPermission( sessionid_t sid, itemid_t iid, itemop_t op ){ return RES_ERROR; }  /**
3803     *
3804     * インデックスからアイテムを削除する
3805     *
3806     * @param sid セッションID
3807     * @param xid 処理対象のインデックスのID
3808     * @param iid インデックスから削除したいアイテムのID
3809     * @return RES_OK
3810     * @return RES_DB_QUERY_ERROR
3811     * @return RES_NO_SUCH_SESSION
3812     * @return RES_NO_WRITE_ACCESS_RIGHT
3813     * @return RES_ERROR
3814     *
3815     */
3816    result_t unregisterItem( sessionid_t sid, indexid_t xid, itemid_t iid )
3817    {
3818        if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
3819        
3820        userid_t uid;
3821        const index_t* index;
3822        unsigned int tmp;
3823        string sql;
3824        
3825        if( !getIndexPermission( sid, xid, index::OP_UNREGISTER ) ) return RES_NO_WRITE_ACCESS_RIGHT;
3826        
3827        // unregister the item.
3828        sql = "DELETE FROM " + dbprefix + "_xnpaccount_index_item_link";
3829        sql += " WHERE index_id=" + unsignedIntToString(xid);
3830        sql += " AND item_id=" + unsignedIntToString(iid);
3831        //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
3832        return querySimple( "unregisterItem", sql );
3833    }
3834    
3835  result_t insertChangeLog( sessionid_t sid, itemid_t iid, time_t insertTime, const char* log ){ return RES_ERROR; }  /**
3836     *
3837     * バインダーにアイテムを追加する
3838     *
3839     * @param sid セッションID
3840     * @param binderid 処理対象のバインダーのID
3841     * @param iid バインダーに追加したいアイテムのID
3842     * @return RES_OK
3843     * @return RES_DB_QUERY_ERROR
3844     * @return RES_NO_SUCH_SESSION
3845     * @return RES_NO_WRITE_ACCESS_RIGHT
3846     * @return RES_ERROR
3847     *
3848     */
3849    result_t registerBinderItem( sessionid_t sid, itemid_t binderid, itemid_t iid )
3850    {
3851        if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
3852        
3853        userid_t uid;
3854        const index_t* index;
3855        unsigned int tmp;
3856        char* certify_item_val;
3857        string sql;
3858        
3859        if( !getItemPermission( sid, binderid, item::OP_MODIFY ) ) return RES_NO_WRITE_ACCESS_RIGHT;
3860        
3861        // register the item.
3862        sql = "INSERT INTO " + dbprefix + "_xnpaccount_binder_item_link (binder_id, item_id) values ("
3863            + unsignedIntToString(binderid) + ","
3864            + unsignedIntToString(iid) + ")";
3865        //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
3866        return querySimple( "registerBinderItem", sql );
3867    }
3868    
3869  result_t insertChangeLog( sessionid_t sid, itemid_t iid, changelog_t** logs, int logsLen ){ return RES_ERROR; }  /**
3870     *
3871     * バインダーからアイテムを削除する
3872     *
3873     * @param sid セッションID
3874     * @param binderid 処理対象のバインダーのID
3875     * @param iid バインダーから削除したいアイテムのID
3876     * @return RES_OK
3877     * @return RES_DB_QUERY_ERROR
3878     * @return RES_NO_SUCH_SESSION
3879     * @return RES_NO_WRITE_ACCESS_RIGHT
3880     * @return RES_ERROR
3881     *
3882     */
3883    result_t unregisterBinderItem( sessionid_t sid, itemid_t binderid, itemid_t iid )
3884    {
3885        if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
3886        
3887        userid_t uid;
3888        const index_t* index;
3889        unsigned int tmp;
3890        string sql;
3891        
3892        if( !getItemPermission( sid, binderid, item::OP_MODIFY ) ) return RES_NO_WRITE_ACCESS_RIGHT;
3893        
3894        // unregister the item.
3895        sql = "DELETE FROM " + dbprefix + "_xnpaccount_binder_item_link";
3896        sql += " WHERE binder_id=" + unsignedIntToString(binderid);
3897        sql += " AND item_id=" + unsignedIntToString(iid);
3898        //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
3899        return querySimple( "unregisterBinderItem", sql );
3900    }
3901    
3902    /**
3903     *
3904     * アイテムへのアクセス権限をチェックする
3905     *
3906     * @refer itemop_t
3907     * @param sid セッションID
3908     * @param iid チェック対象となるアイテムのID
3909     * @param op アクセスの種類
3910     * @return true 権限あり
3911     * @return false 権限なし
3912     *
3913     */
3914    bool getItemPermission( sessionid_t sid, itemid_t iid, itemop_t op )
3915    {
3916        string sql;
3917        itemid_t itemid;
3918        userid_t uid;
3919        
3920        if( sessionID2UID( sid, &uid ) != RES_OK ) return false;
3921        if( op == item::OP_READ ){
3922            sql = "SELECT DISTINCT tlink.item_id FROM " + dbprefix + "_xnpaccount_index_item_link AS tlink";
3923            sql+= " LEFT JOIN " + dbprefix + "_xnpaccount_index AS tx ON tlink.index_id = tx.index_id";
3924            sql+= " LEFT JOIN " + dbprefix + "_xnpaccount_item_basic AS ti ON tlink.item_id = ti.item_id";
3925            sql+= " LEFT JOIN " + dbprefix + "_xnpaccount_groups_users_link as tgulink ON tx.gid=tgulink.gid AND (ti.uid=";
3926            sql+= unsignedIntToString( uid ) + " OR ";
3927            sql+= string( isModerator( sid, uid ) ? "1" : "0" ) + ") LEFT JOIN ";
3928            sql+= dbprefix + "_xnpaccount_config AS tc ON tc.name=\"" XNP_CONFIG_PUBLIC_ITEM_TARGET_USER_KEY "\" LEFT JOIN ";
3929            sql+= dbprefix + "_xnpaccount_session AS tsess ON tsess.uid=" + unsignedIntToString( uid );
3930            sql+= " WHERE ( tc.value=\"" XNP_CONFIG_PUBLIC_ITEM_TARGET_USER_ALL "\"";
3931            sql+=       " AND tx.open_level=" + unsignedIntToString( index::OL_PUBLIC ) + " AND tsess.uid IS NULL";
3932            sql+=    " OR tc.value!=\"" XNP_CONFIG_PUBLIC_ITEM_TARGET_USER_ALL "\"";
3933            sql+=       " AND tx.open_level=" + unsignedIntToString( index::OL_PUBLIC ) + " AND tsess.uid IS NOT NULL";
3934            sql+=    " OR tgulink.uid=" + unsignedIntToString( uid );
3935            sql+=    " OR tx.open_level=" + unsignedIntToString( index::OL_PRIVATE );
3936            sql+=       " AND tx.uid=" + unsignedIntToString( uid );
3937            sql+=    " OR " + string( isModerator( sid, uid ) ? "1" : "0" );
3938            sql+=    " OR tx.uid IS NULL ";
3939            sql+=    " AND tx.open_level=" + unsignedIntToString( index::OL_PUBLIC );
3940            sql+=    ") AND tlink.item_id=" + unsignedIntToString( iid );
3941            //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
3942            if( queryGetUnsignedInt( "getItemPermission", sql, (unsigned int*)&itemid ) == RES_OK ){
3943                return itemid == iid;
3944            }
3945        }else if( op == item::OP_MODIFY || op == item::OP_DELETE ){
3946            sql = "SELECT item_id FROM " + dbprefix + "_xnpaccount_item_basic";
3947            sql += " WHERE uid=" + unsignedIntToString( uid );
3948            sql += " AND item_id=" + unsignedIntToString( iid );
3949            //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
3950            if( queryGetUnsignedInt( "getItemPermission", sql, (unsigned int*)&itemid ) == RES_OK ){
3951                return itemid == iid;
3952            }
3953        }
3954        return false;
3955    }
3956    
3957    /**
3958     *
3959     * インデックスへのアクセス権限をチェックする
3960     *
3961     * @refer indexop_t
3962     * @param sid セッションID
3963     * @param xid チェック対象となるインデックスのID
3964     * @param op アクセスの種類
3965     * @return true 権限あり
3966     * @return false 権限なし
3967     *
3968     */
3969    bool getIndexPermission( sessionid_t sid, indexid_t xid, indexop_t op )
3970    {
3971        userid_t uid;
3972        unsigned int tmp;
3973        string sql;
3974        if( xid == index::IID_ROOT ) return RES_NO_WRITE_ACCESS_RIGHT;
3975        
3976        if( isModeratorBySession( sid ) );
3977        else if( xid == item::IID_BINDERS );
3978        else if( sessionID2UID( sid, &uid ) == RES_OK ){
3979            sql = "SELECT index_id FROM " + dbprefix + "_xnpaccount_index as tx";
3980            sql += " LEFT JOIN " + dbprefix + "_xnpaccount_users AS tuser ON tx.uid=tuser.uid";
3981            sql += " LEFT JOIN " + dbprefix + "_xnpaccount_groups AS tgroup ON tx.gid=tgroup.gid";
3982            sql += " LEFT JOIN " + dbprefix + "_xnpaccount_groups_users_link AS tlink ON tx.gid=tlink.gid";
3983            sql += " WHERE ( tx.open_level=1";
3984            sql += " OR tx.open_level=2 AND tlink.uid=" + unsignedIntToString( uid );
3985            sql += " OR tx.open_level=3 AND tx.uid=" + unsignedIntToString( uid ) + " )";
3986            sql += " AND index_id=" + unsignedIntToString( xid );
3987            //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
3988            if( queryGetUnsignedInt( "getIndexPermission", sql, &tmp ) == RES_OK
3989                && tmp == xid );
3990            else return false;
3991        }
3992        else return false;
3993        return true;
3994    }
3995    
3996  /**  /**
3997   *   *
# Line 3638  static result_t getIndexesInternal( sess Line 4135  static result_t getIndexesInternal( sess
4135              SQLUINTEGER xid = 0, parentXID = 0, ownerUID = 0, ownerGID = 0, openLevel = 0,              SQLUINTEGER xid = 0, parentXID = 0, ownerUID = 0, ownerGID = 0, openLevel = 0,
4136               sortNumber = 0, itemTypeID = 0, contributorUID = 0, lastUpdateDate = 0, creationDate = 0;               sortNumber = 0, itemTypeID = 0, contributorUID = 0, lastUpdateDate = 0, creationDate = 0;
4137              SQLINTEGER lens[14];              SQLINTEGER lens[14];
4138                memset( lens, 0, sizeof( SQLINTEGER ) * 14 );
4139              SQLBindCol( hstmt, 1, SQL_C_ULONG, &xid, 0, lens+0 );              SQLBindCol( hstmt, 1, SQL_C_ULONG, &xid, 0, lens+0 );
4140              SQLBindCol( hstmt, 2, SQL_C_ULONG, &parentXID, 0, lens+1 );              SQLBindCol( hstmt, 2, SQL_C_ULONG, &parentXID, 0, lens+1 );
4141              SQLBindCol( hstmt, 3, SQL_C_ULONG, &ownerUID, 0, lens+2 );              SQLBindCol( hstmt, 3, SQL_C_ULONG, &ownerUID, 0, lens+2 );
# Line 4055  result_t insertIndex( sessionid_t sid, i Line 4553  result_t insertIndex( sessionid_t sid, i
4553          SQLBindParameter(hstmt,  1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, XNP_ITEM_TITLE_LEN, 0, (SQLCHAR *)index->getTitle(), strlen(index->getTitle()), &cbTitle );          SQLBindParameter(hstmt,  1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, XNP_ITEM_TITLE_LEN, 0, (SQLCHAR *)index->getTitle(), strlen(index->getTitle()), &cbTitle );
4554          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
4555              SQLUINTEGER parentUID = 0, parentGID = 0, parentOpenLevel = 0, childXID = 0, childItemID = 0;              SQLUINTEGER parentUID = 0, parentGID = 0, parentOpenLevel = 0, childXID = 0, childItemID = 0;
4556              SQLINTEGER len1, len2, len3, len4, len5;              SQLINTEGER len1 = 0, len2 = 0, len3 = 0, len4 = 0, len5 = 0;
4557              SQLBindCol( hstmt, 1, SQL_C_ULONG, &parentUID, 0, &len1 );              SQLBindCol( hstmt, 1, SQL_C_ULONG, &parentUID, 0, &len1 );
4558              SQLBindCol( hstmt, 2, SQL_C_ULONG, &parentGID, 0, &len2 );              SQLBindCol( hstmt, 2, SQL_C_ULONG, &parentGID, 0, &len2 );
4559              SQLBindCol( hstmt, 3, SQL_C_ULONG, &parentOpenLevel, 0, &len3 );              SQLBindCol( hstmt, 3, SQL_C_ULONG, &parentOpenLevel, 0, &len3 );
# Line 4163  result_t getDescendantIndexID( int xid, Line 4661  result_t getDescendantIndexID( int xid,
4661              string sql = "SELECT index_id FROM " + dbprefix + "_xnpaccount_index WHERE parent_index_id=" + unsignedIntToString(xid);              string sql = "SELECT index_id FROM " + dbprefix + "_xnpaccount_index WHERE parent_index_id=" + unsignedIntToString(xid);
4662              if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){              if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
4663                  SQLUINTEGER sXID = 0;                  SQLUINTEGER sXID = 0;
4664                  SQLINTEGER len;                  SQLINTEGER len = 0;
4665                  SQLBindCol( hstmt, 1, SQL_C_ULONG, &sXID, 0, &len );                  SQLBindCol( hstmt, 1, SQL_C_ULONG, &sXID, 0, &len );
4666                  while ( ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS ){                  while ( ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS ){
4667                      *pFill++ = sXID;                      *pFill++ = sXID;
# Line 4223  static result_t getAllIndexLink( indexLi Line 4721  static result_t getAllIndexLink( indexLi
4721          string sql("SELECT index_id, parent_index_id FROM " + dbprefix + "_xnpaccount_index ORDER BY index_id");          string sql("SELECT index_id, parent_index_id FROM " + dbprefix + "_xnpaccount_index ORDER BY index_id");
4722          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){          if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
4723              SQLUINTEGER xid = 0, parentXID = 0;              SQLUINTEGER xid = 0, parentXID = 0;
4724              SQLINTEGER len1, len2;              SQLINTEGER len1 = 0, len2 = 0;
4725              SQLBindCol( hstmt, 1, SQL_C_ULONG, &xid, 0, &len1 );              SQLBindCol( hstmt, 1, SQL_C_ULONG, &xid, 0, &len1 );
4726              SQLBindCol( hstmt, 2, SQL_C_ULONG, &parentXID, 0, &len2 );              SQLBindCol( hstmt, 2, SQL_C_ULONG, &parentXID, 0, &len2 );
4727                            
# Line 4499  result_t updateIndexInternal( sessionid_ Line 4997  result_t updateIndexInternal( sessionid_
4997              }              }
4998              freeIndexID( descXID );              freeIndexID( descXID );
4999                            
5000              // todo: インデックスが移動した場合は、アイテムの所有者に何らかの通知を行う。              // todo: インデックスが移動した場合は、アイテムの所有者に何らかの通知を行う。(PHP)
5001          }          }
5002      }      }
5003            
# Line 4624  result_t deleteIndex( sessionid_t sid, Line 5122  result_t deleteIndex( sessionid_t sid,
5122                          " WHERE t1.index_id=" + strXID + " and t2.item_id is NULL" );                          " WHERE t1.index_id=" + strXID + " and t2.item_id is NULL" );
5123                      if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){                      if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
5124                          SQLUINTEGER sLinkID = 0;                          SQLUINTEGER sLinkID = 0;
5125                          SQLINTEGER len;                          SQLINTEGER len = 0;
5126                          SQLBindCol( hstmt, 1, SQL_C_ULONG, &sLinkID, 0, &len );                          SQLBindCol( hstmt, 1, SQL_C_ULONG, &sLinkID, 0, &len );
5127                          while ( ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS ){                          while ( ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS ){
5128                              string sql2( "UPDATE " + linkTable + " set index_id= " + strParentXID + " where index_item_link_id = " + unsignedIntToString(sLinkID) );                              string sql2( "UPDATE " + linkTable + " set index_id= " + strParentXID + " where index_item_link_id = " + unsignedIntToString(sLinkID) );
# Line 4744  result_t swapIndexSortNumber( sessionid_ Line 5242  result_t swapIndexSortNumber( sessionid_
5242      return result;      return result;
5243  }  }
5244    
5245    /**
5246     *
5247     * 設定名keyに値をvauleを設定する
5248     *
5249     * @param key 設定キー名
5250     * @param value 設定値
5251     *
5252     * @return RES_OK
5253     * @return RES_DB_QUERY_ERROR
5254     * @return RES_ERROR
5255     */
5256    result_t setConfigValue( const char* key, const char* value )
5257    {
5258        SQLRETURN sqlcode;
5259        SQLHANDLE hstmt = NULL;    
5260        SQLHANDLE hstmt2 = NULL;    
5261        SQLINTEGER cbKey = SQL_NTS, cbValue = SQL_NTS;
5262        result_t ret = RES_ERROR;
5263        
5264        if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
5265            string sql( "UPDATE " + dbprefix + "_xnpaccount_config SET value=? WHERE name=?");
5266            //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
5267            sqlcode = SQLPrepare(hstmt, (SQLCHAR*)sql.c_str(), SQL_NTS);
5268            if( sqlcode == SQL_SUCCESS || sqlcode == SQL_SUCCESS_WITH_INFO ){
5269                SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, SQL_DESC_LENGTH, 0, (SQLCHAR*)value, strlen(value), &cbValue );
5270                SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, XNP_CONFIG_NAME_LEN, 0, (SQLCHAR*)key  , strlen(key)  , &cbKey );
5271                if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
5272                    SQLINTEGER count = 0;
5273                    if( ( sqlcode = SQLRowCount( hstmt, &count ) ) == SQL_SUCCESS && count > 0 ){
5274                        ret = RES_OK;
5275                    }else if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt2 ) ) == SQL_SUCCESS ) {
5276                        string sql( "INSERT INTO " + dbprefix + "_xnpaccount_config (name,value) VALUES (?,?)");
5277                        //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
5278                        sqlcode = SQLPrepare(hstmt2, (SQLCHAR*)sql.c_str(), SQL_NTS);
5279                        if( sqlcode == SQL_SUCCESS || sqlcode == SQL_SUCCESS_WITH_INFO ){
5280                            SQLBindParameter(hstmt2, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, XNP_CONFIG_NAME_LEN, 0, (SQLCHAR*)key  , strlen(key)  , &cbKey );
5281                            SQLBindParameter(hstmt2, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, SQL_DESC_LENGTH, 0, (SQLCHAR*)value, strlen(value), &cbValue );
5282                            if( ( sqlcode = SQLExecDirect( hstmt2, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
5283                                if( ( sqlcode = SQLRowCount( hstmt2, &count ) ) == SQL_SUCCESS && count > 0 ){
5284                                    ret = RES_OK;
5285                                }
5286                            }else{
5287                                string s( "SQLExecDirect in setConfigValue" );
5288                                s += odbcDiagString( SQL_HANDLE_STMT, hstmt2, sqlcode );
5289                                s += "sql=";
5290                                s += string( sql );
5291                                setLastErrorString( s.c_str( ) );
5292                                ret = RES_DB_QUERY_ERROR;
5293                            }
5294                        }else {
5295                            setLastErrorString( "SQLPrepare in setConfigValue" );
5296                            ret = RES_ERROR;
5297                        }
5298                        SQLFreeHandle( SQL_HANDLE_STMT, hstmt2 );
5299                    }else{
5300                        setLastErrorString( "SQLAllocHandle(SQL_HANDLE_STMT,...) in setConfigValue" );
5301                        ret = RES_ERROR;
5302                    }
5303                }else{
5304                    string s( "SQLExecDirect in setConfigValue" );
5305                    s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );
5306                    s += "sql=";
5307                    s += string( sql );
5308                    setLastErrorString( s.c_str( ) );
5309                    ret = RES_DB_QUERY_ERROR;
5310                }
5311            }else {
5312                setLastErrorString( "SQLPrepare in setConfigValue" );
5313                ret = RES_ERROR;
5314            }
5315            SQLFreeHandle( SQL_HANDLE_STMT, hstmt );
5316        }else{
5317            setLastErrorString( "SQLAllocHandle(SQL_HANDLE_STMT,...) in setConfigValue" );
5318            ret = RES_ERROR;
5319        }
5320        return ret;
5321    }
5322    
5323    /**
5324     *
5325     * 設定名keyに対応する値をvauleに取得する.
5326     * valueの使用後はfreeStringで解放する.
5327     * @param key 設定キー名
5328     * @param value 設定値を受け取るポインタ
5329     *
5330     * @return RES_OK
5331     * @return RES_DB_QUERY_ERROR
5332     * @return RES_ERROR
5333     */
5334    result_t getConfigValue( const char* key, char** value )
5335    {
5336        SQLRETURN sqlcode;
5337        SQLHANDLE hstmt = NULL;    
5338        SQLINTEGER cbKey = SQL_NTS;
5339        result_t ret = RES_ERROR;
5340        
5341        if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
5342            string sql("SELECT value FROM " + dbprefix + "_xnpaccount_config WHERE name=?");
5343            sqlcode = SQLPrepare(hstmt, (SQLCHAR*)sql.c_str(), SQL_NTS);
5344            if( sqlcode == SQL_SUCCESS || sqlcode == SQL_SUCCESS_WITH_INFO ){
5345                SQLBindParameter(hstmt,  1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, XNP_CONFIG_NAME_LEN, 0, (SQLCHAR*)key, strlen(key), &cbKey );
5346                if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
5347                    while( SQLFetch( hstmt ) == SQL_SUCCESS ){
5348                        string valuestr;
5349                        SQLINTEGER length;
5350                        //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
5351                        // get amount of data
5352                        if( SQLGetData(hstmt, 1, SQL_C_BINARY, *value, 0, &length)
5353                            == SQL_SUCCESS_WITH_INFO ){
5354                        
5355                        // Get all the data at once.
5356                            *value = new char[ length + 1 ];
5357                            memset( *value, '\0', length + 1 );
5358                            if( SQLGetData(hstmt, 1, SQL_C_DEFAULT, *value,
5359                                           length, &length) == SQL_SUCCESS ){
5360                                ret = RES_OK;
5361                            }else{
5362                                string s( "SQLGetData in getConfigValue" );
5363                                s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );
5364                                s += "sql=";
5365                                s += string( sql );
5366                                setLastErrorString( s.c_str( ) );
5367                                ret = RES_DB_QUERY_ERROR;
5368                            }
5369                        }else{
5370                            string s( "SQLGetData in getConfigValue" );
5371                            s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );
5372                            s += "sql=";
5373                            s += string( sql );
5374                            setLastErrorString( s.c_str( ) );
5375                            ret = RES_DB_QUERY_ERROR;
5376                        }
5377                        break;
5378                    }
5379                }else{
5380                    string s( "SQLExecDirect in getConfigValue" );
5381                    s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );
5382                    s += "sql=";
5383                    s += string( sql );
5384                    setLastErrorString( s.c_str( ) );
5385                    ret = RES_DB_QUERY_ERROR;
5386                }
5387            }else {
5388                string s( "SQLPrepare in getConfigValue" );
5389                s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );
5390                s += "sql=";
5391                s += string( sql );
5392                setLastErrorString( s.c_str( ) );
5393                ret = RES_ERROR;
5394            }
5395            SQLFreeHandle( SQL_HANDLE_STMT, hstmt );
5396        }else{
5397            setLastErrorString( "SQLAllocHandle(SQL_HANDLE_STMT,...) in getConfigValue" );
5398            ret = RES_ERROR;
5399        }
5400        return ret;
5401    }
5402    
5403    /**
5404     *
5405     * アイテムの変更履歴ログを取得する.
5406     *
5407     * @param sid セッションID
5408     * @param itemid 変更履歴を取得するアイテムのID
5409     * @param logs ログ内容を受け取るポインタ
5410     * @param logsLen logsに格納されたログの個数
5411     * @return RES_OK
5412     * @return RES_NO_SUCH_SESSION
5413     * @return RES_NO_READ_ACCESS_RIGHT
5414     * @return RES_DB_NOT_INITIALIZED
5415     * @return RES_ERROR
5416     *
5417     */
5418    result_t getChangeLogs( sessionid_t sid, itemid_t itemid, const changelog_t** logs, int* logsLen )
5419    {
5420        if( hdbc == NULL ) return RES_DB_NOT_INITIALIZED;
5421        if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
5422        if( !getItemPermission( sid, itemid, item::OP_READ ) ) return RES_NO_READ_ACCESS_RIGHT;
5423        
5424        result_t ret = RES_ERROR;
5425        SQLRETURN sqlcode;
5426        SQLHANDLE hstmt = NULL;
5427        SQLINTEGER count = 0;
5428        string sql;
5429        SQLINTEGER len = 0;
5430        time_t log_date = 0;
5431        
5432        sql = "SELECT log_date, log FROM " +  dbprefix + "_xnpaccount_changelog"
5433            + " WHERE item_id=" + unsignedIntToString( itemid )
5434            + " ORDER BY log_date DESC, log_id DESC";
5435        if( countResultRows( sql.c_str(), &count ) == RES_OK ){
5436            changelog_t* logs_ = new changelog_t[ count ];
5437            if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
5438                if( ( sqlcode = SQLExecDirect( hstmt, (SQLCHAR*)sql.c_str(), sql.length() ) ) == SQL_SUCCESS ){
5439                    SQLBindCol( hstmt, 1, SQL_C_ULONG, &log_date, 0, &len );
5440                    for( *logsLen=0; ( sqlcode = SQLFetch( hstmt ) ) == SQL_SUCCESS && *logsLen < count ; (*logsLen)++ ){
5441                        ( logs_ + *logsLen ) -> setDate( log_date );
5442                        ( logs_ + *logsLen ) -> setLog( getResultCol( hstmt, 2 ).c_str() );
5443                    }
5444                    *logs = logs_;
5445                    ret = RES_OK;
5446                }else{
5447                    string s( "SQLExecDirect in getChangeLogs ");
5448                    s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );
5449                    s += string( ", sql=" ) + string( sql );
5450                    setLastErrorString( s.c_str( ) );
5451                    ret = RES_ERROR;
5452                }
5453            }else {
5454                string s( "SQLAllocHandle in updateIndex ");
5455                s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );
5456                setLastErrorString( s.c_str( ) );
5457                ret = RES_ERROR;
5458            }
5459        }
5460        return ret;
5461    }
5462    
5463    
5464    /**
5465     *
5466     * 変更履歴ログを記録する.
5467     *
5468     * @param sid セッションID
5469     * @param itemid 変更履歴を記録するアイテムのID
5470     * @param log ログ内容
5471     * @return RES_OK
5472     * @return RES_NO_SUCH_SESSION
5473     * @return RES_DB_QUERY_ERROR
5474     * @return RES_NO_WRITE_ACCESS_RIGHT
5475     * @return RES_DB_NOT_INITIALIZED
5476     * @return RES_ERROR
5477     *
5478     */
5479    result_t insertChangeLog( sessionid_t sid, itemid_t itemid, const char* log )
5480    {
5481        if( hdbc == NULL ) return RES_DB_NOT_INITIALIZED;
5482        if( !isValidSessionID( sid ) ) return RES_NO_SUCH_SESSION;
5483        if( !getItemPermission( sid, itemid, item::OP_MODIFY ) ) return RES_NO_WRITE_ACCESS_RIGHT;
5484        
5485        result_t ret = RES_ERROR;
5486        string sql;
5487        SQLHANDLE hstmt = NULL;
5488        SQLRETURN sqlcode;
5489        
5490        // insert change log
5491        if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
5492            SQLINTEGER cbLog = SQL_NTS;
5493            string sql;
5494            sql = "INSERT INTO " + dbprefix + "_xnpaccount_changelog (item_id, log_date, log) VALUES (";
5495            sql += unsignedIntToString( itemid ) + ", ";
5496            sql += unsignedIntToString( time( NULL ) ) + ", ?)";
5497            //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
5498            sqlcode = SQLPrepare(hstmt, (SQLCHAR*)sql.c_str(), SQL_NTS);
5499            if( sqlcode == SQL_SUCCESS || sqlcode == SQL_SUCCESS_WITH_INFO ){
5500                SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, XNP_CHANGELOG_LOG_LEN, 0,
5501                                 (SQLCHAR*)( log ), strlen( log ), &cbLog );
5502                if( ( sqlcode = SQLExecute( hstmt ) ) == SQL_SUCCESS ){
5503                    ret = RES_OK;
5504                }else{
5505                    string s( "SQLExecute in insertChangeLog " );
5506                    s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );
5507                    setLastErrorString( s.c_str( ) );
5508                    ret = RES_DB_QUERY_ERROR;
5509                }
5510            }else{
5511                string s( "SQLPrepare in insertChangeLog " );
5512                s += odbcDiagString( SQL_HANDLE_STMT, hstmt, sqlcode );
5513                setLastErrorString( s.c_str( ) );
5514                ret = RES_ERROR;
5515            }
5516            SQLFreeHandle( SQL_HANDLE_STMT, hstmt );
5517        }
5518        return ret;
5519    }
5520    
5521  void freeIndex( const index_t* ptr ){ delete[] ptr; }  void freeIndex( const index_t* ptr ){ delete[] ptr; }
5522  void freeIndexID( const indexid_t* ptr ){ delete[] ptr; }  void freeIndexID( const indexid_t* ptr ){ delete[] ptr; }
5523  void freeAccount( const account_t* ptr ){ delete[] ( account_t* )ptr; }  void freeAccount( const account_t* ptr ){ delete[] ( account_t* )ptr; }
# Line 4753  void freeUID( const userid_t* ptr ){ del Line 5527  void freeUID( const userid_t* ptr ){ del
5527  void freeGID( const groupid_t* ptr ){ delete[] ( groupid_t* )ptr; }  void freeGID( const groupid_t* ptr ){ delete[] ( groupid_t* )ptr; }
5528  void freeItem( const item_t* ptr ){ delete[] ( item_t* )ptr; }  void freeItem( const item_t* ptr ){ delete[] ( item_t* )ptr; }
5529  void freeItemID( const itemid_t* ptr ){ delete[] ( itemid_t* )ptr; }  void freeItemID( const itemid_t* ptr ){ delete[] ( itemid_t* )ptr; }
5530    void freeString( char* str ){ delete[] str; }
5531    void freeChangeLog( const changelog_t* ptr ){ delete[] ( changelog_t* )ptr; }
5532    
5533  static string errstr;  static string errstr;
5534  const char* getLastErrorString()  const char* getLastErrorString()
# Line 4768  void setLastErrorString( const char* str Line 5544  void setLastErrorString( const char* str
5544      syslog( LOG_DEBUG, "%s", str );      syslog( LOG_DEBUG, "%s", str );
5545      closelog( );      closelog( );
5546  #endif  #endif
5547  //    fprintf( stderr, "setLastErrorString( '%s' );\n", str );      //fprintf( stderr, "\nsetLastErrorString( '%s' );\n", str );
5548  }  }

Legend:
Removed from v.1.43  
changed lines
  Added in v.1.44

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