215 lines
8.8 KiB
PHP
215 lines
8.8 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Admin\Cloudflare;
|
|
|
|
use App\Entities\Cloudflare\AccountEntity;
|
|
use App\Helpers\Cloudflare\ZoneHelper;
|
|
use App\Models\Cloudflare\AccountModel;
|
|
use App\Services\Cloudflare\RecordService;
|
|
use App\Services\Cloudflare\ZoneService;
|
|
use App\Services\MyLogService;
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class ZoneController extends CloudflareController
|
|
{
|
|
private $_account_entity = null;
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
parent::initController($request, $response, $logger);
|
|
$this->title = lang("{$this->getService()->getClassPath()}.title");;
|
|
$this->helper = new ZoneHelper();
|
|
}
|
|
protected function getService(): ZoneService
|
|
{
|
|
if ($this->service === null) {
|
|
$this->service = new ZoneService();
|
|
$this->class_name = $this->service->getClassName();
|
|
$this->class_path = $this->service->getClassPath();
|
|
}
|
|
return $this->service;
|
|
}
|
|
protected function getFormFieldOption(string $field, array $options = []): array
|
|
{
|
|
switch ($field) {
|
|
case $this->getService()->getModel()::PARENT:
|
|
// $this->getAccountModel()->where('status', DEFAULTS['STATUS']);
|
|
$options[$field] = $this->getAccountModel()->getFormFieldOption($field);
|
|
// echo $this->getAccountModel()->getLastQuery();
|
|
// dd($options);
|
|
break;
|
|
case 'type':
|
|
case 'proxied':
|
|
$options[$field] = lang('Cloudflare/Record.' . strtoupper($field));
|
|
break;
|
|
default:
|
|
$options = parent::getFormFieldOption($field, $options);
|
|
break;
|
|
}
|
|
return $options;
|
|
}
|
|
|
|
//부모데이터 정의
|
|
private function getParentEntity($uid): AccountEntity
|
|
{
|
|
//부모데이터정의
|
|
$account_entity = $this->getAccountModel()->getEntityByPK($uid);
|
|
if ($account_entity === null) {
|
|
throw new \Exception("Zone: [{$this->entity->getParent()}] 정보를 찾을수 없습니다.");
|
|
}
|
|
return $account_entity;
|
|
}
|
|
|
|
//생성
|
|
protected function create_init(string $action, $fields = []): void
|
|
{
|
|
$fields = [
|
|
'fields' => [$this->getService()->getModel()::PARENT, 'hosts', 'type', 'content', 'proxied'],
|
|
'filter_fields' => [$this->getService()->getModel()::PARENT, 'type', 'proxied']
|
|
];
|
|
parent::create_init($action, $fields);
|
|
//부모데이터 정의
|
|
$parent_field = $this->getService()->getModel()::PARENT;
|
|
$this->$parent_field = $this->request->getVar($parent_field) ?: DEFAULTS["EMPTY"];
|
|
}
|
|
protected function create_process(): void
|
|
{
|
|
//DB작업도 Socket에서 다 처리하므로 parent::create_process()하면 않됨
|
|
$this->formDatas = $this->create_validate($this->action, $this->fields);
|
|
//부모데이터 정의
|
|
$this->_account_entity = $this->getParentEntity($this->formDatas[$this->getService()->getModel()::PARENT]);
|
|
//데이터검증
|
|
//Type이 A형식일경우 IP형태인지 확인
|
|
if ($this->formDatas['type'] === 'A') {
|
|
if (!$this->helper->isIPAddress($this->formDatas['content'], $this->formDatas['type'])) {
|
|
throw new \Exception("{$this->formDatas['type']}, {$this->formDatas['content']} 형식 오류[사설IP 않됨]");
|
|
}
|
|
}
|
|
//도메인명 형식확인
|
|
$domains = explode("\n", $this->formDatas['domains']);
|
|
if (!is_array($domains) || !count($domains)) {
|
|
throw new \Exception("도메인명이 정의되지 않았습니다.");
|
|
}
|
|
$cnt = 1;
|
|
foreach ($domains as $domain) {
|
|
//도메인명 형식확인
|
|
if (!$this->helper->isDomain($domain)) {
|
|
throw new \Exception("{$cnt}번째 : [{$domain}]는 도메인명 형식에 맞지 않습니다.");
|
|
}
|
|
//도메인명이 해당계정의 유일한 도메인인지 확인
|
|
if (!$this->getService()->getModel()->isUniqueDomain($this->_account_entity, $domain)) {
|
|
throw new \Exception("{$cnt}번째 : [{$domain}]는 이미 등록된 도메인입니다.");
|
|
}
|
|
$cnt++;
|
|
}
|
|
//호스트명 형식확인
|
|
$hosts = explode("\n", $this->formDatas['hosts']);
|
|
if (!is_array($hosts) || !count($hosts)) {
|
|
throw new \Exception("호스트명이 정의되지 않았습니다.");
|
|
}
|
|
$cnt = 1;
|
|
foreach ($hosts as $host) {
|
|
if (!$this->helper->isHost($host)) {
|
|
throw new \Exception("{$cnt}번째 : [{$host}]는 호스트명 형식에 맞지 않습니다.");
|
|
}
|
|
$cnt++;
|
|
}
|
|
//Socket처리
|
|
//Zone생성
|
|
$entitys = [];
|
|
foreach ($domains as $domain) {
|
|
$entity = $this->getService()->create($this->_account_entity, $domain);
|
|
$entitys[] = $entity;
|
|
}
|
|
//Record생성
|
|
$record = new RecordService();
|
|
foreach ($entitys as $entity) {
|
|
$record_entitys = [];
|
|
foreach ($hosts as $host) {
|
|
$record_entitys[] = $record->create(
|
|
$entity,
|
|
$host,
|
|
$this->formDatas['type'],
|
|
$this->formDatas['content'],
|
|
$this->formDatas['proxied']
|
|
);
|
|
}
|
|
$entity->records = $record_entitys;
|
|
}
|
|
$this->entitys = $entitys;
|
|
}
|
|
|
|
//수정관련 (modify,toggle,batchjob사용)
|
|
protected function modify_process(mixed $uid): void
|
|
{
|
|
//DB작업도 Socket에서 다 처리하므로 parent::modify_process($uid)하면 않됨
|
|
$this->formDatas = $this->modify_validate($this->action, $this->fields);
|
|
//자신정보정의
|
|
$this->entity = $this->getService()->getModel()->getEntityByPK($uid);
|
|
if ($this->entity === null) {
|
|
throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
|
|
}
|
|
//부모데이터정의
|
|
$this->_account_entity = $this->getParentEntity($this->entity->getParent());
|
|
//Socket처리
|
|
$this->entity = $this->getService()->modify($this->_account_entity, $this->entity, $this->formDatas);
|
|
}
|
|
//삭제관련
|
|
protected function delete_process(mixed $uid): void
|
|
{
|
|
//DB작업도 Socket에서 다 처리하므로 parent::delete_process($uid)하면 않됨
|
|
//자신정보정의
|
|
$this->entity = $this->getService()->getModel()->getEntityByPK($uid);
|
|
if ($this->entity === null) {
|
|
throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
|
|
}
|
|
//Record부터 삭제필요
|
|
$this->getRecordModel()->where($this->getRecordModel()::PARENT, $this->entity->getPK());
|
|
foreach ($this->getRecordModel()->getEntitys() as $record_entity) {
|
|
$record = new RecordService();
|
|
$record->delete($this->entity, $record_entity);
|
|
}
|
|
//Zone 삭제
|
|
//부모데이터정의
|
|
$this->_account_entity = $this->getParentEntity($this->entity->getParent());
|
|
$this->entity = $this->getService()->delete($this->_account_entity, $this->entity);
|
|
}
|
|
//리스트관련
|
|
protected function index_entitys_process(): array
|
|
{
|
|
//기본Soring처리
|
|
$this->getService()->getModel()->orderBy(orderBy: AccountModel::TABLE . "." . AccountModel::TITLE . " ASC ," . $this->getService()->getModel()::TITLE . " ASC");
|
|
//Join을 해서 Account부터 Sorting하기위함
|
|
$this->getService()->getModel()->join(AccountModel::TABLE, sprintf(
|
|
"%s.%s=%s.%s",
|
|
$this->getService()->getModel()::TABLE,
|
|
$this->getService()->getModel()::PARENT,
|
|
AccountModel::TABLE,
|
|
AccountModel::PK
|
|
));
|
|
return parent::index_entitys_process();
|
|
}
|
|
//Sync작업관련
|
|
protected function sync_process(mixed $uid): void
|
|
{
|
|
//자신정보정의
|
|
$this->entity = $this->getService()->getModel()->getEntityByPK($uid);
|
|
if ($this->entity === null) {
|
|
throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
|
|
}
|
|
//부모데이터정의
|
|
$this->_account_entity = $this->getParentEntity($this->entity->getParent());
|
|
//Socket처리
|
|
$this->entity = $this->getService()->sync($this->_account_entity, $this->entity);
|
|
}
|
|
//Reload작업관련
|
|
protected function reload_process(mixed $account_uid): void
|
|
{
|
|
//부모데이터정의
|
|
$this->_account_entity = $this->getParentEntity($account_uid);
|
|
$this->getService()->reload($this->_account_entity);
|
|
}
|
|
}
|