|
...
|
...
|
@@ -7,15 +7,14 @@ use App\Enums\Common\Code; |
|
|
|
use App\Exceptions\AsideGlobalException;
|
|
|
|
use App\Exceptions\BsideGlobalException;
|
|
|
|
use App\Http\Logic\Aside\BaseLogic;
|
|
|
|
use App\Http\Logic\Aside\Devops\ServerInformationLogic;
|
|
|
|
use App\Models\Aside\Domain\DomainInfo;
|
|
|
|
use App\Models\Aside\Domain\DomainInfoLog;
|
|
|
|
use App\Models\Devops\ServerInformation;
|
|
|
|
use App\Models\Devops\ServerInformationLog;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
class DomainInfoLogic extends BaseLogic
|
|
|
|
{
|
|
...
|
...
|
@@ -42,23 +41,19 @@ class DomainInfoLogic extends BaseLogic |
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
$request = $this->param;
|
|
|
|
$request = $this->param ?? [];
|
|
|
|
if ($this->checkDomain($request['domain'])) {
|
|
|
|
return $this->fail('域名已存在!');
|
|
|
|
}
|
|
|
|
$domain = new DomainInfo();
|
|
|
|
$this->extracted($request, $domain);
|
|
|
|
DB::beginTransaction();
|
|
|
|
if ($domain->save()) {
|
|
|
|
$original = [
|
|
|
|
'id' => $domain->id,
|
|
|
|
'domain' => $domain->domain,
|
|
|
|
'belong_to' => $domain->belong_to,
|
|
|
|
'status' => $domain->status,
|
|
|
|
'domain_start_time' => $domain->domain_start_time,
|
|
|
|
'domain_end_time' => $domain->domain_end_time,
|
|
|
|
'certificate_start_time' => $domain->certificate_start_time,
|
|
|
|
'certificate_end_time' => $domain->certificate_end_time,
|
|
|
|
];
|
|
|
|
$original = $domain->getOriginal();
|
|
|
|
$original['belong_to'] = $request['belong_to'];
|
|
|
|
$original['status'] = $request['status'];
|
|
|
|
// 添加日志
|
|
|
|
$this->domain_action_log(ServerInformationLog::ACTION_ADD, $original, $original, '添加服务器信息成功 - ID : ' . $domain->id);
|
|
|
|
$this->domain_action_log(DomainInfoLog::ACTION_ADD, $original, $original, '添加域名信息成功 - ID : ' . $domain->id);
|
|
|
|
DB::commit();
|
|
|
|
return $this->success();
|
|
|
|
}
|
|
...
|
...
|
@@ -75,25 +70,25 @@ class DomainInfoLogic extends BaseLogic |
|
|
|
*/
|
|
|
|
public function update()
|
|
|
|
{
|
|
|
|
$domain = new DomainInfo();
|
|
|
|
$domain = $this->getDomain();
|
|
|
|
$original = $domain->getOriginal();
|
|
|
|
$original['belong_to'] = $domain->BelongToStr($original['belong_to']);
|
|
|
|
$original['status'] = $domain->StatusToStr($original['status']);
|
|
|
|
$request = $this->param;
|
|
|
|
$this->extracted($request, $domain);
|
|
|
|
// 检查ip是否存在
|
|
|
|
if ($domain->domain != $request['domain']) {
|
|
|
|
if ($this->checkDomain($request['domain'])) {
|
|
|
|
$this->fail('域名信息修改失败,域名已存在', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
|
|
if ($domain->save()) {
|
|
|
|
$fields_array = $domain->FieldsArray();
|
|
|
|
$original = $domain->getOriginal();
|
|
|
|
$revised = [
|
|
|
|
'id' => $domain->id,
|
|
|
|
'domain' => $domain->domain,
|
|
|
|
'belong_to' => $domain->belong_to,
|
|
|
|
'status' => $domain->status,
|
|
|
|
'domain_start_time' => $domain->domain_start_time,
|
|
|
|
'domain_end_time' => $domain->domain_end_time,
|
|
|
|
'certificate_start_time' => $domain->certificate_start_time,
|
|
|
|
'certificate_end_time' => $domain->certificate_end_time,
|
|
|
|
'delete' => $domain->delete,
|
|
|
|
];
|
|
|
|
$revised = $domain->getAttributes();
|
|
|
|
$diff = array_diff_assoc($original, $revised);
|
|
|
|
unset($diff['created_at']);
|
|
|
|
unset($diff['updated_at']);
|
|
|
|
unset($diff['deleted']);
|
|
|
|
$remarks = '';
|
|
|
|
if ($diff) {
|
|
...
|
...
|
@@ -101,9 +96,11 @@ class DomainInfoLogic extends BaseLogic |
|
|
|
foreach ($diff as $key => $value) {
|
|
|
|
$remarks .= $fields_array[$key] . ' 由 ' . $value . ' 修改为 ' . $revised[$key] . '; ';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$remarks .= '修改ID为 ' . $domain->id . ' 的域名信息,无修改';
|
|
|
|
}
|
|
|
|
// 添加日志
|
|
|
|
$this->domain_action_log(ServerInformationLog::ACTION_UPDATE, $original, $revised, $remarks);
|
|
|
|
$this->domain_action_log(DomainInfoLog::ACTION_UPDATE, $original, $revised, $remarks);
|
|
|
|
DB::commit();
|
|
|
|
return $this->success();
|
|
|
|
}
|
|
...
|
...
|
@@ -112,6 +109,25 @@ class DomainInfoLogic extends BaseLogic |
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 根据ID获取数据
|
|
|
|
* @return array
|
|
|
|
* @throws AsideGlobalException
|
|
|
|
* @throws BsideGlobalException
|
|
|
|
*/
|
|
|
|
public function getDomain(int $deleted = DomainInfo::DELETED_NORMAL)
|
|
|
|
{
|
|
|
|
$id = $this->param['id'] ?? 0;
|
|
|
|
if (!$id) {
|
|
|
|
return $this->fail('ID不能为空');
|
|
|
|
}
|
|
|
|
$data = DomainInfo::query()->where('deleted', $deleted)->find($id);
|
|
|
|
if (!$data) {
|
|
|
|
return $this->fail('数据不存在!');
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 检查域名是否存在
|
|
|
|
* @param $domain
|
|
|
|
* @return bool
|
|
...
|
...
|
@@ -135,11 +151,11 @@ class DomainInfoLogic extends BaseLogic |
|
|
|
*/
|
|
|
|
public function domainInfo(int $deleted = DomainInfo::DELETED_NORMAL)
|
|
|
|
{
|
|
|
|
$id = request()->input('id');
|
|
|
|
$id = $this->param['id'] ?? 0;
|
|
|
|
if (!$id) {
|
|
|
|
return $this->fail('参数错误');
|
|
|
|
return $this->fail('ID不能为空');
|
|
|
|
}
|
|
|
|
$data = DomainInfo::query()->where('deleted', $deleted)->find($id, ['domain', 'belong_to', 'title', 'domain_start_time', 'domain_end_time', 'certificate_start_time', 'certificate_end_time', 'create_at', 'update_at']);
|
|
|
|
$data = DomainInfo::query()->where('deleted', $deleted)->find($id, ['domain', 'belong_to', 'status', 'domain_start_time', 'domain_end_time', 'certificate_start_time', 'certificate_end_time', 'created_at', 'updated_at']);
|
|
|
|
if (!$data) {
|
|
|
|
return $this->fail('数据不存在!');
|
|
|
|
}
|
|
...
|
...
|
@@ -154,44 +170,27 @@ class DomainInfoLogic extends BaseLogic |
|
|
|
*/
|
|
|
|
public function domain_action_log(int $action = DomainInfoLog::ACTION_ADD, array $original = [], array $revised = [], $remarks = '')
|
|
|
|
{
|
|
|
|
// $action 1:添加 2:修改 3:删除 4:恢复
|
|
|
|
$actionArr = DomainInfoLog::actionArr();
|
|
|
|
$actionStr = $actionArr[$action];
|
|
|
|
$ip = request()->getClientIp();
|
|
|
|
$url = request()->getRequestUri();
|
|
|
|
$method = request()->getMethod();
|
|
|
|
$userId = $this->uid ?? 0;
|
|
|
|
$log = new ServerInformationLog();
|
|
|
|
$log->user_id = $userId;
|
|
|
|
$log->action = $actionStr;
|
|
|
|
$log->original = json_encode($original);
|
|
|
|
$log->revised = json_encode($revised);
|
|
|
|
$log->ip = $ip;
|
|
|
|
$log->url = $url;
|
|
|
|
$log->method = $method;
|
|
|
|
$log->remarks = $remarks;
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
|
|
$log->save();
|
|
|
|
DB::commit();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
DB::rollBack();
|
|
|
|
Log::error('服务器信息日志添加失败');
|
|
|
|
}
|
|
|
|
$log = new DomainInfoLog();
|
|
|
|
$this->log($log, $action, $original, $revised, $remarks);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $request
|
|
|
|
* @param $domain
|
|
|
|
* @return void
|
|
|
|
* @throws AsideGlobalException
|
|
|
|
* @throws BsideGlobalException
|
|
|
|
*/
|
|
|
|
public function extracted(array $request, $domain)
|
|
|
|
{
|
|
|
|
$domain->domain = trim($request['domain']); // 域名
|
|
|
|
$domain->domain = trim($request['domain']);
|
|
|
|
if (!$this->validateDomain($domain->domain)) {
|
|
|
|
$this->fail('域名格式错误');
|
|
|
|
}
|
|
|
|
$domain->belong_to = trim($request['belong_to']); // 域名归属 1 - 公司 2 - 客户
|
|
|
|
$domain->status = trim($request['status']); // 域名状态 0 - 正常 1 - 关闭
|
|
|
|
$domain->domain_start_time = trim($request['domain_start_time']); // 域名开始时间
|
|
|
|
$domain->domain_end_time = trim($request['domain_end_time']); // 域名结束时间
|
|
|
|
$domain->domain_end_time = trim($request['domain_end_time']);
|
|
|
|
$domain->certificate_start_time = trim($request['certificate_start_time']); // 证书开始时间
|
|
|
|
$domain->certificate_end_time = trim($request['certificate_end_time']); // 证书结束时间
|
|
|
|
}
|
|
...
|
...
|
@@ -204,36 +203,78 @@ class DomainInfoLogic extends BaseLogic |
|
|
|
*/
|
|
|
|
public function get_batch_update($action = DomainInfoLog::ACTION_DELETE, $deleted = DomainInfo::DELETED_NORMAL)
|
|
|
|
{
|
|
|
|
$id = request()->input('id');
|
|
|
|
if (!$id) {
|
|
|
|
return $this->fail('参数错误');
|
|
|
|
}
|
|
|
|
$ids = [];
|
|
|
|
if (!is_array($id)) {
|
|
|
|
$ids = explode(',', $id);
|
|
|
|
}
|
|
|
|
$ids = array_filter($ids, 'intval');
|
|
|
|
if (empty($ids)) {
|
|
|
|
return $this->fail('参数错误');
|
|
|
|
}
|
|
|
|
$ids = (new ServerInformationLogic())->getIds();
|
|
|
|
$data = DomainInfo::query()->whereIn('id', $ids)->where('deleted', $deleted)->get();
|
|
|
|
$restore_ids = $data->pluck('id')->toArray();
|
|
|
|
$actionArr = DomainInfoLog::actionArr();
|
|
|
|
$actionStr = $actionArr[$action];
|
|
|
|
if (empty($restore_ids)) {
|
|
|
|
$this->fail($actionStr . '服务器信息不存在!', Code::USER_ERROR);
|
|
|
|
$this->fail($actionStr . '域名信息不存在!', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
$original = $data->toArray();
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
|
|
$update = $deleted == DomainInfo::DELETED_NORMAL ? DomainInfo::DELETED_DELETE : DomainInfo::DELETED_NORMAL;
|
|
|
|
DomainInfo::query()->whereIn('id', $restore_ids)->update(['deleted' => $update]);
|
|
|
|
$this->domain_action_log($action, $original, $original, $actionStr . '服务器信息 - ID : ' . implode(', ', $restore_ids));
|
|
|
|
$this->domain_action_log($action, $original, $original, $actionStr . '域名信息 - ID : ' . implode(', ', $restore_ids));
|
|
|
|
DB::commit();
|
|
|
|
return $this->success();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
DB::rollBack();
|
|
|
|
return $this->fail('服务器信息' . $actionStr . '失败', Code::USER_ERROR);
|
|
|
|
return $this->fail('域名信息' . $actionStr . '失败', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 域名到期时间
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function domainCertificateTime($domain)
|
|
|
|
{
|
|
|
|
$domain = trim($domain);
|
|
|
|
$data = [];
|
|
|
|
$context = stream_context_create(['ssl' => ['capture_peer_cert' => true]]); // Notice: only 7.0.7+ supports this
|
|
|
|
$stream = stream_socket_client("ssl://$domain:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
|
|
|
|
if ($stream) {
|
|
|
|
$params = stream_context_get_params($stream);
|
|
|
|
$peerCertificate = openssl_x509_parse($params['options']['ssl']['peer_certificate']);
|
|
|
|
if ($peerCertificate) {
|
|
|
|
$validFrom = date_create_from_format('U', $peerCertificate['validFrom_time_t']); // 有效期开始时间
|
|
|
|
$validTo = date_create_from_format('U', $peerCertificate['validTo_time_t']); // 有效期结束时间
|
|
|
|
$data['validFrom'] = $validFrom->format('Y-m-d H:i:s');
|
|
|
|
$data['validTo'] = $validTo->format('Y-m-d H:i:s');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 验证给定的值是否是有效的域名。
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validateDomain($value)
|
|
|
|
{
|
|
|
|
// 从域中删除任何空间或路径。
|
|
|
|
$domain = preg_replace('/\s|\/.*$/', '', $value);
|
|
|
|
// 验证域是否至少包含一个句点。
|
|
|
|
if (strpos($domain, '.') === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 验证域是否以句点开始或结束。
|
|
|
|
if (strpos($domain, '.') === 0 || strrpos($domain, '.') === strlen($domain) - 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 验证域是否不包含无效字符。
|
|
|
|
if (!preg_match('/^[a-zA-Z0-9\-\.]+$/', $domain)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 验证域是否具有有效的顶级域。
|
|
|
|
$tld = substr($domain, strrpos($domain, '.') + 1);
|
|
|
|
if (!in_array($tld, ['com', 'net', 'org'])) { // 如有必要,添加更多TLD。
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|