| 1 |
<?php |
| 2 |
|
| 3 |
if(!defined('ABSPATH')){ |
| 4 |
die('Hacking Attempt!'); |
| 5 |
} |
| 6 |
|
| 7 |
include_once dirname(__FILE__, 2) . '/lib/hybridauth/autoload.php'; |
| 8 |
include_once __DIR__ .'/social-base.php'; |
| 9 |
|
| 10 |
use Hybridauth\Exception\Exception; |
| 11 |
use Hybridauth\Hybridauth; |
| 12 |
use Hybridauth\HttpClient; |
| 13 |
use Hybridauth\Storage\Transient; |
| 14 |
|
| 15 |
class Loginizer_Social_Login extends Loginizer_Social_Base{ |
| 16 |
|
| 17 |
// Process all the requests from here. |
| 18 |
static function login_init(){ |
| 19 |
global $loginizer; |
| 20 |
|
| 21 |
try { |
| 22 |
self::$storage = new Transient(); |
| 23 |
|
| 24 |
// Security check here. |
| 25 |
$lz_social_nonce = ''; |
| 26 |
if(!empty($_GET['social_security'])){ |
| 27 |
$lz_social_nonce = sanitize_text_field(wp_unslash($_GET['social_security'])); |
| 28 |
} elseif(!empty(self::$storage) && !empty(self::$storage->get('social_security'))){ |
| 29 |
$lz_social_nonce = sanitize_text_field(self::$storage->get('social_security')); |
| 30 |
} |
| 31 |
|
| 32 |
if(!wp_verify_nonce($lz_social_nonce, 'loginizer_social_check')){ |
| 33 |
self::$error['security-check'] = __('Security check failed when trying to login', 'loginizer'); |
| 34 |
self::trigger_error(); |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
$providers = self::build_provider_arr(); |
| 39 |
|
| 40 |
if(empty($providers)){ |
| 41 |
self::$error['login_error'] = __('No Provider is configured, please contact the admin about this issue', 'loginizer'); |
| 42 |
self::trigger_error(); |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
$callback_query = []; |
| 47 |
$callback_query['lz_social_provider'] = lz_optget('lz_social_provider'); |
| 48 |
|
| 49 |
$config = [ |
| 50 |
// Location where to redirect users once they authenticate with a provider |
| 51 |
'callback' => wp_login_url().'?'.http_build_query($callback_query), |
| 52 |
|
| 53 |
// Providers specifics |
| 54 |
'providers' => $providers |
| 55 |
]; |
| 56 |
|
| 57 |
$hybridauth = new Hybridauth($config, null, self::$storage); |
| 58 |
|
| 59 |
//Step 1: Here we will be redirected to the App Auth page |
| 60 |
if(!empty($_GET['lz_social_provider'])){ |
| 61 |
if(is_array($hybridauth->getProviders()) && in_array($_GET['lz_social_provider'], $hybridauth->getProviders())) { |
| 62 |
// Store the provider for the callback event |
| 63 |
self::$storage->set('provider', lz_optget('lz_social_provider')); |
| 64 |
|
| 65 |
if(!empty($_REQUEST['test'])){ |
| 66 |
self::$storage->set('test', true); |
| 67 |
} |
| 68 |
|
| 69 |
self::$storage->set('social_security', wp_create_nonce('loginizer_social_check')); |
| 70 |
|
| 71 |
if(!empty($_REQUEST['ref']) && wp_http_validate_url(sanitize_url(wp_unslash($_REQUEST['ref'])))){ |
| 72 |
self::$storage->set('ref', rawurlencode(sanitize_url(wp_unslash($_REQUEST['ref'])))); |
| 73 |
} |
| 74 |
|
| 75 |
if(isset($_REQUEST['interim-login'])){ |
| 76 |
self::$storage->set('interim_login', 'lz'); |
| 77 |
} |
| 78 |
|
| 79 |
} else { |
| 80 |
self::$error['provider_error'] = esc_html__('The app you are trying to login through is not configured', 'loginizer'); |
| 81 |
self::trigger_error(); |
| 82 |
return; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// Step 2: After we are back from the Apps auth page. |
| 87 |
if($provider = self::$storage->get('provider')){ |
| 88 |
if(!is_array($hybridauth->getProviders()) || !in_array($provider, $hybridauth->getProviders())) { |
| 89 |
self::$error['provider_error'] = esc_html__('The app you are trying to login through is not configured', 'loginizer'); |
| 90 |
self::trigger_error(); |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
$hybridauth->authenticate($provider); |
| 95 |
|
| 96 |
self::$storage->set('provider', null); // Cleaning |
| 97 |
self::$storage->delete('social_security'); |
| 98 |
|
| 99 |
self::$provider = $provider; |
| 100 |
|
| 101 |
if(self::$storage->get('test')){ |
| 102 |
self::$storage->delete('test'); |
| 103 |
self::$test = true; |
| 104 |
} |
| 105 |
|
| 106 |
if(self::$storage->get('ref')){ |
| 107 |
self::$ref = rawurldecode(self::$storage->get('ref')); |
| 108 |
self::$storage->delete('ref'); |
| 109 |
} |
| 110 |
|
| 111 |
if(self::$storage->get('interim_login')){ |
| 112 |
self::$interim_login = self::$storage->get('interim_login'); |
| 113 |
self::$storage->delete('interim_login'); |
| 114 |
} |
| 115 |
|
| 116 |
// Retrieve the provider record |
| 117 |
$adapter = $hybridauth->getAdapter($provider); |
| 118 |
$userProfile = $adapter->getUserProfile(); |
| 119 |
$accessToken = $adapter->getAccessToken(); |
| 120 |
|
| 121 |
// Check if the user have account which is verified |
| 122 |
if(empty($userProfile->emailVerified)){ |
| 123 |
self::$error['login_failed'] = __('The social account you are using does not have a verified email.', 'loginizer'); |
| 124 |
$adapter->disconnect(); |
| 125 |
self::trigger_error(); |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
$data = [ |
| 130 |
'access_token' => $accessToken, |
| 131 |
'identifier' => $userProfile->identifier, |
| 132 |
'email' => $userProfile->email, |
| 133 |
'first_name' => $userProfile->firstName, |
| 134 |
'last_name' => $userProfile->lastName, |
| 135 |
'photoURL' => !empty($userProfile->photoURL) ? strtok($userProfile->photoURL, '?') : '', |
| 136 |
]; |
| 137 |
|
| 138 |
$adapter->disconnect(); |
| 139 |
|
| 140 |
if(empty($data['email'])){ |
| 141 |
self::$error['login_failed'] = __('No email details were returned !', 'loginizer'); |
| 142 |
self::trigger_error(); |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
// If it is a test then, we are satisfied that the Provider is returning data |
| 147 |
// As this verifies the provider is working. |
| 148 |
if(self::$test === true){ |
| 149 |
self::close_tab(); |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
// Create an account if it does not exists. |
| 154 |
if(empty(email_exists(sanitize_email($data['email'])))){ |
| 155 |
if(self::$test === true){ |
| 156 |
self::$error['test_error'] = __('The email you are using for the test is not registered on this website. So register this email first.', 'loginizer'); |
| 157 |
self::trigger_error(); |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
if(defined('LOGINIZER_PREMIUM') && !empty($loginizer['social_settings']['general']['register_new'])){ |
| 162 |
self::register_account($data); |
| 163 |
echo 'Register Acount'; |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
self::$error['login_error'] = __('You can not register through Social Login', 'loginizer'); |
| 168 |
self::trigger_error(); |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
$user = get_user_by('email', sanitize_email($data['email'])); |
| 173 |
if(empty($user)){ |
| 174 |
self::$error['login_error'] = __('User with this email does not exists.', 'loginizer'); |
| 175 |
self::trigger_error(); |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
$authenticated = loginizer_wp_authenticate($user, $user->user_login, $user->user_pass); |
| 180 |
if(is_wp_error($authenticated)){ |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
self::login_user($user); |
| 185 |
self::close_tab(); |
| 186 |
} |
| 187 |
|
| 188 |
}catch(\Exception $e){ |
| 189 |
@error_log('Loginizer Log(Social): '. esc_html($e->getMessage())); |
| 190 |
self::$error['login_error'] = __('Oops, we ran into an issue! ', 'loginizer') . $e->getMessage(); |
| 191 |
self::trigger_error(); |
| 192 |
//wp_safe_redirect(wp_login_url()); |
| 193 |
return; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Creates an array of Config which is valid for HybridAuth using the setting of the provider. |
| 199 |
* |
| 200 |
* @return mixed[] |
| 201 |
*/ |
| 202 |
private static function build_provider_arr(){ |
| 203 |
$config = []; |
| 204 |
$providers = get_option('loginizer_provider_settings', []); |
| 205 |
|
| 206 |
if(empty($providers)){ |
| 207 |
return $config; |
| 208 |
} |
| 209 |
|
| 210 |
foreach($providers as $key => $provider){ |
| 211 |
|
| 212 |
if(empty($provider['enabled']) || empty($provider['client_id']) || empty($provider['client_secret'])){ |
| 213 |
continue; |
| 214 |
} |
| 215 |
|
| 216 |
$config_index = ucfirst($key); |
| 217 |
|
| 218 |
$config[$config_index] = [ |
| 219 |
'enabled' => true, |
| 220 |
'keys' => [ |
| 221 |
'id' => $provider['client_id'], |
| 222 |
'secret' => $provider['client_secret'] |
| 223 |
] |
| 224 |
]; |
| 225 |
|
| 226 |
if($key == 'MicrosoftGraph' && !empty($provider['account_type']) && $provider['account_type'] != 'common'){ |
| 227 |
$config[$config_index]['tenant'] = $provider['account_type']; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
return $config; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
Loginizer_Social_Login::login_init(); |