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.

202 lines
5.3 KiB

3 years 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. private function GetKeyData(){
  93. $now_time = time();
  94. $p_data = array(
  95. 'request_token' => md5($now_time.''.md5($this->BT_KEY)),
  96. 'request_time' => $now_time
  97. );
  98. return $p_data;
  99. }
  100. private function curl($url, $data = null, $timeout = 60)
  101. {
  102. //定义cookie保存位置
  103. $cookie_file=app()->getRuntimePath().md5($this->BT_PANEL).'.cookie';
  104. if(!file_exists($cookie_file)){
  105. touch($cookie_file);
  106. }
  107. $ch = curl_init();
  108. curl_setopt($ch, CURLOPT_URL, $url);
  109. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  110. if($data){
  111. curl_setopt($ch, CURLOPT_POST, 1);
  112. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  113. }
  114. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  115. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  116. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  117. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  118. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  119. $output = curl_exec($ch);
  120. curl_close($ch);
  121. return $output;
  122. }
  123. private function curl_download($url, $localpath, $timeout = 60)
  124. {
  125. //定义cookie保存位置
  126. $cookie_file=app()->getRuntimePath().md5($this->BT_PANEL).'.cookie';
  127. if(!file_exists($cookie_file)){
  128. touch($cookie_file);
  129. }
  130. $ch = curl_init();
  131. curl_setopt($ch, CURLOPT_URL, $url);
  132. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  133. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
  134. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
  135. $fp = fopen($localpath, 'w+');
  136. curl_setopt($ch, CURLOPT_FILE, $fp);
  137. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  138. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  139. curl_exec($ch);
  140. if (curl_errno($ch)) {
  141. $message = curl_error($ch);
  142. curl_close($ch);
  143. fclose($fp);
  144. throw new Exception('下载文件失败:'.$message);
  145. }
  146. $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  147. if($httpcode>299){
  148. curl_close($ch);
  149. fclose($fp);
  150. throw new Exception('下载文件失败:HTTPCODE-'.$httpcode);
  151. }
  152. curl_close($ch);
  153. fclose($fp);
  154. return true;
  155. }
  156. }