diff --git a/controllers/API.php b/controllers/API.php index ae9fc63..172e32d 100644 --- a/controllers/API.php +++ b/controllers/API.php @@ -103,9 +103,7 @@ class API { foreach($xpath->query('//a[@href]') as $href) { $url = $href->getAttribute('href'); $domain = parse_url($url, PHP_URL_HOST); - if($url == $target || $domain == $target_domain || - # subdomain check - ($target_domain and $domain and substr_compare($domain, '.' . $target_domain, -(strlen($target_domain) + 1)) == 0)) { + if($url == $target || $domain == $target_domain || str_ends_with($domain, $target_domain)) { $found[$url] = null; } } diff --git a/lib/helpers.php b/lib/helpers.php index 185e49d..10002da 100644 --- a/lib/helpers.php +++ b/lib/helpers.php @@ -42,6 +42,12 @@ function random_string($len) { return $str; } +// Returns true if $needle is the end of the $haystack +function str_ends_with($haystack, $needle) { + if($needle == '' || $haystack == '') return false; + return strpos(strrev($haystack), strrev($needle)) === 0; +} + function display_url($url) { return preg_replace(['/^https?:\/\//','/\/$/'], '', $url); }