📄 正在查看:admin/control/plugin_control.class.php
大小:7,233 字节 · 修改:2014-01-23 01:42:52 · 行数:250
1<?php
2/**
3 * (C)2012-2014 twcms.com TongWang Inc.
4 * Author: wuzhaohuan <kongphp@gmail.com>
5 */
6
7defined('TWCMS_PATH') or exit;
8
9class plugin_control extends admin_control {
10 // 插件管理
11 public function index() {
12 $plugins = core::get_plugins();
13
14 // 检查是否有图标和设置功能
15 foreach($plugins as &$arr) {
16 if(isset($arr) && is_array($arr)) {
17 foreach($arr as $dir => &$v) {
18 is_file(PLUGIN_PATH.$dir.'/show.jpg') && $v['is_show'] = 1;
19 }
20 }
21 }
22
23 $this->assign('plugins', $plugins);
24 $this->display();
25 }
26
27 // 插件启用
28 public function enable() {
29 $dir = R('dir', 'P');
30 $this->check_plugin($dir);
31 $plugins = $this->get_plugin_config();
32 isset($plugins[$dir]) || E(1, '启用出错,插件未安装!');
33
34 // 如果是编辑器插件,卸载其他编辑器插件
35 if(substr($dir, 0, 7) == 'editor_') {
36 foreach($plugins as $k => $v) {
37 substr($k, 0, 7) == 'editor_' && $plugins[$k]['enable'] = 0;
38 }
39 }
40
41 $plugins[$dir]['enable'] = 1;
42 if($this->set_plugin_config($plugins)) {
43 $this->clear_cache();
44 E(0, '启用完成!');
45 }else{
46 E(1, '写入 plugin.inc.php 文件失败!');
47 }
48 }
49
50 // 插件停用
51 public function disabled() {
52 $dir = R('dir', 'P');
53 $this->check_plugin($dir);
54 $plugins = $this->get_plugin_config();
55 isset($plugins[$dir]) || E(1, '停用出错,插件未安装!');
56
57 $plugins[$dir]['enable'] = 0;
58 if($this->set_plugin_config($plugins)) {
59 $this->clear_cache();
60 E(0, '停用完成!');
61 }else{
62 E(1, '写入 plugin.inc.php 文件失败!');
63 }
64 }
65
66 // 插件删除
67 public function delete() {
68 $dir = R('dir', 'P');
69 $this->check_plugin($dir);
70
71 $plugins = $this->get_plugin_config();
72
73 // 只允许删除停用或未安装的插件
74 if(empty($plugins[$dir]['enable'])) {
75 // 检测有 uninstall.php 文件,则执行卸载
76 $uninstall = PLUGIN_PATH.$dir.'/uninstall.php';
77 if(is_file($uninstall)) {
78 include $uninstall;
79 }
80
81 if(_rmdir(PLUGIN_PATH.$dir)) {
82 if(isset($plugins[$dir])) {
83 unset($plugins[$dir]);
84 if(!$this->set_plugin_config($plugins)) {
85 E(1, '写入 plugin.inc.php 文件失败!');
86 }
87 }
88 E(0, '删除完成!');
89 }else{
90 E(1, '删除出错!');
91 }
92 }else{
93 E(1, '启用的插件不允许删除!');
94 }
95 }
96
97 // 本地插件安装
98 public function install() {
99 $dir = R('dir', 'P');
100 $this->check_plugin($dir);
101
102 $plugins = $this->get_plugin_config();
103 isset($plugins[$dir]) && E(1, '插件已经安装过!');
104
105 $cms_version = $this->get_version($dir);
106 $cms_version && version_compare($cms_version, C('version'), '>') && E(1, '无法安装,最低版本要求:TWCMS '.$cms_version);
107
108 // 检测有 install.php 文件,则执行安装
109 $install = PLUGIN_PATH.$dir.'/install.php';
110 if(is_file($install)) include $install;
111
112 $plugins[$dir] = array('enable' => 0);
113 if(!$this->set_plugin_config($plugins)) E(1, '写入 plugin.inc.php 文件失败!');
114
115 E(0, '安装完成!');
116 }
117
118 // 在线安装插件
119 public function install_plugin() {
120 $dir = R('dir');
121
122 if(empty($dir)) $this->install_tips('插件目录名不能为空!');
123 if(preg_match('/\W/', $dir)) $this->install_tips('插件目录名不正确!');
124 $install_dir = PLUGIN_PATH.$dir;
125 if(is_dir($install_dir)) $this->install_tips('插件目录已存在,已安装过?');
126
127 $this->download($dir);
128 $zipfile = $install_dir.'.zip';
129 try{
130 kp_zip::unzip($zipfile, $install_dir);
131 }catch(Exception $e) {
132 $this->install_tips('解压插件文件出错!');
133 }
134 unlink($zipfile);
135
136 // ====== 开始安装 ======
137 $cms_version = $this->get_version($dir);
138 $cms_version && version_compare($cms_version, C('version'), '>') && $this->install_tips('无法安装,最低版本要求:TWCMS '.$cms_version);
139
140 // 检测有 install.php 文件,则执行安装
141 $install = PLUGIN_PATH.$dir.'/install.php';
142 if(is_file($install)) include $install;
143
144 $plugins = $this->get_plugin_config();
145 $plugins[$dir] = array('enable' => 0);
146 if(!$this->set_plugin_config($plugins)) $this->install_tips('写入配置文件失败!');
147
148 $this->install_tips('下载并安装完成!', 0);
149 }
150
151 // 在线升级插件
152 public function upgrade() {
153 $dir = R('dir');
154
155 if(empty($dir)) $this->install_tips('插件目录名不能为空!');
156 if(preg_match('/\W/', $dir)) $this->install_tips('插件目录名不正确!');
157
158 // 1. 下载插件
159 $this->download($dir, TRUE);
160
161 // 2. 删除旧版插件
162 $install_dir = PLUGIN_PATH.$dir;
163 _rmdir($install_dir);
164
165 // 3. 解压新版插件
166 $zipfile = $install_dir.'.zip';
167 try{
168 kp_zip::unzip($zipfile, $install_dir);
169 }catch(Exception $e) {
170 $this->install_tips('解压插件文件出错!');
171 }
172
173 // 4. 删除安装包
174 unlink($zipfile);
175
176 // 5. 判断是否已安装
177 $plugins = $this->get_plugin_config();
178 if(isset($plugins[$dir])) {
179 // 检测有 upgrade.php 文件,则执行升级
180 $upgrade = PLUGIN_PATH.$dir.'/upgrade.php';
181 if(is_file($upgrade)) include $upgrade;
182 }
183
184 // 6. 清除缓存
185 $this->clear_cache();
186
187 $this->install_tips('下载并升级完成!', 0);
188 }
189
190 // 在线下载插件
191 private function download($dir, $is_upgrade = FALSE) {
192 if(function_exists('set_time_limit')) {
193 set_time_limit(600); // 10分钟
194 $timeout = 300;
195 }else{
196 $timeout = 20;
197 }
198
199 $url = 'http://www.twcms.com/app/download.php?plugin='.$dir.($is_upgrade ? '&upgrade=1' : '');
200 try{
201 $s = fetch_url($url, $timeout);
202 if(empty($s) || substr($s, 0, 2) != 'PK') throw new Exception();
203 }catch(Exception $e) {
204 $this->install_tips('下载插件失败!');
205 }
206 try{
207 file_put_contents(PLUGIN_PATH.$dir.'.zip', $s);
208 }catch(Exception $e) {
209 $this->install_tips('插件写入出错,写入权限不对?');
210 }
211 }
212
213 // 在线安装提示
214 private function install_tips($s, $err = 1) {
215 echo '$(".ajaxtips b").html("'.$s.'");';
216 echo 'var err = '.$err.';';
217 exit;
218 }
219
220 // 检查是否为合法的插件名
221 private function check_plugin($dir) {
222 if(empty($dir)) {
223 E(1, '插件目录名不能为空!');
224 }elseif(preg_match('/\W/', $dir)) {
225 E(1, '插件目录名不正确!');
226 }elseif(!is_dir(PLUGIN_PATH.$dir)) {
227 E(1, '插件目录名不存在!');
228 }
229 }
230
231 // 检查版本
232 private function get_version($dir) {
233 $cfg = is_file(PLUGIN_PATH.$dir.'/conf.php') ? (array)include(PLUGIN_PATH.$dir.'/conf.php') : array();
234 return isset($cfg['cms_version']) ? $cfg['cms_version'] : 0;
235 }
236
237 // 获取插件配置信息
238 private function get_plugin_config() {
239 return is_file(CONFIG_PATH.'plugin.inc.php') ? (array)include(CONFIG_PATH.'plugin.inc.php') : array();
240 }
241
242 // 设置插件配置信息
243 private function set_plugin_config($plugins) {
244 $file = CONFIG_PATH.'plugin.inc.php';
245 !is_file($file) && _is_writable(dirname($file)) && file_put_contents($file, '');
246 if(!_is_writable($file)) return FALSE;
247 return file_put_contents($file, "<?php\nreturn ".var_export($plugins, TRUE).";\n?>");
248 }
249}
250