cfmgrv4 init...3
This commit is contained in:
parent
38d6544ff4
commit
e6f0a3c11c
@ -20,6 +20,9 @@ class AccountHelper extends MVCHelper
|
||||
$value = $value ?: DEFAULTS['EMPTY'];
|
||||
switch ($field) {
|
||||
case AccountModel::PARENT:
|
||||
//기존 작성하던값old($field)가 있으면 그값을 넣고 없으면 부모값이 있으면 넣고 없으면 entiy가 있으면 그값을 넣고 없으면 디폴트값을 넣는다.
|
||||
$value = $value ?: (isset($viewDatas[$field]) ? $viewDatas[$field] : (isset($viewDatas['entity']) ? $viewDatas['entity']->getParent() : DEFAULTS['EMPTY']));
|
||||
|
||||
$extra_class = isset($extras['class']) ? 'select-field ' . $extras['class'] : 'select-field';
|
||||
$form = form_dropdown($field, [
|
||||
"" => lang($viewDatas['class_path'] . '.label.' . $field) . ' 선택',
|
||||
|
||||
@ -33,6 +33,9 @@ class RecordHelper extends MVCHelper
|
||||
$value = $value ?: DEFAULTS['EMPTY'];
|
||||
switch ($field) {
|
||||
case RecordModel::PARENT:
|
||||
//기존 작성하던값old($field)가 있으면 그값을 넣고 없으면 부모값이 있으면 넣고 없으면 entiy가 있으면 그값을 넣고 없으면 디폴트값을 넣는다.
|
||||
$value = $value ?: (isset($viewDatas[$field]) ? $viewDatas[$field] : (isset($viewDatas['entity']) ? $viewDatas['entity']->getParent() : DEFAULTS['EMPTY']));
|
||||
|
||||
$extra_class = isset($extras['class']) ? 'select-field ' . $extras['class'] : 'select-field';
|
||||
$form = form_dropdown($field, [
|
||||
"" => lang($viewDatas['class_path'] . '.label.' . $field) . ' 선택',
|
||||
|
||||
@ -20,6 +20,9 @@ class ZoneHelper extends MVCHelper
|
||||
$value = $value ?: DEFAULTS['EMPTY'];
|
||||
switch ($field) {
|
||||
case ZoneModel::PARENT:
|
||||
//기존 작성하던값old($field)가 있으면 그값을 넣고 없으면 부모값이 있으면 넣고 없으면 entiy가 있으면 그값을 넣고 없으면 디폴트값을 넣는다.
|
||||
$value = $value ?: (isset($viewDatas[$field]) ? $viewDatas[$field] : (isset($viewDatas['entity']) ? $viewDatas['entity']->getParent() : DEFAULTS['EMPTY']));
|
||||
|
||||
$extra_class = isset($extras['class']) ? 'select-field ' . $extras['class'] : 'select-field';
|
||||
$form = form_dropdown($field, [
|
||||
"" => lang($viewDatas['class_path'] . '.label.' . $field) . ' 선택',
|
||||
|
||||
@ -119,7 +119,7 @@ abstract class MVCHelper extends CommonHelper
|
||||
'입력',
|
||||
$action,
|
||||
[
|
||||
"data-src" => current_url() . '/' . $action,
|
||||
"data-src" => current_url() . '/' . $action . '?' . $viewDatas['uri']->getQuery(),
|
||||
"data-bs-toggle" => "modal",
|
||||
"data-bs-target" => "#index_action_form",
|
||||
...$extras
|
||||
|
||||
@ -5,15 +5,12 @@ namespace App\Libraries\MySocket;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use GuzzleHttp\Cookie\CookieJar;
|
||||
use GuzzleHttp\Client;
|
||||
use Cloudflare\API\Adapter\ResponseException;
|
||||
|
||||
abstract class MySocket
|
||||
{
|
||||
private $_cookieJar = null;
|
||||
protected function __construct()
|
||||
{
|
||||
}
|
||||
protected function __construct() {}
|
||||
abstract public function getClient(): mixed;
|
||||
final protected function getCookieJar(): CookieJar
|
||||
{
|
||||
@ -53,15 +50,23 @@ abstract class MySocket
|
||||
}
|
||||
try {
|
||||
$options = $this->getRequestOptions($method, $options, $headers);
|
||||
// log_message("debug", __FUNCTION__ .
|
||||
// "=> 호출 Socket URL:{$uri}\n--------------\n" .
|
||||
// var_export($options, true) .
|
||||
// "\n--------------\n");
|
||||
$response = $this->getClient()->$method($uri, $options);
|
||||
$body = json_decode(json: $response->getBody());
|
||||
if (!$body->success) {
|
||||
$message = sprintf(
|
||||
"%s에서 {$uri} 실패:\nrequest:%s\nresponse:%s",
|
||||
$method,
|
||||
$uri,
|
||||
var_export($options, true),
|
||||
var_export($response, true)
|
||||
);
|
||||
log_message("error", $message);
|
||||
throw new ResponseException($message);
|
||||
}
|
||||
return $response;
|
||||
} catch (RequestException $err) {
|
||||
throw ResponseException::fromRequestException($err);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
final public function get($uri, array $options = [], array $headers = []): ResponseInterface
|
||||
{
|
||||
|
||||
@ -5,15 +5,24 @@ namespace App\Services\Cloudflare;
|
||||
use App\Models\Cloudflare\AccountModel;
|
||||
use App\Entities\Cloudflare\AuthEntity;
|
||||
use App\Entities\Cloudflare\AccountEntity;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class Account extends Cloudflare
|
||||
{
|
||||
private $_model = null;
|
||||
public function __construct(AuthEntity $auth_entity)
|
||||
private ?AuthEntity $_parent_entity = null;
|
||||
private ?AccountModel $_model = null;
|
||||
public function __construct(AuthEntity $parent_entity)
|
||||
{
|
||||
parent::__construct($auth_entity);
|
||||
$this->_parent_entity = $parent_entity;
|
||||
parent::__construct($parent_entity);
|
||||
}
|
||||
protected function getParentEntity(): AuthEntity
|
||||
{
|
||||
if ($this->_parent_entity === null) {
|
||||
throw new \Exception(__FUNCTION__ . "에서 부모정보가 없습니다.");
|
||||
}
|
||||
return $this->_parent_entity;
|
||||
}
|
||||
|
||||
protected function getModel(): AccountModel
|
||||
{
|
||||
if ($this->_model === null) {
|
||||
@ -36,7 +45,7 @@ class Account extends Cloudflare
|
||||
// "created_on":"2017-06-26T05:44:49.470184Z"}
|
||||
// ]
|
||||
|
||||
protected function getArrayByResult($result, array $formDatas = []): array
|
||||
protected function getArrayByResult(\stdClass $result, array $formDatas = []): array
|
||||
{
|
||||
$formDatas[AccountModel::PK] = $result->id;
|
||||
$formDatas[AccountModel::PARENT] = $this->getAuthEntity()->getPK();
|
||||
@ -53,9 +62,9 @@ class Account extends Cloudflare
|
||||
log_message("notice", "\n----------Auth {$this->getAuthEntity()->getTitle()}의 Account 처리 시작-----------");
|
||||
$entitys = [];
|
||||
try {
|
||||
$account_results = $this->reload_procedure("accounts");
|
||||
if (count($account_results) > 0) {
|
||||
foreach ($account_results as $result) {
|
||||
$results = $this->reload_procedure("accounts");
|
||||
if (count($results) > 0) {
|
||||
foreach ($results as $result) {
|
||||
$formDatas = $this->getArrayByResult($result);
|
||||
$entitys[$formDatas[AccountModel::PK]] = $this->getModel()->modify(new AccountEntity(), $formDatas);
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@ namespace App\Services\Cloudflare;
|
||||
use App\Models\Cloudflare\AuthModel;
|
||||
use App\Libraries\MySocket\CloudflareSocket;
|
||||
use App\Entities\Cloudflare\AuthEntity;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
abstract class Cloudflare
|
||||
{
|
||||
@ -15,7 +16,8 @@ abstract class Cloudflare
|
||||
{
|
||||
$this->_auth_entity = $auth_entity;
|
||||
}
|
||||
abstract protected function getArrayByResult($result, array $formDatas = []): array;
|
||||
abstract protected function getArrayByResult(\stdClass $result, array $formDatas = []): array;
|
||||
abstract protected function getParentEntity(): mixed;
|
||||
final public function getMySocket(): CloudflareSocket
|
||||
{
|
||||
if ($this->_mySocket === null) {
|
||||
@ -37,7 +39,7 @@ abstract class Cloudflare
|
||||
}
|
||||
return $this->_authModel;
|
||||
}
|
||||
private function reload_page(string $uri, int $page, int $per_page = 50): mixed
|
||||
private function reload_page(string $uri, int $page, int $per_page = 50): ResponseInterface
|
||||
{
|
||||
$query = [
|
||||
'page' => $page,
|
||||
@ -51,29 +53,24 @@ abstract class Cloudflare
|
||||
'Pragma' => 'no-cache',
|
||||
];
|
||||
// log_message("debug", var_export($query, true));
|
||||
$response = $this->getMySocket()->get($uri, $query, $headers);
|
||||
$cf = json_decode($response->getBody());
|
||||
if (!$cf->success) {
|
||||
$message = __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true);
|
||||
log_message("error", $message);
|
||||
throw new \Exception($message);
|
||||
}
|
||||
// log_message("debug", "Page {$page} response: " . var_export($cf->result_info, true));
|
||||
return $cf;
|
||||
return $this->getMySocket()->get($uri, $query, $headers);
|
||||
}
|
||||
final protected function reload_procedure(string $uri): array
|
||||
{
|
||||
$page = 1; //1부터 시작
|
||||
//한번에 가져올수 있는 갯수 (countfalre 5~50사이)
|
||||
$cf = $this->reload_page($uri, $page);
|
||||
$per_page = $cf->result_info->per_page;
|
||||
$total_page = $cf->result_info->total_pages;
|
||||
$results = $cf->result;
|
||||
$response = $this->reload_page($uri, $page);
|
||||
$body = json_decode($response->getBody());
|
||||
// log_message("debug", var_export($body, true));
|
||||
$per_page = $body->result_info->per_page;
|
||||
$total_page = $body->result_info->total_pages;
|
||||
$results = [$body->result];
|
||||
for ($i = $page + 1; $i <= $total_page; $i++) {
|
||||
// API 제한을 고려한 지연 추가
|
||||
usleep(100000); // 0.1초 대기
|
||||
$cf = $this->reload_page($uri, $i, $per_page);
|
||||
$results = array_merge($results, $cf->result);
|
||||
$response = $this->reload_page($uri, $i, $per_page);
|
||||
$body = json_decode($response->getBody());
|
||||
$results = array_merge($results, $body->result);
|
||||
log_message("notice", "현재: page[{$i}/{$total_page}] , result수[" . count($results) . "]");
|
||||
}
|
||||
return $results;
|
||||
|
||||
@ -4,18 +4,19 @@ namespace App\Services\Cloudflare;
|
||||
|
||||
use App\Entities\Cloudflare\RecordEntity;
|
||||
use App\Entities\Cloudflare\ZoneEntity;
|
||||
use App\Models\Cloudflare\RecordModel;
|
||||
use App\Models\Cloudflare\AccountModel;
|
||||
use App\Models\Cloudflare\RecordModel;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class Record extends Cloudflare
|
||||
{
|
||||
private $_model = null;
|
||||
private $_accountModel = null;
|
||||
private $_zone_entity = null;
|
||||
private ?ZoneEntity $_parent_entity = null;
|
||||
private ?RecordModel $_model = null;
|
||||
private ?AccountModel $_accountModel = null;
|
||||
public function __construct(ZoneEntity $zone_entity)
|
||||
{
|
||||
$this->_zone_entity = $zone_entity;
|
||||
$account_entity = $this->getAccountModel()->getEntityByPK($this->_zone_entity->getParent());
|
||||
$this->_parent_entity = $zone_entity;
|
||||
$account_entity = $this->getAccountModel()->getEntityByPK($this->getParentEntity()->getParent());
|
||||
if ($account_entity === null) {
|
||||
throw new \Exception("해당 계정정보를 찾을수 없습니다.");
|
||||
}
|
||||
@ -25,6 +26,13 @@ class Record extends Cloudflare
|
||||
}
|
||||
parent::__construct($auth_entity);
|
||||
}
|
||||
protected function getParentEntity(): ZoneEntity
|
||||
{
|
||||
if ($this->_parent_entity === null) {
|
||||
throw new \Exception(__FUNCTION__ . "에서 부모정보가 없습니다.");
|
||||
}
|
||||
return $this->_parent_entity;
|
||||
}
|
||||
protected function getModel(): RecordModel
|
||||
{
|
||||
if ($this->_model === null) {
|
||||
@ -32,15 +40,16 @@ class Record extends Cloudflare
|
||||
}
|
||||
return $this->_model;
|
||||
}
|
||||
final protected function getAccountModel(): AccountModel
|
||||
protected function getAccountModel(): AccountModel
|
||||
{
|
||||
if ($this->_accountModel === null) {
|
||||
$this->_accountModel = new AccountModel();
|
||||
}
|
||||
return $this->_accountModel;
|
||||
}
|
||||
public function getArrayByResult($result, array $formDatas = []): array
|
||||
protected function getArrayByResult(\stdClass $result, array $formDatas = []): array
|
||||
{
|
||||
// log_message("debug", var_export($result, true));
|
||||
$formDatas[RecordModel::PK] = $result->id;
|
||||
$formDatas[RecordModel::PARENT] = $result->zone_id;
|
||||
$formDatas[RecordModel::TITLE] = $result->name;
|
||||
@ -49,13 +58,10 @@ class Record extends Cloudflare
|
||||
$formDatas['ttl'] = (int) $result->ttl;
|
||||
$formDatas['proxiable'] = $result->proxiable ? "on" : "off";
|
||||
$formDatas['proxied'] = $result->proxied ? "on" : "off";
|
||||
$formDatas['locked'] = "on";
|
||||
if (isset($result->locked) && $result->locked) {
|
||||
$formDatas['locked'] = "off";
|
||||
}
|
||||
$formDatas['locked'] = isset($result->locked) && $result->locked ? "off" : "on";
|
||||
$formDatas['updated_at'] = date("Y-m-d H:i:s");
|
||||
$formDatas['created_at'] = $result->created_on;
|
||||
// log_message("debug", var_export($result, return: true));
|
||||
// log_message("debug", print_r($formDatas, true));
|
||||
return $formDatas;
|
||||
}
|
||||
public function create(string $host, string $type, string $content, string $proxied): RecordEntity
|
||||
@ -68,16 +74,10 @@ class Record extends Cloudflare
|
||||
'content' => $content,
|
||||
'proxied' => $proxied === 'on' ? true : false
|
||||
];
|
||||
$cf = $this->getMySocket()->post("zones/{$this->_zone_entity->getPK()}/dns_records", $datas);
|
||||
$cf = json_decode($cf->getBody());
|
||||
if (!$cf->success) {
|
||||
$message = "Record:" . __FUNCTION__ . "에서 실패:\nrequest:" . var_export($datas, true) . "\nresponse:" . var_export($cf, true);
|
||||
log_message("error", $message);
|
||||
throw new \Exception($message);
|
||||
}
|
||||
$response = $this->getMySocket()->post("zones/{$this->getParentEntity()->getPK()}/dns_records", $datas);
|
||||
$body = json_decode($response->getBody());
|
||||
//DB생성
|
||||
$formDatas = $this->getArrayByResult($cf->result);
|
||||
return $this->getModel()->create($formDatas);
|
||||
return $this->getModel()->create($this->getArrayByResult($body->result));
|
||||
}
|
||||
public function modify(RecordEntity $entity, array $formDatas): RecordEntity
|
||||
{
|
||||
@ -100,47 +100,29 @@ class Record extends Cloudflare
|
||||
}
|
||||
// 인코딩된 JSON을 확인
|
||||
// throw new \Exception("Record:" . __FUNCTION__ . "\n" . json_encode($datas, JSON_PRETTY_PRINT) . "\n" . var_export($datas, true));
|
||||
$cf = $this->getMySocket()->put("zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}", $datas);
|
||||
$cf = json_decode($cf->getBody());
|
||||
if (!$cf->success) {
|
||||
$message = "Record:" . __FUNCTION__ . "에서 실패:\nrequest:" . var_export($datas, true) . "\nresponse:" . var_export($cf, true);
|
||||
log_message("error", $message);
|
||||
throw new \Exception($message);
|
||||
}
|
||||
//DB수정
|
||||
$formDatas = $this->getArrayByResult($cf->result);
|
||||
return $this->getModel()->modify($entity, $formDatas);
|
||||
$response = $this->getMySocket()->put("zones/{$this->getParentEntity()->getPK()}/dns_records/{$entity->getPK()}", $datas);
|
||||
$body = json_decode($response->getBody());
|
||||
// DB 수정
|
||||
return $this->getModel()->modify($entity, $this->getArrayByResult($body->result));
|
||||
}
|
||||
public function delete(RecordEntity $entity): RecordEntity
|
||||
{
|
||||
$cf = $this->getMySocket()->delete("zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}");
|
||||
$cf = json_decode($cf->getBody());
|
||||
if (!$cf->success) {
|
||||
$message = "Record:" . __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true);
|
||||
log_message("error", $message);
|
||||
throw new \Exception($message);
|
||||
}
|
||||
$this->getMySocket()->delete("zones/{$this->getParentEntity()->getPK()}/dns_records/{$entity->getPK()}");
|
||||
//DB삭제
|
||||
$this->getModel()->where(RecordModel::PK, $entity->getPK());
|
||||
$this->getModel()->delete();
|
||||
$this->getModel()->delete($entity->getPK());
|
||||
log_message("debug", $this->getModel()->getLastQuery());
|
||||
return $entity;
|
||||
}
|
||||
public function sync(RecordEntity $entity): RecordEntity
|
||||
{
|
||||
// 기존 Sync형태
|
||||
$cf = $this->getMySocket()->get("zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}");
|
||||
$cf = json_decode($cf->getBody());
|
||||
if (!$cf->success) {
|
||||
$message = "Record:" . __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true);
|
||||
log_message("error", $message);
|
||||
throw new \Exception($message);
|
||||
}
|
||||
$response = $this->getMySocket()->get("zones/{$this->getParentEntity()->getPK()}/dns_records/{$entity->getPK()}");
|
||||
$body = json_decode($response->getBody());
|
||||
// DB수정
|
||||
// log_message("debug", var_export($cf->result, true));
|
||||
$entity = $this->getModel()->modify($entity, $this->getArrayByResult($cf->result));
|
||||
$entity = $this->getModel()->modify($entity, $this->getArrayByResult($body->result));
|
||||
//Async형태
|
||||
// $promise = $this->getMySocket()getAsync("zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}");
|
||||
// $promise = $this->getMySocket()getAsync("zones/{$this->getParentEntity()->getPK()}/dns_records/{$entity->getPK()}");
|
||||
// $entity =$promise->then(
|
||||
// onFulfilled: function ($response) use ($entity): RecordEntity {
|
||||
// $record = json_decode($response->getBody(), true)['result'];
|
||||
@ -159,17 +141,19 @@ class Record extends Cloudflare
|
||||
//Reload
|
||||
public function reload(): array
|
||||
{
|
||||
log_message("notice", "\n-----------Zone {$this->_zone_entity->getTitle()}의 Record 처리 시작-----------");
|
||||
log_message("notice", "\n-----------Zone {$this->getParentEntity()->getTitle()}의 Record 처리 시작-----------");
|
||||
$entitys = [];
|
||||
try {
|
||||
$record_results = $this->reload_procedure("zones/{$this->_zone_entity->getPK()}/dns_records");
|
||||
if (count($record_results) > 0) {
|
||||
foreach ($record_results as $result) {
|
||||
$formDatas = $this->getArrayByResult($result);
|
||||
$entitys[$formDatas[RecordModel::PK]] = $this->getModel()->modify(new RecordEntity(), $formDatas);
|
||||
$results_array = $this->reload_procedure("zones/{$this->getParentEntity()->getPK()}/dns_records");
|
||||
if (count(value: $results_array) > 0) {
|
||||
foreach ($results_array as $results) {
|
||||
foreach ($results as $result) {
|
||||
$formDatas = $this->getArrayByResult($result);
|
||||
$entitys[$formDatas[RecordModel::PK]] = $this->getModel()->modify(new RecordEntity(), $formDatas);
|
||||
}
|
||||
}
|
||||
//부모키를 기준으로 CF에 존재하지 않는 데이터 DB삭제
|
||||
$this->getModel()->where(RecordModel::PARENT, $this->_zone_entity->getPK());
|
||||
$this->getModel()->where(RecordModel::PARENT, $this->getParentEntity()->getPK());
|
||||
$this->getModel()->whereNotIn(RecordModel::PK, array_keys($entitys));
|
||||
$this->getModel()->delete();
|
||||
log_message("debug", $this->getModel()->getLastQuery());
|
||||
@ -178,7 +162,7 @@ class Record extends Cloudflare
|
||||
log_message("error", $e->getMessage());
|
||||
throw new \Exception($e->getMessage());
|
||||
}
|
||||
log_message("notice", "\n-----------Zone {$this->_zone_entity->getTitle()}의 Record처리[" . count($entitys) . "개] 완료-----------");
|
||||
log_message("notice", "\n-----------Zone {$this->getParentEntity()->getTitle()}의 Record처리[" . count($entitys) . "개] 완료-----------");
|
||||
return $entitys;
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,12 +5,12 @@ namespace App\Services\Cloudflare;
|
||||
use App\Entities\Cloudflare\AccountEntity;
|
||||
use App\Entities\Cloudflare\ZoneEntity;
|
||||
use App\Models\Cloudflare\ZoneModel;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class Zone extends Cloudflare
|
||||
{
|
||||
private $_model = null;
|
||||
private $_account_entity = null;
|
||||
private ?AccountEntity $_parent_entity = null;
|
||||
private ?ZoneModel $_model = null;
|
||||
private $_setting_fields = [
|
||||
'development_mode' => 'off',
|
||||
'ipv6' => 'off',
|
||||
@ -20,13 +20,20 @@ class Zone extends Cloudflare
|
||||
];
|
||||
public function __construct(AccountEntity $account_entity)
|
||||
{
|
||||
$this->_account_entity = $account_entity;
|
||||
$auth_entity = $this->getAuthModel()->getEntityByPK($this->_account_entity->getParent());
|
||||
$this->_parent_entity = $account_entity;
|
||||
$auth_entity = $this->getAuthModel()->getEntityByPK(uid: $this->getParentEntity()->getParent());
|
||||
if ($auth_entity === null) {
|
||||
throw new \Exception("해당 계정정보를 찾을수 없습니다.");
|
||||
}
|
||||
parent::__construct($auth_entity);
|
||||
}
|
||||
protected function getParentEntity(): AccountEntity
|
||||
{
|
||||
if ($this->_parent_entity === null) {
|
||||
throw new \Exception(__FUNCTION__ . "에서 부모정보가 없습니다.");
|
||||
}
|
||||
return $this->_parent_entity;
|
||||
}
|
||||
protected function getModel(): ZoneModel
|
||||
{
|
||||
if ($this->_model === null) {
|
||||
@ -34,8 +41,9 @@ class Zone extends Cloudflare
|
||||
}
|
||||
return $this->_model;
|
||||
}
|
||||
protected function getArrayByResult($result, array $formDatas = []): array
|
||||
protected function getArrayByResult(\stdClass $result, array $formDatas = []): array
|
||||
{
|
||||
// log_message("debug", var_export($result, true));
|
||||
$formDatas[ZoneModel::PK] = $result->id;
|
||||
$formDatas[ZoneModel::PARENT] = $result->account->id;
|
||||
$formDatas[ZoneModel::TITLE] = $result->name;
|
||||
@ -61,22 +69,18 @@ class Zone extends Cloudflare
|
||||
$formDatas['updated_at'] = date("Y-m-d H:i:s");
|
||||
$formDatas['created_at'] = $result->created_on;
|
||||
$formDatas['plan'] = $result->plan->name;
|
||||
// log_message("debug", var_export($result, true));
|
||||
// log_message("debug", var_export($formDatas, true));
|
||||
return $formDatas;
|
||||
}
|
||||
//Cfzone에서 가져온 값을 zone에 setting
|
||||
private function getCFSetting(string $uid, array $formDatas = []): array
|
||||
{
|
||||
$cf = $this->getMySocket()->get('zones/' . $uid . '/settings');
|
||||
$cf = json_decode($cf->getBody());
|
||||
if (!$cf->success) {
|
||||
$message = "Zone:" . __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true);
|
||||
log_message("error", $message);
|
||||
throw new \Exception($message);
|
||||
}
|
||||
foreach ($cf->result as $result) {
|
||||
$response = $this->getMySocket()->get('zones/' . $uid . '/settings');
|
||||
$body = json_decode($response->getBody());
|
||||
// log_message("debug", var_export($body, true));
|
||||
foreach ($body->result as $result) {
|
||||
if (in_array(needle: $result->id, haystack: array_keys($this->_setting_fields))) {
|
||||
$formDatas[$result->id === 'ssl' ? 'ssl_mode' : $result->id] = $result->value;
|
||||
$formDatas[($result->id === 'ssl') ? 'ssl_mode' : $result->id] = $result->value;
|
||||
}
|
||||
}
|
||||
// log_message("debug", var_export($cf, return: true));
|
||||
@ -84,38 +88,26 @@ class Zone extends Cloudflare
|
||||
}
|
||||
private function setCFSetting(string $uid, string $field, string $value): string
|
||||
{
|
||||
if ($field === 'ssl_mode') {
|
||||
$field = 'ssl';
|
||||
}
|
||||
$field = ($field === 'ssl_mode') ? 'ssl' : $field;
|
||||
$datas = ['value' => $value];
|
||||
$cf = $this->getMySocket()->patch('zones/' . $uid . '/settings/' . $field, $datas);
|
||||
$cf = json_decode($cf->getBody());
|
||||
if (!$cf->success || $cf->result->id !== $field) {
|
||||
$message = "Zone:" . __FUNCTION__ . "에서 실패:\nrequest:" . var_export($datas, true) . "\nresponse:" . var_export($cf, true);
|
||||
log_message("error", $message);
|
||||
throw new \Exception($message);
|
||||
}
|
||||
$response = $this->getMySocket()->patch('zones/' . $uid . '/settings/' . $field, $datas);
|
||||
$body = json_decode($response->getBody());
|
||||
//최종 결과값은 body->result->id='필드명',body->result->value='on/off'이런식으로 받음
|
||||
return $cf->result->value;
|
||||
return $body->result->value;
|
||||
}
|
||||
public function create(string $domain, bool $jump_start = false): ZoneEntity
|
||||
{
|
||||
//Socket용
|
||||
//도메인생성을 위해 Cloudflare에 전송
|
||||
$datas = [
|
||||
'accountId' => $this->_account_entity->getPK(),
|
||||
'accountId' => $this->getParentEntity()->getPK(),
|
||||
'name' => $domain,
|
||||
'jump_start' => $jump_start,
|
||||
];
|
||||
$cf = $this->getMySocket()->post('zones/', $datas);
|
||||
$cf = json_decode($cf->getBody());
|
||||
if (!$cf->success) {
|
||||
$message = "Zone:" . __FUNCTION__ . "에서 실패:\nrequest:" . var_export($datas, true) . "\nresponse:" . var_export($cf, true);
|
||||
log_message("error", $message);
|
||||
throw new \Exception($message);
|
||||
}
|
||||
$response = $this->getMySocket()->post('zones/', $datas);
|
||||
$body = json_decode($response->getBody());
|
||||
//DB생성
|
||||
$formDatas = $this->getArrayByResult($cf->result);
|
||||
$formDatas = $this->getArrayByResult($body->result);
|
||||
foreach ($this->_setting_fields as $field => $default) {
|
||||
//초기화값 추가셋팅 ipv6 , development_mode , security_level
|
||||
$formDatas[$field] = $this->setCFSetting($formDatas[ZoneModel::PK], $field, $default);
|
||||
@ -134,29 +126,17 @@ class Zone extends Cloudflare
|
||||
}
|
||||
public function delete(ZoneEntity $entity): ZoneEntity
|
||||
{
|
||||
$cf = $this->getMySocket()->delete("zones/{$entity->getPK()}");
|
||||
$cf = json_decode($cf->getBody());
|
||||
if (!$cf->success) {
|
||||
$message = "Zone:" . __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true);
|
||||
log_message("error", $message);
|
||||
throw new \Exception($message);
|
||||
}
|
||||
$this->getMySocket()->delete("zones/{$entity->getPK()}");
|
||||
//DB삭제
|
||||
$this->getModel()->where(ZoneModel::PK, $entity->getPK());
|
||||
$this->getModel()->delete();
|
||||
$this->getModel()->delete($entity->getPK());
|
||||
log_message("debug", $this->getModel()->getLastQuery());
|
||||
return $entity;
|
||||
}
|
||||
public function sync(ZoneEntity $entity): ZoneEntity
|
||||
{
|
||||
$cf = $this->getMySocket()->get("zones/{$entity->getPK()}");
|
||||
$cf = json_decode($cf->getBody());
|
||||
if (!$cf->success) {
|
||||
$message = "Zone:" . __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true);
|
||||
log_message("error", $message);
|
||||
throw new \Exception($message);
|
||||
}
|
||||
$formDatas = $this->getArrayByResult($cf->result);
|
||||
$response = $this->getMySocket()->get("zones/{$entity->getPK()}");
|
||||
$body = json_decode($response->getBody());
|
||||
$formDatas = $this->getArrayByResult($body->result);
|
||||
// log_message("debug", var_export($formDatas, true));
|
||||
$formDatas = $this->getCFSetting($formDatas[ZoneModel::PK], $formDatas);
|
||||
// log_message("debug", var_export($formDatas, true));
|
||||
@ -167,18 +147,20 @@ class Zone extends Cloudflare
|
||||
//Reload
|
||||
public function reload(): array
|
||||
{
|
||||
log_message("notice", "\n-----------Account {$this->_account_entity->getTitle()}의 Zone처리 시작-----------");
|
||||
log_message("notice", "\n-----------Account {$this->getParentEntity()->getTitle()}의 Zone처리 시작-----------");
|
||||
$entitys = [];
|
||||
try {
|
||||
$zone_results = $this->reload_procedure("zones");
|
||||
if (count($zone_results) > 0) {
|
||||
foreach ($zone_results as $result) {
|
||||
$formDatas = $this->getArrayByResult($result);
|
||||
$formDatas = $this->getCFSetting($formDatas[ZoneModel::PK], $formDatas);
|
||||
$entitys[$formDatas[ZoneModel::PK]] = $this->getModel()->modify(new ZoneEntity(), $formDatas);
|
||||
$results_array = $this->reload_procedure("zones");
|
||||
if (count(value: $results_array) > 0) {
|
||||
foreach ($results_array as $results) {
|
||||
foreach ($results as $result) {
|
||||
$formDatas = $this->getArrayByResult($result);
|
||||
$formDatas = $this->getCFSetting($formDatas[ZoneModel::PK], $formDatas);
|
||||
$entitys[$formDatas[ZoneModel::PK]] = $this->getModel()->modify(new ZoneEntity(), $formDatas);
|
||||
}
|
||||
}
|
||||
//부모키를 기준으로 CF에 존재하지 않는 데이터 삭제용
|
||||
$this->getModel()->where(ZoneModel::PARENT, $this->_account_entity->getPK());
|
||||
$this->getModel()->where(ZoneModel::PARENT, value: $this->getParentEntity()->getPK());
|
||||
$this->getModel()->whereNotIn(ZoneModel::PK, array_keys($entitys));
|
||||
$this->getModel()->delete();
|
||||
log_message("debug", $this->getModel()->getLastQuery());
|
||||
@ -187,7 +169,7 @@ class Zone extends Cloudflare
|
||||
log_message("error", $e->getMessage());
|
||||
throw new \Exception($e->getMessage());
|
||||
}
|
||||
log_message("notice", "\n----------Account {$this->_account_entity->getTitle()}의 Zone 처리[" . count($entitys) . "개] 완료-----------");
|
||||
log_message("notice", "\n----------Account {$this->getParentEntity()->getTitle()}의 Zone 처리[" . count($entitys) . "개] 완료-----------");
|
||||
return $entitys;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user