qbittorrent.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. trait QBitTorrentHomepageItem
  3. {
  4. public function qBittorrentSettingsArray()
  5. {
  6. return array(
  7. 'name' => 'qBittorrent',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/qBittorrent.png',
  10. 'category' => 'Downloader',
  11. 'settings' => array(
  12. 'Enable' => array(
  13. array(
  14. 'type' => 'switch',
  15. 'name' => 'homepageqBittorrentEnabled',
  16. 'label' => 'Enable',
  17. 'value' => $this->config['homepageqBittorrentEnabled']
  18. ),
  19. array(
  20. 'type' => 'select',
  21. 'name' => 'homepageqBittorrentAuth',
  22. 'label' => 'Minimum Authentication',
  23. 'value' => $this->config['homepageqBittorrentAuth'],
  24. 'options' => $this->groupOptions
  25. )
  26. ),
  27. 'Connection' => array(
  28. array(
  29. 'type' => 'input',
  30. 'name' => 'qBittorrentURL',
  31. 'label' => 'URL',
  32. 'value' => $this->config['qBittorrentURL'],
  33. 'help' => 'Please make sure to use local IP address and port - You also may use local dns name too.',
  34. 'placeholder' => 'http(s)://hostname:port'
  35. ),
  36. array(
  37. 'type' => 'select',
  38. 'name' => 'qBittorrentApiVersion',
  39. 'label' => 'API Version',
  40. 'value' => $this->config['qBittorrentApiVersion'],
  41. 'options' => $this->qBittorrentApiOptions()
  42. ),
  43. array(
  44. 'type' => 'input',
  45. 'name' => 'qBittorrentUsername',
  46. 'label' => 'Username',
  47. 'value' => $this->config['qBittorrentUsername']
  48. ),
  49. array(
  50. 'type' => 'password',
  51. 'name' => 'qBittorrentPassword',
  52. 'label' => 'Password',
  53. 'value' => $this->config['qBittorrentPassword']
  54. )
  55. ),
  56. 'Misc Options' => array(
  57. array(
  58. 'type' => 'switch',
  59. 'name' => 'qBittorrentHideSeeding',
  60. 'label' => 'Hide Seeding',
  61. 'value' => $this->config['qBittorrentHideSeeding']
  62. ),
  63. array(
  64. 'type' => 'switch',
  65. 'name' => 'qBittorrentHideCompleted',
  66. 'label' => 'Hide Completed',
  67. 'value' => $this->config['qBittorrentHideCompleted']
  68. ),
  69. array(
  70. 'type' => 'select',
  71. 'name' => 'qBittorrentSortOrder',
  72. 'label' => 'Order',
  73. 'value' => $this->config['qBittorrentSortOrder'],
  74. 'options' => $this->qBittorrentSortOptions()
  75. ), array(
  76. 'type' => 'switch',
  77. 'name' => 'qBittorrentReverseSorting',
  78. 'label' => 'Reverse Sorting',
  79. 'value' => $this->config['qBittorrentReverseSorting']
  80. ),
  81. array(
  82. 'type' => 'select',
  83. 'name' => 'homepageDownloadRefresh',
  84. 'label' => 'Refresh Seconds',
  85. 'value' => $this->config['homepageDownloadRefresh'],
  86. 'options' => $this->timeOptions()
  87. ),
  88. array(
  89. 'type' => 'switch',
  90. 'name' => 'qBittorrentCombine',
  91. 'label' => 'Add to Combined Downloader',
  92. 'value' => $this->config['qBittorrentCombine']
  93. ),
  94. ),
  95. 'Test Connection' => array(
  96. array(
  97. 'type' => 'blank',
  98. 'label' => 'Please Save before Testing'
  99. ),
  100. array(
  101. 'type' => 'button',
  102. 'label' => '',
  103. 'icon' => 'fa fa-flask',
  104. 'class' => 'pull-right',
  105. 'text' => 'Test Connection',
  106. 'attr' => 'onclick="testAPIConnection(\'qbittorrent\')"'
  107. ),
  108. )
  109. )
  110. );
  111. }
  112. public function testConnectionQBittorrent()
  113. {
  114. if (empty($this->config['qBittorrentURL'])) {
  115. $this->setAPIResponse('error', 'qBittorrent URL is not defined', 422);
  116. return false;
  117. }
  118. $digest = $this->qualifyURL($this->config['qBittorrentURL'], true);
  119. $data = array('username' => $this->config['qBittorrentUsername'], 'password' => $this->decrypt($this->config['qBittorrentPassword']));
  120. $apiVersionLogin = ($this->config['qBittorrentApiVersion'] == '1') ? '/login' : '/api/v2/auth/login';
  121. $apiVersionQuery = ($this->config['qBittorrentApiVersion'] == '1') ? '/query/torrents?sort=' : '/api/v2/torrents/info?sort=';
  122. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $apiVersionLogin;
  123. try {
  124. $options = ($this->localURL($this->config['qBittorrentURL'])) ? array('verify' => false) : array();
  125. $response = Requests::post($url, array(), $data, $options);
  126. $reflection = new ReflectionClass($response->cookies);
  127. $cookie = $reflection->getProperty("cookies");
  128. $cookie->setAccessible(true);
  129. $cookie = $cookie->getValue($response->cookies);
  130. if ($cookie) {
  131. $headers = array(
  132. 'Cookie' => 'SID=' . $cookie['SID']->value
  133. );
  134. $reverse = $this->config['qBittorrentReverseSorting'] ? 'true' : 'false';
  135. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $apiVersionQuery . $this->config['qBittorrentSortOrder'] . '&reverse=' . $reverse;
  136. $response = Requests::get($url, $headers, $options);
  137. if ($response) {
  138. $torrents = json_decode($response->body, true);
  139. if (is_array($torrents)) {
  140. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  141. return true;
  142. } else {
  143. $this->setAPIResponse('error', 'qBittorrent Error Occurred - Check URL or Credentials', 500);
  144. return true;
  145. }
  146. } else {
  147. $this->setAPIResponse('error', 'qBittorrent Connection Error Occurred - Check URL or Credentials', 500);
  148. return true;
  149. }
  150. } else {
  151. $this->writeLog('error', 'qBittorrent Connect Function - Error: Could not get session ID', 'SYSTEM');
  152. $this->setAPIResponse('error', 'qBittorrent Connect Function - Error: Could not get session ID', 409);
  153. return false;
  154. }
  155. } catch (Requests_Exception $e) {
  156. $this->writeLog('error', 'qBittorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  157. $this->setAPIResponse('error', $e->getMessage(), 500);
  158. return false;
  159. }
  160. }
  161. public function qBittorrentHomepagePermissions($key = null)
  162. {
  163. $permissions = [
  164. 'main' => [
  165. 'enabled' => [
  166. 'homepageqBittorrentEnabled'
  167. ],
  168. 'auth' => [
  169. 'homepageqBittorrentAuth'
  170. ],
  171. 'not_empty' => [
  172. 'qBittorrentURL'
  173. ]
  174. ]
  175. ];
  176. if (array_key_exists($key, $permissions)) {
  177. return $permissions[$key];
  178. } elseif ($key == 'all') {
  179. return $permissions;
  180. } else {
  181. return [];
  182. }
  183. }
  184. public function homepageOrderqBittorrent()
  185. {
  186. if ($this->homepageItemPermissions($this->qBittorrentHomepagePermissions('main'))) {
  187. $loadingBox = ($this->config['qBittorrentCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  188. $builder = ($this->config['qBittorrentCombine']) ? 'buildDownloaderCombined(\'qBittorrent\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("qBittorrent"));';
  189. return '
  190. <div id="' . __FUNCTION__ . '">
  191. ' . $loadingBox . '
  192. <script>
  193. // homepageOrderqBittorrent
  194. ' . $builder . '
  195. homepageDownloader("qBittorrent", "' . $this->config['homepageDownloadRefresh'] . '");
  196. // End homepageOrderqBittorrent
  197. </script>
  198. </div>
  199. ';
  200. }
  201. }
  202. public function getQBittorrentHomepageQueue()
  203. {
  204. if (!$this->homepageItemPermissions($this->qBittorrentHomepagePermissions('main'), true)) {
  205. return false;
  206. }
  207. $digest = $this->qualifyURL($this->config['qBittorrentURL'], true);
  208. $data = array('username' => $this->config['qBittorrentUsername'], 'password' => $this->decrypt($this->config['qBittorrentPassword']));
  209. $apiVersionLogin = ($this->config['qBittorrentApiVersion'] == '1') ? '/login' : '/api/v2/auth/login';
  210. $apiVersionQuery = ($this->config['qBittorrentApiVersion'] == '1') ? '/query/torrents?sort=' : '/api/v2/torrents/info?sort=';
  211. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $apiVersionLogin;
  212. try {
  213. $options = ($this->localURL($this->config['qBittorrentURL'])) ? array('verify' => false) : array();
  214. $response = Requests::post($url, array(), $data, $options);
  215. $reflection = new ReflectionClass($response->cookies);
  216. $cookie = $reflection->getProperty("cookies");
  217. $cookie->setAccessible(true);
  218. $cookie = $cookie->getValue($response->cookies);
  219. if ($cookie) {
  220. $headers = array(
  221. 'Cookie' => 'SID=' . $cookie['SID']->value
  222. );
  223. $reverse = $this->config['qBittorrentReverseSorting'] ? 'true' : 'false';
  224. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $apiVersionQuery . $this->config['qBittorrentSortOrder'] . '&reverse=' . $reverse;
  225. $response = Requests::get($url, $headers, $options);
  226. if ($response) {
  227. $torrentList = json_decode($response->body, true);
  228. if ($this->config['qBittorrentHideSeeding'] || $this->config['qBittorrentHideCompleted']) {
  229. $filter = array();
  230. $torrents = array();
  231. if ($this->config['qBittorrentHideSeeding']) {
  232. array_push($filter, 'uploading', 'stalledUP', 'queuedUP');
  233. }
  234. if ($this->config['qBittorrentHideCompleted']) {
  235. array_push($filter, 'pausedUP');
  236. }
  237. foreach ($torrentList as $key => $value) {
  238. if (!in_array($value['state'], $filter)) {
  239. $torrents[] = $value;
  240. }
  241. }
  242. } else {
  243. $torrents = json_decode($response->body, true);
  244. }
  245. $api['content']['queueItems'] = $torrents;
  246. $api['content']['historyItems'] = false;
  247. $api['content'] = isset($api['content']) ? $api['content'] : false;
  248. $this->setAPIResponse('success', null, 200, $api);
  249. return $api;
  250. }
  251. } else {
  252. $this->writeLog('error', 'qBittorrent Connect Function - Error: Could not get session ID', 'SYSTEM');
  253. $this->setAPIResponse('error', 'qBittorrent Connect Function - Error: Could not get session ID', 409);
  254. return false;
  255. }
  256. } catch (Requests_Exception $e) {
  257. $this->writeLog('error', 'qBittorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  258. $this->setAPIResponse('error', $e->getMessage(), 500);
  259. return false;
  260. }
  261. }
  262. }