From 8bf9a1bdecf2f8b3cbf355de247dcfb0e6b2271c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ya=C4=9F=C4=B1zhan?= Date: Thu, 8 May 2025 21:30:08 +0300 Subject: [PATCH] 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 --- src/library/Box/TwigExtensions.php | 13 ++------- src/library/FOSSBilling/Tools.php | 16 +++++++++++ src/modules/Servicehosting/Service.php | 40 +++++++++++++++++++++----- 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/library/Box/TwigExtensions.php b/src/library/Box/TwigExtensions.php index d3f7de7f7..b6bf65006 100644 --- a/src/library/Box/TwigExtensions.php +++ b/src/library/Box/TwigExtensions.php @@ -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) diff --git a/src/library/FOSSBilling/Tools.php b/src/library/FOSSBilling/Tools.php index e2bc3dc28..71241ca3c 100644 --- a/src/library/FOSSBilling/Tools.php +++ b/src/library/FOSSBilling/Tools.php @@ -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'; + } } diff --git a/src/modules/Servicehosting/Service.php b/src/modules/Servicehosting/Service.php index 7e3b0bf7c..e4b94ce58 100644 --- a/src/modules/Servicehosting/Service.php +++ b/src/modules/Servicehosting/Service.php @@ -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)); + } }