146 lines
5.8 KiB
PHP
146 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Admin\Cloudflare;
|
|
|
|
use App\Controllers\CloudflareController;
|
|
use App\Helpers\Cloudflare\FirewallHelper;
|
|
use App\Models\Cloudflare\FirewallModel;
|
|
use App\Models\Cloudflare\ZoneModel;
|
|
use App\Services\Cloudflare\Firewall;
|
|
use CodeIgniter\HTTP\DownloadResponse;
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class FirewallController 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 .= "Firewall";
|
|
$this->class_path .= $this->class_name;
|
|
$this->title = lang("{$this->class_path}.title");
|
|
$this->helper = new FirewallHelper();
|
|
}
|
|
final protected function getModel(): FirewallModel
|
|
{
|
|
if ($this->model === null) {
|
|
$this->model = new FirewallModel();
|
|
}
|
|
return $this->model;
|
|
}
|
|
final protected function getMyLibrary(): Firewall
|
|
{
|
|
if (!isset($this->_myLibrays[$this->_zone_entity->getPK()])) {
|
|
$this->_myLibrays[$this->_zone_entity->getPK()] = new Firewall($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;
|
|
}
|
|
//전송된 데이터
|
|
private function init(string $action, array $fields = []): void
|
|
{
|
|
$this->action = $action;
|
|
$this->fields = count($fields) ? $fields : [$this->getModel()::PARENT, 'mode', 'description', 'expression', 'enabled', 'updated_at', 'created_at'];
|
|
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
|
|
$this->filter_fields = [$this->getModel()::PARENT, 'mode', 'enabled'];
|
|
$this->field_options = $this->getFormFieldOptions($this->filter_fields);
|
|
$this->batchjob_fields = [];
|
|
}
|
|
//수정 (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} 정보를 찾을수 없습니다.");
|
|
}
|
|
//부모데이터정의
|
|
$this->_zone_entity = $this->getZoneModel()->getEntityByPK($this->entity->getParent());
|
|
//Socket처리
|
|
$this->entity = $this->getMyLibrary()->modify($this->entity, $this->formDatas);
|
|
}
|
|
//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 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->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);
|
|
}
|
|
//reload Firewall 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);
|
|
}
|
|
}
|