sabnzbd.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. trait SabNZBdHomepageItem
  3. {
  4. public function sabNZBdSettingsArray()
  5. {
  6. return array(
  7. 'name' => 'SabNZBD',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/sabnzbd.png',
  10. 'category' => 'Downloader',
  11. 'settings' => array(
  12. 'Enable' => array(
  13. array(
  14. 'type' => 'switch',
  15. 'name' => 'homepageSabnzbdEnabled',
  16. 'label' => 'Enable',
  17. 'value' => $this->config['homepageSabnzbdEnabled']
  18. ),
  19. array(
  20. 'type' => 'select',
  21. 'name' => 'homepageSabnzbdAuth',
  22. 'label' => 'Minimum Authentication',
  23. 'value' => $this->config['homepageSabnzbdAuth'],
  24. 'options' => $this->groupOptions
  25. )
  26. ),
  27. 'Connection' => array(
  28. array(
  29. 'type' => 'input',
  30. 'name' => 'sabnzbdURL',
  31. 'label' => 'URL',
  32. 'value' => $this->config['sabnzbdURL'],
  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' => 'password-alt',
  38. 'name' => 'sabnzbdToken',
  39. 'label' => 'Token',
  40. 'value' => $this->config['sabnzbdToken']
  41. )
  42. ),
  43. 'Misc Options' => array(
  44. array(
  45. 'type' => 'select',
  46. 'name' => 'homepageDownloadRefresh',
  47. 'label' => 'Refresh Seconds',
  48. 'value' => $this->config['homepageDownloadRefresh'],
  49. 'options' => $this->timeOptions()
  50. ),
  51. array(
  52. 'type' => 'switch',
  53. 'name' => 'sabnzbdCombine',
  54. 'label' => 'Add to Combined Downloader',
  55. 'value' => $this->config['sabnzbdCombine']
  56. ),
  57. ),
  58. 'Test Connection' => array(
  59. array(
  60. 'type' => 'blank',
  61. 'label' => 'Please Save before Testing'
  62. ),
  63. array(
  64. 'type' => 'button',
  65. 'label' => '',
  66. 'icon' => 'fa fa-flask',
  67. 'class' => 'pull-right',
  68. 'text' => 'Test Connection',
  69. 'attr' => 'onclick="testAPIConnection(\'sabnzbd\')"'
  70. ),
  71. )
  72. )
  73. );
  74. }
  75. public function testConnectionSabNZBd()
  76. {
  77. if (!empty($this->config['sabnzbdURL']) && !empty($this->config['sabnzbdToken'])) {
  78. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  79. $url = $url . '/api?mode=queue&output=json&apikey=' . $this->config['sabnzbdToken'];
  80. try {
  81. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  82. $response = Requests::get($url, array(), $options);
  83. if ($response->success) {
  84. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  85. return true;
  86. }
  87. } catch (Requests_Exception $e) {
  88. $this->setAPIResponse('error', $e->getMessage(), 500);
  89. return false;
  90. };
  91. } else {
  92. $this->setAPIResponse('error', 'URL and/or Token not setup', 422);
  93. return 'URL and/or Token not setup';
  94. }
  95. }
  96. public function sabNZBdHomepagePermissions($key = null)
  97. {
  98. $permissions = [
  99. 'main' => [
  100. 'enabled' => [
  101. 'homepageSabnzbdEnabled'
  102. ],
  103. 'auth' => [
  104. 'homepageSabnzbdAuth'
  105. ],
  106. 'not_empty' => [
  107. 'sabnzbdURL',
  108. 'sabnzbdToken'
  109. ]
  110. ]
  111. ];
  112. if (array_key_exists($key, $permissions)) {
  113. return $permissions[$key];
  114. } elseif ($key == 'all') {
  115. return $permissions;
  116. } else {
  117. return [];
  118. }
  119. }
  120. public function homepageOrdersabnzbd()
  121. {
  122. if ($this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'))) {
  123. $loadingBox = ($this->config['sabnzbdCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  124. $builder = ($this->config['sabnzbdCombine']) ? 'buildDownloaderCombined(\'sabnzbd\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("sabnzbd"));';
  125. return '
  126. <div id="' . __FUNCTION__ . '">
  127. ' . $loadingBox . '
  128. <script>
  129. // homepageOrdersabnzbd
  130. ' . $builder . '
  131. homepageDownloader("sabnzbd", "' . $this->config['homepageDownloadRefresh'] . '");
  132. // End homepageOrdersabnzbd
  133. </script>
  134. </div>
  135. ';
  136. }
  137. }
  138. public function getSabNZBdHomepageQueue()
  139. {
  140. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  141. return false;
  142. }
  143. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  144. $url = $url . '/api?mode=queue&output=json&apikey=' . $this->config['sabnzbdToken'];
  145. try {
  146. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  147. $response = Requests::get($url, array(), $options);
  148. if ($response->success) {
  149. $api['content']['queueItems'] = json_decode($response->body, true);
  150. }
  151. } catch (Requests_Exception $e) {
  152. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  153. $this->setAPIResponse('error', $e->getMessage(), 500);
  154. return false;
  155. };
  156. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  157. $url = $url . '/api?mode=history&output=json&limit=100&apikey=' . $this->config['sabnzbdToken'];
  158. try {
  159. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  160. $response = Requests::get($url, array(), $options);
  161. if ($response->success) {
  162. $api['content']['historyItems'] = json_decode($response->body, true);
  163. }
  164. } catch (Requests_Exception $e) {
  165. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  166. $this->setAPIResponse('error', $e->getMessage(), 500);
  167. return false;
  168. };
  169. $api['content'] = isset($api['content']) ? $api['content'] : false;
  170. $this->setAPIResponse('success', null, 200, $api);
  171. return $api;
  172. }
  173. public function pauseSabNZBdQueue($target = null)
  174. {
  175. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  176. return false;
  177. }
  178. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  179. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=pause&value=' . $target . '&' : 'mode=pause';
  180. $url = $url . '/api?' . $id . '&output=json&apikey=' . $this->config['sabnzbdToken'];
  181. try {
  182. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  183. $response = Requests::get($url, array(), $options);
  184. if ($response->success) {
  185. $api['content'] = json_decode($response->body, true);
  186. }
  187. } catch (Requests_Exception $e) {
  188. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  189. $this->setAPIResponse('error', $e->getMessage(), 500);
  190. return false;
  191. };
  192. $api['content'] = isset($api['content']) ? $api['content'] : false;
  193. $this->setAPIResponse('success', null, 200, $api);
  194. return $api;
  195. }
  196. public function resumeSabNZBdQueue($target = null)
  197. {
  198. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  199. return false;
  200. }
  201. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  202. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=resume&value=' . $target . '&' : 'mode=resume';
  203. $url = $url . '/api?' . $id . '&output=json&apikey=' . $this->config['sabnzbdToken'];
  204. try {
  205. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  206. $response = Requests::get($url, array(), $options);
  207. if ($response->success) {
  208. $api['content'] = json_decode($response->body, true);
  209. }
  210. } catch (Requests_Exception $e) {
  211. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  212. $this->setAPIResponse('error', $e->getMessage(), 500);
  213. return false;
  214. };
  215. $api['content'] = isset($api['content']) ? $api['content'] : false;
  216. $this->setAPIResponse('success', null, 200, $api);
  217. return $api;
  218. }
  219. }