From bb717a7cd65dacccca2a4540a53f46860048c053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Wed, 19 Jul 2023 16:23:30 +0900 Subject: [PATCH] servermgrv2 init... --- app/Config/Constants.php | 9 ++- app/Controllers/Admin/HPILOController.php | 4 +- app/Controllers/Admin/UserSNSController.php | 49 ++++++---------- app/Libraries/Adapter/API/Adapter.php | 64 ++++++--------------- app/Libraries/Adapter/API/CurlAdapter.php | 51 +++++++++------- app/Libraries/Adapter/API/GuzzleAdapter.php | 10 ++-- app/Libraries/Adapter/API/LocalAdapter.php | 53 +++++++++++++++++ 7 files changed, 133 insertions(+), 107 deletions(-) create mode 100644 app/Libraries/Adapter/API/LocalAdapter.php diff --git a/app/Config/Constants.php b/app/Config/Constants.php index 207d2b6..6be7234 100644 --- a/app/Config/Constants.php +++ b/app/Config/Constants.php @@ -167,11 +167,14 @@ define('STATUS', [ //Upload , Download 관련 define('PATHS', [ 'EXCEL' => getenv('path.excel') ? getenv('path.excel') : "../writable/Excel", - 'UPLOADS' => getenv('path.upload') ? getenv('path.upload') : "../writable/uploads", + 'UPLOAD' => getenv('path.upload') ? getenv('path.upload') : "../writable/uploads", 'DOWNLOAD' => getenv('path.download') ? getenv('path.download') : "../writable/download", + 'API' => getenv('path.api') ? getenv('path.api') : "../writable/api", ]); -if (!is_dir(APPPATH . PATHS['EXCEL'])) { - mkdir(APPPATH . PATHS['EXCEL'], 0640); +foreach (PATHS as $key => $path) { + if (!is_dir(APPPATH . $path)) { + mkdir(APPPATH . $path, 0640); + } } //아이콘 및 Sound관련 diff --git a/app/Controllers/Admin/HPILOController.php b/app/Controllers/Admin/HPILOController.php index 52b1676..4126c47 100644 --- a/app/Controllers/Admin/HPILOController.php +++ b/app/Controllers/Admin/HPILOController.php @@ -65,8 +65,8 @@ class HPILOController extends \App\Controllers\Admin\AdminController private function getAdapter(HPILOEntity $entity) { if (is_null($this->_adapter)) { - $adapterClass = HPILOS['ADAPTER']; - $this->_adapter = new $adapterClass($entity, HPILOS['DEBUG']); + $adapterClass = getenv('hpilo.api.adapter') ? getenv('hpilo.api.adapter') : "\App\Libraries\Log\GuzzleAdapter"; + $this->_adapter = new $adapterClass($entity, getenv('hpilo.api.debug') ? getenv('hpilo.api.debug') : false); } return $this->_adapter; } diff --git a/app/Controllers/Admin/UserSNSController.php b/app/Controllers/Admin/UserSNSController.php index 01bbb53..eb06c91 100644 --- a/app/Controllers/Admin/UserSNSController.php +++ b/app/Controllers/Admin/UserSNSController.php @@ -59,38 +59,27 @@ class UserSNSController extends \App\Controllers\Admin\AdminController } } - //Insert관련 - protected function insert_process() - { - // return parent::insert_process(); - } - //Update관련 - protected function update_process($entity) - { - // return parent::update_process($entity); - } - ////Action 모음 //Insert관련 - final public function insert() - { - // return $this->insert_procedure(); - } - //Update관련 - final public function update($uid) - { - // return $this->update_procedure($uid); - } - //Toggle관련 - final public function toggle($uid, string $field) - { - // return $this->toggle_procedure($uid, $field); - } - //Batchjob 관련 - final public function batchjob() - { - // return $this->batchjob_procedure(); - } + // final public function insert() + // { + // // return $this->insert_procedure(); + // } + // //Update관련 + // final public function update($uid) + // { + // // return $this->update_procedure($uid); + // } + // //Toggle관련 + // final public function toggle($uid, string $field) + // { + // // return $this->toggle_procedure($uid, $field); + // } + // //Batchjob 관련 + // final public function batchjob() + // { + // // return $this->batchjob_procedure(); + // } //Delete 관련 final public function delete($uid) { diff --git a/app/Libraries/Adapter/API/Adapter.php b/app/Libraries/Adapter/API/Adapter.php index 22478e3..fb48909 100644 --- a/app/Libraries/Adapter/API/Adapter.php +++ b/app/Libraries/Adapter/API/Adapter.php @@ -5,17 +5,19 @@ namespace App\Libraries\Adapter\API; use \App\Entities\HPILOEntity; // 참고:https://github.com/SyntaxPhoenix/iloclient -class Adapter +abstract class Adapter { private $_entity = null; protected $_client = null; protected $_debug = false; - public function __construct($entity, $debug = false) + protected function __construct($entity, $debug = false) { $this->_entity = $entity; $this->_debug = $debug; } - protected function getEntity(): HPILOEntity + abstract protected function getClient(); + abstract protected function requestURL(string $url, string $method, array $datas = []): object; + final protected function getEntity() { return $this->_entity; } @@ -28,6 +30,19 @@ class Adapter //type: basic , digest return array($this->getEntity()->getID(), $this->getEntity()->getPassword(), $type); } + protected function isSSLVerifiy(): bool + { + return getenv('hpilo.ssl') == 'true' ? true : false; + } + protected function getCookieFile() + { + return PATHS['API'] . getenv('api.cookie') ? getenv('api.cookie') : "api-cookie_" . date("Ymd") . ".log"; + } + protected function getDebugFile() + { + return PATHS['API'] . getenv('api.debug') ? getenv('api.debug') : "api-debug_" . date("Ymd") . ".log"; + } + final public function get(string $url): object { return $this->requestURL($url, 'GET'); @@ -36,47 +51,4 @@ class Adapter { return $this->requestURL($url, 'POST', $datas); } - - protected function getClient() - { - if (is_null($this->_client)) { - // 참조:https://www.codeigniter.com/user_guide/libraries/curlrequest.html?highlight=curl# - // ex:)$options = [ 'baseURI' => 'http://www.foo.com/1.0/', 'timeout' => 0, 'allow_redirects' => false, 'proxy' => '192.168.16.1:10' ] - $options = [ - 'baseURI' => $this->getServerInfo(), - 'auth' => $this->getAccountInfo(), - 'verify' => getenv('hpilo.verify') == 'true' ? true : false, - 'cookie' => HPILOS['PATH'] . getenv('hpilo.cookie.file'), - ]; - if ($this->_debug) { - $options['debug'] = HPILOS['PATH'] . getenv('hpilo.debug.file'); //or true - } - $this->_client = \Config\Services::curlrequest($options); - } - return $this->_client; - } - protected function requestURL(string $url, string $method, array $datas = []): object - { - // dd($this->getClient()); - $options = array(); - switch ($method) { - case 'POST': - $response = $this->getClient()->setBody($datas)->request($method, $url, $options); - break; - case 'HEAD': - break; - default: - $response = $this->getClient()->request($method, $url, $options); - break; - } - dd($response); - if ($response->getStatusCode() != 200) { - throw new \Exception(sprintf( - "오류가 발생하였습니다.\n%s\n%s", - $response->getHeaderLine('content-type'), - $response->getBody() - )); - } - return json_decode($response->getBody()); - } } diff --git a/app/Libraries/Adapter/API/CurlAdapter.php b/app/Libraries/Adapter/API/CurlAdapter.php index 087e26b..21ee009 100644 --- a/app/Libraries/Adapter/API/CurlAdapter.php +++ b/app/Libraries/Adapter/API/CurlAdapter.php @@ -7,19 +7,19 @@ namespace App\Libraries\Adapter\API; // https://github.com/SyntaxPhoenix/iloclient class CurlAdapter extends Adapter { - public function __construct(\App\Entities\HPILOEntity $entity, $debug = false) + public function __construct($entity, $debug = false) { parent::__construct($entity, $debug); } - private function debug($ch, $response, array $datas = array()) + private function debugging($response, array $datas = array()) { if (!$this->_debug) { return false; } if ($response === false) { - log_message('error', curl_error($ch)); + log_message('error', curl_error($this->getClient())); } - $info = curl_getinfo($ch); + $info = curl_getinfo($this->getClient()); log_message('debug', var_export($info, true)); log_message('debug', var_export($datas, true)); log_message('debug', "{$info['total_time']}초, url:{$info['url']}, return:{$info['http_code']}"); @@ -152,36 +152,43 @@ class CurlAdapter extends Adapter break; } } + protected function getClient() + { + if (is_null($this->_client)) { + $this->_client = curl_init(); + } + return $this->_client; + } protected function requestURL(string $url, string $method, array $datas = []): object { - $ch = curl_init($this->getServerInfo() . $url); + curl_setopt($this->getClient(), CURLOPT_URL, $this->getServerInfo() . $url); switch ($method) { case 'POST': - curl_setopt($ch, CURLOPT_POST, TRUE); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($datas)); - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); + curl_setopt($this->getClient(), CURLOPT_POST, TRUE); + curl_setopt($this->getClient(), CURLOPT_CUSTOMREQUEST, 'PATCH'); + curl_setopt($this->getClient(), CURLOPT_POSTFIELDS, json_encode($datas)); + curl_setopt($this->getClient(), CURLOPT_HTTPHEADER, array('Content-Type: application/json')); //cookie값 파일저장용 - curl_setopt($ch, CURLOPT_COOKIEJAR, HPILOS['CURL_COOKIE_FILE']); - curl_setopt($ch, CURLOPT_COOKIEFILE, HPILOS['CURL_COOKIE_FILE']); + curl_setopt($this->getClient(), CURLOPT_COOKIEJAR, $this->getCookieFile()); + curl_setopt($this->getClient(), CURLOPT_COOKIEFILE, $this->getCookieFile()); break; default: break; } //cookie값 전달용 - foreach (curl_getinfo($ch, CURLINFO_COOKIELIST) as $cookie_line) { - curl_setopt($ch, CURLOPT_COOKIELIST, $cookie_line); + foreach (curl_getinfo($this->getClient(), CURLINFO_COOKIELIST) as $cookie_line) { + curl_setopt($this->getClient(), CURLOPT_COOKIELIST, $cookie_line); } //SSL 확인여부용 - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, HPILOS['SSL']); - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, HPILOS['SSL']); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); - curl_setopt($ch, CURLOPT_USERPWD, implode(":", $this->getAccountInfo())); + curl_setopt($this->getClient(), CURLOPT_SSL_VERIFYPEER, $this->isSSLVerifiy()); + curl_setopt($this->getClient(), CURLOPT_SSL_VERIFYHOST, $this->isSSLVerifiy()); + curl_setopt($this->getClient(), CURLOPT_RETURNTRANSFER, true); + curl_setopt($this->getClient(), CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + curl_setopt($this->getClient(), CURLOPT_USERPWD, implode(":", $this->getAccountInfo())); //header값 전달용 $headers = []; curl_setopt( - $ch, + $this->getClient(), CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$headers) { $length = strlen($header); @@ -193,9 +200,9 @@ class CurlAdapter extends Adapter return $length; } ); - $response = curl_exec($ch); - $this->debug($ch, $response); - curl_close($ch); + $response = curl_exec($this->getClient()); + $this->debugging($response); + curl_close($this->getClient()); if (is_null($response)) { throw new \Exception("해당서버[{$this->getServerInfo()}]의 ILO접속 오류가 발생하였습니다."); } diff --git a/app/Libraries/Adapter/API/GuzzleAdapter.php b/app/Libraries/Adapter/API/GuzzleAdapter.php index 2f46bf0..3575ac0 100644 --- a/app/Libraries/Adapter/API/GuzzleAdapter.php +++ b/app/Libraries/Adapter/API/GuzzleAdapter.php @@ -8,7 +8,7 @@ use GuzzleHttp\Exception\ClientException; class GuzzleAdapter extends Adapter { private $_jar = null; - public function __construct(\App\Entities\HPILOEntity $entity, $debug = false) + public function __construct($entity, $debug = false) { parent::__construct($entity, $debug); } @@ -26,7 +26,8 @@ class GuzzleAdapter extends Adapter log_message('debug', var_export($this->getCookieJar()->getCookieByName('session' . $key), true)); } } - protected function getClient() + + protected function getClient() //Guzzle이용시 { if (is_null($this->_client)) { // 참조:https://docs.guzzlephp.org/en/stable/request-options.html @@ -34,8 +35,8 @@ class GuzzleAdapter extends Adapter $options = [ 'base_uri' => $this->getServerInfo(), 'auth' => $this->getAccountInfo(), - 'verify' => HPILOS['SSL'], - 'cookie' => HPILOS['GUZZLE_COOKIE'], + 'verify' => $this->isSSLVerifiy(), + 'cookie' => $this->getCookieFile(), // \GuzzleHttp\RequestOptions::ALLOW_REDIRECTS => [ // 'max' => 10, // allow at most 10 redirects. // 'strict' => true, // use "strict" RFC compliant redirects. @@ -47,6 +48,7 @@ class GuzzleAdapter extends Adapter } return $this->_client; } + protected function requestURL(string $url, string $method, array $datas = []): object { try { diff --git a/app/Libraries/Adapter/API/LocalAdapter.php b/app/Libraries/Adapter/API/LocalAdapter.php new file mode 100644 index 0000000..13d3b37 --- /dev/null +++ b/app/Libraries/Adapter/API/LocalAdapter.php @@ -0,0 +1,53 @@ +_client)) { + // 참조:https://www.codeigniter.com/user_guide/libraries/curlrequest.html?highlight=curl# + // ex:)$options = [ 'baseURI' => 'http://www.foo.com/1.0/', 'timeout' => 0, 'allow_redirects' => false, 'proxy' => '192.168.16.1:10' ] + $options = [ + 'baseURI' => $this->getServerInfo(), + 'auth' => $this->getAccountInfo(), + 'verify' => $this->isSSLVerifiy(), + 'cookie' => $this->getCookieFile(), + ]; + if ($this->_debug) { + $options['debug'] = PATHS['API'] . getenv('api.adapter.debug') ? getenv('api.adapter.debug') : "api-debug_" . date("Ymd"); + } + $this->_client = \Config\Services::curlrequest($options); + } + return $this->_client; + } + protected function requestURL(string $url, string $method, array $datas = []): object + { + $options = array(); + switch ($method) { + case 'POST': + $response = $this->getClient()->setBody($datas)->request($method, $url, $options); + break; + case 'HEAD': + break; + default: + $response = $this->getClient()->request($method, $url, $options); + break; + } + dd($response); + if ($response->getStatusCode() != 200) { + throw new \Exception(sprintf( + "오류가 발생하였습니다.\n%s\n%s", + $response->getHeaderLine('content-type'), + $response->getBody() + )); + } + return json_decode($response->getBody()); + } +}