Skip to content

Instantly share code, notes, and snippets.

@sajanp
Created May 22, 2013 10:56

Revisions

  1. Sajan Parikh created this gist May 22, 2013.
    81 changes: 81 additions & 0 deletions whoisnz.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    <?php
    /**
    * Basic WHOIS query script for exipry date
    */

    // feed domain via script, or GET request
    $domain = (PHP_SAPI == 'cli') ? $argv[1] : $_GET['domain'];

    // exit if no domain is provided
    if (!$domain) {
    exit('No domain');
    }

    // only NZ domains
    $nz = substr($domain, -3, 3);
    if ($nz !== '.nz') {
    exit('Domain is not an NZ TLD');
    }


    // return date
    echo Whois::queryExpiry($domain);


    /********** DO NOT EDIT BELOW THIS LINE **********/

    /**
    * Basic whois wrapper
    */
    class Whois
    {
    /**
    * Query Whois server via CLI -> Shell access is required
    *
    * @param string $domain Domain in question
    * @param string $timezone PHP timezone -> http://php.net/manual/en/timezones.php
    * @param string $format Formatted date -> http://php.net/manual/en/function.date.php
    * @return string Expiry time domain
    */
    public static function queryExpiry($domain, $timezone = 'Pacific/Auckland', $format = 'd/m/Y')
    {
    // CLI whois
    $whois = shell_exec(escapeshellcmd('whois ' . $domain));

    $lines = explode("\n", $whois);
    foreach ($lines as $val) {
    if (strpos($val, 'domain_datebilleduntil') !== false) {
    // save to DateTime -> Compensate for timezone
    $date = new DateTime(self::getString($val, ': '));
    break;
    }
    }

    // return expiry date in requested format/timezone
    $date->setTimezone(new DateTimeZone($timezone));
    return $date->format($format);
    }

    /**
    * Extract a substring between two tokens
    *
    * @param string $string String to search
    * @param string $start Starting token
    * @param boolean|string $end End token. False = end of $search
    * @return string String between $start and $end
    */
    public static function getString($string, $start, $end = false)
    {
    if ((strpos($string, $start) === false)
    || ($end !== false && (strpos($string, $end) === false))
    ) {
    return false;
    }

    $start = strpos($string, $start) + strlen($start);

    return ($end)
    ? substr($string, $start, (strpos($string, $end, $start) - $start))
    : substr($string, $start);
    }
    }