jdownloader.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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('blank'),
  47. $this->settingsOption('disable-cert-check', 'jdownloaderDisableCertCheck'),
  48. $this->settingsOption('use-custom-certificate', 'jdownloaderUseCustomCertificate'),
  49. ],
  50. 'Misc Options' => [
  51. $this->settingsOption('refresh', 'jdownloaderRefresh'),
  52. $this->settingsOption('combine', 'jdownloaderCombine'),
  53. ],
  54. 'Test Connection' => [
  55. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  56. $this->settingsOption('test', 'jdownloader'),
  57. ]
  58. ]
  59. ];
  60. return array_merge($homepageInformation, $homepageSettings);
  61. }
  62. public function testConnectionJDownloader()
  63. {
  64. if (empty($this->config['jdownloaderURL'])) {
  65. $this->setAPIResponse('error', 'JDownloader URL is not defined', 422);
  66. return false;
  67. }
  68. $url = $this->qualifyURL($this->config['jdownloaderURL']);
  69. try {
  70. $options = $this->requestOptions($url, $this->config['jdownloaderRefresh'], $this->config['jdownloaderDisableCertCheck'], $this->config['jdownloaderUseCustomCertificate']);
  71. $response = Requests::get($url, [], $options);
  72. if ($response->success) {
  73. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  74. return true;
  75. } else {
  76. $this->setAPIResponse('success', 'JDownloader Error Occurred', 500);
  77. return false;
  78. }
  79. } catch (Requests_Exception $e) {
  80. $this->writeLog('error', 'JDownloader Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  81. $this->setAPIResponse('error', $e->getMessage(), 500);
  82. return false;
  83. };
  84. }
  85. public function jDownloaderHomepagePermissions($key = null)
  86. {
  87. $permissions = [
  88. 'main' => [
  89. 'enabled' => [
  90. 'homepageJdownloaderEnabled'
  91. ],
  92. 'auth' => [
  93. 'homepageJdownloaderAuth'
  94. ],
  95. 'not_empty' => [
  96. 'jdownloaderURL'
  97. ]
  98. ]
  99. ];
  100. return $this->homepageCheckKeyPermissions($key, $permissions);
  101. }
  102. public function homepageOrderjdownloader()
  103. {
  104. if ($this->homepageItemPermissions($this->jDownloaderHomepagePermissions('main'))) {
  105. $loadingBox = ($this->config['jdownloaderCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  106. $builder = ($this->config['jdownloaderCombine']) ? 'buildDownloaderCombined(\'jdownloader\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("jdownloader"));';
  107. return '
  108. <div id="' . __FUNCTION__ . '">
  109. ' . $loadingBox . '
  110. <script>
  111. // homepageOrderjdownloader
  112. ' . $builder . '
  113. homepageDownloader("jdownloader", "' . $this->config['jdownloaderRefresh'] . '");
  114. // End homepageOrderjdownloader
  115. </script>
  116. </div>
  117. ';
  118. }
  119. }
  120. public function getJdownloaderHomepageQueue()
  121. {
  122. if (!$this->homepageItemPermissions($this->jDownloaderHomepagePermissions('main'), true)) {
  123. return false;
  124. }
  125. $url = $this->qualifyURL($this->config['jdownloaderURL']);
  126. try {
  127. $options = $this->requestOptions($url, $this->config['jdownloaderRefresh'], $this->config['jdownloaderDisableCertCheck'], $this->config['jdownloaderUseCustomCertificate']);
  128. $response = Requests::get($url, [], $options);
  129. if ($response->success) {
  130. $temp = json_decode($response->body, true);
  131. $packages = $temp['packages'];
  132. if ($packages['downloader']) {
  133. $api['content']['queueItems'] = $packages['downloader'];
  134. } else {
  135. $api['content']['queueItems'] = [];
  136. }
  137. if ($packages['linkgrabber_decrypted']) {
  138. $api['content']['grabberItems'] = $packages['linkgrabber_decrypted'];
  139. } else {
  140. $api['content']['grabberItems'] = [];
  141. }
  142. if ($packages['linkgrabber_failed']) {
  143. $api['content']['encryptedItems'] = $packages['linkgrabber_failed'];
  144. } else {
  145. $api['content']['encryptedItems'] = [];
  146. }
  147. if ($packages['linkgrabber_offline']) {
  148. $api['content']['offlineItems'] = $packages['linkgrabber_offline'];
  149. } else {
  150. $api['content']['offlineItems'] = [];
  151. }
  152. $api['content']['$status'] = array($temp['downloader_state'], $temp['grabber_collecting'], $temp['update_ready']);
  153. }
  154. } catch (Requests_Exception $e) {
  155. $this->writeLog('error', 'JDownloader Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  156. $this->setAPIResponse('error', $e->getMessage(), 500);
  157. return false;
  158. };
  159. $api['content'] = isset($api['content']) ? $api['content'] : false;
  160. $this->setAPIResponse('success', null, 200, $api);
  161. return $api;
  162. }
  163. }