sabnzbd.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. if (array_key_exists($key, $permissions)) {
  97. return $permissions[$key];
  98. } elseif ($key == 'all') {
  99. return $permissions;
  100. } else {
  101. return [];
  102. }
  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. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  125. return false;
  126. }
  127. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  128. $url = $url . '/api?mode=queue&output=json&apikey=' . $this->config['sabnzbdToken'];
  129. try {
  130. $options = $this->requestOptions($url, $this->config['sabnzbdRefresh'], $this->config['sabnzbdDisableCertCheck'], $this->config['sabnzbdUseCustomCertificate']);
  131. $response = Requests::get($url, [], $options);
  132. if ($response->success) {
  133. $api['content']['queueItems'] = json_decode($response->body, true);
  134. }
  135. } catch (Requests_Exception $e) {
  136. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  137. $this->setAPIResponse('error', $e->getMessage(), 500);
  138. return false;
  139. };
  140. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  141. $url = $url . '/api?mode=history&output=json&limit=100&apikey=' . $this->config['sabnzbdToken'];
  142. try {
  143. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  144. $response = Requests::get($url, array(), $options);
  145. if ($response->success) {
  146. $api['content']['historyItems'] = json_decode($response->body, true);
  147. }
  148. } catch (Requests_Exception $e) {
  149. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  150. $this->setAPIResponse('error', $e->getMessage(), 500);
  151. return false;
  152. };
  153. $api['content'] = isset($api['content']) ? $api['content'] : false;
  154. $this->setAPIResponse('success', null, 200, $api);
  155. return $api;
  156. }
  157. public function pauseSabNZBdQueue($target = null)
  158. {
  159. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  160. return false;
  161. }
  162. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  163. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=pause&value=' . $target . '&' : 'mode=pause';
  164. $url = $url . '/api?' . $id . '&output=json&apikey=' . $this->config['sabnzbdToken'];
  165. try {
  166. $options = $this->requestOptions($url, $this->config['sabnzbdRefresh'], $this->config['sabnzbdDisableCertCheck'], $this->config['sabnzbdUseCustomCertificate']);
  167. $response = Requests::get($url, [], $options);
  168. if ($response->success) {
  169. $api['content'] = json_decode($response->body, true);
  170. }
  171. } catch (Requests_Exception $e) {
  172. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  173. $this->setAPIResponse('error', $e->getMessage(), 500);
  174. return false;
  175. };
  176. $api['content'] = isset($api['content']) ? $api['content'] : false;
  177. $this->setAPIResponse('success', null, 200, $api);
  178. return $api;
  179. }
  180. public function resumeSabNZBdQueue($target = null)
  181. {
  182. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  183. return false;
  184. }
  185. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  186. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=resume&value=' . $target . '&' : 'mode=resume';
  187. $url = $url . '/api?' . $id . '&output=json&apikey=' . $this->config['sabnzbdToken'];
  188. try {
  189. $options = $this->requestOptions($url, $this->config['sabnzbdRefresh'], $this->config['sabnzbdDisableCertCheck'], $this->config['sabnzbdUseCustomCertificate']);
  190. $response = Requests::get($url, [], $options);
  191. if ($response->success) {
  192. $api['content'] = json_decode($response->body, true);
  193. }
  194. } catch (Requests_Exception $e) {
  195. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  196. $this->setAPIResponse('error', $e->getMessage(), 500);
  197. return false;
  198. };
  199. $api['content'] = isset($api['content']) ? $api['content'] : false;
  200. $this->setAPIResponse('success', null, 200, $api);
  201. return $api;
  202. }
  203. }