PluginProbe
Loginizer / 2.0.2
Loginizer v2.0.2
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.2, at main/social-base.php

360 lines 10.2 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 if(!empty(self::$ref)){
126 $redirect_to = self::handle_redirect(self::$ref);
127 }
128
129 $target_window = 'same'; // If to redirect or to close the poup
130 $is_interim = ''; // If interim add query string as a identifier
131 if(self::$interim_login == 'lz'){
132 $target_window = 'popup';
133 $is_interim = '?interim_login=lz';
134 } else if(!empty(self::$test)){
135 $target_window = 'popup';
136
137 $redirect_to .= '&provider='.self::$provider.'&test=1';
138 }else if(!empty($loginizer['social_settings']['general']['target_window'])){
139 $target_window = $loginizer['social_settings']['general']['target_window'];
140 }
141
142 if(empty($redirect_to) || $redirect_to == admin_url()){
143 $redirect_to = admin_url($is_interim);
144 }
145
146 if($target_window === 'same'){
147 wp_safe_redirect($redirect_to);
148 die();
149 }
150
151 if(isset(self::$interim_login) && self::$interim_login === 'lz' && is_user_logged_in()){
152 echo esc_html__('Login Successful', 'loginizer');
153 }
154
155 echo '<script>
156 window.opener.location.href="'.wp_validate_redirect(wp_sanitize_redirect($redirect_to)).'";
157 window.close();
158 </script>';
159
160 die();
161 }
162
163 // Download the avatar and returns Image ID
164 protected static function save_avatar($url, $user_id){
165
166 update_user_meta($user_id, 'loginizer_avatar_download', true);
167
168 $tmp_file = self::download_avatar($url);
169
170 if(is_wp_error($tmp_file) || empty($tmp_file)){
171 return $tmp_file;
172 }
173
174 $mime = wp_get_image_mime($tmp_file);
175
176 $allowed_mime = [
177 'image/webp' => 'webp',
178 'image/tiff' => 'tif',
179 'image/gif' => 'gif',
180 'image/jpeg' => 'jpg',
181 'image/bmp' => 'bmp',
182 'image/png' => 'png',
183 ];
184
185 if(!array_key_exists($mime, $allowed_mime)){
186 error_log('Loginizer Error: ' . __('The avatar has unsupported mime type.', 'loginizer'));
187 return;
188 }
189
190 $upload_dir = wp_upload_dir();
191 $avatar_upload_dir = trailingslashit($upload_dir['basedir']) . 'lz_avatars';
192
193 if(!wp_mkdir_p($avatar_upload_dir)){
194 error_log('Loginizer Error: ' . __('Unable to create Directory to save avatars', 'loginizer'));
195 return;
196 }
197
198 $avatar_file = wp_hash($user_id) .'.'. $allowed_mime[$mime];
199 $avatar_file = wp_unique_filename($avatar_upload_dir, $avatar_file);
200 $avatar_file_path = trailingslashit($avatar_upload_dir) . $avatar_file;
201
202 $new_file = copy($tmp_file, $avatar_file_path);
203 unlink($tmp_file);
204
205 if(empty($new_file)){
206 error_log('Loginizer Error: ' . __('Unable to copy the avatar from the tmp file', 'loginizer'));
207 return;
208 }
209
210 $avatar_url = $upload_dir['baseurl'] . '/lz_avatars/' . basename($avatar_file);
211
212 $attachment = [
213 'guid' => $avatar_url,
214 'post_title' => '',
215 'post_content' => '',
216 'post_author' => $user_id,
217 'post_status' => 'private',
218 'post_mime_type' => $mime,
219 ];
220
221 $attachment_id = wp_insert_attachment($attachment, $avatar_file_path);
222
223 if(is_wp_error($attachment_id)){
224 unlink($avatar_file_path);
225 error_log('Loginizer Error: ' . __('Unable to create an attachment of the Avatar', 'loginizer'));
226 return;
227 }
228
229 global $wpdb, $blog_id;
230
231 include_once(ABSPATH . 'wp-admin/includes/image.php');
232
233 wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $avatar_file_path));
234
235 update_post_meta($attachment_id, '_wp_attachment_wp_user_avatar', $user_id);
236 update_user_meta($user_id, $wpdb->get_blog_prefix($blog_id) . 'lz_avatar', $attachment_id);
237
238 }
239
240 private static function download_avatar($url){
241
242 if(empty($url)){
243 error_log('Loginizer Error: ' . __('The URL provided to download avatar is empty', 'loginizer'));
244 return;
245 }
246
247 $tmp_file = uniqid();
248
249 if(empty($tmp_file)){
250 error_log('Loginizer Error: ' . __('Unable to create a tmp file!', 'loginizer'));
251 return;
252 }
253
254 $response = wp_remote_get($url, [
255 'timeout' => 30,
256 'stream' => true,
257 'filename' => $tmp_file,
258 ]);
259
260 if(is_wp_error($response)){
261 unlink($tmp_file);
262 error_log('Loginizer Error: ' . __('Download of the avatar failed!', 'loginizer'));
263 return;
264 }
265
266 $code = wp_remote_retrieve_response_code($response);
267
268 if($code != 200){
269 unlink($tmp_file);
270 error_log('Loginizer Error: ' . sprintf(__('Download of the avatar failed with error code %s!', 'loginizer'), esc_html($code)));
271 return;
272 }
273
274 $content_md5 = wp_remote_retrieve_header($response, 'content-md5');
275 if(!empty($content_md5)){
276 if(!function_exists('verify_file_md5')){
277 include_once ABSPATH . 'wp-admin/includes/file.php';
278 }
279
280 $md5_check = verify_file_md5($tmp_file, $content_md5);
281 if(is_wp_error($md5_check)){
282 unlink($tmpfname);
283 return $md5_check;
284 }
285 }
286
287 return $tmp_file;
288 }
289
290 protected static function handle_redirect($url){
291
292 $redirect = '';
293 if(empty($url)){
294 return $redirect;
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 $redirect;
312 }
313
314 if(strpos(wp_login_url(), $parsed_url['path']) !== FALSE){
315 return $redirect;
316 }
317
318 // If none of the above happens then we will just make the page reload.
319 return $url;
320 }
321
322 static function trigger_error(){
323 global $loginizer;
324
325 if(empty(self::$error)){
326 return;
327 }
328
329 // If we are testing we can just die,
330 // becuase we don't want the user to be redirected anywhere
331 if(!empty(self::$test) || (!empty(self::$storage) && self::$storage->get('test'))){
332 wp_die(wp_kses_post(current(self::$error)));
333 }
334
335 if(loginizer_is_whitelisted()){
336 $loginizer['ip_is_whitelisted'] = 1;
337 }
338
339 do_action('wp_login_failed', '');
340
341 self::error_state();
342 self::close_tab(); // This will redirect to the appropriate page.
343 }
344
345 // Stores the errors to be used once redirected.
346 static function error_state(){
347 global $loginizer;
348
349 $data = [
350 'errors' => self::$error,
351 'retries_left' => $loginizer['retries_left']
352 ];
353
354 $identifier = uniqid('lz_social', true);
355 set_site_transient($identifier, $data, 300);
356
357 setcookie('lz_social_error', $identifier, time() + 300, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);
358 }
359 }
360