nzbget.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'Enable' => [
  20. $this->settingsOption('enable', 'homepageNzbgetEnabled'),
  21. $this->settingsOption('auth', 'homepageNzbgetAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('url', 'nzbgetURL'),
  25. $this->settingsOption('blank'),
  26. $this->settingsOption('username', 'nzbgetUsername'),
  27. $this->settingsOption('password', 'nzbgetPassword'),
  28. $this->settingsOption('disable-cert-check', 'nzbgetDisableCertCheck'),
  29. $this->settingsOption('use-custom-certificate', 'nzbgetUseCustomCertificate'),
  30. ],
  31. 'API SOCKS' => [
  32. $this->settingsOption('socks', 'nzbget'),
  33. $this->settingsOption('blank'),
  34. $this->settingsOption('enable', 'nzbgetSocksEnabled'),
  35. $this->settingsOption('auth', 'nzbgetSocksAuth'),
  36. ],
  37. 'Misc Options' => [
  38. $this->settingsOption('refresh', 'nzbgetRefresh'),
  39. $this->settingsOption('combine', 'nzbgetCombine'),
  40. ],
  41. 'Test Connection' => [
  42. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  43. $this->settingsOption('test', 'nzbget'),
  44. ]
  45. ]
  46. ];
  47. return array_merge($homepageInformation, $homepageSettings);
  48. }
  49. public function testConnectionNZBGet()
  50. {
  51. if (empty($this->config['nzbgetURL'])) {
  52. $this->setAPIResponse('error', 'NZBGet URL is not defined', 422);
  53. return false;
  54. }
  55. try {
  56. $url = $this->qualifyURL($this->config['nzbgetURL']);
  57. $options = $this->requestOptions($url, null, $this->config['nzbgetDisableCertCheck'], $this->config['nzbgetUseCustomCertificate']);
  58. $urlGroups = $url . '/jsonrpc/listgroups';
  59. if ($this->config['nzbgetUsername'] !== '' && $this->decrypt($this->config['nzbgetPassword']) !== '') {
  60. $credentials = array('auth' => new Requests_Auth_Basic(array($this->config['nzbgetUsername'], $this->decrypt($this->config['nzbgetPassword']))));
  61. $options = array_merge($options, $credentials);
  62. }
  63. $response = Requests::get($urlGroups, array(), $options);
  64. if ($response->success) {
  65. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  66. return true;
  67. } else {
  68. $this->setAPIResponse('success', 'NZBGet: An Error Occurred', 500);
  69. return false;
  70. }
  71. } catch (Requests_Exception $e) {
  72. $this->setLoggerChannel('NZBGet')->error($e);
  73. $this->setResponse(500, $e->getMessage());
  74. return false;
  75. }
  76. }
  77. public function nzbgetHomepagePermissions($key = null)
  78. {
  79. $permissions = [
  80. 'main' => [
  81. 'enabled' => [
  82. 'homepageNzbgetEnabled'
  83. ],
  84. 'auth' => [
  85. 'homepageNzbgetAuth'
  86. ],
  87. 'not_empty' => [
  88. 'nzbgetURL'
  89. ]
  90. ]
  91. ];
  92. return $this->homepageCheckKeyPermissions($key, $permissions);
  93. }
  94. public function homepageOrdernzbget()
  95. {
  96. if ($this->homepageItemPermissions($this->nzbgetHomepagePermissions('main'))) {
  97. $loadingBox = ($this->config['nzbgetCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  98. $builder = ($this->config['nzbgetCombine']) ? 'buildDownloaderCombined(\'nzbget\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("nzbget"));';
  99. return '
  100. <div id="' . __FUNCTION__ . '">
  101. ' . $loadingBox . '
  102. <script>
  103. // homepageOrdernzbget
  104. ' . $builder . '
  105. homepageDownloader("nzbget", "' . $this->config['nzbgetRefresh'] . '");
  106. // End homepageOrdernzbget
  107. </script>
  108. </div>
  109. ';
  110. }
  111. }
  112. public function getNzbgetHomepageQueue()
  113. {
  114. if (!$this->homepageItemPermissions($this->nzbgetHomepagePermissions('main'), true)) {
  115. return false;
  116. }
  117. try {
  118. $url = $this->qualifyURL($this->config['nzbgetURL']);
  119. $options = $this->requestOptions($url, $this->config['nzbgetRefresh'], $this->config['nzbgetDisableCertCheck'], $this->config['nzbgetUseCustomCertificate']);
  120. $urlGroups = $url . '/jsonrpc/listgroups';
  121. $urlHistory = $url . '/jsonrpc/history';
  122. if ($this->config['nzbgetUsername'] !== '' && $this->decrypt($this->config['nzbgetPassword']) !== '') {
  123. $credentials = array('auth' => new Requests_Auth_Basic(array($this->config['nzbgetUsername'], $this->decrypt($this->config['nzbgetPassword']))));
  124. $options = array_merge($options, $credentials);
  125. }
  126. $response = Requests::get($urlGroups, array(), $options);
  127. if ($response->success) {
  128. $api['content']['queueItems'] = json_decode($response->body, true);
  129. }
  130. $response = Requests::get($urlHistory, array(), $options);
  131. if ($response->success) {
  132. $api['content']['historyItems'] = json_decode($response->body, true);
  133. }
  134. $api['content'] = isset($api['content']) ? $api['content'] : false;
  135. $this->setAPIResponse('success', null, 200, $api);
  136. return $api;
  137. } catch (Requests_Exception $e) {
  138. $this->setLoggerChannel('NZBGet')->error($e);
  139. $this->setResponse(500, $e->getMessage());
  140. return false;
  141. }
  142. }
  143. }