作者 刘锟

update

@@ -53,6 +53,33 @@ public function checkDomainSsl() @@ -53,6 +53,33 @@ public function checkDomainSsl()
53 $site_id = $site_list['data'][0]['id']; 53 $site_id = $site_list['data'][0]['id'];
54 $host = $site_list['data'][0]['name']; 54 $host = $site_list['data'][0]['name'];
55 55
  56 + if (env('IS_SSL', false)) {
  57 + //通配符证书
  58 + $transmitUrl = env("TRANSMIT_URL");
  59 + $resp = $this->httpPost($transmitUrl . "api/selfSiteSsl/", json_encode([]));
  60 + if (isset($resp['status']) && $resp['status'] == 200) {
  61 + $ssl_key = $resp['data']['ssl_key'] ?? '';
  62 + $ssl_cert = $resp['data']['ssl_cert'] ?? '';
  63 + if ($ssl_key && $ssl_cert) {
  64 + //申请证书之前,还原主站配置
  65 + $config_before = file_get_contents(public_path('main_site_default.txt'));
  66 + $re_config_before = $this->bt->SaveFileBody('/www/server/panel/vhost/nginx/' . $host . '.conf', $config_before, 'utf-8', 1);
  67 + if (!($re_config_before['status'] ?? false)) {
  68 + throw new \Exception($re_config_before['msg'] ?? '还原主站nginx配置失败');
  69 + }
  70 +
  71 + //设置站点证书
  72 + $this->setDomainSsl($site_id, $host, [], $ssl_key, $ssl_cert);
  73 +
  74 + //申请证书之后,更新主站配置
  75 + $config_after = file_get_contents(public_path('main_site_config.txt'));
  76 + $re_config_after = $this->bt->SaveFileBody('/www/server/panel/vhost/nginx/' . $host . '.conf', $config_after, 'utf-8', 1);
  77 + if (!($re_config_after['status'] ?? false)) {
  78 + throw new \Exception($re_config_after['msg'] ?? '更新主站nginx配置失败');
  79 + }
  80 + }
  81 + }
  82 + } else {
56 //获取站点可用于设置证书的域名 83 //获取站点可用于设置证书的域名
57 $site_domain_list = $this->bt->WebDoaminList($site_id); 84 $site_domain_list = $this->bt->WebDoaminList($site_id);
58 $apply_ssl_domain_list = []; 85 $apply_ssl_domain_list = [];
@@ -81,6 +108,7 @@ public function checkDomainSsl() @@ -81,6 +108,7 @@ public function checkDomainSsl()
81 if (!($re_config_after['status'] ?? false)) { 108 if (!($re_config_after['status'] ?? false)) {
82 throw new \Exception($re_config_after['msg'] ?? '更新主站nginx配置失败'); 109 throw new \Exception($re_config_after['msg'] ?? '更新主站nginx配置失败');
83 } 110 }
  111 + }
84 112
85 $this->output('主站证书更新成功'); 113 $this->output('主站证书更新成功');
86 } 114 }
@@ -232,6 +260,39 @@ public function getDomainSslTime($domain) @@ -232,6 +260,39 @@ public function getDomainSslTime($domain)
232 return ['from' => $valid_from, 'to' => $valid_to]; 260 return ['from' => $valid_from, 'to' => $valid_to];
233 } 261 }
234 262
  263 + public function httpPost($url, $data, $header = [], $is_json = true)
  264 + {
  265 + if (empty($header)) {
  266 + $header = array(
  267 + "Accept: application/json",
  268 + "Content-Type:application/json;charset=utf-8",
  269 + "token:" . env("SECRET_TOKEN"),
  270 + "pid:" . env("MERCHANT_NUMBER")
  271 + );
  272 + }
  273 + $ch = curl_init();
  274 + curl_setopt($ch, CURLOPT_URL, $url);
  275 + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  276 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  277 + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  278 + curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  279 + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  280 + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  281 + curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  282 + curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  283 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  284 + $res = curl_exec($ch);
  285 + if (curl_errno($ch)) {
  286 + $error_message = curl_error($ch);
  287 + @file_put_contents(storage_path('logs/error.log'), var_export($error_message, true) . PHP_EOL, FILE_APPEND);
  288 + }
  289 + curl_close($ch);
  290 + if ($is_json) {
  291 + return json_decode($res, true);
  292 + }
  293 + return trim($res);
  294 + }
  295 +
235 /** 296 /**
236 * 输出处理日志 297 * 输出处理日志
237 * @param $message 298 * @param $message