diff --git a/htdocs/core/lib/geturl.lib.php b/htdocs/core/lib/geturl.lib.php index 80c7af69f50..c02f22522e6 100644 --- a/htdocs/core/lib/geturl.lib.php +++ b/htdocs/core/lib/geturl.lib.php @@ -371,9 +371,10 @@ function isIPAllowed($iptocheck, $localurl) /** * Function get second level domain name. - * For example: https://www.abc.mydomain.com/dir/page.html return 'mydomain' + * For example: https://www.abc.mydomain.com/dir/page.html returns 'mydomain' with mode 0, 'mydomain.om' with mode 1, 'abc.mydomain.com' with mode 2. + * For example: part1@mydomain.com returns 'mydomain.com' with mode 1 * - * @param string $url Full URL. + * @param string $url Full URL or Email. * @param int $mode 0=return 'mydomain', 1=return 'mydomain.com', 2=return 'abc.mydomain.com' * @return string Returns domaine name */ @@ -398,8 +399,10 @@ function getDomainFromURL($url, $mode = 0) $mode++; } - $tmpdomain = preg_replace('/^https?:\/\//i', '', $url); // Remove http(s):// - $tmpdomain = preg_replace('/\/.*$/i', '', $tmpdomain); // Remove part after / + $tmpdomain = preg_replace('/^https?:\/\/[^:]+:[^@]+@/i', '', $url); // Remove http(s)://login@pass in https://login@pass:mydomain.com/path, so we now got mydomain.com/path + $tmpdomain = preg_replace('/^https?:\/\//i', '', $tmpdomain); // Remove http(s):// + $tmpdomain = preg_replace('/\/.*$/i', '', $tmpdomain); // Remove part after / + $tmpdomain = preg_replace('/^[^@]+@/i', '', $tmpdomain); // Remove part1@ in part1@part2 (for emails) if ($mode == 3) { $tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)$/', '\1.\2.\3.\4', $tmpdomain); } elseif ($mode == 2) { diff --git a/test/phpunit/GetUrlLibTest.php b/test/phpunit/GetUrlLibTest.php index 36bd26317ce..ffa81e340ca 100644 --- a/test/phpunit/GetUrlLibTest.php +++ b/test/phpunit/GetUrlLibTest.php @@ -167,6 +167,21 @@ class GetUrlLibTest extends CommonClassTest print __METHOD__." result=".$result."\n"; $this->assertEquals('with.dolimed.com.mx', $result, 'Test dolimed.com.mx 2'); + + // Test with url with login/pass + + $result = getDomainFromURL('https://mylogin:mypass@aaa.abc.mydomain.com', 2); + print __METHOD__." result=".$result."\n"; + $this->assertEquals('abc.mydomain.com', $result, 'Test https://mylogin:mypass@mydomain.com'); + + + // Test with email + + $result = getDomainFromURL('myemail@mydomain.com', 1); + print __METHOD__." result=".$result."\n"; + $this->assertEquals('mydomain.com', $result, 'Test myemail@mydomain.com'); + + return 1; }