PluginProbe
Loginizer / 1.8.2
Loginizer v1.8.2
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 1.8.2, at init.php

929 lines 31.2 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', '1.8.2');
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 }
51
52 /**
53 * Updates the database structure for Loginizer
54 *
55 * If the plugin files are updated but database structure is not updated
56 * this function will update the database structure as per the plugin version
57 * NOTE: This does not update plugin files it just updates the database structure
58 */
59 function loginizer_update_check(){
60
61 global $wpdb;
62
63 $sql = array();
64 $current_version = get_option('loginizer_version');
65
66 // It must be the 1.0 pre stuff
67 if(empty($current_version)){
68 $current_version = get_option('lz_version');
69 }
70
71 $version = (int) str_replace('.', '', $current_version);
72
73 // No update required
74 if($current_version == LOGINIZER_VERSION){
75 return true;
76 }
77
78 // Is it first run ?
79 if(empty($current_version)){
80
81 // Reinstall
82 loginizer_activation();
83
84 // Trick the following if conditions to not run
85 $version = (int) str_replace('.', '', LOGINIZER_VERSION);
86
87 }
88
89 // Is it less than 1.0.1 ?
90 if($version < 101){
91
92 // TODO : GET the existing settings
93
94 // Get the existing settings
95 $lz_failed_logs = lz_selectquery("SELECT * FROM `".$wpdb->prefix."lz_failed_logs`;", 1);
96 $lz_options = lz_selectquery("SELECT * FROM `".$wpdb->prefix."lz_options`;", 1);
97 $lz_iprange = lz_selectquery("SELECT * FROM `".$wpdb->prefix."lz_iprange`;", 1);
98
99 // Delete the three tables
100 $sql = array();
101 $sql[] = "DROP TABLE IF EXISTS ".$wpdb->prefix."lz_failed_logs;";
102 $sql[] = "DROP TABLE IF EXISTS ".$wpdb->prefix."lz_options;";
103 $sql[] = "DROP TABLE IF EXISTS ".$wpdb->prefix."lz_iprange;";
104
105 foreach($sql as $sk => $sv){
106 $wpdb->query($sv);
107 }
108
109 // Delete option
110 delete_option('lz_version');
111
112 // Reinstall
113 loginizer_activation();
114
115 // TODO : Save the existing settings
116
117 // Update the existing failed logs to new table
118 if(is_array($lz_failed_logs)){
119 foreach($lz_failed_logs as $fk => $fv){
120 $insert_data = array('username' => $fv['username'],
121 'time' => $fv['time'],
122 'count' => $fv['count'],
123 'lockout' => $fv['lockout'],
124 'ip' => $fv['ip']);
125
126 $format = array('%s','%d','%d','%d','%s');
127
128 $wpdb->insert($wpdb->prefix.'loginizer_logs', $insert_data, $format);
129 }
130 }
131
132 // Update the existing options to new structure
133 if(is_array($lz_options)){
134 foreach($lz_options as $ok => $ov){
135
136 if($ov['option_name'] == 'lz_last_reset'){
137 update_option('loginizer_last_reset', $ov['option_value']);
138 continue;
139 }
140
141 $old_option[str_replace('lz_', '', $ov['option_name'])] = $ov['option_value'];
142 }
143 // Save the options
144 update_option('loginizer_options', $old_option);
145 }
146
147 // Update the existing iprange to new structure
148 if(is_array($lz_iprange)){
149
150 $old_blacklist = array();
151 $old_whitelist = array();
152 $bid = 1;
153 $wid = 1;
154 foreach($lz_iprange as $ik => $iv){
155
156 if(!empty($iv['blacklist'])){
157 $old_blacklist[$bid] = array();
158 $old_blacklist[$bid]['start'] = long2ip($iv['start']);
159 $old_blacklist[$bid]['end'] = long2ip($iv['end']);
160 $old_blacklist[$bid]['time'] = strtotime($iv['date']);
161 $bid = $bid + 1;
162 }
163
164 if(!empty($iv['whitelist'])){
165 $old_whitelist[$wid] = array();
166 $old_whitelist[$wid]['start'] = long2ip($iv['start']);
167 $old_whitelist[$wid]['end'] = long2ip($iv['end']);
168 $old_whitelist[$wid]['time'] = strtotime($iv['date']);
169 $wid = $wid + 1;
170 }
171 }
172
173 if(!empty($old_blacklist)) update_option('loginizer_blacklist', $old_blacklist);
174 if(!empty($old_whitelist)) update_option('loginizer_whitelist', $old_whitelist);
175 }
176
177 }
178
179 // Is it less than 1.3.9 ?
180 if($version < 139){
181
182 $wpdb->query("ALTER TABLE ".$wpdb->prefix."loginizer_logs ADD `url` VARCHAR(255) NOT NULL DEFAULT '' AFTER `ip`;");
183
184 }
185
186 // Save the new Version
187 update_option('loginizer_version', LOGINIZER_VERSION);
188
189 // In Sitepad Math Captcha is enabled by default
190 if(defined('SITEPAD') && get_option('loginizer_captcha') === false){
191 $option['captcha_no_google'] = 1;
192 add_option('loginizer_captcha', $option);
193 }
194
195 }
196
197 // Add the action to load the plugin
198 add_action('plugins_loaded', 'loginizer_load_plugin');
199
200 // The function that will be called when the plugin is loaded
201 function loginizer_load_plugin(){
202
203 global $loginizer;
204
205 // Check if the installed version is outdated
206 loginizer_update_check();
207
208 // Set the array
209 $loginizer = array();
210
211 $loginizer['prefix'] = !defined('SITEPAD') ? 'Loginizer ' : 'SitePad ';
212 $loginizer['app'] = !defined('SITEPAD') ? 'WordPress' : 'SitePad';
213 $loginizer['login_basename'] = !defined('SITEPAD') ? 'wp-login.php' : 'login.php';
214 $loginizer['wp-includes'] = !defined('SITEPAD') ? 'wp-includes' : 'site-inc';
215
216 // The IP Method to use
217 $loginizer['ip_method'] = get_option('loginizer_ip_method');
218 if($loginizer['ip_method'] == 3){
219 $loginizer['custom_ip_method'] = get_option('loginizer_custom_ip_method');
220 }
221
222 // Load settings
223 $options = get_option('loginizer_options');
224 $loginizer['max_retries'] = empty($options['max_retries']) ? 3 : $options['max_retries'];
225 $loginizer['lockout_time'] = empty($options['lockout_time']) ? 900 : $options['lockout_time']; // 15 minutes
226 $loginizer['max_lockouts'] = empty($options['max_lockouts']) ? 5 : $options['max_lockouts'];
227 $loginizer['lockouts_extend'] = empty($options['lockouts_extend']) ? 86400 : $options['lockouts_extend']; // 24 hours
228 $loginizer['reset_retries'] = empty($options['reset_retries']) ? 86400 : $options['reset_retries']; // 24 hours
229 $loginizer['notify_email'] = empty($options['notify_email']) ? 0 : $options['notify_email'];
230 $loginizer['notify_email_address'] = lz_is_multisite() ? get_site_option('admin_email') : get_option('admin_email');
231 $loginizer['trusted_ips'] = empty($options['trusted_ips']) ? false : true;
232
233 if(!empty($options['notify_email_address'])){
234 $loginizer['notify_email_address'] = $options['notify_email_address'];
235 $loginizer['custom_notify_email'] = 1;
236 }
237
238 // Default messages
239 $loginizer['d_msg']['inv_userpass'] = __('Incorrect Username or Password', 'loginizer');
240 $loginizer['d_msg']['ip_blacklisted'] = __('Your IP has been blacklisted', 'loginizer');
241 $loginizer['d_msg']['attempts_left'] = __('attempt(s) left', 'loginizer');
242 $loginizer['d_msg']['lockout_err'] = __('You have exceeded maximum login retries<br /> Please try after', 'loginizer');
243 $loginizer['d_msg']['minutes_err'] = __('minute(s)', 'loginizer');
244 $loginizer['d_msg']['hours_err'] = __('hour(s)', 'loginizer');
245
246 // Message Strings
247 $loginizer['msg'] = get_option('loginizer_msg', []);
248
249 foreach($loginizer['d_msg'] as $lk => $lv){
250 if(empty($loginizer['msg'][$lk])){
251 $loginizer['msg'][$lk] = $loginizer['d_msg'][$lk];
252 }
253 }
254
255 $loginizer['2fa_d_msg']['otp_app'] = __('Please enter the OTP as seen in your App', 'loginizer');
256 $loginizer['2fa_d_msg']['otp_email'] = __('Please enter the OTP emailed to you', 'loginizer');
257 $loginizer['2fa_d_msg']['otp_field'] = __('One Time Password', 'loginizer');
258 $loginizer['2fa_d_msg']['otp_question'] = __('Please answer your security question', 'loginizer');
259 $loginizer['2fa_d_msg']['otp_answer'] = __('Your Answer', 'loginizer');
260
261 // Message Strings
262 $loginizer['2fa_msg'] = get_option('loginizer_2fa_msg', []);
263
264 foreach($loginizer['2fa_d_msg'] as $lk => $lv){
265 if(empty($loginizer['2fa_msg'][$lk])){
266 $loginizer['2fa_msg'][$lk] = $loginizer['2fa_d_msg'][$lk];
267 }
268 }
269
270 // Load the blacklist and whitelist
271 $loginizer['blacklist'] = get_option('loginizer_blacklist');
272 $loginizer['whitelist'] = get_option('loginizer_whitelist');
273 $loginizer['2fa_whitelist'] = get_option('loginizer_2fa_whitelist');
274
275 // It should not be false
276 if(empty($loginizer['2fa_whitelist'])){
277 $loginizer['2fa_whitelist'] = array();
278 }
279
280 // When was the database cleared last time
281 $loginizer['last_reset'] = get_option('loginizer_last_reset');
282
283 //print_r($loginizer);
284
285 // Clear retries
286 if((time() - $loginizer['last_reset']) >= $loginizer['reset_retries']){
287 loginizer_reset_retries();
288 }
289
290 $ins_time = get_option('loginizer_ins_time');
291 if(empty($ins_time)){
292 $ins_time = time();
293 update_option('loginizer_ins_time', $ins_time);
294 }
295 $loginizer['ins_time'] = $ins_time;
296
297 // Set the current IP
298 $loginizer['current_ip'] = lz_getip();
299
300 // Is Brute Force Disabled ?
301 $loginizer['disable_brute'] = get_option('loginizer_disable_brute');
302
303 // Filters and actions
304 if(empty($loginizer['disable_brute'])){
305
306 // Use this to verify before WP tries to login
307 // Is always called and is the first function to be called
308 //add_action('wp_authenticate', 'loginizer_wp_authenticate', 10, 2);// Not called by XML-RPC
309 add_filter('authenticate', 'loginizer_wp_authenticate', 10001, 3);// This one is called by xmlrpc as well as GUI
310
311 // Is called when a login attempt fails
312 // Hence Update our records that the login failed
313 add_action('wp_login_failed', 'loginizer_login_failed');
314
315 // Is called before displaying the error message so that we dont show that the username is wrong or the password
316 // Update Error message
317 add_action('wp_login_errors', 'loginizer_error_handler', 10001, 2);
318 add_action('woocommerce_login_failed', 'loginizer_woocommerce_error_handler', 10001);
319 add_action('wp_login', 'loginizer_login_success', 10, 2);
320
321 }
322
323 // ----------------
324 // PRO INIT
325 // ----------------
326
327 // Email to Login
328 $options = get_option('loginizer_epl');
329 $loginizer['pl_d_sub'] = __('Login at $site_name','loginizer');
330 $loginizer['pl_d_msg'] = __('Hi,
331
332 A login request was submitted for your account $email at :
333 $site_name - $site_url
334
335 Login at $site_name by visiting this url :
336 $login_url
337
338 If you have not requested for the Login URL, please ignore this email.
339
340 Regards,
341 $site_name','loginizer');
342 $loginizer['email_pass_less'] = empty($options['email_pass_less']) ? 0 : $options['email_pass_less'];
343 $loginizer['passwordless_sub'] = empty($options['passwordless_sub']) ? $loginizer['pl_d_sub'] : $options['passwordless_sub'];
344 $loginizer['passwordless_msg'] = empty($options['passwordless_msg']) ? $loginizer['pl_d_msg'] : $options['passwordless_msg'];
345 $loginizer['passwordless_msg_is_custom'] = empty($options['passwordless_msg']) ? 0 : 1;
346 $loginizer['passwordless_html'] = empty($options['passwordless_html']) ? 0 : $options['passwordless_html'];
347 $loginizer['passwordless_redirect'] = empty($options['passwordless_redirect']) ? 0 : $options['passwordless_redirect'];
348 $loginizer['passwordless_redirect_for'] = empty($options['passwordless_redirect_for']) ? 0 : $options['passwordless_redirect_for'];
349
350 // 2FA OTP Email to Login
351 $options = get_option('loginizer_2fa_email_template');
352 $loginizer['2fa_email_d_sub'] = 'OTP : Login at $site_name';
353 $loginizer['2fa_email_d_msg'] = 'Hi,
354
355 A login request was submitted for your account $email at :
356 $site_name - $site_url
357
358 Please use the following One Time password (OTP) to login :
359 $otp
360
361 Note : The OTP expires after 10 minutes.
362
363 If you haven\'t requested for the OTP, please ignore this email.
364
365 Regards,
366 $site_name';
367
368 $loginizer['2fa_email_sub'] = empty($options['2fa_email_sub']) ? $loginizer['2fa_email_d_sub'] : $options['2fa_email_sub'];
369 $loginizer['2fa_email_msg'] = empty($options['2fa_email_msg']) ? $loginizer['2fa_email_d_msg'] : $options['2fa_email_msg'];
370
371 // For SitePad its always on
372 if(defined('SITEPAD')){
373 $loginizer['email_pass_less'] = 1;
374 }
375
376 // Captcha
377 $options = get_option('loginizer_captcha');
378 $loginizer['captcha_type'] = empty($options['captcha_type']) ? '' : $options['captcha_type'];
379 $loginizer['captcha_key'] = empty($options['captcha_key']) ? '' : $options['captcha_key'];
380 $loginizer['captcha_secret'] = empty($options['captcha_secret']) ? '' : $options['captcha_secret'];
381 $loginizer['captcha_theme'] = empty($options['captcha_theme']) ? 'light' : $options['captcha_theme'];
382 $loginizer['captcha_size'] = empty($options['captcha_size']) ? 'normal' : $options['captcha_size'];
383 $loginizer['captcha_lang'] = empty($options['captcha_lang']) ? '' : $options['captcha_lang'];
384 $loginizer['captcha_user_hide'] = !isset($options['captcha_user_hide']) ? 0 : $options['captcha_user_hide'];
385 $loginizer['captcha_no_css_login'] = !isset($options['captcha_no_css_login']) ? 0 : $options['captcha_no_css_login'];
386 $loginizer['captcha_no_js'] = 1;
387 $loginizer['captcha_login'] = !isset($options['captcha_login']) ? 1 : $options['captcha_login'];
388 $loginizer['captcha_lostpass'] = !isset($options['captcha_lostpass']) ? 1 : $options['captcha_lostpass'];
389 $loginizer['captcha_resetpass'] = !isset($options['captcha_resetpass']) ? 1 : $options['captcha_resetpass'];
390 $loginizer['captcha_register'] = !isset($options['captcha_register']) ? 1 : $options['captcha_register'];
391 $loginizer['captcha_comment'] = !isset($options['captcha_comment']) ? 1 : $options['captcha_comment'];
392 $loginizer['captcha_wc_checkout'] = !isset($options['captcha_wc_checkout']) ? 1 : $options['captcha_wc_checkout'];
393
394 $loginizer['captcha_no_google'] = !isset($options['captcha_no_google']) ? 0 : $options['captcha_no_google'];
395 $loginizer['captcha_domain'] = empty($options['captcha_domain']) ? 'www.google.com' : $options['captcha_domain'];
396
397 $loginizer['captcha_text'] = empty($options['captcha_text']) ? __('Math Captcha', 'loginizer') : $options['captcha_text'];
398 $loginizer['captcha_time'] = empty($options['captcha_time']) ? 300 : $options['captcha_time'];
399 $loginizer['captcha_words'] = !isset($options['captcha_words']) ? 0 : $options['captcha_words'];
400 $loginizer['captcha_add'] = !isset($options['captcha_add']) ? 1 : $options['captcha_add'];
401 $loginizer['captcha_subtract'] = !isset($options['captcha_subtract']) ? 1 : $options['captcha_subtract'];
402 $loginizer['captcha_multiply'] = !isset($options['captcha_multiply']) ? 0 : $options['captcha_multiply'];
403 $loginizer['captcha_divide'] = !isset($options['captcha_divide']) ? 0 : $options['captcha_divide'];
404
405 // 2fa/question
406 $options = get_option('loginizer_2fa');
407 $loginizer['2fa_app'] = !isset($options['2fa_app']) ? 0 : $options['2fa_app'];
408 $loginizer['2fa_email'] = !isset($options['2fa_email']) ? 0 : $options['2fa_email'];
409 $loginizer['2fa_email_force'] = !isset($options['2fa_email_force']) ? 0 : $options['2fa_email_force'];
410 $loginizer['2fa_sms'] = !isset($options['2fa_sms']) ? 0 : $options['2fa_sms'];
411 $loginizer['question'] = !isset($options['question']) ? 0 : $options['question'];
412 $loginizer['2fa_default'] = empty($options['2fa_default']) ? 'question' : $options['2fa_default'];
413 $loginizer['2fa_roles'] = empty($options['2fa_roles']) ? array() : $options['2fa_roles'];
414
415 // Security Settings
416 $options = get_option('loginizer_security');
417 $loginizer['login_slug'] = empty($options['login_slug']) ? '' : $options['login_slug'];
418 $loginizer['rename_login_secret'] = empty($options['rename_login_secret']) ? '' : $options['rename_login_secret'];
419 $loginizer['xmlrpc_slug'] = empty($options['xmlrpc_slug']) ? '' : $options['xmlrpc_slug'];
420 $loginizer['xmlrpc_disable'] = empty($options['xmlrpc_disable']) ? '' : $options['xmlrpc_disable'];// Disable XML-RPC
421 $loginizer['pingbacks_disable'] = empty($options['pingbacks_disable']) ? '' : $options['pingbacks_disable'];// Disable Pingbacks
422
423 // Admin Slug Settings
424 $options = get_option('loginizer_wp_admin');
425 $loginizer['admin_slug'] = empty($options['admin_slug']) ? '' : $options['admin_slug'];
426 $loginizer['restrict_wp_admin'] = empty($options['restrict_wp_admin']) ? '' : $options['restrict_wp_admin'];
427 $loginizer['wp_admin_msg'] = empty($options['wp_admin_msg']) ? '' : $options['wp_admin_msg'];
428
429 // Checksum Settings
430 $options = get_option('loginizer_checksums');
431 $loginizer['disable_checksum'] = empty($options['disable_checksum']) ? '' : $options['disable_checksum'];
432 $loginizer['checksum_time'] = empty($options['checksum_time']) ? '' : $options['checksum_time'];
433 $loginizer['checksum_frequency'] = empty($options['checksum_frequency']) ? 7 : $options['checksum_frequency'];
434 $loginizer['no_checksum_email'] = empty($options['no_checksum_email']) ? '' : $options['no_checksum_email'];
435 $loginizer['checksums_last_run'] = get_option('loginizer_checksums_last_run');
436
437 // Auto Blacklist Usernames
438 $loginizer['username_blacklist'] = get_option('loginizer_username_blacklist');
439
440 $loginizer['domains_blacklist'] = get_option('loginizer_domains_blacklist');
441
442 $loginizer['wp_admin_d_msg'] = __('LZ : Not allowed via WP-ADMIN. Please access over the new Admin URL', 'loginizer');
443
444 // CSRF Protection
445 $loginizer['enable_csrf_protection'] = get_option('loginizer_csrf_protection');
446 $loginizer['2fa_custom_login_redirect'] = get_option('loginizer_2fa_custom_redirect');
447 $loginizer['limit_session'] = get_option('loginizer_limit_session');
448
449 if((function_exists('wp_doing_ajax') && wp_doing_ajax()) || (defined( 'DOING_AJAX' ) && DOING_AJAX)){
450 include_once LOGINIZER_DIR . '/main/ajax.php';
451 }
452
453 if(is_admin()){
454 include_once LOGINIZER_DIR . '/main/admin.php';
455 }
456
457 // ----------------
458 // PRO INIT END
459 // ----------------
460
461 // Is the premium features there ?
462 if(file_exists(LOGINIZER_DIR.'/premium.php')){
463
464 // Include the file
465 include_once(LOGINIZER_DIR.'/premium.php');
466
467 loginizer_security_init();
468
469 // Its the free version
470 }else{
471
472 // The promo time
473 $loginizer['promo_time'] = get_option('loginizer_promo_time');
474 if(empty($loginizer['promo_time'])){
475 $loginizer['promo_time'] = time();
476 update_option('loginizer_promo_time', $loginizer['promo_time']);
477 }
478
479 // Are we to show the loginizer promo
480 if(!empty($loginizer['promo_time']) && $loginizer['promo_time'] > 0 && $loginizer['promo_time'] < (time() - (30*24*3600))){
481
482 add_action('admin_notices', 'loginizer_promo');
483
484 }
485
486 if(!file_exists(LOGINIZER_DIR.'/premium.php') && current_user_can('activate_plugins') && !empty($loginizer['csrf_promo']) && $loginizer['csrf_promo'] > 0 && $loginizer['csrf_promo'] < (time() - 86400)){
487
488 add_action('admin_notices', 'loginizer_csrf_promo');
489
490 }
491
492 // Are we to disable the promo
493 if(isset($_GET['loginizer_promo']) && (int)$_GET['loginizer_promo'] == 0){
494 update_option('loginizer_promo_time', (0 - time()) );
495 die('DONE');
496 }
497
498 $loginizer['backuply_promo'] = get_option('loginizer_backuply_promo_time');
499
500 if(empty($loginizer['backuply_promo'])){
501 $loginizer['backuply_promo'] = abs($loginizer['promo_time']);
502 update_option('loginizer_backuply_promo_time', $loginizer['backuply_promo']);
503 }
504
505 // Setting CSRF Promo time
506 $loginizer['csrf_promo'] = get_option('loginizer_csrf_promo_time');
507
508 if(empty($loginizer['csrf_promo'])){
509 $loginizer['csrf_promo'] = abs($loginizer['promo_time']);
510 update_option('loginizer_csrf_promo_time', $loginizer['csrf_promo']);
511 }
512 }
513
514 }
515
516 // Should return NULL if everything is fine
517 function loginizer_wp_authenticate($user, $username, $password){
518
519 global $loginizer, $lz_error, $lz_cannot_login, $lz_user_pass;
520
521 if(!empty($username) && !empty($password)){
522 $lz_user_pass = 1;
523 }
524
525 // Are you whitelisted ?
526 if(loginizer_is_whitelisted()){
527 $loginizer['ip_is_whitelisted'] = 1;
528 return $user;
529
530 } else if (!empty($loginizer['trusted_ips'])){
531 $lz_cannot_login = 1;
532
533 // This is used by WP Activity Log
534 apply_filters( 'wp_login_blocked', $username );
535
536 return new WP_Error('ip_blacklisted', __('Your IP is not whitelisted, so you can not log in', 'loginizer'));
537 }
538
539 // Are you blacklisted ?
540 if(loginizer_is_blacklisted()){
541 $lz_cannot_login = 1;
542
543 // This is used by WP Activity Log
544 apply_filters( 'wp_login_blocked', $username );
545
546 return new WP_Error('ip_blacklisted', implode('', $lz_error), 'loginizer');
547 }
548
549 // Is the username blacklisted ?
550 if(function_exists('loginizer_user_blacklisted')){
551 if(loginizer_user_blacklisted($username)){
552 $lz_cannot_login = 1;
553
554 // This is used by WP Activity Log
555 apply_filters( 'wp_login_blocked', $username );
556
557 return new WP_Error('user_blacklisted', implode('', $lz_error), 'loginizer');
558 }
559 }
560
561 if(loginizer_can_login()){
562 return $user;
563 }
564
565 $lz_cannot_login = 1;
566
567 // This is used by WP Activity Log
568 apply_filters( 'wp_login_blocked', $username );
569
570 return new WP_Error('ip_blocked', implode('', $lz_error), 'loginizer');
571
572 }
573
574 function loginizer_can_login(){
575
576 global $wpdb, $loginizer, $lz_error;
577
578 // Get the logs
579 $sel_query = $wpdb->prepare("SELECT * FROM `".$wpdb->prefix."loginizer_logs` WHERE `ip` = %s", $loginizer['current_ip']);
580 $result = lz_selectquery($sel_query);
581
582 if(!empty($result['count']) && ($result['count'] % $loginizer['max_retries']) == 0){
583
584 // Has he reached max lockouts ?
585 if($result['lockout'] >= $loginizer['max_lockouts']){
586 $loginizer['lockout_time'] = $loginizer['lockouts_extend'];
587 }
588
589 // Is he in the lockout time ?
590 if($result['time'] >= (time() - $loginizer['lockout_time'])){
591 $banlift = ceil((($result['time'] + $loginizer['lockout_time']) - time()) / 60);
592
593 //echo 'Current Time '.date('d/M/Y H:i:s P', time()).'<br />';
594 //echo 'Last attempt '.date('d/M/Y H:i:s P', $result['time']).'<br />';
595 //echo 'Unlock Time '.date('d/M/Y H:i:s P', $result['time'] + $loginizer['lockout_time']).'<br />';
596
597 $_time = $banlift.' '.$loginizer['msg']['minutes_err'];
598
599 if($banlift > 60){
600 $banlift = ceil($banlift / 60);
601 $_time = $banlift.' '.$loginizer['msg']['hours_err'];
602 }
603
604 $lz_error['ip_blocked'] = $loginizer['msg']['lockout_err'].' '.$_time;
605
606 return false;
607 }
608 }
609
610 return true;
611 }
612
613 function loginizer_is_blacklisted(){
614
615 global $wpdb, $loginizer, $lz_error;
616
617 $blacklist = $loginizer['blacklist'];
618
619 if(empty($blacklist)){
620 return false;
621 }
622
623 foreach($blacklist as $k => $v){
624
625 // Is the IP in the blacklist ?
626 if(inet_ptoi($v['start']) <= inet_ptoi($loginizer['current_ip']) && inet_ptoi($loginizer['current_ip']) <= inet_ptoi($v['end'])){
627 $result = 1;
628 break;
629 }
630
631 // Is it in a wider range ?
632 if(inet_ptoi($v['start']) >= 0 && inet_ptoi($v['end']) < 0){
633
634 // Since the end of the RANGE (i.e. current IP range) is beyond the +ve value of inet_ptoi,
635 // if the current IP is <= than the start of the range, it is within the range
636 // OR
637 // if the current IP is <= than the end of the range, it is within the range
638 if(inet_ptoi($v['start']) <= inet_ptoi($loginizer['current_ip'])
639 || inet_ptoi($loginizer['current_ip']) <= inet_ptoi($v['end'])){
640 $result = 1;
641 break;
642 }
643
644 }
645
646 }
647
648 // You are blacklisted
649 if(!empty($result)){
650 $lz_error['ip_blacklisted'] = $loginizer['msg']['ip_blacklisted'];
651 return true;
652 }
653
654 return false;
655
656 }
657
658 function loginizer_is_whitelisted(){
659
660 global $wpdb, $loginizer, $lz_error;
661
662 $whitelist = $loginizer['whitelist'];
663
664 if(empty($whitelist)){
665 return false;
666 }
667
668 foreach($whitelist as $k => $v){
669
670 // Is the IP in the blacklist ?
671 if(inet_ptoi($v['start']) <= inet_ptoi($loginizer['current_ip']) && inet_ptoi($loginizer['current_ip']) <= inet_ptoi($v['end'])){
672 $result = 1;
673 break;
674 }
675
676 // Is it in a wider range ?
677 if(inet_ptoi($v['start']) >= 0 && inet_ptoi($v['end']) < 0){
678
679 // Since the end of the RANGE (i.e. current IP range) is beyond the +ve value of inet_ptoi,
680 // if the current IP is <= than the start of the range, it is within the range
681 // OR
682 // if the current IP is <= than the end of the range, it is within the range
683 if(inet_ptoi($v['start']) <= inet_ptoi($loginizer['current_ip'])
684 || inet_ptoi($loginizer['current_ip']) <= inet_ptoi($v['end'])){
685 $result = 1;
686 break;
687 }
688
689 }
690
691 }
692
693 // You are whitelisted
694 if(!empty($result)){
695 return true;
696 }
697
698 return false;
699
700 }
701
702
703 // When the login fails, then this is called
704 // We need to update the database
705 function loginizer_login_failed($username, $is_2fa = ''){
706
707 global $wpdb, $loginizer, $lz_cannot_login;
708
709 // 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
710 if(empty($username) || is_null($username)){
711 $username = '';
712 }
713
714 $fail_type = 'Login';
715
716 if(!empty($is_2fa)){
717 $fail_type = '2FA';
718 }
719
720 if(empty($lz_cannot_login) && empty($loginizer['ip_is_whitelisted']) && empty($loginizer['no_loginizer_logs'])){
721
722 $url = @addslashes((!empty($_SERVER['HTTPS']) ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
723 $url = esc_url($url);
724
725 $sel_query = $wpdb->prepare("SELECT * FROM `".$wpdb->prefix."loginizer_logs` WHERE `ip` = %s", $loginizer['current_ip']);
726 $result = lz_selectquery($sel_query);
727
728 if(!empty($result)){
729 $lockout = floor((($result['count']+1) / $loginizer['max_retries']));
730
731 $update_data = array('username' => $username,
732 'time' => time(),
733 'count' => $result['count']+1,
734 'lockout' => $lockout,
735 'url' => $url);
736
737 $where_data = array('ip' => $loginizer['current_ip']);
738
739 $format = array('%s','%d','%d','%d','%s');
740 $where_format = array('%s');
741
742 $wpdb->update($wpdb->prefix.'loginizer_logs', $update_data, $where_data, $format, $where_format);
743
744 // Do we need to email admin ?
745 if(!empty($loginizer['notify_email']) && $lockout >= $loginizer['notify_email']){
746
747 $lockout_time = $loginizer['lockout_time'];
748
749 if($lockout >= $loginizer['max_lockouts']){
750 // extended lockout is in hours so we have to convert to minute
751 $lockout_time = $loginizer['lockouts_extend'];
752 }
753
754 $sitename = lz_is_multisite() ? get_site_option('site_name') : get_option('blogname');
755 $mail = array();
756 $mail['to'] = $loginizer['notify_email_address'];
757 $mail['subject'] = 'Failed '.$fail_type.' Attempts from IP '.$loginizer['current_ip'].' ('.$sitename.')';
758 $mail['message'] = 'Hi,
759
760 '.($result['count']+1).' failed '.strtolower($fail_type).' attempts and '.$lockout.' lockout(s) from IP '.$loginizer['current_ip'].' on your site :
761 '.home_url().'
762
763 Last '.$fail_type.' Attempt : '.date('d/M/Y H:i:s P', time()).'
764 Last User Attempt : '.$username.'
765 IP has been blocked until : '.date('d/M/Y H:i:s P', time() + $lockout_time).'
766
767 Regards,
768 Loginizer';
769
770 @wp_mail($mail['to'], $mail['subject'], $mail['message']);
771 }
772 }else{
773 $result = array();
774 $result['count'] = 0;
775
776 $insert_data = array('username' => $username,
777 'time' => time(),
778 'count' => 1,
779 'ip' => $loginizer['current_ip'],
780 'lockout' => 0,
781 'url' => $url);
782
783 $format = array('%s','%d','%d','%s','%d','%s');
784
785 $wpdb->insert($wpdb->prefix.'loginizer_logs', $insert_data, $format);
786 }
787
788 // We need to add one as this is a failed attempt as well
789 $result['count'] = $result['count'] + 1;
790 loginizer_update_attempt_stats(0);
791 $loginizer['retries_left'] = ($loginizer['max_retries'] - ($result['count'] % $loginizer['max_retries']));
792 $loginizer['retries_left'] = $loginizer['retries_left'] == $loginizer['max_retries'] ? 0 : $loginizer['retries_left'];
793
794 }
795 }
796
797 function loginizer_login_success($user_login, $user){
798 loginizer_update_attempt_stats(1);
799 }
800
801 function loginizer_update_attempt_stats($type){
802
803 $stats = get_option('loginizer_login_attempt_stats', []);
804 $time = strtotime(date('Y-m-d H:00:00'));
805
806 if(empty($stats[$time][$type])){
807 $stats[$time][$type] = 0;
808 }
809
810 $stats[$time][$type] += 1;
811
812 update_option('loginizer_login_attempt_stats', $stats, false);
813 }
814
815 // Handles the error of the password not being there
816 function loginizer_error_handler($errors, $redirect_to){
817
818 global $wpdb, $loginizer, $lz_user_pass, $lz_cannot_login;
819
820 //echo 'loginizer_error_handler :';print_r($errors->errors);echo '<br>';
821 if(is_null($errors) || empty($errors)){
822 return true;
823 }
824
825 // Remove the empty password error
826 if(is_wp_error($errors)){
827
828 $codes = $errors->get_error_codes();
829
830 foreach($codes as $k => $v){
831 if($v == 'invalid_username' || $v == 'incorrect_password'){
832 $show_error = 1;
833 }
834 }
835
836 $errors->remove('invalid_username');
837 $errors->remove('incorrect_password');
838
839 // Add the error
840 if(!empty($lz_user_pass) && !empty($show_error) && empty($lz_cannot_login)){
841 $errors->add('invalid_userpass', '<b>ERROR:</b> ' . $loginizer['msg']['inv_userpass']);
842 }
843
844 // Add the number of retires left as well
845 if(count($errors->get_error_codes()) > 0 && isset($loginizer['retries_left'])){
846 $errors->add('retries_left', loginizer_retries_left());
847 }
848
849 }
850
851 return $errors;
852
853 }
854
855 // Handles the error of the password not being there
856 function loginizer_woocommerce_error_handler(){
857
858 global $wpdb, $loginizer, $lz_user_pass, $lz_cannot_login;
859
860 if(function_exists('wc_add_notice')){
861 wc_add_notice( loginizer_retries_left(), 'error' );
862 }
863
864 }
865
866 // Returns a string with the number of retries left
867 function loginizer_retries_left(){
868
869 global $wpdb, $loginizer, $lz_user_pass, $lz_cannot_login;
870
871 // If we are to show the number of retries left
872 if(isset($loginizer['retries_left'])){
873 $retries_left = apply_filters('loginizer_retries_left_num', $loginizer['retries_left']);
874
875 return '<b>'.esc_html($retries_left).'</b> '.$loginizer['msg']['attempts_left'];
876 }
877
878 }
879
880 function loginizer_reset_retries(){
881
882 global $wpdb, $loginizer;
883
884 $deltime = time() - $loginizer['reset_retries'];
885
886 $del_query = $wpdb->prepare("DELETE FROM `".$wpdb->prefix."loginizer_logs` WHERE `time` <= %d", $deltime);
887 $result = $wpdb->query($del_query);
888
889 update_option('loginizer_last_reset', time());
890
891 }
892
893 // Sorry to see you going
894 register_uninstall_hook(LOGINIZER_FILE, 'loginizer_deactivation');
895
896 function loginizer_deactivation(){
897
898 global $wpdb;
899
900 $sql = array();
901 $sql[] = "DROP TABLE ".$wpdb->prefix."loginizer_logs;";
902
903 foreach($sql as $sk => $sv){
904 $wpdb->query($sv);
905 }
906
907 delete_option('loginizer_version');
908 delete_option('loginizer_options');
909 delete_option('loginizer_last_reset');
910 delete_option('loginizer_whitelist');
911 delete_option('loginizer_blacklist');
912 delete_option('loginizer_msg');
913 delete_option('loginizer_2fa_msg');
914 delete_option('loginizer_2fa_email_template');
915 delete_option('loginizer_security');
916 delete_option('loginizer_wp_admin');
917 delete_option('loginizer_csrf_promo_time');
918 delete_option('loginizer_backuply_promo_time');
919 delete_option('loginizer_promo_time');
920 delete_option('loginizer_ins_time');
921 delete_option('loginizer_2fa_whitelist');
922 delete_option('loginizer_checksums_last_run');
923 delete_option('loginizer_checksums_diff');
924 delete_option('loginizer_ip_method');
925 delete_option('loginizer_2fa_custom_redirect');
926 delete_option('external_updates-loginizer-security');
927 delete_option('loginizer_login_attempt_stats');
928
929 }