diff --git a/app/Controllers/Cloudflare/AccountController.php b/app/Controllers/Cloudflare/AccountController.php index a51564a..159a03f 100644 --- a/app/Controllers/Cloudflare/AccountController.php +++ b/app/Controllers/Cloudflare/AccountController.php @@ -26,13 +26,7 @@ class AccountController extends MVController $this->class_name = 'Account'; helper($this->class_name); } - final protected function getMySocket(): CloudflareSocket - { - if ($this->_mySocket === null) { - $this->_mySocket = new CloudflareSocket(); - } - return $this->_mySocket; - } + final protected function getModel(): AccountModel { if ($this->_model === null) { @@ -53,7 +47,7 @@ class AccountController extends MVController } protected function create_process_submit(): AccountEntity { - $account = new Account($this->getMySocket(), $this->getModel()); + $account = new Account(); return $account->create($this->formDatas); } public function create(): RedirectResponse diff --git a/app/Controllers/Cloudflare/RecordController.php b/app/Controllers/Cloudflare/RecordController.php index 9b2670a..2d77df8 100644 --- a/app/Controllers/Cloudflare/RecordController.php +++ b/app/Controllers/Cloudflare/RecordController.php @@ -2,24 +2,27 @@ namespace App\Controllers\Cloudflare; +use App\Controllers\MVController; use Psr\Log\LoggerInterface; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\RedirectResponse; use App\Traits\AuthTrait; -use App\Models\Cloudflare\RecordModel; -use App\Models\Cloudflare\AccountModel; use App\Libraries\MySocket\CloudflareSocket; use App\Libraries\MyCloudflare\Record; +use App\Models\Cloudflare\ZoneModel; +use App\Models\Cloudflare\AccountModel; +use App\Models\Cloudflare\API\RecordModel; use App\Entities\Cloudflare\RecordEntity; -use App\Controllers\MVController; + class RecordController extends MVController { use AuthTrait; private $_model = null; private $_accountModel = null; + private $_zoneModel = null; public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { parent::initController($request, $response, $logger); @@ -27,13 +30,6 @@ class RecordController extends MVController $this->class_name = 'Record'; helper($this->class_name); } - final protected function getMySocket(): CloudflareSocket - { - if ($this->_mySocket === null) { - $this->_mySocket = new CloudflareSocket(); - } - return $this->_mySocket; - } final protected function getModel(): RecordModel { if ($this->_model === null) { @@ -48,6 +44,13 @@ class RecordController extends MVController } return $this->_accountModel; } + final protected function getZoneModel(): ZoneModel + { + if ($this->_zoneModel === null) { + $this->_zoneModel = new ZoneModel(); + } + return $this->_zoneModel; + } protected function create_init(): void { $this->fields = ['id', 'apikey']; @@ -61,11 +64,9 @@ class RecordController extends MVController } protected function create_process_submit(): RecordEntity { - $Record = new Record( - $this->getMySocket(), - $this->getModel(), - $this->getAccountModel()->getEntityByPK($this->formDatas['account_uid']) - ); + $zone_entity = $this->getZoneModel()->getEntityByPK($this->formDatas[$this->getModel()::PARENT]); + $account_entity = $this->getAccountModel()->getEntityByPK($zone_entity->getParent()); + $Record = new Record($account_entity, $zone_entity); return $Record->create($this->formDatas); } public function create(): RedirectResponse diff --git a/app/Controllers/Cloudflare/ZoneController.php b/app/Controllers/Cloudflare/ZoneController.php index 17283de..1bad8bb 100644 --- a/app/Controllers/Cloudflare/ZoneController.php +++ b/app/Controllers/Cloudflare/ZoneController.php @@ -2,6 +2,7 @@ namespace App\Controllers\Cloudflare; +use App\Controllers\MVController; use Psr\Log\LoggerInterface; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\HTTP\RequestInterface; @@ -13,7 +14,7 @@ use App\Models\Cloudflare\AccountModel; use App\Libraries\MySocket\CloudflareSocket; use App\Libraries\MyCloudflare\Zone; use App\Entities\Cloudflare\ZoneEntity; -use App\Controllers\MVController; + class ZoneController extends MVController { @@ -27,13 +28,6 @@ class ZoneController extends MVController $this->class_name = 'Zone'; helper($this->class_name); } - final protected function getMySocket(): CloudflareSocket - { - if ($this->_mySocket === null) { - $this->_mySocket = new CloudflareSocket(); - } - return $this->_mySocket; - } final protected function getModel(): ZoneModel { if ($this->_model === null) { @@ -61,11 +55,8 @@ class ZoneController extends MVController } protected function create_process_submit(): ZoneEntity { - $zone = new Zone( - $this->getMySocket(), - $this->getModel(), - $this->getAccountModel()->getEntityByPK($this->formDatas['account_uid']) - ); + $account_entity = $this->getAccountModel()->getEntityByPK($this->formDatas[$this->getModel()::PARENT]); + $zone = new Zone($account_entity); return $zone->create($this->formDatas); } public function create(): RedirectResponse diff --git a/app/Controllers/Mangboard/CrawlerController.php b/app/Controllers/Mangboard/CrawlerController.php index f080110..1c12a08 100644 --- a/app/Controllers/Mangboard/CrawlerController.php +++ b/app/Controllers/Mangboard/CrawlerController.php @@ -2,15 +2,18 @@ namespace App\Controllers\Mangboard; +use App\Controllers\CommonController; use Psr\Log\LoggerInterface; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\HTTP\RequestInterface; -use App\Models\Mangboard\UserModel; -use App\Libraries\MyStorage\MangboardStorage; -use App\Libraries\MySocket\WebSocket; + use App\Libraries\MyCrawler\Yamap; +use App\Libraries\MyCrawler\Yamoon; +use App\Libraries\MyCrawler\Sir; +use App\Libraries\MyCrawler\Inven; +use App\Models\Mangboard\UserModel; use App\Entities\Mangboard\UserEntity; -use App\Controllers\CommonController; + class CrawlerController extends CommonController { @@ -59,9 +62,7 @@ class CrawlerController extends CommonController { try { $user_entity = $this->login_process($user_id); - $mySocket = new WebSocket(getenv("yamap.host.url")); - $myStorage = new MangboardStorage($board_name, $user_entity); - $myCrawler = new Yamap($mySocket, $myStorage); + $myCrawler = new Yamap(getenv("yamap.host.url"), $board_name, $user_entity); //추가옵션 $myCrawler->isDebug = in_array('debug', $params); $myCrawler->isCopy = in_array('copy', $params); @@ -78,9 +79,7 @@ class CrawlerController extends CommonController { try { $user_entity = $this->login_process($user_id); - $mySocket = new WebSocket(getenv("yamoon.host.url")); - $myStorage = new MangboardStorage($board_name, $user_entity); - $myCrawler = new Yamap($mySocket, $myStorage); + $myCrawler = new Yamoon(getenv("yamoon.host.url"), $board_name, $user_entity); //추가옵션 $myCrawler->isDebug = in_array('debug', $params); $myCrawler->isCopy = in_array('copy', $params); @@ -97,9 +96,7 @@ class CrawlerController extends CommonController { try { $user_entity = $this->login_process($user_id); - $mySocket = new WebSocket(getenv("sir.host.url")); - $myStorage = new MangboardStorage($board_name, $user_entity); - $myCrawler = new Yamap($mySocket, $myStorage); + $myCrawler = new Sir(getenv("sir.host.url"), $board_name, $user_entity); //추가옵션 $myCrawler->isDebug = in_array('debug', $params); $myCrawler->isCopy = in_array('copy', $params); @@ -118,9 +115,7 @@ class CrawlerController extends CommonController // exit; try { $user_entity = $this->login_process($user_id); - $mySocket = new WebSocket(getenv("inven.host.url")); - $myStorage = new MangboardStorage($board_name, $user_entity); - $myCrawler = new Yamap($mySocket, $myStorage); + $myCrawler = new Inven(getenv("inven.host.url"), $board_name, $user_entity); //추가옵션 $myCrawler->isDebug = in_array('debug', $params); $myCrawler->isCopy = in_array('copy', $params); diff --git a/app/Libraries/MyCloudflare/Account.php b/app/Libraries/MyCloudflare/Account.php index 97dd986..7a44364 100644 --- a/app/Libraries/MyCloudflare/Account.php +++ b/app/Libraries/MyCloudflare/Account.php @@ -2,20 +2,22 @@ namespace App\Libraries\MyCloudflare; +use Cloudflare\API\Adapter\Guzzle; use App\Models\Cloudflare\AccountModel; use App\Libraries\MyCloudflare\MyCloudflare; use App\Entities\Cloudflare\AccountEntity; class Account extends MyCloudflare { - public function __construct($mySocket, $myStorage) + private $_myStorage = null; + public function __construct() { - parent::__construct($mySocket, $myStorage); + parent::__construct(); } final protected function getMyStorage(): AccountModel { if ($this->_myStorage === null) { - throw new \Exception("MyStorage가 정의되지 않았습니다."); + $this->_myStorage = new AccountModel(); } return $this->_myStorage; } @@ -33,19 +35,36 @@ class Account extends MyCloudflare // "legacy_flags":{"enterprise_zone_quota":{"maximum":0,"current":0,"available":0}}, // "created_on":"2017-06-26T05:44:49.470184Z"} // ] + protected function getArrayByResult($result): array + { + $formDatas[$this->getMyStorage()->getPKField()] = $result->id; + $formDatas[$this->getMyStorage()->getTitleField()] = $result->name; + $formDatas['type'] = $result->type; + $formDatas['status'] = 'use'; + $formDatas['updated_at'] = $result->created_on; + $formDatas['created_at'] = $result->created_on; + return $formDatas; + } public function create(array $formDatas): AccountEntity { //Socket용 - $cf = $this->getMySocket()->getAccount($formDatas['apikey'])->addAccount($formDatas['id']); + $cf = $this->getMySocket()->request($formDatas['apikey']) + ->post('accounts', [ + 'name' => $formDatas[$this->getMyStorage()->getTitleField()], + 'type' => $formDatas['type'], + ]); + $cf = json_decode($cf->getBody()); + if (!$cf->success) { + throw new \Exception(__FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); + } //Storage용 - $formDatas[$this->getMyStorage()->PK()] = $cf->id; - $formDatas[$this->getMyStorage()->getTitleField()] = $cf->name; - $formDatas['type'] = $cf->type; - $formDatas['status'] = 'use'; - $formDatas['updated_at'] = $cf->created_on; - $formDatas['created_at'] = $cf->created_on; + $formDatas = $this->getArrayByResult($cf->result); $entity = $this->getMyStorage()->create($formDatas); log_message("notice", "Account::" . __FUNCTION__ . "=> 작업을 완료하였습니다."); return $entity; } + protected function reload_entity($cf): AccountEntity + { + return $this->getMyStorage()->modify(new AccountEntity, $this->getArrayByResult($cf)); + } } diff --git a/app/Libraries/MyCloudflare/MyCloudflare.php b/app/Libraries/MyCloudflare/MyCloudflare.php index bbcc024..c37c841 100644 --- a/app/Libraries/MyCloudflare/MyCloudflare.php +++ b/app/Libraries/MyCloudflare/MyCloudflare.php @@ -2,26 +2,66 @@ namespace App\Libraries\MyCloudflare; +use Cloudflare\API\Adapter\Guzzle; use App\Libraries\MySocket\CloudflareSocket; use App\Libraries\CommonLibrary; abstract class MyCloudflare extends CommonLibrary { private $_mySocket = null; - protected $_myStorage = null; - protected function __construct($mySocket, $myStorage) + protected function __construct() { parent::__construct(); - $this->_mySocket = $mySocket; - $this->_myStorage = $myStorage; } + abstract protected function getArrayByResult($result): array; + abstract protected function reload_entity($cf): mixed; abstract protected function getMyStorage(): mixed; - //-----------------------필수항목-------------------// final protected function getMySocket(): CloudflareSocket { if ($this->_mySocket === null) { - throw new \Exception("MySocket이 정의되지 않았습니다."); + $this->_mySocket = new CloudflareSocket(); } return $this->_mySocket; } + //-----------------------필수항목-------------------// + final protected function reload_entitys(string $parent, array $cfs): array + { + $entity_uids = []; + if (count($cfs)) { + $cnt = 1; + foreach ($cfs as $cf) { + $entity = $this->reload_entity($cf); + $entity_uids[] = $entity->getPK(); + log_message("debug", "{$cnt}번째: {$entity->getTitle()} 저장"); + $cnt++; + } + //부모키를 기준으로 CF에 존재하지 않는 데이터 삭제용 + $this->getMyStorage()->where($this->getMyStorage()::PARENT, $parent); + $this->getMyStorage()->whereNotIn($this->getMyStorage()->getPKField(), $entity_uids); + $this->getMyStorage()->delete(); + } + return $entity_uids; + } + final protected function reload_cfs(Guzzle $request, $uri): array + { + $page = 1; //Page는 1부터 시작해야함 + $perpage_max = getenv("cfmgr.request.perpage.max"); + $cfs = []; + do { + $query = [ + 'page' => $page, + 'per_page' => $perpage_max, + 'match' => 'all' + ]; + $cf = $request->get($uri, $query); + $cf = json_decode($cf->getBody()); + if (!$cf->success) { + throw new \Exception(__FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); + } + $cfs = [$cfs, ...$cf->result]; + //Loop 제한 : 한페이지에서 가져온 갯수가 perpage_max보다 적다는것은 더이상 다음페이지기 없으므로 0로 종료시키기 위함 + $page = count($cf->result) < $perpage_max ? 0 : $page + 1; + } while (0 < $page); + return $cfs; + } } diff --git a/app/Libraries/MyCloudflare/Record.php b/app/Libraries/MyCloudflare/Record.php index c079cbb..88622eb 100644 --- a/app/Libraries/MyCloudflare/Record.php +++ b/app/Libraries/MyCloudflare/Record.php @@ -1,127 +1,129 @@ _model = new \App\Models\Cloudflare\API\RecordModel(); - } - final protected function setAdapter() - { - if (!is_null($this->_adapter)) { - throw new \Exception("Adapter가 이미 지정되었습니다."); + private $_myStorage = null; + private $_account_entity = null; + private $_zone_entity = null; + public function __construct(AccountEntity $account_entity, ZoneEntity $zone_entity) + { + parent::__construct(); + $this->_account_entity = $account_entity; + $this->_zone_entity = $zone_entity; } - $accountModel = new \App\Models\Cloudflare\API\AccountModel(); - $account = $accountModel->getEntity($this->getParent()->getParentFieldData()); - $authModel = new \App\Models\Cloudflare\API\AuthModel(); - $auth = $authModel->getEntity($account->getParentFieldData()); - $apikey = new \Cloudflare\API\Auth\APIKey($auth->getAuthId(), $auth->getAuthKey()); - $this->_adapter = new \Cloudflare\API\Adapter\Guzzle($apikey); - // throw new \Exception(var_export($this->_adapter, true)); - } - public function getClassName() - { - return 'Record'; - } - protected function getEntityByResult(\stdClass $cfResult): \App\Entities\Cloudflare\API\RecordEntity - { - // throw new \Exception(var_export($cfResult, true)); - $entity = is_null($this->_entity) ? new \App\Entities\Cloudflare\API\RecordEntity() : $this->_entity; - // throw new \Exception(var_export($cfResult, true)); - $entity->uid = $cfResult->id; - $entity->zone_uid = $cfResult->zone_id; - $entity->host = $cfResult->name; - $entity->type = $cfResult->type; - $entity->content = $cfResult->content; - $entity->ttl = (int) $cfResult->ttl; - $entity->proxiable = $cfResult->proxiable ? "on" : "off"; - $entity->proxied = $cfResult->proxied ? "on" : "off"; - $entity->locked = "on"; - if (isset($cfResult->locked) && $cfResult->locked) { - $entity->locked = "off"; + final protected function getMyStorage(): RecordModel + { + if ($this->_myStorage === null) { + $this->_myStorage = new RecordModel(); + } + return $this->_myStorage; } - // $entity->updated_at = $cfResult->modified_on; - $entity->created_at = $cfResult->created_on; - // throw new \Exception(var_export($cfResult, true)); - return $entity; - } - - public function insert(array $fieldDatas): \App\Entities\Cloudflare\API\RecordEntity - { - //TTL값은 CDN(proxied)가 사용함일때는 무조건 1, 않함일때는 120이 적용 - $options = [ - 'name' => $fieldDatas['host'], - 'type' => $fieldDatas['type'], - 'content' => $fieldDatas['content'], - 'proxied' => isset($fieldDatas['proxied']) && $fieldDatas['proxied'] === 'on' ? true : false - ]; - // throw new \Exception(var_export($options, true)); - $cfResult = $this->getAdapter()->post('zones/' . $this->getParent()->getPrimaryKey() . '/dns_records', $options); - $cfResult = json_decode($cfResult->getBody()); - if (!$cfResult->success) { - throw new \Exception(var_export($cfResult, true)); + protected function getRequest(): Guzzle + { + return $this->getMySocket()->request($this->_account_entity->getAPIKey()); } - $entity = $this->getEntityByResult($cfResult->result); - Log::add("warning", "Record API: {$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다."); - return $entity; - } - public function update(\App\Entities\Cloudflare\API\RecordEntity $entity, array $fieldDatas): \App\Entities\Cloudflare\API\RecordEntity - { - //TTL값은 CDN(proxied)가 사용함일때는 무조건 1, 않함일때는 120이 적용 - $options = [ - 'type' => isset($fieldDatas['type']) ? $fieldDatas['type'] : $entity->type, - 'name' => isset($fieldDatas['host']) ? $fieldDatas['host'] : $entity->host, - 'content' => isset($fieldDatas['content']) ? $fieldDatas['content'] : $entity->content, - 'proxied' => $entity->proxied == 'on' ? true : false, - 'ttl' => intval($entity->ttl) - ]; - //변경작업: 2024-08-09 - if (isset($fieldDatas['proxied']) && $fieldDatas['proxied'] === 'on') { - $options['proxied'] = true; - $options['ttl'] = 1; - } elseif (isset($fieldDatas['proxied']) && $fieldDatas['proxied'] === 'off') { - $options['proxied'] = false; - $options['ttl'] = 120; + protected function getArrayByResult($result): array + { + $formDatas[$this->getMyStorage()->getPKField()] = $result->id; + $formDatas[$this->getMyStorage()::PARENT] = $result->zone_id; + $formDatas[$this->getMyStorage()->getTitleField()] = $result->name; + $formDatas['type'] = $result->type; + $formDatas['content'] = $result->content; + $formDatas['ttl'] = (int) $result->ttl; + $formDatas['proxiable'] = $result->proxiable ? "on" : "off"; + $formDatas['proxied'] = $result->proxied ? "on" : "off"; + $formDatas['locked'] = "on"; + if (isset($result->locked) && $result->locked) { + $formDatas['locked'] = "off"; + } + // $formDatas['updated_at'] = $cfResult->modified_on; + $formDatas['created_at'] = $result->created_on; + return $formDatas; } - //dd($options); - // throw new \Exception(var_export($fieldDatas, true) . "
" . var_export($options, true)); - $cfResult = $this->getAdapter()->put('zones/' . $this->getParent()->getPrimaryKey() . '/dns_records/' . $entity->getPrimaryKey(), $options); - $cfResult = json_decode($cfResult->getBody()); - if (!$cfResult->success) { - throw new \Exception(var_export($cfResult, true)); + public function create(array $formDatas): RecordEntity + { + //Socket용 + //도메인생성을 위해 Cloudflare에 전송 + $cf = $this->getRequest()->post('zones/' . $this->_zone_entity->getPK() . '/dns_records', [ + 'name' => $formDatas['host'], + 'type' => $formDatas['type'], + 'content' => $formDatas['content'], + 'proxied' => isset($formDatas['proxied']) && $formDatas['proxied'] === 'on' ? true : false + ]); + $cf = json_decode($cf->getBody()); + if (!$cf->success) { + throw new \Exception("Record:" . __FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); + } + //Storage용 + $formDatas = $this->getArrayByResult($cf->result); + $entity = $this->$this->getMyStorage()->create($formDatas); + log_message("notice", "Record:" . __FUNCTION__ . "=> 작업을 완료하였습니다."); + return $entity; } - Log::add("warning", "Record API: {$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다."); - return $this->getEntityByResult($cfResult->result); - } - public function delete(\App\Entities\Cloudflare\API\RecordEntity $entity) - { - $cfResult = $this->getAdapter()->delete('zones/' . $this->getParent()->getPrimaryKey() . '/dns_records/' . $entity->getPrimaryKey()); - $cfResult = json_decode($cfResult->getBody()); - if (!$cfResult->success) { - throw new \Exception(var_export($cfResult, true)); + public function update(RecordEntity $entity, array $formDatas): RecordEntity + { + //TTL값은 CDN(proxied)가 사용함일때는 무조건 1, 않함일때는 120이 적용 + $datas = [ + 'type' => isset($formDatas['type']) ? $formDatas['type'] : $entity->type, + 'name' => isset($formDatas['host']) ? $formDatas['host'] : $entity->host, + 'content' => isset($formDatas['content']) ? $formDatas['content'] : $entity->content, + 'proxied' => $entity->proxied == 'on' ? true : false, + 'ttl' => intval($entity->ttl) + ]; + //변경작업: 2024-08-09 + 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; + } + $cf = $this->getRequest()->put('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)); + } + log_message("notice", "Record:" . __FUNCTION__ . "=> 작업을 완료하였습니다."); + return $entity; } - Log::add("warning", "Record API: {$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다."); - } - public function sync(\App\Entities\Cloudflare\API\RecordEntity $entity): \App\Entities\Cloudflare\API\RecordEntity - { - $cfResult = $this->getAdapter()->get('zones/' . $this->getParent()->getPrimaryKey() . '/dns_records/' . $entity->getPrimaryKey()); - $cfResult = json_decode($cfResult->getBody()); - if (!$cfResult->success) { - throw new \Exception(var_export($cfResult, true)); + public function delete(RecordEntity $entity): void + { + $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)); + } + log_message("notice", "Record:" . __FUNCTION__ . "=> 작업을 완료하였습니다."); + } + public function sync(RecordEntity $entity): RecordEntity + { + $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)); + } + log_message("notice", "Record:" . __FUNCTION__ . "=> 작업을 완료하였습니다."); + $formDatas = $this->getArrayByResult($cf->result); + return $this->$this->getMyStorage()->create($formDatas); + } + protected function reload_entity($cf): RecordEntity + { + return $this->getMyStorage()->modify(new RecordEntity, $this->getArrayByResult($cf)); + } + public function reload(): void + { + $cfs = $this->reload_cfs($this->getRequest(), 'zones/' . $this->_zone_entity->getPK() . '/dns_records'); + log_message("notice", "-----{$this->_zone_entity->getTitle()} 처리[" . count($cfs) . "개] 시작-----"); + $entitys = $this->reload_entitys($this->_zone_entity->getPK(), $cfs); + log_message("notice", "-----{$this->_zone_entity->getTitle()} DB 처리[" . count($entitys) . "개] 완료-----"); } - Log::add("warning", "Record API: {$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다."); - return $this->getEntityByResult($cfResult->result); - } - protected function getCFResults_List(int $page): array - { - $this->_endPoint = is_null($this->_endPoint) ? new \Cloudflare\API\Endpoints\DNS($this->getAdapter()) : $this->_endPoint; - return $this->_endPoint->listRecords($this->getParent()->getPrimaryKey(), '', '', '', $page, CF_ADAPTER_PERPAGE_MAX)->result; - } } diff --git a/app/Libraries/MyCloudflare/Record_old.php b/app/Libraries/MyCloudflare/Record_old.php new file mode 100644 index 0000000..c079cbb --- /dev/null +++ b/app/Libraries/MyCloudflare/Record_old.php @@ -0,0 +1,127 @@ +_model = new \App\Models\Cloudflare\API\RecordModel(); + } + final protected function setAdapter() + { + if (!is_null($this->_adapter)) { + throw new \Exception("Adapter가 이미 지정되었습니다."); + } + $accountModel = new \App\Models\Cloudflare\API\AccountModel(); + $account = $accountModel->getEntity($this->getParent()->getParentFieldData()); + $authModel = new \App\Models\Cloudflare\API\AuthModel(); + $auth = $authModel->getEntity($account->getParentFieldData()); + $apikey = new \Cloudflare\API\Auth\APIKey($auth->getAuthId(), $auth->getAuthKey()); + $this->_adapter = new \Cloudflare\API\Adapter\Guzzle($apikey); + // throw new \Exception(var_export($this->_adapter, true)); + } + public function getClassName() + { + return 'Record'; + } + protected function getEntityByResult(\stdClass $cfResult): \App\Entities\Cloudflare\API\RecordEntity + { + // throw new \Exception(var_export($cfResult, true)); + $entity = is_null($this->_entity) ? new \App\Entities\Cloudflare\API\RecordEntity() : $this->_entity; + // throw new \Exception(var_export($cfResult, true)); + $entity->uid = $cfResult->id; + $entity->zone_uid = $cfResult->zone_id; + $entity->host = $cfResult->name; + $entity->type = $cfResult->type; + $entity->content = $cfResult->content; + $entity->ttl = (int) $cfResult->ttl; + $entity->proxiable = $cfResult->proxiable ? "on" : "off"; + $entity->proxied = $cfResult->proxied ? "on" : "off"; + $entity->locked = "on"; + if (isset($cfResult->locked) && $cfResult->locked) { + $entity->locked = "off"; + } + // $entity->updated_at = $cfResult->modified_on; + $entity->created_at = $cfResult->created_on; + // throw new \Exception(var_export($cfResult, true)); + return $entity; + } + + public function insert(array $fieldDatas): \App\Entities\Cloudflare\API\RecordEntity + { + //TTL값은 CDN(proxied)가 사용함일때는 무조건 1, 않함일때는 120이 적용 + $options = [ + 'name' => $fieldDatas['host'], + 'type' => $fieldDatas['type'], + 'content' => $fieldDatas['content'], + 'proxied' => isset($fieldDatas['proxied']) && $fieldDatas['proxied'] === 'on' ? true : false + ]; + // throw new \Exception(var_export($options, true)); + $cfResult = $this->getAdapter()->post('zones/' . $this->getParent()->getPrimaryKey() . '/dns_records', $options); + $cfResult = json_decode($cfResult->getBody()); + if (!$cfResult->success) { + throw new \Exception(var_export($cfResult, true)); + } + $entity = $this->getEntityByResult($cfResult->result); + Log::add("warning", "Record API: {$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다."); + return $entity; + } + public function update(\App\Entities\Cloudflare\API\RecordEntity $entity, array $fieldDatas): \App\Entities\Cloudflare\API\RecordEntity + { + //TTL값은 CDN(proxied)가 사용함일때는 무조건 1, 않함일때는 120이 적용 + $options = [ + 'type' => isset($fieldDatas['type']) ? $fieldDatas['type'] : $entity->type, + 'name' => isset($fieldDatas['host']) ? $fieldDatas['host'] : $entity->host, + 'content' => isset($fieldDatas['content']) ? $fieldDatas['content'] : $entity->content, + 'proxied' => $entity->proxied == 'on' ? true : false, + 'ttl' => intval($entity->ttl) + ]; + //변경작업: 2024-08-09 + if (isset($fieldDatas['proxied']) && $fieldDatas['proxied'] === 'on') { + $options['proxied'] = true; + $options['ttl'] = 1; + } elseif (isset($fieldDatas['proxied']) && $fieldDatas['proxied'] === 'off') { + $options['proxied'] = false; + $options['ttl'] = 120; + } + //dd($options); + // throw new \Exception(var_export($fieldDatas, true) . "
" . var_export($options, true)); + $cfResult = $this->getAdapter()->put('zones/' . $this->getParent()->getPrimaryKey() . '/dns_records/' . $entity->getPrimaryKey(), $options); + $cfResult = json_decode($cfResult->getBody()); + if (!$cfResult->success) { + throw new \Exception(var_export($cfResult, true)); + } + Log::add("warning", "Record API: {$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다."); + return $this->getEntityByResult($cfResult->result); + } + public function delete(\App\Entities\Cloudflare\API\RecordEntity $entity) + { + $cfResult = $this->getAdapter()->delete('zones/' . $this->getParent()->getPrimaryKey() . '/dns_records/' . $entity->getPrimaryKey()); + $cfResult = json_decode($cfResult->getBody()); + if (!$cfResult->success) { + throw new \Exception(var_export($cfResult, true)); + } + Log::add("warning", "Record API: {$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다."); + } + public function sync(\App\Entities\Cloudflare\API\RecordEntity $entity): \App\Entities\Cloudflare\API\RecordEntity + { + $cfResult = $this->getAdapter()->get('zones/' . $this->getParent()->getPrimaryKey() . '/dns_records/' . $entity->getPrimaryKey()); + $cfResult = json_decode($cfResult->getBody()); + if (!$cfResult->success) { + throw new \Exception(var_export($cfResult, true)); + } + Log::add("warning", "Record API: {$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다."); + return $this->getEntityByResult($cfResult->result); + } + protected function getCFResults_List(int $page): array + { + $this->_endPoint = is_null($this->_endPoint) ? new \Cloudflare\API\Endpoints\DNS($this->getAdapter()) : $this->_endPoint; + return $this->_endPoint->listRecords($this->getParent()->getPrimaryKey(), '', '', '', $page, CF_ADAPTER_PERPAGE_MAX)->result; + } +} diff --git a/app/Libraries/MyCloudflare/Zone.php b/app/Libraries/MyCloudflare/Zone.php index 6f57b45..16feea1 100644 --- a/app/Libraries/MyCloudflare/Zone.php +++ b/app/Libraries/MyCloudflare/Zone.php @@ -3,63 +3,67 @@ namespace App\Libraries\MyCloudflare; use App\Models\Cloudflare\ZoneModel; -use App\Models\Cloudflare\AccountModel; use App\Libraries\MyCloudflare\MyCloudflare; use App\Entities\Cloudflare\ZoneEntity; use App\Entities\Cloudflare\AccountEntity; +use Cloudflare\API\Adapter\Guzzle; + class Zone extends MyCloudflare { + private $_myStorage = null; private $_account_entity = null; - public function __construct($mySocket, $myStorage, AccountEntity $account_entity) + public function __construct(AccountEntity $account_entity) { - parent::__construct($mySocket, $myStorage); + parent::__construct(); $this->_account_entity = $account_entity; } final protected function getMyStorage(): ZoneModel { if ($this->_myStorage === null) { - throw new \Exception("MyStorage가 정의되지 않았습니다."); + $this->_myStorage = new ZoneModel(); } return $this->_myStorage; } - private function getArrayByResult($cf): array + protected function getRequest(): Guzzle { - $formDatas['uid'] = $cf->id; - $formDatas['account_uid'] = $cf->account->id; - $formDatas['domain'] = $cf->name; - $formDatas['status'] = $cf->status; - //$formDatas['type'] = $cf->type; // full 이게있는데 뭔지 잘모름 + return $this->getMySocket()->request($this->_account_entity->getAPIKey()); + } + protected function getArrayByResult($result): array + { + $formDatas[$this->getMyStorage()->getPKField()] = $result->id; + $formDatas[$this->getMyStorage()::PARENT] = $result->account->id; + $formDatas[$this->getMyStorage()->getTitleField()] = $result->name; + $formDatas['status'] = $result->status; + //$formDatas['type'] = $result->type; // full 이게있는데 뭔지 잘모름 $formDatas['name_servers'] = 'none'; - if (isset($cf->name_servers)) { - $formDatas['name_servers'] = is_array($cf->name_servers) ? + if (isset($result->name_servers)) { + $formDatas['name_servers'] = is_array($result->name_servers) ? implode( ',', - $cf->name_servers - ) : $cf->name_servers; + $result->name_servers + ) : $result->name_servers; } $formDatas['original_name_servers'] = 'none'; - if (isset($cf->original_name_servers)) { - $formDatas['original_name_servers'] = is_array($cf->original_name_servers) ? + if (isset($result->original_name_servers)) { + $formDatas['original_name_servers'] = is_array($result->original_name_servers) ? implode( ',', - $cf->original_name_servers - ) : $cf->original_name_servers; + $result->original_name_servers + ) : $result->original_name_servers; } - $formDatas['updated_at'] = $cf->modified_on; - $formDatas['created_at'] = $cf->created_on; - $formDatas['plan'] = $cf->plan->name; + $formDatas['updated_at'] = $result->modified_on; + $formDatas['created_at'] = $result->created_on; + $formDatas['plan'] = $result->plan->name; return $formDatas; } - //Cfzone에서 가져온 값을 zone에 setting final public function getCFSetting(ZoneEntity $entity): ZoneEntity { - $cf = $this->getMySocket()->getAdapter($this->_account_entity->getAPIKey()) - ->patch('zones/' . $entity->getPK() . '/settings/'); + $cf = $this->getRequest()->patch('zones/' . $entity->getPK() . '/settings/'); $cf = json_decode($cf->getBody()); if (!$cf->success) { - throw new \Exception(__FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); + throw new \Exception("Zone:" . __FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); } foreach ($cf->result as $cf) { switch ($cf->id) { @@ -78,36 +82,28 @@ class Zone extends MyCloudflare } final public function setCFSetting(ZoneEntity $entity, string $field, string $value): ZoneEntity { - $cf = $this->getMySocket()->getAdapter($this->_account_entity->getAPIKEY()) - ->patch('zones/' . $entity->getPK() . '/settings/' . $field, array('value' => $value)); + $cf = $this->getRequest()->patch('zones/' . $entity->getPK() . '/settings/' . $field, array('value' => $value)); $cf = json_decode($cf->getBody()); if (!$cf->success || $cf->result->id !== $field) { - throw new \Exception(__FUNCTION__ . "에서 {$field}->{$value} 변경실패:\n" . var_export($cf, true)); + throw new \Exception("Zone:" . __FUNCTION__ . "에서 {$field}->{$value} 변경실패:\n" . var_export($cf, true)); } //최종 결과값은 body->result->id='필드명',body->result->value='on/off'이런식으로 받음 $entity->$field = $cf->result->value; return $entity; } - //Result 형태 - public function create(array $formDatas, $jump_start = false): ZoneEntity { //Socket용 - // $cf = $this->getMySocket()->getZone($this->_account_entity->getAPIKEY()) - // ->addZone($formDatas[ZoneModel::TITLE], $jump_start, $this->_account_entity->getPK()); //도메인생성을 위해 Cloudflare에 전송 - $options = [ + $cf = $this->getRequest()->post('zones/', [ 'accountId' => $this->_account_entity->getPK(), 'name' => $formDatas['domain'], 'jump_start' => $jump_start, - ]; - $cf = $this->getMySocket()->getAdapter($this->_account_entity->getAPIKey()) - ->post('zones/', $options); + ]); $cf = json_decode($cf->getBody()); if (!$cf->success) { - throw new \Exception(__FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); + throw new \Exception("Zone:" . __FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); } - //Storage용 $formDatas = $this->getArrayByResult($cf->result); $entity = $this->$this->getMyStorage()->create($formDatas); @@ -115,44 +111,47 @@ class Zone extends MyCloudflare $entity = $this->setCFSetting($entity, 'ipv6', 'off'); $entity = $this->setCFSetting($entity, 'development_mode', 'off'); $entity = $this->setCFSetting($entity, 'security_level', 'medium'); - log_message("notice", "Zone::" . __FUNCTION__ . "=> 작업을 완료하였습니다."); + log_message("notice", "Zone:" . __FUNCTION__ . "=> 작업을 완료하였습니다."); return $entity; } - public function update(ZoneEntity $entity, array $fieldDatas): ZoneEntity + public function update(ZoneEntity $entity, array $formDatas): ZoneEntity { //ipv6 , //development_mode , //security_level - foreach ($fieldDatas as $field => $value) { + foreach ($formDatas as $field => $value) { $entity = $this->setCFSetting($entity, $field, $value); } - log_message("notice", "Zone::" . __FUNCTION__ . "=> 작업을 완료하였습니다."); + log_message("notice", "Zone:" . __FUNCTION__ . "=> 작업을 완료하였습니다."); return $entity; } public function delete(ZoneEntity $entity): void { - $cf = $this->getMySocket()->getAdapter($this->_account_entity->getAPIKey()) - ->delete('zones/' . $entity->getPK()); + $cf = $this->getRequest()->delete('zones/' . $entity->getPK()); $cf = json_decode($cf->getBody()); if (!$cf->success) { - throw new \Exception(__FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); + throw new \Exception("Zone:" . __FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); } - log_message("notice", "Zone::" . __FUNCTION__ . "=> 작업을 완료하였습니다."); + log_message("notice", "Zone:" . __FUNCTION__ . "=> 작업을 완료하였습니다."); } public function sync(ZoneEntity $entity): ZoneEntity { - $cf = $this->getMySocket()->getAdapter($this->_account_entity->getAPIKey()) - ->get('zones/' . $entity->getPK()); + $cf = $this->getRequest()->get('zones/' . $entity->getPK()); $cf = json_decode($cf->getBody()); if (!$cf->success) { - throw new \Exception(__FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); + throw new \Exception("Zone:" . __FUNCTION__ . "에서 실패:\n" . var_export($cf, true)); } - log_message("notice", "Zone::" . __FUNCTION__ . "=> 작업을 완료하였습니다."); + log_message("notice", "Zone:" . __FUNCTION__ . "=> 작업을 완료하였습니다."); $formDatas = $this->getArrayByResult($cf->result); return $this->$this->getMyStorage()->create($formDatas); } - // protected function getCFResults_List(int $page): array - // { - // $this->_endPoint = is_null($this->_endPoint) ? new \Cloudflare\API\Endpoints\Zones($this->getAdapter()) : $this->_endPoint; - // return $this->_endPoint->listZones('', '', $page, CF_ADAPTER_PERPAGE_MAX)->result; - // } - public function reload() {} + protected function reload_entity($cf): ZoneEntity + { + return $this->getMyStorage()->modify(new ZoneEntity, $this->getArrayByResult($cf)); + } + public function reload(): void + { + $cfs = $this->reload_cfs($this->getRequest(), 'zones'); + log_message("notice", "-----{$this->_account_entity->getTitle()} 처리[" . count($cfs) . "개] 시작-----"); + $entitys = $this->reload_entitys($this->_account_entity->getPK(), $cfs); + log_message("notice", "-----{$this->_account_entity->getTitle()} DB 처리[" . count($entitys) . "개] 완료-----"); + } } diff --git a/app/Libraries/MyCrawler/Inven.php b/app/Libraries/MyCrawler/Inven.php index 5504ff0..f3329e0 100644 --- a/app/Libraries/MyCrawler/Inven.php +++ b/app/Libraries/MyCrawler/Inven.php @@ -3,12 +3,13 @@ namespace App\Libraries\MyCrawler; use Symfony\Component\DomCrawler\Crawler; +use App\Entities\Mangboard\UserEntity; class Inven extends MyCrawler { - public function __construct($mySocket, $myStorage) + public function __construct(string $host, string $board_name, UserEntity $user_entity) { - parent::__construct($mySocket, $myStorage); + parent::__construct($host, $board_name, $user_entity); } protected function getUrlByMediaType(Crawler $node, string $media_type, string $attr): null|string { diff --git a/app/Libraries/MyCrawler/MyCrawler.php b/app/Libraries/MyCrawler/MyCrawler.php index 06ae68a..a9ccdb0 100644 --- a/app/Libraries/MyCrawler/MyCrawler.php +++ b/app/Libraries/MyCrawler/MyCrawler.php @@ -2,45 +2,48 @@ namespace App\Libraries\MyCrawler; -use App\Entities\Mangboard\BoardEntity; -use App\Entities\Mangboard\BoardsEntity; -use App\Libraries\CommonLibrary; -use App\Libraries\MySocket\WebSocket; -use App\Libraries\MyStorage\MangboardStorage; -use App\Models\Mangboard\BoardModel; -use App\Models\Mangboard\BoardsModel; -use App\Traits\FileTrait; use Symfony\Component\DomCrawler\Crawler; +use App\Traits\FileTrait; +use App\Models\Mangboard\BoardsModel; +use App\Models\Mangboard\BoardModel; +use App\Libraries\MyStorage\MangboardStorage; +use App\Libraries\MySocket\WebSocket; +use App\Libraries\CommonLibrary; +use App\Entities\Mangboard\UserEntity; +use App\Entities\Mangboard\BoardsEntity; +use App\Entities\Mangboard\BoardEntity; abstract class MyCrawler extends CommonLibrary { use FileTrait; + private $_host = ""; + private $_board_name = ""; + private $_user_entity = null; private $_mySocket = null; private $_myStorage = null; private $_board_model = null; private $_user_model = null; - private $_user_entity = null; private $_boards_entity = null; - private $_board_name = ""; - protected function __construct($mySocket, $myStorage) + protected function __construct(string $host, string $board_name, UserEntity $user_entity) { parent::__construct(); - $this->_mySocket = $mySocket; - $this->_myStorage = $myStorage; + $this->_host = $host; + $this->_board_name = $board_name; + $this->_user_entity = $user_entity; } abstract protected function getDetailSelector(array $listInfo): array; //-----------------------필수항목-------------------// final protected function getMySocket(): WebSocket { if ($this->_mySocket === null) { - throw new \Exception("MySocket이 정의되지 않았습니다."); + $this->_mySocket = new WebSocket($this->_host); } return $this->_mySocket; } final protected function getMyStorage(): MangboardStorage { if ($this->_myStorage === null) { - throw new \Exception("MyStorage가 정의되지 않았습니다."); + $this->_myStorage = new MangboardStorage($this->_board_name, $this->_user_entity); } return $this->_myStorage; } diff --git a/app/Libraries/MyCrawler/Sir.php b/app/Libraries/MyCrawler/Sir.php index 87e9380..9143ca9 100644 --- a/app/Libraries/MyCrawler/Sir.php +++ b/app/Libraries/MyCrawler/Sir.php @@ -2,14 +2,15 @@ namespace App\Libraries\MyCrawler; -use DateTime; use Symfony\Component\DomCrawler\Crawler; +use DateTime; +use App\Entities\Mangboard\UserEntity; class Sir extends MyCrawler { - public function __construct($mySocket, $myStorage) + public function __construct(string $host, string $board_name, UserEntity $user_entity) { - parent::__construct($mySocket, $myStorage); + parent::__construct($host, $board_name, $user_entity); } protected function changeURLByCrawler(string $url): string { diff --git a/app/Libraries/MyCrawler/Yamap.php b/app/Libraries/MyCrawler/Yamap.php index 203728e..598d714 100644 --- a/app/Libraries/MyCrawler/Yamap.php +++ b/app/Libraries/MyCrawler/Yamap.php @@ -2,13 +2,14 @@ namespace App\Libraries\MyCrawler; +use App\Entities\Mangboard\UserEntity; use Symfony\Component\DomCrawler\Crawler; class Yamap extends MyCrawler { - public function __construct($mySocket, $myStorage) + public function __construct(string $host, string $board_name, UserEntity $user_entity) { - parent::__construct($mySocket, $myStorage); + parent::__construct($host, $board_name, $user_entity); } protected function getDetailSelector(array $listInfo): array { diff --git a/app/Libraries/MyCrawler/Yamoon.php b/app/Libraries/MyCrawler/Yamoon.php index 3d50614..88fd1ff 100644 --- a/app/Libraries/MyCrawler/Yamoon.php +++ b/app/Libraries/MyCrawler/Yamoon.php @@ -3,12 +3,13 @@ namespace App\Libraries\MyCrawler; use Symfony\Component\DomCrawler\Crawler; +use App\Entities\Mangboard\UserEntity; class Yamoon extends MyCrawler { - public function __construct($mySocket, $myStorage) + public function __construct(string $host, string $board_name, UserEntity $user_entity) { - parent::__construct($mySocket, $myStorage); + parent::__construct($host, $board_name, $user_entity); } //작성내용 //
diff --git a/app/Libraries/MySocket/CloudflareSocket.php b/app/Libraries/MySocket/CloudflareSocket.php index 95152b2..c16d7f6 100644 --- a/app/Libraries/MySocket/CloudflareSocket.php +++ b/app/Libraries/MySocket/CloudflareSocket.php @@ -2,14 +2,15 @@ namespace App\Libraries\MySocket; -use Cloudflare\API\Endpoints\Zones; -use Cloudflare\API\Endpoints\DNS; -use Cloudflare\API\Endpoints\Accounts; + use Cloudflare\API\Auth\APIKey; use Cloudflare\API\Adapter\Guzzle; use App\Models\Cloudflare\AccountModel; use App\Libraries\CommonLibrary; -use App\Entities\Cloudflare\AccountEntity; +// use App\Entities\Cloudflare\AccountEntity; +// use Cloudflare\API\Endpoints\Zones; +// use Cloudflare\API\Endpoints\DNS; +// use Cloudflare\API\Endpoints\Accounts; class CloudflareSocket extends CommonLibrary { @@ -18,7 +19,6 @@ class CloudflareSocket extends CommonLibrary private static int $_request_timewait = 60; private $_accountModel = null; private $_clients = []; - protected $_apikey = ""; public function __construct() { parent::__construct(); @@ -40,7 +40,7 @@ class CloudflareSocket extends CommonLibrary ); } } - public function getAdapter(string $apikey): Guzzle + public function request(string $apikey): Guzzle { if (!array_key_exists($apikey, $this->_clients)) { throw new \Exception(+__FUNCTION__ . " => {$apikey}에 해당하는 Adapter를 찾을수 없습니다."); @@ -54,16 +54,16 @@ class CloudflareSocket extends CommonLibrary self::$_request++; return $this->_clients[$apikey]; } - public function getAccount(string $apikey): Accounts - { - return new Accounts($this->getAdapter($apikey)); - } - public function getZone(string $apikey): Zones - { - return new Zones($this->getAdapter($apikey)); - } - public function getRecord(string $apikey): DNS - { - return new DNS($this->getAdapter($apikey)); - } + // public function getAccount(string $apikey): Accounts + // { + // return new Accounts($this->request($apikey)); + // } + // public function getZone(string $apikey): Zones + // { + // return new Zones($this->request($apikey)); + // } + // public function getRecord(string $apikey): DNS + // { + // return new DNS($this->request($apikey)); + // } }