qbittorrent.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. trait QBitTorrentHomepageItem
  3. {
  4. public function qBittorrentSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'qBittorrent',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/qBittorrent.png',
  10. 'category' => 'Downloader',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'Enable' => [
  20. $this->settingsOption('enable', 'homepageqBittorrentEnabled'),
  21. $this->settingsOption('auth', 'homepageqBittorrentAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('url', 'qBittorrentURL'),
  25. $this->settingsOption('select', 'qBittorrentApiVersion', ['label' => 'API Version', 'options' => $this->qBittorrentApiOptions()]),
  26. $this->settingsOption('username', 'qBittorrentUsername'),
  27. $this->settingsOption('password', 'qBittorrentPassword'),
  28. $this->settingsOption('disable-cert-check', 'qBittorrentDisableCertCheck'),
  29. $this->settingsOption('use-custom-certificate', 'qBittorrentUseCustomCertificate'),
  30. ],
  31. 'API SOCKS' => [
  32. $this->settingsOption('socks', 'qbittorrent'),
  33. $this->settingsOption('blank'),
  34. $this->settingsOption('enable', 'qBittorrentSocksEnabled'),
  35. $this->settingsOption('auth', 'qBittorrentSocksAuth'),
  36. ],
  37. 'Misc Options' => [
  38. $this->settingsOption('hide-seeding', 'qBittorrentHideSeeding'),
  39. $this->settingsOption('hide-completed', 'qBittorrentHideCompleted'),
  40. $this->settingsOption('select', 'qBittorrentSortOrder', ['label' => 'Order', 'options' => $this->qBittorrentSortOptions()]),
  41. $this->settingsOption('switch', 'qBittorrentReverseSorting', ['label' => 'Reverse Sorting']),
  42. $this->settingsOption('refresh', 'qBittorrentRefresh'),
  43. $this->settingsOption('combine', 'qBittorrentCombine'),
  44. ],
  45. 'Test Connection' => [
  46. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  47. $this->settingsOption('test', 'qbittorrent'),
  48. ]
  49. ]
  50. ];
  51. return array_merge($homepageInformation, $homepageSettings);
  52. }
  53. public function testConnectionQBittorrent()
  54. {
  55. if (empty($this->config['qBittorrentURL'])) {
  56. $this->setAPIResponse('error', 'qBittorrent URL is not defined', 422);
  57. return false;
  58. }
  59. $digest = $this->qualifyURL($this->config['qBittorrentURL'], true);
  60. $data = array('username' => $this->config['qBittorrentUsername'], 'password' => $this->decrypt($this->config['qBittorrentPassword']));
  61. $apiVersionLogin = ($this->config['qBittorrentApiVersion'] == '1') ? '/login' : '/api/v2/auth/login';
  62. $apiVersionQuery = ($this->config['qBittorrentApiVersion'] == '1') ? '/query/torrents?sort=' : '/api/v2/torrents/info?sort=';
  63. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $apiVersionLogin;
  64. try {
  65. $options = $this->requestOptions($this->config['qBittorrentURL'], null, $this->config['qBittorrentDisableCertCheck'], $this->config['qBittorrentUseCustomCertificate']);
  66. $response = Requests::post($url, [], $data, $options);
  67. $reflection = new ReflectionClass($response->cookies);
  68. $cookie = $reflection->getProperty("cookies");
  69. $cookie->setAccessible(true);
  70. $cookie = $cookie->getValue($response->cookies);
  71. if ($cookie) {
  72. $headers = array(
  73. 'Cookie' => 'SID=' . $cookie['SID']->value
  74. );
  75. $reverse = $this->config['qBittorrentReverseSorting'] ? 'true' : 'false';
  76. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $apiVersionQuery . $this->config['qBittorrentSortOrder'] . '&reverse=' . $reverse;
  77. $response = Requests::get($url, $headers, $options);
  78. if ($response) {
  79. $torrents = json_decode($response->body, true);
  80. if (is_array($torrents)) {
  81. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  82. return true;
  83. } else {
  84. $this->setAPIResponse('error', 'qBittorrent Error Occurred - Check URL or Credentials', 500);
  85. return true;
  86. }
  87. } else {
  88. $this->setAPIResponse('error', 'qBittorrent Connection Error Occurred - Check URL or Credentials', 500);
  89. return true;
  90. }
  91. } else {
  92. $this->setLoggerChannel('qBittorrent')->warning('Could not get session ID');
  93. $this->setAPIResponse('error', 'qBittorrent Connect Function - Error: Could not get session ID', 409);
  94. return false;
  95. }
  96. } catch (Requests_Exception $e) {
  97. $this->setLoggerChannel('qBittorrent')->error($e);
  98. $this->setResponse(500, $e->getMessage());
  99. return false;
  100. }
  101. }
  102. public function qBittorrentHomepagePermissions($key = null)
  103. {
  104. $permissions = [
  105. 'main' => [
  106. 'enabled' => [
  107. 'homepageqBittorrentEnabled'
  108. ],
  109. 'auth' => [
  110. 'homepageqBittorrentAuth'
  111. ],
  112. 'not_empty' => [
  113. 'qBittorrentURL'
  114. ]
  115. ]
  116. ];
  117. return $this->homepageCheckKeyPermissions($key, $permissions);
  118. }
  119. public function homepageOrderqBittorrent()
  120. {
  121. if ($this->homepageItemPermissions($this->qBittorrentHomepagePermissions('main'))) {
  122. $loadingBox = ($this->config['qBittorrentCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  123. $builder = ($this->config['qBittorrentCombine']) ? 'buildDownloaderCombined(\'qBittorrent\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("qBittorrent"));';
  124. return '
  125. <div id="' . __FUNCTION__ . '">
  126. ' . $loadingBox . '
  127. <script>
  128. // homepageOrderqBittorrent
  129. ' . $builder . '
  130. homepageDownloader("qBittorrent", "' . $this->config['qBittorrentRefresh'] . '");
  131. // End homepageOrderqBittorrent
  132. </script>
  133. </div>
  134. ';
  135. }
  136. }
  137. public function getQBittorrentHomepageQueue()
  138. {
  139. if (!$this->homepageItemPermissions($this->qBittorrentHomepagePermissions('main'), true)) {
  140. return false;
  141. }
  142. $digest = $this->qualifyURL($this->config['qBittorrentURL'], true);
  143. $data = array('username' => $this->config['qBittorrentUsername'], 'password' => $this->decrypt($this->config['qBittorrentPassword']));
  144. $apiVersionLogin = ($this->config['qBittorrentApiVersion'] == '1') ? '/login' : '/api/v2/auth/login';
  145. $apiVersionQuery = ($this->config['qBittorrentApiVersion'] == '1') ? '/query/torrents?sort=' : '/api/v2/torrents/info?sort=';
  146. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $apiVersionLogin;
  147. try {
  148. $options = $this->requestOptions($this->config['qBittorrentURL'], $this->config['qBittorrentRefresh'], $this->config['qBittorrentDisableCertCheck'], $this->config['qBittorrentUseCustomCertificate']);
  149. $response = Requests::post($url, [], $data, $options);
  150. $reflection = new ReflectionClass($response->cookies);
  151. $cookie = $reflection->getProperty("cookies");
  152. $cookie->setAccessible(true);
  153. $cookie = $cookie->getValue($response->cookies);
  154. if ($cookie) {
  155. $headers = array(
  156. 'Cookie' => 'SID=' . $cookie['SID']->value
  157. );
  158. $reverse = $this->config['qBittorrentReverseSorting'] ? 'true' : 'false';
  159. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $apiVersionQuery . $this->config['qBittorrentSortOrder'] . '&reverse=' . $reverse;
  160. $response = Requests::get($url, $headers, $options);
  161. if ($response) {
  162. $torrentList = json_decode($response->body, true);
  163. if ($this->config['qBittorrentHideSeeding'] || $this->config['qBittorrentHideCompleted']) {
  164. $filter = array();
  165. $torrents = array();
  166. if ($this->config['qBittorrentHideSeeding']) {
  167. array_push($filter, 'uploading', 'stalledUP', 'queuedUP');
  168. }
  169. if ($this->config['qBittorrentHideCompleted']) {
  170. array_push($filter, 'pausedUP');
  171. }
  172. foreach ($torrentList as $key => $value) {
  173. if (!in_array($value['state'], $filter)) {
  174. $torrents[] = $value;
  175. }
  176. }
  177. } else {
  178. $torrents = json_decode($response->body, true);
  179. }
  180. $api['content']['queueItems'] = $torrents;
  181. $api['content']['historyItems'] = false;
  182. $api['content'] = $api['content'] ?? false;
  183. $this->setAPIResponse('success', null, 200, $api);
  184. return $api;
  185. }
  186. } else {
  187. $this->setLoggerChannel('qBittorrent')->warning('Could not get session ID');
  188. $this->setAPIResponse('error', 'qBittorrent Connect Function - Error: Could not get session ID', 409);
  189. return false;
  190. }
  191. } catch (Requests_Exception $e) {
  192. $this->setLoggerChannel('qBittorrent')->error($e);
  193. $this->setResponse(500, $e->getMessage());
  194. return false;
  195. }
  196. }
  197. }