DoliCloud/SellYourSaas

domain with 2 sufixes returns com.mx instead myfirma.com.mx

erwinpalma opened this issue · 3 comments

Describe the bug

I have configured my main domain as firma.com.mx, but in some places the "admin" url is only com.mx instead myaccount.firma.com.mx or firma.com.mx

image

image

image

Steps to Reproduce

Configure a double sufix domain

  1. go to the admin web site https://admin.firma.com.mx
  2. Click on Home->Setup->Modules/Application->SELLYOURSAAS module
  3. Into the field List of Sub-domain to deploy new instances type firma.com.mx
  4. Save the changes.

Check the changes

  1. Acces again to the module SELLYOURSAAS (do not refresh)
  2. Chech the label Default product/service for instance (suggested for new contract/instance)

Expected behavior

The domain must be firma.com.mx instead com.mx

Screenshots

image

Image with only one sufix

SELLYOURSAAS:

  • Version: 2.1

Dolibarr:

  • Version: 18.0.0

Additional context
Add any other context about the problem here.

As Workarround

Into the file /home/admin/wwwroot/dolibarr/htdocs/core/lib/geturl.lib.php line 345, I have noticed that the $mode == 2 fills the requirements, but the chalenge was to know if there is more than one sufixes.

I have added a validation (of course, probably is not the mort professional code ever developed but it works).

function getDomainFromURL($url, $mode = 0)
{
	$parts = explode('.', $url); 

	foreach ( array_reverse($parts) as $key => $value ) {
		if ($key == 0 and $value == "com") {
			$mode = 1;
		}
		if ($key == 1 and $value == "com") {
			$mode = 2;
		}
	}

	$tmpdomain = preg_replace('/^https?:\/\//i', '', $url); // Remove http(s)://
	$tmpdomain = preg_replace('/\/.*$/i', '', $tmpdomain); // Remove part after domain
	if ($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'
	}
	if (empty($mode)) {
		$tmpdomain = preg_replace('/\.[^\.]+$/', '', $tmpdomain); // Remove first level domain (.com, .net, ...)
	}

	return $tmpdomain;
}

Results of the workarround

image

image

eldy commented

Thanks for your accurate analysis.
Using domain with a TLD on 2 parts instead of 1, like .com.mx was not taken into account during development of sellyoursaas.

A better fix can be done in Dolibarr in method getDomainFromURL :

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'
	);

	// Set if tld is on 2 levels
	$tldon2level = 0;
	$parts = array_reverse(explode('.', $url));
	if (!empty($parts[1]) && in_array($parts[1].'.'.$parts[0], $arrayof2levetopdomain)) {
		$tldon2level = 1;
	}

	if ($tldon2level && $mode > 0) {
		$mode++;
	}

	$tmpdomain = preg_replace('/^https?:\/\//i', '', $url); // Remove http(s)://
	$tmpdomain = preg_replace('/\/.*$/i', '', $tmpdomain); // Remove part after /
	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'
	} elseif ($mode == 1) {
		$tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)$/', '\1.\2', $tmpdomain); // Remove part 'www.abc.' before 'mydomain.com'
	}

	if (empty($mode)) {
		if ($tldon2level) {
			$tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)\.([^\.]+)$/', '\1.\2.\3', $tmpdomain); // Remove part 'www.abc.' before 'mydomain.com'
			$tmpdomain = preg_replace('/\.[^\.]+\.[^\.]+$/', '', $tmpdomain); // Remove TLD (.com.mx, .co.uk, ...)
		} else {
			$tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)$/', '\1.\2', $tmpdomain); // Remove part 'www.abc.' before 'mydomain.com'
			$tmpdomain = preg_replace('/\.[^\.]+$/', '', $tmpdomain); // Remove TLD (.com, .net, ...)
		}
	}

	return $tmpdomain;
}

eldy commented

Don't know if the fix into function getDomainFromURL() that will be available in standard dolibarr is enough. Thanks to reopen if you experience other errors depsite this change.