jdownloader.php 6.9 KB

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