Addon.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. use think\addons\AddonException;
  5. use think\addons\Service;
  6. use think\Cache;
  7. use think\Config;
  8. use think\Exception;
  9. use app\common\util\Dir;
  10. class Addon extends Base
  11. {
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. }
  16. public function index()
  17. {
  18. $param = input();
  19. $this->assign('title',lang('admin/addon/title'));
  20. return $this->fetch('admin@addon/index');
  21. }
  22. public function config()
  23. {
  24. $param = input();
  25. $name = $param['name'];
  26. if(empty($name)){
  27. return $this->error(lang('param_err'));
  28. }
  29. if (!is_dir(ADDON_PATH . $name)) {
  30. return $this->error(lang('get_dir_err'));
  31. }
  32. $info = get_addon_info($name);
  33. $config = get_addon_fullconfig($name);
  34. if (!$info){
  35. return $this->error(lang('get_addon_info_err'));
  36. }
  37. if ($this->request->isPost()) {
  38. $params = $this->request->post("row/a");
  39. if(empty($params)){
  40. return $this->error(lang('param_err'));
  41. }
  42. foreach ($config as $k => &$v) {
  43. if (isset($params[$v['name']])) {
  44. if ($v['type'] == 'array') {
  45. $params[$v['name']] = is_array($params[$v['name']]) ? $params[$v['name']] : (array)json_decode($params[$v['name']], true);
  46. $value = $params[$v['name']];
  47. } else {
  48. $value = is_array($params[$v['name']]) ? implode(',', $params[$v['name']]) : $params[$v['name']];
  49. }
  50. $v['value'] = $value;
  51. }
  52. }
  53. try {
  54. //更新配置文件
  55. set_addon_fullconfig($name, $config);
  56. Service::refresh();
  57. return $this->success(lang('save_ok'));
  58. } catch (Exception $e) {
  59. return $this->error($e->getMessage());
  60. }
  61. }
  62. $this->assign('info',$info);
  63. $this->assign('config',$config);
  64. return $this->fetch('admin@addon/config');
  65. }
  66. public function info()
  67. {
  68. }
  69. public function downloaded()
  70. {
  71. $offset = (int)$this->request->get("offset");
  72. $limit = (int)$this->request->get("limit");
  73. $filter = $this->request->get("filter");
  74. $search = $this->request->get("search");
  75. $search = htmlspecialchars(strip_tags($search));
  76. $key = $GLOBALS['config']['app']['cache_flag']. '_'. 'onlineaddons';
  77. $onlineaddons = Cache::get($key);
  78. if (!is_array($onlineaddons)) {
  79. $onlineaddons = [];
  80. $result = mac_curl_get( "h"."t"."t"."p:/"."/a"."p"."i"."."."m"."a"."c"."c"."m"."s."."c"."o"."m"."/" . 'addon/index');
  81. if ($result['ret']) {
  82. $json = json_decode($result['msg'], TRUE);
  83. $rows = isset($json['rows']) ? $json['rows'] : [];
  84. foreach ($rows as $index => $row) {
  85. $onlineaddons[$row['name']] = $row;
  86. }
  87. }
  88. Cache::set($key, $onlineaddons, 600);
  89. }
  90. $filter = (array)json_decode($filter, true);
  91. $addons = get_addon_list();
  92. $list = [];
  93. foreach ($addons as $k => $v) {
  94. if ($search && stripos($v['name'], $search) === FALSE && stripos($v['intro'], $search) === FALSE)
  95. continue;
  96. if (isset($onlineaddons[$v['name']])) {
  97. $v = array_merge($onlineaddons[$v['name']], $v);
  98. } else {
  99. if(!isset($v['category_id'])) {
  100. $v['category_id'] = 0;
  101. }
  102. if(!isset($v['flag'])) {
  103. $v['flag'] = '';
  104. }
  105. if(!isset($v['banner'])) {
  106. $v['banner'] = '';
  107. }
  108. if(!isset($v['image'])) {
  109. $v['image'] = '';
  110. }
  111. if(!isset($v['donateimage'])) {
  112. $v['donateimage'] = '';
  113. }
  114. if(!isset($v['demourl'])) {
  115. $v['demourl'] = '';
  116. }
  117. if(!isset($v['price'])) {
  118. $v['price'] = '0.00';
  119. }
  120. }
  121. $v['url'] = addon_url($v['name']);
  122. $v['createtime'] = filemtime(ADDON_PATH . $v['name']);
  123. $v['install'] = '1';
  124. if ($filter && isset($filter['category_id']) && is_numeric($filter['category_id']) && $filter['category_id'] != $v['category_id']) {
  125. continue;
  126. }
  127. $list[] = $v;
  128. }
  129. $total = count($list);
  130. if ($limit) {
  131. $list = array_slice($list, $offset, $limit);
  132. }
  133. $result = array("total" => $total, "rows" => $list);
  134. $callback = $this->request->get('callback') ? "jsonp" : "json";
  135. return $callback($result);
  136. }
  137. /**
  138. * 安装
  139. */
  140. public function install()
  141. {
  142. $param = input();
  143. $name = $param['name'];
  144. $force = (int)$param['force'];
  145. if (!$name) {
  146. return $this->error(lang('param_err'));
  147. }
  148. try {
  149. $uid = $this->request->post("uid");
  150. $token = $this->request->post("token");
  151. $version = $this->request->post("version");
  152. $faversion = $this->request->post("faversion");
  153. $extend = [
  154. 'uid' => $uid,
  155. 'token' => $token,
  156. 'version' => $version,
  157. 'faversion' => $faversion
  158. ];
  159. Service::install($name, $force, $extend);
  160. $info = get_addon_info($name);
  161. $info['config'] = get_addon_config($name) ? 1 : 0;
  162. $info['state'] = 1;
  163. return $this->success(lang('install_err'));
  164. } catch (AddonException $e) {
  165. return $this->result($e->getData(), $e->getCode(), $e->getMessage());
  166. } catch (Exception $e) {
  167. return $this->error($e->getMessage(), $e->getCode());
  168. }
  169. }
  170. /**
  171. * 卸载
  172. */
  173. public function uninstall()
  174. {
  175. $param = input();
  176. $name = $param['name'];
  177. $force = (int)$param['force'];
  178. if (!$name) {
  179. return $this->error(lang('param_err'));
  180. }
  181. try {
  182. if( strpos($name,".")!==false || strpos($name,"/")!==false || strpos($name,"\\")!==false ) {
  183. $this->error(lang('admin/addon/path_err'));
  184. return;
  185. }
  186. Service::uninstall($name, $force);
  187. return $this->success(lang('uninstall_ok'));
  188. } catch (AddonException $e) {
  189. return $this->result($e->getData(), $e->getCode(), $e->getMessage());
  190. } catch (Exception $e) {
  191. return $this->error($e->getMessage());
  192. }
  193. }
  194. /**
  195. * 禁用启用
  196. */
  197. public function state()
  198. {
  199. $param = input();
  200. $name = $param['name'];
  201. $action = $param['action'];
  202. $force = (int)$param['force'];
  203. if (!$name) {
  204. return $this->error(lang('param_err'));
  205. }
  206. try {
  207. $action = $action == 'enable' ? $action : 'disable';
  208. //调用启用、禁用的方法
  209. Service::$action($name, $force);
  210. Cache::rm('__menu__');
  211. return $this->success(lang('opt_ok'));
  212. } catch (AddonException $e) {
  213. return $this->result($e->getData(), $e->getCode(), $e->getMessage());
  214. } catch (Exception $e) {
  215. return $this->error($e->getMessage());
  216. }
  217. }
  218. /**
  219. * 本地上传
  220. */
  221. public function local()
  222. {
  223. $param = input();
  224. $validate = \think\Loader::validate('Token');
  225. if(!$validate->check($param)){
  226. return $this->error($validate->getError());
  227. }
  228. echo 'closed';exit;
  229. $file = $this->request->file('file');
  230. $addonTmpDir = RUNTIME_PATH . 'addons' . DS;
  231. if (!is_dir($addonTmpDir)) {
  232. @mkdir($addonTmpDir, 0755, true);
  233. }
  234. $info = $file->rule('uniqid')->validate(['size' => 10240000, 'ext' => 'zip'])->move($addonTmpDir);
  235. if ($info) {
  236. $tmpName = substr($info->getFilename(), 0, stripos($info->getFilename(), '.'));
  237. $tmpAddonDir = ADDON_PATH . $tmpName . DS;
  238. $tmpFile = $addonTmpDir . $info->getSaveName();
  239. try {
  240. Service::unzip($tmpName);
  241. @unlink($tmpFile);
  242. $infoFile = $tmpAddonDir . 'info.ini';
  243. if (!is_file($infoFile)) {
  244. throw new Exception(lang('admin/addon/lack_config_err'));
  245. }
  246. $config = Config::parse($infoFile, '', $tmpName);
  247. $name = isset($config['name']) ? $config['name'] : '';
  248. if (!$name) {
  249. throw new Exception(lang('admin/addon/name_empty_err'));
  250. }
  251. $newAddonDir = ADDON_PATH . $name . DS;
  252. if (is_dir($newAddonDir)) {
  253. throw new Exception(lang('admin/addon/haved_err'));
  254. }
  255. //重命名插件文件夹
  256. rename($tmpAddonDir, $newAddonDir);
  257. try {
  258. //默认禁用该插件
  259. $info = get_addon_info($name);
  260. if ($info['state']) {
  261. $info['state'] = 0;
  262. set_addon_info($name, $info);
  263. }
  264. //执行插件的安装方法
  265. $class = get_addon_class($name);
  266. if (class_exists($class)) {
  267. $addon = new $class();
  268. $addon->install();
  269. }
  270. //导入SQL
  271. Service::importsql($name);
  272. $info['config'] = get_addon_config($name) ? 1 : 0;
  273. return $this->success(lang('install_ok'));
  274. } catch (Exception $e) {
  275. if (Dir::delDir($newAddonDir) === false) {
  276. }
  277. throw new Exception($e->getMessage());
  278. }
  279. } catch (Exception $e) {
  280. @unlink($tmpFile);
  281. if (Dir::delDir($tmpAddonDir) === false) {
  282. }
  283. return $this->error($e->getMessage());
  284. }
  285. } else {
  286. // 上传失败获取错误信息
  287. return $this->error($file->getError());
  288. }
  289. }
  290. public function add()
  291. {
  292. return $this->fetch('admin@addon/add');
  293. }
  294. /**
  295. * 更新插件
  296. */
  297. public function upgrade()
  298. {
  299. $name = $this->request->post("name");
  300. if (!$name) {
  301. return $this->error(lang('param_err'));
  302. }
  303. try {
  304. $uid = $this->request->post("uid");
  305. $token = $this->request->post("token");
  306. $version = $this->request->post("version");
  307. $faversion = $this->request->post("faversion");
  308. $extend = [
  309. 'uid' => $uid,
  310. 'token' => $token,
  311. 'version' => $version,
  312. 'faversion' => $faversion
  313. ];
  314. //调用更新的方法
  315. Service::upgrade($name, $extend);
  316. Cache::rm('__menu__');
  317. return $this->success(lang('update_ok'));
  318. } catch (AddonException $e) {
  319. return $this->result($e->getData(), $e->getCode(), $e->getMessage());
  320. } catch (Exception $e) {
  321. return $this->error($e->getMessage());
  322. }
  323. }
  324. }