jdownloader.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 = array(
  17. 'debug' => true,
  18. 'settings' => array(
  19. 'custom' => '
  20. <div class="row">
  21. <div class="col-lg-12">
  22. <div class="panel panel-info">
  23. <div class="panel-heading">
  24. <span lang="en">Notice</span>
  25. </div>
  26. <div class="panel-wrapper collapse in" aria-expanded="true">
  27. <div class="panel-body">
  28. <ul class="list-icons">
  29. <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>
  30. <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>
  31. </ul>
  32. </div>
  33. </div>
  34. </div>
  35. </div>
  36. </div>
  37. ',
  38. 'Enable' => array(
  39. array(
  40. 'type' => 'switch',
  41. 'name' => 'homepageJdownloaderEnabled',
  42. 'label' => 'Enable',
  43. 'value' => $this->config['homepageJdownloaderEnabled']
  44. ),
  45. array(
  46. 'type' => 'select',
  47. 'name' => 'homepageJdownloaderAuth',
  48. 'label' => 'Minimum Authentication',
  49. 'value' => $this->config['homepageJdownloaderAuth'],
  50. 'options' => $this->groupOptions
  51. )
  52. ),
  53. 'Connection' => array(
  54. array(
  55. 'type' => 'input',
  56. 'name' => 'jdownloaderURL',
  57. 'label' => 'URL',
  58. 'value' => $this->config['jdownloaderURL'],
  59. 'help' => 'Please make sure to use local IP address and port - You also may use local dns name too.',
  60. 'placeholder' => 'http(s)://hostname:port'
  61. )
  62. ),
  63. 'Misc Options' => array(
  64. array(
  65. 'type' => 'select',
  66. 'name' => 'jdownloaderRefresh',
  67. 'label' => 'Refresh Seconds',
  68. 'value' => $this->config['jdownloaderRefresh'],
  69. 'options' => $this->timeOptions()
  70. ),
  71. array(
  72. 'type' => 'switch',
  73. 'name' => 'jdownloaderCombine',
  74. 'label' => 'Add to Combined Downloader',
  75. 'value' => $this->config['jdownloaderCombine']
  76. ),
  77. ),
  78. 'Test Connection' => array(
  79. array(
  80. 'type' => 'blank',
  81. 'label' => 'Please Save before Testing'
  82. ),
  83. array(
  84. 'type' => 'button',
  85. 'label' => '',
  86. 'icon' => 'fa fa-flask',
  87. 'class' => 'pull-right',
  88. 'text' => 'Test Connection',
  89. 'attr' => 'onclick="testAPIConnection(\'jdownloader\')"'
  90. ),
  91. )
  92. )
  93. );
  94. return array_merge($homepageInformation, $homepageSettings);
  95. }
  96. public function testConnectionJDownloader()
  97. {
  98. if (empty($this->config['jdownloaderURL'])) {
  99. $this->setAPIResponse('error', 'JDownloader URL is not defined', 422);
  100. return false;
  101. }
  102. $url = $this->qualifyURL($this->config['jdownloaderURL']);
  103. try {
  104. $options = $this->requestOptions($this->config['jdownloaderURL'], false, $this->config['jdownloaderRefresh']);
  105. $response = Requests::get($url, array(), $options);
  106. if ($response->success) {
  107. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  108. return true;
  109. } else {
  110. $this->setAPIResponse('success', 'JDownloader Error Occurred', 500);
  111. return false;
  112. }
  113. } catch (Requests_Exception $e) {
  114. $this->writeLog('error', 'JDownloader Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  115. $this->setAPIResponse('error', $e->getMessage(), 500);
  116. return false;
  117. };
  118. }
  119. public function jDownloaderHomepagePermissions($key = null)
  120. {
  121. $permissions = [
  122. 'main' => [
  123. 'enabled' => [
  124. 'homepageJdownloaderEnabled'
  125. ],
  126. 'auth' => [
  127. 'homepageJdownloaderAuth'
  128. ],
  129. 'not_empty' => [
  130. 'jdownloaderURL'
  131. ]
  132. ]
  133. ];
  134. if (array_key_exists($key, $permissions)) {
  135. return $permissions[$key];
  136. } elseif ($key == 'all') {
  137. return $permissions;
  138. } else {
  139. return [];
  140. }
  141. }
  142. public function homepageOrderjdownloader()
  143. {
  144. if ($this->homepageItemPermissions($this->jDownloaderHomepagePermissions('main'))) {
  145. $loadingBox = ($this->config['jdownloaderCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  146. $builder = ($this->config['jdownloaderCombine']) ? 'buildDownloaderCombined(\'jdownloader\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("jdownloader"));';
  147. return '
  148. <div id="' . __FUNCTION__ . '">
  149. ' . $loadingBox . '
  150. <script>
  151. // homepageOrderjdownloader
  152. ' . $builder . '
  153. homepageDownloader("jdownloader", "' . $this->config['jdownloaderRefresh'] . '");
  154. // End homepageOrderjdownloader
  155. </script>
  156. </div>
  157. ';
  158. }
  159. }
  160. public function getJdownloaderHomepageQueue()
  161. {
  162. if (!$this->homepageItemPermissions($this->jDownloaderHomepagePermissions('main'), true)) {
  163. return false;
  164. }
  165. $url = $this->qualifyURL($this->config['jdownloaderURL']);
  166. try {
  167. $options = $this->requestOptions($this->config['jdownloaderURL'], false, $this->config['jdownloaderRefresh']);
  168. $response = Requests::get($url, array(), $options);
  169. if ($response->success) {
  170. $temp = json_decode($response->body, true);
  171. $packages = $temp['packages'];
  172. if ($packages['downloader']) {
  173. $api['content']['queueItems'] = $packages['downloader'];
  174. } else {
  175. $api['content']['queueItems'] = [];
  176. }
  177. if ($packages['linkgrabber_decrypted']) {
  178. $api['content']['grabberItems'] = $packages['linkgrabber_decrypted'];
  179. } else {
  180. $api['content']['grabberItems'] = [];
  181. }
  182. if ($packages['linkgrabber_failed']) {
  183. $api['content']['encryptedItems'] = $packages['linkgrabber_failed'];
  184. } else {
  185. $api['content']['encryptedItems'] = [];
  186. }
  187. if ($packages['linkgrabber_offline']) {
  188. $api['content']['offlineItems'] = $packages['linkgrabber_offline'];
  189. } else {
  190. $api['content']['offlineItems'] = [];
  191. }
  192. $api['content']['$status'] = array($temp['downloader_state'], $temp['grabber_collecting'], $temp['update_ready']);
  193. }
  194. } catch (Requests_Exception $e) {
  195. $this->writeLog('error', 'JDownloader Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  196. $this->setAPIResponse('error', $e->getMessage(), 500);
  197. return false;
  198. };
  199. $api['content'] = isset($api['content']) ? $api['content'] : false;
  200. $this->setAPIResponse('success', null, 200, $api);
  201. return $api;
  202. }
  203. }