diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 060bbfa..119cf5b 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -22,7 +22,7 @@ $routes->group('admin', ['namespace' => 'App\Controllers\Admin'], function ($rou $routes->get('modify/(:num)', 'UserController::modify_form/$1'); $routes->post('modify/(:num)', 'UserController::modify/$1'); $routes->get('delete/(:num)', 'UserController::delete/$1'); - $routes->get('toggle/(:num)/(:alpha)', 'UserController::toggle/$1/$2'); + $routes->get('toggle/(:num)/(:any)', 'UserController::toggle/$1/$2'); $routes->post('batchjob', 'UserController::batcjob'); $routes->get('download/(:alpha)', 'UserController::download/$1'); }); @@ -33,7 +33,7 @@ $routes->group('admin', ['namespace' => 'App\Controllers\Admin'], function ($rou $routes->get('modify/(:num)', 'MapurlController::modify_form/$1'); $routes->post('modify/(:num)', 'MapurlController::modify/$1'); $routes->get('delete/(:num)', 'MapurlController::delete/$1'); - $routes->get('toggle/(:num)/(:alpha)', 'MapurlController::toggle/$1/$2'); + $routes->get('toggle/(:num)/(:any)', 'MapurlController::toggle/$1/$2'); $routes->post('batchjob', 'MapurlController::batcjob'); $routes->get('download/(:alpha)', 'MapurlController::download/$1'); }); @@ -44,7 +44,7 @@ $routes->group('admin', ['namespace' => 'App\Controllers\Admin'], function ($rou $routes->post('create', 'AuthController::create'); $routes->get('modify/(:num)', 'AuthController::modify_form/$1'); $routes->get('delete/(:num)', 'AuthController::delete/$1'); - $routes->get('toggle/(:num)/(:alpha)', 'AuthController::toggle/$1/$2'); + $routes->get('toggle/(:num)/(:any)', 'AuthController::toggle/$1/$2'); $routes->post('batchjob', 'AuthController::batcjob'); $routes->get('reload/(:num)', 'AuthController::reload/$1'); $routes->get('reload', 'AuthController::reload'); @@ -66,7 +66,8 @@ $routes->group('admin', ['namespace' => 'App\Controllers\Admin'], function ($rou $routes->get('create', 'ZoneController::create_form'); $routes->post('create', 'ZoneController::create'); $routes->get('delete/(:alphanum)', 'ZoneController::delete/$1'); - $routes->get('toggle/(:alphanum)/(:alpha)', 'ZoneController::toggle/$1/$2'); + $routes->get('sync/(:alphanum)', 'ZoneController::sync/$1'); + $routes->get('toggle/(:alphanum)/(:any)', 'ZoneController::toggle/$1/$2'); $routes->post('batchjob', 'ZoneController::batcjob'); $routes->get('reload/(:alphanum)', 'ZoneController::reload/$1'); $routes->get('reload', 'ZoneController::reload'); @@ -79,7 +80,8 @@ $routes->group('admin', ['namespace' => 'App\Controllers\Admin'], function ($rou $routes->get('create', 'RecordController::create_form'); $routes->post('create)', 'RecordController::create'); $routes->get('delete/(:alphanum)', 'RecordController::delete/$1'); - $routes->get('toggle/(:alphanum)/(:alpha)', 'RecordController::toggle/$1/$2'); + $routes->get('sync/(:alphanum)', 'RecordController::sync/$1'); + $routes->get('toggle/(:alphanum)/(:any)', 'RecordController::toggle/$1/$2'); $routes->post('batchjob', 'RecordController::batcjob'); $routes->get('download/(:alpha)', 'RecordController::download/$1'); }); diff --git a/app/Controllers/Admin/Cloudflare/CloudflareController.php b/app/Controllers/Admin/Cloudflare/CloudflareController.php index ba654e7..ca3e1b1 100644 --- a/app/Controllers/Admin/Cloudflare/CloudflareController.php +++ b/app/Controllers/Admin/Cloudflare/CloudflareController.php @@ -51,6 +51,26 @@ abstract class CloudflareController extends AdminController } return $this->_recordModel; } + protected function sync_process(string $uid): void {} + final protected function sync_procedure(string $uid): RedirectResponse + { + //Transaction Start + $this->getModel()->transStart(); + try { + $this->sync_process($uid); + $message = "Sync 작업을 완료하였습니다."; + log_message("notice", $message); + $this->session->setFlashdata(SESSION_NAMES['RETURN_MSG'], $message); + $this->getModel()->transCommit(); + return redirect()->to($this->session->getFlashdata(SESSION_NAMES['RETURN_URL']) ?: "/"); + } catch (\Exception $e) { + //Transaction Rollback + $this->getModel()->transRollback(); + log_message("error", $e->getMessage()); + $this->session->setFlashdata(SESSION_NAMES['RETURN_MSG'], __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage()); + return redirect()->to($this->session->getFlashdata(SESSION_NAMES['RETURN_URL']) ?: "/"); + } + } protected function reload_process(mixed $uid = false): void {} final protected function reload_procedure(mixed $uid = false): RedirectResponse diff --git a/app/Controllers/Admin/Cloudflare/RecordController.php b/app/Controllers/Admin/Cloudflare/RecordController.php index facc6e7..44fcb33 100644 --- a/app/Controllers/Admin/Cloudflare/RecordController.php +++ b/app/Controllers/Admin/Cloudflare/RecordController.php @@ -139,12 +139,29 @@ class RecordController extends CloudflareController $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields); return $this->batcjob_procedure(); } + //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->getMySocket()->sync($this->entity); + } + public function sync(string $uid): RedirectResponse + { + return $this->sync_procedure($uid); + } //삭제 protected function delete_process(string $uid): void { //DB작업도 Socket에서 다 처리하므로 parent::delete_process($uid)하면 않됨 //자신정보정의 - $this->entity = $this->getModel()->getEntityByPK(intval($uid)); + $this->entity = $this->getModel()->getEntityByPK($uid); if ($this->entity === null) { throw new \Exception("{$uid} 정보를 찾을수 없습니다."); } diff --git a/app/Controllers/Admin/Cloudflare/ZoneController.php b/app/Controllers/Admin/Cloudflare/ZoneController.php index b08d881..def24c7 100644 --- a/app/Controllers/Admin/Cloudflare/ZoneController.php +++ b/app/Controllers/Admin/Cloudflare/ZoneController.php @@ -165,7 +165,7 @@ class ZoneController extends CloudflareController $this->modify_validate($this->action, $this->fields); $this->formDatas = $this->getFormDatas(); //자신정보정의 - $this->entity = $this->getModel()->getEntityByPK(intval($uid)); + $this->entity = $this->getModel()->getEntityByPK($uid); if ($this->entity === null) { throw new \Exception("{$uid} 정보를 찾을수 없습니다."); } @@ -173,7 +173,7 @@ class ZoneController extends CloudflareController $this->_account_entity = $this->getAccountModel()->getEntityByPK($this->entity->getParent()); //Socket처리 $entity = $this->getMySocket()->modify($this->entity, $this->formDatas); - log_message("debug", "Record:{$entity->getTitle()} 수정 작업을 완료하였습니다."); + log_message("debug", "Zone:{$entity->getTitle()} 수정 작업을 완료하였습니다."); } //일괄처리작업 public function batcjob(): RedirectResponse @@ -183,12 +183,29 @@ class ZoneController extends CloudflareController $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields); return $this->batcjob_procedure(); } + //Sync작업 + protected function sync_process(mixed $uid): void + { + //자신정보정의 + $this->entity = $this->getModel()->getEntityByPK($uid); + if ($this->entity === null) { + throw new \Exception("{$uid} 정보를 찾을수 없습니다."); + } + //부모데이터정의 + $this->_account_entity = $this->getAccountModel()->getEntityByPK($this->entity->getParent()); + //Socket처리 + $this->getMySocket()->sync($this->entity); + } + public function sync(string $uid): RedirectResponse + { + return $this->sync_procedure($uid); + } //삭제 protected function delete_process(string $uid): void { //DB작업도 Socket에서 다 처리하므로 parent::delete_process($uid)하면 않됨 //자신정보정의 - $this->entity = $this->getModel()->getEntityByPK(intval($uid)); + $this->entity = $this->getModel()->getEntityByPK($uid); if ($this->entity === null) { throw new \Exception("{$uid} 정보를 찾을수 없습니다."); } diff --git a/app/Helpers/Admin/Cloudflare/Account_helper.php b/app/Helpers/Admin/Cloudflare/Account_helper.php index a548313..adf51b1 100644 --- a/app/Helpers/Admin/Cloudflare/Account_helper.php +++ b/app/Helpers/Admin/Cloudflare/Account_helper.php @@ -174,7 +174,7 @@ function getListButton_AccountHelper(string $action, array $viewDatas, array $ex $extras = ["class" => "btn btn-outline btn-primary btn-circle", "target" => "_self", ...$extras]; $action = getListButtonLabel_AccountHelper($action, '입력', $viewDatas, $extras); break; - case 'modify': + case 'sync': // $checkbox = form_checkbox([ // "id" => "checkbox_uid_{$viewDatas['entity']->getPK()}", // "name" => "batchjob_uids[]", @@ -186,9 +186,10 @@ function getListButton_AccountHelper(string $action, array $viewDatas, array $ex $action = $viewDatas['cnt']; break; case 'delete': - $viewDatas['list_action_url'] = current_url() . '/' . $action . '/' . $viewDatas['entity']->getPK(); - $extras = ["class" => "btn btn-sm btn-danger btn-circle", "target" => "_self", ...$extras]; - $action = anchor($viewDatas['list_action_url'], ICONS['DELETE'], $extras); + // $viewDatas['list_action_url'] = current_url() . '/' . $action . '/' . $viewDatas['entity']->getPK(); + // $extras = ["class" => "btn btn-sm btn-danger btn-circle", "target" => "_self", ...$extras]; + // $action = anchor($viewDatas['list_action_url'], ICONS['DELETE'], $extras); + $action = ""; break; } return $action; diff --git a/app/Libraries/MySocket/Cloudflare/RecordSocket.php b/app/Libraries/MySocket/Cloudflare/RecordSocket.php index 6cad3d3..3882296 100644 --- a/app/Libraries/MySocket/Cloudflare/RecordSocket.php +++ b/app/Libraries/MySocket/Cloudflare/RecordSocket.php @@ -117,27 +117,32 @@ class RecordSocket extends CloudflareSocket } public function sync(RecordEntity $entity): void { - //기존 Sync형태 - // $cf = $this->getRequest('get',"zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}"); - // $cf = json_decode($cf->getBody()); - // if (!$cf->success) { - // throw new \Exception(__FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); - // } - // return new RecordEntity($this->getArrayByResult($cf->result)); + // 기존 Sync형태 + $cf = $this->getRequest('get', "zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}"); + $cf = json_decode($cf->getBody()); + if (!$cf->success) { + $message = "Record:" . __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true); + log_message("error", $message); + throw new \Exception($message); + } + // DB수정 + log_message("debug", var_export($cf->result, true)); + $entity = $this->getModel()->modify($entity, $this->getArrayByResult($cf->result)); //Async형태 - $promise = $this->getRequest('getAsync', "zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}"); - $promise->then( - onFulfilled: function ($response) use ($entity): RecordEntity { - $record = json_decode($response->getBody(), true)['result']; - //DB수정 - return $this->getModel()->modify($entity, $this->getArrayByResult($record)); - }, - onRejected: function ($error) { - log_message('error', 'Failed to fetch DNS records: ' . $error->getMessage()); - // throw new \Exception('Failed to fetch DNS records: ' . $error->getMessage()); - } - ); - $promise->wait(); + // $promise = $this->getRequest('getAsync', "zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}"); + // $promise->then( + // onFulfilled: function ($response) use ($entity): RecordEntity { + // $record = json_decode($response->getBody(), true)['result']; + // log_message("debug", var_export($record)); + // //DB수정 + // return $this->getModel()->modify($entity, $this->getArrayByResult($record)); + // }, + // onRejected: function ($error) { + // log_message('error', 'Failed to fetch DNS records: ' . $error->getMessage()); + // // throw new \Exception('Failed to fetch DNS records: ' . $error->getMessage()); + // } + // ); + // $promise->wait(); } //Reload public function reload(): void diff --git a/app/Libraries/MySocket/Cloudflare/ZoneSocket.php b/app/Libraries/MySocket/Cloudflare/ZoneSocket.php index 60917bc..1acd627 100644 --- a/app/Libraries/MySocket/Cloudflare/ZoneSocket.php +++ b/app/Libraries/MySocket/Cloudflare/ZoneSocket.php @@ -11,6 +11,7 @@ class ZoneSocket extends CloudflareSocket { private $_model = null; private $_account_entity = null; + private $_setting_fields = ['development_mode' => 'off', 'ipv6' => 'off', 'security_level' => 'medium']; public function __construct(AccountEntity $account_entity) { $this->_account_entity = $account_entity; @@ -57,31 +58,24 @@ class ZoneSocket extends CloudflareSocket return $formDatas; } //Cfzone에서 가져온 값을 zone에 setting - protected function getCFSetting(ZoneEntity $entity): ZoneEntity + private function getCFSetting(ZoneEntity $entity): array { - $cf = $this->getRequest('patch', 'zones/' . $entity->getPK() . '/settings/'); + $cf = $this->getRequest('get', 'zones/' . $entity->getPK() . '/settings'); $cf = json_decode($cf->getBody()); if (!$cf->success) { $message = "Zone:" . __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true); log_message("error", $message); throw new \Exception($message); } + $formDatas = []; foreach ($cf->result as $cf) { - switch ($cf->id) { - case 'development_mode': - $entity->development_mode = $cf->value; - break; - case 'ipv6': - $entity->ipv6 = $cf->value; - break; - case 'security_level': - $entity->security_level = $cf->value; - break; + if (in_array($cf->id, array_keys($this->_setting_fields))) { + $formDatas[$cf->id] = $cf->value; } } - return $entity; + return $formDatas; } - protected function setCFSetting(ZoneEntity $entity, string $field, string $value): ZoneEntity + private function setCFSetting(ZoneEntity $entity, string $field, string $value): string { $datas = ['value' => $value]; $cf = $this->getRequest('patch', 'zones/' . $entity->getPK() . '/settings/' . $field, $datas); @@ -116,16 +110,16 @@ class ZoneSocket extends CloudflareSocket //초기화값 추가셋팅 ipv6 , development_mode , security_level $formDatas = []; - $formDatas['ipv6'] = $this->setCFSetting($entity, 'ipv6', 'off'); - $formDatas['development_mode'] = $this->setCFSetting($entity, 'development_mode', 'off'); - $formDatas['security_level'] = $this->setCFSetting($entity, 'security_level', 'medium'); + foreach ($this->_setting_fields as $field => $default) { + $formDatas[$field] = $this->setCFSetting($entity, $field, $default); + } //DB수정 return $this->getModel()->modify($entity, $formDatas); } public function modify(ZoneEntity $entity, array $formDatas): ZoneEntity { + // throw new \Exception("zone:modify" . var_export($formDatas, true)); //modify,toggle,batchjob 사용 셋팅 ipv6 , //development_mode , //security_level - $formDatas = []; foreach ($formDatas as $field => $value) { $formDatas[$field] = $this->setCFSetting($entity, $field, $value); } @@ -147,28 +141,18 @@ class ZoneSocket extends CloudflareSocket } public function sync(ZoneEntity $entity): void { - //기존 Sync형태 - // $cf = $this->getRequest('get',"zones/{$entity->getPK()}"); - // $cf = json_decode($cf->getBody()); - // if (!$cf->success) { - // throw new \Exception("Zone:" . __FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); - // } - // log_message("notice", "Zone:" . __FUNCTION__ . "=> 작업을 완료하였습니다."); - // return new ZoneEntity($this->getArrayByResult($cf->result)); - //Async형태 - $promise = $this->getRequest('getAsync', "zones/{$entity->getPK()}"); - $promise->then( - onFulfilled: function ($response) use ($entity): ZoneEntity { - $record = json_decode($response->getBody(), true)['result']; - //DB수정 - return $this->getModel()->modify($entity, $this->getArrayByResult($record)); - }, - onRejected: function ($error) { - log_message('error', 'Failed to fetch DNS records: ' . $error->getMessage()); - // throw new \Exception('Failed to fetch DNS records: ' . $error->getMessage()); - } - ); - $promise->wait(); + $cf = $this->getRequest('get', "zones/{$entity->getPK()}"); + $cf = json_decode($cf->getBody()); + if (!$cf->success) { + $message = "Zone:" . __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true); + log_message("error", $message); + throw new \Exception($message); + } + $formDatas = $this->getArrayByResult($cf->result); + $entity = $this->getModel()->modify($this->getModel()->getEntity(), $formDatas); + //추가 셋팅용 + $formDatas = $this->getCFSetting($entity); + $this->getModel()->modify($entity, $formDatas); } //Reload public function reload(): void @@ -180,6 +164,9 @@ class ZoneSocket extends CloudflareSocket foreach ($results as $result) { $formDatas = $this->getArrayByResult($result); $entity = $this->getModel()->modify($this->getModel()->getEntity(), $formDatas); + //추가 셋팅용 + $formDatas = $this->getCFSetting($entity); + $entity = $this->getModel()->modify($entity, $formDatas); $entity_uids[] = $entity->getPK(); } //부모키를 기준으로 CF에 존재하지 않는 데이터 삭제용 diff --git a/app/Libraries/MySocket/CloudflareSocket.php b/app/Libraries/MySocket/CloudflareSocket.php index 8760dfd..60a6740 100644 --- a/app/Libraries/MySocket/CloudflareSocket.php +++ b/app/Libraries/MySocket/CloudflareSocket.php @@ -63,12 +63,12 @@ abstract class CloudflareSocket extends CommonLibrary } final protected function getRequest(string $method, string $uri, array $datas = []) { - if (!in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) { + if (!in_array($method, ['get', 'getAsync', 'post', 'put', 'patch', 'delete'])) { throw new \InvalidArgumentException('Request method must be get, post, put, patch, or delete'); } try { $response = $this->getClient()->$method($uri, [ - $method == 'get' ? 'query' : 'json' => $datas, + in_array($method, ['get', 'getAsync']) ? 'query' : 'json' => $datas, ]); if (self::$_request >= self::$_request_max) { log_message('warning', sprintf("--Cloudflare API Call %s초 대기 시작--", self::$_request_timewait)); diff --git a/app/Models/CommonModel.php b/app/Models/CommonModel.php index 14830c5..dab42cc 100644 --- a/app/Models/CommonModel.php +++ b/app/Models/CommonModel.php @@ -182,7 +182,7 @@ abstract class CommonModel extends Model var_export($this->errors(), true) )); } - log_message("notice", __FUNCTION__ . " 저장이 완료되었습니다."); + log_message("notice", $this->getTable() . " => " . __FUNCTION__ . " DB 저장이 완료되었습니다."); } else { log_message("notice", __FUNCTION__ . " 변경된 내용이 없습니다."); } @@ -202,7 +202,7 @@ abstract class CommonModel extends Model $pkField = $this->getPKField(); $entity->$pkField = $this->getInsertID(); } - log_message("debug", $this->getTable() . " => " . __FUNCTION__ . " 작업 완료"); + log_message("debug", $this->getTable() . " => " . __FUNCTION__ . " DB 작업 완료"); return $entity; } final protected function modify_process($entity, array $formDatas): mixed @@ -214,7 +214,7 @@ abstract class CommonModel extends Model $entity->$field = $this->convertEntityData($field, $formDatas); } $this->save_process($entity); - log_message("debug", $this->getTable() . " => " . __FUNCTION__ . " 작업 완료"); + log_message("debug", $this->getTable() . " => " . __FUNCTION__ . " DB 작업 완료"); return $entity; } diff --git a/app/Views/admin/cloudflare/account/index.php b/app/Views/admin/cloudflare/account/index.php index 4e38b93..da04a29 100644 --- a/app/Views/admin/cloudflare/account/index.php +++ b/app/Views/admin/cloudflare/account/index.php @@ -23,7 +23,7 @@ - "cursor:pointer"]) ?> + "cursor:pointer"]) ?>