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.

393 lines
14 KiB

1 year ago
1 year ago
  1. <?php
  2. namespace app\controller;
  3. use think\facade\Db;
  4. use app\BaseController;
  5. use app\lib\Plugins;
  6. class Api extends BaseController
  7. {
  8. //获取插件列表
  9. public function get_plugin_list(){
  10. if(!$this->checklist()) return '';
  11. $record = Db::name('record')->where('ip',$this->clientip)->find();
  12. if($record){
  13. Db::name('record')->where('id',$record['id'])->update(['usetime'=>date("Y-m-d H:i:s")]);
  14. }else{
  15. Db::name('record')->insert(['ip'=>$this->clientip, 'addtime'=>date("Y-m-d H:i:s"), 'usetime'=>date("Y-m-d H:i:s")]);
  16. }
  17. $json_arr = Plugins::get_plugin_list();
  18. if(!$json_arr) return json((object)[]);
  19. return json($json_arr);
  20. }
  21. //获取插件列表(win)
  22. public function get_plugin_list_win(){
  23. if(!$this->checklist()) return '';
  24. $record = Db::name('record')->where('ip',$this->clientip)->find();
  25. if($record){
  26. Db::name('record')->where('id',$record['id'])->update(['usetime'=>date("Y-m-d H:i:s")]);
  27. }else{
  28. Db::name('record')->insert(['ip'=>$this->clientip, 'addtime'=>date("Y-m-d H:i:s"), 'usetime'=>date("Y-m-d H:i:s")]);
  29. }
  30. $json_arr = Plugins::get_plugin_list('Windows');
  31. if(!$json_arr) return json((object)[]);
  32. return json($json_arr);
  33. }
  34. //下载插件包
  35. public function download_plugin(){
  36. $plugin_name = input('post.name');
  37. $version = input('post.version');
  38. $os = input('post.os');
  39. if(!$plugin_name || !$version){
  40. return '参数不能为空';
  41. }
  42. if(!in_array($os,['Windows','Linux'])) $os = 'Linux';
  43. if(!preg_match('/^[a-zA-Z0-9_]+$/', $plugin_name) || !preg_match('/^[0-9.]+$/', $version)){
  44. return '参数不正确';
  45. }
  46. if(!$this->checklist()) '你的服务器被禁止使用此云端';
  47. $filepath = get_data_dir($os).'plugins/package/'.$plugin_name.'-'.$version.'.zip';
  48. if(file_exists($filepath)){
  49. $filename = $plugin_name.'.zip';
  50. $this->output_file($filepath, $filename);
  51. }else{
  52. return '云端不存在该插件包';
  53. }
  54. }
  55. //下载插件主文件
  56. public function download_plugin_main(){
  57. $plugin_name = input('post.name');
  58. $version = input('post.version');
  59. $os = input('post.os');
  60. if(!$plugin_name || !$version){
  61. return '参数不能为空';
  62. }
  63. if(!in_array($os,['Windows','Linux'])) $os = 'Linux';
  64. if(!preg_match('/^[a-zA-Z0-9_]+$/', $plugin_name) || !preg_match('/^[0-9.]+$/', $version)){
  65. return '参数不正确';
  66. }
  67. if(!$this->checklist()) '你的服务器被禁止使用此云端';
  68. $filepath = get_data_dir($os).'plugins/main/'.$plugin_name.'-'.$version.'.dat';
  69. if(file_exists($filepath)){
  70. $filename = $plugin_name.'_main.py';
  71. $this->output_file($filepath, $filename);
  72. }else{
  73. $filepath = get_data_dir($os).'plugins/folder/'.$plugin_name.'-'.$version.'/'.$plugin_name.'/'.$plugin_name.'_main.py';
  74. if(file_exists($filepath)){
  75. $filename = $plugin_name.'_main.py';
  76. $this->output_file($filepath, $filename);
  77. }else{
  78. return '云端不存在该插件主文件';
  79. }
  80. }
  81. }
  82. //下载插件其他文件
  83. public function download_plugin_other(){
  84. $fname = input('get.fname');
  85. if(!$fname){
  86. return json(['status'=>false, 'msg'=>'参数不能为空']);
  87. }
  88. if(strpos(dirname($fname),'.')!==false)return json(['status'=>false, 'msg'=>'参数不正确']);
  89. if(!$this->checklist()) return json(['status'=>false, 'msg'=>'你的服务器被禁止使用此云端']);
  90. $filepath = get_data_dir().'plugins/other/'.$fname;
  91. if(file_exists($filepath)){
  92. $filename = basename($fname);
  93. $this->output_file($filepath, $filename);
  94. }else{
  95. return json(['status'=>false, 'msg'=>'云端不存在该插件文件']);
  96. }
  97. }
  98. public function get_update_logs(){
  99. $type = input('get.type');
  100. if($type == 'Windows'){
  101. $version = config_get('new_version_win');
  102. $data = [
  103. [
  104. 'title' => 'Linux面板'.$version,
  105. 'body' => config_get('update_msg_win'),
  106. 'addtime' => config_get('update_date_win')
  107. ]
  108. ];
  109. }else{
  110. $version = config_get('new_version');
  111. $data = [
  112. [
  113. 'title' => 'Linux面板'.$version,
  114. 'body' => config_get('update_msg'),
  115. 'addtime' => config_get('update_date')
  116. ]
  117. ];
  118. }
  119. return jsonp($data);
  120. }
  121. public function get_version(){
  122. $version = config_get('new_version');
  123. return $version;
  124. }
  125. public function get_version_win(){
  126. $version = config_get('new_version_win');
  127. return $version;
  128. }
  129. //安装统计
  130. public function setup_count(){
  131. return 'ok';
  132. }
  133. //检测更新
  134. public function check_update(){
  135. $version = config_get('new_version');
  136. $down_url = request()->root(true).'/install/update/LinuxPanel-'.$version.'.zip';
  137. $data = [
  138. 'force' => false,
  139. 'version' => $version,
  140. 'downUrl' => $down_url,
  141. 'updateMsg' => config_get('update_msg'),
  142. 'uptime' => config_get('update_date'),
  143. 'is_beta' => 0,
  144. 'adviser' => -1,
  145. 'btb' => '',
  146. 'beta' => [
  147. 'version' => $version,
  148. 'downUrl' => $down_url,
  149. 'updateMsg' => config_get('update_msg'),
  150. 'uptime' => config_get('update_date'),
  151. ]
  152. ];
  153. return json($data);
  154. }
  155. //检测更新(win)
  156. public function check_update_win(){
  157. $version = config_get('new_version_win');
  158. $down_url = request()->root(true).'/win/panel/panel_'.$version.'.zip';
  159. $data = [
  160. 'force' => false,
  161. 'version' => $version,
  162. 'downUrl' => $down_url,
  163. 'updateMsg' => config_get('update_msg_win'),
  164. 'uptime' => config_get('update_date_win'),
  165. 'is_beta' => 0,
  166. 'py_version' => '3.8.6',
  167. 'adviser' => -1,
  168. 'is_rec' => -1,
  169. 'btb' => '',
  170. 'beta' => [
  171. 'py_version' => '3.8.6',
  172. 'version' => $version,
  173. 'downUrl' => $down_url,
  174. 'updateMsg' => config_get('update_msg_win'),
  175. 'uptime' => config_get('update_date_win'),
  176. ]
  177. ];
  178. return json($data);
  179. }
  180. //宝塔云监控获取最新版本
  181. public function btm_latest_version(){
  182. $data = [
  183. 'version' => config_get('new_version_btm'),
  184. 'description' => config_get('update_msg_btm'),
  185. 'create_time' => config_get('update_date_btm')
  186. ];
  187. return json($data);
  188. }
  189. //宝塔云监控更新日志
  190. public function btm_update_history(){
  191. $data = [
  192. [
  193. 'version' => config_get('new_version_btm'),
  194. 'description' => config_get('update_msg_btm'),
  195. 'create_time' => config_get('update_date_btm')
  196. ]
  197. ];
  198. return json($data);
  199. }
  200. //获取内测版更新日志
  201. public function get_beta_logs(){
  202. return json(['beta_ps'=>'当前暂无内测版', 'list'=>[]]);
  203. }
  204. //检查用户绑定是否正确
  205. public function check_auth_key(){
  206. return '1';
  207. }
  208. //从云端验证域名是否可访问
  209. public function check_domain(){
  210. $domain = input('post.domain',null,'trim');
  211. $ssl = input('post.ssl/d');
  212. if(!$domain) return json(['status'=>false, 'msg'=>'域名不能为空']);
  213. if(!strpos($domain,'.')) return json(['status'=>false, 'msg'=>'域名格式不正确']);
  214. $domain = str_replace('*.','',$domain);
  215. $ip = gethostbyname($domain);
  216. if(!$ip || $ip == $domain){
  217. return json(['status'=>false, 'msg'=>'无法访问']);
  218. }else{
  219. return json(['status'=>true, 'msg'=>'访问正常']);
  220. }
  221. }
  222. //同步时间
  223. public function get_time(){
  224. return time();
  225. }
  226. //同步时间
  227. public function get_win_date(){
  228. return date("Y-m-d H:i:s");
  229. }
  230. //查询是否专业版(废弃)
  231. public function is_pro(){
  232. return json(['endtime'=>true, 'code'=>1]);
  233. }
  234. //获取产品推荐信息
  235. public function get_plugin_remarks(){
  236. return json(['list'=>[], 'pro_list'=>[], 'kfqq'=>'', 'kf'=>'', 'qun'=>'']);
  237. }
  238. //获取指定插件评分
  239. public function get_plugin_socre(){
  240. return json(['total'=>0, 'split'=>[0,0,0,0,0],'page'=>"<div><span class='Pcurrent'>1</span><span class='Pcount'>共计0条数据</span></div>",'data'=>[]]);
  241. }
  242. //提交插件评分
  243. public function plugin_score(){
  244. return json(['status'=>true, 'msg'=>'您的评分已成功提交,感谢您的支持!']);
  245. }
  246. //获取IP地址
  247. public function get_ip_address(){
  248. return $this->clientip;
  249. }
  250. //绑定账号
  251. public function get_auth_token(){
  252. if(!$_POST['data']) return json(['status'=>false, 'msg'=>'参数不能为空']);
  253. $reqData = hex2bin($_POST['data']);
  254. parse_str($reqData, $arr);
  255. $serverid = $arr['serverid'];
  256. $userinfo = ['uid'=>1, 'username'=>'Administrator', 'address'=>'127.0.0.1', 'serverid'=>$serverid, 'access_key'=>random(32), 'secret_key'=>random(48), 'ukey'=>md5(time()), 'state'=>1];
  257. $data = bin2hex(urlencode(json_encode($userinfo)));
  258. return json(['status'=>true, 'msg'=>'登录成功!', 'data'=>$data]);
  259. }
  260. //绑定账号新
  261. public function authorization_login(){
  262. if(!$_POST['data']) return json(['status'=>false, 'msg'=>'参数不能为空']);
  263. $reqData = hex2bin($_POST['data']);
  264. parse_str($reqData, $arr);
  265. $serverid = $arr['serverid'];
  266. $userinfo = ['uid'=>1, 'username'=>'Administrator', 'ip'=>'127.0.0.1', 'server_id'=>$serverid, 'access_key'=>random(32), 'secret_key'=>random(48)];
  267. $data = bin2hex(urlencode(json_encode($userinfo)));
  268. return json(['status'=>true, 'msg'=>'登录成功!', 'data'=>$data]);
  269. }
  270. //刷新授权信息
  271. public function authorization_info(){
  272. if(!$_POST['data']) return json(['status'=>false, 'msg'=>'参数不能为空']);
  273. $reqData = hex2bin($_POST['data']);
  274. parse_str($reqData, $arr);
  275. $id = isset($arr['id'])&&$arr['id']>0?$arr['id']:1;
  276. $userinfo = ['id'=>$id, 'product'=>$arr['product'], 'status'=>2, 'clients'=>9999, 'durations'=>0, 'end_time'=>strtotime('+10 year')];
  277. $data = bin2hex(urlencode(json_encode($userinfo)));
  278. return json(['status'=>true, 'data'=>$data]);
  279. }
  280. //一键部署列表
  281. public function get_deplist(){
  282. $os = input('post.os');
  283. $json_arr = Plugins::get_deplist($os);
  284. if(!$json_arr) return json([]);
  285. return json($json_arr);
  286. }
  287. //获取宝塔SSL列表
  288. public function get_ssl_list(){
  289. $data = bin2hex('[]');
  290. return json(['status'=>true, 'msg'=>'', 'data'=>$data]);
  291. }
  292. public function return_success(){
  293. return json(['status'=>true, 'msg'=>1, 'data'=>(object)[]]);
  294. }
  295. public function return_error(){
  296. return json(['status'=>false, 'msg'=>'不支持当前操作']);
  297. }
  298. public function return_error2(){
  299. return json(['success'=>false, 'res'=>'不支持当前操作']);
  300. }
  301. public function return_empty(){
  302. return '';
  303. }
  304. public function return_empty_array(){
  305. return json([]);
  306. }
  307. public function return_page_data(){
  308. return json(['page'=>"<div><span class='Pcurrent'>1</span><span class='Pnumber'>1/0</span><span class='Pline'>从1-1000条</span><span class='Pcount'>共计0条数据</span></div>", 'data'=>[]]);
  309. }
  310. public function btwaf_getspiders(){
  311. try{
  312. $result = Plugins::btwaf_getspiders();
  313. return json($result);
  314. }catch(\Exception $e){
  315. return json(['status'=>false, 'msg'=>$e->getMessage()]);
  316. }
  317. }
  318. //检查黑白名单
  319. private function checklist(){
  320. if(config_get('whitelist') == 1){
  321. if(Db::name('white')->where('ip', $this->clientip)->where('enable', 1)->find()){
  322. return true;
  323. }
  324. return false;
  325. }else{
  326. if(Db::name('black')->where('ip', $this->clientip)->where('enable', 1)->find()){
  327. return false;
  328. }
  329. return true;
  330. }
  331. }
  332. //下载大文件
  333. private function output_file($filepath, $filename){
  334. $filesize = filesize($filepath);
  335. $filemd5 = md5_file($filepath);
  336. ob_clean();
  337. header("Content-Type: application/octet-stream");
  338. header("Content-Disposition: attachment; filename={$filename}.zip");
  339. header("Content-Length: {$filesize}");
  340. header("File-size: {$filesize}");
  341. header("Content-md5: {$filemd5}");
  342. $read_buffer = 1024 * 100;
  343. $handle = fopen($filepath, 'rb');
  344. $sum_buffer = 0;
  345. while(!feof($handle) && $sum_buffer<$filesize) {
  346. echo fread($handle, min($read_buffer, ($filesize - $sum_buffer) + 1));
  347. $sum_buffer += $read_buffer;
  348. flush();
  349. }
  350. fclose($handle);
  351. exit;
  352. }
  353. }