Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions src/Helper/Convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,23 @@ public static function bytes(int $bytes, int $precision = 10): string
// force calculation with positive numbers only
$bytes = floatval(abs($bytes));

$unit_conversions = array(
0 => array("UNIT" => "B", "VALUE" => pow(1024, 0)),
1 => array("UNIT" => "KiB", "VALUE" => pow(1024, 1)),
2 => array("UNIT" => "MiB", "VALUE" => pow(1024, 2)),
3 => array("UNIT" => "GiB", "VALUE" => pow(1024, 3)),
4 => array("UNIT" => "TiB", "VALUE" => pow(1024, 4)),
5 => array("UNIT" => "PiB", "VALUE" => pow(1024, 5)),
6 => array("UNIT" => "EiB", "VALUE" => pow(1024, 6)),
7 => array("UNIT" => "ZiB", "VALUE" => pow(1024, 7)),
8 => array("UNIT" => "YiB", "VALUE" => pow(1024, 8)),
);
$unit_conversions = [
0 => ["UNIT" => "B", "VALUE" => pow(1024, 0)],
1 => ["UNIT" => "KiB", "VALUE" => pow(1024, 1)],
2 => ["UNIT" => "MiB", "VALUE" => pow(1024, 2)],
3 => ["UNIT" => "GiB", "VALUE" => pow(1024, 3)],
4 => ["UNIT" => "TiB", "VALUE" => pow(1024, 4)],
5 => ["UNIT" => "PiB", "VALUE" => pow(1024, 5)],
6 => ["UNIT" => "EiB", "VALUE" => pow(1024, 6)],
7 => ["UNIT" => "ZiB", "VALUE" => pow(1024, 7)],
8 => ["UNIT" => "YiB", "VALUE" => pow(1024, 8)],
];

// Sort from the biggest defined unit to smallest to get the best human readable format.
krsort($unit_conversions);

foreach($unit_conversions as $conversion)
{
if($bytes >= $conversion["VALUE"])
{
foreach ($unit_conversions as $conversion) {
if ($bytes >= $conversion["VALUE"]) {
$result = $bytes / $conversion["VALUE"];
$result = strval(round($result, $precision)) . " " . $conversion["UNIT"];
return ($negative) ? '-' . $result : $result;
Expand Down
12 changes: 6 additions & 6 deletions src/Helper/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,17 @@ public function checkHost(string $host = null): bool
}

switch ($host) {
// Valid IPv4 or IPv6 address
case filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4|FILTER_FLAG_IPV6):
// Valid IPv4 or IPv6 address
break;
// Valid hostname as specified in RFC 1123 (IPv4 like strings are excluded)
// `filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)` unfortunately also reports invalid IP
// addresses (e.g. `127.0.0.300`) as valid, so we can not use this filter here.
// Regex reference: https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address
case !preg_match("/^(([0-9]{1,3})\.){3}([0-9]{1,3})$/", $host) and preg_match("/^(([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)*([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])$/i", $host):
// Valid hostname as specified in RFC 1123 (IPv4 like strings are excluded)
// `filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)` unfortunately also reports invalid IP
// addresses (e.g. `127.0.0.300`) as valid, so we can not use this filter here.
// Regex reference: https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address
break;
// Otherwise invalid host provided
default:
// Otherwise invalid host provided
return false;
}

Expand Down