transmission.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. trait TransmissionHomepageItem
  3. {
  4. public function transmissionSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'Transmission',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/transmission.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' => 'homepageTransmissionEnabled',
  23. 'label' => 'Enable',
  24. 'value' => $this->config['homepageTransmissionEnabled']
  25. ),
  26. array(
  27. 'type' => 'select',
  28. 'name' => 'homepageTransmissionAuth',
  29. 'label' => 'Minimum Authentication',
  30. 'value' => $this->config['homepageTransmissionAuth'],
  31. 'options' => $this->groupOptions
  32. )
  33. ),
  34. 'Connection' => array(
  35. array(
  36. 'type' => 'input',
  37. 'name' => 'transmissionURL',
  38. 'label' => 'URL',
  39. 'value' => $this->config['transmissionURL'],
  40. 'help' => 'Please do not included /web in URL. 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' => 'transmissionDisableCertCheck',
  46. 'label' => 'Disable Certificate Check',
  47. 'value' => $this->config['transmissionDisableCertCheck']
  48. ),
  49. array(
  50. 'type' => 'input',
  51. 'name' => 'transmissionUsername',
  52. 'label' => 'Username',
  53. 'value' => $this->config['transmissionUsername']
  54. ),
  55. array(
  56. 'type' => 'password',
  57. 'name' => 'transmissionPassword',
  58. 'label' => 'Password',
  59. 'value' => $this->config['transmissionPassword']
  60. )
  61. ),
  62. 'Misc Options' => array(
  63. array(
  64. 'type' => 'switch',
  65. 'name' => 'transmissionHideSeeding',
  66. 'label' => 'Hide Seeding',
  67. 'value' => $this->config['transmissionHideSeeding']
  68. ), array(
  69. 'type' => 'switch',
  70. 'name' => 'transmissionHideCompleted',
  71. 'label' => 'Hide Completed',
  72. 'value' => $this->config['transmissionHideCompleted']
  73. ),
  74. array(
  75. 'type' => 'select',
  76. 'name' => 'transmissionRefresh',
  77. 'label' => 'Refresh Seconds',
  78. 'value' => $this->config['transmissionRefresh'],
  79. 'options' => $this->timeOptions()
  80. ),
  81. array(
  82. 'type' => 'switch',
  83. 'name' => 'transmissionCombine',
  84. 'label' => 'Add to Combined Downloader',
  85. 'value' => $this->config['transmissionCombine']
  86. ),
  87. ),
  88. 'Test Connection' => array(
  89. array(
  90. 'type' => 'blank',
  91. 'label' => 'Please Save before Testing'
  92. ),
  93. array(
  94. 'type' => 'button',
  95. 'label' => '',
  96. 'icon' => 'fa fa-flask',
  97. 'class' => 'pull-right',
  98. 'text' => 'Test Connection',
  99. 'attr' => 'onclick="testAPIConnection(\'transmission\')"'
  100. ),
  101. )
  102. )
  103. );
  104. return array_merge($homepageInformation, $homepageSettings);
  105. }
  106. public function testConnectionTransmission()
  107. {
  108. if (empty($this->config['transmissionURL'])) {
  109. $this->setAPIResponse('error', 'Transmission URL is not defined', 422);
  110. return false;
  111. }
  112. $digest = $this->qualifyURL($this->config['transmissionURL'], true);
  113. $passwordInclude = ($this->config['transmissionUsername'] != '' && $this->config['transmissionPassword'] != '') ? $this->config['transmissionUsername'] . ':' . rawurlencode($this->decrypt($this->config['transmissionPassword'])) . "@" : '';
  114. $url = $digest['scheme'] . '://' . $passwordInclude . $digest['host'] . $digest['port'] . $digest['path'] . '/rpc';
  115. try {
  116. $options = $this->requestOptions($this->config['transmissionURL'], $this->config['transmissionDisableCertCheck'], $this->config['transmissionRefresh']);
  117. $response = Requests::get($url, array(), $options);
  118. if ($response->headers['x-transmission-session-id']) {
  119. $headers = array(
  120. 'X-Transmission-Session-Id' => $response->headers['x-transmission-session-id'],
  121. 'Content-Type' => 'application/json'
  122. );
  123. $data = array(
  124. 'method' => 'torrent-get',
  125. 'arguments' => array(
  126. 'fields' => array(
  127. "id", "name", "totalSize", "eta", "isFinished", "isStalled", "percentDone", "rateDownload", "status", "downloadDir", "errorString"
  128. ),
  129. ),
  130. 'tags' => ''
  131. );
  132. $response = Requests::post($url, $headers, json_encode($data), $options);
  133. if ($response->success) {
  134. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  135. return true;
  136. } else {
  137. $this->setAPIResponse('error', 'Transmission Connect Function - Error: Unknown', 500);
  138. return false;
  139. }
  140. } else {
  141. $this->writeLog('error', 'Transmission Connect Function - Error: Could not get session ID', 'SYSTEM');
  142. $this->setAPIResponse('error', 'Transmission Connect Function - Error: Could not get session ID', 500);
  143. return false;
  144. }
  145. } catch (Requests_Exception $e) {
  146. $this->writeLog('error', 'Transmission Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  147. $this->setAPIResponse('error', $e->getMessage(), 500);
  148. return false;
  149. }
  150. }
  151. public function transmissionHomepagePermissions($key = null)
  152. {
  153. $permissions = [
  154. 'main' => [
  155. 'enabled' => [
  156. 'homepageTransmissionEnabled'
  157. ],
  158. 'auth' => [
  159. 'homepageTransmissionAuth'
  160. ],
  161. 'not_empty' => [
  162. 'transmissionURL'
  163. ]
  164. ]
  165. ];
  166. if (array_key_exists($key, $permissions)) {
  167. return $permissions[$key];
  168. } elseif ($key == 'all') {
  169. return $permissions;
  170. } else {
  171. return [];
  172. }
  173. }
  174. public function homepageOrdertransmission()
  175. {
  176. if ($this->homepageItemPermissions($this->transmissionHomepagePermissions('main'))) {
  177. $loadingBox = ($this->config['transmissionCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  178. $builder = ($this->config['transmissionCombine']) ? 'buildDownloaderCombined(\'transmission\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("transmission"));';
  179. return '
  180. <div id="' . __FUNCTION__ . '">
  181. ' . $loadingBox . '
  182. <script>
  183. // homepageOrdertransmission
  184. ' . $builder . '
  185. homepageDownloader("transmission", "' . $this->config['transmissionRefresh'] . '");
  186. // End homepageOrdertransmission
  187. </script>
  188. </div>
  189. ';
  190. }
  191. }
  192. public function getTransmissionHomepageQueue()
  193. {
  194. if (!$this->homepageItemPermissions($this->transmissionHomepagePermissions('main'), true)) {
  195. return false;
  196. }
  197. $digest = $this->qualifyURL($this->config['transmissionURL'], true);
  198. $passwordInclude = ($this->config['transmissionUsername'] != '' && $this->config['transmissionPassword'] != '') ? $this->config['transmissionUsername'] . ':' . rawurlencode($this->decrypt($this->config['transmissionPassword'])) . "@" : '';
  199. $url = $digest['scheme'] . '://' . $passwordInclude . $digest['host'] . $digest['port'] . $digest['path'] . '/rpc';
  200. try {
  201. $options = $this->requestOptions($this->config['transmissionURL'], $this->config['transmissionDisableCertCheck'], $this->config['transmissionRefresh']);
  202. $response = Requests::get($url, array(), $options);
  203. if ($response->headers['x-transmission-session-id']) {
  204. $headers = array(
  205. 'X-Transmission-Session-Id' => $response->headers['x-transmission-session-id'],
  206. 'Content-Type' => 'application/json'
  207. );
  208. $data = array(
  209. 'method' => 'torrent-get',
  210. 'arguments' => array(
  211. 'fields' => array(
  212. "id", "name", "totalSize", "eta", "isFinished", "isStalled", "percentDone", "rateDownload", "status", "downloadDir", "errorString", "addedDate"
  213. ),
  214. ),
  215. 'tags' => ''
  216. );
  217. $response = Requests::post($url, $headers, json_encode($data), $options);
  218. if ($response->success) {
  219. $torrentList = json_decode($response->body, true)['arguments']['torrents'];
  220. if ($this->config['transmissionHideSeeding'] || $this->config['transmissionHideCompleted']) {
  221. $filter = array();
  222. $torrents = array();
  223. if ($this->config['transmissionHideSeeding']) {
  224. array_push($filter, 6, 5);
  225. }
  226. if ($this->config['transmissionHideCompleted']) {
  227. array_push($filter, 0);
  228. }
  229. foreach ($torrentList as $key => $value) {
  230. if (!in_array($value['status'], $filter)) {
  231. $torrents[] = $value;
  232. }
  233. }
  234. } else {
  235. $torrents = json_decode($response->body, true)['arguments']['torrents'];
  236. }
  237. usort($torrents, function ($a, $b) {
  238. return $a["addedDate"] <=> $b["addedDate"];
  239. });
  240. $api['content']['queueItems'] = $torrents;
  241. $api['content']['historyItems'] = false;
  242. }
  243. } else {
  244. $this->writeLog('error', 'Transmission Connect Function - Error: Could not get session ID', 'SYSTEM');
  245. $this->setAPIResponse('error', 'Transmission Connect Function - Error: Could not get session ID', 500);
  246. return false;
  247. }
  248. } catch (Requests_Exception $e) {
  249. $this->writeLog('error', 'Transmission Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  250. $this->setAPIResponse('error', $e->getMessage(), 500);
  251. return false;
  252. };
  253. $api['content'] = $api['content'] ?? false;
  254. $this->setAPIResponse('success', null, 200, $api);
  255. return $api;
  256. }
  257. }