cfmgrv4/app/Controllers/Admin/Cloudflare/ZoneController.php
2024-10-29 17:02:53 +09:00

290 lines
12 KiB
PHP

<?php
namespace App\Controllers\Admin\Cloudflare;
use App\Helpers\Cloudflare\ZoneHelper;
use App\Models\Cloudflare\AccountModel;
use App\Models\Cloudflare\ZoneModel;
use App\Services\Cloudflare\RecordService;
use App\Services\Cloudflare\ZoneService;
use CodeIgniter\HTTP\DownloadResponse;
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()->class_path}.title");;
$this->helper = new ZoneHelper();
}
protected function getModel(): ZoneModel
{
if ($this->_model === null) {
$this->_model = new ZoneModel();
}
return $this->_model;
}
protected function getService(): ZoneService
{
if ($this->service === null) {
$this->service = new ZoneService();
}
return $this->service;
}
protected function getFormFieldOption(string $field, array $options = []): array
{
switch ($field) {
case $this->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 init(string $action, array $fields = []): void
{
$this->action = $action;
$this->fields = count($fields) ? $fields : [$this->getModel()::PARENT, $this->getModel()::TITLE, 'name_servers', 'original_name_servers', 'plan', 'development_mode', 'ipv6', 'security_level', 'ssl_mode', 'always_use_https', 'status', 'updated_at', 'created_at'];
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
$this->filter_fields = [$this->getModel()::PARENT, 'plan', 'development_mode', 'ipv6', 'security_level', 'ssl_mode', 'always_use_https'];
$this->field_options = $this->getFormFieldOptions($this->filter_fields);
$this->batchjob_fields = ['development_mode', 'ipv6', 'security_level', 'ssl_mode', 'always_use_https'];
}
//생성
public function create_form(): RedirectResponse|string
{
$this->init('create', [$this->getModel()::PARENT, 'domains', 'hosts', 'type', 'content', 'proxied']);
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
$this->filter_fields = [$this->getModel()::PARENT, 'type', 'proxied'];
$this->field_options = $this->getFormFieldOptions($this->filter_fields);
//부모데이터 정의
$parent_field = $this->getModel()::PARENT;
$this->$parent_field = $this->request->getVar($parent_field) ?: DEFAULTS["EMPTY"];
return $this->create_form_procedure();
}
protected function create_process(): void
{
//DB작업도 Socket에서 다 처리하므로 parent::create_process()하면 않됨
$this->formDatas = $this->create_validate($this->action, $this->fields);
//데이터검증
//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->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처리
//부모데이터 정의
$this->_account_entity = $this->getAccountModel()->getEntityByPK($this->formDatas[$this->getModel()::PARENT]);
//Zone생성
$cnt = 1;
$entitys = [];
foreach ($this->formDatas['domains'] as $domain) {
$entity = $this->getService()->create($this->_account_entity, $domain);
$entitys[] = $entity;
$cnt++;
}
//Record생성
foreach ($entitys as $entity) {
$entity->records = [];
$record = new RecordService();
foreach ($this->formDatas['hosts'] as $host) {
$entity->records[] = $record->create(
$entity,
$host,
$this->formDatas['type'],
$this->formDatas['content'],
$this->formDatas['proxied']
);
}
}
$this->entitys = $entitys;
}
protected function create_process_result(): RedirectResponse|string
{
$this->init(__FUNCTION__);
helper(['form']);
$this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []];
return view(
strtolower($this->view_path . $this->class_path . "/create_result"),
data: ['viewDatas' => $this->getViewDatas()]
);
}
public function create(): RedirectResponse|string
{
$this->init(__FUNCTION__, [$this->getModel()::PARENT, 'domains', 'hosts', 'type', 'content', 'proxied']);
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
$this->filter_fields = [$this->getModel()::PARENT, 'type', 'proxied'];
$this->field_options = $this->getFormFieldOptions($this->filter_fields);
return $this->create_procedure();
}
//수정 (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->getModel()->getEntityByPK($uid);
if ($this->entity === null) {
throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
}
//부모데이터정의
$this->_account_entity = $this->getAccountModel()->getEntityByPK($this->entity->getParent());
//Socket처리
$this->entity = $this->getService()->modify($this->_account_entity, $this->entity, $this->formDatas);
}
public function toggle(mixed $uid, string $field): RedirectResponse
{
return $this->toggle_procedure($uid, $field);
}
//일괄처리작업
public function batchjob(): RedirectResponse
{
$this->init(__FUNCTION__);
return $this->batchjob_procedure();
}
//View
//create_process_result에서 결과값을 entitys에 저장하고 호출하기때문에 아래와 같이 처리함
//create_process_result에서 같이 사용한다는 점 주의
public function view(string $uid): RedirectResponse|string
{
$this->init(__FUNCTION__);
return $this->view_procedure($uid);
}
//삭제
protected function delete_process(mixed $uid): void
{
//DB작업도 Socket에서 다 처리하므로 parent::delete_process($uid)하면 않됨
//자신정보정의
$this->entity = $this->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->getAccountModel()->getEntityByPK($this->entity->getParent());
$this->entity = $this->getService()->delete($this->_account_entity, $this->entity);
}
// 리스트
protected function list_entitys_process(): array
{
$this->list_condition_process();
//기본Soring처리
$this->getModel()->orderBy(orderBy: AccountModel::TABLE . "." . AccountModel::TITLE . " ASC ," . $this->getModel()::TITLE . " ASC");
//Sorting 처리
$this->order_field = $this->request->getVar('order_field') ?: DEFAULTS['EMPTY'];
$this->order_value = $this->request->getVar('order_value') ?: DEFAULTS['EMPTY'];
if ($this->order_field !== DEFAULTS['EMPTY'] && $this->order_value !== DEFAULTS['EMPTY']) {
$this->getModel()->orderBy(sprintf(
"%s.%s %s",
$this->getModel()::TABLE,
$this->order_field,
$this->order_value
));
}
$this->getModel()->limit($this->per_page, $this->page * $this->per_page - $this->per_page);
//Join을 해서 도메인부터 Sorting하기위함
$this->getModel()->join(AccountModel::TABLE, sprintf(
"%s.%s=%s.%s",
$this->getModel()::TABLE,
$this->getModel()::PARENT,
AccountModel::TABLE,
AccountModel::PK
));
$entitys = $this->getModel()->select($this->getModel()::TABLE . '.*')->findAll();
log_message("debug", $this->getModel()->getLastQuery());
return $entitys;
}
public function index(): string
{
$this->init(__FUNCTION__);
return $this->list_procedure();
}
// Download
public function download(string $output_type, mixed $uid = false): DownloadResponse|string
{
$this->init(__FUNCTION__);
return $this->download_procedure($output_type, $uid);
}
//Sync작업
protected function sync_process(mixed $uid): void
{
//자신정보정의
$this->entity = $this->getModel()->getEntityByPK($uid);
if ($this->entity === null) {
throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
}
//부모데이터정의
$this->_account_entity = $this->getAccountModel()->getEntityByPK($this->entity->getParent());
//Socket처리
$this->entity = $this->getService()->sync($this->_account_entity, $this->entity);
}
public function sync(string $uid): RedirectResponse
{
return $this->sync_procedure($uid);
}
//reload Zone By Account
protected function reload_process(mixed $uid): void
{
//부모데이터정의
$this->_account_entity = $this->getAccountModel()->getEntityByPK($uid);
if ($this->_account_entity === null) {
throw new \Exception("Account: {$uid} 정보를 찾을수 없습니다.");
}
$this->getService()->reload($this->_account_entity);
}
public function reload(string $uid): RedirectResponse
{
return $this->reload_procedure($uid);
}
}