mirror of
https://github.com/Dolibarr/dolibarr.git
synced 2025-12-06 09:38:23 +01:00
FIX To support domain names with TLD on 2 levels.
This commit is contained in:
@@ -345,9 +345,26 @@ function isIPAllowed($iptocheck, $localurl)
|
||||
*/
|
||||
function getDomainFromURL($url, $mode = 0)
|
||||
{
|
||||
$arrayof2levetopdomain = array(
|
||||
'co.at', 'or.at', 'gv.at',
|
||||
'avocat.fr', 'aeroport.fr', 'veterinaire.fr',
|
||||
'com.ng', 'gov.ng', 'gov.ua', 'com.ua', 'in.ua', 'org.ua', 'edu.ua', 'net.ua',
|
||||
'net.uk', 'org.uk', 'gov.uk', 'co.uk',
|
||||
'com.mx'
|
||||
);
|
||||
|
||||
$parts = array_reverse(explode('.', $url));
|
||||
if (!empty($parts[1])) {
|
||||
if (in_array($parts[1].'.'.$parts[0], $arrayof2levetopdomain)) {
|
||||
$mode++;
|
||||
}
|
||||
}
|
||||
|
||||
$tmpdomain = preg_replace('/^https?:\/\//i', '', $url); // Remove http(s)://
|
||||
$tmpdomain = preg_replace('/\/.*$/i', '', $tmpdomain); // Remove part after domain
|
||||
if ($mode == 2) {
|
||||
if ($mode == 3) {
|
||||
$tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)$/', '\1.\2.\3.\4', $tmpdomain);
|
||||
} elseif ($mode == 2) {
|
||||
$tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)\.([^\.]+)$/', '\1.\2.\3', $tmpdomain); // Remove part 'www.' before 'abc.mydomain.com'
|
||||
} else {
|
||||
$tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)$/', '\1.\2', $tmpdomain); // Remove part 'www.abc.' before 'mydomain.com'
|
||||
|
||||
@@ -189,37 +189,61 @@ class GetUrlLibTest extends PHPUnit\Framework\TestCase
|
||||
$langs=$this->savlangs;
|
||||
$db=$this->savdb;
|
||||
|
||||
// Tests with param 0
|
||||
|
||||
$result=getDomainFromURL('http://localhost');
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('localhost', $result, 'Test 0a');
|
||||
$this->assertEquals('localhost', $result, 'Test localhost 0');
|
||||
|
||||
$result=getDomainFromURL('http://localhost', 1);
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('localhost', $result, 'Test 0b');
|
||||
$this->assertEquals('localhost', $result, 'Test localhost 1');
|
||||
|
||||
$result=getDomainFromURL('https://dolimed.com');
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('dolimed', $result, 'Test 1');
|
||||
$this->assertEquals('dolimed', $result, 'Test dolimed.com 0');
|
||||
|
||||
$result=getDomainFromURL('http://www.dolimed.com/screenshots/afile');
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('dolimed', $result, 'Test 2');
|
||||
$this->assertEquals('dolimed', $result, 'Test dolimed.com/... 0');
|
||||
|
||||
$result=getDomainFromURL('http://www.with.dolimed.com/screenshots/afile');
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('dolimed', $result, 'Test 3');
|
||||
$this->assertEquals('dolimed', $result, 'Test ...dolimed.com/ 0');
|
||||
|
||||
// Tests with param 1
|
||||
|
||||
$result=getDomainFromURL('https://dolimed.com', 1);
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('dolimed.com', $result, 'Test 4');
|
||||
$this->assertEquals('dolimed.com', $result, 'Test dolimed.com 1');
|
||||
|
||||
$result=getDomainFromURL('http://www.dolimed.com/screenshots/afile', 1);
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('dolimed.com', $result, 'Test 5');
|
||||
$this->assertEquals('dolimed.com', $result, 'Test dolimed.com/... 1');
|
||||
|
||||
$result=getDomainFromURL('http://www.with.dolimed.com/screenshots/afile', 1);
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('dolimed.com', $result, 'Test 6');
|
||||
$this->assertEquals('dolimed.com', $result, 'Test .../dolimed.com 1');
|
||||
|
||||
// Tests with param 2
|
||||
|
||||
$result=getDomainFromURL('http://www.with.dolimed.com/screenshots/afile', 2);
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('with.dolimed.com', $result, 'Test .../dolimed.com 2');
|
||||
|
||||
// For domains with top domain on 2 levels
|
||||
|
||||
$result=getDomainFromURL('https://www.with.dolimed.com.mx', 0);
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('com.mx', $result, 'Test dolimed.com.mx 0');
|
||||
|
||||
$result=getDomainFromURL('https://www.with.dolimed.com.mx', 1);
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('dolimed.com.mx', $result, 'Test dolimed.com.mx 1');
|
||||
|
||||
$result=getDomainFromURL('https://www.with.dolimed.com.mx', 2);
|
||||
print __METHOD__." result=".$result."\n";
|
||||
$this->assertEquals('with.dolimed.com.mx', $result, 'Test dolimed.com.mx 2');
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user