Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> NukeSentinel(tm) v2.6.x
Author Message
rovshan
Hangin' Around



Joined: Nov 26, 2005
Posts: 39

PostPosted: Tue Apr 23, 2019 10:00 am Reply with quote

Here is what I found in:

1. RavenNuke-2.5.1 (not php7) distribution => nukesentinel.php =>
// Clearing of expired blocks
// CAUTION: This function can slow your sites load time
$clearedtime = strtotime(date('Y-m-d 23:59:59', $nsnst_const['ban_time']));
$cleartime = strtotime(date('Y-m-d 23:59:59', $nsnst_const['ban_time'])) - 86400;
if( $ab_config['self_expire'] == 1 AND $ab_config['blocked_clear'] < $cleartime) {

does not found this value $ab_config['blocked_clear'] in config table => even not in INSTALL SQL file // seems missed

BUT....

2. RavenNuke-master (php7) distribution => nukesentinel.php =>

// Clearing of expired blocks
// CAUTION: This function can slow your sites load time
$clearedtime = strtotime(date('Y-m-d 23:59:59', $nsnst_const['ban_time']));
$cleartime = strtotime(date('Y-m-d 23:59:59', $nsnst_const['ban_time'])) - 86400;
if( $ab_config['self_expire'] == 1 AND $clearedtime < $cleartime) {

Which one is correct ?!

I just remarked that Blocked IPs for limited time (1,2 days) still in DB... need to be AUTO removed...
 
View user's profile Send private message
neralex
Site Admin



Joined: Aug 22, 2007
Posts: 1772

PostPosted: Tue Apr 23, 2019 10:28 am Reply with quote

That is a very good question, because I found the same strange situation while patching it for php7 and I fixed only the PHP-error, which was caused by the undefined index 'blocked_clear'. Maybe it was an option of prior versions. So I replaced only $ab_config['blocked_clear'] with $clearedtime in the if-statemanet, which you mentioned.

But the db-field: 'self_expire' exists in the _nsnst_config db-table. It is set to '0' per default. You can try to set it from '0' to '1' and see what happend. I never used this feature - feel free to test it and let me know when you get php-errors.

Edit: I tested again and maybe I made a mistake in the if-statement and this is right way because $cleartime is smaller than $clearedtime:

php Code:
if($cleartime < $clearedtime) {

_________________
Github: RavenNuke 
View user's profile Send private message
rovshan







PostPosted: Wed Apr 24, 2019 4:04 pm Reply with quote

Here is how I fixed it :

1. Added $result = $db->sql_query("INSERT INTO `".$prefix."_nsnst_config` VALUES ('blocked_clear', '0')");

2. Modified code in // Clearing of expired blocks part :
php Code:


// Clearing of expired blocks
// CAUTION: This function can slow your sites load time
$clearedtime = strtotime(date('Y-m-d 23:59:59', $nsnst_const['ban_time']));
$cleartime = strtotime(date('Y-m-d 23:59:59', $nsnst_const['ban_time'])) - 86400;

if( $ab_config['self_expire'] == 1 AND $ab_config['blocked_clear'] < $cleartime) {

// check if minimum one blocker configured for save in htaccess file
$htnum = $db->sql_numrows($db->sql_query('SELECT * FROM `' . $prefix . '_nsnst_blockers` WHERE `htaccess`!="0" '));

$clearresult = $db->sql_query('SELECT * FROM `' . $prefix. '_nsnst_blocked_ips` WHERE (`expires` < "' . $clearedtime . '" AND `expires`!="0")');
while($clearblock = $db->sql_fetchrow($clearresult)) {

$a_ip = $clearblock['ip_addr'] ;
if(!empty($ab_config['htaccess_path']) AND $htnum > 0 ) {
$ipfile = file($ab_config['htaccess_path']);
$ipfile = implode('', $ipfile);
$i = 1;
while ($i <= 3) {
$tip = substr($clearblock['ip_addr'], -2);
if($tip == '.*') { $clearblock['ip_addr'] = substr($clearblock['ip_addr'], 0, -2); }
$i++;
}
$testip = 'deny from ' . $clearblock['ip_addr'] . "\n";
$ipfile = str_replace($testip, '', $ipfile);
$doit = @fopen($ab_config['htaccess_path'], 'w');
@fwrite($doit, $ipfile);
@fclose($doit);
}

$db->sql_query('DELETE FROM `' . $prefix . '_nsnst_blocked_ips` WHERE `ip_addr`="' . $a_ip . '"');
$db->sql_query('OPTIMIZE TABLE `' . $prefix . '_nsnst_blocked_ips`');
}
$clearresult = $db->sql_query('SELECT * FROM `' . $prefix . '_nsnst_blocked_ranges` WHERE (`expires`<"' . $clearedtime . '" AND `expires`!="0")');
while($clearblock = $db->sql_fetchrow($clearresult)) {
$old_masscidr = ABGetCIDRs($clearblock['ip_lo'], $clearblock['ip_hi']);
if(!empty($ab_config['htaccess_path']) AND $htnum > 0) {
$old_masscidr = explode('||', $old_masscidr);
for ($i=0, $maxi=sizeof($old_masscidr); $i < $maxi; $i++) {
if(!empty($old_masscidr[$i])) {
$old_masscidr[$i] = 'deny from ' . $old_masscidr[$i] . "\n";
}
}
$ipfile = file($ab_config['htaccess_path']);
$ipfile = implode('', $ipfile);
$ipfile = str_replace($old_masscidr, '', $ipfile);
$ipfile = $ipfile;
$doit = @fopen($ab_config['htaccess_path'], 'w');
@fwrite($doit, $ipfile);
@fclose($doit);
}
$db->sql_query('DELETE FROM `' . $prefix . '_nsnst_blocked_ranges` WHERE `ip_lo`="' . $clearblock['ip_lo'] . '" AND `ip_hi`="' . $clearblock['ip_hi'] . '"');
$db->sql_query('OPTIMIZE TABLE `' . $prefix . '_nsnst_blocked_ranges`');
}
$db->sql_query('UPDATE `' . $prefix . '_nsnst_config` SET `config_value`="' . $clearedtime . '" WHERE `config_name`="blocked_clear"');
}


I am not sure that OPTIMIZE query should be in loop function ?!
 
neralex







PostPosted: Thu Apr 25, 2019 11:57 am Reply with quote

rovshan, let me say thank you! Wink

After checking the installation-files, I found the entry, which is adding the field blocked_clear into the _nsnst_config table inside the rndb_upgrade_nukesentinel.php. I never noticed it but I added this field also into the ns_core.sql, which is used while a fresh installation and I added this into the database-upgrade function of the rndb_upgrade.php. So there are existing now two ways to add this field into the config-table of NukeSentinel, when RN is already installed.

My suggestion for your question about the placing of the OPTIMIZE-queries: You are right, to let run this query after each record could be end in a very high usage. I would count the records inside the while-loops and if the count of records are > 0, then I would run the query only once outside of the while-loops like this:

php Code:
// Clearing of expired blocks

// CAUTION: This function can slow your sites load time
$clearedtime = strtotime(date('Y-m-d 23:59:59', $nsnst_const['ban_time']));
$cleartime = strtotime(date('Y-m-d 23:59:59', $nsnst_const['ban_time'])) - 86400;
if( $ab_config['self_expire'] == 1 AND $ab_config['blocked_clear'] < $clearedtime) {

// check if minimum one blocker configured for save in htaccess file
$htnum = $db->sql_numrows($db->sql_query('SELECT * FROM `' . $prefix . '_nsnst_blockers` WHERE `htaccess`!="0" '));

// if the value are 0, there is no need to optimize the tables
$optimize_blocked_ips = 0;
$optimize_blocked_ranges = 0;

$clearresult = $db->sql_query('SELECT * FROM `' . $prefix. '_nsnst_blocked_ips` WHERE (`expires` < "' . $clearedtime . '" AND `expires`!="0")');
while($clearblock = $db->sql_fetchrow($clearresult)) {
if(!empty($ab_config['htaccess_path']) AND $htnum > 0) {
$ipfile = file($ab_config['htaccess_path']);
$ipfile = implode('', $ipfile);
$i = 1;
while ($i <= 3) {
$tip = substr($clearblock['ip_addr'], -2);
if($tip == '.*') { $clearblock['ip_addr'] = substr($clearblock['ip_addr'], 0, -2); }
$i++;
}
$testip = 'deny from ' . $clearblock['ip_addr'] . "\n";
$ipfile = str_replace($testip, '', $ipfile);
$doit = @fopen($ab_config['htaccess_path'], 'w');
@fwrite($doit, $ipfile);
@fclose($doit);
// count the records, to the check if the table should be optimized
$optimize_blocked_ips++;
}
$db->sql_query('DELETE FROM `' . $prefix . '_nsnst_blocked_ips` WHERE `ip_addr`="' . $clearblock['ip_addr'] . '"');
}
if ($optimize_blocked_ips > 0) {
$db->sql_query('OPTIMIZE TABLE `' . $prefix . '_nsnst_blocked_ips`');
}

$clearresult = $db->sql_query('SELECT * FROM `' . $prefix . '_nsnst_blocked_ranges` WHERE (`expires`<"' . $clearedtime . '" AND `expires`!="0")');
while($clearblock = $db->sql_fetchrow($clearresult)) {
$old_masscidr = ABGetCIDRs($clearblock['ip_lo'], $clearblock['ip_hi']);
if(!empty($ab_config['htaccess_path']) AND $htnum > 0) {
$old_masscidr = explode('||', $old_masscidr);
for ($i=0, $maxi=sizeof($old_masscidr); $i < $maxi; $i++) {
if(!empty($old_masscidr[$i])) {
$old_masscidr[$i] = 'deny from ' . $old_masscidr[$i] . "\n";
}
}
$ipfile = file($ab_config['htaccess_path']);
$ipfile = implode('', $ipfile);
$ipfile = str_replace($old_masscidr, '', $ipfile);
$ipfile = $ipfile;
$doit = @fopen($ab_config['htaccess_path'], 'w');
@fwrite($doit, $ipfile);
@fclose($doit);
// count the records, to the check if the table should be optimized
$optimize_blocked_ranges++;
}
$db->sql_query('DELETE FROM `' . $prefix . '_nsnst_blocked_ranges` WHERE `ip_lo`="' . $clearblock['ip_lo'] . '" AND `ip_hi`="' . $clearblock['ip_hi'] . '"');
}
if ($optimize_blocked_ranges > 0) {
$db->sql_query('OPTIMIZE TABLE `' . $prefix . '_nsnst_blocked_ranges`');
}
$db->sql_query('UPDATE `' . $prefix . '_nsnst_config` SET `config_value`="' . $clearedtime . '" WHERE `config_name`="blocked_clear"');
}


Added to the Github-repo: [ Only registered users can see links on this board! Get registered or login! ]

Any suggestion?
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> NukeSentinel(tm) v2.6.x

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©