From 61406ca20bed002ecb9ec5e8cb8e20b1592a8c93 Mon Sep 17 00:00:00 2001 From: Chaim Date: Mon, 18 May 2020 16:46:54 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=E5=AE=9D=E5=A1=94?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/exception/BtException.php | 32 +++ src/service/bt/ApiService.php | 406 ++++++++++++++++++++++++++++++++++ src/service/bt/bin/mimes.php | 29 +++ 3 files changed, 467 insertions(+) create mode 100644 src/exception/BtException.php create mode 100644 src/service/bt/ApiService.php create mode 100644 src/service/bt/bin/mimes.php diff --git a/src/exception/BtException.php b/src/exception/BtException.php new file mode 100644 index 0000000..92d849c --- /dev/null +++ b/src/exception/BtException.php @@ -0,0 +1,32 @@ +getMessage(); + } +} diff --git a/src/service/bt/ApiService.php b/src/service/bt/ApiService.php new file mode 100644 index 0000000..09b53a9 --- /dev/null +++ b/src/service/bt/ApiService.php @@ -0,0 +1,406 @@ +key = $key; + return $this; + } + + public function panel(string $panel) + { + $this->panel = $panel; + return $this; + } + + /** + * 获取监控信息 + * @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) + { + if (empty($start_time)) $start_time = strtotime(date('Y-m-d')); + if (empty($end_time)) $end_time = time(); + $this->url = "/ajax?action={$type}&start={$start_time}&end={$end_time}"; + return $this; + } + + /** + * 获取网站列表 + * @return mixed + */ + public function getSites() + { + $this->url = $this->mimes('GetDataList'); + $this->where['type'] = 'sites'; + return $this; + } + + /** + * 获取数据库列表 + * @return mixed + */ + public function getDatabases() + { + $this->url = $this->mimes('getData'); + $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() + { + $this->url = $this->mimes('getData'); + $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() + { + $this->url = $this->mimes('getData'); + $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() + { + $this->url = $this->mimes('get_settings'); + return $this; + } + + /** + * 获取网站列表 + * @return mixed + */ + public function getCronTabs() + { + $this->url = $this->mimes('getData'); + $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() + { + $this->url = $this->mimes('get_site_types'); + return $this; + } + + /** + * 获取软件列表 + * @return mixed + */ + public function getSoFts() + { + $this->url = $this->mimes('get_soft_list'); + $this->where['p'] = $this->page; + $this->where['tojs'] = 'soft.get_list'; + return $this; + } + + /** + * 获取硬盘信息 + * @return mixed + */ + public function getDiskInfo() + { + $this->url = $this->mimes('GetDiskInfo'); + return $this; + } + + /** + * 获取信息系统 + * @return mixed + */ + public function getSystemTotal() + { + $this->url = $this->mimes('GetSystemTotal'); + return $this; + } + + /** + * 获取用户信息 + * @return mixed + */ + public function getUserInfo() + { + $this->url = $this->mimes('GetUserInfo'); + return $this; + } + + /** + * 获取网络信息 + * @return mixed + */ + public function getNetWork() + { + $this->url = $this->mimes('GetNetWork'); + return $this; + } + + /** + * 获取插件信息 + * @return mixed + */ + public function getPlugin() + { + $this->url = $this->mimes('get_index_list'); + return $this; + } + + /** + * 获取软件信息 + * @return mixed + */ + public function getSoft() + { + $this->url = $this->mimes('get_soft_list'); + return $this; + } + + /** + * 获取更新信息 + * @return mixed + */ + public function getUpdatePanel() + { + $this->url = $this->mimes('UpdatePanel'); + return $this; + } + + /** + * 当前页码 + * @param int $is + * @return ApiService + */ + public function page(int $is = 1) + { + $this->page = $is; + return $this; + } + + /** + * 返回数量 + * @param int $is + * @return ApiService + */ + public function limit(int $is = 15) + { + $this->limit = $is; + return $this; + } + + /** + * 排序 + * @param string $ss + * @return $this + */ + public function order(string $ss = 'id desc') + { + $this->order = $ss; + return $this; + } + + /** + * 查询条件 + * @param array $array + * @return ApiService + */ + public function where($array = []) + { + $this->where = $array; + return $this; + } + + /** + * 获取数据和总数 + */ + private function getDataWithOrderOpt() + { + $this->backtrack['data'] = $this->contents['data']; + $this->backtrack['orderOpt'] = $this->contents['orderOpt']; + return $this; + } + + /** + * 获取数据和总数 + */ + private function getDataWithCount() + { + if (empty($this->contents['data'])) $this->contents['data'] = []; + if (!is_array($this->contents['data'])) $this->contents['data'] = []; + $this->backtrack['data'] = $this->contents; + if (empty($this->contents['page'])) $this->contents['page'] = 0; + $this->backtrack['count'] = $this->getCountData($this->contents['page']); + return $this; + } + + /** + * 获取数据 + * @return $this + */ + private function getData() + { + $this->backtrack['data'] = $this->contents; + return $this; + } + + /** + * 发起网络请求 + * @return $this + * @throws CurlException + */ + private function getHttp() + { + //请求面板接口 + $this->contents = $this->HttpPostCookie($this->url, $this->where); + return $this; + } + + /** + * 返回Array + * @return array|mixed + * @throws CurlException + */ + public function toArray() + { + $this->getHttp(); + switch ($this->where['type']) { + case 'sites': + $this->getDataWithOrderOpt(); + break; + default: + $this->getDataWithCount(); + break; + } + if (empty($this->backtrack)) return []; + if (is_array($this->backtrack)) return $this->backtrack; + return json_decode($this->backtrack, true); + } + + /** + * 获取文件的信息 + * @param $name + * @return string + */ + private function mimes($name): string + { + $mimes = include __DIR__ . '/bin/mimes.php'; + if (isset($mimes[$name])) return '/' . $mimes[$name]; + return ''; + } + + /** + * 发起POST请求 + * @param string $url 网址 + * @param array $data 数据 + * @param bool $is_json 是否返回Json格式 + * @return bool|mixed|string + * @throws CurlException + */ + protected function HttpPostCookie(string $url, array $data = [], bool $is_json = true) + { + $config = [ + 'bt_panel' => $this->panel, + 'bt_key' => $this->key + ]; + //定义cookie保存位置 + $file = app()->getRootPath() . 'runtime/dtapp/bt/cookie/'; + $cookie_file = $file . md5($this->panel) . '.cookie'; + is_dir($file) or mkdir($file, 0777, true); + if (!file_exists($cookie_file)) { + $fp = fopen($cookie_file, 'w+'); + fclose($fp); + } + 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); + if (empty($count)) return 0; + return $count; + } +} diff --git a/src/service/bt/bin/mimes.php b/src/service/bt/bin/mimes.php new file mode 100644 index 0000000..a14363e --- /dev/null +++ b/src/service/bt/bin/mimes.php @@ -0,0 +1,29 @@ + 'data?action=getData', + 'GetDataList' => 'crontab?action=GetDataList', + 'get_settings' => 'config?action=get_settings', + 'GetNetWork' => 'system?action=GetNetWork', + 'GetDiskInfo' => 'system?action=GetDiskInfo', + 'get_site_types' => 'site?action=get_site_types', + 'GetSystemTotal' => 'system?action=GetSystemTotal', + 'UpdatePanel' => 'ajax?action=UpdatePanel', + 'get_soft_list' => 'plugin?action=get_soft_list', + 'get_index_list' => 'plugin?action=get_index_list', + 'GetUserInfo' => 'ssl?action=GetUserInfo', +];