rtorrent.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. trait RTorrentHomepageItem
  3. {
  4. public function rTorrentSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'rTorrent',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/rTorrent.png',
  10. 'category' => 'Downloader',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $xmlStatus = (extension_loaded('xmlrpc')) ? 'Installed' : 'Not Installed';
  17. $homepageSettings = [
  18. 'debug' => true,
  19. 'settings' => [
  20. 'FYI' => [
  21. $this->settingsOption('html', null, ['label' => '', 'override' => 12,
  22. 'html' => '
  23. <div class="row">
  24. <div class="col-lg-12">
  25. <div class="panel panel-info">
  26. <div class="panel-heading">
  27. <span lang="en">ATTENTION</span>
  28. </div>
  29. <div class="panel-wrapper collapse in" aria-expanded="true">
  30. <div class="panel-body">
  31. <h4 lang="en">This module requires XMLRPC</h4>
  32. <span lang="en">Status: [ <b>' . $xmlStatus . '</b> ]</span>
  33. <br/></br>
  34. <span lang="en">
  35. <h4><b>Note about API URL</b></h4>
  36. Organizr appends the url with <code>/RPC2</code> unless the URL ends in <code>.php</code><br/>
  37. <h5>Possible URLs:</h5>
  38. <li>http://localhost:8080</li>
  39. <li>https://domain.site/xmlrpc.php</li>
  40. <li>https://seedbox.site/rutorrent/plugins/httprpc/action.php</li>
  41. </span>
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. </div>
  47. '
  48. ]),
  49. ],
  50. 'Enable' => [
  51. $this->settingsOption('enable', 'homepagerTorrentEnabled'),
  52. $this->settingsOption('auth', 'homepagerTorrentAuth'),
  53. ],
  54. 'Connection' => [
  55. $this->settingsOption('url', 'rTorrentURL'),
  56. $this->settingsOption('input', 'rTorrentURLOverride', ['label' => 'rTorrent API URL Override', 'help' => 'Only use if you cannot connect. Please make sure to use local IP address and port - You also may use local dns name too.', 'placeholder' => 'http(s)://hostname:port/xmlrpc']),
  57. $this->settingsOption('username', 'rTorrentUsername'),
  58. $this->settingsOption('password', 'rTorrentPassword'),
  59. $this->settingsOption('disable-cert-check', 'rTorrentDisableCertCheck'),
  60. $this->settingsOption('use-custom-certificate', 'rTorrentUseCustomCertificate'),
  61. ],
  62. 'Misc Options' => [
  63. $this->settingsOption('hide-seeding', 'rTorrentHideSeeding'),
  64. $this->settingsOption('hide-completed', 'rTorrentHideCompleted'),
  65. $this->settingsOption('select', 'rTorrentSortOrder', ['label' => 'Order', 'options' => $this->rTorrentSortOptions()]),
  66. $this->settingsOption('limit', 'rTorrentLimit'),
  67. $this->settingsOption('multiple', 'rTorrentIgnoreLabel', ['label' => 'Ignore Torrent with Label(s)']),
  68. $this->settingsOption('refresh', 'rTorrentRefresh'),
  69. $this->settingsOption('combine', 'rTorrentCombine'),
  70. ],
  71. 'Test Connection' => [
  72. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  73. $this->settingsOption('test', 'rtorrent'),
  74. ]
  75. ]
  76. ];
  77. return array_merge($homepageInformation, $homepageSettings);
  78. }
  79. public function testConnectionRTorrent()
  80. {
  81. if (empty($this->config['rTorrentURL']) && empty($this->config['rTorrentURLOverride'])) {
  82. $this->setAPIResponse('error', 'rTorrent URL is not defined', 422);
  83. return false;
  84. }
  85. try {
  86. $url = $this->rTorrentURL();
  87. $options = $this->requestOptions($url, null, $this->config['rTorrentDisableCertCheck'], $this->config['rTorrentUseCustomCertificate']);
  88. if ($this->config['rTorrentUsername'] !== '' && $this->decrypt($this->config['rTorrentPassword']) !== '') {
  89. $credentials = array('auth' => new Requests_Auth_Digest(array($this->config['rTorrentUsername'], $this->decrypt($this->config['rTorrentPassword']))));
  90. $options = array_merge($options, $credentials);
  91. }
  92. $data = xmlrpc_encode_request("system.listMethods", null);
  93. $response = Requests::post($url, [], $data, $options);
  94. if ($response->success) {
  95. $methods = xmlrpc_decode(str_replace('i8>', 'i4>', $response->body));
  96. if (count($methods) !== 0) {
  97. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  98. return true;
  99. }
  100. }
  101. $this->setAPIResponse('error', 'rTorrent error occurred', 500);
  102. return false;
  103. } catch
  104. (Requests_Exception $e) {
  105. $this->setLoggerChannel('rTorrent')->error($e);
  106. $this->setResponse(500, $e->getMessage());
  107. return false;
  108. }
  109. }
  110. public function rTorrentHomepagePermissions($key = null)
  111. {
  112. $permissions = [
  113. 'main' => [
  114. 'enabled' => [
  115. 'homepagerTorrentEnabled'
  116. ],
  117. 'auth' => [
  118. 'homepagerTorrentAuth'
  119. ],
  120. 'not_empty' => []
  121. ]
  122. ];
  123. return $this->homepageCheckKeyPermissions($key, $permissions);
  124. }
  125. public function homepageOrderrTorrent()
  126. {
  127. if ($this->homepageItemPermissions($this->rTorrentHomepagePermissions('main'))) {
  128. $loadingBox = ($this->config['rTorrentCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  129. $builder = ($this->config['rTorrentCombine']) ? 'buildDownloaderCombined(\'rTorrent\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("rTorrent"));';
  130. return '
  131. <div id="' . __FUNCTION__ . '">
  132. ' . $loadingBox . '
  133. <script>
  134. // homepageOrderrTorrent
  135. ' . $builder . '
  136. homepageDownloader("rTorrent", "' . $this->config['rTorrentRefresh'] . '");
  137. // End homepageOrderrTorrent
  138. </script>
  139. </div>
  140. ';
  141. }
  142. }
  143. public function checkOverrideURL($url, $override)
  144. {
  145. if (strpos($override, $url) !== false) {
  146. return $override;
  147. } else {
  148. return $url . $override;
  149. }
  150. }
  151. public function rTorrentStatus($completed, $state, $status)
  152. {
  153. if ($completed && $state && $status == 'seed') {
  154. $state = 'Seeding';
  155. } elseif (!$completed && !$state && $status == 'leech') {
  156. $state = 'Stopped';
  157. } elseif (!$completed && $state && $status == 'leech') {
  158. $state = 'Downloading';
  159. } elseif ($completed && !$state && $status == 'seed') {
  160. $state = 'Finished';
  161. } elseif ($completed && !$state && $status == 'leech') {
  162. $state = 'Finished';
  163. }
  164. return ($state) ?: $status;
  165. }
  166. public function getRTorrentHomepageQueue()
  167. {
  168. if (empty($this->config['rTorrentURL']) && empty($this->config['rTorrentURLOverride'])) {
  169. $this->setAPIResponse('error', 'rTorrent URL is not defined', 422);
  170. return false;
  171. }
  172. if (!$this->homepageItemPermissions($this->rTorrentHomepagePermissions('main'), true)) {
  173. return false;
  174. }
  175. try {
  176. if ($this->config['rTorrentLimit'] == '0') {
  177. $this->config['rTorrentLimit'] = '1000';
  178. }
  179. $torrents = array();
  180. $url = $this->rTorrentURL();
  181. $options = $this->requestOptions($url, $this->config['rTorrentRefresh'], $this->config['rTorrentDisableCertCheck'], $this->config['rTorrentUseCustomCertificate']);
  182. if ($this->config['rTorrentUsername'] !== '' && $this->decrypt($this->config['rTorrentPassword']) !== '') {
  183. $credentials = array('auth' => new Requests_Auth_Digest(array($this->config['rTorrentUsername'], $this->decrypt($this->config['rTorrentPassword']))));
  184. $options = array_merge($options, $credentials);
  185. }
  186. $data = xmlrpc_encode_request("d.multicall2", array(
  187. "",
  188. "main",
  189. "d.name=",
  190. "d.base_path=",
  191. "d.up.total=",
  192. "d.size_bytes=",
  193. "d.down.total=",
  194. "d.completed_bytes=",
  195. "d.connection_current=",
  196. "d.down.rate=",
  197. "d.up.rate=",
  198. "d.timestamp.started=",
  199. "d.state=",
  200. "d.group.name=",
  201. "d.hash=",
  202. "d.complete=",
  203. "d.ratio=",
  204. "d.chunk_size=",
  205. "f.size_bytes=",
  206. "f.size_chunks=",
  207. "f.completed_chunks=",
  208. "d.custom=",
  209. "d.custom1=",
  210. "d.custom2=",
  211. "d.custom3=",
  212. "d.custom4=",
  213. "d.custom5=",
  214. ), array());
  215. $response = Requests::post($url, [], $data, $options);
  216. if ($response->success) {
  217. $torrentList = xmlrpc_decode(str_replace('i8>', 'string>', $response->body));
  218. if (is_array($torrentList)) {
  219. foreach ($torrentList as $key => $value) {
  220. $tempStatus = $this->rTorrentStatus($value[13], $value[10], $value[6]);
  221. if ($tempStatus == 'Seeding' && $this->config['rTorrentHideSeeding']) {
  222. //do nothing
  223. } elseif ($tempStatus == 'Finished' && $this->config['rTorrentHideCompleted']) {
  224. //do nothing
  225. } elseif (stripos($this->config['rTorrentIgnoreLabel'], $value[20]) !== false) {
  226. //do nothing
  227. } else {
  228. $torrents[$key] = array(
  229. 'name' => $value[0],
  230. 'base' => $value[1],
  231. 'upTotal' => $value[2],
  232. 'size' => $value[3],
  233. 'downTotal' => $value[4],
  234. 'downloaded' => $value[5],
  235. 'connectionState' => $value[6],
  236. 'leech' => $value[7],
  237. 'seed' => $value[8],
  238. 'date' => $value[9],
  239. 'state' => ($value[10]) ? 'on' : 'off',
  240. 'group' => $value[11],
  241. 'hash' => $value[12],
  242. 'complete' => ($value[13]) ? 'yes' : 'no',
  243. 'ratio' => $value[14],
  244. 'label' => $value[20],
  245. 'status' => $tempStatus,
  246. 'temp' => $value[16] . ' - ' . $value[17] . ' - ' . $value[18],
  247. 'custom' => $value[19] . ' - ' . $value[20] . ' - ' . $value[21],
  248. 'custom2' => $value[22] . ' - ' . $value[23] . ' - ' . $value[24],
  249. );
  250. }
  251. }
  252. }
  253. if (count($torrents) !== 0) {
  254. usort($torrents, function ($a, $b) {
  255. $direction = substr($this->config['rTorrentSortOrder'], -1);
  256. $sort = substr($this->config['rTorrentSortOrder'], 0, strlen($this->config['rTorrentSortOrder']) - 1);
  257. switch ($direction) {
  258. case 'a':
  259. return $a[$sort] <=> $b[$sort];
  260. case 'd':
  261. return $b[$sort] <=> $a[$sort];
  262. default:
  263. return $b['date'] <=> $a['date'];
  264. }
  265. });
  266. $torrents = array_slice($torrents, 0, $this->config['rTorrentLimit']);
  267. }
  268. $api['content']['queueItems'] = $torrents;
  269. $api['content']['historyItems'] = false;
  270. }
  271. } catch
  272. (Requests_Exception $e) {
  273. $this->setLoggerChannel('rTorrent')->error($e);
  274. $this->setResponse(500, $e->getMessage());
  275. return false;
  276. };
  277. $api['content'] = $api['content'] ?? false;
  278. $this->setAPIResponse('success', null, 200, $api);
  279. return $api;
  280. }
  281. /**
  282. * @return string
  283. */
  284. public function rTorrentURL(): string
  285. {
  286. $digest = (empty($this->config['rTorrentURLOverride'])) ? $this->qualifyURL($this->config['rTorrentURL'], true) : $this->qualifyURL($this->checkOverrideURL($this->config['rTorrentURL'], $this->config['rTorrentURLOverride']), true);
  287. $extraPath = (strpos($this->config['rTorrentURL'], '.php') !== false) ? '' : '/RPC2';
  288. $extraPath = (empty($this->config['rTorrentURLOverride'])) ? $extraPath : '';
  289. return $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $extraPath;
  290. }
  291. }