[72] | 1 | <?php
|
---|
[85] | 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 | }
|
---|
[72] | 10 |
|
---|
[85] | 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;
|
---|
[72] | 29 | }
|
---|
[85] | 30 | }
|
---|
[72] | 31 |
|
---|
[85] | 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 | */
|
---|
[87] | 36 | function get_xoops_group_role($uid=0){
|
---|
| 37 | $ans = array();
|
---|
| 38 | if ($uid == 0){
|
---|
| 39 | return '';
|
---|
| 40 | }
|
---|
[72] | 41 |
|
---|
[87] | 42 | global $xoops_db;
|
---|
| 43 | $db_groups_users_link = get_xoops_prefix() . 'groups_users_link';
|
---|
| 44 | $db_group_role = get_wp_prefix() . 'group_role';
|
---|
[95] | 45 | $db_groups = get_xoops_prefix() . 'groups';
|
---|
[87] | 46 |
|
---|
| 47 | $sql = "SELECT * ";
|
---|
| 48 | $sql .= "FROM $db_groups_users_link ";
|
---|
| 49 | $sql .= "LEFT JOIN $db_group_role ON $db_groups_users_link.groupid = $db_group_role.groupid ";
|
---|
[95] | 50 | $sql .= "LEFT JOIN $db_groups ON $db_groups_users_link.groupid = $db_groups.groupid ";
|
---|
[87] | 51 | $sql .= "WHERE uid = $uid";
|
---|
| 52 | $user_groups = $xoops_db->get_results($sql);
|
---|
| 53 | $ans['capabillities'] = '';
|
---|
| 54 | $ans['allway_update'] = 0;
|
---|
| 55 |
|
---|
| 56 | foreach($user_groups as $user_group){
|
---|
[95] | 57 | if ($user_group->group_type == 'Admin'){
|
---|
| 58 | $ans['capabillities'] = 'administrator';
|
---|
| 59 | $ans['allway_update'] = 0;
|
---|
| 60 | break;
|
---|
| 61 | }
|
---|
[87] | 62 | $before_level = get_role_level($ans['capabillities']);
|
---|
[95] | 63 |
|
---|
[87] | 64 | $now_level = get_role_level($user_group->role);
|
---|
| 65 | if ($now_level > $before_level){
|
---|
| 66 | $ans['capabillities'] = $user_group->role;
|
---|
| 67 | $ans['allway_update'] = $user_group->login_all;
|
---|
[72] | 68 | }
|
---|
| 69 | }
|
---|
[87] | 70 | return $ans;
|
---|
| 71 | }
|
---|
[72] | 72 |
|
---|
[85] | 73 | /*
|
---|
| 74 | * Get User ID of WordPress from the login name.
|
---|
| 75 | */
|
---|
| 76 | function get_wp_user_id($login_name){
|
---|
| 77 | global $xoops_db;
|
---|
| 78 | $sql = "SELECT ID FROM " . get_wp_prefix() . "users WHERE user_login = '$login_name'";
|
---|
| 79 | $uid = $xoops_db->get_var($sql);
|
---|
| 80 | return $uid;
|
---|
| 81 | }
|
---|
[72] | 82 |
|
---|
[85] | 83 | /*
|
---|
| 84 | * The user data of XOOPS is written in the WordPress user data.
|
---|
| 85 | * If $sync_uid is 0, all users are written.
|
---|
| 86 | */
|
---|
[87] | 87 | function user_sync_to_wordpress($sync_uid = 0, &$message){
|
---|
[85] | 88 | global $xoops_db;
|
---|
| 89 |
|
---|
[95] | 90 | $db_xoops_users = get_xoops_prefix() . 'users';
|
---|
| 91 | $db_xpress_users = get_wp_prefix() . 'users';
|
---|
[85] | 92 |
|
---|
[95] | 93 | $message = 'Do Sync';
|
---|
[72] | 94 |
|
---|
[95] | 95 | if ($sync_uid == 0) {
|
---|
| 96 | $xu_sql = "SELECT uid ,name ,uname ,pass ,email, url, user_regdate, user_aim, user_yim FROM $db_xoops_users";
|
---|
| 97 | } else {
|
---|
| 98 | $xu_sql = "SELECT uid ,name ,uname ,pass ,email, url, user_regdate, user_aim, user_yim FROM $db_xoops_users WHERE uid = $sync_uid";
|
---|
| 99 | }
|
---|
[72] | 100 |
|
---|
[95] | 101 | $xoops_users = $xoops_db->get_results($xu_sql);
|
---|
[230] | 102 |
|
---|
[95] | 103 | if (empty($xoops_users)){
|
---|
| 104 | $message .= '...ERR ('. $xu_sql . ')';
|
---|
| 105 | return false;
|
---|
| 106 | }
|
---|
[72] | 107 |
|
---|
[95] | 108 | foreach($xoops_users as $xoops_user){
|
---|
| 109 | $role = get_xoops_group_role($xoops_user->uid);
|
---|
| 110 | $capabillities_name = $role['capabillities'];
|
---|
| 111 | $allway_update = $role['allway_update'];
|
---|
[230] | 112 |
|
---|
| 113 | $wp_user_id = get_wp_user_id($xoops_user->uname) ;
|
---|
| 114 |
|
---|
[95] | 115 | if (empty($capabillities_name)){
|
---|
[230] | 116 | if ($sync_uid != 0){
|
---|
| 117 | if ($wp_user_id) {
|
---|
| 118 | if ($allway_update){
|
---|
| 119 | $message .= '...NOT XPRESS USER ' . $xoops_user->uname;
|
---|
| 120 | return false;
|
---|
| 121 | }
|
---|
| 122 | } else {
|
---|
| 123 | $message .= '...NOT XPRESS USER ' . $xoops_user->uname;
|
---|
| 124 | return false;
|
---|
| 125 | }
|
---|
| 126 | } else {
|
---|
[95] | 127 | $message .= "...PASS '" . $xoops_user->uname ."'[uid=".$xoops_user->uid ."](not xpress user)";
|
---|
[230] | 128 | continue;
|
---|
[95] | 129 | }
|
---|
[230] | 130 | }
|
---|
[72] | 131 |
|
---|
[230] | 132 | $user_regist_time = date('Y-m-d H:i:s' , $xoops_user->user_regdate);
|
---|
| 133 | $user_status = 0;
|
---|
| 134 | $user_display_name =empty($xoops_user->name) ? $xoops_user->uname :$xoops_user->name ;
|
---|
[72] | 135 |
|
---|
[230] | 136 | $is_update = false;
|
---|
| 137 |
|
---|
| 138 | if ($wp_user_id){
|
---|
| 139 | $wu_sql = "UPDATE $db_xpress_users ";
|
---|
| 140 | $wu_sql .= 'SET ';
|
---|
| 141 | $wu_sql .= "user_pass = '$xoops_user->pass', ";
|
---|
| 142 | $wu_sql .= "user_email = '$xoops_user->email', ";
|
---|
| 143 | $wu_sql .= "user_url = '$xoops_user->url', ";
|
---|
| 144 | $wu_sql .= "user_nicename = '$xoops_user->uname', ";
|
---|
| 145 | $wu_sql .= "user_registered = '$user_regist_time', ";
|
---|
| 146 | $wu_sql .= "user_status = 0 ";
|
---|
| 147 | $wu_sql .= "WHERE (user_login = '$xoops_user->uname' )";
|
---|
[95] | 148 |
|
---|
[230] | 149 | $xoops_db->query($wu_sql);
|
---|
| 150 | if ($allway_update){
|
---|
| 151 | $sycc_user = new WP_User($wp_user_id);
|
---|
| 152 | $sycc_user->set_role($capabillities_name);
|
---|
| 153 | $message .= '...UPDATE ' . $xoops_user->uname . '(' . $capabillities_name . ')';
|
---|
[175] | 154 |
|
---|
[230] | 155 | } else {
|
---|
| 156 | $message .= '...UPDATE ' . $xoops_user->uname . '(Level Not Change)';
|
---|
[95] | 157 | }
|
---|
[230] | 158 | if (!check_user_meta_prefix($wp_user_id)){
|
---|
| 159 | repair_user_meta_prefix();
|
---|
| 160 | }
|
---|
[85] | 161 |
|
---|
[230] | 162 | $is_update = true;
|
---|
| 163 | }else{
|
---|
| 164 | $wu_sql = "INSERT INTO $db_xpress_users ";
|
---|
| 165 | $wu_sql .= "(user_login , user_pass ,user_email , user_url , user_nicename " ;
|
---|
| 166 | $wu_sql .= " , user_registered , user_status , display_name) ";
|
---|
| 167 | $wu_sql .= "VALUES ";
|
---|
| 168 | $wu_sql .= "('$xoops_user->uname', '$xoops_user->pass', '$xoops_user->email', '$xoops_user->url', '$xoops_user->uname' ";
|
---|
| 169 | $wu_sql .= " , '$user_regist_time', $user_status, '$user_display_name')";
|
---|
| 170 | $xoops_db->query($wu_sql);
|
---|
| 171 | // $wp_user_id = get_wp_user_id($user_login_name) ;
|
---|
| 172 | $wp_user_id = mysql_insert_id();
|
---|
| 173 | $sycc_user = new WP_User($wp_user_id);
|
---|
| 174 | $sycc_user->set_role($capabillities_name);
|
---|
| 175 | $message .= '...INSERT ' . $user_login_name . '(' . $capabillities_name . ')';
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | $user_nickname = empty($xoops_user->name) ? $xoops_user->uname :$xoops_user->name ;
|
---|
| 179 | $user_rich_editing = 'true';
|
---|
| 180 | $user_first_name = $xoops_user->uname;
|
---|
| 181 | $user_last_name = '';
|
---|
| 182 | $user_description = '';
|
---|
| 183 | $user_jabber = '';
|
---|
| 184 |
|
---|
| 185 | update_usermeta( $wp_user_id,'nickname',$user_nickname);
|
---|
| 186 | update_usermeta( $wp_user_id,'first_name',$user_first_name);
|
---|
| 187 | update_usermeta( $wp_user_id,'last_name',$user_last_name);
|
---|
| 188 | update_usermeta( $wp_user_id,'description',$user_description);
|
---|
| 189 | update_usermeta( $wp_user_id,'jabber',$user_jabber);
|
---|
| 190 | update_usermeta( $wp_user_id,'aim',$xoops_user->user_aim);
|
---|
| 191 | update_usermeta( $wp_user_id,'yim',$xoops_user->user_yim);
|
---|
| 192 | if ($is_update === false ) {
|
---|
| 193 | update_usermeta( $wp_user_id,'rich_editing',$user_rich_editing);
|
---|
| 194 | }
|
---|
[175] | 195 |
|
---|
[72] | 196 | }
|
---|
[95] | 197 | $message .= "...END";
|
---|
| 198 | return true;
|
---|
[85] | 199 | }
|
---|
[72] | 200 |
|
---|
| 201 | /*
|
---|
| 202 | * Get User ID of XOOPS from the login name.
|
---|
| 203 | */
|
---|
| 204 | function get_xoops_user_id($login_name){
|
---|
| 205 | global $xoops_db;
|
---|
| 206 | $sql = "SELECT uid FROM " . get_xoops_prefix() . "users WHERE uname = '$login_name'";
|
---|
| 207 | $uid = $xoops_db->get_var($sql);
|
---|
| 208 | return $uid;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /*
|
---|
| 212 | *The user data of wordpress is written in the xoops user data.
|
---|
| 213 | */
|
---|
| 214 | function user_sync_to_xoops($user_ID){
|
---|
| 215 | global $xoops_db;
|
---|
| 216 |
|
---|
| 217 | $user_info = get_userdata($user_ID);
|
---|
| 218 | $xoops_uid = get_xoops_user_id($user_info->user_login);
|
---|
| 219 | $db_xoops_users = get_xoops_prefix() . "users";
|
---|
| 220 | $user_regdate = strtotime($user_info->user_registered);
|
---|
| 221 | $aim = get_usermeta($user_ID,'aim');
|
---|
| 222 | $yim = get_usermeta($user_ID,'yim');
|
---|
| 223 |
|
---|
| 224 | if ($xoops_uid){
|
---|
| 225 | $wu_sql = "UPDATE $db_xoops_users ";
|
---|
| 226 | $wu_sql .= 'SET ';
|
---|
| 227 | $wu_sql .= "uname = '$user_info->user_login', ";
|
---|
| 228 | $wu_sql .= "pass = '$user_info->user_pass', ";
|
---|
| 229 | $wu_sql .= "email = '$user_info->user_email', ";
|
---|
| 230 | $wu_sql .= "url = '$user_info->user_url', ";
|
---|
| 231 | $wu_sql .= "name = '$user_info->display_name', ";
|
---|
| 232 | $wu_sql .= "user_aim = '$aim', ";
|
---|
| 233 | $wu_sql .= "user_yim = '$yim', ";
|
---|
| 234 | $wu_sql .= "user_regdate = $user_regdate ";
|
---|
| 235 | $wu_sql .= "WHERE (uid = $xoops_uid )";
|
---|
[82] | 236 | $xoops_db->query($wu_sql);
|
---|
[72] | 237 | }else{
|
---|
| 238 | $wu_sql = "INSERT INTO $db_xoops_users ";
|
---|
| 239 | $wu_sql .= "(uname , pass ,email , url , name , user_aim , user_yim , user_regdate) " ;
|
---|
| 240 | $wu_sql .= "VALUES ";
|
---|
| 241 | $wu_sql .= "('$user_info->user_login', '$user_info->user_pass', '$user_info->user_email', '$user_info->user_url', '$user_info->display_name' ";
|
---|
| 242 | $wu_sql .= " , '$aim' , '$yim' , $user_regdate )";
|
---|
[82] | 243 | $xoops_db->query($wu_sql);
|
---|
| 244 | // get xoops users default groupe ID
|
---|
| 245 | $db_xoops_group = get_xoops_prefix() . 'groups';
|
---|
| 246 | $default_xoops_group_id = $xoops_db->get_var("SELECT groupid FROM $db_xoops_group WHERE group_type = 'User'");
|
---|
| 247 | // get insert users ID
|
---|
| 248 | $db_xoops_group = get_xoops_prefix() . 'users';
|
---|
| 249 | $user_id = $xoops_db->get_var("SELECT uid FROM $db_xoops_group WHERE uname = '$user_info->user_login'");
|
---|
| 250 | // insert groups_users_link
|
---|
| 251 | $db_xoops_group_users_link = get_xoops_prefix() . 'groups_users_link';
|
---|
| 252 | $default_xoops_group_id = $xoops_db->get_var("INSERT INTO $db_xoops_group_users_link (groupid , uid ) VALUES ($default_xoops_group_id , $user_id)");
|
---|
[72] | 253 | }
|
---|
| 254 | }
|
---|
[87] | 255 |
|
---|
| 256 |
|
---|
| 257 | /*
|
---|
| 258 | * When I changed the pre-fix of the database in XOOPS Protector Module, user authority data of WordPress read it and cannot do it.
|
---|
| 259 | * This is because a meta_key pre-fix of the usermeta table of WordPress is not changed.
|
---|
| 260 | * and user_roles option_name pre-fix of the option table of WordPress is not changed.
|
---|
| 261 | */
|
---|
[175] | 262 | function check_user_meta_prefix($uid){
|
---|
| 263 | global $xoops_db;
|
---|
| 264 | $db_xpress_usermeta = get_wp_prefix() . 'usermeta';
|
---|
| 265 | $user_meta_prefix = get_wp_prefix();
|
---|
| 266 |
|
---|
| 267 | $sql = "SELECT * FROM $db_xpress_usermeta WHERE user_id = $uid AND meta_key = '" . $user_meta_prefix ."user_level'" ;
|
---|
| 268 |
|
---|
| 269 | $user_level = $xoops_db->get_results($sql);
|
---|
| 270 | if (empty($user_level)) return false ;
|
---|
| 271 |
|
---|
| 272 | $sql = "SELECT * FROM $db_xpress_usermeta WHERE user_id = $uid AND meta_key = '" . $user_meta_prefix ."capabilities'" ;
|
---|
| 273 | $capabilities = $xoops_db->get_results($sql);
|
---|
| 274 | if (empty($capabilities)) return false ;
|
---|
| 275 |
|
---|
| 276 | return true;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[87] | 279 | function repair_user_meta_prefix(){
|
---|
| 280 | global $xoops_db;
|
---|
| 281 |
|
---|
| 282 | // repair usermeta db
|
---|
| 283 | $db_xpress_usermeta = get_wp_prefix() . 'usermeta';
|
---|
| 284 | $wp_prefix_only = get_wp_prefix_only();
|
---|
| 285 | $user_meta_prefix = get_wp_prefix();
|
---|
| 286 | $sql = "SELECT * FROM $db_xpress_usermeta WHERE meta_key LIKE '%_" . $wp_prefix_only . "%'" ;
|
---|
| 287 | $user_metas = $xoops_db->get_results($sql);
|
---|
| 288 | foreach($user_metas as $user_meta){
|
---|
| 289 | if (strpos($user_meta->meta_key,$user_meta_prefix) === false) {
|
---|
| 290 | $new_meta_key = '';
|
---|
| 291 | if (strpos($user_meta->meta_key,$wp_prefix_only.'user_level'))
|
---|
| 292 | $new_meta_key = $user_meta_prefix . 'user_level';
|
---|
| 293 | if (strpos($user_meta->meta_key,$wp_prefix_only.'capabilities'))
|
---|
| 294 | $new_meta_key = $user_meta_prefix . 'capabilities';
|
---|
| 295 | if (strpos($user_meta->meta_key,$wp_prefix_only.'autosave_draft_ids'))
|
---|
| 296 | $new_meta_key = $user_meta_prefix . 'autosave_draft_ids';
|
---|
| 297 | if (strpos($user_meta->meta_key,$wp_prefix_only.'usersettings')){
|
---|
| 298 | if (strpos($user_meta->meta_key,$wp_prefix_only.'usersettingstime'))
|
---|
| 299 | $new_meta_key = $user_meta_prefix . 'usersettingstime';
|
---|
| 300 | else
|
---|
| 301 | $new_meta_key = $user_meta_prefix . 'usersettings';
|
---|
| 302 | }
|
---|
| 303 | if (!empty($new_meta_key)){
|
---|
| 304 | $repair_sql = "UPDATE $db_xpress_usermeta ";
|
---|
| 305 | $repair_sql .= 'SET ';
|
---|
| 306 | $repair_sql .= "meta_key = '$new_meta_key' ";
|
---|
| 307 | $repair_sql .= "WHERE (umeta_id = $user_meta->umeta_id )";
|
---|
| 308 | $xoops_db->query($repair_sql);
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | // repair option db user_roles
|
---|
| 314 | $db_wp_option = get_wp_prefix() . 'options';
|
---|
| 315 | $new_option_name = get_wp_prefix() . 'user_roles';
|
---|
| 316 | $sql = "SELECT option_id , option_name FROM $db_wp_option WHERE option_name LIKE '%_" . $wp_prefix_only . "user_roles'" ;
|
---|
| 317 | $option= $xoops_db->get_row($sql);
|
---|
| 318 | if ($option->option_name != $new_option_name){
|
---|
| 319 | $repair_sql = "UPDATE $db_wp_option ";
|
---|
| 320 | $repair_sql .= 'SET ';
|
---|
| 321 | $repair_sql .= "option_name = '$new_option_name' ";
|
---|
| 322 | $repair_sql .= "WHERE (option_id = $option->option_id )";
|
---|
| 323 | $xoops_db->query($repair_sql);
|
---|
| 324 | }
|
---|
| 325 | }
|
---|
[72] | 326 | ?> |
---|