qbittorrent.php 11 KB

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