cfmgrv4/app/Controllers/Admin/Cloudflare/ZoneController.php
2024-09-27 19:44:50 +09:00

169 lines
7.4 KiB
PHP

<?php
namespace App\Controllers\Admin\Cloudflare;
use App\Libraries\MySocket\Cloudflare\RecordSocket;
use App\Libraries\MySocket\Cloudflare\ZoneSocket;
use App\Models\Cloudflare\ZoneModel;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class ZoneController extends CloudflareController
{
private $_mySocket = null;
private $_account_entity = null;
private $_model = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->class_name = "Zone";
$this->class_path .= $this->class_name;
$this->title = lang("{$this->class_path}.title");
helper($this->class_path);
}
final protected function getModel(): ZoneModel
{
if ($this->_model === null) {
$this->_model = new ZoneModel();
}
return $this->_model;
}
final protected function getMySocket(): ZoneSocket
{
if ($this->_mySocket === null) {
$this->_mySocket = new ZoneSocket($this->_account_entity);
}
return $this->_mySocket;
}
protected function getFormFieldOption(string $field, array $options = []): array
{
switch ($field) {
case $this->getModel()::PARENT:
$this->getAccountModel()->where('status', DEFAULTS['STATUS']);
$options[$field] = [
DEFAULTS['EMPTY'] => lang($this->class_path . '.label.' . $field) . ' 선택',
...$this->getAccountModel()->getFormFieldOption($field, $options),
];
// echo $this->getAccountModel()->getLastQuery();
// dd($options);
break;
default:
$options = parent::getFormFieldOption($field, $options);
break;
}
return $options;
}
protected function getFormData(string $field, array $formDatas): array
{
switch ($field) {
case 'domains':
$formDatas[$field] = explode("\n", $this->request->getVar($field));
if (!is_array($formDatas[$field]) || !count($formDatas[$field])) {
throw new \Exception("도메인명이 정의되지 않았습니다.");
}
break;
case 'hosts':
$formDatas[$field] = explode("\n", $this->request->getVar($field));
if (!is_array($formDatas[$field]) || !count($formDatas[$field])) {
throw new \Exception("호스트명이 정의되지 않았습니다");
}
break;
default:
$formDatas = parent::getFormData($field, $formDatas);
break;
}
return $formDatas;
}
//생성
protected function create_validate(): void
{
//domains,hosts를 제외한 fields Valid처리
$this->validateFormDatas(array_diff($this->fields, ['domains', 'hosts']));
}
protected function create_process(): void
{
parent::create_process();
//Zone생성
$zone_entitys = [];
foreach ($this->formDatas['domains'] as $domain) {
//Storage용
$result = $this->getMySocket()->create($domain);
$formDatas = $this->getMySocket()->getArrayByResult($result);
$entity = $this->getModel()->create($formDatas);
//아래는 추가 셋팅 ipv6 TurnOFF , //Development mode TurnOFF
$entity = $this->getMySocket()->setCFSetting($entity, 'ipv6', 'off');
$entity = $this->getMySocket()->setCFSetting($entity, 'development_mode', 'off');
$entity = $this->getMySocket()->setCFSetting($entity, 'security_level', 'medium');
log_message("notice", "Zone:" . __FUNCTION__ . "=> 작업을 완료하였습니다.");
$zone_entitys[] = $entity;
}
//Record생성
foreach ($zone_entitys as $zone_entity) {
$record = new RecordSocket($this->_account_entity, $zone_entity);
foreach ($this->formDatas['hosts'] as $host) {
$result = $record->create($host, [
'type' => $this->formDatas['type'],
'content' => $this->formDatas['content'],
'proxied' => $this->formDatas['proxied'],
]);
$formDatas = $record->getArrayByResult($result);
$entity = $this->getRecordModel()->create($formDatas);
log_message("notice", "Record:" . __FUNCTION__ . "=> 작업을 완료하였습니다.");
}
}
}
public function create(): RedirectResponse
{
$this->fields = [$this->getModel()::PARENT, 'domains', 'hosts', 'type', 'content', 'proxied'];
$this->field_rules = $this->getModel()->getFieldRules(__FUNCTION__, $this->fields);
// $this->filter_fields = [$this->getModel()::PARENT, 'type', 'proxied'];
// $this->field_options = $this->getFormFieldOptions($this->filter_fields);
$this->create_validate();
$this->formDatas = $this->getFormDatas();
//부모데이터 정의
$this->_account_entity = $this->getAccountModel()->getEntityByPK($this->formDatas[$this->getModel()::PARENT]);
return $this->create_procedure();
}
// //수정
// protected function modify_process(): void
// {
// $this->entity = $this->getMySocket()->modify($this->entity, $this->formDatas);
// }
// public function modify(string $uid): RedirectResponse
// {
// $this->entity = $this->getModel()->getEntityByPK($uid);
// if ($this->entity === null) {
// throw new \Exception("해당 정보를 찾을수 없습니다.");
// }
// //부모데이터 정의
// $this->_account_entity = $this->getAccountModel()->getEntityByPK($this->entity->getParent());
// $this->fields = [$this->getModel()::PARENT, 'domains', 'hosts', 'type', 'content', 'proxied'];
// $this->field_rules = $this->getModel()->getFieldRules(__FUNCTION__, $this->fields);
// // $this->filter_fields = [$this->getModel()::PARENT, 'type', 'proxied'];
// // $this->field_options = $this->getFormFieldOptions($this->filter_fields);
// $this->modify_validate();
// $this->formDatas = $this->getFormDatas();
// return $this->create_procedure();
// }
// 리스트
public function index(): string
{
$this->fields = [$this->getModel()::PARENT, $this->getModel()::TITLE, 'name_servers', 'original_name_servers', 'plan', 'development_mode', 'ipv6', 'security_level', 'status', 'updated_at', 'created_at'];
$this->field_rules = $this->getModel()->getFieldRules(__FUNCTION__, $this->fields);
$this->filter_fields = [$this->getModel()::PARENT, 'development_mode', 'ipv6', 'security_level', 'status'];
$this->batchjob_fields = ['development_mode', 'ipv6', 'security_level'];
$this->field_options = $this->getFormFieldOptions($this->filter_fields);
return $this->list_procedure();
}
//reload
public function reload(string $account_uid): RedirectResponse
{
//부모데이터 정의
$this->_account_entity = $this->getAccountModel()->getEntityByPK($account_uid);
return $this->reload_procedure();
}
}