BtRepositories.php 3.1 KB
<?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 BtRepositories
 * @package App\Repositories
 */
class BtRepositories
{
    public $instance = [];

    /**
     * @param $domain
     * @param $ssl_open
     * @param $ssl_auto
     * @param $ssl_auto_day
     * @return array|bool
     */
    public function createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day)
    {
        $domain_array = parse_url($domain);
        $host = $domain_array['host'];

        $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);

        if (empty($result) || empty($result['id']))
            return false;

        $ssl_status = 0;
        $result = BtSites::createBtSite($host, $result['id'], $ssl_open, $ssl_status, $ssl_auto, $ssl_auto_day);
        return $result->toArray();
    }

    /**
     * 删除站点
     * @param $domain
     * @return bool
     */
    public function deleteBtSite($domain)
    {
        $domain = parse_url($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'] == $domain) {
                $id = $v['id'];
                break;
            }
        }

        if (empty($id))
            return false;
        $result = $bt->WebDeleteSite($id,$domain,false,false,false);

        $site->is_del = BtSites::IS_DEL_TRUE;
        $site->save();
        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];
    }
}