nzbget.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 getNzbgetHomepageQueue()
  110. {
  111. if (!$this->config['homepageNzbgetEnabled']) {
  112. $this->setAPIResponse('error', 'NZBGet homepage item is not enabled', 409);
  113. return false;
  114. }
  115. if (!$this->qualifyRequest($this->config['homepageNzbgetAuth'])) {
  116. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  117. return false;
  118. }
  119. if (empty($this->config['nzbgetURL'])) {
  120. $this->setAPIResponse('error', 'NZBGet URL is not defined', 422);
  121. return false;
  122. }
  123. try {
  124. $url = $this->qualifyURL($this->config['nzbgetURL']);
  125. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  126. $urlGroups = $url . '/jsonrpc/listgroups';
  127. $urlHistory = $url . '/jsonrpc/history';
  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. $api['content']['queueItems'] = json_decode($response->body, true);
  135. }
  136. $response = Requests::get($urlHistory, array(), $options);
  137. if ($response->success) {
  138. $api['content']['historyItems'] = json_decode($response->body, true);
  139. }
  140. $api['content'] = isset($api['content']) ? $api['content'] : false;
  141. $this->setAPIResponse('success', null, 200, $api);
  142. return $api;
  143. } catch (Requests_Exception $e) {
  144. $this->writeLog('error', 'NZBGet Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  145. $this->setAPIResponse('error', $e->getMessage(), 500);
  146. return false;
  147. }
  148. }
  149. }