From efa4e3a6d71746f52c58091e5ebda0e431b8fe42 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Wed, 3 Feb 2016 12:11:53 -0800 Subject: [PATCH] move subdomain check into a function --- controllers/API.php | 4 +--- lib/helpers.php | 6 ++++++ 2 files changed, 7 insertions(+), 3 deletions(-) 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); }