4
0

qbittorrent.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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->writeLog('error', 'qBittorrent Connect Function - Error: Could not get session ID', 'SYSTEM');
  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->writeLog('error', 'qBittorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  98. $this->setAPIResponse('error', $e->getMessage(), 500);
  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. if (array_key_exists($key, $permissions)) {
  118. return $permissions[$key];
  119. } elseif ($key == 'all') {
  120. return $permissions;
  121. } else {
  122. return [];
  123. }
  124. }
  125. public function homepageOrderqBittorrent()
  126. {
  127. if ($this->homepageItemPermissions($this->qBittorrentHomepagePermissions('main'))) {
  128. $loadingBox = ($this->config['qBittorrentCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  129. $builder = ($this->config['qBittorrentCombine']) ? 'buildDownloaderCombined(\'qBittorrent\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("qBittorrent"));';
  130. return '
  131. <div id="' . __FUNCTION__ . '">
  132. ' . $loadingBox . '
  133. <script>
  134. // homepageOrderqBittorrent
  135. ' . $builder . '
  136. homepageDownloader("qBittorrent", "' . $this->config['qBittorrentRefresh'] . '");
  137. // End homepageOrderqBittorrent
  138. </script>
  139. </div>
  140. ';
  141. }
  142. }
  143. public function getQBittorrentHomepageQueue()
  144. {
  145. if (!$this->homepageItemPermissions($this->qBittorrentHomepagePermissions('main'), true)) {
  146. return false;
  147. }
  148. $digest = $this->qualifyURL($this->config['qBittorrentURL'], true);
  149. $data = array('username' => $this->config['qBittorrentUsername'], 'password' => $this->decrypt($this->config['qBittorrentPassword']));
  150. $apiVersionLogin = ($this->config['qBittorrentApiVersion'] == '1') ? '/login' : '/api/v2/auth/login';
  151. $apiVersionQuery = ($this->config['qBittorrentApiVersion'] == '1') ? '/query/torrents?sort=' : '/api/v2/torrents/info?sort=';
  152. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $apiVersionLogin;
  153. try {
  154. $options = $this->requestOptions($this->config['qBittorrentURL'], $this->config['qBittorrentRefresh'], $this->config['qBittorrentDisableCertCheck'], $this->config['qBittorrentUseCustomCertificate']);
  155. $response = Requests::post($url, [], $data, $options);
  156. $reflection = new ReflectionClass($response->cookies);
  157. $cookie = $reflection->getProperty("cookies");
  158. $cookie->setAccessible(true);
  159. $cookie = $cookie->getValue($response->cookies);
  160. if ($cookie) {
  161. $headers = array(
  162. 'Cookie' => 'SID=' . $cookie['SID']->value
  163. );
  164. $reverse = $this->config['qBittorrentReverseSorting'] ? 'true' : 'false';
  165. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $apiVersionQuery . $this->config['qBittorrentSortOrder'] . '&reverse=' . $reverse;
  166. $response = Requests::get($url, $headers, $options);
  167. if ($response) {
  168. $torrentList = json_decode($response->body, true);
  169. if ($this->config['qBittorrentHideSeeding'] || $this->config['qBittorrentHideCompleted']) {
  170. $filter = array();
  171. $torrents = array();
  172. if ($this->config['qBittorrentHideSeeding']) {
  173. array_push($filter, 'uploading', 'stalledUP', 'queuedUP');
  174. }
  175. if ($this->config['qBittorrentHideCompleted']) {
  176. array_push($filter, 'pausedUP');
  177. }
  178. foreach ($torrentList as $key => $value) {
  179. if (!in_array($value['state'], $filter)) {
  180. $torrents[] = $value;
  181. }
  182. }
  183. } else {
  184. $torrents = json_decode($response->body, true);
  185. }
  186. $api['content']['queueItems'] = $torrents;
  187. $api['content']['historyItems'] = false;
  188. $api['content'] = $api['content'] ?? false;
  189. $this->setAPIResponse('success', null, 200, $api);
  190. return $api;
  191. }
  192. } else {
  193. $this->writeLog('error', 'qBittorrent Connect Function - Error: Could not get session ID', 'SYSTEM');
  194. $this->setAPIResponse('error', 'qBittorrent Connect Function - Error: Could not get session ID', 409);
  195. return false;
  196. }
  197. } catch (Requests_Exception $e) {
  198. $this->writeLog('error', 'qBittorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  199. $this->setAPIResponse('error', $e->getMessage(), 500);
  200. return false;
  201. }
  202. }
  203. }