NEW Add function isValidMXRecord

This commit is contained in:
Laurent Destailleur
2018-07-04 11:07:28 +02:00
parent a2f517685e
commit 91b7ee5fc2
2 changed files with 54 additions and 0 deletions

View File

@@ -2782,6 +2782,35 @@ function isValidEmail($address, $acceptsupervisorkey=0)
return false;
}
/**
* Return if the domain name has a valid MX record.
* WARNING: This need function idn_to_ascii, checkdnsrr and getmxrr
*
* @param string $domain Domain name (Ex: "yahoo.com", "yhaoo.com", "dolibarr.fr")
* @return int -1 if error (function not available), 0=Not valid, 1=Valid
*/
function isValidMXRecord($domain)
{
if (function_exists('idn_to_ascii') && function_exists('checkdnsrr'))
{
if (! checkdnsrr(idn_to_ascii($domain), 'MX'))
{
return 0;
}
if (function_exists('getmxrr'))
{
$mxhosts=array();
$weight=array();
getmxrr(idn_to_ascii($domain), $mxhosts, $weight);
if (count($mxhosts) > 1) return 1;
if (count($mxhosts) == 1 && ! empty($mxhosts[0])) return 1;
return 0;
}
}
return -1;
}
/**
* Return true if phone number syntax is ok
* TODO Decide what to do with this

View File

@@ -122,6 +122,31 @@ class FunctionsLibTest extends PHPUnit_Framework_TestCase
/**
* testIsValidMXRecord
*
* @return void
*/
public function testIsValidMXRecord()
{
// Nb of line is same than entry text
$input="yahoo.com";
$result=isValidMXRecord($input);
print __METHOD__." result=".$result."\n";
$this->assertEquals(1, $result);
$input="yhaoo.com";
$result=isValidMXRecord($input);
print __METHOD__." result=".$result."\n";
$this->assertEquals(0, $result);
$input="dolibarr.fr";
$result=isValidMXRecord($input);
print __METHOD__." result=".$result."\n";
$this->assertEquals(0, $result);
}
/**
* testDolGetFirstLineOfText
*