Skip to content

Commit

Permalink
- 测试版本
Browse files Browse the repository at this point in the history
  • Loading branch information
dtapps committed Mar 18, 2020
0 parents commit 96112e2
Show file tree
Hide file tree
Showing 17 changed files with 680 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 李光春

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<img align="right" width="100" src="https://cdn.oss.liguangchun.cn/04/999e9f2f06d396968eacc10ce9bc8a.png" alt="dtApp Logo"/>

<h1 align="left"><a href="https://www.liguangchun.cn/">宝塔Api接口</a></h1>

📦 宝塔Api接口 PHP扩展包

[![Latest Stable Version](https://poser.pugx.org/liguangchun/bt/v/stable)](https://packagist.dtapp.net/#liguangchun/bt)
[![Latest Unstable Version](https://poser.pugx.org/liguangchun/bt/v/unstable)](https://packagist.dtapp.net/#liguangchun/bt)
[![Total Downloads](https://poser.pugx.org/liguangchun/bt/downloads)](https://packagist.dtapp.net/#liguangchun/bt)
[![License](https://poser.pugx.org/liguangchun/bt/license)](https://packagist.dtapp.net/#liguangchun/bt)

## 依赖环境

1. PHP7.0 版本及以上(低版本和7.4版本未做兼容处理!)


# 安装
## 配置源库

```text
"repositories": {
"0": {
"type": "composer",
"url": "https://packagist.liguangchun.cn"
},
"packagist": {
"type": "composer",
"url": "https://mirrors.aliyun.com/composer/"
}
}
```

## 安装

```text
composer require liguangchun/bt -vvv
```

## 更新

```text
composer update liguangchun/bt -vvv
```

## 删除

```text
composer remove liguangchun/bt -vvv
```
2 changes: 2 additions & 0 deletions dtapp.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
composer update -vvv --profile --prefer-dist --optimize-autoloader
66 changes: 66 additions & 0 deletions src/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* (c) Chaim <gc@dtapp.net>
*/

namespace LiGuAngChUn\Bt;

class Base
{
/**
* 配置
* @var
*/
public $config;

public function __construct(array $options)
{
if (empty($options['key'])) throw new BtException('请检查配置 接口密钥:[key],示例:x0m1NM1yumUVTyzLrpoJ4tgbVAZFzWVj');
if (empty($options['panel'])) throw new BtException('请检查配置 面板地址:[panel],示例:http://127.0.0.1:8888');
$this->config = new DataArray($options);
}

/**
* 发起POST请求
* @param String $url 目标网填,带http://
* @param array $data 欲提交的数据
* @param int $timeout
* @return string
*/
protected function HttpPostCookie($url, $data = [], $timeout = 60)
{
$p_data = $this->GetKeyData();
//定义cookie保存位置
$cookie_file = __DIR__ . '/../cookie/' . md5($this->config->get('panel')) . '.cookie';
if (!file_exists($cookie_file)) {
$fp = fopen($cookie_file, 'w+');
fclose($fp);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->config->get('panel') . $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array_merge($p_data, $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;
}

/**
* 构造带有签名的关联数组
*/
private function GetKeyData()
{
$now_time = time();
return array(
'request_token' => md5($now_time . '' . md5($this->config->get('key'))),
'request_time' => $now_time
);
}
}
19 changes: 19 additions & 0 deletions src/BtException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* (c) Chaim <gc@dtapp.net>
*/

namespace LiGuAngChUn\Bt;

/**
* 错误处理
* Class BtException
* @package LiGuAngChUn\Bt
*/
class BtException extends \Exception
{
public function errorMessage()
{
return $this->getMessage();
}
}
32 changes: 32 additions & 0 deletions src/Control.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* (c) Chaim <gc@dtapp.net>
*/


namespace LiGuAngChUn\Bt;


/**
* 系统监控
* Class Control
* @package LiGuAngChUn\Bt
*/
class Control extends Base
{
/**
* 获取监控信息
* @param string $type 类型 GetCpuIo = CPU信息/内存 GetDiskIo = 磁盘IO GetNetWorkIo = 网络IO
* @param string $start_time 开始时间
* @param string $end_time 结束时间
* @return mixed
*/
public function getInfo($type = 'GetCpuIo', $start_time = '1584547201', $end_time = '1584547904')
{
$url = "/ajax?action={$type}&start={$start_time}&end={$end_time}";
//请求面板接口
$result = $this->HttpPostCookie($url, []);
//解析JSON数据
return json_decode($result, true);
}
}
29 changes: 29 additions & 0 deletions src/CronTab.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* (c) Chaim <gc@dtapp.net>
*/


namespace LiGuAngChUn\Bt;

/**
* 计划任务
* Class CronTab
* @package LiGuAngChUn\Bt
*/
class CronTab extends Base
{
/**
* 获取网站列表
* @return mixed
*/
public function getDataList()
{
$url = '/crontab?action=GetDataList';
$p_data['type'] = 'sites';
//请求面板接口
$result = $this->HttpPostCookie($url, $p_data);
//解析JSON数据
return json_decode($result, true);
}
}
110 changes: 110 additions & 0 deletions src/DataArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* (c) Chaim <gc@dtapp.net>
*/


namespace LiGuAngChUn\Bt;


class DataArray implements \ArrayAccess
{
/**
* 当前配置值
* @var array
*/
private $config = [];

/**
* DataArray constructor.
* @param array $options
*/
public function __construct(array $options)
{
$this->config = $options;
}

/**
* 设置配置项值
* @param $offset
* @param $value
*/
public function set($offset, $value)
{
$this->offsetSet($offset, $value);
}

/**
* 获取配置项参数
* @param null $offset
* @return mixed|void
*/
public function get($offset = null)
{
return $this->offsetGet($offset);
}

/**
* 合并数据到对象
* @param array $data 需要合并的数据
* @param bool $append 是否追加数据
* @return array
*/
public function merge(array $data, $append = false)
{
if ($append) {
return $this->config = array_merge($this->config, $data);
}
return array_merge($this->config, $data);
}

/**
* 设置配置项值
* @param string $offset
* @param string|array|null|integer $value
*/
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
$this->config[] = $value;
} else {
$this->config[$offset] = $value;
}
}

/**
* 判断配置Key是否存在
* @param string $offset
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->config[$offset]);
}

/**
* 清理配置项
* @param string|null $offset
*/
public function offsetUnset($offset = null)
{
if (is_null($offset)) {
$this->config = [];
} else {
unset($this->config[$offset]);
}
}

/**
* 获取配置项参数
* @param string|null $offset
* @return array|string|null
*/
public function offsetGet($offset = null)
{
if (is_null($offset)) {
return $this->config;
}
return isset($this->config[$offset]) ? $this->config[$offset] : null;
}
}
34 changes: 34 additions & 0 deletions src/Database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* (c) Chaim <gc@dtapp.net>
*/


namespace LiGuAngChUn\Bt;

/**
* 数据库管理
* Class Database
* @package LiGuAngChUn\Bt
*/
class Database extends Base
{
/**
* 获取数据库列表
* @return mixed
*/
public function getList()
{
$url = '/data?action=getData';
$p_data['tojs'] = 'database.get_list';
$p_data['table'] = 'databases';
$p_data['limit'] = 15;
$p_data['p'] = 1;
$p_data['search'] = '';
$p_data['order'] = 'id desc';
//请求面板接口
$result = $this->HttpPostCookie($url, $p_data);
//解析JSON数据
return json_decode($result, true);
}
}
17 changes: 17 additions & 0 deletions src/Files.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* (c) Chaim <gc@dtapp.net>
*/


namespace LiGuAngChUn\Bt;

/**
* 文件管理
* Class Files
* @package LiGuAngChUn\Bt
*/
class Files extends Base
{

}

0 comments on commit 96112e2

Please sign in to comment.