1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\NetworkUtilities; |
6
|
|
|
|
7
|
|
|
class DnsHelper |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param string $hostname hostname without dot at end |
11
|
|
|
* @return bool |
12
|
|
|
* @link https://bugs.php.net/bug.php?id=78008 |
13
|
|
|
*/ |
14
|
2 |
|
public static function existsMx(string $hostname): bool |
15
|
|
|
{ |
16
|
2 |
|
$hostname = rtrim($hostname, '.') . '.'; |
17
|
|
|
try { |
18
|
2 |
|
if (!@dns_check_record($hostname, 'MX')) { |
19
|
2 |
|
return false; |
20
|
|
|
} |
21
|
2 |
|
$result = @dns_get_record($hostname, DNS_MX); |
22
|
2 |
|
return $result !== false && count($result) > 0; |
23
|
|
|
} catch (\Throwable $t) { |
24
|
|
|
assert($t); |
25
|
|
|
// eg. name servers are not found https://github.com/yiisoft/yii2/issues/17602 |
26
|
|
|
} |
27
|
|
|
return false; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @link https://bugs.php.net/bug.php?id=78008 |
32
|
|
|
* @param string $hostname |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
2 |
|
public static function existsA(string $hostname): bool |
36
|
|
|
{ |
37
|
|
|
try { |
38
|
2 |
|
if (!@dns_check_record($hostname, 'A')) { |
39
|
2 |
|
return false; |
40
|
|
|
} |
41
|
1 |
|
$result = @dns_get_record($hostname, DNS_A); |
42
|
1 |
|
return $result !== false && count($result) > 0; |
43
|
|
|
} catch (\Throwable $t) { |
44
|
|
|
assert($t); |
45
|
|
|
// eg. name servers are not found https://github.com/yiisoft/yii2/issues/17602 |
46
|
|
|
} |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @link https://tools.ietf.org/html/rfc5321#section-5 |
52
|
|
|
* @param string $hostnameOrEmail |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
1 |
|
public static function acceptsEmails(string $hostnameOrEmail): bool |
56
|
|
|
{ |
57
|
1 |
|
if (strpos($hostnameOrEmail, '@') !== false) { |
58
|
1 |
|
[, $hostnameOrEmail] = explode('@', $hostnameOrEmail, 2); |
59
|
|
|
} |
60
|
1 |
|
return self::existsMx($hostnameOrEmail) || self::existsA($hostnameOrEmail); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|