if(!function_exists('add_action')){
echo 'You are not allowed to access this page directly.';
define('LOGINIZER_VERSION', '2.0.6');
define('LOGINIZER_DIR', dirname(LOGINIZER_FILE));
define('LOGINIZER_URL', plugins_url('', LOGINIZER_FILE));
define('LOGINIZER_PRO_URL', 'https://loginizer.com/features#compare');
define('LOGINIZER_PRICING_URL', 'https://loginizer.com/pricing');
define('LOGINIZER_DOCS', 'https://loginizer.com/docs/');
include_once(LOGINIZER_DIR.'/functions.php');
// Ok so we are now ready to go
register_activation_hook(LOGINIZER_FILE, 'loginizer_activation');
// Is called when the ADMIN enables the plugin
function loginizer_activation(){
$sql[] = "DROP TABLE IF EXISTS `".$wpdb->prefix."loginizer_logs`";
$sql[] = "CREATE TABLE `".$wpdb->prefix."loginizer_logs` (
`username` varchar(255) NOT NULL DEFAULT '',
`time` int(10) NOT NULL DEFAULT '0',
`count` int(10) NOT NULL DEFAULT '0',
`lockout` int(10) NOT NULL DEFAULT '0',
`ip` varchar(255) NOT NULL DEFAULT '',
`url` varchar(255) NOT NULL DEFAULT '',
) DEFAULT CHARSET=utf8;";
foreach($sql as $sk => $sv){
add_option('loginizer_version', LOGINIZER_VERSION);
add_option('loginizer_options', array());
add_option('loginizer_last_reset', 0);
add_option('loginizer_whitelist', array());
add_option('loginizer_blacklist', array());
add_option('loginizer_2fa_whitelist', array());
// TODO:: REMOVE THIS AFTER MARCH 2025
$softwp_upgrade = get_option('loginizer_softwp_upgrade', 0);
if(!defined('SITEPAD') && empty($softwp_upgrade)){
loginizer_check_softaculous();
* Updates the database structure for Loginizer
* If the plugin files are updated but database structure is not updated
* this function will update the database structure as per the plugin version
* NOTE: This does not update plugin files it just updates the database structure
function loginizer_update_check(){
$current_version = get_option('loginizer_version');
// It must be the 1.0 pre stuff
if(empty($current_version)){
$current_version = get_option('lz_version');
$version = (int) str_replace('.', '', $current_version);
if($current_version == LOGINIZER_VERSION){
if(empty($current_version)){
// Trick the following if conditions to not run
$version = (int) str_replace('.', '', LOGINIZER_VERSION);
// Is it less than 1.0.1 ?
// TODO : GET the existing settings
// Get the existing settings
$lz_failed_logs = lz_selectquery("SELECT * FROM `".$wpdb->prefix."lz_failed_logs`;", 1);
$lz_options = lz_selectquery("SELECT * FROM `".$wpdb->prefix."lz_options`;", 1);
$lz_iprange = lz_selectquery("SELECT * FROM `".$wpdb->prefix."lz_iprange`;", 1);
// Delete the three tables
$sql[] = "DROP TABLE IF EXISTS ".$wpdb->prefix."lz_failed_logs;";
$sql[] = "DROP TABLE IF EXISTS ".$wpdb->prefix."lz_options;";
$sql[] = "DROP TABLE IF EXISTS ".$wpdb->prefix."lz_iprange;";
foreach($sql as $sk => $sv){
delete_option('lz_version');
// TODO : Save the existing settings
// Update the existing failed logs to new table
if(is_array($lz_failed_logs)){
foreach($lz_failed_logs as $fk => $fv){
$insert_data = array('username' => $fv['username'],
'lockout' => $fv['lockout'],
$format = array('%s','%d','%d','%d','%s');
$wpdb->insert($wpdb->prefix.'loginizer_logs', $insert_data, $format);
// Update the existing options to new structure
if(is_array($lz_options)){
foreach($lz_options as $ok => $ov){
if($ov['option_name'] == 'lz_last_reset'){
update_option('loginizer_last_reset', $ov['option_value']);
$old_option[str_replace('lz_', '', $ov['option_name'])] = $ov['option_value'];
update_option('loginizer_options', $old_option);
// Update the existing iprange to new structure
if(is_array($lz_iprange)){
$old_blacklist = array();
$old_whitelist = array();
foreach($lz_iprange as $ik => $iv){
if(!empty($iv['blacklist'])){
$old_blacklist[$bid] = array();
$old_blacklist[$bid]['start'] = long2ip($iv['start']);
$old_blacklist[$bid]['end'] = long2ip($iv['end']);
$old_blacklist[$bid]['time'] = strtotime($iv['date']);
if(!empty($iv['whitelist'])){
$old_whitelist[$wid] = array();
$old_whitelist[$wid]['start'] = long2ip($iv['start']);
$old_whitelist[$wid]['end'] = long2ip($iv['end']);
$old_whitelist[$wid]['time'] = strtotime($iv['date']);
if(!empty($old_blacklist)) update_option('loginizer_blacklist', $old_blacklist);
if(!empty($old_whitelist)) update_option('loginizer_whitelist', $old_whitelist);
// Is it less than 1.3.9 ?
$wpdb->query("ALTER TABLE ".$wpdb->prefix."loginizer_logs ADD `url` VARCHAR(255) NOT NULL DEFAULT '' AFTER `ip`;");
// Setting alignment to left in social login ?
$social_settings = get_option('loginizer_social_settings', []);
if(!empty($social_settings)){
if(!empty($social_settings['login']) && (!empty($social_settings['login']['login_form']) || !empty($social_settings['login']['registration_form']))){
$social_settings['login']['button_alignment'] = 'left';
if(!empty($social_settings['woocommerce']) && (!empty($social_settings['woocommmerce']['login_form']) || !empty($social_settings['woocommerce']['registration_form']))){
$social_settings['woocommerce']['button_alignment'] = 'left';
if(!empty($social_settings['comment']) && !empty($social_settings['comment']['enable_buttons'])){
$social_settings['comment']['button_alignment'] = 'left';
update_option('loginizer_social_settings', $social_settings);
update_option('loginizer_version', LOGINIZER_VERSION);
// TODO:: REMOVE THIS AFTER MARCH 2025
$softwp_upgrade = get_option('loginizer_softwp_upgrade', 0);
if(!defined('SITEPAD') && empty($softwp_upgrade)){
loginizer_check_softaculous();
// In Sitepad Math Captcha is enabled by default
if(defined('SITEPAD') && get_option('loginizer_captcha') === false){
$option['captcha_no_google'] = 1;
add_option('loginizer_captcha', $option);
// Add the action to load the plugin
add_action('plugins_loaded', 'loginizer_load_plugin');
// The function that will be called when the plugin is loaded
function loginizer_load_plugin(){
// Check if the installed version is outdated
loginizer_update_check();
$loginizer['prefix'] = !defined('SITEPAD') ? 'Loginizer ' : 'SitePad ';
$loginizer['app'] = !defined('SITEPAD') ? 'WordPress' : 'SitePad';
$loginizer['login_basename'] = !defined('SITEPAD') ? 'wp-login.php' : 'login.php';
$loginizer['wp-includes'] = !defined('SITEPAD') ? 'wp-includes' : 'site-inc';
$loginizer['ip_method'] = get_option('loginizer_ip_method');
if($loginizer['ip_method'] == 3){
$loginizer['custom_ip_method'] = get_option('loginizer_custom_ip_method');
$options = get_option('loginizer_options');
$loginizer['max_retries'] = empty($options['max_retries']) ? 3 : $options['max_retries'];
$loginizer['lockout_time'] = empty($options['lockout_time']) ? 900 : $options['lockout_time']; // 15 minutes
$loginizer['max_lockouts'] = empty($options['max_lockouts']) ? 5 : $options['max_lockouts'];
$loginizer['lockouts_extend'] = empty($options['lockouts_extend']) ? 86400 : $options['lockouts_extend']; // 24 hours
$loginizer['reset_retries'] = empty($options['reset_retries']) ? 86400 : $options['reset_retries']; // 24 hours
$loginizer['notify_email'] = empty($options['notify_email']) ? 0 : $options['notify_email'];
$loginizer['notify_email_address'] = lz_is_multisite() ? get_site_option('admin_email') : get_option('admin_email');
$loginizer['trusted_ips'] = empty($options['trusted_ips']) ? false : true;
$loginizer['blocked_screen'] = empty($options['blocked_screen']) ? false : true;
$loginizer['social_settings'] = get_option('loginizer_social_settings', []);
if(!empty($options['notify_email_address'])){
$loginizer['notify_email_address'] = $options['notify_email_address'];
$loginizer['custom_notify_email'] = 1;
// Login Success Email Notification.
$loginizer['login_mail'] = get_option('loginizer_login_mail', []);
add_action('init', 'loginizer_load_translation_vars', 0);
$loginizer['login_mail_subject'] = empty($loginizer['login_mail']['subject']) ? '' : $loginizer['login_mail']['subject'];
$loginizer['login_mail_body'] = empty($loginizer['login_mail']['body']) ? '' : $loginizer['login_mail']['body'];
// Load the blacklist and whitelist
$loginizer['blacklist'] = get_option('loginizer_blacklist', []);
$loginizer['whitelist'] = get_option('loginizer_whitelist', []);
$loginizer['2fa_whitelist'] = get_option('loginizer_2fa_whitelist');
// It should not be false
if(empty($loginizer['2fa_whitelist'])){
$loginizer['2fa_whitelist'] = array();
// When was the database cleared last time
$loginizer['last_reset'] = get_option('loginizer_last_reset');
if(!isset($loginizer['ultimate-member-active'])){
$um_is_active = in_array('ultimate-member/ultimate-member.php', apply_filters('active_plugins', get_option('active_plugins', [])));
$loginizer['ultimate-member-active'] = !empty($um_is_active) ? true : false;
if((time() - $loginizer['last_reset']) >= $loginizer['reset_retries']){
loginizer_reset_retries();
$ins_time = get_option('loginizer_ins_time');
update_option('loginizer_ins_time', $ins_time);
$loginizer['ins_time'] = $ins_time;
$loginizer['current_ip'] = lz_getip();
// Is Brute Force Disabled ?
$loginizer['disable_brute'] = get_option('loginizer_disable_brute');
if(empty($loginizer['disable_brute'])){
// Use this to verify before WP tries to login
// Is always called and is the first function to be called
//add_action('wp_authenticate', 'loginizer_wp_authenticate', 10, 2);// Not called by XML-RPC
add_filter('authenticate', 'loginizer_wp_authenticate', 10001, 3);// This one is called by xmlrpc as well as GUI
// Is called when a login attempt fails
// Hence Update our records that the login failed
add_action('wp_login_failed', 'loginizer_login_failed');
// Is called before displaying the error message so that we dont show that the username is wrong or the password
add_action('wp_login_errors', 'loginizer_error_handler', 10001, 2);
add_action('woocommerce_login_failed', 'loginizer_woocommerce_error_handler', 10001);
add_action('wp_login', 'loginizer_login_success', 10, 2);
if(!empty($loginizer['ultimate-member-active'])){
add_action('wp_login_failed', 'loginizer_ultimatemember_error_handler', 10001);
if(!empty($_COOKIE['lz_social_error']) && !empty($loginizer['social_settings']) && !loginizer_is_blacklisted()){
add_filter('wp_login_errors', 'loginizer_social_login_error_handler', 10000, 2);
// Social Login Form Actions
if(!empty($loginizer['social_settings']) && !loginizer_is_blacklisted()){
if(!empty($loginizer['social_settings']['login']['login_form'])){
add_action('login_form', 'loginizer_social_btn_login');
if((function_exists('wp_doing_ajax') && wp_doing_ajax()) || (defined( 'DOING_AJAX' ) && DOING_AJAX)){
include_once LOGINIZER_DIR . '/main/ajax.php';
include_once LOGINIZER_DIR . '/main/admin.php';
// Secuity checks for social login.
if(!empty($_GET['lz_social_provider']) && loginizer_can_login() && empty($_GET['lz_api'])){
add_action('init', 'loginizer_social_login_load');
// Should return NULL if everything is fine
function loginizer_wp_authenticate($user, $username, $password){
global $loginizer, $lz_error, $lz_cannot_login, $lz_user_pass;
if(!empty($username) && !empty($password)){
if(loginizer_is_whitelisted()){
$loginizer['ip_is_whitelisted'] = 1;
} else if (!empty($loginizer['trusted_ips'])){
// This is used by WP Activity Log
apply_filters( 'wp_login_blocked', $username );
// Shows a blocked screen
if(!empty($loginizer['blocked_screen'])){
$lz_error['trusted_ip'] = __('You are restricted from logging in as your IP is not whitelisted.', 'loginizer');
loginizer_blocked_page($lz_error);
return new WP_Error('ip_blacklisted', __('You are restricted from logging in as your IP is not whitelisted.', 'loginizer'));
if(loginizer_is_blacklisted()){
// This is used by WP Activity Log
apply_filters( 'wp_login_blocked', $username );
// Shows a blocked screen
if(!empty($loginizer['blocked_screen'])){
loginizer_blocked_page($lz_error);
return new WP_Error('ip_blacklisted', implode('', $lz_error), 'loginizer');
// Is the username blacklisted ?
if(function_exists('loginizer_user_blacklisted')){
if(loginizer_user_blacklisted($username)){
// This is used by WP Activity Log
apply_filters( 'wp_login_blocked', $username );
return new WP_Error('user_blacklisted', implode('', $lz_error), 'loginizer');
if(loginizer_can_login()){
// This is used by WP Activity Log
apply_filters( 'wp_login_blocked', $username );
// Shows a blocked screen
if(!empty($loginizer['blocked_screen'])){
loginizer_blocked_page($lz_error);
return new WP_Error('ip_blocked', implode('', $lz_error), 'loginizer');
function loginizer_can_login(){
global $wpdb, $loginizer, $lz_error;
$sel_query = $wpdb->prepare("SELECT * FROM `".$wpdb->prefix."loginizer_logs` WHERE `ip` = %s", $loginizer['current_ip']);
$result = lz_selectquery($sel_query);
if(!empty($result['count']) && ($result['count'] % $loginizer['max_retries']) == 0){
// Has he reached max lockouts ?
if($result['lockout'] >= $loginizer['max_lockouts']){
$loginizer['lockout_time'] = $loginizer['lockouts_extend'];
// Is he in the lockout time ?
if($result['time'] >= (time() - $loginizer['lockout_time'])){
$banlift = ceil((($result['time'] + $loginizer['lockout_time']) - time()) / 60);
//echo 'Current Time '.date('d/M/Y H:i:s P', time()).'<br />';
//echo 'Last attempt '.date('d/M/Y H:i:s P', $result['time']).'<br />';
//echo 'Unlock Time '.date('d/M/Y H:i:s P', $result['time'] + $loginizer['lockout_time']).'<br />';
$_time = $banlift.' '.$loginizer['msg']['minutes_err'];
$banlift = ceil($banlift / 60);
$_time = $banlift.' '.$loginizer['msg']['hours_err'];
$lz_error['ip_blocked'] = $loginizer['msg']['lockout_err'].' '.$_time;
if(!empty($loginizer['ultimate-member-active']) && class_exists('UM')){
\UM()->form()->add_error('blocked_msg', $lz_error['ip_blocked']);
function loginizer_is_blacklisted(){
global $wpdb, $loginizer, $lz_error;
$blacklist = isset($loginizer['blacklist']) ? $loginizer['blacklist'] : [];
$current_ip_inet = inet_ptoi($loginizer['current_ip']);