rtorrent.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 = array(
  18. 'settings' => array(
  19. 'FYI' => array(
  20. array(
  21. 'type' => 'html',
  22. 'label' => '',
  23. 'override' => 12,
  24. 'html' => '
  25. <div class="row">
  26. <div class="col-lg-12">
  27. <div class="panel panel-info">
  28. <div class="panel-heading">
  29. <span lang="en">ATTENTION</span>
  30. </div>
  31. <div class="panel-wrapper collapse in" aria-expanded="true">
  32. <div class="panel-body">
  33. <h4 lang="en">This module requires XMLRPC</h4>
  34. <span lang="en">Status: [ <b>' . $xmlStatus . '</b> ]</span>
  35. <br/></br>
  36. <span lang="en">
  37. <h4><b>Note about API URL</b></h4>
  38. Organizr appends the url with <code>/RPC2</code> unless the URL ends in <code>.php</code><br/>
  39. <h5>Possible URLs:</h5>
  40. <li>http://localhost:8080</li>
  41. <li>https://domain.site/xmlrpc.php</li>
  42. <li>https://seedbox.site/rutorrent/plugins/httprpc/action.php</li>
  43. </span>
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. </div>
  49. '
  50. )
  51. ),
  52. 'Enable' => array(
  53. array(
  54. 'type' => 'switch',
  55. 'name' => 'homepagerTorrentEnabled',
  56. 'label' => 'Enable',
  57. 'value' => $this->config['homepagerTorrentEnabled']
  58. ),
  59. array(
  60. 'type' => 'select',
  61. 'name' => 'homepagerTorrentAuth',
  62. 'label' => 'Minimum Authentication',
  63. 'value' => $this->config['homepagerTorrentAuth'],
  64. 'options' => $this->groupOptions
  65. )
  66. ),
  67. 'Connection' => array(
  68. array(
  69. 'type' => 'input',
  70. 'name' => 'rTorrentURL',
  71. 'label' => 'URL',
  72. 'value' => $this->config['rTorrentURL'],
  73. 'help' => 'Please make sure to use local IP address and port - You also may use local dns name too.',
  74. 'placeholder' => 'http(s)://hostname:port'
  75. ),
  76. array(
  77. 'type' => 'input',
  78. 'name' => 'rTorrentURLOverride',
  79. 'label' => 'rTorrent API URL Override',
  80. 'value' => $this->config['rTorrentURLOverride'],
  81. '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.',
  82. 'placeholder' => 'http(s)://hostname:port/xmlrpc'
  83. ),
  84. array(
  85. 'type' => 'input',
  86. 'name' => 'rTorrentUsername',
  87. 'label' => 'Username',
  88. 'value' => $this->config['rTorrentUsername']
  89. ),
  90. array(
  91. 'type' => 'password',
  92. 'name' => 'rTorrentPassword',
  93. 'label' => 'Password',
  94. 'value' => $this->config['rTorrentPassword']
  95. ),
  96. array(
  97. 'type' => 'switch',
  98. 'name' => 'rTorrentDisableCertCheck',
  99. 'label' => 'Disable Certificate Check',
  100. 'value' => $this->config['rTorrentDisableCertCheck']
  101. ),
  102. ),
  103. 'Misc Options' => array(
  104. array(
  105. 'type' => 'switch',
  106. 'name' => 'rTorrentHideSeeding',
  107. 'label' => 'Hide Seeding',
  108. 'value' => $this->config['rTorrentHideSeeding']
  109. ), array(
  110. 'type' => 'switch',
  111. 'name' => 'rTorrentHideCompleted',
  112. 'label' => 'Hide Completed',
  113. 'value' => $this->config['rTorrentHideCompleted']
  114. ),
  115. array(
  116. 'type' => 'select',
  117. 'name' => 'rTorrentSortOrder',
  118. 'label' => 'Order',
  119. 'value' => $this->config['rTorrentSortOrder'],
  120. 'options' => $this->rTorrentSortOptions()
  121. ),
  122. array(
  123. 'type' => 'select',
  124. 'name' => 'rTorrentRefresh',
  125. 'label' => 'Refresh Seconds',
  126. 'value' => $this->config['rTorrentRefresh'],
  127. 'options' => $this->timeOptions()
  128. ),
  129. array(
  130. 'type' => 'number',
  131. 'name' => 'rTorrentLimit',
  132. 'label' => 'Item Limit',
  133. 'value' => $this->config['rTorrentLimit'],
  134. ),
  135. array(
  136. 'type' => 'switch',
  137. 'name' => 'rTorrentCombine',
  138. 'label' => 'Add to Combined Downloader',
  139. 'value' => $this->config['rTorrentCombine']
  140. ),
  141. ),
  142. 'Test Connection' => array(
  143. array(
  144. 'type' => 'blank',
  145. 'label' => 'Please Save before Testing'
  146. ),
  147. array(
  148. 'type' => 'button',
  149. 'label' => '',
  150. 'icon' => 'fa fa-flask',
  151. 'class' => 'pull-right',
  152. 'text' => 'Test Connection',
  153. 'attr' => 'onclick="testAPIConnection(\'rtorrent\')"'
  154. ),
  155. )
  156. )
  157. );
  158. return array_merge($homepageInformation, $homepageSettings);
  159. }
  160. public function testConnectionRTorrent()
  161. {
  162. if (empty($this->config['rTorrentURL']) && empty($this->config['rTorrentURLOverride'])) {
  163. $this->setAPIResponse('error', 'rTorrent URL is not defined', 422);
  164. return false;
  165. }
  166. try {
  167. $digest = (empty($this->config['rTorrentURLOverride'])) ? $this->qualifyURL($this->config['rTorrentURL'], true) : $this->qualifyURL($this->checkOverrideURL($this->config['rTorrentURL'], $this->config['rTorrentURLOverride']), true);
  168. $extraPath = (strpos($this->config['rTorrentURL'], '.php') !== false) ? '' : '/RPC2';
  169. $extraPath = (empty($this->config['rTorrentURLOverride'])) ? $extraPath : '';
  170. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $extraPath;
  171. $options = ($this->localURL($url, $this->config['rTorrentDisableCertCheck'])) ? array('verify' => false) : array();
  172. if ($this->config['rTorrentUsername'] !== '' && $this->decrypt($this->config['rTorrentPassword']) !== '') {
  173. $credentials = array('auth' => new Requests_Auth_Digest(array($this->config['rTorrentUsername'], $this->decrypt($this->config['rTorrentPassword']))));
  174. $options = array_merge($options, $credentials);
  175. }
  176. $data = xmlrpc_encode_request("system.listMethods", null);
  177. $response = Requests::post($url, array(), $data, $options);
  178. if ($response->success) {
  179. $methods = xmlrpc_decode(str_replace('i8>', 'i4>', $response->body));
  180. if (count($methods) !== 0) {
  181. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  182. return true;
  183. }
  184. }
  185. $this->setAPIResponse('error', 'rTorrent error occurred', 500);
  186. return false;
  187. } catch
  188. (Requests_Exception $e) {
  189. $this->writeLog('error', 'rTorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  190. $this->setAPIResponse('error', $e->getMessage(), 500);
  191. return false;
  192. }
  193. }
  194. public function rTorrentHomepagePermissions($key = null)
  195. {
  196. $permissions = [
  197. 'main' => [
  198. 'enabled' => [
  199. 'homepagerTorrentEnabled'
  200. ],
  201. 'auth' => [
  202. 'homepagerTorrentAuth'
  203. ],
  204. 'not_empty' => []
  205. ]
  206. ];
  207. if (array_key_exists($key, $permissions)) {
  208. return $permissions[$key];
  209. } elseif ($key == 'all') {
  210. return $permissions;
  211. } else {
  212. return [];
  213. }
  214. }
  215. public function homepageOrderrTorrent()
  216. {
  217. if ($this->homepageItemPermissions($this->rTorrentHomepagePermissions('main'))) {
  218. $loadingBox = ($this->config['rTorrentCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  219. $builder = ($this->config['rTorrentCombine']) ? 'buildDownloaderCombined(\'rTorrent\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("rTorrent"));';
  220. return '
  221. <div id="' . __FUNCTION__ . '">
  222. ' . $loadingBox . '
  223. <script>
  224. // homepageOrderrTorrent
  225. ' . $builder . '
  226. homepageDownloader("rTorrent", "' . $this->config['rTorrentRefresh'] . '");
  227. // End homepageOrderrTorrent
  228. </script>
  229. </div>
  230. ';
  231. }
  232. }
  233. public function checkOverrideURL($url, $override)
  234. {
  235. if (strpos($override, $url) !== false) {
  236. return $override;
  237. } else {
  238. return $url . $override;
  239. }
  240. }
  241. public function rTorrentStatus($completed, $state, $status)
  242. {
  243. if ($completed && $state && $status == 'seed') {
  244. $state = 'Seeding';
  245. } elseif (!$completed && !$state && $status == 'leech') {
  246. $state = 'Stopped';
  247. } elseif (!$completed && $state && $status == 'leech') {
  248. $state = 'Downloading';
  249. } elseif ($completed && !$state && $status == 'seed') {
  250. $state = 'Finished';
  251. }
  252. return ($state) ? $state : $status;
  253. }
  254. public function getRTorrentHomepageQueue()
  255. {
  256. if (empty($this->config['rTorrentURL']) && empty($this->config['rTorrentURLOverride'])) {
  257. $this->setAPIResponse('error', 'rTorrent URL is not defined', 422);
  258. return false;
  259. }
  260. if (!$this->homepageItemPermissions($this->rTorrentHomepagePermissions('main'), true)) {
  261. return false;
  262. }
  263. try {
  264. if ($this->config['rTorrentLimit'] == '0') {
  265. $this->config['rTorrentLimit'] = '1000';
  266. }
  267. $torrents = array();
  268. $digest = (empty($this->config['rTorrentURLOverride'])) ? $this->qualifyURL($this->config['rTorrentURL'], true) : $this->qualifyURL($this->checkOverrideURL($this->config['rTorrentURL'], $this->config['rTorrentURLOverride']), true);
  269. $extraPath = (strpos($this->config['rTorrentURL'], '.php') !== false) ? '' : '/RPC2';
  270. $extraPath = (empty($this->config['rTorrentURLOverride'])) ? $extraPath : '';
  271. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $extraPath;
  272. $options = ($this->localURL($url, $this->config['rTorrentDisableCertCheck'])) ? array('verify' => false) : array();
  273. if ($this->config['rTorrentUsername'] !== '' && $this->decrypt($this->config['rTorrentPassword']) !== '') {
  274. $credentials = array('auth' => new Requests_Auth_Digest(array($this->config['rTorrentUsername'], $this->decrypt($this->config['rTorrentPassword']))));
  275. $options = array_merge($options, $credentials);
  276. }
  277. $data = xmlrpc_encode_request("d.multicall2", array(
  278. "",
  279. "main",
  280. "d.name=",
  281. "d.base_path=",
  282. "d.up.total=",
  283. "d.size_bytes=",
  284. "d.down.total=",
  285. "d.completed_bytes=",
  286. "d.connection_current=",
  287. "d.down.rate=",
  288. "d.up.rate=",
  289. "d.timestamp.started=",
  290. "d.state=",
  291. "d.group.name=",
  292. "d.hash=",
  293. "d.complete=",
  294. "d.ratio=",
  295. "d.chunk_size=",
  296. "f.size_bytes=",
  297. "f.size_chunks=",
  298. "f.completed_chunks=",
  299. "d.custom=",
  300. "d.custom1=",
  301. "d.custom2=",
  302. "d.custom3=",
  303. "d.custom4=",
  304. "d.custom5=",
  305. ), array());
  306. $response = Requests::post($url, array(), $data, $options);
  307. if ($response->success) {
  308. $torrentList = xmlrpc_decode(str_replace('i8>', 'string>', $response->body));
  309. if (is_array($torrentList)) {
  310. foreach ($torrentList as $key => $value) {
  311. $tempStatus = $this->rTorrentStatus($value[13], $value[10], $value[6]);
  312. if ($tempStatus == 'Seeding' && $this->config['rTorrentHideSeeding']) {
  313. //do nothing
  314. } elseif ($tempStatus == 'Finished' && $this->config['rTorrentHideCompleted']) {
  315. //do nothing
  316. } else {
  317. $torrents[$key] = array(
  318. 'name' => $value[0],
  319. 'base' => $value[1],
  320. 'upTotal' => $value[2],
  321. 'size' => $value[3],
  322. 'downTotal' => $value[4],
  323. 'downloaded' => $value[5],
  324. 'connectionState' => $value[6],
  325. 'leech' => $value[7],
  326. 'seed' => $value[8],
  327. 'date' => $value[9],
  328. 'state' => ($value[10]) ? 'on' : 'off',
  329. 'group' => $value[11],
  330. 'hash' => $value[12],
  331. 'complete' => ($value[13]) ? 'yes' : 'no',
  332. 'ratio' => $value[14],
  333. 'label' => $value[20],
  334. 'status' => $tempStatus,
  335. 'temp' => $value[16] . ' - ' . $value[17] . ' - ' . $value[18],
  336. 'custom' => $value[19] . ' - ' . $value[20] . ' - ' . $value[21],
  337. 'custom2' => $value[22] . ' - ' . $value[23] . ' - ' . $value[24],
  338. );
  339. }
  340. }
  341. }
  342. if (count($torrents) !== 0) {
  343. usort($torrents, function ($a, $b) {
  344. $direction = substr($this->config['rTorrentSortOrder'], -1);
  345. $sort = substr($this->config['rTorrentSortOrder'], 0, strlen($this->config['rTorrentSortOrder']) - 1);
  346. switch ($direction) {
  347. case 'a':
  348. return $a[$sort] <=> $b[$sort];
  349. break;
  350. case 'd':
  351. return $b[$sort] <=> $a[$sort];
  352. break;
  353. default:
  354. return $b['date'] <=> $a['date'];
  355. }
  356. });
  357. $torrents = array_slice($torrents, 0, $this->config['rTorrentLimit']);
  358. }
  359. $api['content']['queueItems'] = $torrents;
  360. $api['content']['historyItems'] = false;
  361. }
  362. } catch
  363. (Requests_Exception $e) {
  364. $this->writeLog('error', 'rTorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  365. $this->setAPIResponse('error', $e->getMessage(), 500);
  366. return false;
  367. };
  368. $api['content'] = isset($api['content']) ? $api['content'] : false;
  369. $this->setAPIResponse('success', null, 200, $api);
  370. return $api;
  371. }
  372. }