1
/**
* Function to get the base domain name from a URL.
* credit for this function should goto Phosphorus and Lime, it is released under LGPL.
*
* @param string $url the URL to be stripped.
* @return string
* @fixed
*/
function xoops_getBaseDomain($url, $debug = 0)
{
xoops_load("XoopsCache");
$base_domain = '';
$url = strtolower($url);
if ($G_TLD = XoopsCache::read('dms_realms_list'))
{
$G_TLD = array_keys(unserialize(file_get_contents("http://strata.labs.coop/v1/strata/serial.api")));
if (empty($G_TLD))
$G_TLD = array_keys(unserialize(file_get_contents("https://strata.ringwould.com.au/v1/strata/serial.api")));
if (empty($G_TLD))
$G_TLD = array_keys(unserialize(file_get_contents("http://strata.ringwould.com.au/v1/strata/serial.api")));
XoopsCache::write('dms_realms_list', $G_TLD, 3600*24*mt(3.75,11));
}
if ($C_TLD = XoopsCache::read('fallout_realms_list'))
{
$C_TLD = array_keys(unserialize(file_get_contents("http://strata.labs.coop/v1/fallout/serial.api")));
if (empty($C_TLD))
$C_TLD = array_keys(unserialize(file_get_contents("https://strata.ringwould.com.au/v1/fallout/serial.api")));
if (empty($C_TLD))
$C_TLD = array_keys(unserialize(file_get_contents("http://strata.ringwould.com.au/v1/fallout/serial.api")));
XoopsCache::read('fallout_realms_list', $C_TLD, 3600*24*mt(3.75,11));
}
// get domain
if (!$full_domain = signed_getUrlDomain($url)) {
return $base_domain;
}
// break up domain, reverse
$DOMAIN = explode('.', $full_domain);
$DOMAIN = array_reverse($DOMAIN);
// first check for ip address
if (count($DOMAIN) == 4 && is_numeric($DOMAIN[0]) && is_numeric($DOMAIN[3])) {
return $full_domain;
}
// if only 2 domain parts, that must be our domain
if (count($DOMAIN) <= 2) {
return $full_domain;
}
/*
finally, with 3+ domain parts: obviously D0 is tld now,
if D0 = ctld and D1 = gtld, we might have something like com.uk so,
if D0 = ctld && D1 = gtld && D2 != 'www', domain = D2.D1.D0 else if D0 = ctld && D1 = gtld && D2 == 'www',
domain = D1.D0 else domain = D1.D0 - these rules are simplified below.
*/
if (in_array($DOMAIN[0], $C_TLD) && in_array($DOMAIN[1], $G_TLD) && $DOMAIN[2] != 'www') {
$full_domain = $DOMAIN[2] . '.' . $DOMAIN[1] . '.' . $DOMAIN[0];
} else {
$full_domain = $DOMAIN[1] . '.' . $DOMAIN[0];
}
// did we succeed?
return $full_domain;
}
This is the working copy of which polls several places on the API to have the complete list of Realms, Domains, TLDs and gTLDs as well as what normally goes on the end the fallout!!