From 0fc51eba1ea92b028889170c4f484a7c20edb66b Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Fri, 21 Feb 2020 00:15:53 +0200 Subject: [PATCH] Added DNS validator --- composer.json | 1 + composer.lock | 16 +++--- src/Network/Validators/CNAME.php | 54 +++++++++++++++++++++ tests/unit/Network/Validators/CNAMETest.php | 34 +++++++++++++ 4 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 src/Network/Validators/CNAME.php create mode 100644 tests/unit/Network/Validators/CNAMETest.php diff --git a/composer.json b/composer.json index eaf52eb383..ab6e0d5a1b 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "Auth\\": "src/Auth", "Database\\": "src/Database", "Event\\": "src/Event", + "Network\\": "src/Network", "OpenSSL\\": "src/OpenSSL", "Resize\\": "src/Resize", "Storage\\": "src/Storage", diff --git a/composer.lock b/composer.lock index 1c54ab5cd6..ea54385d31 100644 --- a/composer.lock +++ b/composer.lock @@ -1935,12 +1935,12 @@ "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "edea3e869367921987704325b997a80cdaea6d66" + "reference": "5463f8d32cf0f31f3d8e6230eb80170a2602b55a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/edea3e869367921987704325b997a80cdaea6d66", - "reference": "edea3e869367921987704325b997a80cdaea6d66", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/5463f8d32cf0f31f3d8e6230eb80170a2602b55a", + "reference": "5463f8d32cf0f31f3d8e6230eb80170a2602b55a", "shasum": "" }, "require": { @@ -1980,7 +1980,7 @@ } ], "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", - "time": "2020-02-12T20:05:04+00:00" + "time": "2020-02-20T20:42:36+00:00" }, { "name": "phpdocumentor/type-resolver", @@ -1988,12 +1988,12 @@ "source": { "type": "git", "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "70d7abb1fdad964379676d629923de0dd1918f4f" + "reference": "7462d5f123dfc080dfdf26897032a6513644fc95" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/70d7abb1fdad964379676d629923de0dd1918f4f", - "reference": "70d7abb1fdad964379676d629923de0dd1918f4f", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/7462d5f123dfc080dfdf26897032a6513644fc95", + "reference": "7462d5f123dfc080dfdf26897032a6513644fc95", "shasum": "" }, "require": { @@ -2026,7 +2026,7 @@ } ], "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", - "time": "2020-02-14T12:50:24+00:00" + "time": "2020-02-18T18:59:58+00:00" }, { "name": "phpspec/prophecy", diff --git a/src/Network/Validators/CNAME.php b/src/Network/Validators/CNAME.php new file mode 100644 index 0000000000..188dba2a55 --- /dev/null +++ b/src/Network/Validators/CNAME.php @@ -0,0 +1,54 @@ +target = $target; + } + + public function getDescription() + { + return 'Inavlid CNAME record'; + } + + /** + * Check if CNAME record target value matches selected target + * + * @param string $domain + * + * @return bool + */ + public function isValid($domain) + { + try { + $records = dns_get_record($domain, DNS_CNAME); + } catch (\Throwable $th) { + return false; + } + + if(!$records || !is_array($records)) { + return false; + } + + foreach($records as $record) { + if(isset($record['target']) && $record['target'] === $this->target) { + return true; + } + } + + return false; + } +} diff --git a/tests/unit/Network/Validators/CNAMETest.php b/tests/unit/Network/Validators/CNAMETest.php new file mode 100644 index 0000000000..8107251977 --- /dev/null +++ b/tests/unit/Network/Validators/CNAMETest.php @@ -0,0 +1,34 @@ +object = new CNAME('new.appwrite.io'); + } + + public function tearDown() + { + } + + public function testValues() + { + $this->assertEquals($this->object->isValid(''), false); + $this->assertEquals($this->object->isValid(null), false); + $this->assertEquals($this->object->isValid(false), false); + $this->assertEquals($this->object->isValid('cert1.tests.appwrite.io'), true); + $this->assertEquals($this->object->isValid('cert2.tests.appwrite.io'), true); + $this->assertEquals($this->object->isValid('cert1.tests.appwrite.com'), false); + $this->assertEquals($this->object->isValid('cert1.tests.appwrite.com'), false); + } +} \ No newline at end of file