diff --git a/bin/php-whois.php b/bin/php-whois.php index 2eb214df..571fbd1e 100644 --- a/bin/php-whois.php +++ b/bin/php-whois.php @@ -8,50 +8,118 @@ } include "$scriptDir/../vendor/autoload.php"; -$action = trim($argv[1] ?? ''); -$args = array_slice($argv, 2); +function main($argv) +{ + $action = trim($argv[1] ?? ''); + $args = array_slice($argv, 2); -if (empty($action)) { - $action = 'help'; + if (empty($action)) { + $action = 'help'; + } + switch (mb_strtolower(ltrim($action, '-'))) { + case 'help': + case 'h': + help(); + return; + } + switch ($action) { + case 'lookup': + lookup($args[0]); + break; + + case 'info': + $opts = parseOpts(implode(' ', array_slice($args, 1))); + info($args[0], $opts['parser'] ?? null); + break; + + default: + echo "Unknown action: {$action}\n"; + exit(1); + } } -switch (mb_strtolower(ltrim($action, '-'))) { - case 'help': - case 'h': - echo implode("\n", [ - 'Welcome to php-whois CLI', - '', - ' Syntax:', - ' php-whois {action} [arg1 arg2 ... argN]', - ' php-whois help|--help|-h', - ' php-whois lookup {domain}', - ' php-whois info {domain}', - '', - ' Examples', - ' php-whois lookup google.com', - ' php-whois info google.com', - '', - '', - ]); - exit; +function parseOpts(string $str): array +{ + $result = []; + $rest = trim($str); + while (preg_match('~--([-_a-z\d]+)(\s+|=)(\'([^\']+)\'|[^-\s]+)~ui', $rest, $m, PREG_OFFSET_CAPTURE)) { + $result[$m[1][0]] = $m[4][0] ?? $m[3][0]; + $rest = trim(mb_substr($rest, $m[0][1] + mb_strlen($m[0][0]))); + } + return $result; } -$whois = Factory::get()->createWhois(); +function help() +{ + echo implode("\n", [ + 'Welcome to php-whois CLI', + '', + ' Syntax:', + ' php-whois {action} [arg1 arg2 ... argN]', + ' php-whois help|--help|-h', + ' php-whois lookup {domain}', + ' php-whois info {domain} [--parser {type}]', + '', + ' Examples', + ' php-whois lookup google.com', + ' php-whois info google.com', + ' php-whois info google.com --parser block', + '', + '', + ]); +} -switch ($action) { - case 'lookup': - echo "action: {$action}\n"; - echo "domain: '{$args[0]}'\n\n"; - var_dump($whois->lookupDomain($args[0])); - break; +function lookup(string $domain) +{ + echo implode("\n", [ + ' action: lookup', + " domain: '{$domain}'", + '', + '', + ]); - case 'info': - echo "action: {$action}\n"; - echo "domain: '{$args[0]}'\n\n"; - var_dump($whois->loadDomainInfo($args[0])); - break; + $whois = Factory::get()->createWhois(); + $result = $whois->lookupDomain($domain); - default: - echo "Unknown action: {$action}\n"; - exit(1); + var_dump($result); } + +function info(string $domain, string $parserType = null) +{ + echo implode("\n", [ + ' action: info', + " domain: '{$domain}'", + " parser: '{$parserType}'", + '', + '', + ]); + + $tld = Factory::get()->createWhois()->getTldModule(); + if ($parserType) { + try { + $parser = Factory::get()->createTldParser($parserType); + } catch (\Throwable $e) { + echo "\nCannot create TLD parser with type '$parserType'\n\n"; + throw $e; + } + $newServers = []; + foreach ($tld->getServers() as $server) { + $newServers[] = new \Iodev\Whois\Modules\Tld\TldServer( + $server->getZone(), + $server->getHost(), + $server->isCentralized(), + $parser, + $server->getQueryFormat() + ); + } + $tld->setServers($newServers); + } + + $result = $tld->loadDomainInfo($domain); + + var_dump($result); +} + +main($argv); + +