Addon.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. $response = mac_curl_get( "h"."t"."t"."p:/"."/a"."p"."i"."."."m"."a"."c"."c"."m"."s."."c"."o"."m"."/" . 'addon/index');
  81. $json = !empty($response) ? json_decode($response, true) : [];
  82. if (!empty($json['rows'])) {
  83. foreach ($json['rows'] as $row) {
  84. $onlineaddons[$row['name']] = $row;
  85. }
  86. }
  87. Cache::set($key, $onlineaddons, 600);
  88. }
  89. $filter = (array)json_decode($filter, true);
  90. $addons = get_addon_list();
  91. $list = [];
  92. foreach ($addons as $k => $v) {
  93. if ($search && stripos($v['name'], $search) === FALSE && stripos($v['intro'], $search) === FALSE)
  94. continue;
  95. if (isset($onlineaddons[$v['name']])) {
  96. $v = array_merge($onlineaddons[$v['name']], $v);
  97. } else {
  98. if(!isset($v['category_id'])) {
  99. $v['category_id'] = 0;
  100. }
  101. if(!isset($v['flag'])) {
  102. $v['flag'] = '';
  103. }
  104. if(!isset($v['banner'])) {
  105. $v['banner'] = '';
  106. }
  107. if(!isset($v['image'])) {
  108. $v['image'] = '';
  109. }
  110. if(!isset($v['donateimage'])) {
  111. $v['donateimage'] = '';
  112. }
  113. if(!isset($v['demourl'])) {
  114. $v['demourl'] = '';
  115. }
  116. if(!isset($v['price'])) {
  117. $v['price'] = '0.00';
  118. }
  119. }
  120. $v['url'] = addon_url($v['name']);
  121. $v['createtime'] = filemtime(ADDON_PATH . $v['name']);
  122. $v['install'] = '1';
  123. if ($filter && isset($filter['category_id']) && is_numeric($filter['category_id']) && $filter['category_id'] != $v['category_id']) {
  124. continue;
  125. }
  126. $list[] = $v;
  127. }
  128. $total = count($list);
  129. if ($limit) {
  130. $list = array_slice($list, $offset, $limit);
  131. }
  132. $result = array("total" => $total, "rows" => $list);
  133. $callback = $this->request->get('callback') ? "jsonp" : "json";
  134. return $callback($result);
  135. }
  136. /**
  137. * 安装
  138. */
  139. public function install()
  140. {
  141. $param = input();
  142. $name = $param['name'];
  143. $force = (int)$param['force'];
  144. if (!$name) {
  145. return $this->error(lang('param_err'));
  146. }
  147. try {
  148. $uid = $this->request->post("uid");
  149. $token = $this->request->post("token");
  150. $version = $this->request->post("version");
  151. $faversion = $this->request->post("faversion");
  152. $extend = [
  153. 'uid' => $uid,
  154. 'token' => $token,
  155. 'version' => $version,
  156. 'faversion' => $faversion
  157. ];
  158. Service::install($name, $force, $extend);
  159. $info = get_addon_info($name);
  160. $info['config'] = get_addon_config($name) ? 1 : 0;
  161. $info['state'] = 1;
  162. return $this->success(lang('install_err'));
  163. } catch (AddonException $e) {
  164. return $this->result($e->getData(), $e->getCode(), $e->getMessage());
  165. } catch (Exception $e) {
  166. return $this->error($e->getMessage(), $e->getCode());
  167. }
  168. }
  169. /**
  170. * 卸载
  171. */
  172. public function uninstall()
  173. {
  174. $param = input();
  175. $name = $param['name'];
  176. $force = (int)$param['force'];
  177. if (!$name) {
  178. return $this->error(lang('param_err'));
  179. }
  180. try {
  181. if( strpos($name,".")!==false || strpos($name,"/")!==false || strpos($name,"\\")!==false ) {
  182. $this->error(lang('admin/addon/path_err'));
  183. return;
  184. }
  185. Service::uninstall($name, $force);
  186. return $this->success(lang('uninstall_ok'));
  187. } catch (AddonException $e) {
  188. return $this->result($e->getData(), $e->getCode(), $e->getMessage());
  189. } catch (Exception $e) {
  190. return $this->error($e->getMessage());
  191. }
  192. }
  193. /**
  194. * 禁用启用
  195. */
  196. public function state()
  197. {
  198. $param = input();
  199. $name = $param['name'];
  200. $action = $param['action'];
  201. $force = (int)$param['force'];
  202. if (!$name) {
  203. return $this->error(lang('param_err'));
  204. }
  205. try {
  206. $action = $action == 'enable' ? $action : 'disable';
  207. //调用启用、禁用的方法
  208. Service::$action($name, $force);
  209. Cache::rm('__menu__');
  210. return $this->success(lang('opt_ok'));
  211. } catch (AddonException $e) {
  212. return $this->result($e->getData(), $e->getCode(), $e->getMessage());
  213. } catch (Exception $e) {
  214. return $this->error($e->getMessage());
  215. }
  216. }
  217. /**
  218. * 本地上传
  219. */
  220. public function local()
  221. {
  222. $param = input();
  223. $validate = \think\Loader::validate('Token');
  224. if(!$validate->check($param)){
  225. return $this->error($validate->getError());
  226. }
  227. echo 'closed';exit;
  228. $file = $this->request->file('file');
  229. $addonTmpDir = RUNTIME_PATH . 'addons' . DS;
  230. if (!is_dir($addonTmpDir)) {
  231. @mkdir($addonTmpDir, 0755, true);
  232. }
  233. $info = $file->rule('uniqid')->validate(['size' => 10240000, 'ext' => 'zip'])->move($addonTmpDir);
  234. if ($info) {
  235. $tmpName = substr($info->getFilename(), 0, stripos($info->getFilename(), '.'));
  236. $tmpAddonDir = ADDON_PATH . $tmpName . DS;
  237. $tmpFile = $addonTmpDir . $info->getSaveName();
  238. try {
  239. Service::unzip($tmpName);
  240. @unlink($tmpFile);
  241. $infoFile = $tmpAddonDir . 'info.ini';
  242. if (!is_file($infoFile)) {
  243. throw new Exception(lang('admin/addon/lack_config_err'));
  244. }
  245. $config = Config::parse($infoFile, '', $tmpName);
  246. $name = isset($config['name']) ? $config['name'] : '';
  247. if (!$name) {
  248. throw new Exception(lang('admin/addon/name_empty_err'));
  249. }
  250. $newAddonDir = ADDON_PATH . $name . DS;
  251. if (is_dir($newAddonDir)) {
  252. throw new Exception(lang('admin/addon/haved_err'));
  253. }
  254. //重命名插件文件夹
  255. rename($tmpAddonDir, $newAddonDir);
  256. try {
  257. //默认禁用该插件
  258. $info = get_addon_info($name);
  259. if ($info['state']) {
  260. $info['state'] = 0;
  261. set_addon_info($name, $info);
  262. }
  263. //执行插件的安装方法
  264. $class = get_addon_class($name);
  265. if (class_exists($class)) {
  266. $addon = new $class();
  267. $addon->install();
  268. }
  269. //导入SQL
  270. Service::importsql($name);
  271. $info['config'] = get_addon_config($name) ? 1 : 0;
  272. return $this->success(lang('install_ok'));
  273. } catch (Exception $e) {
  274. if (Dir::delDir($newAddonDir) === false) {
  275. }
  276. throw new Exception($e->getMessage());
  277. }
  278. } catch (Exception $e) {
  279. @unlink($tmpFile);
  280. if (Dir::delDir($tmpAddonDir) === false) {
  281. }
  282. return $this->error($e->getMessage());
  283. }
  284. } else {
  285. // 上传失败获取错误信息
  286. return $this->error($file->getError());
  287. }
  288. }
  289. public function add()
  290. {
  291. return $this->fetch('admin@addon/add');
  292. }
  293. /**
  294. * 更新插件
  295. */
  296. public function upgrade()
  297. {
  298. $name = $this->request->post("name");
  299. if (!$name) {
  300. return $this->error(lang('param_err'));
  301. }
  302. try {
  303. $uid = $this->request->post("uid");
  304. $token = $this->request->post("token");
  305. $version = $this->request->post("version");
  306. $faversion = $this->request->post("faversion");
  307. $extend = [
  308. 'uid' => $uid,
  309. 'token' => $token,
  310. 'version' => $version,
  311. 'faversion' => $faversion
  312. ];
  313. //调用更新的方法
  314. Service::upgrade($name, $extend);
  315. Cache::rm('__menu__');
  316. return $this->success(lang('update_ok'));
  317. } catch (AddonException $e) {
  318. return $this->result($e->getData(), $e->getCode(), $e->getMessage());
  319. } catch (Exception $e) {
  320. return $this->error($e->getMessage());
  321. }
  322. }
  323. }