Skip to content

Instantly share code, notes, and snippets.

@jeromefitzpatrick
Last active February 3, 2021 06:58
Show Gist options
  • Save jeromefitzpatrick/9f1582fe39468f173ab6b8660a95dbbc to your computer and use it in GitHub Desktop.
Save jeromefitzpatrick/9f1582fe39468f173ab6b8660a95dbbc to your computer and use it in GitHub Desktop.
Who squatting deh so?
class SquatChecker
{
public function find(array $domains = null, array $suspects = null): array
{
$domains = $domains ?? [
'palantir.com',
'apple.com',
'nic.ci',
];
$suspects = $suspects ?? [
'pla@ntir.com',
'plaantir.com',
'aplantirtechnologies.com',
'palanti.rbiz',
'palantir.biz',
'apple.org',
'apple.com',
'appleorchard.net',
'paiantir.com',
'nic.cl',
'palantirtechnologies.com',
'nlc.biz',
];
$fakes = [
'i' => ['1', 'l', '!', '|'],
's' => ['5', '$'],
'0' => '0',
'a' => '@',
'e' => '3',
];
$squatters = [];
foreach ($domains as $domain) {
// Normalise domain with fakes
$normalisedDomain = $domain;
foreach ($fakes as $real => $fake) {
$normalisedDomain = str_replace($fake, $real, $normalisedDomain);
}
foreach ($suspects as $suspectIndex => $suspect) {
// Domains are identical, so not a squatter... next!
if ($domain === $suspect) {
continue;
}
// Normalise the suspect domain with fakes
$normalisedSuspect = $suspect;
foreach ($fakes as $real => $fake) {
$normalisedSuspect = str_replace($fake, $real, $normalisedSuspect);
}
// Swap each pair of characters in the suspect domain and check for
// 2nd level domain name matches
// Compare first without swapping to cover Task 1 and 2
for ($i = 0; $i < (strpos($normalisedSuspect, '.') + 2); $i++) {
$temp = $normalisedSuspect;
if ($i > 0) {
$temp[$i - 1] = $normalisedSuspect[$i];
$temp[$i] = $normalisedSuspect[$i - 1];
}
echo $temp . PHP_EOL;
if (substr($normalisedDomain, 0, strpos($normalisedDomain, '.')) === substr($temp, 0, strpos($temp, '.'))) {
$squatters[] = $suspect;
unset($suspects[$suspectIndex]);
break;
}
}
}
}
return $squatters;
}
}
(new SquatChecker)->find();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment