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. $extraPath = (strpos($this->config['rTorrentURL'], '.php') !== false) ? '' : '/RPC2';
  162. $extraPath = (empty($this->config['rTorrentURLOverride'])) ? $extraPath : '';
  163. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $extraPath;
  164. $options = ($this->localURL($url, $this->config['rTorrentDisableCertCheck'])) ? array('verify' => false) : array();
  165. if ($this->config['rTorrentUsername'] !== '' && $this->decrypt($this->config['rTorrentPassword']) !== '') {
  166. $credentials = array('auth' => new Requests_Auth_Digest(array($this->config['rTorrentUsername'], $this->decrypt($this->config['rTorrentPassword']))));
  167. $options = array_merge($options, $credentials);
  168. }
  169. $data = xmlrpc_encode_request("system.listMethods", null);
  170. $response = Requests::post($url, array(), $data, $options);
  171. if ($response->success) {
  172. $methods = xmlrpc_decode(str_replace('i8>', 'i4>', $response->body));
  173. if (count($methods) !== 0) {
  174. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  175. return true;
  176. }
  177. }
  178. $this->setAPIResponse('error', 'rTorrent error occurred', 500);
  179. return false;
  180. } catch
  181. (Requests_Exception $e) {
  182. $this->writeLog('error', 'rTorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  183. $this->setAPIResponse('error', $e->getMessage(), 500);
  184. return false;
  185. }
  186. }
  187. public function rTorrentHomepagePermissions($key = null)
  188. {
  189. $permissions = [
  190. 'main' => [
  191. 'enabled' => [
  192. 'homepagerTorrentEnabled'
  193. ],
  194. 'auth' => [
  195. 'homepagerTorrentAuth'
  196. ],
  197. 'not_empty' => []
  198. ]
  199. ];
  200. if (array_key_exists($key, $permissions)) {
  201. return $permissions[$key];
  202. } elseif ($key == 'all') {
  203. return $permissions;
  204. } else {
  205. return [];
  206. }
  207. }
  208. public function homepageOrderrTorrent()
  209. {
  210. if ($this->homepageItemPermissions($this->rTorrentHomepagePermissions('main'))) {
  211. $loadingBox = ($this->config['rTorrentCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  212. $builder = ($this->config['rTorrentCombine']) ? 'buildDownloaderCombined(\'rTorrent\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("rTorrent"));';
  213. return '
  214. <div id="' . __FUNCTION__ . '">
  215. ' . $loadingBox . '
  216. <script>
  217. // homepageOrderrTorrent
  218. ' . $builder . '
  219. homepageDownloader("rTorrent", "' . $this->config['homepageDownloadRefresh'] . '");
  220. // End homepageOrderrTorrent
  221. </script>
  222. </div>
  223. ';
  224. }
  225. }
  226. public function checkOverrideURL($url, $override)
  227. {
  228. if (strpos($override, $url) !== false) {
  229. return $override;
  230. } else {
  231. return $url . $override;
  232. }
  233. }
  234. public function rTorrentStatus($completed, $state, $status)
  235. {
  236. if ($completed && $state && $status == 'seed') {
  237. $state = 'Seeding';
  238. } elseif (!$completed && !$state && $status == 'leech') {
  239. $state = 'Stopped';
  240. } elseif (!$completed && $state && $status == 'leech') {
  241. $state = 'Downloading';
  242. } elseif ($completed && !$state && $status == 'seed') {
  243. $state = 'Finished';
  244. }
  245. return ($state) ? $state : $status;
  246. }
  247. public function getRTorrentHomepageQueue()
  248. {
  249. if (empty($this->config['rTorrentURL']) && empty($this->config['rTorrentURLOverride'])) {
  250. $this->setAPIResponse('error', 'rTorrent URL is not defined', 422);
  251. return false;
  252. }
  253. if (!$this->homepageItemPermissions($this->rTorrentHomepagePermissions('main'), true)) {
  254. return false;
  255. }
  256. try {
  257. if ($this->config['rTorrentLimit'] == '0') {
  258. $this->config['rTorrentLimit'] = '1000';
  259. }
  260. $torrents = array();
  261. $digest = (empty($this->config['rTorrentURLOverride'])) ? $this->qualifyURL($this->config['rTorrentURL'], true) : $this->qualifyURL($this->checkOverrideURL($this->config['rTorrentURL'], $this->config['rTorrentURLOverride']), true);
  262. $extraPath = (strpos($this->config['rTorrentURL'], '.php') !== false) ? '' : '/RPC2';
  263. $extraPath = (empty($this->config['rTorrentURLOverride'])) ? $extraPath : '';
  264. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $extraPath;
  265. $options = ($this->localURL($url, $this->config['rTorrentDisableCertCheck'])) ? array('verify' => false) : array();
  266. if ($this->config['rTorrentUsername'] !== '' && $this->decrypt($this->config['rTorrentPassword']) !== '') {
  267. $credentials = array('auth' => new Requests_Auth_Digest(array($this->config['rTorrentUsername'], $this->decrypt($this->config['rTorrentPassword']))));
  268. $options = array_merge($options, $credentials);
  269. }
  270. $data = xmlrpc_encode_request("d.multicall2", array(
  271. "",
  272. "main",
  273. "d.name=",
  274. "d.base_path=",
  275. "d.up.total=",
  276. "d.size_bytes=",
  277. "d.down.total=",
  278. "d.completed_bytes=",
  279. "d.connection_current=",
  280. "d.down.rate=",
  281. "d.up.rate=",
  282. "d.timestamp.started=",
  283. "d.state=",
  284. "d.group.name=",
  285. "d.hash=",
  286. "d.complete=",
  287. "d.ratio=",
  288. "d.chunk_size=",
  289. "f.size_bytes=",
  290. "f.size_chunks=",
  291. "f.completed_chunks=",
  292. "d.custom=",
  293. "d.custom1=",
  294. "d.custom2=",
  295. "d.custom3=",
  296. "d.custom4=",
  297. "d.custom5=",
  298. ), array());
  299. $response = Requests::post($url, array(), $data, $options);
  300. if ($response->success) {
  301. $torrentList = xmlrpc_decode(str_replace('i8>', 'string>', $response->body));
  302. if (is_array($torrentList)) {
  303. foreach ($torrentList as $key => $value) {
  304. $tempStatus = $this->rTorrentStatus($value[13], $value[10], $value[6]);
  305. if ($tempStatus == 'Seeding' && $this->config['rTorrentHideSeeding']) {
  306. //do nothing
  307. } elseif ($tempStatus == 'Finished' && $this->config['rTorrentHideCompleted']) {
  308. //do nothing
  309. } else {
  310. $torrents[$key] = array(
  311. 'name' => $value[0],
  312. 'base' => $value[1],
  313. 'upTotal' => $value[2],
  314. 'size' => $value[3],
  315. 'downTotal' => $value[4],
  316. 'downloaded' => $value[5],
  317. 'connectionState' => $value[6],
  318. 'leech' => $value[7],
  319. 'seed' => $value[8],
  320. 'date' => $value[9],
  321. 'state' => ($value[10]) ? 'on' : 'off',
  322. 'group' => $value[11],
  323. 'hash' => $value[12],
  324. 'complete' => ($value[13]) ? 'yes' : 'no',
  325. 'ratio' => $value[14],
  326. 'label' => $value[20],
  327. 'status' => $tempStatus,
  328. 'temp' => $value[16] . ' - ' . $value[17] . ' - ' . $value[18],
  329. 'custom' => $value[19] . ' - ' . $value[20] . ' - ' . $value[21],
  330. 'custom2' => $value[22] . ' - ' . $value[23] . ' - ' . $value[24],
  331. );
  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. }