BtRepository.php
7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2022/11/1
* Time: 11:56
*/
namespace App\Repositories;
use App\Models\BtSites;
use App\Repositories\Bt\Bt;
/**
* Class BtRepository
* @package App\Repositories
*/
class BtRepository
{
public $instance = [];
/**
* @param $domain
* @param $ssl_open
* @param $ssl_auto
* @param $ssl_auto_day
* @param int $ssl_status
* @return array|bool
*/
public function createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day, $ssl_status = 0)
{
$domain_array = parse_url($domain);
$host = $domain_array['host'];
// 如果站点已经存在, 更新数据
$site = BtSites::getSiteByDomain($host);
if ($site) {
$result = BtSites::createBtSite($host, $site->site_id, $ssl_open, $ssl_status, $ssl_auto, $ssl_auto_day);
return $result->toArray();
}
$host_array = explode('.', $host);
if (empty($host_array[0]) && $host_array[0] == 'www')
unset($host_array[0]);
$domain_top = implode('.', $host_array);
$web_name = ['domain' => $host, 'domainlist' => [$domain_top], 'count' => 0];
$array = [
'webname' => json_encode($web_name),
'path' => env('PROJECT_PATH') ? env('PROJECT_PATH') : dirname(dirname(app_path())) . DIRECTORY_SEPARATOR . $domain,
'type_id' => 0,
'type' => 'PHP',
'version' => '73',
'port' => '80',
'ps' => $host,
'ftp' => false,
'ftp_username' => '',
'ftp_password' => '',
'sql' => false,
'codeing' => '',
'datauser' => '',
'datapassword' => '',
];
$bt = $this->getBtObject();
$result = $bt->AddSite($array);
// result 返回信息
// $result = ['siteStatus' => true, 'siteId' => 39, 'ftpStatus' => false, 'databaseStatus' => false,];
if (empty($result) || empty($result['siteId']))
return false;
// 伪静态设置
// $bt->SaveFileBody($host, '');
$result = BtSites::createBtSite($host, $result['siteId'], $ssl_open, $ssl_status, $ssl_auto, $ssl_auto_day);
$site = $result->toArray();
unset($site['id']);
unset($site['created_at']);
unset($site['updated_at']);
unset($site['is_del']);
return $site;
}
/**
* 删除站点
* @param $domain
* @return bool
*/
public function deleteBtSite($domain)
{
$domain_array = parse_url($domain);
$host = $domain_array['host'];
$bt = $this->getBtObject();
$site = BtSites::getSiteByDomain($host);
// $id = $site->site_id;
// 获取bt数据 可能要可靠一些
$result = $bt->Websites($host);
if (empty($result['data']))
return false;
$id = 0;
foreach ($result['data'] as $v) {
if ($v['name'] == $host) {
$id = $v['id'];
break;
}
}
if (empty($id))
return false;
$result = $bt->WebDeleteSite($id,$host,false,false,false);
// $result 返回信息
// $result = ['status' => true, 'msg' => '站点删除成功!',]
if (empty($result['status']))
return false;
if ($site) {
$site->is_del = BtSites::IS_DEL_TRUE;
$site->save();
}
return true;
}
/**
* 申请ssl 并设置ssl访问
* @param $host
* @return bool
*/
public function applySsl($host)
{
$site = BtSites::getSiteByDomain($host);
if (empty($site) || empty($site->ssl_open))
return false;
$bt = $this->getBtObject();
// $domains = [["id" => 71, "pid" => 35, "name" => "www.shopk.com", "port" => 80, "addtime" => "2022-11-03 14:22:08",]];
$domain_list = $bt->WebDoaminList($site->site_id);
// 被删除的站点 返回空数组 不做数据删除同步 可能bt接口问题
if (empty($domain_list))
return false;
// $result = ["status" => false, "msg" => "指定网站配置文件不存在!"];
// $result = ["status" => true, "oid" => -1, "domain" => [["name" => "*.shopk.com"],["name" => "www.shopk.com"]], "key" => "key", "csr" => "csr", "type" => 0, "httpTohttps" => true,
// "cert_data" => ["issuer" => "TrustAsia TLS RSA CA", "notAfter" => "2023-02-22", "notBefore" => "2022-02-21", "dns" => [0 => "*.shopk.com", 1 => "shopk.com"],
// "subject" => "*.shopk.com", "endtime" => 110], "email" => "test@message.com", "index" => null, "auth_type" => "http", "push" => ["status" => false]];
$ssl = $bt->GetSSL($host);
// 已经有ssl证书了
if (FALSE == empty($ssl['status'])) {
$bt->SetSSL('1', $host, $ssl['key'], $ssl['csr']);
// 开启强制https
// $bt->HttpToHttps($host);
$site->ssl_status = 1;
$site->save();
return true;
}
$domain = array_column($domain_list, 'name');
$result = $bt->ApplyCert(json_encode($domain), $site->site_id);
if (empty($result) || empty($result['cert']))
return false;
$bt->SetSSL('1', $host, $result['private_key'], $result['cert']);
// 开启强制https
// $bt->HttpToHttps($host);
$site->ssl_status = 1;
$site->save();
return true;
}
/**
* 续签ssl
* @param $host
* @return bool
*/
public function renewalSsl($host)
{
$site = BtSites::getSiteByDomain($host);
if (empty($site) || empty($site->ssl_open) || empty($site->ssl_auto))
return false;
$bt = $this->getBtObject();
// $result = ["status" => false, "oid" => -1, "domain" => [["name" => "test.hagro.cn"], ["name" => "test.hagro.cn"]], "key" => false, "csr" => false,
// "type" => -1, "httpTohttps" => false, "cert_data" => [], "email" => "test@message.com", "index" => "", "auth_type" => "http", "push" => ["status" => false]];
$ssl = $bt->GetSSL($host);
if (empty($ssl['status']))
return $this->applySsl($host);
$end = strtotime($ssl["cert_data"]["notAfter"]);
if ($end - time() >= $site->ssl_auto_day * 86400)
return true;
$domain_list = $bt->WebDoaminList($site->site_id);
// 被删除的站点 返回空数组 不做数据删除同步 可能bt接口问题
if (empty($domain_list))
return false;
// 不是宝塔生成的ssl 不能续签, 直接申请ssl
$result = $bt->RenewCert($ssl["index"]);
if (empty($result['status']))
return $this->applySsl($host);
$bt->SetSSL(1, $host, $result['private_key'], $result['cert']);
return true;
}
/**
* 获取bt对象
* @param string $key
* @param string $panel
* @return Bt
*/
public function getBtObject($key = '', $panel = '')
{
$key = $key ?: env('BT_KEY');
$panel = $panel ?: env('BT_PANEL');
$object_key = md5($key . $panel);
if (empty($this->instance[$object_key])) {
$bt = new Bt($panel, $key);
$this->instance[$object_key] = $bt;
}
return $this->instance[$object_key];
}
}