<?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;
    }
}