diff --git a/app/Services/Subnet.php b/app/Services/Subnet.php index 358eda5..36322f3 100644 --- a/app/Services/Subnet.php +++ b/app/Services/Subnet.php @@ -40,27 +40,7 @@ class Subnet throw new Exception("CIDR must be between 0 and 32."); } - if ($cidr === 32) { - return [ - 'network' => $ip, - 'first' => $ip, - 'last' => $ip, - 'hosts' => 1, - ]; - } - - if ($cidr === 31) { - $ipLong = ip2long($ip); - $network = $ipLong & (-1 << (32 - $cidr)); - return [ - 'network' => long2ip($network), - 'first' => long2ip($network), - 'last' => long2ip($network + 1), - 'hosts' => 2, - ]; - } - - $hosts = (1 << (32 - $cidr)) - 2; + $hosts = (1 << (32 - $cidr)) - 2; // Excludes network and broadcast $ipLong = ip2long($ip); $mask = -1 << (32 - $cidr); $network = $ipLong & $mask; @@ -76,7 +56,6 @@ class Subnet ]; } - /** * Calculate IPv6 * @@ -91,50 +70,35 @@ class Subnet throw new Exception("CIDR must be between 0 and 128."); } - if ($cidr === 128) { - return [ - 'network' => $ip, - 'first' => $ip, - 'last' => $ip, - 'hosts' => 1, - ]; + // Convert IP to binary representation + $binaryIP = inet_pton($ip); + if ($binaryIP === false) { + throw new Exception("Failed to parse IPv6 address."); } - if ($cidr === 127) { - $binaryIP = inet_pton($ip); - $network = substr($binaryIP, 0, intval($cidr / 8)); - $firstHost = inet_ntop($network); - $lastHost = inet_ntop($binaryIP); - return [ - 'network' => $firstHost, - 'first' => $firstHost, - 'last' => $lastHost, - 'hosts' => 2, - ]; - } - - // General case for other IPv6 subnets - $totalHosts = bcpow(2, 128 - $cidr); // Number of total IPs - $binaryIP = inet_pton($ip); - - // Calculate the network address + // Calculate network address $network = substr($binaryIP, 0, intval($cidr / 8)); $remainder = $cidr % 8; if ($remainder > 0) { - $lastByte = ord($binaryIP[intval($cidr / 8)]) & (0xFF << (8 - $remainder)); + $lastByte = ord($binaryIP[$cidr / 8]) & (0xFF << (8 - $remainder)); $network .= chr($lastByte); } + + // Pad the rest with zeros $network = str_pad($network, 16, "\0"); + // Calculate first and last addresses $networkAddress = inet_ntop($network); + $totalHosts = $cidr == 128 ? 1 : bcpow(2, 128 - $cidr); + $firstHost = ($cidr == 128) ? $networkAddress : inet_ntop($network); + $lastHost = inet_ntop(pack("H*", str_repeat("F", 32))); return [ 'network' => $networkAddress, - 'first' => $cidr == 128 ? $networkAddress : inet_ntop($network), - 'last' => $cidr == 128 ? $networkAddress : inet_ntop($network), // Adjusted for simplicity + 'first' => $firstHost, + 'last' => $lastHost, 'hosts' => $totalHosts, ]; } - } \ No newline at end of file diff --git a/composer.json b/composer.json index b2f7d6e..fcdd64b 100644 --- a/composer.json +++ b/composer.json @@ -10,8 +10,7 @@ "require": { "php": ">=8.3.0", "filp/whoops": "^2.16", - "vlucas/phpdotenv": "^5.6", - "ext-bcmath": "*" + "vlucas/phpdotenv": "^5.6" }, "require-dev": { "symfony/var-dumper": "^7.1", diff --git a/src/Http/View/Engine/HtmlEngine.php b/src/Http/View/Engine/HtmlEngine.php index 887e125..c094be6 100644 --- a/src/Http/View/Engine/HtmlEngine.php +++ b/src/Http/View/Engine/HtmlEngine.php @@ -15,11 +15,14 @@ class HtmlEngine extends Render $basePath = $_SERVER['DOCUMENT_ROOT']; $viewsPath = app()->resourcePath('views/' . str_replace('.', '/', $this->view) . '.php'); - if (!file_exists($viewsPath)) { - throw new \Exception('View not found'); + if (file_exists($viewsPath)) { + extract($this->data); + + include $viewsPath; + + return; } - extract($this->data); - include $viewsPath; + throw new \Exception('View not found'); } } \ No newline at end of file diff --git a/src/Routing/RouteCollection.php b/src/Routing/RouteCollection.php index 4579091..75a8fe4 100644 --- a/src/Routing/RouteCollection.php +++ b/src/Routing/RouteCollection.php @@ -71,7 +71,6 @@ class RouteCollection fn($key) => !is_numeric($key), ARRAY_FILTER_USE_KEY ); - return $route; } } diff --git a/src/Routing/Router.php b/src/Routing/Router.php new file mode 100644 index 0000000..6d90c2a --- /dev/null +++ b/src/Routing/Router.php @@ -0,0 +1,22 @@ +