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