jdownloader.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. trait JDownloaderHomepageItem
  3. {
  4. public function jDownloaderSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'JDownloader',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/jdownloader.png',
  10. 'category' => 'Downloader',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'FYI' => [
  20. $this->settingsOption('html', null, ['override' => 12, 'html' => '
  21. <div class="row">
  22. <div class="col-lg-12">
  23. <div class="panel panel-info">
  24. <div class="panel-heading">
  25. <span lang="en">Notice</span>
  26. </div>
  27. <div class="panel-wrapper collapse in" aria-expanded="true">
  28. <div class="panel-body">
  29. <ul class="list-icons">
  30. <li><i class="fa fa-chevron-right text-danger"></i> <a href="https://pypi.org/project/myjd-api/" target="_blank">Download [myjd-api] Module</a></li>
  31. <li><i class="fa fa-chevron-right text-danger"></i> Add <b>/api/myjd</b> to the URL if you are using <a href="https://pypi.org/project/FeedCrawler/" target="_blank">FeedCrawler</a></li>
  32. </ul>
  33. </div>
  34. </div>
  35. </div>
  36. </div>
  37. </div>']
  38. ),
  39. ],
  40. 'Enable' => [
  41. $this->settingsOption('enable', 'homepageJdownloaderEnabled'),
  42. $this->settingsOption('auth', 'homepageJdownloaderAuth'),
  43. ],
  44. 'Connection' => [
  45. $this->settingsOption('url', 'jdownloaderURL'),
  46. $this->settingsOption('username', 'jdownloaderUsername'),
  47. $this->settingsOption('password', 'jdownloaderPassword'),
  48. $this->settingsOption('blank'),
  49. $this->settingsOption('disable-cert-check', 'jdownloaderDisableCertCheck'),
  50. $this->settingsOption('use-custom-certificate', 'jdownloaderUseCustomCertificate'),
  51. ],
  52. 'Misc Options' => [
  53. $this->settingsOption('refresh', 'jdownloaderRefresh'),
  54. $this->settingsOption('combine', 'jdownloaderCombine'),
  55. ],
  56. 'Test Connection' => [
  57. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  58. $this->settingsOption('test', 'jdownloader'),
  59. ]
  60. ]
  61. ];
  62. return array_merge($homepageInformation, $homepageSettings);
  63. }
  64. public function testConnectionJDownloader()
  65. {
  66. if (empty($this->config['jdownloaderURL'])) {
  67. $this->setAPIResponse('error', 'JDownloader URL is not defined', 422);
  68. return false;
  69. }
  70. if (!empty($this->config['jdownloaderUsername']) || !empty($this->config['jdownloaderPassword'])) {
  71. $auth = array('auth' => array($this->config['jdownloaderUsername'], $this->decrypt($this->config['jdownloaderPassword'])));
  72. } else {
  73. $auth = [];
  74. }
  75. $url = $this->qualifyURL($this->config['jdownloaderURL']);
  76. try {
  77. $options = $this->requestOptions($url, $this->config['jdownloaderRefresh'], $this->config['jdownloaderDisableCertCheck'], $this->config['jdownloaderUseCustomCertificate'], $auth);
  78. $response = Requests::get($url, [], $options);
  79. if ($response->success) {
  80. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  81. return true;
  82. } else {
  83. echo($response->body);
  84. $this->setAPIResponse('success', 'JDownloader Error Occurred', 500);
  85. return false;
  86. }
  87. } catch (Requests_Exception $e) {
  88. $this->setLoggerChannel('JDownloader')->error($e);
  89. $this->setResponse(500, $e->getMessage());
  90. return false;
  91. }
  92. }
  93. public function jDownloaderHomepagePermissions($key = null)
  94. {
  95. $permissions = [
  96. 'main' => [
  97. 'enabled' => [
  98. 'homepageJdownloaderEnabled'
  99. ],
  100. 'auth' => [
  101. 'homepageJdownloaderAuth'
  102. ],
  103. 'not_empty' => [
  104. 'jdownloaderURL'
  105. ]
  106. ]
  107. ];
  108. return $this->homepageCheckKeyPermissions($key, $permissions);
  109. }
  110. public function homepageOrderjdownloader()
  111. {
  112. if ($this->homepageItemPermissions($this->jDownloaderHomepagePermissions('main'))) {
  113. $loadingBox = ($this->config['jdownloaderCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  114. $builder = ($this->config['jdownloaderCombine']) ? 'buildDownloaderCombined(\'jdownloader\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("jdownloader"));';
  115. return '
  116. <div id="' . __FUNCTION__ . '">
  117. ' . $loadingBox . '
  118. <script>
  119. // homepageOrderjdownloader
  120. ' . $builder . '
  121. homepageDownloader("jdownloader", "' . $this->config['jdownloaderRefresh'] . '");
  122. // End homepageOrderjdownloader
  123. </script>
  124. </div>
  125. ';
  126. }
  127. }
  128. public function getJdownloaderHomepageQueue()
  129. {
  130. if (!$this->homepageItemPermissions($this->jDownloaderHomepagePermissions('main'), true)) {
  131. return false;
  132. }
  133. $url = $this->qualifyURL($this->config['jdownloaderURL']);
  134. if (!empty($this->config['jdownloaderUsername']) || !empty($this->config['jdownloaderPassword'])) {
  135. $auth = array('auth' => array($this->config['jdownloaderUsername'], $this->decrypt($this->config['jdownloaderPassword'])));
  136. } else {
  137. $auth = [];
  138. }
  139. try {
  140. $options = $this->requestOptions($url, $this->config['jdownloaderRefresh'], $this->config['jdownloaderDisableCertCheck'], $this->config['jdownloaderUseCustomCertificate'], $auth);
  141. $response = Requests::get($url, [], $options);
  142. if ($response->success) {
  143. $temp = json_decode($response->body, true);
  144. $packages = $temp['packages'];
  145. if ($packages['downloader']) {
  146. $api['content']['queueItems'] = $packages['downloader'];
  147. } else {
  148. $api['content']['queueItems'] = [];
  149. }
  150. if ($packages['linkgrabber_decrypted']) {
  151. $api['content']['grabberItems'] = $packages['linkgrabber_decrypted'];
  152. } else {
  153. $api['content']['grabberItems'] = [];
  154. }
  155. if ($packages['linkgrabber_failed']) {
  156. $api['content']['encryptedItems'] = $packages['linkgrabber_failed'];
  157. } else {
  158. $api['content']['encryptedItems'] = [];
  159. }
  160. if ($packages['linkgrabber_offline']) {
  161. $api['content']['offlineItems'] = $packages['linkgrabber_offline'];
  162. } else {
  163. $api['content']['offlineItems'] = [];
  164. }
  165. $api['content']['$status'] = array($temp['downloader_state'], $temp['grabber_collecting'], $temp['update_ready']);
  166. }
  167. } catch (Requests_Exception $e) {
  168. $this->setLoggerChannel('JDownloader')->error($e);
  169. $this->setResponse(500, $e->getMessage());
  170. return false;
  171. };
  172. $api['content'] = isset($api['content']) ? $api['content'] : false;
  173. $this->setAPIResponse('success', null, 200, $api);
  174. return $api;
  175. }
  176. }