Skip to content

Commit a6aadf5

Browse files
Merge pull request #671 from CleanTalk/upd-net-count
Upd. Settings. Improved counting of FW nets.
2 parents 4e45b03 + 40f99ea commit a6aadf5

1 file changed

Lines changed: 49 additions & 5 deletions

File tree

inc/fw-update.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -722,12 +722,56 @@ function spbc_security_firewall_update__get_ips_count()
722722
'%t2' => SPBC_TBL_FIREWALL_DATA__IPS_V4
723723
];
724724
$query_v4 = str_replace(array_keys($data), array_values($data), $query);
725-
$data['%q'] = str_replace("%m", (string)$mask, "(%m - mask1) + (%m - mask2) + (%m - mask3) + (%m - mask4)");
726-
$data['%t1'] = SPBC_TBL_FIREWALL_DATA_V6;
727-
$data['%t2'] = SPBC_TBL_FIREWALL_DATA__IPS_V6;
728-
$query_v6 = str_replace(array_keys($data), array_values($data), $query);
729725

730-
return $wpdb->get_var($query_v4) + $wpdb->get_var($query_v6);
726+
return (int)$wpdb->get_var($query_v4) + spbc_security_firewall_update__get_ipv6_ips_count();
727+
}
728+
729+
/**
730+
* Counts IPv6 addresses covered by a single firewall table.
731+
*
732+
* Mirrors IP::convertLongIntMaskToDec() in SQL to avoid loading all rows into PHP.
733+
* Large prefixes (/0–/95) are excluded from host expansion.
734+
*
735+
* @param string $table
736+
*
737+
* @return int
738+
*/
739+
function spbc_security_firewall_update__get_ipv6_ips_count_for_table($table)
740+
{
741+
global $wpdb;
742+
743+
$allowed_tables = array(
744+
SPBC_TBL_FIREWALL_DATA_V6,
745+
SPBC_TBL_FIREWALL_DATA__IPS_V6,
746+
);
747+
if ( ! in_array($table, $allowed_tables, true) ) {
748+
return 0;
749+
}
750+
751+
$sql = 'SELECT COALESCE(SUM(
752+
CASE
753+
WHEN prefix = 128 THEN 1
754+
WHEN prefix > 0 AND prefix < 128 AND (128 - prefix) <= 32 THEN CAST(POW(2, 128 - prefix) AS UNSIGNED)
755+
ELSE 0
756+
END
757+
), 0)
758+
FROM (
759+
SELECT
760+
BIT_COUNT(mask1) + BIT_COUNT(mask2) + BIT_COUNT(mask3) + BIT_COUNT(mask4) AS prefix
761+
FROM `' . $table . '`
762+
) networks
763+
WHERE prefix >= 96 AND prefix <= 128';
764+
765+
return (int)$wpdb->get_var($sql);
766+
}
767+
768+
/**
769+
* Counts IPv6 addresses covered by the firewall database.
770+
*/
771+
function spbc_security_firewall_update__get_ipv6_ips_count()
772+
{
773+
return spbc_security_firewall_update__get_ipv6_ips_count_for_table(SPBC_TBL_FIREWALL_DATA_V6)
774+
+ spbc_security_firewall_update__get_ipv6_ips_count_for_table(SPBC_TBL_FIREWALL_DATA__IPS_V6);
731775
}
732776

733777
function spbc_security_firewall_update__is_in_progress()

0 commit comments

Comments
 (0)