transmission.php 8.7 KB

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