Passed
Push — v6 ( 25b5d7...533a60 )
by 光春
03:05
created

ApiService::getNetWork()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
// +----------------------------------------------------------------------
4
// | ThinkLibrary 6.0 for ThinkPhP 6.0
5
// +----------------------------------------------------------------------
6
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
7
// +----------------------------------------------------------------------
8
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
9
// +----------------------------------------------------------------------
10
// | 开源协议 ( https://mit-license.org )
11
// +----------------------------------------------------------------------
12
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
13
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
14
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary
15
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary
16
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
17
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
18
// +----------------------------------------------------------------------
19
20
namespace DtApp\ThinkLibrary\service\bt;
21
22
use DtApp\ThinkLibrary\exception\DtaException;
23
use DtApp\ThinkLibrary\facade\Files;
24
use DtApp\ThinkLibrary\Service;
25
use DtApp\ThinkLibrary\service\curl\BtService;
26
27
/**
28
 * 宝塔Api
29
 * https://www.bt.cn/
30
 * Class ApiService
31
 * @package DtApp\ThinkLibrary\service\bt
32
 */
33
class ApiService extends Service
34
{
35
    /**
36
     * @var string
37
     */
38
    private $url = '';
39
40
    /**
41
     * @var int
42
     */
43
    private $page = 1;
44
45
    /**
46
     * @var int
47
     */
48
    private $limit = 15;
49
50
    /**
51
     * @var string
52
     */
53
    private $order = 'id desc';
54
55
    /**
56
     * @var array
57
     */
58
    private $where = [];
59
60
    /**
61
     * @var
62
     */
63
    private $contents, $backtrack, $key, $panel;
64
65
    /**
66
     * @param string $key
67
     * @return $this
68
     */
69
    public function key(string $key): self
70
    {
71
        $this->key = $key;
72
        return $this;
73
    }
74
75
    /**
76
     * @param string $panel
77
     * @return $this
78
     */
79
    public function panel(string $panel)
80
    {
81
        $this->panel = $panel;
82
        return $this;
83
    }
84
85
    /**
86
     * 获取配置信息
87
     * @return $this
88
     */
89
    private function getConfig(): self
90
    {
91
        $this->key = config('dtapp.bt.key');
92
        $this->panel = config('dtapp.bt.panel');
93
        return $this;
94
    }
95
96
    /**
97
     * 获取监控信息
98
     * @param string $type 类型 GetCpuIo = CPU信息/内存 GetDiskIo = 磁盘IO GetNetWorkIo = 网络IO
99
     * @param int $start_time 开始时间
100
     * @param int $end_time 结束时间
101
     * @return mixed
102
     */
103
    public function getCpuIoInfo($type = 'GetCpuIo', $start_time = 0, $end_time = 0)
104
    {
105
        if (empty($start_time)) {
106
            $start_time = strtotime(date('Y-m-d'));
107
        }
108
        if (empty($end_time)) {
109
            $end_time = time();
110
        }
111
        $this->url = "/ajax?action={$type}&start={$start_time}&end={$end_time}";
112
        return $this;
113
    }
114
115
    /**
116
     * 获取网站列表
117
     * @return mixed
118
     */
119
    public function getSites()
120
    {
121
        $this->url = "crontab?action=GetDataList";
122
        $this->where['type'] = 'sites';
123
        return $this;
124
    }
125
126
    /**
127
     * 获取数据库列表
128
     * @return mixed
129
     */
130
    public function getDatabases()
131
    {
132
        $this->url = 'data?action=getData';
133
        $this->where['tojs'] = 'database.get_list';
134
        $this->where['table'] = 'databases';
135
        $this->where['limit'] = $this->limit;
136
        $this->where['p'] = $this->page;
137
        $this->where['order'] = $this->order;
138
        return $this;
139
    }
140
141
    /**
142
     * 获取防火墙
143
     * @return mixed
144
     */
145
    public function getFirewalls()
146
    {
147
        $this->url = 'data?action=getData';
148
        $this->where['tojs'] = 'firewall.get_list';
149
        $this->where['table'] = 'firewall';
150
        $this->where['limit'] = $this->limit;
151
        $this->where['p'] = $this->page;
152
        $this->where['order'] = $this->order;
153
        return $this;
154
    }
155
156
    /**
157
     * 获取面板日志
158
     * @return mixed
159
     */
160
    public function getLogs()
161
    {
162
        $this->url = 'data?action=getData';
163
        $this->where['tojs'] = 'firewall.get_log_list';
164
        $this->where['table'] = 'logs';
165
        $this->where['limit'] = $this->limit;
166
        $this->where['p'] = $this->page;
167
        $this->where['order'] = $this->order;
168
        return $this;
169
    }
170
171
    /**
172
     * 获取消息通道
173
     * @return mixed
174
     */
175
    public function getNews()
176
    {
177
        $this->url = 'config?action=get_settings';
178
        return $this;
179
    }
180
181
    /**
182
     * 获取网站列表
183
     * @return mixed
184
     */
185
    public function getCronTabs()
186
    {
187
        $this->url = 'data?action=getData';
188
        $this->where['tojs'] = 'site.get_list';
189
        $this->where['table'] = 'sites';
190
        $this->where['limit'] = $this->limit;
191
        $this->where['p'] = $this->page;
192
        $this->where['order'] = $this->order;
193
        return $this;
194
    }
195
196
    /**
197
     * 获取网站分类
198
     * @return mixed
199
     */
200
    public function getTypes()
201
    {
202
        $this->url = 'site?action=get_site_types';
203
        return $this;
204
    }
205
206
    /**
207
     * 获取软件列表
208
     * @return mixed
209
     */
210
    public function getSoFts()
211
    {
212
        $this->url = 'plugin?action=get_soft_list';
213
        $this->where['p'] = $this->page;
214
        $this->where['tojs'] = 'soft.get_list';
215
        return $this;
216
    }
217
218
    /**
219
     * 获取硬盘信息
220
     * @return mixed
221
     */
222
    public function getDiskInfo()
223
    {
224
        $this->url = 'system?action=GetDiskInfo';
225
        return $this;
226
    }
227
228
    /**
229
     * 获取信息系统
230
     * @return mixed
231
     */
232
    public function getSystemTotal()
233
    {
234
        $this->url = 'system?action=GetSystemTotal';
235
        return $this;
236
    }
237
238
    /**
239
     * 获取用户信息
240
     * @return mixed
241
     */
242
    public function getUserInfo()
243
    {
244
        $this->url = 'ssl?action=GetUserInfo';
245
        return $this;
246
    }
247
248
    /**
249
     * 获取网络信息
250
     * @return mixed
251
     */
252
    public function getNetWork()
253
    {
254
        $this->url = 'system?action=GetNetWork';
255
        return $this;
256
    }
257
258
    /**
259
     * 获取插件信息
260
     * @return mixed
261
     */
262
    public function getPlugin()
263
    {
264
        $this->url = 'plugin?action=get_index_list';
265
        return $this;
266
    }
267
268
    /**
269
     * 获取软件信息
270
     * @return mixed
271
     */
272
    public function getSoft()
273
    {
274
        $this->url = 'plugin?action=get_soft_list';
275
        return $this;
276
    }
277
278
    /**
279
     * 获取更新信息
280
     * @return mixed
281
     */
282
    public function getUpdatePanel()
283
    {
284
        $this->url = 'ajax?action=UpdatePanel';
285
        return $this;
286
    }
287
288
    /**
289
     * 当前页码
290
     * @param int $is
291
     * @return $this
292
     */
293
    public function page(int $is = 1): self
294
    {
295
        $this->page = $is;
296
        return $this;
297
    }
298
299
    /**
300
     * 返回数量
301
     * @param int $is
302
     * @return $this
303
     */
304
    public function limit(int $is = 15): self
305
    {
306
        $this->limit = $is;
307
        return $this;
308
    }
309
310
    /**
311
     * 排序
312
     * @param string $ss
313
     * @return $this
314
     */
315
    public function order(string $ss = 'id desc'): self
316
    {
317
        $this->order = $ss;
318
        return $this;
319
    }
320
321
    /**
322
     * 查询条件
323
     * @param array $array
324
     * @return ApiService
325
     */
326
    public function where($array = []): ApiService
327
    {
328
        $this->where = $array;
329
        return $this;
330
    }
331
332
    /**
333
     * 获取数据和总数
334
     * @return $this
335
     */
336
    private function getDataWithOrderOpt(): self
337
    {
338
        $this->backtrack['data'] = $this->contents['data'];
339
        $this->backtrack['orderOpt'] = $this->contents['orderOpt'];
340
        return $this;
341
    }
342
343
    /**
344
     * 获取数据和总数
345
     * @return $this
346
     */
347
    private function getDataWithCount(): self
348
    {
349
        if (empty($this->contents['data'])) {
350
            $this->contents['data'] = [];
351
        }
352
        if (!is_array($this->contents['data'])) {
353
            $this->contents['data'] = [];
354
        }
355
        $this->backtrack['data'] = $this->contents;
356
        if (empty($this->contents['page'])) {
357
            $this->contents['page'] = 0;
358
        }
359
        $this->backtrack['count'] = $this->getCountData($this->contents['page']);
360
        return $this;
361
    }
362
363
    /**
364
     * 获取数据
365
     * @return $this
366
     */
367
    private function getData()
0 ignored issues
show
Unused Code introduced by
The method getData() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
368
    {
369
        $this->backtrack['data'] = $this->contents;
370
        return $this;
371
    }
372
373
    /**
374
     * 发起网络请求
375
     * @return $this
376
     * @throws DtaException
377
     */
378
    private function getHttp(): self
379
    {
380
        //请求面板接口
381
        $this->contents = $this->HttpPostCookie($this->url, $this->where);
382
        return $this;
383
    }
384
385
    /**
386
     * 返回Array
387
     * @return array|mixed
388
     * @throws DtaException
389
     */
390
    public function toArray()
391
    {
392
        $this->getHttp();
393
        if ($this->where['type'] === 'sites') {
394
            $this->getDataWithOrderOpt();
395
        } else {
396
            $this->getDataWithCount();
397
        }
398
        if (empty($this->backtrack)) {
399
            return [];
400
        }
401
        if (is_array($this->backtrack)) {
402
            return $this->backtrack;
403
        }
404
        return json_decode($this->backtrack, true);
405
    }
406
407
    /**
408
     * 发起POST请求
409
     * @param string $url 网址
410
     * @param array $data 数据
411
     * @param bool $is_json 是否返回Json格式
412
     * @return bool|mixed|string
413
     * @throws DtaException
414
     */
415
    protected function HttpPostCookie(string $url, array $data = [], bool $is_json = true)
416
    {
417
        if (empty($this->panel)) {
418
            $this->getConfig();
419
        }
420
        if (empty($this->panel)) {
421
            throw new DtaException('请检查panel参数');
422
        }
423
        //定义cookie保存位置
424
        $file = app()->getRootPath() . 'runtime/dtapp/bt/cookie/';
425
        $cookie_file = $file . md5($this->panel) . '.cookie';
426
        if (empty(Files::judgeContents($file)) && !mkdir($file, 0777, true) && !is_dir($file)) {
427
            throw new \RuntimeException(sprintf('Directory "%s" was not created', $file));
428
        }
429
        if (!file_exists($cookie_file)) {
430
            $fp = fopen($cookie_file, 'wb+');
431
            fclose($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

431
            fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
432
        }
433
        if (empty($this->key)) {
434
            $this->getConfig();
435
        }
436
        if (empty($this->key)) {
437
            throw new DtaException('请检查key参数');
438
        }
439
        return BtService::instance()
440
            ->panel($this->panel)
441
            ->key($this->key)
442
            ->url($url)
443
            ->cookie($cookie_file)
444
            ->data($data)
445
            ->toArray($is_json);
446
    }
447
448
    /**
449
     * 获取总数
450
     * @param string $str
451
     * @return false|int|string
452
     */
453
    protected function getCountData(string $str)
454
    {
455
        $start = strpos($str, "共");
456
        $end = strpos($str, "条数据");
457
        $count = substr($str, $start + 3, $end - $start - 3);
458
        if (empty($count)) {
459
            return 0;
460
        }
461
        return $count;
462
    }
463
}
464