cfmgrv3/app/Controllers/Admin/Cloudflare/API/RecordController.php
2023-06-21 11:49:43 +09:00

214 lines
9.7 KiB
PHP

<?php
namespace App\Controllers\Admin\Cloudflare\API;
use App\Libraries\Log\Log;
use App\Models\Cloudflare\API\RecordModel;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Libraries\Cloudflare\API\Record;
class RecordController extends APIController
{
private $_zone_uids = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->_className .= '/Record';
$this->_model = new RecordModel();
$this->_defines = [
'insert' => [
'fields' => ['zone_uid', 'type', 'content', 'proxied', 'hosts'],
'fieldFilters' => [],
'fieldRules' => [
'zone_uid' => 'required|min_length[10]|max_length[200]',
'type' => 'required|in_list[A,CNAME,MX,SPF,TXT,NS,INFO]',
'content' => 'required|trim|min_length[4]',
'proxied' => 'required|string',
'hosts' => 'required|trim|string',
]
],
'index' => [
'fields' => ['zone_uid', 'host', 'type', 'content', 'ttl', 'proxied', 'locked', 'updated_at', 'created_at'],
'fieldFilters' => ['zone_uid', 'type', 'fixed', 'proxied', 'locked'],
'batchjobFilters' => ['content', 'proxied'],
],
'excel' => [
'fields' => ['zone_uid', 'host', 'type', 'content', 'ttl', 'proxied', 'locked', 'updated_at', 'created_at'],
'fieldFilters' => ['zone_uid', 'type', 'proxied', 'locked'],
],
];
helper($this->_className);
$this->_viewPath = strtolower($this->_className);
$this->_viewDatas['title'] = lang($this->_className . '.title');
$this->_viewDatas['className'] = $this->_className;
}
//Field별 Form Option용
protected function getFieldFormOption(string $field): array
{
switch ($field) {
case 'zone_uid':
if (is_null($this->_zone_uids)) {
//모든 필요한 FormOption등 조기화작업 필요
$this->_zone_uids = [DEFAULT_EMPTY => lang($this->_className . '.label.' . $field) . ' 선택'];
foreach ($this->getZoneModel()->orderBy('domain', 'asc')->findAll() as $zone) {
$this->_zone_uids[$zone['uid']] = $zone['domain'];
}
}
return $this->_zone_uids;
break;
default:
return parent::getFieldFormOption($field);
break;
}
}
//Insert관련
protected function insert_validate()
{
parent::insert_validate();
//Content 검증 Type이 A 인경우 IP형식 검사
if ($this->_viewDatas['fieldDatas']['type'] === 'A') {
if (!isIPAddress_CommonHelper($this->_viewDatas['fieldDatas']['content'], $this->_viewDatas['fieldDatas']['type'])) {
throw new \Exception("{$this->_viewDatas['title']}{$this->_viewDatas['fieldDatas']['type']}, {$this->_viewDatas['fieldDatas']['content']} 형식 오류[사설IP 않됨]");
}
}
//Host 검증
$this->_viewDatas['fieldDatas']['hosts'] = explode("\n", $this->_viewDatas['fieldDatas']['hosts']);
$cnt = 1;
foreach ($this->_viewDatas['fieldDatas']['hosts'] as $host) {
if (!isHost_CommonHelper($host)) {
throw new \Exception("{$this->_viewDatas['title']}{$cnt}번째 {$host} 호스트명 형식 오류");
}
if (!$this->_model->isUniqueHost($this->_viewDatas['fieldDatas'][$this->_model::PARENT_FIELD], $host, $this->_viewDatas['fieldDatas']['content'])) {
throw new \Exception("{$this->_viewDatas['title']}{$cnt}번째 {$host}:{$this->_viewDatas['fieldDatas']['content']}은 이미 등록된 호스트입니다.");
}
$cnt++;
}
}
protected function insert_process()
{
foreach ($this->_viewDatas['fieldDatas']['hosts'] as $host) {
$zone = $this->getZoneModel()->getEntity($this->_viewDatas['fieldDatas'][$this->_model::PARENT_FIELD]);
$this->insert_Host(
$zone,
$host,
$this->_viewDatas['fieldDatas']['type'],
$this->_viewDatas['fieldDatas']['content'],
$this->_viewDatas['fieldDatas']['proxied']
);
}
}
//Update관련
protected function update_process($entity)
{
$api = new Record($this->getZoneModel()->getEntity($entity->getParentFieldData()));
$entity = $api->update($entity, $this->_viewDatas['fieldDatas']);
return parent::update_process($entity);
}
//Toggle관련
protected function toggle_process($entity)
{
$api = new Record($this->getZoneModel()->getEntity($entity->getParentFieldData()));
$entity = $api->update($entity, $this->_viewDatas['fieldDatas']);
return parent::toggle_process($entity);
}
//Batchjob관련
protected function batchjob_process($entity)
{
$api = new Record($this->getZoneModel()->getEntity($entity->getParentFieldData()));
$entity = $api->update($entity, $this->_viewDatas['fieldDatas']);
return parent::batchjob_process($entity);
}
//Delete 관련
protected function delete_process($entity)
{
$api = new Record($this->getZoneModel()->getEntity($entity->getParentFieldData()));
$api->delete($entity);
return parent::delete_process($entity);
}
//Sync관련
protected function sync_process($entity)
{
$api = new Record($this->getZoneModel()->getEntity($entity->getParentFieldData()));
$entity = $api->sync($entity);
return parent::sync_process($entity);
}
//index 모델 전처리
private function index_setCondition_builder($builder)
{
foreach ($this->_viewDatas['fieldFilters'] as $field) {
$value = $this->request->getVar($field) ? $this->request->getVar($field) : false;
if ($value) {
$builder->where("cloudflarerecord.{$field}", $value);
}
}
$word = $this->request->getVar('word') ? $this->request->getVar('word') : '';
if (isset($word) && $word !== '') {
$builder->like('cloudflarerecord.host', $word, 'before'); //befor , after , both
$builder->orWhere('cloudflarerecord.content', $word);
}
$start = $this->request->getVar('start') ? $this->request->getVar('start') : '';
$end = $this->request->getVar('end') ? $this->request->getVar('end') : '';
if (isset($start) && $start !== '' && isset($end) && $end !== '') {
$builder->where('cloudflarerecord.created_at >=', $start);
$builder->where('cloudflarerecord.created_at <=', $end);
}
return $builder;
}
//Index관련
protected function index_getRows_builder(int $page = 0, int $per_page = 0): array
{
//Totalcount 처리
$builder = $this->_model->builder();
$builder->select("cloudflarerecord.*");
$builder->join("cloudflarezone", "cloudflarezone.uid = cloudflarerecord.zone_uid");
$builder = $this->index_setCondition_builder($builder);
// log_message("debug", __METHOD__ . "에서 호출\n" . $builder->getCompiledSelect(false));
$this->_viewDatas['total_count'] = $builder->countAllResults();
//Rows 처리
$builder->select("cloudflarerecord.*");
$builder->join("cloudflarezone", "cloudflarezone.uid = cloudflarerecord.zone_uid");
$builder = $this->index_setCondition_builder($builder);
//OrderBy
$order_field = $this->request->getVar('order_field') ? $this->request->getVar('order_field') : 'uid';
$order_value = $this->request->getVar('order_value') ? $this->request->getVar('order_value') : 'DESC';
$builder->orderBy("cloudflarezone.domain ASC, cloudflarerecord.host ASC, cloudflarerecord.{$order_field} {$order_value}");
//Limit
$builder->limit($per_page, $page * $per_page - $per_page);
// log_message("debug", __METHOD__ . "에서 호출\n" . $builder->getCompiledSelect(false));
return $builder->get()->getResultArray();
}
//CDN고정관련
final public function cdnToggle(string $uid)
{
try {
$entity = $this->_model->getEntity($uid);
$fixedRecordModel = new \App\Models\Cloudflare\API\FixedRecordModel();
if ($entity->fixed == 'on') {
$entity->fixed = "off";
$this->_model->save($entity);
$fixedRecordModel->where('host', $entity->host)->delete();
Log::add("info", "{$entity->getTitle()}의 fixed : on=>off");
} else {
$entity->fixed = "on";
$this->_model->save($entity);
//throw new \Exception($entity);
$fixedRecordModel->insert(['host' => $entity->host]);
Log::add("info", "{$entity->getTitle()}의 fixed : off=>on");
}
$message = "{$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다.";
Log::save("{$this->_viewDatas['title']} {$message}");
return alert_CommonHelper($message, session()->get(RETURN_URL));
} catch (\Exception $e) {
$message = "{$entity->getTitle()} " . __FUNCTION__ . " 실패하였습니다.";
Log::add("warning", $message . "<br>\n{$e->getMessage()}");
Log::save("{$this->_viewDatas['title']} {$message}", false);
return alert_CommonHelper($message, 'back');
}
}
}