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