transmission.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 getTransmissionHomepageQueue()
  138. {
  139. if (!$this->config['homepageTransmissionEnabled']) {
  140. $this->setAPIResponse('error', 'Transmission homepage item is not enabled', 409);
  141. return false;
  142. }
  143. if (!$this->qualifyRequest($this->config['homepageTransmissionAuth'])) {
  144. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  145. return false;
  146. }
  147. if (empty($this->config['transmissionURL'])) {
  148. $this->setAPIResponse('error', 'Transmission URL is not defined', 422);
  149. return false;
  150. }
  151. $digest = $this->qualifyURL($this->config['transmissionURL'], true);
  152. $passwordInclude = ($this->config['transmissionUsername'] != '' && $this->config['transmissionPassword'] != '') ? $this->config['transmissionUsername'] . ':' . $this->decrypt($this->config['transmissionPassword']) . "@" : '';
  153. $url = $digest['scheme'] . '://' . $passwordInclude . $digest['host'] . $digest['port'] . $digest['path'] . '/rpc';
  154. try {
  155. $options = ($this->localURL($this->config['transmissionURL'])) ? array('verify' => false) : array();
  156. $response = Requests::get($url, array(), $options);
  157. if ($response->headers['x-transmission-session-id']) {
  158. $headers = array(
  159. 'X-Transmission-Session-Id' => $response->headers['x-transmission-session-id'],
  160. 'Content-Type' => 'application/json'
  161. );
  162. $data = array(
  163. 'method' => 'torrent-get',
  164. 'arguments' => array(
  165. 'fields' => array(
  166. "id", "name", "totalSize", "eta", "isFinished", "isStalled", "percentDone", "rateDownload", "status", "downloadDir", "errorString"
  167. ),
  168. ),
  169. 'tags' => ''
  170. );
  171. $response = Requests::post($url, $headers, json_encode($data), $options);
  172. if ($response->success) {
  173. $torrentList = json_decode($response->body, true)['arguments']['torrents'];
  174. if ($this->config['transmissionHideSeeding'] || $this->config['transmissionHideCompleted']) {
  175. $filter = array();
  176. $torrents = array();
  177. if ($this->config['transmissionHideSeeding']) {
  178. array_push($filter, 6, 5);
  179. }
  180. if ($this->config['transmissionHideCompleted']) {
  181. array_push($filter, 0);
  182. }
  183. foreach ($torrentList as $key => $value) {
  184. if (!in_array($value['status'], $filter)) {
  185. $torrents[] = $value;
  186. }
  187. }
  188. } else {
  189. $torrents = json_decode($response->body, true);
  190. }
  191. $api['content']['queueItems'] = $torrents;
  192. $api['content']['historyItems'] = false;
  193. }
  194. } else {
  195. $this->writeLog('error', 'Transmission Connect Function - Error: Could not get session ID', 'SYSTEM');
  196. $this->setAPIResponse('error', 'Transmission Connect Function - Error: Could not get session ID', 500);
  197. return false;
  198. }
  199. } catch (Requests_Exception $e) {
  200. $this->writeLog('error', 'Transmission Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  201. $this->setAPIResponse('error', $e->getMessage(), 500);
  202. return false;
  203. };
  204. $api['content'] = isset($api['content']) ? $api['content'] : false;
  205. $this->setAPIResponse('success', null, 200, $api);
  206. return $api;
  207. }
  208. }