From df3342fc4bd38d8fd9bd08823cec9753d5e3ea7f Mon Sep 17 00:00:00 2001 From: ghost Date: Fri, 15 Dec 2023 18:14:45 +0200 Subject: [PATCH] initial commit --- .gitignore | 1 + README.md | 34 +++++++++++++++++++++++++++++- composer.json | 11 ++++++++++ src/Dig.php | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Dig.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/README.md b/README.md index 3585b20..3689c79 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,34 @@ # dns-php -DNS Library for PHP + +DNS Library for PHP with native Yggdrasil support + +## Install + +`composer require yggverse/dns` + +## Usage + +### Resolve records +``` +var_dump( + \Yggverse\Dns\Dig::records('yo.index', ['A', 'AAAA']) +); +``` + +## Check hostname valid + +var_dump( + \Yggverse\Dns\Dig::isHostName('yo.index') +); + +## Check record valid + +var_dump( + \Yggverse\Dns\Dig::isRecord('A') +); + +## Check record value valid + +var_dump( + \Yggverse\Dns\Dig::isRecordValue('A', '127.0.0.1') +); diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..8db11f8 --- /dev/null +++ b/composer.json @@ -0,0 +1,11 @@ +{ + "name": "yggverse/dns", + "type": "library", + "license": "MIT", + "autoload": { + "psr-4": { + "Yggverse\\Dns\\": "src/" + } + }, + "require": {} +} diff --git a/src/Dig.php b/src/Dig.php new file mode 100644 index 0000000..3611834 --- /dev/null +++ b/src/Dig.php @@ -0,0 +1,58 @@ + function(string $value): bool {return false !== filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);}, + 'AAAA' => function(string $value): bool {return false !== filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);}, + // ... + ]; + } + + public static function isHostName(string $hostname): bool + { + return false !== filter_var($hostname, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME); + } + + public static function isRecord(string $record): bool + { + return isset(self::_records()[$record]); + } + + public static function isRecordValue(string $record, string $value): bool + { + return isset(self::_records()[$record]) && self::_records()[$record]($value); + } + + public static function records(string $hostname, array $records, array &$result = [], array &$error = []): array + { + if (self::isHostName($hostname)) + { + foreach ($records as $record) + { + if (self::isRecord($record)) + { + if ($values = exec(sprintf('dig %s %s +short', $record, $hostname))) + { + foreach (explode(PHP_EOL, $values) as $value) + { + if (self::isRecordValue($record, $value)) + { + $result[$record][] = $value; + } + } + } + } + } + } + + return $result; + } +} \ No newline at end of file