[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 | */
|
---|
| 36 | function get_xoops_group_role($mydirname , $uid=0){
|
---|
| 37 | $ans = array();
|
---|
[72] | 38 | if ($uid == 0){
|
---|
| 39 | return '';
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[85] | 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";
|
---|
[72] | 50 |
|
---|
[85] | 51 | $user_groups = $xoops_db->get_results($sql);
|
---|
| 52 | $ans['capabillities'] = '';
|
---|
| 53 | $ans['allway_update'] = 0;
|
---|
[72] | 54 |
|
---|
[85] | 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 | }
|
---|
[85] | 63 | return $ans;
|
---|
[72] | 64 | }
|
---|
| 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 |
|
---|
| 76 |
|
---|
| 77 | //if( ! function_exists( 'xpress_usermeta_sync' ) ) :
|
---|
| 78 | function xpress_usermeta_sync($mydirname , $uid,$meta_key,$meta_value, &$message){
|
---|
[85] | 79 | $wp_prefix = get_wp_prefix();
|
---|
[72] | 80 | $meta_key = "'" . $meta_key . "'";
|
---|
| 81 | $meta_value = "'" . $meta_value . "'";
|
---|
| 82 | $xoopsDB =& Database::getInstance();
|
---|
| 83 | $db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta');
|
---|
| 84 | $sql = "SELECT user_id FROM $db_xpress_usermeta WHERE (user_id = $uid) AND (meta_key = $meta_key)";
|
---|
| 85 | $res = $xoopsDB->query($sql, 0, 0);
|
---|
| 86 |
|
---|
| 87 | if ($res === false){
|
---|
| 88 | $sql2 = "INSERT INTO $db_xpress_usermeta (user_id,meta_key,meta_value) VALUES ($uid,$meta_key,$meta_value)";
|
---|
| 89 | }else {
|
---|
| 90 | if ($xoopsDB->getRowsNum($res) > 0){
|
---|
| 91 |
|
---|
| 92 | $sql2 = "UPDATE $db_xpress_usermeta SET meta_value = $meta_value WHERE (user_id = $uid) AND (meta_key = $meta_key)";
|
---|
| 93 | } else {
|
---|
| 94 | $sql2 = "INSERT INTO $db_xpress_usermeta (user_id,meta_key,meta_value) VALUES ($uid,$meta_key,$meta_value)";
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | $res = $xoopsDB->queryF($sql2, 0, 0);
|
---|
| 98 | if ($res === false) {
|
---|
| 99 | $message .= "...ERR($sql2)";
|
---|
| 100 | return false;
|
---|
| 101 | } else {
|
---|
| 102 | return true;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | }
|
---|
| 106 | //endif;
|
---|
| 107 |
|
---|
[85] | 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';
|
---|
[72] | 123 |
|
---|
[85] | 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 | }
|
---|
[72] | 129 |
|
---|
[85] | 130 | $xoops_users = $xoops_db->get_results($xu_sql);
|
---|
| 131 | if (empty($xoops_users)){
|
---|
| 132 | $message .= '...ERR ('. $xu_sql . ')';
|
---|
| 133 | return false;
|
---|
| 134 | }
|
---|
[72] | 135 |
|
---|
[85] | 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 | }
|
---|
[72] | 146 | } else {
|
---|
[85] | 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';
|
---|
[72] | 164 |
|
---|
[85] | 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' )";
|
---|
[72] | 178 |
|
---|
[85] | 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 . ')';
|
---|
[72] | 184 |
|
---|
[85] | 185 | } else {
|
---|
| 186 | $message .= '...UPDATE ' . $user_login_name . '(Level Not Change)';
|
---|
[72] | 187 | }
|
---|
[85] | 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 . ')';
|
---|
[72] | 200 | }
|
---|
[85] | 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 | }
|
---|
[72] | 212 | }
|
---|
| 213 | }
|
---|
[85] | 214 | $message .= "...END";
|
---|
| 215 | return true;
|
---|
[72] | 216 | }
|
---|
[85] | 217 | }
|
---|
[72] | 218 |
|
---|
| 219 | //if( ! function_exists( 'check_user_role_prefix' ) ) :
|
---|
| 220 | function check_user_role_prefix($mydirname){
|
---|
[85] | 221 | $wp_prefix = get_wp_prefix();
|
---|
[72] | 222 | $xoopsDB =& Database::getInstance();
|
---|
| 223 | $db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta');
|
---|
| 224 | $user_meta_prefix =$xoopsDB->prefix($wp_prefix);
|
---|
| 225 |
|
---|
| 226 | $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'";
|
---|
| 227 | $result = $xoopsDB->query($sql, 0, 0);
|
---|
| 228 | if ($result === false){
|
---|
| 229 | return -1;
|
---|
| 230 | }else{
|
---|
| 231 | $i=0;
|
---|
| 232 | while($row = $xoopsDB->fetchArray($result)){
|
---|
| 233 | $meta_key = $row['meta_key'];
|
---|
| 234 | if (strpos($meta_key,$user_meta_prefix) === false) {
|
---|
| 235 | $i++;
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 | return $i;
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | //endif;
|
---|
| 242 |
|
---|
| 243 | //if( ! function_exists( 'same_metakey_delete' ) ) :
|
---|
| 244 | function same_metakey_delete($mydirname,$metakey){
|
---|
[85] | 245 | $wp_prefix = get_wp_prefix();
|
---|
[72] | 246 | $xoopsDB =& Database::getInstance();
|
---|
| 247 | $db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta');
|
---|
| 248 | $user_meta_prefix =$xoopsDB->prefix($wp_prefix);
|
---|
| 249 |
|
---|
| 250 | $sql = "DELETE FROM $db_xpress_usermeta WHERE meta_key LIKE '$metakey'";
|
---|
| 251 | $result = $xoopsDB->query($sql, 0, 0);
|
---|
| 252 | if ($result === false){
|
---|
| 253 | return false;
|
---|
| 254 | }else{
|
---|
| 255 | return true;
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 | //endif;
|
---|
| 259 |
|
---|
| 260 | //if( ! function_exists( 'repair_user_role_prefix' ) ) :
|
---|
| 261 | function repair_user_role_prefix($mydirname){
|
---|
[85] | 262 | $wp_prefix = get_wp_prefix();
|
---|
[72] | 263 | $xoopsDB =& Database::getInstance();
|
---|
| 264 | $db_xpress_usermeta = $xoopsDB->prefix($wp_prefix . 'usermeta');
|
---|
| 265 | $user_meta_prefix =$xoopsDB->prefix($wp_prefix);
|
---|
| 266 | $ret = '';
|
---|
| 267 |
|
---|
| 268 | $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'";
|
---|
| 269 | $result = $xoopsDB->queryF($sql, 0, 0);
|
---|
| 270 | if ($result === false){
|
---|
| 271 | return -1;
|
---|
| 272 | }else{
|
---|
| 273 | $i=0;
|
---|
| 274 | while($row = $xoopsDB->fetchArray($result)){
|
---|
| 275 | $meta_key = $row['meta_key'];
|
---|
| 276 | $umeta_id = $row['umeta_id'];
|
---|
| 277 | if (strpos($meta_key,$user_meta_prefix) === false) {
|
---|
| 278 | $new_meta_key = '';
|
---|
| 279 | if (strpos($meta_key,$wp_prefix.'user_level')){
|
---|
| 280 | $new_meta_key = $xoopsDB->prefix($wp_prefix . 'user_level');
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | if (strpos($meta_key,$wp_prefix.'capabilities')){
|
---|
| 284 | $new_meta_key = $xoopsDB->prefix($wp_prefix . 'capabilities');
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | if (strpos($meta_key,$wp_prefix.'autosave_draft_ids')){
|
---|
| 288 | $new_meta_key = $xoopsDB->prefix($wp_prefix . 'autosave_draft_ids');
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | if (!empty($new_meta_key)){
|
---|
| 292 | same_metakey_delete($mydirname,$new_meta_key);
|
---|
| 293 |
|
---|
| 294 | $repair_sql = "UPDATE $db_xpress_usermeta ";
|
---|
| 295 | $repair_sql .= 'SET ';
|
---|
| 296 | $repair_sql .= "meta_key = '$new_meta_key' ";
|
---|
| 297 | $repair_sql .= "WHERE (umeta_id = $umeta_id )";
|
---|
| 298 | $repair_res = $xoopsDB->queryF($repair_sql, 0, 0);
|
---|
| 299 | if ($repair_res === false){
|
---|
| 300 | $ret .= '...ERR(' . $meta_key . '->' . $new_meta_key . ')<br>';
|
---|
| 301 | } else {
|
---|
| 302 | $ret .= '...DO(' . $meta_key . '->' . $new_meta_key . ')<br>';
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 | return $ret;
|
---|
| 309 | }
|
---|
| 310 | //endif;
|
---|
| 311 |
|
---|
| 312 |
|
---|
| 313 | /*
|
---|
| 314 | * Get User ID of XOOPS from the login name.
|
---|
| 315 | */
|
---|
| 316 | function get_xoops_user_id($login_name){
|
---|
| 317 | global $xoops_db;
|
---|
| 318 | $sql = "SELECT uid FROM " . get_xoops_prefix() . "users WHERE uname = '$login_name'";
|
---|
| 319 | $uid = $xoops_db->get_var($sql);
|
---|
| 320 | return $uid;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | /*
|
---|
| 324 | *The user data of wordpress is written in the xoops user data.
|
---|
| 325 | */
|
---|
| 326 | function user_sync_to_xoops($user_ID){
|
---|
| 327 | global $xoops_db;
|
---|
| 328 |
|
---|
| 329 | $user_info = get_userdata($user_ID);
|
---|
| 330 | $xoops_uid = get_xoops_user_id($user_info->user_login);
|
---|
| 331 | $db_xoops_users = get_xoops_prefix() . "users";
|
---|
| 332 | $user_regdate = strtotime($user_info->user_registered);
|
---|
| 333 | $aim = get_usermeta($user_ID,'aim');
|
---|
| 334 | $yim = get_usermeta($user_ID,'yim');
|
---|
| 335 |
|
---|
| 336 | if ($xoops_uid){
|
---|
| 337 | $wu_sql = "UPDATE $db_xoops_users ";
|
---|
| 338 | $wu_sql .= 'SET ';
|
---|
| 339 | $wu_sql .= "uname = '$user_info->user_login', ";
|
---|
| 340 | $wu_sql .= "pass = '$user_info->user_pass', ";
|
---|
| 341 | $wu_sql .= "email = '$user_info->user_email', ";
|
---|
| 342 | $wu_sql .= "url = '$user_info->user_url', ";
|
---|
| 343 | $wu_sql .= "name = '$user_info->display_name', ";
|
---|
| 344 | $wu_sql .= "user_aim = '$aim', ";
|
---|
| 345 | $wu_sql .= "user_yim = '$yim', ";
|
---|
| 346 | $wu_sql .= "user_regdate = $user_regdate ";
|
---|
| 347 | $wu_sql .= "WHERE (uid = $xoops_uid )";
|
---|
[82] | 348 | $xoops_db->query($wu_sql);
|
---|
[72] | 349 | }else{
|
---|
| 350 | $wu_sql = "INSERT INTO $db_xoops_users ";
|
---|
| 351 | $wu_sql .= "(uname , pass ,email , url , name , user_aim , user_yim , user_regdate) " ;
|
---|
| 352 | $wu_sql .= "VALUES ";
|
---|
| 353 | $wu_sql .= "('$user_info->user_login', '$user_info->user_pass', '$user_info->user_email', '$user_info->user_url', '$user_info->display_name' ";
|
---|
| 354 | $wu_sql .= " , '$aim' , '$yim' , $user_regdate )";
|
---|
[82] | 355 | $xoops_db->query($wu_sql);
|
---|
| 356 | // get xoops users default groupe ID
|
---|
| 357 | $db_xoops_group = get_xoops_prefix() . 'groups';
|
---|
| 358 | $default_xoops_group_id = $xoops_db->get_var("SELECT groupid FROM $db_xoops_group WHERE group_type = 'User'");
|
---|
| 359 | // get insert users ID
|
---|
| 360 | $db_xoops_group = get_xoops_prefix() . 'users';
|
---|
| 361 | $user_id = $xoops_db->get_var("SELECT uid FROM $db_xoops_group WHERE uname = '$user_info->user_login'");
|
---|
| 362 | // insert groups_users_link
|
---|
| 363 | $db_xoops_group_users_link = get_xoops_prefix() . 'groups_users_link';
|
---|
| 364 | $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] | 365 | }
|
---|
| 366 | }
|
---|
| 367 | ?> |
---|