mirror of
https://github.com/FOSSBilling/FOSSBilling.git
synced 2026-12-02 21:30:16 +09:00
Apply PHP Code Styling & Quality Changes (#2727)
Co-authored-by: FOSSBilling Bot <fossbilling-bot>
This commit is contained in:
1 parent
2a1561fa5d
commit
a3f51f1080
3 files changed
+26
-18
No files matched your search
@@ -275,7 +275,7 @@ class Box_TwigExtensions extends AbstractExtension implements InjectionAwareInte
|
||||
|
||||
public function twig_size_filter($value): string
|
||||
{
|
||||
return \FOSSBilling\Tools::humanReadableBytes($value);
|
||||
return FOSSBilling\Tools::humanReadableBytes($value);
|
||||
}
|
||||
|
||||
public function twig_markdown_filter(Twig\Environment $env, $value)
|
||||
|
||||
@@ -415,17 +415,22 @@ class Tools
|
||||
|
||||
/**
|
||||
* 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';
|
||||
|
||||
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';
|
||||
}
|
||||
}
|
||||
@@ -689,7 +689,9 @@ class Service implements InjectionAwareInterface
|
||||
|
||||
$model->hostname = $data['hostname'] ?? null;
|
||||
$assigned_ips = $data['assigned_ips'] ?? '';
|
||||
if (!empty($assigned_ips)) $model->assigned_ips = self::processAssignedIPs($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;
|
||||
@@ -733,7 +735,9 @@ class Service implements InjectionAwareInterface
|
||||
$model->hostname = $data['hostname'] ?? $model->hostname;
|
||||
|
||||
$assigned_ips = $data['assigned_ips'] ?? '';
|
||||
if (!empty($assigned_ips)) $model->assigned_ips = self::processAssignedIPs($assigned_ips);
|
||||
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;
|
||||
@@ -1087,15 +1091,16 @@ class Service implements InjectionAwareInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Post-processing for the assigned IPs.
|
||||
* 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
|
||||
@@ -1105,12 +1110,10 @@ class Service implements InjectionAwareInterface
|
||||
|
||||
// Trim each entry and remove any empty strings
|
||||
$array = array_map('trim', $array);
|
||||
$array = array_filter($array, fn($ip) => $ip !== '');
|
||||
$array = array_filter($array, fn ($ip): bool => $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);
|
||||
});
|
||||
$array = array_filter($array, fn ($ip) => filter_var($ip, FILTER_VALIDATE_IP));
|
||||
|
||||
return json_encode(array_values($array));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user