Skip to content

Instantly share code, notes, and snippets.

@jochumdev
Last active April 24, 2019 07:48
Show Gist options
  • Save jochumdev/841f0243fc7f5f1f9b7a9a57c6538a1b to your computer and use it in GitHub Desktop.
Save jochumdev/841f0243fc7f5f1f9b7a9a57c6538a1b to your computer and use it in GitHub Desktop.
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