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 / init.php

init.php in Loginizer 2.0.1, at init.php

1,079 lines 32.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if(!function_exists('add_action')){
4 echo 'You are not allowed to access this page directly.';
5 exit;
6 }
7
8 define('LOGINIZER_VERSION', '2.0.1');
9 define('LOGINIZER_DIR', dirname(LOGINIZER_FILE));
10 define('LOGINIZER_URL', plugins_url('', LOGINIZER_FILE));
11 define('LOGINIZER_PRO_URL', 'https://loginizer.com/features#compare');
12 define('LOGINIZER_PRICING_URL', 'https://loginizer.com/pricing');
13 define('LOGINIZER_DOCS', 'https://loginizer.com/docs/');
14
15 include_once(LOGINIZER_DIR.'/functions.php');
16
17 // Ok so we are now ready to go
18 register_activation_hook(LOGINIZER_FILE, 'loginizer_activation');
19
20 // Is called when the ADMIN enables the plugin
21 function loginizer_activation(){
22
23 global $wpdb;
24
25 $sql = array();
26
27 $sql[] = "DROP TABLE IF EXISTS `".$wpdb->prefix."loginizer_logs`";
28
29 $sql[] = "CREATE TABLE `".$wpdb->prefix."loginizer_logs` (
30 `username` varchar(255) NOT NULL DEFAULT '',
31 `time` int(10) NOT NULL DEFAULT '0',
32 `count` int(10) NOT NULL DEFAULT '0',
33 `lockout` int(10) NOT NULL DEFAULT '0',
34 `ip` varchar(255) NOT NULL DEFAULT '',
35 `url` varchar(255) NOT NULL DEFAULT '',
36 UNIQUE KEY `ip` (`ip`)
37 ) DEFAULT CHARSET=utf8;";
38
39 foreach($sql as $sk => $sv){
40 $wpdb->query($sv);
41 }
42
43 add_option('loginizer_version', LOGINIZER_VERSION);
44 add_option('loginizer_options', array());
45 add_option('loginizer_last_reset', 0);
46 add_option('loginizer_whitelist', array());
47 add_option('loginizer_blacklist', array());
48 add_option('loginizer_2fa_whitelist', array());
49
50 // TODO:: REMOVE THIS AFTER MARCH 2025
51 $softwp_upgrade = get_option('loginizer_softwp_upgrade', 0);
52 if(!defined('SITEPAD') && empty($softwp_upgrade)){
53 loginizer_check_softaculous();
54 }
55 }
56
57 /**
58 * Updates the database structure for Loginizer
59 *
60 * If the plugin files are updated but database structure is not updated
61 * this function will update the database structure as per the plugin version
62 * NOTE: This does not update plugin files it just updates the database structure
63 */
64 function loginizer_update_check(){
65
66 global $wpdb;
67
68 $sql = array();
69 $current_version = get_option('loginizer_version');
70
71 // It must be the 1.0 pre stuff
72 if(empty($current_version)){
73 $current_version = get_option('lz_version');
74 }
75
76 $version = (int) str_replace('.', '', $current_version);
77
78 // No update required
79 if($current_version == LOGINIZER_VERSION){
80 return true;
81 }
82
83 // Is it first run ?
84 if(empty($current_version)){
85
86 // Reinstall
87 loginizer_activation();
88
89 // Trick the following if conditions to not run
90 $version = (int) str_replace('.', '', LOGINIZER_VERSION);
91
92 }
93
94 // Is it less than 1.0.1 ?
95 if($version < 101){
96
97 // TODO : GET the existing settings
98
99 // Get the existing settings
100 $lz_failed_logs = lz_selectquery("SELECT * FROM `".$wpdb->prefix."lz_failed_logs`;", 1);
101 $lz_options = lz_selectquery("SELECT * FROM `".$wpdb->prefix."lz_options`;", 1);
102 $lz_iprange = lz_selectquery("SELECT * FROM `".$wpdb->prefix."lz_iprange`;", 1);
103
104 // Delete the three tables
105 $sql = array();
106 $sql[] = "DROP TABLE IF EXISTS ".$wpdb->prefix."lz_failed_logs;";
107 $sql[] = "DROP TABLE IF EXISTS ".$wpdb->prefix."lz_options;";
108 $sql[] = "DROP TABLE IF EXISTS ".$wpdb->prefix."lz_iprange;";
109
110 foreach($sql as $sk => $sv){
111 $wpdb->query($sv);
112 }
113
114 // Delete option
115 delete_option('lz_version');
116
117 // Reinstall
118 loginizer_activation();
119
120 // TODO : Save the existing settings
121
122 // Update the existing failed logs to new table
123 if(is_array($lz_failed_logs)){
124 foreach($lz_failed_logs as $fk => $fv){
125 $insert_data = array('username' => $fv['username'],
126 'time' => $fv['time'],
127 'count' => $fv['count'],
128 'lockout' => $fv['lockout'],
129 'ip' => $fv['ip']);
130
131 $format = array('%s','%d','%d','%d','%s');
132
133 $wpdb->insert($wpdb->prefix.'loginizer_logs', $insert_data, $format);
134 }
135 }
136
137 // Update the existing options to new structure
138 if(is_array($lz_options)){
139 foreach($lz_options as $ok => $ov){
140
141 if($ov['option_name'] == 'lz_last_reset'){
142 update_option('loginizer_last_reset', $ov['option_value']);
143 continue;
144 }
145
146 $old_option[str_replace('lz_', '', $ov['option_name'])] = $ov['option_value'];
147 }
148 // Save the options
149 update_option('loginizer_options', $old_option);
150 }
151
152 // Update the existing iprange to new structure
153 if(is_array($lz_iprange)){
154
155 $old_blacklist = array();
156 $old_whitelist = array();
157 $bid = 1;
158 $wid = 1;
159 foreach($lz_iprange as $ik => $iv){
160
161 if(!empty($iv['blacklist'])){
162 $old_blacklist[$bid] = array();
163 $old_blacklist[$bid]['start'] = long2ip($iv['start']);
164 $old_blacklist[$bid]['end'] = long2ip($iv['end']);
165 $old_blacklist[$bid]['time'] = strtotime($iv['date']);
166 $bid = $bid + 1;
167 }
168
169 if(!empty($iv['whitelist'])){
170 $old_whitelist[$wid] = array();
171 $old_whitelist[$wid]['start'] = long2ip($iv['start']);
172 $old_whitelist[$wid]['end'] = long2ip($iv['end']);
173 $old_whitelist[$wid]['time'] = strtotime($iv['date']);
174 $wid = $wid + 1;
175 }
176 }
177
178 if(!empty($old_blacklist)) update_option('loginizer_blacklist', $old_blacklist);
179 if(!empty($old_whitelist)) update_option('loginizer_whitelist', $old_whitelist);
180 }
181
182 }
183
184 // Is it less than 1.3.9 ?
185 if($version < 139){
186
187 $wpdb->query("ALTER TABLE ".$wpdb->prefix."loginizer_logs ADD `url` VARCHAR(255) NOT NULL DEFAULT '' AFTER `ip`;");
188
189 }
190
191 // Setting alignment to left in social login ?
192 if($version < 201){
193 $social_settings = get_option('loginizer_social_settings', []);
194
195 if(!empty($social_settings)){
196 if(!empty($social_settings['login']) && (!empty($social_settings['login']['login_form']) || !empty($social_settings['login']['registration_form']))){
197 $social_settings['login']['button_alignment'] = 'left';
198 }
199
200 if(!empty($social_settings['woocommerce']) && (!empty($social_settings['woocommmerce']['login_form']) || !empty($social_settings['woocommerce']['registration_form']))){
201 $social_settings['woocommerce']['button_alignment'] = 'left';
202 }
203
204 if(!empty($social_settings['comment']) && !empty($social_settings['comment']['enable_buttons'])){
205 $social_settings['comment']['button_alignment'] = 'left';
206 }
207
208 update_option('loginizer_social_settings', $social_settings);
209 }
210 }
211
212 // Save the new Version
213 update_option('loginizer_version', LOGINIZER_VERSION);
214
215 // TODO:: REMOVE THIS AFTER MARCH 2025
216 $softwp_upgrade = get_option('loginizer_softwp_upgrade', 0);
217 if(!defined('SITEPAD') && empty($softwp_upgrade)){
218 loginizer_check_softaculous();
219 }
220
221 // In Sitepad Math Captcha is enabled by default
222 if(defined('SITEPAD') && get_option('loginizer_captcha') === false){
223 $option['captcha_no_google'] = 1;
224 add_option('loginizer_captcha', $option);
225 }
226
227 }
228
229 // Add the action to load the plugin
230 add_action('plugins_loaded', 'loginizer_load_plugin');
231
232 // The function that will be called when the plugin is loaded
233 function loginizer_load_plugin(){
234
235 global $loginizer;
236
237 // Check if the installed version is outdated
238 loginizer_update_check();
239
240 // Set the array
241 if(empty($loginizer)){
242 $loginizer = array();
243 }
244
245 $loginizer['prefix'] = !defined('SITEPAD') ? 'Loginizer ' : 'SitePad ';
246 $loginizer['app'] = !defined('SITEPAD') ? 'WordPress' : 'SitePad';
247 $loginizer['login_basename'] = !defined('SITEPAD') ? 'wp-login.php' : 'login.php';
248 $loginizer['wp-includes'] = !defined('SITEPAD') ? 'wp-includes' : 'site-inc';
249
250 // The IP Method to use
251 $loginizer['ip_method'] = get_option('loginizer_ip_method');
252 if($loginizer['ip_method'] == 3){
253 $loginizer['custom_ip_method'] = get_option('loginizer_custom_ip_method');
254 }
255
256 // Load settings
257 $options = get_option('loginizer_options');
258 $loginizer['max_retries'] = empty($options['max_retries']) ? 3 : $options['max_retries'];
259 $loginizer['lockout_time'] = empty($options['lockout_time']) ? 900 : $options['lockout_time']; // 15 minutes
260 $loginizer['max_lockouts'] = empty($options['max_lockouts']) ? 5 : $options['max_lockouts'];
261 $loginizer['lockouts_extend'] = empty($options['lockouts_extend']) ? 86400 : $options['lockouts_extend']; // 24 hours
262 $loginizer['reset_retries'] = empty($options['reset_retries']) ? 86400 : $options['reset_retries']; // 24 hours
263 $loginizer['notify_email'] = empty($options['notify_email']) ? 0 : $options['notify_email'];
264 $loginizer['notify_email_address'] = lz_is_multisite() ? get_site_option('admin_email') : get_option('admin_email');
265 $loginizer['trusted_ips'] = empty($options['trusted_ips']) ? false : true;
266 $loginizer['blocked_screen'] = empty($options['blocked_screen']) ? false : true;
267 $loginizer['social_settings'] = get_option('loginizer_social_settings', []);
268
269 if(!empty($options['notify_email_address'])){
270 $loginizer['notify_email_address'] = $options['notify_email_address'];
271 $loginizer['custom_notify_email'] = 1;
272 }
273
274 // Login Success Email Notification.
275 $loginizer['login_mail'] = get_option('loginizer_login_mail', []);
276 add_action('init', 'loginizer_load_translation_vars', 0);
277
278 $loginizer['login_mail_subject'] = empty($loginizer['login_mail']['subject']) ? '' : $loginizer['login_mail']['subject'];
279 $loginizer['login_mail_body'] = empty($loginizer['login_mail']['body']) ? '' : $loginizer['login_mail']['body'];
280
281 // Load the blacklist and whitelist
282 $loginizer['blacklist'] = get_option('loginizer_blacklist', []);
283 $loginizer['whitelist'] = get_option('loginizer_whitelist', []);
284 $loginizer['2fa_whitelist'] = get_option('loginizer_2fa_whitelist');
285
286 // It should not be false
287 if(empty($loginizer['2fa_whitelist'])){
288 $loginizer['2fa_whitelist'] = array();
289 }
290
291 // When was the database cleared last time
292 $loginizer['last_reset'] = get_option('loginizer_last_reset');
293
294 if(!isset($loginizer['ultimate-member-active'])){
295 $um_is_active = in_array('ultimate-member/ultimate-member.php', apply_filters('active_plugins', get_option('active_plugins', [])));
296
297 $loginizer['ultimate-member-active'] = !empty($um_is_active) ? true : false;
298 }
299
300 //print_r($loginizer);
301
302 // Clear retries
303 if((time() - $loginizer['last_reset']) >= $loginizer['reset_retries']){
304 loginizer_reset_retries();
305 }
306
307 $ins_time = get_option('loginizer_ins_time');
308 if(empty($ins_time)){
309 $ins_time = time();
310 update_option('loginizer_ins_time', $ins_time);
311 }
312 $loginizer['ins_time'] = $ins_time;
313
314 // Set the current IP
315 $loginizer['current_ip'] = lz_getip();
316
317 // Is Brute Force Disabled ?
318 $loginizer['disable_brute'] = get_option('loginizer_disable_brute');
319
320 // Filters and actions
321 if(empty($loginizer['disable_brute'])){
322
323 // Use this to verify before WP tries to login
324 // Is always called and is the first function to be called
325 //add_action('wp_authenticate', 'loginizer_wp_authenticate', 10, 2);// Not called by XML-RPC
326 add_filter('authenticate', 'loginizer_wp_authenticate', 10001, 3);// This one is called by xmlrpc as well as GUI
327
328 // Is called when a login attempt fails
329 // Hence Update our records that the login failed
330 add_action('wp_login_failed', 'loginizer_login_failed');
331
332 // Is called before displaying the error message so that we dont show that the username is wrong or the password
333 // Update Error message
334 add_action('wp_login_errors', 'loginizer_error_handler', 10001, 2);
335 add_action('woocommerce_login_failed', 'loginizer_woocommerce_error_handler', 10001);
336 add_action('wp_login', 'loginizer_login_success', 10, 2);
337
338 if(!empty($loginizer['ultimate-member-active'])){
339 add_action('wp_login_failed', 'loginizer_ultimatemember_error_handler', 10001);
340 }
341
342 if(!empty($_COOKIE['lz_social_error']) && !empty($loginizer['social_settings']) && !loginizer_is_blacklisted()){
343 add_filter('wp_login_errors', 'loginizer_social_login_error_handler', 10000, 2);
344 }
345 }
346
347 // Social Login Form Actions
348 if(!empty($loginizer['social_settings']) && !loginizer_is_blacklisted()){
349 if(!empty($loginizer['social_settings']['login']['login_form'])){
350 add_action('login_form', 'loginizer_social_btn_login');
351 }
352 }
353
354 if((function_exists('wp_doing_ajax') && wp_doing_ajax()) || (defined( 'DOING_AJAX' ) && DOING_AJAX)){
355 include_once LOGINIZER_DIR . '/main/ajax.php';
356 }
357
358 if(is_admin()){
359 include_once LOGINIZER_DIR . '/main/admin.php';
360 }
361
362 // ----------------
363 // PRO INIT END
364 // ----------------
365
366 // Is the premium features there ?
367 if(!defined('LOGINIZER_PREMIUM')){
368
369 if(current_user_can('activate_plugins')){
370 // The promo time
371 $loginizer['promo_time'] = get_option('loginizer_promo_time');
372 if(empty($loginizer['promo_time'])){
373 $loginizer['promo_time'] = time();
374 update_option('loginizer_promo_time', $loginizer['promo_time']);
375 }
376
377 // Are we to show the loginizer promo
378 if(!empty($loginizer['promo_time']) && $loginizer['promo_time'] > 0 && $loginizer['promo_time'] < (time() - (30*24*3600))){
379
380 add_action('admin_notices', 'loginizer_promo');
381
382 }
383
384 if(!empty($loginizer['csrf_promo']) && $loginizer['csrf_promo'] > 0 && $loginizer['csrf_promo'] < (time() - 86400)){
385
386 add_action('admin_notices', 'loginizer_csrf_promo');
387
388 }
389
390 // Are we to disable the promo
391 if(isset($_GET['loginizer_promo']) && (int)$_GET['loginizer_promo'] == 0){
392 update_option('loginizer_promo_time', (0 - time()) );
393 die('DONE');
394 }
395
396 $loginizer['backuply_promo'] = get_option('loginizer_backuply_promo_time');
397
398 if(empty($loginizer['backuply_promo'])){
399 $loginizer['backuply_promo'] = abs($loginizer['promo_time']);
400 update_option('loginizer_backuply_promo_time', $loginizer['backuply_promo']);
401 }
402
403 // Setting CSRF Promo time
404 $loginizer['csrf_promo'] = get_option('loginizer_csrf_promo_time');
405
406 if(empty($loginizer['csrf_promo'])){
407 $loginizer['csrf_promo'] = abs($loginizer['promo_time']);
408 update_option('loginizer_csrf_promo_time', $loginizer['csrf_promo']);
409 }
410 }
411 }
412
413 // Secuity checks for social login.
414 if(!empty($_GET['lz_social_provider']) && loginizer_can_login() && empty($_GET['lz_api'])){
415 add_action('init', 'loginizer_social_login_load');
416 return;
417 }
418 }
419
420 // Should return NULL if everything is fine
421 function loginizer_wp_authenticate($user, $username, $password){
422
423 global $loginizer, $lz_error, $lz_cannot_login, $lz_user_pass;
424
425 if(!empty($username) && !empty($password)){
426 $lz_user_pass = 1;
427 }
428
429 // Are you whitelisted ?
430 if(loginizer_is_whitelisted()){
431 $loginizer['ip_is_whitelisted'] = 1;
432 return $user;
433
434 } else if (!empty($loginizer['trusted_ips'])){
435 $lz_cannot_login = 1;
436
437 // This is used by WP Activity Log
438 apply_filters( 'wp_login_blocked', $username );
439
440 // Shows a blocked screen
441 if(!empty($loginizer['blocked_screen'])){
442 $lz_error['trusted_ip'] = __('You are restricted from logging in as your IP is not whitelisted.', 'loginizer');
443 loginizer_blocked_page($lz_error);
444 }
445
446 return new WP_Error('ip_blacklisted', __('You are restricted from logging in as your IP is not whitelisted.', 'loginizer'));
447 }
448
449 // Are you blacklisted ?
450 if(loginizer_is_blacklisted()){
451 $lz_cannot_login = 1;
452
453 // This is used by WP Activity Log
454 apply_filters( 'wp_login_blocked', $username );
455
456 // Shows a blocked screen
457 if(!empty($loginizer['blocked_screen'])){
458 loginizer_blocked_page($lz_error);
459 }
460
461 return new WP_Error('ip_blacklisted', implode('', $lz_error), 'loginizer');
462 }
463
464 // Is the username blacklisted ?
465 if(function_exists('loginizer_user_blacklisted')){
466 if(loginizer_user_blacklisted($username)){
467 $lz_cannot_login = 1;
468
469 // This is used by WP Activity Log
470 apply_filters( 'wp_login_blocked', $username );
471
472 return new WP_Error('user_blacklisted', implode('', $lz_error), 'loginizer');
473 }
474 }
475
476 if(loginizer_can_login()){
477 return $user;
478 }
479
480 $lz_cannot_login = 1;
481
482 // This is used by WP Activity Log
483 apply_filters( 'wp_login_blocked', $username );
484
485 // Shows a blocked screen
486 if(!empty($loginizer['blocked_screen'])){
487 loginizer_blocked_page($lz_error);
488 }
489
490 return new WP_Error('ip_blocked', implode('', $lz_error), 'loginizer');
491
492 }
493
494 function loginizer_can_login(){
495
496 global $wpdb, $loginizer, $lz_error;
497
498 // Get the logs
499 $sel_query = $wpdb->prepare("SELECT * FROM `".$wpdb->prefix."loginizer_logs` WHERE `ip` = %s", $loginizer['current_ip']);
500 $result = lz_selectquery($sel_query);
501
502 if(!empty($result['count']) && ($result['count'] % $loginizer['max_retries']) == 0){
503
504 // Has he reached max lockouts ?
505 if($result['lockout'] >= $loginizer['max_lockouts']){
506 $loginizer['lockout_time'] = $loginizer['lockouts_extend'];
507 }
508
509 // Is he in the lockout time ?
510 if($result['time'] >= (time() - $loginizer['lockout_time'])){
511 $banlift = ceil((($result['time'] + $loginizer['lockout_time']) - time()) / 60);
512
513 //echo 'Current Time '.date('d/M/Y H:i:s P', time()).'<br />';
514 //echo 'Last attempt '.date('d/M/Y H:i:s P', $result['time']).'<br />';
515 //echo 'Unlock Time '.date('d/M/Y H:i:s P', $result['time'] + $loginizer['lockout_time']).'<br />';
516
517 $_time = $banlift.' '.$loginizer['msg']['minutes_err'];
518
519 if($banlift > 60){
520 $banlift = ceil($banlift / 60);
521 $_time = $banlift.' '.$loginizer['msg']['hours_err'];
522 }
523
524 $lz_error['ip_blocked'] = $loginizer['msg']['lockout_err'].' '.$_time;
525
526 if(!empty($loginizer['ultimate-member-active']) && class_exists('UM')){
527 \UM()->form()->add_error('blocked_msg', $lz_error['ip_blocked']);
528 }
529 return false;
530 }
531 }
532
533 return true;
534 }
535
536 function loginizer_is_blacklisted(){
537
538 global $wpdb, $loginizer, $lz_error;
539
540 $blacklist = isset($loginizer['blacklist']) ? $loginizer['blacklist'] : [];
541
542 if(empty($blacklist)){
543 return false;
544 }
545
546 foreach($blacklist as $k => $v){
547
548 // Is the IP in the blacklist ?
549 if(inet_ptoi($v['start']) <= inet_ptoi($loginizer['current_ip']) && inet_ptoi($loginizer['current_ip']) <= inet_ptoi($v['end'])){
550 $result = 1;
551 break;
552 }
553
554 // Is it in a wider range ?
555 if(inet_ptoi($v['start']) >= 0 && inet_ptoi($v['end']) < 0){
556
557 // Since the end of the RANGE (i.e. current IP range) is beyond the +ve value of inet_ptoi,
558 // if the current IP is <= than the start of the range, it is within the range
559 // OR
560 // if the current IP is <= than the end of the range, it is within the range
561 if(inet_ptoi($v['start']) <= inet_ptoi($loginizer['current_ip'])
562 || inet_ptoi($loginizer['current_ip']) <= inet_ptoi($v['end'])){
563 $result = 1;
564 break;
565 }
566
567 }
568
569 }
570
571 // You are blacklisted
572 if(!empty($result)){
573 $lz_error['ip_blacklisted'] = $loginizer['msg']['ip_blacklisted'];
574 return true;
575 }
576
577 return false;
578
579 }
580
581 function loginizer_is_whitelisted(){
582
583 global $wpdb, $loginizer, $lz_error;
584
585 $whitelist = $loginizer['whitelist'];
586
587 if(empty($whitelist)){
588 return false;
589 }
590
591 foreach($whitelist as $k => $v){
592
593 // Is the IP in the blacklist ?
594 if(inet_ptoi($v['start']) <= inet_ptoi($loginizer['current_ip']) && inet_ptoi($loginizer['current_ip']) <= inet_ptoi($v['end'])){
595 $result = 1;
596 break;
597 }
598
599 // Is it in a wider range ?
600 if(inet_ptoi($v['start']) >= 0 && inet_ptoi($v['end']) < 0){
601
602 // Since the end of the RANGE (i.e. current IP range) is beyond the +ve value of inet_ptoi,
603 // if the current IP is <= than the start of the range, it is within the range
604 // OR
605 // if the current IP is <= than the end of the range, it is within the range
606 if(inet_ptoi($v['start']) <= inet_ptoi($loginizer['current_ip'])
607 || inet_ptoi($loginizer['current_ip']) <= inet_ptoi($v['end'])){
608 $result = 1;
609 break;
610 }
611
612 }
613
614 }
615
616 // You are whitelisted
617 if(!empty($result)){
618 return true;
619 }
620
621 return false;
622
623 }
624
625 // When the login fails, then this is called
626 // We need to update the database
627 function loginizer_login_failed($username, $is_2fa = ''){
628
629 global $wpdb, $loginizer, $lz_cannot_login;
630
631 // Some plugins are changing the value for username as null so we need to handle it before using it for the INSERT OR UPDATE query
632 if(empty($username) || is_null($username)){
633 $username = '';
634 }
635
636 $fail_type = 'Login';
637
638 if(!empty($is_2fa)){
639 $fail_type = '2FA';
640 }
641
642 if(empty($lz_cannot_login) && empty($loginizer['ip_is_whitelisted']) && empty($loginizer['no_loginizer_logs'])){
643
644 // The params which comes when social login returns an error, have some characters, which WordPress could not save.
645 $server_uri = $_SERVER['REQUEST_URI'];
646 if(!empty($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], 'lz_social_provider') !== FALSE){
647 $request_uri = explode('=', $_SERVER['REQUEST_URI']);
648 $server_uri = $request_uri[0];
649 }
650
651 $url = @addslashes((!empty($_SERVER['HTTPS']) ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$server_uri);
652 $url = esc_url($url);
653
654 $sel_query = $wpdb->prepare("SELECT * FROM `".$wpdb->prefix."loginizer_logs` WHERE `ip` = %s", $loginizer['current_ip']);
655 $result = lz_selectquery($sel_query);
656
657 if(!empty($result)){
658 $lockout = floor((($result['count']+1) / $loginizer['max_retries']));
659
660 $update_data = array('username' => $username,
661 'time' => time(),
662 'count' => $result['count']+1,
663 'lockout' => $lockout,
664 'url' => $url);
665
666 $where_data = array('ip' => $loginizer['current_ip']);
667
668 $format = array('%s','%d','%d','%d','%s');
669 $where_format = array('%s');
670
671 $wpdb->update($wpdb->prefix.'loginizer_logs', $update_data, $where_data, $format, $where_format);
672
673 // Do we need to email admin ?
674 if(!empty($loginizer['notify_email']) && $lockout >= $loginizer['notify_email']){
675
676 $lockout_time = $loginizer['lockout_time'];
677
678 if($lockout >= $loginizer['max_lockouts']){
679 // extended lockout is in hours so we have to convert to minute
680 $lockout_time = $loginizer['lockouts_extend'];
681 }
682
683 $sitename = lz_is_multisite() ? get_site_option('site_name') : get_option('blogname');
684 $mail = array();
685 $mail['to'] = $loginizer['notify_email_address'];
686 $mail['subject'] = 'Failed '.$fail_type.' Attempts from IP '.$loginizer['current_ip'].' ('.$sitename.')';
687 $mail['message'] = 'Hi,
688
689 '.($result['count']+1).' failed '.strtolower($fail_type).' attempts and '.$lockout.' lockout(s) from IP '.$loginizer['current_ip'].' on your site :
690 '.home_url().'
691
692 Last '.$fail_type.' Attempt : '.date('d/M/Y H:i:s P', time()).'
693 Last User Attempt : '.$username.'
694 IP has been blocked until : '.date('d/M/Y H:i:s P', time() + $lockout_time).'
695
696 Regards,
697 Loginizer';
698
699 @wp_mail($mail['to'], $mail['subject'], $mail['message']);
700 }
701 }else{
702 $result = array();
703 $result['count'] = 0;
704
705 $insert_data = array('username' => $username,
706 'time' => time(),
707 'count' => 1,
708 'ip' => $loginizer['current_ip'],
709 'lockout' => 0,
710 'url' => $url);
711
712 $format = array('%s','%d','%d','%s','%d','%s');
713
714 $wpdb->insert($wpdb->prefix.'loginizer_logs', $insert_data, $format);
715 }
716
717 // We need to add one as this is a failed attempt as well
718 $result['count'] = $result['count'] + 1;
719 loginizer_update_attempt_stats(0);
720 $loginizer['retries_left'] = ($loginizer['max_retries'] - ($result['count'] % $loginizer['max_retries']));
721 $loginizer['retries_left'] = $loginizer['retries_left'] == $loginizer['max_retries'] ? 0 : $loginizer['retries_left'];
722
723 }
724 }
725
726 function loginizer_login_success($user_login, $user) {
727 global $wp_version, $loginizer;
728
729 loginizer_update_attempt_stats(1);
730
731 if(empty($loginizer['login_mail'])){
732 return;
733 }
734
735 if(empty($loginizer['login_mail']['enable'])){
736 return;
737 }
738
739 if(!empty($loginizer['login_mail']['disable_whitelist'])){
740 // Check its whitelist ip
741 if(loginizer_is_whitelisted()){
742 return;
743 }
744 }
745
746 if(empty($user_login) && empty($user)){
747 error_log('Loginizer: No user information to send email');
748 return;
749 }
750
751 if(empty($user)){
752 $user = get_user_by('login', $user_login);
753 }
754
755 if(empty($user)){
756 error_log('Loginizer: Unable to get the user');
757 return;
758 }
759
760 if(empty($loginizer['login_mail']['roles']) || !is_array($loginizer['login_mail']['roles'])){
761 return;
762 }
763
764 // Check if the user role is enabled for email notification.
765 if(!array_intersect($user->roles, $loginizer['login_mail']['roles'])){
766 return;
767 }
768
769 // current_datetime & wp_timezone_string were introduced in WordPress 5.3
770 if(!empty($wp_version) && version_compare($wp_version, '5.3', '>') && function_exists('current_datetime')){
771 $time_zone = wp_timezone_string();
772
773 if(!empty($time_zone) && isset($time_zone[1]) && is_numeric($time_zone[1])){
774 $time_zone = 'UTC'.$time_zone;
775 }
776
777 // Setting up data variables.
778 $date = current_datetime()->format('Y-m-d H:i:s') .' '. $time_zone;
779 } else {
780 $date = date("Y-m-d H:i:s", time()) . ' ' . date_default_timezone_get();
781 }
782
783 $sitename = lz_is_multisite() ? get_site_option('site_name') : get_option('blogname');
784 $email = $user->data->user_email;
785
786 $vars = array(
787 'date' => $date,
788 'ip' => esc_html($loginizer['current_ip']),
789 'sitename' => $sitename,
790 'user_login' => $user_login
791 );
792
793 $message = lz_lang_vars_name($loginizer['login_mail_body'], $vars);
794 $subject = lz_lang_vars_name($loginizer['login_mail_subject'], $vars);
795
796 $headers = [];
797
798 // Do we need to send the email as HTML ?
799 if(!empty($loginizer['login_mail']['html_mail'])){
800 $headers[] = 'Content-Type: text/html; charset=UTF-8';
801
802 if(!empty($loginizer['login_mail']['body'])){
803 $message = html_entity_decode($message);
804 }else{
805 $message = preg_replace("/\<br\s*\/\>/i", "<br/>", $message);
806 $message = preg_replace('/(?<!<br\/>)\n/i', "<br/>\n", $message);
807 }
808 }
809
810 // Sending notification
811 if(empty(wp_mail($email, $subject, $message, $headers))){
812 error_log(__('There was a problem sending your email.', 'loginizer'));
813 return;
814 }
815 }
816
817 function loginizer_update_attempt_stats($type){
818
819 $stats = get_option('loginizer_login_attempt_stats', []);
820 $time = strtotime(date('Y-m-d H:00:00'));
821
822 if(empty($stats[$time][$type])){
823 $stats[$time][$type] = 0;
824 }
825
826 $stats[$time][$type] += 1;
827
828 update_option('loginizer_login_attempt_stats', $stats, false);
829 }
830
831 // Handles the error of the password not being there
832 function loginizer_error_handler($errors, $redirect_to){
833
834 global $wpdb, $loginizer, $lz_user_pass, $lz_cannot_login;
835
836 //echo 'loginizer_error_handler :';print_r($errors->errors);echo '<br>';
837 if(is_null($errors) || empty($errors)){
838 return true;
839 }
840
841 // Remove the empty password error
842 if(is_wp_error($errors)){
843
844 $codes = $errors->get_error_codes();
845
846 foreach($codes as $k => $v){
847 if($v == 'invalid_username' || $v == 'incorrect_password'){
848 $show_error = 1;
849 }
850 }
851
852 $errors->remove('invalid_username');
853 $errors->remove('incorrect_password');
854
855 // Add the error
856 if(!empty($lz_user_pass) && !empty($show_error) && empty($lz_cannot_login)){
857 $errors->add('invalid_userpass', '<b>ERROR:</b> ' . $loginizer['msg']['inv_userpass']);
858 }
859
860 // Add the number of retires left as well
861 if(count($errors->get_error_codes()) > 0 && isset($loginizer['retries_left'])){
862 $errors->add('retries_left', loginizer_retries_left());
863 }
864
865 }
866
867 return $errors;
868
869 }
870
871 // Handles the error of the password not being there
872 function loginizer_woocommerce_error_handler(){
873
874 global $wpdb, $loginizer, $lz_user_pass, $lz_cannot_login;
875
876 if(function_exists('wc_add_notice')){
877 wc_add_notice( loginizer_retries_left(), 'error' );
878 }
879 }
880
881 function loginizer_ultimatemember_error_handler(){
882
883 if(class_exists('UM')){
884 \UM()->form()->add_error('remaining_tries', loginizer_retries_left());
885 }
886 }
887
888 // Handles social login URL
889 function loginizer_social_login_error_handler($errors = '', $redirect_to = ''){
890 global $loginizer;
891
892 loginizer_get_social_error();
893
894 if(empty($loginizer['social_errors'])){
895 return $errors;
896 }
897
898 if(is_null($errors) || empty($errors) || !is_wp_error($errors)){
899 $errors = new WP_Error();
900 }
901
902 foreach($loginizer['social_errors'] as $key => $text){
903 $errors->add($key, $text);
904 }
905
906 return $errors;
907 }
908
909 // Returns a string with the number of retries left
910 function loginizer_retries_left(){
911
912 global $wpdb, $loginizer, $lz_user_pass, $lz_cannot_login;
913
914 // If we are to show the number of retries left
915 if(isset($loginizer['retries_left'])){
916 $retries_left = apply_filters('loginizer_retries_left_num', $loginizer['retries_left']);
917
918 return '<b>'.esc_html($retries_left).'</b> '.$loginizer['msg']['attempts_left'];
919 }
920
921 }
922
923 function loginizer_reset_retries(){
924
925 global $wpdb, $loginizer;
926
927 $deltime = time() - $loginizer['reset_retries'];
928
929 $del_query = $wpdb->prepare("DELETE FROM `".$wpdb->prefix."loginizer_logs` WHERE `time` <= %d", $deltime);
930 $result = $wpdb->query($del_query);
931
932 update_option('loginizer_last_reset', time());
933
934 }
935
936 function loginizer_load_translation_vars(){
937 global $loginizer;
938
939 $loginizer['login_mail_default_sub'] = __('Login Successful at $sitename', 'loginizer');
940 $loginizer['login_mail_default_msg'] = __('Hello $user_login,
941
942 Your account was recently logged in from the IP : $ip
943 Time : $date
944 If it was not you who logged in then please report this to us immediately.
945
946 Regards,
947 $sitename','loginizer');
948
949 if(empty($loginizer['login_mail_subject'])){
950 $loginizer['login_mail_subject'] = $loginizer['login_mail_default_sub'];
951 }
952
953 if(empty($loginizer['login_mail_body'])){
954 $loginizer['login_mail_body'] = $loginizer['login_mail_default_msg'];
955 }
956
957 // Default messages
958 $loginizer['d_msg']['inv_userpass'] = __('Incorrect Username or Password', 'loginizer');
959 $loginizer['d_msg']['ip_blacklisted'] = __('Your IP has been blacklisted', 'loginizer');
960 $loginizer['d_msg']['attempts_left'] = __('attempt(s) left', 'loginizer');
961 $loginizer['d_msg']['lockout_err'] = __('You have exceeded maximum login retries<br /> Please try after', 'loginizer');
962 $loginizer['d_msg']['minutes_err'] = __('minute(s)', 'loginizer');
963 $loginizer['d_msg']['hours_err'] = __('hour(s)', 'loginizer');
964
965 // Message Strings
966 $loginizer['msg'] = get_option('loginizer_msg', []);
967
968 foreach($loginizer['d_msg'] as $lk => $lv){
969 if(empty($loginizer['msg'][$lk])){
970 $loginizer['msg'][$lk] = $loginizer['d_msg'][$lk];
971 }
972 }
973
974 $loginizer['2fa_d_msg']['otp_app'] = __('Please enter the OTP as seen in your App', 'loginizer');
975 $loginizer['2fa_d_msg']['otp_email'] = __('Please enter the OTP emailed to you', 'loginizer');
976 $loginizer['2fa_d_msg']['otp_field'] = __('One Time Password', 'loginizer');
977 $loginizer['2fa_d_msg']['otp_question'] = __('Please answer your security question', 'loginizer');
978 $loginizer['2fa_d_msg']['otp_answer'] = __('Your Answer', 'loginizer');
979
980 // Message Strings
981 $loginizer['2fa_msg'] = get_option('loginizer_2fa_msg', []);
982
983 foreach($loginizer['2fa_d_msg'] as $lk => $lv){
984 if(empty($loginizer['2fa_msg'][$lk])){
985 $loginizer['2fa_msg'][$lk] = $loginizer['2fa_d_msg'][$lk];
986 }
987 }
988
989 }
990
991 function loginizer_social_login_load(){
992 include_once LOGINIZER_DIR . '/main/social-login.php';
993 }
994
995 // Checks if softaculous is installed on the server.
996 function loginizer_check_softaculous(){
997
998 // Checking if we have Softaculous installed?
999 if(!preg_match('/^\/home(?:\d+)?\/.*\//U', ABSPATH, $matches)){
1000 return false;
1001 }
1002
1003 if(empty($matches) || empty($matches[0])){
1004 return false;
1005 }
1006
1007 $softaculous_path = $matches[0] . '.softaculous/installations.php';
1008 if(!file_exists($softaculous_path)){
1009 return false;
1010 }
1011
1012 // Checking if users has changed the branding of Softaculous.
1013 $universal_file = '';
1014 // Plesk, ISPManager, ISPConfig, InterWorx, H-Sphere, CentOS Web Panel, Softaculous Remote and Softaculous Enterprise
1015 if(file_exists('/usr/local/softaculous/enduser/universal.php')){
1016 $universal_file = '/usr/local/softaculous/enduser/universal.php';
1017 }else if(file_exists('/usr/local/cpanel/whostmgr/docroot/cgi/softaculous/enduser/universal.php')){
1018 $universal_file = '/usr/local/cpanel/whostmgr/docroot/cgi/softaculous/enduser/universal.php';
1019 }else if(file_exists('/usr/local/directadmin/plugins/softaculous/enduser/universal.php')){
1020 $universal_file = '/usr/local/directadmin/plugins/softaculous/enduser/universal.php';
1021 }else if(file_exists('/usr/local/vesta/softaculous/enduser/universal.php')){
1022 $universal_file = '/usr/local/vesta/softaculous/enduser/universal.php';
1023 }
1024
1025 if(empty($universal_file)){
1026 return false;
1027 }
1028
1029 $universal = file_get_contents($universal_file);
1030
1031 if(empty($universal)){
1032 return false;
1033 }
1034
1035 // Checking if Softaculous is being whitelabeled
1036 if(preg_match('/\$globals\[["\']sn["\']\]\s.?=\s.?["\']Softaculous["\']/', $universal)){
1037 update_option('loginizer_softwp_upgrade', time());
1038 }
1039
1040 return false;
1041 }
1042
1043 // Sorry to see you going
1044 register_uninstall_hook(LOGINIZER_FILE, 'loginizer_deactivation');
1045
1046 function loginizer_deactivation(){
1047
1048 global $wpdb;
1049
1050 $sql = array();
1051 $sql[] = "DROP TABLE ".$wpdb->prefix."loginizer_logs;";
1052
1053 foreach($sql as $sk => $sv){
1054 $wpdb->query($sv);
1055 }
1056
1057 delete_option('loginizer_version');
1058 delete_option('loginizer_options');
1059 delete_option('loginizer_last_reset');
1060 delete_option('loginizer_whitelist');
1061 delete_option('loginizer_blacklist');
1062 delete_option('loginizer_msg');
1063 delete_option('loginizer_2fa_msg');
1064 delete_option('loginizer_2fa_email_template');
1065 delete_option('loginizer_security');
1066 delete_option('loginizer_wp_admin');
1067 delete_option('loginizer_csrf_promo_time');
1068 delete_option('loginizer_backuply_promo_time');
1069 delete_option('loginizer_promo_time');
1070 delete_option('loginizer_ins_time');
1071 delete_option('loginizer_2fa_whitelist');
1072 delete_option('loginizer_checksums_last_run');
1073 delete_option('loginizer_checksums_diff');
1074 delete_option('loginizer_ip_method');
1075 delete_option('loginizer_2fa_custom_redirect');
1076 delete_option('external_updates-loginizer-security');
1077 delete_option('loginizer_login_attempt_stats');
1078
1079 }