cfmgrv4/app/Controllers/CLI/Cloudflare.php
2024-11-18 11:12:00 +09:00

135 lines
5.1 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\Models\Cloudflare\AuthModel;
use App\Models\Cloudflare\RecordModel;
use App\Models\Cloudflare\ZoneModel;
use App\Services\Cloudflare\AccountService;
use App\Services\Cloudflare\AuditLogService;
use App\Services\Cloudflare\FirewallService;
use App\Services\Cloudflare\RecordService;
use App\Services\Cloudflare\ZoneService;
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();
}
public function reload(mixed $uid = false): void
{
//Transaction Start
// $this->_db->transStart();
try {
$auth_model = model(AuthModel::class);
if (is_numeric($uid) && $uid > 0) {
$auth_model->where(AuthModel::PK, intval($uid));
} else {
$auth_model->where('status', DEFAULTS["STATUS"]);
}
$auth_entitys = $auth_model->getEntitys();
foreach ($auth_entitys as $auth_entity) {
$account = new AccountService();
$account_entitys = $account->reload($auth_entity);
foreach (array_values($account_entitys) as $account_entity) {
$zone = new ZoneService();
$record = new RecordService();
$firewall = new FirewallService();
$zone_entitys = $zone->reload($account_entity);
$cnt = 1;
$total = count($zone_entitys);
foreach (array_values($zone_entitys) as $zone_entity) {
log_message("notice", "\n-----------[{$cnt}/$total}] Zone {$zone_entity->getTitle()}의 Record/Firewall 처리 시작-----------");
$record->reload($zone_entity);
$firewall->reload($zone_entity);
log_message("notice", "\n-----------[{$cnt}/$total}] Zone {$zone_entity->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"
);
}
}
public function auditlog(mixed $uid = false): void
{
//Transaction Start
// $this->_db->transStart();
try {
$auth_model = model(AuthModel::class);
if (is_numeric($uid) && $uid > 0) {
$auth_model->where(AuthModel::PK, intval($uid));
} else {
$auth_model->where('status', DEFAULTS["STATUS"]);
}
$auth_entitys = $auth_model->getEntitys();
foreach ($auth_entitys as $auth_entity) {
$account = new AccountService();
$account_entitys = $account->reload($auth_entity);
$auditlog = new AuditLogService();
foreach ($account_entitys as $account_entity) {
$auditlog->reload($account_entity);
}
}
log_message("notice", "AuditLogs 작업을 완료하였습니다.");
// $this->_db->transCommit();
} catch (\Exception $e) {
//Transaction Rollback
// $this->_db->transRollback();
log_message(
"error",
"Reload 작업을 실패하였습니다.\n--------------\n" .
$e->getMessage() .
"\n--------------\n"
);
}
}
public function expire(string $domain = ""): void
{
//Transaction Start
// $this->_db->transStart();
try {
$zone_model = model(ZoneModel::class);
if ($domain !== "") {
$zone_model->where(['domain' => $domain]);
}
$zone = new ZoneService();
foreach ($zone_model->getEntitys() as $entity) {
$zone->expire($entity);
}
log_message("notice", "Expire 작업을 완료하였습니다.");
// $this->_db->transCommit();
} catch (\Exception $e) {
//Transaction Rollback
// $this->_db->transRollback();
log_message(
"error",
"Expire 작업을 실패하였습니다.\n--------------\n" .
$e->getMessage() .
"\n--------------\n"
);
}
}
}