rtorrent.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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('refresh', 'rTorrentRefresh'),
  68. $this->settingsOption('combine', 'rTorrentCombine'),
  69. ],
  70. 'Test Connection' => [
  71. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  72. $this->settingsOption('test', 'rtorrent'),
  73. ]
  74. ]
  75. ];
  76. return array_merge($homepageInformation, $homepageSettings);
  77. }
  78. public function testConnectionRTorrent()
  79. {
  80. if (empty($this->config['rTorrentURL']) && empty($this->config['rTorrentURLOverride'])) {
  81. $this->setAPIResponse('error', 'rTorrent URL is not defined', 422);
  82. return false;
  83. }
  84. try {
  85. $digest = (empty($this->config['rTorrentURLOverride'])) ? $this->qualifyURL($this->config['rTorrentURL'], true) : $this->qualifyURL($this->checkOverrideURL($this->config['rTorrentURL'], $this->config['rTorrentURLOverride']), true);
  86. $extraPath = (strpos($this->config['rTorrentURL'], '.php') !== false) ? '' : '/RPC2';
  87. $extraPath = (empty($this->config['rTorrentURLOverride'])) ? $extraPath : '';
  88. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $extraPath;
  89. $options = $this->requestOptions($url, null, $this->config['rTorrentDisableCertCheck'], $this->config['rtorrentUseCustomCertificate']);
  90. if ($this->config['rTorrentUsername'] !== '' && $this->decrypt($this->config['rTorrentPassword']) !== '') {
  91. $credentials = array('auth' => new Requests_Auth_Digest(array($this->config['rTorrentUsername'], $this->decrypt($this->config['rTorrentPassword']))));
  92. $options = array_merge($options, $credentials);
  93. }
  94. $data = xmlrpc_encode_request("system.listMethods", null);
  95. $response = Requests::post($url, [], $data, $options);
  96. if ($response->success) {
  97. $methods = xmlrpc_decode(str_replace('i8>', 'i4>', $response->body));
  98. if (count($methods) !== 0) {
  99. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  100. return true;
  101. }
  102. }
  103. $this->setAPIResponse('error', 'rTorrent error occurred', 500);
  104. return false;
  105. } catch
  106. (Requests_Exception $e) {
  107. $this->writeLog('error', 'rTorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  108. $this->setAPIResponse('error', $e->getMessage(), 500);
  109. return false;
  110. }
  111. }
  112. public function rTorrentHomepagePermissions($key = null)
  113. {
  114. $permissions = [
  115. 'main' => [
  116. 'enabled' => [
  117. 'homepagerTorrentEnabled'
  118. ],
  119. 'auth' => [
  120. 'homepagerTorrentAuth'
  121. ],
  122. 'not_empty' => []
  123. ]
  124. ];
  125. return $this->homepageCheckKeyPermissions($key, $permissions);
  126. }
  127. public function homepageOrderrTorrent()
  128. {
  129. if ($this->homepageItemPermissions($this->rTorrentHomepagePermissions('main'))) {
  130. $loadingBox = ($this->config['rTorrentCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  131. $builder = ($this->config['rTorrentCombine']) ? 'buildDownloaderCombined(\'rTorrent\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("rTorrent"));';
  132. return '
  133. <div id="' . __FUNCTION__ . '">
  134. ' . $loadingBox . '
  135. <script>
  136. // homepageOrderrTorrent
  137. ' . $builder . '
  138. homepageDownloader("rTorrent", "' . $this->config['rTorrentRefresh'] . '");
  139. // End homepageOrderrTorrent
  140. </script>
  141. </div>
  142. ';
  143. }
  144. }
  145. public function checkOverrideURL($url, $override)
  146. {
  147. if (strpos($override, $url) !== false) {
  148. return $override;
  149. } else {
  150. return $url . $override;
  151. }
  152. }
  153. public function rTorrentStatus($completed, $state, $status)
  154. {
  155. if ($completed && $state && $status == 'seed') {
  156. $state = 'Seeding';
  157. } elseif (!$completed && !$state && $status == 'leech') {
  158. $state = 'Stopped';
  159. } elseif (!$completed && $state && $status == 'leech') {
  160. $state = 'Downloading';
  161. } elseif ($completed && !$state && $status == 'seed') {
  162. $state = 'Finished';
  163. }
  164. return ($state) ? $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. $digest = (empty($this->config['rTorrentURLOverride'])) ? $this->qualifyURL($this->config['rTorrentURL'], true) : $this->qualifyURL($this->checkOverrideURL($this->config['rTorrentURL'], $this->config['rTorrentURLOverride']), true);
  181. $extraPath = (strpos($this->config['rTorrentURL'], '.php') !== false) ? '' : '/RPC2';
  182. $extraPath = (empty($this->config['rTorrentURLOverride'])) ? $extraPath : '';
  183. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $extraPath;
  184. $options = $this->requestOptions($url, $this->config['rTorrentRefresh'], $this->config['rTorrentDisableCertCheck'], $this->config['rTorrentUseCustomCertificate']);
  185. if ($this->config['rTorrentUsername'] !== '' && $this->decrypt($this->config['rTorrentPassword']) !== '') {
  186. $credentials = array('auth' => new Requests_Auth_Digest(array($this->config['rTorrentUsername'], $this->decrypt($this->config['rTorrentPassword']))));
  187. $options = array_merge($options, $credentials);
  188. }
  189. $data = xmlrpc_encode_request("d.multicall2", array(
  190. "",
  191. "main",
  192. "d.name=",
  193. "d.base_path=",
  194. "d.up.total=",
  195. "d.size_bytes=",
  196. "d.down.total=",
  197. "d.completed_bytes=",
  198. "d.connection_current=",
  199. "d.down.rate=",
  200. "d.up.rate=",
  201. "d.timestamp.started=",
  202. "d.state=",
  203. "d.group.name=",
  204. "d.hash=",
  205. "d.complete=",
  206. "d.ratio=",
  207. "d.chunk_size=",
  208. "f.size_bytes=",
  209. "f.size_chunks=",
  210. "f.completed_chunks=",
  211. "d.custom=",
  212. "d.custom1=",
  213. "d.custom2=",
  214. "d.custom3=",
  215. "d.custom4=",
  216. "d.custom5=",
  217. ), array());
  218. $response = Requests::post($url, [], $data, $options);
  219. if ($response->success) {
  220. $torrentList = xmlrpc_decode(str_replace('i8>', 'string>', $response->body));
  221. if (is_array($torrentList)) {
  222. foreach ($torrentList as $key => $value) {
  223. $tempStatus = $this->rTorrentStatus($value[13], $value[10], $value[6]);
  224. if ($tempStatus == 'Seeding' && $this->config['rTorrentHideSeeding']) {
  225. //do nothing
  226. } elseif ($tempStatus == 'Finished' && $this->config['rTorrentHideCompleted']) {
  227. //do nothing
  228. } else {
  229. $torrents[$key] = array(
  230. 'name' => $value[0],
  231. 'base' => $value[1],
  232. 'upTotal' => $value[2],
  233. 'size' => $value[3],
  234. 'downTotal' => $value[4],
  235. 'downloaded' => $value[5],
  236. 'connectionState' => $value[6],
  237. 'leech' => $value[7],
  238. 'seed' => $value[8],
  239. 'date' => $value[9],
  240. 'state' => ($value[10]) ? 'on' : 'off',
  241. 'group' => $value[11],
  242. 'hash' => $value[12],
  243. 'complete' => ($value[13]) ? 'yes' : 'no',
  244. 'ratio' => $value[14],
  245. 'label' => $value[20],
  246. 'status' => $tempStatus,
  247. 'temp' => $value[16] . ' - ' . $value[17] . ' - ' . $value[18],
  248. 'custom' => $value[19] . ' - ' . $value[20] . ' - ' . $value[21],
  249. 'custom2' => $value[22] . ' - ' . $value[23] . ' - ' . $value[24],
  250. );
  251. }
  252. }
  253. }
  254. if (count($torrents) !== 0) {
  255. usort($torrents, function ($a, $b) {
  256. $direction = substr($this->config['rTorrentSortOrder'], -1);
  257. $sort = substr($this->config['rTorrentSortOrder'], 0, strlen($this->config['rTorrentSortOrder']) - 1);
  258. switch ($direction) {
  259. case 'a':
  260. return $a[$sort] <=> $b[$sort];
  261. break;
  262. case 'd':
  263. return $b[$sort] <=> $a[$sort];
  264. break;
  265. default:
  266. return $b['date'] <=> $a['date'];
  267. }
  268. });
  269. $torrents = array_slice($torrents, 0, $this->config['rTorrentLimit']);
  270. }
  271. $api['content']['queueItems'] = $torrents;
  272. $api['content']['historyItems'] = false;
  273. }
  274. } catch
  275. (Requests_Exception $e) {
  276. $this->writeLog('error', 'rTorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  277. $this->setAPIResponse('error', $e->getMessage(), 500);
  278. return false;
  279. };
  280. $api['content'] = isset($api['content']) ? $api['content'] : false;
  281. $this->setAPIResponse('success', null, 200, $api);
  282. return $api;
  283. }
  284. }