From 5d22aa8235e4ea250328daae6b8c39c9f58badd6 Mon Sep 17 00:00:00 2001 From: Alex Javadi Date: Tue, 20 Feb 2024 22:06:48 +0000 Subject: [PATCH] V1.0.0 - Beta --- demo/demo.php | 68 +++++++++++++++++++++ src/AuthoriaDNS.php | 146 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100644 demo/demo.php create mode 100644 src/AuthoriaDNS.php diff --git a/demo/demo.php b/demo/demo.php new file mode 100644 index 0000000..0736463 --- /dev/null +++ b/demo/demo.php @@ -0,0 +1,68 @@ +new("example.com")); +// Response: +/* + Array + ( + [id] => UUID + [token] => authoria-dns-verification=HASH_TOKEN + [how_to_verify] => Add a TXT record with the value 'authoria-dns-verification=HASH_TOKEN' to your domain's DNS records + ) +*/ + +// Get Verification Request Status +print_r($authoria->verify("YOUR-VERIFICATION-ID")); +// Response: +/* + Array + ( + [id] => UUID + [domain] => example.com + [verified] => true || false (boolean) + [status] => PENDING || VERIFIED || EXPIRED || NOT_FOUND (string) + ) +*/ + + +// Bulk Verification Request Status +print_r($authoria->bulkVerify(["YOUR-VERIFICATION-ID-1", "YOUR-VERIFICATION-ID-2"])); +// Response: +/* + Array + ( + Array + ( + [id] => UUID + [domain] => example.com + [verified] => true || false (boolean) + [status] => PENDING || VERIFIED || EXPIRED || NOT_FOUND (string) + ) + , + Array + ( + [id] => UUID + [domain] => example.com + [verified] => true || false (boolean) + [status] => PENDING || VERIFIED || EXPIRED || NOT_FOUND (string) + ) + ) +*/ diff --git a/src/AuthoriaDNS.php b/src/AuthoriaDNS.php new file mode 100644 index 0000000..d8fa143 --- /dev/null +++ b/src/AuthoriaDNS.php @@ -0,0 +1,146 @@ +config['base_url'] . $path); + + // Set the cURL options + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + if (!$this->config['ssl_check']) { + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + } + + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, + (in_array(strtoupper($method), ['GET', 'POST', 'PUT', 'OPTIONS']) ? strtoupper($method) : throw new \RuntimeException('Invalid HTTP method!')) + ); + + if ($data) { + curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: ' . ($json_body ? 'application/json' : 'application/x-www-form-urlencoded')]); + curl_setopt($ch, CURLOPT_POSTFIELDS, ($json_body ? json_encode($data) : http_build_query($data))); + } + + // Execute the request + $response = curl_exec($ch); + + // Check for errors + if ($response === false) { + throw new \Exception(curl_error($ch), curl_errno($ch)); + } + + return json_decode($response, true) ?? throw new \RuntimeException('AuthoriaDNS API response could not be decoded! ' . $response); + } + catch (\Exception $e) { + die("AuthoriaDNS API CRequest failed: {$e->getMessage()}.
Please try again later. (Trace: {$e->getTraceAsString()})."); + } + finally { + // Close the cURL session + curl_close($ch); + } + } + + /** + * AuthoriaDNS constructor. + * @param string $authoriaDnsInstanceURL The URL of the AuthoriaDNS instance to connect to + * @param bool $ssl_check Whether to check the SSL certificate (default is true) + * @throws \Exception If the URL is invalid, or the server is not reachable or doesn't respond + */ + public function __construct(string $authoriaDnsInstanceURL, bool $ssl_check = true) + { + + try { + + // If URL hasn't http or https, we'll add it + if (!preg_match('/^https?:\/\//', $authoriaDnsInstanceURL)) + $authoriaDnsInstanceURL = ($ssl_check ? 'https://' : 'http://') . $authoriaDnsInstanceURL; + + // Check if the URL is valid + if (!filter_var($authoriaDnsInstanceURL, FILTER_VALIDATE_URL)) { + throw new \RuntimeException('Invalid AuthoriaDNS instance URL!'); + } + + $this->config = [ + 'base_url' => $authoriaDnsInstanceURL . '/api/v1', + 'ssl_check' => $ssl_check + ]; + + // verify the URL + $server_response = $this->RequestManager("/is-that-authoria", 'OPTIONS', null); + + + if (!$server_response['authoria'] ?? false) { + throw new \RuntimeException('The server is not an AuthoriaDNS instance!'); + } + } + catch (\Exception $e) { + die("AuthoriaDNS API Initialisation failed: {$e->getMessage()}.
Please try again later. (Trace: {$e->getTraceAsString()})."); + } + } + + /** + * New DNS Verification Request + * @param string $domain The domain to verify (e.g. example.com) + * @param int $ttl The TTL for the DNS record (in seconds, default is 300 seconds = 5 minutes) + * @return string|array The ID of the verification request plus the token, or an error message + * @throws \Exception If the request fails, an exception is thrown + */ + public function new(string $domain, int $ttl = 300): string|array + { + $req = $this->RequestManager('/new', 'POST', ['domain' => $domain, 'ttl' => $ttl]); + + if ($req['error'] ?? false) { + return $req['error']; + } + + return ['id' => $req['id'], 'token' => $req['TXT_record_to_verify'], 'how_to_verify' => "Add a TXT record with the value '{$req['TXT_record_to_verify']}' to your domain's DNS records"]; + } + + /** + * Get DNS Verification Request Details (and the Status of the Verification) + * @param string $id The ID of the verification request + * @return array The details of the verification request, including the status + * @throws \Exception If the request fails, an exception is thrown + */ + public function verify(string $id): array + { + return $this->RequestManager("/verify?id=$id", 'GET', null); + } + + /** + * Bulk Result of DNS Verification Requests + * @param array $ids The IDs of the verification requests + * @return array The details of the verification requests, including the status + * @throws \Exception If the request fails, an exception is thrown + */ + public function bulkVerify(array $ids): array + { + return $this->RequestManager("/bulk-verify", 'POST', ['ids' => $ids], true); + } +} \ No newline at end of file