PluginProbe
Loginizer / 1.8.3
Loginizer v1.8.3
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.3, at init.php

931 lines 31.3 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.3');
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 if(current_user_can('activate_plugins')){
473 // The promo time
474 $loginizer['promo_time'] = get_option('loginizer_promo_time');
475 if(empty($loginizer['promo_time'])){
476 $loginizer['promo_time'] = time();
477 update_option('loginizer_promo_time', $loginizer['promo_time']);
478 }
479
480 // Are we to show the loginizer promo
481 if(!empty($loginizer['promo_time']) && $loginizer['promo_time'] > 0 && $loginizer['promo_time'] < (time() - (30*24*3600))){
482
483 add_action('admin_notices', 'loginizer_promo');
484
485 }
486
487 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)){
488
489 add_action('admin_notices', 'loginizer_csrf_promo');
490
491 }
492
493 // Are we to disable the promo
494 if(isset($_GET['loginizer_promo']) && (int)$_GET['loginizer_promo'] == 0){
495 update_option('loginizer_promo_time', (0 - time()) );
496 die('DONE');
497 }
498
499 $loginizer['backuply_promo'] = get_option('loginizer_backuply_promo_time');
500
501 if(empty($loginizer['backuply_promo'])){
502 $loginizer['backuply_promo'] = abs($loginizer['promo_time']);
503 update_option('loginizer_backuply_promo_time', $loginizer['backuply_promo']);
504 }
505
506 // Setting CSRF Promo time
507 $loginizer['csrf_promo'] = get_option('loginizer_csrf_promo_time');
508
509 if(empty($loginizer['csrf_promo'])){
510 $loginizer['csrf_promo'] = abs($loginizer['promo_time']);
511 update_option('loginizer_csrf_promo_time', $loginizer['csrf_promo']);
512 }
513 }
514 }
515
516 }
517
518 // Should return NULL if everything is fine
519 function loginizer_wp_authenticate($user, $username, $password){
520
521 global $loginizer, $lz_error, $lz_cannot_login, $lz_user_pass;
522
523 if(!empty($username) && !empty($password)){
524 $lz_user_pass = 1;
525 }
526
527 // Are you whitelisted ?
528 if(loginizer_is_whitelisted()){
529 $loginizer['ip_is_whitelisted'] = 1;
530 return $user;
531
532 } else if (!empty($loginizer['trusted_ips'])){
533 $lz_cannot_login = 1;
534
535 // This is used by WP Activity Log
536 apply_filters( 'wp_login_blocked', $username );
537
538 return new WP_Error('ip_blacklisted', __('Your IP is not whitelisted, so you can not log in', 'loginizer'));
539 }
540
541 // Are you blacklisted ?
542 if(loginizer_is_blacklisted()){
543 $lz_cannot_login = 1;
544
545 // This is used by WP Activity Log
546 apply_filters( 'wp_login_blocked', $username );
547
548 return new WP_Error('ip_blacklisted', implode('', $lz_error), 'loginizer');
549 }
550
551 // Is the username blacklisted ?
552 if(function_exists('loginizer_user_blacklisted')){
553 if(loginizer_user_blacklisted($username)){
554 $lz_cannot_login = 1;
555
556 // This is used by WP Activity Log
557 apply_filters( 'wp_login_blocked', $username );
558
559 return new WP_Error('user_blacklisted', implode('', $lz_error), 'loginizer');
560 }
561 }
562
563 if(loginizer_can_login()){
564 return $user;
565 }
566
567 $lz_cannot_login = 1;
568
569 // This is used by WP Activity Log
570 apply_filters( 'wp_login_blocked', $username );
571
572 return new WP_Error('ip_blocked', implode('', $lz_error), 'loginizer');
573
574 }
575
576 function loginizer_can_login(){
577
578 global $wpdb, $loginizer, $lz_error;
579
580 // Get the logs
581 $sel_query = $wpdb->prepare("SELECT * FROM `".$wpdb->prefix."loginizer_logs` WHERE `ip` = %s", $loginizer['current_ip']);
582 $result = lz_selectquery($sel_query);
583
584 if(!empty($result['count']) && ($result['count'] % $loginizer['max_retries']) == 0){
585
586 // Has he reached max lockouts ?
587 if($result['lockout'] >= $loginizer['max_lockouts']){
588 $loginizer['lockout_time'] = $loginizer['lockouts_extend'];
589 }
590
591 // Is he in the lockout time ?
592 if($result['time'] >= (time() - $loginizer['lockout_time'])){
593 $banlift = ceil((($result['time'] + $loginizer['lockout_time']) - time()) / 60);
594
595 //echo 'Current Time '.date('d/M/Y H:i:s P', time()).'<br />';
596 //echo 'Last attempt '.date('d/M/Y H:i:s P', $result['time']).'<br />';
597 //echo 'Unlock Time '.date('d/M/Y H:i:s P', $result['time'] + $loginizer['lockout_time']).'<br />';
598
599 $_time = $banlift.' '.$loginizer['msg']['minutes_err'];
600
601 if($banlift > 60){
602 $banlift = ceil($banlift / 60);
603 $_time = $banlift.' '.$loginizer['msg']['hours_err'];
604 }
605
606 $lz_error['ip_blocked'] = $loginizer['msg']['lockout_err'].' '.$_time;
607
608 return false;
609 }
610 }
611
612 return true;
613 }
614
615 function loginizer_is_blacklisted(){
616
617 global $wpdb, $loginizer, $lz_error;
618
619 $blacklist = $loginizer['blacklist'];
620
621 if(empty($blacklist)){
622 return false;
623 }
624
625 foreach($blacklist as $k => $v){
626
627 // Is the IP in the blacklist ?
628 if(inet_ptoi($v['start']) <= inet_ptoi($loginizer['current_ip']) && inet_ptoi($loginizer['current_ip']) <= inet_ptoi($v['end'])){
629 $result = 1;
630 break;
631 }
632
633 // Is it in a wider range ?
634 if(inet_ptoi($v['start']) >= 0 && inet_ptoi($v['end']) < 0){
635
636 // Since the end of the RANGE (i.e. current IP range) is beyond the +ve value of inet_ptoi,
637 // if the current IP is <= than the start of the range, it is within the range
638 // OR
639 // if the current IP is <= than the end of the range, it is within the range
640 if(inet_ptoi($v['start']) <= inet_ptoi($loginizer['current_ip'])
641 || inet_ptoi($loginizer['current_ip']) <= inet_ptoi($v['end'])){
642 $result = 1;
643 break;
644 }
645
646 }
647
648 }
649
650 // You are blacklisted
651 if(!empty($result)){
652 $lz_error['ip_blacklisted'] = $loginizer['msg']['ip_blacklisted'];
653 return true;
654 }
655
656 return false;
657
658 }
659
660 function loginizer_is_whitelisted(){
661
662 global $wpdb, $loginizer, $lz_error;
663
664 $whitelist = $loginizer['whitelist'];
665
666 if(empty($whitelist)){
667 return false;
668 }
669
670 foreach($whitelist as $k => $v){
671
672 // Is the IP in the blacklist ?
673 if(inet_ptoi($v['start']) <= inet_ptoi($loginizer['current_ip']) && inet_ptoi($loginizer['current_ip']) <= inet_ptoi($v['end'])){
674 $result = 1;
675 break;
676 }
677
678 // Is it in a wider range ?
679 if(inet_ptoi($v['start']) >= 0 && inet_ptoi($v['end']) < 0){
680
681 // Since the end of the RANGE (i.e. current IP range) is beyond the +ve value of inet_ptoi,
682 // if the current IP is <= than the start of the range, it is within the range
683 // OR
684 // if the current IP is <= than the end of the range, it is within the range
685 if(inet_ptoi($v['start']) <= inet_ptoi($loginizer['current_ip'])
686 || inet_ptoi($loginizer['current_ip']) <= inet_ptoi($v['end'])){
687 $result = 1;
688 break;
689 }
690
691 }
692
693 }
694
695 // You are whitelisted
696 if(!empty($result)){
697 return true;
698 }
699
700 return false;
701
702 }
703
704
705 // When the login fails, then this is called
706 // We need to update the database
707 function loginizer_login_failed($username, $is_2fa = ''){
708
709 global $wpdb, $loginizer, $lz_cannot_login;
710
711 // 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
712 if(empty($username) || is_null($username)){
713 $username = '';
714 }
715
716 $fail_type = 'Login';
717
718 if(!empty($is_2fa)){
719 $fail_type = '2FA';
720 }
721
722 if(empty($lz_cannot_login) && empty($loginizer['ip_is_whitelisted']) && empty($loginizer['no_loginizer_logs'])){
723
724 $url = @addslashes((!empty($_SERVER['HTTPS']) ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
725 $url = esc_url($url);
726
727 $sel_query = $wpdb->prepare("SELECT * FROM `".$wpdb->prefix."loginizer_logs` WHERE `ip` = %s", $loginizer['current_ip']);
728 $result = lz_selectquery($sel_query);
729
730 if(!empty($result)){
731 $lockout = floor((($result['count']+1) / $loginizer['max_retries']));
732
733 $update_data = array('username' => $username,
734 'time' => time(),
735 'count' => $result['count']+1,
736 'lockout' => $lockout,
737 'url' => $url);
738
739 $where_data = array('ip' => $loginizer['current_ip']);
740
741 $format = array('%s','%d','%d','%d','%s');
742 $where_format = array('%s');
743
744 $wpdb->update($wpdb->prefix.'loginizer_logs', $update_data, $where_data, $format, $where_format);
745
746 // Do we need to email admin ?
747 if(!empty($loginizer['notify_email']) && $lockout >= $loginizer['notify_email']){
748
749 $lockout_time = $loginizer['lockout_time'];
750
751 if($lockout >= $loginizer['max_lockouts']){
752 // extended lockout is in hours so we have to convert to minute
753 $lockout_time = $loginizer['lockouts_extend'];
754 }
755
756 $sitename = lz_is_multisite() ? get_site_option('site_name') : get_option('blogname');
757 $mail = array();
758 $mail['to'] = $loginizer['notify_email_address'];
759 $mail['subject'] = 'Failed '.$fail_type.' Attempts from IP '.$loginizer['current_ip'].' ('.$sitename.')';
760 $mail['message'] = 'Hi,
761
762 '.($result['count']+1).' failed '.strtolower($fail_type).' attempts and '.$lockout.' lockout(s) from IP '.$loginizer['current_ip'].' on your site :
763 '.home_url().'
764
765 Last '.$fail_type.' Attempt : '.date('d/M/Y H:i:s P', time()).'
766 Last User Attempt : '.$username.'
767 IP has been blocked until : '.date('d/M/Y H:i:s P', time() + $lockout_time).'
768
769 Regards,
770 Loginizer';
771
772 @wp_mail($mail['to'], $mail['subject'], $mail['message']);
773 }
774 }else{
775 $result = array();
776 $result['count'] = 0;
777
778 $insert_data = array('username' => $username,
779 'time' => time(),
780 'count' => 1,
781 'ip' => $loginizer['current_ip'],
782 'lockout' => 0,
783 'url' => $url);
784
785 $format = array('%s','%d','%d','%s','%d','%s');
786
787 $wpdb->insert($wpdb->prefix.'loginizer_logs', $insert_data, $format);
788 }
789
790 // We need to add one as this is a failed attempt as well
791 $result['count'] = $result['count'] + 1;
792 loginizer_update_attempt_stats(0);
793 $loginizer['retries_left'] = ($loginizer['max_retries'] - ($result['count'] % $loginizer['max_retries']));
794 $loginizer['retries_left'] = $loginizer['retries_left'] == $loginizer['max_retries'] ? 0 : $loginizer['retries_left'];
795
796 }
797 }
798
799 function loginizer_login_success($user_login, $user){
800 loginizer_update_attempt_stats(1);
801 }
802
803 function loginizer_update_attempt_stats($type){
804
805 $stats = get_option('loginizer_login_attempt_stats', []);
806 $time = strtotime(date('Y-m-d H:00:00'));
807
808 if(empty($stats[$time][$type])){
809 $stats[$time][$type] = 0;
810 }
811
812 $stats[$time][$type] += 1;
813
814 update_option('loginizer_login_attempt_stats', $stats, false);
815 }
816
817 // Handles the error of the password not being there
818 function loginizer_error_handler($errors, $redirect_to){
819
820 global $wpdb, $loginizer, $lz_user_pass, $lz_cannot_login;
821
822 //echo 'loginizer_error_handler :';print_r($errors->errors);echo '<br>';
823 if(is_null($errors) || empty($errors)){
824 return true;
825 }
826
827 // Remove the empty password error
828 if(is_wp_error($errors)){
829
830 $codes = $errors->get_error_codes();
831
832 foreach($codes as $k => $v){
833 if($v == 'invalid_username' || $v == 'incorrect_password'){
834 $show_error = 1;
835 }
836 }
837
838 $errors->remove('invalid_username');
839 $errors->remove('incorrect_password');
840
841 // Add the error
842 if(!empty($lz_user_pass) && !empty($show_error) && empty($lz_cannot_login)){
843 $errors->add('invalid_userpass', '<b>ERROR:</b> ' . $loginizer['msg']['inv_userpass']);
844 }
845
846 // Add the number of retires left as well
847 if(count($errors->get_error_codes()) > 0 && isset($loginizer['retries_left'])){
848 $errors->add('retries_left', loginizer_retries_left());
849 }
850
851 }
852
853 return $errors;
854
855 }
856
857 // Handles the error of the password not being there
858 function loginizer_woocommerce_error_handler(){
859
860 global $wpdb, $loginizer, $lz_user_pass, $lz_cannot_login;
861
862 if(function_exists('wc_add_notice')){
863 wc_add_notice( loginizer_retries_left(), 'error' );
864 }
865
866 }
867
868 // Returns a string with the number of retries left
869 function loginizer_retries_left(){
870
871 global $wpdb, $loginizer, $lz_user_pass, $lz_cannot_login;
872
873 // If we are to show the number of retries left
874 if(isset($loginizer['retries_left'])){
875 $retries_left = apply_filters('loginizer_retries_left_num', $loginizer['retries_left']);
876
877 return '<b>'.esc_html($retries_left).'</b> '.$loginizer['msg']['attempts_left'];
878 }
879
880 }
881
882 function loginizer_reset_retries(){
883
884 global $wpdb, $loginizer;
885
886 $deltime = time() - $loginizer['reset_retries'];
887
888 $del_query = $wpdb->prepare("DELETE FROM `".$wpdb->prefix."loginizer_logs` WHERE `time` <= %d", $deltime);
889 $result = $wpdb->query($del_query);
890
891 update_option('loginizer_last_reset', time());
892
893 }
894
895 // Sorry to see you going
896 register_uninstall_hook(LOGINIZER_FILE, 'loginizer_deactivation');
897
898 function loginizer_deactivation(){
899
900 global $wpdb;
901
902 $sql = array();
903 $sql[] = "DROP TABLE ".$wpdb->prefix."loginizer_logs;";
904
905 foreach($sql as $sk => $sv){
906 $wpdb->query($sv);
907 }
908
909 delete_option('loginizer_version');
910 delete_option('loginizer_options');
911 delete_option('loginizer_last_reset');
912 delete_option('loginizer_whitelist');
913 delete_option('loginizer_blacklist');
914 delete_option('loginizer_msg');
915 delete_option('loginizer_2fa_msg');
916 delete_option('loginizer_2fa_email_template');
917 delete_option('loginizer_security');
918 delete_option('loginizer_wp_admin');
919 delete_option('loginizer_csrf_promo_time');
920 delete_option('loginizer_backuply_promo_time');
921 delete_option('loginizer_promo_time');
922 delete_option('loginizer_ins_time');
923 delete_option('loginizer_2fa_whitelist');
924 delete_option('loginizer_checksums_last_run');
925 delete_option('loginizer_checksums_diff');
926 delete_option('loginizer_ip_method');
927 delete_option('loginizer_2fa_custom_redirect');
928 delete_option('external_updates-loginizer-security');
929 delete_option('loginizer_login_attempt_stats');
930
931 }