You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

387 lines
15 KiB

1 year ago
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use think\facade\Db;
  5. use think\facade\View;
  6. use think\facade\Request;
  7. use think\facade\Cache;
  8. use app\lib\Btapi;
  9. use app\lib\Plugins;
  10. class Admin extends BaseController
  11. {
  12. public function verifycode()
  13. {
  14. return captcha();
  15. }
  16. public function login(){
  17. if(request()->islogin){
  18. return redirect('/admin');
  19. }
  20. if(request()->isAjax()){
  21. $username = input('post.username',null,'trim');
  22. $password = input('post.password',null,'trim');
  23. $code = input('post.code',null,'trim');
  24. if(empty($username) || empty($password)){
  25. return json(['code'=>-1, 'msg'=>'用户名或密码不能为空']);
  26. }
  27. if(!captcha_check($code)){
  28. return json(['code'=>-1, 'msg'=>'验证码错误']);
  29. }
  30. if($username == config_get('admin_username') && $password == config_get('admin_password')){
  31. Db::name('log')->insert(['uid' => 0, 'action' => '登录后台', 'data' => 'IP:'.$this->clientip, 'addtime' => date("Y-m-d H:i:s")]);
  32. $session = md5($username.config_get('admin_password'));
  33. $expiretime = time()+2562000;
  34. $token = authcode("{$username}\t{$session}\t{$expiretime}", 'ENCODE', config_get('syskey'));
  35. cookie('admin_token', $token, ['expire' => $expiretime, 'httponly' => true]);
  36. config_set('admin_lastlogin', date('Y-m-d H:i:s'));
  37. return json(['code'=>0]);
  38. }else{
  39. return json(['code'=>-1, 'msg'=>'用户名或密码错误']);
  40. }
  41. }
  42. return view();
  43. }
  44. public function logout()
  45. {
  46. cookie('admin_token', null);
  47. return redirect('/admin/login');
  48. }
  49. public function index()
  50. {
  51. $stat = ['total'=>0, 'free'=>0, 'pro'=>0, 'ltd'=>0, 'third'=>0];
  52. $json_arr = Plugins::get_plugin_list();
  53. if($json_arr){
  54. foreach($json_arr['list'] as $plugin){
  55. $stat['total']++;
  56. if($plugin['type']==10) $stat['third']++;
  57. elseif($plugin['type']==12) $stat['ltd']++;
  58. elseif($plugin['type']==8) $stat['pro']++;
  59. elseif($plugin['type']==5 || $plugin['type']==6 || $plugin['type']==7) $stat['free']++;
  60. }
  61. }
  62. $stat['runtime'] = Db::name('config')->where('key','runtime')->value('value') ?? '<font color="red">未运行</font>';
  63. $stat['record_total'] = Db::name('record')->count();
  64. $stat['record_isuse'] = Db::name('record')->whereTime('usetime', '>=', strtotime('-7 days'))->count();
  65. View::assign('stat', $stat);
  66. $tmp = 'version()';
  67. $mysqlVersion = Db::query("select version()")[0][$tmp];
  68. $info = [
  69. 'framework_version' => app()::VERSION,
  70. 'php_version' => PHP_VERSION,
  71. 'mysql_version' => $mysqlVersion,
  72. 'software' => $_SERVER['SERVER_SOFTWARE'],
  73. 'os' => php_uname(),
  74. 'date' => date("Y-m-d H:i:s"),
  75. ];
  76. View::assign('info', $info);
  77. return view();
  78. }
  79. public function set(){
  80. if(request()->isAjax()){
  81. $params = Request::param();
  82. foreach ($params as $key => $value) {
  83. config_set($key, $value);
  84. }
  85. cache('configs', NULL);
  86. return json(['code'=>0]);
  87. }
  88. $mod = input('param.mod', 'sys');
  89. View::assign('mod', $mod);
  90. View::assign('conf', config('sys'));
  91. $runtime = Db::name('config')->where('key','runtime')->value('value') ?? '<font color="red">未运行</font>';
  92. View::assign('runtime', $runtime);
  93. return view();
  94. }
  95. public function setaccount(){
  96. $params = Request::param();
  97. if(isset($params['username']))$params['username']=trim($params['username']);
  98. if(isset($params['oldpwd']))$params['oldpwd']=trim($params['oldpwd']);
  99. if(isset($params['newpwd']))$params['newpwd']=trim($params['newpwd']);
  100. if(isset($params['newpwd2']))$params['newpwd2']=trim($params['newpwd2']);
  101. if(empty($params['username'])) return json(['code'=>-1, 'msg'=>'用户名不能为空']);
  102. config_set('admin_username', $params['username']);
  103. if(!empty($params['oldpwd']) && !empty($params['newpwd']) && !empty($params['newpwd2'])){
  104. if(config_get('admin_password') != $params['oldpwd']){
  105. return json(['code'=>-1, 'msg'=>'旧密码不正确']);
  106. }
  107. if($params['newpwd'] != $params['newpwd2']){
  108. return json(['code'=>-1, 'msg'=>'两次新密码输入不一致']);
  109. }
  110. config_set('admin_password', $params['newpwd']);
  111. }
  112. cache('configs', NULL);
  113. cookie('admin_token', null);
  114. return json(['code'=>0]);
  115. }
  116. public function testbturl(){
  117. $bt_url = input('post.bt_url');
  118. $bt_key = input('post.bt_key');
  119. if(!$bt_url || !$bt_key)return json(['code'=>-1, 'msg'=>'参数不能为空']);
  120. $btapi = new Btapi($bt_url, $bt_key);
  121. $result = $btapi->get_config();
  122. if($result && isset($result['status']) && ($result['status']==1 || isset($result['sites_path']))){
  123. $result = $btapi->get_user_info();
  124. if($result && isset($result['username'])){
  125. return json(['code'=>0, 'msg'=>'面板连接测试成功!']);
  126. }else{
  127. return json(['code'=>-1, 'msg'=>'面板连接测试成功,但未安装专用插件']);
  128. }
  129. }else{
  130. return json(['code'=>-1, 'msg'=>isset($result['msg'])?$result['msg']:'面板地址无法连接']);
  131. }
  132. }
  133. public function plugins(){
  134. $typelist = [];
  135. $json_arr = Plugins::get_plugin_list();
  136. if($json_arr){
  137. foreach($json_arr['type'] as $type){
  138. $typelist[$type['id']] = $type['title'];
  139. }
  140. }
  141. View::assign('typelist', $typelist);
  142. return view();
  143. }
  144. public function pluginswin(){
  145. $typelist = [];
  146. $json_arr = Plugins::get_plugin_list('Windows');
  147. if($json_arr){
  148. foreach($json_arr['type'] as $type){
  149. $typelist[$type['id']] = $type['title'];
  150. }
  151. }
  152. View::assign('typelist', $typelist);
  153. return view();
  154. }
  155. public function plugins_data(){
  156. $type = input('post.type/d');
  157. $keyword = input('post.keyword', null, 'trim');
  158. $os = input('get.os');
  159. if(!$os) $os = 'Linux';
  160. $json_arr = Plugins::get_plugin_list($os);
  161. if(!$json_arr) return json([]);
  162. $typelist = [];
  163. foreach($json_arr['type'] as $row){
  164. $typelist[$row['id']] = $row['title'];
  165. }
  166. $list = [];
  167. foreach($json_arr['list'] as $plugin){
  168. if($type > 0 && $plugin['type']!=$type) continue;
  169. if(!empty($keyword) && $keyword != $plugin['name'] && stripos($plugin['title'], $keyword)===false) continue;
  170. $versions = [];
  171. foreach($plugin['versions'] as $version){
  172. $ver = $version['m_version'].'.'.$version['version'];
  173. if(isset($version['download'])){
  174. $status = false;
  175. if(file_exists(get_data_dir().'plugins/other/'.$version['download'])){
  176. $status = true;
  177. }
  178. $versions[] = ['status'=>$status, 'type'=>1, 'version'=>$ver, 'download'=>$version['download'], 'md5'=>$version['md5']];
  179. }else{
  180. $status = false;
  181. if(file_exists(get_data_dir($os).'plugins/package/'.$plugin['name'].'-'.$ver.'.zip')){
  182. $status = true;
  183. }
  184. $versions[] = ['status'=>$status, 'type'=>0, 'version'=>$ver];
  185. }
  186. }
  187. if($plugin['name'] == 'obs') $plugin['ps'] = substr($plugin['ps'],0,strpos($plugin['ps'],'<a '));
  188. $list[] = [
  189. 'id' => $plugin['id'],
  190. 'name' => $plugin['name'],
  191. 'title' => $plugin['title'],
  192. 'type' => $plugin['type'],
  193. 'typename' => $typelist[$plugin['type']],
  194. 'desc' => str_replace('target="_blank"','target="_blank" rel="noopener noreferrer"',$plugin['ps']),
  195. 'price' => $plugin['price'],
  196. 'author' => isset($plugin['author']) ? $plugin['author'] : '官方',
  197. 'versions' => $versions
  198. ];
  199. }
  200. return json($list);
  201. }
  202. public function download_plugin(){
  203. $name = input('post.name', null, 'trim');
  204. $version = input('post.version', null, 'trim');
  205. $os = input('post.os');
  206. if(!$os) $os = 'Linux';
  207. if(!$name || !$version) return json(['code'=>-1, 'msg'=>'参数不能为空']);
  208. try{
  209. Plugins::download_plugin($name, $version, $os);
  210. Db::name('log')->insert(['uid' => 0, 'action' => '下载插件', 'data' => $name.'-'.$version.' os:'.$os, 'addtime' => date("Y-m-d H:i:s")]);
  211. return json(['code'=>0,'msg'=>'下载成功']);
  212. }catch(\Exception $e){
  213. return json(['code'=>-1, 'msg'=>$e->getMessage()]);
  214. }
  215. }
  216. public function refresh_plugins(){
  217. $os = input('get.os');
  218. if(!$os) $os = 'Linux';
  219. try{
  220. Plugins::refresh_plugin_list($os);
  221. Db::name('log')->insert(['uid' => 0, 'action' => '刷新插件列表', 'data' => '刷新'.$os.'插件列表成功', 'addtime' => date("Y-m-d H:i:s")]);
  222. return json(['code'=>0,'msg'=>'获取最新插件列表成功!']);
  223. }catch(\Exception $e){
  224. return json(['code'=>-1, 'msg'=>$e->getMessage()]);
  225. }
  226. }
  227. public function record(){
  228. return view();
  229. }
  230. public function record_data(){
  231. $ip = input('post.ip', null, 'trim');
  232. $offset = input('post.offset/d');
  233. $limit = input('post.limit/d');
  234. $select = Db::name('record');
  235. if(!empty($ip)){
  236. $select->where('ip', $ip);
  237. }
  238. $total = $select->count();
  239. $rows = $select->order('id','desc')->limit($offset, $limit)->select();
  240. return json(['total'=>$total, 'rows'=>$rows]);
  241. }
  242. public function log(){
  243. return view();
  244. }
  245. public function log_data(){
  246. $action = input('post.action', null, 'trim');
  247. $offset = input('post.offset/d');
  248. $limit = input('post.limit/d');
  249. $select = Db::name('log');
  250. if(!empty($action)){
  251. $select->where('action', $action);
  252. }
  253. $total = $select->count();
  254. $rows = $select->order('id','desc')->limit($offset, $limit)->select();
  255. return json(['total'=>$total, 'rows'=>$rows]);
  256. }
  257. public function list(){
  258. $type = input('param.type', 'black');
  259. View::assign('type', $type);
  260. View::assign('typename', $type=='white'?'白名单':'黑名单');
  261. return view();
  262. }
  263. public function list_data(){
  264. $type = input('param.type', 'black');
  265. $ip = input('post.ip', null, 'trim');
  266. $offset = input('post.offset/d');
  267. $limit = input('post.limit/d');
  268. $tablename = $type == 'black' ? 'black' : 'white';
  269. $select = Db::name($tablename);
  270. if(!empty($ip)){
  271. $select->where('ip', $ip);
  272. }
  273. $total = $select->count();
  274. $rows = $select->order('id','desc')->limit($offset, $limit)->select();
  275. return json(['total'=>$total, 'rows'=>$rows]);
  276. }
  277. public function list_op(){
  278. $type = input('param.type', 'black');
  279. $tablename = $type == 'black' ? 'black' : 'white';
  280. $act = input('post.act', null);
  281. if($act == 'get'){
  282. $id = input('post.id/d');
  283. if(!$id) return json(['code'=>-1, 'msg'=>'no id']);
  284. $data = Db::name($tablename)->where('id', $id)->find();
  285. return json(['code'=>0, 'data'=>$data]);
  286. }elseif($act == 'add'){
  287. $ip = input('post.ip', null, 'trim');
  288. if(!$ip) return json(['code'=>-1, 'msg'=>'IP不能为空']);
  289. if(Db::name($tablename)->where('ip', $ip)->find()){
  290. return json(['code'=>-1, 'msg'=>'该IP已存在']);
  291. }
  292. Db::name($tablename)->insert([
  293. 'ip' => $ip,
  294. 'enable' => 1,
  295. 'addtime' => date("Y-m-d H:i:s")
  296. ]);
  297. return json(['code'=>0, 'msg'=>'succ']);
  298. }elseif($act == 'edit'){
  299. $id = input('post.id/d');
  300. $ip = input('post.ip', null, 'trim');
  301. if(!$id || !$ip) return json(['code'=>-1, 'msg'=>'IP不能为空']);
  302. if(Db::name($tablename)->where('ip', $ip)->where('id', '<>', $id)->find()){
  303. return json(['code'=>-1, 'msg'=>'该IP已存在']);
  304. }
  305. Db::name($tablename)->where('id', $id)->update([
  306. 'ip' => $ip
  307. ]);
  308. return json(['code'=>0, 'msg'=>'succ']);
  309. }elseif($act == 'enable'){
  310. $id = input('post.id/d');
  311. $enable = input('post.enable/d');
  312. if(!$id) return json(['code'=>-1, 'msg'=>'no id']);
  313. Db::name($tablename)->where('id', $id)->update([
  314. 'enable' => $enable
  315. ]);
  316. return json(['code'=>0, 'msg'=>'succ']);
  317. }elseif($act == 'del'){
  318. $id = input('post.id/d');
  319. if(!$id) return json(['code'=>-1, 'msg'=>'no id']);
  320. Db::name($tablename)->where('id', $id)->delete();
  321. return json(['code'=>0, 'msg'=>'succ']);
  322. }
  323. return json(['code'=>-1, 'msg'=>'no act']);
  324. }
  325. public function deplist(){
  326. $deplist_linux = get_data_dir().'config/deployment_list.json';
  327. $deplist_win = get_data_dir('Windows').'config/deployment_list.json';
  328. $deplist_linux_time = file_exists($deplist_linux) ? date("Y-m-d H:i:s", filemtime($deplist_linux)) : '不存在';
  329. $deplist_win_time = file_exists($deplist_win) ? date("Y-m-d H:i:s", filemtime($deplist_win)) : '不存在';
  330. View::assign('deplist_linux_time', $deplist_linux_time);
  331. View::assign('deplist_win_time', $deplist_win_time);
  332. return view();
  333. }
  334. public function refresh_deplist(){
  335. $os = input('get.os');
  336. if(!$os) $os = 'Linux';
  337. try{
  338. Plugins::refresh_deplist($os);
  339. Db::name('log')->insert(['uid' => 0, 'action' => '刷新一键部署列表', 'data' => '刷新'.$os.'一键部署列表成功', 'addtime' => date("Y-m-d H:i:s")]);
  340. return json(['code'=>0,'msg'=>'获取最新一键部署列表成功!']);
  341. }catch(\Exception $e){
  342. return json(['code'=>-1, 'msg'=>$e->getMessage()]);
  343. }
  344. }
  345. public function cleancache(){
  346. Cache::clear();
  347. return json(['code'=>0,'msg'=>'succ']);
  348. }
  349. }