Bt.php
6.4 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
<?php
namespace fast;
/**
* 宝塔API接口示例Demo
* 仅供参考,请根据实际项目需求开发,并做好安全处理
* date 2018/12/12
* author 阿良
*/
class Bt {
private $BT_KEY = ""; //接口密钥
private $BT_PANEL = ""; //面板地址
private $Web_ID = null;
//如果希望多台面板,可以在实例化对象时,将面板地址与密钥传入
public function __construct($bt_panel = null,$bt_key = null,$Web_ID = null){
if($bt_panel) $this->BT_PANEL = $bt_panel;
if($bt_key) $this->BT_KEY = $bt_key;
$this->Web_ID = $Web_ID;
}
//示例取面板日志
public function GetLogs(){
//拼接URL地址
$url = $this->BT_PANEL.'/data?action=getData';
//准备POST数据
$p_data = $this->GetKeyData(); //取签名
$p_data['table'] = 'logs';
$p_data['limit'] = 10;
$p_data['tojs'] = 'test';
//请求面板接口
$result = $this->HttpPostCookie($url,$p_data);
//解析JSON数据
$data = json_decode($result,true);
return $data;
}
//示例取面板日志
public function GetSites(){
//拼接URL地址
$url = $this->BT_PANEL.'/data?action=getData';
//准备POST数据
$p_data = $this->GetKeyData(); //取签名
$p_data['table'] = 'sites';
$p_data['type'] = -1;
$p_data['limit'] = 100;
$p_data['p'] = 1;
$p_data['search'] = '';
//请求面板接口
$result = $this->HttpPostCookie($url,$p_data);
//解析JSON数据
$data = json_decode($result,true);
return $data;
}
public function apply_cert_api(){
$url = $this->BT_PANEL.'/acme?action=apply_cert_api';
//准备POST数据
$p_data = $this->GetKeyData(); //取签名
$p_data['domains'] = json_encode(['msljhome.com']);
$p_data['auth_type'] = 'http';
$p_data['auth_to'] = $this->Web_ID;
$p_data['auto_wildcard'] = 0;
$p_data['id'] = $this->Web_ID;
//请求面板接口
$result = $this->HttpPostCookie($url,$p_data);
//解析JSON数据
$data = json_decode($result,true);
return $data;
}
public function UserInfo(){
//ssl?action=GetUserInfo
$url = $this->BT_PANEL.'/ssl?action=GetUserInfo';
$p_data = $this->GetKeyData(); //取签
$result = $this->HttpPostCookie($url,$p_data);
//解析JSON数据
$data = json_decode($result,true);
return $data;
}
public function get_account_info(){
$url = $this->BT_PANEL.'/acme?action=get_account_info';
$p_data = $this->GetKeyData(); //取签
$result = $this->HttpPostCookie($url,$p_data);
//解析JSON数据
$data = json_decode($result,true);
return $data;
}
public function setSSl($keyData){
//http://47.92.25.106:8888/site?action=SetSSL
$url = $this->BT_PANEL.'/site?action=SetSSL';
$p_data = $this->GetKeyData(); //取签
$p_data['type'] = 1;
$p_data['siteName'] = $keyData['domains'][0];
$p_data['key'] = $keyData['private_key'];
$p_data['csr'] = $keyData['cert'];
$result = $this->HttpPostCookie($url,$p_data);
//解析JSON数据
$data = json_decode($result,true);
return $data;
}
public function GetSiteDomains(){
$url = $this->BT_PANEL.'/site?action=GetSiteDomains';
$p_data = $this->GetKeyData(); //取签
$p_data['id'] = $this->Web_ID; // id 为网站ID
$result = $this->HttpPostCookie($url,$p_data);
//解析JSON数据
$data = json_decode($result,true);
return $data;
}
/**
* 网站新增域名
* @param $domain 域名
* @param $webName 网站名
* @param $webId 网站ID
* @return mixed
*/
public function addDomain($domain,$webName){
//site?action=AddDomain
$url = $this->BT_PANEL.'/site?action=AddDomain';
$p_data = $this->GetKeyData(); //取签
$p_data['domain'] = $domain; //当前域名
$p_data['webname'] = $webName;
$p_data['id'] = $this->Web_ID;
$result = $this->HttpPostCookie($url,$p_data);
//解析JSON数据
$data = json_decode($result,true);
return $data;
}
public function get_lines(){
//ajax?action=get_lines
$url = $this->BT_PANEL.'/site?action=GetSiteDomains';
$p_data = $this->GetKeyData(); //取签
$p_data['num'] = 10;
$p_data['filename'] = '/www/server/panel/logs/letsencrypt.log';
$result = $this->HttpPostCookie($url,$p_data);
//解析JSON数据
$data = json_decode($result,true);
return $data;
}
public function GetSSL($domain){
//site?action=GetSSL
$url = $this->BT_PANEL.'/site?action=GetSSL';
$p_data = $this->GetKeyData(); //取签
$p_data['siteName'] = $domain;
$result = $this->HttpPostCookie($url,$p_data);
//解析JSON数据
$data = json_decode($result,true);
return $data;
}
/**
* 构造带有签名的关联数组
*/
private function GetKeyData(){
$now_time = time();
$p_data = array (
'request_token' => md5( $now_time.''.md5( $this->BT_KEY ) ),
'request_time' => $now_time
);
return $p_data;
}
/**
* 发起POST请求
* @param String $url 目标网填,带http://
* @param Array|String $data 欲提交的数据
* @return string
*/
private function HttpPostCookie($url, $data,$timeout = 60)
{
//定义cookie保存位置
$cookie_file='./'.md5($this->BT_PANEL).'.cookie';
if(!file_exists($cookie_file)){
$fp = fopen($cookie_file,'w+');
fclose($fp);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
}