2 new tools and & fix for additional IP address processing logic (#2717)

* Update Service.php

* wording

* +ipInSubnet & +humanReadableBytes

* Make PHP 8.4 happier

* Add terabytes

* remove ipInSubnet in favor of the Symfony component

* Update TwigExtensions.php

---------

Co-authored-by: Belle Aerni <belleaerni@gmail.com>
This commit is contained in:
YağızhanandBelle Aerni authored and GitHub committed 2025-05-08 11:30:08 -07:00
1 parent e85d3e6f5b
commit 8bf9a1bdec
3 files changed
+51 -18

No files matched your search

+2 -11
View File
@@ -273,18 +273,9 @@ class Box_TwigExtensions extends AbstractExtension implements InjectionAwareInte
return sprintf('%d %s ', $no, $pds[$v]);
}
public function twig_size_filter($value)
public function twig_size_filter($value): string
{
$precision = 2;
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$bytes = max($value, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= 1024 ** $pow;
return round($bytes, $precision) . ' ' . $units[$pow];
return \FOSSBilling\Tools::humanReadableBytes($value);
}
public function twig_markdown_filter(Twig\Environment $env, $value)
+16
View File
@@ -412,4 +412,20 @@ class Tools
return null;
}
/**
* Converts bytes to a human-readable format (B, KB, MB, GB and TB).
*
* @param int $bytes
* @return string
*/
public static function humanReadableBytes(int $bytes): string
{
if ($bytes < 1024) return $bytes . ' B';
if ($bytes < 1048576) return round($bytes / 1024, 2) . ' KB';
if ($bytes < 1073741824) return round($bytes / 1048576, 2) . ' MB';
if ($bytes < 1099511627776) return round($bytes / 1073741824, 2) . ' GB';
return round($bytes / 1099511627776, 2) . ' TB';
}
}
+33 -7
View File
@@ -688,7 +688,9 @@ class Service implements InjectionAwareInterface
$model->ip = $ip;
$model->hostname = $data['hostname'] ?? null;
$model->assigned_ips = $data['assigned_ips'] ?? null;
$assigned_ips = $data['assigned_ips'] ?? '';
if (!empty($assigned_ips)) $model->assigned_ips = self::processAssignedIPs($assigned_ips);
$model->active = $data['active'] ?? 1;
$model->status_url = $data['status_url'] ?? null;
$model->max_accounts = $data['max_accounts'] ?? null;
@@ -731,12 +733,7 @@ class Service implements InjectionAwareInterface
$model->hostname = $data['hostname'] ?? $model->hostname;
$assigned_ips = $data['assigned_ips'] ?? '';
if (!empty($assigned_ips)) {
$array = explode(PHP_EOL, $data['assigned_ips']);
$array = array_map(trim(...), $array);
$array = array_diff($array, ['']);
$model->assigned_ips = json_encode($array);
}
if (!empty($assigned_ips)) $model->assigned_ips = self::processAssignedIPs($assigned_ips);
$model->active = $data['active'] ?? $model->active;
$model->status_url = $data['status_url'] ?? $model->status_url;
@@ -1088,4 +1085,33 @@ class Service implements InjectionAwareInterface
return $result;
}
/**
* Post-processing for the assigned IPs.
* The data from the server management form (/admin/servicehosting/server/{id}) sends the data like this:
* assigned_ips: "10.0.0.1\n10.0.0.2\n"
* As you see, it isn't really an array, it also doesn't filter out empty lines and whitespaces at all.
*
* We can't rely on it as-is. So we need to make sure only the valid IP addresses are going inside the array.
* We'll split on any type of line break (\n, \r\n, or \r) and make sure each IP address is valid.
*
* @param string $assigned_ips Raw string from the form data (example form: /admin/servicehosting/server/{ip})
* @return string JSON encoded array of filtered valid IPs
*/
public static function processAssignedIPs(string $assigned_ips): string
{
// Split the input by any type of line break (\n, \r\n, or \r)
$array = preg_split('/\r\n|\r|\n/', $assigned_ips);
// Trim each entry and remove any empty strings
$array = array_map('trim', $array);
$array = array_filter($array, fn($ip) => $ip !== '');
// Validate that each entry is a valid IP address (works both with IPv4 and IPv6)
$array = array_filter($array, function($ip) {
return filter_var($ip, FILTER_VALIDATE_IP);
});
return json_encode(array_values($array));
}
}