From 42f638e0194c54e8c19d084fc3c8f4dd63e77ccf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Fri, 4 Oct 2024 19:15:57 +0900 Subject: [PATCH] cfmgrv4 init...1 --- app/Config/Constants.php | 27 ++++---- app/Config/Routes.php | 21 ++---- .../Admin/Cloudflare/RecordController.php | 39 ++++++----- .../Admin/Cloudflare/ZoneController.php | 56 +++++++++------- app/Controllers/MVController.php | 9 +-- .../Admin/Cloudflare/Account_helper.php | 4 +- app/Helpers/Admin/Cloudflare/Auth_helper.php | 2 +- .../Admin/Cloudflare/Record_helper.php | 35 +++++----- app/Helpers/Admin/Cloudflare/Zone_helper.php | 4 +- app/Helpers/Admin/Mapurl_helper.php | 2 +- app/Helpers/Admin/User_helper.php | 2 +- .../MySocket/Cloudflare/RecordSocket.php | 43 +++++++----- .../MySocket/Cloudflare/ZoneSocket.php | 32 +++++---- app/Libraries/MySocket/CloudflareSocket.php | 62 +++++++++++------- app/Models/Cloudflare/AuthModel.php | 4 +- app/Models/MapurlModel.php | 4 +- app/Models/SNSUserModel.php | 4 +- app/Models/UserModel.php | 4 +- app/Views/layouts/admin.php | 4 +- app/Views/layouts/admin/direct_form.php | 4 +- app/Views/layouts/admin/iframe_form.php | 1 + app/Views/layouts/admin/modal_form.php | 1 + app/Views/layouts/front.php | 57 ++++++++-------- app/Views/layouts/front_simple.php | 49 -------------- app/Views/layouts/main.php | 65 +++++-------------- app/Views/templates/admin/footer.php | 2 +- composer.json | 6 +- 27 files changed, 255 insertions(+), 288 deletions(-) delete mode 100644 app/Views/layouts/front_simple.php diff --git a/app/Config/Constants.php b/app/Config/Constants.php index 7600ef7..29a20da 100644 --- a/app/Config/Constants.php +++ b/app/Config/Constants.php @@ -139,22 +139,22 @@ define('AUTH_FIELDS', [ //메신저 관련 define("MESSENGERS", [ "skype" => [ - "url" => "https://join.skype.com/invite/uKUgXfZThSQC", + "url" => "//join.skype.com/invite/uKUgXfZThSQC", "icon" => '스카이프', "id" => '', ], "discord" => [ - "url" => "https://discord.gg/k6nQg84N", + "url" => "//discord.gg/k6nQg84N", "icon" => '디스코드', "id" => '', ], "telegram" => [ - "url" => "https://t.me/daemonidc", + "url" => "//t.me/daemonidc", "icon" => '텔레그램', "id" => '@daemonidc', ], "kakaotalk" => [ - "url" => "https://t.me/daemonidc", + "url" => "//t.me/daemonidc", "icon" => '카카오톡', "id" => '', ], @@ -241,21 +241,21 @@ define('LAYOUTS', [ '', ], 'stylesheets' => [ - '', + '', '', '', '', '', - '', - '', - '', + '', + '', + '', '', ], 'javascripts' => [ '', '', '', - '', + '', '', ], ], @@ -267,16 +267,17 @@ define('LAYOUTS', [ '', '', '', - '', - '', - '', + '', + '', + '', + '', '', ], 'javascripts' => [ '', '', '', - '', + '', '', ], ], diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 79247c0..060bbfa 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -25,7 +25,6 @@ $routes->group('admin', ['namespace' => 'App\Controllers\Admin'], function ($rou $routes->get('toggle/(:num)/(:alpha)', 'UserController::toggle/$1/$2'); $routes->post('batchjob', 'UserController::batcjob'); $routes->get('download/(:alpha)', 'UserController::download/$1'); - $routes->get('download/(:alpha)/(:any)', 'UserController::download/$1/$2'); }); $routes->group('mapurl', function ($routes) { $routes->get('/', 'MapurlController::index'); @@ -37,7 +36,6 @@ $routes->group('admin', ['namespace' => 'App\Controllers\Admin'], function ($rou $routes->get('toggle/(:num)/(:alpha)', 'MapurlController::toggle/$1/$2'); $routes->post('batchjob', 'MapurlController::batcjob'); $routes->get('download/(:alpha)', 'MapurlController::download/$1'); - $routes->get('download/(:alpha)/(:any)', 'MapurlController::download/$1/$2'); }); $routes->group('cloudflare', ['namespace' => 'App\Controllers\Admin\Cloudflare'], function ($routes) { $routes->group('auth', function ($routes) { @@ -46,49 +44,44 @@ $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->post('modify/(:num)', 'AuthController::modify/$1'); $routes->get('toggle/(:num)/(:alpha)', 'AuthController::toggle/$1/$2'); $routes->post('batchjob', 'AuthController::batcjob'); - $routes->get('download/(:alpha)', 'AuthController::download/$1'); - $routes->get('download/(:alpha)/(:any)', 'AuthController::download/$1/$2'); $routes->get('reload/(:num)', 'AuthController::reload/$1'); $routes->get('reload', 'AuthController::reload'); $routes->cli('reload/(:num)', 'AuthController::reload/$1'); $routes->cli('reload', 'AuthController::reload'); + $routes->get('download/(:alpha)', 'AccountController::download/$1'); }); $routes->group('account', function ($routes) { $routes->get('/', 'AccountController::index'); $routes->post('create/(:uuid)', 'AccountController::create'); - $routes->get('download/(:alpha)', 'AccountController::download/$1'); - $routes->get('download/(:alpha)/(:any)', 'AccountController::download/$1/$2'); $routes->get('reload/(:alphanum)', 'AccountController::reload/$1'); $routes->get('reload', 'AccountController::reload'); $routes->cli('reload/(:alphanum)', 'AccountController::reload/$1'); $routes->cli('reload', 'AccountController::reload'); + $routes->get('download/(:alpha)', 'AccountController::download/$1'); }); $routes->group('zone', function ($routes) { $routes->get('/', 'ZoneController::index'); $routes->get('create', 'ZoneController::create_form'); $routes->post('create', 'ZoneController::create'); - $routes->get('delete/(:uuid)', 'ZoneController::delete/$1'); - $routes->get('toggle/(:uuid)/(:alpha)', 'ZoneController::toggle/$1/$2'); + $routes->get('delete/(:alphanum)', 'ZoneController::delete/$1'); + $routes->get('toggle/(:alphanum)/(:alpha)', 'ZoneController::toggle/$1/$2'); $routes->post('batchjob', 'ZoneController::batcjob'); - $routes->get('download/(:alpha)', 'ZoneController::download/$1'); - $routes->get('download/(:alpha)/(:any)', 'ZoneController::download/$1/$2'); $routes->get('reload/(:alphanum)', 'ZoneController::reload/$1'); $routes->get('reload', 'ZoneController::reload'); $routes->cli('reload/(:alphanum)', 'ZoneController::reload/$1'); $routes->cli('reload', 'ZoneController::reload'); + $routes->get('download/(:alpha)', 'ZoneController::download/$1'); }); $routes->group('record', function ($routes) { $routes->get('/', 'RecordController::index'); $routes->get('create', 'RecordController::create_form'); $routes->post('create)', 'RecordController::create'); - $routes->get('delete/(:uuid)', 'RecordController::delete/$1'); - $routes->get('toggle/(:uuid)/(:alpha)', 'RecordController::toggle/$1/$2'); + $routes->get('delete/(:alphanum)', 'RecordController::delete/$1'); + $routes->get('toggle/(:alphanum)/(:alpha)', 'RecordController::toggle/$1/$2'); $routes->post('batchjob', 'RecordController::batcjob'); $routes->get('download/(:alpha)', 'RecordController::download/$1'); - $routes->get('download/(:alpha)/(:any)', 'RecordController::download/$1/$2'); }); }); }); diff --git a/app/Controllers/Admin/Cloudflare/RecordController.php b/app/Controllers/Admin/Cloudflare/RecordController.php index 7bf5bff..facc6e7 100644 --- a/app/Controllers/Admin/Cloudflare/RecordController.php +++ b/app/Controllers/Admin/Cloudflare/RecordController.php @@ -78,6 +78,8 @@ class RecordController extends CloudflareController 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 @@ -88,40 +90,44 @@ class RecordController extends CloudflareController protected function create_process(): void { //DB작업도 Socket에서 다 처리하므로 parent::create_process()하면 않됨 - //부모데이터정의 - $this->_zone_entity = $this->getZoneModel()->getEntityByPK($this->formDatas[$this->getModel()::PARENT]); - foreach ($this->formDatas['hosts'] as $host) { - //Cloudflare 생성 - $entity = $this->getMySocket()->create($host, $this->formDatas['type'], $this->formDatas['content'], $this->formDatas['proxied']); - log_message("debug", "Record:{$entity->getTitle()} 생성 작업을 완료하였습니다."); - } - } - public function create(): RedirectResponse|string - { - $this->init(__FUNCTION__); $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 (!isHost_CommonHelper($host)) { - throw new \Exception("{$this->_account_entity->getTitle()}의 {$cnt}번째 {$host} 호스트명 형식 오류"); + throw new \Exception("{$this->_zone_entity->getTitle()}의 {$cnt}번째 {$host} 호스트명 형식 오류"); } $cnt++; } + //Socket처리 + foreach ($this->formDatas['hosts'] as $host) { + $entity = $this->getMySocket()->create($host, $this->formDatas['type'], $this->formDatas['content'], $this->formDatas['proxied']); + log_message("debug", "Record:{$entity->getTitle()} 생성 작업을 완료하였습니다."); + } + } + public function create(mixed $zone_uid = false): RedirectResponse|string + { + $this->init(__FUNCTION__); return $this->create_procedure($this->action_form); } //수정 (modify,toggle,batchjob사용) protected function modify_process(string $uid): void { //DB작업도 Socket에서 다 처리하므로 parent::modify_process($uid)하면 않됨 - $this->entity = $this->getModel()->getEntityByPK(intval($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->formDatas[$this->getModel()::PARENT]); - //Cloudflare 수정 + $this->_zone_entity = $this->getZoneModel()->getEntityByPK($this->entity->getParent()); + //Socket처리 $entity = $this->getMySocket()->modify($this->entity, $this->formDatas); log_message("debug", "Record:{$entity->getTitle()} 수정 작업을 완료하였습니다."); } @@ -131,14 +137,13 @@ class RecordController extends CloudflareController $this->action = __FUNCTION__; $this->fields = ['proxied']; $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields); - $this->modify_validate($this->action, $this->fields); - $this->formDatas = $this->getFormDatas(); return $this->batcjob_procedure(); } //삭제 protected function delete_process(string $uid): void { //DB작업도 Socket에서 다 처리하므로 parent::delete_process($uid)하면 않됨 + //자신정보정의 $this->entity = $this->getModel()->getEntityByPK(intval($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 3fe0e45..b08d881 100644 --- a/app/Controllers/Admin/Cloudflare/ZoneController.php +++ b/app/Controllers/Admin/Cloudflare/ZoneController.php @@ -91,6 +91,8 @@ class ZoneController extends CloudflareController 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 @@ -101,31 +103,11 @@ class ZoneController extends CloudflareController protected function create_process(): void { //DB작업도 Socket에서 다 처리하므로 parent::create_process()하면 않됨 - //Zone생성 - $cnt = 1; - $zone_entitys = []; - foreach ($this->formDatas['domains'] as $domain) { - $entity = $this->getMySocket()->create($domain); - log_message("debug", "Zone:{$entity->getTitle()} 작업을 완료하였습니다."); - $zone_entitys[] = $entity; - $cnt++; - } - //Record생성 - foreach ($zone_entitys as $zone_entity) { - $record_socket = new RecordSocket($entity); - foreach ($this->formDatas['hosts'] as $host) { - $entity = $record_socket->create($host, $this->formDatas['type'], $this->formDatas['content'], $this->formDatas['proxied']); - log_message("debug", "Record:{$entity->getTitle()} 작업을 완료하였습니다."); - } - } - } - public function create(): RedirectResponse|string - { - $this->init(__FUNCTION__); $this->create_validate($this->action, $this->fields); $this->formDatas = $this->getFormDatas(); //부모데이터 정의 $this->_account_entity = $this->getAccountModel()->getEntityByPK($this->formDatas[$this->getModel()::PARENT]); + //데이터검증 //Type이 A형식일경우 IP형태인지 확인 if ($this->formDatas['type'] === 'A') { if (!isIPAddress_CommonHelper($this->formDatas['content'], $this->formDatas['type'])) { @@ -152,19 +134,44 @@ class ZoneController extends CloudflareController } $cnt++; } + //Socket처리 + //Zone생성 + $cnt = 1; + $zone_entitys = []; + foreach ($this->formDatas['domains'] as $domain) { + $entity = $this->getMySocket()->create($domain); + log_message("debug", "Zone:{$entity->getTitle()} 작업을 완료하였습니다."); + $zone_entitys[] = $entity; + $cnt++; + } + //Record생성 + foreach ($zone_entitys as $zone_entity) { + $record_socket = new RecordSocket($entity); + foreach ($this->formDatas['hosts'] as $host) { + $entity = $record_socket->create($host, $this->formDatas['type'], $this->formDatas['content'], $this->formDatas['proxied']); + log_message("debug", "Record:{$entity->getTitle()} 작업을 완료하였습니다."); + } + } + } + public function create(): RedirectResponse|string + { + $this->init(__FUNCTION__); return $this->create_procedure($this->action_form); } //수정 (modify,toggle,batchjob사용) protected function modify_process(string $uid): void { //DB작업도 Socket에서 다 처리하므로 parent::modify_process($uid)하면 않됨 + $this->modify_validate($this->action, $this->fields); + $this->formDatas = $this->getFormDatas(); + //자신정보정의 $this->entity = $this->getModel()->getEntityByPK(intval($uid)); if ($this->entity === null) { throw new \Exception("{$uid} 정보를 찾을수 없습니다."); } //부모데이터정의 - $this->_account_entity = $this->getAccountModel()->getEntityByPK($this->formDatas[$this->getModel()::PARENT]); - //Cloudflare 수정 + $this->_account_entity = $this->getAccountModel()->getEntityByPK($this->entity->getParent()); + //Socket처리 $entity = $this->getMySocket()->modify($this->entity, $this->formDatas); log_message("debug", "Record:{$entity->getTitle()} 수정 작업을 완료하였습니다."); } @@ -174,14 +181,13 @@ class ZoneController extends CloudflareController $this->action = __FUNCTION__; $this->fields = ['development_mode', 'ipv6', 'security_level']; $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields); - $this->modify_validate($this->action, $this->fields); - $this->formDatas = $this->getFormDatas(); return $this->batcjob_procedure(); } //삭제 protected function delete_process(string $uid): void { //DB작업도 Socket에서 다 처리하므로 parent::delete_process($uid)하면 않됨 + //자신정보정의 $this->entity = $this->getModel()->getEntityByPK(intval($uid)); if ($this->entity === null) { throw new \Exception("{$uid} 정보를 찾을수 없습니다."); diff --git a/app/Controllers/MVController.php b/app/Controllers/MVController.php index 29454c5..7dbf4d3 100644 --- a/app/Controllers/MVController.php +++ b/app/Controllers/MVController.php @@ -179,12 +179,12 @@ abstract class MVController extends CommonController } protected function modify_process(string $uid): void { - // dd($this->fields); $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} 정보를 찾을수 없습니다."); + throw new \Exception(__FUNCTION__, " => {$uid} 정보를 찾을수 없습니다."); } $this->entity = $this->getModel()->modify($this->entity, $this->formDatas); } @@ -258,6 +258,7 @@ abstract class MVController extends CommonController //삭제 protected function delete_process(string $uid): void { + //자신정보정의 $this->entity = $this->getModel()->getEntityByPK(intval($uid)); if ($this->entity === null) { throw new \Exception("{$uid} 정보를 찾을수 없습니다."); @@ -372,7 +373,7 @@ abstract class MVController extends CommonController //모델 처리 $this->entitys = $this->list_entitys_process(); //setting return_url to session flashdata - $this->session->setFlashdata(SESSION_NAMES['RETURN_URL'], current_url() . '?' . $this->request->getUri()->getQuery() ?: ""); + $this->session->setFlashdata(SESSION_NAMES['RETURN_URL'], current_url() . '?' . $this->uri->getQuery() ?: ""); return view( strtolower($this->class_path) . "/index", ['viewDatas' => $this->getViewDatas()] diff --git a/app/Helpers/Admin/Cloudflare/Account_helper.php b/app/Helpers/Admin/Cloudflare/Account_helper.php index 3f1e7bb..a548313 100644 --- a/app/Helpers/Admin/Cloudflare/Account_helper.php +++ b/app/Helpers/Admin/Cloudflare/Account_helper.php @@ -25,7 +25,7 @@ function getFieldForm_AccountHelper(string $field, mixed $value, array $viewData $form = form_dropdown($field, [ "" => lang($viewDatas['class_path'] . '.label.' . $field) . ' 선택', ...$viewDatas['field_options'][$field] - ], $value, [...$extras, 'class' => "select-field"]); + ], isset($viewDatas[AccountModel::PARENT]) ? $viewDatas[AccountModel::PARENT] : $value, [...$extras, 'class' => "select-field"]); // // return form_multiselect($field, $field_options[$field], is_array($value) ? [...$value] : [$value], [$extras]); // foreach ($viewDatas['field_options'][$field] as $key => $label) { // $checkboxs[] = form_checkbox("{$field}[]", $key, in_array($key, explode(DEFAULTS["DELIMITER_ROLE"], $value))) . $label; @@ -170,7 +170,7 @@ function getListButton_AccountHelper(string $action, array $viewDatas, array $ex { switch ($action) { case 'create': - $viewDatas['list_action_url'] = $url = current_url() . '/' . $action; + $viewDatas['list_action_url'] = current_url() . '/' . $action . '?' . $viewDatas['uri']->getQuery(['only' => AccountModel::PARENT]); $extras = ["class" => "btn btn-outline btn-primary btn-circle", "target" => "_self", ...$extras]; $action = getListButtonLabel_AccountHelper($action, '입력', $viewDatas, $extras); break; diff --git a/app/Helpers/Admin/Cloudflare/Auth_helper.php b/app/Helpers/Admin/Cloudflare/Auth_helper.php index 65359cf..3f87349 100644 --- a/app/Helpers/Admin/Cloudflare/Auth_helper.php +++ b/app/Helpers/Admin/Cloudflare/Auth_helper.php @@ -138,7 +138,7 @@ function getListButton_AuthHelper(string $action, array $viewDatas, array $extra { switch ($action) { case 'create': - $viewDatas['list_action_url'] = $url = current_url() . '/' . $action; + $viewDatas['list_action_url'] = current_url() . '/' . $action; $extras = ["class" => "btn btn-outline btn-primary btn-circle", "target" => "_self", ...$extras]; $action = getListButtonLabel_AuthHelper($action, '입력', $viewDatas, $extras); break; diff --git a/app/Helpers/Admin/Cloudflare/Record_helper.php b/app/Helpers/Admin/Cloudflare/Record_helper.php index 48a589b..de0b8ca 100644 --- a/app/Helpers/Admin/Cloudflare/Record_helper.php +++ b/app/Helpers/Admin/Cloudflare/Record_helper.php @@ -1,6 +1,7 @@ lang($viewDatas['class_path'] . '.label.' . $field) . ' 선택', - ...$viewDatas['field_options'][$field] - ], $value, [...$extras, 'class' => "select-field"]); + $form = form_dropdown( + $field, + [ + "" => lang($viewDatas['class_path'] . '.label.' . $field) . ' 선택', + ...$viewDatas['field_options'][$field] + ], + isset($viewDatas[RecordModel::PARENT]) ? $viewDatas[RecordModel::PARENT] : $value, + [...$extras, 'class' => "select-field"] + ); // // return form_multiselect($field, $field_options[$field], is_array($value) ? [...$value] : [$value], [$extras]); // foreach ($viewDatas['field_options'][$field] as $key => $label) { // $checkboxs[] = form_checkbox("{$field}[]", $key, in_array($key, explode(DEFAULTS["DELIMITER_ROLE"], $value))) . $label; @@ -84,19 +90,12 @@ function getFieldView_RecordHelper(string $field, array $viewDatas, array $extra $value = sprintf("%s%s", $viewDatas['entity']->fixed == 'on' ? ICONS['LOCK'] : "", $value); $value = anchor($url, $value, ["target" => "_self"]); break; - case 'category_uid': - foreach (array_values($viewDatas['field_options'][$field]) as $category_2depths) { - foreach ($category_2depths as $key => $depth) { - if ($key == $depth) { - $value = $depth; - } - } - } - break; - case 'cost': - case 'price': - case 'sale': - $value = number_format(!$value ? 0 : $value) . "원"; + case 'content': + $value = sprintf( + "%s", + $value, + mb_strlen($value) > 40 ? mb_substr($value, 0, 40) . '...' : $value + ); break; case 'updated_at': case 'created_at': @@ -197,7 +196,7 @@ function getListButton_RecordHelper(string $action, array $viewDatas, array $ext { switch ($action) { case 'create': - $viewDatas['list_action_url'] = $url = current_url() . '/' . $action; + $viewDatas['list_action_url'] = current_url() . '/' . $action . '?' . $viewDatas['uri']->getQuery(['only' => RecordModel::PARENT]); $extras = ["class" => "btn btn-outline btn-primary btn-circle", "target" => "_self", ...$extras]; $action = getListButtonLabel_RecordHelper($action, '입력', $viewDatas, $extras); break; diff --git a/app/Helpers/Admin/Cloudflare/Zone_helper.php b/app/Helpers/Admin/Cloudflare/Zone_helper.php index dfb3c0a..2664d20 100644 --- a/app/Helpers/Admin/Cloudflare/Zone_helper.php +++ b/app/Helpers/Admin/Cloudflare/Zone_helper.php @@ -25,7 +25,7 @@ function getFieldForm_ZoneHelper(string $field, mixed $value, array $viewDatas, $form = form_dropdown($field, [ "" => lang($viewDatas['class_path'] . '.label.' . $field) . ' 선택', ...$viewDatas['field_options'][$field] - ], $value, [...$extras, 'class' => "select-field"]); + ], isset($viewDatas[ZoneModel::PARENT]) ? $viewDatas[ZoneModel::PARENT] : $value, [...$extras, 'class' => "select-field"]); // // return form_multiselect($field, $field_options[$field], is_array($value) ? [...$value] : [$value], [$extras]); // foreach ($viewDatas['field_options'][$field] as $key => $label) { // $checkboxs[] = form_checkbox("{$field}[]", $key, in_array($key, explode(DEFAULTS["DELIMITER_ROLE"], $value))) . $label; @@ -223,7 +223,7 @@ function getListButton_ZoneHelper(string $action, array $viewDatas, array $extra { switch ($action) { case 'create': - $viewDatas['list_action_url'] = $url = current_url() . '/' . $action; + $viewDatas['list_action_url'] = current_url() . '/' . $action . '?' . $viewDatas['uri']->getQuery(['only' => ZoneModel::PARENT]); $extras = ["class" => "btn btn-outline btn-primary btn-circle", "target" => "_self", ...$extras]; $action = getListButtonLabel_ZoneHelper($action, '입력', $viewDatas, $extras); break; diff --git a/app/Helpers/Admin/Mapurl_helper.php b/app/Helpers/Admin/Mapurl_helper.php index a3916bf..6b321c3 100644 --- a/app/Helpers/Admin/Mapurl_helper.php +++ b/app/Helpers/Admin/Mapurl_helper.php @@ -132,7 +132,7 @@ function getListButton_MapurlHelper(string $action, array $viewDatas, array $ext { switch ($action) { case 'create': - $viewDatas['list_action_url'] = $url = current_url() . '/' . $action; + $viewDatas['list_action_url'] = current_url() . '/' . $action; $extras = ["class" => "btn btn-outline btn-primary btn-circle", "target" => "_self", ...$extras]; $action = getListButtonLabel_MapurlHelper($action, '입력', $viewDatas, $extras); break; diff --git a/app/Helpers/Admin/User_helper.php b/app/Helpers/Admin/User_helper.php index e295e60..0037198 100644 --- a/app/Helpers/Admin/User_helper.php +++ b/app/Helpers/Admin/User_helper.php @@ -151,7 +151,7 @@ function getListButton_UserHelper(string $action, array $viewDatas, array $extra { switch ($action) { case 'create': - $viewDatas['list_action_url'] = $url = current_url() . '/' . $action; + $viewDatas['list_action_url'] = current_url() . '/' . $action; $extras = ["class" => "btn btn-outline btn-primary btn-circle", "target" => "_self", ...$extras]; $action = getListButtonLabel_UserHelper($action, '입력', $viewDatas, $extras); break; diff --git a/app/Libraries/MySocket/Cloudflare/RecordSocket.php b/app/Libraries/MySocket/Cloudflare/RecordSocket.php index a66aae9..6cad3d3 100644 --- a/app/Libraries/MySocket/Cloudflare/RecordSocket.php +++ b/app/Libraries/MySocket/Cloudflare/RecordSocket.php @@ -52,16 +52,19 @@ class RecordSocket extends CloudflareSocket public function create(string $host, string $type, string $content, string $proxied): RecordEntity { //Socket용 - //도메인생성을 위해 Cloudflare에 전송 - $cf = $this->getClient()->post('zones/' . $this->_zone_entity->getPK() . '/dns_records', [ + //호스트생성을 위해 Cloudflare에 전송 + $datas = [ 'name' => $host, 'type' => $type, 'content' => $content, 'proxied' => $proxied === 'on' ? true : false - ]); + ]; + $cf = $this->getRequest('post', "zones/{$this->_zone_entity->getPK()}/dns_records", $datas); $cf = json_decode($cf->getBody()); if (!$cf->success) { - throw new \Exception("Record:" . __FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); + $message = "Record:" . __FUNCTION__ . "에서 실패:\nrequest:" . var_export($datas, true) . "\nresponse:" . var_export($cf, true); + log_message("error", $message); + throw new \Exception($message); } //DB생성 $formDatas = $this->getArrayByResult($cf->result); @@ -77,17 +80,23 @@ class RecordSocket extends CloudflareSocket 'proxied' => $entity->proxied == 'on' ? true : false, 'ttl' => intval($entity->ttl), ]; - if (isset($formDatas['proxied']) && $formDatas['proxied'] === 'on') { - $datas['proxied'] = true; - $datas['ttl'] = 1; - } elseif (isset($formDatas['proxied']) && $formDatas['proxied'] === 'off') { - $datas['proxied'] = false; - $datas['ttl'] = 120; + if (isset($formDatas['proxied'])) { + if ($formDatas['proxied'] === 'on') { + $datas['proxied'] = true; + $datas['ttl'] = 1; + } else { + $datas['proxied'] = false; + $datas['ttl'] = 120; + } } - $cf = $this->getClient()->put('zones/' . $this->_zone_entity->getPK() . '/dns_records', $datas); + // 인코딩된 JSON을 확인 + // throw new \Exception("Record:" . __FUNCTION__ . "\n" . json_encode($datas, JSON_PRETTY_PRINT) . "\n" . var_export($datas, true)); + $cf = $this->getRequest('put', "zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}", $datas); $cf = json_decode($cf->getBody()); if (!$cf->success) { - throw new \Exception("Record:" . __FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); + $message = "Record:" . __FUNCTION__ . "에서 실패:\nrequest:" . var_export($datas, true) . "\nresponse:" . var_export($cf, true); + log_message("error", $message); + throw new \Exception($message); } //DB수정 $formDatas = $this->getArrayByResult($cf->result); @@ -95,10 +104,12 @@ class RecordSocket extends CloudflareSocket } public function delete(RecordEntity $entity): void { - $cf = $this->getClient()->delete('zones/' . $this->_zone_entity->getPK() . '/dns_records/' . $entity->getPK()); + $cf = $this->getRequest('delete', "zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}"); $cf = json_decode($cf->getBody()); if (!$cf->success) { - throw new \Exception("Record:" . __FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); + $message = "Record:" . __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true); + log_message("error", $message); + throw new \Exception($message); } //DB삭제 $this->getModel()->where(RecordModel::PK, $entity->getPK()); @@ -107,14 +118,14 @@ class RecordSocket extends CloudflareSocket public function sync(RecordEntity $entity): void { //기존 Sync형태 - // $cf = $this->getClient()->get("zones/{$this->_zone_entity->getPK()}/dns_records/" . $entity->getPK()); + // $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)); //Async형태 - $promise = $this->getClient()->getAsync("zones/{$this->_zone_entity->getPK()}/dns_records/{$entity->getPK()}"); + $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']; diff --git a/app/Libraries/MySocket/Cloudflare/ZoneSocket.php b/app/Libraries/MySocket/Cloudflare/ZoneSocket.php index 418239a..60917bc 100644 --- a/app/Libraries/MySocket/Cloudflare/ZoneSocket.php +++ b/app/Libraries/MySocket/Cloudflare/ZoneSocket.php @@ -59,10 +59,12 @@ class ZoneSocket extends CloudflareSocket //Cfzone에서 가져온 값을 zone에 setting protected function getCFSetting(ZoneEntity $entity): ZoneEntity { - $cf = $this->getClient()->patch('zones/' . $entity->getPK() . '/settings/'); + $cf = $this->getRequest('patch', 'zones/' . $entity->getPK() . '/settings/'); $cf = json_decode($cf->getBody()); if (!$cf->success) { - throw new \Exception("Zone:" . __FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); + $message = "Zone:" . __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true); + log_message("error", $message); + throw new \Exception($message); } foreach ($cf->result as $cf) { switch ($cf->id) { @@ -81,10 +83,13 @@ class ZoneSocket extends CloudflareSocket } protected function setCFSetting(ZoneEntity $entity, string $field, string $value): ZoneEntity { - $cf = $this->getClient()->patch('zones/' . $entity->getPK() . '/settings/' . $field, array('value' => $value)); + $datas = ['value' => $value]; + $cf = $this->getRequest('patch', 'zones/' . $entity->getPK() . '/settings/' . $field, $datas); $cf = json_decode($cf->getBody()); if (!$cf->success || $cf->result->id !== $field) { - throw new \Exception("Zone:" . __FUNCTION__ . "에서 {$field}->{$value} 변경실패:\n" . var_export($cf, true)); + $message = "Zone:" . __FUNCTION__ . "에서 실패:\nrequest:" . var_export($datas, true) . "\nresponse:" . var_export($cf, true); + log_message("error", $message); + throw new \Exception($message); } //최종 결과값은 body->result->id='필드명',body->result->value='on/off'이런식으로 받음 return $cf->result->value; @@ -93,14 +98,17 @@ class ZoneSocket extends CloudflareSocket { //Socket용 //도메인생성을 위해 Cloudflare에 전송 - $cf = $this->getClient()->post('zones/', [ + $datas = [ 'accountId' => $this->_account_entity->getPK(), 'name' => $domain, 'jump_start' => $jump_start, - ]); + ]; + $cf = $this->getRequest('post', 'zones/', $datas); $cf = json_decode($cf->getBody()); if (!$cf->success) { - throw new \Exception("Zone:" . __FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); + $message = "Zone:" . __FUNCTION__ . "에서 실패:\nrequest:" . var_export($datas, true) . "\nresponse:" . var_export($cf, true); + log_message("error", $message); + throw new \Exception($message); } //DB생성 $formDatas = $this->getArrayByResult($cf->result); @@ -126,10 +134,12 @@ class ZoneSocket extends CloudflareSocket } public function delete(ZoneEntity $entity): void { - $cf = $this->getClient()->delete('zones/' . $entity->getPK()); + $cf = $this->getRequest('delete', "zones/{$entity->getPK()}"); $cf = json_decode($cf->getBody()); if (!$cf->success) { - throw new \Exception("Zone:" . __FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); + $message = "Zone:" . __FUNCTION__ . "에서 실패:\nresponse:" . var_export($cf, true); + log_message("error", $message); + throw new \Exception($message); } //DB삭제 $this->getModel()->where(ZoneModel::PK, $entity->getPK()); @@ -138,7 +148,7 @@ class ZoneSocket extends CloudflareSocket public function sync(ZoneEntity $entity): void { //기존 Sync형태 - // $cf = $this->getClient()->get('zones/' . $entity->getPK()); + // $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)); @@ -146,7 +156,7 @@ class ZoneSocket extends CloudflareSocket // log_message("notice", "Zone:" . __FUNCTION__ . "=> 작업을 완료하였습니다."); // return new ZoneEntity($this->getArrayByResult($cf->result)); //Async형태 - $promise = $this->getClient()->getAsync("zones/{$entity->getPK()}"); + $promise = $this->getRequest('getAsync', "zones/{$entity->getPK()}"); $promise->then( onFulfilled: function ($response) use ($entity): ZoneEntity { $record = json_decode($response->getBody(), true)['result']; diff --git a/app/Libraries/MySocket/CloudflareSocket.php b/app/Libraries/MySocket/CloudflareSocket.php index 8af570f..8760dfd 100644 --- a/app/Libraries/MySocket/CloudflareSocket.php +++ b/app/Libraries/MySocket/CloudflareSocket.php @@ -3,11 +3,13 @@ namespace App\Libraries\MySocket; -use GuzzleHttp\Client; -use App\Models\Cloudflare\AuthModel; -use App\Models\Cloudflare\AccountModel; -use App\Libraries\CommonLibrary; use App\Entities\Cloudflare\AuthEntity; +use App\Libraries\CommonLibrary; +use App\Models\Cloudflare\AccountModel; +use App\Models\Cloudflare\AuthModel; +use Cloudflare\API\Adapter\ResponseException; +use GuzzleHttp\Client; +use GuzzleHttp\Exception\RequestException; abstract class CloudflareSocket extends CommonLibrary { @@ -29,6 +31,22 @@ abstract class CloudflareSocket extends CommonLibrary self::$_request_timewait = getenv("cfmgr.request.timewait") ?: 60; } abstract protected function getArrayByResult($result, array $formDatas = []): array; + private function getClient(): Client + { + if ($this->_client === null) { + // Guzzle HTTP 클라이언트를 설정하면서 Cloudflare API 토큰 사용 + $this->_client = new Client([ + 'base_uri' => 'https://api.cloudflare.com/client/v4/', + 'headers' => [ + 'X-Auth-Email' => $this->_auth_entity->getID(), // 인증 토큰 사용 + 'X-Auth-Key' => $this->_auth_entity->getAuthKey(), // 인증 토큰 사용 + 'Content-Type' => 'application/json', + ], + 'verify' => false // SSL 인증서 검증을 비활성화 + ]); + } + return $this->_client; + } final protected function getAuthModel(): AuthModel { if ($this->_authModel === null) { @@ -43,28 +61,26 @@ abstract class CloudflareSocket extends CommonLibrary } return $this->_accountModel; } - final protected function getClient(): Client + final protected function getRequest(string $method, string $uri, array $datas = []) { - if ($this->_client === null) { - // Guzzle HTTP 클라이언트를 설정하면서 Cloudflare API 토큰 사용 - $this->_client = new Client([ - 'base_uri' => 'https://api.cloudflare.com/client/v4/', - 'headers' => [ - 'X-Auth-Email' => $this->_auth_entity->getID(), // 인증 토큰 사용 - 'X-Auth-Key' => $this->_auth_entity->getAuthKey(), // 인증 토큰 사용 - 'Content-Type' => 'application/json', - ], - 'verify' => false // SSL 인증서 검증을 비활성화 + if (!in_array($method, ['get', '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, ]); + if (self::$_request >= self::$_request_max) { + log_message('warning', sprintf("--Cloudflare API Call %s초 대기 시작--", self::$_request_timewait)); + sleep(intval(self::$_request_timewait)); + self::$_request = 0; + log_message('warning', sprintf("--Cloudflare API Call %s초 대기 종료--", self::$_request_timewait)); + } + self::$_request++; + } catch (RequestException $err) { + throw ResponseException::fromRequestException($err); } - if (self::$_request >= self::$_request_max) { - log_message('warning', sprintf("--Cloudflare API Call %s초 대기 시작--", self::$_request_timewait)); - sleep(intval(self::$_request_timewait)); - self::$_request = 0; - log_message('warning', sprintf("--Cloudflare API Call %s초 대기 종료--", self::$_request_timewait)); - } - self::$_request++; - return $this->_client; + return $response; } final protected function reload_procedure($uri): array diff --git a/app/Models/Cloudflare/AuthModel.php b/app/Models/Cloudflare/AuthModel.php index d3b7e2f..66ec28b 100644 --- a/app/Models/Cloudflare/AuthModel.php +++ b/app/Models/Cloudflare/AuthModel.php @@ -54,9 +54,9 @@ class AuthModel extends CommonModel } return $options; } - public function getEntityByPK(int $uid): null|AuthEntity + public function getEntityByPK(string $uid): null|AuthEntity { - $this->where($this->getPKField(), $uid); + $this->where($this->getPKField(), intval($uid)); return $this->getEntity(); } public function getEntityByID(string $id): null|AuthEntity diff --git a/app/Models/MapurlModel.php b/app/Models/MapurlModel.php index 3c598e1..9c683aa 100644 --- a/app/Models/MapurlModel.php +++ b/app/Models/MapurlModel.php @@ -45,9 +45,9 @@ class MapurlModel extends CommonModel } return $rule; } - public function getEntityByPK(int $uid): null|MapurlEntity + public function getEntityByPK(string $uid): null|MapurlEntity { - $this->where($this->getPKField(), $uid); + $this->where($this->getPKField(), intval($uid)); return $this->getEntity(); } //create용 diff --git a/app/Models/SNSUserModel.php b/app/Models/SNSUserModel.php index c3ecfdc..2f075ab 100644 --- a/app/Models/SNSUserModel.php +++ b/app/Models/SNSUserModel.php @@ -46,9 +46,9 @@ class SNSUserModel extends CommonModel } return $rule; } - public function getEntityByPK(int $uid): null|SNSUSerEntity + public function getEntityByPK(string $uid): null|SNSUSerEntity { - $this->where($this->getPKField(), $uid); + $this->where($this->getPKField(), intval($uid)); return $this->getEntity(); } public function getEntityByID(string $id): null|SNSUSerEntity diff --git a/app/Models/UserModel.php b/app/Models/UserModel.php index 60b982d..65f84a2 100644 --- a/app/Models/UserModel.php +++ b/app/Models/UserModel.php @@ -54,9 +54,9 @@ class UserModel extends CommonModel return $rule; } - public function getEntityByPK(int $uid): null|UserEntity + public function getEntityByPK(string $uid): null|UserEntity { - $this->where($this->getPKField(), $uid); + $this->where($this->getPKField(), intval($uid)); return $this->getEntity(); } public function getEntityByID(string $id): null|UserEntity diff --git a/app/Views/layouts/admin.php b/app/Views/layouts/admin.php index 3b49891..c3102ce 100644 --- a/app/Views/layouts/admin.php +++ b/app/Views/layouts/admin.php @@ -29,9 +29,9 @@
include(LAYOUTS[$viewDatas['layout']]['path'] . '/left_menu'); ?>
-
include('templates/admin/header'); ?>
+
include("templates/{$viewDatas['layout']}/header"); ?>
renderSection('content') ?>
- +
diff --git a/app/Views/layouts/admin/direct_form.php b/app/Views/layouts/admin/direct_form.php index 3b49891..c3102ce 100644 --- a/app/Views/layouts/admin/direct_form.php +++ b/app/Views/layouts/admin/direct_form.php @@ -29,9 +29,9 @@
include(LAYOUTS[$viewDatas['layout']]['path'] . '/left_menu'); ?>
-
include('templates/admin/header'); ?>
+
include("templates/{$viewDatas['layout']}/header"); ?>
renderSection('content') ?>
- +
diff --git a/app/Views/layouts/admin/iframe_form.php b/app/Views/layouts/admin/iframe_form.php index 2631393..01324f1 100644 --- a/app/Views/layouts/admin/iframe_form.php +++ b/app/Views/layouts/admin/iframe_form.php @@ -24,6 +24,7 @@
renderSection('content') ?>
+ \ No newline at end of file diff --git a/app/Views/layouts/admin/modal_form.php b/app/Views/layouts/admin/modal_form.php index 2631393..01324f1 100644 --- a/app/Views/layouts/admin/modal_form.php +++ b/app/Views/layouts/admin/modal_form.php @@ -24,6 +24,7 @@
renderSection('content') ?>
+ \ No newline at end of file diff --git a/app/Views/layouts/front.php b/app/Views/layouts/front.php index 1b507c1..c3102ce 100644 --- a/app/Views/layouts/front.php +++ b/app/Views/layouts/front.php @@ -2,40 +2,41 @@ - - - - - + + + <?= $viewDatas['title'] ?> + + + + + + + + + + + -