rtorrent.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 checkOverrideURL($url, $override)
  189. {
  190. if (strpos($override, $url) !== false) {
  191. return $override;
  192. } else {
  193. return $url . $override;
  194. }
  195. }
  196. public function rTorrentStatus($completed, $state, $status)
  197. {
  198. if ($completed && $state && $status == 'seed') {
  199. $state = 'Seeding';
  200. } elseif (!$completed && !$state && $status == 'leech') {
  201. $state = 'Stopped';
  202. } elseif (!$completed && $state && $status == 'leech') {
  203. $state = 'Downloading';
  204. } elseif ($completed && !$state && $status == 'seed') {
  205. $state = 'Finished';
  206. }
  207. return ($state) ? $state : $status;
  208. }
  209. public function getRTorrentHomepageQueue()
  210. {
  211. if (!$this->config['homepagerTorrentEnabled']) {
  212. $this->setAPIResponse('error', 'rTorrent homepage item is not enabled', 409);
  213. return false;
  214. }
  215. if (!$this->qualifyRequest($this->config['homepagerTorrentAuth'])) {
  216. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  217. return false;
  218. }
  219. if (empty($this->config['rTorrentURL']) && empty($this->config['rTorrentURLOverride'])) {
  220. $this->setAPIResponse('error', 'rTorrent URL is not defined', 422);
  221. return false;
  222. }
  223. try {
  224. if ($this->config['rTorrentLimit'] == '0') {
  225. $this->config['rTorrentLimit'] = '1000';
  226. }
  227. $torrents = array();
  228. $digest = (empty($this->config['rTorrentURLOverride'])) ? $this->qualifyURL($this->config['rTorrentURL'], true) : $this->qualifyURL($this->checkOverrideURL($this->config['rTorrentURL'], $this->config['rTorrentURLOverride']), true);
  229. $passwordInclude = ($this->config['rTorrentUsername'] !== '' && $this->config['rTorrentPassword'] !== '') ? $this->config['rTorrentUsername'] . ':' . $this->decrypt($this->config['rTorrentPassword']) . "@" : '';
  230. $extraPath = (strpos($this->config['rTorrentURL'], '.php') !== false) ? '' : '/RPC2';
  231. $extraPath = (empty($this->config['rTorrentURLOverride'])) ? $extraPath : '';
  232. $url = $digest['scheme'] . '://' . $passwordInclude . $digest['host'] . $digest['port'] . $digest['path'] . $extraPath;
  233. $options = (localURL($url, $this->config['rTorrentDisableCertCheck'])) ? array('verify' => false) : array();
  234. if ($this->config['rTorrentUsername'] !== '' && $this->decrypt($this->config['rTorrentPassword']) !== '') {
  235. $credentials = array('auth' => new Requests_Auth_Digest(array($this->config['rTorrentUsername'], $this->decrypt($this->config['rTorrentPassword']))));
  236. $options = array_merge($options, $credentials);
  237. }
  238. $data = xmlrpc_encode_request("d.multicall2", array(
  239. "",
  240. "main",
  241. "d.name=",
  242. "d.base_path=",
  243. "d.up.total=",
  244. "d.size_bytes=",
  245. "d.down.total=",
  246. "d.completed_bytes=",
  247. "d.connection_current=",
  248. "d.down.rate=",
  249. "d.up.rate=",
  250. "d.timestamp.started=",
  251. "d.state=",
  252. "d.group.name=",
  253. "d.hash=",
  254. "d.complete=",
  255. "d.ratio=",
  256. "d.chunk_size=",
  257. "f.size_bytes=",
  258. "f.size_chunks=",
  259. "f.completed_chunks=",
  260. "d.custom=",
  261. "d.custom1=",
  262. "d.custom2=",
  263. "d.custom3=",
  264. "d.custom4=",
  265. "d.custom5=",
  266. ), array());
  267. $response = Requests::post($url, array(), $data, $options);
  268. if ($response->success) {
  269. $torrentList = xmlrpc_decode(str_replace('i8>', 'string>', $response->body));
  270. foreach ($torrentList as $key => $value) {
  271. $tempStatus = $this->rTorrentStatus($value[13], $value[10], $value[6]);
  272. if ($tempStatus == 'Seeding' && $this->config['rTorrentHideSeeding']) {
  273. //do nothing
  274. } elseif ($tempStatus == 'Finished' && $this->config['rTorrentHideCompleted']) {
  275. //do nothing
  276. } else {
  277. $torrents[$key] = array(
  278. 'name' => $value[0],
  279. 'base' => $value[1],
  280. 'upTotal' => $value[2],
  281. 'size' => $value[3],
  282. 'downTotal' => $value[4],
  283. 'downloaded' => $value[5],
  284. 'connectionState' => $value[6],
  285. 'leech' => $value[7],
  286. 'seed' => $value[8],
  287. 'date' => $value[9],
  288. 'state' => ($value[10]) ? 'on' : 'off',
  289. 'group' => $value[11],
  290. 'hash' => $value[12],
  291. 'complete' => ($value[13]) ? 'yes' : 'no',
  292. 'ratio' => $value[14],
  293. 'label' => $value[20],
  294. 'status' => $tempStatus,
  295. 'temp' => $value[16] . ' - ' . $value[17] . ' - ' . $value[18],
  296. 'custom' => $value[19] . ' - ' . $value[20] . ' - ' . $value[21],
  297. 'custom2' => $value[22] . ' - ' . $value[23] . ' - ' . $value[24],
  298. );
  299. }
  300. }
  301. if (count($torrents) !== 0) {
  302. usort($torrents, function ($a, $b) {
  303. $direction = substr($this->config['rTorrentSortOrder'], -1);
  304. $sort = substr($this->config['rTorrentSortOrder'], 0, strlen($this->config['rTorrentSortOrder']) - 1);
  305. switch ($direction) {
  306. case 'a':
  307. return $a[$sort] <=> $b[$sort];
  308. break;
  309. case 'd':
  310. return $b[$sort] <=> $a[$sort];
  311. break;
  312. default:
  313. return $b['date'] <=> $a['date'];
  314. }
  315. });
  316. $torrents = array_slice($torrents, 0, $this->config['rTorrentLimit']);
  317. }
  318. $api['content']['queueItems'] = $torrents;
  319. $api['content']['historyItems'] = false;
  320. }
  321. } catch
  322. (Requests_Exception $e) {
  323. $this->writeLog('error', 'rTorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  324. $this->setAPIResponse('error', $e->getMessage(), 500);
  325. return false;
  326. };
  327. $api['content'] = isset($api['content']) ? $api['content'] : false;
  328. $this->setAPIResponse('success', null, 200, $api);
  329. return $api;
  330. }
  331. }