utorrent.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. trait uTorrentHomepageItem
  3. {
  4. public function uTorrentSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'uTorrent',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/utorrent.png',
  10. 'category' => 'Downloader',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'Enable' => [
  20. $this->settingsOption('enable', 'homepageuTorrentEnabled'),
  21. $this->settingsOption('auth', 'homepageuTorrentAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('url', 'uTorrentURL'),
  25. $this->settingsOption('blank'),
  26. $this->settingsOption('username', 'uTorrentUsername'),
  27. $this->settingsOption('password', 'uTorrentPassword'),
  28. $this->settingsOption('disable-cert-check', 'uTorrentDisableCertCheck'),
  29. $this->settingsOption('use-custom-certificate', 'uTorrentUseCustomCertificate'),
  30. ],
  31. 'Misc Options' => [
  32. $this->settingsOption('hide-seeding', 'uTorrentHideSeeding', ['label' => 'Hide Seeding']),
  33. $this->settingsOption('hide-completed', 'uTorrentHideCompleted'),
  34. $this->settingsOption('refresh', 'uTorrentRefresh'),
  35. $this->settingsOption('combine', 'uTorrentCombine'),
  36. ],
  37. 'Test Connection' => [
  38. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  39. $this->settingsOption('test', 'utorrent'),
  40. ]
  41. ]
  42. ];
  43. return array_merge($homepageInformation, $homepageSettings);
  44. }
  45. public function uTorrentHomepagePermissions($key = null)
  46. {
  47. $permissions = [
  48. 'main' => [
  49. 'enabled' => [
  50. 'homepageuTorrentEnabled'
  51. ],
  52. 'auth' => [
  53. 'homepageuTorrentAuth'
  54. ],
  55. 'not_empty' => [
  56. 'uTorrentURL'
  57. ]
  58. ]
  59. ];
  60. if (array_key_exists($key, $permissions)) {
  61. return $permissions[$key];
  62. } elseif ($key == 'all') {
  63. return $permissions;
  64. } else {
  65. return [];
  66. }
  67. }
  68. public function testConnectionuTorrent()
  69. {
  70. if (empty($this->config['uTorrentURL'])) {
  71. $this->setAPIResponse('error', 'uTorrent URL is not defined', 422);
  72. return false;
  73. }
  74. try {
  75. $response = $this->getuTorrentToken();
  76. } catch (Requests_Exception $e) {
  77. $this->setLoggerChannel('uTorrent')->error($e);
  78. $this->setResponse(500, $e->getMessage());
  79. return false;
  80. }
  81. }
  82. public function homepageOrderuTorrent()
  83. {
  84. if ($this->homepageItemPermissions($this->uTorrentHomepagePermissions('main'))) {
  85. $loadingBox = ($this->config['uTorrentCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  86. $builder = ($this->config['uTorrentCombine']) ? 'buildDownloaderCombined(\'utorrent\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("utorrent"));';
  87. return '
  88. <div id="' . __FUNCTION__ . '">
  89. ' . $loadingBox . '
  90. <script>
  91. // homepageOrderuTorrent
  92. ' . $builder . '
  93. homepageDownloader("utorrent", "' . $this->config['uTorrentRefresh'] . '");
  94. // End homepageOrderuTorrent
  95. </script>
  96. </div>
  97. ';
  98. }
  99. }
  100. public function getuTorrentToken()
  101. {
  102. try {
  103. $tokenUrl = '/gui/token.html';
  104. $digest = $this->qualifyURL($this->config['uTorrentURL'], true);
  105. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $tokenUrl;
  106. $data = array('username' => $this->config['uTorrentUsername'], 'password' => $this->decrypt($this->config['uTorrentPassword']));
  107. $options = $this->requestOptions($url, null, $this->config['uTorrentDisableCertCheck'], $this->config['uTorrentUseCustomCertificate']);
  108. if ($this->config['uTorrentUsername'] !== '' && $this->decrypt($this->config['uTorrentPassword']) !== '') {
  109. $credentials = array('auth' => new Requests_Auth_Basic(array($this->config['uTorrentUsername'], $this->decrypt($this->config['uTorrentPassword']))));
  110. $options = array_merge($options, $credentials);
  111. }
  112. $response = Requests::post($url, [], $data, $options);
  113. $dom = new PHPHtmlParser\Dom;
  114. $dom->loadStr($response->body);
  115. $id = $dom->getElementById('token')->text;
  116. $uTorrentConfig = array(
  117. "uTorrentToken" => $id,
  118. "uTorrentCookie" => "",
  119. );
  120. $reflection = new ReflectionClass($response->cookies);
  121. $cookie = $reflection->getProperty("cookies");
  122. $cookie->setAccessible(true);
  123. $cookie = $cookie->getValue($response->cookies);
  124. if ($cookie['GUID']) {
  125. $uTorrentConfig['uTorrentCookie'] = $cookie['GUID']->value;
  126. }
  127. if ($uTorrentConfig['uTorrentToken'] || $uTorrentConfig['uTorrentCookie']) {
  128. $this->updateConfigItems($uTorrentConfig);
  129. }
  130. } catch (Requests_Exception $e) {
  131. $this->setLoggerChannel('uTorrent')->error($e);
  132. $this->setResponse(500, $e->getMessage());
  133. return false;
  134. }
  135. }
  136. public function getuTorrentHomepageQueue()
  137. {
  138. if (!$this->homepageItemPermissions($this->uTorrentHomepagePermissions('main'), true)) {
  139. return false;
  140. }
  141. try {
  142. if (!$this->config['uTorrentToken'] || !$this->config['uTorrentCookie']) {
  143. $this->getuTorrentToken();
  144. }
  145. $queryUrl = '/gui/?token=' . $this->config['uTorrentToken'] . '&list=1';
  146. $digest = $this->qualifyURL($this->config['uTorrentURL'], true);
  147. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $queryUrl;
  148. $options = $this->requestOptions($url, null, $this->config['uTorrentDisableCertCheck'], $this->config['uTorrentUseCustomCertificate']);
  149. if ($this->config['uTorrentUsername'] !== '' && $this->decrypt($this->config['uTorrentPassword']) !== '') {
  150. $credentials = array('auth' => new Requests_Auth_Basic(array($this->config['uTorrentUsername'], $this->decrypt($this->config['uTorrentPassword']))));
  151. $options = array_merge($options, $credentials);
  152. }
  153. $headers = array(
  154. 'Cookie' => 'GUID=' . $this->config['uTorrentCookie']
  155. );
  156. $response = Requests::get($url, $headers, $options);
  157. $httpResponse = $response->status_code;
  158. if ($httpResponse == 400) {
  159. $this->setLoggerChannel('uTorrent')->warning('Token or Cookie Expired. Generating new session...');
  160. $this->getuTorrentToken();
  161. $response = Requests::get($url, $headers, $options);
  162. $httpResponse = $response->status_code;
  163. }
  164. if ($httpResponse == 200) {
  165. $responseData = json_decode($response->body);
  166. $keyArray = (array)$responseData->torrents;
  167. //Populate values
  168. $valueArray = array();
  169. foreach ($keyArray as $keyArr) {
  170. preg_match('/(?<Status>(\w+\s+)+)(?<Percentage>\d+.\d+.*)/', $keyArr[21], $matches);
  171. $Status = str_replace(' ', '', $matches['Status']);
  172. if ($this->config['uTorrentHideSeeding'] && $Status == "Seeding") {
  173. // Do Nothing
  174. } else if ($this->config['uTorrentHideCompleted'] && $Status == "Finished") {
  175. // Do Nothing
  176. } else {
  177. $value = array(
  178. 'Hash' => $keyArr[0],
  179. 'TorrentStatus' => $keyArr[1],
  180. 'Name' => $keyArr[2],
  181. 'Size' => $keyArr[3],
  182. 'Progress' => $keyArr[4],
  183. 'Downloaded' => $keyArr[5],
  184. 'Uploaded' => $keyArr[6],
  185. 'Ratio' => $keyArr[7],
  186. 'upSpeed' => $keyArr[8],
  187. 'downSpeed' => $keyArr[9],
  188. 'eta' => $keyArr[10],
  189. 'Labels' => $keyArr[11],
  190. 'PeersConnected' => $keyArr[12],
  191. 'PeersInSwarm' => $keyArr[13],
  192. 'SeedsConnected' => $keyArr[14],
  193. 'SeedsInSwarm' => $keyArr[15],
  194. 'Availability' => $keyArr[16],
  195. 'TorrentQueueOrder' => $keyArr[17],
  196. 'Remaining' => $keyArr[18],
  197. 'DownloadUrl' => $keyArr[19],
  198. 'RssFeedUrl' => $keyArr[20],
  199. 'Message' => $keyArr[21],
  200. 'StreamId' => $keyArr[22],
  201. 'DateAdded' => $keyArr[23],
  202. 'DateCompleted' => $keyArr[24],
  203. 'AppUpdateUrl' => $keyArr[25],
  204. 'RootDownloadPath' => $keyArr[26],
  205. 'Unknown27' => $keyArr[27],
  206. 'Unknown28' => $keyArr[28],
  207. 'Status' => $Status,
  208. 'Percent' => str_replace(' ', '', $matches['Percentage']),
  209. );
  210. array_push($valueArray, $value);
  211. }
  212. }
  213. $api['content']['queueItems'] = $valueArray;
  214. $api['content'] = $api['content'] ?? false;
  215. $this->setAPIResponse('success', null, 200, $api);
  216. return $api;
  217. }
  218. } catch (Requests_Exception $e) {
  219. $this->setLoggerChannel('uTorrent')->error($e);
  220. $this->setResponse(500, $e->getMessage());
  221. return false;
  222. }
  223. }
  224. }