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 0e5baba..7ce78d6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,34 @@ # dns-api + Simple DNS API + +## Install + +``` +git clone https://github.com/YGGverse/dns-api.git +cd dns-api +composer install +``` + +## Run + +``` +cd src/public +php -S localhost:8080 +``` + +## Usage + +### Dig + +#### Single record + +``` +dig.php?name=php.net&record=A +``` + +#### Multiple records + +``` +dig.php?name=php.net&records[]=A&records[]=AAAA +``` \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..d1fa667 --- /dev/null +++ b/composer.json @@ -0,0 +1,13 @@ +{ + "name": "yggverse/dns-api", + "type": "project", + "license": "MIT", + "autoload": { + "psr-4": { + "Yggverse\\DnsApi\\": "src/" + } + }, + "require": { + "yggverse/dns": "^1.0" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..269f886 --- /dev/null +++ b/composer.lock @@ -0,0 +1,49 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "4fd3c797b2c98cf2e93558b64c5da47b", + "packages": [ + { + "name": "yggverse/dns", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/YGGverse/dns-php.git", + "reference": "e54a40bb673e3c4cb87d8d7275adc7121042281e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/YGGverse/dns-php/zipball/e54a40bb673e3c4cb87d8d7275adc7121042281e", + "reference": "e54a40bb673e3c4cb87d8d7275adc7121042281e", + "shasum": "" + }, + "type": "library", + "autoload": { + "psr-4": { + "Yggverse\\Dns\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "support": { + "issues": "https://github.com/YGGverse/dns-php/issues", + "source": "https://github.com/YGGverse/dns-php/tree/1.0.0" + }, + "time": "2023-12-15T16:19:42+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.3.0" +} diff --git a/src/public/dig.php b/src/public/dig.php new file mode 100644 index 0000000..bad5ea1 --- /dev/null +++ b/src/public/dig.php @@ -0,0 +1,92 @@ + false, + 'message' => _('valid name required') + ] + ) + ); +} + +// Valid records required to continue +$records = []; + +if (isset($_GET['record']) && is_string($_GET['record']) && \Yggverse\Dns\Dig::isRecord($_GET['record'])) +{ + $records[] = $_GET['record']; +} + +if (isset($_GET['records']) && is_array($_GET['records'])) +{ + foreach ($_GET['records'] as $record) + { + if (is_string($record) && \Yggverse\Dns\Dig::isRecord($record)) + { + $records[] = $record; + } + } +} + +// At least one record required +if (empty($records)) +{ + exit( + json_encode( + [ + 'success' => false, + 'message' => sprintf( + _('valid record(s) required: %s'), + implode( + ',', + array_unique( + $records + ) + ) + ) + ] + ) + ); +} + +// Resolve begin +if (!$result = \Yggverse\Dns\Dig::records($_GET['name'], array_unique($records))) +{ + exit( + json_encode( + [ + 'success' => false, + 'message' => sprintf( + _('%s records for %s not found'), + implode( + ',', + array_unique( + $records + ) + ), + $_GET['name'] + ) + ] + ) + ); +} + +// Done +exit( + json_encode( + [ + 'success' => true, + 'records' => $result + ] + ) +);