sabnzbd.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. trait SabNZBdHomepageItem
  3. {
  4. public function sabNZBdSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'SabNZBD',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/sabnzbd.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', 'homepageSabnzbdEnabled'),
  21. $this->settingsOption('auth', 'homepageSabnzbdAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('url', 'sabnzbdURL'),
  25. $this->settingsOption('token', 'sabnzbdToken'),
  26. $this->settingsOption('disable-cert-check', 'sabnzbdDisableCertCheck'),
  27. $this->settingsOption('use-custom-certificate', 'sabnzbdUseCustomCertificate'),
  28. ],
  29. 'API SOCKS' => [
  30. $this->settingsOption('socks', 'sabnzbd'),
  31. $this->settingsOption('blank'),
  32. $this->settingsOption('enable', 'sabnzbdSocksEnabled'),
  33. $this->settingsOption('auth', 'sabnzbdSocksAuth'),
  34. ],
  35. 'Misc Options' => [
  36. $this->settingsOption('refresh', 'sabnzbdRefresh'),
  37. $this->settingsOption('combine', 'sabnzbdCombine'),
  38. ],
  39. 'Test Connection' => [
  40. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  41. $this->settingsOption('test', 'sabnzbd'),
  42. ]
  43. ]
  44. ];
  45. return array_merge($homepageInformation, $homepageSettings);
  46. }
  47. public function testConnectionSabNZBd()
  48. {
  49. $this->setLoggerChannel('Sabnzbd Homepage');
  50. $this->logger->debug('Starting API Connection Test');
  51. if (!empty($this->config['sabnzbdURL']) && !empty($this->config['sabnzbdToken'])) {
  52. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  53. $url = $url . '/api?mode=queue&output=json&apikey=' . $this->config['sabnzbdToken'];
  54. try {
  55. $options = $this->requestOptions($url, null, $this->config['sabnzbdDisableCertCheck'], $this->config['sabnzbdUseCustomCertificate']);
  56. $response = Requests::get($url, [], $options);
  57. if ($response->success) {
  58. $data = json_decode($response->body, true);
  59. $status = 'success';
  60. $responseCode = 200;
  61. $message = 'API Connection succeeded';
  62. if (isset($data['error'])) {
  63. $status = 'error';
  64. $responseCode = 500;
  65. $message = $data['error'];
  66. }
  67. $this->setAPIResponse($status, $message, $responseCode, $data);
  68. $this->logger->debug('API Connection Test was successful');
  69. return true;
  70. } else {
  71. $this->setAPIResponse('error', $response->body, 500);
  72. $this->logger->debug('API Connection Test was unsuccessful');
  73. return false;
  74. }
  75. } catch (Requests_Exception $e) {
  76. $this->logger->critical($e, [$url]);
  77. $this->setResponse(500, $e->getMessage());
  78. return false;
  79. }
  80. } else {
  81. $this->logger->debug('URL and/or Token not setup');
  82. $this->setAPIResponse('error', 'URL and/or Token not setup', 422);
  83. return 'URL and/or Token not setup';
  84. }
  85. }
  86. public function sabNZBdHomepagePermissions($key = null)
  87. {
  88. $permissions = [
  89. 'main' => [
  90. 'enabled' => [
  91. 'homepageSabnzbdEnabled'
  92. ],
  93. 'auth' => [
  94. 'homepageSabnzbdAuth'
  95. ],
  96. 'not_empty' => [
  97. 'sabnzbdURL',
  98. 'sabnzbdToken'
  99. ]
  100. ]
  101. ];
  102. return $this->homepageCheckKeyPermissions($key, $permissions);
  103. }
  104. public function homepageOrdersabnzbd()
  105. {
  106. if ($this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'))) {
  107. $loadingBox = ($this->config['sabnzbdCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  108. $builder = ($this->config['sabnzbdCombine']) ? 'buildDownloaderCombined(\'sabnzbd\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("sabnzbd"));';
  109. return '
  110. <div id="' . __FUNCTION__ . '">
  111. ' . $loadingBox . '
  112. <script>
  113. // homepageOrdersabnzbd
  114. ' . $builder . '
  115. homepageDownloader("sabnzbd", "' . $this->config['sabnzbdRefresh'] . '");
  116. // End homepageOrdersabnzbd
  117. </script>
  118. </div>
  119. ';
  120. }
  121. }
  122. public function getSabNZBdHomepageQueue()
  123. {
  124. $this->setLoggerChannel('Sabnzbd Homepage');
  125. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  126. return false;
  127. }
  128. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  129. $url = $url . '/api?mode=queue&output=json&apikey=' . $this->config['sabnzbdToken'];
  130. try {
  131. $options = $this->requestOptions($url, $this->config['sabnzbdRefresh'], $this->config['sabnzbdDisableCertCheck'], $this->config['sabnzbdUseCustomCertificate']);
  132. $response = Requests::get($url, [], $options);
  133. if ($response->success) {
  134. $api['content']['queueItems'] = json_decode($response->body, true);
  135. }
  136. } catch (Requests_Exception $e) {
  137. $this->logger->critical($e, [$url]);
  138. $this->setResponse(500, $e->getMessage());
  139. return false;
  140. };
  141. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  142. $url = $url . '/api?mode=history&output=json&limit=100&apikey=' . $this->config['sabnzbdToken'];
  143. try {
  144. $options = $this->requestOptions($url, $this->config['sabnzbdRefresh'], $this->config['sabnzbdDisableCertCheck'], $this->config['sabnzbdUseCustomCertificate']);
  145. $response = Requests::get($url, array(), $options);
  146. if ($response->success) {
  147. $api['content']['historyItems'] = json_decode($response->body, true);
  148. }
  149. } catch (Requests_Exception $e) {
  150. $this->logger->critical($e, [$url]);
  151. $this->setResponse(500, $e->getMessage());
  152. return false;
  153. };
  154. $api['content'] = isset($api['content']) ? $api['content'] : false;
  155. $this->setAPIResponse('success', null, 200, $api);
  156. return $api;
  157. }
  158. public function pauseSabNZBdQueue($target = null)
  159. {
  160. $this->setLoggerChannel('Sabnzbd Homepage');
  161. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  162. return false;
  163. }
  164. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  165. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=pause&value=' . $target . '&' : 'mode=pause';
  166. $url = $url . '/api?' . $id . '&output=json&apikey=' . $this->config['sabnzbdToken'];
  167. try {
  168. $options = $this->requestOptions($url, $this->config['sabnzbdRefresh'], $this->config['sabnzbdDisableCertCheck'], $this->config['sabnzbdUseCustomCertificate']);
  169. $response = Requests::get($url, [], $options);
  170. if ($response->success) {
  171. $api['content'] = json_decode($response->body, true);
  172. }
  173. } catch (Requests_Exception $e) {
  174. $this->logger->critical($e, [$url]);
  175. $this->setResponse(500, $e->getMessage());
  176. return false;
  177. };
  178. $api['content'] = isset($api['content']) ? $api['content'] : false;
  179. $this->setAPIResponse('success', null, 200, $api);
  180. return $api;
  181. }
  182. public function resumeSabNZBdQueue($target = null)
  183. {
  184. $this->setLoggerChannel('Sabnzbd Homepage');
  185. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  186. return false;
  187. }
  188. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  189. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=resume&value=' . $target . '&' : 'mode=resume';
  190. $url = $url . '/api?' . $id . '&output=json&apikey=' . $this->config['sabnzbdToken'];
  191. try {
  192. $options = $this->requestOptions($url, $this->config['sabnzbdRefresh'], $this->config['sabnzbdDisableCertCheck'], $this->config['sabnzbdUseCustomCertificate']);
  193. $response = Requests::get($url, [], $options);
  194. if ($response->success) {
  195. $api['content'] = json_decode($response->body, true);
  196. }
  197. } catch (Requests_Exception $e) {
  198. $this->logger->critical($e, [$url]);
  199. $this->setResponse(500, $e->getMessage());
  200. return false;
  201. };
  202. $api['content'] = isset($api['content']) ? $api['content'] : false;
  203. $this->setAPIResponse('success', null, 200, $api);
  204. return $api;
  205. }
  206. }