Skip to content

Instantly share code, notes, and snippets.

@vodanh1213
Created May 27, 2015 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vodanh1213/bc148a3d082c4a35d1c9 to your computer and use it in GitHub Desktop.
Save vodanh1213/bc148a3d082c4a35d1c9 to your computer and use it in GitHub Desktop.
add_shortcode('list_domains_wpmu', 'list_domains_wpmu');
function list_domains_wpmu($attrs = array())
{
$default = array(
'root_domain' => parse_url(get_site_url(1), PHP_URL_HOST)
);
$a = wp_parse_args($attrs, $default);
$domains = get_site_option('md_domains');
$data = array();
foreach ($domains as $domain) {
$host = get_domain_5123($domain['domain_name']);
if ($host != $a['root_domain']) {
$data[] = sprintf('<a href="%s">%s</a>', $domain['domain_name'], $domain['domain_name']);
}
}
echo implode('<br/>', $data);
}
function get_domain_5123($domain, $debug = false)
{
$original = $domain = strtolower($domain);
if (filter_var($domain, FILTER_VALIDATE_IP)) {
return $domain;
}
$debug ? print('<strong style="color:green">&raquo;</strong> Parsing: ' . $original) : false;
$arr = array_slice(array_filter(explode('.', $domain, 4), function ($value) {
return $value !== 'www';
}), 0); //rebuild array indexes
if (count($arr) > 2) {
$count = count($arr);
$_sub = explode('.', $count === 4 ? $arr[3] : $arr[2]);
$debug ? print(" (parts count: {$count})") : false;
if (count($_sub) === 2) // two level TLD
{
$removed = array_shift($arr);
if ($count === 4) // got a subdomain acting as a domain
{
$removed = array_shift($arr);
}
$debug ? print("<br>\n" . '[*] Two level TLD: <strong>' . join('.', $_sub) . '</strong> ') : false;
} elseif (count($_sub) === 1) // one level TLD
{
$removed = array_shift($arr); //remove the subdomain
if (strlen($_sub[0]) === 2 && $count === 3) // TLD domain must be 2 letters
{
array_unshift($arr, $removed);
} else {
// non country TLD according to IANA
$tlds = array(
'aero',
'arpa',
'asia',
'biz',
'cat',
'com',
'coop',
'edu',
'gov',
'info',
'jobs',
'mil',
'mobi',
'museum',
'name',
'net',
'org',
'post',
'pro',
'tel',
'travel',
'xxx',
);
if (count($arr) > 2 && in_array($_sub[0], $tlds) !== false) //special TLD don't have a country
{
array_shift($arr);
}
}
$debug ? print("<br>\n" . '[*] One level TLD: <strong>' . join('.', $_sub) . '</strong> ') : false;
} else // more than 3 levels, something is wrong
{
for ($i = count($_sub); $i > 1; $i--) {
$removed = array_shift($arr);
}
$debug ? print("<br>\n" . '[*] Three level TLD: <strong>' . join('.', $_sub) . '</strong> ') : false;
}
} elseif (count($arr) === 2) {
$arr0 = array_shift($arr);
if (strpos(join('.', $arr), '.') === false
&& in_array($arr[0], array('localhost', 'test', 'invalid')) === false
) // not a reserved domain
{
$debug ? print("<br>\n" . 'Seems invalid domain: <strong>' . join('.', $arr) . '</strong> re-adding: <strong>' . $arr0 . '</strong> ') : false;
// seems invalid domain, restore it
array_unshift($arr, $arr0);
}
}
$debug ? print("<br>\n" . '<strong style="color:gray">&laquo;</strong> Done parsing: <span style="color:red">' . $original . '</span> as <span style="color:blue">' . join('.', $arr) . "</span><br>\n") : false;
return join('.', $arr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment