293 lines
12 KiB
PHP
293 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Admin\Cloudflare;
|
|
|
|
use App\Helpers\Cloudflare\RecordHelper;
|
|
use App\Libraries\Cloudflare\Record;
|
|
use App\Models\Cloudflare\RecordModel;
|
|
use App\Models\Cloudflare\ZoneModel;
|
|
use CodeIgniter\HTTP\DownloadResponse;
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class RecordController extends CloudflareController
|
|
{
|
|
private $_zone_entity = null;
|
|
private $_myLibrays = [];
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
parent::initController($request, $response, $logger);
|
|
$this->class_name .= "Record";
|
|
$this->class_path .= $this->class_name;
|
|
$this->title = lang("{$this->class_path}.title");
|
|
$this->helper = new RecordHelper();
|
|
}
|
|
final protected function getModel(): RecordModel
|
|
{
|
|
if ($this->_model === null) {
|
|
$this->_model = new RecordModel();
|
|
}
|
|
return $this->_model;
|
|
}
|
|
final protected function getMyLibrary(): Record
|
|
{
|
|
if (!isset($this->_myLibrays[$this->_zone_entity->getPK()])) {
|
|
$this->_myLibrays[$this->_zone_entity->getPK()] = new Record($this->_zone_entity);
|
|
}
|
|
return $this->_myLibrays[$this->_zone_entity->getPK()];
|
|
}
|
|
protected function getFormFieldOption(string $field, array $options = []): array
|
|
{
|
|
switch ($field) {
|
|
case $this->getModel()::PARENT:
|
|
// $this->getZoneModel()->where('status', 'active');
|
|
$options[$field] = $this->getZoneModel()->getFormFieldOption($field);
|
|
// 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 '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;
|
|
}
|
|
private function init(string $action): void
|
|
{
|
|
$this->action = $action;
|
|
$this->fields = [$this->getModel()::PARENT, '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);
|
|
}
|
|
//생성
|
|
public function create_form(): RedirectResponse|string
|
|
{
|
|
$this->init('create');
|
|
$parent_field = $this->getModel()::PARENT;
|
|
$this->$parent_field = $this->request->getVar($parent_field) ?: DEFAULTS["EMPTY"];
|
|
return $this->create_form_procedure();
|
|
}
|
|
protected function create_validate(string $action, array $fields): void
|
|
{
|
|
//hosts를 제외한 fields Valid처리
|
|
parent::create_validate($action, array_diff($fields, ['hosts']));
|
|
}
|
|
private function setEntitys(array $entitys): void
|
|
{
|
|
$this->entitys = $entitys;
|
|
}
|
|
protected function create_process(): void
|
|
{
|
|
//DB작업도 Socket에서 다 처리하므로 parent::create_process()하면 않됨
|
|
$this->create_validate($this->action, $this->fields);
|
|
$this->formDatas = $this->getFormDatas();
|
|
//부모데이터정의
|
|
$this->_zone_entity = $this->getZoneModel()->getEntityByPK($this->formDatas[$this->getModel()::PARENT]);
|
|
//데이터 검증
|
|
$cnt = 1;
|
|
foreach ($this->formDatas['hosts'] as $host) {
|
|
//호스트명 형식확인
|
|
if (!$this->helper->isHost($host)) {
|
|
throw new \Exception("{$host} 호스트명 형식 오류");
|
|
}
|
|
$cnt++;
|
|
}
|
|
//Socket처리
|
|
$entitys = [];
|
|
foreach ($this->formDatas['hosts'] as $host) {
|
|
$entity = $this->getMyLibrary()->create(
|
|
$host,
|
|
$this->formDatas['type'],
|
|
$this->formDatas['content'],
|
|
$this->formDatas['proxied']
|
|
);
|
|
log_message("debug", message: "Record:{$entity->getTitle()} 생성 작업을 완료하였습니다.");
|
|
$entitys[] = $entity;
|
|
}
|
|
$this->setEntitys($entitys); // 새로운 메서드를 사용하여 entitys 설정
|
|
}
|
|
protected function create_process_result(): RedirectResponse|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($this->action, $this->fields);
|
|
$this->filter_fields = [$this->getModel()::PARENT, 'development_mode', 'ipv6', 'security_level'];
|
|
$this->field_options = $this->getFormFieldOptions($this->filter_fields);
|
|
return $this->view_process_result();
|
|
}
|
|
public function create(): RedirectResponse|string
|
|
{
|
|
$this->init(__FUNCTION__);
|
|
return $this->create_procedure();
|
|
}
|
|
//수정 (modify,toggle,batchjob사용)
|
|
protected function modify_process(mixed $uid): void
|
|
{
|
|
//DB작업도 Socket에서 다 처리하므로 parent::modify_process($uid)하면 않됨
|
|
$this->modify_validate($this->action, $this->fields);
|
|
$this->formDatas = $this->getFormDatas();
|
|
//자신정보정의
|
|
$this->entity = $this->getModel()->getEntityByPK($uid);
|
|
if ($this->entity === null) {
|
|
throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
|
|
}
|
|
//fixed 필드가 있고 값이 변경되었을때
|
|
if (in_array('fixed', $this->fields)) {
|
|
// dd($this->formDatas, $this->entity->fixed);
|
|
if (isset($this->formDatas['fixed']) && $this->formDatas['fixed'] !== $this->entity->fixed) {
|
|
$this->entity = $this->getModel()->modify($this->entity, $this->formDatas);
|
|
}
|
|
} else {
|
|
//부모데이터정의
|
|
$this->_zone_entity = $this->getZoneModel()->getEntityByPK($this->entity->getParent());
|
|
//Socket처리
|
|
$this->entity = $this->getMyLibrary()->modify($this->entity, $this->formDatas);
|
|
}
|
|
}
|
|
//일괄처리작업
|
|
public function batcjob(): RedirectResponse
|
|
{
|
|
$this->action = __FUNCTION__;
|
|
$this->fields = ['proxied'];
|
|
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
|
|
return $this->batcjob_procedure();
|
|
}
|
|
//View
|
|
protected function view_process(mixed $uid): void
|
|
{
|
|
//해당 Zone의 모든 Record를 가져오기
|
|
$this->getModel()->where($this->getModel()::PARENT, $uid);
|
|
$this->entitys = $this->getModel()->getEntitys();
|
|
}
|
|
protected function view_process_result(): string
|
|
{
|
|
helper(['form']);
|
|
$this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []];
|
|
return view(
|
|
strtolower($this->view_path . $this->class_path . "/view"),
|
|
data: ['viewDatas' => $this->getViewDatas()]
|
|
);
|
|
}
|
|
public function view(string $uid): RedirectResponse|string
|
|
{
|
|
$this->action = __FUNCTION__;
|
|
$this->fields = [$this->getModel()::PARENT, $this->getModel()::TITLE, 'type', 'content', 'ttl', 'proxied', 'locked', 'updated_at', 'created_at'];
|
|
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
|
|
$this->filter_fields = [$this->getModel()::PARENT, 'type', 'proxied', 'fixed'];
|
|
$this->field_options = $this->getFormFieldOptions($this->filter_fields);
|
|
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} 정보를 찾을수 없습니다.");
|
|
}
|
|
//부모데이터정의
|
|
$this->_zone_entity = $this->getZoneModel()->getEntityByPK($this->entity->getParent());
|
|
//Cloudflare 삭제
|
|
$this->entity = $this->getMyLibrary()->delete($this->entity);
|
|
}
|
|
// 리스트
|
|
protected function list_entitys_process(): array
|
|
{
|
|
$this->list_condition_process();
|
|
//기본Soring처리
|
|
$this->getModel()->orderBy(ZoneModel::TABLE . "." . ZoneModel::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(ZoneModel::TABLE, sprintf(
|
|
"%s.%s=%s.%s",
|
|
$this->getModel()::TABLE,
|
|
$this->getModel()::PARENT,
|
|
ZoneModel::TABLE,
|
|
ZoneModel::PK
|
|
));
|
|
$entitys = $this->getModel()->select($this->getModel()::TABLE . '.*')->findAll();
|
|
log_message("debug", $this->getModel()->getLastQuery());
|
|
return $entitys;
|
|
}
|
|
public function index(): string
|
|
{
|
|
$this->action = __FUNCTION__;
|
|
$this->fields = [$this->getModel()::PARENT, $this->getModel()::TITLE, 'type', 'content', 'ttl', 'proxied', 'locked', 'updated_at', 'created_at'];
|
|
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
|
|
$this->filter_fields = [$this->getModel()::PARENT, 'type', 'proxied', 'fixed'];
|
|
$this->field_options = $this->getFormFieldOptions($this->filter_fields);
|
|
$this->batchjob_fields = ['content', 'proxied'];
|
|
return $this->list_procedure();
|
|
}
|
|
// Download
|
|
public function download(string $output_type, mixed $uid = false): DownloadResponse|string
|
|
{
|
|
$this->action = __FUNCTION__;
|
|
$this->fields = [$this->getModel()::PARENT, $this->getModel()::TITLE, 'type', 'content', 'ttl', 'proxied', 'locked', 'updated_at', 'created_at'];
|
|
$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);
|
|
$this->batchjob_fields = ['proxied'];
|
|
return $this->download_procedure($output_type, $uid);
|
|
}
|
|
//Sync작업
|
|
protected function sync_process(string $uid): void
|
|
{
|
|
//자신정보정의
|
|
$this->entity = $this->getModel()->getEntityByPK($uid);
|
|
if ($this->entity === null) {
|
|
throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
|
|
}
|
|
//부모데이터정의
|
|
$this->_zone_entity = $this->getZoneModel()->getEntityByPK($this->entity->getParent());
|
|
//Socket처리
|
|
$this->entity = $this->getMyLibrary()->sync($this->entity);
|
|
}
|
|
public function sync(string $uid): RedirectResponse
|
|
{
|
|
return $this->sync_procedure($uid);
|
|
}
|
|
//reload Record By Zone
|
|
protected function reload_process(mixed $uid): void
|
|
{
|
|
$this->_zone_entity = $this->getZoneModel()->getEntityByPK($uid);
|
|
if ($this->_zone_entity === null) {
|
|
throw new \Exception("Zone: {$uid} 정보를 찾을수 없습니다.");
|
|
}
|
|
$this->getMyLibrary()->reload();
|
|
}
|
|
public function reload(string $uid): RedirectResponse
|
|
{
|
|
return $this->reload_procedure($uid);
|
|
}
|
|
}
|