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