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