Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F125361355
D5894.1779443033.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
5 KB
Referenced Files
None
Subscribers
None
D5894.1779443033.diff
View Options
diff --git a/src/app/Domain.php b/src/app/Domain.php
--- a/src/app/Domain.php
+++ b/src/app/Domain.php
@@ -146,8 +146,7 @@
}
/**
- * Returns whether this (external) domain has been verified
- * to exist in DNS.
+ * Returns whether this (external) domain has been verified to exist in DNS.
*/
public function isVerified(): bool
{
@@ -218,11 +217,7 @@
$confirmed = false;
// Get DNS records and find a matching TXT entry
- $records = \dns_get_record($this->namespace, \DNS_TXT);
-
- if ($records === false) {
- throw new \Exception("Failed to get DNS record for {$this->namespace}");
- }
+ $records = Utils::getDNSRecord($this->namespace, \DNS_TXT);
foreach ($records as $record) {
if ($record['txt'] === $hash) {
@@ -237,11 +232,7 @@
// i.e.: kolab-verify IN CNAME <hash>.domain.tld.
if (!$confirmed) {
$cname = $this->hash(self::HASH_CODE) . '.' . $this->namespace;
- $records = \dns_get_record('kolab-verify.' . $this->namespace, \DNS_CNAME);
-
- if ($records === false) {
- throw new \Exception("Failed to get DNS record for {$this->namespace}");
- }
+ $records = Utils::getDNSRecord('kolab-verify.' . $this->namespace, \DNS_CNAME);
foreach ($records as $records) {
if ($records['target'] === $cname) {
@@ -356,11 +347,7 @@
return true;
}
- $records = \dns_get_record($this->namespace, \DNS_ANY);
-
- if ($records === false) {
- throw new \Exception("Failed to get DNS record for {$this->namespace}");
- }
+ $records = Utils::getDNSRecord($this->namespace, \DNS_ANY);
// It may happen that result contains other domains depending on the host DNS setup
// that's why in_array() and not just !empty()
diff --git a/src/app/Http/Resources/DomainInfoResource.php b/src/app/Http/Resources/DomainInfoResource.php
--- a/src/app/Http/Resources/DomainInfoResource.php
+++ b/src/app/Http/Resources/DomainInfoResource.php
@@ -4,6 +4,7 @@
use App\Domain;
use App\Http\Controllers\API\V4\DomainsController;
+use App\Utils;
use Illuminate\Http\Request;
/**
@@ -56,14 +57,16 @@
// copy MX entries from an existing domain
if ($master = \config('dns.copyfrom')) {
// TODO: cache this lookup
- foreach ((array) dns_get_record($master, \DNS_MX) as $entry) {
- $entries[] = sprintf(
- "@\t%s\t%s\tMX\t%d %s.",
- \config('dns.ttl', $entry['ttl']),
- $entry['class'],
- $entry['pri'],
- $entry['target']
- );
+ if (($dns = Utils::getDNSRecord($master, \DNS_MX, false)) !== false) {
+ foreach ($dns as $entry) {
+ $entries[] = sprintf(
+ "@\t%s\t%s\tMX\t%d %s.",
+ \config('dns.ttl', $entry['ttl']),
+ $entry['class'],
+ $entry['pri'],
+ $entry['target']
+ );
+ }
}
} elseif ($static = \config('dns.static')) {
$entries[] = strtr($static, ['\n' => "\n", '%s' => $namespace]);
diff --git a/src/app/Utils.php b/src/app/Utils.php
--- a/src/app/Utils.php
+++ b/src/app/Utils.php
@@ -206,6 +206,36 @@
return $result;
}
+ /**
+ * Retrieve DNS record(s)
+ *
+ * @param string $domain Domain namespace
+ * @param int $type Record type
+ * @param bool $throw Throw an exception on failure
+ *
+ * @return array<array>|false
+ */
+ public static function getDNSRecord(string $domain, int $type = \DNS_ANY, bool $throw = true)
+ {
+ $error = null;
+ $error_handler = static function ($errno, $errstr, $errfile, $errline) use (&$error) {
+ $error = $errstr;
+ return true;
+ };
+
+ set_error_handler($error_handler, \E_WARNING);
+
+ $records = dns_get_record($domain, $type);
+
+ restore_error_handler();
+
+ if ($throw && $records === false) {
+ throw new \Exception("Failed to get DNS record for {$domain}: {$error}");
+ }
+
+ return $records;
+ }
+
/**
* Retrieve the network ID and Type from a client address
*
diff --git a/src/tests/Unit/UtilsTest.php b/src/tests/Unit/UtilsTest.php
--- a/src/tests/Unit/UtilsTest.php
+++ b/src/tests/Unit/UtilsTest.php
@@ -96,6 +96,18 @@
$this->assertSame(['anyone, lrswitednp'], Utils::ensureAclPostPermission($acl));
}
+ /**
+ * Test for Utils::getDNSRecord()
+ */
+ public function testGetDNSRecord(): void
+ {
+ $this->assertTrue(is_array(Utils::getDNSRecord('kolab.org', \DNS_ANY)));
+ $this->assertSame([], Utils::getDNSRecord('kolab.unknown.abcdef.ch', \DNS_ANY));
+ $this->assertFalse(Utils::getDNSRecord('1', \DNS_ANY, false));
+ $this->expectException(\Exception::class);
+ $this->assertFalse(Utils::getDNSRecord('1', \DNS_ANY));
+ }
+
/**
* Test for Utils::isSoftDeletable()
*/
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, May 22, 9:43 AM (15 h, 18 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18929688
Default Alt Text
D5894.1779443033.diff (5 KB)
Attached To
Mode
D5894: Nicely handle warnings on get_dns_record()
Attached
Detach File
Event Timeline