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.

290 lines
13 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
  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. $btapi = self::get_btapi($os);
  158. $result = $btapi->get_decode_plugin_main($plugin_name, $version);
  159. if($result && isset($result['status'])){
  160. if($result['status'] == true){
  161. $filename = $result['filename'];
  162. self::download_file($btapi, $filename, $main_filepath);
  163. return true;
  164. }else{
  165. throw new Exception('解密插件主程序文件失败:'.($result['msg']?$result['msg']:'未知错误'));
  166. }
  167. }else{
  168. throw new Exception('解密插件主程序文件失败,接口返回错误');
  169. }
  170. }
  171. //去除插件主程序文件授权校验
  172. public static function noauth_plugin_main($main_filepath){
  173. $data = file_get_contents($main_filepath);
  174. if(!$data) return false;
  175. $data = str_replace('\'http://www.bt.cn/api/panel/get_soft_list_test', 'public.GetConfigValue(\'home\')+\'/api/panel/get_soft_list_test', $data);
  176. $data = str_replace('\'https://www.bt.cn/api/panel/get_soft_list_test', 'public.GetConfigValue(\'home\')+\'/api/panel/get_soft_list_test', $data);
  177. $data = str_replace('\'http://www.bt.cn/api/panel/get_soft_list', 'public.GetConfigValue(\'home\')+\'/api/panel/get_soft_list', $data);
  178. $data = str_replace('\'https://www.bt.cn/api/panel/get_soft_list', 'public.GetConfigValue(\'home\')+\'/api/panel/get_soft_list', $data);
  179. $data = str_replace('\'http://www.bt.cn/api/panel/notpro', 'public.GetConfigValue(\'home\')+\'/api/panel/notpro', $data);
  180. $data = str_replace('\'https://www.bt.cn/api/panel/notpro', 'public.GetConfigValue(\'home\')+\'/api/panel/notpro', $data);
  181. $data = str_replace('\'http://www.bt.cn/api/wpanel/get_soft_list_test', 'public.GetConfigValue(\'home\')+\'/api/wpanel/get_soft_list_test', $data);
  182. $data = str_replace('\'https://www.bt.cn/api/wpanel/get_soft_list_test', 'public.GetConfigValue(\'home\')+\'/api/wpanel/get_soft_list_test', $data);
  183. $data = str_replace('\'http://www.bt.cn/api/wpanel/get_soft_list', 'public.GetConfigValue(\'home\')+\'/api/wpanel/get_soft_list', $data);
  184. $data = str_replace('\'https://www.bt.cn/api/wpanel/get_soft_list', 'public.GetConfigValue(\'home\')+\'/api/wpanel/get_soft_list', $data);
  185. $data = str_replace('\'http://www.bt.cn/api/wpanel/notpro', 'public.GetConfigValue(\'home\')+\'/api/wpanel/notpro', $data);
  186. $data = str_replace('\'https://www.bt.cn/api/wpanel/notpro', 'public.GetConfigValue(\'home\')+\'/api/wpanel/notpro', $data);
  187. file_put_contents($main_filepath, $data);
  188. }
  189. //下载插件其他文件
  190. public static function download_plugin_other($fname, $filemd5 = null, $os = 'Linux'){
  191. $filepath = get_data_dir().'plugins/other/'.$fname;
  192. @mkdir(dirname($filepath), 0777, true);
  193. $btapi = self::get_btapi($os);
  194. $result = $btapi->get_plugin_other_filename($fname);
  195. if($result && isset($result['status'])){
  196. if($result['status'] == true){
  197. $filename = $result['filename'];
  198. self::download_file($btapi, $filename, $filepath);
  199. if(file_exists($filepath)){
  200. if($filemd5 && md5_file($filepath) != $filemd5){
  201. @unlink($filepath);
  202. throw new Exception('插件文件MD5校验失败');
  203. }
  204. return true;
  205. }else{
  206. throw new Exception('下载插件文件失败,本地文件不存在');
  207. }
  208. }else{
  209. throw new Exception('下载插件文件失败:'.($result['msg']?$result['msg']:'未知错误'));
  210. }
  211. }else{
  212. throw new Exception('下载插件文件失败,接口返回错误');
  213. }
  214. }
  215. //下载文件
  216. private static function download_file($btapi, $filename, $filepath){
  217. try{
  218. $btapi->download($filename, $filepath);
  219. }catch(Exception $e){
  220. @unlink($filepath);
  221. //宝塔bug小文件下载失败,改用base64下载
  222. $result = $btapi->get_file($filename);
  223. if($result && isset($result['status']) && $result['status']==true){
  224. $filedata = base64_decode($result['data']);
  225. if(strlen($filedata) < 4096 && substr($filedata,0,1)=='{' && substr($filedata,-1,1)=='}'){
  226. $arr = json_decode($filedata, true);
  227. if($arr){
  228. throw new Exception('获取文件失败:'.($arr['msg']?$arr['msg']:'未知错误'));
  229. }
  230. }
  231. if(!$filedata){
  232. throw new Exception('获取文件失败:文件内容为空');
  233. }
  234. file_put_contents($filepath, $filedata);
  235. }elseif($result){
  236. throw new Exception('获取文件失败:'.($result['msg']?$result['msg']:'未知错误'));
  237. }else{
  238. throw new Exception('获取文件失败:未知错误');
  239. }
  240. }
  241. }
  242. //刷新一键部署列表
  243. public static function refresh_deplist($os = 'Linux'){
  244. $btapi = self::get_btapi($os);
  245. $result = $btapi->get_deplist();
  246. if($result && isset($result['list']) && isset($result['type'])){
  247. if(empty($result['list']) || empty($result['type'])){
  248. throw new Exception('获取一键部署列表失败:一键部署列表为空');
  249. }
  250. $json_file = get_data_dir($os).'config/deployment_list.json';
  251. if(!file_put_contents($json_file, json_encode($result))){
  252. throw new Exception('保存一键部署列表失败,文件无写入权限');
  253. }
  254. }else{
  255. throw new Exception('获取一键部署列表失败:'.(isset($result['msg'])?$result['msg']:'面板连接失败'));
  256. }
  257. }
  258. //获取一键部署列表
  259. public static function get_deplist($os = 'Linux'){
  260. $json_file = get_data_dir($os).'config/deployment_list.json';
  261. if(file_exists($json_file)){
  262. $data = file_get_contents($json_file);
  263. $json_arr = json_decode($data, true);
  264. if($json_arr){
  265. return $json_arr;
  266. }
  267. }
  268. return false;
  269. }
  270. }