From 454316a5faada99b18a0b2f40294c4f0bd9a0fcd Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 28 May 2019 22:18:22 +0200 Subject: [PATCH] Fix getDomainFromURL --- htdocs/core/lib/geturl.lib.php | 10 ++++++--- test/phpunit/GetUrlLibTest.php | 41 +++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/htdocs/core/lib/geturl.lib.php b/htdocs/core/lib/geturl.lib.php index c161eb056fe..ba349edd1b2 100644 --- a/htdocs/core/lib/geturl.lib.php +++ b/htdocs/core/lib/geturl.lib.php @@ -172,14 +172,18 @@ function getURLContent($url, $postorget = 'GET', $param = '', $followlocation = * For example: https://www.abc.mydomain.com/dir/page.html return 'mydomain' * * @param string $url Full URL. + * @param string $mode 0=return 'mydomain', 1=return 'mydomain.com' * @return string Returns domaine name */ -function getDomainFromURL($url) +function getDomainFromURL($url, $mode=0) { $tmpdomain = preg_replace('/^https?:\/\//i', '', $url); // Remove http(s):// $tmpdomain = preg_replace('/\/.*$/i', '', $tmpdomain); // Remove part after domain - $tmpdomain = preg_replace('/\.[^\.]+$/', '', $tmpdomain); // Remove first level domain (.com, .net, ...) - $tmpdomain = preg_replace('/^[^\.]+\./', '', $tmpdomain); // Remove part www. before domain name + $tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)$/', '\1.\2', $tmpdomain); // Remove part www.abc before domain name + if (empty($mode)) + { + $tmpdomain = preg_replace('/\.[^\.]+$/', '', $tmpdomain); // Remove first level domain (.com, .net, ...) + } return $tmpdomain; } diff --git a/test/phpunit/GetUrlLibTest.php b/test/phpunit/GetUrlLibTest.php index 0e0cfb7d767..a8fb1f1718c 100644 --- a/test/phpunit/GetUrlLibTest.php +++ b/test/phpunit/GetUrlLibTest.php @@ -46,7 +46,7 @@ $conf->global->MAIN_DISABLE_ALL_MAILS=1; * @backupStaticAttributes enabled * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased. */ -class GetUrlLibTest extends PHPUnit_Framework_TestCase +class GetUrlLibTest extends PHPUnit\Framework\TestCase { protected $savconf; protected $savuser; @@ -167,6 +167,45 @@ class GetUrlLibTest extends PHPUnit_Framework_TestCase return 1; } + /** + * testGetDomainFromURL + * + * @return int + */ + public function testGetDomainFromURL() + { + global $conf,$user,$langs,$db; + $conf=$this->savconf; + $user=$this->savuser; + $langs=$this->savlangs; + $db=$this->savdb; + + $result=getDomainFromURL('https://dolimed.com'); + print __METHOD__." result=".$result."\n"; + $this->assertEquals('dolimed', $result, 'Test 1'); + + $result=getDomainFromURL('http://www.dolimed.com/screenshots/afile'); + print __METHOD__." result=".$result."\n"; + $this->assertEquals('dolimed', $result, 'Test 2'); + + $result=getDomainFromURL('http://www.with.dolimed.com/screenshots/afile'); + print __METHOD__." result=".$result."\n"; + $this->assertEquals('dolimed', $result, 'Test 3'); + + $result=getDomainFromURL('https://dolimed.com', 1); + print __METHOD__." result=".$result."\n"; + $this->assertEquals('dolimed.com', $result, 'Test 4'); + + $result=getDomainFromURL('http://www.dolimed.com/screenshots/afile', 1); + print __METHOD__." result=".$result."\n"; + $this->assertEquals('dolimed.com', $result, 'Test 5'); + + $result=getDomainFromURL('http://www.with.dolimed.com/screenshots/afile', 1); + print __METHOD__." result=".$result."\n"; + $this->assertEquals('dolimed.com', $result, 'Test 6'); + + return 1; + } /** * testRemoveHtmlComment