qbittorrent.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 getQBittorrentHomepageQueue()
  162. {
  163. if (!$this->config['homepageqBittorrentEnabled']) {
  164. $this->setAPIResponse('error', 'qBittorrent homepage item is not enabled', 409);
  165. return false;
  166. }
  167. if (!$this->qualifyRequest($this->config['homepageqBittorrentAuth'])) {
  168. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  169. return false;
  170. }
  171. if (empty($this->config['qBittorrentURL'])) {
  172. $this->setAPIResponse('error', 'qBittorrent URL is not defined', 422);
  173. return false;
  174. }
  175. $digest = $this->qualifyURL($this->config['qBittorrentURL'], true);
  176. $data = array('username' => $this->config['qBittorrentUsername'], 'password' => $this->decrypt($this->config['qBittorrentPassword']));
  177. $apiVersionLogin = ($this->config['qBittorrentApiVersion'] == '1') ? '/login' : '/api/v2/auth/login';
  178. $apiVersionQuery = ($this->config['qBittorrentApiVersion'] == '1') ? '/query/torrents?sort=' : '/api/v2/torrents/info?sort=';
  179. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $apiVersionLogin;
  180. try {
  181. $options = ($this->localURL($this->config['qBittorrentURL'])) ? array('verify' => false) : array();
  182. $response = Requests::post($url, array(), $data, $options);
  183. $reflection = new ReflectionClass($response->cookies);
  184. $cookie = $reflection->getProperty("cookies");
  185. $cookie->setAccessible(true);
  186. $cookie = $cookie->getValue($response->cookies);
  187. if ($cookie) {
  188. $headers = array(
  189. 'Cookie' => 'SID=' . $cookie['SID']->value
  190. );
  191. $reverse = $this->config['qBittorrentReverseSorting'] ? 'true' : 'false';
  192. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $apiVersionQuery . $this->config['qBittorrentSortOrder'] . '&reverse=' . $reverse;
  193. $response = Requests::get($url, $headers, $options);
  194. if ($response) {
  195. $torrentList = json_decode($response->body, true);
  196. if ($this->config['qBittorrentHideSeeding'] || $this->config['qBittorrentHideCompleted']) {
  197. $filter = array();
  198. $torrents = array();
  199. if ($this->config['qBittorrentHideSeeding']) {
  200. array_push($filter, 'uploading', 'stalledUP', 'queuedUP');
  201. }
  202. if ($this->config['qBittorrentHideCompleted']) {
  203. array_push($filter, 'pausedUP');
  204. }
  205. foreach ($torrentList as $key => $value) {
  206. if (!in_array($value['state'], $filter)) {
  207. $torrents[] = $value;
  208. }
  209. }
  210. } else {
  211. $torrents = json_decode($response->body, true);
  212. }
  213. $api['content']['queueItems'] = $torrents;
  214. $api['content']['historyItems'] = false;
  215. $api['content'] = isset($api['content']) ? $api['content'] : false;
  216. $this->setAPIResponse('success', null, 200, $api);
  217. return $api;
  218. }
  219. } else {
  220. $this->writeLog('error', 'qBittorrent Connect Function - Error: Could not get session ID', 'SYSTEM');
  221. $this->setAPIResponse('error', 'qBittorrent Connect Function - Error: Could not get session ID', 409);
  222. return false;
  223. }
  224. } catch (Requests_Exception $e) {
  225. $this->writeLog('error', 'qBittorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  226. $this->setAPIResponse('error', $e->getMessage(), 500);
  227. return false;
  228. }
  229. }
  230. }