diff --git a/controllers/routes.php b/controllers/routes.php index cc9f87c..40380d8 100644 --- a/controllers/routes.php +++ b/controllers/routes.php @@ -11,5 +11,8 @@ function fromController(string $path) { return function(Router $router) { $router->addRoute(GET, "/", fromController("/GET")); $router->addRoute(GET, "/test", useRenderer(fromController("/test/GET"))); + $router->addRoute(GET, "/ipaddress", useRenderer(fromController("/ipaddress/GET"))); + + $router->addRoute(GET, "/whois", useRenderer(fromController("/whois/GET"))); }; diff --git a/controllers/whois/GET.php b/controllers/whois/GET.php new file mode 100644 index 0000000..0157c4d --- /dev/null +++ b/controllers/whois/GET.php @@ -0,0 +1,71 @@ + $a[0] == "fn"))[0][3] ?? null; + $kind = array_values(array_filter($vcardArray, fn(array $a) => $a[0] == "kind"))[0][3] ?? null; + $email = array_map(fn(array $a) => $a[3], array_values(array_filter($vcardArray, fn(array $a) => $a[0] == "email"))); + $adr = array_values(array_filter($vcardArray, fn(array $a) => $a[0] == "adr"))[0]["label"] ?? null; + + $contacts[] = [ + "handle" => $entity["handle"], + "roles" => $entity["roles"], + "function" => $fn, + "kind" => $kind, + "address" => $adr, + "email" => $email, + ]; + } + + return $contacts; +} + +function whoisIp(string $ip) { + $data = json_decode(getRequest(RDAP_URL . "/ip/" . $ip), true); + return [ + "query" => $ip, + "version" => $data["ipVersion"], + "network" => [ + "name" => $data["name"], + "type" => $data["type"] ?? null, + "cidr" => $data["cidr0_cidrs"][0][$data["ipVersion"] . "prefix"] . "/" . $data["cidr0_cidrs"][0]["length"], + "startAddress" => $data["startAddress"], + "endAddress" => $data["endAddress"], + "status" => $data["status"], + "remarks" => $data["remarks"]["description"][0] ?? "", + "contacts" => getContactData($data), + ] + ]; +} + +return function (array $context) { + $result = null; + + if (key_exists("ip", $_GET)) { + $ip = $_GET["ip"]; + if (!filter_var($ip, FILTER_VALIDATE_IP)) { + setStatusCode(400); + $result = errorResponse("Invalid IP address", "Please provide a valid IP address."); + } else { + $result = whoisIp($ip); + } + } else { + setStatusCode(400); + $result = errorResponse("Unknown mode", "Please specify one of the following query parameters: ip"); + } + + $context["renderer"]($result); +}; + diff --git a/controllers/whois/settings.php b/controllers/whois/settings.php new file mode 100644 index 0000000..084d753 --- /dev/null +++ b/controllers/whois/settings.php @@ -0,0 +1,3 @@ +notFoundHandler = function($context) { - header("HTTP/", true, 404); + setStatusCode(404); require(ROOT . "/templates/404.php"); }; } diff --git a/utils/arrays.php b/utils/arrays.php index 1ca50ea..0361edd 100644 --- a/utils/arrays.php +++ b/utils/arrays.php @@ -8,11 +8,3 @@ function &array_get_or_add($needle, array &$haystack, $default=null) { return $haystack[$needle]; } } - -function array_default($needle, array $haystack, $default=null) { - if (key_exists($needle, $haystack)) { - return $haystack[$needle]; - } else { - return $default; - } -} diff --git a/utils/error.php b/utils/error.php new file mode 100644 index 0000000..bb7ffbd --- /dev/null +++ b/utils/error.php @@ -0,0 +1,13 @@ + $error, + "description" => $description, + "timestamp" => (new DateTime())->format("c") + ]; +} \ No newline at end of file diff --git a/utils/http.php b/utils/http.php new file mode 100644 index 0000000..38822c7 --- /dev/null +++ b/utils/http.php @@ -0,0 +1,35 @@ + $k . ": " . $v, array_keys($headers), array_values($headers)); + + + curl_setopt($curlResource, CURLOPT_HTTPHEADER, $headers); + } + + $result = curl_exec($curlResource); + + curl_close($curlResource); + + return $result; +} + +function getRequest(string $url, array $headers = []) { + return request("GET", $url, null, $headers); +} + +function postRequest(string $url, string $body = null, $headers = []) { + return request("POST", $url, $body, $headers); +} \ No newline at end of file