From 4ee9156229a06c61a62c9ec7b1253101a9b8a057 Mon Sep 17 00:00:00 2001 From: molong Date: Sat, 17 Sep 2016 17:54:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=92=E4=BB=B6=E5=8A=9F=E8=83=BD=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/systeminfo/Systeminfo.php | 102 +++++------ application/admin/view/addons/index.html | 3 +- application/admin/view/public/base.html | 2 +- application/common/controller/Addons.php | 4 +- application/common/controller/Admin.php | 1 + application/common/model/Addons.php | 207 +++++++++++------------ application/common/model/Hooks.php | 17 +- 7 files changed, 172 insertions(+), 164 deletions(-) diff --git a/addons/systeminfo/Systeminfo.php b/addons/systeminfo/Systeminfo.php index c6292f1a..29408a5d 100644 --- a/addons/systeminfo/Systeminfo.php +++ b/addons/systeminfo/Systeminfo.php @@ -7,7 +7,6 @@ // | Author: yangweijie // +---------------------------------------------------------------------- - namespace addons\systeminfo; use app\common\controller\Addons; @@ -16,62 +15,63 @@ use app\common\controller\Addons; * @author thinkphp */ -class Systeminfo extends Addons{ +class Systeminfo extends Addons { - public $info = array( - 'name'=>'Systeminfo', - 'title'=>'系统环境信息', - 'description'=>'用于显示一些服务器的信息', - 'status'=>1, - 'author'=>'molong', - 'version'=>'0.1' - ); + public $info = array( + 'name' => 'Systeminfo', + 'title' => '系统环境信息', + 'description' => '用于显示一些服务器的信息', + 'status' => 1, + 'author' => 'molong', + 'version' => '0.1', + ); - public function install(){ - return true; - } + public function install() { + return true; + } - public function uninstall(){ - return true; - } + public function uninstall() { + return true; + } - //实现的AdminIndex钩子方法 - public function AdminIndex($param){ - $config = $this->getConfig(); - - if(false){//extension_loaded('curl') - $url = 'http://www.tensent.cn/index.php?m=home&c=version&a=check_version'; - $params = array( - 'version' => ONETHINK_VERSION, - 'domain' => $_SERVER['HTTP_HOST'], - 'auth' => sha1(config('DATA_AUTH_KEY')), - ); + //实现的AdminIndex钩子方法 + public function AdminIndex($param) { + $config = $this->getConfig(); - $vars = http_build_query($params); - $opts = array( - CURLOPT_TIMEOUT => 5, - CURLOPT_RETURNTRANSFER => 1, - CURLOPT_URL => $url, - CURLOPT_POST => 1, - CURLOPT_POSTFIELDS => $vars, - CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'], - ); + if (false) { +//extension_loaded('curl') + $url = 'http://www.tensent.cn/index.php?m=home&c=version&a=check_version'; + $params = array( + 'version' => ONETHINK_VERSION, + 'domain' => $_SERVER['HTTP_HOST'], + 'auth' => sha1(config('DATA_AUTH_KEY')), + ); - /* 初始化并执行curl请求 */ - $ch = curl_init(); - curl_setopt_array($ch, $opts); - $data = curl_exec($ch); - $error = curl_error($ch); - curl_close($ch); - } + $vars = http_build_query($params); + $opts = array( + CURLOPT_TIMEOUT => 5, + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_URL => $url, + CURLOPT_POST => 1, + CURLOPT_POSTFIELDS => $vars, + CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'], + ); - if(!empty($data) && strlen($data)<400 ){ - $config['new_version'] = $data; - } + /* 初始化并执行curl请求 */ + $ch = curl_init(); + curl_setopt_array($ch, $opts); + $data = curl_exec($ch); + $error = curl_error($ch); + curl_close($ch); + } - $this->assign('addons_config', $config); - if($config['display']){ - $this->template('widget'); - } - } + if (!empty($data) && strlen($data) < 400) { + $config['new_version'] = $data; + } + + $this->assign('addons_config', $config); + if ($config['display']) { + $this->template('widget'); + } + } } \ No newline at end of file diff --git a/application/admin/view/addons/index.html b/application/admin/view/addons/index.html index 1a95b2e8..4e6e8283 100644 --- a/application/admin/view/addons/index.html +++ b/application/admin/view/addons/index.html @@ -9,6 +9,7 @@

{$meta_title}

+ 更 新 新 增
@@ -41,7 +42,7 @@ {$item['author']} {$item['version']} - {if $item['uninstall']} + {if !$item['isinstall']} 安装 {else/} 卸载 diff --git a/application/admin/view/public/base.html b/application/admin/view/public/base.html index 9fbb5954..89cfef06 100644 --- a/application/admin/view/public/base.html +++ b/application/admin/view/public/base.html @@ -207,7 +207,7 @@ {if $meta_title} {$meta_title} {else/} - {:lang($Request->controller().'_'.$Request->action())} + {:lang(request()->controller().'_'.request()->action())} {/if} diff --git a/application/common/controller/Addons.php b/application/common/controller/Addons.php index bbf12093..333b2a81 100644 --- a/application/common/controller/Addons.php +++ b/application/common/controller/Addons.php @@ -34,7 +34,7 @@ class Addons extends Base { public function template($template) { $mc = $this->getAddonsName(); - $ac = input('get.ac', '', 'trim,strtolower'); + $ac = input('ac', '', 'trim,strtolower'); $parse_str = \think\Config::get('parse_str'); $parse_str['__ADDONROOT__'] = ROOT_PATH . "/addons/{$mc}"; \think\Config::set('parse_str', $parse_str); @@ -52,7 +52,7 @@ class Addons extends Base { } final public function getAddonsName() { - $mc = input('get.mc', '', 'trim,strtolower'); + $mc = input('mc', '', 'trim,strtolower'); if ($mc) { return $mc; } else { diff --git a/application/common/controller/Admin.php b/application/common/controller/Admin.php index 1fe37706..0ce2edd3 100644 --- a/application/common/controller/Admin.php +++ b/application/common/controller/Admin.php @@ -201,6 +201,7 @@ class Admin extends Base { $model = db('Addons'); $list = array(); $map = array( + 'isinstall' => array('gt', 0), 'status' => array('gt', 0), ); $list = $model->field("name,id,title,'' as 'style'")->where($map)->select(); diff --git a/application/common/model/Addons.php b/application/common/model/Addons.php index 13c45486..b52b8d51 100644 --- a/application/common/model/Addons.php +++ b/application/common/model/Addons.php @@ -16,123 +16,120 @@ namespace app\common\model; */ class Addons extends \app\common\model\Base { - protected $auto = array('status'); - protected $insert = array('create_time'); + protected $auto = array('status', 'isinstall', 'update_time'); + protected $insert = array('create_time'); - protected function setStatusAttr($value){ - return 1; - } + protected function setStatusAttr($value) { + return $value ? $value : 0; + } - protected function setIsinstallAttr($value){ - return 0; - } + protected function setIsinstallAttr($value) { + return $value ? $value : 0; + } - protected function getStatusTextAttr($value, $data){ - return $data['status'] ? "启用" : "禁用"; - } + protected function getStatusTextAttr($value, $data) { + return $data['status'] ? "启用" : "禁用"; + } - protected function getUninstallAttr($value, $data){ - return 0; - } + protected function getUninstallAttr($value, $data) { + return 0; + } - /** - * 更新插件列表 - * @param string $addon_dir - */ - public function refresh($addon_dir = ''){ - if(!$addon_dir){ - $addon_dir = SENT_ADDON_PATH; - } - $dirs = array_map('basename',glob($addon_dir.'*', GLOB_ONLYDIR)); - if($dirs === FALSE || !file_exists($addon_dir)){ - $this->error = '插件目录不可读或者不存在'; - return FALSE; - } - $where['name'] = array('in',$dirs); - $addons = $this->where($where)->select(); - - foreach ($dirs as $value) { - $value = ucfirst($value); - if(!isset($addons[$value])){ + /** + * 更新插件列表 + * @param string $addon_dir + */ + public function refresh($addon_dir = '') { + if (!$addon_dir) { + $addon_dir = SENT_ADDON_PATH; + } + $dirs = array_map('basename', glob($addon_dir . '*', GLOB_ONLYDIR)); + if ($dirs === FALSE || !file_exists($addon_dir)) { + $this->error = '插件目录不可读或者不存在'; + return FALSE; + } + $where['name'] = array('in', $dirs); + $addons = $this->where($where)->column('*', 'name'); + + foreach ($dirs as $value) { + $value = ucfirst($value); + if (!isset($addons[$value])) { $class = get_addon_class($value); - if(!class_exists($class)){ - // 实例化插件失败忽略执行 - \think\Log::record('插件'.$value.'的入口文件不存在!'); + if (!class_exists($class)) { + // 实例化插件失败忽略执行 + \think\Log::record('插件' . $value . '的入口文件不存在!'); continue; } - $obj = new $class; - $addons[$value] = $obj->info; - if($addons[$value]){ - $addons[$value]['id'] = 0; - $addons[$value]['uninstall'] = 1; - unset($addons[$value]['status']); - } + $obj = new $class; + $save = $obj->info; + self::create($save); } - } - } + } + } - /** - * 获取插件的后台列表 - */ - public function getAdminList(){ - $admin = array(); - $db_addons = db('Addons')->where("status=1 AND has_adminlist=1")->field('title,name')->select(); - if($db_addons){ - foreach ($db_addons as $value) { - $admin[] = array('title'=>$value['title'],'url'=>"Addons/adminList?name={$value['name']}"); - } - } - return $admin; - } + /** + * 获取插件的后台列表 + */ + public function getAdminList() { + $admin = array(); + $db_addons = db('Addons')->where("status=1 AND has_adminlist=1")->field('title,name')->select(); + if ($db_addons) { + foreach ($db_addons as $value) { + $admin[] = array('title' => $value['title'], 'url' => "Addons/adminList?name={$value['name']}"); + } + } + return $admin; + } - public function install($data){ - if ($data) { - $info = $this->where('name', $data['name'])->find(); - if (null == $info) { - $result = $this->save($data); - if ($result) { - return model('Hooks')->addHooks($data['name']); - }else{ - return false; - } - }else{ - $this->error = "已安装!"; - return false; - } - }else{ - return false; - } - } + public function install($data) { + if ($data) { + $info = $this->where('name', $data['name'])->value('id'); + if ($info) { + $result = $this->save(array('isinstall'=>1, 'status'=>1), array('id'=>$info)); + }else{ + $data['isinstall'] = 1; + $data['status'] = 1; + $result = $this->save($data); + } + if (false !== $result) { + return model('Hooks')->addHooks($data['name']); + }else{ + return false; + } + } else { + return false; + } + } - public function uninstall($id){ - $info = $this->get($id); - if (!$info) { - $this->error = "无此插件!"; - return false; - } - $class = get_addon_class($info['name']); - if (class_exists($class)) { - //插件卸载方法 - $addons = new $class; - if (!method_exists($addons,'uninstall')) { - $this->error = "插件卸载方法!"; - return false; - } - $result = $addons->uninstall(); - if ($result) { - //卸载挂载点中的插件 - $result = model('Hooks')->removeHooks($info['name']); - //删除插件表中数据 - $this->where(array('id'=>$id))->delete(); - return true; - }else{ - $this->error = "无法卸载插件!"; - return false; - } - } - } + public function uninstall($id) { + $info = $this->get($id); + if (!$info) { + $this->error = "无此插件!"; + return false; + } + $class = get_addon_class($info['name']); + if (class_exists($class)) { + //插件卸载方法 + $addons = new $class; + if (!method_exists($addons, 'uninstall')) { + $this->error = "插件卸载方法!"; + return false; + } + $result = $addons->uninstall(); + if ($result) { + //卸载挂载点中的插件 + $result = model('Hooks')->removeHooks($info['name']); + //删除插件表中数据 + $this->where(array('id' => $id))->delete(); + return true; + } else { + $this->error = "无法卸载插件!"; + return false; + } + } + } - public function build(){ + public function build() { - } + } } \ No newline at end of file diff --git a/application/common/model/Hooks.php b/application/common/model/Hooks.php index 80028396..c8034e0c 100644 --- a/application/common/model/Hooks.php +++ b/application/common/model/Hooks.php @@ -85,12 +85,13 @@ class Hooks extends Base { 'update_time' => time(), 'status' => 1 ); - $this->save($save); + self::create($save); }else{ if ($info['addons']) { - # code... + $addons = explode(',', $info['addons']); + array_push($addons, substr($item, 0, -5)); }else{ - $addons = substr($item, 0, -5); + $addons = array(substr($item, 0, -5)); } $this->where('name', $addons_name)->setField('addons', $addons); } @@ -105,6 +106,14 @@ class Hooks extends Base { $this->error = "未实现{$addons_name}插件的入口文件"; return false; } - $methods = get_class_methods($addons_class); + $row = $this->where(array('addons'=>array('like',$addons_name)))->select(); + foreach ($row as $key => $value) { + $value['addons'] = explode(',', $info['addons']); + $key = array_search($addons_name, $value['addons']); + unset($value['addons'][$key]); + $addons = $value['addons']; + $this->where('id', $value['id'])->setField('addons', $addons); + } + return true; } } \ No newline at end of file