transmission.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'Enable' => [
  20. $this->settingsOption('enable', 'homepageTransmissionEnabled'),
  21. $this->settingsOption('auth', 'homepageTransmissionAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('url', 'transmissionURL', ['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.']),
  25. $this->settingsOption('blank'),
  26. $this->settingsOption('username', 'transmissionUsername'),
  27. $this->settingsOption('password', 'transmissionPassword'),
  28. $this->settingsOption('disable-cert-check', 'transmissionDisableCertCheck'),
  29. $this->settingsOption('use-custom-certificate', 'transmissionUseCustomCertificate'),
  30. ],
  31. 'Misc Options' => [
  32. $this->settingsOption('hide-seeding', 'transmissionHideSeeding'),
  33. $this->settingsOption('hide-completed', 'transmissionHideCompleted'),
  34. $this->settingsOption('refresh', 'transmissionRefresh'),
  35. $this->settingsOption('combine', 'transmissionCombine'),
  36. ],
  37. 'Test Connection' => [
  38. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  39. $this->settingsOption('test', 'transmission'),
  40. ]
  41. ]
  42. ];
  43. return array_merge($homepageInformation, $homepageSettings);
  44. }
  45. public function testConnectionTransmission()
  46. {
  47. if (empty($this->config['transmissionURL'])) {
  48. $this->setAPIResponse('error', 'Transmission URL is not defined', 422);
  49. return false;
  50. }
  51. $digest = $this->qualifyURL($this->config['transmissionURL'], true);
  52. $passwordInclude = ($this->config['transmissionUsername'] != '' && $this->config['transmissionPassword'] != '') ? $this->config['transmissionUsername'] . ':' . rawurlencode($this->decrypt($this->config['transmissionPassword'])) . "@" : '';
  53. $url = $digest['scheme'] . '://' . $passwordInclude . $digest['host'] . $digest['port'] . $digest['path'] . '/rpc';
  54. try {
  55. $options = $this->requestOptions($this->config['transmissionURL'], $this->config['transmissionRefresh'], $this->config['transmissionDisableCertCheck'], $this->config['transmissionUseCustomCertificate']);
  56. $response = Requests::get($url, [], $options);
  57. if ($response->headers['x-transmission-session-id']) {
  58. $headers = array(
  59. 'X-Transmission-Session-Id' => $response->headers['x-transmission-session-id'],
  60. 'Content-Type' => 'application/json'
  61. );
  62. $data = array(
  63. 'method' => 'torrent-get',
  64. 'arguments' => array(
  65. 'fields' => array(
  66. "id", "name", "totalSize", "eta", "isFinished", "isStalled", "percentDone", "rateDownload", "status", "downloadDir", "errorString"
  67. ),
  68. ),
  69. 'tags' => ''
  70. );
  71. $response = Requests::post($url, $headers, json_encode($data), $options);
  72. if ($response->success) {
  73. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  74. return true;
  75. } else {
  76. $this->setAPIResponse('error', 'Transmission Connect Function - Error: Unknown', 500);
  77. return false;
  78. }
  79. } else {
  80. $this->setLoggerChannel('Transmission')->warning('Could not get session ID');
  81. $this->setAPIResponse('error', 'Transmission Connect Function - Error: Could not get session ID', 500);
  82. return false;
  83. }
  84. } catch (Requests_Exception $e) {
  85. $this->setLoggerChannel('Transmission')->error($e);
  86. $this->setResponse(500, $e->getMessage());
  87. return false;
  88. }
  89. }
  90. public function transmissionHomepagePermissions($key = null)
  91. {
  92. $permissions = [
  93. 'main' => [
  94. 'enabled' => [
  95. 'homepageTransmissionEnabled'
  96. ],
  97. 'auth' => [
  98. 'homepageTransmissionAuth'
  99. ],
  100. 'not_empty' => [
  101. 'transmissionURL'
  102. ]
  103. ]
  104. ];
  105. return $this->homepageCheckKeyPermissions($key, $permissions);
  106. }
  107. public function homepageOrdertransmission()
  108. {
  109. if ($this->homepageItemPermissions($this->transmissionHomepagePermissions('main'))) {
  110. $loadingBox = ($this->config['transmissionCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  111. $builder = ($this->config['transmissionCombine']) ? 'buildDownloaderCombined(\'transmission\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("transmission"));';
  112. return '
  113. <div id="' . __FUNCTION__ . '">
  114. ' . $loadingBox . '
  115. <script>
  116. // homepageOrdertransmission
  117. ' . $builder . '
  118. homepageDownloader("transmission", "' . $this->config['transmissionRefresh'] . '");
  119. // End homepageOrdertransmission
  120. </script>
  121. </div>
  122. ';
  123. }
  124. }
  125. public function getTransmissionHomepageQueue()
  126. {
  127. if (!$this->homepageItemPermissions($this->transmissionHomepagePermissions('main'), true)) {
  128. return false;
  129. }
  130. $digest = $this->qualifyURL($this->config['transmissionURL'], true);
  131. $passwordInclude = ($this->config['transmissionUsername'] != '' && $this->config['transmissionPassword'] != '') ? $this->config['transmissionUsername'] . ':' . rawurlencode($this->decrypt($this->config['transmissionPassword'])) . "@" : '';
  132. $url = $digest['scheme'] . '://' . $passwordInclude . $digest['host'] . $digest['port'] . $digest['path'] . '/rpc';
  133. try {
  134. $options = $this->requestOptions($this->config['transmissionURL'], $this->config['transmissionRefresh'], $this->config['transmissionDisableCertCheck'], $this->config['transmissionUseCustomCertificate']);
  135. $response = Requests::get($url, array(), $options);
  136. if ($response->headers['x-transmission-session-id']) {
  137. $headers = array(
  138. 'X-Transmission-Session-Id' => $response->headers['x-transmission-session-id'],
  139. 'Content-Type' => 'application/json'
  140. );
  141. $data = array(
  142. 'method' => 'torrent-get',
  143. 'arguments' => array(
  144. 'fields' => array(
  145. "id", "name", "totalSize", "eta", "isFinished", "isStalled", "percentDone", "rateDownload", "status", "downloadDir", "errorString", "addedDate"
  146. ),
  147. ),
  148. 'tags' => ''
  149. );
  150. $response = Requests::post($url, $headers, json_encode($data), $options);
  151. if ($response->success) {
  152. $torrentList = json_decode($response->body, true)['arguments']['torrents'];
  153. if ($this->config['transmissionHideSeeding'] || $this->config['transmissionHideCompleted']) {
  154. $filter = array();
  155. $torrents = array();
  156. if ($this->config['transmissionHideSeeding']) {
  157. array_push($filter, 6, 5);
  158. }
  159. if ($this->config['transmissionHideCompleted']) {
  160. array_push($filter, 0);
  161. }
  162. foreach ($torrentList as $key => $value) {
  163. if (!in_array($value['status'], $filter)) {
  164. $torrents[] = $value;
  165. }
  166. }
  167. } else {
  168. $torrents = json_decode($response->body, true)['arguments']['torrents'];
  169. }
  170. usort($torrents, function ($a, $b) {
  171. return $a["addedDate"] <=> $b["addedDate"];
  172. });
  173. $api['content']['queueItems'] = $torrents;
  174. $api['content']['historyItems'] = false;
  175. }
  176. } else {
  177. $this->setLoggerChannel('Transmission')->warning('Could not get session ID');
  178. $this->setAPIResponse('error', 'Transmission Connect Function - Error: Could not get session ID', 500);
  179. return false;
  180. }
  181. } catch (Requests_Exception $e) {
  182. $this->setLoggerChannel('Transmission')->error($e);
  183. $this->setResponse(500, $e->getMessage());
  184. return false;
  185. };
  186. $api['content'] = $api['content'] ?? false;
  187. $this->setAPIResponse('success', null, 200, $api);
  188. return $api;
  189. }
  190. }