ApiService.php 10.6 KB
Newer Older
李光春's avatar
李光春 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php

// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------

namespace DtApp\ThinkLibrary\service\bt;

李光春's avatar
李光春 已提交
19
use DtApp\ThinkLibrary\exception\DtaException;
李光春's avatar
李光春 已提交
20
use DtApp\ThinkLibrary\facade\Files;
李光春's avatar
李光春 已提交
21
use DtApp\ThinkLibrary\Service;
李光春's avatar
李光春 已提交
22
use DtApp\ThinkLibrary\service\curl\BtService;
李光春's avatar
李光春 已提交
23 24 25

/**
 * 宝塔Api
李光春's avatar
李光春 已提交
26
 * https://www.bt.cn/
李光春's avatar
李光春 已提交
27 28 29 30 31
 * Class ApiService
 * @package DtApp\ThinkLibrary\service\bt
 */
class ApiService extends Service
{
李光春's avatar
李光春 已提交
32 33 34
    /**
     * @var string
     */
李光春's avatar
李光春 已提交
35
    private $url = '';
李光春's avatar
李光春 已提交
36 37 38 39

    /**
     * @var int
     */
李光春's avatar
李光春 已提交
40
    private $page = 1;
李光春's avatar
李光春 已提交
41 42 43 44

    /**
     * @var int
     */
李光春's avatar
李光春 已提交
45
    private $limit = 15;
李光春's avatar
李光春 已提交
46 47 48 49

    /**
     * @var string
     */
李光春's avatar
李光春 已提交
50
    private $order = 'id desc';
李光春's avatar
李光春 已提交
51 52 53 54

    /**
     * @var array
     */
李光春's avatar
李光春 已提交
55
    private $where = [];
李光春's avatar
李光春 已提交
56 57 58 59

    /**
     * @var
     */
李光春's avatar
李光春 已提交
60
    private $contents, $backtrack, $key, $panel;
李光春's avatar
李光春 已提交
61

李光春's avatar
李光春 已提交
62 63 64 65 66
    /**
     * @param string $key
     * @return $this
     */
    public function key(string $key): self
李光春's avatar
李光春 已提交
67 68 69 70 71
    {
        $this->key = $key;
        return $this;
    }

李光春's avatar
李光春 已提交
72 73 74 75
    /**
     * @param string $panel
     * @return $this
     */
李光春's avatar
李光春 已提交
76 77 78 79 80 81
    public function panel(string $panel)
    {
        $this->panel = $panel;
        return $this;
    }

82 83 84 85
    /**
     * 获取配置信息
     * @return $this
     */
李光春's avatar
李光春 已提交
86
    private function getConfig(): self
87
    {
李光春's avatar
李光春 已提交
88 89
        $this->key = config('dtapp.bt.key');
        $this->panel = config('dtapp.bt.panel');
90 91 92
        return $this;
    }

李光春's avatar
李光春 已提交
93 94 95 96 97 98 99 100 101
    /**
     * 获取监控信息
     * @param string $type 类型 GetCpuIo = CPU信息/内存 GetDiskIo = 磁盘IO GetNetWorkIo = 网络IO
     * @param int $start_time 开始时间
     * @param int $end_time 结束时间
     * @return mixed
     */
    public function getCpuIoInfo($type = 'GetCpuIo', $start_time = 0, $end_time = 0)
    {
李光春's avatar
李光春 已提交
102 103 104 105 106 107
        if (empty($start_time)) {
            $start_time = strtotime(date('Y-m-d'));
        }
        if (empty($end_time)) {
            $end_time = time();
        }
李光春's avatar
李光春 已提交
108 109 110 111 112 113 114 115 116 117
        $this->url = "/ajax?action={$type}&start={$start_time}&end={$end_time}";
        return $this;
    }

    /**
     * 获取网站列表
     * @return mixed
     */
    public function getSites()
    {
李光春's avatar
李光春 已提交
118
        $this->url = "crontab?action=GetDataList";
李光春's avatar
李光春 已提交
119 120 121 122 123 124 125 126 127 128
        $this->where['type'] = 'sites';
        return $this;
    }

    /**
     * 获取数据库列表
     * @return mixed
     */
    public function getDatabases()
    {
李光春's avatar
李光春 已提交
129
        $this->url = 'data?action=getData';
李光春's avatar
李光春 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143
        $this->where['tojs'] = 'database.get_list';
        $this->where['table'] = 'databases';
        $this->where['limit'] = $this->limit;
        $this->where['p'] = $this->page;
        $this->where['order'] = $this->order;
        return $this;
    }

    /**
     * 获取防火墙
     * @return mixed
     */
    public function getFirewalls()
    {
李光春's avatar
李光春 已提交
144
        $this->url = 'data?action=getData';
李光春's avatar
李光春 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158
        $this->where['tojs'] = 'firewall.get_list';
        $this->where['table'] = 'firewall';
        $this->where['limit'] = $this->limit;
        $this->where['p'] = $this->page;
        $this->where['order'] = $this->order;
        return $this;
    }

    /**
     * 获取面板日志
     * @return mixed
     */
    public function getLogs()
    {
李光春's avatar
李光春 已提交
159
        $this->url = 'data?action=getData';
李光春's avatar
李光春 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173
        $this->where['tojs'] = 'firewall.get_log_list';
        $this->where['table'] = 'logs';
        $this->where['limit'] = $this->limit;
        $this->where['p'] = $this->page;
        $this->where['order'] = $this->order;
        return $this;
    }

    /**
     * 获取消息通道
     * @return mixed
     */
    public function getNews()
    {
李光春's avatar
李光春 已提交
174
        $this->url = 'config?action=get_settings';
李光春's avatar
李光春 已提交
175 176 177 178 179 180 181 182 183
        return $this;
    }

    /**
     * 获取网站列表
     * @return mixed
     */
    public function getCronTabs()
    {
李光春's avatar
李光春 已提交
184
        $this->url = 'data?action=getData';
李光春's avatar
李光春 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198
        $this->where['tojs'] = 'site.get_list';
        $this->where['table'] = 'sites';
        $this->where['limit'] = $this->limit;
        $this->where['p'] = $this->page;
        $this->where['order'] = $this->order;
        return $this;
    }

    /**
     * 获取网站分类
     * @return mixed
     */
    public function getTypes()
    {
李光春's avatar
李光春 已提交
199
        $this->url = 'site?action=get_site_types';
李光春's avatar
李光春 已提交
200 201 202 203 204 205 206 207 208
        return $this;
    }

    /**
     * 获取软件列表
     * @return mixed
     */
    public function getSoFts()
    {
李光春's avatar
李光春 已提交
209
        $this->url = 'plugin?action=get_soft_list';
李光春's avatar
李光春 已提交
210 211 212 213 214 215 216 217 218 219 220
        $this->where['p'] = $this->page;
        $this->where['tojs'] = 'soft.get_list';
        return $this;
    }

    /**
     * 获取硬盘信息
     * @return mixed
     */
    public function getDiskInfo()
    {
李光春's avatar
李光春 已提交
221
        $this->url = 'system?action=GetDiskInfo';
李光春's avatar
李光春 已提交
222 223 224 225 226 227 228 229 230
        return $this;
    }

    /**
     * 获取信息系统
     * @return mixed
     */
    public function getSystemTotal()
    {
李光春's avatar
李光春 已提交
231
        $this->url = 'system?action=GetSystemTotal';
李光春's avatar
李光春 已提交
232 233 234 235 236 237 238 239 240
        return $this;
    }

    /**
     * 获取用户信息
     * @return mixed
     */
    public function getUserInfo()
    {
李光春's avatar
李光春 已提交
241
        $this->url = 'ssl?action=GetUserInfo';
李光春's avatar
李光春 已提交
242 243 244 245 246 247 248 249 250
        return $this;
    }

    /**
     * 获取网络信息
     * @return mixed
     */
    public function getNetWork()
    {
李光春's avatar
李光春 已提交
251
        $this->url = 'system?action=GetNetWork';
李光春's avatar
李光春 已提交
252 253 254 255 256 257 258 259 260
        return $this;
    }

    /**
     * 获取插件信息
     * @return mixed
     */
    public function getPlugin()
    {
李光春's avatar
李光春 已提交
261
        $this->url = 'plugin?action=get_index_list';
李光春's avatar
李光春 已提交
262 263 264 265 266 267 268 269 270
        return $this;
    }

    /**
     * 获取软件信息
     * @return mixed
     */
    public function getSoft()
    {
李光春's avatar
李光春 已提交
271
        $this->url = 'plugin?action=get_soft_list';
李光春's avatar
李光春 已提交
272 273 274 275 276 277 278 279 280
        return $this;
    }

    /**
     * 获取更新信息
     * @return mixed
     */
    public function getUpdatePanel()
    {
李光春's avatar
李光春 已提交
281
        $this->url = 'ajax?action=UpdatePanel';
李光春's avatar
李光春 已提交
282 283 284 285 286 287
        return $this;
    }

    /**
     * 当前页码
     * @param int $is
李光春's avatar
李光春 已提交
288
     * @return $this
李光春's avatar
李光春 已提交
289
     */
李光春's avatar
李光春 已提交
290
    public function page(int $is = 1): self
李光春's avatar
李光春 已提交
291 292 293 294 295 296 297 298
    {
        $this->page = $is;
        return $this;
    }

    /**
     * 返回数量
     * @param int $is
李光春's avatar
李光春 已提交
299
     * @return $this
李光春's avatar
李光春 已提交
300
     */
李光春's avatar
李光春 已提交
301
    public function limit(int $is = 15): self
李光春's avatar
李光春 已提交
302 303 304 305 306 307 308 309 310 311
    {
        $this->limit = $is;
        return $this;
    }

    /**
     * 排序
     * @param string $ss
     * @return $this
     */
李光春's avatar
李光春 已提交
312
    public function order(string $ss = 'id desc'): self
李光春's avatar
李光春 已提交
313 314 315 316 317 318 319 320 321 322
    {
        $this->order = $ss;
        return $this;
    }

    /**
     * 查询条件
     * @param array $array
     * @return ApiService
     */
李光春's avatar
李光春 已提交
323
    public function where($array = []): ApiService
李光春's avatar
李光春 已提交
324 325 326 327 328 329 330
    {
        $this->where = $array;
        return $this;
    }

    /**
     * 获取数据和总数
李光春's avatar
李光春 已提交
331
     * @return $this
李光春's avatar
李光春 已提交
332
     */
李光春's avatar
李光春 已提交
333
    private function getDataWithOrderOpt(): self
李光春's avatar
李光春 已提交
334 335 336 337 338 339 340 341
    {
        $this->backtrack['data'] = $this->contents['data'];
        $this->backtrack['orderOpt'] = $this->contents['orderOpt'];
        return $this;
    }

    /**
     * 获取数据和总数
李光春's avatar
李光春 已提交
342
     * @return $this
李光春's avatar
李光春 已提交
343
     */
李光春's avatar
李光春 已提交
344
    private function getDataWithCount(): self
李光春's avatar
李光春 已提交
345
    {
李光春's avatar
李光春 已提交
346 347 348 349 350 351
        if (empty($this->contents['data'])) {
            $this->contents['data'] = [];
        }
        if (!is_array($this->contents['data'])) {
            $this->contents['data'] = [];
        }
李光春's avatar
李光春 已提交
352
        $this->backtrack['data'] = $this->contents;
李光春's avatar
李光春 已提交
353 354 355
        if (empty($this->contents['page'])) {
            $this->contents['page'] = 0;
        }
李光春's avatar
李光春 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
        $this->backtrack['count'] = $this->getCountData($this->contents['page']);
        return $this;
    }

    /**
     * 获取数据
     * @return $this
     */
    private function getData()
    {
        $this->backtrack['data'] = $this->contents;
        return $this;
    }

    /**
     * 发起网络请求
     * @return $this
李光春's avatar
李光春 已提交
373
     * @throws DtaException
李光春's avatar
李光春 已提交
374
     */
李光春's avatar
李光春 已提交
375
    private function getHttp(): self
李光春's avatar
李光春 已提交
376 377 378 379 380 381 382 383 384
    {
        //请求面板接口
        $this->contents = $this->HttpPostCookie($this->url, $this->where);
        return $this;
    }

    /**
     * 返回Array
     * @return array|mixed
李光春's avatar
李光春 已提交
385
     * @throws DtaException
李光春's avatar
李光春 已提交
386 387 388 389
     */
    public function toArray()
    {
        $this->getHttp();
李光春's avatar
李光春 已提交
390
        if ($this->where['type'] === 'sites') {
李光春's avatar
李光春 已提交
391 392 393 394 395 396 397 398 399 400
            $this->getDataWithOrderOpt();
        } else {
            $this->getDataWithCount();
        }
        if (empty($this->backtrack)) {
            return [];
        }
        if (is_array($this->backtrack)) {
            return $this->backtrack;
        }
李光春's avatar
李光春 已提交
401 402 403 404 405 406 407 408 409
        return json_decode($this->backtrack, true);
    }

    /**
     * 发起POST请求
     * @param string $url 网址
     * @param array $data 数据
     * @param bool $is_json 是否返回Json格式
     * @return bool|mixed|string
李光春's avatar
李光春 已提交
410
     * @throws DtaException
李光春's avatar
李光春 已提交
411 412 413
     */
    protected function HttpPostCookie(string $url, array $data = [], bool $is_json = true)
    {
李光春's avatar
李光春 已提交
414 415 416 417 418 419
        if (empty($this->panel)) {
            $this->getConfig();
        }
        if (empty($this->panel)) {
            throw new DtaException('请检查panel参数');
        }
李光春's avatar
李光春 已提交
420 421 422
        //定义cookie保存位置
        $file = app()->getRootPath() . 'runtime/dtapp/bt/cookie/';
        $cookie_file = $file . md5($this->panel) . '.cookie';
李光春's avatar
李光春 已提交
423 424
        if (empty(Files::judgeContents($file)) && !mkdir($file, 0777, true) && !is_dir($file)) {
            throw new \RuntimeException(sprintf('Directory "%s" was not created', $file));
李光春's avatar
李光春 已提交
425
        }
李光春's avatar
李光春 已提交
426
        if (!file_exists($cookie_file)) {
李光春's avatar
李光春 已提交
427
            $fp = fopen($cookie_file, 'wb+');
李光春's avatar
李光春 已提交
428 429
            fclose($fp);
        }
李光春's avatar
李光春 已提交
430 431 432 433 434 435
        if (empty($this->key)) {
            $this->getConfig();
        }
        if (empty($this->key)) {
            throw new DtaException('请检查key参数');
        }
李光春's avatar
李光春 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
        return BtService::instance()
            ->panel($this->panel)
            ->key($this->key)
            ->url($url)
            ->cookie($cookie_file)
            ->data($data)
            ->toArray($is_json);
    }

    /**
     * 获取总数
     * @param string $str
     * @return false|int|string
     */
    protected function getCountData(string $str)
    {
        $start = strpos($str, "共");
        $end = strpos($str, "条数据");
        $count = substr($str, $start + 3, $end - $start - 3);
李光春's avatar
李光春 已提交
455 456 457
        if (empty($count)) {
            return 0;
        }
李光春's avatar
李光春 已提交
458 459 460
        return $count;
    }
}