cfmgrv4/app/Controllers/Admin/Cloudflare/RecordController.php
2025-03-14 16:43:48 +09:00

189 lines
7.7 KiB
PHP

<?php
namespace App\Controllers\Admin\Cloudflare;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Entities\Cloudflare\ZoneEntity;
use App\Helpers\Cloudflare\RecordHelper;
use App\Models\Cloudflare\ZoneModel;
use App\Services\Cloudflare\RecordService;
use CodeIgniter\HTTP\RedirectResponse;
use App\Services\MyLogService;
class RecordController extends CloudflareController
{
private $_zone_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 RecordHelper();
}
protected function getService(): RecordService
{
if ($this->service === null) {
$this->service = new RecordService();
$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->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;
}
//부모데이터 정의
private function getParentEntity($uid): ZoneEntity
{
//부모데이터정의
$zone_entity = $this->getZoneModel()->getEntityByPK($uid);
if ($zone_entity === null) {
throw new \Exception("Zone: [{$this->entity->getParent()}] 정보를 찾을수 없습니다.");
}
return $zone_entity;
}
//생성
// protected function create_process_result($message): RedirectResponse|string
// {
// $this->init(__FUNCTION__);
// helper(['form']);
// $this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []];
// return view(
// strtolower($this->view_path . $this->getService()->getClassPath() . "/create_result"),
// data: ['viewDatas' => $this->getViewDatas()]
// );
// }
protected function create_process_result($message): RedirectResponse|string
{
MyLogService::save($this->getService(), __FUNCTION__, $this->myauth, $message, DEFAULTS['STATUS']);
return redirect()->to($this->myauth->popPreviousUrl())->with('error', $message);
}
protected function create_init(string $action, $fields = []): void
{
$fields = [
'fields' => [$this->getService()->getModel()::PARENT, 'hosts', 'type', 'content', '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);
//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 않됨]");
}
}
//호스트명 형식확인
$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->_zone_entity = $this->getParentEntity($this->formDatas[$this->getService()->getModel()::PARENT]);
$entitys = [];
foreach ($hosts as $host) {
$entity = $this->getService()->create(
$this->_zone_entity,
$host,
$this->formDatas['type'],
$this->formDatas['content'],
$this->formDatas['proxied']
);
$entitys[] = $entity;
}
$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->_zone_entity = $this->getParentEntity($this->entity->getParent());
//Socket처리
$this->entity = $this->getService()->modify($this->_zone_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} 정보를 찾을수 없습니다.");
}
//부모데이터정의
$this->_zone_entity = $this->getParentEntity($this->entity->getParent());
//Cloudflare 삭제
$this->entity = $this->getService()->delete($this->_zone_entity, $this->entity);
}
//Sync작업
protected function sync_process(string $uid): void
{
//자신정보정의
$this->entity = $this->getService()->getModel()->getEntityByPK($uid);
if ($this->entity === null) {
throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
}
//부모데이터정의
$this->_zone_entity = $this->getParentEntity($this->entity->getParent());
//Socket처리
$this->entity = $this->getService()->sync($this->_zone_entity, $this->entity);
}
//reload Record By Zone
protected function reload_process(mixed $zone_uid): void
{
//부모데이터정의
$this->_zone_entity = $this->getParentEntity($zone_uid);
$this->getService()->reload($this->_zone_entity);
}
//리스트
protected function index_entitys_process(): array
{
//기본Soring처리
$this->getService()->getModel()->orderBy(ZoneModel::TABLE . "." . ZoneModel::TITLE . " ASC ," . $this->getService()->getModel()::TITLE . " ASC");
//Join을 해서 도메인부터 Sorting하기위함
$this->getService()->getModel()->join(ZoneModel::TABLE, sprintf(
"%s.%s=%s.%s",
$this->getService()->getModel()::TABLE,
$this->getService()->getModel()::PARENT,
ZoneModel::TABLE,
ZoneModel::PK
));
return parent::index_entitys_process();
}
}