キャッシュデータの作成
This commit is contained in:
@@ -4,15 +4,15 @@ namespace Std\Lib;
|
||||
use Std\Lib\Curl;
|
||||
|
||||
class Openprovider {
|
||||
protected string $dataFile = ROOT."/data/session.json";
|
||||
protected string $dataDir = ROOT."/data/cache/";
|
||||
protected int $tokenDuration = 172800; // 48時間
|
||||
|
||||
private string $user = '';
|
||||
private string $pass = '';
|
||||
private string $token = '';
|
||||
private string $ip = '';
|
||||
private int $reseller_id = 0;
|
||||
private int $last_auth = 0;
|
||||
private int $resellerId = 0;
|
||||
private int $lastAuth = 0;
|
||||
private string $BASEURL = DEBUG_MODE ? 'http://api.sandbox.openprovider.nl:8480/v1beta'
|
||||
: 'https://api.openprovider.eu/v1beta';
|
||||
|
||||
@@ -27,14 +27,18 @@ class Openprovider {
|
||||
* トークンの受け取り。
|
||||
* このライブリリーを使ったら、一回「login()」を実行する事が必須となります。
|
||||
*
|
||||
* @return Result|false 結果
|
||||
* @return Result 結果
|
||||
*/
|
||||
public function login(): \Result|false {
|
||||
public function login(): \Result {
|
||||
if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
||||
$this->getCache();
|
||||
if (time() < ($this->last_auth + $this->tokenDuration)) return false;
|
||||
$curl = new Curl("{$this->BASEURL}/auth/login");
|
||||
$cache = $this->getCache('login');
|
||||
if (time() < ($this->lastAuth + $this->tokenDuration)) {
|
||||
$this->token = $cache['data']['token'];
|
||||
$this->lastAuth = $cache['last_auth'];
|
||||
return \Result::Success('', $cache);
|
||||
}
|
||||
|
||||
$curl = new Curl("{$this->BASEURL}/auth/login");
|
||||
$payload = [
|
||||
'username' => $this->user,
|
||||
'password' => $this->pass,
|
||||
@@ -62,17 +66,17 @@ class Openprovider {
|
||||
}
|
||||
assert_not_null($body, "返事ボディーは空です。");
|
||||
|
||||
$data = json_decode($body, true);
|
||||
if (isset($data['data']['token'])) {
|
||||
$this->token = $data['data']['token'];
|
||||
$this->last_auth = time();
|
||||
$this->reseller_id = $data['data']['reseller_id'];
|
||||
$this->mkCache();
|
||||
$res = json_decode($body, true);
|
||||
if (isset($res['data']['token'])) {
|
||||
$this->token = $res['data']['token'];
|
||||
$this->lastAuth = time();
|
||||
$res['last_auth'] = $this->lastAuth;
|
||||
$this->setCache('login', $res);
|
||||
|
||||
return \Result::Success();
|
||||
return \Result::Success('', $res);
|
||||
}
|
||||
|
||||
return \Result::Error("ログインに失敗。");
|
||||
return \Result::Error('ログインに失敗。');
|
||||
}
|
||||
|
||||
// カスタマー
|
||||
@@ -84,15 +88,21 @@ class Openprovider {
|
||||
/***
|
||||
* ドメイン一覧。
|
||||
*
|
||||
* @return Result|array 結果。
|
||||
* @return Result 結果。
|
||||
*/
|
||||
public function listDomains(array $query = []): \Result|array {
|
||||
public function listDomains(array $query = []): \Result {
|
||||
if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
||||
$curl = $this->setupCurl('/domains');
|
||||
$data = $this->curlResult($curl);
|
||||
if (isset($data['data']['results'])) return $data['data']['results'];
|
||||
$cache = $this->getCache('listdomains');
|
||||
if (!empty($cache)) return \Result::Success('', $cache);
|
||||
|
||||
return \Result::Error("ドメインの確認に失敗。");
|
||||
$curl = $this->setupCurl('/domains');
|
||||
$res = $this->curlResult($curl);
|
||||
if (isset($res['data']['results'])) {
|
||||
$this->setCache('listdomains', $res->data);
|
||||
return \Result::Success('', $res['data']['results']);
|
||||
}
|
||||
|
||||
return \Result::Error('ドメインの確認に失敗。');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,9 +111,9 @@ class Openprovider {
|
||||
* @param string $name 登録したいドメイン名(例:"076studio.jp")
|
||||
* @param array $info カスタマー情報等
|
||||
* @param int $period 年間(デフォルト=1年)
|
||||
* @return Result|array 結果。配列の場合:'activation_date'(リアルタイムで成功したのみ), 'auth_code'(TLDが対応している場合のみ), 'expiration_date', 'id', 'renewal_date', 'status'(ACT又はREQ)
|
||||
* @return Result 結果。配列の場合:'activation_date'(リアルタイムで成功したのみ), 'auth_code'(TLDが対応している場合のみ), 'expiration_date', 'id', 'renewal_date', 'status'(ACT又はREQ)
|
||||
*/
|
||||
public function createDomain(string $name, array $info, int $period = 1): \Result|array {
|
||||
public function createDomain(string $name, array $info, int $period = 1): \Result {
|
||||
if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
||||
if (!isset($info['owner_handle']) || $info['owner_handle'] === '') $info['owner_handle'] = OPENPROVIDER['owner_handle'];
|
||||
if (!isset($info['admin_handle']) || $info['admin_handle'] === '') $info['admin_handle'] = OPENPROVIDER['owner_handle'];
|
||||
@@ -142,10 +152,10 @@ class Openprovider {
|
||||
|
||||
$curl = $this->setupCurl('/domains/', 'POST', $payload);
|
||||
kys('TODO');
|
||||
$data = $this->curlResult($curl);
|
||||
if (isset($data['data'])) return $data['data'];
|
||||
$res = $this->curlResult($curl);
|
||||
if (isset($res['data'])) return \Result::Success('', $res);
|
||||
|
||||
return \Result::Error("ドメインの確認に失敗。");
|
||||
return \Result::Error('ドメインの確認に失敗。');
|
||||
}
|
||||
|
||||
/***
|
||||
@@ -153,9 +163,9 @@ class Openprovider {
|
||||
*
|
||||
* @param array $domains ドメイン名リスト。例:['076.moe', '076.co.jp', '076.com']
|
||||
* @param bool $with_price 値段を表示するかどうか。デフォルトは「false」
|
||||
* @return Result|array 結果。配列の場合:'domain'と'status'はいつでもあり、'reason', 'premium', 'is_premium'が多分あり、そして'price'が「with_price」が「true」の場合のみ。
|
||||
* @return Result 結果。配列の場合:'domain'と'status'はいつでもあり、'reason', 'premium', 'is_premium'が多分あり、そして'price'が「with_price」が「true」の場合のみ。
|
||||
*/
|
||||
public function checkDomainAvailable(array $domains, bool $with_price = false): \Result|array {
|
||||
public function checkDomainAvailable(array $domains, bool $with_price = false): \Result {
|
||||
if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
||||
$domainList = [];
|
||||
foreach ($domains as $d) {
|
||||
@@ -178,10 +188,10 @@ class Openprovider {
|
||||
];
|
||||
|
||||
$curl = $this->setupCurl('/domains/check', 'POST', $payload);
|
||||
$data = $this->curlResult($curl);
|
||||
if (isset($data['data']['results'])) return $data['data']['results'];
|
||||
$res = $this->curlResult($curl);
|
||||
if (isset($res['data']['results'])) return \Result::Success('', $res['data']['results']);
|
||||
|
||||
return \Result::Error("ドメインの確認に失敗。");
|
||||
return \Result::Error('ドメインの確認に失敗。');
|
||||
}
|
||||
|
||||
//// 追加データ
|
||||
@@ -192,18 +202,24 @@ class Openprovider {
|
||||
* 顧客様の検索
|
||||
*
|
||||
* @param array $query 検索クエリー
|
||||
* @return Result|array
|
||||
* @return Result
|
||||
*/
|
||||
public function searchCustomers(array $query = []): \Result|array {
|
||||
public function listCustomers(array $query = []): \Result {
|
||||
if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
||||
foreach ($query as $k => $v) { if (is_bool($v)) $query[$k] = $v ? '1' : '0'; }
|
||||
$cache = $this->getCache('listcustomer');
|
||||
if (!empty($cache) && (isset($cache['query']) && $cache['query'] === $query)) return \Result::Success('', $cache);
|
||||
|
||||
foreach ($query as $k => $v) { if (is_bool($v)) $query[$k] = $v ? '1' : '0'; }
|
||||
$uri = '/customers?'.http_build_query($query, '', '&', PHP_QUERY_RFC3986);
|
||||
$curl = $this->setupCurl($uri);
|
||||
$data = $this->curlResult($curl);
|
||||
if (isset($data['data'])) return $data['data'];
|
||||
$res = $this->curlResult($curl);
|
||||
if (isset($res->data['data'])) {
|
||||
$res->data['query'] = $query;
|
||||
$this->setCache('listcustomer', $res->data);
|
||||
return \Result::Success('', $res->data);
|
||||
}
|
||||
|
||||
return \Result::Error("TLD一覧の受け取りに失敗。");
|
||||
return \Result::Error('TLD一覧の受け取りに失敗。');
|
||||
}
|
||||
|
||||
//// ドメイン値段
|
||||
@@ -212,16 +228,28 @@ class Openprovider {
|
||||
* ドメイン値段一覧
|
||||
*
|
||||
* @param array $query 検索クエリー
|
||||
* @return Result|array
|
||||
* @return Result
|
||||
*/
|
||||
public function getDomainPrices(array $query): \Result|array {
|
||||
public function getDomainPrices(array $query): \Result {
|
||||
if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
||||
$name = '';
|
||||
$ext = '';
|
||||
if (isset($query['domain.name'])) $name = $query['domain.name'];
|
||||
if (isset($query['domain.extension'])) $ext = $query['domain.extension'];
|
||||
$cacheName = $name && $ext ? "domainprices-{$name}.{$ext}" : 'domainprices';
|
||||
|
||||
$cache = $this->getCache($cacheName);
|
||||
if (!empty($cache)) return \Result::Success('', $cache);
|
||||
|
||||
$uri = "/domains/prices?".http_build_query($query, '', '&', PHP_QUERY_RFC3986);
|
||||
$curl = $this->setupCurl($uri);
|
||||
$data = $this->curlResult($curl);
|
||||
if (isset($data['data'])) return $data['data'];
|
||||
$res = $this->curlResult($curl);
|
||||
if (isset($res->data['data'])) {
|
||||
$this->setCache($cacheName, $res->data);
|
||||
return \Result::Success('', $res->data);
|
||||
}
|
||||
|
||||
return \Result::Error("TLD一覧の受け取りに失敗。");
|
||||
return \Result::Error('TLD一覧の受け取りに失敗。');
|
||||
}
|
||||
|
||||
//// 認証コード
|
||||
@@ -232,22 +260,25 @@ class Openprovider {
|
||||
* TLD一覧
|
||||
*
|
||||
* @param array $query 検索クエリー
|
||||
* @return Result|array
|
||||
* @return Result
|
||||
*/
|
||||
public function listTlds(array $query = []): \Result|array {
|
||||
public function listTlds(array $query = []): \Result {
|
||||
if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
||||
if (empty($query)) $query = [ 'limit' => 10, 'offset' => 0, 'order' => 'ASC' ];
|
||||
$cache = $this->getCache('listtlds');
|
||||
if (!empty($cache)) return \Result::Success('', $cache);
|
||||
|
||||
foreach ($query as $k => $v) {
|
||||
if (is_bool($v)) $query[$k] = $v ? '1' : '0';
|
||||
}
|
||||
if (empty($query)) $query = [ 'limit' => 10, 'offset' => 0, 'order' => 'ASC' ];
|
||||
foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0';
|
||||
|
||||
$uri = '/tlds?'.http_build_query($query, '', '&', PHP_QUERY_RFC3986);
|
||||
$curl = $this->setupCurl($uri);
|
||||
$data = $this->curlResult($curl);
|
||||
if (isset($data['data'])) return $data['data'];
|
||||
$res = $this->curlResult($curl);
|
||||
if (isset($res->data['data'])) {
|
||||
$this->setCache('listtlds', $res->data);
|
||||
return \Result::Success('', $res->data);
|
||||
}
|
||||
|
||||
return \Result::Error("TLD一覧の受け取りに失敗。");
|
||||
return \Result::Error('TLD一覧の受け取りに失敗。');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -255,22 +286,25 @@ class Openprovider {
|
||||
*
|
||||
* @param string $tld TLD
|
||||
* @param array $query 検索クエリー
|
||||
* @return Result|array
|
||||
* @return Result
|
||||
*/
|
||||
public function getTld(string $tld, array $query = []): \Result|array {
|
||||
public function getTld(string $tld, array $query = []): \Result {
|
||||
if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
||||
if (empty($query)) $query = [ 'limit' => 10, 'offset' => 0 ];
|
||||
$cache = $this->getCache("gettld-{$tld}");
|
||||
if (!empty($cache)) return \Result::Success('', $cache);
|
||||
|
||||
foreach ($query as $k => $v) {
|
||||
if (is_bool($v)) $query[$k] = $v ? '1' : '0';
|
||||
}
|
||||
if (empty($query)) $query = [ 'limit' => 10, 'offset' => 0 ];
|
||||
foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0';
|
||||
|
||||
$uri = "/tlds/{$tld}?".http_build_query($query, '', '&', PHP_QUERY_RFC3986);
|
||||
$curl = $this->setupCurl($uri);
|
||||
$data = $this->curlResult($curl);
|
||||
if (isset($data['data'])) return $data['data'];
|
||||
$res = $this->curlResult($curl);
|
||||
if (isset($res->data['data'])) {
|
||||
$this->setCache("gettld-{$tld}", $res->data);
|
||||
return \Result::Success('', $res->data);
|
||||
}
|
||||
|
||||
return \Result::Error("TLD一覧の受け取りに失敗。");
|
||||
return \Result::Error('TLD一覧の受け取りに失敗。');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -293,22 +327,25 @@ class Openprovider {
|
||||
* DNSゾーン一覧
|
||||
*
|
||||
* @param array $query
|
||||
* @return Result|array
|
||||
* @return Result
|
||||
*/
|
||||
public function listDnsZones(array $query = []): \Result|array {
|
||||
public function listDnsZones(array $query = []): \Result {
|
||||
if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
||||
if (empty($query)) $query = [ 'limit' => 25, 'offset' => 0, 'order_by.name' => 'asc' ];
|
||||
$cache = $this->getCache('listdnszones');
|
||||
if (!empty($cache)) return \Result::Success('', $cache);
|
||||
|
||||
foreach ($query as $k => $v) {
|
||||
if (is_bool($v)) $query[$k] = $v ? '1' : '0';
|
||||
}
|
||||
if (empty($query)) $query = [ 'limit' => 25, 'offset' => 0, 'order_by.name' => 'asc' ];
|
||||
foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0';
|
||||
|
||||
$uri = "/dns/zones?".http_build_query($query, '', '&', PHP_QUERY_RFC3986);
|
||||
$curl = $this->setupCurl($uri);
|
||||
$data = $this->curlResult($curl);
|
||||
if (isset($data['data'])) return $data['data'];
|
||||
$res = $this->curlResult($curl);
|
||||
if (isset($res->data['data'])) {
|
||||
$this->setCache('listdnszones', $res->data);
|
||||
return \Result::Success('', $res->data);
|
||||
}
|
||||
|
||||
return \Result::Error("TLD一覧の受け取りに失敗。");
|
||||
return \Result::Error('TLD一覧の受け取りに失敗。');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -316,19 +353,25 @@ class Openprovider {
|
||||
*
|
||||
* @param string $domain ドメイン名
|
||||
* @param array $query 検索クエリー
|
||||
* @return Result|array
|
||||
* @return Result
|
||||
*/
|
||||
public function getDnsZone(string $domain, array $query = []): \Result|array {
|
||||
public function getDnsZone(string $domain, array $query = []): \Result {
|
||||
if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
||||
$cache = $this->getCache("getdnszone-{$domain}");
|
||||
if (!empty($cache)) return \Result::Success('', $cache);
|
||||
|
||||
if (empty($query)) $query = [ 'limit' => 100, 'offset' => 0, 'order_by.name' => 'asc' ];
|
||||
foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0';
|
||||
|
||||
$uri = "/dns/zones/{$domain}?".http_build_query($query, '', '&', PHP_QUERY_RFC3986);
|
||||
$curl = $this->setupCurl($uri);
|
||||
$data = $this->curlResult($curl);
|
||||
if (isset($data['data'])) return $data['data'];
|
||||
$res = $this->curlResult($curl);
|
||||
if (isset($res->data['data'])) {
|
||||
$this->setCache("getdnszone-{$domain}", $res->data);
|
||||
return \Result::Success('', $res->data);
|
||||
}
|
||||
|
||||
return \Result::Error("TLD一覧の受け取りに失敗。");
|
||||
return \Result::Error('TLD一覧の受け取りに失敗。');
|
||||
}
|
||||
|
||||
//// ゾーンレコード
|
||||
@@ -337,15 +380,21 @@ class Openprovider {
|
||||
* DNSレコード一覧
|
||||
*
|
||||
* @param string $domain
|
||||
* @return Result|array
|
||||
* @return Result
|
||||
*/
|
||||
public function listZoneRecords(string $domain): \Result|array {
|
||||
public function listZoneRecords(string $domain): \Result {
|
||||
if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
||||
$curl = $this->setupCurl("/dns/zones/{$domain}/records");
|
||||
$data = $this->curlResult($curl);
|
||||
if (isset($data['data'])) return $data['data'];
|
||||
$cache = $this->getCache("getzonerecords-{$domain}");
|
||||
if (!empty($cache)) return \Result::Success('', $cache);
|
||||
|
||||
return \Result::Error("TLD一覧の受け取りに失敗。");
|
||||
$curl = $this->setupCurl("/dns/zones/{$domain}/records");
|
||||
$res = $this->curlResult($curl);
|
||||
if (isset($res->data['data'])) {
|
||||
$this->setCache("getzonerecords-{$domain}", $res->data);
|
||||
return \Result::Success('', $res->data);
|
||||
}
|
||||
|
||||
return \Result::Error('TLD一覧の受け取りに失敗。');
|
||||
}
|
||||
|
||||
// Easydmarc
|
||||
@@ -377,7 +426,7 @@ class Openprovider {
|
||||
*/
|
||||
public function getResellerId(): int {
|
||||
if (!OPENPROVIDER_ENABLED) return 0;
|
||||
return $this->reseller_id;
|
||||
return $this->resellerId;
|
||||
}
|
||||
|
||||
//////////
|
||||
@@ -394,7 +443,7 @@ class Openprovider {
|
||||
return $curl;
|
||||
}
|
||||
|
||||
private function curlResult(Curl &$curl): \Result|array {
|
||||
private function curlResult(Curl &$curl): \Result {
|
||||
$res = $curl->execute();
|
||||
if (!$res->isSuccess) {
|
||||
$err = "CURL実行に失敗: {$curl->message}";
|
||||
@@ -411,32 +460,47 @@ class Openprovider {
|
||||
}
|
||||
assert_not_null($body, "返事ボディーは空です。");
|
||||
|
||||
return json_decode($body, true);
|
||||
return \Result::Success('', json_decode($body, true));
|
||||
}
|
||||
|
||||
private function mkCache(): \Result {
|
||||
$session = new \stdClass;
|
||||
$session->lastAuth = $this->last_auth;
|
||||
$session->token = $this->token;
|
||||
$session->resellerId = $this->reseller_id;
|
||||
|
||||
$json = json_encode($session, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
if (file_put_contents($this->dataFile, $json) === false) return \Result::Error('エラー:ユーザーデータの保存に失敗。');
|
||||
/**
|
||||
* キャッシュの作成
|
||||
*
|
||||
* @param string $type キャッシュ類(例:login、domainlist等)
|
||||
* @param array $data 保存するデータ
|
||||
* @return Result
|
||||
*/
|
||||
private function setCache(string $type, array $data): \Result {
|
||||
if (!file_exists($this->dataDir)) {
|
||||
if (!mkdir($this->dataDir, 0755))
|
||||
return \Result::Error('エラー:ユーザーのアイコンディレクトリの作成に失敗。');
|
||||
}
|
||||
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
if (file_put_contents("{$this->dataDir}{$type}.json", $json) === false) return \Result::Error('エラー:ユーザーデータの保存に失敗。');
|
||||
|
||||
return \Result::Success();
|
||||
}
|
||||
|
||||
private function getCache(): void {
|
||||
if (!file_exists($this->dataFile)) return;
|
||||
/**
|
||||
* キャッシュから受け取り
|
||||
*
|
||||
* @param string $type キャッシュ類(例:login、domainlist等)
|
||||
* @return array
|
||||
*/
|
||||
private function getCache(string $type): array {
|
||||
if (!file_exists("{$this->dataDir}{$type}.json")) return [];
|
||||
|
||||
$content = file_get_contents($this->dataFile);
|
||||
if ($content === false) return;
|
||||
$content = file_get_contents("{$this->dataDir}{$type}.json");
|
||||
if ($content === false) return [];
|
||||
|
||||
$data = json_decode($content, true);
|
||||
if (json_last_error() !== JSON_ERROR_NONE || !is_array($data)) return;
|
||||
if (json_last_error() !== JSON_ERROR_NONE || !is_array($data)) return [];
|
||||
|
||||
$this->token = $data['token'] ?? null;
|
||||
$this->reseller_id = $data['resellerId'] ?? null;
|
||||
$this->last_auth = $data['lastAuth'] ?? null;
|
||||
if ($type === 'login') {
|
||||
$this->token = $data['data']['token'];
|
||||
$this->lastAuth = $data['last_auth'];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user