qbittorrent.php 10 KB

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