Plugin.php
5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace app\admin\controller;
use think\facade\Request;
use think\facade\Env;
class Plugin extends Common{
public function initialize(){
parent::initialize();
// 更新插件
$this->insertPlugin($this->scanPlugin());
}
public function index(){
if(Request::isAjax()) {
$list = db('plugin')->select();
return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
}
return $this->fetch();
}
public function set(){
$condition['type'] = input('param.type');
$condition['code'] = input('param.code');
$model = db('plugin');
$info = $model->where($condition)->find();
if(!$info){
exit($this->error("不存在该插件"));
}
$this->assign('title','配置登录插件');
$info['config'] = unserialize($info['config']);
$this->assign('info',$info);
$this->assign('config_value',unserialize($info['config_value']));
return $this->fetch();
}
public function update(){
$condition['type'] = input('post.type');
$condition['code'] = input('post.code');
$model = db('plugin');
$config = input('post.config/a');
//空格过滤
$config = trim_array_element($config);
if($config){
$config = serialize($config);
}
$row = $model->where($condition)->update(array('config_value'=>$config));
if($row!==false){
$result['code'] = 1;
$result['msg'] = '修改成功!';
}else{
$result['code'] = 0;
$result['msg'] = '修改失败!';
}
return $result;
}
/**
* 插件安装卸载
*/
public function install(){
$condition['type'] = input('post.type');
$condition['code'] = input('post.code');
$update['status'] = input('post.install');
$model = db('plugin');
//卸载插件时 删除配置信息
if($update['status']==0){
$row = db('plugin')->where($condition)->delete();
}else{
$row = $model->where($condition)->update($update);
}
if($row){
$info['code'] = 1;
$info['url'] = url($condition['type']);
$info['msg'] = $update['status'] ? '安装成功!' : '卸载成功!';
}else{
$info['code'] = 0;
$info['msg'] = $update['status'] ? '安装失败' : '卸载失败';
}
return $info;
}
/**
* 插件目录扫描
* @return array 返回目录数组
*/
private function scanPlugin(){
$plugin_field = array();
$field = Env::get('root_path')."/plugins";
$plugin_field = $this->dirscan($field);
$plugin_list=array();
foreach($plugin_field as $k=>$v){
$plugin_list[$v] = $this->dirscan($field.'/'.$v);
}
foreach($plugin_list as $k=>$v){
foreach($v as $k2=>$v2){
if(!file_exists(Env::get('root_path').'/plugins/'.$k.'/'.$v2.'/config.php')){
unset($plugin_list[$k][$k2]);
}else {
$plugin_list[$k][$v2] = include(Env::get('root_path').'/plugins/'.$k.'/'.$v2.'/config.php');
unset($plugin_list[$k][$k2]);
}
}
}
//dump($plugin_list);
return $plugin_list;
}
/**
* 获取插件目录列表
* @param $dir
* @return array
*/
private function dirscan($dir){
$dirArray = array();
if (false != ($handle = opendir($dir))) {
$i=0;
while ( false !== ($file = readdir ( $handle )) ) {
//去掉"“.”、“..”以及带“.xxx”后缀的文件
if ($file != "." && $file != ".."&&!strpos($file,".")) {
$dirArray[$i]=$file;
$i++;
}
}
//关闭句柄
closedir ( $handle );
}
return $dirArray;
}
/**
* 更新插件到数据库
* @param $plugin_list 本地插件数组
*/
private function insertPlugin($plugin_list){
$d_list = db('plugin')->field('code,type')->select(); // 数据库
$cv = array();
$new_arr = array(); // 本地
//插件类型
foreach($plugin_list as $pt=>$pv){
// 本地对比数据库
foreach($pv as $t=>$v){
foreach ($v['config'] as $key=>$val){
$cv[$val['name']] = $val['value'];
}
$tmp['code'] = $v['code'];
$tmp['type'] = $pt;
$new_arr[] = $tmp;
// 对比数据库 本地有 数据库没有
$is_exit = db('plugin')->where(array('type'=>$pt,'code'=>$v['code']))->find();
if(empty($is_exit)){
$add['code'] = $v['code'];
$add['name'] = $v['name'];
$add['version'] = $v['version'];
$add['icon'] = $v['icon'];
$add['author'] = $v['author'];
$add['desc'] = $v['desc'];
if(isset($v['bank_code'])){
$add['bank_code'] = serialize($v['bank_code']);
}
$add['type'] = $pt;
$add['scene'] = $v['scene'];
$add['config'] = empty($v['config']) ? '' : serialize($v['config']);
$add['config_value'] = empty($cv) ? '' : serialize($cv);
db('plugin')->insert($add);
}
}
}
foreach($d_list as $k=>$v){
if(!in_array($v,$new_arr)){
db('plugin')->where($v)->delete();
}
}
}
}