PluginProbe
Loginizer / 2.0.9
Loginizer v2.0.9
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.9, at init.php

1,002 lines 30.4 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.9');
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', 11, 2);
337 add_action('rsssl_two_factor_user_authenticated', 'loginizer_rsssl_2fa_success');
338
339 if(!empty($loginizer['ultimate-member-active'])){
340 add_action('wp_login_failed', 'loginizer_ultimatemember_error_handler', 10001);
341 }
342
343 if(!empty($_COOKIE['lz_social_error']) && !empty($loginizer['social_settings'])){
344 add_filter('wp_login_errors', 'loginizer_social_login_error_handler', 10000, 2);
345 }
346 }
347
348 // Social Login Form Actions
349 if(!empty($loginizer['social_settings'])){
350 if(!empty($loginizer['social_settings']['login']['login_form'])){
351 add_action('login_form', 'loginizer_social_btn_login');
352 }
353 }
354
355 if((function_exists('wp_doing_ajax') && wp_doing_ajax()) || (defined( 'DOING_AJAX' ) && DOING_AJAX)){
356 include_once LOGINIZER_DIR . '/main/ajax.php';
357 }
358
359 if(is_admin()){
360 include_once LOGINIZER_DIR . '/main/admin.php';
361 }
362
363 // ----------------
364 // PRO INIT END
365 // ----------------
366
367 // Secuity checks for social login.
368 if(!empty($_GET['lz_social_provider']) && loginizer_can_login() && empty($_GET['lz_api'])){
369 add_action('init', 'loginizer_social_login_load');
370 return;
371 }
372 }
373
374 // Should return NULL if everything is fine
375 function loginizer_wp_authenticate($user, $username, $password){
376
377 global $loginizer, $lz_error, $lz_cannot_login, $lz_user_pass;
378
379 if(!empty($username) && !empty($password)){
380 $lz_user_pass = 1;
381 }
382
383 // Are you whitelisted ?
384 if(loginizer_is_whitelisted()){
385 $loginizer['ip_is_whitelisted'] = 1;
386 return $user;
387
388 } else if (!empty($loginizer['trusted_ips'])){
389 $lz_cannot_login = 1;
390
391 // This is used by WP Activity Log
392 apply_filters( 'wp_login_blocked', $username );
393
394 // Shows a blocked screen
395 if(!empty($loginizer['blocked_screen'])){
396 $lz_error['trusted_ip'] = __('You are restricted from logging in as your IP is not whitelisted.', 'loginizer');
397 loginizer_blocked_page($lz_error);
398 }
399
400 return new WP_Error('ip_blacklisted', __('You are restricted from logging in as your IP is not whitelisted.', 'loginizer'));
401 }
402
403 // Are you blacklisted ?
404 if(loginizer_is_blacklisted()){
405 $lz_cannot_login = 1;
406
407 // This is used by WP Activity Log
408 apply_filters( 'wp_login_blocked', $username );
409
410 // Shows a blocked screen
411 if(!empty($loginizer['blocked_screen'])){
412 loginizer_blocked_page($lz_error);
413 }
414
415 return new WP_Error('ip_blacklisted', implode('', $lz_error), 'loginizer');
416 }
417
418 // Is the username blacklisted ?
419 if(function_exists('loginizer_user_blacklisted')){
420 if(loginizer_user_blacklisted($username)){
421 $lz_cannot_login = 1;
422
423 // This is used by WP Activity Log
424 apply_filters( 'wp_login_blocked', $username );
425
426 return new WP_Error('user_blacklisted', implode('', $lz_error), 'loginizer');
427 }
428 }
429
430 if(loginizer_can_login()){
431 return $user;
432 }
433
434 $lz_cannot_login = 1;
435
436 // This is used by WP Activity Log
437 apply_filters( 'wp_login_blocked', $username );
438
439 // Shows a blocked screen
440 if(!empty($loginizer['blocked_screen'])){
441 loginizer_blocked_page($lz_error);
442 }
443
444 return new WP_Error('ip_blocked', implode('', $lz_error), 'loginizer');
445
446 }
447
448 function loginizer_can_login(){
449
450 global $wpdb, $loginizer, $lz_error;
451
452 // Get the logs
453 $sel_query = $wpdb->prepare("SELECT * FROM `".$wpdb->prefix."loginizer_logs` WHERE `ip` = %s", $loginizer['current_ip']);
454 $result = lz_selectquery($sel_query);
455
456 if(!empty($result['count']) && ($result['count'] % $loginizer['max_retries']) == 0){
457
458 // Has he reached max lockouts ?
459 if($result['lockout'] >= $loginizer['max_lockouts']){
460 $loginizer['lockout_time'] = $loginizer['lockouts_extend'];
461 }
462
463 // Is he in the lockout time ?
464 if($result['time'] >= (time() - $loginizer['lockout_time'])){
465 $banlift = ceil((($result['time'] + $loginizer['lockout_time']) - time()) / 60);
466
467 //echo 'Current Time '.date('d/M/Y H:i:s P', time()).'<br />';
468 //echo 'Last attempt '.date('d/M/Y H:i:s P', $result['time']).'<br />';
469 //echo 'Unlock Time '.date('d/M/Y H:i:s P', $result['time'] + $loginizer['lockout_time']).'<br />';
470
471 $_time = $banlift.' '.$loginizer['msg']['minutes_err'];
472
473 if($banlift > 60){
474 $banlift = ceil($banlift / 60);
475 $_time = $banlift.' '.$loginizer['msg']['hours_err'];
476 }
477
478 $lz_error['ip_blocked'] = $loginizer['msg']['lockout_err'].' '.$_time;
479
480 if(!empty($loginizer['ultimate-member-active']) && class_exists('UM')){
481 \UM()->form()->add_error('blocked_msg', $lz_error['ip_blocked']);
482 }
483 return false;
484 }
485 }
486
487 return true;
488 }
489
490 function loginizer_is_blacklisted(){
491
492 global $wpdb, $loginizer, $lz_error;
493
494 $blacklist = isset($loginizer['blacklist']) ? $loginizer['blacklist'] : [];
495
496 if(empty($blacklist)){
497 return false;
498 }
499
500 $current_ip_inet = inet_ptoi($loginizer['current_ip']);
501
502 foreach($blacklist as $k => $v){
503
504 $start_inet = inet_ptoi($v['start']);
505 $end_inet = inet_ptoi($v['end']);
506
507 // Is the IP in the blacklist ?
508 if($start_inet <= $current_ip_inet && $current_ip_inet <= $end_inet){
509 $result = 1;
510 break;
511 }
512
513 // Is it in a wider range ?
514 if($start_inet >= 0 && $end_inet < 0){
515
516 // Since the end of the RANGE (i.e. current IP range) is beyond the +ve value of inet_ptoi,
517 // if the current IP is <= than the start of the range, it is within the range
518 // OR
519 // if the current IP is <= than the end of the range, it is within the range
520 if($start_inet <= $current_ip_inet
521 || $current_ip_inet <= $end_inet){
522 $result = 1;
523 break;
524 }
525
526 }
527
528 }
529
530 // You are blacklisted
531 if(!empty($result)){
532 $lz_error['ip_blacklisted'] = $loginizer['msg']['ip_blacklisted'];
533 return true;
534 }
535
536 return false;
537
538 }
539
540 // When the login fails, then this is called
541 // We need to update the database
542 function loginizer_login_failed($username, $is_2fa = ''){
543
544 global $wpdb, $loginizer, $lz_cannot_login;
545
546 // 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
547 if(empty($username) || is_null($username)){
548 $username = '';
549 }
550
551 $fail_type = 'Login';
552
553 if(!empty($is_2fa)){
554 $fail_type = '2FA';
555 }
556
557 if(empty($lz_cannot_login) && empty($loginizer['ip_is_whitelisted']) && empty($loginizer['no_loginizer_logs'])){
558
559 // The params which comes when social login returns an error, have some characters, which WordPress could not save.
560 $server_uri = $_SERVER['REQUEST_URI'];
561 if(!empty($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], 'lz_social_provider') !== FALSE){
562 $request_uri = explode('=', $_SERVER['REQUEST_URI']);
563 $server_uri = $request_uri[0];
564 }
565
566 $url = @addslashes((!empty($_SERVER['HTTPS']) ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$server_uri);
567 $url = esc_url($url);
568
569 $sel_query = $wpdb->prepare("SELECT * FROM `".$wpdb->prefix."loginizer_logs` WHERE `ip` = %s", $loginizer['current_ip']);
570 $result = lz_selectquery($sel_query);
571
572 if(!empty($result)){
573 $lockout = floor((($result['count']+1) / $loginizer['max_retries']));
574
575 $update_data = array('username' => $username,
576 'time' => time(),
577 'count' => $result['count']+1,
578 'lockout' => $lockout,
579 'url' => $url);
580
581 $where_data = array('ip' => $loginizer['current_ip']);
582
583 $format = array('%s','%d','%d','%d','%s');
584 $where_format = array('%s');
585
586 $wpdb->update($wpdb->prefix.'loginizer_logs', $update_data, $where_data, $format, $where_format);
587
588 // Do we need to email admin ?
589 if(!empty($loginizer['notify_email']) && $lockout >= $loginizer['notify_email']){
590
591 $lockout_time = $loginizer['lockout_time'];
592
593 if($lockout >= $loginizer['max_lockouts']){
594 // extended lockout is in hours so we have to convert to minute
595 $lockout_time = $loginizer['lockouts_extend'];
596 }
597
598 $sitename = lz_is_multisite() ? get_site_option('site_name') : get_option('blogname');
599 $mail = array();
600 $mail['to'] = $loginizer['notify_email_address'];
601 $mail['subject'] = 'Failed '.$fail_type.' Attempts from IP '.$loginizer['current_ip'].' ('.$sitename.')';
602 $mail['message'] = 'Hi,
603
604 '.($result['count']+1).' failed '.strtolower($fail_type).' attempts and '.$lockout.' lockout(s) from IP '.$loginizer['current_ip'].' on your site :
605 '.home_url().'
606
607 Last '.$fail_type.' Attempt : '.date('d/M/Y H:i:s P', time()).'
608 Last User Attempt : '.$username.'
609 IP has been blocked until : '.date('d/M/Y H:i:s P', time() + $lockout_time).'
610
611 Regards,
612 Loginizer';
613
614 @wp_mail($mail['to'], $mail['subject'], $mail['message']);
615 }
616 }else{
617 $result = array();
618 $result['count'] = 0;
619
620 $insert_data = array('username' => $username,
621 'time' => time(),
622 'count' => 1,
623 'ip' => $loginizer['current_ip'],
624 'lockout' => 0,
625 'url' => $url);
626
627 $format = array('%s','%d','%d','%s','%d','%s');
628
629 $wpdb->insert($wpdb->prefix.'loginizer_logs', $insert_data, $format);
630 }
631
632 // We need to add one as this is a failed attempt as well
633 $result['count'] = $result['count'] + 1;
634 loginizer_update_attempt_stats(0);
635 $loginizer['retries_left'] = ($loginizer['max_retries'] - ($result['count'] % $loginizer['max_retries']));
636 $loginizer['retries_left'] = $loginizer['retries_left'] == $loginizer['max_retries'] ? 0 : $loginizer['retries_left'];
637
638 }
639 }
640
641 function loginizer_rsssl_2fa_success($user){
642 loginizer_login_success('', $user);
643 }
644
645 function loginizer_login_success($user_login, $user) {
646 global $wp_version, $loginizer;
647
648 loginizer_update_attempt_stats(1);
649
650 if(empty($loginizer['login_mail'])){
651 return;
652 }
653
654 if(empty($loginizer['login_mail']['enable'])){
655 return;
656 }
657
658 if(!empty($loginizer['login_mail']['disable_whitelist'])){
659 // Check its whitelist ip
660 if(loginizer_is_whitelisted()){
661 return;
662 }
663 }
664
665 if(empty($user_login) && empty($user)){
666 error_log('Loginizer: No user information to send email');
667 return;
668 }
669
670 if(empty($user)){
671 $user = get_user_by('login', $user_login);
672 }
673
674 if(empty($user)){
675 error_log('Loginizer: Unable to get the user');
676 return;
677 }
678
679 if(empty($loginizer['login_mail']['roles']) || !is_array($loginizer['login_mail']['roles'])){
680 return;
681 }
682
683 // Check if the user role is enabled for email notification.
684 if(!array_intersect($user->roles, $loginizer['login_mail']['roles'])){
685 return;
686 }
687
688 // current_datetime & wp_timezone_string were introduced in WordPress 5.3
689 if(!empty($wp_version) && version_compare($wp_version, '5.3', '>') && function_exists('current_datetime')){
690 $time_zone = wp_timezone_string();
691
692 if(!empty($time_zone) && isset($time_zone[1]) && is_numeric($time_zone[1])){
693 $time_zone = 'UTC'.$time_zone;
694 }
695
696 // Setting up data variables.
697 $date = current_datetime()->format('Y-m-d H:i:s') .' '. $time_zone;
698 } else {
699 $date = date("Y-m-d H:i:s", time()) . ' ' . date_default_timezone_get();
700 }
701
702 $sitename = lz_is_multisite() ? get_site_option('site_name') : get_option('blogname');
703 $email = $user->data->user_email;
704
705 $vars = array(
706 'date' => $date,
707 'ip' => esc_html($loginizer['current_ip']),
708 'sitename' => $sitename,
709 'user_login' => $user_login
710 );
711
712 $message = lz_lang_vars_name($loginizer['login_mail_body'], $vars);
713 $subject = lz_lang_vars_name($loginizer['login_mail_subject'], $vars);
714
715 $headers = [];
716
717 // Do we need to send the email as HTML ?
718 if(!empty($loginizer['login_mail']['html_mail'])){
719 $headers[] = 'Content-Type: text/html; charset=UTF-8';
720
721 if(!empty($loginizer['login_mail']['body'])){
722 $message = html_entity_decode($message);
723 }else{
724 $message = preg_replace("/\<br\s*\/\>/i", "<br/>", $message);
725 $message = preg_replace('/(?<!<br\/>)\n/i', "<br/>\n", $message);
726 }
727 }
728
729 // Sending notification
730 if(empty(wp_mail($email, $subject, $message, $headers))){
731 error_log(__('There was a problem sending your email.', 'loginizer'));
732 return;
733 }
734 }
735
736 function loginizer_update_attempt_stats($type){
737
738 $stats = get_option('loginizer_login_attempt_stats', []);
739 $time = strtotime(date('Y-m-d H:00:00'));
740
741 if(empty($stats[$time][$type])){
742 $stats[$time][$type] = 0;
743 }
744
745 $stats[$time][$type] += 1;
746
747 update_option('loginizer_login_attempt_stats', $stats, false);
748 }
749
750 // Handles the error of the password not being there
751 function loginizer_error_handler($errors, $redirect_to){
752
753 global $wpdb, $loginizer, $lz_user_pass, $lz_cannot_login;
754
755 //echo 'loginizer_error_handler :';print_r($errors->errors);echo '<br>';
756 if(is_null($errors) || empty($errors)){
757 return true;
758 }
759
760 // Remove the empty password error
761 if(is_wp_error($errors)){
762
763 $codes = $errors->get_error_codes();
764
765 foreach($codes as $k => $v){
766 if($v == 'invalid_username' || $v == 'incorrect_password'){
767 $show_error = 1;
768 }
769 }
770
771 $errors->remove('invalid_username');
772 $errors->remove('incorrect_password');
773
774 // Add the error
775 if(!empty($lz_user_pass) && !empty($show_error) && empty($lz_cannot_login)){
776 $errors->add('invalid_userpass', '<b>ERROR:</b> ' . $loginizer['msg']['inv_userpass']);
777 }
778
779 // Add the number of retires left as well
780 if(count($errors->get_error_codes()) > 0 && isset($loginizer['retries_left'])){
781 $errors->add('retries_left', loginizer_retries_left());
782 }
783
784 }
785
786 return $errors;
787
788 }
789
790 // Handles the error of the password not being there
791 function loginizer_woocommerce_error_handler(){
792
793 global $wpdb, $loginizer, $lz_user_pass, $lz_cannot_login;
794
795 if(function_exists('wc_add_notice')){
796 wc_add_notice( loginizer_retries_left(), 'error' );
797 }
798 }
799
800 function loginizer_ultimatemember_error_handler(){
801
802 if(class_exists('UM')){
803 \UM()->form()->add_error('remaining_tries', loginizer_retries_left());
804 }
805 }
806
807 // Handles social login URL
808 function loginizer_social_login_error_handler($errors = '', $redirect_to = ''){
809 global $loginizer;
810
811 if(loginizer_is_blacklisted()){
812 return $errors;
813 }
814
815 loginizer_get_social_error();
816
817 if(empty($loginizer['social_errors'])){
818 return $errors;
819 }
820
821 if(is_null($errors) || empty($errors) || !is_wp_error($errors)){
822 $errors = new WP_Error();
823 }
824
825 foreach($loginizer['social_errors'] as $key => $text){
826 $errors->add($key, $text);
827 }
828
829 return $errors;
830 }
831
832 // Returns a string with the number of retries left
833 function loginizer_retries_left(){
834
835 global $wpdb, $loginizer, $lz_user_pass, $lz_cannot_login;
836
837 // If we are to show the number of retries left
838 if(isset($loginizer['retries_left'])){
839 $retries_left = apply_filters('loginizer_retries_left_num', $loginizer['retries_left']);
840
841 return '<b>'.esc_html($retries_left).'</b> '.$loginizer['msg']['attempts_left'];
842 }
843
844 }
845
846 function loginizer_reset_retries(){
847
848 global $wpdb, $loginizer;
849
850 $deltime = time() - $loginizer['reset_retries'];
851
852 $del_query = $wpdb->prepare("DELETE FROM `".$wpdb->prefix."loginizer_logs` WHERE `time` <= %d", $deltime);
853 $result = $wpdb->query($del_query);
854
855 update_option('loginizer_last_reset', time());
856
857 }
858
859 function loginizer_load_translation_vars(){
860 global $loginizer;
861
862 $loginizer['login_mail_default_sub'] = __('Login Successful at $sitename', 'loginizer');
863 $loginizer['login_mail_default_msg'] = __('Hello $user_login,
864
865 Your account was recently logged in from the IP : $ip
866 Time : $date
867 If it was not you who logged in then please report this to us immediately.
868
869 Regards,
870 $sitename','loginizer');
871
872 if(empty($loginizer['login_mail_subject'])){
873 $loginizer['login_mail_subject'] = $loginizer['login_mail_default_sub'];
874 }
875
876 if(empty($loginizer['login_mail_body'])){
877 $loginizer['login_mail_body'] = $loginizer['login_mail_default_msg'];
878 }
879
880 // Default messages
881 $loginizer['d_msg']['inv_userpass'] = __('Incorrect Username or Password', 'loginizer');
882 $loginizer['d_msg']['ip_blacklisted'] = __('Your IP has been blacklisted', 'loginizer');
883 $loginizer['d_msg']['attempts_left'] = __('attempt(s) left', 'loginizer');
884 $loginizer['d_msg']['lockout_err'] = __('You have exceeded maximum login retries<br /> Please try after', 'loginizer');
885 $loginizer['d_msg']['minutes_err'] = __('minute(s)', 'loginizer');
886 $loginizer['d_msg']['hours_err'] = __('hour(s)', 'loginizer');
887
888 // Message Strings
889 $loginizer['msg'] = get_option('loginizer_msg', []);
890
891 foreach($loginizer['d_msg'] as $lk => $lv){
892 if(empty($loginizer['msg'][$lk])){
893 $loginizer['msg'][$lk] = $loginizer['d_msg'][$lk];
894 }
895 }
896
897 $loginizer['2fa_d_msg']['otp_app'] = __('Please enter the OTP as seen in your App', 'loginizer');
898 $loginizer['2fa_d_msg']['otp_email'] = __('Please enter the OTP emailed to you', 'loginizer');
899 $loginizer['2fa_d_msg']['otp_field'] = __('One Time Password', 'loginizer');
900 $loginizer['2fa_d_msg']['otp_question'] = __('Please answer your security question', 'loginizer');
901 $loginizer['2fa_d_msg']['otp_answer'] = __('Your Answer', 'loginizer');
902
903 // Message Strings
904 $loginizer['2fa_msg'] = get_option('loginizer_2fa_msg', []);
905
906 foreach($loginizer['2fa_d_msg'] as $lk => $lv){
907 if(empty($loginizer['2fa_msg'][$lk])){
908 $loginizer['2fa_msg'][$lk] = $loginizer['2fa_d_msg'][$lk];
909 }
910 }
911
912 }
913
914 function loginizer_social_login_load(){
915 include_once LOGINIZER_DIR . '/main/social-login.php';
916 }
917
918 // Checks if softaculous is installed on the server.
919 function loginizer_check_softaculous(){
920
921 // Checking if we have Softaculous installed?
922 if(!preg_match('/^\/home(?:\d+)?\/.*\//U', ABSPATH, $matches)){
923 return false;
924 }
925
926 if(empty($matches) || empty($matches[0])){
927 return false;
928 }
929
930 $softaculous_path = $matches[0] . '.softaculous/installations.php';
931 if(!file_exists($softaculous_path)){
932 return false;
933 }
934
935 // Checking if users has changed the branding of Softaculous.
936 $universal_file = '';
937 // Plesk, ISPManager, ISPConfig, InterWorx, H-Sphere, CentOS Web Panel, Softaculous Remote and Softaculous Enterprise
938 if(file_exists('/usr/local/softaculous/enduser/universal.php')){
939 $universal_file = '/usr/local/softaculous/enduser/universal.php';
940 }else if(file_exists('/usr/local/cpanel/whostmgr/docroot/cgi/softaculous/enduser/universal.php')){
941 $universal_file = '/usr/local/cpanel/whostmgr/docroot/cgi/softaculous/enduser/universal.php';
942 }else if(file_exists('/usr/local/directadmin/plugins/softaculous/enduser/universal.php')){
943 $universal_file = '/usr/local/directadmin/plugins/softaculous/enduser/universal.php';
944 }else if(file_exists('/usr/local/vesta/softaculous/enduser/universal.php')){
945 $universal_file = '/usr/local/vesta/softaculous/enduser/universal.php';
946 }
947
948 if(empty($universal_file)){
949 return false;
950 }
951
952 $universal = file_get_contents($universal_file);
953
954 if(empty($universal)){
955 return false;
956 }
957
958 // Checking if Softaculous is being whitelabeled
959 if(preg_match('/\$globals\[["\']sn["\']\]\s.?=\s.?["\']Softaculous["\']/', $universal)){
960 update_option('loginizer_softwp_upgrade', time());
961 }
962
963 return false;
964 }
965
966 // Sorry to see you going
967 register_uninstall_hook(LOGINIZER_FILE, 'loginizer_deactivation');
968
969 function loginizer_deactivation(){
970
971 global $wpdb;
972
973 $sql = array();
974 $sql[] = "DROP TABLE ".$wpdb->prefix."loginizer_logs;";
975
976 foreach($sql as $sk => $sv){
977 $wpdb->query($sv);
978 }
979
980 delete_option('loginizer_version');
981 delete_option('loginizer_options');
982 delete_option('loginizer_last_reset');
983 delete_option('loginizer_whitelist');
984 delete_option('loginizer_blacklist');
985 delete_option('loginizer_msg');
986 delete_option('loginizer_2fa_msg');
987 delete_option('loginizer_2fa_email_template');
988 delete_option('loginizer_security');
989 delete_option('loginizer_wp_admin');
990 delete_option('loginizer_csrf_promo_time');
991 delete_option('loginizer_backuply_promo_time');
992 delete_option('loginizer_promo_time');
993 delete_option('loginizer_ins_time');
994 delete_option('loginizer_2fa_whitelist');
995 delete_option('loginizer_checksums_last_run');
996 delete_option('loginizer_checksums_diff');
997 delete_option('loginizer_ip_method');
998 delete_option('loginizer_2fa_custom_redirect');
999 delete_option('external_updates-loginizer-security');
1000 delete_option('loginizer_login_attempt_stats');
1001
1002 }