fix: trim data when loading from external sources

This commit is contained in:
Rekryt
2024-11-07 17:57:57 +03:00
parent 86db85f896
commit cfb57718f6
2 changed files with 21 additions and 10 deletions

View File

@@ -120,7 +120,7 @@ final class Site {
if (isset($this->external->domains)) {
foreach ($this->external->domains as $url) {
$this->domains = SiteFactory::normalize(
array_merge($this->domains, explode("\n", file_get_contents($url)))
array_merge($this->domains, SiteFactory::trimArray(explode("\n", file_get_contents($url))))
);
}
}
@@ -128,7 +128,7 @@ final class Site {
if (isset($this->external->ip4) && $this->isUseIpv4) {
foreach ($this->external->ip4 as $url) {
$this->ip4 = SiteFactory::normalize(
array_merge($this->ip4, explode("\n", file_get_contents($url))),
array_merge($this->ip4, SiteFactory::trimArray(explode("\n", file_get_contents($url)))),
true
);
}
@@ -137,7 +137,7 @@ final class Site {
if (isset($this->external->ip6) && $this->isUseIpv6) {
foreach ($this->external->ip6 as $url) {
$this->ip6 = SiteFactory::normalize(
array_merge($this->ip6, explode("\n", file_get_contents($url))),
array_merge($this->ip6, SiteFactory::trimArray(explode("\n", file_get_contents($url)))),
true
);
}
@@ -146,7 +146,10 @@ final class Site {
if (isset($this->external->cidr4) && $this->isUseIpv4) {
foreach ($this->external->cidr4 as $url) {
$this->cidr4 = IP4Helper::minimizeSubnets(
SiteFactory::normalize(array_merge($this->cidr4, explode("\n", file_get_contents($url))), true)
SiteFactory::normalize(
array_merge($this->cidr4, SiteFactory::trimArray(explode("\n", file_get_contents($url)))),
true
)
);
}
}
@@ -155,7 +158,7 @@ final class Site {
foreach ($this->external->cidr6 as $url) {
// todo IP6Helper::minimizeSubnets
$this->cidr6 = SiteFactory::normalize(
array_merge($this->cidr6, explode("\n", file_get_contents($url))),
array_merge($this->cidr6, SiteFactory::trimArray(explode("\n", file_get_contents($url)))),
true
);
}

View File

@@ -44,35 +44,35 @@ class SiteFactory {
if (isset($external->domains)) {
foreach ($external->domains as $url) {
App::getLogger()->debug('Loading external domains from ' . $url);
$domains = array_merge($domains, explode("\n", file_get_contents($url)));
$domains = array_merge($domains, self::trimArray(explode("\n", file_get_contents($url))));
}
}
if (isset($external->ip4) && $isUseIpv4) {
foreach ($external->ip4 as $url) {
App::getLogger()->debug('Loading external ip4 from ' . $url);
$ip4 = array_merge($ip4, explode("\n", file_get_contents($url)));
$ip4 = array_merge($ip4, self::trimArray(explode("\n", file_get_contents($url))));
}
}
if (isset($external->ip6) && $isUseIpv6) {
foreach ($external->ip6 as $url) {
App::getLogger()->debug('Loading external ip6 from ' . $url);
$ip6 = array_merge($ip6, explode("\n", file_get_contents($url)));
$ip6 = array_merge($ip6, self::trimArray(explode("\n", file_get_contents($url))));
}
}
if (isset($external->cidr4) && $isUseIpv4) {
foreach ($external->cidr4 as $url) {
App::getLogger()->debug('Loading external cidr4 from ' . $url);
$cidr4 = array_merge($cidr4, explode("\n", file_get_contents($url)));
$cidr4 = array_merge($cidr4, self::trimArray(explode("\n", file_get_contents($url))));
}
}
if (isset($external->cidr6) && $isUseIpv6) {
foreach ($external->cidr6 as $url) {
App::getLogger()->debug('Loading external cidr6 from ' . $url);
$cidr6 = array_merge($cidr6, explode("\n", file_get_contents($url)));
$cidr6 = array_merge($cidr6, self::trimArray(explode("\n", file_get_contents($url))));
}
}
}
@@ -120,4 +120,12 @@ class SiteFactory {
sort($array);
return SiteFactory::normalize($array, $isIpAddresses);
}
/**
* @param array $array
* @return array
*/
public static function trimArray(array $array): array {
return array_map(trim(...), $array);
}
}