/var/www/hkosl.com/aga/wp-content/plugins/simple-membership/classes/class.swpm-auth.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php

class SwpmAuth {

    public 
$protected;
    public 
$permitted;
    private 
$isLoggedIn;
    private 
$lastStatusMsg;
    private static 
$_this;
    public 
$userData;

    private function 
__construct() {
        
//check if we need to display custom message on the login form
        
$custom_msg filter_input(INPUT_COOKIE'swpm-login-form-custom-msg'FILTER_SANITIZE_STRING);
        if (!empty(
$custom_msg)) {
            
$this->lastStatusMsg $custom_msg;
            
//let's 'unset' the cookie
            
setcookie('swpm-login-form-custom-msg'''time() - 3600COOKIEPATHCOOKIE_DOMAIN);
        }
        
$this->isLoggedIn false;
        
$this->userData null;
        
$this->protected SwpmProtection::get_instance();
    }

    private function 
init() {
        
$valid $this->validate();
        
//SwpmLog::log_auth_debug("init:". ($valid? "valid": "invalid"), true);
        
if (!$valid) {
            
$this->authenticate();
        }
    }

    public static function 
get_instance() {
        if (empty(
self::$_this)) {
            
self::$_this = new SwpmAuth();
            
self::$_this->init();
        }
        return 
self::$_this;
    }

    private function 
authenticate($user null$pass null) {
        global 
$wpdb;
        
$swpm_password = empty($pass) ? filter_input(INPUT_POST'swpm_password') : $pass;
        
$swpm_user_name = empty($user) ? apply_filters('swpm_user_name'filter_input(INPUT_POST'swpm_user_name')) : $user;

        if (!empty(
$swpm_user_name) && !empty($swpm_password)) {
            
//SWPM member login request.
            //Trigger action hook that can be used to check stuff before the login request is processed by the plugin.
            
$args = array('username' => $swpm_user_name'password' => $swpm_password);
            
do_action('swpm_before_login_request_is_processed'$args);

            
//First, lets make sure this user is not already logged into the site as an "Admin" user. We don't want to override that admin login session.
            
if (current_user_can('administrator')) {
                
//This user is logged in as ADMIN then trying to do another login as a member. Stop the login request processing (we don't want to override your admin login session).
                
$wp_profile_page SIMPLE_WP_MEMBERSHIP_SITE_HOME_URL '/wp-admin/profile.php';
                
$error_msg '';
                
$error_msg .= '<p>' SwpmUtils::_('Warning! Simple Membership plugin cannot process this login request to prevent you from getting logged out of WP Admin accidentally.') . '</p>';
                
$error_msg .= '<p><a href="' $wp_profile_page '" target="_blank">' SwpmUtils::_('Click here') . '</a>' SwpmUtils::_(' to see the profile you are currently logged into in this browser.') . '</p>';
                
$error_msg .= '<p>' SwpmUtils::_('You are logged into the site as an ADMIN user in this browser. First, logout from WP Admin then you will be able to log in as a normal member.') . '</p>';
                
$error_msg .= '<p>' SwpmUtils::_('Alternatively, you can use a different browser (where you are not logged-in as ADMIN) to test the membership login.') . '</p>';
                
$error_msg .= '<p>' SwpmUtils::_('Your normal visitors or members will never see this message. This message is ONLY for ADMIN user.') . '</p>';
                
wp_die($error_msg);
            }

            
//If captcha is present and validation failed, it returns an error string. If validation succeeds, it returns an empty string.
            
$captcha_validation_output apply_filters('swpm_validate_login_form_submission''');
            if (!empty(
$captcha_validation_output)) {
                
$this->lastStatusMsg SwpmUtils::_('Captcha validation failed on login form.');
                return;
            }

            if (
is_email($swpm_user_name)) {//User is trying to log-in using an email address
                
$email sanitize_email($swpm_user_name);
                
$query $wpdb->prepare("SELECT user_name FROM " $wpdb->prefix "swpm_members_tbl WHERE email = %s"$email);
                
$username $wpdb->get_var($query);
                if (
$username) {//Found a user record
                    
$swpm_user_name $username//Grab the usrename value so it can be used in the authentication process.
                    
SwpmLog::log_auth_debug("Authentication request using email address: " $email ', Found a user record with username: ' $swpm_user_nametrue);
                }
            }

            
//Lets process the request. Check username and password
            
$user sanitize_user($swpm_user_name);
            
$pass trim($swpm_password);
            
SwpmLog::log_auth_debug("Authentication request - Username: " $swpm_user_nametrue);

            
$query "SELECT * FROM " $wpdb->prefix "swpm_members_tbl WHERE user_name = %s";
            
$userData $wpdb->get_row($wpdb->prepare($query$user));
            
$this->userData $userData;
            if (!
$userData) {
                
$this->isLoggedIn false;
                
$this->userData null;
                
$this->lastStatusMsg SwpmUtils::_("User Not Found.");
                return 
false;
            }
            
$check $this->check_password($pass$userData->password);
            if (!
$check) {
                
$this->isLoggedIn false;
                
$this->userData null;
                
$this->lastStatusMsg SwpmUtils::_("Password Empty or Invalid.");
                return 
false;
            }
            if (
$this->check_constraints()) {
                
$rememberme filter_input(INPUT_POST'rememberme');
                
$remember = empty($rememberme) ? false true;
                
$this->set_cookie($remember);
                
$this->isLoggedIn true;
                
$this->lastStatusMsg "Logged In.";
                
SwpmLog::log_auth_debug("Authentication successful for username: " $user ". Executing swpm_login action hook."true);
                
do_action('swpm_login'$user$pass$remember);
                return 
true;
            }
        }
        return 
false;
    }

    private function 
check_constraints() {
        if (empty(
$this->userData)) {
            return 
false;
        }
        global 
$wpdb;
        
$enable_expired_login SwpmSettings::get_instance()->get_value('enable-expired-account-login''');

        
//Update the last accessed date and IP address for this login attempt. $wpdb->update(table, data, where, format, where format)
        
$last_accessed_date current_time('mysql');
        
$last_accessed_ip SwpmUtils::get_user_ip_address();
        
$wpdb->update($wpdb->prefix 'swpm_members_tbl', array('last_accessed' => $last_accessed_date'last_accessed_from_ip' => $last_accessed_ip), array('member_id' => $this->userData->member_id), array('%s''%s'), array('%d')
        );

        
//Check the member's account status.
        
$can_login true;
        if (
$this->userData->account_state == 'inactive' && empty($enable_expired_login)) {
            
$this->lastStatusMsg SwpmUtils::_('Account is inactive.');
            
$can_login false;
        } else if ((
$this->userData->account_state == 'expired') && empty($enable_expired_login)) {
            
$this->lastStatusMsg SwpmUtils::_('Account has expired.');
            
$can_login false;
        } else if (
$this->userData->account_state == 'pending') {
            
$this->lastStatusMsg SwpmUtils::_('Account is pending.');
            
$can_login false;
        } else if (
$this->userData->account_state == 'activation_required') {
            
$resend_email_url add_query_arg(array(
                
'swpm_resend_activation_email' => '1',
                
'swpm_member_id' => $this->userData->member_id,
                    ), 
get_home_url());
            
$msg sprintf(SwpmUtils::_('You need to activate your account. If you didn\'t receive an email then %s to resend the activation email.'), '<a href="' $resend_email_url '">' SwpmUtils::_('click here') . '</a>');
            
$this->lastStatusMsg $msg;
            
$can_login false;
        }

        if (!
$can_login) {
            
$this->isLoggedIn false;
            
$this->userData null;
            return 
false;
        }

        if (
SwpmUtils::is_subscription_expired($this->userData)) {
            if (
$this->userData->account_state == 'active') {
                
$wpdb->update($wpdb->prefix 'swpm_members_tbl', array('account_state' => 'expired'), array('member_id' => $this->userData->member_id), array('%s'), array('%d'));
            }
            if (empty(
$enable_expired_login)) {
                
$this->lastStatusMsg SwpmUtils::_('Account has expired.');
                
$this->isLoggedIn false;
                
$this->userData null;
                return 
false;
            }
        }

        
$this->permitted SwpmPermission::get_instance($this->userData->membership_level);
        
$this->lastStatusMsg SwpmUtils::_("You are logged in as:") . $this->userData->user_name;
        
$this->isLoggedIn true;
        return 
true;
    }

    private function 
check_password($plain_password$hashed_pw) {
        global 
$wp_hasher;
        if (empty(
$plain_password)) {
            return 
false;
        }
        if (empty(
$wp_hasher)) {
            require_once( 
ABSPATH 'wp-includes/class-phpass.php');
            
$wp_hasher = new PasswordHash(8TRUE);
        }
        return 
$wp_hasher->CheckPassword($plain_password$hashed_pw);
    }

    public function 
match_password($password) {
        if (!
$this->is_logged_in()) {
            return 
false;
        }
        return 
$this->check_password($password$this->get('password'));
    }

    public function 
login_to_swpm_using_wp_user($user) {
        if (
$this->isLoggedIn) {
            return 
false;
        }
        
$email $user->user_email;
        
$member SwpmMemberUtils::get_user_by_email($email);
        if (empty(
$member)) {
            
//There is no swpm profile with this email.
            
return false;
        }
        
$this->userData $member;
        
$this->isLoggedIn true;
        
$this->set_cookie();
        
SwpmLog::log_auth_debug('Member has been logged in using WP User object.'true);
        
$this->check_constraints();
        return 
true;
    }

    public function 
login($user$pass$remember ''$secure '') {
        
SwpmLog::log_auth_debug("SwpmAuth::login()"true);
        if (
$this->isLoggedIn) {
            return;
        }
        if (
$this->authenticate($user$pass) && $this->validate()) {
            
$this->set_cookie($remember$secure);
        } else {
            
$this->isLoggedIn false;
            
$this->userData null;
        }
        return 
$this->lastStatusMsg;
    }

    public function 
logout() {
        if (!
$this->isLoggedIn) {
            return;
        }
        
setcookie(SIMPLE_WP_MEMBERSHIP_AUTH' 'time() - YEAR_IN_SECONDSCOOKIEPATHCOOKIE_DOMAIN);
        
setcookie(SIMPLE_WP_MEMBERSHIP_SEC_AUTH' 'time() - YEAR_IN_SECONDSCOOKIEPATHCOOKIE_DOMAIN);
        
$this->userData null;
        
$this->isLoggedIn false;
        
$this->lastStatusMsg SwpmUtils::_("Logged Out Successfully.");
        
do_action('swpm_logout');
    }

    private function 
set_cookie($remember ''$secure '') {
        if (
$remember) {
            
$expiration time() + 1209600//14 days
            
$expire $expiration 43200//12 hours grace period
        
} else {
            
$expiration time() + 259200//3 days.
            
$expire $expiration//The minimum cookie expiration should be at least a few days.
        
}

        
$expire apply_filters('swpm_auth_cookie_expiry_value'$expire);

        
setcookie("swpm_in_use""swpm_in_use"$expireCOOKIEPATHCOOKIE_DOMAIN);

        
$expiration_timestamp SwpmUtils::get_expiration_timestamp($this->userData);
        
$enable_expired_login SwpmSettings::get_instance()->get_value('enable-expired-account-login''');
        
// make sure cookie doesn't live beyond account expiration date.
        // but if expired account login is enabled then ignore if account is expired
        
$expiration = empty($enable_expired_login) ? min($expiration$expiration_timestamp) : $expiration;
        
$pass_frag substr($this->userData->password84);
        
$scheme 'auth';
        if (!
$secure) {
            
$secure is_ssl();
        }
        
$key SwpmAuth::b_hash($this->userData->user_name $pass_frag '|' $expiration$scheme);
        
$hash hash_hmac('md5'$this->userData->user_name '|' $expiration$key);
        
$auth_cookie $this->userData->user_name '|' $expiration '|' $hash;
        
$auth_cookie_name $secure SIMPLE_WP_MEMBERSHIP_SEC_AUTH SIMPLE_WP_MEMBERSHIP_AUTH;
        
setcookie($auth_cookie_name$auth_cookie$expireCOOKIEPATHCOOKIE_DOMAIN$securetrue);
    }

    private function 
validate() {
        
$auth_cookie_name is_ssl() ? SIMPLE_WP_MEMBERSHIP_SEC_AUTH SIMPLE_WP_MEMBERSHIP_AUTH;
        if (!isset(
$_COOKIE[$auth_cookie_name]) || empty($_COOKIE[$auth_cookie_name])) {
            return 
false;
        }
        
$cookie_elements explode('|'$_COOKIE[$auth_cookie_name]);
        if (
count($cookie_elements) != 3) {
            return 
false;
        }

        
//SwpmLog::log_auth_debug("validate() - " . $_COOKIE[$auth_cookie_name], true);
        
list($username$expiration$hmac) = $cookie_elements;
        
$expired $expiration;
        
// Allow a grace period for POST and AJAX requests
        
if (defined('DOING_AJAX') || 'POST' == $_SERVER['REQUEST_METHOD']) {
            
$expired += HOUR_IN_SECONDS;
        }
        
// Quick check to see if an honest cookie has expired
        
if ($expired time()) {
            
$this->lastStatusMsg SwpmUtils::_("Session Expired."); //do_action('auth_cookie_expired', $cookie_elements);
            
SwpmLog::log_auth_debug("validate() - Session Expired"true);
            return 
false;
        }

        global 
$wpdb;
        
$query " SELECT * FROM " $wpdb->prefix "swpm_members_tbl WHERE user_name = %s";
        
$user $wpdb->get_row($wpdb->prepare($query$username));
        if (empty(
$user)) {
            
$this->lastStatusMsg SwpmUtils::_("Invalid Username");
            return 
false;
        }

        
$pass_frag substr($user->password84);
        
$key SwpmAuth::b_hash($username $pass_frag '|' $expiration);
        
$hash hash_hmac('md5'$username '|' $expiration$key);
        if (
$hmac != $hash) {
            
$this->lastStatusMsg SwpmUtils::_("Please login again.");
            
SwpmLog::log_auth_debug("validate() - Bad Hash"true);
            
wp_logout(); //Force logout of WP user session to clear the bad hash.
            
return false;
        }

        if (
$expiration time()) {
            
$GLOBALS['login_grace_period'] = 1;
        }
        
$this->userData $user;
        return 
$this->check_constraints();
    }

    public static function 
b_hash($data$scheme 'auth') {
        
$salt wp_salt($scheme) . 'j4H!B3TA,J4nIn4.';
        return 
hash_hmac('md5'$data$salt);
    }

    public function 
is_logged_in() {
        return 
$this->isLoggedIn;
    }

    public function 
get($key$default "") {
        if (isset(
$this->userData->$key)) {
            return 
$this->userData->$key;
        }
        if (isset(
$this->permitted->$key)) {
            return 
$this->permitted->$key;
        }
        if (!empty(
$this->permitted)) {
            return 
$this->permitted->get($key$default);
        }
        return 
$default;
    }

    public function 
get_message() {
        return 
$this->lastStatusMsg;
    }

    public function 
get_expire_date() {
        if (
$this->isLoggedIn) {
            return 
SwpmUtils::get_formatted_expiry_date($this->get('subscription_starts'), $this->get('subscription_period'), $this->get('subscription_duration_type'));
        }
        return 
"";
    }

    public function 
delete() {
        if (!
$this->is_logged_in()) {
            return;
        }
        
$user_name $this->get('user_name');
        
$user_id $this->get('member_id');
        
$subscr_id $this->get('subscr_id');
        
$email $this->get('email');
        
// let's check if Stripe subscription needs to be cancelled as well
        
global $wpdb;
        
$q $wpdb->prepare('SELECT * 
        FROM  `' 
$wpdb->prefix 'swpm_payments_tbl` 
        WHERE email =  %s
        AND gateway =  "stripe"
        AND subscr_id = %s
        LIMIT 1'
, array($email$subscr_id));

        
$member $wpdb->get_row($qARRAY_A);
        if (!
is_null($member)) {
            
//looks like we need to cancel Stripe subscription
            
$pieces explode('|'$subscr_id);
            if (!empty(
$pieces)) {
                
$subscr_id $pieces[0];
                
$button_id $pieces[1];
                
SwpmLog::log_simple_debug("Attempting to cancel Stripe Subscription #" $subscr_idtrue);
                
//check if button exists
                
if (get_post($button_id)) {
                    
$settings SwpmSettings::get_instance();
                    
$sandbox_enabled $settings->get_value('enable-sandbox-testing');
                    if (
$sandbox_enabled) {
                        
SwpmLog::log_simple_debug("Sandbox payment mode is enabled. Using test API key details."true);
                        
$secret_key get_post_meta($button_id'stripe_test_secret_key'true);
                        ; 
//Use sandbox API key
                    
} else {
                        
$secret_key get_post_meta($button_id'stripe_live_secret_key'true);
                        ; 
//Use live API key
                    
}
                    
//Include the Stripe library.
                    
include(SIMPLE_WP_MEMBERSHIP_PATH 'lib/stripe-gateway/init.php');
                    \
Stripe\Stripe::setApiKey($secret_key);
                    
// Let's try to cancel subscription
                    
try {
                        
$sub = \Stripe\Subscription::retrieve($subscr_id);
                        
$sub->cancel();
                    } catch (
Exception $e) {
                        
SwpmLog::log_simple_debug("Error occurred during Stripe Subscription cancellation. " $e->getMessage(), false);
                        
$body $e->getJsonBody();
                        
$error $body['error'];
                        
$error_string print_r($errortrue);
                        
SwpmLog::log_simple_debug("Error details: " $error_stringfalse);
                    }
                    if (!isset(
$error)) {
                        
SwpmLog::log_simple_debug("Stripe Subscription has been cancelled."true);
                    }
                }
            }
        }

        
wp_clear_auth_cookie();
        
$this->logout();
        
SwpmMembers::delete_swpm_user_by_id($user_id);
        
SwpmMembers::delete_wp_user($user_name);
    }

    public function 
reload_user_data() {
        if (!
$this->is_logged_in()) {
            return;
        }
        global 
$wpdb;
        
$query "SELECT * FROM " $wpdb->prefix "swpm_members_tbl WHERE member_id = %d";
        
$this->userData $wpdb->get_row($wpdb->prepare($query$this->userData->member_id));
    }

    public function 
is_expired_account() {
        if (!
$this->is_logged_in()) {
            return 
null;
        }
        
$account_status $this->get('account_state');
        if (
$account_status == 'expired' || $account_status == 'inactive') {
            
//Expired or Inactive accounts are both considered to be expired.
            
return true;
        }
        return 
false;
    }

}