Browse Source

initial commit

main
ghost 1 year ago
parent
commit
df3342fc4b
  1. 1
      .gitignore
  2. 34
      README.md
  3. 11
      composer.json
  4. 58
      src/Dig.php

1
.gitignore vendored

@ -0,0 +1 @@ @@ -0,0 +1 @@
/vendor/

34
README.md

@ -1,2 +1,34 @@ @@ -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')
);

11
composer.json

@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
{
"name": "yggverse/dns",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Yggverse\\Dns\\": "src/"
}
},
"require": {}
}

58
src/Dig.php

@ -0,0 +1,58 @@ @@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace Yggverse\Dns;
class Dig
{
private static function _records(): array
{
return
[
'A' => 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;
}
}
Loading…
Cancel
Save