From 55ad6b4fad2aa8e4e941c40980bdc5afc406881b Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sat, 10 Dec 2022 22:07:11 +0300 Subject: composer support --- .gitignore | 2 + FastDNS.php | 307 ----------------------------------------------- README.md | 9 ++ composer.json | 21 ++++ src/FastDNS.php | 307 +++++++++++++++++++++++++++++++++++++++++++++++ src/FastDNSException.php | 5 + 6 files changed, 344 insertions(+), 307 deletions(-) create mode 100644 .gitignore delete mode 100644 FastDNS.php create mode 100644 composer.json create mode 100644 src/FastDNS.php create mode 100644 src/FastDNSException.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f45219c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor/ +/.idea \ No newline at end of file diff --git a/FastDNS.php b/FastDNS.php deleted file mode 100644 index f7c9736..0000000 --- a/FastDNS.php +++ /dev/null @@ -1,307 +0,0 @@ -jwtToken = $response['token']; - $this->expire = $response['expire']; - } - - /** - * @return mixed - * @throws FastDNSException - */ - public function getDomains() { - return $this->get('/api/domains', []); - } - - /** - * @param $id - * @return mixed - * @throws FastDNSException - */ - public function getDomain($id) { - return $this->get('/api/domains/'.$id); - } - - /** - * @param $name - * @return mixed - * @throws FastDNSException - */ - public function getDomainByName($name) { - return $this->get('/api/domains/'.$name.'/name'); - } - - /** - * @param string $name - * @param string $ip - * @param int $mail_service - * @return mixed - * @throws FastDNSException - */ - public function createDomain(string $name, string $ip, int $mail_service = self::MAIL_SERVICE_MAIN) { - return $this->post('/api/domains', [ - 'name' => $name, - 'ip' => $ip, - 'mail_service' => $mail_service - ]); - } - - /** - * No idea what this does and what "required" means - * - * @param $domain_id - * @param int $required - * @return mixed - * @throws FastDNSException - */ - public function updateDomain($domain_id, int $required) { - return $this->put('/api/domains/'.$domain_id, [ - 'required' => $required - ]); - } - - /** - * @param int $domain_id - * @return mixed - * @throws FastDNSException - */ - public function deleteDomain(int $domain_id) { - return $this->delete('/api/domains/'.$domain_id); - } - - /** - * @param int $domain_id - * @return mixed - * @throws FastDNSException - */ - public function getRecords(int $domain_id) { - return $this->get('/api/domains/'.$domain_id.'/records'); - } - - /** - * @param int $domain_id - * @param string $name - * @param string $type - * @param string $content - * @param int $ttl - * @param string $tag - * @param int $flag - * @param int $priority - * @param int $weight - * @param int $port - * @return mixed - * @throws FastDNSException - */ - public function createRecord( - int $domain_id, - string $name, - string $type, - string $content, - int $ttl, - string $tag = '', - int $flag = 0, - int $priority = 5, - int $weight = 0, - int $port = 0) { - return $this->post('/api/domains/'.$domain_id.'/records', [ - 'name' => $name, - 'type' => $type, - 'content' => $content, - 'ttl' => $ttl, - 'tag' => $tag, - 'flag' => $flag, - 'priority' => $priority, - 'weight' => $weight, - 'port' => $port - ]); - } - - /** - * @param int $domain_id - * @param string $record_id - * @return mixed - * @throws FastDNSException - */ - public function getRecord(int $domain_id, string $record_id) { - return $this->get('/api/domains/'.$domain_id.'/records/'.$record_id); - } - - /** - * @param int $domain_id - * @param string $record_id - * @param string $name - * @param string $type - * @param string $content - * @param int $ttl - * @param string $tag - * @param int $flag - * @param int $priority - * @param int $weight - * @param int $port - * @return mixed - * @throws FastDNSException - */ - public function updateRecord( - int $domain_id, - string $record_id, - string $name, - string $type, - string $content, - int $ttl, - string $tag = '', - int $flag = 0, - int $priority = 5, - int $weight = 0, - int $port = 0) { - - // если здесь передавать все поля, то сервер возвращает ошибку "запись уже существует", - return $this->put('/api/domains/'.$domain_id.'/records/'.$record_id, [ - // 'type' => $type, - 'content' => $content, - 'name' => $name, - // 'ttl' => $ttl, - // 'tag' => $tag, - // 'flag' => $flag, - // 'priority' => $priority, - // 'weight' => $weight, - // 'port' => $port - ]); - } - - /** - * @param int $domain_id - * @param string $record_id - * @throws FastDNSException - */ - public function deleteRecord(int $domain_id, string $record_id) { - return $this->delete('/api/domains/'.$domain_id.'/records/'.$record_id); - } - - /** - * @return mixed - * @throws FastDNSException - */ - public function getUserInfo() { - return $this->get('/api/me'); - } - - /** - * @param string $endpoint - * @param array $params - * @return mixed - * @throws FastDNSException - */ - protected function get(string $endpoint, array $params = []) { - return $this->request('GET', $endpoint, $params); - } - - /** - * @param string $endpoint - * @param array $params - * @return mixed - * @throws FastDNSException - */ - protected function post(string $endpoint, array $params = []) { - return $this->request('POST', $endpoint, $params); - } - - /** - * @param string $endpoint - * @param array $params - * @return mixed - * @throws FastDNSException - */ - protected function put(string $endpoint, array $params = []) { - return $this->request('PUT', $endpoint, $params); - } - - /** - * @param string $endpoint - * @param array $params - * @return mixed - * @throws FastDNSException - */ - protected function delete(string $endpoint, array $params = []) { - return $this->request('DELETE', $endpoint, $params); - } - - /** - * @param string $method - * @param string $endpoint - * @param array $params - * @return mixed - * @throws FastDNSException - */ - protected function request(string $method, string $endpoint, array $params = []) { - if (!$this->jwtToken) - throw new FastDNSException(__METHOD__.': JWT token is null, forgot to authorize?'); - - $url = self::API_HOST.$endpoint; - $ch = curl_init(); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); - if (!empty($params)) { - if ($method === 'POST' || $method == 'PUT') { - curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); - } else if ($method === 'GET') { // Probably never used - $url .= '?'.http_build_query($params); - } - } - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_HTTPHEADER, [ - 'Accept: application/json', - 'Authorization: Bearer '.$this->jwtToken - ]); - $body = curl_exec($ch); - $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); - - $response = json_decode($body, true); - if ($code >= 400) { - if (!empty($response['code']) && !empty($response['message'])) { - $message = $response['message']; - $code = $response['code']; - } else { - $message = $response['errors']['name'] ?? $body; - } - throw new FastDNSException($message, $code); - } - - return $response; - } - -} - -class FastDNSException extends Exception {} \ No newline at end of file diff --git a/README.md b/README.md index de191c6..11e1d5e 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,18 @@ Полезно для автоматизации, если у вас много доменов/поддоменов. +## Установка + +```shell +composer require ch1p/php-fastdns +``` + ## Использование ```php +use ch1p\FastDNS; +use ch1p\FastDNSException; + $fastdns = new FastDNS(); try { // авторизуемся diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..70a1279 --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "ch1p/php-fastdns", + "description": "FastVPS DNS API Client", + "type": "library", + "license": "BSD-2c", + "authors": [ + { + "name": "Evgeny Zinoviev", + "email": "me@ch1p.io" + } + ], + "require": { + "ext-curl": "*", + "ext-json": "*" + }, + "autoload": { + "psr-4": { + "ch1p\\": "src/" + } + } +} diff --git a/src/FastDNS.php b/src/FastDNS.php new file mode 100644 index 0000000..7ee38cc --- /dev/null +++ b/src/FastDNS.php @@ -0,0 +1,307 @@ +jwtToken = $response['token']; + $this->expire = $response['expire']; + } + + /** + * @return mixed + * @throws FastDNSException + */ + public function getDomains() { + return $this->get('/api/domains', []); + } + + /** + * @param $id + * @return mixed + * @throws FastDNSException + */ + public function getDomain($id) { + return $this->get('/api/domains/'.$id); + } + + /** + * @param $name + * @return mixed + * @throws FastDNSException + */ + public function getDomainByName($name) { + return $this->get('/api/domains/'.$name.'/name'); + } + + /** + * @param string $name + * @param string $ip + * @param int $mail_service + * @return mixed + * @throws FastDNSException + */ + public function createDomain(string $name, string $ip, int $mail_service = self::MAIL_SERVICE_MAIN) { + return $this->post('/api/domains', [ + 'name' => $name, + 'ip' => $ip, + 'mail_service' => $mail_service + ]); + } + + /** + * No idea what this does and what "required" means + * + * @param $domain_id + * @param int $required + * @return mixed + * @throws FastDNSException + */ + public function updateDomain($domain_id, int $required) { + return $this->put('/api/domains/'.$domain_id, [ + 'required' => $required + ]); + } + + /** + * @param int $domain_id + * @return mixed + * @throws FastDNSException + */ + public function deleteDomain(int $domain_id) { + return $this->delete('/api/domains/'.$domain_id); + } + + /** + * @param int $domain_id + * @return mixed + * @throws FastDNSException + */ + public function getRecords(int $domain_id) { + return $this->get('/api/domains/'.$domain_id.'/records'); + } + + /** + * @param int $domain_id + * @param string $name + * @param string $type + * @param string $content + * @param int $ttl + * @param string $tag + * @param int $flag + * @param int $priority + * @param int $weight + * @param int $port + * @return mixed + * @throws FastDNSException + */ + public function createRecord( + int $domain_id, + string $name, + string $type, + string $content, + int $ttl, + string $tag = '', + int $flag = 0, + int $priority = 5, + int $weight = 0, + int $port = 0) { + return $this->post('/api/domains/'.$domain_id.'/records', [ + 'name' => $name, + 'type' => $type, + 'content' => $content, + 'ttl' => $ttl, + 'tag' => $tag, + 'flag' => $flag, + 'priority' => $priority, + 'weight' => $weight, + 'port' => $port + ]); + } + + /** + * @param int $domain_id + * @param string $record_id + * @return mixed + * @throws FastDNSException + */ + public function getRecord(int $domain_id, string $record_id) { + return $this->get('/api/domains/'.$domain_id.'/records/'.$record_id); + } + + /** + * @param int $domain_id + * @param string $record_id + * @param string $name + * @param string $type + * @param string $content + * @param int $ttl + * @param string $tag + * @param int $flag + * @param int $priority + * @param int $weight + * @param int $port + * @return mixed + * @throws FastDNSException + */ + public function updateRecord( + int $domain_id, + string $record_id, + string $name, + string $type, + string $content, + int $ttl, + string $tag = '', + int $flag = 0, + int $priority = 5, + int $weight = 0, + int $port = 0) { + + // если здесь передавать все поля, то сервер возвращает ошибку "запись уже существует", + return $this->put('/api/domains/'.$domain_id.'/records/'.$record_id, [ + // 'type' => $type, + 'content' => $content, + 'name' => $name, + // 'ttl' => $ttl, + // 'tag' => $tag, + // 'flag' => $flag, + // 'priority' => $priority, + // 'weight' => $weight, + // 'port' => $port + ]); + } + + /** + * @param int $domain_id + * @param string $record_id + * @throws FastDNSException + */ + public function deleteRecord(int $domain_id, string $record_id) { + return $this->delete('/api/domains/'.$domain_id.'/records/'.$record_id); + } + + /** + * @return mixed + * @throws FastDNSException + */ + public function getUserInfo() { + return $this->get('/api/me'); + } + + /** + * @param string $endpoint + * @param array $params + * @return mixed + * @throws FastDNSException + */ + protected function get(string $endpoint, array $params = []) { + return $this->request('GET', $endpoint, $params); + } + + /** + * @param string $endpoint + * @param array $params + * @return mixed + * @throws FastDNSException + */ + protected function post(string $endpoint, array $params = []) { + return $this->request('POST', $endpoint, $params); + } + + /** + * @param string $endpoint + * @param array $params + * @return mixed + * @throws FastDNSException + */ + protected function put(string $endpoint, array $params = []) { + return $this->request('PUT', $endpoint, $params); + } + + /** + * @param string $endpoint + * @param array $params + * @return mixed + * @throws FastDNSException + */ + protected function delete(string $endpoint, array $params = []) { + return $this->request('DELETE', $endpoint, $params); + } + + /** + * @param string $method + * @param string $endpoint + * @param array $params + * @return mixed + * @throws FastDNSException + */ + protected function request(string $method, string $endpoint, array $params = []) { + if (!$this->jwtToken) + throw new FastDNSException(__METHOD__.': JWT token is null, forgot to authorize?'); + + $url = self::API_HOST.$endpoint; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); + if (!empty($params)) { + if ($method === 'POST' || $method == 'PUT') { + curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); + } else if ($method === 'GET') { // Probably never used + $url .= '?'.http_build_query($params); + } + } + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Accept: application/json', + 'Authorization: Bearer '.$this->jwtToken + ]); + $body = curl_exec($ch); + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + $response = json_decode($body, true); + if ($code >= 400) { + if (!empty($response['code']) && !empty($response['message'])) { + $message = $response['message']; + $code = $response['code']; + } else { + $message = $response['errors']['name'] ?? $body; + } + throw new FastDNSException($message, $code); + } + + return $response; + } + +} \ No newline at end of file diff --git a/src/FastDNSException.php b/src/FastDNSException.php new file mode 100644 index 0000000..c8690a4 --- /dev/null +++ b/src/FastDNSException.php @@ -0,0 +1,5 @@ +