Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
omnilance.php
1<?php
3
4use \Bitrix\Main\Text\Encoding;
5
6class Omnilance extends Registrar
7{
11 const BASE_ENDPOINT = 'https://api.omnilance.com/v3/';
12
17 private $apiKey;
18
23 private $secretKey;
24
29 private $http;
30
36 public function __construct(string $apiKey, string $secretKey)
37 {
38 $this->apiKey = $apiKey;
39 $this->secretKey = $secretKey;
40 $this->http = new \Bitrix\Main\Web\HttpClient;
41 $this->http->setTimeout(5);
42 }
43
50 private function setHeaders(string $endPoint, ?string $payLoad = null): void
51 {
52 if (!$payLoad)
53 {
54 $payLoad = '';
55 }
56
57 $signature = hash_hmac('sha256', $this->apiKey . $endPoint . $payLoad, $this->secretKey);
58 $this->http->setHeader('X-OMNI-APIKEY', $this->apiKey);
59 $this->http->setHeader('X-OMNI-SIGNATURE', $signature);
60 $this->http->setHeader('content-type', 'application/json');
61 }
62
69 private function sendPostCommand(string $endPoint, string $payLoad): string
70 {
71 $endPoint = $this::BASE_ENDPOINT . $endPoint;
72 $this->setHeaders($endPoint, $payLoad);
73 $this->http->post($endPoint, $payLoad);
74 return $this->http->getResult();
75 }
76
82 private function sendGetCommand(string $endPoint): string
83 {
84 $endPoint = $this::BASE_ENDPOINT . $endPoint;
85 $this->setHeaders($endPoint);
86 $this->http->get($endPoint);
87 return $this->http->getResult();
88 }
89
98 public static function checkDomain(string $user, string $password, string $domain, ?string &$error): ?bool
99 {
100 $domain = mb_strtolower($domain);
101 $domain = Encoding::convertEncoding($domain, SITE_CHARSET, 'UTF-8');
102
103 $omnilance = new self($user, $password);
104
105 $payLoad = json_encode([
106 'domainNames' => [
107 $domain
108 ]
109 ]);
110 $res = $omnilance->sendPostCommand('domains/checkAvailability', $payLoad);
111 $res = json_decode($res, true);
112
113 if (isset($res['error']))
114 {
115 $error = $res['message'] ?? $res['error'];
116 return null;
117 }
118
119 if (isset($res['results']) && is_array($res['results']))
120 {
121 foreach ($res['results'] as $item)
122 {
123 if ($item['domainName'] == $domain)
124 {
125 if ($item['status'] == 'registered')
126 {
127 return true;
128 }
129 break;
130 }
131 }
132 }
133
134 return false;
135 }
136
147 public static function suggestDomain(string $user, string $password, string $word1, string $word2, array $tlds, ?string &$error): ?array
148 {
149 // method is not allowed by provider
150 return null;
151 }
152
162 public static function createDomain(string $user, string $password, string $domain, array $params, ?string &$error): ?bool
163 {
164 $domain = mb_strtolower($domain);
165 $domain = Encoding::convertEncoding($domain, SITE_CHARSET, 'UTF-8');
166
167 $payLoad = json_encode([
168 'domain' => [
169 'domainName' => $domain,
170 'privacyEnabled' => true
171 ],
172 'years' => 1
173 ]);
174
175 $omnilance = new self($user, $password);
176 $res = $omnilance->sendPostCommand('domains/createDomain', $payLoad);
177 $res = json_decode($res, true);
178
179 if (isset($res['error']))
180 {
181 $error = $res['message'] ?? $res['error'];
182 return null;
183 }
184
185 return true;
186 }
187
196 public static function renewDomain(string $user, string $password, string $domain, ?string &$error): ?bool
197 {
198 $domain = mb_strtolower($domain);
199 $domain = Encoding::convertEncoding($domain, SITE_CHARSET, 'UTF-8');
200
201 $payLoad = json_encode([
202 'domain' => [
203 'domainName' => $domain
204 ],
205 'years' => 1
206 ]);
207
208 $omnilance = new self($user, $password);
209 $res = $omnilance->sendPostCommand('domains/renewDomain/'.$domain, $payLoad);
210 $res = json_decode($res, true);
211
212 if (isset($res['error']))
213 {
214 $error = $res['message'] ?? $res['error'];
215 return null;
216 }
217
218 return true;
219 }
220
230 public static function updateDns(string $user, string $password, string $domain, array $params, ?string &$error): ?bool
231 {
232 $error = null;
233 $domain = mb_strtolower($domain);
234 $domain = Encoding::convertEncoding($domain, SITE_CHARSET, 'UTF-8');
235 $params = Encoding::convertEncoding($params, SITE_CHARSET, 'UTF-8');
236
237 $first = true;
238 foreach ($params as $dns)
239 {
240 $payLoad = [
241 'type' => (isset($dns['type']) && is_string($dns['type'])) ? strtoupper($dns['type']) : null,
242 'name' => (isset($dns['name']) && is_string($dns['name'])) ? $dns['name'] : '',
243 'value' => isset($dns['value']) ? [$dns['value']] : [],
244 'ttl' => 3600
245 ];
246 $payLoad = json_encode($payLoad);
247
248 $omnilance = new self($user, $password);
249
250 if ($first)
251 {
252 $res = $omnilance->sendPostCommand('domains/createZoneRecord/'.$domain, $payLoad);
253 }
254 else
255 {
256 $res = $omnilance->sendPostCommand('domains/createDnsRecord/'.$domain, $payLoad);
257 }
258
259 $res = json_decode($res, true);
260
261 if (isset($res['error']))
262 {
263 $error = $res['message'] ?? $res['error'];
264 }
265 $first = false;
266 }
267
268 return $error === null;
269 }
270
278 public static function getDomainsList(string $user, string $password, ?string &$error): ?array
279 {
280 $list = [];
281 $currentPage = 1;
282 $omnilance = new self($user, $password);
283
284 do
285 {
286 $res = $omnilance->sendGetCommand('domains/100/' . $currentPage);
287 $res = json_decode($res, true);
288
289 if (isset($res['error']))
290 {
291 $error = $res['message'] ?? $res['error'];
292 return null;
293 }
294
295 if (!isset($res['domains']) || !isset($res['lastPage']))
296 {
297 $error = 'Unknown error';
298 return null;
299 }
300
301 foreach ($res['domains'] as $domain)
302 {
303 $list[$domain['domainName']] = [
304 'domain_name' => $domain['domainName'],
305 'creation_date' => $domain['createDate'],
306 'expiration_date' => $domain['expireDate'],
307 'status' => null
308 ];
309 }
310
311 $currentPage++;
312
313 } while ($currentPage <= $res['lastPage']);
314
315 return $list;
316 }
317}
static renewDomain(string $user, string $password, string $domain, ?string &$error)
static updateDns(string $user, string $password, string $domain, array $params, ?string &$error)
static suggestDomain(string $user, string $password, string $word1, string $word2, array $tlds, ?string &$error)
static checkDomain(string $user, string $password, string $domain, ?string &$error)
Definition omnilance.php:98
static createDomain(string $user, string $password, string $domain, array $params, ?string &$error)
static getDomainsList(string $user, string $password, ?string &$error)
__construct(string $apiKey, string $secretKey)
Definition omnilance.php:36