PluginProbe
Loginizer / 2.0.3
Loginizer v2.0.3
2.1.0 2.0.9 2.0.8 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 All 74 releases
loginizer / main / social-base.php

social-base.php in Loginizer 2.0.3, at main/social-base.php

356 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if(!defined('ABSPATH')){
4 die('Hacking Attempt!');
5 }
6
7 class Loginizer_Social_Base{
8
9 public static $error = [];
10 public static $test = false;
11 public static $ref = '';
12 public static $interim_login = '';
13 public static $provider = '';
14 public static $storage;
15
16 protected static function login_user($user, $username = '', $password = ''){
17
18 if(isset($user) && is_object($user) && property_exists($user, 'ID') && empty(self::$test)){
19 clean_user_cache(get_current_user_id());
20 clean_user_cache($user->ID);
21 wp_clear_auth_cookie();
22
23 do_action('authenticate', $user, $user->user_login, '');
24
25 // If the user has enabled limit concurrent sessions
26 if(defined('LOGINIZER_PRO_VERSION')){
27 $limit_session = apply_filters('loginizer_pro_limit_sessions', $user);
28 if(!empty($limit_session) && is_wp_error($limit_session)){
29 self::$error['concurrent_logins'] = $limit_session->get_error_message();
30 return false;
31 }
32 }
33
34 wp_set_current_user($user->ID, $user->user_login);
35 wp_set_auth_cookie($user->ID, true, is_ssl());
36 do_action('wp_login', $user->user_login, $user);
37 update_user_caches($user);
38
39 return true;
40 }
41
42 return false;
43 }
44
45 /**
46 * Creates a User account
47 *
48 * @param mixed[] $data Data we get from the Social App
49 * @return void
50 */
51 protected static function register_account($data){
52 global $loginizer;
53
54 $username = $data['first_name'] . $data['last_name'];
55
56 if(empty($username)){
57 $parsed_email = explode('@', $data['email']);
58
59 if(!empty($parsed_email[0])){
60 $username = preg_replace('/[^A-Za-z0-9\-]/', '', $parsed_email[0]);
61 }
62 }
63
64 $username = str_replace(' ', '', strtolower($username));
65 $username = sanitize_user($username, true);
66
67 $i = 1;
68 while(username_exists($username)){
69 $username .= $i;
70 $i++;
71 }
72
73 $password = wp_generate_password(12);
74 $userdata = [
75 'user_login' => sanitize_text_field($username),
76 'user_pass' => $password,
77 'user_email' => sanitize_email($data['email']),
78 'role' => (!empty($loginizer['social_settings']['general']['default_role']) ? sanitize_text_field($loginizer['social_settings']['general']['default_role']) : 'subscriber'),
79 'show_admin_bar_front' => (!empty($loginizer['social_settings']['general']['hide_admin_bar']) ? false : true),
80 ];
81
82 $user_id = wp_insert_user($userdata);
83
84 // TODO: Handle Error here.
85 if(is_wp_error($user_id)){
86 self::$error['registration_failed'] = __('Something went wrong while creating the user', 'loginizer'). $user_id->get_error_message();
87 return;
88 }
89
90 if(empty($user_id)){
91 self::$error['registration_failed'] = __('Unable to register your account, try again later!', 'loginizer');
92 self::close_tab();
93 return;
94 }
95
96 update_user_option($user_id, 'default_password_nag', true, true); // This will show alert to user to change the password.
97 $user = get_user_by('ID', $user_id);
98
99 // Save avatar if possible.
100 $tried_to_download = get_user_meta($user->ID, 'loginizer_avatar_download', true);
101 if(!empty($data['photoURL']) && !empty($loginizer['social_settings']['general']['save_avatar']) && empty($tried_to_download)){
102 self::save_avatar($data['photoURL'], $user->ID);
103 }
104
105 // Logging In the new user.
106 self::login_user($user);
107
108 // Closing the tab and redirecting to the admin.
109 $redirect_to = admin_url();
110
111 self::close_tab();
112
113 }
114
115 /**
116 * Close the Tab or redirects back to the Login Page.
117 *
118 * @param string $redirect_to URL where the user should be redirected, leave empty if want to redirect to admin.
119 * @return void
120 */
121 protected static function close_tab(){
122 global $loginizer;
123
124 // Check if the URL is safe to use.
125 $redirect_to = '';
126 if(!empty(self::$ref)){
127 $redirect_to = self::handle_redirect(self::$ref);
128 }
129
130 $target_window = 'same'; // If to redirect or to close the poup
131 $is_interim = ''; // If interim add query string as a identifier
132 if(self::$interim_login == 'lz'){
133 $target_window = 'popup';
134 $is_interim = '?interim_login=lz';
135 } else if(!empty(self::$test)){
136 $target_window = 'popup';
137
138 $redirect_to .= '&provider='.self::$provider.'&test=1';
139 }else if(!empty($loginizer['social_settings']['general']['target_window'])){
140 $target_window = $loginizer['social_settings']['general']['target_window'];
141 }
142
143 if(empty($redirect_to) || $redirect_to == admin_url()){
144 $redirect_to = admin_url($is_interim);
145 }
146
147 if($target_window === 'same'){
148 wp_safe_redirect($redirect_to);
149 die();
150 }
151
152 if(isset(self::$interim_login) && self::$interim_login === 'lz' && is_user_logged_in()){
153 echo esc_html__('Login Successful', 'loginizer');
154 }
155
156 echo '<script>
157 window.opener.location.href="'.wp_validate_redirect(wp_sanitize_redirect($redirect_to)).'";
158 window.close();
159 </script>';
160
161 die();
162 }
163
164 // Download the avatar and returns Image ID
165 protected static function save_avatar($url, $user_id){
166
167 update_user_meta($user_id, 'loginizer_avatar_download', true);
168
169 $tmp_file = self::download_avatar($url);
170
171 if(is_wp_error($tmp_file) || empty($tmp_file)){
172 return $tmp_file;
173 }
174
175 $mime = wp_get_image_mime($tmp_file);
176
177 $allowed_mime = [
178 'image/webp' => 'webp',
179 'image/tiff' => 'tif',
180 'image/gif' => 'gif',
181 'image/jpeg' => 'jpg',
182 'image/bmp' => 'bmp',
183 'image/png' => 'png',
184 ];
185
186 if(!array_key_exists($mime, $allowed_mime)){
187 error_log('Loginizer Error: ' . __('The avatar has unsupported mime type.', 'loginizer'));
188 return;
189 }
190
191 $upload_dir = wp_upload_dir();
192 $avatar_upload_dir = trailingslashit($upload_dir['basedir']) . 'lz_avatars';
193
194 if(!wp_mkdir_p($avatar_upload_dir)){
195 error_log('Loginizer Error: ' . __('Unable to create Directory to save avatars', 'loginizer'));
196 return;
197 }
198
199 $avatar_file = wp_hash($user_id) .'.'. $allowed_mime[$mime];
200 $avatar_file = wp_unique_filename($avatar_upload_dir, $avatar_file);
201 $avatar_file_path = trailingslashit($avatar_upload_dir) . $avatar_file;
202
203 $new_file = copy($tmp_file, $avatar_file_path);
204 unlink($tmp_file);
205
206 if(empty($new_file)){
207 error_log('Loginizer Error: ' . __('Unable to copy the avatar from the tmp file', 'loginizer'));
208 return;
209 }
210
211 $avatar_url = $upload_dir['baseurl'] . '/lz_avatars/' . basename($avatar_file);
212
213 $attachment = [
214 'guid' => $avatar_url,
215 'post_title' => '',
216 'post_content' => '',
217 'post_author' => $user_id,
218 'post_status' => 'private',
219 'post_mime_type' => $mime,
220 ];
221
222 $attachment_id = wp_insert_attachment($attachment, $avatar_file_path);
223
224 if(is_wp_error($attachment_id)){
225 unlink($avatar_file_path);
226 error_log('Loginizer Error: ' . __('Unable to create an attachment of the Avatar', 'loginizer'));
227 return;
228 }
229
230 global $wpdb, $blog_id;
231
232 include_once(ABSPATH . 'wp-admin/includes/image.php');
233
234 wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $avatar_file_path));
235
236 update_post_meta($attachment_id, '_wp_attachment_wp_user_avatar', $user_id);
237 update_user_meta($user_id, $wpdb->get_blog_prefix($blog_id) . 'lz_avatar', $attachment_id);
238
239 }
240
241 private static function download_avatar($url){
242
243 if(empty($url)){
244 error_log('Loginizer Error: ' . __('The URL provided to download avatar is empty', 'loginizer'));
245 return;
246 }
247
248 $tmp_file = uniqid();
249
250 if(empty($tmp_file)){
251 error_log('Loginizer Error: ' . __('Unable to create a tmp file!', 'loginizer'));
252 return;
253 }
254
255 $response = wp_remote_get($url, [
256 'timeout' => 30,
257 'stream' => true,
258 'filename' => $tmp_file,
259 ]);
260
261 if(is_wp_error($response)){
262 unlink($tmp_file);
263 error_log('Loginizer Error: ' . __('Download of the avatar failed!', 'loginizer'));
264 return;
265 }
266
267 $code = wp_remote_retrieve_response_code($response);
268
269 if($code != 200){
270 unlink($tmp_file);
271 error_log('Loginizer Error: ' . sprintf(__('Download of the avatar failed with error code %s!', 'loginizer'), esc_html($code)));
272 return;
273 }
274
275 $content_md5 = wp_remote_retrieve_header($response, 'content-md5');
276 if(!empty($content_md5)){
277 if(!function_exists('verify_file_md5')){
278 include_once ABSPATH . 'wp-admin/includes/file.php';
279 }
280
281 $md5_check = verify_file_md5($tmp_file, $content_md5);
282 if(is_wp_error($md5_check)){
283 unlink($tmpfname);
284 return $md5_check;
285 }
286 }
287
288 return $tmp_file;
289 }
290
291 protected static function handle_redirect($url){
292
293 if(empty($url)){
294 return '';
295 }
296
297 $url = rawurldecode($url);
298 $parsed_url = parse_url($url);
299
300 // If we have something in redirect to, then redirect to that page
301 if(!empty($parsed_url['query'])){
302 preg_match('/(redirect_to|redirect)=([^&]*)/', $parsed_url['query'], $redirect_url);
303
304 if(!empty($redirect_url[2])){
305 return rawurldecode($redirect_url[2]);
306 }
307 }
308
309 // Reloading the page wont show the admin page so we need to redirect it to the admin page.
310 if($parsed_url['scheme'].'://'.$parsed_url['host'] . $parsed_url['path'] == wp_login_url()){
311 return admin_url();
312 }
313
314 // If none of the above happens then we will just make the page reload.
315 return $url;
316 }
317
318 static function trigger_error(){
319 global $loginizer;
320
321 if(empty(self::$error)){
322 return;
323 }
324
325 // If we are testing we can just die,
326 // becuase we don't want the user to be redirected anywhere
327 if(!empty(self::$test) || (!empty(self::$storage) && self::$storage->get('test'))){
328 wp_die(wp_kses_post(current(self::$error)));
329 }
330
331 if(loginizer_is_whitelisted()){
332 $loginizer['ip_is_whitelisted'] = 1;
333 }
334
335 do_action('wp_login_failed', '');
336
337 self::error_state();
338 self::close_tab(); // This will redirect to the appropriate page.
339 }
340
341 // Stores the errors to be used once redirected.
342 static function error_state(){
343 global $loginizer;
344
345 $data = [
346 'errors' => self::$error,
347 'retries_left' => $loginizer['retries_left']
348 ];
349
350 $identifier = uniqid('lz_social', true);
351 set_site_transient($identifier, $data, 300);
352
353 setcookie('lz_social_error', $identifier, time() + 300, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);
354 }
355 }
356