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.

229 lines
5.9 KiB

1 year ago
  1. <?php
  2. namespace app\lib;
  3. use Exception;
  4. class Btapi
  5. {
  6. private $BT_KEY; //接口密钥
  7. private $BT_PANEL; //面板地址
  8. public function __construct($bt_panel, $bt_key){
  9. $this->BT_PANEL = $bt_panel;
  10. $this->BT_KEY = $bt_key;
  11. }
  12. //获取面板配置信息
  13. public function get_config(){
  14. $url = $this->BT_PANEL.'/config?action=get_config';
  15. $p_data = $this->GetKeyData();
  16. $result = $this->curl($url,$p_data);
  17. $data = json_decode($result,true);
  18. return $data;
  19. }
  20. //获取已登录用户信息
  21. public function get_user_info(){
  22. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=get_user_info';
  23. $p_data = $this->GetKeyData();
  24. $result = $this->curl($url,$p_data);
  25. $data = json_decode($result,true);
  26. return $data;
  27. }
  28. //从云端获取插件列表
  29. public function get_plugin_list(){
  30. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=get_plugin_list';
  31. $p_data = $this->GetKeyData();
  32. $result = $this->curl($url,$p_data);
  33. $data = json_decode($result,true);
  34. return $data;
  35. }
  36. //下载插件包,返回文件路径
  37. public function get_plugin_filename($plugin_name, $version){
  38. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=download_plugin';
  39. $p_data = $this->GetKeyData();
  40. $p_data['plugin_name'] = $plugin_name;
  41. $p_data['version'] = $version;
  42. $result = $this->curl($url,$p_data);
  43. $data = json_decode($result,true);
  44. return $data;
  45. }
  46. //下载插件主程序文件,返回文件路径
  47. public function get_plugin_main_filename($plugin_name, $version){
  48. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=download_plugin_main';
  49. $p_data = $this->GetKeyData();
  50. $p_data['plugin_name'] = $plugin_name;
  51. $p_data['version'] = $version;
  52. $result = $this->curl($url,$p_data);
  53. $data = json_decode($result,true);
  54. return $data;
  55. }
  56. //解密插件主程序py代码,返回文件路径
  57. public function get_decode_plugin_main($plugin_name, $version){
  58. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=decode_plugin_main';
  59. $p_data = $this->GetKeyData();
  60. $p_data['plugin_name'] = $plugin_name;
  61. $p_data['version'] = $version;
  62. $result = $this->curl($url,$p_data);
  63. $data = json_decode($result,true);
  64. return $data;
  65. }
  66. //下载插件其他文件,返回文件路径
  67. public function get_plugin_other_filename($fname){
  68. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=download_plugin_other';
  69. $p_data = $this->GetKeyData();
  70. $p_data['fname'] = $fname;
  71. $result = $this->curl($url,$p_data);
  72. $data = json_decode($result,true);
  73. return $data;
  74. }
  75. //下载文件
  76. public function download($filename, $localpath){
  77. $url = $this->BT_PANEL.'/download';
  78. $p_data = $this->GetKeyData();
  79. $p_data['filename'] = $filename;
  80. $result = $this->curl_download($url.'?'.http_build_query($p_data), $localpath);
  81. return $result;
  82. }
  83. //获取文件base64
  84. public function get_file($filename){
  85. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=get_file';
  86. $p_data = $this->GetKeyData();
  87. $p_data['filename'] = $filename;
  88. $result = $this->curl($url,$p_data);
  89. $data = json_decode($result,true);
  90. return $data;
  91. }
  92. //购买第三方插件
  93. public function create_plugin_other_order($pid){
  94. $url = $this->BT_PANEL.'/auth?action=create_plugin_other_order';
  95. $p_data = $this->GetKeyData();
  96. $p_data['pid'] = $pid;
  97. $p_data['cycle'] = '999';
  98. $p_data['type'] = '0';
  99. $result = $this->curl($url,$p_data);
  100. $data = json_decode($result,true);
  101. return $data;
  102. }
  103. //获取一键部署列表
  104. public function get_deplist(){
  105. $url = $this->BT_PANEL.'/plugin?action=a&name=kaixin&s=get_deplist';
  106. $p_data = $this->GetKeyData();
  107. $result = $this->curl($url,$p_data);
  108. $data = json_decode($result,true);
  109. return $data;
  110. }
  111. private function GetKeyData(){
  112. $now_time = time();
  113. $p_data = array(
  114. 'request_token' => md5($now_time.''.md5($this->BT_KEY)),
  115. 'request_time' => $now_time
  116. );
  117. return $p_data;
  118. }
  119. private function curl($url, $data = null, $timeout = 60)
  120. {
  121. //定义cookie保存位置
  122. $cookie_file=app()->getRuntimePath().md5($this->BT_PANEL).'.cookie';
  123. if(!file_exists($cookie_file)){
  124. touch($cookie_file);
  125. }
  126. $ch = curl_init();
  127. curl_setopt($ch, CURLOPT_URL, $url);
  128. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  129. if($data){
  130. curl_setopt($ch, CURLOPT_POST, 1);
  131. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  132. }
  133. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  134. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  135. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  136. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  137. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  138. $output = curl_exec($ch);
  139. curl_close($ch);
  140. return $output;
  141. }
  142. private function curl_download($url, $localpath, $timeout = 300)
  143. {
  144. //定义cookie保存位置
  145. $cookie_file=app()->getRuntimePath().md5($this->BT_PANEL).'.cookie';
  146. if(!file_exists($cookie_file)){
  147. touch($cookie_file);
  148. }
  149. $ch = curl_init();
  150. curl_setopt($ch, CURLOPT_URL, $url);
  151. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  152. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  153. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  154. $fp = fopen($localpath, 'w+');
  155. curl_setopt($ch, CURLOPT_FILE, $fp);
  156. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  157. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  158. curl_exec($ch);
  159. if (curl_errno($ch)) {
  160. $message = curl_error($ch);
  161. curl_close($ch);
  162. fclose($fp);
  163. throw new Exception('下载文件失败:'.$message);
  164. }
  165. $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  166. if($httpcode>299){
  167. curl_close($ch);
  168. fclose($fp);
  169. throw new Exception('下载文件失败:HTTPCODE-'.$httpcode);
  170. }
  171. curl_close($ch);
  172. fclose($fp);
  173. return true;
  174. }
  175. }