PluginProbe
Loginizer / 2.0.1
Loginizer v2.0.1
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.1, at main/social-base.php

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