Skip to content

Instantly share code, notes, and snippets.

@pcdummy pcdummy/test_hostname.php
Last active Apr 24, 2019

Embed
What would you like to do?
phpIPAM, testscript for fixing bug #1471 and #2374
<?php
$test_domains = array(
"a.webmeisterei.com",
"b.webmeisterei.com",
"webmeisterei.com",
"google.at",
"webmeisterei.com1",
"twebmeisterei.com",
"z.webmeisterei.com",
);
$test_hostnames = array(
"foo.webmeisterei.com" => "webmeisterei.com",
"foo.b.webmeisterei.com" => "b.webmeisterei.com",
"foo.webmeisterei.com1" => "webmeisterei.com1",
"foo.a.webmeisterei.com" => "a.webmeisterei.com",
"foo.twebmeisterei.com" => "twebmeisterei.com",
"foo.z.webmeisterei.com" => "z.webmeisterei.com",
"zfoo.webmeisterei.com" => "webmeisterei.com",
"foo.test.webmeisterei.com" => "webmeisterei.com",
"foo.google.at" => "google.at",
"foo.test.google.at" => "google.at",
);
function find_domain($domains, $hostname) {
// Reverse the hostname, so its easier to compare
$r_hostname = implode(".", array_reverse(array_slice(explode(".", $hostname), 1)));
// Reverse the domains to compare with the reversed hostname
$r_domains = array_map(function($domain) {
return implode(".", array_reverse(explode(".", $domain)));
}, $domains);
$matches = [];
foreach($r_domains as $r_domain) {
if (substr($r_hostname, 0, strlen($r_domain)) == $r_domain) {
$matches[] = $r_domain;
}
}
if (count($matches) < 0) {
return "";
}
$max = 0;
$result = "";
foreach($matches as $m) {
$length = strlen($m);
if ($length > $max) {
$max = $length;
$result = $m;
}
}
return implode(".", array_reverse(explode(".", $result)));
}
foreach($test_hostnames as $test_hostname => $test_result) {
$result = find_domain($test_domains, $test_hostname);
if ($result == $test_result) {
print($test_hostname . "\tOK\t" . $result . "\t" . $test_result . "\n");
} else {
print($test_hostname . "\tNOK\t\t" . $test_result . "\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.