nzbget.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. trait NZBGetHomepageItem
  3. {
  4. public function nzbgetSettingsArray()
  5. {
  6. return array(
  7. 'name' => 'NZBGet',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/nzbget.png',
  10. 'category' => 'Downloader',
  11. 'settings' => array(
  12. 'Enable' => array(
  13. array(
  14. 'type' => 'switch',
  15. 'name' => 'homepageNzbgetEnabled',
  16. 'label' => 'Enable',
  17. 'value' => $this->config['homepageNzbgetEnabled']
  18. ),
  19. array(
  20. 'type' => 'select',
  21. 'name' => 'homepageNzbgetAuth',
  22. 'label' => 'Minimum Authentication',
  23. 'value' => $this->config['homepageNzbgetAuth'],
  24. 'options' => $this->groupOptions
  25. )
  26. ),
  27. 'Connection' => array(
  28. array(
  29. 'type' => 'input',
  30. 'name' => 'nzbgetURL',
  31. 'label' => 'URL',
  32. 'value' => $this->config['nzbgetURL'],
  33. 'help' => 'Please make sure to use local IP address and port - You also may use local dns name too.',
  34. 'placeholder' => 'http(s)://hostname:port'
  35. ),
  36. array(
  37. 'type' => 'input',
  38. 'name' => 'nzbgetUsername',
  39. 'label' => 'Username',
  40. 'value' => $this->config['nzbgetUsername']
  41. ),
  42. array(
  43. 'type' => 'password',
  44. 'name' => 'nzbgetPassword',
  45. 'label' => 'Password',
  46. 'value' => $this->config['nzbgetPassword']
  47. )
  48. ),
  49. 'Misc Options' => array(
  50. array(
  51. 'type' => 'select',
  52. 'name' => 'homepageDownloadRefresh',
  53. 'label' => 'Refresh Seconds',
  54. 'value' => $this->config['homepageDownloadRefresh'],
  55. 'options' => $this->timeOptions()
  56. ),
  57. array(
  58. 'type' => 'switch',
  59. 'name' => 'nzbgetCombine',
  60. 'label' => 'Add to Combined Downloader',
  61. 'value' => $this->config['nzbgetCombine']
  62. ),
  63. ),
  64. 'Test Connection' => array(
  65. array(
  66. 'type' => 'blank',
  67. 'label' => 'Please Save before Testing'
  68. ),
  69. array(
  70. 'type' => 'button',
  71. 'label' => '',
  72. 'icon' => 'fa fa-flask',
  73. 'class' => 'pull-right',
  74. 'text' => 'Test Connection',
  75. 'attr' => 'onclick="testAPIConnection(\'nzbget\')"'
  76. ),
  77. )
  78. )
  79. );
  80. }
  81. public function testConnectionNZBGet()
  82. {
  83. if (empty($this->config['nzbgetURL'])) {
  84. $this->setAPIResponse('error', 'NZBGet URL is not defined', 422);
  85. return false;
  86. }
  87. try {
  88. $url = $this->qualifyURL($this->config['nzbgetURL']);
  89. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  90. $urlGroups = $url . '/jsonrpc/listgroups';
  91. if ($this->config['nzbgetUsername'] !== '' && $this->decrypt($this->config['nzbgetPassword']) !== '') {
  92. $credentials = array('auth' => new Requests_Auth_Basic(array($this->config['nzbgetUsername'], $this->decrypt($this->config['nzbgetPassword']))));
  93. $options = array_merge($options, $credentials);
  94. }
  95. $response = Requests::get($urlGroups, array(), $options);
  96. if ($response->success) {
  97. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  98. return true;
  99. } else {
  100. $this->setAPIResponse('success', 'NZBGet: An Error Occurred', 500);
  101. return false;
  102. }
  103. } catch (Requests_Exception $e) {
  104. $this->writeLog('error', 'NZBGet Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  105. $this->setAPIResponse('error', $e->getMessage(), 500);
  106. return false;
  107. }
  108. }
  109. public function nzbgetHomepagePermissions($key = null)
  110. {
  111. $permissions = [
  112. 'main' => [
  113. 'enabled' => [
  114. 'homepageNzbgetEnabled'
  115. ],
  116. 'auth' => [
  117. 'homepageNzbgetAuth'
  118. ],
  119. 'not_empty' => [
  120. 'nzbgetURL'
  121. ]
  122. ]
  123. ];
  124. if (array_key_exists($key, $permissions)) {
  125. return $permissions[$key];
  126. } elseif ($key == 'all') {
  127. return $permissions;
  128. } else {
  129. return [];
  130. }
  131. }
  132. public function homepageOrdernzbget()
  133. {
  134. if ($this->homepageItemPermissions($this->nzbgetHomepagePermissions('main'))) {
  135. $loadingBox = ($this->config['nzbgetCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  136. $builder = ($this->config['nzbgetCombine']) ? 'buildDownloaderCombined(\'nzbget\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("nzbget"));';
  137. return '
  138. <div id="' . __FUNCTION__ . '">
  139. ' . $loadingBox . '
  140. <script>
  141. // homepageOrdernzbget
  142. ' . $builder . '
  143. homepageDownloader("nzbget", "' . $this->config['homepageDownloadRefresh'] . '");
  144. // End homepageOrdernzbget
  145. </script>
  146. </div>
  147. ';
  148. }
  149. }
  150. public function getNzbgetHomepageQueue()
  151. {
  152. if (!$this->homepageItemPermissions($this->nzbgetHomepagePermissions('main'), true)) {
  153. return false;
  154. }
  155. try {
  156. $url = $this->qualifyURL($this->config['nzbgetURL']);
  157. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  158. $urlGroups = $url . '/jsonrpc/listgroups';
  159. $urlHistory = $url . '/jsonrpc/history';
  160. if ($this->config['nzbgetUsername'] !== '' && $this->decrypt($this->config['nzbgetPassword']) !== '') {
  161. $credentials = array('auth' => new Requests_Auth_Basic(array($this->config['nzbgetUsername'], $this->decrypt($this->config['nzbgetPassword']))));
  162. $options = array_merge($options, $credentials);
  163. }
  164. $response = Requests::get($urlGroups, array(), $options);
  165. if ($response->success) {
  166. $api['content']['queueItems'] = json_decode($response->body, true);
  167. }
  168. $response = Requests::get($urlHistory, array(), $options);
  169. if ($response->success) {
  170. $api['content']['historyItems'] = json_decode($response->body, true);
  171. }
  172. $api['content'] = isset($api['content']) ? $api['content'] : false;
  173. $this->setAPIResponse('success', null, 200, $api);
  174. return $api;
  175. } catch (Requests_Exception $e) {
  176. $this->writeLog('error', 'NZBGet Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  177. $this->setAPIResponse('error', $e->getMessage(), 500);
  178. return false;
  179. }
  180. }
  181. }