sabnzbd.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. if (!empty($this->config['sabnzbdURL']) && !empty($this->config['sabnzbdToken'])) {
  50. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  51. $url = $url . '/api?mode=queue&output=json&apikey=' . $this->config['sabnzbdToken'];
  52. try {
  53. $options = $this->requestOptions($url, null, $this->config['sabnzbdDisableCertCheck'], $this->config['sabnzbdUseCustomCertificate']);
  54. $response = Requests::get($url, [], $options);
  55. if ($response->success) {
  56. $data = json_decode($response->body, true);
  57. $status = 'success';
  58. $responseCode = 200;
  59. $message = 'API Connection succeeded';
  60. if (isset($data['error'])) {
  61. $status = 'error';
  62. $responseCode = 500;
  63. $message = $data['error'];
  64. }
  65. $this->setAPIResponse($status, $message, $responseCode, $data);
  66. return true;
  67. } else {
  68. $this->setAPIResponse('error', $response->body, 500);
  69. return false;
  70. }
  71. } catch (Requests_Exception $e) {
  72. $this->setAPIResponse('error', $e->getMessage(), 500);
  73. return false;
  74. };
  75. } else {
  76. $this->setAPIResponse('error', 'URL and/or Token not setup', 422);
  77. return 'URL and/or Token not setup';
  78. }
  79. }
  80. public function sabNZBdHomepagePermissions($key = null)
  81. {
  82. $permissions = [
  83. 'main' => [
  84. 'enabled' => [
  85. 'homepageSabnzbdEnabled'
  86. ],
  87. 'auth' => [
  88. 'homepageSabnzbdAuth'
  89. ],
  90. 'not_empty' => [
  91. 'sabnzbdURL',
  92. 'sabnzbdToken'
  93. ]
  94. ]
  95. ];
  96. return $this->homepageCheckKeyPermissions($key, $permissions);
  97. }
  98. public function homepageOrdersabnzbd()
  99. {
  100. if ($this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'))) {
  101. $loadingBox = ($this->config['sabnzbdCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  102. $builder = ($this->config['sabnzbdCombine']) ? 'buildDownloaderCombined(\'sabnzbd\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("sabnzbd"));';
  103. return '
  104. <div id="' . __FUNCTION__ . '">
  105. ' . $loadingBox . '
  106. <script>
  107. // homepageOrdersabnzbd
  108. ' . $builder . '
  109. homepageDownloader("sabnzbd", "' . $this->config['sabnzbdRefresh'] . '");
  110. // End homepageOrdersabnzbd
  111. </script>
  112. </div>
  113. ';
  114. }
  115. }
  116. public function getSabNZBdHomepageQueue()
  117. {
  118. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  119. return false;
  120. }
  121. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  122. $url = $url . '/api?mode=queue&output=json&apikey=' . $this->config['sabnzbdToken'];
  123. try {
  124. $options = $this->requestOptions($url, $this->config['sabnzbdRefresh'], $this->config['sabnzbdDisableCertCheck'], $this->config['sabnzbdUseCustomCertificate']);
  125. $response = Requests::get($url, [], $options);
  126. if ($response->success) {
  127. $api['content']['queueItems'] = json_decode($response->body, true);
  128. }
  129. } catch (Requests_Exception $e) {
  130. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  131. $this->setAPIResponse('error', $e->getMessage(), 500);
  132. return false;
  133. };
  134. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  135. $url = $url . '/api?mode=history&output=json&limit=100&apikey=' . $this->config['sabnzbdToken'];
  136. try {
  137. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  138. $response = Requests::get($url, array(), $options);
  139. if ($response->success) {
  140. $api['content']['historyItems'] = json_decode($response->body, true);
  141. }
  142. } catch (Requests_Exception $e) {
  143. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  144. $this->setAPIResponse('error', $e->getMessage(), 500);
  145. return false;
  146. };
  147. $api['content'] = isset($api['content']) ? $api['content'] : false;
  148. $this->setAPIResponse('success', null, 200, $api);
  149. return $api;
  150. }
  151. public function pauseSabNZBdQueue($target = null)
  152. {
  153. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  154. return false;
  155. }
  156. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  157. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=pause&value=' . $target . '&' : 'mode=pause';
  158. $url = $url . '/api?' . $id . '&output=json&apikey=' . $this->config['sabnzbdToken'];
  159. try {
  160. $options = $this->requestOptions($url, $this->config['sabnzbdRefresh'], $this->config['sabnzbdDisableCertCheck'], $this->config['sabnzbdUseCustomCertificate']);
  161. $response = Requests::get($url, [], $options);
  162. if ($response->success) {
  163. $api['content'] = json_decode($response->body, true);
  164. }
  165. } catch (Requests_Exception $e) {
  166. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  167. $this->setAPIResponse('error', $e->getMessage(), 500);
  168. return false;
  169. };
  170. $api['content'] = isset($api['content']) ? $api['content'] : false;
  171. $this->setAPIResponse('success', null, 200, $api);
  172. return $api;
  173. }
  174. public function resumeSabNZBdQueue($target = null)
  175. {
  176. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  177. return false;
  178. }
  179. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  180. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=resume&value=' . $target . '&' : 'mode=resume';
  181. $url = $url . '/api?' . $id . '&output=json&apikey=' . $this->config['sabnzbdToken'];
  182. try {
  183. $options = $this->requestOptions($url, $this->config['sabnzbdRefresh'], $this->config['sabnzbdDisableCertCheck'], $this->config['sabnzbdUseCustomCertificate']);
  184. $response = Requests::get($url, [], $options);
  185. if ($response->success) {
  186. $api['content'] = json_decode($response->body, true);
  187. }
  188. } catch (Requests_Exception $e) {
  189. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  190. $this->setAPIResponse('error', $e->getMessage(), 500);
  191. return false;
  192. };
  193. $api['content'] = isset($api['content']) ? $api['content'] : false;
  194. $this->setAPIResponse('success', null, 200, $api);
  195. return $api;
  196. }
  197. }