93 lines
3.4 KiB
PHP
93 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\CLI;
|
|
|
|
use App\Controllers\BaseController;
|
|
use App\Entities\Cloudflare\AccountEntity;
|
|
use App\Entities\Cloudflare\AuthEntity;
|
|
use App\Entities\Cloudflare\ZoneEntity;
|
|
use App\Services\Cloudflare\AccountService;
|
|
use App\Services\Cloudflare\ZoneService;
|
|
use App\Services\Cloudflare\RecordService;
|
|
use App\Services\Cloudflare\FirewallService;
|
|
use App\Models\Cloudflare\AuthModel;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class Cloudflare extends BaseController
|
|
{
|
|
private $_db = null;
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
parent::initController($request, $response, $logger);
|
|
// $this->_db = \Config\Database::connect();
|
|
}
|
|
private function auth_process(mixed $uid = false): array
|
|
{
|
|
$authModel = model(AuthModel::class);
|
|
if (is_numeric($uid) && $uid > 0) {
|
|
$authModel->where(AuthModel::PK, intval($uid));
|
|
} else {
|
|
$authModel->where('status', DEFAULTS["STATUS"]);
|
|
}
|
|
$entitys = $authModel->getEntitys();
|
|
log_message("debug", $authModel->getLastQuery());
|
|
return $entitys;
|
|
}
|
|
private function account_process(AuthEntity $auth_entity): array
|
|
{
|
|
$account = new AccountService();
|
|
return $account->reload($auth_entity);
|
|
}
|
|
private function zone_process(AccountEntity $account_entity): array
|
|
{
|
|
$zone = new ZoneService();
|
|
return $zone->reload($account_entity);
|
|
}
|
|
private function record_process(ZoneEntity $zone_entity): array
|
|
{
|
|
$record = new RecordService();
|
|
return $record->reload($zone_entity);
|
|
}
|
|
private function firewall_process(ZoneEntity $zone_entity): array
|
|
{
|
|
$firewall = new FirewallService();
|
|
return $firewall->reload($zone_entity);
|
|
}
|
|
public function reload(mixed $uid = false): void
|
|
{
|
|
//Transaction Start
|
|
// $this->_db->transStart();
|
|
try {
|
|
$auths = $this->auth_process($uid);
|
|
foreach ($auths as $auth) {
|
|
$accounts = $this->account_process($auth);
|
|
foreach (array_values($accounts) as $account) {
|
|
$zones = $this->zone_process($account);
|
|
$cnt = 1;
|
|
$total = count($zones);
|
|
foreach (array_values($zones) as $zone) {
|
|
log_message("notice", "\n-----------[{$cnt}/$total}] Zone {$zone->getTitle()}의 Record/Firewall 처리 시작-----------");
|
|
$records = $this->record_process($zone);
|
|
$firewalls = $this->firewall_process($zone);
|
|
log_message("notice", "\n-----------[{$cnt}/$total}] Zone {$zone->getTitle()}의 Record/Firewall 처리 완료-----------");
|
|
$cnt++;
|
|
}
|
|
}
|
|
}
|
|
log_message("notice", "Reload 작업을 완료하였습니다.");
|
|
// $this->_db->transCommit();
|
|
} catch (\Exception $e) {
|
|
//Transaction Rollback
|
|
// $this->_db->transRollback();
|
|
log_message(
|
|
"error",
|
|
"Reload 작업을 실패하였습니다.\n--------------\n" .
|
|
$e->getMessage() .
|
|
"\n--------------\n"
|
|
);
|
|
}
|
|
}
|
|
}
|