rtorrent.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. if (array_key_exists($key, $permissions)) {
  126. return $permissions[$key];
  127. } elseif ($key == 'all') {
  128. return $permissions;
  129. } else {
  130. return [];
  131. }
  132. }
  133. public function homepageOrderrTorrent()
  134. {
  135. if ($this->homepageItemPermissions($this->rTorrentHomepagePermissions('main'))) {
  136. $loadingBox = ($this->config['rTorrentCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  137. $builder = ($this->config['rTorrentCombine']) ? 'buildDownloaderCombined(\'rTorrent\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("rTorrent"));';
  138. return '
  139. <div id="' . __FUNCTION__ . '">
  140. ' . $loadingBox . '
  141. <script>
  142. // homepageOrderrTorrent
  143. ' . $builder . '
  144. homepageDownloader("rTorrent", "' . $this->config['rTorrentRefresh'] . '");
  145. // End homepageOrderrTorrent
  146. </script>
  147. </div>
  148. ';
  149. }
  150. }
  151. public function checkOverrideURL($url, $override)
  152. {
  153. if (strpos($override, $url) !== false) {
  154. return $override;
  155. } else {
  156. return $url . $override;
  157. }
  158. }
  159. public function rTorrentStatus($completed, $state, $status)
  160. {
  161. if ($completed && $state && $status == 'seed') {
  162. $state = 'Seeding';
  163. } elseif (!$completed && !$state && $status == 'leech') {
  164. $state = 'Stopped';
  165. } elseif (!$completed && $state && $status == 'leech') {
  166. $state = 'Downloading';
  167. } elseif ($completed && !$state && $status == 'seed') {
  168. $state = 'Finished';
  169. }
  170. return ($state) ? $state : $status;
  171. }
  172. public function getRTorrentHomepageQueue()
  173. {
  174. if (empty($this->config['rTorrentURL']) && empty($this->config['rTorrentURLOverride'])) {
  175. $this->setAPIResponse('error', 'rTorrent URL is not defined', 422);
  176. return false;
  177. }
  178. if (!$this->homepageItemPermissions($this->rTorrentHomepagePermissions('main'), true)) {
  179. return false;
  180. }
  181. try {
  182. if ($this->config['rTorrentLimit'] == '0') {
  183. $this->config['rTorrentLimit'] = '1000';
  184. }
  185. $torrents = array();
  186. $digest = (empty($this->config['rTorrentURLOverride'])) ? $this->qualifyURL($this->config['rTorrentURL'], true) : $this->qualifyURL($this->checkOverrideURL($this->config['rTorrentURL'], $this->config['rTorrentURLOverride']), true);
  187. $extraPath = (strpos($this->config['rTorrentURL'], '.php') !== false) ? '' : '/RPC2';
  188. $extraPath = (empty($this->config['rTorrentURLOverride'])) ? $extraPath : '';
  189. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $extraPath;
  190. $options = $this->requestOptions($url, $this->config['rTorrentRefresh'], $this->config['rTorrentDisableCertCheck'], $this->config['rtorrentUseCustomCertificate']);
  191. if ($this->config['rTorrentUsername'] !== '' && $this->decrypt($this->config['rTorrentPassword']) !== '') {
  192. $credentials = array('auth' => new Requests_Auth_Digest(array($this->config['rTorrentUsername'], $this->decrypt($this->config['rTorrentPassword']))));
  193. $options = array_merge($options, $credentials);
  194. }
  195. $data = xmlrpc_encode_request("d.multicall2", array(
  196. "",
  197. "main",
  198. "d.name=",
  199. "d.base_path=",
  200. "d.up.total=",
  201. "d.size_bytes=",
  202. "d.down.total=",
  203. "d.completed_bytes=",
  204. "d.connection_current=",
  205. "d.down.rate=",
  206. "d.up.rate=",
  207. "d.timestamp.started=",
  208. "d.state=",
  209. "d.group.name=",
  210. "d.hash=",
  211. "d.complete=",
  212. "d.ratio=",
  213. "d.chunk_size=",
  214. "f.size_bytes=",
  215. "f.size_chunks=",
  216. "f.completed_chunks=",
  217. "d.custom=",
  218. "d.custom1=",
  219. "d.custom2=",
  220. "d.custom3=",
  221. "d.custom4=",
  222. "d.custom5=",
  223. ), array());
  224. $response = Requests::post($url, [], $data, $options);
  225. if ($response->success) {
  226. $torrentList = xmlrpc_decode(str_replace('i8>', 'string>', $response->body));
  227. if (is_array($torrentList)) {
  228. foreach ($torrentList as $key => $value) {
  229. $tempStatus = $this->rTorrentStatus($value[13], $value[10], $value[6]);
  230. if ($tempStatus == 'Seeding' && $this->config['rTorrentHideSeeding']) {
  231. //do nothing
  232. } elseif ($tempStatus == 'Finished' && $this->config['rTorrentHideCompleted']) {
  233. //do nothing
  234. } else {
  235. $torrents[$key] = array(
  236. 'name' => $value[0],
  237. 'base' => $value[1],
  238. 'upTotal' => $value[2],
  239. 'size' => $value[3],
  240. 'downTotal' => $value[4],
  241. 'downloaded' => $value[5],
  242. 'connectionState' => $value[6],
  243. 'leech' => $value[7],
  244. 'seed' => $value[8],
  245. 'date' => $value[9],
  246. 'state' => ($value[10]) ? 'on' : 'off',
  247. 'group' => $value[11],
  248. 'hash' => $value[12],
  249. 'complete' => ($value[13]) ? 'yes' : 'no',
  250. 'ratio' => $value[14],
  251. 'label' => $value[20],
  252. 'status' => $tempStatus,
  253. 'temp' => $value[16] . ' - ' . $value[17] . ' - ' . $value[18],
  254. 'custom' => $value[19] . ' - ' . $value[20] . ' - ' . $value[21],
  255. 'custom2' => $value[22] . ' - ' . $value[23] . ' - ' . $value[24],
  256. );
  257. }
  258. }
  259. }
  260. if (count($torrents) !== 0) {
  261. usort($torrents, function ($a, $b) {
  262. $direction = substr($this->config['rTorrentSortOrder'], -1);
  263. $sort = substr($this->config['rTorrentSortOrder'], 0, strlen($this->config['rTorrentSortOrder']) - 1);
  264. switch ($direction) {
  265. case 'a':
  266. return $a[$sort] <=> $b[$sort];
  267. break;
  268. case 'd':
  269. return $b[$sort] <=> $a[$sort];
  270. break;
  271. default:
  272. return $b['date'] <=> $a['date'];
  273. }
  274. });
  275. $torrents = array_slice($torrents, 0, $this->config['rTorrentLimit']);
  276. }
  277. $api['content']['queueItems'] = $torrents;
  278. $api['content']['historyItems'] = false;
  279. }
  280. } catch
  281. (Requests_Exception $e) {
  282. $this->writeLog('error', 'rTorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  283. $this->setAPIResponse('error', $e->getMessage(), 500);
  284. return false;
  285. };
  286. $api['content'] = isset($api['content']) ? $api['content'] : false;
  287. $this->setAPIResponse('success', null, 200, $api);
  288. return $api;
  289. }
  290. }