159 lines
6.6 KiB
PHP
159 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries\MyCloudflare;
|
|
|
|
use App\Models\Cloudflare\ZoneModel;
|
|
use App\Models\Cloudflare\AccountModel;
|
|
use App\Libraries\MyCloudflare\MyCloudflare;
|
|
use App\Entities\Cloudflare\ZoneEntity;
|
|
use App\Entities\Cloudflare\AccountEntity;
|
|
|
|
class Zone extends MyCloudflare
|
|
{
|
|
private $_account_entity = null;
|
|
public function __construct($mySocket, $myStorage, AccountEntity $account_entity)
|
|
{
|
|
parent::__construct($mySocket, $myStorage);
|
|
$this->_account_entity = $account_entity;
|
|
}
|
|
final protected function getMyStorage(): ZoneModel
|
|
{
|
|
if ($this->_myStorage === null) {
|
|
throw new \Exception("MyStorage가 정의되지 않았습니다.");
|
|
}
|
|
return $this->_myStorage;
|
|
}
|
|
private function getArrayByResult($cf): array
|
|
{
|
|
$formDatas['uid'] = $cf->id;
|
|
$formDatas['account_uid'] = $cf->account->id;
|
|
$formDatas['domain'] = $cf->name;
|
|
$formDatas['status'] = $cf->status;
|
|
//$formDatas['type'] = $cf->type; // full 이게있는데 뭔지 잘모름
|
|
$formDatas['name_servers'] = 'none';
|
|
if (isset($cf->name_servers)) {
|
|
$formDatas['name_servers'] = is_array($cf->name_servers) ?
|
|
implode(
|
|
',',
|
|
$cf->name_servers
|
|
) : $cf->name_servers;
|
|
}
|
|
$formDatas['original_name_servers'] = 'none';
|
|
if (isset($cf->original_name_servers)) {
|
|
$formDatas['original_name_servers'] = is_array($cf->original_name_servers) ?
|
|
implode(
|
|
',',
|
|
$cf->original_name_servers
|
|
) : $cf->original_name_servers;
|
|
}
|
|
$formDatas['updated_at'] = $cf->modified_on;
|
|
$formDatas['created_at'] = $cf->created_on;
|
|
$formDatas['plan'] = $cf->plan->name;
|
|
return $formDatas;
|
|
}
|
|
|
|
//Cfzone에서 가져온 값을 zone에 setting
|
|
final public function getCFSetting(ZoneEntity $entity): ZoneEntity
|
|
{
|
|
$cf = $this->getMySocket()->getAdapter($this->_account_entity->getAPIKey())
|
|
->patch('zones/' . $entity->getPK() . '/settings/');
|
|
$cf = json_decode($cf->getBody());
|
|
if (!$cf->success) {
|
|
throw new \Exception(__FUNCTION__ . "에서 실패:\n" . var_export($cf, true));
|
|
}
|
|
foreach ($cf->result as $cf) {
|
|
switch ($cf->id) {
|
|
case 'development_mode':
|
|
$entity->development_mode = $cf->value;
|
|
break;
|
|
case 'ipv6':
|
|
$entity->ipv6 = $cf->value;
|
|
break;
|
|
case 'security_level':
|
|
$entity->security_level = $cf->value;
|
|
break;
|
|
}
|
|
}
|
|
return $entity;
|
|
}
|
|
final public function setCFSetting(ZoneEntity $entity, string $field, string $value): ZoneEntity
|
|
{
|
|
$cf = $this->getMySocket()->getAdapter($this->_account_entity->getAPIKEY())
|
|
->patch('zones/' . $entity->getPK() . '/settings/' . $field, array('value' => $value));
|
|
$cf = json_decode($cf->getBody());
|
|
if (!$cf->success || $cf->result->id !== $field) {
|
|
throw new \Exception(__FUNCTION__ . "에서 {$field}->{$value} 변경실패:\n" . var_export($cf, true));
|
|
}
|
|
//최종 결과값은 body->result->id='필드명',body->result->value='on/off'이런식으로 받음
|
|
$entity->$field = $cf->result->value;
|
|
return $entity;
|
|
}
|
|
//Result 형태
|
|
|
|
public function create(array $formDatas, $jump_start = false): ZoneEntity
|
|
{
|
|
//Socket용
|
|
// $cf = $this->getMySocket()->getZone($this->_account_entity->getAPIKEY())
|
|
// ->addZone($formDatas[ZoneModel::TITLE], $jump_start, $this->_account_entity->getPK());
|
|
//도메인생성을 위해 Cloudflare에 전송
|
|
$options = [
|
|
'accountId' => $this->_account_entity->getPK(),
|
|
'name' => $formDatas['domain'],
|
|
'jump_start' => $jump_start,
|
|
];
|
|
$cf = $this->getMySocket()->getAdapter($this->_account_entity->getAPIKey())
|
|
->post('zones/', $options);
|
|
$cf = json_decode($cf->getBody());
|
|
if (!$cf->success) {
|
|
throw new \Exception(__FUNCTION__ . "에서 실패:\n" . var_export($cf, true));
|
|
}
|
|
|
|
//Storage용
|
|
$formDatas = $this->getArrayByResult($cf->result);
|
|
$entity = $this->$this->getMyStorage()->create($formDatas);
|
|
//아래는 추가 셋팅 ipv6 TurnOFF , //Development mode TurnOFF
|
|
$entity = $this->setCFSetting($entity, 'ipv6', 'off');
|
|
$entity = $this->setCFSetting($entity, 'development_mode', 'off');
|
|
$entity = $this->setCFSetting($entity, 'security_level', 'medium');
|
|
log_message("notice", "Zone::" . __FUNCTION__ . "=> 작업을 완료하였습니다.");
|
|
return $entity;
|
|
}
|
|
public function update(ZoneEntity $entity, array $fieldDatas): ZoneEntity
|
|
{
|
|
//ipv6 , //development_mode , //security_level
|
|
foreach ($fieldDatas as $field => $value) {
|
|
$entity = $this->setCFSetting($entity, $field, $value);
|
|
}
|
|
log_message("notice", "Zone::" . __FUNCTION__ . "=> 작업을 완료하였습니다.");
|
|
return $entity;
|
|
}
|
|
public function delete(ZoneEntity $entity): void
|
|
{
|
|
$cf = $this->getMySocket()->getAdapter($this->_account_entity->getAPIKey())
|
|
->delete('zones/' . $entity->getPK());
|
|
$cf = json_decode($cf->getBody());
|
|
if (!$cf->success) {
|
|
throw new \Exception(__FUNCTION__ . "에서 실패:\n" . var_export($cf, true));
|
|
}
|
|
log_message("notice", "Zone::" . __FUNCTION__ . "=> 작업을 완료하였습니다.");
|
|
}
|
|
public function sync(ZoneEntity $entity): ZoneEntity
|
|
{
|
|
$cf = $this->getMySocket()->getAdapter($this->_account_entity->getAPIKey())
|
|
->get('zones/' . $entity->getPK());
|
|
$cf = json_decode($cf->getBody());
|
|
if (!$cf->success) {
|
|
throw new \Exception(__FUNCTION__ . "에서 실패:\n" . var_export($cf, true));
|
|
}
|
|
log_message("notice", "Zone::" . __FUNCTION__ . "=> 작업을 완료하였습니다.");
|
|
$formDatas = $this->getArrayByResult($cf->result);
|
|
return $this->$this->getMyStorage()->create($formDatas);
|
|
}
|
|
// protected function getCFResults_List(int $page): array
|
|
// {
|
|
// $this->_endPoint = is_null($this->_endPoint) ? new \Cloudflare\API\Endpoints\Zones($this->getAdapter()) : $this->_endPoint;
|
|
// return $this->_endPoint->listZones('', '', $page, CF_ADAPTER_PERPAGE_MAX)->result;
|
|
// }
|
|
public function reload() {}
|
|
}
|