审查视图

application/admin/controller/Addon.php 11.4 KB
Karson authored
1 2 3 4 5
<?php

namespace app\admin\controller;

use app\common\controller\Backend;
6
use fast\Http;
Karson authored
7 8
use think\addons\AddonException;
use think\addons\Service;
9
use think\Cache;
Karson authored
10 11 12 13 14 15 16
use think\Config;
use think\Exception;

/**
 * 插件管理
 *
 * @icon fa fa-circle-o
Karson authored
17
 * @remark 可在线安装、卸载、禁用、启用插件,同时支持添加本地插件。FastAdmin已上线插件商店 ,你可以发布你的免费或付费插件:<a href="https://www.fastadmin.net/store.html" target="_blank">https://www.fastadmin.net/store.html</a>
Karson authored
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 */
class Addon extends Backend
{

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 查看
     */
    public function index()
    {
        $addons = get_addon_list();
35
        foreach ($addons as $k => &$v) {
Karson authored
36 37 38 39 40 41 42 43 44 45 46 47 48
            $config = get_addon_config($v['name']);
            $v['config'] = $config ? 1 : 0;
        }
        $this->assignconfig(['addons' => $addons]);
        return $this->view->fetch();
    }

    /**
     * 配置
     */
    public function config($ids = NULL)
    {
        $name = $this->request->get("name");
49
        if (!$name) {
Karson authored
50 51
            $this->error(__('Parameter %s can not be empty', $ids ? 'id' : 'name'));
        }
52
        if (!is_dir(ADDON_PATH . $name)) {
Karson authored
53 54 55 56 57 58
            $this->error(__('Directory not found'));
        }
        $info = get_addon_info($name);
        $config = get_addon_fullconfig($name);
        if (!$info)
            $this->error(__('No Results were found'));
59
        if ($this->request->isPost()) {
Karson authored
60
            $params = $this->request->post("row/a");
61 62 63
            if ($params) {
                foreach ($config as $k => &$v) {
                    if (isset($params[$v['name']])) {
64 65 66 67 68 69
                        if ($v['type'] == 'array') {
                            $params[$v['name']] = is_array($params[$v['name']]) ? $params[$v['name']] : (array)json_decode($params[$v['name']], true);
                            $value = $params[$v['name']];
                        } else {
                            $value = is_array($params[$v['name']]) ? implode(',', $params[$v['name']]) : $params[$v['name']];
                        }
Karson authored
70 71 72
                        $v['value'] = $value;
                    }
                }
73
                try {
Karson authored
74 75
                    //更新配置文件
                    set_addon_fullconfig($name, $config);
76
                    Service::refresh();
Karson authored
77
                    $this->success();
78
                } catch (Exception $e) {
79
                    $this->error(__($e->getMessage()));
Karson authored
80 81 82 83 84
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("addon", ['info' => $info, 'config' => $config]);
85 86 87
        $configFile = ADDON_PATH . $name . DS . 'config.html';
        $viewFile = is_file($configFile) ? $configFile : '';
        return $this->view->fetch($viewFile);
Karson authored
88 89 90 91 92 93 94 95
    }

    /**
     * 安装
     */
    public function install()
    {
        $name = $this->request->post("name");
96 97
        $force = (int)$this->request->post("force");
        if (!$name) {
Karson authored
98 99
            $this->error(__('Parameter %s can not be empty', 'name'));
        }
100
        try {
Karson authored
101 102
            $uid = $this->request->post("uid");
            $token = $this->request->post("token");
103 104 105 106 107 108 109 110 111
            $version = $this->request->post("version");
            $faversion = $this->request->post("faversion");
            $extend = [
                'uid'       => $uid,
                'token'     => $token,
                'version'   => $version,
                'faversion' => $faversion
            ];
            Service::install($name, $force, $extend);
Karson authored
112 113
            $info = get_addon_info($name);
            $info['config'] = get_addon_config($name) ? 1 : 0;
114
            $info['state'] = 1;
Karson authored
115
            $this->success(__('Install successful'), null, ['addon' => $info]);
116
        } catch (AddonException $e) {
117
            $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
118
        } catch (Exception $e) {
119
            $this->error(__($e->getMessage()), $e->getCode());
Karson authored
120 121 122 123 124 125 126 127 128
        }
    }

    /**
     * 卸载
     */
    public function uninstall()
    {
        $name = $this->request->post("name");
129 130
        $force = (int)$this->request->post("force");
        if (!$name) {
Karson authored
131 132
            $this->error(__('Parameter %s can not be empty', 'name'));
        }
133
        try {
Karson authored
134 135
            Service::uninstall($name, $force);
            $this->success(__('Uninstall successful'));
136
        } catch (AddonException $e) {
137
            $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
138
        } catch (Exception $e) {
139
            $this->error(__($e->getMessage()));
Karson authored
140 141 142 143 144 145 146 147 148 149
        }
    }

    /**
     * 禁用启用
     */
    public function state()
    {
        $name = $this->request->post("name");
        $action = $this->request->post("action");
150 151
        $force = (int)$this->request->post("force");
        if (!$name) {
Karson authored
152 153
            $this->error(__('Parameter %s can not be empty', 'name'));
        }
154
        try {
Karson authored
155 156 157
            $action = $action == 'enable' ? $action : 'disable';
            //调用启用、禁用的方法
            Service::$action($name, $force);
158
            Cache::rm('__menu__');
Karson authored
159
            $this->success(__('Operate successful'));
160
        } catch (AddonException $e) {
161
            $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
162
        } catch (Exception $e) {
163
            $this->error(__($e->getMessage()));
Karson authored
164 165 166 167 168 169 170 171 172 173 174 175
        }
    }

    /**
     * 本地上传
     */
    public function local()
    {
        Config::set('default_return_type', 'json');

        $file = $this->request->file('file');
        $addonTmpDir = RUNTIME_PATH . 'addons' . DS;
176
        if (!is_dir($addonTmpDir)) {
Karson authored
177 178 179
            @mkdir($addonTmpDir, 0755, true);
        }
        $info = $file->rule('uniqid')->validate(['size' => 10240000, 'ext' => 'zip'])->move($addonTmpDir);
180
        if ($info) {
Karson authored
181 182 183
            $tmpName = substr($info->getFilename(), 0, stripos($info->getFilename(), '.'));
            $tmpAddonDir = ADDON_PATH . $tmpName . DS;
            $tmpFile = $addonTmpDir . $info->getSaveName();
184
            try {
Karson authored
185 186 187
                Service::unzip($tmpName);
                @unlink($tmpFile);
                $infoFile = $tmpAddonDir . 'info.ini';
188
                if (!is_file($infoFile)) {
Karson authored
189 190 191 192 193
                    throw new Exception(__('Addon info file was not found'));
                }

                $config = Config::parse($infoFile, '', $tmpName);
                $name = isset($config['name']) ? $config['name'] : '';
194
                if (!$name) {
Karson authored
195 196 197 198
                    throw new Exception(__('Addon info file data incorrect'));
                }

                $newAddonDir = ADDON_PATH . $name . DS;
199
                if (is_dir($newAddonDir)) {
Karson authored
200 201 202 203 204
                    throw new Exception(__('Addon already exists'));
                }

                //重命名插件文件夹
                rename($tmpAddonDir, $newAddonDir);
205
                try {
Karson authored
206 207
                    //默认禁用该插件
                    $info = get_addon_info($name);
208
                    if ($info['state']) {
Karson authored
209 210 211 212 213 214
                        $info['state'] = 0;
                        set_addon_info($name, $info);
                    }

                    //执行插件的安装方法
                    $class = get_addon_class($name);
215
                    if (class_exists($class)) {
Karson authored
216 217 218 219 220 221 222 223 224
                        $addon = new $class();
                        $addon->install();
                    }

                    //导入SQL
                    Service::importsql($name);

                    $info['config'] = get_addon_config($name) ? 1 : 0;
                    $this->success(__('Offline installed tips'), null, ['addon' => $info]);
225
                } catch (Exception $e) {
Karson authored
226
                    @rmdirs($newAddonDir);
227
                    throw new Exception(__($e->getMessage()));
Karson authored
228
                }
229
            } catch (Exception $e) {
Karson authored
230 231
                @unlink($tmpFile);
                @rmdirs($tmpAddonDir);
232
                $this->error(__($e->getMessage()));
Karson authored
233
            }
234
        } else {
Karson authored
235
            // 上传失败获取错误信息
236
            $this->error(__($file->getError()));
Karson authored
237 238 239 240
        }
    }

    /**
241 242 243 244 245
     * 更新插件
     */
    public function upgrade()
    {
        $name = $this->request->post("name");
246
        if (!$name) {
247 248
            $this->error(__('Parameter %s can not be empty', 'name'));
        }
249
        try {
250 251 252 253 254 255 256 257 258 259 260 261
            $uid = $this->request->post("uid");
            $token = $this->request->post("token");
            $version = $this->request->post("version");
            $faversion = $this->request->post("faversion");
            $extend = [
                'uid'       => $uid,
                'token'     => $token,
                'version'   => $version,
                'faversion' => $faversion
            ];
            //调用更新的方法
            Service::upgrade($name, $extend);
262
            Cache::rm('__menu__');
263
            $this->success(__('Operate successful'));
264
        } catch (AddonException $e) {
265
            $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
266
        } catch (Exception $e) {
267
            $this->error(__($e->getMessage()));
Karson authored
268 269 270 271 272 273 274 275
        }
    }

    /**
     * 已装插件
     */
    public function downloaded()
    {
276 277 278
        $offset = (int)$this->request->get("offset");
        $limit = (int)$this->request->get("limit");
        $filter = $this->request->get("filter");
279 280
        $search = $this->request->get("search");
        $search = htmlspecialchars(strip_tags($search));
281 282 283 284 285 286 287 288 289 290 291 292 293 294
        $onlineaddons = Cache::get("onlineaddons");
        if (!is_array($onlineaddons)) {
            $onlineaddons = [];
            $result = Http::sendRequest(config('fastadmin.api_url') . '/addon/index');
            if ($result['ret']) {
                $json = json_decode($result['msg'], TRUE);
                $rows = isset($json['rows']) ? $json['rows'] : [];
                foreach ($rows as $index => $row) {
                    $onlineaddons[$row['name']] = $row;
                }
            }
            Cache::set("onlineaddons", $onlineaddons, 600);
        }
        $filter = (array)json_decode($filter, true);
Karson authored
295 296
        $addons = get_addon_list();
        $list = [];
297
        foreach ($addons as $k => $v) {
298 299
            if ($search && stripos($v['name'], $search) === FALSE && stripos($v['intro'], $search) === FALSE)
                continue;
300
301
            if (isset($onlineaddons[$v['name']])) {
302
                $v = array_merge($v, $onlineaddons[$v['name']]);
303 304 305 306 307 308 309 310
            } else {
                $v['category_id'] = 0;
                $v['flag'] = '';
                $v['banner'] = '';
                $v['image'] = '';
                $v['donateimage'] = '';
                $v['demourl'] = '';
                $v['price'] = '0.00';
311 312
                $v['screenshots'] = [];
                $v['releaselist'] = [];
313
            }
314 315
            $v['url'] = addon_url($v['name']);
            $v['createtime'] = filemtime(ADDON_PATH . $v['name']);
316 317 318
            if ($filter && isset($filter['category_id']) && is_numeric($filter['category_id']) && $filter['category_id'] != $v['category_id']) {
                continue;
            }
Karson authored
319 320
            $list[] = $v;
        }
321
        $total = count($list);
322
        if ($limit) {
323 324
            $list = array_slice($list, $offset, $limit);
        }
325
        $result = array("total" => $total, "rows" => $list);
Karson authored
326 327 328 329 330 331

        $callback = $this->request->get('callback') ? "jsonp" : "json";
        return $callback($result);
    }

}