generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
DnsHelper.php
82 lines (71 loc) · 2.31 KB
/
DnsHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
declare(strict_types=1);
namespace Yiisoft\NetworkUtilities;
use RuntimeException;
/**
* DnsHelper contains static methods to work with DNS.
*/
final class DnsHelper
{
/**
* Checks DNS MX record availability.
*
* @param string $hostname Hostname without dot at end.
*
* @return bool Whether MX record exists.
*/
public static function existsMx(string $hostname): bool
{
/** @psalm-suppress InvalidArgument, MixedArgumentTypeCoercion */
set_error_handler(
static function (int $errorNumber, string $errorString) use ($hostname): ?bool {
throw new RuntimeException(
sprintf('Failed to get DNS record "%s". ', $hostname) . $errorString,
$errorNumber
);
}
);
$hostname = rtrim($hostname, '.') . '.';
$result = dns_get_record($hostname, DNS_MX);
restore_error_handler();
return count($result) > 0;
}
/**
* Checks DNS A record availability.
*
* @param string $hostname Hostname without dot at end.
*
* @return bool Whether A records exists.
*/
public static function existsA(string $hostname): bool
{
/** @psalm-suppress InvalidArgument, MixedArgumentTypeCoercion */
set_error_handler(
static function (int $errorNumber, string $errorString) use ($hostname): ?bool {
throw new RuntimeException(
sprintf('Failed to get DNS record "%s". ', $hostname) . $errorString,
$errorNumber
);
}
);
$result = dns_get_record($hostname, DNS_A);
restore_error_handler();
return count($result) > 0;
}
/**
* Checks email's domain availability.
*
* @link https://tools.ietf.org/html/rfc5321#section-5
*
* @param string $hostnameOrEmail Hostname without dot at end or an email.
*
* @return bool Whether email domain is available.
*/
public static function acceptsEmails(string $hostnameOrEmail): bool
{
if (strpos($hostnameOrEmail, '@') !== false) {
[, $hostnameOrEmail] = explode('@', $hostnameOrEmail, 2);
}
return self::existsMx($hostnameOrEmail) || self::existsA($hostnameOrEmail);
}
}