transmission.php 9.0 KB

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