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