nzbget.php 6.7 KB

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