- Timestamp:
- Feb 2, 2009, 10:20:09 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-content/plugins/xpressme/include/user_sync_xoops.php
r82 r85 1 1 <?php 2 function get_xpress_mid($mydirname){ 3 $xoopsDB =& Database::getInstance(); 4 $module_tbl = $xoopsDB->prefix('modules'); 5 $module_dir = XOOPS_ROOT_PATH . '/modules/' . $mydirname; 6 $module_id = 0; 7 8 $sql = "SELECT mid FROM $module_tbl WHERE dirname LIKE '$mydirname'"; 9 $result = $xoopsDB->query($sql, 0, 0); 10 if ($xoopsDB->getRowsNum($result) > 0){ 11 $row = $xoopsDB->fetchArray($result); 12 $module_id = $row['mid']; 13 } 14 return $module_id; 15 } 16 17 18 function get_xoops_group_capabillities_name($mydirname , $uid=0){ 2 /* 3 * The default authority setting value given by a new user of WordPress 4 */ 5 function get_default_capabillities(){ 6 global $xoops_db; 7 $db_option = get_wp_prefix() . 'options'; 8 return $xoops_db->get_var("SELECT option_value FROM $db_option WHERE option_name = 'default_role'"); 9 } 10 11 /* 12 * Get The level from the capabillities name. 13 */ 14 function get_role_level($capabillities){ 15 if ($capabillities == 'default') $capabillities = get_default_capabillities(); 16 switch($capabillities) { 17 case 'administrator': 18 return 5; 19 case 'editor': 20 return 4; 21 case 'author': 22 return 3; 23 case 'contributor': 24 return 2; 25 case 'subscriber': 26 return 1; 27 default: 28 return 0; 29 } 30 } 31 32 /* 33 * The highest authority and the accompanying data 34 * in the WordPress authority given to the group to which the user belongs are obtained. 35 */ 36 function get_xoops_group_role($mydirname , $uid=0){ 37 $ans = array(); 19 38 if ($uid == 0){ 20 39 return ''; 21 40 } 22 23 global $xoopsModule; 24 $xoopsDB =& Database::getInstance(); 25 $db_groups_users_link = $xoopsDB->prefix('groups_users_link'); 26 $db_group_permission = $xoopsDB->prefix('group_permission'); 27 $module_tbl = $xoopsDB->prefix('modules'); 28 29 $module_id = get_xpress_mid($mydirname); 30 $sql = "SELECT uid FROM $db_groups_users_link WHERE groupid = 1 AND uid = $uid"; 31 32 $result = $xoopsDB->query($sql, 0, 0); 33 if ($xoopsDB->getRowsNum($result) > 0){ 34 return 'administrator'; 35 } 36 37 $sql = "SELECT Max($db_group_permission.gperm_itemid) AS user_level ,$db_groups_users_link.uid "; 38 $sql .= "FROM $db_groups_users_link LEFT JOIN $db_group_permission ON $db_groups_users_link.groupid = $db_group_permission.gperm_groupid "; 39 $sql .= "WHERE $db_group_permission.gperm_modid = $module_id "; 40 $sql .= "GROUP BY $db_groups_users_link.uid "; 41 $sql .= "HAVING $db_groups_users_link.uid = $uid"; 42 $result = $xoopsDB->query($sql, 0, 0); 43 if ($result === false){ 44 return ''; 45 }else{ 46 if ($xoopsDB->getRowsNum($result) > 0){ 47 $row = $xoopsDB->fetchArray($result); 48 $level = $row['user_level']; 49 switch($level) { 50 case 5: 51 return 'administrator'; 52 case 4: 53 return 'editor'; 54 case 3: 55 return 'author'; 56 case 2: 57 return 'contributor'; 58 case 1: 59 return 'subscriber'; 60 default: 61 return 'subscriber'; 62 } 63 } else { 64 return ''; 65 } 66 } 67 } 68 69 //endif; 70 71 //if( ! function_exists( 'get_xpress_prefix' ) ) : 72 function get_xpress_prefix($mydirname){ 73 global $xoopsModule; 74 if ($mydirname == 'wordpress'){ 75 return 'wp_'; 76 } else { 77 return $mydirname . '_'; 78 } 79 } 80 //endif; 81 82 //if( ! function_exists( 'xpress_user_found' ) ) : 83 function xpress_user_found($mydirname, $login_name){ 84 $wp_prefix = get_xpress_prefix($mydirname); 85 $xoopsDB =& Database::getInstance(); 86 $db_xpress_users = $xoopsDB->prefix($wp_prefix . 'users'); 87 88 $sql = "SELECT ID FROM $db_xpress_users WHERE user_login = '$login_name'"; 89 $res = $xoopsDB->query($sql, 0, 0); 90 if ($res === false){ 91 return 0; 92 } else { 93 if ($xoopsDB->getRowsNum($res) > 0){ 94 $row = $xoopsDB->fetchArray($res); 95 return $row['ID']; 96 } else { 97 return 0; 98 } 99 } 100 } 101 //endif; 41 42 global $xoops_db; 43 $db_groups_users_link = get_xoops_prefix() . 'groups_users_link'; 44 $db_group_role = get_wp_prefix() . 'group_role'; 45 46 $sql = "SELECT * "; 47 $sql .= "FROM $db_groups_users_link "; 48 $sql .= "LEFT JOIN $db_group_role ON $db_groups_users_link.groupid = $db_group_role.groupid "; 49 $sql .= "WHERE uid = $uid"; 50 51 $user_groups = $xoops_db->get_results($sql); 52 $ans['capabillities'] = ''; 53 $ans['allway_update'] = 0; 54 55 foreach($user_groups as $user_group){ 56 $before_level = get_role_level($ans['capabillities']); 57 $now_level = get_role_level($user_group->role); 58 if ($now_level > $before_level){ 59 $ans['capabillities'] = $user_group->role; 60 $ans['allway_update'] = $user_group->login_all; 61 } 62 } 63 return $ans; 64 } 65 66 /* 67 * Get User ID of WordPress from the login name. 68 */ 69 function get_wp_user_id($login_name){ 70 global $xoops_db; 71 $sql = "SELECT ID FROM " . get_wp_prefix() . "users WHERE user_login = '$login_name'"; 72 $uid = $xoops_db->get_var($sql); 73 return $uid; 74 } 102 75 103 76 104 77 //if( ! function_exists( 'xpress_usermeta_sync' ) ) : 105 78 function xpress_usermeta_sync($mydirname , $uid,$meta_key,$meta_value, &$message){ 106 $wp_prefix = get_ xpress_prefix($mydirname);79 $wp_prefix = get_wp_prefix(); 107 80 $meta_key = "'" . $meta_key . "'"; 108 81 $meta_value = "'" . $meta_value . "'"; … … 133 106 //endif; 134 107 135 function sql_str($text = ''){ 136 return "'" . $text . "'"; 137 } 138 139 //if( ! function_exists( 'xpress_user_sync' ) ) : 140 function xpress_user_sync($sync_uid = 0, &$message){ 141 $mydirname = basename( dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) ) ); 142 143 if( is_object( @$GLOBALS["xoopsModule"] ) ) { 144 $wp_prefix = get_xpress_prefix($mydirname); 145 $xoopsDB =& Database::getInstance(); 146 $db_xoops_users = $xoopsDB->prefix('users'); 147 $db_xpress_users = $xoopsDB->prefix($wp_prefix . 'users'); 148 149 $message = 'Do Sync'; 150 151 if ($sync_uid == 0) { 152 $xu_sql = "SELECT uid ,name ,uname ,pass ,email, url, user_regdate, user_aim, user_yim FROM $db_xoops_users"; 108 /* 109 * The user data of XOOPS is written in the WordPress user data. 110 * If $sync_uid is 0, all users are written. 111 */ 112 function user_sync_to_wordpress($sync_uid = 0, &$message) 113 global $xoops_db; 114 global $xoopsModule; 115 116 $mydirname = basename( dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) ) ); 117 118 if( is_object($xoopsModule)) { 119 $db_xoops_users = get_xoops_prefix() . 'users'; 120 $db_xpress_users = get_wp_prefix() . 'users'; 121 122 $message = 'Do Sync'; 123 124 if ($sync_uid == 0) { 125 $xu_sql = "SELECT uid ,name ,uname ,pass ,email, url, user_regdate, user_aim, user_yim FROM $db_xoops_users"; 126 } else { 127 $xu_sql = "SELECT uid ,name ,uname ,pass ,email, url, user_regdate, user_aim, user_yim FROM $db_xoops_users WHERE uid = $sync_uid"; 128 } 129 130 $xoops_users = $xoops_db->get_results($xu_sql); 131 if (empty($xoops_users)){ 132 $message .= '...ERR ('. $xu_sql . ')'; 133 return false; 134 } 135 136 foreach($xoops_users as $xoops_user){ 137 $role = get_xoops_group_role($mydirname , $xoops_user->uid); 138 $capabillities_name = $role['capabillities']; 139 $allway_update = $role['allway_update']; 140 if (empty($capabillities_name)){ 141 $message .= "...PASS '" . $xoops_user->uname ."'[uid=".$xoops_user->uid ."](not xpress user)"; 142 if ($sync_uid != 0){ 143 $message .= '...NOT XPRESS USER ' . $xoops_user->uname; 144 return false; 145 } 153 146 } else { 154 $xu_sql = "SELECT uid ,name ,uname ,pass ,email, url, user_regdate, user_aim, user_yim FROM $db_xoops_users WHERE uid = $sync_uid"; 155 } 156 157 $xu_res = $xoopsDB->query($xu_sql, 0, 0); 158 if ($xu_res === false){ 159 $message .= '...ERR ('. $xu_sql . ')'; 160 return false; 161 }else { 162 while($xu_row = $xoopsDB->fetchArray($xu_res)){ 163 $capabillities_name = get_xoops_group_capabillities_name($mydirname , $xu_row['uid']); 164 if (empty($capabillities_name)){ 165 $message .= "...PASS '" . $xu_row['uname'] ."'[uid=".$xu_row['uid'] ."](not xpress user)"; 166 if ($sync_uid != 0){ 167 $message .= '...NOT XPRESS USER ' . $xu_row['uname']; 168 return false; 169 } 147 $user_ID = $xoops_user->uid; 148 $user_login_name = $xoops_user->uname; 149 $user_pass_md5 = $xoops_user->pass; 150 $user_email = $xoops_user->email; 151 $user_url = $xoops_user->url; 152 $user_regist_time = date('Y-m-d H:i:s' , $xoops_user->user_regdate); 153 $user_nicename = $xoops_user->uname; 154 $user_status = 0; 155 $user_display_name =empty($xoops_user->name) ? $xoops_user->uname :$xoops_user->name ; 156 $user_nickname = empty($xoops_user->name) ? $xoops_user->uname :$xoops_user->name ; 157 $user_aim = $xoops_user->user_aim; 158 $user_yim = $xoops_user->user_yim; 159 $user_first_name = $xoops_user->uname; 160 $user_last_name = ''; 161 $user_description = ''; 162 $user_jabber = ''; 163 $user_rich_editing = 'true'; 164 165 $is_update = false; 166 167 $wp_user_id = get_wp_user_id($user_login_name) ; 168 if ($wp_user_id){ 169 $wu_sql = "UPDATE $db_xpress_users "; 170 $wu_sql .= 'SET '; 171 $wu_sql .= "user_pass = '$user_pass_md5', "; 172 $wu_sql .= "user_email = '$user_email', "; 173 $wu_sql .= "user_url = '$user_url', "; 174 $wu_sql .= "user_nicename = '$user_nicename', "; 175 $wu_sql .= "user_registered = '$user_regist_time', "; 176 $wu_sql .= "user_status = 0 "; 177 $wu_sql .= "WHERE (user_login = '$user_login_name' )"; 178 179 $xoops_db->query($wu_sql); 180 if ($allway_update){ 181 $sycc_user = new WP_User($wp_user_id); 182 $sycc_user->set_role($capabillities_name); 183 $message .= '...UPDATE ' . $user_login_name . '(' . $capabillities_name . ')'; 184 170 185 } else { 171 $user_ID = $xu_row['uid']; 172 if ($capabillities_name != 'administrator' ){ 173 if (!empty($GLOBALS["xoopsModuleConfig"]["use_wp_userlevel"])){ 174 $capabillities_name = get_option('default_role'); 175 } 176 } 177 $user_login_name = $xu_row['uname']; 178 $user_pass_md5 = $xu_row['pass']; 179 $user_email = $xu_row['email']; 180 $user_url = $xu_row['url']; 181 $user_regist_time = date('Y-m-d H:i:s' , $xu_row['user_regdate']); 182 $user_nicename = $xu_row['uname']; 183 $user_status = 0; 184 $user_display_name =empty($xu_row['name']) ? $xu_row['uname'] :$xu_row['name'] ; 185 $user_nickname = empty($xu_row['name']) ? $xu_row['uname'] :$xu_row['name'] ; 186 $user_aim = $xu_row['user_aim']; 187 $user_yim = $xu_row['user_yim']; 188 $user_first_name = $xu_row['uname']; 189 $user_last_name = ''; 190 $user_description = ''; 191 $user_jabber = ''; 192 $user_rich_editing = 'true'; 193 194 $is_update = false; 195 if (xpress_user_found($mydirname, $user_login_name)){ 196 $wu_sql = "UPDATE $db_xpress_users "; 197 $wu_sql .= 'SET '; 198 $wu_sql .= "user_pass = '$user_pass_md5', "; 199 $wu_sql .= "user_email = '$user_email', "; 200 $wu_sql .= "user_url = '$user_url', "; 201 $wu_sql .= "user_nicename = '$user_nicename', "; 202 $wu_sql .= "user_registered = '$user_regist_time', "; 203 $wu_sql .= "user_status = 0 "; 204 $wu_sql .= "WHERE (user_login = '$user_login_name' )"; 205 if (!empty($GLOBALS["xoopsModuleConfig"]["use_wp_userlevel"])){ 206 $message .= '...UPDATE ' . $user_login_name . '(Level Not Change)'; 207 } else { 208 $message .= '...UPDATE ' . $user_login_name . '(' . $capabillities_name . ')'; 209 } 210 $is_update = true; 211 }else{ 212 $wu_sql = "INSERT INTO $db_xpress_users "; 213 $wu_sql .= "(user_login , user_pass ,user_email , user_url , user_nicename " ; 214 $wu_sql .= " , user_registered , user_status , display_name) "; 215 $wu_sql .= "VALUES "; 216 $wu_sql .= "('$user_login_name', '$user_pass_md5', '$user_email', '$user_url', '$user_nicename' "; 217 $wu_sql .= " , '$user_regist_time', $user_status, '$user_display_name')"; 218 if (!empty($GLOBALS["xoopsModuleConfig"]["use_wp_userlevel"])){ 219 $message .= '...INSERT ' . $user_login_name . '(' . $capabillities_name . '-WP Default)'; 220 } else { 221 $message .= '...INSERT ' . $user_login_name . '(' . $capabillities_name . ')'; 222 } 223 } 224 225 $wu_res = $xoopsDB->queryF($wu_sql, 0, 0); 226 227 if ($wu_res === false){ 228 $message .= '...ERR(' . $wu_sql . ')'; 229 return false; 230 } 231 232 if (!empty($GLOBALS["xoopsModuleConfig"]["use_wp_userlevel"])){ 233 if (!$is_update){ 234 $sycc_user = new WP_User($user_ID); 235 $sycc_user->set_role($capabillities_name); 236 } 237 } else { 238 $sycc_user = new WP_User($user_ID); 239 $sycc_user->set_role($capabillities_name); 240 } 241 242 // if( $is_update === false){ 243 if(!xpress_usermeta_sync($mydirname, $user_ID,'nickname',$user_nickname,$message) )return false; 244 if(!xpress_usermeta_sync($mydirname, $user_ID,'first_name',$user_first_name,$message) )return false; 245 if(!xpress_usermeta_sync($mydirname, $user_ID,'last_name',$user_last_name,$message) )return false; 246 if(!xpress_usermeta_sync($mydirname, $user_ID,'description',$user_description,$message) )return false; 247 if(!xpress_usermeta_sync($mydirname, $user_ID,'jabber',$user_jabber,$message) )return false; 248 // } 249 if(!xpress_usermeta_sync($mydirname, $user_ID,'aim',$user_aim,$message) )return false; 250 if(!xpress_usermeta_sync($mydirname, $user_ID,'yim',$user_yim,$message) )return false; 251 if ($is_update === false ) { 252 if(!xpress_usermeta_sync($mydirname, $user_ID,'rich_editing',$user_rich_editing,$message) )return false; 253 } 254 } 186 $message .= '...UPDATE ' . $user_login_name . '(Level Not Change)'; 187 } 188 }else{ 189 $wu_sql = "INSERT INTO $db_xpress_users "; 190 $wu_sql .= "(user_login , user_pass ,user_email , user_url , user_nicename " ; 191 $wu_sql .= " , user_registered , user_status , display_name) "; 192 $wu_sql .= "VALUES "; 193 $wu_sql .= "('$user_login_name', '$user_pass_md5', '$user_email', '$user_url', '$user_nicename' "; 194 $wu_sql .= " , '$user_regist_time', $user_status, '$user_display_name')"; 195 $xoops_db->query($wu_sql); 196 $wp_user_id = get_wp_user_id($user_login_name) ; 197 $sycc_user = new WP_User($wp_user_id); 198 $sycc_user->set_role($capabillities_name); 199 $message .= '...INSERT ' . $user_login_name . '(' . $capabillities_name . ')'; 255 200 } 256 } 257 $message .= "...END"; 258 return true; 259 } 260 } 261 //endif; 262 263 201 202 if(!xpress_usermeta_sync($mydirname, $user_ID,'nickname',$user_nickname,$message) )return false; 203 if(!xpress_usermeta_sync($mydirname, $user_ID,'first_name',$user_first_name,$message) )return false; 204 if(!xpress_usermeta_sync($mydirname, $user_ID,'last_name',$user_last_name,$message) )return false; 205 if(!xpress_usermeta_sync($mydirname, $user_ID,'description',$user_description,$message) )return false; 206 if(!xpress_usermeta_sync($mydirname, $user_ID,'jabber',$user_jabber,$message) )return false; 207 if(!xpress_usermeta_sync($mydirname, $user_ID,'aim',$user_aim,$message) )return false; 208 if(!xpress_usermeta_sync($mydirname, $user_ID,'yim',$user_yim,$message) )return false; 209 if ($is_update === false ) { 210 if(!xpress_usermeta_sync($mydirname, $user_ID,'rich_editing',$user_rich_editing,$message) )return false; 211 } 212 } 213 } 214 $message .= "...END"; 215 return true; 216 } 217 } 264 218 265 219 //if( ! function_exists( 'check_user_role_prefix' ) ) : 266 220 function check_user_role_prefix($mydirname){ 267 $wp_prefix = get_ xpress_prefix($mydirname);221 $wp_prefix = get_wp_prefix(); 268 222 $xoopsDB =& Database::getInstance(); 269 223 $db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta'); … … 289 243 //if( ! function_exists( 'same_metakey_delete' ) ) : 290 244 function same_metakey_delete($mydirname,$metakey){ 291 $wp_prefix = get_ xpress_prefix($mydirname);245 $wp_prefix = get_wp_prefix(); 292 246 $xoopsDB =& Database::getInstance(); 293 247 $db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta'); … … 306 260 //if( ! function_exists( 'repair_user_role_prefix' ) ) : 307 261 function repair_user_role_prefix($mydirname){ 308 $wp_prefix = get_ xpress_prefix($mydirname);262 $wp_prefix = get_wp_prefix(); 309 263 $xoopsDB =& Database::getInstance(); 310 264 $db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta');
Note: See TracChangeset
for help on using the changeset viewer.