sabnzbd.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 = array(
  17. 'settings' => array(
  18. 'Enable' => array(
  19. array(
  20. 'type' => 'switch',
  21. 'name' => 'homepageSabnzbdEnabled',
  22. 'label' => 'Enable',
  23. 'value' => $this->config['homepageSabnzbdEnabled']
  24. ),
  25. array(
  26. 'type' => 'select',
  27. 'name' => 'homepageSabnzbdAuth',
  28. 'label' => 'Minimum Authentication',
  29. 'value' => $this->config['homepageSabnzbdAuth'],
  30. 'options' => $this->groupOptions
  31. )
  32. ),
  33. 'Connection' => array(
  34. array(
  35. 'type' => 'input',
  36. 'name' => 'sabnzbdURL',
  37. 'label' => 'URL',
  38. 'value' => $this->config['sabnzbdURL'],
  39. 'help' => 'Please make sure to use local IP address and port - You also may use local dns name too.',
  40. 'placeholder' => 'http(s)://hostname:port'
  41. ),
  42. array(
  43. 'type' => 'password-alt',
  44. 'name' => 'sabnzbdToken',
  45. 'label' => 'Token',
  46. 'value' => $this->config['sabnzbdToken']
  47. )
  48. ),
  49. 'API SOCKS' => array(
  50. array(
  51. 'type' => 'html',
  52. 'override' => 12,
  53. 'label' => '',
  54. 'html' => '
  55. <div class="panel panel-default">
  56. <div class="panel-wrapper collapse in">
  57. <div class="panel-body">' . $this->socksHeadingHTML('sabnzbd') . '</div>
  58. </div>
  59. </div>'
  60. ),
  61. array(
  62. 'type' => 'switch',
  63. 'name' => 'sabnzbdSocksEnabled',
  64. 'label' => 'Enable',
  65. 'value' => $this->config['sabnzbdSocksEnabled']
  66. ),
  67. array(
  68. 'type' => 'select',
  69. 'name' => 'sabnzbdSocksAuth',
  70. 'label' => 'Minimum Authentication',
  71. 'value' => $this->config['sabnzbdSocksAuth'],
  72. 'options' => $this->groupOptions
  73. ),
  74. ),
  75. 'Misc Options' => array(
  76. array(
  77. 'type' => 'select',
  78. 'name' => 'sabnzbdRefresh',
  79. 'label' => 'Refresh Seconds',
  80. 'value' => $this->config['sabnzbdRefresh'],
  81. 'options' => $this->timeOptions()
  82. ),
  83. array(
  84. 'type' => 'switch',
  85. 'name' => 'sabnzbdCombine',
  86. 'label' => 'Add to Combined Downloader',
  87. 'value' => $this->config['sabnzbdCombine']
  88. ),
  89. ),
  90. 'Test Connection' => array(
  91. array(
  92. 'type' => 'blank',
  93. 'label' => 'Please Save before Testing'
  94. ),
  95. array(
  96. 'type' => 'button',
  97. 'label' => '',
  98. 'icon' => 'fa fa-flask',
  99. 'class' => 'pull-right',
  100. 'text' => 'Test Connection',
  101. 'attr' => 'onclick="testAPIConnection(\'sabnzbd\')"'
  102. ),
  103. )
  104. )
  105. );
  106. return array_merge($homepageInformation, $homepageSettings);
  107. }
  108. public function testConnectionSabNZBd()
  109. {
  110. if (!empty($this->config['sabnzbdURL']) && !empty($this->config['sabnzbdToken'])) {
  111. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  112. $url = $url . '/api?mode=queue&output=json&apikey=' . $this->config['sabnzbdToken'];
  113. try {
  114. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  115. $response = Requests::get($url, array(), $options);
  116. if ($response->success) {
  117. $data = json_decode($response->body, true);
  118. $status = 'success';
  119. $responseCode = 200;
  120. $message = 'API Connection succeeded';
  121. if (isset($data['error'])) {
  122. $status = 'error';
  123. $responseCode = 500;
  124. $message = $data['error'];
  125. }
  126. $this->setAPIResponse($status, $message, $responseCode, $data);
  127. return true;
  128. } else {
  129. $this->setAPIResponse('error', $response->body, 500);
  130. return false;
  131. }
  132. } catch (Requests_Exception $e) {
  133. $this->setAPIResponse('error', $e->getMessage(), 500);
  134. return false;
  135. };
  136. } else {
  137. $this->setAPIResponse('error', 'URL and/or Token not setup', 422);
  138. return 'URL and/or Token not setup';
  139. }
  140. }
  141. public function sabNZBdHomepagePermissions($key = null)
  142. {
  143. $permissions = [
  144. 'main' => [
  145. 'enabled' => [
  146. 'homepageSabnzbdEnabled'
  147. ],
  148. 'auth' => [
  149. 'homepageSabnzbdAuth'
  150. ],
  151. 'not_empty' => [
  152. 'sabnzbdURL',
  153. 'sabnzbdToken'
  154. ]
  155. ]
  156. ];
  157. if (array_key_exists($key, $permissions)) {
  158. return $permissions[$key];
  159. } elseif ($key == 'all') {
  160. return $permissions;
  161. } else {
  162. return [];
  163. }
  164. }
  165. public function homepageOrdersabnzbd()
  166. {
  167. if ($this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'))) {
  168. $loadingBox = ($this->config['sabnzbdCombine']) ? '' : '<div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Download Queue...</h2></div>';
  169. $builder = ($this->config['sabnzbdCombine']) ? 'buildDownloaderCombined(\'sabnzbd\');' : '$("#' . __FUNCTION__ . '").html(buildDownloader("sabnzbd"));';
  170. return '
  171. <div id="' . __FUNCTION__ . '">
  172. ' . $loadingBox . '
  173. <script>
  174. // homepageOrdersabnzbd
  175. ' . $builder . '
  176. homepageDownloader("sabnzbd", "' . $this->config['sabnzbdRefresh'] . '");
  177. // End homepageOrdersabnzbd
  178. </script>
  179. </div>
  180. ';
  181. }
  182. }
  183. public function getSabNZBdHomepageQueue()
  184. {
  185. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  186. return false;
  187. }
  188. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  189. $url = $url . '/api?mode=queue&output=json&apikey=' . $this->config['sabnzbdToken'];
  190. try {
  191. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  192. $response = Requests::get($url, array(), $options);
  193. if ($response->success) {
  194. $api['content']['queueItems'] = json_decode($response->body, true);
  195. }
  196. } catch (Requests_Exception $e) {
  197. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  198. $this->setAPIResponse('error', $e->getMessage(), 500);
  199. return false;
  200. };
  201. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  202. $url = $url . '/api?mode=history&output=json&limit=100&apikey=' . $this->config['sabnzbdToken'];
  203. try {
  204. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  205. $response = Requests::get($url, array(), $options);
  206. if ($response->success) {
  207. $api['content']['historyItems'] = json_decode($response->body, true);
  208. }
  209. } catch (Requests_Exception $e) {
  210. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  211. $this->setAPIResponse('error', $e->getMessage(), 500);
  212. return false;
  213. };
  214. $api['content'] = isset($api['content']) ? $api['content'] : false;
  215. $this->setAPIResponse('success', null, 200, $api);
  216. return $api;
  217. }
  218. public function pauseSabNZBdQueue($target = null)
  219. {
  220. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  221. return false;
  222. }
  223. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  224. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=pause&value=' . $target . '&' : 'mode=pause';
  225. $url = $url . '/api?' . $id . '&output=json&apikey=' . $this->config['sabnzbdToken'];
  226. try {
  227. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  228. $response = Requests::get($url, array(), $options);
  229. if ($response->success) {
  230. $api['content'] = json_decode($response->body, true);
  231. }
  232. } catch (Requests_Exception $e) {
  233. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  234. $this->setAPIResponse('error', $e->getMessage(), 500);
  235. return false;
  236. };
  237. $api['content'] = isset($api['content']) ? $api['content'] : false;
  238. $this->setAPIResponse('success', null, 200, $api);
  239. return $api;
  240. }
  241. public function resumeSabNZBdQueue($target = null)
  242. {
  243. if (!$this->homepageItemPermissions($this->sabNZBdHomepagePermissions('main'), true)) {
  244. return false;
  245. }
  246. $url = $this->qualifyURL($this->config['sabnzbdURL']);
  247. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=resume&value=' . $target . '&' : 'mode=resume';
  248. $url = $url . '/api?' . $id . '&output=json&apikey=' . $this->config['sabnzbdToken'];
  249. try {
  250. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  251. $response = Requests::get($url, array(), $options);
  252. if ($response->success) {
  253. $api['content'] = json_decode($response->body, true);
  254. }
  255. } catch (Requests_Exception $e) {
  256. $this->writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  257. $this->setAPIResponse('error', $e->getMessage(), 500);
  258. return false;
  259. };
  260. $api['content'] = isset($api['content']) ? $api['content'] : false;
  261. $this->setAPIResponse('success', null, 200, $api);
  262. return $api;
  263. }
  264. }