<?php
/**
* 宝塔接口
* Class Bt
*/
class Bt
{
private $bt_panel_from = "http://1111:123"; //面板地址
private $bt_key_from = "abc"; //接口密钥*/
//private $bt_panel_to = "http://2222:566"; //面板地址
//private $bt_key_to = "ccc"; //接口密钥
private $name_pre = '';//名称前缀
//示例取面板日志
public function GetLogs()
{
//拼接URL地址
$url = $this->bt_panel_from . '/data?action=getData';
//准备POST数据
$p_data = $this->GetKeyData($this->bt_key_from); //取签名
$p_data['table'] = 'logs';
$p_data['limit'] = 10;
$p_data['tojs'] = 'test';
//请求面板接口
$result = $this->HttpPostCookie($url, $p_data, $this->bt_panel_from, 60);
//解析JSON数据
$data = json_decode($result, true);
return $data;
}
/**
* 获取定时任务列表
* @param string $type
* @return array|bool|float|int|mixed|stdClass|string|null
*/
public function get_task_list($type = 'from')
{
if ($type == 'from') {
$panel = $this->bt_panel_from;
$key = $this->bt_key_from;
} else {
$panel = $this->bt_panel_to;
$key = $this->bt_key_to;
}
//拼接URL地址
$url = $panel . '/crontab?action=GetCrontab';
//准备POST数据
$p_data = $this->GetKeyData($key);//取签名
//请求面板接口
$result = $this->HttpPostCookie($url, $p_data, $panel);
if ($type == 'from') {
//备份信息
}
//解析JSON数据
return json_decode($result, true);
}
//批量还原(添加)定时任务
public function bitch_add_task()
{
//格式可能有变化,请先测试
$data = $this->get_task_list('from');
if (!empty($data)) {
$data = json_decode($data, true);
} else {
exit('数据不存在');
}
$newData = [];
$ids_ = array_column($data, 'id');
array_multisort($ids_, SORT_ASC, $data);
print_r($data);
exit('确认格式没有问题再关闭这里和上面一行');
foreach ($data as $k => $value) {
if (stripos($value['name'], 'xlh') === false) {
continue;
}
$temp = [
'name' => $this->name_pre . $value['name'],
'type' => '',
'where1' => $value['where1'],
'hour' => $value['where_hour'],
'minute' => $value['where_minute'],
'week' => '',
'sType' => $value['sType'],
'sBody' => $value['sBody'],
'sName' => '',
'backupTo' => 'localhost',
'save' => '',
'status' => $value['status'],
'urladdress' => $value['urladdress']
];
if ($value['type'] == '每天' && $value['where1'] === '') {
$temp['type'] = 'day';
} elseif ($value['type'] == '每月') {
$temp['type'] = 'month';
} elseif ($value['type'] == '每小时') {
$temp['type'] = 'hour';
} elseif ($value['type'] == '每周') {
$temp['type'] = 'week';
$temp['week'] = $value['where1'];
$temp['where1'] = '';
} else if ($value['where_minute'] === '' && $value['where_hour'] === '') {
$temp['type'] = 'minute-n';//每x分钟
} elseif (is_int($value['where_minute']) && is_int($value['where_hour'])) {
$temp['type'] = 'day-n';//每x天
} elseif (is_int($value['where_minute']) && $value['where_hour'] === '') {
$temp['type'] = 'hour-n';//每x小时
}
$add = $this->add_task($temp);
if ($add['id'] > 0 && $temp['status'] == 0) {
//$this->disableTask[] = $temp['name'];//临时存放在待禁用列表
$this->set_task_status($add['id']);
}
$temp['addStatus'] = $add;
$newData[] = $temp;
}
// $newData['setStatus'] = $this->bitch_set_task_status();//批量设置禁用
returnMsg(200, 'success', $newData);
}
//添加定时任务
public function add_task($task = [])
{
$url = $this->bt_panel_to . '/crontab?action=AddCrontab';
$p_data = $this->GetKeyData($this->bt_key_to);
$p_data['name'] = $task['name'];
$p_data['type'] = 'minute-n';
$p_data['where1'] = $task['where1'];
$p_data['hour'] = '';
$p_data['minute'] = '';
$p_data['week'] = '';
$p_data['sType'] = $task['sType'];
$p_data['sName'] = $task['sName'];
$p_data['backupTo'] = $task['backupTo'];
$p_data['save'] = $task['save'];
$p_data['sBody'] = $task['sBody'];
$p_data['urladdress'] = $task['urladdress'];
$result = $this->HttpPostCookie($url, $p_data, $this->bt_panel_to);
return json_decode($result, true);
}
//修改定时任务状态
private function set_task_status($id)
{
$url = $this->bt_panel_to . '/crontab?action=set_cron_status';
$p_data = $this->GetKeyData($this->bt_key_to);
$p_data['id'] = $id;
$result = $this->HttpPostCookie($url, $p_data, $this->bt_panel_to);
$res = json_decode($result, true);
return $res;
}
//删除定时任务
private function del_task($id)
{
$url = $this->bt_panel_to . '/crontab?action=DelCrontab';
$p_data = $this->GetKeyData($this->bt_key_to);
$p_data['id'] = $id;
$result = $this->HttpPostCookie($url, $p_data, $this->bt_panel_to);
$res = json_decode($result, true);
return $res;
}
/**
* 构造带有签名的关联数组
*/
private function GetKeyData($key)
{
$now_time = time();
$p_data = array(
'request_token' => md5($now_time . '' . md5($key)),
'request_time' => $now_time
);
return $p_data;
}
/**
* 发起POST请求
* @param String $url 目标网填,带http://
* @param String|array $data 欲提交的数据
* @param $panel string 面板地址
* @param int $timeout
* @return string
*/
private function HttpPostCookie($url, $data, $panel, $timeout = 60)
{
//定义cookie保存位置
$cookie_file = './' . md5($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;
}
}