nzbget.php 6.7 KB

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