4
0

rtorrent.php 13 KB

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