| | @@ -4,15 +4,15 @@ |
| 4 | 4 | use Std\Lib\Curl;
|
| 5 | 5 |
|
| 6 | 6 | class Openprovider {
|
| 7 | | - protected string $dataFile = ROOT."/data/session.json";
|
| 7 | + protected string $dataDir = ROOT."/data/cache/";
|
| 8 | 8 | protected int $tokenDuration = 172800; // 48時間
|
| 9 | 9 |
|
| 10 | 10 | private string $user = '';
|
| 11 | 11 | private string $pass = '';
|
| 12 | 12 | private string $token = '';
|
| 13 | 13 | private string $ip = '';
|
| 14 | | - private int $reseller_id = 0;
|
| 15 | | - private int $last_auth = 0;
|
| 14 | + private int $resellerId = 0;
|
| 15 | + private int $lastAuth = 0;
|
| 16 | 16 | private string $BASEURL = DEBUG_MODE ? 'http://api.sandbox.openprovider.nl:8480/v1beta'
|
| 17 | 17 | : 'https://api.openprovider.eu/v1beta';
|
| 18 | 18 |
|
| | @@ -27,14 +27,18 @@ |
| 27 | 27 | * トークンの受け取り。
|
| 28 | 28 | * このライブリリーを使ったら、一回「login()」を実行する事が必須となります。
|
| 29 | 29 | *
|
| 30 | | - * @return Result|false 結果
|
| 30 | + * @return Result 結果
|
| 31 | 31 | */
|
| 32 | | - public function login(): \Result|false {
|
| 32 | + public function login(): \Result {
|
| 33 | 33 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
| 34 | | - $this->getCache();
|
| 35 | | - if (time() < ($this->last_auth + $this->tokenDuration)) return false;
|
| 36 | | - $curl = new Curl("{$this->BASEURL}/auth/login");
|
| 34 | + $cache = $this->getCache('login');
|
| 35 | + if (time() < ($this->lastAuth + $this->tokenDuration)) {
|
| 36 | + $this->token = $cache['data']['token'];
|
| 37 | + $this->lastAuth = $cache['last_auth'];
|
| 38 | + return \Result::Success('', $cache);
|
| 39 | + }
|
| 37 | 40 |
|
| 41 | + $curl = new Curl("{$this->BASEURL}/auth/login");
|
| 38 | 42 | $payload = [
|
| 39 | 43 | 'username' => $this->user,
|
| 40 | 44 | 'password' => $this->pass,
|
| | @@ -62,17 +66,17 @@ |
| 62 | 66 | }
|
| 63 | 67 | assert_not_null($body, "返事ボディーは空です。");
|
| 64 | 68 |
|
| 65 | | - $data = json_decode($body, true);
|
| 66 | | - if (isset($data['data']['token'])) {
|
| 67 | | - $this->token = $data['data']['token'];
|
| 68 | | - $this->last_auth = time();
|
| 69 | | - $this->reseller_id = $data['data']['reseller_id'];
|
| 70 | | - $this->mkCache();
|
| 69 | + $res = json_decode($body, true);
|
| 70 | + if (isset($res['data']['token'])) {
|
| 71 | + $this->token = $res['data']['token'];
|
| 72 | + $this->lastAuth = time();
|
| 73 | + $res['last_auth'] = $this->lastAuth;
|
| 74 | + $this->setCache('login', $res);
|
| 71 | 75 |
|
| 72 | | - return \Result::Success();
|
| 76 | + return \Result::Success('', $res);
|
| 73 | 77 | }
|
| 74 | 78 |
|
| 75 | | - return \Result::Error("ログインに失敗。");
|
| 79 | + return \Result::Error('ログインに失敗。');
|
| 76 | 80 | }
|
| 77 | 81 |
|
| 78 | 82 | // カスタマー
|
| | @@ -84,15 +88,21 @@ |
| 84 | 88 | /***
|
| 85 | 89 | * ドメイン一覧。
|
| 86 | 90 | *
|
| 87 | | - * @return Result|array 結果。
|
| 91 | + * @return Result 結果。
|
| 88 | 92 | */
|
| 89 | | - public function listDomains(array $query = []): \Result|array {
|
| 93 | + public function listDomains(array $query = []): \Result {
|
| 90 | 94 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
| 95 | + $cache = $this->getCache('listdomains');
|
| 96 | + if (!empty($cache)) return \Result::Success('', $cache);
|
| 97 | +
|
| 91 | 98 | $curl = $this->setupCurl('/domains');
|
| 92 | | - $data = $this->curlResult($curl);
|
| 93 | | - if (isset($data['data']['results'])) return $data['data']['results'];
|
| 99 | + $res = $this->curlResult($curl);
|
| 100 | + if (isset($res['data']['results'])) {
|
| 101 | + $this->setCache('listdomains', $res->data);
|
| 102 | + return \Result::Success('', $res['data']['results']);
|
| 103 | + }
|
| 94 | 104 |
|
| 95 | | - return \Result::Error("ドメインの確認に失敗。");
|
| 105 | + return \Result::Error('ドメインの確認に失敗。');
|
| 96 | 106 | }
|
| 97 | 107 |
|
| 98 | 108 | /**
|
| | @@ -101,9 +111,9 @@ |
| 101 | 111 | * @param string $name 登録したいドメイン名(例:"076studio.jp")
|
| 102 | 112 | * @param array $info カスタマー情報等
|
| 103 | 113 | * @param int $period 年間(デフォルト=1年)
|
| 104 | | - * @return Result|array 結果。配列の場合:'activation_date'(リアルタイムで成功したのみ), 'auth_code'(TLDが対応している場合のみ), 'expiration_date', 'id', 'renewal_date', 'status'(ACT又はREQ)
|
| 114 | + * @return Result 結果。配列の場合:'activation_date'(リアルタイムで成功したのみ), 'auth_code'(TLDが対応している場合のみ), 'expiration_date', 'id', 'renewal_date', 'status'(ACT又はREQ)
|
| 105 | 115 | */
|
| 106 | | - public function createDomain(string $name, array $info, int $period = 1): \Result|array {
|
| 116 | + public function createDomain(string $name, array $info, int $period = 1): \Result {
|
| 107 | 117 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
| 108 | 118 | if (!isset($info['owner_handle']) || $info['owner_handle'] === '') $info['owner_handle'] = OPENPROVIDER['owner_handle'];
|
| 109 | 119 | if (!isset($info['admin_handle']) || $info['admin_handle'] === '') $info['admin_handle'] = OPENPROVIDER['owner_handle'];
|
| | @@ -142,10 +152,10 @@ |
| 142 | 152 |
|
| 143 | 153 | $curl = $this->setupCurl('/domains/', 'POST', $payload);
|
| 144 | 154 | kys('TODO');
|
| 145 | | - $data = $this->curlResult($curl);
|
| 146 | | - if (isset($data['data'])) return $data['data'];
|
| 155 | + $res = $this->curlResult($curl);
|
| 156 | + if (isset($res['data'])) return \Result::Success('', $res);
|
| 147 | 157 |
|
| 148 | | - return \Result::Error("ドメインの確認に失敗。");
|
| 158 | + return \Result::Error('ドメインの確認に失敗。');
|
| 149 | 159 | }
|
| 150 | 160 |
|
| 151 | 161 | /***
|
| | @@ -153,9 +163,9 @@ |
| 153 | 163 | *
|
| 154 | 164 | * @param array $domains ドメイン名リスト。例:['076.moe', '076.co.jp', '076.com']
|
| 155 | 165 | * @param bool $with_price 値段を表示するかどうか。デフォルトは「false」
|
| 156 | | - * @return Result|array 結果。配列の場合:'domain'と'status'はいつでもあり、'reason', 'premium', 'is_premium'が多分あり、そして'price'が「with_price」が「true」の場合のみ。
|
| 166 | + * @return Result 結果。配列の場合:'domain'と'status'はいつでもあり、'reason', 'premium', 'is_premium'が多分あり、そして'price'が「with_price」が「true」の場合のみ。
|
| 157 | 167 | */
|
| 158 | | - public function checkDomainAvailable(array $domains, bool $with_price = false): \Result|array {
|
| 168 | + public function checkDomainAvailable(array $domains, bool $with_price = false): \Result {
|
| 159 | 169 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
| 160 | 170 | $domainList = [];
|
| 161 | 171 | foreach ($domains as $d) {
|
| | @@ -178,10 +188,10 @@ |
| 178 | 188 | ];
|
| 179 | 189 |
|
| 180 | 190 | $curl = $this->setupCurl('/domains/check', 'POST', $payload);
|
| 181 | | - $data = $this->curlResult($curl);
|
| 182 | | - if (isset($data['data']['results'])) return $data['data']['results'];
|
| 191 | + $res = $this->curlResult($curl);
|
| 192 | + if (isset($res['data']['results'])) return \Result::Success('', $res['data']['results']);
|
| 183 | 193 |
|
| 184 | | - return \Result::Error("ドメインの確認に失敗。");
|
| 194 | + return \Result::Error('ドメインの確認に失敗。');
|
| 185 | 195 | }
|
| 186 | 196 |
|
| 187 | 197 | //// 追加データ
|
| | @@ -192,18 +202,24 @@ |
| 192 | 202 | * 顧客様の検索
|
| 193 | 203 | *
|
| 194 | 204 | * @param array $query 検索クエリー
|
| 195 | | - * @return Result|array
|
| 205 | + * @return Result
|
| 196 | 206 | */
|
| 197 | | - public function searchCustomers(array $query = []): \Result|array {
|
| 207 | + public function listCustomers(array $query = []): \Result {
|
| 198 | 208 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
| 199 | | - foreach ($query as $k => $v) { if (is_bool($v)) $query[$k] = $v ? '1' : '0'; }
|
| 209 | + $cache = $this->getCache('listcustomer');
|
| 210 | + if (!empty($cache) && (isset($cache['query']) && $cache['query'] === $query)) return \Result::Success('', $cache);
|
| 200 | 211 |
|
| 212 | + foreach ($query as $k => $v) { if (is_bool($v)) $query[$k] = $v ? '1' : '0'; }
|
| 201 | 213 | $uri = '/customers?'.http_build_query($query, '', '&', PHP_QUERY_RFC3986);
|
| 202 | 214 | $curl = $this->setupCurl($uri);
|
| 203 | | - $data = $this->curlResult($curl);
|
| 204 | | - if (isset($data['data'])) return $data['data'];
|
| 215 | + $res = $this->curlResult($curl);
|
| 216 | + if (isset($res->data['data'])) {
|
| 217 | + $res->data['query'] = $query;
|
| 218 | + $this->setCache('listcustomer', $res->data);
|
| 219 | + return \Result::Success('', $res->data);
|
| 220 | + }
|
| 205 | 221 |
|
| 206 | | - return \Result::Error("TLD一覧の受け取りに失敗。");
|
| 222 | + return \Result::Error('TLD一覧の受け取りに失敗。');
|
| 207 | 223 | }
|
| 208 | 224 |
|
| 209 | 225 | //// ドメイン値段
|
| | @@ -212,16 +228,28 @@ |
| 212 | 228 | * ドメイン値段一覧
|
| 213 | 229 | *
|
| 214 | 230 | * @param array $query 検索クエリー
|
| 215 | | - * @return Result|array
|
| 231 | + * @return Result
|
| 216 | 232 | */
|
| 217 | | - public function getDomainPrices(array $query): \Result|array {
|
| 233 | + public function getDomainPrices(array $query): \Result {
|
| 218 | 234 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
| 235 | + $name = '';
|
| 236 | + $ext = '';
|
| 237 | + if (isset($query['domain.name'])) $name = $query['domain.name'];
|
| 238 | + if (isset($query['domain.extension'])) $ext = $query['domain.extension'];
|
| 239 | + $cacheName = $name && $ext ? "domainprices-{$name}.{$ext}" : 'domainprices';
|
| 240 | +
|
| 241 | + $cache = $this->getCache($cacheName);
|
| 242 | + if (!empty($cache)) return \Result::Success('', $cache);
|
| 243 | +
|
| 219 | 244 | $uri = "/domains/prices?".http_build_query($query, '', '&', PHP_QUERY_RFC3986);
|
| 220 | 245 | $curl = $this->setupCurl($uri);
|
| 221 | | - $data = $this->curlResult($curl);
|
| 222 | | - if (isset($data['data'])) return $data['data'];
|
| 246 | + $res = $this->curlResult($curl);
|
| 247 | + if (isset($res->data['data'])) {
|
| 248 | + $this->setCache($cacheName, $res->data);
|
| 249 | + return \Result::Success('', $res->data);
|
| 250 | + }
|
| 223 | 251 |
|
| 224 | | - return \Result::Error("TLD一覧の受け取りに失敗。");
|
| 252 | + return \Result::Error('TLD一覧の受け取りに失敗。');
|
| 225 | 253 | }
|
| 226 | 254 |
|
| 227 | 255 | //// 認証コード
|
| | @@ -232,22 +260,25 @@ |
| 232 | 260 | * TLD一覧
|
| 233 | 261 | *
|
| 234 | 262 | * @param array $query 検索クエリー
|
| 235 | | - * @return Result|array
|
| 263 | + * @return Result
|
| 236 | 264 | */
|
| 237 | | - public function listTlds(array $query = []): \Result|array {
|
| 265 | + public function listTlds(array $query = []): \Result {
|
| 238 | 266 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
| 267 | + $cache = $this->getCache('listtlds');
|
| 268 | + if (!empty($cache)) return \Result::Success('', $cache);
|
| 269 | +
|
| 239 | 270 | if (empty($query)) $query = [ 'limit' => 10, 'offset' => 0, 'order' => 'ASC' ];
|
| 271 | + foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0';
|
| 240 | 272 |
|
| 241 | | - foreach ($query as $k => $v) {
|
| 242 | | - if (is_bool($v)) $query[$k] = $v ? '1' : '0';
|
| 243 | | - }
|
| 244 | | -
|
| 245 | 273 | $uri = '/tlds?'.http_build_query($query, '', '&', PHP_QUERY_RFC3986);
|
| 246 | 274 | $curl = $this->setupCurl($uri);
|
| 247 | | - $data = $this->curlResult($curl);
|
| 248 | | - if (isset($data['data'])) return $data['data'];
|
| 275 | + $res = $this->curlResult($curl);
|
| 276 | + if (isset($res->data['data'])) {
|
| 277 | + $this->setCache('listtlds', $res->data);
|
| 278 | + return \Result::Success('', $res->data);
|
| 279 | + }
|
| 249 | 280 |
|
| 250 | | - return \Result::Error("TLD一覧の受け取りに失敗。");
|
| 281 | + return \Result::Error('TLD一覧の受け取りに失敗。');
|
| 251 | 282 | }
|
| 252 | 283 |
|
| 253 | 284 | /**
|
| | @@ -255,22 +286,25 @@ |
| 255 | 286 | *
|
| 256 | 287 | * @param string $tld TLD
|
| 257 | 288 | * @param array $query 検索クエリー
|
| 258 | | - * @return Result|array
|
| 289 | + * @return Result
|
| 259 | 290 | */
|
| 260 | | - public function getTld(string $tld, array $query = []): \Result|array {
|
| 291 | + public function getTld(string $tld, array $query = []): \Result {
|
| 261 | 292 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
| 293 | + $cache = $this->getCache("gettld-{$tld}");
|
| 294 | + if (!empty($cache)) return \Result::Success('', $cache);
|
| 295 | +
|
| 262 | 296 | if (empty($query)) $query = [ 'limit' => 10, 'offset' => 0 ];
|
| 297 | + foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0';
|
| 263 | 298 |
|
| 264 | | - foreach ($query as $k => $v) {
|
| 265 | | - if (is_bool($v)) $query[$k] = $v ? '1' : '0';
|
| 266 | | - }
|
| 267 | | -
|
| 268 | 299 | $uri = "/tlds/{$tld}?".http_build_query($query, '', '&', PHP_QUERY_RFC3986);
|
| 269 | 300 | $curl = $this->setupCurl($uri);
|
| 270 | | - $data = $this->curlResult($curl);
|
| 271 | | - if (isset($data['data'])) return $data['data'];
|
| 301 | + $res = $this->curlResult($curl);
|
| 302 | + if (isset($res->data['data'])) {
|
| 303 | + $this->setCache("gettld-{$tld}", $res->data);
|
| 304 | + return \Result::Success('', $res->data);
|
| 305 | + }
|
| 272 | 306 |
|
| 273 | | - return \Result::Error("TLD一覧の受け取りに失敗。");
|
| 307 | + return \Result::Error('TLD一覧の受け取りに失敗。');
|
| 274 | 308 | }
|
| 275 | 309 |
|
| 276 | 310 | /**
|
| | @@ -293,22 +327,25 @@ |
| 293 | 327 | * DNSゾーン一覧
|
| 294 | 328 | *
|
| 295 | 329 | * @param array $query
|
| 296 | | - * @return Result|array
|
| 330 | + * @return Result
|
| 297 | 331 | */
|
| 298 | | - public function listDnsZones(array $query = []): \Result|array {
|
| 332 | + public function listDnsZones(array $query = []): \Result {
|
| 299 | 333 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
| 334 | + $cache = $this->getCache('listdnszones');
|
| 335 | + if (!empty($cache)) return \Result::Success('', $cache);
|
| 336 | +
|
| 300 | 337 | if (empty($query)) $query = [ 'limit' => 25, 'offset' => 0, 'order_by.name' => 'asc' ];
|
| 338 | + foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0';
|
| 301 | 339 |
|
| 302 | | - foreach ($query as $k => $v) {
|
| 303 | | - if (is_bool($v)) $query[$k] = $v ? '1' : '0';
|
| 304 | | - }
|
| 305 | | -
|
| 306 | 340 | $uri = "/dns/zones?".http_build_query($query, '', '&', PHP_QUERY_RFC3986);
|
| 307 | 341 | $curl = $this->setupCurl($uri);
|
| 308 | | - $data = $this->curlResult($curl);
|
| 309 | | - if (isset($data['data'])) return $data['data'];
|
| 342 | + $res = $this->curlResult($curl);
|
| 343 | + if (isset($res->data['data'])) {
|
| 344 | + $this->setCache('listdnszones', $res->data);
|
| 345 | + return \Result::Success('', $res->data);
|
| 346 | + }
|
| 310 | 347 |
|
| 311 | | - return \Result::Error("TLD一覧の受け取りに失敗。");
|
| 348 | + return \Result::Error('TLD一覧の受け取りに失敗。');
|
| 312 | 349 | }
|
| 313 | 350 |
|
| 314 | 351 | /**
|
| | @@ -316,19 +353,25 @@ |
| 316 | 353 | *
|
| 317 | 354 | * @param string $domain ドメイン名
|
| 318 | 355 | * @param array $query 検索クエリー
|
| 319 | | - * @return Result|array
|
| 356 | + * @return Result
|
| 320 | 357 | */
|
| 321 | | - public function getDnsZone(string $domain, array $query = []): \Result|array {
|
| 358 | + public function getDnsZone(string $domain, array $query = []): \Result {
|
| 322 | 359 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
| 360 | + $cache = $this->getCache("getdnszone-{$domain}");
|
| 361 | + if (!empty($cache)) return \Result::Success('', $cache);
|
| 362 | +
|
| 323 | 363 | if (empty($query)) $query = [ 'limit' => 100, 'offset' => 0, 'order_by.name' => 'asc' ];
|
| 324 | 364 | foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0';
|
| 325 | 365 |
|
| 326 | 366 | $uri = "/dns/zones/{$domain}?".http_build_query($query, '', '&', PHP_QUERY_RFC3986);
|
| 327 | 367 | $curl = $this->setupCurl($uri);
|
| 328 | | - $data = $this->curlResult($curl);
|
| 329 | | - if (isset($data['data'])) return $data['data'];
|
| 368 | + $res = $this->curlResult($curl);
|
| 369 | + if (isset($res->data['data'])) {
|
| 370 | + $this->setCache("getdnszone-{$domain}", $res->data);
|
| 371 | + return \Result::Success('', $res->data);
|
| 372 | + }
|
| 330 | 373 |
|
| 331 | | - return \Result::Error("TLD一覧の受け取りに失敗。");
|
| 374 | + return \Result::Error('TLD一覧の受け取りに失敗。');
|
| 332 | 375 | }
|
| 333 | 376 |
|
| 334 | 377 | //// ゾーンレコード
|
| | @@ -337,15 +380,21 @@ |
| 337 | 380 | * DNSレコード一覧
|
| 338 | 381 | *
|
| 339 | 382 | * @param string $domain
|
| 340 | | - * @return Result|array
|
| 383 | + * @return Result
|
| 341 | 384 | */
|
| 342 | | - public function listZoneRecords(string $domain): \Result|array {
|
| 385 | + public function listZoneRecords(string $domain): \Result {
|
| 343 | 386 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
|
| 387 | + $cache = $this->getCache("getzonerecords-{$domain}");
|
| 388 | + if (!empty($cache)) return \Result::Success('', $cache);
|
| 389 | +
|
| 344 | 390 | $curl = $this->setupCurl("/dns/zones/{$domain}/records");
|
| 345 | | - $data = $this->curlResult($curl);
|
| 346 | | - if (isset($data['data'])) return $data['data'];
|
| 391 | + $res = $this->curlResult($curl);
|
| 392 | + if (isset($res->data['data'])) {
|
| 393 | + $this->setCache("getzonerecords-{$domain}", $res->data);
|
| 394 | + return \Result::Success('', $res->data);
|
| 395 | + }
|
| 347 | 396 |
|
| 348 | | - return \Result::Error("TLD一覧の受け取りに失敗。");
|
| 397 | + return \Result::Error('TLD一覧の受け取りに失敗。');
|
| 349 | 398 | }
|
| 350 | 399 |
|
| 351 | 400 | // Easydmarc
|
| | @@ -377,7 +426,7 @@ |
| 377 | 426 | */
|
| 378 | 427 | public function getResellerId(): int {
|
| 379 | 428 | if (!OPENPROVIDER_ENABLED) return 0;
|
| 380 | | - return $this->reseller_id;
|
| 429 | + return $this->resellerId;
|
| 381 | 430 | }
|
| 382 | 431 |
|
| 383 | 432 | //////////
|
| | @@ -394,7 +443,7 @@ |
| 394 | 443 | return $curl;
|
| 395 | 444 | }
|
| 396 | 445 |
|
| 397 | | - private function curlResult(Curl &$curl): \Result|array {
|
| 446 | + private function curlResult(Curl &$curl): \Result {
|
| 398 | 447 | $res = $curl->execute();
|
| 399 | 448 | if (!$res->isSuccess) {
|
| 400 | 449 | $err = "CURL実行に失敗: {$curl->message}";
|
| | @@ -411,32 +460,47 @@ |
| 411 | 460 | }
|
| 412 | 461 | assert_not_null($body, "返事ボディーは空です。");
|
| 413 | 462 |
|
| 414 | | - return json_decode($body, true);
|
| 463 | + return \Result::Success('', json_decode($body, true));
|
| 415 | 464 | }
|
| 416 | 465 |
|
| 417 | | - private function mkCache(): \Result {
|
| 418 | | - $session = new \stdClass;
|
| 419 | | - $session->lastAuth = $this->last_auth;
|
| 420 | | - $session->token = $this->token;
|
| 421 | | - $session->resellerId = $this->reseller_id;
|
| 466 | + /**
|
| 467 | + * キャッシュの作成
|
| 468 | + *
|
| 469 | + * @param string $type キャッシュ類(例:login、domainlist等)
|
| 470 | + * @param array $data 保存するデータ
|
| 471 | + * @return Result
|
| 472 | + */
|
| 473 | + private function setCache(string $type, array $data): \Result {
|
| 474 | + if (!file_exists($this->dataDir)) {
|
| 475 | + if (!mkdir($this->dataDir, 0755))
|
| 476 | + return \Result::Error('エラー:ユーザーのアイコンディレクトリの作成に失敗。');
|
| 477 | + }
|
| 478 | + $json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
| 479 | + if (file_put_contents("{$this->dataDir}{$type}.json", $json) === false) return \Result::Error('エラー:ユーザーデータの保存に失敗。');
|
| 422 | 480 |
|
| 423 | | - $json = json_encode($session, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
| 424 | | - if (file_put_contents($this->dataFile, $json) === false) return \Result::Error('エラー:ユーザーデータの保存に失敗。');
|
| 425 | | -
|
| 426 | 481 | return \Result::Success();
|
| 427 | 482 | }
|
| 428 | 483 |
|
| 429 | | - private function getCache(): void {
|
| 430 | | - if (!file_exists($this->dataFile)) return;
|
| 484 | + /**
|
| 485 | + * キャッシュから受け取り
|
| 486 | + *
|
| 487 | + * @param string $type キャッシュ類(例:login、domainlist等)
|
| 488 | + * @return array
|
| 489 | + */
|
| 490 | + private function getCache(string $type): array {
|
| 491 | + if (!file_exists("{$this->dataDir}{$type}.json")) return [];
|
| 431 | 492 |
|
| 432 | | - $content = file_get_contents($this->dataFile);
|
| 433 | | - if ($content === false) return;
|
| 493 | + $content = file_get_contents("{$this->dataDir}{$type}.json");
|
| 494 | + if ($content === false) return [];
|
| 434 | 495 |
|
| 435 | 496 | $data = json_decode($content, true);
|
| 436 | | - if (json_last_error() !== JSON_ERROR_NONE || !is_array($data)) return;
|
| 497 | + if (json_last_error() !== JSON_ERROR_NONE || !is_array($data)) return [];
|
| 437 | 498 |
|
| 438 | | - $this->token = $data['token'] ?? null;
|
| 439 | | - $this->reseller_id = $data['resellerId'] ?? null;
|
| 440 | | - $this->last_auth = $data['lastAuth'] ?? null;
|
| 499 | + if ($type === 'login') {
|
| 500 | + $this->token = $data['data']['token'];
|
| 501 | + $this->lastAuth = $data['last_auth'];
|
| 502 | + }
|
| 503 | +
|
| 504 | + return $data;
|
| 441 | 505 | }
|
| 442 | 506 | } |