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.

322 lines
14 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <?php
  2. namespace app\lib;
  3. use Exception;
  4. use ZipArchive;
  5. class Plugins
  6. {
  7. private static function get_btapi($os){
  8. if($os == 'Windows'){
  9. $bt_url = config_get('wbt_url');
  10. $bt_key = config_get('wbt_key');
  11. }else{
  12. $bt_url = config_get('bt_url');
  13. $bt_key = config_get('bt_key');
  14. }
  15. if(!$bt_url || !$bt_key) throw new Exception('请先配置好宝塔面板接口信息');
  16. $btapi = new Btapi($bt_url, $bt_key);
  17. return $btapi;
  18. }
  19. //刷新插件列表
  20. public static function refresh_plugin_list($os = 'Linux'){
  21. $btapi = self::get_btapi($os);
  22. $result = $btapi->get_plugin_list();
  23. if($result && isset($result['list']) && isset($result['type'])){
  24. if(empty($result['list']) || empty($result['type'])){
  25. throw new Exception('获取插件列表失败:插件列表为空');
  26. }
  27. self::save_plugin_list($result, $os);
  28. }else{
  29. throw new Exception('获取插件列表失败:'.(isset($result['msg'])?$result['msg']:'面板连接失败'));
  30. }
  31. }
  32. //保存插件列表
  33. private static function save_plugin_list($data, $os){
  34. $data['ip'] = '127.0.0.1';
  35. $data['serverid'] = '';
  36. $data['beta'] = 0;
  37. $data['uid'] = 1;
  38. $data['skey'] = '';
  39. $list = [];
  40. foreach($data['list'] as $plugin){
  41. if(isset($plugin['endtime'])) $plugin['endtime'] = 0;
  42. $list[] = $plugin;
  43. }
  44. $data['list'] = $list;
  45. if($data['pro']>-1) $data['pro'] = 0;
  46. if($data['ltd']>-1) $data['ltd'] = strtotime('+1 year');
  47. $json_file = get_data_dir($os).'config/plugin_list.json';
  48. if(!file_put_contents($json_file, json_encode($data))){
  49. throw new Exception('保存插件列表失败,文件无写入权限');
  50. }
  51. }
  52. //获取插件列表
  53. public static function get_plugin_list($os = 'Linux'){
  54. $json_file = get_data_dir($os).'config/plugin_list.json';
  55. if(file_exists($json_file)){
  56. $data = file_get_contents($json_file);
  57. $json_arr = json_decode($data, true);
  58. if($json_arr){
  59. return $json_arr;
  60. }
  61. }
  62. return false;
  63. }
  64. //获取一个插件信息
  65. public static function get_plugin_info($name, $os = 'Linux'){
  66. $json_arr = self::get_plugin_list($os);
  67. if(!$json_arr) return null;
  68. foreach($json_arr['list'] as $plugin){
  69. if($plugin['name'] == $name){
  70. return $plugin;
  71. }
  72. }
  73. return null;
  74. }
  75. //下载插件(自动判断是否第三方)
  76. public static function download_plugin($plugin_name, $version, $os = 'Linux'){
  77. $plugin_info = Plugins::get_plugin_info($plugin_name, $os);
  78. if(!$plugin_info) throw new Exception('未找到该插件信息');
  79. if($plugin_info['type'] == 10 && isset($plugin_info['versions'][0]['download'])){
  80. if($plugin_info['price'] == 0){
  81. $btapi = self::get_btapi($os);
  82. $btapi->create_plugin_other_order($plugin_info['id']);
  83. }
  84. $fname = $plugin_info['versions'][0]['download'];
  85. $filemd5 = $plugin_info['versions'][0]['md5'];
  86. Plugins::download_plugin_other($fname, $filemd5, $os);
  87. if(isset($plugin_info['min_image']) && strpos($plugin_info['min_image'], 'fname=')){
  88. $fname = substr($plugin_info['min_image'], strpos($plugin_info['min_image'], '?fname=')+7);
  89. Plugins::download_plugin_other($fname, null, $os);
  90. }
  91. }else{
  92. Plugins::download_plugin_package($plugin_name, $version, $os);
  93. }
  94. }
  95. //下载插件包
  96. public static function download_plugin_package($plugin_name, $version, $os = 'Linux'){
  97. $filepath = get_data_dir($os).'plugins/package/'.$plugin_name.'-'.$version.'.zip';
  98. $btapi = self::get_btapi($os);
  99. $result = $btapi->get_plugin_filename($plugin_name, $version);
  100. if($result && isset($result['status'])){
  101. if($result['status'] == true){
  102. $filename = $result['filename'];
  103. self::download_file($btapi, $filename, $filepath);
  104. if(file_exists($filepath)){
  105. $zip = new ZipArchive;
  106. if ($zip->open($filepath) === true)
  107. {
  108. $zip->extractTo(get_data_dir($os).'plugins/folder/'.$plugin_name.'-'.$version);
  109. $zip->close();
  110. $main_filepath = get_data_dir($os).'plugins/folder/'.$plugin_name.'-'.$version.'/'.$plugin_name.'/'.$plugin_name.'_main.py';
  111. if(file_exists($main_filepath) && filesize($main_filepath)>10){
  112. if(!strpos(file_get_contents($main_filepath), 'import ')){ //加密py文件,需要解密
  113. self::decode_plugin_main($plugin_name, $version, $main_filepath, $os);
  114. self::noauth_plugin_main($main_filepath);
  115. $zip->open($filepath, ZipArchive::CREATE);
  116. $zip->addFile($main_filepath, $plugin_name.'/'.$plugin_name.'_main.py');
  117. $zip->close();
  118. }
  119. }
  120. }else{
  121. throw new Exception('插件包解压缩失败');
  122. }
  123. return true;
  124. }else{
  125. throw new Exception('下载插件包失败,本地文件不存在');
  126. }
  127. }else{
  128. throw new Exception('下载插件包失败:'.($result['msg']?$result['msg']:'未知错误'));
  129. }
  130. }else{
  131. throw new Exception('下载插件包失败,接口返回错误');
  132. }
  133. }
  134. //下载插件主程序文件
  135. public static function download_plugin_main($plugin_name, $version, $os = 'Linux'){
  136. $filepath = get_data_dir($os).'plugins/main/'.$plugin_name.'-'.$version.'.dat';
  137. $btapi = self::get_btapi($os);
  138. $result = $btapi->get_plugin_main_filename($plugin_name, $version);
  139. if($result && isset($result['status'])){
  140. if($result['status'] == true){
  141. $filename = $result['filename'];
  142. self::download_file($btapi, $filename, $filepath);
  143. if(file_exists($filepath)){
  144. return true;
  145. }else{
  146. throw new Exception('下载插件主程序文件失败,本地文件不存在');
  147. }
  148. }else{
  149. throw new Exception('下载插件主程序文件失败:'.($result['msg']?$result['msg']:'未知错误'));
  150. }
  151. }else{
  152. throw new Exception('下载插件主程序文件失败,接口返回错误');
  153. }
  154. }
  155. //解密并下载插件主程序文件
  156. public static function decode_plugin_main($plugin_name, $version, $main_filepath, $os = 'Linux'){
  157. if(self::decode_plugin_main_local($main_filepath, $os)) return true;
  158. $btapi = self::get_btapi($os);
  159. $result = $btapi->get_decode_plugin_main($plugin_name, $version);
  160. if($result && isset($result['status'])){
  161. if($result['status'] == true){
  162. $filename = $result['filename'];
  163. self::download_file($btapi, $filename, $main_filepath);
  164. return true;
  165. }else{
  166. throw new Exception('解密插件主程序文件失败:'.($result['msg']?$result['msg']:'未知错误'));
  167. }
  168. }else{
  169. throw new Exception('解密插件主程序文件失败,接口返回错误');
  170. }
  171. }
  172. //本地解密插件主程序文件
  173. public static function decode_plugin_main_local($main_filepath, $os = 'Linux'){
  174. $btapi = self::get_btapi($os);
  175. $userinfo = $btapi->get_user_info();
  176. if(isset($userinfo['uid'])){
  177. $src = file_get_contents($main_filepath);
  178. $uid = $userinfo['uid'];
  179. $serverid = $userinfo['serverid'];
  180. $key = md5(substr($serverid, 10, 16).$uid.$serverid);
  181. $iv = md5($key.$serverid);
  182. $key = substr($key, 8, 16);
  183. $iv = substr($iv, 8, 16);
  184. $data_arr = explode("\n", $src);
  185. $de_text = '';
  186. foreach($data_arr as $data){
  187. $data = trim($data);
  188. if(!empty($data) && strlen($data)!=24){
  189. $tmp = openssl_decrypt($data, 'aes-128-cbc', $key, 0, $iv);
  190. if($tmp) $de_text .= $tmp;
  191. }
  192. }
  193. if(!empty($de_text)){
  194. file_put_contents($main_filepath, $de_text);
  195. return true;
  196. }
  197. return false;
  198. }else{
  199. throw new Exception('解密插件主程序文件失败,获取用户信息失败');
  200. }
  201. }
  202. //去除插件主程序文件授权校验
  203. public static function noauth_plugin_main($main_filepath){
  204. $data = file_get_contents($main_filepath);
  205. if(!$data) return false;
  206. $data = str_replace('\'http://www.bt.cn/api/panel/get_soft_list_test', 'public.GetConfigValue(\'home\')+\'/api/panel/get_soft_list_test', $data);
  207. $data = str_replace('\'https://www.bt.cn/api/panel/get_soft_list_test', 'public.GetConfigValue(\'home\')+\'/api/panel/get_soft_list_test', $data);
  208. $data = str_replace('\'http://www.bt.cn/api/panel/get_soft_list', 'public.GetConfigValue(\'home\')+\'/api/panel/get_soft_list', $data);
  209. $data = str_replace('\'https://www.bt.cn/api/panel/get_soft_list', 'public.GetConfigValue(\'home\')+\'/api/panel/get_soft_list', $data);
  210. $data = str_replace('\'http://www.bt.cn/api/panel/notpro', 'public.GetConfigValue(\'home\')+\'/api/panel/notpro', $data);
  211. $data = str_replace('\'https://www.bt.cn/api/panel/notpro', 'public.GetConfigValue(\'home\')+\'/api/panel/notpro', $data);
  212. $data = str_replace('\'http://www.bt.cn/api/wpanel/get_soft_list_test', 'public.GetConfigValue(\'home\')+\'/api/wpanel/get_soft_list_test', $data);
  213. $data = str_replace('\'https://www.bt.cn/api/wpanel/get_soft_list_test', 'public.GetConfigValue(\'home\')+\'/api/wpanel/get_soft_list_test', $data);
  214. $data = str_replace('\'http://www.bt.cn/api/wpanel/get_soft_list', 'public.GetConfigValue(\'home\')+\'/api/wpanel/get_soft_list', $data);
  215. $data = str_replace('\'https://www.bt.cn/api/wpanel/get_soft_list', 'public.GetConfigValue(\'home\')+\'/api/wpanel/get_soft_list', $data);
  216. $data = str_replace('\'http://www.bt.cn/api/wpanel/notpro', 'public.GetConfigValue(\'home\')+\'/api/wpanel/notpro', $data);
  217. $data = str_replace('\'https://www.bt.cn/api/wpanel/notpro', 'public.GetConfigValue(\'home\')+\'/api/wpanel/notpro', $data);
  218. file_put_contents($main_filepath, $data);
  219. }
  220. //下载插件其他文件
  221. public static function download_plugin_other($fname, $filemd5 = null, $os = 'Linux'){
  222. $filepath = get_data_dir().'plugins/other/'.$fname;
  223. @mkdir(dirname($filepath), 0777, true);
  224. $btapi = self::get_btapi($os);
  225. $result = $btapi->get_plugin_other_filename($fname);
  226. if($result && isset($result['status'])){
  227. if($result['status'] == true){
  228. $filename = $result['filename'];
  229. self::download_file($btapi, $filename, $filepath);
  230. if(file_exists($filepath)){
  231. if($filemd5 && md5_file($filepath) != $filemd5){
  232. @unlink($filepath);
  233. throw new Exception('插件文件MD5校验失败');
  234. }
  235. return true;
  236. }else{
  237. throw new Exception('下载插件文件失败,本地文件不存在');
  238. }
  239. }else{
  240. throw new Exception('下载插件文件失败:'.($result['msg']?$result['msg']:'未知错误'));
  241. }
  242. }else{
  243. throw new Exception('下载插件文件失败,接口返回错误');
  244. }
  245. }
  246. //下载文件
  247. private static function download_file($btapi, $filename, $filepath){
  248. try{
  249. $btapi->download($filename, $filepath);
  250. }catch(Exception $e){
  251. @unlink($filepath);
  252. //宝塔bug小文件下载失败,改用base64下载
  253. $result = $btapi->get_file($filename);
  254. if($result && isset($result['status']) && $result['status']==true){
  255. $filedata = base64_decode($result['data']);
  256. if(strlen($filedata) < 4096 && substr($filedata,0,1)=='{' && substr($filedata,-1,1)=='}'){
  257. $arr = json_decode($filedata, true);
  258. if($arr){
  259. throw new Exception('获取文件失败:'.($arr['msg']?$arr['msg']:'未知错误'));
  260. }
  261. }
  262. if(!$filedata){
  263. throw new Exception('获取文件失败:文件内容为空');
  264. }
  265. file_put_contents($filepath, $filedata);
  266. }elseif($result){
  267. throw new Exception('获取文件失败:'.($result['msg']?$result['msg']:'未知错误'));
  268. }else{
  269. throw new Exception('获取文件失败:未知错误');
  270. }
  271. }
  272. }
  273. //刷新一键部署列表
  274. public static function refresh_deplist($os = 'Linux'){
  275. $btapi = self::get_btapi($os);
  276. $result = $btapi->get_deplist();
  277. if($result && isset($result['list']) && isset($result['type'])){
  278. if(empty($result['list']) || empty($result['type'])){
  279. throw new Exception('获取一键部署列表失败:一键部署列表为空');
  280. }
  281. $json_file = get_data_dir($os).'config/deployment_list.json';
  282. if(!file_put_contents($json_file, json_encode($result))){
  283. throw new Exception('保存一键部署列表失败,文件无写入权限');
  284. }
  285. }else{
  286. throw new Exception('获取一键部署列表失败:'.(isset($result['msg'])?$result['msg']:'面板连接失败'));
  287. }
  288. }
  289. //获取一键部署列表
  290. public static function get_deplist($os = 'Linux'){
  291. $json_file = get_data_dir($os).'config/deployment_list.json';
  292. if(file_exists($json_file)){
  293. $data = file_get_contents($json_file);
  294. $json_arr = json_decode($data, true);
  295. if($json_arr){
  296. return $json_arr;
  297. }
  298. }
  299. return false;
  300. }
  301. }