transmission.php 9.2 KB

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