diff --git a/.gitignore b/.gitignore index 19982ea..987e2a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ composer.lock -vendor \ No newline at end of file +vendor diff --git a/composer.json b/composer.json index 3c56d51..62f6f7b 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "autoload": { "psr-4": { "CodeSpace\\WhoisParser\\": "src/", - "CodeSpace\\WhoisParser\\Provider\\": "src/provider/" + "CodeSpace\\WhoisParser\\Parser\\": "src/parser/" } }, "require": { diff --git a/src/ParserInterface.php b/src/ParserInterface.php new file mode 100644 index 0000000..2bf6559 --- /dev/null +++ b/src/ParserInterface.php @@ -0,0 +1,19 @@ +lines = $lines; + $parser->provider = $provider; + return $parser; + } + + public static function fromString(string $whois, string $provider) { + return self::fromArray(explode("\n", $whois), $provider); + } + + private function getOffset(array $arr, int $index): mixed { + return count($arr) > $index ? $arr[$index] : null; + } + + function workWithKeys(array $keys, $fn): array { + $num = 0; + $arr = []; + foreach ($this->lines as &$line) { + foreach ($keys as &$key) { + if (str_starts_with($line, "$key: ")) { + array_push($arr, $fn($line, $num)); + } + } + $num++; + } + return $arr; + } + + function getKeyValues(string $key): ?array { + return $this->getKeysValues([$key]); + } + + function getKeysValues(array $keys): ?array { + return $this->workWithKeys($keys, function($line, $num) { + $start = strpos($line, ": ") + 2; + return trim(mb_substr($line, $start)); + }); + } + + function getKeyValue(string $key, int $offset = 0): ?string { + return $this->getKeysValue([$key], $offset); + } + + function getKeysValue(array $keys, int $offset = 0): ?string { + $arr = $this->workWithKeys($keys, function($line, $num) { + $start = strpos($line, ": ") + 2; + return trim(substr($line, $start)); + }); + return $this->getOffset($arr, $offset); + } + + function findKey(string $key, int $offset = 0): ?int { + return $this->findFirstKey([$key], $offset); + } + + function findFirstKey(array $keys, int $offset = 0): ?int { + $arr = $this->workWithKeys($keys, function($line, $num) { + return $num; + }); + return $this->getOffset($arr, $offset); + } + + function getSection(int $line) { + $start = $end = $line; + $line = null; + while ($line !== "") { + $start--; + $line = $this->lines[$start]; + } + $line_count = count($this->lines) - 1; + $line = null; + while ($line !== "" && $end < $line_count) { + $end++; + $line = $this->lines[$end]; + } + return $this->getSubset($start, $end); + } + + function getSubset(int $start, int $end) { + return self::fromArray(array_slice($this->lines, $start, $end-$start), $this->provider); + } + + function getSectionWithKey(string $key) { + return $this->getSectionWithKeys([$key]); + } + + function getSectionWithKeys(array $keys): ?WhoisParser { + $line = $this->findFirstKey($keys); + if ($line == null) return null; + return $this->getSection($line); + } + +} diff --git a/src/parser/RipeParser.php b/src/parser/RipeParser.php new file mode 100644 index 0000000..dc72bf4 --- /dev/null +++ b/src/parser/RipeParser.php @@ -0,0 +1,69 @@ +getSectionWithKey("aut-num"); + return [ + "asn" => $as->getKeyValue("aut-num"), + "name" => $as->getKeyValue("as-name"), + "source" => $as->getKeyValue("source") + ]; + } + + public function parseAbuse(WhoisParser $whois): ?array { + $abuse = $whois->getSectionWithKey("abuse-mailbox"); + return [ + "role" => $abuse->getKeyValue("role"), + "address" => $abuse->getKeyValues("address"), + "phone" => $abuse->getKeyValue("phone"), + "fax" => $abuse->getKeyValue("fax-no"), + "email" => $abuse->getKeyValue("abuse-mailbox") + ]; + } + + public function parseOrg(WhoisParser $whois): ?array { + $org = $whois->getSectionWithKey("organisation"); + return [ + "id" => $org->getKeyValue("organisation"), + "name" => $org->getKeyValue("org-name"), + "address" => $org->getKeyValues("address"), + "phone" => $org->getKeyValue("phone"), + "fax" => $org->getKeyValue("fax-no"), + "email" => $org->getKeyValue("e-mail") + ]; + } + + public function parseRoute(WhoisParser $whois): ?array { + $section = $whois->getSectionWithKeys(["route", "route6"]); + $range = $section->getKeysValue(["route", "route6"]); + return [ + "range" => $range, + "description" => $section->getKeyValue("descr"), + "asn" => $section->getKeyValue("origin") + ]; + } + + public function parseInet(WhoisParser $whois): ?array { + $section = $whois->getSectionWithKeys(["inetnum", "inet6num"]); + $range = $section->getKeysValue(["inetnum", "inet6num"]); + return [ + "range" => $range, + "name" => $section->getKeyValue("netname"), + "description" => $section->getKeyValue("descr"), + "country" => $section->getKeyValue("country") + ]; + } + + public function findAsn(WhoisParser $whois): ?string { + return $whois->getKeyValue("origin"); + } + + public function findAbuseContact(WhoisParser $whois): ?string { + return $whois->getKeyValue("abuse-c"); + } + +}