utorrent.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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('username', 'uTorrentUsername'),
  26. $this->settingsOption('password', 'uTorrentPassword'),
  27. $this->settingsOption('disable-cert-check', 'uTorrentDisableCertCheck'),
  28. $this->settingsOption('use-custom-certificate', 'uTorrentUseCustomCertificate'),
  29. ],
  30. 'Misc Options' => [
  31. $this->settingsOption('hide-seeding', 'uTorrentHideSeeding', ['label' => 'Hide Seeding']),
  32. $this->settingsOption('hide-completed', 'uTorrentHideCompleted'),
  33. $this->settingsOption('refresh', 'uTorrentRefresh'),
  34. $this->settingsOption('combine', 'uTorrentCombine'),
  35. ],
  36. 'Test Connection' => [
  37. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  38. $this->settingsOption('test', 'utorrent'),
  39. ]
  40. ]
  41. ];
  42. return array_merge($homepageInformation, $homepageSettings);
  43. }
  44. public function uTorrentHomepagePermissions($key = null)
  45. {
  46. $permissions = [
  47. 'main' => [
  48. 'enabled' => [
  49. 'homepageuTorrentEnabled'
  50. ],
  51. 'auth' => [
  52. 'homepageuTorrentAuth'
  53. ],
  54. 'not_empty' => [
  55. 'uTorrentURL'
  56. ]
  57. ]
  58. ];
  59. if (array_key_exists($key, $permissions)) {
  60. return $permissions[$key];
  61. } elseif ($key == 'all') {
  62. return $permissions;
  63. } else {
  64. return [];
  65. }
  66. }
  67. public function testConnectionuTorrent()
  68. {
  69. if (empty($this->config['uTorrentURL'])) {
  70. $this->setAPIResponse('error', 'uTorrent URL is not defined', 422);
  71. return false;
  72. }
  73. try {
  74. $response = $this->getuTorrentToken();
  75. } catch (Requests_Exception $e) {
  76. $this->writeLog('error', 'uTorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  77. $this->setAPIResponse('error', $e->getMessage(), 500);
  78. return false;
  79. }
  80. }
  81. public function homepageOrderuTorrent()
  82. {
  83. if ($this->homepageItemPermissions($this->uTorrentHomepagePermissions('main'))) {
  84. $loadingBox = ($this->config['uTorrentCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  85. $builder = ($this->config['uTorrentCombine']) ? 'buildDownloaderCombined(\'utorrent\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("utorrent"));';
  86. return '
  87. <div id="' . __FUNCTION__ . '">
  88. ' . $loadingBox . '
  89. <script>
  90. // homepageOrderuTorrent
  91. ' . $builder . '
  92. homepageDownloader("utorrent", "' . $this->config['uTorrentRefresh'] . '");
  93. // End homepageOrderuTorrent
  94. </script>
  95. </div>
  96. ';
  97. }
  98. }
  99. public function getuTorrentToken()
  100. {
  101. try {
  102. $tokenUrl = '/gui/token.html';
  103. $digest = $this->qualifyURL($this->config['uTorrentURL'], true);
  104. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $tokenUrl;
  105. $data = array('username' => $this->config['uTorrentUsername'], 'password' => $this->decrypt($this->config['uTorrentPassword']));
  106. $options = $this->requestOptions($url, null, $this->config['uTorrentDisableCertCheck'], $this->config['uTorrentUseCustomCertificate']);
  107. if ($this->config['uTorrentUsername'] !== '' && $this->decrypt($this->config['uTorrentPassword']) !== '') {
  108. $credentials = array('auth' => new Requests_Auth_Basic(array($this->config['uTorrentUsername'], $this->decrypt($this->config['uTorrentPassword']))));
  109. $options = array_merge($options, $credentials);
  110. }
  111. $response = Requests::post($url, [], $data, $options);
  112. $doc = new DOMDocument();
  113. $doc->loadHTML($response->body);
  114. $id = $doc->getElementById('token');
  115. $uTorrentConfig = new stdClass();
  116. $uTorrentConfig->uTorrentToken = $id->textContent;
  117. $reflection = new ReflectionClass($response->cookies);
  118. $cookie = $reflection->getProperty("cookies");
  119. $cookie->setAccessible(true);
  120. $cookie = $cookie->getValue($response->cookies);
  121. if ($cookie['GUID']) {
  122. $uTorrentConfig->uTorrentCookie = $cookie['GUID']->value;
  123. }
  124. if ($uTorrentConfig->uTorrentToken || $uTorrentConfig->uTorrentCookie) {
  125. $this->updateConfigItems($uTorrentConfig);
  126. }
  127. } catch (Requests_Exception $e) {
  128. $this->writeLog('error', 'uTorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  129. $this->setAPIResponse('error', $e->getMessage(), 500);
  130. return false;
  131. }
  132. }
  133. public function getuTorrentHomepageQueue()
  134. {
  135. if (!$this->homepageItemPermissions($this->uTorrentHomepagePermissions('main'), true)) {
  136. return false;
  137. }
  138. try {
  139. if (!$this->config['uTorrentToken'] || !$this->config['uTorrentCookie']) {
  140. $this->getuTorrentToken();
  141. }
  142. $queryUrl = '/gui/?token='.$this->config['uTorrentToken'].'&list=1';
  143. $digest = $this->qualifyURL($this->config['uTorrentURL'], true);
  144. $url = $digest['scheme'] . '://' . $digest['host'] . $digest['port'] . $digest['path'] . $queryUrl;
  145. $options = $this->requestOptions($url, null, $this->config['uTorrentDisableCertCheck'], $this->config['uTorrentUseCustomCertificate']);
  146. if ($this->config['uTorrentUsername'] !== '' && $this->decrypt($this->config['uTorrentPassword']) !== '') {
  147. $credentials = array('auth' => new Requests_Auth_Basic(array($this->config['uTorrentUsername'], $this->decrypt($this->config['uTorrentPassword']))));
  148. $options = array_merge($options, $credentials);
  149. }
  150. $headers = array(
  151. 'Cookie' => 'GUID=' . $this->config['uTorrentCookie']
  152. );
  153. $response = Requests::get($url, $headers, $options);
  154. $httpResponse = $response->status_code;
  155. if ($httpResponse == 400) {
  156. $this->writeLog('warn', 'uTorrent Token or Cookie Expired. Generating new session..', 'SYSTEM');
  157. $this->getuTorrentToken();
  158. $response = Requests::get($url, $headers, $options);
  159. $httpResponse = $response->status_code;
  160. }
  161. if ($httpResponse == 200) {
  162. $responseData = json_decode($response->body);
  163. $keyArray = (array) $responseData->torrents;
  164. //Populate values
  165. $valueArray = array();
  166. foreach ($keyArray as $keyArr) {
  167. preg_match('/(?<Status>(\w+\s+)+)(?<Percentage>\d+.\d+.*)/', $keyArr[21], $matches);
  168. $Status = str_replace(' ', '', $matches['Status']);
  169. if ($this->config['uTorrentHideSeeding'] && $Status == "Seeding") {
  170. // Do Nothing
  171. } else if ($this->config['uTorrentHideCompleted'] && $Status == "Finished") {
  172. // Do Nothing
  173. } else {
  174. $value = array(
  175. 'Name' => $keyArr[2],
  176. 'Labels' => $keyArr[11],
  177. 'Percent' => str_replace(' ', '', $matches['Percentage']),
  178. 'Status' => $Status,
  179. 'Availability' => $keyArr[4],
  180. 'Done' => $keyArr[5],
  181. 'Size' => $keyArr[3],
  182. 'upSpeed' => $keyArr[8],
  183. 'downSpeed' => $keyArr[9],
  184. 'Message' => $keyArr[21],
  185. );
  186. array_push($valueArray, $value);
  187. }
  188. }
  189. $api['content']['queueItems'] = $valueArray;
  190. $api['content'] = $api['content'] ?? false;
  191. $this->setAPIResponse('success', null, 200, $api);
  192. return $api;
  193. }
  194. } catch (Requests_Exception $e) {
  195. $this->writeLog('error', 'uTorrent Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  196. $this->setAPIResponse('error', $e->getMessage(), 500);
  197. return false;
  198. }
  199. }
  200. }