Created
May 22, 2013 10:56
-
-
Save sajanp/5626750 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment