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.

341 lines
13 KiB

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