Index: trunk/wp-content/plugins/xpressme/include/user_sync_xoops.php
===================================================================
--- trunk/wp-content/plugins/xpressme/include/user_sync_xoops.php	(revision 82)
+++ trunk/wp-content/plugins/xpressme/include/user_sync_xoops.php	(revision 85)
@@ -1,108 +1,81 @@
 <?php
-	function get_xpress_mid($mydirname){
-		$xoopsDB =& Database::getInstance();
-		$module_tbl = $xoopsDB->prefix('modules');		
-		$module_dir = XOOPS_ROOT_PATH . '/modules/' . $mydirname;
-		$module_id = 0;
-
-		$sql = "SELECT mid FROM $module_tbl WHERE dirname LIKE '$mydirname'";
-		$result =  $xoopsDB->query($sql, 0, 0);
-		if ($xoopsDB->getRowsNum($result)  > 0){
-			$row = $xoopsDB->fetchArray($result);
-			$module_id = $row['mid'];
-		}
-		return $module_id;
-	}
-
-
-	function get_xoops_group_capabillities_name($mydirname , $uid=0){
+/*	
+ * The default authority setting value given by a new user of WordPress
+*/
+function get_default_capabillities(){
+	global $xoops_db;
+	$db_option = get_wp_prefix() . 'options';
+	return $xoops_db->get_var("SELECT option_value FROM $db_option WHERE option_name = 'default_role'");
+}
+
+/*
+ * Get The level from the capabillities name.
+ */
+function get_role_level($capabillities){
+	if ($capabillities == 'default') $capabillities = get_default_capabillities();
+	switch($capabillities) {
+		case 'administrator':
+			return 5;
+		case 'editor':
+			return 4;
+		case 'author':
+			return 3;
+		case 'contributor':
+			return 2;
+		case 'subscriber':
+			return 1;
+		default:
+			return 0;
+	}
+}
+
+/*
+ * The highest authority and the accompanying data
+ * 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 $xoopsModule;
-		$xoopsDB =& Database::getInstance();
-		$db_groups_users_link = $xoopsDB->prefix('groups_users_link');
-		$db_group_permission = $xoopsDB->prefix('group_permission');
-		$module_tbl = $xoopsDB->prefix('modules');
-		
-		$module_id = get_xpress_mid($mydirname);
-		$sql = "SELECT uid FROM $db_groups_users_link WHERE groupid = 1 AND uid = $uid";
-	
-		$result =  $xoopsDB->query($sql, 0, 0);
-		if ($xoopsDB->getRowsNum($result)  > 0){
-				return 'administrator';
-		}
-		
-		$sql  = "SELECT Max($db_group_permission.gperm_itemid) AS user_level ,$db_groups_users_link.uid ";
-		$sql .= "FROM $db_groups_users_link LEFT JOIN $db_group_permission ON $db_groups_users_link.groupid = $db_group_permission.gperm_groupid ";
-		$sql .= "WHERE $db_group_permission.gperm_modid = $module_id ";
-		$sql .= "GROUP BY $db_groups_users_link.uid ";
-		$sql .= "HAVING $db_groups_users_link.uid = $uid";
-		$result =  $xoopsDB->query($sql, 0, 0);
-		if ($result === false){
-			return '';
-		}else{
-			if ($xoopsDB->getRowsNum($result)  > 0){
-				$row = $xoopsDB->fetchArray($result);	
-				$level =  $row['user_level'];
-				switch($level) {
-					case 5:
-						return 'administrator';
-					case 4:
-						return 'editor';
-					case 3:
-						return 'author';
-					case 2:
-						return 'contributor';
-					case 1:
-						return 'subscriber';
-					default:
-						return 'subscriber';
-				}
-			} else { 
-				return '';
-			}
-		}
-	}
-	
-//endif;
-
-//if( ! function_exists( 'get_xpress_prefix' ) ) :
-	function get_xpress_prefix($mydirname){
-		global $xoopsModule;
-		if ($mydirname == 'wordpress'){
-			return 'wp_';
-		} else {
-			return $mydirname . '_';
-		}
-	}
-//endif;
-
-//if( ! function_exists( 'xpress_user_found' ) ) :
-	function xpress_user_found($mydirname, $login_name){
-		$wp_prefix = get_xpress_prefix($mydirname);
-		$xoopsDB =& Database::getInstance();
-		$db_xpress_users = $xoopsDB->prefix($wp_prefix . 'users');
-
-		$sql = "SELECT ID FROM $db_xpress_users WHERE user_login = '$login_name'";
-		$res = $xoopsDB->query($sql, 0, 0);
-		if ($res === false){
-			return 0;
-		} else {
-			if ($xoopsDB->getRowsNum($res)  > 0){
-				$row = $xoopsDB->fetchArray($res);
-				return $row['ID'];
-			} else {
-				return 0;
-			}
-		}
-	}
-//endif;
+		
+		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;
+	}
+	
+/*
+ * Get User ID of WordPress from the login name. 
+*/
+function get_wp_user_id($login_name){
+	global $xoops_db;
+	$sql = "SELECT ID FROM " . get_wp_prefix() . "users WHERE user_login = '$login_name'";
+	$uid = $xoops_db->get_var($sql);
+	return $uid;
+}
 
 
 //if( ! function_exists( 'xpress_usermeta_sync' ) ) :
 	function xpress_usermeta_sync($mydirname , $uid,$meta_key,$meta_value, &$message){
-		$wp_prefix = get_xpress_prefix($mydirname);
+		$wp_prefix = get_wp_prefix();
 		$meta_key = "'" . $meta_key . "'";
 		$meta_value = "'" . $meta_value . "'";
@@ -133,137 +106,118 @@
 //endif;
 
-	function sql_str($text = ''){
-		return "'" . $text . "'";
-	}
-	
-//if( ! function_exists( 'xpress_user_sync' ) ) :
-	function xpress_user_sync($sync_uid = 0, &$message){
-		$mydirname = basename( dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) ) );
-
-		if( is_object( @$GLOBALS["xoopsModule"] ) ) {
-			$wp_prefix = get_xpress_prefix($mydirname);
-			$xoopsDB =& Database::getInstance();
-			$db_xoops_users = $xoopsDB->prefix('users');
-			$db_xpress_users = $xoopsDB->prefix($wp_prefix . 'users');
-
-			$message = 'Do Sync';
-		
-			if ($sync_uid == 0) {
-				$xu_sql  = "SELECT uid ,name ,uname ,pass ,email, url, user_regdate, user_aim, user_yim FROM $db_xoops_users";
+/*
+ * 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)
+	global $xoops_db;
+	global $xoopsModule;
+
+	$mydirname = basename( dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) ) );
+
+	if( is_object($xoopsModule)) {
+		$db_xoops_users = get_xoops_prefix() . 'users';
+		$db_xpress_users = get_wp_prefix() . 'users';
+
+		$message = 'Do Sync';
+	
+		if ($sync_uid == 0) {
+			$xu_sql  = "SELECT uid ,name ,uname ,pass ,email, url, user_regdate, user_aim, user_yim FROM $db_xoops_users";
+		} else {
+			$xu_sql  = "SELECT uid ,name ,uname ,pass ,email, url, user_regdate, user_aim, user_yim FROM $db_xoops_users WHERE uid = $sync_uid";
+		}
+
+		$xoops_users = $xoops_db->get_results($xu_sql);
+		if (empty($xoops_users)){
+			$message .= '...ERR ('. $xu_sql . ')';
+			return false;
+		}
+
+		foreach($xoops_users as $xoops_user){
+			$role = get_xoops_group_role($mydirname , $xoops_user->uid);
+			$capabillities_name = $role['capabillities'];
+			$allway_update = $role['allway_update'];
+			if (empty($capabillities_name)){
+					$message .= "...PASS '" . $xoops_user->uname ."'[uid=".$xoops_user->uid ."](not xpress user)";
+				if ($sync_uid != 0){
+					$message .= '...NOT XPRESS USER ' . $xoops_user->uname;
+					return false;
+				}
 			} else {
-				$xu_sql  = "SELECT uid ,name ,uname ,pass ,email, url, user_regdate, user_aim, user_yim FROM $db_xoops_users WHERE uid = $sync_uid";
-			}	
-
-			$xu_res =  $xoopsDB->query($xu_sql, 0, 0);
-			if ($xu_res === false){
-				$message .= '...ERR ('. $xu_sql . ')';
-				return false;
-			}else {
-				while($xu_row = 	$xoopsDB->fetchArray($xu_res)){
-					$capabillities_name = get_xoops_group_capabillities_name($mydirname , $xu_row['uid']);
-					if (empty($capabillities_name)){
-						$message .= "...PASS '" . $xu_row['uname'] ."'[uid=".$xu_row['uid'] ."](not xpress user)";
-						if ($sync_uid != 0){
-							$message .= '...NOT XPRESS USER ' . $xu_row['uname'];
-							return false;
-						}
+				$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) ;
+				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_registered = '$user_regist_time', ";
+					$wu_sql .=		"user_status = 0 ";
+					$wu_sql .=	"WHERE (user_login = '$user_login_name' )";
+
+					$xoops_db->query($wu_sql);
+					if ($allway_update){
+						$sycc_user = new WP_User($wp_user_id);
+						$sycc_user->set_role($capabillities_name);
+						$message .= '...UPDATE ' . $user_login_name . '(' . $capabillities_name . ')';
+
 					} else {
-						$user_ID = 			$xu_row['uid'];
-						if ($capabillities_name != 'administrator' ){
-							if (!empty($GLOBALS["xoopsModuleConfig"]["use_wp_userlevel"])){
-								$capabillities_name = get_option('default_role');
-							}
-						}
-						$user_login_name = 	$xu_row['uname'];
-						$user_pass_md5 = 	$xu_row['pass'];
-						$user_email = 		$xu_row['email'];
-						$user_url = 		$xu_row['url'];
-						$user_regist_time = date('Y-m-d H:i:s' , $xu_row['user_regdate']);
-						$user_nicename =	$xu_row['uname'];
-						$user_status = 0;
-						$user_display_name =empty($xu_row['name']) ? $xu_row['uname'] :$xu_row['name'] ;
-						$user_nickname =	empty($xu_row['name']) ? $xu_row['uname'] :$xu_row['name'] ;
-						$user_aim =			$xu_row['user_aim'];
-						$user_yim =			$xu_row['user_yim'];
-						$user_first_name = 	$xu_row['uname'];
-						$user_last_name = 	'';
-						$user_description = '';
-						$user_jabber = 		'';
-						$user_rich_editing = 'true';
-
-						$is_update = false;
-						if (xpress_user_found($mydirname, $user_login_name)){
-							$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_registered = '$user_regist_time', ";
-							$wu_sql .=		"user_status = 0 ";
-							$wu_sql .=	"WHERE (user_login = '$user_login_name' )";
-							if (!empty($GLOBALS["xoopsModuleConfig"]["use_wp_userlevel"])){
-								$message .= '...UPDATE ' . $user_login_name . '(Level Not Change)';
-							} else {
-								$message .= '...UPDATE ' . $user_login_name . '(' . $capabillities_name . ')';
-							}
-							$is_update = true;
-						}else{
-							$wu_sql  =	"INSERT INTO $db_xpress_users ";
-							$wu_sql .=  	"(user_login , user_pass ,user_email , user_url , user_nicename " ;
-							$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 .=		" , '$user_regist_time', $user_status, '$user_display_name')";
-							if (!empty($GLOBALS["xoopsModuleConfig"]["use_wp_userlevel"])){
-								$message .= '...INSERT ' . $user_login_name . '(' . $capabillities_name . '-WP Default)';
-							} else {
-								$message .= '...INSERT ' . $user_login_name . '(' . $capabillities_name . ')';
-							}
-						}
-		
-						$wu_res = $xoopsDB->queryF($wu_sql, 0, 0);
-		
-						if ($wu_res === false){
-							$message .= '...ERR(' . $wu_sql . ')';
-							return false;
-						}
-								
-						if (!empty($GLOBALS["xoopsModuleConfig"]["use_wp_userlevel"])){
-							if (!$is_update){
-								$sycc_user = new WP_User($user_ID);
-								$sycc_user->set_role($capabillities_name);
-							}
-						} else {
-							$sycc_user = new WP_User($user_ID);
-							$sycc_user->set_role($capabillities_name);						
-						}
-
-	//					if( $is_update === false){
-							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;
-						if ($is_update === false ) {
-							if(!xpress_usermeta_sync($mydirname, $user_ID,'rich_editing',$user_rich_editing,$message) )return false;
-						}					
-					}
+						$message .= '...UPDATE ' . $user_login_name . '(Level Not Change)';
+					}
+				}else{
+					$wu_sql  =	"INSERT INTO $db_xpress_users ";
+					$wu_sql .=  	"(user_login , user_pass ,user_email , user_url , user_nicename " ;
+					$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 .=		" , '$user_regist_time', $user_status, '$user_display_name')";
+					$xoops_db->query($wu_sql);
+					$wp_user_id = get_wp_user_id($user_login_name) ;					
+					$sycc_user = new WP_User($wp_user_id);
+					$sycc_user->set_role($capabillities_name);						
+					$message .= '...INSERT ' . $user_login_name . '(' . $capabillities_name . ')';
 				}
-			}
-			$message .= "...END";
-			return true;
-		}
-	}
-//endif;
-
-
+
+				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;
+				if ($is_update === false ) {
+					if(!xpress_usermeta_sync($mydirname, $user_ID,'rich_editing',$user_rich_editing,$message) )return false;
+				}			
+			}
+		}
+		$message .= "...END";
+		return true;
+	}
+}
 
 //if( ! function_exists( 'check_user_role_prefix' ) ) :
 	function check_user_role_prefix($mydirname){
-		$wp_prefix = get_xpress_prefix($mydirname);
+		$wp_prefix = get_wp_prefix();
 		$xoopsDB =& Database::getInstance();		
 		$db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta');
@@ -289,5 +243,5 @@
 //if( ! function_exists( 'same_metakey_delete' ) ) :
 	function same_metakey_delete($mydirname,$metakey){
-		$wp_prefix = get_xpress_prefix($mydirname);
+		$wp_prefix = get_wp_prefix();
 		$xoopsDB =& Database::getInstance();		
 		$db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta');
@@ -306,5 +260,5 @@
 //if( ! function_exists( 'repair_user_role_prefix' ) ) :
 	function repair_user_role_prefix($mydirname){
-		$wp_prefix = get_xpress_prefix($mydirname);
+		$wp_prefix = get_wp_prefix();
 		$xoopsDB =& Database::getInstance();		
 		$db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta');
