nzbget.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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">
  64. <h3 lang="en">Nzbget SOCKS API Connection</h3>
  65. <p>Using this feature allows you to access the API without having to reverse proxy it. Just access it from: </p>
  66. <code>' . $this->getServerPath() . 'api/v2/socks/nzbget/</code>
  67. </div>
  68. </div>
  69. </div>'
  70. ),
  71. array(
  72. 'type' => 'switch',
  73. 'name' => 'nzbgetSocksEnabled',
  74. 'label' => 'Enable',
  75. 'value' => $this->config['nzbgetSocksEnabled']
  76. ),
  77. array(
  78. 'type' => 'select',
  79. 'name' => 'nzbgetSocksAuth',
  80. 'label' => 'Minimum Authentication',
  81. 'value' => $this->config['nzbgetSocksAuth'],
  82. 'options' => $this->groupOptions
  83. ),
  84. ),
  85. 'Misc Options' => array(
  86. array(
  87. 'type' => 'select',
  88. 'name' => 'homepageDownloadRefresh',
  89. 'label' => 'Refresh Seconds',
  90. 'value' => $this->config['homepageDownloadRefresh'],
  91. 'options' => $this->timeOptions()
  92. ),
  93. array(
  94. 'type' => 'switch',
  95. 'name' => 'nzbgetCombine',
  96. 'label' => 'Add to Combined Downloader',
  97. 'value' => $this->config['nzbgetCombine']
  98. ),
  99. ),
  100. 'Test Connection' => array(
  101. array(
  102. 'type' => 'blank',
  103. 'label' => 'Please Save before Testing'
  104. ),
  105. array(
  106. 'type' => 'button',
  107. 'label' => '',
  108. 'icon' => 'fa fa-flask',
  109. 'class' => 'pull-right',
  110. 'text' => 'Test Connection',
  111. 'attr' => 'onclick="testAPIConnection(\'nzbget\')"'
  112. ),
  113. )
  114. )
  115. );
  116. return array_merge($homepageInformation, $homepageSettings);
  117. }
  118. public function testConnectionNZBGet()
  119. {
  120. if (empty($this->config['nzbgetURL'])) {
  121. $this->setAPIResponse('error', 'NZBGet URL is not defined', 422);
  122. return false;
  123. }
  124. try {
  125. $url = $this->qualifyURL($this->config['nzbgetURL']);
  126. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  127. $urlGroups = $url . '/jsonrpc/listgroups';
  128. if ($this->config['nzbgetUsername'] !== '' && $this->decrypt($this->config['nzbgetPassword']) !== '') {
  129. $credentials = array('auth' => new Requests_Auth_Basic(array($this->config['nzbgetUsername'], $this->decrypt($this->config['nzbgetPassword']))));
  130. $options = array_merge($options, $credentials);
  131. }
  132. $response = Requests::get($urlGroups, array(), $options);
  133. if ($response->success) {
  134. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  135. return true;
  136. } else {
  137. $this->setAPIResponse('success', 'NZBGet: An Error Occurred', 500);
  138. return false;
  139. }
  140. } catch (Requests_Exception $e) {
  141. $this->writeLog('error', 'NZBGet Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  142. $this->setAPIResponse('error', $e->getMessage(), 500);
  143. return false;
  144. }
  145. }
  146. public function nzbgetHomepagePermissions($key = null)
  147. {
  148. $permissions = [
  149. 'main' => [
  150. 'enabled' => [
  151. 'homepageNzbgetEnabled'
  152. ],
  153. 'auth' => [
  154. 'homepageNzbgetAuth'
  155. ],
  156. 'not_empty' => [
  157. 'nzbgetURL'
  158. ]
  159. ]
  160. ];
  161. if (array_key_exists($key, $permissions)) {
  162. return $permissions[$key];
  163. } elseif ($key == 'all') {
  164. return $permissions;
  165. } else {
  166. return [];
  167. }
  168. }
  169. public function homepageOrdernzbget()
  170. {
  171. if ($this->homepageItemPermissions($this->nzbgetHomepagePermissions('main'))) {
  172. $loadingBox = ($this->config['nzbgetCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  173. $builder = ($this->config['nzbgetCombine']) ? 'buildDownloaderCombined(\'nzbget\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("nzbget"));';
  174. return '
  175. <div id="' . __FUNCTION__ . '">
  176. ' . $loadingBox . '
  177. <script>
  178. // homepageOrdernzbget
  179. ' . $builder . '
  180. homepageDownloader("nzbget", "' . $this->config['homepageDownloadRefresh'] . '");
  181. // End homepageOrdernzbget
  182. </script>
  183. </div>
  184. ';
  185. }
  186. }
  187. public function getNzbgetHomepageQueue()
  188. {
  189. if (!$this->homepageItemPermissions($this->nzbgetHomepagePermissions('main'), true)) {
  190. return false;
  191. }
  192. try {
  193. $url = $this->qualifyURL($this->config['nzbgetURL']);
  194. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  195. $urlGroups = $url . '/jsonrpc/listgroups';
  196. $urlHistory = $url . '/jsonrpc/history';
  197. if ($this->config['nzbgetUsername'] !== '' && $this->decrypt($this->config['nzbgetPassword']) !== '') {
  198. $credentials = array('auth' => new Requests_Auth_Basic(array($this->config['nzbgetUsername'], $this->decrypt($this->config['nzbgetPassword']))));
  199. $options = array_merge($options, $credentials);
  200. }
  201. $response = Requests::get($urlGroups, array(), $options);
  202. if ($response->success) {
  203. $api['content']['queueItems'] = json_decode($response->body, true);
  204. }
  205. $response = Requests::get($urlHistory, array(), $options);
  206. if ($response->success) {
  207. $api['content']['historyItems'] = json_decode($response->body, true);
  208. }
  209. $api['content'] = isset($api['content']) ? $api['content'] : false;
  210. $this->setAPIResponse('success', null, 200, $api);
  211. return $api;
  212. } catch (Requests_Exception $e) {
  213. $this->writeLog('error', 'NZBGet Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  214. $this->setAPIResponse('error', $e->getMessage(), 500);
  215. return false;
  216. }
  217. }
  218. }