225 lines
9.5 KiB
PHP
225 lines
9.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloudflare;
|
|
|
|
use App\Entities\Cloudflare\RecordEntity;
|
|
use App\Entities\Cloudflare\ZoneEntity;
|
|
use App\Models\Cloudflare\AccountModel;
|
|
use App\Models\Cloudflare\RecordModel;
|
|
use App\Services\MyLogService;
|
|
|
|
class RecordService extends CloudflareService
|
|
{
|
|
private ?ZoneEntity $_parent_entity = null;
|
|
private ?RecordModel $_model = null;
|
|
private ?AccountModel $_accountModel = null;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct("Record", "Record");
|
|
}
|
|
|
|
private function getParentEntity(): ZoneEntity
|
|
{
|
|
if ($this->_parent_entity === null) {
|
|
throw new \Exception(__FUNCTION__ . "에서 부모정보가 없습니다.");
|
|
}
|
|
return $this->_parent_entity;
|
|
}
|
|
|
|
private function setParentEntity(ZoneEntity $parent_entity): void
|
|
{
|
|
$this->_parent_entity = $parent_entity;
|
|
$account_entity = $this->getAccountModel()->getEntityByPK($this->getParentEntity()->getParent());
|
|
if ($account_entity === null) {
|
|
throw new \Exception("해당 계정정보를 찾을수 없습니다.");
|
|
}
|
|
$auth_entity = $this->getAuthModel()->getEntityByPK($account_entity->getParent());
|
|
if ($auth_entity === null) {
|
|
throw new \Exception("해당 계정정보를 찾을수 없습니다.");
|
|
}
|
|
$this->setAuthEntity($auth_entity);
|
|
}
|
|
|
|
public function getModel(): RecordModel
|
|
{
|
|
if ($this->_model === null) {
|
|
$this->_model = new RecordModel();
|
|
}
|
|
return $this->_model;
|
|
}
|
|
|
|
protected function getAccountModel(): AccountModel
|
|
{
|
|
if ($this->_accountModel === null) {
|
|
$this->_accountModel = new AccountModel();
|
|
}
|
|
return $this->_accountModel;
|
|
}
|
|
|
|
protected function getArrayByResult(\stdClass $result, array $formDatas = []): array
|
|
{
|
|
// log_message("debug", var_export($result, true));
|
|
$formDatas[RecordModel::PK] = $result->id;
|
|
$formDatas[RecordModel::PARENT] = $this->getParentEntity()->getPK();
|
|
$formDatas[RecordModel::TITLE] = $result->name;
|
|
$formDatas['type'] = $result->type;
|
|
$formDatas['content'] = $result->content;
|
|
$formDatas['ttl'] = (int) $result->ttl;
|
|
$formDatas['proxiable'] = $result->proxiable ? "on" : "off";
|
|
$formDatas['proxied'] = $result->proxied ? "on" : "off";
|
|
$formDatas['locked'] = "on";
|
|
$formDatas['updated_at'] = date("Y-m-d H:i:s");
|
|
$formDatas['created_at'] = $result->created_on;
|
|
// log_message("debug", print_r($formDatas, true));
|
|
return $formDatas;
|
|
}
|
|
|
|
public function create(ZoneEntity $parent_entity, string $host, string $type, string $content, string $proxied): RecordEntity
|
|
{
|
|
//부모데이터정의
|
|
$this->setParentEntity($parent_entity);
|
|
//Socket용
|
|
//호스트생성을 위해 Cloudflare에 전송
|
|
$datas = [
|
|
'name' => $host,
|
|
'type' => $type,
|
|
'content' => $content,
|
|
'proxied' => $proxied === 'on'
|
|
];
|
|
$response = $this->getMySocket()->post("zones/{$this->getParentEntity()->getPK()}/dns_records", $datas);
|
|
$body = json_decode($response->getBody());
|
|
$formDatas = $this->getArrayByResult($body->result);
|
|
//생성값 formDatas Log남기기
|
|
foreach ($formDatas as $field => $value) {
|
|
MyLogService::add("info", "{$field}:{$value}");
|
|
}
|
|
//DB생성
|
|
$entity = $this->getModel()->create($formDatas);
|
|
// log_message("debug", $this->getModel()->getLastQuery());
|
|
return $entity;
|
|
}
|
|
|
|
public function modify(ZoneEntity $parent_entity, RecordEntity $entity, array $formDatas): RecordEntity
|
|
{
|
|
//fixed 필드가 있고 값이 변경되었을때(toggle에서 fixed만 설정시사용)
|
|
if (in_array('fixed', array_keys($formDatas))) {
|
|
if ($formDatas['fixed'] !== $entity->fixed) {
|
|
$entity = $this->getModel()->modify($entity, $formDatas);
|
|
}
|
|
return $entity;
|
|
}
|
|
|
|
//부모데이터정의
|
|
$this->setParentEntity($parent_entity);
|
|
//TTL값은 CDN(proxied)가 사용함일때는 무조건 1, 않함일때는 120이 적용
|
|
$datas = [
|
|
'type' => $formDatas['type'] ?? $entity->type,
|
|
'name' => $formDatas['host'] ?? $entity->host,
|
|
'content' => $formDatas['content'] ?? $entity->content,
|
|
'proxied' => $entity->proxied === 'on',
|
|
'ttl' => intval($entity->ttl),
|
|
];
|
|
if (isset($formDatas['proxied'])) {
|
|
if ($formDatas['proxied'] === 'on') {
|
|
$datas['proxied'] = true;
|
|
$datas['ttl'] = 1;
|
|
} else {
|
|
$datas['proxied'] = false;
|
|
$datas['ttl'] = 120;
|
|
}
|
|
}
|
|
// 인코딩된 JSON을 확인
|
|
// throw new \Exception("Record:" . __FUNCTION__ . "\n" . json_encode($datas, JSON_PRETTY_PRINT) . "\n" . var_export($datas, true));
|
|
$response = $this->getMySocket()->put("zones/{$this->getParentEntity()->getPK()}/dns_records/{$entity->getPK()}", $datas);
|
|
$body = json_decode($response->getBody());
|
|
$formDatas = $this->getArrayByResult($body->result);
|
|
//변경전 entity 값, 변경값 formDatas Log남기기
|
|
foreach ($formDatas as $field => $value) {
|
|
MyLogService::add("info", "{$field}:{$entity->$field}=>{$value}");
|
|
}
|
|
//DB수정
|
|
$entity = $this->getModel()->modify($entity, $formDatas);
|
|
// log_message("debug", $this->getModel()->getLastQuery());
|
|
return $entity;
|
|
}
|
|
|
|
public function delete(ZoneEntity $parent_entity, RecordEntity $entity): RecordEntity
|
|
{
|
|
//부모데이터정의
|
|
$this->setParentEntity($parent_entity);
|
|
$this->getMySocket()->delete("zones/{$this->getParentEntity()->getPK()}/dns_records/{$entity->getPK()}");
|
|
//DB삭제
|
|
$this->getModel()->delete($entity->getPK());
|
|
//삭제전 entity 값 Log남기기
|
|
foreach ($this->getModel()->getAllowedFields() as $field) {
|
|
MyLogService::add("info", "{$field}:{$entity->$field}");
|
|
}
|
|
// log_message("debug", $this->getModel()->getLastQuery());
|
|
return $entity;
|
|
}
|
|
|
|
public function sync(ZoneEntity $parent_entity, RecordEntity $entity): RecordEntity
|
|
{
|
|
//부모데이터정의
|
|
$this->setParentEntity($parent_entity);
|
|
// 기존 Sync형태
|
|
$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($body->result));
|
|
//Async형태
|
|
// $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'];
|
|
// log_message("debug", var_export($record));
|
|
// //DB수정
|
|
// return $this->getModel()->modify($entity, $this->getArrayByResult($record));
|
|
// },
|
|
// onRejected: function ($error) {
|
|
// log_message('error', 'Failed to fetch DNS records: ' . $error->getMessage());
|
|
// throw new \Exception('Failed to fetch DNS records: ' . $error->getMessage());
|
|
// }
|
|
// );
|
|
// $promise->wait();
|
|
return $entity;
|
|
}
|
|
|
|
//Reload
|
|
public function reload(ZoneEntity $parent_entity): array
|
|
{
|
|
//부모데이터정의
|
|
$this->setParentEntity($parent_entity);
|
|
// log_message("notice", "\n-----------Zone {$this->getParentEntity()->getTitle()}의 Record 처리 시작-----------");
|
|
$entities = [];
|
|
try {
|
|
$results = $this->reload_procedure("zones/{$this->getParentEntity()->getPK()}/dns_records");
|
|
$total = count($results);
|
|
if ($total > 0) {
|
|
foreach ($results as $index => $result) {
|
|
if (!is_object($result) || get_class($result) !== 'stdClass') {
|
|
log_message("error", "Record: result is not a stdClass:\n" . var_export($result, true) . "\n");
|
|
continue;
|
|
}
|
|
$formDatas = $this->getArrayByResult($result);
|
|
$entity = $this->getModel()->modify(new RecordEntity(), $formDatas);
|
|
log_message("debug", ($index + 1) . "/{$total} => {$entity->getTitle()} Record 처리,[{$this->getMySocket()::$_request}]");
|
|
$entities[$entity->getPK()] = $entity;
|
|
}
|
|
//부모키를 기준으로 CF에 존재하지 않는 데이터 DB삭제
|
|
$this->getModel()->where(RecordModel::PARENT, $this->getParentEntity()->getPK());
|
|
$this->getModel()->whereNotIn(RecordModel::PK, array_keys($entities));
|
|
$this->getModel()->delete();
|
|
// log_message("debug", $this->getModel()->getLastQuery());
|
|
}
|
|
} catch (\Exception $e) {
|
|
log_message("error", $e->getMessage());
|
|
throw new \Exception($e->getMessage());
|
|
}
|
|
// log_message("notice", "\n-----------Zone {$this->getParentEntity()->getTitle()}의 Record처리[" . count($entities) . "개] 완료-----------");
|
|
return $entities;
|
|
}
|
|
}
|