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 '',
UNIQUE KEY `ip` (`ip`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
foreach($sql as $sk => $sv){
$wpdb->query($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());
}
// Checks if we are to update ?
function loginizer_update_check(){
global $wpdb;
$sql = array();
$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);
// No update required
if($current_version == LOGINIZER_VERSION){
return true;
}
// Is it first run ?
if(empty($current_version)){
// Reinstall
loginizer_activation();
// Trick the following if conditions to not run
$version = (int) str_replace('.', '', LOGINIZER_VERSION);
}
// Is it less than 1.0.1 ?
if($version < 101){
// 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 = array();
$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){
$wpdb->query($sv);
}
// Delete option
delete_option('lz_version');
// Reinstall
loginizer_activation();
// 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){
$wpdb->query("INSERT INTO ".$wpdb->prefix."loginizer_logs SET `username` = '".$fv['username']."', `time` = '".$fv['time']."', `count` = '".$fv['count']."', `lockout` = '".$fv['lockout']."', `ip` = '".$fv['ip']."';");
}
}
// 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']);
continue;
}
$old_option[str_replace('lz_', '', $ov['option_name'])] = $ov['option_value'];
}
// Save the options
update_option('loginizer_options', $old_option);
}
// Update the existing iprange to new structure
if(is_array($lz_iprange)){
$old_blacklist = array();
$old_whitelist = array();
$bid = 1;
$wid = 1;
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']);
$bid = $bid + 1;
}
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']);
$wid = $wid + 1;
}
}
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 ?
if($version < 139){
$wpdb->query("ALTER TABLE ".$wpdb->prefix."loginizer_logs ADD `url` VARCHAR(255) NOT NULL DEFAULT '' AFTER `ip`;");
}
// Save the new Version
update_option('loginizer_version', LOGINIZER_VERSION);
// 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(){
global $loginizer;
// Check if the installed version is outdated
loginizer_update_check();
// Set the array
$loginizer = array();
$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';
// The IP Method to use
$loginizer['ip_method'] = get_option('loginizer_ip_method');
if($loginizer['ip_method'] == 3){
$loginizer['custom_ip_method'] = get_option('loginizer_custom_ip_method');
}
// Load settings
$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'];
// Default messages
$loginizer['d_msg']['inv_userpass'] = 'Incorrect Username or Password';
$loginizer['d_msg']['ip_blacklisted'] = 'Your IP has been blacklisted';
// Message Strings
$loginizer['msg'] = get_option('loginizer_msg');
foreach($loginizer['d_msg'] as $lk => $lv){
if(empty($loginizer['msg'][$lk])){
$loginizer['msg'][$lk] = $loginizer['d_msg'][$lk];
}
}
// Load the blacklist and whitelist
$loginizer['blacklist'] = get_option('loginizer_blacklist');
$loginizer['whitelist'] = get_option('loginizer_whitelist');
// When was the database cleared last time
$loginizer['last_reset'] = get_option('loginizer_last_reset');
//print_r($loginizer);
// Clear retries
if((time() - $loginizer['last_reset']) >= $loginizer['reset_retries']){
loginizer_reset_retries();
}
$ins_time = get_option('loginizer_ins_time');
if(empty($ins_time)){
$ins_time = time();
update_option('loginizer_ins_time', $ins_time);
}
$loginizer['ins_time'] = $ins_time;
// Set the current IP
$loginizer['current_ip'] = lz_getip();
// Is Brute Force Disabled ?
$loginizer['disable_brute'] = get_option('loginizer_disable_brute');
// Filters and actions
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
// Update Error message
add_action('wp_login_errors', 'loginizer_error_handler', 10001, 2);
add_action('woocommerce_login_failed', 'loginizer_woocommerce_error_handler', 10001);
}
// Is the premium features there ?
if(file_exists(LOGINIZER_DIR.'/premium.php')){
// Include the file
include_once(LOGINIZER_DIR.'/premium.php');
loginizer_security_init();
// Its the free version
}else{
// The promo time
$loginizer['promo_time'] = get_option('loginizer_promo_time');
if(empty($loginizer['promo_time'])){
$loginizer['promo_time'] = time();
update_option('loginizer_promo_time', $loginizer['promo_time']);
}
// Are we to show the loginizer promo
if(!empty($loginizer['promo_time']) && $loginizer['promo_time'] > 0 && $loginizer['promo_time'] < (time() - (30*24*3600))){
add_action('admin_notices', 'loginizer_promo');
}
// Are we to disable the promo
if(isset($_GET['loginizer_promo']) && (int)$_GET['loginizer_promo'] == 0){
update_option('loginizer_promo_time', (0 - time()) );
die('DONE');
}
}
}
// Show the promo
function loginizer_promo(){
echo '
';
}
// 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)){
$lz_user_pass = 1;
}
// Are you whitelisted ?
if(loginizer_is_whitelisted()){
$loginizer['ip_is_whitelisted'] = 1;
return $user;
}
// Are you blacklisted ?
if(loginizer_is_blacklisted()){
$lz_cannot_login = 1;
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)){
$lz_cannot_login = 1;
return new WP_Error('user_blacklisted', implode('', $lz_error), 'loginizer');
}
}
if(loginizer_can_login()){
return $user;
}
$lz_cannot_login = 1;
return new WP_Error('ip_blocked', implode('', $lz_error), 'loginizer');
}
function loginizer_can_login(){
global $wpdb, $loginizer, $lz_error;
// Get the logs
$result = lz_selectquery("SELECT * FROM `".$wpdb->prefix."loginizer_logs` WHERE `ip` = '".$loginizer['current_ip']."';");
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('m/d/Y H:i:s', time()).' ';
//echo 'Last attempt '.date('m/d/Y H:i:s', $result['time']).' ';
//echo 'Unlock Time '.date('m/d/Y H:i:s', $result['time'] + $loginizer['lockout_time']).' ';
$_time = $banlift.' minute(s)';
if($banlift > 60){
$banlift = ceil($banlift / 60);
$_time = $banlift.' hour(s)';
}
$lz_error['ip_blocked'] = 'You have exceeded maximum login retries Please try after '.$_time;
return false;
}
}
return true;
}
function loginizer_is_blacklisted(){
global $wpdb, $loginizer, $lz_error;
$blacklist = $loginizer['blacklist'];
foreach($blacklist as $k => $v){
// Is the IP in the blacklist ?
if(inet_ptoi($v['start']) <= inet_ptoi($loginizer['current_ip']) && inet_ptoi($loginizer['current_ip']) <= inet_ptoi($v['end'])){
$result = 1;
break;
}
// Is it in a wider range ?
if(inet_ptoi($v['start']) >= 0 && inet_ptoi($v['end']) < 0){
// Since the end of the RANGE (i.e. current IP range) is beyond the +ve value of inet_ptoi,
// if the current IP is <= than the start of the range, it is within the range
// OR
// if the current IP is <= than the end of the range, it is within the range
if(inet_ptoi($v['start']) <= inet_ptoi($loginizer['current_ip'])
|| inet_ptoi($loginizer['current_ip']) <= inet_ptoi($v['end'])){
$result = 1;
break;
}
}
}
// You are blacklisted
if(!empty($result)){
$lz_error['ip_blacklisted'] = $loginizer['msg']['ip_blacklisted'];
return true;
}
return false;
}
function loginizer_is_whitelisted(){
global $wpdb, $loginizer, $lz_error;
$whitelist = $loginizer['whitelist'];
foreach($whitelist as $k => $v){
// Is the IP in the blacklist ?
if(inet_ptoi($v['start']) <= inet_ptoi($loginizer['current_ip']) && inet_ptoi($loginizer['current_ip']) <= inet_ptoi($v['end'])){
$result = 1;
break;
}
// Is it in a wider range ?
if(inet_ptoi($v['start']) >= 0 && inet_ptoi($v['end']) < 0){
// Since the end of the RANGE (i.e. current IP range) is beyond the +ve value of inet_ptoi,
// if the current IP is <= than the start of the range, it is within the range
// OR
// if the current IP is <= than the end of the range, it is within the range
if(inet_ptoi($v['start']) <= inet_ptoi($loginizer['current_ip'])
|| inet_ptoi($loginizer['current_ip']) <= inet_ptoi($v['end'])){
$result = 1;
break;
}
}
}
// You are whitelisted
if(!empty($result)){
return true;
}
return false;
}
// When the login fails, then this is called
// We need to update the database
function loginizer_login_failed($username){
global $wpdb, $loginizer, $lz_cannot_login;
if(empty($lz_cannot_login) && empty($loginizer['ip_is_whitelisted']) && empty($loginizer['no_loginizer_logs'])){
$url = @addslashes((!empty($_SERVER['HTTPS']) ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$url = esc_url($url);
$result = lz_selectquery("SELECT * FROM `".$wpdb->prefix."loginizer_logs` WHERE `ip` = '".$loginizer['current_ip']."';");
if(!empty($result)){
$lockout = floor((($result['count']+1) / $loginizer['max_retries']));
$sresult = $wpdb->query("UPDATE `".$wpdb->prefix."loginizer_logs` SET `username` = '".$username."', `time` = '".time()."', `count` = `count`+1, `lockout` = '".$lockout."', `url` = '".$url."' WHERE `ip` = '".$loginizer['current_ip']."';");
// Do we need to email admin ?
if(!empty($loginizer['notify_email']) && $lockout >= $loginizer['notify_email']){
$sitename = lz_is_multisite() ? get_site_option('site_name') : get_option('blogname');
$mail = array();
$mail['to'] = lz_is_multisite() ? get_site_option('admin_email') : get_option('admin_email');
$mail['subject'] = 'Failed Login Attempts from IP '.$loginizer['current_ip'].' ('.$sitename.')';
$mail['message'] = 'Hi,
'.($result['count']+1).' failed login attempts and '.$lockout.' lockout(s) from IP '.$loginizer['current_ip'].'
Last Login Attempt : '.date('d/m/Y H:i:s', time()).'
Last User Attempt : '.$username.'
IP has been blocked until : '.date('d/m/Y H:i:s', time() + $loginizer['lockout_time']).'
Regards,
Loginizer';
@wp_mail($mail['to'], $mail['subject'], $mail['message']);
}
}else{
$insert = $wpdb->query("INSERT INTO `".$wpdb->prefix."loginizer_logs` SET `username` = '".$username."', `time` = '".time()."', `count` = '1', `ip` = '".$loginizer['current_ip']."', `lockout` = '0', `url` = '".$url."';");
}
// We need to add one as this is a failed attempt as well
$result['count'] = $result['count'] + 1;
$loginizer['retries_left'] = ($loginizer['max_retries'] - ($result['count'] % $loginizer['max_retries']));
$loginizer['retries_left'] = $loginizer['retries_left'] == $loginizer['max_retries'] ? 0 : $loginizer['retries_left'];
}
}
// Handles the error of the password not being there
function loginizer_error_handler($errors, $redirect_to){
global $wpdb, $loginizer, $lz_user_pass, $lz_cannot_login;
//echo 'loginizer_error_handler :';print_r($errors->errors);echo ' ';
// Remove the empty password error
if(is_wp_error($errors)){
$codes = $errors->get_error_codes();
foreach($codes as $k => $v){
if($v == 'invalid_username' || $v == 'incorrect_password'){
$show_error = 1;
}
}
$errors->remove('invalid_username');
$errors->remove('incorrect_password');
}
// Add the error
if(!empty($lz_user_pass) && !empty($show_error) && empty($lz_cannot_login)){
$errors->add('invalid_userpass', 'ERROR: ' . $loginizer['msg']['inv_userpass']);
}
// Add the number of retires left as well
if(count($errors->get_error_codes()) > 0 && isset($loginizer['retries_left'])){
$errors->add('retries_left', loginizer_retries_left());
}
return $errors;
}
// Handles the error of the password not being there
function loginizer_woocommerce_error_handler(){
global $wpdb, $loginizer, $lz_user_pass, $lz_cannot_login;
if(function_exists('wc_add_notice')){
wc_add_notice( loginizer_retries_left(), 'error' );
}
}
// Returns a string with the number of retries left
function loginizer_retries_left(){
global $wpdb, $loginizer, $lz_user_pass, $lz_cannot_login;
// If we are to show the number of retries left
if(isset($loginizer['retries_left'])){
return ''.$loginizer['retries_left'].' attempt(s) left';
}
}
function loginizer_reset_retries(){
global $wpdb, $loginizer;
$deltime = time() - $loginizer['reset_retries'];
$result = $wpdb->query("DELETE FROM `".$wpdb->prefix."loginizer_logs` WHERE `time` <= '".$deltime."';");
update_option('loginizer_last_reset', time());
}
add_filter("plugin_action_links_$plugin_loginizer", 'loginizer_plugin_action_links');
// Add settings link on plugin page
function loginizer_plugin_action_links($links) {
if(!defined('LOGINIZER_PREMIUM')){
$links[] = ''._x('Upgrade', 'Plugin action link label.', 'loginizer').'';
}
$settings_link = 'Settings';
array_unshift($links, $settings_link);
return $links;
}
add_action('admin_menu', 'loginizer_admin_menu');
// Shows the admin menu of Loginizer
function loginizer_admin_menu() {
global $wp_version, $loginizer;
if(!defined('SITEPAD')){
// Add the menu page
add_menu_page(__('Loginizer Dashboard'), __('Loginizer Security'), 'activate_plugins', 'loginizer', 'loginizer_page_dashboard');
// Dashboard
add_submenu_page('loginizer', __('Loginizer Dashboard'), __('Dashboard'), 'activate_plugins', 'loginizer', 'loginizer_page_dashboard');
}else{
// Add the menu page
add_menu_page(__('Security'), __('Security'), 'activate_plugins', 'loginizer', 'loginizer_page_security', 'dashicons-shield', 85);
// Rename Login
add_submenu_page('loginizer', __('Security Settings'), __('Rename Login'), 'activate_plugins', 'loginizer', 'loginizer_page_security');
}
// Brute Force
add_submenu_page('loginizer', __('Brute Force Settings'), __('Brute Force'), 'activate_plugins', 'loginizer_brute_force', 'loginizer_page_brute_force');
if(defined('LOGINIZER_PREMIUM')){
// PasswordLess
add_submenu_page('loginizer', __($loginizer['prefix'].'PasswordLess Settings'), __('PasswordLess'), 'activate_plugins', 'loginizer_passwordless', 'loginizer_page_passwordless');
// Security Settings
if(!defined('SITEPAD')){
// Two Factor Auth
add_submenu_page('loginizer', __($loginizer['prefix'].' Two Factor Authentication'), __('Two Factor Auth'), 'activate_plugins', 'loginizer_2fa', 'loginizer_page_2fa');
}
// reCaptcha
add_submenu_page('loginizer', __($loginizer['prefix'].'reCAPTCHA Settings'), __('reCAPTCHA'), 'activate_plugins', 'loginizer_recaptcha', 'loginizer_page_recaptcha');
// Security Settings
if(!defined('SITEPAD')){
// Security Settings
add_submenu_page('loginizer', __($loginizer['prefix'].'Security Settings'), __('Security Settings'), 'activate_plugins', 'loginizer_security', 'loginizer_page_security');
// File Checksums
add_submenu_page('loginizer', __('Loginizer File Checksums'), __('File Checksums'), 'activate_plugins', 'loginizer_checksums', 'loginizer_page_checksums');
}
}elseif(!defined('LOGINIZER_PREMIUM') && !empty($loginizer['ins_time']) && $loginizer['ins_time'] < (time() - (30*24*3600))){
// Go Pro link
add_submenu_page('loginizer', __('Loginizer Go Pro'), __('Go Pro'), 'activate_plugins', LOGINIZER_PRO_URL);
}
}
// The Loginizer Admin Options Page
function loginizer_page_header($title = 'Loginizer'){
?>
'
. __('The settings were saved successfully', 'loginizer')
. '
';
}
}
// Delete a Blackist IP range
if(isset($_POST['bdelid'])){
$delid = (int) lz_optreq('bdelid');
// Unset and save
$blacklist = $loginizer['blacklist'];
unset($blacklist[$delid]);
update_option('loginizer_blacklist', $blacklist);
echo '
'
. __('The Blacklist IP range has been deleted successfully', 'loginizer')
. '
';
}
// Delete all Blackist IP ranges
if(isset($_POST['del_all_blacklist'])){
// Unset and save
update_option('loginizer_blacklist', array());
echo '
'
. __('The Blacklist IP range(s) have been cleared successfully', 'loginizer')
. '
';
}
// Delete a Whitelist IP range
if(isset($_POST['delid'])){
$delid = (int) lz_optreq('delid');
// Unset and save
$whitelist = $loginizer['whitelist'];
unset($whitelist[$delid]);
update_option('loginizer_whitelist', $whitelist);
echo '
'
. __('The Whitelist IP range has been deleted successfully', 'loginizer')
. '
';
}
// Delete all Blackist IP ranges
if(isset($_POST['del_all_whitelist'])){
// Unset and save
update_option('loginizer_whitelist', array());
echo '
'
. __('The Whitelist IP range(s) have been cleared successfully', 'loginizer')
. '
';
}
// Reset All Logs
if(isset($_POST['lz_reset_all_ip'])){
$result = $wpdb->query("DELETE FROM `".$wpdb->prefix."loginizer_logs`
WHERE `time` > 0");
echo '
'
. __('All the IP Logs have been cleared', 'loginizer')
. '
';
}
// Reset Logs
if(isset($_POST['lz_reset_ips']) && is_array($_POST['lz_reset_ips'])){
$ips = $_POST['lz_reset_ips'];
foreach($ips as $ip){
if(!lz_valid_ip($ip)){
$error[] = 'The IP - '.$ip.' is invalid !';
}
}
if(count($ips) < 1){
$error[] = 'There are no IPs submitted';
}
// Should we start deleting logs
if(empty($error)){
$result = $wpdb->query("DELETE FROM `".$wpdb->prefix."loginizer_logs`
WHERE `ip` IN ('".implode("', '", $ips)."')");
if(empty($error)){
echo '
'
. __('The selected IP Logs have been reset', 'loginizer')
. '
';
}
}
if(!empty($error)){
lz_report_error($error);echo ' ';
}
}
if(isset($_POST['blacklist_iprange'])){
$start_ip = lz_optpost('start_ip');
$end_ip = lz_optpost('end_ip');
if(empty($start_ip)){
$error[] = 'Please enter the Start IP';
}
// If no end IP we consider only 1 IP
if(empty($end_ip)){
$end_ip = $start_ip;
}
if(!lz_valid_ip($start_ip)){
$error[] = 'Please provide a valid start IP';
}
if(!lz_valid_ip($end_ip)){
$error[] = 'Please provide a valid end IP';
}
// Regular ranges will work
if(inet_ptoi($start_ip) > inet_ptoi($end_ip)){
// BUT, if 0.0.0.1 - 255.255.255.255 is given, it will not work
if(inet_ptoi($start_ip) >= 0 && inet_ptoi($end_ip) < 0){
// This is right
}else{
$error[] = 'The End IP cannot be smaller than the Start IP';
}
}
if(empty($error)){
$blacklist = $loginizer['blacklist'];
foreach($blacklist as $k => $v){
// This is to check if there is any other range exists with the same Start or End IP
if(( inet_ptoi($start_ip) <= inet_ptoi($v['start']) && inet_ptoi($v['start']) <= inet_ptoi($end_ip) )
|| ( inet_ptoi($start_ip) <= inet_ptoi($v['end']) && inet_ptoi($v['end']) <= inet_ptoi($end_ip) )
){
$error[] = 'The Start IP or End IP submitted conflicts with an existing IP range !';
break;
}
// This is to check if there is any other range exists with the same Start IP
if(inet_ptoi($v['start']) <= inet_ptoi($start_ip) && inet_ptoi($start_ip) <= inet_ptoi($v['end'])){
$error[] = 'The Start IP is present in an existing range !';
break;
}
// This is to check if there is any other range exists with the same End IP
if(inet_ptoi($v['start']) <= inet_ptoi($end_ip) && inet_ptoi($end_ip) <= inet_ptoi($v['end'])){
$error[] = 'The End IP is present in an existing range!';
break;
}
}
$newid = ( empty($blacklist) ? 0 : max(array_keys($blacklist)) ) + 1;
if(empty($error)){
$blacklist[$newid] = array();
$blacklist[$newid]['start'] = $start_ip;
$blacklist[$newid]['end'] = $end_ip;
$blacklist[$newid]['time'] = time();
update_option('loginizer_blacklist', $blacklist);
echo '
'
. __('Blacklist IP range added successfully', 'loginizer')
. '
';
}
}
if(!empty($error)){
lz_report_error($error);echo ' ';
}
}
if(isset($_POST['whitelist_iprange'])){
$start_ip = lz_optpost('start_ip_w');
$end_ip = lz_optpost('end_ip_w');
if(empty($start_ip)){
$error[] = 'Please enter the Start IP';
}
// If no end IP we consider only 1 IP
if(empty($end_ip)){
$end_ip = $start_ip;
}
if(!lz_valid_ip($start_ip)){
$error[] = 'Please provide a valid start IP';
}
if(!lz_valid_ip($end_ip)){
$error[] = 'Please provide a valid end IP';
}
if(inet_ptoi($start_ip) > inet_ptoi($end_ip)){
// BUT, if 0.0.0.1 - 255.255.255.255 is given, it will not work
if(inet_ptoi($start_ip) >= 0 && inet_ptoi($end_ip) < 0){
// This is right
}else{
$error[] = 'The End IP cannot be smaller than the Start IP';
}
}
if(empty($error)){
$whitelist = $loginizer['whitelist'];
foreach($whitelist as $k => $v){
// This is to check if there is any other range exists with the same Start or End IP
if(( inet_ptoi($start_ip) <= inet_ptoi($v['start']) && inet_ptoi($v['start']) <= inet_ptoi($end_ip) )
|| ( inet_ptoi($start_ip) <= inet_ptoi($v['end']) && inet_ptoi($v['end']) <= inet_ptoi($end_ip) )
){
$error[] = 'The Start IP or End IP submitted conflicts with an existing IP range !';
break;
}
// This is to check if there is any other range exists with the same Start IP
if(inet_ptoi($v['start']) <= inet_ptoi($start_ip) && inet_ptoi($start_ip) <= inet_ptoi($v['end'])){
$error[] = 'The Start IP is present in an existing range !';
break;
}
// This is to check if there is any other range exists with the same End IP
if(inet_ptoi($v['start']) <= inet_ptoi($end_ip) && inet_ptoi($end_ip) <= inet_ptoi($v['end'])){
$error[] = 'The End IP is present in an existing range!';
break;
}
}
$newid = ( empty($whitelist) ? 0 : max(array_keys($whitelist)) ) + 1;
if(empty($error)){
$whitelist[$newid] = array();
$whitelist[$newid]['start'] = $start_ip;
$whitelist[$newid]['end'] = $end_ip;
$whitelist[$newid]['time'] = time();
update_option('loginizer_whitelist', $whitelist);
echo '
'
. __('Whitelist IP range added successfully', 'loginizer')
. '
';
}
}
if(!empty($error)){
lz_report_error($error);echo ' ';
}
}
// Save the messages
if(isset($_POST['save_err_msgs_lz'])){
$msgs['inv_userpass'] = lz_optpost('msg_inv_userpass');
$msgs['ip_blacklisted'] = lz_optpost('msg_ip_blacklisted');
// Update them
update_option('loginizer_msg', $msgs);
echo '
'
. __('Error messages were saved successfully', 'loginizer')
. '
';
}
// Count the Results
$tmp = lz_selectquery("SELECT COUNT(*) AS num FROM `".$wpdb->prefix."loginizer_logs`");
//print_r($tmp);
// Which Page is it
$lz_env['res_len'] = 10;
$lz_env['cur_page'] = lz_get_page('lzpage', $lz_env['res_len']);
$lz_env['num_res'] = $tmp['num'];
$lz_env['max_page'] = ceil($lz_env['num_res'] / $lz_env['res_len']);
// Get the logs
$result = lz_selectquery("SELECT * FROM `".$wpdb->prefix."loginizer_logs`
ORDER BY `time` DESC
LIMIT ".$lz_env['cur_page'].", ".$lz_env['res_len']."", 1);
//print_r($result);
$lz_env['cur_page'] = ($lz_env['cur_page'] / $lz_env['res_len']) + 1;
$lz_env['cur_page'] = $lz_env['cur_page'] < 1 ? 1 : $lz_env['cur_page'];
$lz_env['next_page'] = ($lz_env['cur_page'] + 1) > $lz_env['max_page'] ? $lz_env['max_page'] : ($lz_env['cur_page'] + 1);
$lz_env['prev_page'] = ($lz_env['cur_page'] - 1) < 1 ? 1 : ($lz_env['cur_page'] - 1);
// Reload the settings
$loginizer['blacklist'] = get_option('loginizer_blacklist');
$loginizer['whitelist'] = get_option('loginizer_whitelist');
$saved_msgs = get_option('loginizer_msg');
?>