utorrent.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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->writeLog('error', 'uTorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  78. $this->setAPIResponse('error', $e->getMessage(), 500);
  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 = new stdClass();
  117. $uTorrentConfig->uTorrentToken = $id;
  118. $reflection = new ReflectionClass($response->cookies);
  119. $cookie = $reflection->getProperty("cookies");
  120. $cookie->setAccessible(true);
  121. $cookie = $cookie->getValue($response->cookies);
  122. if ($cookie['GUID']) {
  123. $uTorrentConfig->uTorrentCookie = $cookie['GUID']->value;
  124. }
  125. if ($uTorrentConfig->uTorrentToken || $uTorrentConfig->uTorrentCookie) {
  126. $this->updateConfigItems($uTorrentConfig);
  127. }
  128. } catch (Requests_Exception $e) {
  129. $this->writeLog('error', 'uTorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  130. $this->setAPIResponse('error', $e->getMessage(), 500);
  131. return false;
  132. }
  133. }
  134. public function getuTorrentHomepageQueue()
  135. {
  136. if (!$this->homepageItemPermissions($this->uTorrentHomepagePermissions('main'), true)) {
  137. return false;
  138. }
  139. try {
  140. if (!$this->config['uTorrentToken'] || !$this->config['uTorrentCookie']) {
  141. $this->getuTorrentToken();
  142. }
  143. $queryUrl = '/gui/?token=' . $this->config['uTorrentToken'] . '&list=1';
  144. $digest = $this->qualifyURL($this->config['uTorrentURL'], true);
  145. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $queryUrl;
  146. $options = $this->requestOptions($url, null, $this->config['uTorrentDisableCertCheck'], $this->config['uTorrentUseCustomCertificate']);
  147. if ($this->config['uTorrentUsername'] !== '' && $this->decrypt($this->config['uTorrentPassword']) !== '') {
  148. $credentials = array('auth' => new Requests_Auth_Basic(array($this->config['uTorrentUsername'], $this->decrypt($this->config['uTorrentPassword']))));
  149. $options = array_merge($options, $credentials);
  150. }
  151. $headers = array(
  152. 'Cookie' => 'GUID=' . $this->config['uTorrentCookie']
  153. );
  154. $response = Requests::get($url, $headers, $options);
  155. $httpResponse = $response->status_code;
  156. if ($httpResponse == 400) {
  157. $this->writeLog('warn', 'uTorrent Token or Cookie Expired. Generating new session..', 'SYSTEM');
  158. $this->getuTorrentToken();
  159. $response = Requests::get($url, $headers, $options);
  160. $httpResponse = $response->status_code;
  161. }
  162. if ($httpResponse == 200) {
  163. $responseData = json_decode($response->body);
  164. $keyArray = (array)$responseData->torrents;
  165. //Populate values
  166. $valueArray = array();
  167. foreach ($keyArray as $keyArr) {
  168. preg_match('/(?<Status>(\w+\s+)+)(?<Percentage>\d+.\d+.*)/', $keyArr[21], $matches);
  169. $Status = str_replace(' ', '', $matches['Status']);
  170. if ($this->config['uTorrentHideSeeding'] && $Status == "Seeding") {
  171. // Do Nothing
  172. } else if ($this->config['uTorrentHideCompleted'] && $Status == "Finished") {
  173. // Do Nothing
  174. } else {
  175. $value = array(
  176. 'Hash' => $keyArr[0],
  177. 'TorrentStatus' => $keyArr[1],
  178. 'Name' => $keyArr[2],
  179. 'Size' => $keyArr[3],
  180. 'Progress' => $keyArr[4],
  181. 'Downloaded' => $keyArr[5],
  182. 'Uploaded' => $keyArr[6],
  183. 'Ratio' => $keyArr[7],
  184. 'upSpeed' => $keyArr[8],
  185. 'downSpeed' => $keyArr[9],
  186. 'eta' => $keyArr[10],
  187. 'Labels' => $keyArr[11],
  188. 'PeersConnected' => $keyArr[12],
  189. 'PeersInSwarm' => $keyArr[13],
  190. 'SeedsConnected' => $keyArr[14],
  191. 'SeedsInSwarm' => $keyArr[15],
  192. 'Availability' => $keyArr[16],
  193. 'TorrentQueueOrder' => $keyArr[17],
  194. 'Remaining' => $keyArr[18],
  195. 'DownloadUrl' => $keyArr[19],
  196. 'RssFeedUrl' => $keyArr[20],
  197. 'Message' => $keyArr[21],
  198. 'StreamId' => $keyArr[22],
  199. 'DateAdded' => $keyArr[23],
  200. 'DateCompleted' => $keyArr[24],
  201. 'AppUpdateUrl' => $keyArr[25],
  202. 'RootDownloadPath' => $keyArr[26],
  203. 'Unknown27' => $keyArr[27],
  204. 'Unknown28' => $keyArr[28],
  205. 'Status' => $Status,
  206. 'Percent' => str_replace(' ', '', $matches['Percentage']),
  207. );
  208. array_push($valueArray, $value);
  209. }
  210. }
  211. $api['content']['queueItems'] = $valueArray;
  212. $api['content'] = $api['content'] ?? false;
  213. $this->setAPIResponse('success', null, 200, $api);
  214. return $api;
  215. }
  216. } catch (Requests_Exception $e) {
  217. $this->writeLog('error', 'uTorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  218. $this->setAPIResponse('error', $e->getMessage(), 500);
  219. return false;
  220. }
  221. }
  222. }