btcloud/app/lib/Plugins.php

323 lines
14 KiB
PHP
Raw Permalink Normal View History

2022-06-04 11:22:19 -04:00
<?php
namespace app\lib;
use Exception;
use ZipArchive;
class Plugins
{
2022-06-14 03:50:39 -04:00
private static function get_btapi($os){
if($os == 'Windows'){
$bt_url = config_get('wbt_url');
$bt_key = config_get('wbt_key');
}else{
$bt_url = config_get('bt_url');
$bt_key = config_get('bt_key');
}
2022-06-04 11:22:19 -04:00
if(!$bt_url || !$bt_key) throw new Exception('请先配置好宝塔面板接口信息');
$btapi = new Btapi($bt_url, $bt_key);
return $btapi;
}
//刷新插件列表
2022-06-14 03:50:39 -04:00
public static function refresh_plugin_list($os = 'Linux'){
$btapi = self::get_btapi($os);
2022-06-04 11:22:19 -04:00
$result = $btapi->get_plugin_list();
if($result && isset($result['list']) && isset($result['type'])){
if(empty($result['list']) || empty($result['type'])){
throw new Exception('获取插件列表失败:插件列表为空');
}
2022-06-14 03:50:39 -04:00
self::save_plugin_list($result, $os);
2022-06-04 11:22:19 -04:00
}else{
2022-06-07 02:21:19 -04:00
throw new Exception('获取插件列表失败:'.(isset($result['msg'])?$result['msg']:'面板连接失败'));
2022-06-04 11:22:19 -04:00
}
}
//保存插件列表
2022-06-14 03:50:39 -04:00
private static function save_plugin_list($data, $os){
2022-06-04 11:22:19 -04:00
$data['ip'] = '127.0.0.1';
$data['serverid'] = '';
$data['beta'] = 0;
$data['uid'] = 1;
$data['skey'] = '';
$list = [];
foreach($data['list'] as $plugin){
if(isset($plugin['endtime'])) $plugin['endtime'] = 0;
$list[] = $plugin;
}
$data['list'] = $list;
if($data['pro']>-1) $data['pro'] = 0;
if($data['ltd']>-1) $data['ltd'] = strtotime('+1 year');
2022-06-14 03:50:39 -04:00
$json_file = get_data_dir($os).'config/plugin_list.json';
2022-06-04 11:22:19 -04:00
if(!file_put_contents($json_file, json_encode($data))){
throw new Exception('保存插件列表失败,文件无写入权限');
}
}
//获取插件列表
2022-06-14 03:50:39 -04:00
public static function get_plugin_list($os = 'Linux'){
$json_file = get_data_dir($os).'config/plugin_list.json';
2022-06-04 11:22:19 -04:00
if(file_exists($json_file)){
$data = file_get_contents($json_file);
$json_arr = json_decode($data, true);
if($json_arr){
return $json_arr;
}
}
return false;
}
//获取一个插件信息
2022-06-14 03:50:39 -04:00
public static function get_plugin_info($name, $os = 'Linux'){
$json_arr = self::get_plugin_list($os);
2022-06-04 11:22:19 -04:00
if(!$json_arr) return null;
foreach($json_arr['list'] as $plugin){
if($plugin['name'] == $name){
return $plugin;
}
}
return null;
}
//下载插件(自动判断是否第三方)
2022-06-14 03:50:39 -04:00
public static function download_plugin($plugin_name, $version, $os = 'Linux'){
$plugin_info = Plugins::get_plugin_info($plugin_name, $os);
2022-06-04 11:22:19 -04:00
if(!$plugin_info) throw new Exception('未找到该插件信息');
if($plugin_info['type'] == 10 && isset($plugin_info['versions'][0]['download'])){
2022-06-06 03:17:54 -04:00
if($plugin_info['price'] == 0){
2022-06-14 03:50:39 -04:00
$btapi = self::get_btapi($os);
2022-06-06 03:17:54 -04:00
$btapi->create_plugin_other_order($plugin_info['id']);
}
2022-06-04 11:22:19 -04:00
$fname = $plugin_info['versions'][0]['download'];
$filemd5 = $plugin_info['versions'][0]['md5'];
2022-06-14 03:50:39 -04:00
Plugins::download_plugin_other($fname, $filemd5, $os);
2022-06-04 11:22:19 -04:00
if(isset($plugin_info['min_image']) && strpos($plugin_info['min_image'], 'fname=')){
$fname = substr($plugin_info['min_image'], strpos($plugin_info['min_image'], '?fname=')+7);
2022-06-14 03:50:39 -04:00
Plugins::download_plugin_other($fname, null, $os);
2022-06-04 11:22:19 -04:00
}
}else{
2022-06-14 03:50:39 -04:00
Plugins::download_plugin_package($plugin_name, $version, $os);
2022-06-04 11:22:19 -04:00
}
}
//下载插件包
2022-06-14 03:50:39 -04:00
public static function download_plugin_package($plugin_name, $version, $os = 'Linux'){
$filepath = get_data_dir($os).'plugins/package/'.$plugin_name.'-'.$version.'.zip';
$btapi = self::get_btapi($os);
2022-06-04 11:22:19 -04:00
$result = $btapi->get_plugin_filename($plugin_name, $version);
if($result && isset($result['status'])){
if($result['status'] == true){
$filename = $result['filename'];
self::download_file($btapi, $filename, $filepath);
if(file_exists($filepath)){
$zip = new ZipArchive;
if ($zip->open($filepath) === true)
{
2022-06-14 03:50:39 -04:00
$zip->extractTo(get_data_dir($os).'plugins/folder/'.$plugin_name.'-'.$version);
2022-06-04 11:22:19 -04:00
$zip->close();
2022-06-14 03:50:39 -04:00
$main_filepath = get_data_dir($os).'plugins/folder/'.$plugin_name.'-'.$version.'/'.$plugin_name.'/'.$plugin_name.'_main.py';
2022-06-04 11:22:19 -04:00
if(file_exists($main_filepath) && filesize($main_filepath)>10){
if(!strpos(file_get_contents($main_filepath), 'import ')){ //加密py文件需要解密
2022-06-14 03:50:39 -04:00
self::decode_plugin_main($plugin_name, $version, $main_filepath, $os);
2022-06-07 02:21:19 -04:00
self::noauth_plugin_main($main_filepath);
2022-06-04 11:22:19 -04:00
$zip->open($filepath, ZipArchive::CREATE);
$zip->addFile($main_filepath, $plugin_name.'/'.$plugin_name.'_main.py');
$zip->close();
}
}
}else{
throw new Exception('插件包解压缩失败');
}
return true;
}else{
throw new Exception('下载插件包失败,本地文件不存在');
}
}else{
throw new Exception('下载插件包失败:'.($result['msg']?$result['msg']:'未知错误'));
}
}else{
throw new Exception('下载插件包失败,接口返回错误');
}
}
//下载插件主程序文件
2022-06-14 03:50:39 -04:00
public static function download_plugin_main($plugin_name, $version, $os = 'Linux'){
$filepath = get_data_dir($os).'plugins/main/'.$plugin_name.'-'.$version.'.dat';
$btapi = self::get_btapi($os);
2022-06-04 11:22:19 -04:00
$result = $btapi->get_plugin_main_filename($plugin_name, $version);
if($result && isset($result['status'])){
if($result['status'] == true){
$filename = $result['filename'];
self::download_file($btapi, $filename, $filepath);
if(file_exists($filepath)){
return true;
}else{
throw new Exception('下载插件主程序文件失败,本地文件不存在');
}
}else{
throw new Exception('下载插件主程序文件失败:'.($result['msg']?$result['msg']:'未知错误'));
}
}else{
throw new Exception('下载插件主程序文件失败,接口返回错误');
}
}
//解密并下载插件主程序文件
2022-06-14 03:50:39 -04:00
public static function decode_plugin_main($plugin_name, $version, $main_filepath, $os = 'Linux'){
2022-07-25 10:46:40 -04:00
if(self::decode_plugin_main_local($main_filepath, $os)) return true;
2022-06-14 03:50:39 -04:00
$btapi = self::get_btapi($os);
2022-06-04 11:22:19 -04:00
$result = $btapi->get_decode_plugin_main($plugin_name, $version);
if($result && isset($result['status'])){
if($result['status'] == true){
$filename = $result['filename'];
self::download_file($btapi, $filename, $main_filepath);
return true;
}else{
throw new Exception('解密插件主程序文件失败:'.($result['msg']?$result['msg']:'未知错误'));
}
}else{
throw new Exception('解密插件主程序文件失败,接口返回错误');
}
}
2022-07-25 10:46:40 -04:00
//本地解密插件主程序文件
public static function decode_plugin_main_local($main_filepath, $os = 'Linux'){
$btapi = self::get_btapi($os);
$userinfo = $btapi->get_user_info();
if(isset($userinfo['uid'])){
$src = file_get_contents($main_filepath);
$uid = $userinfo['uid'];
$serverid = $userinfo['serverid'];
$key = md5(substr($serverid, 10, 16).$uid.$serverid);
$iv = md5($key.$serverid);
$key = substr($key, 8, 16);
$iv = substr($iv, 8, 16);
2022-07-28 06:54:51 -04:00
$data_arr = explode("\n", $src);
$de_text = '';
foreach($data_arr as $data){
$data = trim($data);
if(!empty($data) && strlen($data)!=24){
$tmp = openssl_decrypt($data, 'aes-128-cbc', $key, 0, $iv);
if($tmp) $de_text .= $tmp;
}
}
2022-07-25 10:46:40 -04:00
if(!empty($de_text)){
file_put_contents($main_filepath, $de_text);
return true;
}
return false;
}else{
throw new Exception('解密插件主程序文件失败,获取用户信息失败');
}
}
2022-06-07 02:21:19 -04:00
//去除插件主程序文件授权校验
public static function noauth_plugin_main($main_filepath){
$data = file_get_contents($main_filepath);
if(!$data) return false;
2022-06-14 03:50:39 -04:00
2022-06-07 02:21:19 -04:00
$data = str_replace('\'http://www.bt.cn/api/panel/get_soft_list_test', 'public.GetConfigValue(\'home\')+\'/api/panel/get_soft_list_test', $data);
$data = str_replace('\'https://www.bt.cn/api/panel/get_soft_list_test', 'public.GetConfigValue(\'home\')+\'/api/panel/get_soft_list_test', $data);
$data = str_replace('\'http://www.bt.cn/api/panel/get_soft_list', 'public.GetConfigValue(\'home\')+\'/api/panel/get_soft_list', $data);
$data = str_replace('\'https://www.bt.cn/api/panel/get_soft_list', 'public.GetConfigValue(\'home\')+\'/api/panel/get_soft_list', $data);
$data = str_replace('\'http://www.bt.cn/api/panel/notpro', 'public.GetConfigValue(\'home\')+\'/api/panel/notpro', $data);
$data = str_replace('\'https://www.bt.cn/api/panel/notpro', 'public.GetConfigValue(\'home\')+\'/api/panel/notpro', $data);
2022-06-14 03:50:39 -04:00
$data = str_replace('\'http://www.bt.cn/api/wpanel/get_soft_list_test', 'public.GetConfigValue(\'home\')+\'/api/wpanel/get_soft_list_test', $data);
$data = str_replace('\'https://www.bt.cn/api/wpanel/get_soft_list_test', 'public.GetConfigValue(\'home\')+\'/api/wpanel/get_soft_list_test', $data);
$data = str_replace('\'http://www.bt.cn/api/wpanel/get_soft_list', 'public.GetConfigValue(\'home\')+\'/api/wpanel/get_soft_list', $data);
$data = str_replace('\'https://www.bt.cn/api/wpanel/get_soft_list', 'public.GetConfigValue(\'home\')+\'/api/wpanel/get_soft_list', $data);
$data = str_replace('\'http://www.bt.cn/api/wpanel/notpro', 'public.GetConfigValue(\'home\')+\'/api/wpanel/notpro', $data);
$data = str_replace('\'https://www.bt.cn/api/wpanel/notpro', 'public.GetConfigValue(\'home\')+\'/api/wpanel/notpro', $data);
2022-06-07 02:21:19 -04:00
file_put_contents($main_filepath, $data);
}
2022-06-04 11:22:19 -04:00
//下载插件其他文件
2022-06-14 03:50:39 -04:00
public static function download_plugin_other($fname, $filemd5 = null, $os = 'Linux'){
2022-06-04 11:22:19 -04:00
$filepath = get_data_dir().'plugins/other/'.$fname;
@mkdir(dirname($filepath), 0777, true);
2022-06-14 03:50:39 -04:00
$btapi = self::get_btapi($os);
2022-06-04 11:22:19 -04:00
$result = $btapi->get_plugin_other_filename($fname);
if($result && isset($result['status'])){
if($result['status'] == true){
$filename = $result['filename'];
self::download_file($btapi, $filename, $filepath);
if(file_exists($filepath)){
if($filemd5 && md5_file($filepath) != $filemd5){
2022-06-04 11:49:34 -04:00
@unlink($filepath);
2022-06-04 11:22:19 -04:00
throw new Exception('插件文件MD5校验失败');
}
return true;
}else{
throw new Exception('下载插件文件失败,本地文件不存在');
}
}else{
throw new Exception('下载插件文件失败:'.($result['msg']?$result['msg']:'未知错误'));
}
}else{
throw new Exception('下载插件文件失败,接口返回错误');
}
}
//下载文件
private static function download_file($btapi, $filename, $filepath){
try{
$btapi->download($filename, $filepath);
}catch(Exception $e){
2022-06-04 11:49:34 -04:00
@unlink($filepath);
2022-06-04 11:22:19 -04:00
//宝塔bug小文件下载失败改用base64下载
$result = $btapi->get_file($filename);
if($result && isset($result['status']) && $result['status']==true){
$filedata = base64_decode($result['data']);
if(strlen($filedata) < 4096 && substr($filedata,0,1)=='{' && substr($filedata,-1,1)=='}'){
$arr = json_decode($filedata, true);
if($arr){
throw new Exception('获取文件失败:'.($arr['msg']?$arr['msg']:'未知错误'));
}
}
if(!$filedata){
throw new Exception('获取文件失败:文件内容为空');
}
file_put_contents($filepath, $filedata);
}elseif($result){
throw new Exception('获取文件失败:'.($result['msg']?$result['msg']:'未知错误'));
}else{
throw new Exception('获取文件失败:未知错误');
}
}
}
2022-07-22 05:11:55 -04:00
//刷新一键部署列表
public static function refresh_deplist($os = 'Linux'){
$btapi = self::get_btapi($os);
$result = $btapi->get_deplist();
if($result && isset($result['list']) && isset($result['type'])){
if(empty($result['list']) || empty($result['type'])){
throw new Exception('获取一键部署列表失败:一键部署列表为空');
}
$json_file = get_data_dir($os).'config/deployment_list.json';
if(!file_put_contents($json_file, json_encode($result))){
throw new Exception('保存一键部署列表失败,文件无写入权限');
}
}else{
throw new Exception('获取一键部署列表失败:'.(isset($result['msg'])?$result['msg']:'面板连接失败'));
}
}
2022-06-14 03:50:39 -04:00
//获取一键部署列表
public static function get_deplist($os = 'Linux'){
$json_file = get_data_dir($os).'config/deployment_list.json';
if(file_exists($json_file)){
$data = file_get_contents($json_file);
$json_arr = json_decode($data, true);
if($json_arr){
return $json_arr;
}
}
return false;
}
2022-06-04 11:22:19 -04:00
}