rtorrent.php 13 KB

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