Index: trunk/wp-content/plugins/xpressme/include/user_sync_xoops.php
===================================================================
--- trunk/wp-content/plugins/xpressme/include/user_sync_xoops.php	(revision 85)
+++ trunk/wp-content/plugins/xpressme/include/user_sync_xoops.php	(revision 87)
@@ -34,33 +34,33 @@
  * in the WordPress authority given to the group to which the user belongs are obtained. 
 */
-	function get_xoops_group_role($mydirname , $uid=0){
-		$ans = array();
-		if ($uid == 0){
-			return '';
-		}
-		
-		global $xoops_db;
-		$db_groups_users_link = get_xoops_prefix() . 'groups_users_link';
-		$db_group_role = get_wp_prefix() . 'group_role';
-		
-		$sql  = "SELECT * ";
-		$sql .= "FROM $db_groups_users_link ";
-		$sql .= "LEFT JOIN $db_group_role ON $db_groups_users_link.groupid = $db_group_role.groupid ";
-		$sql .= "WHERE  uid = $uid";
-	
-		$user_groups = $xoops_db->get_results($sql);
-		$ans['capabillities'] = '';
-		$ans['allway_update'] = 0;
-		
-		foreach($user_groups as $user_group){
-			$before_level = get_role_level($ans['capabillities']);
-			$now_level = get_role_level($user_group->role);
-			if ($now_level > $before_level){
-				$ans['capabillities'] = $user_group->role;
-				$ans['allway_update'] = $user_group->login_all;
-			}
-		}
-		return $ans;
-	}
+function get_xoops_group_role($uid=0){
+	$ans = array();
+	if ($uid == 0){
+		return '';
+	}
+	
+	global $xoops_db;
+	$db_groups_users_link = get_xoops_prefix() . 'groups_users_link';
+	$db_group_role = get_wp_prefix() . 'group_role';
+	
+	$sql  = "SELECT * ";
+	$sql .= "FROM $db_groups_users_link ";
+	$sql .= "LEFT JOIN $db_group_role ON $db_groups_users_link.groupid = $db_group_role.groupid ";
+	$sql .= "WHERE  uid = $uid";
+
+	$user_groups = $xoops_db->get_results($sql);
+	$ans['capabillities'] = '';
+	$ans['allway_update'] = 0;
+	
+	foreach($user_groups as $user_group){
+		$before_level = get_role_level($ans['capabillities']);
+		$now_level = get_role_level($user_group->role);
+		if ($now_level > $before_level){
+			$ans['capabillities'] = $user_group->role;
+			$ans['allway_update'] = $user_group->login_all;
+		}
+	}
+	return $ans;
+}
 	
 /*
@@ -74,45 +74,11 @@
 }
 
-
-//if( ! function_exists( 'xpress_usermeta_sync' ) ) :
-	function xpress_usermeta_sync($mydirname , $uid,$meta_key,$meta_value, &$message){
-		$wp_prefix = get_wp_prefix();
-		$meta_key = "'" . $meta_key . "'";
-		$meta_value = "'" . $meta_value . "'";
-		$xoopsDB =& Database::getInstance();
-		$db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta');
-		$sql = "SELECT user_id FROM $db_xpress_usermeta WHERE (user_id = $uid) AND (meta_key = $meta_key)";
-		$res =  $xoopsDB->query($sql, 0, 0);
-
-		if ($res === false){
-			$sql2 = "INSERT INTO $db_xpress_usermeta (user_id,meta_key,meta_value) VALUES ($uid,$meta_key,$meta_value)";
-		}else {
-			if ($xoopsDB->getRowsNum($res)  > 0){
-				
-				$sql2 = "UPDATE $db_xpress_usermeta SET meta_value = $meta_value WHERE (user_id = $uid) AND (meta_key = $meta_key)";
-			} else {
-				$sql2 = "INSERT INTO $db_xpress_usermeta (user_id,meta_key,meta_value) VALUES ($uid,$meta_key,$meta_value)";
-			}
-		}
-		$res = $xoopsDB->queryF($sql2, 0, 0);
-		if ($res === false) {
-			$message .= "...ERR($sql2)";
-			return false;
-		} else {
-			return true;
-		}
-
-	}
-//endif;
-
 /*
  * The user data of XOOPS is written in the WordPress user data. 
  * If $sync_uid is 0, all users are written. 
 */
-function user_sync_to_wordpress($sync_uid = 0, &$message)
+function user_sync_to_wordpress($sync_uid = 0, &$message){
 	global $xoops_db;
 	global $xoopsModule;
-
-	$mydirname = basename( dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) ) );
 
 	if( is_object($xoopsModule)) {
@@ -135,5 +101,5 @@
 
 		foreach($xoops_users as $xoops_user){
-			$role = get_xoops_group_role($mydirname , $xoops_user->uid);
+			$role = get_xoops_group_role($xoops_user->uid);
 			$capabillities_name = $role['capabillities'];
 			$allway_update = $role['allway_update'];
@@ -145,35 +111,21 @@
 				}
 			} else {
-				$user_ID = 			$xoops_user->uid;
-				$user_login_name = 	$xoops_user->uname;
-				$user_pass_md5 = 	$xoops_user->pass;
-				$user_email = 		$xoops_user->email;
-				$user_url = 		$xoops_user->url;
 				$user_regist_time = date('Y-m-d H:i:s' , $xoops_user->user_regdate);
-				$user_nicename =	$xoops_user->uname;
 				$user_status = 0;
 				$user_display_name =empty($xoops_user->name) ? $xoops_user->uname :$xoops_user->name ;
-				$user_nickname =	empty($xoops_user->name) ? $xoops_user->uname :$xoops_user->name ;
-				$user_aim =			$xoops_user->user_aim;
-				$user_yim =			$xoops_user->user_yim;
-				$user_first_name = 	$xoops_user->uname;
-				$user_last_name = 	'';
-				$user_description = '';
-				$user_jabber = 		'';
-				$user_rich_editing = 'true';
 
 				$is_update = false;
 				
-				$wp_user_id = get_wp_user_id($user_login_name) ;
+				$wp_user_id = get_wp_user_id($xoops_user->uname) ;
 				if ($wp_user_id){
 					$wu_sql  = 	"UPDATE $db_xpress_users ";
 					$wu_sql .= 	'SET ';
-					$wu_sql .=		"user_pass  = '$user_pass_md5', ";
-					$wu_sql .=		"user_email = '$user_email', ";
-					$wu_sql .=		"user_url = '$user_url', ";
-					$wu_sql .=		"user_nicename = '$user_nicename', ";
+					$wu_sql .=		"user_pass  = '$xoops_user->pass', ";
+					$wu_sql .=		"user_email = '$xoops_user->email', ";
+					$wu_sql .=		"user_url = '$xoops_user->url', ";
+					$wu_sql .=		"user_nicename = '$xoops_user->uname', ";
 					$wu_sql .=		"user_registered = '$user_regist_time', ";
 					$wu_sql .=		"user_status = 0 ";
-					$wu_sql .=	"WHERE (user_login = '$user_login_name' )";
+					$wu_sql .=	"WHERE (user_login = '$xoops_user->uname' )";
 
 					$xoops_db->query($wu_sql);
@@ -191,22 +143,30 @@
 					$wu_sql .=		" , user_registered , user_status , display_name) ";
 					$wu_sql .=	"VALUES ";
-					$wu_sql .=		"('$user_login_name', '$user_pass_md5', '$user_email', '$user_url', '$user_nicename' ";
+					$wu_sql .=		"('$xoops_user->uname', '$xoops_user->pass', '$xoops_user->email', '$xoops_user->url', '$xoops_user->uname' ";
 					$wu_sql .=		" , '$user_regist_time', $user_status, '$user_display_name')";
 					$xoops_db->query($wu_sql);
-					$wp_user_id = get_wp_user_id($user_login_name) ;					
+//					$wp_user_id = get_wp_user_id($user_login_name) ;					
+ 					$wp_user_id = mysql_insert_id();
 					$sycc_user = new WP_User($wp_user_id);
 					$sycc_user->set_role($capabillities_name);						
 					$message .= '...INSERT ' . $user_login_name . '(' . $capabillities_name . ')';
 				}
-
-				if(!xpress_usermeta_sync($mydirname, $user_ID,'nickname',$user_nickname,$message) )return false;
-				if(!xpress_usermeta_sync($mydirname, $user_ID,'first_name',$user_first_name,$message) )return false;
-				if(!xpress_usermeta_sync($mydirname, $user_ID,'last_name',$user_last_name,$message) )return false;
-				if(!xpress_usermeta_sync($mydirname, $user_ID,'description',$user_description,$message) )return false;
-				if(!xpress_usermeta_sync($mydirname, $user_ID,'jabber',$user_jabber,$message) )return false;
-				if(!xpress_usermeta_sync($mydirname, $user_ID,'aim',$user_aim,$message) )return false;
-				if(!xpress_usermeta_sync($mydirname, $user_ID,'yim',$user_yim,$message) )return false;
+				
+				$user_nickname =	empty($xoops_user->name) ? $xoops_user->uname :$xoops_user->name ;
+				$user_rich_editing = 'true';
+				$user_first_name = 	$xoops_user->uname;
+				$user_last_name = 	'';
+				$user_description = '';
+				$user_jabber = 		'';
+
+				update_usermeta( $wp_user_id,'nickname',$user_nickname);
+				update_usermeta( $wp_user_id,'first_name',$user_first_name);
+				update_usermeta( $wp_user_id,'last_name',$user_last_name);
+				update_usermeta( $wp_user_id,'description',$user_description);
+				update_usermeta( $wp_user_id,'jabber',$user_jabber);
+				update_usermeta( $wp_user_id,'aim',$xoops_user->user_aim);
+				update_usermeta( $wp_user_id,'yim',$xoops_user->user_yim);
 				if ($is_update === false ) {
-					if(!xpress_usermeta_sync($mydirname, $user_ID,'rich_editing',$user_rich_editing,$message) )return false;
+					update_usermeta( $wp_user_id,'rich_editing',$user_rich_editing);
 				}			
 			}
@@ -216,98 +176,4 @@
 	}
 }
-
-//if( ! function_exists( 'check_user_role_prefix' ) ) :
-	function check_user_role_prefix($mydirname){
-		$wp_prefix = get_wp_prefix();
-		$xoopsDB =& Database::getInstance();		
-		$db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta');
-		$user_meta_prefix =$xoopsDB->prefix($wp_prefix);
-		
-		$sql = "SELECT * FROM $db_xpress_usermeta WHERE meta_key LIKE '%_" . $wp_prefix . "user_level' OR meta_key LIKE '%_" . $wp_prefix . "capabilities' OR meta_key LIKE '%_" . $wp_prefix . "autosave_draft_ids'";
-		$result =  $xoopsDB->query($sql, 0, 0);
-		if ($result === false){
-			return -1;
-		}else{
-			$i=0;
-			while($row = $xoopsDB->fetchArray($result)){
-				$meta_key = $row['meta_key'];
-				if (strpos($meta_key,$user_meta_prefix) === false) {
-					$i++;
-				}
-			}
-			return $i;
-		}
-	}
-//endif;
-
-//if( ! function_exists( 'same_metakey_delete' ) ) :
-	function same_metakey_delete($mydirname,$metakey){
-		$wp_prefix = get_wp_prefix();
-		$xoopsDB =& Database::getInstance();		
-		$db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta');
-		$user_meta_prefix =$xoopsDB->prefix($wp_prefix);
-		
-		$sql = "DELETE FROM $db_xpress_usermeta WHERE meta_key LIKE '$metakey'";
-		$result =  $xoopsDB->query($sql, 0, 0);
-		if ($result === false){
-			return false;
-		}else{
-			return true;
-		}
-	}
-//endif;
-
-//if( ! function_exists( 'repair_user_role_prefix' ) ) :
-	function repair_user_role_prefix($mydirname){
-		$wp_prefix = get_wp_prefix();
-		$xoopsDB =& Database::getInstance();		
-		$db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta');
-		$user_meta_prefix =$xoopsDB->prefix($wp_prefix);
-		$ret = '';
-		
-		$sql = "SELECT * FROM $db_xpress_usermeta WHERE meta_key LIKE '%_" . $wp_prefix . "user_level' OR meta_key LIKE '%_" . $wp_prefix . "capabilities' OR meta_key LIKE '%_" . $wp_prefix . "autosave_draft_ids'";
-		$result =  $xoopsDB->queryF($sql, 0, 0);
-		if ($result === false){
-			return -1;
-		}else{
-			$i=0;
-			while($row = $xoopsDB->fetchArray($result)){
-				$meta_key = $row['meta_key'];
-				$umeta_id = $row['umeta_id'];
-				if (strpos($meta_key,$user_meta_prefix) === false) {
-					$new_meta_key = '';
-					if (strpos($meta_key,$wp_prefix.'user_level')){
-						$new_meta_key = $xoopsDB->prefix($wp_prefix . 'user_level');
-					}
-					
-					if (strpos($meta_key,$wp_prefix.'capabilities')){
-						$new_meta_key = $xoopsDB->prefix($wp_prefix . 'capabilities');
-					}
-					
-					if (strpos($meta_key,$wp_prefix.'autosave_draft_ids')){
-						$new_meta_key = $xoopsDB->prefix($wp_prefix . 'autosave_draft_ids');
-					}
-					
-					if (!empty($new_meta_key)){
-						same_metakey_delete($mydirname,$new_meta_key);
-
-						$repair_sql  = 	"UPDATE $db_xpress_usermeta ";
-						$repair_sql .= 	'SET ';
-						$repair_sql .=	"meta_key = '$new_meta_key' ";
-						$repair_sql .=	"WHERE (umeta_id = $umeta_id )";
-						$repair_res = $xoopsDB->queryF($repair_sql, 0, 0);
-						if ($repair_res === false){
-							$ret .= '...ERR(' . $meta_key . '->' . $new_meta_key . ')<br>';
-						} else {
-							$ret .= '...DO(' . $meta_key . '->' . $new_meta_key . ')<br>';
-						}
-					}
-				}
-			}
-		}
-		return $ret;
-	}
-//endif;		
-
 
 /*
@@ -365,3 +231,57 @@
 	}
 }
+
+
+/*
+ * When I changed the pre-fix of the database in XOOPS Protector Module, user authority data of WordPress read it and cannot do it. 
+ * This is because a meta_key pre-fix of the usermeta table of WordPress is not changed.
+ * and  user_roles option_name pre-fix of the option table of WordPress is not changed.
+*/
+function repair_user_meta_prefix(){
+	global $xoops_db;
+
+	// repair usermeta db
+	$db_xpress_usermeta = get_wp_prefix() . 'usermeta';
+	$wp_prefix_only = get_wp_prefix_only();
+	$user_meta_prefix = get_wp_prefix();
+	$sql = "SELECT * FROM $db_xpress_usermeta WHERE meta_key LIKE '%_" . $wp_prefix_only . "%'" ;
+	$user_metas = $xoops_db->get_results($sql);
+	foreach($user_metas as $user_meta){
+		if (strpos($user_meta->meta_key,$user_meta_prefix) === false) {
+			$new_meta_key = '';
+			if (strpos($user_meta->meta_key,$wp_prefix_only.'user_level'))
+				$new_meta_key = $user_meta_prefix . 'user_level';
+			if (strpos($user_meta->meta_key,$wp_prefix_only.'capabilities'))
+				$new_meta_key = $user_meta_prefix . 'capabilities';
+			if (strpos($user_meta->meta_key,$wp_prefix_only.'autosave_draft_ids'))
+				$new_meta_key = $user_meta_prefix . 'autosave_draft_ids';
+			if (strpos($user_meta->meta_key,$wp_prefix_only.'usersettings')){
+				if (strpos($user_meta->meta_key,$wp_prefix_only.'usersettingstime'))
+					$new_meta_key = $user_meta_prefix . 'usersettingstime';
+				else
+					$new_meta_key = $user_meta_prefix . 'usersettings';
+			}
+			if (!empty($new_meta_key)){
+				$repair_sql  = 	"UPDATE $db_xpress_usermeta ";
+				$repair_sql .= 	'SET ';
+				$repair_sql .=	"meta_key = '$new_meta_key' ";
+				$repair_sql .=	"WHERE (umeta_id = $user_meta->umeta_id )";
+				$xoops_db->query($repair_sql);
+			}
+		}
+	}
+	
+	// repair option db user_roles
+	$db_wp_option = get_wp_prefix() . 'options';
+	$new_option_name = get_wp_prefix() . 'user_roles';
+	$sql = "SELECT option_id , option_name FROM $db_wp_option WHERE option_name LIKE '%_" . $wp_prefix_only . "user_roles'" ;
+	$option= $xoops_db->get_row($sql);
+	if ($option->option_name != $new_option_name){
+		$repair_sql  = 	"UPDATE $db_wp_option ";
+		$repair_sql .= 	'SET ';
+		$repair_sql .=	"option_name = '$new_option_name' ";
+		$repair_sql .=	"WHERE (option_id = $option->option_id )";
+		$xoops_db->query($repair_sql);
+	}
+}
 ?>
